@withgauge/cli 0.28.0 → 0.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{LOCAL_AGENTS,LOCAL_MCP_HINT,LOCAL_TARGETS,analysisNext,assertLocalCapabilities,commentSourceSchema,deploymentSourceSchema,detachLocal,freezeWebFixtures,inspectLocalCredential,inspectLocalGateway,inspectLocalRuntime,mergeWebFixtures,parseWebFixtureInput,planLocal,pullLocalGateway,pullLocalRuntime,readProfiles,readWebFixtureArchive,requireProfile,runLocal,setProfile,websiteSourceSchema,websiteUseSchema,writeProfiles}from"./chunk-TAFB66SK.js";import{setTelemetryEnabled,telemetryStatus}from"./chunk-WYYRMPRX.js";import{readJson}from"./chunk-Y4Y2AKDY.js";import{cancelLocal,cleanupLocal,localBatchStatus,localStatus,openLocalStore}from"./chunk-PUNARKS2.js";import"./chunk-QV7MOO3M.js";import{CHAT_BATCH_SIZE,ChatApiClient,OUTPUT_FORMATS,addPageOptions,categoryScope,chatPath,chatScope,chunks,createChatApiClient,csv,describeScopes,fetchPages,findWorkspace,gaugeState,header,hostedOnly,instructionMode,printItems,printJson,printPage,readGaugeState,requireHosted,resolveChatCategory,resolveOrg,resolveOutput,resolveTagIds,resolveTopicId,selectedOrg,style,truncate,withCategory}from"./chunk-PVTU7NGL.js";import{createdOrganization,credentialKind,fetchDirectory,findOrganization,isUuid,legacySelection,productsLabel,resolveOrgValue}from"./chunk-AZKXHO6C.js";import{ANALYSIS_SCHEMA_VERSION,ApiClient,CliError,DEFAULT_LIFECYCLE_TIMEOUT_SECONDS,MAX_LIFECYCLE_SOURCE_LENGTH,MAX_LIFECYCLE_TIMEOUT_SECONDS,agentConfigInputSchema,agentConfigSchema,agentSchema,analysisSubmissionSchema,assetRefListSchema,batteries_exports,brands_exports,clearToken,createClient,createdBySchema,credentialsPath,errorEnvelopeSchema,exitCodeFor,feedback_exports,keys_exports,lifecycleSchema,listResponseSchema,readCredentials,resolveAuthUrl,resolveBaseUrl,resolveToken,runConfig_exports,runCreditsChargedSchema,runEventSchema,runTriggeredBySchema,saveCredentials,saveToken,sharedTokenSchema,storedSharedCredentials,withCredentialLock}from"./chunk-PMX3NOGH.js";import{CLI_VERSION,CONFIG_KEYS,USER_AGENT,configPath,readConfig,updateConfig}from"./chunk-OIGVDK3F.js";import{__export}from"./chunk-PSBF4TEK.js";import{Command,CommanderError as CommanderError2,Option as Option6}from"commander";import{fileURLToPath as fileURLToPath2}from"url";var auth_exports={};__export(auth_exports,{browserLogin:()=>browserLogin,deviceLogin:()=>deviceLogin,logout:()=>logout,parseLoopbackCallback:()=>parseLoopbackCallback,readStdin:()=>readStdin,register:()=>register,useOnlyOrg:()=>useOnlyOrg});import{spawn}from"child_process";import{createHash,randomBytes,randomUUID}from"crypto";import{createServer}from"http";import{hostname}from"os";import{createInterface}from"readline";import{setTimeout as sleep}from"timers/promises";import{z}from"zod";var deviceAuthorizeRequestSchema=z.object({deviceName:z.string().trim().max(64).optional(),email:z.string().trim().email().max(320).optional()});var deviceAuthorizeResponseSchema=z.object({deviceCode:z.string(),userCode:z.string(),verificationUri:z.string(),verificationUriComplete:z.string(),interval:z.number().int().positive(),expiresIn:z.number().int().positive(),emailSent:z.boolean()});var deviceTokenRequestSchema=z.object({deviceCode:z.string().min(1)});var deviceTokenApprovedSchema=z.object({status:z.literal("approved"),accessToken:z.string(),tokenType:z.literal("bearer"),expiresAt:z.string().nullable()});var deviceTokenResultSchema=z.discriminatedUnion("status",[z.object({status:z.literal("pending")}),z.object({status:z.literal("slow_down"),interval:z.number().int()}),z.object({status:z.literal("denied")}),z.object({status:z.literal("expired")}),deviceTokenApprovedSchema]);async function credentialIdentity(){if(credentialKind()==="legacy"){const{user}=await createClient().get("/api/v1/me");return{user,apiKey:null,token:{kind:"legacy",scopes:null,expiresAt:null}}}return createChatApiClient().me()}async function principalIdentity(){const{user,apiKey,token}=await credentialIdentity();const orgs=token.kind==="legacy"?null:apiKey?.organization?[apiKey.organization]:await fetchDirectory();return{user,apiKey,orgs}}function printPrincipal(identity){const{user,apiKey,orgs}=identity;if(apiKey)console.log(`Org API key "${apiKey.name}" (${apiKey.prefix}\u2026)`);else if(user)console.log(`Signed in as ${user.email||user.id}${user.name?` (${user.name})`:""}`);if(orgs)console.log(`Organizations: ${orgs.map(org=>`${org.name} (${org.slug})`).join(", ")||"(none)"}`);else if(orgs===null)console.log("Use `gauge auth login` to list your Gauge organizations.")}async function fetchAllPages(client,path,query={}){const items=[];let cursor;do{const page=await client.get(path,{limit:200,...query,cursor});items.push(...page.items);cursor=page.nextCursor}while(cursor);return items}var LOGIN_TIMEOUT_MS=2*60*1e3;function tokenSource(){if(process.env.GAUGE_API_TOKEN?.trim())return"env";return resolveToken()?"file":"none"}async function fetchMe(client,shared=false){return shared?client.get("/api/cli/me"):client.get("/api/v1/me")}function sessionCredentials(raw){return{version:1,sessionId:randomUUID(),...sharedTokenSchema.parse(raw),authUrl:resolveAuthUrl(),apiUrl:resolveBaseUrl()}}async function verifyIssuedToken(token){const shared=token.startsWith("gauge_cli_");return fetchMe(new ApiClient({token,baseUrl:shared?resolveAuthUrl():resolveBaseUrl()}),shared)}function parseLoopbackCallback(rawUrl,expectedState){const url=new URL(rawUrl??"/","http://127.0.0.1");if(url.pathname!=="/callback")return null;if(url.searchParams.get("state")===expectedState&&url.searchParams.get("error")==="access_denied"){throw new CliError("unauthorized","Authorization was denied in the browser.")}const code=url.searchParams.get("code");if(url.searchParams.get("state")!==expectedState||!code)return null;return code}function printIdentity(me){const orgs=me.orgs.map(o=>o.slug).join(", ")||"(none)";console.log(`Logged in as ${me.user.email??me.user.id}`);console.log(`Orgs: ${orgs}`);console.log(`API URL: ${resolveBaseUrl()}`)}function useOnlyOrg(me,shared=false,log=console.log){const org=me.orgs.length===1?me.orgs[0]:void 0;const only=!org?void 0:shared&&"id"in org?org.id:"canonicalGaugeOrganizationId"in org?org.canonicalGaugeOrganizationId:void 0;if(!only||readConfig().orgId)return;updateConfig({orgId:only,defaultOrg:void 0});log(`Default organization set to ${org?.name??only}.`)}function openBrowser(url){const[cmd,args]=process.platform==="darwin"?["open",[url]]:process.platform==="win32"?["cmd",["/c","start","",url]]:["xdg-open",[url]];try{const child=spawn(cmd,args,{stdio:"ignore",detached:true});child.on("error",()=>{});child.unref()}catch{}}async function browserLogin(){const state=randomBytes(16).toString("base64url");const codeVerifier=randomBytes(32).toString("base64url");const codeChallenge=createHash("sha256").update(codeVerifier).digest("base64url");const server=createServer();const codePromise=new Promise((resolve5,reject)=>{server.on("request",(req,res)=>{const url=new URL(req.url??"/","http://127.0.0.1");if(url.pathname!=="/callback"){res.writeHead(404).end();return}if(req.method!=="GET"){res.writeHead(405,{allow:"GET"}).end();return}let code;try{code=parseLoopbackCallback(req.url,state)}catch(error){res.writeHead(200,{"content-type":"text/plain"}).end("Authorization denied. Return to your terminal.");reject(error);return}if(!code){res.writeHead(400,{"content-type":"text/plain; charset=utf-8"});res.end("Could not complete sign-in. Run gauge auth login again.");return}res.writeHead(200,{"content-type":"text/plain; charset=utf-8"});res.end("Authorized. Return to your terminal.");resolve5(code)});server.on("error",e=>reject(new CliError("network",`Could not receive browser sign-in on this computer: ${e.message}`,{hint:"Try `gauge auth login --device` to sign in using a browser on another device."})))});await new Promise(resolve5=>{server.listen(0,"127.0.0.1",resolve5)});const address=server.address();if(!address||typeof address==="string"){server.close();throw new CliError("internal","Could not start browser sign-in on this computer.",{hint:"Try `gauge auth login --device` instead."})}const base=resolveAuthUrl();const authorizeUrl=new URL(`${base}/cli/authorize`);authorizeUrl.searchParams.set("port",String(address.port));authorizeUrl.searchParams.set("state",state);authorizeUrl.searchParams.set("name",hostname()||"this device");authorizeUrl.searchParams.set("code_challenge",codeChallenge);authorizeUrl.searchParams.set("code_challenge_method","S256");console.log("Opening your browser to authorize the Gauge CLI\u2026");console.log(`If it doesn't open, visit:
2
+ import{LOCAL_AGENTS,LOCAL_MCP_HINT,LOCAL_TARGETS,analysisNext,assertLocalCapabilities,commentSourceSchema,deploymentSourceSchema,detachLocal,freezeWebFixtures,inspectLocalCredential,inspectLocalGateway,inspectLocalRuntime,mergeWebFixtures,parseWebFixtureInput,planLocal,pullLocalGateway,pullLocalRuntime,readProfiles,readWebFixtureArchive,requireProfile,runLocal,setProfile,websiteSourceSchema,websiteUseSchema,writeProfiles}from"./chunk-4SZPSHNF.js";import{setTelemetryEnabled,telemetryStatus}from"./chunk-WYYRMPRX.js";import{readJson}from"./chunk-MKC7K5NV.js";import{cancelLocal,cleanupLocal,localBatchStatus,localStatus,openLocalStore}from"./chunk-ZZK6V3ZF.js";import"./chunk-QV7MOO3M.js";import{CHAT_BATCH_SIZE,ChatApiClient,OUTPUT_FORMATS,addPageOptions,categoryScope,chatPath,chatScope,chunks,createChatApiClient,csv,describeScopes,fetchPages,findWorkspace,gaugeState,header,hostedOnly,instructionMode,printItems,printJson,printPage,readGaugeState,requireHosted,resolveChatCategory,resolveOrg,resolveOutput,resolveTagIds,resolveTopicId,selectedOrg,style,truncate,withCategory}from"./chunk-VHV7YMN5.js";import{createdOrganization,credentialKind,fetchDirectory,findOrganization,isUuid,legacySelection,productsLabel,resolveOrgValue}from"./chunk-ZXRPUGCO.js";import{ANALYSIS_SCHEMA_VERSION,ApiClient,CliError,DEFAULT_LIFECYCLE_TIMEOUT_SECONDS,MAX_LIFECYCLE_SOURCE_LENGTH,MAX_LIFECYCLE_TIMEOUT_SECONDS,agentConfigInputSchema,agentConfigSchema,agentSchema,analysisSubmissionSchema,assetRefListSchema,batteries_exports,brands_exports,clearToken,createClient,createdBySchema,credentialsPath,errorEnvelopeSchema,exitCodeFor,feedback_exports,keys_exports,lifecycleSchema,listResponseSchema,readCredentials,resolveAuthUrl,resolveBaseUrl,resolveToken,runConfig_exports,runCreditsChargedSchema,runEventSchema,runTriggeredBySchema,saveCredentials,saveToken,sharedTokenSchema,storedSharedCredentials,withCredentialLock}from"./chunk-QJ2UAX2V.js";import{CLI_VERSION,CONFIG_KEYS,USER_AGENT,configPath,readConfig,updateConfig}from"./chunk-OIGVDK3F.js";import{__export}from"./chunk-PSBF4TEK.js";import{Command,CommanderError as CommanderError2,Option as Option6}from"commander";import{fileURLToPath as fileURLToPath2}from"url";var auth_exports={};__export(auth_exports,{browserLogin:()=>browserLogin,deviceLogin:()=>deviceLogin,logout:()=>logout,parseLoopbackCallback:()=>parseLoopbackCallback,readStdin:()=>readStdin,register:()=>register,useOnlyOrg:()=>useOnlyOrg});import{spawn}from"child_process";import{createHash,randomBytes,randomUUID}from"crypto";import{createServer}from"http";import{hostname}from"os";import{createInterface}from"readline";import{setTimeout as sleep}from"timers/promises";import{z}from"zod";var deviceAuthorizeRequestSchema=z.object({deviceName:z.string().trim().max(64).optional(),email:z.string().trim().email().max(320).optional()});var deviceAuthorizeResponseSchema=z.object({deviceCode:z.string(),userCode:z.string(),verificationUri:z.string(),verificationUriComplete:z.string(),interval:z.number().int().positive(),expiresIn:z.number().int().positive(),emailSent:z.boolean()});var deviceTokenRequestSchema=z.object({deviceCode:z.string().min(1)});var deviceTokenApprovedSchema=z.object({status:z.literal("approved"),accessToken:z.string(),tokenType:z.literal("bearer"),expiresAt:z.string().nullable()});var deviceTokenResultSchema=z.discriminatedUnion("status",[z.object({status:z.literal("pending")}),z.object({status:z.literal("slow_down"),interval:z.number().int()}),z.object({status:z.literal("denied")}),z.object({status:z.literal("expired")}),deviceTokenApprovedSchema]);async function credentialIdentity(){if(credentialKind()==="legacy"){const{user}=await createClient().get("/api/v1/me");return{user,apiKey:null,token:{kind:"legacy",scopes:null,expiresAt:null}}}return createChatApiClient().me()}async function principalIdentity(){const{user,apiKey,token}=await credentialIdentity();const orgs=token.kind==="legacy"?null:apiKey?.organization?[apiKey.organization]:await fetchDirectory();return{user,apiKey,orgs}}function printPrincipal(identity){const{user,apiKey,orgs}=identity;if(apiKey)console.log(`Org API key "${apiKey.name}" (${apiKey.prefix}\u2026)`);else if(user)console.log(`Signed in as ${user.email||user.id}${user.name?` (${user.name})`:""}`);if(orgs)console.log(`Organizations: ${orgs.map(org=>`${org.name} (${org.slug})`).join(", ")||"(none)"}`);else if(orgs===null)console.log("Use `gauge auth login` to list your Gauge organizations.")}async function fetchAllPages(client,path,query={}){const items=[];let cursor;do{const page=await client.get(path,{limit:200,...query,cursor});items.push(...page.items);cursor=page.nextCursor}while(cursor);return items}var LOGIN_TIMEOUT_MS=2*60*1e3;function tokenSource(){if(process.env.GAUGE_API_TOKEN?.trim())return"env";return resolveToken()?"file":"none"}async function fetchMe(client,shared=false){return shared?client.get("/api/cli/me"):client.get("/api/v1/me")}function sessionCredentials(raw){return{version:1,sessionId:randomUUID(),...sharedTokenSchema.parse(raw),authUrl:resolveAuthUrl(),apiUrl:resolveBaseUrl()}}async function verifyIssuedToken(token){const shared=token.startsWith("gauge_cli_");return fetchMe(new ApiClient({token,baseUrl:shared?resolveAuthUrl():resolveBaseUrl()}),shared)}function parseLoopbackCallback(rawUrl,expectedState){const url=new URL(rawUrl??"/","http://127.0.0.1");if(url.pathname!=="/callback")return null;if(url.searchParams.get("state")===expectedState&&url.searchParams.get("error")==="access_denied"){throw new CliError("unauthorized","Authorization was denied in the browser.")}const code=url.searchParams.get("code");if(url.searchParams.get("state")!==expectedState||!code)return null;return code}function printIdentity(me){const orgs=me.orgs.map(o=>o.slug).join(", ")||"(none)";console.log(`Logged in as ${me.user.email??me.user.id}`);console.log(`Orgs: ${orgs}`);console.log(`API URL: ${resolveBaseUrl()}`)}function useOnlyOrg(me,shared=false,log=console.log){const org=me.orgs.length===1?me.orgs[0]:void 0;const only=!org?void 0:shared&&"id"in org?org.id:"canonicalGaugeOrganizationId"in org?org.canonicalGaugeOrganizationId:void 0;if(!only||readConfig().orgId)return;updateConfig({orgId:only,defaultOrg:void 0});log(`Default organization set to ${org?.name??only}.`)}function openBrowser(url){const[cmd,args]=process.platform==="darwin"?["open",[url]]:process.platform==="win32"?["cmd",["/c","start","",url]]:["xdg-open",[url]];try{const child=spawn(cmd,args,{stdio:"ignore",detached:true});child.on("error",()=>{});child.unref()}catch{}}async function browserLogin(){const state=randomBytes(16).toString("base64url");const codeVerifier=randomBytes(32).toString("base64url");const codeChallenge=createHash("sha256").update(codeVerifier).digest("base64url");const server=createServer();const codePromise=new Promise((resolve5,reject)=>{server.on("request",(req,res)=>{const url=new URL(req.url??"/","http://127.0.0.1");if(url.pathname!=="/callback"){res.writeHead(404).end();return}if(req.method!=="GET"){res.writeHead(405,{allow:"GET"}).end();return}let code;try{code=parseLoopbackCallback(req.url,state)}catch(error){res.writeHead(200,{"content-type":"text/plain"}).end("Authorization denied. Return to your terminal.");reject(error);return}if(!code){res.writeHead(400,{"content-type":"text/plain; charset=utf-8"});res.end("Could not complete sign-in. Run gauge auth login again.");return}res.writeHead(200,{"content-type":"text/plain; charset=utf-8"});res.end("Authorized. Return to your terminal.");resolve5(code)});server.on("error",e=>reject(new CliError("network",`Could not receive browser sign-in on this computer: ${e.message}`,{hint:"Try `gauge auth login --device` to sign in using a browser on another device."})))});await new Promise(resolve5=>{server.listen(0,"127.0.0.1",resolve5)});const address=server.address();if(!address||typeof address==="string"){server.close();throw new CliError("internal","Could not start browser sign-in on this computer.",{hint:"Try `gauge auth login --device` instead."})}const base=resolveAuthUrl();const authorizeUrl=new URL(`${base}/cli/authorize`);authorizeUrl.searchParams.set("port",String(address.port));authorizeUrl.searchParams.set("state",state);authorizeUrl.searchParams.set("name",hostname()||"this device");authorizeUrl.searchParams.set("code_challenge",codeChallenge);authorizeUrl.searchParams.set("code_challenge_method","S256");console.log("Opening your browser to authorize the Gauge CLI\u2026");console.log(`If it doesn't open, visit:
3
3
 
4
4
  ${authorizeUrl}
5
5
  `);console.log("Waiting for browser authorization\u2026");console.log("No local network access or using SSH? Re-run with gauge auth login --device.");openBrowser(authorizeUrl.toString());let timer;try{const code=await Promise.race([codePromise,new Promise((_,reject)=>{timer=setTimeout(()=>{reject(new CliError("network","Timed out waiting for browser authorization (2 minutes). Retry, or use gauge auth login --device. New to Gauge? Sign-up needs a work email."))},LOGIN_TIMEOUT_MS)})]);const raw=await bootstrapPost("/api/cli/token",{code,codeVerifier});const parsed=sharedTokenSchema.safeParse(raw);if(!parsed.success){throw new CliError("internal","Gauge returned a sign-in response the CLI could not read. Try `gauge auth login` again.")}const exchange=parsed.data;const token=exchange.accessToken;const me=await verifyIssuedToken(token);await withCredentialLock(async()=>{saveCredentials(sessionCredentials(raw))});console.log(`Token saved to ${credentialsPath()}`);printIdentity(me);useOnlyOrg(me,token.startsWith("gauge_cli_"))}finally{if(timer)clearTimeout(timer);server.close();server.closeAllConnections()}}async function bootstrapPost(path,body2){const base=resolveAuthUrl();let res;try{res=await fetch(`${base}${path}`,{method:"POST",redirect:"error",signal:AbortSignal.timeout(15e3),headers:{"Content-Type":"application/json",Accept:"application/json","User-Agent":USER_AGENT},body:JSON.stringify(body2)})}catch(e){throw new CliError("network",`Request to ${base} failed: ${e instanceof Error?e.message:String(e)}`)}const text2=await res.text();if(!res.ok){try{const parsed=errorEnvelopeSchema.safeParse(JSON.parse(text2));if(parsed.success){throw new CliError(parsed.data.error.code,parsed.data.error.message)}}catch(e){if(e instanceof CliError)throw e}throw new CliError("internal",`Authentication request failed: HTTP ${res.status} ${res.statusText}`.trim())}return JSON.parse(text2)}async function deviceLogin(opts){const start=await bootstrapPost("/api/cli/device/authorize",{deviceName:hostname()||"this device",email:opts.email});console.log("To sign in, open this URL in a browser:\n");console.log(` ${start.verificationUriComplete}
@@ -438,14 +438,14 @@ description: Write Gauge eval cases, choose models and judging criteria, and
438
438
 
439
439
  - Antigravity is hosted-only and requires organization enrollment. Discover available model identifiers with \`gauge models list --hosted\`. Use \`--agent antigravity:<model>\` when creating or launching saved evals/preferences, or \`agent: ANTIGRAVITY\` with explicit \`models\` in case configuration. Preview case files or JSON case specs with \`--hosted --dry-run\`; launch with \`--hosted\`. The server checks access and model availability. Hosted launches use credits; previews contact Gauge and may upload inputs.
440
440
  - Launch with \`gauge run\` (this repo's gauge-cases/ cases), \`gauge run --eval <id>\` or \`--preference <id>\` (saved: \`gauge instructions eval\` or \`gauge instructions preference\`), or \`gauge run --apply <file>\` (\`-\` for stdin) with JSON: a case spec (\`cases\`: Markdown cases as JSON, each with criteria or preference; \`gauge run --example\`) or a battery spec (\`prompts\`: saved prompts and schedules; \`gauge run --example --format battery\`). Follow a launch with \`gauge run wait <request-id>\` (exits 0 once settled; \`--check\` exits 1 unless PASSED or MEASURED), \`gauge run status <request-id>\` (one snapshot), \`gauge run watch <run-id>\`, or \`gauge run get <run-id> --logs\`. For one launch with other agents or sample counts, pass \`--agent\` and \`--samples\` with \`--eval\` or \`--preference\`; do not edit or copy the eval set or preference prompt for a smoke test.
441
+ - Fork a finished hosted run to test one change fast. The fork repeats the recorded steps before the fork point, then continues live, and costs only the part after the fork. \`gauge run fork <run-id> --before <seq>\` reruns from that seq; add \`--message <text>\` at a persona message to send a different message. \`--page <url> --edits <file>\` mocks one page the run read, with find text from \`gauge run page <run-id> <url> --query <text>\`. Only runs that \`gauge run get\` marks recorded can fork. When a finding points to one page or message, offer a fork to the user before you propose wider changes. For changes across several sessions, use an optimization.
441
442
  - \`gauge run "<prompt>"\` runs one open-ended preference prompt and saves no measurement. It uses the calling agent if codex or opencode, else claude-code; \`--agent\` overrides.
442
443
  ${AUTHORING}
443
444
  - For one-off setup/teardown, use \`gauge run --hosted "<prompt>" --setup-file ./setup.ts --teardown-file ./teardown.ts\`; add \`--dry-run\` to validate without executing hooks or launching sessions. No saved measurement is created. Files upload as inline UTF-8 source; \`--setup-timeout\`, \`--teardown-timeout\`, and \`--lifecycle-params\` configure this launch. These flags also override a saved \`--eval <id>\` or \`--preference <id>\` for one launch, inheriting unspecified hooks/timeouts/params; \`--clear-setup\`/\`--clear-teardown\`/\`--clear-lifecycle\` remove saved hooks only for that launch. Saved configuration is never edited. Hook flags do not work with local execution, case files, or \`--apply\`; saved measurement launches do not support \`--dry-run\`. Hooks use \`@withgauge/session\` bindings and do not isolate shared remote state between concurrent sessions.
444
- - Keep repository revisions, credentials, connections, runtime setup, and budgets in supported configuration or fixtures where possible. Inspect \`gauge eval create --help\` and \`gauge run --help\` for current controls; do not assume proposed limits are enforced.
445
+ - Keep repository revisions, credentials, connections, runtime setup, and budgets in supported configuration or fixtures where possible. Inspect \`gauge eval create --help\` and \`gauge run --help\` for current controls; do not assume proposed limits are enforced. Pilot before scaling; confirm before spending credits. Creating definitions does not authorize launching runs. Report sample counts and distinguish incorrect behavior, missing evidence, environment failures, and budget exhaustion.
445
446
  - For shared Markdown cases, preview with \`gauge run --dry-run\` before \`gauge run\`; \`gauge-cases/**/*.md\` cases are discovered automatically and \`--files\` overrides discovery with paths/globs. Root \`gauge.json\` can attach existing GitHub Actions artifacts and committed files; Gauge consumes them without building. Website inputs route a canonical HTTPS origin to an existing public preview; sources are GitHub Deployments or an existing provider PR comment with exact author, readiness pattern, and candidate SHA. Comments without commit identity and private or path-prefixed previews are unsupported.
446
447
  - Native command inputs use \`type: "binary"\`, \`platform: "linux-x64"\`, selected \`paths\`, and a \`commands\` map from command names to explicit relative ELF executable files. CI must upload already unpacked files. Gauge restores executable permissions and exposes declared commands on PATH; compatible system libraries are required, nested archives and arbitrary installers are unsupported.
447
448
  - Automatic GitHub PR checks support root \`gauge.json\` \`checks.paths\` as a repo-wide gate and per-case frontmatter \`config.checks.paths\` as a narrower filter. Both accept positive repository-relative globs. Cases without filters run when the repo-wide gate passes; changes to \`gauge.json\` select all cases and edits to a case select that case. Explicit CLI launches and GitHub reruns ignore both filters. Failed/unfinished previous checks, changed target branches, and unavailable diffs run conservatively. \`config.inputs\` selects inputs, not triggers.
448
- - Pilot before scaling; confirm before spending credits. Creating definitions does not authorize launching runs. Report sample counts and distinguish incorrect behavior, missing evidence, environment failures, and budget exhaustion.
449
449
  - Gauge tests artifacts from the PR's successful build; preserve CI's existing checkout settings, including the default merge checkout. Fork PRs are disabled by default. In Gauge's GitHub settings, check **Allow fork pull requests**; a workspace member then follows the GitHub check link to authorize that revision using workspace credits and connections. New revisions and explicit reruns need fresh approval. Inputs still determine readiness.
450
450
  - For authenticated MCP access, use \`gauge mcp add --hosted --name <handle> --url <https-url> --oauth\`. Complete consent in your browser; \`--no-browser\` prints the URL for remote terminals. To reuse a saved connection use \`--connection <id>\` instead (\`gauge connections list --hosted\`). Put the resulting versioned reference in case \`config.mcpRefs\`. Gauge stores and refreshes session credentials; keep tokens out of prompts and Git. See \`gauge mcp add --help\` for manual OAuth client registration options.
451
451
  - To test local build output without the GitHub App, use \`gauge run --input name=path --yes -o json\`. Repeat \`--input\` for each input. This uses current evaluation files, including edits; other inputs keep their configured sources. The path replaces the input's configured files entirely and may be a glob (quote it): skills accept a directory or archive, npm packages a .tgz or a glob matching one, files a directory, file, or glob, and binaries a directory or glob with \`commands\` relative to it. Use \`gauge run wait <id> --check -o json\` for the verdict, criterion results, and session links; with \`--check\`, failed checks exit 1 and stdout is only the JSON.
@@ -617,6 +617,8 @@ Compare winners and losers to distinguish discovery opportunities from preferenc
617
617
 
618
618
  Choose an intervention tied to the evidence: improve distribution, clarify an integration or tradeoff, fix product friction, or test a different task context. Keep organic discovery distinct from experiments that deliberately attach guidance or require research. Hold the relevant baseline fixed when testing a change; read \`gauge instructions optimization\` for supported trials and confirm before additional paid launches.
619
619
 
620
+ Offer a fork as the quick test of one hypothesis on one recorded session. \`gauge run fork <run-id> --page <url> --edits <file>\` mocks a page the agent read; \`--before <seq> --message <text>\` sends a different persona message. The fork repeats the recorded steps, continues live from the fork point, and costs only that part. Find seqs with \`gauge run trace <run-id> --seq <n>\` and pages with \`gauge run web <run-id>\`. Use an optimization to test changes across several sessions.
621
+
620
622
  Open the final answer with a compact findings list: **Friction:** what slowed or stopped agents; **Objections:** why they avoided a product; **Opportunities:** changes worth testing. Include every supported category, even when an Opportunity addresses the same incident as a Friction finding; omit empty categories. Give evidence and a practical next step for each, with uncertainty that changes its interpretation alongside it. Put run counts, setup, command recaps, and general limitations afterward. Link representative and contradictory traces alongside cohort results, rather than substituting anecdotes for the aggregate.
621
623
  `;var SESSION_ANALYSIS_LOCAL=`---
622
624
  name: gauge-session-analysis
@@ -849,12 +851,11 @@ Investigate why agents select or reject a product and which changes could improv
849
851
  its prospects. Start with existing measurements; reconstruct the initial choice,
850
852
  task context, and content that reached the agent before selection. Distinguish
851
853
  discovery from preference after exposure, and observed evidence from inferred
852
- mechanisms. Across larger cohorts, use consistent classification, validate it
853
- against sampled traces, and compare task and model/harness groups with counts
854
- and denominators. Explain missing evidence and unresolved choices. Choose a
855
- useful presentation, link supporting and contradictory sessions, and turn the
856
- findings into hypotheses for controlled interventions. Deliberately supplying
857
- guidance measures a different condition from organic discovery.
854
+ mechanisms. Across larger cohorts, validate classification against sampled
855
+ traces and compare groups with counts and denominators. Link supporting and
856
+ contradictory sessions, and turn findings into hypotheses. To test one fast,
857
+ offer to fork a recorded session with \`gauge run fork\`: mock one page it read or
858
+ send a new persona message, and pay only for the steps after the fork.
858
859
 
859
860
  Full guidance: \`gauge instructions session-analysis\`.
860
861
 
@@ -958,16 +959,16 @@ Files not in the tree rerun as the source session had them.
958
959
 
959
960
  ## Rerun and compare
960
961
 
961
- gauge optimizations launch <id> --run <run> --yes # ask first; restores auto
962
- gauge optimizations launch <id> --restore-before <rerun>:<seq> --yes # or --fresh
962
+ gauge optimizations launch <id> --run <run> --yes # ask first; forks auto
963
+ gauge optimizations launch <id> --fork-before <run>:<seq> --yes # or --fresh
963
964
  gauge optimizations watch <id> # waits for reruns and analysis
964
965
 
965
- Launch restores each rerun before its first read of a changed surface, or
966
- starts fresh, and prints the checkpoint and seq. Rerun only the sessions your
967
- edits address; check that launch lists every edit as changed. Findings come
968
- back resolved, returned (also the same barrier in other words), or introduced.
969
- Before you credit an edit, check with \`gauge run web <rerun>\` that the rerun
970
- read it. Judge findings first, report criteria and failed reruns, and iterate.
966
+ Launch forks each recorded source run: it repeats the source's steps to its last tool call before
967
+ the first read of a changed surface, then goes live. Others restore a checkpoint or start fresh.
968
+ --fork-before names the step; --fresh gives a clean baseline. Rerun only sessions your edits
969
+ address; check that launch lists every edit as changed. Findings come back resolved, returned (same
970
+ barrier, new words), or introduced. Before you credit an edit, check with \`gauge run web <rerun>\`
971
+ that the rerun read it. Judge findings first, report criteria and failed reruns, and iterate.
971
972
 
972
973
  gauge optimizations finish <id> --from result.json --yes
973
974
 
@@ -995,22 +996,29 @@ ${result.gateway.problem.hint}`)}if(!result.ready||!result.gateway.ready)process
995
996
  ${url}
996
997
  Waiting for consent\u2026`);if(options.browser)deps.openBrowser(url.toString());const deadline=Math.min(expiresAt,deps.now()+options.timeoutSeconds*1e3);while(deps.now()<deadline){const signal=AbortSignal.timeout(Math.max(1,deadline-deps.now()));let status;try{status=await client.get(`${path}/${encodeURIComponent(result.authorizationId)}`,void 0,signal)}catch(error){if(signal.aborted)break;throw error}if(status.status==="connected")return status.connectionId;if(status.status==="failed"||status.status==="expired")throw new CliError("invalid_request",status.message);await deps.sleep(Math.min(2e3,Math.max(0,deadline-deps.now())))}throw new CliError("invalid_request","Timed out waiting for MCP authorization.",{hint:"If consent succeeded, use `gauge connections list --hosted` and retry with --connection <id>. Otherwise retry with --oauth; incomplete saved grants can resume without new consent."})}function mcpPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/mcp-servers`}function serverPath(org,ref){return`${mcpPath(org)}/${encodeURIComponent(ref)}`}function specLabel(spec){return spec.transport==="stdio"?[spec.command,...spec.args??[]].join(" "):spec.url??""}function specFromOpts(opts){if(opts.specFile!==void 0){if(opts.command!==void 0||opts.url!==void 0)throw new CliError("usage","--spec-file conflicts with --command/--url");try{return JSON.parse(readFileSync8(opts.specFile==="-"?0:opts.specFile,"utf8"))}catch(e){throw new CliError("usage",`could not read --spec-file: ${e instanceof Error?e.message:e}`)}}if(opts.command!==void 0===(opts.url!==void 0))throw new CliError("usage","pass exactly one of --command (stdio) or --url (http), or --spec-file");const tools=opts.tool.map(line=>{const colon=line.indexOf(":");return colon>0?{name:line.slice(0,colon).trim(),description:line.slice(colon+1).trim()}:{name:line.trim()}});return{transport:opts.command!==void 0?"stdio":"http",...opts.command!==void 0?{command:opts.command,args:opts.arg,envNames:opts.env}:{url:opts.url,headerNames:opts.header},...opts.serverName?{serverName:opts.serverName}:{},...opts.description?{description:opts.description}:{},...tools.length?{tools}:{}}}function register18(program2){const group=hostedOnly(program2.command("mcp"),{hint:LOCAL_MCP_HINT}).description("Manage MCP servers used by hosted eval and preference runs");group.command("list").description("List the org's MCP servers with their newest version").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const res=await createClient().get(mcpPath(org));if(resolveOutput(cmd)==="json")printJson(res.items);else printItems(res.items.map(s=>({name:s.name,latest:s.latestVersion?.label??"",versions:s.versionCount,transport:s.latestVersion?.spec.transport??"",invocation:s.latestVersion?specLabel(s.latestVersion.spec):"",note:s.note??""})),"table")});group.command("get <ref>").description("Show one MCP server: note and every version").action(async(ref,_opts,cmd)=>{const org=await resolveOrg(cmd);const server=await createClient().get(serverPath(org,ref));if(resolveOutput(cmd)==="json"){printJson(server);return}console.log(`name: ${server.name}`);console.log(`id: ${server.id}`);console.log(`note: ${server.note??"(none)"}`);console.log("versions:");for(const v of server.versions){console.log(` ${server.name}@${v.label} [${v.spec.transport}] ${specLabel(v.spec)} ${v.createdAt}`);if(v.connectionId)console.log(` connection: ${v.connectionId}`);for(const w of v.warnings)console.log(` warning: ${w.message}`)}});group.command("add").description("Add a server, or append a version to an existing --name (versions are immutable)").requiredOption("--name <handle>","the handle (the agent config key)").option("--command <cmd>","stdio launch command, e.g. npx").option("--arg <arg>","stdio argument (repeatable)",collect5,[]).option("--env <NAME>","required env-var NAME \u2014 never a value (repeatable)",collect5,[]).option("--url <url>","http endpoint").option("--header <NAME>","required header NAME \u2014 never a value (repeatable)",collect5,[]).option("--server-name <name>","agent-visible server name (default: handle)").option("--description <text>","agent-visible description").option("--tool <name[: description]>","declared tool (repeatable)",collect5,[]).option("--spec-file <path>",'whole spec as JSON ("-" for stdin)').option("--label <label>","version label (a typed label always mints; omitted dedups by spec digest)").option("--note <text>","note shown on the index").option("--connection <id>","bind an existing connection to this HTTP MCP server").option("--oauth","authorize this HTTP MCP in a browser, then save its connection binding").option("--oauth-client-id <id>","pre-registered OAuth client id, when the server requires one").option("--oauth-client-secret-env <NAME>","environment variable holding an optional OAuth client secret").option("--no-browser","print the OAuth URL without opening a browser").option("--oauth-timeout <seconds>","maximum time to wait for OAuth consent (1-600)","600").action(async(opts,cmd)=>{const org=await resolveOrg(cmd);if(opts.oauth&&opts.connection)throw new CliError("usage","--oauth conflicts with --connection; choose new consent or an existing connection.");if(!opts.oauth&&(opts.oauthClientId||opts.oauthClientSecretEnv||opts.browser===false||opts.oauthTimeout!=="600"))throw new CliError("usage","OAuth options require --oauth.");const timeoutSeconds=Number(opts.oauthTimeout);if(!Number.isInteger(timeoutSeconds)||timeoutSeconds<1||timeoutSeconds>600)throw new CliError("usage","--oauth-timeout must be an integer from 1 to 600 seconds.");let spec;let body2;try{const raw=specFromOpts(opts);spec=parseMcpSpec(raw&&typeof raw==="object"&&!Array.isArray(raw)?{specVersion:1,serverName:opts.name,...raw}:raw);body2=saveMcpServerBodySchema.parse({name:opts.name,spec,label:opts.label,note:opts.note,connectionId:opts.connection})}catch(error){if(error instanceof CliError)throw error;throw new CliError("usage",error instanceof Error?error.message:"Invalid MCP configuration.")}if((opts.oauth||opts.connection)&&spec.transport!=="http")throw new CliError("usage","--oauth and --connection require an HTTP MCP server.");const client=createClient();if(opts.oauth){let clientSecret;if(opts.oauthClientSecretEnv){if(!/^[A-Za-z_][A-Za-z0-9_]*$/.test(opts.oauthClientSecretEnv))throw new CliError("usage","--oauth-client-secret-env must name an environment variable.");clientSecret=process.env[opts.oauthClientSecretEnv];if(!clientSecret)throw new CliError("usage","The OAuth client secret environment variable is empty or unset.");if(!opts.oauthClientId)throw new CliError("usage","--oauth-client-secret-env requires --oauth-client-id.")}body2.connectionId=await connectMcpOauth(client,org,{name:opts.name,url:spec.url,clientId:opts.oauthClientId,clientSecret},{browser:opts.browser,timeoutSeconds})}let result;try{result=await client.post(mcpPath(org),body2)}catch(error){if(opts.oauth&&body2.connectionId)console.error(`OAuth connected, but saving the MCP version failed. Retry with --connection ${body2.connectionId} instead of --oauth.`);throw error}if(body2.connectionId&&result.connectionId!==body2.connectionId)throw new CliError("internal","The server did not confirm the MCP connection binding.",{hint:`Upgrade the Gauge API before retrying with --connection ${body2.connectionId}.`});if(resolveOutput(cmd)==="json"){printJson(result);return}console.log(result.unchanged?`Already current: ${result.handle}@${result.label} carries this spec`:`Saved ${result.handle}@${result.label} \u2014 ${result.invocation}`);for(const w of result.warnings)console.error(`warning: ${w.message}`);console.error(`Attach it to an eval: gauge eval edit <id> --mcp ${result.handle}@${result.label}`)});group.command("edit <ref>").description("Rename or note a server (append versions with `gauge mcp add --name <handle>`)").option("--rename <handle>","new handle (name@label refs must be updated)").option("--note <text>","replace the note").option("--clear-note","remove the note").action(async(ref,opts,cmd)=>{const org=await resolveOrg(cmd);if(opts.note!==void 0&&opts.clearNote)throw new CliError("usage","--note and --clear-note conflict");const body2={};if(opts.rename!==void 0)body2.name=opts.rename;if(opts.clearNote)body2.note=null;else if(opts.note!==void 0)body2.note=opts.note;if(Object.keys(body2).length===0)throw new CliError("usage","nothing to change");const updated=await createClient().patch(serverPath(org,ref),body2);if(resolveOutput(cmd)==="json")printJson(updated);else console.log(`Updated MCP server ${updated.name}`)});group.command("rm <ref>").description("Remove a server; the handle stays reserved and finished runs keep their snapshot").action(async(ref,_opts,cmd)=>{const org=await resolveOrg(cmd);const result=await createClient().delete(serverPath(org,ref));if(resolveOutput(cmd)==="json")printJson(result);else console.log(`Deleted MCP server ${ref} (${result.referencedRuns} finished run(s) referenced it)`)})}var members_exports={};__export(members_exports,{register:()=>register19});function register19(program2){const group=hostedOnly(program2.command("members")).description("Team members and pending invites (read-only)");group.command("list").description("List members and pending invites").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const client=createClient();const base=`/api/v1/orgs/${encodeURIComponent(org)}`;const[members,pending]=await Promise.all([client.get(`${base}/members`),client.get(`${base}/invites`)]);if(resolveOutput(cmd)==="json"){printJson({members:members.items,invites:pending.items});return}printItems(members.items.map(m=>({email:m.email??"",name:m.name??"",joined:m.joinedAt})),"table");if(pending.items.length>0){console.log("\npending invites:");printItems(pending.items.map(i=>({id:i.id,email:i.email,"invited by":i.invitedBy.email??i.invitedBy.name??"",expires:i.expiresAt})),"table")}})}var models_exports={};__export(models_exports,{capabilityLabel:()=>capabilityLabel,executionTargetsPath:()=>executionTargetsPath,register:()=>register20});function executionTargetsPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/execution-targets`}function capabilityLabel(capabilities){return Object.entries(capabilities).filter(([,supported])=>supported).map(([name])=>name==="agentContext"?"context":name).join(", ")}function register20(program2){const group=program2.command("models").description("Discover supported models, harnesses, and providers");group.command("list",{isDefault:true}).description("List execution targets available to the organization. Local: the agents local runs support, with default models").action(async(_opts,cmd)=>{if(gaugeState(cmd).mode==="local"){const agents=Object.entries(LOCAL_AGENTS).map(([agent,target])=>({agent,harness:target.kind,defaultModel:target.model}));if(resolveOutput(cmd)==="json")printJson(agents);else printItems(agents,"table");return}const org=await resolveOrg(cmd);const response=await createClient().get(executionTargetsPath(org));if(resolveOutput(cmd)==="json"){printJson(response.items);return}printItems(response.items.map(target=>({model:target.model,name:target.modelLabel,harness:target.harnessSlug,provider:target.providerSlug,capabilities:capabilityLabel(target.capabilities)})),"table")})}var localProfiles_exports={};__export(localProfiles_exports,{register:()=>register21});function register21(program2){const local=program2.commands.find(command=>command.name()==="local")??program2.command("local").description("Configure local execution");const profiles=local.command("profiles").description("Manage local agent credentials (references only; separate from Gauge account login)");profiles.command("list").description("List profiles and per-harness defaults; never print secrets").action((_opts,cmd)=>{const store=readProfiles();printItems(Object.entries(store.profiles).map(([name,p])=>({name,...p,active:store.defaults[p.agent]===name})),resolveOutput(cmd))});profiles.command("set <profile>").description("Create a profile or update it with --replace; --env takes a variable name, never a secret").requiredOption("--agent <agent>","CLAUDE_CODE, CODEX_CLI, PI, or OPENCODE").requiredOption("--provider <provider>","anthropic, openai, openai-codex, fireworks, or openrouter").requiredOption("--auth <mode>","access_token (native login), api_key, or setup_token (Claude only)").option("--env <variable>","environment variable holding the key/token (defaults to the provider's standard variable)").option("--replace","replace an existing profile").action((name,opts,cmd)=>{const{replace,...profile}=opts;const store=setProfile(name,profile,replace);const result={name,...store.profiles[name],active:store.defaults[profile.agent]===name};if(resolveOutput(cmd)==="json")printJson(result);else console.log(`Saved profile ${name}. Select with gauge local profiles use ${name}, or gauge run --local --profile ${name}.`)});profiles.command("use <profile>").description("Set the default for this profile's harness").action((name,_opts,cmd)=>{const store=readProfiles();const profile=requireProfile(store,name);store.defaults[profile.agent]=name;writeProfiles(store);if(resolveOutput(cmd)==="json")printJson({agent:profile.agent,profile:name});else console.log(`${profile.agent} now uses ${name}.`)});profiles.command("remove <profile>").description("Remove a reference and its default selection; keep native logins and environment variables").action((name,_opts,cmd)=>{const store=readProfiles();requireProfile(store,name);delete store.profiles[name];const cleared=Object.keys(store.defaults).filter(agent=>store.defaults[agent]===name);for(const agent of cleared)delete store.defaults[agent];writeProfiles(store);if(resolveOutput(cmd)==="json")printJson({removed:name,clearedDefaults:cleared});else console.log(`Removed ${name}.${cleared.length?` ${cleared.join(", ")} now uses automatic credential discovery.`:""}`)});profiles.command("inspect <profile>").description("Check local compatibility, credential availability and expiry; does not call the provider").option("--model <model>","check this model (default: the harness's default model)").action(async(name,opts,cmd)=>{const store=readProfiles();const p=requireProfile(store,name);if(!opts.model&&(p.agent==="PI"||p.agent==="OPENCODE")&&!LOCAL_TARGETS[p.agent].model.startsWith(`${p.provider}/`)){throw new CliError("usage",`Specify --model ${p.provider}/<model> to inspect this profile.`,{hint:`The ${p.agent} default model uses a different provider. Choose a model available through ${p.provider}.`})}const result=await inspectLocalCredential(p.agent,opts.model??LOCAL_TARGETS[p.agent].model,{profile:name},store);if(resolveOutput(cmd)==="json")printJson(result);else{console.log(`${name}: ${result.ready?"ready locally":"not ready"} for ${result.agent} ${result.model}`);if(result.profile)console.log(`Source: ${result.profile.source}; auth: ${result.profile.auth}`);if(result.expiresAt)console.log(`Expires: ${result.expiresAt}`);if(result.problem)console.log(result.problem);console.log("Local checks do not verify provider acceptance or model access.")}if(!result.ready)process.exitCode=1})}var onboard_exports={};__export(onboard_exports,{register:()=>register22});var PERSONAL_DOMAINS=new Set(["gmail.com","googlemail.com","yahoo.com","hotmail.com","outlook.com","live.com","icloud.com","me.com","aol.com","proton.me","protonmail.com"]);function emailDomain(email){const domain=email?.split("@")[1]?.toLowerCase();return domain&&!PERSONAL_DOMAINS.has(domain)?domain:void 0}function orgNameFromEmail(email){const base=emailDomain(email)?.split(".")[0]??"";if(!base)return"My Workspace";return base.charAt(0).toUpperCase()+base.slice(1)}function websiteFromEmail(email){const domain=emailDomain(email);return domain?`https://${domain}`:void 0}async function ensureAccount(opts={}){const log=opts.log??console.log;let client;let me;if(resolveToken()){client=new ApiClient;me=await client.get("/api/v1/me");log(`Already signed in as ${me.user.email??me.user.id}.`)}else{const login=await deviceLogin({email:opts.email});await withCredentialLock(async()=>{if(login.credentials)saveCredentials(login.credentials);else saveToken(login.token)});useOnlyOrg(login.me,login.token.startsWith("gauge_cli_"),log);client=new ApiClient;me=await client.get("/api/v1/me");log(`
997
998
  Signed in as ${me.user.email??me.user.id} \u2014 token saved to ${credentialsPath()}.`)}const directory=await fetchDirectory();const wantNewOrg=opts.new===true||opts.orgName!==void 0||me.orgs.length===0;let organization;let createdOrg=null;const website=opts.website??websiteFromEmail(me.user.email);if(wantNewOrg){const name=opts.orgName?.trim()||orgNameFromEmail(me.user.email);createdOrg=await client.post("/api/v1/orgs",{name,website});organization=await createdOrganization(createdOrg.slug)}else{const config=readConfig();if(!config.orgId&&config.defaultOrg)legacySelection("config");const linkedIds=new Set(me.orgs.map(org2=>org2.canonicalGaugeOrganizationId).filter(Boolean));const available=directory.filter(org2=>linkedIds.has(org2.id));const selected=config.orgId?available.find(org2=>org2.id===config.orgId):available.length===1?available[0]:void 0;if(selected)organization=selected;else throw new CliError("usage","Select a Gauge Agents organization before onboarding.",{hint:"Run `gauge orgs`, then `gauge orgs use <slug>`."})}const org=organization.id;updateConfig({orgId:org,defaultOrg:void 0});log(createdOrg?`Created workspace ${organization.name} (${organization.slug}).`:`Using workspace ${organization.name} (${organization.slug}).`);let seeded=null;if(opts.seed!==false&&createdOrg){log("Seeding starter measurements (scanning your site)\u2026");try{seeded=await client.post(`/api/v1/orgs/${encodeURIComponent(org)}/onboarding/seed`,website?{website}:{});log(`Seeded ${seeded.evalSets} eval set(s) and ${seeded.visibilityPrompts} preference prompt(s)`+(seeded.markets?` across ${seeded.markets} market(s).`:"."))}catch(e){console.error(`warning: seeding skipped \u2014 ${e instanceof Error?e.message:e}`)}}return{user:me.user,org,organization,createdOrg,seeded}}function register22(program2){hostedOnly(program2.command("onboard"),{account:true}).description("Sign in to hosted Gauge and set up your workspace. Then run `gauge init --hosted` in a repo to connect it").option("--email <email>","email a sign-in link (device-flow convenience)").option("--org-name <name>","workspace name (default: from your email)").option("--website <url>","company site, scanned to seed the workspace (default: from your email)").option("--new","create a new workspace even if you already belong to one").option("--no-seed","skip seeding starter eval sets and the Agent Preference portfolio").action(async(opts,command)=>{const output=resolveOutput(command);const account=await ensureAccount({...opts,log:output==="json"?()=>{}:console.log});if(output==="json"){printJson({user:account.user,org:account.organization,orgId:account.org,seeded:account.seeded});return}console.log(`
998
- Default org set to ${account.organization.slug}. You're ready:`);console.log(" gauge status # the org at a glance");console.log(" gauge eval list # eval sets"," gauge preference list # preference prompts");console.log(" gauge run --eval <id> # launch one now");let inRepo=false;try{inRepo=gaugeState(command).inRepo}catch{}console.log(inRepo?" gauge init --hosted # connect this repo":" gauge init --hosted # connect a repo (run it inside the repo)")})}var optimizations_exports={};__export(optimizations_exports,{fileEditFromOpts:()=>fileEditFromOpts,fileSourceFromOpts:()=>fileSourceFromOpts,latestLaunch:()=>latestLaunch,launchSettled:()=>launchSettled,parseEdits:()=>parseEdits,parseRestoreBefore:()=>parseRestoreBefore,parseStatus:()=>parseStatus,parseSubjectRef:()=>parseSubjectRef,register:()=>register23});import{readFileSync as readFileSync9,writeFileSync as writeFileSync2}from"fs";var STATUSES2=["OPEN","DONE","ARCHIVED"];var TERMINAL_RUN=new Set(["SUCCEEDED","FAILED","TIMED_OUT","CANCELED"]);function basePath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/optimizations`}function itemPath(org,id2){return`${basePath(org)}/${encodeURIComponent(id2)}`}function readText(path){return readFileSync9(path==="-"?0:path,"utf8")}function readJson2(path){try{return JSON.parse(readText(path))}catch{throw new CliError("usage",`${path} must contain valid JSON`)}}function sleep3(ms){return new Promise(resolve5=>setTimeout(resolve5,ms))}function plural(n,noun){return`${n} ${noun}${n===1?"":"s"}`}function parseCount(raw,flag2,max){if(raw===void 0)return void 0;const n=Number(raw);if(!Number.isInteger(n)||n<1||n>max)throw new CliError("usage",`${flag2} must be a positive integer no greater than ${max}`);return n}function parseStatus(raw){if(!raw)return void 0;const status=raw.trim().toUpperCase();if(!STATUSES2.includes(status))throw new CliError("usage",`unknown status "${raw}" (expected ${STATUSES2.join(", ")})`);return status}function parseSubjectRef(opts){if([opts.issue,opts.finding,opts.goal].filter(Boolean).length!==1)throw new CliError("usage","pass one of --issue <id>, --finding <id> --run <runId>, or --goal <text>");if(opts.run&&!opts.finding)throw new CliError("usage","--run only applies with --finding");if(opts.issue)return{kind:"issue",issueId:opts.issue};if(opts.finding){if(!opts.run)throw new CliError("usage","--finding needs --run <runId>");return{kind:"finding",observationId:opts.finding,runId:opts.run}}return{kind:"free",text:opts.goal}}function fileEditFromOpts(opts,read2=readText){if([opts.page,opts.skill,opts.cli].filter(Boolean).length!==1)throw new CliError("usage","pass one of --page <url>, --skill <name> --path <path>, or --cli <package>");if(opts.delete&&!opts.skill)throw new CliError("usage","--delete removes one path from a skill");if(opts.path&&!opts.skill)throw new CliError("usage","--path only applies with --skill");if(opts.contentType&&!opts.page)throw new CliError("usage","--content-type only applies with --page");if(opts.delete&&opts.from)throw new CliError("usage","pass --delete or --from, not both");if(!opts.delete&&!opts.from)throw new CliError("usage","--from <file> is required ('-' for stdin)");const from=opts.from;if(opts.page)return{kind:"page",url:opts.page,body:read2(from),...opts.contentType?{contentType:opts.contentType}:{}};if(opts.skill){if(!opts.path)throw new CliError("usage","--skill needs --path <path inside the skill>");return{kind:"skill",name:opts.skill,path:opts.path,body:opts.delete?null:read2(from)}}let rules;try{rules=JSON.parse(read2(from))}catch{throw new CliError("usage","--from must contain CLI rules as JSON")}return{kind:"cli",package:opts.cli,rules}}function fileSourceFromOpts(opts){if([opts.page,opts.skill,opts.cli].filter(Boolean).length!==1)throw new CliError("usage","pass one of --page <url>, --skill <name>, or --cli");if(opts.page)return{kind:"page",url:opts.page};if(opts.skill)return{kind:"skill",name:opts.skill};return{kind:"cli"}}function latestLaunch(launches){return launches.reduce((latest,launch2)=>!latest||launch2.number>latest.number?launch2:latest,void 0)}function launchSettled(launch2){return launch2.runs.every(run=>TERMINAL_RUN.has(run.status)&&(run.status!=="SUCCEEDED"||run.analyzed))}function subjectLine(subject){switch(subject.kind){case"issue":return`issue "${subject.title}" (${subject.issueId})`;case"finding":return`finding "${subject.label}" on run ${subject.runId}`;case"free":return`goal "${subject.text}"`}}function toRow2(o){return{id:o.id,title:o.title.length>48?`${o.title.slice(0,45)}...`:o.title,status:o.status,subject:o.subject.kind,changed:o.changed,launches:o.launches,updated:o.updatedAt}}function fileRow(key,file,change){return{key,change:change??"",size:file.kind==="page"?`${file.body.length} chars`:file.kind==="skill"?plural(Object.keys(file.files).length,"file"):plural(file.rules.rules.length,"rule")}}function launchProgress(launch2){const done2=launch2.runs.filter(run=>TERMINAL_RUN.has(run.status));const analyzed=launch2.runs.filter(run=>run.analyzed);return`${done2.length}/${launch2.runs.length} finished, ${analyzed.length} analyzed`}function launchRow(launch2){return{id:launch2.id,number:launch2.number,reruns:launchProgress(launch2),changed:launch2.changed.length,note:launch2.note??"",created:launch2.createdAt}}function printDetail2(o){console.log(`id: ${o.id}`);console.log(`title: ${o.title}`);console.log(`status: ${o.status}`);console.log(`subject: ${subjectLine(o.subject)}`);console.log(`sources: ${o.sourceRunIds.join(", ")||"(none)"}`);const changes=new Map(o.changes.map(c=>[c.key,c.status]));const files=Object.entries(o.head.files);console.log(`files: ${files.length?"":"(empty tree)"}`);if(files.length)printItems(files.map(([key,file])=>fileRow(key,file,changes.get(key))),"table");for(const[key,change]of changes)if(change==="removed")console.log(` ${key}: removed`);console.log(`launches: ${o.launchList.length?"":"(none)"}`);if(o.launchList.length)printItems(o.launchList.map(launchRow),"table");if(o.result)console.log(`result: ${o.result.summary}`)}function printFile(file,path){if(file.kind==="page")return void console.log(file.body);if(file.kind==="cli")return printJson(file.rules);if(path){const body2=file.files[path];if(body2===void 0)throw new CliError("not_found",`no ${path} in skill ${file.name}`);return void console.log(body2)}for(const[name,body2]of Object.entries(file.files)){console.log(`=== ${name}`);console.log(body2)}}function printLaunch(launch2){console.log(`launch ${launch2.number} (${launch2.id})${launch2.note?`: ${launch2.note}`:""}`);console.log(`changed: ${launch2.changed.join(", ")||"(nothing)"}`);for(const run of launch2.runs){console.log(`
999
- ${run.id} rerun of ${run.sourceRunId??"?"}: ${run.status}, ${run.agent} ${run.model}`);if(run.restoredFrom)console.log(` restored at seq ${run.restoredFrom.seq} (${run.restoredFrom.checkpointId}); cost and time cover only the part after the restore`);if(!run.findings){console.log(" not analyzed");continue}const now=new Set(run.findings.map(f=>f.label));const before=new Set(run.sourceFindings.map(f=>f.label));for(const f of run.sourceFindings)console.log(` ${now.has(f.label)?"returned":"resolved"} ${f.label}`);for(const f of run.findings)if(!before.has(f.label))console.log(` introduced ${f.label}`)}}function parseEdits(value){const ok=Array.isArray(value)&&value.length>0&&value.every(edit=>edit&&typeof edit==="object"&&typeof edit.find==="string"&&edit.find.length>0&&typeof edit.replace==="string");if(!ok)throw new CliError("usage","--edits must be a JSON array of {find, replace} with non-empty find");return value.map(({find,replace})=>({find,replace}))}function printWrite(res){if(!res.change){console.log(`Wrote ${res.key}`);return}const{linesBefore,linesAfter,retainedPercent,hunks}=res.change;console.log(`Wrote ${res.key}: ${linesBefore} \u2192 ${linesAfter} lines, ${retainedPercent??"\u2014"}% of the original kept`);console.log(hunks)}function printPageView(view){if(view.text!==void 0){console.log(view.text);return}console.log(`${view.matches??0} matches for ${JSON.stringify(view.query)}`);for(const excerpt of view.excerpts??[]){console.log(`=== offset ${excerpt.offset}`);console.log(excerpt.raw)}if(view.truncated)console.log("\u2026 more matches; search a longer phrase");if(view.hint)console.log(view.hint)}function parseRestoreBefore(raw){const at=raw.lastIndexOf(":");const rerunId=raw.slice(0,at);const seq=raw.slice(at+1);if(at<=0||!/^-?\d+$/.test(seq))throw new CliError("usage",`--restore-before takes <rerunId>:<seq>, e.g. run_1:140 (got ${raw})`);return{rerunId,seq:Number(seq)}}function checkpointRow(cp){const exposure=[...cp.exposure.surfaces,...cp.exposure.skillChanged?["skill changed"]:[],...cp.exposureIncomplete?["trace incomplete"]:[]];return{checkpoint:cp.checkpointId,source:cp.sourceRunId,rerun:cp.rerunId,seq:`${cp.seq}/${cp.rerunEventCount}`,exposure:exposure.join(", ")||"clean","last step":cp.lastSteps.at(-1)??"",captured:cp.capturedAt}}function printLaunchResult(id2,res){console.log(`Launched ${plural(res.batches.flatMap(b=>b.runIds).length,"rerun")} (${res.launchId}) with ${plural(res.changed.length,"changed file")}:`);for(const key of res.changed)console.log(` ${key}`);for(const batch of res.batches){const from=batch.restoredFrom;console.log(from?`${batch.sourceRunId}: restored at seq ${from.seq} of rerun ${from.rerunId} (${from.checkpointId})`:`${batch.sourceRunId}: fresh`);for(const warning of batch.warnings)console.log(` ! ${warning}`)}console.log(`Follow with \`gauge optimizations watch ${id2}\`.`)}function register23(program2){const group=hostedOnly(program2.command("optimizations")).description("Hill climb your agent surfaces: edit the pages, skills, and CLI rules agent sessions used, rerun those sessions, and compare findings");group.command("list").description("List the org's optimizations, newest activity first").option("--status <status>",STATUSES2.join(", ")).option("--limit <n>","max rows (default 50)").action(async(opts,cmd)=>{const res=await createClient().get(basePath(await resolveOrg(cmd)),{status:parseStatus(opts.status),limit:parseCount(opts.limit,"--limit",200)});if(resolveOutput(cmd)==="json")printJson(res.items);else printItems(res.items.map(toRow2),"table")});group.command("create").description("Open an optimization on an issue, one finding, or a goal; the tree starts empty").option("--issue <id>","an issue id from `gauge issues list`").option("--finding <id>","a finding id from `gauge run insight <run> -o json`; needs --run").option("--run <runId>","the run the finding is on").option("--goal <text>","free text; starts with no sessions, attach them with `sessions add`").option("--title <title>","name (default: from the subject)").action(async(opts,cmd)=>{const detail=await createClient().post(basePath(await resolveOrg(cmd)),{subject:parseSubjectRef(opts),...opts.title?{title:opts.title}:{}});if(resolveOutput(cmd)==="json")printJson(detail);else console.log(detail.sourceRunIds.length?`Opened ${detail.id} "${detail.title}" on ${plural(detail.sourceRunIds.length,"source session")}. Next: \`gauge optimizations authoring ${detail.id}\`.`:`Opened ${detail.id} "${detail.title}" with no sessions. Next: \`gauge optimizations sessions add ${detail.id} <runId...>\`.`)});group.command("get <id>").description("One optimization: subject, source runs, tree, and launches").action(async(id2,_opts,cmd)=>{const detail=await createClient().get(itemPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(detail);else printDetail2(detail)});group.command("authoring <id>").description("Authoring guidance: the source sessions' findings, the tree, and the editing practices to follow").action(async(id2,_opts,cmd)=>{const res=await createClient().get(`${itemPath(await resolveOrg(cmd),id2)}/authoring`);if(resolveOutput(cmd)==="json")printJson(res);else console.log(res.guidance)});group.command("sessions").description("Attach sessions an optimization can rerun").command("add <id> <runIds...>").description("Attach sessions to rerun, up to 12 in all; a goal starts with none. Optimization reruns cannot be sources").action(async(id2,runIds,_opts,cmd)=>{const res=await createClient().post(`${itemPath(await resolveOrg(cmd),id2)}/sessions`,{runIds});if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Attached ${plural(res.added.length,"session")}; ${id2} has ${plural(res.sourceRunIds.length,"source session")}.`)});const file=group.command("file").description("Read and edit the files in an optimization's tree");file.command("read <id> <key>").description("Print one file by tree key: a page's exact source (--text for readable text, --query for the source around a match), a skill (or one --path in it), or CLI rules").option("--text","page only: readable text instead of the source").option("--query <text>","page only: the exact source around each match, to copy into file edit").option("--path <path>","one file inside a skill").option("--out <file>","save the page source or skill --path to a file").action(async(id2,key,opts,cmd)=>{const path=`${itemPath(await resolveOrg(cmd),id2)}/files`;if(opts.text||opts.query){if(opts.out)throw new CliError("usage","--out saves the stored file only");const view=await createClient().get(path,{key,...opts.query?{query:opts.query}:{view:"text"}});if(resolveOutput(cmd)==="json")printJson(view);else printPageView(view);return}const res=await createClient().get(path,{key});if(opts.out){const body2=res.kind==="page"?res.body:res.kind==="skill"&&opts.path?res.files[opts.path]:void 0;if(body2===void 0)throw new CliError("usage","--out saves a page source or one skill --path");writeFileSync2(opts.out,body2);console.error(`Saved ${opts.out}`)}else if(resolveOutput(cmd)==="json")printJson(res);else printFile(res,opts.path)});file.command("edit <id>").description("Edit a page in the tree with find/replace on its exact source; every byte outside the edits stays. Each find must appear once; all edits apply or none do").requiredOption("--page <url>","the page URL").requiredOption("--edits <file>","JSON [{find, replace}]; copy find text from `file read --query`; '-' for stdin").action(async(id2,opts,cmd)=>{const res=await createClient().patch(`${itemPath(await resolveOrg(cmd),id2)}/files`,{url:opts.page,edits:parseEdits(readJson2(opts.edits))});if(resolveOutput(cmd)==="json")printJson(res);else printWrite(res)});file.command("add <id>").description("Copy an original into the tree before you edit it: a page a source session read (else the live page), a staged skill, or the CLI rules").option("--page <url>","a page by URL").option("--skill <name>","a skill a source session staged").option("--cli","the CLI rules a source session ran with").action(async(id2,opts,cmd)=>{const res=await createClient().post(`${itemPath(await resolveOrg(cmd),id2)}/files`,fileSourceFromOpts(opts));if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Added ${res.key} (from ${res.origin})`)});file.command("write <id>").description("Write a new page or rewrite a whole one, one skill file, or the CLI rules; to change part of a page, use file edit").option("--page <url>","the page URL; --from holds the complete body").option("--content-type <type>","the page content type (default: keep)").option("--skill <name>","the skill; needs --path").option("--path <path>","the file inside the skill, e.g. SKILL.md").option("--delete","remove --path from the skill").option("--cli <package>","the CLI package; --from holds {schemaVersion: 3, target, rules, shippingIntent}").option("--from <file>","the new content; '-' for stdin").action(async(id2,opts,cmd)=>{const res=await createClient().put(`${itemPath(await resolveOrg(cmd),id2)}/files`,fileEditFromOpts(opts));if(resolveOutput(cmd)==="json")printJson(res);else printWrite(res)});file.command("rm <id> <key>").description("Drop a file from the tree; reruns use what the source session had").action(async(id2,key,_opts,cmd)=>{const res=await createClient().delete(`${itemPath(await resolveOrg(cmd),id2)}/files`,{key});if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Removed ${res.removed}`)});group.command("notes <id>").description("Print the working notes, or replace them with --from: markdown the user reads beside the chat").option("--from <file>","the complete notes; '-' for stdin").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();const detail=opts.from?await client.patch(path,{notes:readText(opts.from)}):await client.get(path);if(resolveOutput(cmd)==="json")printJson({notes:detail.notes});else if(opts.from)console.log(`Saved notes (${detail.notes.length} chars)`);else console.log(detail.notes||"(no notes)")});group.command("launch <id>").description("Rerun the source sessions with the tree injected; each rerun restores from the deepest checkpoint before any changed surface, or starts fresh").option("--run <runId...>","rerun only these source runs").option("--note <text>","one line on what this launch tests").option("--restore-before <rerunId:seq...>","restore from the deepest checkpoint at or before this seq of an earlier rerun").option("--checkpoint <checkpointId...>","restore from this checkpoint (see `checkpoints`)").option("--fresh [sourceRunId...]","start from zero: these source runs, or every one").option("--yes","skip the confirmation").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);await confirmOrAbort(`Rerun ${opts.run?.length?plural(opts.run.length,"source session"):"every source session"} of ${id2} with the tree?`,opts.yes);const client=createClient();const restoreAt=[...(opts.checkpoint??[]).map(checkpointId=>({checkpointId})),...(opts.restoreBefore??[]).map(parseRestoreBefore)];const fresh=opts.fresh===true?opts.run??(await client.get(path)).sourceRunIds:opts.fresh??[];const res=await client.post(`${path}/launch`,{...opts.run?.length?{runIds:opts.run}:{},...opts.note?{note:opts.note}:{},...fresh.length?{restore:Object.fromEntries(fresh.map(runId=>[runId,"none"]))}:{},...restoreAt.length?{restoreAt}:{}});if(resolveOutput(cmd)==="json")printJson(res);else printLaunchResult(id2,res)});group.command("checkpoints <id>").description("Checkpoints captured on fresh reruns, with how far in each is and whether its prefix saw a changed surface").option("--run <sourceRunId>","only this source run").action(async(id2,opts,cmd)=>{const res=await createClient().get(`${itemPath(await resolveOrg(cmd),id2)}/checkpoints`,{sourceRunId:opts.run});if(resolveOutput(cmd)==="json")printJson(res.items);else if(!res.items.length)console.log("(no checkpoints)");else printItems(res.items.map(checkpointRow),"table")});group.command("launches <id> [launchId]").description("List launches, or one launch's reruns with their findings beside the source findings").action(async(id2,launchId,_opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();if(!launchId){const detail=await client.get(path);if(resolveOutput(cmd)==="json")printJson(detail.launchList);else printItems(detail.launchList.map(launchRow),"table");return}const launch2=await client.get(`${path}/launches/${encodeURIComponent(launchId)}`);if(resolveOutput(cmd)==="json")printJson(launch2);else printLaunch(launch2)});group.command("watch <id>").description("Wait until the latest launch's reruns finish and are analyzed, then print them").option("--interval <seconds>","poll interval (default 15)").option("--timeout <minutes>","give up after this long (default 60)").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();const interval=(parseCount(opts.interval,"--interval",600)??15)*1e3;const timeoutMinutes=parseCount(opts.timeout,"--timeout",1440)??60;const deadline=Date.now()+timeoutMinutes*6e4;let last="";for(;;){const detail=await client.get(path);const latest=latestLaunch(detail.launchList);if(!latest)throw new CliError("usage",`${id2} has no launches yet`);if(launchSettled(latest)){const launch2=await client.get(`${path}/launches/${encodeURIComponent(latest.id)}`);if(resolveOutput(cmd)==="json")printJson(launch2);else printLaunch(launch2);return}const progress=launchProgress(latest);if(progress!==last)console.error(`launch ${latest.number}: ${progress}`);last=progress;if(Date.now()>=deadline)throw new CliError("network",`Timed out after ${timeoutMinutes} minutes (launch ${latest.number}: ${progress}). Re-run watch, or check \`gauge optimizations launches ${id2} ${latest.id}\`.`);await sleep3(interval)}});group.command("finish <id>").description("Close the optimization with its result: summary, changed keys, and findings resolved, returned, and introduced").requiredOption("--from <file>","result JSON: {summary, changed, resolved, returned, introduced}").option("--yes","skip the confirmation").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const result=readJson2(opts.from);await confirmOrAbort(`Finish ${id2}? Its tree can no longer change.`,opts.yes);const detail=await createClient().post(`${path}/finish`,result);if(resolveOutput(cmd)==="json")printJson(detail);else console.log(`Finished ${detail.id} "${detail.title}"`)})}var org_exports={};__export(org_exports,{register:()=>register24});function printSettings(s,billingStatus){console.log(`slug: ${s.slug}`);console.log(`name: ${s.name}`);console.log(`website: ${s.website??"(none)"}`);console.log(`max spend/run: ${s.maxSpendUsd==null?"(server default)":`$${s.maxSpendUsd}`}`);console.log(`overage: ${fmtOverage(billingStatus)}`);console.log(`auto-join: ${s.autoJoinDomain??"off"}`)}function register24(program2){const group=hostedOnly(program2.command("org")).description("This org's settings, read-only (see `gauge orgs` for the org list)");group.command("get").description("Show the org's settings (edit them in the web app)").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const client=createClient();const base=`/api/v1/orgs/${encodeURIComponent(org)}`;const[settings,billingStatus]=await Promise.all([client.get(base),client.get(`${base}/billing`)]);if(resolveOutput(cmd)==="json")printJson({...settings,paymentType:billingStatus.paymentType??null,creditLine:billingStatus.creditLine??null});else printSettings(settings,billingStatus)})}var orgs_exports={};__export(orgs_exports,{register:()=>register25});import{writeFileSync as writeFileSync3}from"fs";import{join as join8}from"path";function register25(program2){const orgs=hostedOnly(program2.command("orgs"),{account:true}).description("Organizations, and which Gauge products each one uses");orgs.command("list",{isDefault:true}).description("List your Gauge organizations (* marks the saved default)").action(async(_opts,command)=>{const rows=await fetchDirectory();const{orgId}=readConfig();if(resolveOutput(command)==="json"){printJson(rows.map(row2=>({...row2,isDefault:row2.id===orgId})));return}printItems(rows.map(row2=>({default:row2.id===orgId?"*":"",slug:row2.slug,name:row2.name,products:productsLabel(row2.products)})),"table")});orgs.command("create <name>").description("Create a Gauge Agents workspace and set its organization as the default").option("--website <url>","company site (used as the logo source)").option("--no-use","don't set the new org as the default").action(async(name,opts,command)=>{await fetchDirectory();const created=await createClient().post("/api/v1/orgs",{name,website:opts.website});const org=await createdOrganization(created.slug);if(opts.use!==false)updateConfig({orgId:org.id,defaultOrg:void 0});if(resolveOutput(command)==="json")return printJson(org);console.log(`Created workspace ${org.name} (${org.slug})`);if(opts.use!==false)console.log(`Default org set to ${org.slug}`)});orgs.command("use <org>").description("Select a Gauge organization by its public slug").option("--project","save the selection in this project's gauge.json").action(async(value,opts,command)=>{const org=await findOrganization(value);if(opts.project){const state=gaugeState(command);if(!state.root||!state.config)throw new CliError("usage","No valid gauge.json found. Run `gauge init` first.");const{org:_legacyOrg,...config}=state.config;writeFileSync3(join8(state.root,"gauge.json"),`${JSON.stringify({...config,orgId:org.id},null,2)}
1000
- `)}else updateConfig({orgId:org.id,defaultOrg:void 0});if(resolveOutput(command)==="json")return printJson({orgId:org.id,slug:org.slug,name:org.name});console.log(`${opts.project?"Project":"Default"} org set to ${org.name} (${org.slug})`)})}var personas_exports={};__export(personas_exports,{register:()=>register26});function personasPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/personas`}function register26(program2){const group=hostedOnly(program2.command("personas")).description("Manage simulated-user personas for eval and preference runs");group.command("list").description("List the org's personas").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const res=await createClient().get(personasPath(org));if(resolveOutput(cmd)==="json")printJson(res.items);else printItems(res.items.map(p=>({id:p.id,name:p.name,description:p.description.length>60?`${p.description.slice(0,57)}...`:p.description})),"table")});group.command("get <id>").description("Show one persona").action(async(id2,_opts,cmd)=>{const org=await resolveOrg(cmd);const persona=await createClient().get(`${personasPath(org)}/${encodeURIComponent(id2)}`);if(resolveOutput(cmd)==="json"){printJson(persona);return}console.log(`id: ${persona.id}`);console.log(`name: ${persona.name}`);console.log(`model: ${persona.model}`);console.log(`description: ${persona.description}`)});group.command("add").description("Create a persona").requiredOption("--name <name>","persona name").requiredOption("--description <text>",'memory-style notes ("The user is a solo founder\u2026")').action(async(opts,cmd)=>{const org=await resolveOrg(cmd);const created=await createClient().post(personasPath(org),{name:opts.name,description:opts.description});if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Created persona ${created.id} (${created.name})`)});group.command("edit <id>").description("Update a persona").option("--name <name>","rename the persona").option("--description <text>","replace the notes").action(async(id2,opts,cmd)=>{const org=await resolveOrg(cmd);if(opts.name===void 0&&opts.description===void 0)throw new CliError("usage","nothing to change");const updated=await createClient().patch(`${personasPath(org)}/${encodeURIComponent(id2)}`,{name:opts.name,description:opts.description});if(resolveOutput(cmd)==="json")printJson(updated);else console.log(`Updated persona ${updated.id}`)});group.command("rm <id>").description("Delete a persona (run configurations fall back to the default persona)").action(async(id2,_opts,cmd)=>{const org=await resolveOrg(cmd);await createClient().delete(`${personasPath(org)}/${encodeURIComponent(id2)}`);if(resolveOutput(cmd)!=="json")console.log(`Deleted persona ${id2}`)})}var preference_exports={};__export(preference_exports,{register:()=>register27});function visibilityPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/visibility-prompts`}function promptPath(org,id2){return`${visibilityPath(org)}/${encodeURIComponent(id2)}`}function scenariosPath(org,id2){return`${promptPath(org,id2)}/scenarios`}function scenarioRow(scenario){return{id:scenario.id,repo:scenario.repoUrl??"(empty project)",ref:scenario.repoRef??"",persona:scenario.profile?.name??"(default)"}}var KIND_CHOICES=["head-to-head","open-ended"];function parseKind(raw){const upper=raw==="open-ended"?"RECOMMEND":raw.trim().toUpperCase().replace(/-/g,"_");if(upper!=="HEAD_TO_HEAD"&&upper!=="RECOMMEND")throw new CliError("usage",`invalid --kind "${raw}" (expected: ${KIND_CHOICES.join(" | ")})`);return upper}function brandsLabel(v){if(v.kind==="HEAD_TO_HEAD")return`${v.brandA} vs ${v.brandB}`;return v.branded?v.brandA??"branded":"organic"}function preferencePromptRow(v){return{id:v.id,name:v.name,kind:v.kind==="RECOMMEND"?"open-ended":"head-to-head",brands:brandsLabel(v),market:v.market?.slug??"",tags:v.tags.join(", "),runs:v.runCount,"last run":v.lastRunAt??"-","created by":creatorLabel(v.createdBy)}}function printDetail3(v){console.log(`id: ${v.id}`);console.log(`name: ${v.name}`);console.log(`kind: ${v.kind==="RECOMMEND"?"open-ended":"head-to-head"}`);console.log(`brands: ${brandsLabel(v)}`);console.log(`market: ${v.market?`${v.market.name} (${v.market.slug})`:"(none)"}`);console.log(`tags: ${v.tags.join(", ")||"(none)"}`);console.log(`runs: ${v.runCount} (last: ${v.lastRunAt??"never"})`);console.log(`creator: ${creatorLabel(v.createdBy)}`);console.log(`prompt: ${v.promptText}`);console.log(`cadence: ${cadenceLabel(v.cadence)} \u2014 ${v.sampleCount} sample(s)`);console.log(`agents: ${v.agents.map(a=>a.models.length?`${a.agent}:${a.models.join(",")}`:a.agent).join(", ")}`);console.log(`skills: ${v.skillRefs.join(", ")||"(none)"}`);console.log(`mcp: ${v.mcpRefs.join(", ")||"(none)"}`);console.log(`conns: ${v.connectionIds.join(", ")||"(none)"}`);console.log(`scenarios: ${v.scenarios.length}`);for(const scenario of v.scenarios)console.log(` [${scenario.id}] ${scenario.repoUrl??"(empty project)"}${scenario.repoRef?`@${scenario.repoRef}`:""} \u2014 ${scenario.profile?.name??"default persona"}`)}async function listPreferencePrompts(cmd){if(isLocal(cmd))return listLocalMeasurements(cmd,"preference");const res=await createClient().get(visibilityPath(await resolveOrg(cmd)));return res.items}async function getPreferencePrompt(cmd,id2){const vp=isLocal(cmd)?localMeasurementView(getLocalMeasurement(cmd,"preference",id2)):await createClient().get(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(vp);else printDetail3(vp)}async function createPreferencePrompt(cmd,opts){if(hasLifecycleFlags(opts)&&isLocal(cmd))throw new CliError("usage","Lifecycle scripts require hosted execution; pass --hosted");if(opts.kind===void 0)throw new CliError("usage",`--kind is required with --preference (${KIND_CHOICES.join(" | ")})`);if(opts.ref!==void 0&&opts.repo===void 0)throw new CliError("usage","--ref requires --repo");const hasScenario=opts.repo!==void 0||opts.persona!==void 0;const body2={name:opts.name,promptText:readPromptText(opts),kind:parseKind(opts.kind),branded:opts.branded,brandA:opts.brandA??null,brandB:opts.brandB??null,marketId:opts.market??null,tags:opts.tag.length>0?opts.tag:void 0,...hasRunSettingsFlags(opts)?runSettingsFromOpts(opts):{},scenarios:hasScenario?[{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:opts.persona??null}]:[]};const created=isLocal(cmd)?createLocalMeasurement(cmd,"preference",body2):await createClient().post(visibilityPath(await resolveOrg(cmd)),body2);if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Created preference prompt ${created.id}`)}async function editPreferencePrompt(cmd,id2,opts){if(hasLifecycleFlags(opts)&&isLocal(cmd))throw new CliError("usage","Lifecycle scripts require hosted execution; pass --hosted");const lifecyclePath=hasLifecycleFlags(opts)&&!opts.clearLifecycle?promptPath(await resolveOrg(cmd),id2):void 0;const existing=lifecyclePath?await createClient().get(lifecyclePath):void 0;const body2={...runSettingsPatchFromOpts(opts,existing?.lifecycle)};if(opts.name!==void 0)body2.name=opts.name;if(opts.text!==void 0||opts.file!==void 0)body2.promptText=readPromptText(opts);if(opts.brandA!==void 0)body2.brandA=opts.brandA;if(opts.brandB!==void 0)body2.brandB=opts.brandB;if(opts.branded&&opts.organic)throw new CliError("usage","--branded and --organic conflict");if(opts.branded)body2.branded=true;else if(opts.organic)body2.branded=false;if(opts.market!==void 0&&opts.clearMarket)throw new CliError("usage","--market and --clear-market conflict");if(opts.clearMarket)body2.marketId=null;else if(opts.market!==void 0)body2.marketId=opts.market;if(opts.tag.length>0&&opts.clearTags)throw new CliError("usage","--tag and --clear-tags conflict");if(opts.clearTags)body2.tags=[];else if(opts.tag.length>0)body2.tags=opts.tag;if(Object.keys(body2).length===0)throw new CliError("usage","nothing to change");const updated=isLocal(cmd)?editLocalMeasurement(cmd,"preference",id2,body2):await createClient().patch(lifecyclePath??promptPath(await resolveOrg(cmd),id2),body2);if(resolveOutput(cmd)==="json")printJson(updated);else console.log(`Updated preference prompt ${updated.id}`)}async function removePreferencePrompt(cmd,id2){if(isLocal(cmd))removeLocalMeasurement(cmd,"preference",id2);else await createClient().delete(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)!=="json")console.log(`Deleted preference prompt ${id2}`)}async function listScenarios(cmd,id2){const vp=isLocal(cmd)?localMeasurementView(getLocalMeasurement(cmd,"preference",id2)):await createClient().get(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(vp.scenarios);else printItems(vp.scenarios.map(scenarioRow),"table")}async function addScenario(cmd,id2,opts){if(opts.ref!==void 0&&opts.repo===void 0)throw new CliError("usage","--ref requires --repo");if(isLocal(cmd)){if(opts.persona)throw new CliError("usage","Personas require hosted Gauge");const row2=getLocalMeasurement(cmd,"preference",id2);const updated=editLocalMeasurement(cmd,"preference",id2,{scenarios:[...row2.scenarios,{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:null}]});const scenario=updated.scenarios.at(-1);if(resolveOutput(cmd)==="json")printJson(scenario);else console.log(`Added scenario ${scenario.id}`);return}const created=await createClient().post(scenariosPath(await resolveOrg(cmd),id2),{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:opts.persona??null});if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Added scenario ${created.id}`)}async function removeScenario(cmd,id2,scenarioId){if(isLocal(cmd)){const row2=getLocalMeasurement(cmd,"preference",id2);const scenarios=row2.scenarios;if(!scenarios.some(s=>s.id===scenarioId))throw new CliError("usage",`Unknown scenario: ${scenarioId}`);editLocalMeasurement(cmd,"preference",id2,{scenarios:scenarios.filter(s=>s.id!==scenarioId)});if(resolveOutput(cmd)!=="json")console.log(`Removed scenario ${scenarioId}`);return}await createClient().delete(`${scenariosPath(await resolveOrg(cmd),id2)}/${encodeURIComponent(scenarioId)}`);if(resolveOutput(cmd)!=="json")console.log(`Removed scenario ${scenarioId}`)}async function runPreferencePrompt(cmd,id2,opts){const org=await resolveOrg(cmd);const overrides={...runOverridesFromOpts(opts),...opts.cliRules?{cliRules:opts.cliRules}:{},...hasLifecycleFlags(opts)?lifecyclePatchFromOpts(opts,(await createClient().get(promptPath(org,id2))).lifecycle):{}};const settings=Object.keys(overrides).length>0?"these overrides (stored settings stay unchanged)":"the stored settings";await confirmOrAbort(opts.scenario.length>0?`Run ${opts.scenario.length} selected scenario${opts.scenario.length===1?"":"s"} with ${settings} now?`:`Run every scenario of this prompt with ${settings} now?`,opts.yes);const result=await createClient().post(`${promptPath(org,id2)}/run`,{visibilityScenarioIds:opts.scenario.length>0?opts.scenario:void 0,...overrides});if(resolveOutput(cmd)==="json")printJson(result);else{console.log(`Started ${result.runIds.length} run${result.runIds.length===1?"":"s"} across ${result.batchIds.length} scenario${result.batchIds.length===1?"":"s"}`);for(const batch of result.batchIds)console.log(`Runs: gauge run list --batch ${batch}`)}return result}async function listPreferencePromptRuns(cmd,id2,opts){const res=await createClient().get(`${promptPath(await resolveOrg(cmd),id2)}/runs`,{limit:opts.limit,cursor:opts.cursor});if(resolveOutput(cmd)==="json"){printJson(res);return}printItems(res.items.map(r=>({id:r.id,status:r.status,agent:r.agent,model:r.resolvedModel??r.model??"",source:r.triggeredBy?.source??"",credits:r.creditsCharged??"",created:r.createdAt,verdict:r.verdict?.status??"-"})),"table");if(res.nextCursor)console.error(`(more results \u2014 rerun with --cursor ${res.nextCursor})`)}async function markets(cmd,opts){if(isLocal(cmd))throw new CliError("usage","Markets require hosted Gauge. Pass --hosted.");const client=createClient();const base=`/api/v1/orgs/${encodeURIComponent(await resolveOrg(cmd))}/markets`;if(opts.add!==void 0){const market=await client.post(base,{marketId:opts.add});if(resolveOutput(cmd)==="json")printJson(market);else console.log(`Tracking market ${market.name} (${market.slug})`);return}const res=await client.get(base);if(resolveOutput(cmd)==="json"){printJson(res);return}printItems([...res.tracked.map(m=>({id:m.marketId,slug:m.slug,name:m.name,tracked:"yes",source:m.source})),...res.addable.map(m=>({id:m.marketId,slug:m.slug,name:m.name,tracked:"no",source:""}))],"table")}function register27(program2){const preference=program2.command("preference").description("Manage preference prompts: which product agents choose. Launch one with `gauge run --preference <id>`");preference.command("list").description("List preference prompts").option("--created-by <email|me>","only prompts created by this user").action(async(opts,cmd)=>{const items=await filterByCreator(await listPreferencePrompts(cmd),opts.createdBy);if(resolveOutput(cmd)==="json")printJson(items);else printItems(items.map(preferencePromptRow),"table")});preference.command("get <id>").description("Show one preference prompt").action(async(id2,_opts,cmd)=>{await getPreferencePrompt(cmd,id2)});addRunSettingsCreateOptions(preference.command("create").description("Create a preference prompt").requiredOption("--name <name>","name").option("--text <text>","prompt text inline").option("--file <path>",'prompt text from a file ("-" for stdin)').option("--repo <url>","the first scenario's repository (default: empty project)").option("--ref <ref>","branch, tag, or commit (with --repo)").option("--persona <profileId>","persona profile id")).option("--kind <kind>",KIND_CHOICES.join(" | ")).option("--brand-a <name>","first brand (head-to-head)").option("--brand-b <name>","second brand (head-to-head)").option("--branded","an open-ended prompt that names a brand").option("--market <marketId>","tracked market id (see `gauge preference markets`)").option("--tag <name>","user tag name, create-on-type (repeatable)",collect5,[]).action(async(opts,cmd)=>{await createPreferencePrompt(cmd,opts)});addRunSettingsEditOptions(preference.command("edit <id>").description("Update a preference prompt. Omitted flags keep their value.").option("--name <name>","rename it").option("--text <text>","replace the prompt text inline").option("--file <path>",'replace the prompt text from a file ("-" stdin)')).option("--brand-a <name>","first brand").option("--brand-b <name>","second brand").option("--branded","mark branded (open-ended only)").option("--organic","mark organic (open-ended only)").option("--market <marketId>","set the measured market").option("--clear-market","unscope from any market").option("--tag <name>","replace the user tag set (repeatable)",collect5,[]).option("--clear-tags","remove all user tags").action(async(id2,opts,cmd)=>{await editPreferencePrompt(cmd,id2,opts)});preference.command("rm <id>").description("Delete a preference prompt (run history stays)").action(async(id2,_opts,cmd)=>{await removePreferencePrompt(cmd,id2)});const scenarios=preference.command("scenarios").description("The repo \xD7 persona scenarios a preference prompt runs in");scenarios.command("list <id>").description("List a preference prompt's scenarios").action(async(id2,_opts,cmd)=>{await listScenarios(cmd,id2)});scenarios.command("add <id>").description("Add one repo \xD7 persona scenario").option("--repo <url>","repository URL (default: empty project)").option("--ref <ref>","branch, tag, or commit (with --repo)").option("--persona <profileId>","persona profile id (default persona if omitted)").action(async(id2,opts,cmd)=>{await addScenario(cmd,id2,opts)});scenarios.command("rm <id>").description("Remove one scenario").requiredOption("--scenario <scenarioId>","scenario id to remove").action(async(id2,opts,cmd)=>{await removeScenario(cmd,id2,opts.scenario)});preference.command("markets").description("List tracked and addable markets for preference prompts, or track one with --add").option("--add <marketId>","track a published market").action(async(opts,cmd)=>{await markets(cmd,opts)})}var repos_exports={};__export(repos_exports,{register:()=>register28});function reposPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/repos`}async function fetchAllRepos(client,org){return fetchAllPages(client,reposPath(org))}function toRow3(repo){return{name:repo.name,url:repo.url,languages:repo.languages.join(", "),frameworks:repo.frameworks.join(", "),tags:repo.tags.join(", "),size:repo.sizeBucket??"",stars:repo.stars,system:repo.isSystem?"yes":""}}function syncWarning(repo){return`warning: GitHub metadata sync failed; retry with \`gauge repos resync ${repo.url}\``}function register28(program2){const repos=hostedOnly(program2.command("repos")).description("Manage repositories");repos.command("list").description("List repositories (yours + read-only Gauge-provided ones)").action(async(_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const items=await fetchAllRepos(createClient(),org);if(output==="json"){printJson(items);return}printItems(items.map(toRow3),"table")});repos.command("add <url>").description("Add a repository (pulls languages/size/stars from GitHub)").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const result=await createClient().post(reposPath(org),{url});if(output==="json"){printJson(result);return}console.log(`Added ${result.repo.name} (${result.repo.url})`);if(!result.synced)console.error(syncWarning(result.repo))});repos.command("edit <url>").description("Update a repository's display name, frameworks, or tags").option("--name <name>","display name").option("--framework <name>","REPLACE the framework list (repeatable)",collect5,[]).option("--tag <name>","REPLACE the tag list (repeatable)",collect5,[]).option("--clear-frameworks","remove all frameworks").option("--clear-tags","remove all tags").action(async(url,opts,command)=>{const org=await resolveOrg(command);if(opts.framework.length>0&&opts.clearFrameworks)throw new CliError("usage","--framework and --clear-frameworks conflict");if(opts.tag.length>0&&opts.clearTags)throw new CliError("usage","--tag and --clear-tags conflict");const body2={url};if(opts.name!==void 0)body2.name=opts.name;if(opts.clearFrameworks)body2.frameworks=[];else if(opts.framework.length>0)body2.frameworks=opts.framework;if(opts.clearTags)body2.tags=[];else if(opts.tag.length>0)body2.tags=opts.tag;if(Object.keys(body2).length===1)throw new CliError("usage","nothing to change");const repo=await createClient().patch(reposPath(org),body2);if(resolveOutput(command)==="json")printJson(repo);else console.log(`Updated ${repo.name} (${repo.url})`)});repos.command("resync <url>").description("Re-fetch a repository's GitHub metadata").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const result=await createClient().post(`${reposPath(org)}/resync`,{url});if(output==="json"){printJson(result);return}if(!result.synced){throw new CliError("internal","GitHub metadata sync failed (is the URL a public GitHub repository?)")}console.log(`Re-synced ${result.repo.name} (${result.repo.url})`)});repos.command("rm <url>").description("Remove a repository from the catalog").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);await createClient().delete(reposPath(org),{url});if(output!=="json")console.log(`Removed ${url}`)})}var run_exports={};__export(run_exports,{register:()=>register29});import{readFileSync as readFileSync10,statSync as statSync3}from"fs";function readCliRules(path){if(path==="-")throw new CliError("usage","--cli-rules expects a JSON file path, not stdin");try{const info=statSync3(path);if(!info.isFile())throw new Error("Expected a regular JSON file");if(info.size>CLI_RULE_LIMITS.bundleBytes)throw new Error("CLI rules file exceeds 2 MiB");return cliRuleAuthorInputSchema.parse(JSON.parse(readFileSync10(path,"utf8")))}catch(error){throw new CliError("usage",`Invalid --cli-rules file: ${error instanceof Error?error.message:error}`)}}import{readFileSync as readFileSync13,existsSync as existsSync4,mkdirSync as mkdirSync2,writeFileSync as writeFileSync4}from"fs";import{z as z15}from"zod";import{join as join11,relative as relative5}from"path";import{z as z8}from"zod";var preferenceDefinitionSchema=z8.discriminatedUnion("kind",[z8.object({kind:z8.literal("open-ended"),branded:z8.boolean().default(false)}).strict(),z8.object({kind:z8.literal("head-to-head"),brandA:z8.string().trim().min(1),brandB:z8.string().trim().min(1)}).strict().refine(p=>p.brandA.toLowerCase()!==p.brandB.toLowerCase(),"A head-to-head needs two different brands")]);var preferenceSnapshotSchema=z8.object({version:z8.literal(1),preference:preferenceDefinitionSchema}).strict();var headToHeadSchema=z8.object({kind:z8.literal("HEAD_TO_HEAD"),assessability:z8.enum(["assessed","partial","unassessable"]).optional(),selectionStatus:z8.enum(["measured","not_measured","unassessable"]).optional(),winner:z8.enum(["A","B","both","neither"]),brandA:z8.string(),brandB:z8.string(),rationale:z8.string(),brandAReasoning:z8.string(),brandBReasoning:z8.string(),brandAWhyRaised:z8.string().optional(),brandBWhyRaised:z8.string().optional()});var recommendPickSchema=z8.object({brand:z8.string(),rank:z8.number().int(),adopted:z8.boolean(),reasoning:z8.string().optional(),whyRaised:z8.string().optional()});var recommendSchema=z8.object({kind:z8.literal("RECOMMEND"),selectedBrands:z8.array(z8.string()).optional(),assessability:z8.enum(["assessed","partial","unassessable"]).optional(),selectionStatus:z8.enum(["measured","not_measured","unassessable"]).optional(),picks:z8.array(recommendPickSchema).default([]),singlePick:z8.boolean(),complianceNote:z8.string()});var visibilityVerdictSchema=z8.discriminatedUnion("kind",[headToHeadSchema,recommendSchema]);function preferenceSelectionLabel(verdict){if(verdict.assessability==="unassessable"||verdict.selectionStatus==="unassessable")return"Not assessable";if(verdict.kind==="HEAD_TO_HEAD")return verdict.winner==="A"?verdict.brandA:verdict.winner==="B"?verdict.brandB:verdict.winner==="both"?`${verdict.brandA} and ${verdict.brandB}`:"Neither";if(verdict.selectedBrands!==void 0)return verdict.selectedBrands.length?verdict.selectedBrands.join(", "):"No selection";if(!verdict.singlePick)return verdict.picks.length?"No clear selection":"No selection";return verdict.picks.find(pick=>pick.rank===1)?.brand??"No selection"}import{Option as Option5}from"commander";import{readFileSync as readFileSync11}from"fs";import{z as z9}from"zod";function assertRequiredExperiment(spec,required){if(required.length===0){throw new CliError("invalid_request","--require-experiment requires a nonempty label")}const mismatches=[];for(const[promptIndex,prompt]of spec.prompts.entries()){const promptExperiment=prompt.experiment??spec.experiment;const schedules=prompt.schedules??[{}];if(schedules.length===0){mismatches.push(`prompts.${promptIndex}.schedules`)}for(const[scheduleIndex,schedule]of schedules.entries()){if((schedule.experiment??promptExperiment)!==required){mismatches.push(`prompts.${promptIndex}.schedules.${scheduleIndex}`)}}}if(mismatches.length>0){throw new CliError("invalid_request",`--require-experiment expected every target to resolve to ${JSON.stringify(required)}; mismatched: ${mismatches.join(", ")}`)}}var EXAMPLE_SPEC={version:1,expand:true,experiment:"my-experiment",defaults:{agents:["CLAUDE_CODE"],samplesPerCycle:3},prompts:[{key:"add-healthcheck",text:"Add a /health endpoint that returns 200 OK."},{key:"migrate-to-vitest",text:"Migrate the test suite from Jest to Vitest.",schedules:[{repoUrl:"https://github.com/acme/api",repoRef:"main",model:"claude-opus-4-8"}]}]};async function readSpecJson(source2){let raw;if(source2==="-"){if(process.stdin.isTTY)throw new CliError("usage","--apply - needs a run spec piped to stdin");const chunks2=[];for await(const chunk of process.stdin)chunks2.push(chunk);raw=Buffer.concat(chunks2).toString("utf8")}else{try{raw=readFileSync11(source2,"utf8")}catch(e){throw new CliError("usage",`cannot read spec file ${source2}: ${e instanceof Error?e.message:String(e)}`)}}try{return JSON.parse(raw)}catch(e){throw new CliError("invalid_request",`spec is not valid JSON: ${e instanceof Error?e.message:String(e)}`)}}function parseSpec(parsed){const result=batteries_exports.batterySpecSchema.safeParse(parsed);if(!result.success){const details=result.error.issues.map(i=>` ${i.path.join(".")||"(spec)"}: ${i.message}`).join("\n");throw new CliError("invalid_request",`not a valid battery spec:
999
+ Default org set to ${account.organization.slug}. You're ready:`);console.log(" gauge status # the org at a glance");console.log(" gauge eval list # eval sets"," gauge preference list # preference prompts");console.log(" gauge run --eval <id> # launch one now");let inRepo=false;try{inRepo=gaugeState(command).inRepo}catch{}console.log(inRepo?" gauge init --hosted # connect this repo":" gauge init --hosted # connect a repo (run it inside the repo)")})}var optimizations_exports={};__export(optimizations_exports,{checkForkBefore:()=>checkForkBefore,fileEditFromOpts:()=>fileEditFromOpts,fileSourceFromOpts:()=>fileSourceFromOpts,latestLaunch:()=>latestLaunch,launchSettled:()=>launchSettled,parseEdits:()=>parseEdits,parseForkBefore:()=>parseForkBefore,parsePositiveSeq:()=>parsePositiveSeq,parseRestoreBefore:()=>parseRestoreBefore,parseStatus:()=>parseStatus,parseSubjectRef:()=>parseSubjectRef,printPageView:()=>printPageView,readJson:()=>readJson2,readText:()=>readText,register:()=>register23});import{readFileSync as readFileSync9,writeFileSync as writeFileSync2}from"fs";var STATUSES2=["OPEN","DONE","ARCHIVED"];var TERMINAL_RUN=new Set(["SUCCEEDED","FAILED","TIMED_OUT","CANCELED"]);function basePath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/optimizations`}function itemPath(org,id2){return`${basePath(org)}/${encodeURIComponent(id2)}`}function readText(path){return readFileSync9(path==="-"?0:path,"utf8")}function readJson2(path,read2=readText){try{return JSON.parse(read2(path))}catch{throw new CliError("usage",`${path} must contain valid JSON`)}}function sleep3(ms){return new Promise(resolve5=>setTimeout(resolve5,ms))}function plural(n,noun){return`${n} ${noun}${n===1?"":"s"}`}function parseCount(raw,flag2,max){if(raw===void 0)return void 0;const n=Number(raw);if(!Number.isInteger(n)||n<1||n>max)throw new CliError("usage",`${flag2} must be a positive integer no greater than ${max}`);return n}function parseStatus(raw){if(!raw)return void 0;const status=raw.trim().toUpperCase();if(!STATUSES2.includes(status))throw new CliError("usage",`unknown status "${raw}" (expected ${STATUSES2.join(", ")})`);return status}function parseSubjectRef(opts){if([opts.issue,opts.finding,opts.goal].filter(Boolean).length!==1)throw new CliError("usage","pass one of --issue <id>, --finding <id> --run <runId>, or --goal <text>");if(opts.run&&!opts.finding)throw new CliError("usage","--run only applies with --finding");if(opts.issue)return{kind:"issue",issueId:opts.issue};if(opts.finding){if(!opts.run)throw new CliError("usage","--finding needs --run <runId>");return{kind:"finding",observationId:opts.finding,runId:opts.run}}return{kind:"free",text:opts.goal}}function fileEditFromOpts(opts,read2=readText){if([opts.page,opts.skill,opts.cli].filter(Boolean).length!==1)throw new CliError("usage","pass one of --page <url>, --skill <name> --path <path>, or --cli <package>");if(opts.delete&&!opts.skill)throw new CliError("usage","--delete removes one path from a skill");if(opts.path&&!opts.skill)throw new CliError("usage","--path only applies with --skill");if(opts.contentType&&!opts.page)throw new CliError("usage","--content-type only applies with --page");if(opts.delete&&opts.from)throw new CliError("usage","pass --delete or --from, not both");if(!opts.delete&&!opts.from)throw new CliError("usage","--from <file> is required ('-' for stdin)");const from=opts.from;if(opts.page)return{kind:"page",url:opts.page,body:read2(from),...opts.contentType?{contentType:opts.contentType}:{}};if(opts.skill){if(!opts.path)throw new CliError("usage","--skill needs --path <path inside the skill>");return{kind:"skill",name:opts.skill,path:opts.path,body:opts.delete?null:read2(from)}}let rules;try{rules=JSON.parse(read2(from))}catch{throw new CliError("usage","--from must contain CLI rules as JSON")}return{kind:"cli",package:opts.cli,rules}}function fileSourceFromOpts(opts){if([opts.page,opts.skill,opts.cli].filter(Boolean).length!==1)throw new CliError("usage","pass one of --page <url>, --skill <name>, or --cli");if(opts.page)return{kind:"page",url:opts.page};if(opts.skill)return{kind:"skill",name:opts.skill};return{kind:"cli"}}function latestLaunch(launches){return launches.reduce((latest,launch2)=>!latest||launch2.number>latest.number?launch2:latest,void 0)}function launchSettled(launch2){return launch2.runs.every(run=>TERMINAL_RUN.has(run.status)&&(run.status!=="SUCCEEDED"||run.analyzed))}function subjectLine(subject){switch(subject.kind){case"issue":return`issue "${subject.title}" (${subject.issueId})`;case"finding":return`finding "${subject.label}" on run ${subject.runId}`;case"free":return`goal "${subject.text}"`}}function toRow2(o){return{id:o.id,title:o.title.length>48?`${o.title.slice(0,45)}...`:o.title,status:o.status,subject:o.subject.kind,changed:o.changed,launches:o.launches,updated:o.updatedAt}}function fileRow(key,file,change){return{key,change:change??"",size:file.kind==="page"?`${file.body.length} chars`:file.kind==="skill"?plural(Object.keys(file.files).length,"file"):plural(file.rules.rules.length,"rule")}}function launchProgress(launch2){const done2=launch2.runs.filter(run=>TERMINAL_RUN.has(run.status));const analyzed=launch2.runs.filter(run=>run.analyzed);return`${done2.length}/${launch2.runs.length} finished, ${analyzed.length} analyzed`}function launchRow(launch2){return{id:launch2.id,number:launch2.number,reruns:launchProgress(launch2),changed:launch2.changed.length,note:launch2.note??"",created:launch2.createdAt}}function printDetail2(o){console.log(`id: ${o.id}`);console.log(`title: ${o.title}`);console.log(`status: ${o.status}`);console.log(`subject: ${subjectLine(o.subject)}`);const recorded=o.recordedSourceRunIds?new Set(o.recordedSourceRunIds):null;console.log(`sources: ${o.sourceRunIds.map(id2=>recorded?`${id2} (${recorded.has(id2)?"recorded":"no recording"})`:id2).join(", ")||"(none)"}`);const changes=new Map(o.changes.map(c=>[c.key,c.status]));const files=Object.entries(o.head.files);console.log(`files: ${files.length?"":"(empty tree)"}`);if(files.length)printItems(files.map(([key,file])=>fileRow(key,file,changes.get(key))),"table");for(const[key,change]of changes)if(change==="removed")console.log(` ${key}: removed`);console.log(`launches: ${o.launchList.length?"":"(none)"}`);if(o.launchList.length)printItems(o.launchList.map(launchRow),"table");if(o.result)console.log(`result: ${o.result.summary}`)}function printFile(file,path){if(file.kind==="page")return void console.log(file.body);if(file.kind==="cli")return printJson(file.rules);if(path){const body2=file.files[path];if(body2===void 0)throw new CliError("not_found",`no ${path} in skill ${file.name}`);return void console.log(body2)}for(const[name,body2]of Object.entries(file.files)){console.log(`=== ${name}`);console.log(body2)}}function printLaunch(launch2){console.log(`launch ${launch2.number} (${launch2.id})${launch2.note?`: ${launch2.note}`:""}`);console.log(`changed: ${launch2.changed.join(", ")||"(nothing)"}`);for(const run of launch2.runs){console.log(`
1000
+ ${run.id} rerun of ${run.sourceRunId??"?"}: ${run.status}, ${run.agent} ${run.model}`);if(run.forkedAfter!=null)console.log(` forked after seq ${run.forkedAfter}; cost and time cover only the part after the fork`);if(run.restoredFrom)console.log(` restored at seq ${run.restoredFrom.seq} (${run.restoredFrom.checkpointId}); cost and time cover only the part after the restore`);if(!run.findings){console.log(" not analyzed");continue}const now=new Set(run.findings.map(f=>f.label));const before=new Set(run.sourceFindings.map(f=>f.label));for(const f of run.sourceFindings)console.log(` ${now.has(f.label)?"returned":"resolved"} ${f.label}`);for(const f of run.findings)if(!before.has(f.label))console.log(` introduced ${f.label}`)}}function parseEdits(value){const ok=Array.isArray(value)&&value.length>0&&value.every(edit=>edit&&typeof edit==="object"&&typeof edit.find==="string"&&edit.find.length>0&&typeof edit.replace==="string");if(!ok)throw new CliError("usage","--edits must be a JSON array of {find, replace} with non-empty find");return value.map(({find,replace})=>({find,replace}))}function printWrite(res){if(!res.change){console.log(`Wrote ${res.key}`);return}const{linesBefore,linesAfter,retainedPercent,hunks}=res.change;console.log(`Wrote ${res.key}: ${linesBefore} \u2192 ${linesAfter} lines, ${retainedPercent??"\u2014"}% of the original kept`);console.log(hunks)}function printPageView(view){const content=view.text??view.raw;if(content!==void 0){console.log(content);return}console.log(`${view.matches??0} matches for ${JSON.stringify(view.query)}`);for(const excerpt of view.excerpts??[]){console.log(`=== offset ${excerpt.offset}`);console.log(excerpt.raw)}if(view.truncated)console.log("\u2026 more matches; search a longer phrase");if(view.hint)console.log(view.hint)}function parsePositiveSeq(raw){const seq=Number(raw);return/^\d+$/.test(raw)&&seq>=1&&Number.isSafeInteger(seq)?seq:null}function parseForkBefore(raw){const at=raw.lastIndexOf(":");const seq=parsePositiveSeq(raw.slice(at+1));if(at<=0||seq===null)throw new CliError("usage",`--fork-before takes <sourceRunId>:<seq>, e.g. run_1:34 (got ${raw})`);return{runId:raw.slice(0,at),seq}}function checkForkBefore(forkBefore,opts){const seen=new Set;for(const{runId}of forkBefore){if(seen.has(runId))throw new CliError("usage",`--fork-before names ${runId} twice`);seen.add(runId);if(opts.run?.length&&!opts.run.includes(runId))throw new CliError("usage",`--fork-before names ${runId}, but --run leaves it out`);if(opts.fresh===true||opts.fresh?.includes(runId))throw new CliError("usage",`--fork-before and --fresh both name ${runId}; pick one`)}}function parseRestoreBefore(raw){const at=raw.lastIndexOf(":");const rerunId=raw.slice(0,at);const seq=raw.slice(at+1);if(at<=0||!/^-?\d+$/.test(seq))throw new CliError("usage",`--restore-before takes <rerunId>:<seq>, e.g. run_1:140 (got ${raw})`);return{rerunId,seq:Number(seq)}}function checkpointRow(cp){const exposure=[...cp.exposure.surfaces,...cp.exposure.skillChanged?["skill changed"]:[],...cp.exposureIncomplete?["trace incomplete"]:[]];return{checkpoint:cp.checkpointId,source:cp.sourceRunId,rerun:cp.rerunId,seq:`${cp.seq}/${cp.rerunEventCount}`,exposure:exposure.join(", ")||"clean","last step":cp.lastSteps.at(-1)??"",captured:cp.capturedAt}}function printLaunchResult(id2,res){console.log(`Launched ${plural(res.batches.flatMap(b=>b.runIds).length,"rerun")} (${res.launchId}) with ${plural(res.changed.length,"changed file")}:`);for(const key of res.changed)console.log(` ${key}`);for(const batch of res.batches){const from=batch.restoredFrom;console.log(batch.forkedAfter!=null?`${batch.sourceRunId}: forked after seq ${batch.forkedAfter}`:from?`${batch.sourceRunId}: restored at seq ${from.seq} of rerun ${from.rerunId} (${from.checkpointId})`:`${batch.sourceRunId}: fresh`);for(const warning of batch.warnings)console.log(` ! ${warning}`)}console.log(`Follow with \`gauge optimizations watch ${id2}\`.`)}function register23(program2){const group=hostedOnly(program2.command("optimizations")).description("Hill climb your agent surfaces: edit the pages, skills, and CLI rules agent sessions used, rerun those sessions, and compare findings");group.command("list").description("List the org's optimizations, newest activity first").option("--status <status>",STATUSES2.join(", ")).option("--limit <n>","max rows (default 50)").action(async(opts,cmd)=>{const res=await createClient().get(basePath(await resolveOrg(cmd)),{status:parseStatus(opts.status),limit:parseCount(opts.limit,"--limit",200)});if(resolveOutput(cmd)==="json")printJson(res.items);else printItems(res.items.map(toRow2),"table")});group.command("create").description("Open an optimization on an issue, one finding, or a goal; the tree starts empty").option("--issue <id>","an issue id from `gauge issues list`").option("--finding <id>","a finding id from `gauge run insight <run> -o json`; needs --run").option("--run <runId>","the run the finding is on").option("--goal <text>","free text; starts with no sessions, attach them with `sessions add`").option("--title <title>","name (default: from the subject)").action(async(opts,cmd)=>{const detail=await createClient().post(basePath(await resolveOrg(cmd)),{subject:parseSubjectRef(opts),...opts.title?{title:opts.title}:{}});if(resolveOutput(cmd)==="json")printJson(detail);else console.log(detail.sourceRunIds.length?`Opened ${detail.id} "${detail.title}" on ${plural(detail.sourceRunIds.length,"source session")}. Next: \`gauge optimizations authoring ${detail.id}\`.`:`Opened ${detail.id} "${detail.title}" with no sessions. Next: \`gauge optimizations sessions add ${detail.id} <runId...>\`.`)});group.command("get <id>").description("One optimization: subject, source runs, tree, and launches").action(async(id2,_opts,cmd)=>{const detail=await createClient().get(itemPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(detail);else printDetail2(detail)});group.command("authoring <id>").description("Authoring guidance: the source sessions' findings, the tree, and the editing practices to follow").action(async(id2,_opts,cmd)=>{const res=await createClient().get(`${itemPath(await resolveOrg(cmd),id2)}/authoring`);if(resolveOutput(cmd)==="json")printJson(res);else console.log(res.guidance)});group.command("sessions").description("Attach sessions an optimization can rerun").command("add <id> <runIds...>").description("Attach sessions to rerun, up to 12 in all; a goal starts with none. Optimization reruns cannot be sources").action(async(id2,runIds,_opts,cmd)=>{const res=await createClient().post(`${itemPath(await resolveOrg(cmd),id2)}/sessions`,{runIds});if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Attached ${plural(res.added.length,"session")}; ${id2} has ${plural(res.sourceRunIds.length,"source session")}.`)});const file=group.command("file").description("Read and edit the files in an optimization's tree");file.command("read <id> <key>").description("Print one file by tree key: a page's exact source (--text for readable text, --query for the source around a match), a skill (or one --path in it), or CLI rules").option("--text","page only: readable text instead of the source").option("--query <text>","page only: the exact source around each match, to copy into file edit").option("--path <path>","one file inside a skill").option("--out <file>","save the page source or skill --path to a file").action(async(id2,key,opts,cmd)=>{const path=`${itemPath(await resolveOrg(cmd),id2)}/files`;if(opts.text||opts.query){if(opts.out)throw new CliError("usage","--out saves the stored file only");const view=await createClient().get(path,{key,...opts.query?{query:opts.query}:{view:"text"}});if(resolveOutput(cmd)==="json")printJson(view);else printPageView(view);return}const res=await createClient().get(path,{key});if(opts.out){const body2=res.kind==="page"?res.body:res.kind==="skill"&&opts.path?res.files[opts.path]:void 0;if(body2===void 0)throw new CliError("usage","--out saves a page source or one skill --path");writeFileSync2(opts.out,body2);console.error(`Saved ${opts.out}`)}else if(resolveOutput(cmd)==="json")printJson(res);else printFile(res,opts.path)});file.command("edit <id>").description("Edit a page in the tree with find/replace on its exact source; every byte outside the edits stays. Each find must appear once; all edits apply or none do").requiredOption("--page <url>","the page URL").requiredOption("--edits <file>","JSON [{find, replace}]; copy find text from `file read --query`; '-' for stdin").action(async(id2,opts,cmd)=>{const res=await createClient().patch(`${itemPath(await resolveOrg(cmd),id2)}/files`,{url:opts.page,edits:parseEdits(readJson2(opts.edits))});if(resolveOutput(cmd)==="json")printJson(res);else printWrite(res)});file.command("add <id>").description("Copy an original into the tree before you edit it: a page a source session read (else the live page), a staged skill, or the CLI rules").option("--page <url>","a page by URL").option("--skill <name>","a skill a source session staged").option("--cli","the CLI rules a source session ran with").action(async(id2,opts,cmd)=>{const res=await createClient().post(`${itemPath(await resolveOrg(cmd),id2)}/files`,fileSourceFromOpts(opts));if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Added ${res.key} (from ${res.origin})`)});file.command("write <id>").description("Write a new page or rewrite a whole one, one skill file, or the CLI rules; to change part of a page, use file edit").option("--page <url>","the page URL; --from holds the complete body").option("--content-type <type>","the page content type (default: keep)").option("--skill <name>","the skill; needs --path").option("--path <path>","the file inside the skill, e.g. SKILL.md").option("--delete","remove --path from the skill").option("--cli <package>","the CLI package; --from holds {schemaVersion: 3, target, rules, shippingIntent}").option("--from <file>","the new content; '-' for stdin").action(async(id2,opts,cmd)=>{const res=await createClient().put(`${itemPath(await resolveOrg(cmd),id2)}/files`,fileEditFromOpts(opts));if(resolveOutput(cmd)==="json")printJson(res);else printWrite(res)});file.command("rm <id> <key>").description("Drop a file from the tree; reruns use what the source session had").action(async(id2,key,_opts,cmd)=>{const res=await createClient().delete(`${itemPath(await resolveOrg(cmd),id2)}/files`,{key});if(resolveOutput(cmd)==="json")printJson(res);else console.log(`Removed ${res.removed}`)});group.command("notes <id>").description("Print the working notes, or replace them with --from: markdown the user reads beside the chat").option("--from <file>","the complete notes; '-' for stdin").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();const detail=opts.from?await client.patch(path,{notes:readText(opts.from)}):await client.get(path);if(resolveOutput(cmd)==="json")printJson({notes:detail.notes});else if(opts.from)console.log(`Saved notes (${detail.notes.length} chars)`);else console.log(detail.notes||"(no notes)")});group.command("launch <id>").description("Rerun the source sessions with the tree injected. A recorded run forks: it repeats the source's recorded steps up to its last tool call before the first changed surface, then continues live. Other runs restore from the deepest checkpoint before any changed surface, or start fresh").option("--run <runId...>","rerun only these source runs").option("--note <text>","one line on what this launch tests").option("--fork-before <sourceRunId:seq...>","fork this recorded source run at its last tool call before this seq").option("--restore-before <rerunId:seq...>","restore from the deepest checkpoint at or before this seq of an earlier rerun").option("--checkpoint <checkpointId...>","restore from this checkpoint (see `checkpoints`)").option("--fresh [sourceRunId...]","start from zero, with no fork and no restore: these source runs, or every one").option("--yes","skip the confirmation").action(async(id2,opts,cmd)=>{const restoreAt=[...(opts.checkpoint??[]).map(checkpointId=>({checkpointId})),...(opts.restoreBefore??[]).map(parseRestoreBefore)];const forkBefore=(opts.forkBefore??[]).map(parseForkBefore);checkForkBefore(forkBefore,opts);const path=itemPath(await resolveOrg(cmd),id2);await confirmOrAbort(`Rerun ${opts.run?.length?plural(opts.run.length,"source session"):"every source session"} of ${id2} with the tree?`,opts.yes);const client=createClient();const fresh=opts.fresh===true?opts.run??(await client.get(path)).sourceRunIds:opts.fresh??[];const res=await client.post(`${path}/launch`,{...opts.run?.length?{runIds:opts.run}:{},...opts.note?{note:opts.note}:{},...fresh.length?{restore:Object.fromEntries(fresh.map(runId=>[runId,"none"]))}:{},...restoreAt.length?{restoreAt}:{},...forkBefore.length?{forkBefore}:{}});if(resolveOutput(cmd)==="json")printJson(res);else printLaunchResult(id2,res)});group.command("checkpoints <id>").description("Checkpoints captured on fresh reruns, with how far in each is and whether its prefix saw a changed surface").option("--run <sourceRunId>","only this source run").action(async(id2,opts,cmd)=>{const res=await createClient().get(`${itemPath(await resolveOrg(cmd),id2)}/checkpoints`,{sourceRunId:opts.run});if(resolveOutput(cmd)==="json")printJson(res.items);else if(!res.items.length)console.log("(no checkpoints)");else printItems(res.items.map(checkpointRow),"table")});group.command("launches <id> [launchId]").description("List launches, or one launch's reruns with their findings beside the source findings").action(async(id2,launchId,_opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();if(!launchId){const detail=await client.get(path);if(resolveOutput(cmd)==="json")printJson(detail.launchList);else printItems(detail.launchList.map(launchRow),"table");return}const launch2=await client.get(`${path}/launches/${encodeURIComponent(launchId)}`);if(resolveOutput(cmd)==="json")printJson(launch2);else printLaunch(launch2)});group.command("watch <id>").description("Wait until the latest launch's reruns finish and are analyzed, then print them").option("--interval <seconds>","poll interval (default 15)").option("--timeout <minutes>","give up after this long (default 60)").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const client=createClient();const interval=(parseCount(opts.interval,"--interval",600)??15)*1e3;const timeoutMinutes=parseCount(opts.timeout,"--timeout",1440)??60;const deadline=Date.now()+timeoutMinutes*6e4;let last="";for(;;){const detail=await client.get(path);const latest=latestLaunch(detail.launchList);if(!latest)throw new CliError("usage",`${id2} has no launches yet`);if(launchSettled(latest)){const launch2=await client.get(`${path}/launches/${encodeURIComponent(latest.id)}`);if(resolveOutput(cmd)==="json")printJson(launch2);else printLaunch(launch2);return}const progress=launchProgress(latest);if(progress!==last)console.error(`launch ${latest.number}: ${progress}`);last=progress;if(Date.now()>=deadline)throw new CliError("network",`Timed out after ${timeoutMinutes} minutes (launch ${latest.number}: ${progress}). Re-run watch, or check \`gauge optimizations launches ${id2} ${latest.id}\`.`);await sleep3(interval)}});group.command("finish <id>").description("Close the optimization with its result: summary, changed keys, and findings resolved, returned, and introduced").requiredOption("--from <file>","result JSON: {summary, changed, resolved, returned, introduced}").option("--yes","skip the confirmation").action(async(id2,opts,cmd)=>{const path=itemPath(await resolveOrg(cmd),id2);const result=readJson2(opts.from);await confirmOrAbort(`Finish ${id2}? Its tree can no longer change.`,opts.yes);const detail=await createClient().post(`${path}/finish`,result);if(resolveOutput(cmd)==="json")printJson(detail);else console.log(`Finished ${detail.id} "${detail.title}"`)})}var org_exports={};__export(org_exports,{register:()=>register24});function printSettings(s,billingStatus){console.log(`slug: ${s.slug}`);console.log(`name: ${s.name}`);console.log(`website: ${s.website??"(none)"}`);console.log(`max spend/run: ${s.maxSpendUsd==null?"(server default)":`$${s.maxSpendUsd}`}`);console.log(`overage: ${fmtOverage(billingStatus)}`);console.log(`auto-join: ${s.autoJoinDomain??"off"}`)}function register24(program2){const group=hostedOnly(program2.command("org")).description("This org's settings, read-only (see `gauge orgs` for the org list)");group.command("get").description("Show the org's settings (edit them in the web app)").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const client=createClient();const base=`/api/v1/orgs/${encodeURIComponent(org)}`;const[settings,billingStatus]=await Promise.all([client.get(base),client.get(`${base}/billing`)]);if(resolveOutput(cmd)==="json")printJson({...settings,paymentType:billingStatus.paymentType??null,creditLine:billingStatus.creditLine??null});else printSettings(settings,billingStatus)})}var orgs_exports={};__export(orgs_exports,{register:()=>register25});import{writeFileSync as writeFileSync3}from"fs";import{join as join8}from"path";function register25(program2){const orgs=hostedOnly(program2.command("orgs"),{account:true}).description("Organizations, and which Gauge products each one uses");orgs.command("list",{isDefault:true}).description("List your Gauge organizations (* marks the saved default)").action(async(_opts,command)=>{const rows=await fetchDirectory();const{orgId}=readConfig();if(resolveOutput(command)==="json"){printJson(rows.map(row2=>({...row2,isDefault:row2.id===orgId})));return}printItems(rows.map(row2=>({default:row2.id===orgId?"*":"",slug:row2.slug,name:row2.name,products:productsLabel(row2.products)})),"table")});orgs.command("create <name>").description("Create a Gauge Agents workspace and set its organization as the default").option("--website <url>","company site (used as the logo source)").option("--no-use","don't set the new org as the default").action(async(name,opts,command)=>{await fetchDirectory();const created=await createClient().post("/api/v1/orgs",{name,website:opts.website});const org=await createdOrganization(created.slug);if(opts.use!==false)updateConfig({orgId:org.id,defaultOrg:void 0});if(resolveOutput(command)==="json")return printJson(org);console.log(`Created workspace ${org.name} (${org.slug})`);if(opts.use!==false)console.log(`Default org set to ${org.slug}`)});orgs.command("use <org>").description("Select a Gauge organization by its public slug").option("--project","save the selection in this project's gauge.json").action(async(value,opts,command)=>{const org=await findOrganization(value);if(opts.project){const state=gaugeState(command);if(!state.root||!state.config)throw new CliError("usage","No valid gauge.json found. Run `gauge init` first.");const{org:_legacyOrg,...config}=state.config;writeFileSync3(join8(state.root,"gauge.json"),`${JSON.stringify({...config,orgId:org.id},null,2)}
1001
+ `)}else updateConfig({orgId:org.id,defaultOrg:void 0});if(resolveOutput(command)==="json")return printJson({orgId:org.id,slug:org.slug,name:org.name});console.log(`${opts.project?"Project":"Default"} org set to ${org.name} (${org.slug})`)})}var personas_exports={};__export(personas_exports,{register:()=>register26});function personasPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/personas`}function register26(program2){const group=hostedOnly(program2.command("personas")).description("Manage simulated-user personas for eval and preference runs");group.command("list").description("List the org's personas").action(async(_opts,cmd)=>{const org=await resolveOrg(cmd);const res=await createClient().get(personasPath(org));if(resolveOutput(cmd)==="json")printJson(res.items);else printItems(res.items.map(p=>({id:p.id,name:p.name,description:p.description.length>60?`${p.description.slice(0,57)}...`:p.description})),"table")});group.command("get <id>").description("Show one persona").action(async(id2,_opts,cmd)=>{const org=await resolveOrg(cmd);const persona=await createClient().get(`${personasPath(org)}/${encodeURIComponent(id2)}`);if(resolveOutput(cmd)==="json"){printJson(persona);return}console.log(`id: ${persona.id}`);console.log(`name: ${persona.name}`);console.log(`model: ${persona.model}`);console.log(`description: ${persona.description}`)});group.command("add").description("Create a persona").requiredOption("--name <name>","persona name").requiredOption("--description <text>",'memory-style notes ("The user is a solo founder\u2026")').action(async(opts,cmd)=>{const org=await resolveOrg(cmd);const created=await createClient().post(personasPath(org),{name:opts.name,description:opts.description});if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Created persona ${created.id} (${created.name})`)});group.command("edit <id>").description("Update a persona").option("--name <name>","rename the persona").option("--description <text>","replace the notes").action(async(id2,opts,cmd)=>{const org=await resolveOrg(cmd);if(opts.name===void 0&&opts.description===void 0)throw new CliError("usage","nothing to change");const updated=await createClient().patch(`${personasPath(org)}/${encodeURIComponent(id2)}`,{name:opts.name,description:opts.description});if(resolveOutput(cmd)==="json")printJson(updated);else console.log(`Updated persona ${updated.id}`)});group.command("rm <id>").description("Delete a persona (run configurations fall back to the default persona)").action(async(id2,_opts,cmd)=>{const org=await resolveOrg(cmd);await createClient().delete(`${personasPath(org)}/${encodeURIComponent(id2)}`);if(resolveOutput(cmd)!=="json")console.log(`Deleted persona ${id2}`)})}var preference_exports={};__export(preference_exports,{register:()=>register27});function visibilityPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/visibility-prompts`}function promptPath(org,id2){return`${visibilityPath(org)}/${encodeURIComponent(id2)}`}function scenariosPath(org,id2){return`${promptPath(org,id2)}/scenarios`}function scenarioRow(scenario){return{id:scenario.id,repo:scenario.repoUrl??"(empty project)",ref:scenario.repoRef??"",persona:scenario.profile?.name??"(default)"}}var KIND_CHOICES=["head-to-head","open-ended"];function parseKind(raw){const upper=raw==="open-ended"?"RECOMMEND":raw.trim().toUpperCase().replace(/-/g,"_");if(upper!=="HEAD_TO_HEAD"&&upper!=="RECOMMEND")throw new CliError("usage",`invalid --kind "${raw}" (expected: ${KIND_CHOICES.join(" | ")})`);return upper}function brandsLabel(v){if(v.kind==="HEAD_TO_HEAD")return`${v.brandA} vs ${v.brandB}`;return v.branded?v.brandA??"branded":"organic"}function preferencePromptRow(v){return{id:v.id,name:v.name,kind:v.kind==="RECOMMEND"?"open-ended":"head-to-head",brands:brandsLabel(v),market:v.market?.slug??"",tags:v.tags.join(", "),runs:v.runCount,"last run":v.lastRunAt??"-","created by":creatorLabel(v.createdBy)}}function printDetail3(v){console.log(`id: ${v.id}`);console.log(`name: ${v.name}`);console.log(`kind: ${v.kind==="RECOMMEND"?"open-ended":"head-to-head"}`);console.log(`brands: ${brandsLabel(v)}`);console.log(`market: ${v.market?`${v.market.name} (${v.market.slug})`:"(none)"}`);console.log(`tags: ${v.tags.join(", ")||"(none)"}`);console.log(`runs: ${v.runCount} (last: ${v.lastRunAt??"never"})`);console.log(`creator: ${creatorLabel(v.createdBy)}`);console.log(`prompt: ${v.promptText}`);console.log(`cadence: ${cadenceLabel(v.cadence)} \u2014 ${v.sampleCount} sample(s)`);console.log(`agents: ${v.agents.map(a=>a.models.length?`${a.agent}:${a.models.join(",")}`:a.agent).join(", ")}`);console.log(`skills: ${v.skillRefs.join(", ")||"(none)"}`);console.log(`mcp: ${v.mcpRefs.join(", ")||"(none)"}`);console.log(`conns: ${v.connectionIds.join(", ")||"(none)"}`);console.log(`scenarios: ${v.scenarios.length}`);for(const scenario of v.scenarios)console.log(` [${scenario.id}] ${scenario.repoUrl??"(empty project)"}${scenario.repoRef?`@${scenario.repoRef}`:""} \u2014 ${scenario.profile?.name??"default persona"}`)}async function listPreferencePrompts(cmd){if(isLocal(cmd))return listLocalMeasurements(cmd,"preference");const res=await createClient().get(visibilityPath(await resolveOrg(cmd)));return res.items}async function getPreferencePrompt(cmd,id2){const vp=isLocal(cmd)?localMeasurementView(getLocalMeasurement(cmd,"preference",id2)):await createClient().get(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(vp);else printDetail3(vp)}async function createPreferencePrompt(cmd,opts){if(hasLifecycleFlags(opts)&&isLocal(cmd))throw new CliError("usage","Lifecycle scripts require hosted execution; pass --hosted");if(opts.kind===void 0)throw new CliError("usage",`--kind is required with --preference (${KIND_CHOICES.join(" | ")})`);if(opts.ref!==void 0&&opts.repo===void 0)throw new CliError("usage","--ref requires --repo");const hasScenario=opts.repo!==void 0||opts.persona!==void 0;const body2={name:opts.name,promptText:readPromptText(opts),kind:parseKind(opts.kind),branded:opts.branded,brandA:opts.brandA??null,brandB:opts.brandB??null,marketId:opts.market??null,tags:opts.tag.length>0?opts.tag:void 0,...hasRunSettingsFlags(opts)?runSettingsFromOpts(opts):{},scenarios:hasScenario?[{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:opts.persona??null}]:[]};const created=isLocal(cmd)?createLocalMeasurement(cmd,"preference",body2):await createClient().post(visibilityPath(await resolveOrg(cmd)),body2);if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Created preference prompt ${created.id}`)}async function editPreferencePrompt(cmd,id2,opts){if(hasLifecycleFlags(opts)&&isLocal(cmd))throw new CliError("usage","Lifecycle scripts require hosted execution; pass --hosted");const lifecyclePath=hasLifecycleFlags(opts)&&!opts.clearLifecycle?promptPath(await resolveOrg(cmd),id2):void 0;const existing=lifecyclePath?await createClient().get(lifecyclePath):void 0;const body2={...runSettingsPatchFromOpts(opts,existing?.lifecycle)};if(opts.name!==void 0)body2.name=opts.name;if(opts.text!==void 0||opts.file!==void 0)body2.promptText=readPromptText(opts);if(opts.brandA!==void 0)body2.brandA=opts.brandA;if(opts.brandB!==void 0)body2.brandB=opts.brandB;if(opts.branded&&opts.organic)throw new CliError("usage","--branded and --organic conflict");if(opts.branded)body2.branded=true;else if(opts.organic)body2.branded=false;if(opts.market!==void 0&&opts.clearMarket)throw new CliError("usage","--market and --clear-market conflict");if(opts.clearMarket)body2.marketId=null;else if(opts.market!==void 0)body2.marketId=opts.market;if(opts.tag.length>0&&opts.clearTags)throw new CliError("usage","--tag and --clear-tags conflict");if(opts.clearTags)body2.tags=[];else if(opts.tag.length>0)body2.tags=opts.tag;if(Object.keys(body2).length===0)throw new CliError("usage","nothing to change");const updated=isLocal(cmd)?editLocalMeasurement(cmd,"preference",id2,body2):await createClient().patch(lifecyclePath??promptPath(await resolveOrg(cmd),id2),body2);if(resolveOutput(cmd)==="json")printJson(updated);else console.log(`Updated preference prompt ${updated.id}`)}async function removePreferencePrompt(cmd,id2){if(isLocal(cmd))removeLocalMeasurement(cmd,"preference",id2);else await createClient().delete(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)!=="json")console.log(`Deleted preference prompt ${id2}`)}async function listScenarios(cmd,id2){const vp=isLocal(cmd)?localMeasurementView(getLocalMeasurement(cmd,"preference",id2)):await createClient().get(promptPath(await resolveOrg(cmd),id2));if(resolveOutput(cmd)==="json")printJson(vp.scenarios);else printItems(vp.scenarios.map(scenarioRow),"table")}async function addScenario(cmd,id2,opts){if(opts.ref!==void 0&&opts.repo===void 0)throw new CliError("usage","--ref requires --repo");if(isLocal(cmd)){if(opts.persona)throw new CliError("usage","Personas require hosted Gauge");const row2=getLocalMeasurement(cmd,"preference",id2);const updated=editLocalMeasurement(cmd,"preference",id2,{scenarios:[...row2.scenarios,{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:null}]});const scenario=updated.scenarios.at(-1);if(resolveOutput(cmd)==="json")printJson(scenario);else console.log(`Added scenario ${scenario.id}`);return}const created=await createClient().post(scenariosPath(await resolveOrg(cmd),id2),{repoUrl:opts.repo??null,repoRef:opts.ref??null,profileId:opts.persona??null});if(resolveOutput(cmd)==="json")printJson(created);else console.log(`Added scenario ${created.id}`)}async function removeScenario(cmd,id2,scenarioId){if(isLocal(cmd)){const row2=getLocalMeasurement(cmd,"preference",id2);const scenarios=row2.scenarios;if(!scenarios.some(s=>s.id===scenarioId))throw new CliError("usage",`Unknown scenario: ${scenarioId}`);editLocalMeasurement(cmd,"preference",id2,{scenarios:scenarios.filter(s=>s.id!==scenarioId)});if(resolveOutput(cmd)!=="json")console.log(`Removed scenario ${scenarioId}`);return}await createClient().delete(`${scenariosPath(await resolveOrg(cmd),id2)}/${encodeURIComponent(scenarioId)}`);if(resolveOutput(cmd)!=="json")console.log(`Removed scenario ${scenarioId}`)}async function runPreferencePrompt(cmd,id2,opts){const org=await resolveOrg(cmd);const overrides={...runOverridesFromOpts(opts),...opts.cliRules?{cliRules:opts.cliRules}:{},...hasLifecycleFlags(opts)?lifecyclePatchFromOpts(opts,(await createClient().get(promptPath(org,id2))).lifecycle):{}};const settings=Object.keys(overrides).length>0?"these overrides (stored settings stay unchanged)":"the stored settings";await confirmOrAbort(opts.scenario.length>0?`Run ${opts.scenario.length} selected scenario${opts.scenario.length===1?"":"s"} with ${settings} now?`:`Run every scenario of this prompt with ${settings} now?`,opts.yes);const result=await createClient().post(`${promptPath(org,id2)}/run`,{visibilityScenarioIds:opts.scenario.length>0?opts.scenario:void 0,...overrides});if(resolveOutput(cmd)==="json")printJson(result);else{console.log(`Started ${result.runIds.length} run${result.runIds.length===1?"":"s"} across ${result.batchIds.length} scenario${result.batchIds.length===1?"":"s"}`);for(const batch of result.batchIds)console.log(`Runs: gauge run list --batch ${batch}`)}return result}async function listPreferencePromptRuns(cmd,id2,opts){const res=await createClient().get(`${promptPath(await resolveOrg(cmd),id2)}/runs`,{limit:opts.limit,cursor:opts.cursor});if(resolveOutput(cmd)==="json"){printJson(res);return}printItems(res.items.map(r=>({id:r.id,status:r.status,agent:r.agent,model:r.resolvedModel??r.model??"",source:r.triggeredBy?.source??"",credits:r.creditsCharged??"",created:r.createdAt,verdict:r.verdict?.status??"-"})),"table");if(res.nextCursor)console.error(`(more results \u2014 rerun with --cursor ${res.nextCursor})`)}async function markets(cmd,opts){if(isLocal(cmd))throw new CliError("usage","Markets require hosted Gauge. Pass --hosted.");const client=createClient();const base=`/api/v1/orgs/${encodeURIComponent(await resolveOrg(cmd))}/markets`;if(opts.add!==void 0){const market=await client.post(base,{marketId:opts.add});if(resolveOutput(cmd)==="json")printJson(market);else console.log(`Tracking market ${market.name} (${market.slug})`);return}const res=await client.get(base);if(resolveOutput(cmd)==="json"){printJson(res);return}printItems([...res.tracked.map(m=>({id:m.marketId,slug:m.slug,name:m.name,tracked:"yes",source:m.source})),...res.addable.map(m=>({id:m.marketId,slug:m.slug,name:m.name,tracked:"no",source:""}))],"table")}function register27(program2){const preference=program2.command("preference").description("Manage preference prompts: which product agents choose. Launch one with `gauge run --preference <id>`");preference.command("list").description("List preference prompts").option("--created-by <email|me>","only prompts created by this user").action(async(opts,cmd)=>{const items=await filterByCreator(await listPreferencePrompts(cmd),opts.createdBy);if(resolveOutput(cmd)==="json")printJson(items);else printItems(items.map(preferencePromptRow),"table")});preference.command("get <id>").description("Show one preference prompt").action(async(id2,_opts,cmd)=>{await getPreferencePrompt(cmd,id2)});addRunSettingsCreateOptions(preference.command("create").description("Create a preference prompt").requiredOption("--name <name>","name").option("--text <text>","prompt text inline").option("--file <path>",'prompt text from a file ("-" for stdin)').option("--repo <url>","the first scenario's repository (default: empty project)").option("--ref <ref>","branch, tag, or commit (with --repo)").option("--persona <profileId>","persona profile id")).option("--kind <kind>",KIND_CHOICES.join(" | ")).option("--brand-a <name>","first brand (head-to-head)").option("--brand-b <name>","second brand (head-to-head)").option("--branded","an open-ended prompt that names a brand").option("--market <marketId>","tracked market id (see `gauge preference markets`)").option("--tag <name>","user tag name, create-on-type (repeatable)",collect5,[]).action(async(opts,cmd)=>{await createPreferencePrompt(cmd,opts)});addRunSettingsEditOptions(preference.command("edit <id>").description("Update a preference prompt. Omitted flags keep their value.").option("--name <name>","rename it").option("--text <text>","replace the prompt text inline").option("--file <path>",'replace the prompt text from a file ("-" stdin)')).option("--brand-a <name>","first brand").option("--brand-b <name>","second brand").option("--branded","mark branded (open-ended only)").option("--organic","mark organic (open-ended only)").option("--market <marketId>","set the measured market").option("--clear-market","unscope from any market").option("--tag <name>","replace the user tag set (repeatable)",collect5,[]).option("--clear-tags","remove all user tags").action(async(id2,opts,cmd)=>{await editPreferencePrompt(cmd,id2,opts)});preference.command("rm <id>").description("Delete a preference prompt (run history stays)").action(async(id2,_opts,cmd)=>{await removePreferencePrompt(cmd,id2)});const scenarios=preference.command("scenarios").description("The repo \xD7 persona scenarios a preference prompt runs in");scenarios.command("list <id>").description("List a preference prompt's scenarios").action(async(id2,_opts,cmd)=>{await listScenarios(cmd,id2)});scenarios.command("add <id>").description("Add one repo \xD7 persona scenario").option("--repo <url>","repository URL (default: empty project)").option("--ref <ref>","branch, tag, or commit (with --repo)").option("--persona <profileId>","persona profile id (default persona if omitted)").action(async(id2,opts,cmd)=>{await addScenario(cmd,id2,opts)});scenarios.command("rm <id>").description("Remove one scenario").requiredOption("--scenario <scenarioId>","scenario id to remove").action(async(id2,opts,cmd)=>{await removeScenario(cmd,id2,opts.scenario)});preference.command("markets").description("List tracked and addable markets for preference prompts, or track one with --add").option("--add <marketId>","track a published market").action(async(opts,cmd)=>{await markets(cmd,opts)})}var repos_exports={};__export(repos_exports,{register:()=>register28});function reposPath(org){return`/api/v1/orgs/${encodeURIComponent(org)}/repos`}async function fetchAllRepos(client,org){return fetchAllPages(client,reposPath(org))}function toRow3(repo){return{name:repo.name,url:repo.url,languages:repo.languages.join(", "),frameworks:repo.frameworks.join(", "),tags:repo.tags.join(", "),size:repo.sizeBucket??"",stars:repo.stars,system:repo.isSystem?"yes":""}}function syncWarning(repo){return`warning: GitHub metadata sync failed; retry with \`gauge repos resync ${repo.url}\``}function register28(program2){const repos=hostedOnly(program2.command("repos")).description("Manage repositories");repos.command("list").description("List repositories (yours + read-only Gauge-provided ones)").action(async(_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const items=await fetchAllRepos(createClient(),org);if(output==="json"){printJson(items);return}printItems(items.map(toRow3),"table")});repos.command("add <url>").description("Add a repository (pulls languages/size/stars from GitHub)").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const result=await createClient().post(reposPath(org),{url});if(output==="json"){printJson(result);return}console.log(`Added ${result.repo.name} (${result.repo.url})`);if(!result.synced)console.error(syncWarning(result.repo))});repos.command("edit <url>").description("Update a repository's display name, frameworks, or tags").option("--name <name>","display name").option("--framework <name>","REPLACE the framework list (repeatable)",collect5,[]).option("--tag <name>","REPLACE the tag list (repeatable)",collect5,[]).option("--clear-frameworks","remove all frameworks").option("--clear-tags","remove all tags").action(async(url,opts,command)=>{const org=await resolveOrg(command);if(opts.framework.length>0&&opts.clearFrameworks)throw new CliError("usage","--framework and --clear-frameworks conflict");if(opts.tag.length>0&&opts.clearTags)throw new CliError("usage","--tag and --clear-tags conflict");const body2={url};if(opts.name!==void 0)body2.name=opts.name;if(opts.clearFrameworks)body2.frameworks=[];else if(opts.framework.length>0)body2.frameworks=opts.framework;if(opts.clearTags)body2.tags=[];else if(opts.tag.length>0)body2.tags=opts.tag;if(Object.keys(body2).length===1)throw new CliError("usage","nothing to change");const repo=await createClient().patch(reposPath(org),body2);if(resolveOutput(command)==="json")printJson(repo);else console.log(`Updated ${repo.name} (${repo.url})`)});repos.command("resync <url>").description("Re-fetch a repository's GitHub metadata").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);const result=await createClient().post(`${reposPath(org)}/resync`,{url});if(output==="json"){printJson(result);return}if(!result.synced){throw new CliError("internal","GitHub metadata sync failed (is the URL a public GitHub repository?)")}console.log(`Re-synced ${result.repo.name} (${result.repo.url})`)});repos.command("rm <url>").description("Remove a repository from the catalog").action(async(url,_opts,command)=>{const org=await resolveOrg(command);const output=resolveOutput(command);await createClient().delete(reposPath(org),{url});if(output!=="json")console.log(`Removed ${url}`)})}var run_exports={};__export(run_exports,{register:()=>register29});import{readFileSync as readFileSync10,statSync as statSync3}from"fs";function readCliRules(path){if(path==="-")throw new CliError("usage","--cli-rules expects a JSON file path, not stdin");try{const info=statSync3(path);if(!info.isFile())throw new Error("Expected a regular JSON file");if(info.size>CLI_RULE_LIMITS.bundleBytes)throw new Error("CLI rules file exceeds 2 MiB");return cliRuleAuthorInputSchema.parse(JSON.parse(readFileSync10(path,"utf8")))}catch(error){throw new CliError("usage",`Invalid --cli-rules file: ${error instanceof Error?error.message:error}`)}}import{readFileSync as readFileSync13,existsSync as existsSync4,mkdirSync as mkdirSync2,writeFileSync as writeFileSync5}from"fs";import{z as z15}from"zod";import{join as join11,relative as relative5}from"path";import{z as z8}from"zod";var preferenceDefinitionSchema=z8.discriminatedUnion("kind",[z8.object({kind:z8.literal("open-ended"),branded:z8.boolean().default(false)}).strict(),z8.object({kind:z8.literal("head-to-head"),brandA:z8.string().trim().min(1),brandB:z8.string().trim().min(1)}).strict().refine(p=>p.brandA.toLowerCase()!==p.brandB.toLowerCase(),"A head-to-head needs two different brands")]);var preferenceSnapshotSchema=z8.object({version:z8.literal(1),preference:preferenceDefinitionSchema}).strict();var headToHeadSchema=z8.object({kind:z8.literal("HEAD_TO_HEAD"),assessability:z8.enum(["assessed","partial","unassessable"]).optional(),selectionStatus:z8.enum(["measured","not_measured","unassessable"]).optional(),winner:z8.enum(["A","B","both","neither"]),brandA:z8.string(),brandB:z8.string(),rationale:z8.string(),brandAReasoning:z8.string(),brandBReasoning:z8.string(),brandAWhyRaised:z8.string().optional(),brandBWhyRaised:z8.string().optional()});var recommendPickSchema=z8.object({brand:z8.string(),rank:z8.number().int(),adopted:z8.boolean(),reasoning:z8.string().optional(),whyRaised:z8.string().optional()});var recommendSchema=z8.object({kind:z8.literal("RECOMMEND"),selectedBrands:z8.array(z8.string()).optional(),assessability:z8.enum(["assessed","partial","unassessable"]).optional(),selectionStatus:z8.enum(["measured","not_measured","unassessable"]).optional(),picks:z8.array(recommendPickSchema).default([]),singlePick:z8.boolean(),complianceNote:z8.string()});var visibilityVerdictSchema=z8.discriminatedUnion("kind",[headToHeadSchema,recommendSchema]);function preferenceSelectionLabel(verdict){if(verdict.assessability==="unassessable"||verdict.selectionStatus==="unassessable")return"Not assessable";if(verdict.kind==="HEAD_TO_HEAD")return verdict.winner==="A"?verdict.brandA:verdict.winner==="B"?verdict.brandB:verdict.winner==="both"?`${verdict.brandA} and ${verdict.brandB}`:"Neither";if(verdict.selectedBrands!==void 0)return verdict.selectedBrands.length?verdict.selectedBrands.join(", "):"No selection";if(!verdict.singlePick)return verdict.picks.length?"No clear selection":"No selection";return verdict.picks.find(pick=>pick.rank===1)?.brand??"No selection"}import{Option as Option5}from"commander";import{readFileSync as readFileSync11}from"fs";import{z as z9}from"zod";function assertRequiredExperiment(spec,required){if(required.length===0){throw new CliError("invalid_request","--require-experiment requires a nonempty label")}const mismatches=[];for(const[promptIndex,prompt]of spec.prompts.entries()){const promptExperiment=prompt.experiment??spec.experiment;const schedules=prompt.schedules??[{}];if(schedules.length===0){mismatches.push(`prompts.${promptIndex}.schedules`)}for(const[scheduleIndex,schedule]of schedules.entries()){if((schedule.experiment??promptExperiment)!==required){mismatches.push(`prompts.${promptIndex}.schedules.${scheduleIndex}`)}}}if(mismatches.length>0){throw new CliError("invalid_request",`--require-experiment expected every target to resolve to ${JSON.stringify(required)}; mismatched: ${mismatches.join(", ")}`)}}var EXAMPLE_SPEC={version:1,expand:true,experiment:"my-experiment",defaults:{agents:["CLAUDE_CODE"],samplesPerCycle:3},prompts:[{key:"add-healthcheck",text:"Add a /health endpoint that returns 200 OK."},{key:"migrate-to-vitest",text:"Migrate the test suite from Jest to Vitest.",schedules:[{repoUrl:"https://github.com/acme/api",repoRef:"main",model:"claude-opus-4-8"}]}]};async function readSpecJson(source2){let raw;if(source2==="-"){if(process.stdin.isTTY)throw new CliError("usage","--apply - needs a run spec piped to stdin");const chunks2=[];for await(const chunk of process.stdin)chunks2.push(chunk);raw=Buffer.concat(chunks2).toString("utf8")}else{try{raw=readFileSync11(source2,"utf8")}catch(e){throw new CliError("usage",`cannot read spec file ${source2}: ${e instanceof Error?e.message:String(e)}`)}}try{return JSON.parse(raw)}catch(e){throw new CliError("invalid_request",`spec is not valid JSON: ${e instanceof Error?e.message:String(e)}`)}}function parseSpec(parsed){const result=batteries_exports.batterySpecSchema.safeParse(parsed);if(!result.success){const details=result.error.issues.map(i=>` ${i.path.join(".")||"(spec)"}: ${i.message}`).join("\n");throw new CliError("invalid_request",`not a valid battery spec:
1001
1002
  ${details}`,{hint:"Run `gauge run --schema --hosted` for the full schema, or `gauge run --example --hosted` for a starter."})}return result.data}async function resolveApplyOrg(command,spec){return resolveOrg(command,spec.org)}function repoLabel2(repoUrl,repoRef){if(!repoUrl)return"greenfield";return repoRef?`${repoUrl}@${repoRef}`:repoUrl}function printPlan(plan){console.log(`Plan for org ${plan.org}`+(plan.experiment?` (experiment: ${plan.experiment})`:""));for(const prompt of plan.prompts){console.log(` ${prompt.action} prompt "${prompt.key}"`);for(const s of prompt.schedules){console.log(` ${s.action} schedule ${repoLabel2(s.repoUrl,s.repoRef)}`+(plan.expand?` \u2192 ${s.runs} run(s)`:""))}}const sum=plan.summary;console.log(`Prompts: ${sum.promptsToCreate} to create, ${sum.promptsToUpdate} to update; schedules: ${sum.schedulesToCreate} to create, ${sum.schedulesToUpdate} to update`);if(plan.expand){const byAgent=Object.entries(sum.runsByAgent).map(([agent,n])=>`${agent}: ${n}`).join(", ");console.log(`Runs to enqueue: ${sum.runsToEnqueue}${byAgent?` (${byAgent})`:""} \u2014 estimated ${sum.estimatedCredits} credits`)}else{console.log('No runs will be enqueued (config only \u2014 set "expand": true to run).')}console.log("Dry run \u2014 nothing was applied.")}function printApplied(res){console.log(`Applied${res.experiment?` (experiment: ${res.experiment})`:""} \u2014 ${res.batchIds.length} batch(es), ${res.runIds.length} run(s) enqueued`);for(const id2 of res.runIds)console.log(` run ${id2}`);if(res.runIds.length>0){console.error(`(watch one live: gauge run watch ${res.runIds[0]})`)}if(res.blocked){console.error(`warning: some schedules were not expanded (${res.blocked}) \u2014 re-apply after resolving to enqueue the rest`)}}var BATTERY_SCHEMA=()=>z9.toJSONSchema(batteries_exports.batterySpecSchema);async function applySpec(command,spec,opts){if(opts.requireExperiment!==void 0)assertRequiredExperiment(spec,opts.requireExperiment);const org=await resolveApplyOrg(command,spec);const result=await createClient().post(`/api/v1/orgs/${encodeURIComponent(org)}/batteries`,spec,opts.dryRun?{dryRun:1}:void 0);if(resolveOutput(command)==="json")printJson(result);else if(opts.dryRun)printPlan(result);else printApplied(result);return result}import{createHash as createHash3}from"crypto";import{z as z13}from"zod";import{z as z10}from"zod";var runAddonsSchema=z10.object({browser:z10.object({session:z10.string().min(1),projectId:z10.number().int().positive(),region:z10.literal("us").default("us")}).strict().optional()}).strict();import{z as z12}from"zod";import{z as z11}from"zod";var inputPathSchema=z11.string().min(1).max(256).regex(/^(?![!/])(?!.*[\\\x00-\x1f\x7f])(?!.*(?:^|\/)\.{1,2}(?:\/|$))(?!.*\/\/)(?!.*\/$).+$/,"Expected a repository-relative path or glob without traversal");var reservedBinaryCommands=["bash","sh","zsh","env","node","nodejs","npm","npx","bun","python","python3","git","tar","curl","cp","chmod","mkdir","rm","ln","test","sha256sum","printenv","uname","claude","codex","opencode","pi","wizard"];var binaryCommandSchema=z11.string().max(128).regex(/^[A-Za-z0-9_][A-Za-z0-9_.+-]*$/,"Expected a command name without paths or shell syntax").refine(name=>!reservedBinaryCommands.includes(name),"Command is reserved for Gauge workspace setup or the agent harness; choose another command name");var binaryUseSchema=z11.object({type:z11.literal("binary"),paths:z11.array(inputPathSchema).min(1).max(32),platform:z11.literal("linux-x64").describe("Linux x86-64 ELF executables compatible with the consumer sandbox. No system packages are installed."),commands:z11.record(binaryCommandSchema,inputPathSchema.regex(/^[^*?{}[\]]+$/,"Command targets must be explicit relative file paths")).refine(commands=>Object.keys(commands).length>=1&&Object.keys(commands).length<=32,"Declare between 1 and 32 commands").meta({minProperties:1,maxProperties:32})}).strict();var inputUseSchema=z11.discriminatedUnion("type",[binaryUseSchema,websiteUseSchema,z11.object({type:z11.literal("web-fixture"),path:inputPathSchema.regex(/^[^*?{}[\]]+$/)}).strict(),z11.object({type:z11.literal("files"),paths:z11.array(inputPathSchema).min(1).max(32).optional()}).strict(),z11.object({type:z11.literal("npm-cli"),package:inputPathSchema,install:z11.enum(["environment","workspace"]).optional()}).strict(),z11.object({type:z11.literal("npm-package"),package:inputPathSchema,install:z11.enum(["environment","workspace"]).optional()}).strict(),z11.object({type:z11.literal("skill"),root:z11.union([z11.literal("."),inputPathSchema]).refine(path=>!/[*?{}[\]]/.test(path),"Skill root must be an explicit directory")}).strict()]);var inputSourceSchema=z11.discriminatedUnion("type",[z11.object({type:z11.literal("git"),paths:z11.array(inputPathSchema).min(1).max(32)}).strict(),z11.object({type:z11.literal("github-actions"),artifact:z11.string().min(1).max(256).optional()}).strict()]);var runPlanResourceSchema=z11.object({name:z11.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).max(64),type:z11.literal("input"),source:z11.discriminatedUnion("type",[z11.object({type:z11.literal("upload"),sha256:z11.string().regex(/^sha256:[a-f0-9]{64}$/),bytes:z11.number().int().positive().max(512*1024*1024),repoUrl:z11.string().nullable(),commit:z11.string().regex(/^[a-f0-9]{40}$/i).nullable()}).strict(),commentSourceSchema.extend({repoUrl:z11.string(),commit:z11.string().regex(/^[a-f0-9]{40}$/i)}).strict(),deploymentSourceSchema.extend({repoUrl:z11.string(),commit:z11.string().regex(/^[a-f0-9]{40}$/i)}).strict(),inputSourceSchema.options[0].extend({repoUrl:z11.string(),commit:z11.string().regex(/^[a-f0-9]{40}$/i)}).strict(),inputSourceSchema.options[1].extend({repoUrl:z11.string(),commit:z11.string().regex(/^[a-f0-9]{40}$/i),workflow:inputPathSchema}).strict()]),use:inputUseSchema}).strict().refine(r=>r.use.type==="website"===(r.source.type==="github-deployment"||r.source.type==="github-pr-comment"),"Website inputs require a deployment or PR comment source; file inputs require Git, Actions, or an upload");var definitionInputSourceSchema=z12.discriminatedUnion("type",[z12.object({type:z12.literal("git")}).strict(),z12.object({type:z12.literal("github-actions"),workflow:inputPathSchema.regex(/^\.github\/workflows\/[^/*?{}[\]]+\.ya?ml$/).describe("Workflow file in this repository, for example .github/workflows/build.yml."),artifact:z12.string().min(1).max(256).optional().describe("Upload name or pattern. Omit only when the workflow produces exactly one artifact.")}).strict()]);var skillInputPathSchema=z12.union([z12.literal("."),inputPathSchema.regex(/^[^*?{}[\]]+$/)]).describe("Explicit Skill directory or ZIP/tar.gz file containing SKILL.md at its root.");var runDefinitionInputSchema=z12.discriminatedUnion("type",[binaryUseSchema.extend({source:definitionInputSourceSchema.default({type:"git"})}).strict(),websiteUseSchema.extend({source:websiteSourceSchema}).strict(),z12.object({type:z12.literal("web-fixture"),source:definitionInputSourceSchema.default({type:"git"}),path:inputPathSchema.regex(/^[^*?{}[\]]+$/).describe("Explicit path to a version 1 web fixture JSON file.")}).strict(),z12.object({type:z12.literal("npm-package"),source:definitionInputSourceSchema.default({type:"git"}),path:inputPathSchema.describe("Path or glob selecting exactly one packed npm .tgz file."),install:z12.enum(["environment","workspace"]).default("environment").describe("environment exposes the package's commands on PATH; workspace adds a consumer application dependency.")}).strict(),z12.object({type:z12.literal("skill"),source:definitionInputSourceSchema.default({type:"git"}),path:skillInputPathSchema}).strict(),z12.object({type:z12.literal("files"),source:definitionInputSourceSchema.default({type:"git"}),paths:z12.array(inputPathSchema).min(1).max(32).describe("Files or globs selected from the committed Git tree or the uploaded artifact.")}).strict()]);function isSkillArchive(path){return/\.(?:zip|tar\.gz|tgz)$/i.test(path)}function compileDefinitionInput(name,input,repoUrl,commit){if(input.type==="website")return{name,type:"input",source:{...input.source,repoUrl,commit},use:{type:"website",url:input.url}};const paths=input.type==="files"||input.type==="binary"?input.paths:input.type==="skill"&&!isSkillArchive(input.path)?[input.path==="."?"**":`${input.path}/**`]:[input.path];return{name,type:"input",source:input.source.type==="git"?{type:"git",paths,repoUrl,commit}:{...input.source,repoUrl,commit},use:input.type==="binary"?{type:"binary",paths:input.paths,platform:input.platform,commands:input.commands}:input.type==="npm-package"?{type:"npm-package",package:input.path,install:input.install}:input.type==="web-fixture"?{type:"web-fixture",path:input.path}:input.type==="skill"?{type:"skill",root:input.path}:{type:"files",paths:input.paths}}}var inputUploadSpecSchema=z13.object({sha256:z13.string().regex(/^sha256:[a-f0-9]{64}$/),bytes:z13.number().int().positive().max(512*1024*1024)}).strict();var uncredentialedRepoUrlSchema=z13.string().min(1).refine(raw=>{if(!/^[a-z][a-z0-9+.-]*:\/\//i.test(raw))return true;try{const url=new URL(raw);return(url.protocol==="ssh:"||!url.username)&&!url.password&&!url.search&&!url.hash}catch{return false}},"Repository URL must not contain embedded credentials or query parameters");var runDefinitionChecksSchema=z13.object({paths:z13.array(inputPathSchema).min(1).max(32)}).strict();var runDefinitionConfigSchema=z13.object({agents:z13.array(agentConfigInputSchema).min(1),sampleCount:z13.number().int().min(1).max(50).optional(),repoUrl:uncredentialedRepoUrlSchema.nullable().optional(),repoRef:z13.string().min(1).nullable().optional(),profileId:z13.string().min(1).nullable().optional(),skillRefs:assetRefListSchema.optional(),mcpRefs:assetRefListSchema.optional(),connectionIds:z13.array(z13.string().min(1)).max(32).optional(),addons:runAddonsSchema.optional(),inputs:z13.array(z13.string()).optional(),checks:runDefinitionChecksSchema.optional()}).strict();var runDefinitionFileConfigSchema=z13.object({version:z13.literal(2).default(2),org:z13.string().min(1).optional(),orgId:z13.string().uuid().optional(),checks:runDefinitionChecksSchema.optional(),cases:z13.array(inputPathSchema).min(1).max(32).default(["gauge-cases/**/*.md"]),inputs:z13.record(z13.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).max(64),runDefinitionInputSchema).refine(inputs=>Object.keys(inputs).length<=32,"At most 32 inputs are allowed").meta({maxProperties:32}).default({})}).strict();var runDefinitionCriterionSchema=z13.object({name:z13.string().min(1),rubric:z13.string().min(1)}).strict();var caseMeasurement={criteria:z13.array(runDefinitionCriterionSchema).min(1).optional(),preference:preferenceDefinitionSchema.optional()};function exactlyOneMeasurement(item){return item.criteria!==void 0!==(item.preference!==void 0)}var runDefinitionFrontmatterSchema=z13.object({...caseMeasurement,config:runDefinitionConfigSchema}).strict().refine(exactlyOneMeasurement,"Specify exactly one of criteria or preference");var runDefinitionCaseSchema=z13.object({path:z13.string().min(1),prompt:z13.string().min(1),...caseMeasurement,config:runDefinitionConfigSchema}).strict().refine(exactlyOneMeasurement,"Specify exactly one of criteria or preference");var runDefinitionSubmissionSchema=z13.object({version:z13.literal(2),lifecycle:lifecycleSchema.nullish(),cliRules:cliRuleAuthorInputSchema.optional(),source:z13.object({repoUrl:uncredentialedRepoUrlSchema.nullable(),commit:z13.string().regex(/^[a-f0-9]{40}$/i).nullable(),digest:z13.string().regex(/^sha256:[a-f0-9]{64}$/),verification:z13.enum(["SUBMITTED_UNVERIFIED","GITHUB_VERIFIED","LOCAL"])}).strict(),resources:z13.array(runPlanResourceSchema).max(32).default([]),cases:z13.array(runDefinitionCaseSchema).min(1).max(20)}).strict().superRefine((definition,ctx)=>{if(definition.source.verification==="GITHUB_VERIFIED"&&definition.cliRules!==void 0)ctx.addIssue({code:"custom",path:["cliRules"],message:"GitHub definitions cannot contain launch-only CLI rules"});if(definition.source.verification==="GITHUB_VERIFIED"&&definition.lifecycle!==void 0)ctx.addIssue({code:"custom",path:["lifecycle"],message:"GitHub definitions cannot contain launch-only lifecycle scripts"});if(definition.source.verification!=="LOCAL"&&(!definition.source.repoUrl||!definition.source.commit))ctx.addIssue({code:"custom",path:["source"],message:"Committed definitions require a repository and commit"});if(definition.source.verification==="GITHUB_VERIFIED"&&definition.resources.some(r=>r.source.type==="upload"))ctx.addIssue({code:"custom",path:["resources"],message:"GitHub definitions cannot contain uploaded inputs"});const paths=new Set;for(const[index,item]of definition.cases.entries()){if(paths.has(item.path))ctx.addIssue({code:"custom",path:["cases",index,"path"],message:"Case paths must be unique"});paths.add(item.path)}const names=definition.resources.map(resource=>resource.name);if(new Set(names).size!==names.length)ctx.addIssue({code:"custom",path:["resources"],message:"Input names must be unique"});for(const[index,item]of definition.cases.entries())for(const name of item.config.inputs??names)if(!names.includes(name))ctx.addIssue({code:"custom",path:["cases",index,"config","inputs"],message:`Unknown input: ${name}`});for(const[index,resource]of definition.resources.entries()){if(resource.source.repoUrl!==definition.source.repoUrl||resource.source.commit!==definition.source.commit)ctx.addIssue({code:"custom",path:["resources",index,"source"],message:"Project preparation must use the definition repository and commit"})}});var runDefinitionPublicSubmissionSchema=runDefinitionSubmissionSchema.refine(definition=>definition.source.verification!=="GITHUB_VERIFIED",{path:["source","verification"],message:"Only the GitHub App may verify committed bytes"});var runDefinitionPlanResponseSchema=z13.object({cliRules:z13.object({ruleCount:z13.number().int().positive(),bundleSha256:z13.string().regex(/^[a-f0-9]{64}$/)}).optional(),source:runDefinitionSubmissionSchema.shape.source,lifecycle:z13.object({setupTimeoutSeconds:z13.number().int().min(1).max(600).optional(),teardownTimeoutSeconds:z13.number().int().min(1).max(600).optional()}).optional(),measurementMode:z13.enum(["AGENT_EXPERIENCE","AGENT_PREFERENCE","MIXED"]),cases:z13.array(z13.object({path:z13.string(),agents:z13.array(agentConfigSchema),samples:z13.number().int().nonnegative(),expectedRuns:z13.number().int().nonnegative(),workspace:z13.object({repoUrl:z13.string().nullable(),repoRef:z13.string().nullable()}),criteriaCount:z13.number().int().nonnegative(),preference:preferenceDefinitionSchema.optional(),inputs:z13.array(z13.string())})),resources:z13.array(z13.object({name:z13.string(),type:z13.literal("input"),source:z13.enum(["git","github-actions","github-deployment","github-pr-comment","upload"]),use:inputUseSchema,readiness:z13.enum(["READY","WAITING","FAILED"]),detail:z13.string().optional(),commands:z13.array(z13.string()).optional()})),expectedRuns:z13.number().int().nonnegative(),canRun:z13.boolean()});var runDefinitionRunResponseSchema=z13.object({runRequestId:z13.string(),batchIds:z13.array(z13.string()),runIds:z13.array(z13.string()),requestedRuns:z13.number().int().nonnegative(),createdRuns:z13.number().int().nonnegative(),aggregateUrl:z13.string()});import{z as z14}from"zod";var caseSpecSchema=z14.object({version:z14.literal(2),cases:z14.array(z14.object({key:z14.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).max(64).describe("Unique case id, lowercase-kebab"),prompt:z14.string().trim().min(1).describe("The user's request"),criteria:z14.array(runDefinitionCriterionSchema).min(1).optional().describe("Optional for local runs; hosted needs criteria or preference"),preference:preferenceDefinitionSchema.optional(),config:runDefinitionConfigSchema}).strict().refine(item=>!(item.criteria&&item.preference),"Specify criteria or preference, not both")).min(1).max(20)}).strict().refine(spec=>new Set(spec.cases.map(item=>item.key)).size===spec.cases.length,"Case keys must be unique");var EXAMPLE_CASE_SPEC={version:2,cases:[{key:"release-notes",prompt:"Write the release notes for version 1.2.0. We added dark mode and fixed a login crash.",criteria:[{name:"Team format",rubric:"RELEASE-NOTES.md lists both changes in the team's format."}],config:{agents:[{agent:"CODEX_CLI"}],inputs:["release-notes"]}}]};var CASE_SPEC_SCHEMA=()=>z14.toJSONSchema(caseSpecSchema);function isCaseSpec(value){return value!==null&&typeof value==="object"&&"cases"in value&&!("prompts"in value)}function parseCaseSpec(value){const result=caseSpecSchema.safeParse(value);if(!result.success)throw new CliError("usage",`not a valid case spec:
1002
- ${result.error.issues.map(i=>` ${i.path.join(".")||"(spec)"}: ${i.message}`).join("\n")}`,{hint:"Run `gauge run --schema --local` for the full schema, or `gauge run --example --local` for a starter."});return result.data}function caseSpecCases(spec){return spec.cases.map(({key,...item})=>({path:key,...item}))}function caseSpecDigest(spec){return`sha256:${createHash3("sha256").update("gauge-case-spec-v2\0").update(JSON.stringify(spec)).digest("hex")}`}import{execFileSync as execFileSync2}from"child_process";import{createHash as createHash5}from"crypto";import{createReadStream as createReadStream2,globSync as globSync2}from"fs";import{lstat,mkdtemp,readdir,rm,stat}from"fs/promises";import{tmpdir}from"os";import{basename,dirname,isAbsolute as isAbsolute4,join as join10,relative as relative4,resolve as resolve4,sep as sep2}from"path";import{setTimeout as delay2}from"timers/promises";import{createHash as createHash4}from"crypto";import{parseDocument}from"yaml";function utf8(bytes,path){try{return new TextDecoder("utf-8",{fatal:true}).decode(bytes)}catch{throw new Error(`${path} is not UTF-8 text`)}}function parseRunDefinitionCase(path,bytes){const source2=utf8(bytes,path);const match=/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)([\s\S]*)$/.exec(source2);if(!match)throw new Error(`${path}: expected YAML frontmatter between --- lines`);const document=parseDocument(match[1],{uniqueKeys:true});if(document.errors.length)throw new Error(`${path}: ${document.errors.map(error=>error.message).join("; ")}`);const parsed=runDefinitionFrontmatterSchema.safeParse(document.toJS());if(!parsed.success)throw new Error(`${path}: ${parsed.error.message}`);const prompt=match[2].trim();if(!prompt)throw new Error(`${path}: Markdown body is empty`);return{path,prompt,...parsed.data}}function compileRunDefinitionFromBlobs(input){if(input.repoUrl!==null&&!uncredentialedRepoUrlSchema.safeParse(input.repoUrl).success)throw new Error("Git origin must not contain embedded credentials");if(input.commit!==null&&!/^[a-f0-9]{40}$/i.test(input.commit))throw new Error("Expected a full Git commit SHA");if(input.casePaths.length===0)throw new Error("At least one Markdown case is required");if(new Set(input.casePaths).size!==input.casePaths.length)throw new Error("Case paths must be unique");const configBytes=input.blobs.get("gauge.json");let config;if(configBytes){try{config=runDefinitionFileConfigSchema.parse(JSON.parse(utf8(configBytes,"gauge.json")))}catch(error){throw new Error(`gauge.json: ${error instanceof Error?error.message:error}`)}}const cases=input.casePaths.map(path=>{const bytes=input.blobs.get(path);if(!path.endsWith(".md")||!bytes)throw new Error(`Missing Markdown case: ${path}`);return parseRunDefinitionCase(path,bytes)});const hash=createHash4("sha256");hash.update("gauge-run-definition-v2\0");for(const[path,bytes]of[...input.blobs.entries()].sort(([a],[b])=>a<b?-1:a>b?1:0)){hash.update(path);hash.update("\0");hash.update(String(bytes.length));hash.update("\0");hash.update(bytes);hash.update("\0")}const submission=runDefinitionSubmissionSchema.parse({version:2,source:{repoUrl:input.repoUrl,commit:input.commit,digest:`sha256:${hash.digest("hex")}`,verification:input.verification},resources:input.resources??Object.entries(config?.inputs??{}).map(([name,item])=>{if(!input.repoUrl||!input.commit)throw new Error(`Supply --input ${name}=<path> outside a Git checkout with an origin`);return compileDefinitionInput(name,item,input.repoUrl,input.commit)}),cases});return{definition:submission,org:config?.orgId??config?.org}}import{minimatch}from"minimatch";function matchInputPaths(paths,patterns,options={}){for(const pattern of patterns)inputPathSchema.parse(pattern);const selections=patterns.map(pattern=>({pattern,matches:paths.filter(path=>minimatch(path,pattern,{dot:true,nocase:false,nonegate:true}))}));if(options.allowEmptySelection&&selections.every(({matches:matches2})=>matches2.length===0))return[];const selected=new Set;for(const{pattern,matches:matches2}of selections){if(!matches2.length)throw new Error(`No committed files match ${pattern}`);for(const path of matches2)selected.add(path)}return[...selected].sort()}import{create as tar}from"tar";import{execFileSync}from"child_process";import{globSync,lstatSync,readdirSync,readFileSync as readFileSync12,realpathSync as realpathSync3}from"fs";import{isAbsolute as isAbsolute3,join as join9,relative as relative3,resolve as resolve3,sep}from"path";import{minimatch as minimatch2}from"minimatch";import{parseDocument as parseDocument2}from"yaml";import{CommanderError}from"commander";import{ZodError}from"zod";function errorReport(error,command,context={}){const base=error instanceof CliError&&error.needsAccount&&context.local?localModeError(command,context.local,error.hint):describe(error);const quiet=base.message==="aborted"||command==="feedback";const isChat=command==="chat"||command.startsWith("chat ");const docs=base.docs??(isChat?void 0:DOCS_URL);return{...base,...docs?{docs}:{},...quiet?{}:{feedback:`gauge feedback -m "<what happened>" --kind friction${command?` --command "${command}"`:""}`}}}function localModeError(command,source2,hint){return{code:"usage",message:`\`gauge ${command}\` requires hosted Gauge, but ${source2==="flag"?"you passed --local":source2==="workspace"?"this project is set to run locally":source2==="user"?"your saved setting is local":"the CLI is using local mode"}.`,hint:hint??"Pass --hosted for this command, or run `gauge init --hosted` to use hosted Gauge in this project. Sign in with `gauge auth login` if needed.",exitCode:exitCodeFor("usage")}}function describe(error){if(error instanceof CliError)return{code:error.apiCode??error.code,message:readable(error.message),...error.details?{details:error.details}:{},hint:error.hint,docs:error.docs,exitCode:error.exitCode};if(error instanceof CommanderError)return{code:"usage",message:error.message.replace(/^error: /,""),hint:"Run the command with --help for its usage.",exitCode:exitCodeFor("usage")};if(error instanceof ZodError)return{code:"usage",message:issueLines("Invalid input",error.issues),exitCode:exitCodeFor("usage")};const node=error;if(node?.code==="ENOENT"&&node.path)return{code:"usage",message:`File not found: ${node.path}`,exitCode:exitCodeFor("usage")};if(node?.code==="EACCES"&&node.path)return{code:"usage",message:`Permission denied: ${node.path}`,exitCode:exitCodeFor("usage")};return{code:"internal",message:`Unexpected error: ${error instanceof Error?readable(error.message):String(error)}`,hint:process.env.GAUGE_DEBUG?void 0:"Set GAUGE_DEBUG=1 to print the stack trace.",exitCode:1}}function readable(message2){const start=message2.search(/\[\s*\{/);if(start<0)return message2;try{const issues=JSON.parse(message2.slice(start));if(!Array.isArray(issues)||!issues.every(issue=>typeof issue?.message==="string"))return message2;return issueLines(message2.slice(0,start).replace(/[:\s]+$/,""),issues)}catch{return message2}}function issueLines(prefix,issues){return[prefix,...issues.map(issue=>` ${issue.path?.length?`${issue.path.map(String).join(".")}: `:""}${issue.message}`)].filter(Boolean).join("\n")}function renderError(report,json){if(json)return`${JSON.stringify({error:report},null,2)}
1003
+ ${result.error.issues.map(i=>` ${i.path.join(".")||"(spec)"}: ${i.message}`).join("\n")}`,{hint:"Run `gauge run --schema --local` for the full schema, or `gauge run --example --local` for a starter."});return result.data}function caseSpecCases(spec){return spec.cases.map(({key,...item})=>({path:key,...item}))}function caseSpecDigest(spec){return`sha256:${createHash3("sha256").update("gauge-case-spec-v2\0").update(JSON.stringify(spec)).digest("hex")}`}import{writeFileSync as writeFileSync4}from"fs";function runPath(id2){return`/api/v1/runs/${encodeURIComponent(id2)}`}function parseSeq(raw){const seq=parsePositiveSeq(raw);if(seq===null)throw new CliError("usage",`--before takes a seq, e.g. 34 (got ${raw})`);return seq}function forkBody(opts,read2=readText){if(opts.page){if(opts.before!==void 0||opts.message!==void 0)throw new CliError("usage","--page picks its own fork point; drop --before and --message");if(!opts.edits===!opts.from)throw new CliError("usage","--page needs --edits <file> or --from <file>");if(opts.contentType&&!opts.from)throw new CliError("usage","--content-type only applies with --from");return opts.edits?{kind:"mock_page",url:opts.page,edits:parseEdits(readJson2(opts.edits,read2))}:{kind:"mock_page",url:opts.page,body:read2(opts.from),...opts.contentType?{contentType:opts.contentType}:{}}}if(opts.edits||opts.from||opts.contentType)throw new CliError("usage","--edits, --from and --content-type need --page");if(!opts.before)throw new CliError("usage","pass --before <seq>, or --page <url> with --edits or --from");const beforeSeq=parseSeq(opts.before);if(opts.message===void 0)return{kind:"run_again",beforeSeq};if(!opts.message.trim())throw new CliError("usage","--message is empty");return{kind:"new_message",beforeSeq,message:opts.message}}function printFork(res){console.log(`Forked ${res.sourceRunId} before seq ${res.forkedBefore}: new run ${res.runId}`);if(res.change){const{linesBefore,linesAfter,retainedPercent,hunks}=res.change;console.log(`Mock ${res.mockId}: ${linesBefore} \u2192 ${linesAfter} lines, ${retainedPercent??"\u2014"}% of the original kept`);console.log(hunks)}console.log(`Follow with \`gauge run watch ${res.runId}\`.`)}function registerRunFork(run){hostedOnly(run.command("fork <runId>")).description("Start a new run that repeats a recorded run's steps up to a point, then continues live. Nothing else changes, unless you send a different persona message or mock a page the run read").option("--before <seq>","the first event that can change; the fork repeats every step before it").option("--message <text>","with --before at a persona message: send this message in its place").option("--page <url>","mock a page the run read; the fork starts at the last tool call before the run first read it").option("--edits <file>","with --page: JSON [{find, replace}] on the page's exact source; copy find text from `gauge run page --query`; '-' for stdin").option("--from <file>","with --page: the complete new page; '-' for stdin").option("--content-type <type>","with --from: the page content type (default: keep)").addHelpText("after",`
1004
+ Examples:
1005
+ gauge run fork <runId> --before 34
1006
+ gauge run fork <runId> --before 12 --message "Use the staging project instead"
1007
+ gauge run fork <runId> --page https://docs.example.com/start --edits edits.json
1008
+
1009
+ Find seqs with \`gauge run trace <runId> --seq <n>\` and the pages a run read with \`gauge run web <runId>\`.`).action(async(runId,opts,command)=>{const res=await createClient().post(`${runPath(runId)}/fork`,forkBody(opts));if(resolveOutput(command)==="json")printJson(res);else printFork(res)});hostedOnly(run.command("page <runId> <url>")).description("Print a page as the run read it: the exact source (--text for readable text, --query for the source around a match)").option("--text","readable text instead of the source").option("--query <text>","the exact source around each match, to copy into `gauge run fork --edits`").option("--out <file>","save the exact source to a file").action(async(runId,url,opts,command)=>{if(opts.out&&(opts.text||opts.query))throw new CliError("usage","--out saves the exact source only");const view=await createClient().get(`${runPath(runId)}/page`,{url,...opts.query?{query:opts.query}:{},...opts.text?{view:"text"}:{}});if(opts.out){if(view.raw===void 0)throw new CliError("internal","The server returned no page source");if(view.raw.length<view.rawChars)throw new CliError("internal",`The page has ${view.rawChars} chars, but the server returned only ${view.raw.length}`);writeFileSync4(opts.out,view.raw);console.error(`Saved ${opts.out}`)}else if(resolveOutput(command)==="json")printJson(view);else printPageView(view)})}import{execFileSync as execFileSync2}from"child_process";import{createHash as createHash5}from"crypto";import{createReadStream as createReadStream2,globSync as globSync2}from"fs";import{lstat,mkdtemp,readdir,rm,stat}from"fs/promises";import{tmpdir}from"os";import{basename,dirname,isAbsolute as isAbsolute4,join as join10,relative as relative4,resolve as resolve4,sep as sep2}from"path";import{setTimeout as delay2}from"timers/promises";import{createHash as createHash4}from"crypto";import{parseDocument}from"yaml";function utf8(bytes,path){try{return new TextDecoder("utf-8",{fatal:true}).decode(bytes)}catch{throw new Error(`${path} is not UTF-8 text`)}}function parseRunDefinitionCase(path,bytes){const source2=utf8(bytes,path);const match=/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)([\s\S]*)$/.exec(source2);if(!match)throw new Error(`${path}: expected YAML frontmatter between --- lines`);const document=parseDocument(match[1],{uniqueKeys:true});if(document.errors.length)throw new Error(`${path}: ${document.errors.map(error=>error.message).join("; ")}`);const parsed=runDefinitionFrontmatterSchema.safeParse(document.toJS());if(!parsed.success)throw new Error(`${path}: ${parsed.error.message}`);const prompt=match[2].trim();if(!prompt)throw new Error(`${path}: Markdown body is empty`);return{path,prompt,...parsed.data}}function compileRunDefinitionFromBlobs(input){if(input.repoUrl!==null&&!uncredentialedRepoUrlSchema.safeParse(input.repoUrl).success)throw new Error("Git origin must not contain embedded credentials");if(input.commit!==null&&!/^[a-f0-9]{40}$/i.test(input.commit))throw new Error("Expected a full Git commit SHA");if(input.casePaths.length===0)throw new Error("At least one Markdown case is required");if(new Set(input.casePaths).size!==input.casePaths.length)throw new Error("Case paths must be unique");const configBytes=input.blobs.get("gauge.json");let config;if(configBytes){try{config=runDefinitionFileConfigSchema.parse(JSON.parse(utf8(configBytes,"gauge.json")))}catch(error){throw new Error(`gauge.json: ${error instanceof Error?error.message:error}`)}}const cases=input.casePaths.map(path=>{const bytes=input.blobs.get(path);if(!path.endsWith(".md")||!bytes)throw new Error(`Missing Markdown case: ${path}`);return parseRunDefinitionCase(path,bytes)});const hash=createHash4("sha256");hash.update("gauge-run-definition-v2\0");for(const[path,bytes]of[...input.blobs.entries()].sort(([a],[b])=>a<b?-1:a>b?1:0)){hash.update(path);hash.update("\0");hash.update(String(bytes.length));hash.update("\0");hash.update(bytes);hash.update("\0")}const submission=runDefinitionSubmissionSchema.parse({version:2,source:{repoUrl:input.repoUrl,commit:input.commit,digest:`sha256:${hash.digest("hex")}`,verification:input.verification},resources:input.resources??Object.entries(config?.inputs??{}).map(([name,item])=>{if(!input.repoUrl||!input.commit)throw new Error(`Supply --input ${name}=<path> outside a Git checkout with an origin`);return compileDefinitionInput(name,item,input.repoUrl,input.commit)}),cases});return{definition:submission,org:config?.orgId??config?.org}}import{minimatch}from"minimatch";function matchInputPaths(paths,patterns,options={}){for(const pattern of patterns)inputPathSchema.parse(pattern);const selections=patterns.map(pattern=>({pattern,matches:paths.filter(path=>minimatch(path,pattern,{dot:true,nocase:false,nonegate:true}))}));if(options.allowEmptySelection&&selections.every(({matches:matches2})=>matches2.length===0))return[];const selected=new Set;for(const{pattern,matches:matches2}of selections){if(!matches2.length)throw new Error(`No committed files match ${pattern}`);for(const path of matches2)selected.add(path)}return[...selected].sort()}import{create as tar}from"tar";import{execFileSync}from"child_process";import{globSync,lstatSync,readdirSync,readFileSync as readFileSync12,realpathSync as realpathSync3}from"fs";import{isAbsolute as isAbsolute3,join as join9,relative as relative3,resolve as resolve3,sep}from"path";import{minimatch as minimatch2}from"minimatch";import{parseDocument as parseDocument2}from"yaml";import{CommanderError}from"commander";import{ZodError}from"zod";function errorReport(error,command,context={}){const base=error instanceof CliError&&error.needsAccount&&context.local?localModeError(command,context.local,error.hint):describe(error);const quiet=base.message==="aborted"||command==="feedback";const isChat=command==="chat"||command.startsWith("chat ");const docs=base.docs??(isChat?void 0:DOCS_URL);return{...base,...docs?{docs}:{},...quiet?{}:{feedback:`gauge feedback -m "<what happened>" --kind friction${command?` --command "${command}"`:""}`}}}function localModeError(command,source2,hint){return{code:"usage",message:`\`gauge ${command}\` requires hosted Gauge, but ${source2==="flag"?"you passed --local":source2==="workspace"?"this project is set to run locally":source2==="user"?"your saved setting is local":"the CLI is using local mode"}.`,hint:hint??"Pass --hosted for this command, or run `gauge init --hosted` to use hosted Gauge in this project. Sign in with `gauge auth login` if needed.",exitCode:exitCodeFor("usage")}}function describe(error){if(error instanceof CliError)return{code:error.apiCode??error.code,message:readable(error.message),...error.details?{details:error.details}:{},hint:error.hint,docs:error.docs,exitCode:error.exitCode};if(error instanceof CommanderError)return{code:"usage",message:error.message.replace(/^error: /,""),hint:"Run the command with --help for its usage.",exitCode:exitCodeFor("usage")};if(error instanceof ZodError)return{code:"usage",message:issueLines("Invalid input",error.issues),exitCode:exitCodeFor("usage")};const node=error;if(node?.code==="ENOENT"&&node.path)return{code:"usage",message:`File not found: ${node.path}`,exitCode:exitCodeFor("usage")};if(node?.code==="EACCES"&&node.path)return{code:"usage",message:`Permission denied: ${node.path}`,exitCode:exitCodeFor("usage")};return{code:"internal",message:`Unexpected error: ${error instanceof Error?readable(error.message):String(error)}`,hint:process.env.GAUGE_DEBUG?void 0:"Set GAUGE_DEBUG=1 to print the stack trace.",exitCode:1}}function readable(message2){const start=message2.search(/\[\s*\{/);if(start<0)return message2;try{const issues=JSON.parse(message2.slice(start));if(!Array.isArray(issues)||!issues.every(issue=>typeof issue?.message==="string"))return message2;return issueLines(message2.slice(0,start).replace(/[:\s]+$/,""),issues)}catch{return message2}}function issueLines(prefix,issues){return[prefix,...issues.map(issue=>` ${issue.path?.length?`${issue.path.map(String).join(".")}: `:""}${issue.message}`)].filter(Boolean).join("\n")}function renderError(report,json){if(json)return`${JSON.stringify({error:report},null,2)}
1003
1010
  `;const red=style("31",process.stderr);const dim=style("2",process.stderr);return`${[`${red("error:")} ${report.message}`,...report.hint?[report.hint]:[],...report.docs?[dim(`Docs: ${report.docs}`)]:[],...report.feedback?[dim(`Hit friction? ${report.feedback}`)]:[]].join("\n")}
1004
- `}function commandPath(program2,argv){const names=[];let command=program2;for(const token of argv){if(token.startsWith("-"))continue;const next=command.commands.find(sub=>sub.name()===token||sub.aliases().includes(token));if(next){names.push(token);command=next}else if(command.commands.length===0)break}return names.join(" ")}function wantsJson(argv){return argv.some((token,i)=>token==="--output=json"||token==="-ojson"||(token==="-o"||token==="--output")&&argv[i+1]==="json")}var record=value=>value!==null&&typeof value==="object"&&!Array.isArray(value)?value:{};var message=error=>oneLine(error instanceof CliError&&error.hint?`${error.message} ${error.hint}`:error instanceof Error?error.message:String(error));var oneLine=text2=>readable(text2).trim().replace(/\n\s*/g,"; ");var check=(id2,state,detail)=>({id:id2,state,detail});var dynamic=value=>typeof value!=="string"||value.includes("${{");var condition=value=>{if(value===void 0||value===true)return true;if(value===false)return false;if(typeof value==="string"){const compact2=value.replace(/\s/g,"");if(compact2==="true"||compact2===`\${{true}}`)return true;if(compact2==="false"||compact2===`\${{false}}`)return false}return void 0};function git(cwd,args){return execFileSync("git",args,{cwd,encoding:"utf8",stdio:["ignore","pipe","pipe"],maxBuffer:16*1024*1024})}function rootDirectory(cwd){return findWorkspace(cwd)??resolve3(cwd)}function regularFile(root,path){const full=join9(root,path);try{const rel=relative3(realpathSync3(root),realpathSync3(full));return rel!==".."&&!rel.startsWith(`..${sep}`)&&!isAbsolute3(rel)&&lstatSync(full).isFile()}catch{return false}}function read(root,path){if(!regularFile(root,path))throw new Error(`${path}: missing regular file inside the working tree`);if(lstatSync(join9(root,path)).size>1024*1024)throw new Error(`${path}: exceeds the 1 MiB static inspection limit`);return readFileSync12(join9(root,path))}function ignoredByGit(root){try{git(root,["check-ignore","-q","."]);return true}catch{return false}}function workingFiles(root){let paths;try{if(ignoredByGit(root))throw new Error("workspace is ignored by Git");paths=git(root,["ls-files","--cached","--others","--exclude-standard","-z"]).split("\0").filter(Boolean)}catch{paths=[];const walk=directory=>{for(const entry of readdirSync(join9(root,directory),{withFileTypes:true})){if([".git",".gauge","node_modules",".next",".venv","venv"].includes(entry.name))continue;const path=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory())walk(path);else if(entry.isFile())paths.push(path);if(paths.length>5e4)throw new Error("Working tree exceeds 50,000 files; initialize Git and ignore generated files before verifying")}};walk("")}return[...new Set(paths)].filter(path=>regularFile(root,path)).sort()}var skippedInputEntry=name=>[".git",".gauge","node_modules",".aws",".ssh",".npmrc",".pypirc"].includes(name)||name===".env"||name.startsWith(".env.");function diskFiles(root,base){const paths=[];const walk=directory=>{for(const entry of readdirSync(join9(root,directory),{withFileTypes:true})){if(skippedInputEntry(entry.name))continue;const path=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory())walk(path);else if(entry.isFile())paths.push(path);if(paths.length>2e4)throw new Error("Input exceeds 20,000 files")}};walk(base==="."?"":base);return paths.sort()}function workflowChecks(root,input){if(input.source.type!=="github-actions")return[];const source2=input.source;const checks=[];let workflow;try{const document=parseDocument2(read(root,source2.workflow).toString("utf8"),{uniqueKeys:true});if(document.errors.length)throw new Error(document.errors.map(message).join("; "));workflow=record(document.toJS());if(!Object.keys(record(workflow.jobs)).length)throw new Error("Expected a workflow jobs mapping");checks.push(check("workflow","yes",`${source2.workflow} exists with a parseable YAML jobs mapping`))}catch(error){return[check("workflow","no",message(error))]}const jobs=Object.entries(record(workflow.jobs)).map(([name,value])=>({name,job:record(value)})).filter(({job})=>condition(job.if)!==false);const steps=jobs.flatMap(({name,job})=>Array.isArray(job.steps)?job.steps.map(value=>({job:name,jobCondition:condition(job.if),step:record(value)})).filter(({step})=>condition(step.if)!==false):[]);const opaque=jobs.some(({job})=>typeof job.uses==="string")||steps.some(({step})=>typeof step.uses==="string"&&!/^actions\/(checkout|upload-artifact|setup-[^@]+)@/.test(step.uses));const uploads=steps.filter(({step})=>typeof step.uses==="string"&&step.uses.startsWith("actions/upload-artifact@"));const candidates=uploads.filter(({step})=>{const name=record(step.with).name??"artifact";return!source2.artifact||dynamic(name)||minimatch2(String(name),source2.artifact,{nonegate:true})});const recognizedUpload=candidates.length===1&&!dynamic(record(candidates[0].step.with).name??"artifact");checks.push(check("artifact-upload",recognizedUpload?"yes":"unknown",recognizedUpload?`One direct upload step declares the matching artifact ${String(record(candidates[0].step.with).name??"artifact")}`:candidates.length?`Found ${candidates.length} potentially matching upload step(s); resolve dynamic artifact names or overlapping uploads to establish a unique selection`:`No direct upload step matches ${source2.artifact??"the configured artifact"}; ${opaque?"inspect the referenced reusable workflows or custom actions":"inspect build scripts or declare a matching actions/upload-artifact step"}`));for(const[index,{job,jobCondition,step}]of candidates.entries()){const options=record(step.with);const version=/^actions\/upload-artifact@v(\d+)(?:\.\d+){0,2}$/.exec(String(step.uses));const major=version?Number(version[1]):void 0;let state=source2.artifact&&dynamic(options.name??"artifact")?"unknown":major===7?options.archive===true||options.archive==="true"?"yes":options.archive===void 0||options.archive===false||options.archive==="false"?"no":"unknown":major!==void 0&&major>=4&&major<=6?"yes":"unknown";const conditional=jobCondition===void 0||condition(step.if)===void 0;if(state==="no"&&conditional)state="unknown";const detail=source2.artifact&&dynamic(options.name??"artifact")?"Resolve the artifact name expression to determine whether this upload is selected":major===7?options.archive===true||options.archive==="true"?"archive: true declares a ZIP artifact":options.archive===void 0||options.archive===false||options.archive==="false"?`archive: true is required for Gauge's ZIP download${conditional?"; resolve the upload condition to determine whether this incompatible upload is selected":""}`:"Resolve the archive expression; Gauge requires archive: true":major!==void 0&&major>=4&&major<=6?"This action version produces ZIP artifacts":"Resolve the pinned action version; ZIP format is recognized for upload-artifact v4\u2013v7 (v7 requires archive: true)";checks.push(check(`artifact-format.${index}`,state,`${job}: ${String(step.uses)}; ${detail}`))}const checkouts=steps.filter(({step})=>typeof step.uses==="string"&&step.uses.startsWith("actions/checkout@"));const events=typeof workflow.on==="string"?[workflow.on]:Array.isArray(workflow.on)?workflow.on:Object.keys(record(workflow.on));const pr=events.includes("pull_request");const recognizedCheckout=step=>{const options=record(step.with);if(options.repository!==void 0)return false;if(!events.length||options.ref!==void 0&&typeof options.ref!=="string")return false;const ref=typeof options.ref==="string"?options.ref.replace(/\s/g,""):"";return events.every(event=>["pull_request","push","workflow_dispatch"].includes(event))&&(!ref||ref===`\${{github.sha}}`)||ref===`\${{github.event.pull_request.head.sha||github.sha}}`||pr&&events.every(event=>event==="pull_request")&&ref===`\${{github.event.pull_request.head.sha}}`};const checkoutState=checkouts.length>0&&checkouts.every(({step})=>recognizedCheckout(step))?"yes":"unknown";checks.push(check("candidate-checkout",checkoutState,checkoutState==="yes"?"Workflow checkout recognized, including the default PR merge checkout":"Could not inspect checkout settings; check that your workflow builds the intended code"));const filtered=Object.values(record(workflow.on)).some(value=>["paths","paths-ignore","branches","branches-ignore","types"].some(key=>record(value)[key]!==void 0));const eligible=events.filter(event=>["pull_request","push","workflow_dispatch"].includes(String(event)));const knownEvents=events.length>0&&events.every(event=>!dynamic(event));checks.push(check("workflow-trigger",eligible.length?"yes":knownEvents?"no":"unknown",eligible.length?`Eligible trigger(s) declared: ${eligible.join(", ")}${filtered?"; event filters are present":""}`:knownEvents?`Declared event(s) ${events.join(", ")} are not eligible artifact sources; declare push, pull_request, or workflow_dispatch`:"Could not read workflow events; declare push, pull_request, or workflow_dispatch"));return checks}function inputChecks(root,files,input,local){const checks=[check("configuration","yes",`Supported ${input.type} input from ${input.source.type==="git"?"project files":input.source.type}`)];if(input.type==="website")return checks;if(input.type==="binary"){const excluded=Object.entries(input.commands).filter(([,target])=>!input.paths.some(pattern=>minimatch2(target,pattern,{dot:true,nocase:false,nonegate:true})));checks.push(check("command-selection",excluded.length?"no":"yes",excluded.length?`Declared command targets are excluded by paths: ${excluded.map(([name,target])=>`${name}: ${target}`).join(", ")}`:"Declared command targets match selected paths"))}if(input.source.type==="github-actions"&&!local)checks.push(...workflowChecks(root,input));else{try{const patterns=input.type==="files"||input.type==="binary"?input.paths:input.type==="skill"&&!isSkillArchive(input.path)?[input.path==="."?"**":`${input.path}/**`]:[input.path];const selected=local&&input.type==="npm-package"?globSync(input.path,{cwd:root}):matchInputPaths(local?diskFiles(root,input.type==="skill"&&!isSkillArchive(input.path)?input.path:""):files,patterns);if(input.type==="binary"){for(const[command,target]of Object.entries(input.commands)){if(!selected.includes(target))throw new Error(`Binary command ${command} target is missing or excluded by paths: ${target}`)}}if(input.type==="web-fixture")parseWebFixtureInput(read(root,input.path).toString("utf8"));if(input.type==="npm-package"&&(selected.length!==1||!selected[0].endsWith(".tgz")))throw new Error("npm-package must select exactly one .tgz file");if(input.type==="skill"&&!isSkillArchive(input.path)&&!selected.includes(input.path==="."?"SKILL.md":`${input.path}/SKILL.md`))throw new Error("Skill directory must contain SKILL.md at its root");checks.push(check("local-paths","yes",`${selected.length} ${local?"local":"working-tree"} file(s) selected`))}catch(error){checks.push(check("local-paths","no",message(error).replace("committed files",local?"local files":"working-tree files")))}}return checks}function verifyEvals(cwd=process.cwd(),{local=false}={}){const root=rootDirectory(cwd);const result={version:1,mode:"local-static",root,status:"passed",summary:{yes:0,no:0,unknown:0},facts:{configFilePresent:null,caseCount:null},info:[],notChecked:["github-app-activation","account-settings","mcp-server-access","mcp-server-reachability","live-input-delivery","artifact-contents","sandbox-installation","workflow-execution"],global:[],inputs:[]};const finish=()=>{for(const item of[...result.global,...result.inputs.flatMap(input=>input.checks)])result.summary[item.state]++;result.status=result.summary.no>0?"failed":result.summary.unknown>0?"incomplete":"passed";return result};let raw={};let files;try{files=workingFiles(root)}catch(error){result.global.push(check("working-tree","no",message(error)));return finish()}try{result.facts.configFilePresent=files.includes("gauge.json")||regularFile(root,"gauge.json");if(result.facts.configFilePresent)raw=JSON.parse(read(root,"gauge.json").toString("utf8"));else result.info.push({id:"config-file-absent",detail:"Optional gauge.json is absent; using default case discovery and no inputs"})}catch(error){result.global.push(check("configuration","no",message(error)));return finish()}const config=runDefinitionFileConfigSchema.safeParse(raw);result.global.push(check("configuration",config.success?"yes":"no",config.success?"Configuration matches the supported gauge.json schema":oneLine(config.error.message)));const rawInputs=record(record(raw).inputs);for(const[name,value]of Object.entries(rawInputs).sort(([a],[b])=>a.localeCompare(b))){const parsed=runDefinitionInputSchema.safeParse(value);result.inputs.push({name,checks:parsed.success?inputChecks(root,files,parsed.data,local):[check("configuration","no",oneLine(parsed.error.message))]})}if(config.success){try{const paths=matchInputPaths(files,config.data.cases,{allowEmptySelection:true});if(paths.length>20||paths.some(path=>!path.endsWith(".md")))throw new Error("Expected at most 20 Markdown cases");result.facts.caseCount=paths.length;if(paths.length)result.global.push(check("case-discovery","yes",`${paths.length} working-tree case(s) selected`));else result.info.push({id:"no-cases",detail:"No eval cases found; input-only setup is valid, but no evaluations will run"});for(const path of paths){try{const item=parseRunDefinitionCase(path,read(root,path));if(local)assertLocalCapabilities(item.config,`${path}: config.`);const missing=(item.config.inputs??[]).filter(name=>!Object.hasOwn(config.data.inputs,name));if(missing.length)throw new Error(`${path}: Unknown input(s): ${missing.join(", ")}`);const commandOwners=new Map;mergeWebFixtures((item.config.inputs??Object.keys(config.data.inputs)).flatMap(name=>{const input=config.data.inputs[name];return input.type==="web-fixture"&&(local||input.source.type==="git")?[parseWebFixtureInput(read(root,input.path).toString("utf8"))]:[]}));for(const name of new Set(item.config.inputs??Object.keys(config.data.inputs))){const input=config.data.inputs[name];if(input.type!=="binary")continue;for(const command of Object.keys(input.commands)){const owner=commandOwners.get(command);if(owner)throw new Error(`${path}: Inputs ${owner} and ${name} both expose command ${command}; choose distinct commands or separate cases`);commandOwners.set(command,name)}}result.global.push(check(`case.${path}`,"yes","Case syntax, input references, and declared command names are valid"))}catch(error){result.global.push(check(`case.${path}`,"no",message(error)))}}}catch(error){result.global.push(check("case-discovery","no",message(error).replace("committed files","working-tree files")))}}else result.info.push({id:"case-validation-skipped",detail:"Fix gauge.json before validating cases and input references"});return finish()}function printEvalVerification(result){console.log(`Local static verification: ${result.root}`);const print2=checks=>{for(const item of checks)console.log(` [${item.state}] ${item.id}: ${item.detail}`)};console.log("Global");print2(result.global);for(const input of result.inputs){console.log(`Input: ${input.name}`);print2(input.checks)}for(const item of result.info)console.log(`[info] ${item.id}: ${item.detail}`);console.log(`Static verification ${result.status}: ${result.summary.yes} yes, ${result.summary.no} no, ${result.summary.unknown} unknown.`);console.log("Scope: repository setup only. Live delivery, account settings, and sandbox execution were not tested; no sessions were launched.")}var LIMIT=512*1024*1024;var SNAPSHOT_FILES=100;function parseInputBindings(values){const bindings=new Map;for(const value of values){const split=value.indexOf("=");const name=split<0?value:value.slice(0,split);const path=split<0?void 0:value.slice(split+1);if(!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)||path==="")throw new CliError("usage","--input expects name or name=path");if(bindings.has(name))throw new CliError("usage",`Duplicate --input: ${name}`);bindings.set(name,path)}return bindings}var isGlob=path=>/[*?[\]{}]/.test(path);function rejectSkippedPatterns(name,patterns){for(const pattern of patterns){const skipped=pattern.split("/").find(skippedInputEntry);if(skipped===".gauge")throw new CliError("usage",`Input ${name}: ${pattern} is inside .gauge/. Inputs never include .gauge/, because it holds run results and local config.`,{hint:"Move the files outside .gauge/ and update the glob."});if(skipped)throw new CliError("usage",`Input ${name}: ${pattern} is inside ${skipped}, which inputs never include`,{hint:`Move the files outside ${skipped} and update the glob.`})}}async function walkInput(root,depth=Infinity){const paths=[];const walk=async(directory,level)=>{for(const entry of await readdir(join10(root,directory),{withFileTypes:true})){if(skippedInputEntry(entry.name)||!entry.isFile()&&!entry.isDirectory())continue;const next=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory()){if(level<depth)await walk(next,level+1)}else paths.push(next);if(paths.length>2e4)throw new CliError("usage","Input exceeds 20,000 files")}};await walk("",1);return paths.sort()}function selectPaths(name,paths,patterns){rejectSkippedPatterns(name,patterns);try{return matchInputPaths(paths,patterns)}catch(error){throw new CliError("usage",`Input ${name}: ${message(error).replace("committed files","files")}`)}}async function expandOverrideGlob(name,glob){const segments=glob.split(sep2);const first=segments.findIndex(isGlob);const root=segments.slice(0,first).join(sep2)||sep2;const pattern=segments.slice(first).join("/");const info=await stat(root).catch(()=>void 0);if(!info?.isDirectory())throw new CliError("usage",`Input ${name}: ${root} is not a directory`);const depth=pattern.includes("**")?Infinity:pattern.split("/").length;return{root,pattern,files:selectPaths(name,await walkInput(root,depth),[pattern])}}async function selectLocalInput(resource,supplied,override){let path=resolve4(supplied);let info=await lstat(path).catch(()=>void 0);if(!info&&override&&isGlob(path)){if(resource.use.type==="skill")throw new CliError("usage",`Input ${resource.name}: Skill inputs take an explicit directory or archive, not a glob`);const{root:root2,pattern,files}=await expandOverrideGlob(resource.name,path);if(resource.use.type==="files")return{use:{type:"files"},root:root2,files,skillDirectory:false};if(resource.use.type==="binary")return{use:{...resource.use,paths:[pattern]},root:root2,files,skillDirectory:false};if(files.length!==1)throw new CliError("usage",`Input ${resource.name}: ${supplied} matches ${files.length} files; keep one`);path=join10(root2,files[0]);info=await lstat(path)}if(!info)throw new CliError("usage",`Input path does not exist: ${supplied}`,{hint:"Pass --input <name>=<path>, or fix the input's path in gauge.json."});if(!info.isFile()&&!info.isDirectory())throw new CliError("usage",`Input must be an ordinary file or directory: ${supplied}`);let use=resource.use;if(use.type==="web-fixture"){if(!info.isFile())throw new CliError("usage","Supply a web fixture JSON file");use={type:"web-fixture",path:basename(path)}}else if(use.type==="skill"){if(info.isFile()&&!isSkillArchive(path))throw new CliError("usage","Supply a Skill directory, .zip, or .tar.gz archive");use={...use,root:info.isDirectory()?".":basename(path)}}else if(use.type==="npm-package"||use.type==="npm-cli"){if(!info.isFile()||!path.endsWith(".tgz"))throw new CliError("usage","Supply the packed npm .tgz file");use={...use,package:basename(path)}}else if(override&&use.type==="files")use={type:"files"};else if(override&&use.type==="binary")use={...use,paths:[info.isDirectory()?"**":basename(path)]};const root=info.isDirectory()?path:dirname(path);const paths=info.isDirectory()?await walkInput(root):[basename(path)];const patterns=!override&&(use.type==="files"||use.type==="binary")?use.paths:void 0;return{use,root,files:patterns?selectPaths(resource.name,paths,patterns):paths,skillDirectory:use.type==="skill"&&info.isDirectory()}}async function packLocalInput(resource,supplied,output,{override=false}={}){if(resource.use.type==="website")throw new CliError("usage",`Input ${resource.name}: website inputs cannot be supplied through --input.`,{hint:"Use the configured public preview with --hosted. For content-only alternatives and their discovery limits, read `gauge instructions hillclimb`."});const{use,root,files,skillDirectory}=await selectLocalInput(resource,supplied,override);if(!files.length)throw new CliError("usage","Input contains no selected files");if(use.type==="binary"){for(const[command,target]of Object.entries(use.commands))if(!files.includes(target))throw new CliError("usage",`Input ${resource.name}: binary command ${command} target is missing: ${target}`,override?{hint:"Command targets are relative to the supplied path."}:void 0)}if(skillDirectory&&!files.includes("SKILL.md"))throw new CliError("usage","Skill directory must contain SKILL.md at its root");let total=0;for(const file of files){inputPathSchema.parse(file);const entry=await lstat(join10(root,file));if(!entry.isFile())throw new CliError("usage",`Input file changed or is not an ordinary file: ${file}`);total+=entry.size;if(total>LIMIT)throw new CliError("usage","Input exceeds 512 MiB")}await tar({cwd:root,file:output,gzip:true,portable:true,noMtime:true,follow:false,noDirRecurse:true},files.map(file=>`./${file}`));const bytes=(await stat(output)).size;if(bytes>LIMIT)throw new CliError("usage","Input archive exceeds 512 MiB");const hash=createHash5("sha256");for await(const chunk of createReadStream2(output))hash.update(chunk);const sha256=`sha256:${hash.digest("hex")}`;if(use.type==="web-fixture")await readWebFixtureArchive(output,use.path,sha256);return{use,upload:{path:output,bytes,sha256},files}}async function loadLocalRunDefinition(files,values,cwd=process.cwd(),{local=false,spec}={}){const directory=await mkdtemp(join10(tmpdir(),"gauge-inputs-"));const cleanup=()=>rm(directory,{recursive:true,force:true});try{const root=rootDirectory(cwd);const paths=workingFiles(root);const blobs=new Map;if(paths.includes("gauge.json"))blobs.set("gauge.json",read(root,"gauge.json"));const configBytes=blobs.get("gauge.json");let config;try{config=runDefinitionFileConfigSchema.parse(configBytes?JSON.parse(configBytes.toString("utf8")):{})}catch(error){throw new CliError("usage",`gauge.json is not valid: ${error instanceof Error?error.message:error}`,{hint:"Run `gauge run --verify` for each problem."})}const patterns=spec?[]:files.length?files.map(file=>{const path=relative4(root,resolve4(cwd,file));if(!path||path===".."||path.startsWith(`..${sep2}`)||isAbsolute4(path))throw new CliError("usage","Case paths must be inside the definition directory");return path.split(sep2).join("/")}):config.cases;const cases=spec?[]:files.length?matchInputPaths(paths,patterns):matchInputPaths(paths,patterns,{allowEmptySelection:true});if(!spec&&cases.length===0)throw noCasesError();if(cases.length>20||cases.some(path=>!path.endsWith(".md")))throw new CliError("usage","Expected 1\u201320 Markdown cases");for(const path of cases)blobs.set(path,read(root,path));const parsed=spec?caseSpecCases(spec):cases.map(path=>parseRunDefinitionCase(path,blobs.get(path)??Buffer.alloc(0)));const unmeasured=parsed.filter(item=>!item.criteria&&!item.preference);if(!local&&unmeasured.length)throw new CliError("usage",`Hosted cases need criteria or preference: ${unmeasured.map(item=>item.path).join(", ")}`,{hint:"Add criteria, or run locally with --local."});const bindings=parseInputBindings(values);for(const name of bindings.keys())if(!(name in config.inputs))throw new CliError("usage",`Unknown input: ${name}`,{hint:`gauge.json defines: ${Object.keys(config.inputs).join(", ")||"none"}`});for(const item of parsed)for(const name of item.config.inputs??[])if(!(name in config.inputs))throw new CliError("usage",`${item.path}: unknown input "${name}". gauge.json defines: ${Object.keys(config.inputs).join(", ")||"none"}`);const selected=new Set(parsed.flatMap(item=>item.config.inputs??Object.keys(config.inputs)));for(const[name,override]of bindings){if(!selected.has(name))throw new CliError("usage",`Input is not used by the selected cases: ${name}`);if(override!==void 0)rejectSkippedPatterns(name,[relative4(root,resolve4(cwd,override)).split(sep2).join("/")])}const git2=args=>{try{return execFileSync2("git",args,{cwd:root,encoding:"utf8",stdio:["ignore","pipe","pipe"]}).trim()}catch{return null}};const commit=git2(["rev-parse","--verify","HEAD^{commit}"]);const origin=git2(["remote","get-url","origin"]);const repoUrl=origin&&uncredentialedRepoUrlSchema.safeParse(origin).success?origin:null;const unattachedInputs=Object.keys(config.inputs).filter(name=>!selected.has(name));const resources=[];const uploads=[];const inputSnapshots=[];for(const[name,input]of Object.entries(config.inputs)){if(!selected.has(name))continue;if(local&&input.type==="website")throw new CliError("usage",`Website input "${name}" requires hosted Gauge to replace the original site with your preview.`,{hint:"Use --hosted with a public preview. For local runs, supply documentation files or a preview URL in the prompt. These give the agent your content directly rather than testing whether it finds your site. See `gauge instructions hillclimb`."});const original=compileDefinitionInput(name,input,repoUrl??"",commit??"");const override=bindings.get(name);const supplied=override??(original.source.type==="git"?workingTreeInput(root,name,input):void 0);if(supplied!==void 0){const{use,upload,files:files2}=await packLocalInput(original,resolve4(cwd,supplied),join10(directory,`${name}.tar.gz`),{override:override!==void 0});uploads.push(upload);inputSnapshots.push({name,source:override!==void 0?"override":"working-tree",path:resolve4(cwd,supplied),gaugeArchive:{sha256:upload.sha256,bytes:upload.bytes},files:files2.slice(0,SNAPSHOT_FILES),fileCount:files2.length});resources.push({...original,use,source:{type:"upload",sha256:upload.sha256,bytes:upload.bytes,repoUrl,commit}})}else{if(local)throw new CliError("usage",`Input ${name} has a remote source. Supply --input ${name}=<path> for local execution, or use --hosted.`);if(!repoUrl||!commit)throw new CliError("usage",`Input ${name}'s configured ${original.source.type} source requires a Git checkout with an origin.`,{hint:input.type==="website"?"Configure the repository origin and publish a matching public preview through the declared source. Website inputs cannot use --input; see `gauge instructions hillclimb` for content-only alternatives.":`Supply --input ${name}=<path> to use local files.`});resources.push(original)}}if(!spec){const compiled=compileRunDefinitionFromBlobs({repoUrl,commit,resources,blobs,casePaths:cases,verification:"LOCAL"});const webFixtures=await freezeWebFixtures({...compiled.definition,resources:resources.filter(r=>r.source.type==="upload")},uploads);return{...compiled,webFixtures,uploads,inputSnapshots,unattachedInputs,cleanup}}const definition={version:2,source:{repoUrl,commit,digest:caseSpecDigest(spec),verification:"LOCAL"},resources,cases:parsed};if(!local)runDefinitionSubmissionSchema.parse(definition);return{definition,webFixtures:await freezeWebFixtures({...definition,resources:resources.filter(r=>r.source.type==="upload")},uploads),org:config.orgId??config.org,uploads,inputSnapshots,unattachedInputs,cleanup}}catch(error){await cleanup();if(error instanceof CliError||!(error instanceof Error))throw error;throw new CliError("usage",error.message,{hint:"Check the case format in `gauge instructions run`, then run `gauge run --verify`."})}}function workingTreeInput(root,name,input){switch(input.type){case"website":throw new CliError("usage",`Input ${name} is a website; website inputs run on hosted Gauge only`);case"files":case"binary":return root;case"web-fixture":case"skill":return join10(root,input.path);case"npm-package":{const matches2=globSync2(input.path,{cwd:root});if(matches2.length!==1)throw new CliError("usage",matches2.length?`Input ${name}: ${input.path} matches ${matches2.length} files; keep one`:`Input ${name}: no file matches ${input.path}. Run \`npm pack\` first`);return join10(root,matches2[0])}}}async function uploadLocalInputs(client,org,uploads){for(const input of uploads){const signed2=await client.post(`/api/v1/orgs/${encodeURIComponent(org)}/run-definitions/uploads`,{sha256:input.sha256,bytes:input.bytes});const signal=AbortSignal.timeout(10*60*1e3);for(let attempt=0;attempt<3;attempt++){const body2=createReadStream2(input.path);let response;try{response=await fetch(signed2.url,{method:"PUT",headers:signed2.headers,body:body2,duplex:"half",redirect:"error",signal})}finally{body2.destroy()}await response.body?.cancel();if(response.ok||response.status===412)break;if(response.status!==409||attempt===2)throw new CliError("network",`Input upload failed (${response.status})`);await delay2(100*2**attempt,void 0,{signal})}}}var SIDE_KEYS=["request","batch","eval","revision","experiment","trial","agent","model","since","until"];var SELECTING_KEYS=new Set(["request","batch","eval","experiment","trial"]);function csv4(value){return value.split(",").map(part=>part.trim()).filter(Boolean)}function parseCompareSide(flag2,pairs){const side={};const seen=new Set;for(const pair2 of pairs){const eq=pair2.indexOf("=");const key=eq<0?"":pair2.slice(0,eq).trim();const value=eq<0?"":pair2.slice(eq+1).trim();if(!SIDE_KEYS.includes(key)||!value)throw new CliError("usage",`invalid ${flag2} "${pair2}" (expected key=value with key one of ${SIDE_KEYS.join(", ")})`);if(seen.has(key)&&key!=="batch")throw new CliError("usage",`${flag2} ${key}= given twice`);seen.add(key);if(key==="request")side.runRequest=value;else if(key==="batch")side.batch=[...side.batch??[],...csv4(value)];else if(key==="eval")side.evalSet=value;else if(key==="revision")side.evalRevision=value;else if(key==="experiment")side.experiment=value;else if(key==="trial")side.trial=value;else if(key==="agent")side.agent=parseAgents(value);else if(key==="model")side.model=csv4(value);else if(key==="since")side.since=value;else side.until=value}if(![...seen].some(key=>SELECTING_KEYS.has(key)))throw new CliError("usage",`${flag2} needs request=, batch=, eval=, experiment=, or trial= to select runs`);return side}function pct2(rate){return rate===null?"":`${Math.round(rate*100)}%`}function passCell(stats){return stats.judged===0?"":`${pct2(stats.passRate)} (${stats.passed}/${stats.judged})`}function signed(value,format){if(value===null)return"";return`${value>0?"+":value<0?"-":"\xB1"}${format(Math.abs(value))}`}var points=rate=>`${Math.round(rate*100)}pp`;var turns=n=>String(Math.round(n*10)/10);var usd=n=>`$${n.toFixed(2)}`;var secs=ms=>`${Math.round(ms/1e3)}s`;function pair(before,after,delta3,format){if(before===null&&after===null)return"";const side=v=>v===null?"\u2014":format(v);return`${side(before)} \u2192 ${side(after)}${delta3===null?"":` (${signed(delta3,format)})`}`}function row(target,item){const{baseline:a,candidate:b,delta:delta3}=item;return{target,runs:`${a.runs} \u2192 ${b.runs}`,completed:`${a.succeeded}/${a.runs} \u2192 ${b.succeeded}/${b.runs}`,baseline:passCell(a),candidate:passCell(b),"\u0394 pass":signed(delta3.passRate,points),turns:pair(a.medianTurns,b.medianTurns,delta3.medianTurns,turns),cost:pair(a.medianUsdCost,b.medianUsdCost,delta3.medianUsdCost,usd),duration:pair(a.medianDurationMs,b.medianDurationMs,delta3.medianDurationMs,secs)}}function compareWarnings(result){const warnings=[];for(const name of["baseline","candidate"]){const side=result.sides[name];const stats=result.overall[name];if(stats.runs===0)warnings.push(`${name} matched no runs. Check its selectors; optimization trial runs need trial=<trialId>.`);else if(stats.finished<stats.runs)warnings.push(`${name} has ${stats.runs-stats.finished} of ${stats.runs} runs still in progress, so its numbers are partial. Run \`gauge run wait <request-id>\` first.`);if(stats.judged<stats.finished)warnings.push(`${name} has ${stats.finished-stats.judged} finished runs without a verdict (not judged yet, or no criteria). Its pass rate covers judged runs only.`);for(const mixed of side.mixedRevisions??[])warnings.push(`${name}: ${mixed.evalSetId?`eval ${mixed.evalSetId}`:`case prompt ${mixed.promptId}`} ran under ${mixed.revisions.length} revisions (${mixed.revisions.join(", ")}): it was edited between runs. Add --${name} revision=<hash> to pick one.`);if(side.truncated)warnings.push(`${name} matched too many runs; only the newest were compared. Narrow it with since=, until=, or batch=.`)}return warnings}function printComparison(result){const revisions=name=>result.sides[name].evalRevisions.join(", ")||"\u2014";console.log(`Baseline revision: ${revisions("baseline")}. Candidate revision: ${revisions("candidate")}.`);for(const name of["baseline","candidate"]){for(const trial of result.sides[name].trials??[])console.log(`${name}: ${trial.runs} run${trial.runs===1?"":"s"} from optimization trial ${trial.key} "${trial.title}" (${trial.kind}).`)}console.log("Medians are baseline \u2192 candidate (difference).");console.log("");printItems([row("all",result.overall),...result.targets.map(target=>row(`${target.agent} ${target.model||"(default)"}`,target))],"table");const before=new Map(result.overall.baseline.criteria.map(c=>[c.name,c]));const after=new Map(result.overall.candidate.criteria.map(c=>[c.name,c]));if(result.overall.delta.criteria.length>0){console.log("");printItems(result.overall.delta.criteria.map(c=>{const a=before.get(c.name);const b=after.get(c.name);return{criterion:c.name,baseline:a&&a.judged>0?`${pct2(a.passRate)} (${a.passed}/${a.judged})`:"",candidate:b&&b.judged>0?`${pct2(b.passRate)} (${b.passed}/${b.judged})`:"","\u0394 pass":signed(c.passRate,points)}}),"table")}}var LOCAL_AGENTS2=new Set(["CLAUDE_CODE","CODEX_CLI","CURSOR","PI","OPENCODE"]);var LOCAL_TERMINAL_STATES=new Set(["completed","failed","canceled","interrupted"]);function localCompareFilter(flag2,side){const hostedOnly2=[side.runRequest!==void 0&&"request=",side.experiment!==void 0&&"experiment=",side.trial!==void 0&&"trial="].filter(Boolean);if(hostedOnly2.length>0)throw new CliError("usage",`${flag2} ${hostedOnly2.join(", ")} select hosted runs; local sides take batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=`);if((side.batch?.length??0)>1)throw new CliError("usage",`${flag2} takes one batch= locally`);if((side.agent?.length??0)>1||(side.model?.length??0)>1)throw new CliError("usage",`${flag2} takes one agent= and one model= locally`);if(side.evalRevision!==void 0&&!/^[1-9]\d*$/.test(side.evalRevision))throw new CliError("usage",`${flag2} local revision= must be a positive integer`);return{batch:side.batch?.[0],evalId:side.evalSet,measurementRevision:side.evalRevision===void 0?void 0:Number(side.evalRevision),agent:side.agent?.[0],model:side.model?.[0],since:side.since?parseSince(side.since,Date.now(),"since="):void 0,until:side.until?parseSince(side.until,Date.now(),"until="):void 0}}function median(values){if(values.length===0)return null;const sorted=[...values].sort((a,b)=>a-b);const mid=sorted.length>>1;return sorted.length%2?sorted[mid]:(sorted[mid-1]+sorted[mid])/2}function localStats(runs){const judged=runs.filter(run=>run.criterionResults?.length&&run.criterionResults.every(criterion=>criterion.outcome!=="UNASSESSABLE"));const passed=judged.filter(run=>run.criterionResults.every(criterion=>criterion.outcome==="PASS")).length;const byCriterion=new Map;for(const run of runs)for(const criterion of run.criterionResults??[]){const current=byCriterion.get(criterion.name)??{name:criterion.name,judged:0,passed:0};if(criterion.outcome!=="UNASSESSABLE"){current.judged++;if(criterion.outcome==="PASS")current.passed++}byCriterion.set(criterion.name,current)}const recorded=values=>values.filter(value=>typeof value==="number"&&Number.isFinite(value));return{runs:runs.length,finished:runs.filter(run=>LOCAL_TERMINAL_STATES.has(run.outcome)).length,succeeded:runs.filter(run=>run.outcome==="completed").length,judged:judged.length,passed,passRate:judged.length?passed/judged.length:null,medianTurns:median(recorded(runs.map(run=>run.turns))),medianUsdCost:null,medianDurationMs:median(recorded(runs.map(run=>run.durationMs))),criteria:[...byCriterion.values()].map(criterion=>({...criterion,passRate:criterion.judged?criterion.passed/criterion.judged:null}))}}function diff(a,b){return a===null||b===null?null:b-a}function localRow(baseline,candidate){const a=localStats(baseline);const b=localStats(candidate);return{baseline:a,candidate:b,delta:{passRate:diff(a.passRate,b.passRate),medianTurns:diff(a.medianTurns,b.medianTurns),medianUsdCost:null,medianDurationMs:diff(a.medianDurationMs,b.medianDurationMs),criteria:[...new Set([...a.criteria,...b.criteria].map(criterion=>criterion.name))].map(name=>({name,passRate:diff(a.criteria.find(criterion=>criterion.name===name)?.passRate??null,b.criteria.find(criterion=>criterion.name===name)?.passRate??null)}))}}}function compareLocalRuns(baseline,candidate){const targets=new Map;for(const run of[...baseline,...candidate]){const agent=LOCAL_AGENTS2.has(run.agent)?run.agent:void 0;if(agent)targets.set(`${run.agent} ${run.model}`,{agent,model:run.model})}const forTarget=(runs,key)=>runs.filter(run=>`${run.agent} ${run.model}`===key);return{overall:localRow(baseline,candidate),targets:[...targets].map(([key,target])=>({...target,...localRow(forTarget(baseline,key),forTarget(candidate,key))})),sides:{baseline:localRevisionMetadata(baseline),candidate:localRevisionMetadata(candidate)}}}function localRevisionMetadata(runs){const byMeasurement=new Map;const allRevisions=new Set;for(const run of runs){if(run.measurementRevision===void 0||run.measurementRevision===null)continue;const revision=String(run.measurementRevision);allRevisions.add(revision);if(run.evalId){const revisions=byMeasurement.get(run.evalId)??new Set;revisions.add(revision);byMeasurement.set(run.evalId,revisions)}}return{evalRevisions:[...allRevisions].sort((a,b)=>Number(a)-Number(b)),mixedRevisions:[...byMeasurement].filter(([,revisions])=>revisions.size>1).map(([evalSetId,revisions])=>({evalSetId,promptId:null,revisions:[...revisions]})),truncated:false}}function localCompareWarnings(result){const warnings=compareWarnings(result).filter(warning=>!warning.includes("matched no runs")).map(warning=>warning.replace("gauge run wait <request-id>","gauge run wait --batch <batch-id> --local").replace("revision=<hash>","revision=<number>").replace("not judged yet, or no criteria","analysis missing, unassessable criteria, or no criteria"));for(const name of["baseline","candidate"])if(result.overall[name].runs===0)warnings.unshift(`${name} matched no local runs. Check its selectors: batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=. \`gauge run list --local\` shows the values.`);return warnings}function runPath(id2){return`/api/v1/runs/${encodeURIComponent(id2)}`}function streamPath(id2){return`/api/runs/${encodeURIComponent(id2)}/stream`}function fmtDuration(ms){if(ms==null)return"";const s=Math.round(ms/1e3);return s>=60?`${Math.floor(s/60)}m${s%60}s`:`${s}s`}function fmtUsd(v){return v==null?"":`$${v.toFixed(2)}`}function compact(value,max=140){const s=typeof value==="string"?value:JSON.stringify(value)??"null";const one=s.replace(/\s+/g," ").trim();return one.length>max?`${one.slice(0,max-1)}\u2026`:one}function toRow4(run){const selected=run.selectedProvider?.slug??"";const observed=run.observedProvider?.slug;return{id:run.id,status:run.status,agent:run.agent,model:run.modelDisplayName??run.resolvedModel??run.model,provider:observed&&observed!==selected?`${selected||"?"} \u2192 ${observed}`:selected||observed||"",experiment:run.experiment??"",revision:run.evalRevision??"",trial:run.optimizationTrial?`${run.optimizationTrial.key} ${run.optimizationTrial.trialId}`:"",source:run.triggeredBy?.source??"",turns:run.turns??"",cost:fmtUsd(run.usdCost),credits:run.creditsCharged??"",duration:fmtDuration(run.durationMs),created:run.createdAt,prompt:compact(run.promptSnippet,48)}}function toLocalRow(run){return{id:run.id,status:run.outcome,agent:run.agent,model:run.model,batch:run.batchId,eval:run.evalId??"",turns:run.turns,analyzed:run.analyzed?"yes":"",ended:run.endedAt}}function detailLines(run){return[["Id",run.id],["Status",run.status],["Agent",run.agent],["Model",run.modelDisplayName||run.resolvedModel||run.model||"(platform default)"],["Requested model",run.model||"(platform default)"],["Selected provider",run.selectedProvider?`${run.selectedProvider.displayName} (${run.selectedProvider.slug})`:"\u2014"],["Observed provider",run.observedProvider?`${run.observedProvider.slug} (${run.observedProvider.source.toLowerCase().replaceAll("_"," ")})`:"\u2014"],["Prompt",compact(run.promptText,120)],["Prompt id",run.promptId],["Batch",run.batchId],["Experiment",run.experiment??"\u2014"],["Eval set",run.evalSetId??"\u2014"],["Eval revision",run.evalRevision??"\u2014"],["Optimization trial",run.optimizationTrial?`${run.optimizationTrial.key} "${run.optimizationTrial.title}" (${run.optimizationTrial.kind}), trial ${run.optimizationTrial.trialId} in optimization ${run.optimizationTrial.optimizationId}`:"\u2014"],["Scenario",run.scenario],["Funding",run.funding],["Repo",run.repoUrl?`${run.repoUrl}${run.repoRef?` @ ${run.repoRef}`:""}`:"\u2014"],["Attempt",String(run.attempt)],["Turns",run.turns==null?"\u2014":String(run.turns)],["Triggered by",triggeredByLabel(run.triggeredBy)||"\u2014"],["Cost",fmtUsd(run.usdCost)||"\u2014"],["Credits",run.creditsCharged==null?"\u2014":String(run.creditsCharged)],["Duration",fmtDuration(run.durationMs)||"\u2014"],["Exit reason",run.exitReason??"\u2014"],["Failure reason",run.failureReason??"\u2014"],["Cancel requested",run.cancelRequested?"yes":"no"],["Diff",run.hasDiff?`yes (gauge run diff ${run.id})`:"no"],...run.criterionResults.map(c=>[`Criterion ${c.name}`,c.passed===null?"not assessed":c.passed?"passed":"failed"]),["Created",run.createdAt],["Started",run.startedAt??"\u2014"],["Finished",run.finishedAt??"\u2014"]]}function passLine(label,pass){const bits=[pass.status];if(pass.model)bits.push(pass.model);const cost=fmtUsd(pass.usdCost);if(cost)bits.push(cost);if(pass.error)bits.push(`error: ${compact(pass.error,60)}`);return`${label.padEnd(20)} ${bits.join(" ")}`}var PASS_LABELS={ANALYSIS:"Session analysis",CLI_DISCOVERY:"CLI discovery",LEVER:"Improvement levers",EXPERIMENT_DELTA:"Experiment delta"};var INSTALLED={NONE:"",ATTEMPTED:"attempted",CONFIGURED:"configured",EXERCISED:"exercised"};var check2=value=>value?"yes":"";function printInsight(insight){for(const pass of insight.passes){console.log(passLine(PASS_LABELS[pass.kind]??pass.kind,pass))}const analysis=insight.analysis;if(!analysis){console.log(insight.decision.status==="SKIPPED"?"\n(skipped)":"\n(no analysis yet)");return}if(analysis.limitation)console.log(`
1011
+ `}function commandPath(program2,argv){const names=[];let command=program2;for(const token of argv){if(token.startsWith("-"))continue;const next=command.commands.find(sub=>sub.name()===token||sub.aliases().includes(token));if(next){names.push(token);command=next}else if(command.commands.length===0)break}return names.join(" ")}function wantsJson(argv){return argv.some((token,i)=>token==="--output=json"||token==="-ojson"||(token==="-o"||token==="--output")&&argv[i+1]==="json")}var record=value=>value!==null&&typeof value==="object"&&!Array.isArray(value)?value:{};var message=error=>oneLine(error instanceof CliError&&error.hint?`${error.message} ${error.hint}`:error instanceof Error?error.message:String(error));var oneLine=text2=>readable(text2).trim().replace(/\n\s*/g,"; ");var check=(id2,state,detail)=>({id:id2,state,detail});var dynamic=value=>typeof value!=="string"||value.includes("${{");var condition=value=>{if(value===void 0||value===true)return true;if(value===false)return false;if(typeof value==="string"){const compact2=value.replace(/\s/g,"");if(compact2==="true"||compact2===`\${{true}}`)return true;if(compact2==="false"||compact2===`\${{false}}`)return false}return void 0};function git(cwd,args){return execFileSync("git",args,{cwd,encoding:"utf8",stdio:["ignore","pipe","pipe"],maxBuffer:16*1024*1024})}function rootDirectory(cwd){return findWorkspace(cwd)??resolve3(cwd)}function regularFile(root,path){const full=join9(root,path);try{const rel=relative3(realpathSync3(root),realpathSync3(full));return rel!==".."&&!rel.startsWith(`..${sep}`)&&!isAbsolute3(rel)&&lstatSync(full).isFile()}catch{return false}}function read(root,path){if(!regularFile(root,path))throw new Error(`${path}: missing regular file inside the working tree`);if(lstatSync(join9(root,path)).size>1024*1024)throw new Error(`${path}: exceeds the 1 MiB static inspection limit`);return readFileSync12(join9(root,path))}function ignoredByGit(root){try{git(root,["check-ignore","-q","."]);return true}catch{return false}}function workingFiles(root){let paths;try{if(ignoredByGit(root))throw new Error("workspace is ignored by Git");paths=git(root,["ls-files","--cached","--others","--exclude-standard","-z"]).split("\0").filter(Boolean)}catch{paths=[];const walk=directory=>{for(const entry of readdirSync(join9(root,directory),{withFileTypes:true})){if([".git",".gauge","node_modules",".next",".venv","venv"].includes(entry.name))continue;const path=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory())walk(path);else if(entry.isFile())paths.push(path);if(paths.length>5e4)throw new Error("Working tree exceeds 50,000 files; initialize Git and ignore generated files before verifying")}};walk("")}return[...new Set(paths)].filter(path=>regularFile(root,path)).sort()}var skippedInputEntry=name=>[".git",".gauge","node_modules",".aws",".ssh",".npmrc",".pypirc"].includes(name)||name===".env"||name.startsWith(".env.");function diskFiles(root,base){const paths=[];const walk=directory=>{for(const entry of readdirSync(join9(root,directory),{withFileTypes:true})){if(skippedInputEntry(entry.name))continue;const path=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory())walk(path);else if(entry.isFile())paths.push(path);if(paths.length>2e4)throw new Error("Input exceeds 20,000 files")}};walk(base==="."?"":base);return paths.sort()}function workflowChecks(root,input){if(input.source.type!=="github-actions")return[];const source2=input.source;const checks=[];let workflow;try{const document=parseDocument2(read(root,source2.workflow).toString("utf8"),{uniqueKeys:true});if(document.errors.length)throw new Error(document.errors.map(message).join("; "));workflow=record(document.toJS());if(!Object.keys(record(workflow.jobs)).length)throw new Error("Expected a workflow jobs mapping");checks.push(check("workflow","yes",`${source2.workflow} exists with a parseable YAML jobs mapping`))}catch(error){return[check("workflow","no",message(error))]}const jobs=Object.entries(record(workflow.jobs)).map(([name,value])=>({name,job:record(value)})).filter(({job})=>condition(job.if)!==false);const steps=jobs.flatMap(({name,job})=>Array.isArray(job.steps)?job.steps.map(value=>({job:name,jobCondition:condition(job.if),step:record(value)})).filter(({step})=>condition(step.if)!==false):[]);const opaque=jobs.some(({job})=>typeof job.uses==="string")||steps.some(({step})=>typeof step.uses==="string"&&!/^actions\/(checkout|upload-artifact|setup-[^@]+)@/.test(step.uses));const uploads=steps.filter(({step})=>typeof step.uses==="string"&&step.uses.startsWith("actions/upload-artifact@"));const candidates=uploads.filter(({step})=>{const name=record(step.with).name??"artifact";return!source2.artifact||dynamic(name)||minimatch2(String(name),source2.artifact,{nonegate:true})});const recognizedUpload=candidates.length===1&&!dynamic(record(candidates[0].step.with).name??"artifact");checks.push(check("artifact-upload",recognizedUpload?"yes":"unknown",recognizedUpload?`One direct upload step declares the matching artifact ${String(record(candidates[0].step.with).name??"artifact")}`:candidates.length?`Found ${candidates.length} potentially matching upload step(s); resolve dynamic artifact names or overlapping uploads to establish a unique selection`:`No direct upload step matches ${source2.artifact??"the configured artifact"}; ${opaque?"inspect the referenced reusable workflows or custom actions":"inspect build scripts or declare a matching actions/upload-artifact step"}`));for(const[index,{job,jobCondition,step}]of candidates.entries()){const options=record(step.with);const version=/^actions\/upload-artifact@v(\d+)(?:\.\d+){0,2}$/.exec(String(step.uses));const major=version?Number(version[1]):void 0;let state=source2.artifact&&dynamic(options.name??"artifact")?"unknown":major===7?options.archive===true||options.archive==="true"?"yes":options.archive===void 0||options.archive===false||options.archive==="false"?"no":"unknown":major!==void 0&&major>=4&&major<=6?"yes":"unknown";const conditional=jobCondition===void 0||condition(step.if)===void 0;if(state==="no"&&conditional)state="unknown";const detail=source2.artifact&&dynamic(options.name??"artifact")?"Resolve the artifact name expression to determine whether this upload is selected":major===7?options.archive===true||options.archive==="true"?"archive: true declares a ZIP artifact":options.archive===void 0||options.archive===false||options.archive==="false"?`archive: true is required for Gauge's ZIP download${conditional?"; resolve the upload condition to determine whether this incompatible upload is selected":""}`:"Resolve the archive expression; Gauge requires archive: true":major!==void 0&&major>=4&&major<=6?"This action version produces ZIP artifacts":"Resolve the pinned action version; ZIP format is recognized for upload-artifact v4\u2013v7 (v7 requires archive: true)";checks.push(check(`artifact-format.${index}`,state,`${job}: ${String(step.uses)}; ${detail}`))}const checkouts=steps.filter(({step})=>typeof step.uses==="string"&&step.uses.startsWith("actions/checkout@"));const events=typeof workflow.on==="string"?[workflow.on]:Array.isArray(workflow.on)?workflow.on:Object.keys(record(workflow.on));const pr=events.includes("pull_request");const recognizedCheckout=step=>{const options=record(step.with);if(options.repository!==void 0)return false;if(!events.length||options.ref!==void 0&&typeof options.ref!=="string")return false;const ref=typeof options.ref==="string"?options.ref.replace(/\s/g,""):"";return events.every(event=>["pull_request","push","workflow_dispatch"].includes(event))&&(!ref||ref===`\${{github.sha}}`)||ref===`\${{github.event.pull_request.head.sha||github.sha}}`||pr&&events.every(event=>event==="pull_request")&&ref===`\${{github.event.pull_request.head.sha}}`};const checkoutState=checkouts.length>0&&checkouts.every(({step})=>recognizedCheckout(step))?"yes":"unknown";checks.push(check("candidate-checkout",checkoutState,checkoutState==="yes"?"Workflow checkout recognized, including the default PR merge checkout":"Could not inspect checkout settings; check that your workflow builds the intended code"));const filtered=Object.values(record(workflow.on)).some(value=>["paths","paths-ignore","branches","branches-ignore","types"].some(key=>record(value)[key]!==void 0));const eligible=events.filter(event=>["pull_request","push","workflow_dispatch"].includes(String(event)));const knownEvents=events.length>0&&events.every(event=>!dynamic(event));checks.push(check("workflow-trigger",eligible.length?"yes":knownEvents?"no":"unknown",eligible.length?`Eligible trigger(s) declared: ${eligible.join(", ")}${filtered?"; event filters are present":""}`:knownEvents?`Declared event(s) ${events.join(", ")} are not eligible artifact sources; declare push, pull_request, or workflow_dispatch`:"Could not read workflow events; declare push, pull_request, or workflow_dispatch"));return checks}function inputChecks(root,files,input,local){const checks=[check("configuration","yes",`Supported ${input.type} input from ${input.source.type==="git"?"project files":input.source.type}`)];if(input.type==="website")return checks;if(input.type==="binary"){const excluded=Object.entries(input.commands).filter(([,target])=>!input.paths.some(pattern=>minimatch2(target,pattern,{dot:true,nocase:false,nonegate:true})));checks.push(check("command-selection",excluded.length?"no":"yes",excluded.length?`Declared command targets are excluded by paths: ${excluded.map(([name,target])=>`${name}: ${target}`).join(", ")}`:"Declared command targets match selected paths"))}if(input.source.type==="github-actions"&&!local)checks.push(...workflowChecks(root,input));else{try{const patterns=input.type==="files"||input.type==="binary"?input.paths:input.type==="skill"&&!isSkillArchive(input.path)?[input.path==="."?"**":`${input.path}/**`]:[input.path];const selected=local&&input.type==="npm-package"?globSync(input.path,{cwd:root}):matchInputPaths(local?diskFiles(root,input.type==="skill"&&!isSkillArchive(input.path)?input.path:""):files,patterns);if(input.type==="binary"){for(const[command,target]of Object.entries(input.commands)){if(!selected.includes(target))throw new Error(`Binary command ${command} target is missing or excluded by paths: ${target}`)}}if(input.type==="web-fixture")parseWebFixtureInput(read(root,input.path).toString("utf8"));if(input.type==="npm-package"&&(selected.length!==1||!selected[0].endsWith(".tgz")))throw new Error("npm-package must select exactly one .tgz file");if(input.type==="skill"&&!isSkillArchive(input.path)&&!selected.includes(input.path==="."?"SKILL.md":`${input.path}/SKILL.md`))throw new Error("Skill directory must contain SKILL.md at its root");checks.push(check("local-paths","yes",`${selected.length} ${local?"local":"working-tree"} file(s) selected`))}catch(error){checks.push(check("local-paths","no",message(error).replace("committed files",local?"local files":"working-tree files")))}}return checks}function verifyEvals(cwd=process.cwd(),{local=false}={}){const root=rootDirectory(cwd);const result={version:1,mode:"local-static",root,status:"passed",summary:{yes:0,no:0,unknown:0},facts:{configFilePresent:null,caseCount:null},info:[],notChecked:["github-app-activation","account-settings","mcp-server-access","mcp-server-reachability","live-input-delivery","artifact-contents","sandbox-installation","workflow-execution"],global:[],inputs:[]};const finish=()=>{for(const item of[...result.global,...result.inputs.flatMap(input=>input.checks)])result.summary[item.state]++;result.status=result.summary.no>0?"failed":result.summary.unknown>0?"incomplete":"passed";return result};let raw={};let files;try{files=workingFiles(root)}catch(error){result.global.push(check("working-tree","no",message(error)));return finish()}try{result.facts.configFilePresent=files.includes("gauge.json")||regularFile(root,"gauge.json");if(result.facts.configFilePresent)raw=JSON.parse(read(root,"gauge.json").toString("utf8"));else result.info.push({id:"config-file-absent",detail:"Optional gauge.json is absent; using default case discovery and no inputs"})}catch(error){result.global.push(check("configuration","no",message(error)));return finish()}const config=runDefinitionFileConfigSchema.safeParse(raw);result.global.push(check("configuration",config.success?"yes":"no",config.success?"Configuration matches the supported gauge.json schema":oneLine(config.error.message)));const rawInputs=record(record(raw).inputs);for(const[name,value]of Object.entries(rawInputs).sort(([a],[b])=>a.localeCompare(b))){const parsed=runDefinitionInputSchema.safeParse(value);result.inputs.push({name,checks:parsed.success?inputChecks(root,files,parsed.data,local):[check("configuration","no",oneLine(parsed.error.message))]})}if(config.success){try{const paths=matchInputPaths(files,config.data.cases,{allowEmptySelection:true});if(paths.length>20||paths.some(path=>!path.endsWith(".md")))throw new Error("Expected at most 20 Markdown cases");result.facts.caseCount=paths.length;if(paths.length)result.global.push(check("case-discovery","yes",`${paths.length} working-tree case(s) selected`));else result.info.push({id:"no-cases",detail:"No eval cases found; input-only setup is valid, but no evaluations will run"});for(const path of paths){try{const item=parseRunDefinitionCase(path,read(root,path));if(local)assertLocalCapabilities(item.config,`${path}: config.`);const missing=(item.config.inputs??[]).filter(name=>!Object.hasOwn(config.data.inputs,name));if(missing.length)throw new Error(`${path}: Unknown input(s): ${missing.join(", ")}`);const commandOwners=new Map;mergeWebFixtures((item.config.inputs??Object.keys(config.data.inputs)).flatMap(name=>{const input=config.data.inputs[name];return input.type==="web-fixture"&&(local||input.source.type==="git")?[parseWebFixtureInput(read(root,input.path).toString("utf8"))]:[]}));for(const name of new Set(item.config.inputs??Object.keys(config.data.inputs))){const input=config.data.inputs[name];if(input.type!=="binary")continue;for(const command of Object.keys(input.commands)){const owner=commandOwners.get(command);if(owner)throw new Error(`${path}: Inputs ${owner} and ${name} both expose command ${command}; choose distinct commands or separate cases`);commandOwners.set(command,name)}}result.global.push(check(`case.${path}`,"yes","Case syntax, input references, and declared command names are valid"))}catch(error){result.global.push(check(`case.${path}`,"no",message(error)))}}}catch(error){result.global.push(check("case-discovery","no",message(error).replace("committed files","working-tree files")))}}else result.info.push({id:"case-validation-skipped",detail:"Fix gauge.json before validating cases and input references"});return finish()}function printEvalVerification(result){console.log(`Local static verification: ${result.root}`);const print2=checks=>{for(const item of checks)console.log(` [${item.state}] ${item.id}: ${item.detail}`)};console.log("Global");print2(result.global);for(const input of result.inputs){console.log(`Input: ${input.name}`);print2(input.checks)}for(const item of result.info)console.log(`[info] ${item.id}: ${item.detail}`);console.log(`Static verification ${result.status}: ${result.summary.yes} yes, ${result.summary.no} no, ${result.summary.unknown} unknown.`);console.log("Scope: repository setup only. Live delivery, account settings, and sandbox execution were not tested; no sessions were launched.")}var LIMIT=512*1024*1024;var SNAPSHOT_FILES=100;function parseInputBindings(values){const bindings=new Map;for(const value of values){const split=value.indexOf("=");const name=split<0?value:value.slice(0,split);const path=split<0?void 0:value.slice(split+1);if(!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)||path==="")throw new CliError("usage","--input expects name or name=path");if(bindings.has(name))throw new CliError("usage",`Duplicate --input: ${name}`);bindings.set(name,path)}return bindings}var isGlob=path=>/[*?[\]{}]/.test(path);function rejectSkippedPatterns(name,patterns){for(const pattern of patterns){const skipped=pattern.split("/").find(skippedInputEntry);if(skipped===".gauge")throw new CliError("usage",`Input ${name}: ${pattern} is inside .gauge/. Inputs never include .gauge/, because it holds run results and local config.`,{hint:"Move the files outside .gauge/ and update the glob."});if(skipped)throw new CliError("usage",`Input ${name}: ${pattern} is inside ${skipped}, which inputs never include`,{hint:`Move the files outside ${skipped} and update the glob.`})}}async function walkInput(root,depth=Infinity){const paths=[];const walk=async(directory,level)=>{for(const entry of await readdir(join10(root,directory),{withFileTypes:true})){if(skippedInputEntry(entry.name)||!entry.isFile()&&!entry.isDirectory())continue;const next=directory?`${directory}/${entry.name}`:entry.name;if(entry.isDirectory()){if(level<depth)await walk(next,level+1)}else paths.push(next);if(paths.length>2e4)throw new CliError("usage","Input exceeds 20,000 files")}};await walk("",1);return paths.sort()}function selectPaths(name,paths,patterns){rejectSkippedPatterns(name,patterns);try{return matchInputPaths(paths,patterns)}catch(error){throw new CliError("usage",`Input ${name}: ${message(error).replace("committed files","files")}`)}}async function expandOverrideGlob(name,glob){const segments=glob.split(sep2);const first=segments.findIndex(isGlob);const root=segments.slice(0,first).join(sep2)||sep2;const pattern=segments.slice(first).join("/");const info=await stat(root).catch(()=>void 0);if(!info?.isDirectory())throw new CliError("usage",`Input ${name}: ${root} is not a directory`);const depth=pattern.includes("**")?Infinity:pattern.split("/").length;return{root,pattern,files:selectPaths(name,await walkInput(root,depth),[pattern])}}async function selectLocalInput(resource,supplied,override){let path=resolve4(supplied);let info=await lstat(path).catch(()=>void 0);if(!info&&override&&isGlob(path)){if(resource.use.type==="skill")throw new CliError("usage",`Input ${resource.name}: Skill inputs take an explicit directory or archive, not a glob`);const{root:root2,pattern,files}=await expandOverrideGlob(resource.name,path);if(resource.use.type==="files")return{use:{type:"files"},root:root2,files,skillDirectory:false};if(resource.use.type==="binary")return{use:{...resource.use,paths:[pattern]},root:root2,files,skillDirectory:false};if(files.length!==1)throw new CliError("usage",`Input ${resource.name}: ${supplied} matches ${files.length} files; keep one`);path=join10(root2,files[0]);info=await lstat(path)}if(!info)throw new CliError("usage",`Input path does not exist: ${supplied}`,{hint:"Pass --input <name>=<path>, or fix the input's path in gauge.json."});if(!info.isFile()&&!info.isDirectory())throw new CliError("usage",`Input must be an ordinary file or directory: ${supplied}`);let use=resource.use;if(use.type==="web-fixture"){if(!info.isFile())throw new CliError("usage","Supply a web fixture JSON file");use={type:"web-fixture",path:basename(path)}}else if(use.type==="skill"){if(info.isFile()&&!isSkillArchive(path))throw new CliError("usage","Supply a Skill directory, .zip, or .tar.gz archive");use={...use,root:info.isDirectory()?".":basename(path)}}else if(use.type==="npm-package"||use.type==="npm-cli"){if(!info.isFile()||!path.endsWith(".tgz"))throw new CliError("usage","Supply the packed npm .tgz file");use={...use,package:basename(path)}}else if(override&&use.type==="files")use={type:"files"};else if(override&&use.type==="binary")use={...use,paths:[info.isDirectory()?"**":basename(path)]};const root=info.isDirectory()?path:dirname(path);const paths=info.isDirectory()?await walkInput(root):[basename(path)];const patterns=!override&&(use.type==="files"||use.type==="binary")?use.paths:void 0;return{use,root,files:patterns?selectPaths(resource.name,paths,patterns):paths,skillDirectory:use.type==="skill"&&info.isDirectory()}}async function packLocalInput(resource,supplied,output,{override=false}={}){if(resource.use.type==="website")throw new CliError("usage",`Input ${resource.name}: website inputs cannot be supplied through --input.`,{hint:"Use the configured public preview with --hosted. For content-only alternatives and their discovery limits, read `gauge instructions hillclimb`."});const{use,root,files,skillDirectory}=await selectLocalInput(resource,supplied,override);if(!files.length)throw new CliError("usage","Input contains no selected files");if(use.type==="binary"){for(const[command,target]of Object.entries(use.commands))if(!files.includes(target))throw new CliError("usage",`Input ${resource.name}: binary command ${command} target is missing: ${target}`,override?{hint:"Command targets are relative to the supplied path."}:void 0)}if(skillDirectory&&!files.includes("SKILL.md"))throw new CliError("usage","Skill directory must contain SKILL.md at its root");let total=0;for(const file of files){inputPathSchema.parse(file);const entry=await lstat(join10(root,file));if(!entry.isFile())throw new CliError("usage",`Input file changed or is not an ordinary file: ${file}`);total+=entry.size;if(total>LIMIT)throw new CliError("usage","Input exceeds 512 MiB")}await tar({cwd:root,file:output,gzip:true,portable:true,noMtime:true,follow:false,noDirRecurse:true},files.map(file=>`./${file}`));const bytes=(await stat(output)).size;if(bytes>LIMIT)throw new CliError("usage","Input archive exceeds 512 MiB");const hash=createHash5("sha256");for await(const chunk of createReadStream2(output))hash.update(chunk);const sha256=`sha256:${hash.digest("hex")}`;if(use.type==="web-fixture")await readWebFixtureArchive(output,use.path,sha256);return{use,upload:{path:output,bytes,sha256},files}}async function loadLocalRunDefinition(files,values,cwd=process.cwd(),{local=false,spec}={}){const directory=await mkdtemp(join10(tmpdir(),"gauge-inputs-"));const cleanup=()=>rm(directory,{recursive:true,force:true});try{const root=rootDirectory(cwd);const paths=workingFiles(root);const blobs=new Map;if(paths.includes("gauge.json"))blobs.set("gauge.json",read(root,"gauge.json"));const configBytes=blobs.get("gauge.json");let config;try{config=runDefinitionFileConfigSchema.parse(configBytes?JSON.parse(configBytes.toString("utf8")):{})}catch(error){throw new CliError("usage",`gauge.json is not valid: ${error instanceof Error?error.message:error}`,{hint:"Run `gauge run --verify` for each problem."})}const patterns=spec?[]:files.length?files.map(file=>{const path=relative4(root,resolve4(cwd,file));if(!path||path===".."||path.startsWith(`..${sep2}`)||isAbsolute4(path))throw new CliError("usage","Case paths must be inside the definition directory");return path.split(sep2).join("/")}):config.cases;const cases=spec?[]:files.length?matchInputPaths(paths,patterns):matchInputPaths(paths,patterns,{allowEmptySelection:true});if(!spec&&cases.length===0)throw noCasesError();if(cases.length>20||cases.some(path=>!path.endsWith(".md")))throw new CliError("usage","Expected 1\u201320 Markdown cases");for(const path of cases)blobs.set(path,read(root,path));const parsed=spec?caseSpecCases(spec):cases.map(path=>parseRunDefinitionCase(path,blobs.get(path)??Buffer.alloc(0)));const unmeasured=parsed.filter(item=>!item.criteria&&!item.preference);if(!local&&unmeasured.length)throw new CliError("usage",`Hosted cases need criteria or preference: ${unmeasured.map(item=>item.path).join(", ")}`,{hint:"Add criteria, or run locally with --local."});const bindings=parseInputBindings(values);for(const name of bindings.keys())if(!(name in config.inputs))throw new CliError("usage",`Unknown input: ${name}`,{hint:`gauge.json defines: ${Object.keys(config.inputs).join(", ")||"none"}`});for(const item of parsed)for(const name of item.config.inputs??[])if(!(name in config.inputs))throw new CliError("usage",`${item.path}: unknown input "${name}". gauge.json defines: ${Object.keys(config.inputs).join(", ")||"none"}`);const selected=new Set(parsed.flatMap(item=>item.config.inputs??Object.keys(config.inputs)));for(const[name,override]of bindings){if(!selected.has(name))throw new CliError("usage",`Input is not used by the selected cases: ${name}`);if(override!==void 0)rejectSkippedPatterns(name,[relative4(root,resolve4(cwd,override)).split(sep2).join("/")])}const git2=args=>{try{return execFileSync2("git",args,{cwd:root,encoding:"utf8",stdio:["ignore","pipe","pipe"]}).trim()}catch{return null}};const commit=git2(["rev-parse","--verify","HEAD^{commit}"]);const origin=git2(["remote","get-url","origin"]);const repoUrl=origin&&uncredentialedRepoUrlSchema.safeParse(origin).success?origin:null;const unattachedInputs=Object.keys(config.inputs).filter(name=>!selected.has(name));const resources=[];const uploads=[];const inputSnapshots=[];for(const[name,input]of Object.entries(config.inputs)){if(!selected.has(name))continue;if(local&&input.type==="website")throw new CliError("usage",`Website input "${name}" requires hosted Gauge to replace the original site with your preview.`,{hint:"Use --hosted with a public preview. For local runs, supply documentation files or a preview URL in the prompt. These give the agent your content directly rather than testing whether it finds your site. See `gauge instructions hillclimb`."});const original=compileDefinitionInput(name,input,repoUrl??"",commit??"");const override=bindings.get(name);const supplied=override??(original.source.type==="git"?workingTreeInput(root,name,input):void 0);if(supplied!==void 0){const{use,upload,files:files2}=await packLocalInput(original,resolve4(cwd,supplied),join10(directory,`${name}.tar.gz`),{override:override!==void 0});uploads.push(upload);inputSnapshots.push({name,source:override!==void 0?"override":"working-tree",path:resolve4(cwd,supplied),gaugeArchive:{sha256:upload.sha256,bytes:upload.bytes},files:files2.slice(0,SNAPSHOT_FILES),fileCount:files2.length});resources.push({...original,use,source:{type:"upload",sha256:upload.sha256,bytes:upload.bytes,repoUrl,commit}})}else{if(local)throw new CliError("usage",`Input ${name} has a remote source. Supply --input ${name}=<path> for local execution, or use --hosted.`);if(!repoUrl||!commit)throw new CliError("usage",`Input ${name}'s configured ${original.source.type} source requires a Git checkout with an origin.`,{hint:input.type==="website"?"Configure the repository origin and publish a matching public preview through the declared source. Website inputs cannot use --input; see `gauge instructions hillclimb` for content-only alternatives.":`Supply --input ${name}=<path> to use local files.`});resources.push(original)}}if(!spec){const compiled=compileRunDefinitionFromBlobs({repoUrl,commit,resources,blobs,casePaths:cases,verification:"LOCAL"});const webFixtures=await freezeWebFixtures({...compiled.definition,resources:resources.filter(r=>r.source.type==="upload")},uploads);return{...compiled,webFixtures,uploads,inputSnapshots,unattachedInputs,cleanup}}const definition={version:2,source:{repoUrl,commit,digest:caseSpecDigest(spec),verification:"LOCAL"},resources,cases:parsed};if(!local)runDefinitionSubmissionSchema.parse(definition);return{definition,webFixtures:await freezeWebFixtures({...definition,resources:resources.filter(r=>r.source.type==="upload")},uploads),org:config.orgId??config.org,uploads,inputSnapshots,unattachedInputs,cleanup}}catch(error){await cleanup();if(error instanceof CliError||!(error instanceof Error))throw error;throw new CliError("usage",error.message,{hint:"Check the case format in `gauge instructions run`, then run `gauge run --verify`."})}}function workingTreeInput(root,name,input){switch(input.type){case"website":throw new CliError("usage",`Input ${name} is a website; website inputs run on hosted Gauge only`);case"files":case"binary":return root;case"web-fixture":case"skill":return join10(root,input.path);case"npm-package":{const matches2=globSync2(input.path,{cwd:root});if(matches2.length!==1)throw new CliError("usage",matches2.length?`Input ${name}: ${input.path} matches ${matches2.length} files; keep one`:`Input ${name}: no file matches ${input.path}. Run \`npm pack\` first`);return join10(root,matches2[0])}}}async function uploadLocalInputs(client,org,uploads){for(const input of uploads){const signed2=await client.post(`/api/v1/orgs/${encodeURIComponent(org)}/run-definitions/uploads`,{sha256:input.sha256,bytes:input.bytes});const signal=AbortSignal.timeout(10*60*1e3);for(let attempt=0;attempt<3;attempt++){const body2=createReadStream2(input.path);let response;try{response=await fetch(signed2.url,{method:"PUT",headers:signed2.headers,body:body2,duplex:"half",redirect:"error",signal})}finally{body2.destroy()}await response.body?.cancel();if(response.ok||response.status===412)break;if(response.status!==409||attempt===2)throw new CliError("network",`Input upload failed (${response.status})`);await delay2(100*2**attempt,void 0,{signal})}}}var SIDE_KEYS=["request","batch","eval","revision","experiment","trial","agent","model","since","until"];var SELECTING_KEYS=new Set(["request","batch","eval","experiment","trial"]);function csv4(value){return value.split(",").map(part=>part.trim()).filter(Boolean)}function parseCompareSide(flag2,pairs){const side={};const seen=new Set;for(const pair2 of pairs){const eq=pair2.indexOf("=");const key=eq<0?"":pair2.slice(0,eq).trim();const value=eq<0?"":pair2.slice(eq+1).trim();if(!SIDE_KEYS.includes(key)||!value)throw new CliError("usage",`invalid ${flag2} "${pair2}" (expected key=value with key one of ${SIDE_KEYS.join(", ")})`);if(seen.has(key)&&key!=="batch")throw new CliError("usage",`${flag2} ${key}= given twice`);seen.add(key);if(key==="request")side.runRequest=value;else if(key==="batch")side.batch=[...side.batch??[],...csv4(value)];else if(key==="eval")side.evalSet=value;else if(key==="revision")side.evalRevision=value;else if(key==="experiment")side.experiment=value;else if(key==="trial")side.trial=value;else if(key==="agent")side.agent=parseAgents(value);else if(key==="model")side.model=csv4(value);else if(key==="since")side.since=value;else side.until=value}if(![...seen].some(key=>SELECTING_KEYS.has(key)))throw new CliError("usage",`${flag2} needs request=, batch=, eval=, experiment=, or trial= to select runs`);return side}function pct2(rate){return rate===null?"":`${Math.round(rate*100)}%`}function passCell(stats){return stats.judged===0?"":`${pct2(stats.passRate)} (${stats.passed}/${stats.judged})`}function signed(value,format){if(value===null)return"";return`${value>0?"+":value<0?"-":"\xB1"}${format(Math.abs(value))}`}var points=rate=>`${Math.round(rate*100)}pp`;var turns=n=>String(Math.round(n*10)/10);var usd=n=>`$${n.toFixed(2)}`;var secs=ms=>`${Math.round(ms/1e3)}s`;function pair(before,after,delta3,format){if(before===null&&after===null)return"";const side=v=>v===null?"\u2014":format(v);return`${side(before)} \u2192 ${side(after)}${delta3===null?"":` (${signed(delta3,format)})`}`}function row(target,item){const{baseline:a,candidate:b,delta:delta3}=item;return{target,runs:`${a.runs} \u2192 ${b.runs}`,completed:`${a.succeeded}/${a.runs} \u2192 ${b.succeeded}/${b.runs}`,baseline:passCell(a),candidate:passCell(b),"\u0394 pass":signed(delta3.passRate,points),turns:pair(a.medianTurns,b.medianTurns,delta3.medianTurns,turns),cost:pair(a.medianUsdCost,b.medianUsdCost,delta3.medianUsdCost,usd),duration:pair(a.medianDurationMs,b.medianDurationMs,delta3.medianDurationMs,secs)}}function compareWarnings(result){const warnings=[];for(const name of["baseline","candidate"]){const side=result.sides[name];const stats=result.overall[name];if(stats.runs===0)warnings.push(`${name} matched no runs. Check its selectors; optimization trial runs need trial=<trialId>.`);else if(stats.finished<stats.runs)warnings.push(`${name} has ${stats.runs-stats.finished} of ${stats.runs} runs still in progress, so its numbers are partial. Run \`gauge run wait <request-id>\` first.`);if(stats.judged<stats.finished)warnings.push(`${name} has ${stats.finished-stats.judged} finished runs without a verdict (not judged yet, or no criteria). Its pass rate covers judged runs only.`);for(const mixed of side.mixedRevisions??[])warnings.push(`${name}: ${mixed.evalSetId?`eval ${mixed.evalSetId}`:`case prompt ${mixed.promptId}`} ran under ${mixed.revisions.length} revisions (${mixed.revisions.join(", ")}): it was edited between runs. Add --${name} revision=<hash> to pick one.`);if(side.truncated)warnings.push(`${name} matched too many runs; only the newest were compared. Narrow it with since=, until=, or batch=.`)}return warnings}function printComparison(result){const revisions=name=>result.sides[name].evalRevisions.join(", ")||"\u2014";console.log(`Baseline revision: ${revisions("baseline")}. Candidate revision: ${revisions("candidate")}.`);for(const name of["baseline","candidate"]){for(const trial of result.sides[name].trials??[])console.log(`${name}: ${trial.runs} run${trial.runs===1?"":"s"} from optimization trial ${trial.key} "${trial.title}" (${trial.kind}).`)}console.log("Medians are baseline \u2192 candidate (difference).");console.log("");printItems([row("all",result.overall),...result.targets.map(target=>row(`${target.agent} ${target.model||"(default)"}`,target))],"table");const before=new Map(result.overall.baseline.criteria.map(c=>[c.name,c]));const after=new Map(result.overall.candidate.criteria.map(c=>[c.name,c]));if(result.overall.delta.criteria.length>0){console.log("");printItems(result.overall.delta.criteria.map(c=>{const a=before.get(c.name);const b=after.get(c.name);return{criterion:c.name,baseline:a&&a.judged>0?`${pct2(a.passRate)} (${a.passed}/${a.judged})`:"",candidate:b&&b.judged>0?`${pct2(b.passRate)} (${b.passed}/${b.judged})`:"","\u0394 pass":signed(c.passRate,points)}}),"table")}}var LOCAL_AGENTS2=new Set(["CLAUDE_CODE","CODEX_CLI","CURSOR","PI","OPENCODE"]);var LOCAL_TERMINAL_STATES=new Set(["completed","failed","canceled","interrupted"]);function localCompareFilter(flag2,side){const hostedOnly2=[side.runRequest!==void 0&&"request=",side.experiment!==void 0&&"experiment=",side.trial!==void 0&&"trial="].filter(Boolean);if(hostedOnly2.length>0)throw new CliError("usage",`${flag2} ${hostedOnly2.join(", ")} select hosted runs; local sides take batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=`);if((side.batch?.length??0)>1)throw new CliError("usage",`${flag2} takes one batch= locally`);if((side.agent?.length??0)>1||(side.model?.length??0)>1)throw new CliError("usage",`${flag2} takes one agent= and one model= locally`);if(side.evalRevision!==void 0&&!/^[1-9]\d*$/.test(side.evalRevision))throw new CliError("usage",`${flag2} local revision= must be a positive integer`);return{batch:side.batch?.[0],evalId:side.evalSet,measurementRevision:side.evalRevision===void 0?void 0:Number(side.evalRevision),agent:side.agent?.[0],model:side.model?.[0],since:side.since?parseSince(side.since,Date.now(),"since="):void 0,until:side.until?parseSince(side.until,Date.now(),"until="):void 0}}function median(values){if(values.length===0)return null;const sorted=[...values].sort((a,b)=>a-b);const mid=sorted.length>>1;return sorted.length%2?sorted[mid]:(sorted[mid-1]+sorted[mid])/2}function localStats(runs){const judged=runs.filter(run=>run.criterionResults?.length&&run.criterionResults.every(criterion=>criterion.outcome!=="UNASSESSABLE"));const passed=judged.filter(run=>run.criterionResults.every(criterion=>criterion.outcome==="PASS")).length;const byCriterion=new Map;for(const run of runs)for(const criterion of run.criterionResults??[]){const current=byCriterion.get(criterion.name)??{name:criterion.name,judged:0,passed:0};if(criterion.outcome!=="UNASSESSABLE"){current.judged++;if(criterion.outcome==="PASS")current.passed++}byCriterion.set(criterion.name,current)}const recorded=values=>values.filter(value=>typeof value==="number"&&Number.isFinite(value));return{runs:runs.length,finished:runs.filter(run=>LOCAL_TERMINAL_STATES.has(run.outcome)).length,succeeded:runs.filter(run=>run.outcome==="completed").length,judged:judged.length,passed,passRate:judged.length?passed/judged.length:null,medianTurns:median(recorded(runs.map(run=>run.turns))),medianUsdCost:null,medianDurationMs:median(recorded(runs.map(run=>run.durationMs))),criteria:[...byCriterion.values()].map(criterion=>({...criterion,passRate:criterion.judged?criterion.passed/criterion.judged:null}))}}function diff(a,b){return a===null||b===null?null:b-a}function localRow(baseline,candidate){const a=localStats(baseline);const b=localStats(candidate);return{baseline:a,candidate:b,delta:{passRate:diff(a.passRate,b.passRate),medianTurns:diff(a.medianTurns,b.medianTurns),medianUsdCost:null,medianDurationMs:diff(a.medianDurationMs,b.medianDurationMs),criteria:[...new Set([...a.criteria,...b.criteria].map(criterion=>criterion.name))].map(name=>({name,passRate:diff(a.criteria.find(criterion=>criterion.name===name)?.passRate??null,b.criteria.find(criterion=>criterion.name===name)?.passRate??null)}))}}}function compareLocalRuns(baseline,candidate){const targets=new Map;for(const run of[...baseline,...candidate]){const agent=LOCAL_AGENTS2.has(run.agent)?run.agent:void 0;if(agent)targets.set(`${run.agent} ${run.model}`,{agent,model:run.model})}const forTarget=(runs,key)=>runs.filter(run=>`${run.agent} ${run.model}`===key);return{overall:localRow(baseline,candidate),targets:[...targets].map(([key,target])=>({...target,...localRow(forTarget(baseline,key),forTarget(candidate,key))})),sides:{baseline:localRevisionMetadata(baseline),candidate:localRevisionMetadata(candidate)}}}function localRevisionMetadata(runs){const byMeasurement=new Map;const allRevisions=new Set;for(const run of runs){if(run.measurementRevision===void 0||run.measurementRevision===null)continue;const revision=String(run.measurementRevision);allRevisions.add(revision);if(run.evalId){const revisions=byMeasurement.get(run.evalId)??new Set;revisions.add(revision);byMeasurement.set(run.evalId,revisions)}}return{evalRevisions:[...allRevisions].sort((a,b)=>Number(a)-Number(b)),mixedRevisions:[...byMeasurement].filter(([,revisions])=>revisions.size>1).map(([evalSetId,revisions])=>({evalSetId,promptId:null,revisions:[...revisions]})),truncated:false}}function localCompareWarnings(result){const warnings=compareWarnings(result).filter(warning=>!warning.includes("matched no runs")).map(warning=>warning.replace("gauge run wait <request-id>","gauge run wait --batch <batch-id> --local").replace("revision=<hash>","revision=<number>").replace("not judged yet, or no criteria","analysis missing, unassessable criteria, or no criteria"));for(const name of["baseline","candidate"])if(result.overall[name].runs===0)warnings.unshift(`${name} matched no local runs. Check its selectors: batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=. \`gauge run list --local\` shows the values.`);return warnings}function runPath2(id2){return`/api/v1/runs/${encodeURIComponent(id2)}`}function streamPath(id2){return`/api/runs/${encodeURIComponent(id2)}/stream`}function fmtDuration(ms){if(ms==null)return"";const s=Math.round(ms/1e3);return s>=60?`${Math.floor(s/60)}m${s%60}s`:`${s}s`}function fmtUsd(v){return v==null?"":`$${v.toFixed(2)}`}function compact(value,max=140){const s=typeof value==="string"?value:JSON.stringify(value)??"null";const one=s.replace(/\s+/g," ").trim();return one.length>max?`${one.slice(0,max-1)}\u2026`:one}function toRow4(run){const selected=run.selectedProvider?.slug??"";const observed=run.observedProvider?.slug;return{id:run.id,status:run.status,agent:run.agent,model:run.modelDisplayName??run.resolvedModel??run.model,provider:observed&&observed!==selected?`${selected||"?"} \u2192 ${observed}`:selected||observed||"",experiment:run.experiment??"",revision:run.evalRevision??"",trial:run.optimizationTrial?`${run.optimizationTrial.key} ${run.optimizationTrial.trialId}`:"",source:run.triggeredBy?.source??"",turns:run.turns??"",cost:fmtUsd(run.usdCost),credits:run.creditsCharged??"",duration:fmtDuration(run.durationMs),created:run.createdAt,prompt:compact(run.promptSnippet,48)}}function toLocalRow(run){return{id:run.id,status:run.outcome,agent:run.agent,model:run.model,batch:run.batchId,eval:run.evalId??"",turns:run.turns,analyzed:run.analyzed?"yes":"",ended:run.endedAt}}function forkLines(run){const from=run.forkedFrom;const lines=[];if(run.recorded!==void 0)lines.push(["Recorded",run.recorded?`yes (gauge run fork ${run.id} --before <seq>)`:"no"]);if(from){const at=from.beforeSeq==null?"":` before seq ${from.beforeSeq}`;lines.push(["Forked from",`${from.runId}${at}`]);if(from.message)lines.push(["Fork change","different persona message"]);if(from.mock)lines.push(["Fork change",`mock of ${from.mock.url}${from.mock.summary?`: ${compact(from.mock.summary,120)}`:""}`])}return lines}function detailLines(run){return[["Id",run.id],["Status",run.status],["Agent",run.agent],["Model",run.modelDisplayName||run.resolvedModel||run.model||"(platform default)"],["Requested model",run.model||"(platform default)"],["Selected provider",run.selectedProvider?`${run.selectedProvider.displayName} (${run.selectedProvider.slug})`:"\u2014"],["Observed provider",run.observedProvider?`${run.observedProvider.slug} (${run.observedProvider.source.toLowerCase().replaceAll("_"," ")})`:"\u2014"],["Prompt",compact(run.promptText,120)],["Prompt id",run.promptId],["Batch",run.batchId],["Experiment",run.experiment??"\u2014"],["Eval set",run.evalSetId??"\u2014"],["Eval revision",run.evalRevision??"\u2014"],["Optimization trial",run.optimizationTrial?`${run.optimizationTrial.key} "${run.optimizationTrial.title}" (${run.optimizationTrial.kind}), trial ${run.optimizationTrial.trialId} in optimization ${run.optimizationTrial.optimizationId}`:"\u2014"],["Scenario",run.scenario],["Funding",run.funding],["Repo",run.repoUrl?`${run.repoUrl}${run.repoRef?` @ ${run.repoRef}`:""}`:"\u2014"],["Attempt",String(run.attempt)],["Turns",run.turns==null?"\u2014":String(run.turns)],["Triggered by",triggeredByLabel(run.triggeredBy)||"\u2014"],["Cost",fmtUsd(run.usdCost)||"\u2014"],["Credits",run.creditsCharged==null?"\u2014":String(run.creditsCharged)],["Duration",fmtDuration(run.durationMs)||"\u2014"],["Exit reason",run.exitReason??"\u2014"],["Failure reason",run.failureReason??"\u2014"],["Cancel requested",run.cancelRequested?"yes":"no"],["Diff",run.hasDiff?`yes (gauge run diff ${run.id})`:"no"],...forkLines(run),...run.criterionResults.map(c=>[`Criterion ${c.name}`,c.passed===null?"not assessed":c.passed?"passed":"failed"]),["Created",run.createdAt],["Started",run.startedAt??"\u2014"],["Finished",run.finishedAt??"\u2014"]]}function passLine(label,pass){const bits=[pass.status];if(pass.model)bits.push(pass.model);const cost=fmtUsd(pass.usdCost);if(cost)bits.push(cost);if(pass.error)bits.push(`error: ${compact(pass.error,60)}`);return`${label.padEnd(20)} ${bits.join(" ")}`}var PASS_LABELS={ANALYSIS:"Session analysis",CLI_DISCOVERY:"CLI discovery",LEVER:"Improvement levers",EXPERIMENT_DELTA:"Experiment delta"};var INSTALLED={NONE:"",ATTEMPTED:"attempted",CONFIGURED:"configured",EXERCISED:"exercised"};var check2=value=>value?"yes":"";function printInsight(insight,recorded=false){for(const pass of insight.passes){console.log(passLine(PASS_LABELS[pass.kind]??pass.kind,pass))}const analysis=insight.analysis;if(!analysis){console.log(insight.decision.status==="SKIPPED"?"\n(skipped)":"\n(no analysis yet)");return}if(analysis.limitation)console.log(`
1005
1012
  ${compact(analysis.limitation,200)}`);console.log(`
1006
- Findings (${analysis.observations.length})`);for(const finding of analysis.observations){console.log(` ${finding.kind}: ${finding.label}`);console.log(` id ${finding.id}${finding.issueId?`, issue ${finding.issueId}`:""}`);console.log(` ${finding.detail}`);console.log(` Evidence: ${JSON.stringify(finding.evidence)}`)}console.log(`
1007
- Products (${analysis.products.length})`);if(analysis.products.length>0)printItems(analysis.products.map(p=>({product:p.name,considered:check2(p.considered),chosen:check2(p.chosen),rejected:check2(p.rejected),installed:INSTALLED[p.installPhase]??p.installPhase,rank:p.rank??"",source:p.source.toLowerCase()})),"table");const delta3=insight.experimentDelta;if(delta3&&typeof delta3==="object"){console.log("\nExperiment delta");console.log([delta3.changed==null?"":delta3.changed?"changed":"no change",delta3.hypothesisSupported??""].filter(Boolean).join(" "));if(delta3.summary)console.log(compact(delta3.summary,300))}console.error("\n(full detail: rerun with -o json)")}function parseEvent(data){try{const parsed=runEventSchema.safeParse(JSON.parse(data));return parsed.success?parsed.data:null}catch{return null}}function formatEventLine(e){const rec=e;const head=`[t${e.turn}#${e.seq}]`;switch(e.type){case"run_meta":{const agent=rec.agent;const model=agent?.model?` model=${agent.model}`:"";return`${head} run_meta agent=${agent?.name??"?"}${model}`}case"agent_message":return`${head} agent: ${compact(rec.text)}`;case"reasoning":return`${head} reasoning: ${compact(rec.text)}`;case"tool_call":return`${head} tool_call ${String(rec.name)}: ${compact(rec.input)}`;case"tool_result":return`${head} tool_result${rec.is_error?" ERROR":""}: ${compact(rec.output,100)}`;case"run_result":return`${head} run_result ${String(rec.exit_reason)} turns=${String(rec.turns)} cost=${fmtUsd(rec.cost_usd)} duration=${fmtDuration(rec.duration_ms)}`;default:{const{run_id:_runId,seq:_seq,turn:_turn,ts:_ts,type:_type,...payload}=rec;return`${head} ${e.type} ${compact(payload)}`}}}var TERMINAL_STATUSES=new Set(["SUCCEEDED","FAILED","TIMED_OUT","CANCELED"]);var STATUS_POLL_MS=5e3;var TERMINAL_DRAIN_MS=2e3;async function streamRun(id2,json,opts){const client=createClient();const controller=new AbortController;let timer;let poller;let drainTimer;let gotResult=false;let sawData=false;const arm=ms=>{if(timer)clearTimeout(timer);if(ms>0)timer=setTimeout(()=>controller.abort(),ms)};arm(opts.idleMs>0?opts.firstEventMs:0);if(opts.pollStatus){let inFlight=false;poller=setInterval(()=>{if(inFlight||drainTimer)return;inFlight=true;client.get(runPath(id2)).then(run=>{if(drainTimer||!TERMINAL_STATUSES.has(run.status))return;if(poller)clearInterval(poller);drainTimer=setTimeout(()=>{if(!gotResult){console.error(`Run is ${run.status}; no more events.`)}controller.abort()},TERMINAL_DRAIN_MS)}).catch(()=>{}).finally(()=>{inFlight=false})},STATUS_POLL_MS)}try{await client.sse(streamPath(id2),frame=>{const event=parseEvent(frame.data);if(!event)return;sawData=true;if(json)console.log(frame.data);else console.log(formatEventLine(event));if(event.type==="run_result"){gotResult=true;controller.abort()}else if(opts.stopAtLiveMs!==void 0&&Date.parse(event.ts)>=opts.stopAtLiveMs){gotResult=true;controller.abort()}else arm(opts.idleMs)},{signal:controller.signal,onComment:()=>{if(!sawData&&opts.idleMs>0)arm(opts.firstEventMs)}})}finally{if(timer)clearTimeout(timer);if(poller)clearInterval(poller);if(drainTimer)clearTimeout(drainTimer)}}async function loadWorkingTree(cmd,opts,upload,spec,cliRules){const lifecycle=opts.prompt!==void 0?lifecyclePatchFromOpts(opts):{};const loaded=await loadLocalRunDefinition(opts.files??[],opts.input,void 0,{spec});try{const org=await resolveOrg(cmd,loaded.org);if(upload)await confirmOrAbort("Run these cases now?",opts.yes);await uploadLocalInputs(createClient(),org,loaded.uploads);return{definition:{...loaded.definition,...lifecycle,...cliRules?{cliRules}:{}},org,inputSnapshots:loaded.inputSnapshots,unattachedInputs:loaded.unattachedInputs}}finally{await loaded.cleanup()}}function printPlan2(result){console.log(`Definition: ${result.source.verification==="LOCAL"?"current files":result.source.commit}`);console.log(`Expected sessions: ${result.expectedRuns}`);if(result.cliRules)console.log(`CLI rules: ${result.cliRules.ruleCount} (this launch only; sha256:${result.cliRules.bundleSha256})`);if(result.lifecycle?.setupTimeoutSeconds!==void 0)console.log(`Setup hook: ${result.lifecycle.setupTimeoutSeconds}s deadline`);if(result.lifecycle?.teardownTimeoutSeconds!==void 0)console.log(`Teardown hook: ${result.lifecycle.teardownTimeoutSeconds}s deadline`);for(const item of result.cases)console.log(` ${item.path}: ${item.expectedRuns} sessions, ${item.preference?`${item.preference.kind} preference (advisory)`:`${item.criteriaCount} criteria`}`);for(const resource of result.resources){const use=resource.use;if(use.type==="npm-package"||use.type==="npm-cli"){const install=use.install??"environment";console.log(` ${resource.name}: npm package \u2192 ${install}`);const label=install==="environment"?"Commands on PATH":"Package commands";console.log(` ${label}: ${resource.commands===void 0?"determined when the input is prepared":resource.commands.join(", ")||"none (library)"}`)}else if(use.type==="binary"){console.log(` ${resource.name}: native executable \u2192 ${use.platform}`);console.log(` Commands on PATH: ${Object.keys(use.commands).join(", ")}`);console.log(" Requires compatible system libraries; Gauge does not install OS packages.")}else console.log(` ${resource.name}: ${use.type}`);if(resource.detail)console.log(` ${resource.detail}`)}if(result.resources.length){console.log(`Inputs: ${result.resources.map(r=>`${r.name} (${r.source}, ${r.readiness})`).join(", ")}`);if(!result.canRun)console.log("Resolve the input errors before launching.")}printInputSnapshots(result.inputSnapshots);printUnattachedInputs(result.unattachedInputs,false)}function printInputSnapshots(snapshots){for(const input of snapshots){console.log(` ${input.name}: ${input.source==="working-tree"?"working-tree snapshot":"local path override snapshot"}`);console.log(` Source path: ${input.path}`);const shown=input.files.slice(0,10);console.log(` Files (${input.fileCount}): ${shown.join(", ")}${input.fileCount>shown.length?`, and ${input.fileCount-shown.length} more`:""}`);console.log(` Gauge archive digest: ${input.gaugeArchive.sha256}`);console.log(` Gauge archive bytes: ${input.gaugeArchive.bytes}`)}if(snapshots.length)console.log("Gauge archive hashes identify delivery archives containing the selected files. For npm inputs, the archive contains the .tgz; its hash and size differ from the .tgz itself. Launching again snapshots current files.")}function unattachedNote(names,saved){return`Not attached: ${names.join(", ")}. ${saved?`Saved measurements attach only inputs passed at launch; add ${names.map(name=>`--input ${name}`).join(" ")} to attach.`:`The selected cases do not use ${names.length===1?"it":"them"}.`}`}function printUnattachedInputs(names,saved){if(names.length)console.log(unattachedNote(names,saved))}function callerAgent(){const caller=detectSkillAgent(process.env);return caller==="codex"||caller==="opencode"?caller:"claude-code"}function promptSpec(prompt,agents){return parseCaseSpec({version:2,cases:[{key:"prompt",prompt,preference:{kind:"open-ended"},config:{agents:buildAgentRoster(agents)}}]})}async function launch(cmd,opts){if(opts.cliRules!==void 0){if(gaugeState(cmd).mode!=="hosted")throw new CliError("usage","--cli-rules requires hosted Gauge; pass --hosted.");if(opts.schema||opts.example||opts.verify)throw new CliError("usage","--cli-rules applies to a launch or --dry-run, not --schema, --example, or --verify.")}const cliRules=opts.cliRules===void 0?void 0:readCliRules(opts.cliRules);if(opts.profile&&gaugeState(cmd).mode!=="local")throw new CliError("usage","--profile applies only to local runs. Pass --local.");const sources=[opts.prompt!==void 0&&"a prompt",opts.eval!==void 0&&"--eval",opts.preference!==void 0&&"--preference",opts.apply!==void 0&&"--apply"].filter(flag2=>Boolean(flag2));const source2=sources[0];if(hasLifecycleFlags(opts)){if(gaugeState(cmd).mode!=="hosted")throw new CliError("usage","Lifecycle hooks require hosted Gauge; pass --hosted.");if(source2!=="a prompt"&&source2!=="--eval"&&source2!=="--preference"||opts.apply!==void 0||opts.schema||opts.example||opts.verify||opts.dryRun&&source2!=="a prompt")throw new CliError("usage","Lifecycle flags apply only to a hosted prompt, --eval, or --preference launch; they do not apply to case files or --apply. Only prompt launches support --dry-run with hooks.")}if(opts.schema||opts.example){const flag2=opts.schema?"--schema":"--example";if(source2||opts.schema&&opts.example)throw new CliError("usage",`${flag2} prints and exits; pass it alone`);const cases=opts.format!=="battery";if(opts.schema)printJson(cases?CASE_SPEC_SCHEMA():BATTERY_SCHEMA());else{printJson(cases?EXAMPLE_CASE_SPEC:EXAMPLE_SPEC);console.error(cases?"\n# Case spec: eval cases as JSON. Save to cases.json, edit, then: gauge run --apply cases.json":"\n# Battery spec: saved prompts and schedules. Save to battery.json, edit, then: gauge run --apply battery.json\n# For eval cases as JSON (criteria, inputs): gauge run --example --format case")}return}if(opts.format)throw new CliError("usage","--format applies with --schema or --example");if(sources.length>1)throw new CliError("usage",`pass only one of ${sources.join(", ")}`);if(opts.verify){const extra=[source2,opts.dryRun&&"--dry-run",opts.files!==void 0&&"--files",opts.input.length>0&&"--input"].filter(Boolean);if(extra.length>0)throw new CliError("usage",`--verify checks the eval files in this repo; drop ${extra.join(", ")}`);const result2=verifyEvals(void 0,{local:gaugeState(cmd).mode==="local"});if(resolveOutput(cmd)==="json")printJson(result2);else printEvalVerification(result2);if(result2.summary.no>0)process.exitCode=1;return}if(opts.scenario.length>0&&source2!=="--preference")throw new CliError("usage","--scenario applies with --preference only");if(source2==="a prompt"){if(opts.samples!==void 0||opts.input.length>0)throw new CliError("usage","--samples and --input do not apply to prompt runs; use `gauge preference` or `gauge eval` for more settings")}else if((opts.agent.length>0||opts.samples!==void 0)&&source2!=="--eval"&&source2!=="--preference")throw new CliError("usage","--agent and --samples apply with --eval or --preference; eval case files set them in their front matter");if(opts.files!==void 0&&source2)throw new CliError("usage",`--files applies to this repo's eval cases; drop ${source2}`);if(opts.input.length>0&&(opts.eval!==void 0||opts.preference!==void 0)&&gaugeState(cmd).mode!=="local")throw new CliError("usage",`Hosted saved measurements do not take project inputs; drop --input or pass --local`,{hint:"For hosted project inputs, use eval case files or a case spec."});if(opts.requireExperiment!==void 0&&!opts.apply)throw new CliError("usage","--require-experiment applies with --apply");let spec;if(opts.apply!==void 0){const json=await readSpecJson(opts.apply);if(!isCaseSpec(json)){if(cliRules)throw new CliError("usage","--cli-rules does not apply to battery specs; use a case spec, prompt, --eval, or --preference.");if(gaugeState(cmd).mode==="local")throw new CliError("usage","Battery specs (with `prompts`) run on hosted Gauge only.",{hint:"For local runs, pass a case spec (with `cases`): `gauge run --example --format case`. Or pass --hosted."});if(opts.input.length>0)throw new CliError("usage","--input applies to case specs, not battery specs");const result2=await applySpec(cmd,parseSpec(json),{dryRun:opts.dryRun,requireExperiment:opts.requireExperiment});if(!opts.dryRun&&"runIds"in result2&&result2.runIds.length>0)console.error(RUN_PRESENTATION_REMINDER);return}if(opts.requireExperiment!==void 0)throw new CliError("usage","--require-experiment applies to battery specs, not case specs");spec=parseCaseSpec(json)}if(opts.prompt!==void 0)spec=promptSpec(opts.prompt,opts.agent.length>0?opts.agent:[callerAgent()]);if(gaugeState(cmd).mode==="local"&&(opts.eval||opts.preference)){const saved=localMeasurementSpec(cmd,opts.eval?"eval":"preference",opts.eval??opts.preference,{...opts,inputs:[...parseInputBindings(opts.input).keys()]});await runLocalCases(cmd,opts,saved.spec,saved.measurement);return}if(opts.dryRun&&source2&&!spec)throw new CliError("usage",`--dry-run previews this repo's eval cases or an --apply spec; drop ${source2}`);if(opts.eval!==void 0||opts.preference!==void 0)requireHosted(cmd);if(opts.eval!==void 0){const result2=await runEvalSet(cmd,opts.eval,{...opts,cliRules});if(result2.runIds.length>0||result2.runRequestId)console.error(RUN_PRESENTATION_REMINDER);return}if(opts.preference!==void 0){const result2=await runPreferencePrompt(cmd,opts.preference,{...opts,cliRules});if(result2.runIds.length>0)console.error(RUN_PRESENTATION_REMINDER);return}if(gaugeState(cmd).mode==="local"){await runLocalCases(cmd,opts,spec);return}const{definition,org,inputSnapshots,unattachedInputs}=await loadWorkingTree(cmd,opts,!opts.dryRun,spec,cliRules);const orgPath=`/api/v1/orgs/${encodeURIComponent(org)}/run-definitions`;if(opts.dryRun){const result2=await createClient().post(`${orgPath}/plan`,definition);const preview={...result2,inputSnapshots,unattachedInputs};if(resolveOutput(cmd)==="json")printJson(preview);else printPlan2(preview);return}const result=await createClient().post(`${orgPath}/run`,definition);if(resolveOutput(cmd)==="json")printJson(result);else{console.log(`Started ${result.createdRuns}/${result.requestedRuns} sessions in request ${result.runRequestId}`);console.log(`Wait: gauge --org ${org} run wait ${result.runRequestId}`)}if(result.requestedRuns>0)console.error(RUN_PRESENTATION_REMINDER)}async function runLocalCases(cmd,opts,spec,measurement){const{root}=gaugeState(cmd);if(!root)throw new CliError("usage","Local runs need a Gauge workspace. Run `gauge init` in this directory first.");const local=await loadLocalRunDefinition(opts.files??[],opts.input,process.cwd(),{local:true,spec});const json=resolveOutput(cmd)==="json";if(measurement&&local.unattachedInputs.length&&!opts.dryRun)console.error(`note: ${unattachedNote(local.unattachedInputs,true)}`);if(opts.dryRun){try{const plan={...await planLocal(local.definition,{profile:opts.profile,runtimeImage:opts.runtimeImage,gatewayImage:opts.gatewayImage}),inputSnapshots:local.inputSnapshots,webFixtures:Object.fromEntries(Object.entries(local.webFixtures).map(([path,{fixture,digest}])=>[path,{digest,pages:fixture.pages.map(p=>p.url),scopes:fixture.scopes??[],origins:fixture.origins??[]}])),unattachedInputs:local.unattachedInputs};if(json)printJson(plan);else printLocalPlan(plan,measurement!==void 0)}finally{await local.cleanup()}return}if(opts.detach){try{const detached=await detachLocal(local.definition,local.uploads,join11(root,".gauge"),{profile:opts.profile,runtimeImage:opts.runtimeImage,gatewayImage:opts.gatewayImage,measurement});if(json)printJson(detached);else console.log(`Batch ${detached.batchId} detached.
1013
+ Findings (${analysis.observations.length})`);for(const finding of analysis.observations){console.log(` ${finding.kind}: ${finding.label}`);console.log(` id ${finding.id}${finding.issueId?`, issue ${finding.issueId}`:""}`);console.log(` ${finding.detail}`);console.log(` Evidence: ${JSON.stringify(finding.evidence)}`)}if(recorded&&analysis.observations.length)console.log(`
1014
+ Test one fix on this session: gauge run fork ${insight.runId} --page <url> --edits <file>, or --before <seq> --message <text>. Pages it read: gauge run web ${insight.runId}.`);console.log(`
1015
+ Products (${analysis.products.length})`);if(analysis.products.length>0)printItems(analysis.products.map(p=>({product:p.name,considered:check2(p.considered),chosen:check2(p.chosen),rejected:check2(p.rejected),installed:INSTALLED[p.installPhase]??p.installPhase,rank:p.rank??"",source:p.source.toLowerCase()})),"table");const delta3=insight.experimentDelta;if(delta3&&typeof delta3==="object"){console.log("\nExperiment delta");console.log([delta3.changed==null?"":delta3.changed?"changed":"no change",delta3.hypothesisSupported??""].filter(Boolean).join(" "));if(delta3.summary)console.log(compact(delta3.summary,300))}console.error("\n(full detail: rerun with -o json)")}function parseEvent(data){try{const parsed=runEventSchema.safeParse(JSON.parse(data));return parsed.success?parsed.data:null}catch{return null}}function formatEventLine(e){const rec=e;const head=`[t${e.turn}#${e.seq}]`;switch(e.type){case"run_meta":{const agent=rec.agent;const model=agent?.model?` model=${agent.model}`:"";return`${head} run_meta agent=${agent?.name??"?"}${model}`}case"agent_message":return`${head} agent: ${compact(rec.text)}`;case"reasoning":return`${head} reasoning: ${compact(rec.text)}`;case"tool_call":return`${head} tool_call ${String(rec.name)}: ${compact(rec.input)}`;case"tool_result":return`${head} tool_result${rec.is_error?" ERROR":""}: ${compact(rec.output,100)}`;case"run_result":return`${head} run_result ${String(rec.exit_reason)} turns=${String(rec.turns)} cost=${fmtUsd(rec.cost_usd)} duration=${fmtDuration(rec.duration_ms)}`;default:{const{run_id:_runId,seq:_seq,turn:_turn,ts:_ts,type:_type,...payload}=rec;return`${head} ${e.type} ${compact(payload)}`}}}var TERMINAL_STATUSES=new Set(["SUCCEEDED","FAILED","TIMED_OUT","CANCELED"]);var STATUS_POLL_MS=5e3;var TERMINAL_DRAIN_MS=2e3;async function streamRun(id2,json,opts){const client=createClient();const controller=new AbortController;let timer;let poller;let drainTimer;let gotResult=false;let sawData=false;const arm=ms=>{if(timer)clearTimeout(timer);if(ms>0)timer=setTimeout(()=>controller.abort(),ms)};arm(opts.idleMs>0?opts.firstEventMs:0);if(opts.pollStatus){let inFlight=false;poller=setInterval(()=>{if(inFlight||drainTimer)return;inFlight=true;client.get(runPath2(id2)).then(run=>{if(drainTimer||!TERMINAL_STATUSES.has(run.status))return;if(poller)clearInterval(poller);drainTimer=setTimeout(()=>{if(!gotResult){console.error(`Run is ${run.status}; no more events.`)}controller.abort()},TERMINAL_DRAIN_MS)}).catch(()=>{}).finally(()=>{inFlight=false})},STATUS_POLL_MS)}try{await client.sse(streamPath(id2),frame=>{const event=parseEvent(frame.data);if(!event)return;sawData=true;if(json)console.log(frame.data);else console.log(formatEventLine(event));if(event.type==="run_result"){gotResult=true;controller.abort()}else if(opts.stopAtLiveMs!==void 0&&Date.parse(event.ts)>=opts.stopAtLiveMs){gotResult=true;controller.abort()}else arm(opts.idleMs)},{signal:controller.signal,onComment:()=>{if(!sawData&&opts.idleMs>0)arm(opts.firstEventMs)}})}finally{if(timer)clearTimeout(timer);if(poller)clearInterval(poller);if(drainTimer)clearTimeout(drainTimer)}}async function loadWorkingTree(cmd,opts,upload,spec,cliRules){const lifecycle=opts.prompt!==void 0?lifecyclePatchFromOpts(opts):{};const loaded=await loadLocalRunDefinition(opts.files??[],opts.input,void 0,{spec});try{const org=await resolveOrg(cmd,loaded.org);if(upload)await confirmOrAbort("Run these cases now?",opts.yes);await uploadLocalInputs(createClient(),org,loaded.uploads);return{definition:{...loaded.definition,...lifecycle,...cliRules?{cliRules}:{}},org,inputSnapshots:loaded.inputSnapshots,unattachedInputs:loaded.unattachedInputs}}finally{await loaded.cleanup()}}function printPlan2(result){console.log(`Definition: ${result.source.verification==="LOCAL"?"current files":result.source.commit}`);console.log(`Expected sessions: ${result.expectedRuns}`);if(result.cliRules)console.log(`CLI rules: ${result.cliRules.ruleCount} (this launch only; sha256:${result.cliRules.bundleSha256})`);if(result.lifecycle?.setupTimeoutSeconds!==void 0)console.log(`Setup hook: ${result.lifecycle.setupTimeoutSeconds}s deadline`);if(result.lifecycle?.teardownTimeoutSeconds!==void 0)console.log(`Teardown hook: ${result.lifecycle.teardownTimeoutSeconds}s deadline`);for(const item of result.cases)console.log(` ${item.path}: ${item.expectedRuns} sessions, ${item.preference?`${item.preference.kind} preference (advisory)`:`${item.criteriaCount} criteria`}`);for(const resource of result.resources){const use=resource.use;if(use.type==="npm-package"||use.type==="npm-cli"){const install=use.install??"environment";console.log(` ${resource.name}: npm package \u2192 ${install}`);const label=install==="environment"?"Commands on PATH":"Package commands";console.log(` ${label}: ${resource.commands===void 0?"determined when the input is prepared":resource.commands.join(", ")||"none (library)"}`)}else if(use.type==="binary"){console.log(` ${resource.name}: native executable \u2192 ${use.platform}`);console.log(` Commands on PATH: ${Object.keys(use.commands).join(", ")}`);console.log(" Requires compatible system libraries; Gauge does not install OS packages.")}else console.log(` ${resource.name}: ${use.type}`);if(resource.detail)console.log(` ${resource.detail}`)}if(result.resources.length){console.log(`Inputs: ${result.resources.map(r=>`${r.name} (${r.source}, ${r.readiness})`).join(", ")}`);if(!result.canRun)console.log("Resolve the input errors before launching.")}printInputSnapshots(result.inputSnapshots);printUnattachedInputs(result.unattachedInputs,false)}function printInputSnapshots(snapshots){for(const input of snapshots){console.log(` ${input.name}: ${input.source==="working-tree"?"working-tree snapshot":"local path override snapshot"}`);console.log(` Source path: ${input.path}`);const shown=input.files.slice(0,10);console.log(` Files (${input.fileCount}): ${shown.join(", ")}${input.fileCount>shown.length?`, and ${input.fileCount-shown.length} more`:""}`);console.log(` Gauge archive digest: ${input.gaugeArchive.sha256}`);console.log(` Gauge archive bytes: ${input.gaugeArchive.bytes}`)}if(snapshots.length)console.log("Gauge archive hashes identify delivery archives containing the selected files. For npm inputs, the archive contains the .tgz; its hash and size differ from the .tgz itself. Launching again snapshots current files.")}function unattachedNote(names,saved){return`Not attached: ${names.join(", ")}. ${saved?`Saved measurements attach only inputs passed at launch; add ${names.map(name=>`--input ${name}`).join(" ")} to attach.`:`The selected cases do not use ${names.length===1?"it":"them"}.`}`}function printUnattachedInputs(names,saved){if(names.length)console.log(unattachedNote(names,saved))}function callerAgent(){const caller=detectSkillAgent(process.env);return caller==="codex"||caller==="opencode"?caller:"claude-code"}function promptSpec(prompt,agents){return parseCaseSpec({version:2,cases:[{key:"prompt",prompt,preference:{kind:"open-ended"},config:{agents:buildAgentRoster(agents)}}]})}async function launch(cmd,opts){if(opts.cliRules!==void 0){if(gaugeState(cmd).mode!=="hosted")throw new CliError("usage","--cli-rules requires hosted Gauge; pass --hosted.");if(opts.schema||opts.example||opts.verify)throw new CliError("usage","--cli-rules applies to a launch or --dry-run, not --schema, --example, or --verify.")}const cliRules=opts.cliRules===void 0?void 0:readCliRules(opts.cliRules);if(opts.profile&&gaugeState(cmd).mode!=="local")throw new CliError("usage","--profile applies only to local runs. Pass --local.");const sources=[opts.prompt!==void 0&&"a prompt",opts.eval!==void 0&&"--eval",opts.preference!==void 0&&"--preference",opts.apply!==void 0&&"--apply"].filter(flag2=>Boolean(flag2));const source2=sources[0];if(hasLifecycleFlags(opts)){if(gaugeState(cmd).mode!=="hosted")throw new CliError("usage","Lifecycle hooks require hosted Gauge; pass --hosted.");if(source2!=="a prompt"&&source2!=="--eval"&&source2!=="--preference"||opts.apply!==void 0||opts.schema||opts.example||opts.verify||opts.dryRun&&source2!=="a prompt")throw new CliError("usage","Lifecycle flags apply only to a hosted prompt, --eval, or --preference launch; they do not apply to case files or --apply. Only prompt launches support --dry-run with hooks.")}if(opts.schema||opts.example){const flag2=opts.schema?"--schema":"--example";if(source2||opts.schema&&opts.example)throw new CliError("usage",`${flag2} prints and exits; pass it alone`);const cases=opts.format!=="battery";if(opts.schema)printJson(cases?CASE_SPEC_SCHEMA():BATTERY_SCHEMA());else{printJson(cases?EXAMPLE_CASE_SPEC:EXAMPLE_SPEC);console.error(cases?"\n# Case spec: eval cases as JSON. Save to cases.json, edit, then: gauge run --apply cases.json":"\n# Battery spec: saved prompts and schedules. Save to battery.json, edit, then: gauge run --apply battery.json\n# For eval cases as JSON (criteria, inputs): gauge run --example --format case")}return}if(opts.format)throw new CliError("usage","--format applies with --schema or --example");if(sources.length>1)throw new CliError("usage",`pass only one of ${sources.join(", ")}`);if(opts.verify){const extra=[source2,opts.dryRun&&"--dry-run",opts.files!==void 0&&"--files",opts.input.length>0&&"--input"].filter(Boolean);if(extra.length>0)throw new CliError("usage",`--verify checks the eval files in this repo; drop ${extra.join(", ")}`);const result2=verifyEvals(void 0,{local:gaugeState(cmd).mode==="local"});if(resolveOutput(cmd)==="json")printJson(result2);else printEvalVerification(result2);if(result2.summary.no>0)process.exitCode=1;return}if(opts.scenario.length>0&&source2!=="--preference")throw new CliError("usage","--scenario applies with --preference only");if(source2==="a prompt"){if(opts.samples!==void 0||opts.input.length>0)throw new CliError("usage","--samples and --input do not apply to prompt runs; use `gauge preference` or `gauge eval` for more settings")}else if((opts.agent.length>0||opts.samples!==void 0)&&source2!=="--eval"&&source2!=="--preference")throw new CliError("usage","--agent and --samples apply with --eval or --preference; eval case files set them in their front matter");if(opts.files!==void 0&&source2)throw new CliError("usage",`--files applies to this repo's eval cases; drop ${source2}`);if(opts.input.length>0&&(opts.eval!==void 0||opts.preference!==void 0)&&gaugeState(cmd).mode!=="local")throw new CliError("usage",`Hosted saved measurements do not take project inputs; drop --input or pass --local`,{hint:"For hosted project inputs, use eval case files or a case spec."});if(opts.requireExperiment!==void 0&&!opts.apply)throw new CliError("usage","--require-experiment applies with --apply");let spec;if(opts.apply!==void 0){const json=await readSpecJson(opts.apply);if(!isCaseSpec(json)){if(cliRules)throw new CliError("usage","--cli-rules does not apply to battery specs; use a case spec, prompt, --eval, or --preference.");if(gaugeState(cmd).mode==="local")throw new CliError("usage","Battery specs (with `prompts`) run on hosted Gauge only.",{hint:"For local runs, pass a case spec (with `cases`): `gauge run --example --format case`. Or pass --hosted."});if(opts.input.length>0)throw new CliError("usage","--input applies to case specs, not battery specs");const result2=await applySpec(cmd,parseSpec(json),{dryRun:opts.dryRun,requireExperiment:opts.requireExperiment});if(!opts.dryRun&&"runIds"in result2&&result2.runIds.length>0)console.error(RUN_PRESENTATION_REMINDER);return}if(opts.requireExperiment!==void 0)throw new CliError("usage","--require-experiment applies to battery specs, not case specs");spec=parseCaseSpec(json)}if(opts.prompt!==void 0)spec=promptSpec(opts.prompt,opts.agent.length>0?opts.agent:[callerAgent()]);if(gaugeState(cmd).mode==="local"&&(opts.eval||opts.preference)){const saved=localMeasurementSpec(cmd,opts.eval?"eval":"preference",opts.eval??opts.preference,{...opts,inputs:[...parseInputBindings(opts.input).keys()]});await runLocalCases(cmd,opts,saved.spec,saved.measurement);return}if(opts.dryRun&&source2&&!spec)throw new CliError("usage",`--dry-run previews this repo's eval cases or an --apply spec; drop ${source2}`);if(opts.eval!==void 0||opts.preference!==void 0)requireHosted(cmd);if(opts.eval!==void 0){const result2=await runEvalSet(cmd,opts.eval,{...opts,cliRules});if(result2.runIds.length>0||result2.runRequestId)console.error(RUN_PRESENTATION_REMINDER);return}if(opts.preference!==void 0){const result2=await runPreferencePrompt(cmd,opts.preference,{...opts,cliRules});if(result2.runIds.length>0)console.error(RUN_PRESENTATION_REMINDER);return}if(gaugeState(cmd).mode==="local"){await runLocalCases(cmd,opts,spec);return}const{definition,org,inputSnapshots,unattachedInputs}=await loadWorkingTree(cmd,opts,!opts.dryRun,spec,cliRules);const orgPath=`/api/v1/orgs/${encodeURIComponent(org)}/run-definitions`;if(opts.dryRun){const result2=await createClient().post(`${orgPath}/plan`,definition);const preview={...result2,inputSnapshots,unattachedInputs};if(resolveOutput(cmd)==="json")printJson(preview);else printPlan2(preview);return}const result=await createClient().post(`${orgPath}/run`,definition);if(resolveOutput(cmd)==="json")printJson(result);else{console.log(`Started ${result.createdRuns}/${result.requestedRuns} sessions in request ${result.runRequestId}`);console.log(`Wait: gauge --org ${org} run wait ${result.runRequestId}`)}if(result.requestedRuns>0)console.error(RUN_PRESENTATION_REMINDER)}async function runLocalCases(cmd,opts,spec,measurement){const{root}=gaugeState(cmd);if(!root)throw new CliError("usage","Local runs need a Gauge workspace. Run `gauge init` in this directory first.");const local=await loadLocalRunDefinition(opts.files??[],opts.input,process.cwd(),{local:true,spec});const json=resolveOutput(cmd)==="json";if(measurement&&local.unattachedInputs.length&&!opts.dryRun)console.error(`note: ${unattachedNote(local.unattachedInputs,true)}`);if(opts.dryRun){try{const plan={...await planLocal(local.definition,{profile:opts.profile,runtimeImage:opts.runtimeImage,gatewayImage:opts.gatewayImage}),inputSnapshots:local.inputSnapshots,webFixtures:Object.fromEntries(Object.entries(local.webFixtures).map(([path,{fixture,digest}])=>[path,{digest,pages:fixture.pages.map(p=>p.url),scopes:fixture.scopes??[],origins:fixture.origins??[]}])),unattachedInputs:local.unattachedInputs};if(json)printJson(plan);else printLocalPlan(plan,measurement!==void 0)}finally{await local.cleanup()}return}if(opts.detach){try{const detached=await detachLocal(local.definition,local.uploads,join11(root,".gauge"),{profile:opts.profile,runtimeImage:opts.runtimeImage,gatewayImage:opts.gatewayImage,measurement});if(json)printJson(detached);else console.log(`Batch ${detached.batchId} detached.
1008
1016
  Status: ${detached.status}
1009
1017
  Wait: ${detached.wait}
1010
1018
  Stop: ${detached.stop}`);console.error(RUN_PRESENTATION_REMINDER)}finally{await local.cleanup()}return}let batch;try{batch=await runLocal(local.definition,local.uploads,join11(root,".gauge"),run=>{if(!json)console.error(`${run.id} ${run.agent} ${run.model} ${run.case}: ${run.outcome}${run.detail?` (${run.detail})`:""}`)},{measurement,profile:opts.profile,runtimeImage:opts.runtimeImage,gatewayImage:opts.gatewayImage,onStart:batchId=>{console.error(`Batch ${batchId}
1011
1019
  Status: gauge run status --batch ${batchId} --local
1012
1020
  Wait: gauge run wait --batch ${batchId} --local
1013
- Stop: gauge run cancel --batch ${batchId} --local`);console.error(RUN_PRESENTATION_REMINDER)}})}finally{await local.cleanup()}if(batch.canceled&&!process.exitCode)process.exitCode=130;if(!process.exitCode&&batch.runs.some(run=>run.outcome!=="completed"))process.exitCode=1;if(json){printJson(batch);return}const counts=new Map;for(const run of batch.runs)counts.set(run.outcome,(counts.get(run.outcome)??0)+1);console.log(`Batch ${batch.batchId}: ${batch.runs.length} runs. ${[...counts].map(([outcome2,n])=>`${n} ${outcome2}`).join(", ")}.`);console.log(`Results: .gauge/runs (gauge run list --local --batch ${batch.batchId})`);console.log("");console.log(batch.next)}function printLocalPlan(plan,saved){console.log(`Local plan: ${plan.expectedSessions} session${plan.expectedSessions===1?"":"s"} in local Docker. Nothing started.`);console.log(`Runtime: ${plan.runtime.reference}${plan.runtime.imageId?` (${plan.runtime.imageId})`:""}. Compatibility probe runs at launch.`);if(plan.gateway)console.log(`Gateway: ${plan.gateway.reference}${plan.gateway.imageId?` (${plan.gateway.imageId})`:""}`);for(const item of plan.cases)console.log(` ${item.path}: ${item.sessions.map(s=>`${s.agent} ${s.model} \xD7${s.samples}`).join(", ")}; ${item.criteria} criteria`);console.log(`Inputs: ${plan.inputs.map(i=>`${i.name} (${i.use})`).join(", ")||"none"}`);printInputSnapshots(plan.inputSnapshots);printUnattachedInputs(plan.unattachedInputs,saved);for(const warning of plan.warnings)console.log(`Warning: ${warning}`);console.log("Run it: the same command without --dry-run.")}var WAIT_POLL_MS=5e3;var LOCAL_WAIT_POLL_MS=2e3;async function runRequestPath(cmd,id2){return`/api/v1/orgs/${encodeURIComponent(await resolveOrg(cmd))}/run-requests/${encodeURIComponent(id2)}`}function printRunRequest(result){const{counts}=result;const measured=counts.measuredRuns??0;if(!result.settled)console.log(`${result.status}: ${counts.terminalRuns}/${counts.createdRuns} sessions finished, ${counts.judgedRuns} judged, ${counts.pendingRuns} pending`);else console.log(measured>0?`${result.result}: ${measured} preference sessions measured${counts.passedRuns?`; ${counts.passedRuns} experience sessions passed`:""}`:`${result.result}: ${counts.passedRuns}/${counts.createdRuns} passed sessions (${counts.judgedRuns} judged)`);for(const batch of result.batches)for(const run of batch.runs)console.log(` ${run.id} ${run.agent} ${run.model||"(default)"}: ${run.preference?`${preferenceSelectionLabel(run.preference)} (advisory)`:run.result}`);console.log(`Request: ${result.id}`)}function failsCheck(result){return result.result!=="PASSED"&&result.result!=="MEASURED"}async function waitForRequest(cmd,id2,opts){const path=await runRequestPath(cmd,id2);const client=createClient();const json=resolveOutput(cmd)==="json";let lastStatus="";while(true){const result=await client.get(path);if(!json&&result.status!==lastStatus){console.error(`Run request ${id2}: ${result.status}`);lastStatus=result.status}if(result.settled){if(json)printJson(result);else printRunRequest(result);if(opts.check&&failsCheck(result))process.exitCode=1;return}await new Promise(resolve5=>setTimeout(resolve5,WAIT_POLL_MS))}}async function waitForLocalBatch(cmd,gaugeDir,batchId,opts){const json=resolveOutput(cmd)==="json";let lastFinished=-1;while(true){let batch;try{batch=await localBatchStatus(gaugeDir,batchId)}catch(error){if(error.code==="ENOENT")throw new CliError("not_found","Local batch not found");throw error}const settled=["completed","canceled","failed","interrupted"].includes(batch.state);if(!json&&!settled&&batch.finishedSessions!==lastFinished){console.error(`Batch ${batchId}: ${batch.finishedSessions??0}/${batch.expectedSessions} finished`);lastFinished=batch.finishedSessions}if(settled){const runs=await localStatus(gaugeDir,{batch:batchId});if(json)printJson({batch,runs,next:analysisNext(batchId)});else{printLocalStatus(batch,runs);console.log(analysisNext(batchId))}if(opts.check&&(batch.state!=="completed"||runs.some(run=>run.state!=="completed")))process.exitCode=1;return}await new Promise(resolve5=>setTimeout(resolve5,LOCAL_WAIT_POLL_MS))}}function printLocalStatus(batch,runs){if(batch)console.log(`Batch ${batch.batchId}: ${batch.state}${batch.lastKnownState?` (last known: ${batch.lastKnownState})`:""}; ${batch.finishedSessions??0}/${batch.expectedSessions} finished`);for(const run of runs){console.log(`${run.id}: ${run.state}${"lastKnownState"in run&&run.lastKnownState?` (last known: ${run.lastKnownState})`:""}${run.cleanupPending?" (cleanup pending)":""}${run.detail?` (${run.detail})`:""}`);if(run.stderrLog)console.log(` agent stderr: ${relative5(process.cwd(),run.stderrLog)}`)}}function register29(program2){const run=addLifecycleEditOptions(program2.command("run")).description('Start runs and read them. `gauge run "<prompt>"` runs one open-ended preference prompt and saves no measurement. With no flag: run this repo\'s eval cases (gauge-cases/) (local: in local Docker). Lifecycle flags attach hooks to hosted prompts or override saved --eval/--preference hooks for this launch only.').argument("[prompt]").usage('[options] ["<prompt>"]').option("--cli-rules <file>","JSON CLI rules for this hosted launch only (schema v3/v4)",once("--cli-rules")).option("--eval <id>","run a saved eval set (see `gauge eval`)",once("--eval")).option("--preference <id>","run a saved preference prompt",once("--preference")).option("--scenario <scenarioId>","with --preference: run only this scenario (repeatable)",collect5,[]).option("--agent <agent[:m1,m2]>","with a prompt or --eval/--preference: run these agents and model pins for this launch only (repeatable)",collect5,[]).option("--samples <n>","with --eval/--preference: sessions per agent and model for this launch only (1-50)").option("--apply <file>",'run a JSON spec ("-" for stdin): a case spec (`cases`, both modes) or a battery spec (`prompts`, hosted). See --example').option("--files <paths...>","eval case Markdown paths or globs (default gauge-cases/**/*.md)").option("--input <name[=path]>","attach a gauge.json input; =path or =glob replaces its configured files (repeatable)",collect5,[]).option("--detach","run a local batch in the background; print status and stop commands").option("--profile <profile>","local credential profile; overrides the default for every target").option("--runtime-image <image>","use a compatible custom Docker image for local runs (tag, digest, or image ID)").option("--gateway-image <image>","use a compatible custom gateway image for local web fixtures").option("--verify","check this repo's eval files; start no sessions").option("--dry-run","preview a prompt, this repo's eval cases, or an --apply spec; start no sessions").option("--require-experiment <label>","with --apply: fail unless every target resolves to this experiment label").option("--schema","print the JSON Schema of an --apply spec and exit").option("--example","print an example --apply spec and exit").addOption(new Option5("--format <format>","schema/example format (default: case)").choices(["case","battery"])).option("--yes","skip the confirmation").action(async(arg2,opts,cmd)=>{if(arg2!==void 0&&!/\s/.test(arg2.trim()))throw new CliError("usage",`unknown run command "${arg2}". Run a prompt with \`gauge run "<prompt>"\`. Launch a saved eval set or preference prompt with \`gauge run --eval <id>\` or \`--preference <id>\`; read runs with \`gauge run list\` or \`gauge run get <id> [--logs]\`.`);if(opts.detach&&(gaugeState(cmd).mode!=="local"||opts.dryRun||opts.verify||opts.schema||opts.example))throw new CliError("usage","--detach requires a local run");if((opts.runtimeImage||opts.gatewayImage)&&gaugeState(cmd).mode!=="local")throw new CliError("usage","--runtime-image and --gateway-image work only on local runs; pass --local");await launch(cmd,{...opts,prompt:arg2?.trim()})});run.hook("preAction",(parent,action)=>{if(action===parent)return;const allowed=action.name()==="list"?["eval","preference","agent"]:[];const stray=Object.entries(parent.opts()).filter(([key,value])=>!allowed.includes(key)&&value!==void 0&&!(Array.isArray(value)&&value.length===0)).map(([key])=>`--${key.replace(/[A-Z]/g,c=>`-${c.toLowerCase()}`)}`);if(stray.length>0)throw new CliError("usage",`${stray.join(", ")} ${stray.length===1?"is a launch flag":"are launch flags"}; \`gauge run ${action.name()}\` does not take ${stray.length===1?"it":"them"}`)});run.command("list").description("List runs (newest first). Filters combine. --preference <id> lists one prompt's runs with verdicts and takes no other filter. Local: runs in .gauge/").option("--eval <id>","filter by eval set (local: by eval)").option("--preference <id>","one preference prompt's runs with verdicts (no other filter)").option("--status <status>","filter by status, e.g. running,queued (local: completed,failed)").option("--batch <id>","filter by batch").option("--prompt <id>","filter by prompt").option("--experiment <experiment>","filter by experiment label").option("--since <window>","e.g. 24h, 7d, or an ISO date").option("--agent <agent>","filter by agent (local only)").option("--model <model>","filter by model (local only)").option("--unanalyzed","only runs without a published analysis (local only)").option("--limit <n>","page size (1-200)","50").option("--cursor <cursor>","resume from a previous page's nextCursor").action(async(_opts,command)=>{const opts=command.optsWithGlobals();const agents=[opts.agent??[]].flat();if(agents.length>1)throw new CliError("usage","pass one --agent to filter by");const agent=agents[0];const output=resolveOutput(command);const state=gaugeState(command);if(state.mode==="local"){if(opts.prompt||opts.experiment||opts.cursor)throw new CliError("usage","Local run list does not support --prompt, --experiment, or --cursor. Use --eval or --preference to select a saved measurement.");const items=state.root?listLocalRuns(join11(state.root,".gauge"),{batch:opts.batch,evalId:opts.eval,preferenceId:opts.preference,agent:agent?parseAgents(agent)[0]:void 0,model:opts.model,outcome:opts.status,since:opts.since?parseSince(opts.since):void 0,analyzed:opts.unanalyzed?false:void 0}).slice(0,Number(opts.limit)):[];if(output==="json")printJson({items});else printItems(items.map(toLocalRow),"table");return}const localOnly=[agent!==void 0&&"--agent",opts.model!==void 0&&"--model",opts.unanalyzed&&"--unanalyzed"].filter(Boolean);if(localOnly.length>0)throw new CliError("usage",`${localOnly.join(", ")} ${localOnly.length===1?"works":"work"} only on local runs; pass --local`);if(opts.preference!==void 0){const filters=[opts.eval!==void 0&&"--eval",opts.status!==void 0&&"--status",opts.batch!==void 0&&"--batch",opts.prompt!==void 0&&"--prompt",opts.experiment!==void 0&&"--experiment",opts.since!==void 0&&"--since"].filter(Boolean);if(filters.length>0)throw new CliError("usage",`--preference lists one prompt's runs and takes no other filter; drop ${filters.join(", ")}`);await listPreferencePromptRuns(command,opts.preference,opts);return}const org=await resolveOrg(command);const page=await createClient().get(`/api/v1/orgs/${encodeURIComponent(org)}/runs`,{status:opts.status,batch:opts.batch,prompt:opts.prompt,experiment:opts.experiment,evalSet:opts.eval,since:opts.since,limit:opts.limit,cursor:opts.cursor});if(output==="json"){printJson(page);return}printItems(page.items.map(toRow4),"table");if(page.nextCursor){console.error(`(more results \u2014 rerun with --cursor ${page.nextCursor})`)}});run.command("get <id>").description("Show a run (status, trigger source, cost, credits, turns, exit reason). --logs prints its trace events instead").option("--logs","print the trace events so far, then exit").action(async(id2,opts,command)=>{if(gaugeState(command).mode==="local"){const dir=localDirectory2(command);const session=withLocalStore(command,store=>store.getSession(id2));if(!session)throw new CliError("usage",`Local run ${id2} was not found. If its files are still in .gauge/runs, run \`gauge run repair --local\` to restore it to the run list.`);if(opts.logs){const path=join11(dir,"runs",session.id,"attempts",String(session.attempt),"session","events.ndjson");if(!existsSync4(path))throw new CliError("usage","This run has no saved transcript yet.");process.stdout.write(readFileSync13(path,"utf8"))}else printJson({...session,analysis:getLocalAnalysis(dir,id2)});return}if(opts.logs){await streamRun(id2,resolveOutput(command)==="json",{idleMs:2e3,firstEventMs:15e3,stopAtLiveMs:Date.now()-2e3});return}const run2=await createClient().get(runPath(id2));if(resolveOutput(command)==="json"){printJson(run2);return}const lines=detailLines(run2);const width=Math.max(...lines.map(([label])=>label.length));for(const[label,value]of lines){console.log(`${label.padEnd(width)} ${value}`)}});run.command("insight <id>").description("Show the run's findings, criteria, and product decisions").action(async(id2,_opts,command)=>{if(gaugeState(command).mode==="local"){const dir=localDirectory2(command);if(!withLocalStore(command,store=>store.getSession(id2)))throw new CliError("usage",`Local run ${id2} was not found.`);const analysis2=getLocalAnalysis(dir,id2);if(resolveOutput(command)==="json")printJson({runId:id2,analysis:analysis2});else console.log(analysis2?renderLocalAnalysisMarkdown(analysis2):"No analysis published. Follow gauge instructions session-analysis --local.");return}const insight=await createClient().get(`${runPath(id2)}/insight`);if(resolveOutput(command)==="json"){printJson(insight);return}printInsight(insight)});const analysis=run.command("analysis").description("Publish structured analysis from your coding agent");analysis.command("schema").description("Print the versioned analysis submission schema").action(()=>printJson(z15.toJSONSchema(analysisSubmissionSchema)));analysis.command("save <id>").description("Validate and publish analysis for a local session attempt").requiredOption("--file <path>","analysis JSON file").action((id2,opts,command)=>{if(gaugeState(command).mode!=="local")throw new CliError("usage","Analysis submission is available locally; hosted Gauge publishes its own analysis. Use --local.");let input;try{input=JSON.parse(readFileSync13(opts.file,"utf8"))}catch(error){throw new CliError("usage",`Cannot read analysis JSON: ${String(error)}`)}const dir=localDirectory2(command);const saved=saveLocalAnalysis(dir,id2,input);const results=join11(dir,"runs",saved.runId,"results");try{mkdirSync2(results,{recursive:true});writeFileSync4(join11(results,"session-analysis.md"),renderLocalAnalysisMarkdown(saved))}catch(error){console.error(`Analysis ${saved.id} was published in SQLite, but its Markdown report could not be written: ${String(error)}. Read it with gauge run insight ${id2} --local.`)}if(resolveOutput(command)==="json")printJson(saved);else console.log(`Published analysis ${saved.id} (revision ${saved.revision}); ${saved.findings.length} findings. Read with gauge run insight ${id2} --local.`)});run.command("repair").description("Restore local runs to the run list from their saved files, keeping measurements and analysis").action((_opts,command)=>{if(gaugeState(command).mode!=="local")throw new CliError("usage","Session repair is local only; pass --local.");printJson(withLocalStore(command,store=>store.repairSessionSummaries()))});hostedOnly(run.command("regrade <id>")).description("Re-judge using current eval criteria or the run's criteria snapshot (no new session)").action(async(id2,_opts,command)=>{const result=await createClient().post(`${runPath(id2)}/regrade`);if(resolveOutput(command)==="json"){printJson(result);return}console.log(`Re-judging run ${result.runId} against ${result.criteria} ${result.criteria===1?"criterion":"criteria"} ${result.evalSetId?`of eval ${result.evalSetId}`:"from its launch-time snapshot"}.`);console.log("Verdicts land on the run; `gauge run get <id>` to check.")});hostedOnly(run.command("watch <id>")).description("Stream normalized trace events live (SSE; exits when the run finishes)").action(async(id2,_opts,command)=>{await streamRun(id2,resolveOutput(command)==="json",{idleMs:0,firstEventMs:0,pollStatus:true})});hostedOnly(run.command("trace <id>")).description("A window of the run's trace around a seq, one line per event: web fetches, tool calls, results, and messages").requiredOption("--seq <n>","the seq at the center, e.g. a finding's evidence seq").option("--before <n>","events before the seq (default 30)").option("--after <n>","events after the seq (default 10)").action(async(id2,opts,command)=>{const res=await createClient().get(`${runPath(id2)}/trace`,{seq:opts.seq,before:opts.before,after:opts.after});if(resolveOutput(command)==="json")printJson(res);else for(const line of res.lines)console.log(line)});hostedOnly(run.command("web <id>")).description("Every URL the run's agent fetched, with the seqs, and its searches").action(async(id2,_opts,command)=>{const res=await createClient().get(`${runPath(id2)}/web`);if(resolveOutput(command)==="json")return printJson(res);for(const page of res.pages)console.log(`${page.seqs.join(",")} ${page.url}`);for(const search of res.searches)console.log(`${search.seq} search: ${search.query}`);if(!res.pages.length&&!res.searches.length)console.log("No web activity recorded.")});hostedOnly(run.command("commands <id>")).description("Shell commands the run's agent executed: without --seq, commands matching --query with their first output; with --seq, that call's full output").option("--query <text>","only commands whose text contains this").option("--seq <n>","one call from the listing").option("--offset <n>","page into a long output").action(async(id2,opts)=>{printJson(await createClient().get(`${runPath(id2)}/commands`,{query:opts.query,seq:opts.seq,offset:opts.offset}))});run.command("diff <id>").description("Print the run's working-tree diff to stdout").action(async(id2,_opts,command)=>{if(gaugeState(command).mode==="local"){const session=withLocalStore(command,store=>store.getSession(id2));if(!session)throw new CliError("usage",`Local run ${id2} was not found.`);const path=join11(localDirectory2(command),"runs",session.id,"attempts",String(session.attempt),"session","diff.patch");if(existsSync4(path))process.stdout.write(readFileSync13(path,"utf8"));else console.error("(no diff)");return}const diff2=await createClient().get(`${runPath(id2)}/diff`);if(!diff2){console.error("(no diff)");return}process.stdout.write(diff2)});run.command("status [id]").description("Print a launch request's progress once, without waiting; local: live session status and batch progress").option("--batch <batch-id>","show this local batch").action(async(id2,opts,command)=>{const state=gaugeState(command);if(state.mode!=="local"){if(!id2||opts.batch)throw new CliError("usage","Hosted status requires a request ID; --batch is local only");const result=await createClient().get(await runRequestPath(command,id2));if(resolveOutput(command)==="json")printJson(result);else printRunRequest(result);return}if(!state.root)throw new CliError("usage","Run this command inside the batch's Gauge workspace");if(id2&&opts.batch)throw new CliError("usage","Pass a run ID or --batch, not both");const gaugeDir=join11(state.root,".gauge");const runs=await localStatus(gaugeDir,{id:id2,batch:opts.batch});let batch;if(opts.batch){try{batch=await localBatchStatus(gaugeDir,opts.batch)}catch(error){if(error.code!=="ENOENT")throw error}}if((id2||opts.batch)&&!runs.length&&!batch)throw new CliError("not_found","Local run or batch not found");if(resolveOutput(command)==="json")printJson({batch,runs});else printLocalStatus(batch,runs)});run.command("cancel [id]").option("--batch <batch-id>","cancel an entire local batch, including queued sessions").description("Request cancellation of a run").action(async(id2,opts,command)=>{const state=gaugeState(command);if(state.mode==="local"){if(!state.root)throw new CliError("usage","Run this command inside the batch's Gauge workspace");const results=await cancelLocal(join11(state.root,".gauge"),{id:id2,batch:opts.batch});if(results.some(r=>r.status==="pending"))process.exitCode=1;if(resolveOutput(command)==="json")printJson({cancelRequested:true,runs:results});else{console.log(`Cancellation requested for ${opts.batch??id2}.`);for(const result2 of results)console.log(`${result2.id}: ${result2.status}${"detail"in result2?` (${result2.detail})`:""}`)}return}if(!id2||opts.batch)throw new CliError("usage","Hosted cancellation requires a run ID; --batch is local only");const result=await createClient().post(`${runPath(id2)}/cancel`);if(resolveOutput(command)==="json"){printJson(result);return}console.log(`Run ${result.id}: ${result.status}${result.cancelRequested?" (cancel requested)":""}`);if(result.note)console.error(result.note)});run.command("compare").description("Compare two sets of runs: pass rate, criteria, and median turns, cost, and duration, per agent and model. Local: completed runs and median turns").requiredOption("--baseline <key=value>","baseline runs (repeatable): request=, batch=, eval=, revision=, experiment=, trial=, agent=, model=, since=, until= (local: batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=)",collect5,[]).requiredOption("--candidate <key=value>","candidate runs (repeatable), same keys as --baseline",collect5,[]).addHelpText("after",`
1021
+ Stop: gauge run cancel --batch ${batchId} --local`);console.error(RUN_PRESENTATION_REMINDER)}})}finally{await local.cleanup()}if(batch.canceled&&!process.exitCode)process.exitCode=130;if(!process.exitCode&&batch.runs.some(run=>run.outcome!=="completed"))process.exitCode=1;if(json){printJson(batch);return}const counts=new Map;for(const run of batch.runs)counts.set(run.outcome,(counts.get(run.outcome)??0)+1);console.log(`Batch ${batch.batchId}: ${batch.runs.length} runs. ${[...counts].map(([outcome2,n])=>`${n} ${outcome2}`).join(", ")}.`);console.log(`Results: .gauge/runs (gauge run list --local --batch ${batch.batchId})`);console.log("");console.log(batch.next)}function printLocalPlan(plan,saved){console.log(`Local plan: ${plan.expectedSessions} session${plan.expectedSessions===1?"":"s"} in local Docker. Nothing started.`);console.log(`Runtime: ${plan.runtime.reference}${plan.runtime.imageId?` (${plan.runtime.imageId})`:""}. Compatibility probe runs at launch.`);if(plan.gateway)console.log(`Gateway: ${plan.gateway.reference}${plan.gateway.imageId?` (${plan.gateway.imageId})`:""}`);for(const item of plan.cases)console.log(` ${item.path}: ${item.sessions.map(s=>`${s.agent} ${s.model} \xD7${s.samples}`).join(", ")}; ${item.criteria} criteria`);console.log(`Inputs: ${plan.inputs.map(i=>`${i.name} (${i.use})`).join(", ")||"none"}`);printInputSnapshots(plan.inputSnapshots);printUnattachedInputs(plan.unattachedInputs,saved);for(const warning of plan.warnings)console.log(`Warning: ${warning}`);console.log("Run it: the same command without --dry-run.")}var WAIT_POLL_MS=5e3;var LOCAL_WAIT_POLL_MS=2e3;async function runRequestPath(cmd,id2){return`/api/v1/orgs/${encodeURIComponent(await resolveOrg(cmd))}/run-requests/${encodeURIComponent(id2)}`}function printRunRequest(result){const{counts}=result;const measured=counts.measuredRuns??0;if(!result.settled)console.log(`${result.status}: ${counts.terminalRuns}/${counts.createdRuns} sessions finished, ${counts.judgedRuns} judged, ${counts.pendingRuns} pending`);else console.log(measured>0?`${result.result}: ${measured} preference sessions measured${counts.passedRuns?`; ${counts.passedRuns} experience sessions passed`:""}`:`${result.result}: ${counts.passedRuns}/${counts.createdRuns} passed sessions (${counts.judgedRuns} judged)`);for(const batch of result.batches)for(const run of batch.runs)console.log(` ${run.id} ${run.agent} ${run.model||"(default)"}: ${run.preference?`${preferenceSelectionLabel(run.preference)} (advisory)`:run.result}`);console.log(`Request: ${result.id}`)}function failsCheck(result){return result.result!=="PASSED"&&result.result!=="MEASURED"}async function waitForRequest(cmd,id2,opts){const path=await runRequestPath(cmd,id2);const client=createClient();const json=resolveOutput(cmd)==="json";let lastStatus="";while(true){const result=await client.get(path);if(!json&&result.status!==lastStatus){console.error(`Run request ${id2}: ${result.status}`);lastStatus=result.status}if(result.settled){if(json)printJson(result);else printRunRequest(result);if(opts.check&&failsCheck(result))process.exitCode=1;return}await new Promise(resolve5=>setTimeout(resolve5,WAIT_POLL_MS))}}async function waitForLocalBatch(cmd,gaugeDir,batchId,opts){const json=resolveOutput(cmd)==="json";let lastFinished=-1;while(true){let batch;try{batch=await localBatchStatus(gaugeDir,batchId)}catch(error){if(error.code==="ENOENT")throw new CliError("not_found","Local batch not found");throw error}const settled=["completed","canceled","failed","interrupted"].includes(batch.state);if(!json&&!settled&&batch.finishedSessions!==lastFinished){console.error(`Batch ${batchId}: ${batch.finishedSessions??0}/${batch.expectedSessions} finished`);lastFinished=batch.finishedSessions}if(settled){const runs=await localStatus(gaugeDir,{batch:batchId});if(json)printJson({batch,runs,next:analysisNext(batchId)});else{printLocalStatus(batch,runs);console.log(analysisNext(batchId))}if(opts.check&&(batch.state!=="completed"||runs.some(run=>run.state!=="completed")))process.exitCode=1;return}await new Promise(resolve5=>setTimeout(resolve5,LOCAL_WAIT_POLL_MS))}}function printLocalStatus(batch,runs){if(batch)console.log(`Batch ${batch.batchId}: ${batch.state}${batch.lastKnownState?` (last known: ${batch.lastKnownState})`:""}; ${batch.finishedSessions??0}/${batch.expectedSessions} finished`);for(const run of runs){console.log(`${run.id}: ${run.state}${"lastKnownState"in run&&run.lastKnownState?` (last known: ${run.lastKnownState})`:""}${run.cleanupPending?" (cleanup pending)":""}${run.detail?` (${run.detail})`:""}`);if(run.stderrLog)console.log(` agent stderr: ${relative5(process.cwd(),run.stderrLog)}`)}}function register29(program2){const run=addLifecycleEditOptions(program2.command("run")).description('Start runs and read them. `gauge run "<prompt>"` runs one open-ended preference prompt and saves no measurement. With no flag: run this repo\'s eval cases (gauge-cases/) (local: in local Docker). Lifecycle flags attach hooks to hosted prompts or override saved --eval/--preference hooks for this launch only.').argument("[prompt]").usage('[options] ["<prompt>"]').option("--cli-rules <file>","JSON CLI rules for this hosted launch only (schema v3/v4)",once("--cli-rules")).option("--eval <id>","run a saved eval set (see `gauge eval`)",once("--eval")).option("--preference <id>","run a saved preference prompt",once("--preference")).option("--scenario <scenarioId>","with --preference: run only this scenario (repeatable)",collect5,[]).option("--agent <agent[:m1,m2]>","with a prompt or --eval/--preference: run these agents and model pins for this launch only (repeatable)",collect5,[]).option("--samples <n>","with --eval/--preference: sessions per agent and model for this launch only (1-50)").option("--apply <file>",'run a JSON spec ("-" for stdin): a case spec (`cases`, both modes) or a battery spec (`prompts`, hosted). See --example').option("--files <paths...>","eval case Markdown paths or globs (default gauge-cases/**/*.md)").option("--input <name[=path]>","attach a gauge.json input; =path or =glob replaces its configured files (repeatable)",collect5,[]).option("--detach","run a local batch in the background; print status and stop commands").option("--profile <profile>","local credential profile; overrides the default for every target").option("--runtime-image <image>","use a compatible custom Docker image for local runs (tag, digest, or image ID)").option("--gateway-image <image>","use a compatible custom gateway image for local web fixtures").option("--verify","check this repo's eval files; start no sessions").option("--dry-run","preview a prompt, this repo's eval cases, or an --apply spec; start no sessions").option("--require-experiment <label>","with --apply: fail unless every target resolves to this experiment label").option("--schema","print the JSON Schema of an --apply spec and exit").option("--example","print an example --apply spec and exit").addOption(new Option5("--format <format>","schema/example format (default: case)").choices(["case","battery"])).option("--yes","skip the confirmation").action(async(arg2,opts,cmd)=>{if(arg2!==void 0&&!/\s/.test(arg2.trim()))throw new CliError("usage",`unknown run command "${arg2}". Run a prompt with \`gauge run "<prompt>"\`. Launch a saved eval set or preference prompt with \`gauge run --eval <id>\` or \`--preference <id>\`; read runs with \`gauge run list\` or \`gauge run get <id> [--logs]\`.`);if(opts.detach&&(gaugeState(cmd).mode!=="local"||opts.dryRun||opts.verify||opts.schema||opts.example))throw new CliError("usage","--detach requires a local run");if((opts.runtimeImage||opts.gatewayImage)&&gaugeState(cmd).mode!=="local")throw new CliError("usage","--runtime-image and --gateway-image work only on local runs; pass --local");await launch(cmd,{...opts,prompt:arg2?.trim()})});run.hook("preAction",(parent,action)=>{if(action===parent)return;const allowed=action.name()==="list"?["eval","preference","agent"]:[];const stray=Object.entries(parent.opts()).filter(([key,value])=>!allowed.includes(key)&&value!==void 0&&!(Array.isArray(value)&&value.length===0)).map(([key])=>`--${key.replace(/[A-Z]/g,c=>`-${c.toLowerCase()}`)}`);if(stray.length>0)throw new CliError("usage",`${stray.join(", ")} ${stray.length===1?"is a launch flag":"are launch flags"}; \`gauge run ${action.name()}\` does not take ${stray.length===1?"it":"them"}`)});run.command("list").description("List runs (newest first). Filters combine. --preference <id> lists one prompt's runs with verdicts and takes no other filter. Local: runs in .gauge/").option("--eval <id>","filter by eval set (local: by eval)").option("--preference <id>","one preference prompt's runs with verdicts (no other filter)").option("--status <status>","filter by status, e.g. running,queued (local: completed,failed)").option("--batch <id>","filter by batch").option("--prompt <id>","filter by prompt").option("--experiment <experiment>","filter by experiment label").option("--since <window>","e.g. 24h, 7d, or an ISO date").option("--agent <agent>","filter by agent (local only)").option("--model <model>","filter by model (local only)").option("--unanalyzed","only runs without a published analysis (local only)").option("--limit <n>","page size (1-200)","50").option("--cursor <cursor>","resume from a previous page's nextCursor").action(async(_opts,command)=>{const opts=command.optsWithGlobals();const agents=[opts.agent??[]].flat();if(agents.length>1)throw new CliError("usage","pass one --agent to filter by");const agent=agents[0];const output=resolveOutput(command);const state=gaugeState(command);if(state.mode==="local"){if(opts.prompt||opts.experiment||opts.cursor)throw new CliError("usage","Local run list does not support --prompt, --experiment, or --cursor. Use --eval or --preference to select a saved measurement.");const items=state.root?listLocalRuns(join11(state.root,".gauge"),{batch:opts.batch,evalId:opts.eval,preferenceId:opts.preference,agent:agent?parseAgents(agent)[0]:void 0,model:opts.model,outcome:opts.status,since:opts.since?parseSince(opts.since):void 0,analyzed:opts.unanalyzed?false:void 0}).slice(0,Number(opts.limit)):[];if(output==="json")printJson({items});else printItems(items.map(toLocalRow),"table");return}const localOnly=[agent!==void 0&&"--agent",opts.model!==void 0&&"--model",opts.unanalyzed&&"--unanalyzed"].filter(Boolean);if(localOnly.length>0)throw new CliError("usage",`${localOnly.join(", ")} ${localOnly.length===1?"works":"work"} only on local runs; pass --local`);if(opts.preference!==void 0){const filters=[opts.eval!==void 0&&"--eval",opts.status!==void 0&&"--status",opts.batch!==void 0&&"--batch",opts.prompt!==void 0&&"--prompt",opts.experiment!==void 0&&"--experiment",opts.since!==void 0&&"--since"].filter(Boolean);if(filters.length>0)throw new CliError("usage",`--preference lists one prompt's runs and takes no other filter; drop ${filters.join(", ")}`);await listPreferencePromptRuns(command,opts.preference,opts);return}const org=await resolveOrg(command);const page=await createClient().get(`/api/v1/orgs/${encodeURIComponent(org)}/runs`,{status:opts.status,batch:opts.batch,prompt:opts.prompt,experiment:opts.experiment,evalSet:opts.eval,since:opts.since,limit:opts.limit,cursor:opts.cursor});if(output==="json"){printJson(page);return}printItems(page.items.map(toRow4),"table");if(page.nextCursor){console.error(`(more results \u2014 rerun with --cursor ${page.nextCursor})`)}});run.command("get <id>").description("Show a run (status, trigger source, cost, credits, turns, exit reason). --logs prints its trace events instead").option("--logs","print the trace events so far, then exit").action(async(id2,opts,command)=>{if(gaugeState(command).mode==="local"){const dir=localDirectory2(command);const session=withLocalStore(command,store=>store.getSession(id2));if(!session)throw new CliError("usage",`Local run ${id2} was not found. If its files are still in .gauge/runs, run \`gauge run repair --local\` to restore it to the run list.`);if(opts.logs){const path=join11(dir,"runs",session.id,"attempts",String(session.attempt),"session","events.ndjson");if(!existsSync4(path))throw new CliError("usage","This run has no saved transcript yet.");process.stdout.write(readFileSync13(path,"utf8"))}else printJson({...session,analysis:getLocalAnalysis(dir,id2)});return}if(opts.logs){await streamRun(id2,resolveOutput(command)==="json",{idleMs:2e3,firstEventMs:15e3,stopAtLiveMs:Date.now()-2e3});return}const run2=await createClient().get(runPath2(id2));if(resolveOutput(command)==="json"){printJson(run2);return}const lines=detailLines(run2);const width=Math.max(...lines.map(([label])=>label.length));for(const[label,value]of lines){console.log(`${label.padEnd(width)} ${value}`)}});run.command("insight <id>").description("Show the run's findings, criteria, and product decisions").action(async(id2,_opts,command)=>{if(gaugeState(command).mode==="local"){const dir=localDirectory2(command);if(!withLocalStore(command,store=>store.getSession(id2)))throw new CliError("usage",`Local run ${id2} was not found.`);const analysis2=getLocalAnalysis(dir,id2);if(resolveOutput(command)==="json")printJson({runId:id2,analysis:analysis2});else console.log(analysis2?renderLocalAnalysisMarkdown(analysis2):"No analysis published. Follow gauge instructions session-analysis --local.");return}const client=createClient();if(resolveOutput(command)==="json"){printJson(await client.get(`${runPath2(id2)}/insight`));return}const[insight,run2]=await Promise.all([client.get(`${runPath2(id2)}/insight`),client.get(runPath2(id2)).catch(()=>null)]);printInsight(insight,run2?.recorded===true)});const analysis=run.command("analysis").description("Publish structured analysis from your coding agent");analysis.command("schema").description("Print the versioned analysis submission schema").action(()=>printJson(z15.toJSONSchema(analysisSubmissionSchema)));analysis.command("save <id>").description("Validate and publish analysis for a local session attempt").requiredOption("--file <path>","analysis JSON file").action((id2,opts,command)=>{if(gaugeState(command).mode!=="local")throw new CliError("usage","Analysis submission is available locally; hosted Gauge publishes its own analysis. Use --local.");let input;try{input=JSON.parse(readFileSync13(opts.file,"utf8"))}catch(error){throw new CliError("usage",`Cannot read analysis JSON: ${String(error)}`)}const dir=localDirectory2(command);const saved=saveLocalAnalysis(dir,id2,input);const results=join11(dir,"runs",saved.runId,"results");try{mkdirSync2(results,{recursive:true});writeFileSync5(join11(results,"session-analysis.md"),renderLocalAnalysisMarkdown(saved))}catch(error){console.error(`Analysis ${saved.id} was published in SQLite, but its Markdown report could not be written: ${String(error)}. Read it with gauge run insight ${id2} --local.`)}if(resolveOutput(command)==="json")printJson(saved);else console.log(`Published analysis ${saved.id} (revision ${saved.revision}); ${saved.findings.length} findings. Read with gauge run insight ${id2} --local.`)});run.command("repair").description("Restore local runs to the run list from their saved files, keeping measurements and analysis").action((_opts,command)=>{if(gaugeState(command).mode!=="local")throw new CliError("usage","Session repair is local only; pass --local.");printJson(withLocalStore(command,store=>store.repairSessionSummaries()))});hostedOnly(run.command("regrade <id>")).description("Re-judge using current eval criteria or the run's criteria snapshot (no new session)").action(async(id2,_opts,command)=>{const result=await createClient().post(`${runPath2(id2)}/regrade`);if(resolveOutput(command)==="json"){printJson(result);return}console.log(`Re-judging run ${result.runId} against ${result.criteria} ${result.criteria===1?"criterion":"criteria"} ${result.evalSetId?`of eval ${result.evalSetId}`:"from its launch-time snapshot"}.`);console.log("Verdicts land on the run; `gauge run get <id>` to check.")});registerRunFork(run);hostedOnly(run.command("watch <id>")).description("Stream normalized trace events live (SSE; exits when the run finishes)").action(async(id2,_opts,command)=>{await streamRun(id2,resolveOutput(command)==="json",{idleMs:0,firstEventMs:0,pollStatus:true})});hostedOnly(run.command("trace <id>")).description("A window of the run's trace around a seq, one line per event: web fetches, tool calls, results, and messages").requiredOption("--seq <n>","the seq at the center, e.g. a finding's evidence seq").option("--before <n>","events before the seq (default 30)").option("--after <n>","events after the seq (default 10)").action(async(id2,opts,command)=>{const res=await createClient().get(`${runPath2(id2)}/trace`,{seq:opts.seq,before:opts.before,after:opts.after});if(resolveOutput(command)==="json")printJson(res);else for(const line of res.lines)console.log(line)});hostedOnly(run.command("web <id>")).description("Every URL the run's agent fetched, with the seqs, and its searches").action(async(id2,_opts,command)=>{const res=await createClient().get(`${runPath2(id2)}/web`);if(resolveOutput(command)==="json")return printJson(res);for(const page of res.pages)console.log(`${page.seqs.join(",")} ${page.url}`);for(const search of res.searches)console.log(`${search.seq} search: ${search.query}`);if(!res.pages.length&&!res.searches.length)console.log("No web activity recorded.")});hostedOnly(run.command("commands <id>")).description("Shell commands the run's agent executed: without --seq, commands matching --query with their first output; with --seq, that call's full output").option("--query <text>","only commands whose text contains this").option("--seq <n>","one call from the listing").option("--offset <n>","page into a long output").action(async(id2,opts)=>{printJson(await createClient().get(`${runPath2(id2)}/commands`,{query:opts.query,seq:opts.seq,offset:opts.offset}))});run.command("diff <id>").description("Print the run's working-tree diff to stdout").action(async(id2,_opts,command)=>{if(gaugeState(command).mode==="local"){const session=withLocalStore(command,store=>store.getSession(id2));if(!session)throw new CliError("usage",`Local run ${id2} was not found.`);const path=join11(localDirectory2(command),"runs",session.id,"attempts",String(session.attempt),"session","diff.patch");if(existsSync4(path))process.stdout.write(readFileSync13(path,"utf8"));else console.error("(no diff)");return}const diff2=await createClient().get(`${runPath2(id2)}/diff`);if(!diff2){console.error("(no diff)");return}process.stdout.write(diff2)});run.command("status [id]").description("Print a launch request's progress once, without waiting; local: live session status and batch progress").option("--batch <batch-id>","show this local batch").action(async(id2,opts,command)=>{const state=gaugeState(command);if(state.mode!=="local"){if(!id2||opts.batch)throw new CliError("usage","Hosted status requires a request ID; --batch is local only");const result=await createClient().get(await runRequestPath(command,id2));if(resolveOutput(command)==="json")printJson(result);else printRunRequest(result);return}if(!state.root)throw new CliError("usage","Run this command inside the batch's Gauge workspace");if(id2&&opts.batch)throw new CliError("usage","Pass a run ID or --batch, not both");const gaugeDir=join11(state.root,".gauge");const runs=await localStatus(gaugeDir,{id:id2,batch:opts.batch});let batch;if(opts.batch){try{batch=await localBatchStatus(gaugeDir,opts.batch)}catch(error){if(error.code!=="ENOENT")throw error}}if((id2||opts.batch)&&!runs.length&&!batch)throw new CliError("not_found","Local run or batch not found");if(resolveOutput(command)==="json")printJson({batch,runs});else printLocalStatus(batch,runs)});run.command("cancel [id]").option("--batch <batch-id>","cancel an entire local batch, including queued sessions").description("Request cancellation of a run").action(async(id2,opts,command)=>{const state=gaugeState(command);if(state.mode==="local"){if(!state.root)throw new CliError("usage","Run this command inside the batch's Gauge workspace");const results=await cancelLocal(join11(state.root,".gauge"),{id:id2,batch:opts.batch});if(results.some(r=>r.status==="pending"))process.exitCode=1;if(resolveOutput(command)==="json")printJson({cancelRequested:true,runs:results});else{console.log(`Cancellation requested for ${opts.batch??id2}.`);for(const result2 of results)console.log(`${result2.id}: ${result2.status}${"detail"in result2?` (${result2.detail})`:""}`)}return}if(!id2||opts.batch)throw new CliError("usage","Hosted cancellation requires a run ID; --batch is local only");const result=await createClient().post(`${runPath2(id2)}/cancel`);if(resolveOutput(command)==="json"){printJson(result);return}console.log(`Run ${result.id}: ${result.status}${result.cancelRequested?" (cancel requested)":""}`);if(result.note)console.error(result.note)});run.command("compare").description("Compare two sets of runs: pass rate, criteria, and median turns, cost, and duration, per agent and model. Local: completed runs and median turns").requiredOption("--baseline <key=value>","baseline runs (repeatable): request=, batch=, eval=, revision=, experiment=, trial=, agent=, model=, since=, until= (local: batch=, eval=<saved id>, revision=<number>, agent=, model=, since=, until=)",collect5,[]).requiredOption("--candidate <key=value>","candidate runs (repeatable), same keys as --baseline",collect5,[]).addHelpText("after",`
1014
1022
  ${style("1")(" EXAMPLES")}
1015
1023
  gauge run compare --baseline request=<id> --candidate request=<id>
1016
1024
  gauge run compare --baseline eval=<id> --candidate trial=<trialId>