@withgauge/cli 0.24.0 → 0.25.1

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/README.md CHANGED
@@ -1,14 +1,15 @@
1
1
  # @withgauge/cli
2
2
 
3
3
  `gauge` — the [Gauge](https://agents.withgauge.com) command-line interface.
4
- Manage your eval sets, Agent Preference prompts, and agent runs, and watch runs
5
- live from your terminal.
4
+ Access Gauge Chat data, manage eval sets and Agent Preference prompts, and
5
+ watch agent runs live from your terminal.
6
6
 
7
7
  ```sh
8
8
  npm install -g @withgauge/cli
9
9
  gauge update # update the CLI to the latest published version
10
10
  gauge onboard # sign in and create a workspace (account only)
11
11
  gauge auth login # (or just sign in)
12
+ gauge orgs # list your organizations and the products each uses
12
13
  gauge orgs use <org> # set your default organization
13
14
  gauge init # initialize a workspace; local is the default
14
15
  gauge init --hosted # set a private hosted backend preference
@@ -17,6 +18,53 @@ gauge init --cases # optionally scaffold shared gauge-cases/
17
18
 
18
19
  Requires Node.js ≥ 22.13.
19
20
 
21
+ ## Organizations
22
+
23
+ Run `gauge orgs` to see your organizations and whether each has Chat, Agents,
24
+ or both. Use the value in the `slug` column to choose one:
25
+
26
+ ```sh
27
+ gauge orgs use acme # save your default organization
28
+ gauge orgs use acme --project # save this project's organization
29
+ gauge chat tags list --org acme # choose an organization for one command
30
+ ```
31
+
32
+ The selection works for both products. A command's `--org` takes priority,
33
+ followed by its run specification, the project setting, then your default.
34
+ Chat can pick your organization automatically if you have only one; Agents
35
+ commands need a saved selection or `--org`.
36
+
37
+ If your account can access an organization that isn't listed by `gauge orgs`,
38
+ select it by public slug with `--org` or `gauge orgs use`. The server checks
39
+ whether your account has access. Lookup outside the list currently requires
40
+ the organization to have Chat enabled. For an unlisted Agents-only organization,
41
+ use `--org <canonical-uuid>`.
42
+
43
+ **Upgrading:** If your account has only one organization, `gauge auth login`
44
+ updates your old saved default automatically. Otherwise, run `gauge orgs`, then
45
+ `gauge orgs use <slug>` to choose your default. This includes defaults that an
46
+ older CLI selected automatically when you signed in or created an organization.
47
+ For an old project setting, run `gauge orgs use <slug> --project`.
48
+ Everyone using an updated project needs
49
+ CLI 0.25.0 or later; restore the previous `gauge.json` before using an older
50
+ CLI. If an older run specification contains an `org` name, pass `--org` to
51
+ choose the organization for that run.
52
+
53
+ Chat commands using an API key default to the key's organization, regardless
54
+ of your saved selection. Agency keys can use `--org` to choose a client.
55
+
56
+ ## Checking your account
57
+
58
+ `gauge whoami` shows the account or API key you are using and its organizations.
59
+ `gauge auth status` checks whether your login or API key works and shows its
60
+ source, permissions, and expiry. See [signing in](../docs/shared-cli-auth.md)
61
+ for login, logout, and API-key setup.
62
+
63
+ For scripts, add `-o json`: `whoami` returns `user`, `apiKey`, and `orgs`;
64
+ `auth status` returns `authenticated`, `source`, `apiUrl`, `authUrl`, and
65
+ `credential` (`kind`, `scopes`, `expiresAt`). Older Agents tokens do not report
66
+ organizations, permissions, or expiry.
67
+
20
68
  ## Workspace and local storage
21
69
 
22
70
  `gauge init --local` works without Git. It writes a minimal shared manifest:
@@ -129,7 +177,8 @@ The UUID is independent of login and is never linked to your Gauge account.
129
177
  This is pseudonymous usage tracking: an installation can be recognized across
130
178
  commands, but no personal identity is sent. HTTP requests necessarily expose a
131
179
  source IP to the receiving infrastructure; event properties mask IP addresses,
132
- disable GeoIP enrichment, and disable person-profile creation.
180
+ disable GeoIP enrichment, and disable person-profile creation. Requests
181
+ identify themselves with a `gauge-cli/<version>` User-Agent.
133
182
 
134
183
  Preferences live in `~/.config/gauge/telemetry.json` and the UUID in
135
184
  `~/.config/gauge/telemetry-id` (respecting `XDG_CONFIG_HOME`). They are shared
@@ -170,6 +219,7 @@ contains the execution preference. Git is optional for workspace initialization.
170
219
  |---|---|---|
171
220
  | Skill, npm package (CLI or SDK), and `files` inputs | yes | yes |
172
221
  | Docs supplied as `files` inputs | yes | yes |
222
+ | Web fixture JSON: exact pages, scopes, and origin proxying | yes | yes |
173
223
  | Eval and Agent Preference cases in `gauge-cases/` | yes (you read the sessions) | yes (judged) |
174
224
  | `run`, `run list`, `run compare --local`, `models list`, `cleanup`, `feedback` | yes | yes |
175
225
  | Binary, website, and MCP inputs | no | yes |
@@ -291,6 +341,14 @@ tarball has a different hash and size. Each launch takes a fresh snapshot.
291
341
  Each case owns its agents, models, consumer repository/ref, Persona and criteria.
292
342
  The optional `config.inputs` selects named inputs; all attach by default.
293
343
 
344
+ A `web-fixture` input controls what agents receive from specific HTTPS URLs:
345
+ exact pages, 404s for missing pages under a scope, or an origin routed to a
346
+ preview. Declare it as `"docs": { "type": "web-fixture", "path": "fixtures/docs.json" }`,
347
+ select it with `config.inputs`, and override it with `--input docs=path.json`
348
+ on either backend. Local runs serve fixtures through a gateway container;
349
+ `gauge local runtime inspect` and `pull` cover its image. See
350
+ [web fixture inputs](https://github.com/gauge-sh/gauge-cli/blob/main/docs/local-web-fixtures.md).
351
+
294
352
  `verify` reads the repository's current files, including non-ignored untracked files,
295
353
  without login, network access, a PR, built CI artifacts, or sandbox compute. It checks
296
354
  configuration, case input references, Git input paths, and recognizable workflow
@@ -527,9 +585,35 @@ run locally yet.
527
585
 
528
586
  ### Authenticated MCP servers
529
587
 
530
- Connect your account in Gauge's MCP setup, completing OAuth in your browser when
531
- required. Add the server's versioned reference to each case's `config.mcpRefs`.
532
- Gauge manages credentials for the session; keep tokens out of prompts and Git.
588
+ Add an authenticated remote MCP from the CLI:
589
+
590
+ ```sh
591
+ gauge mcp add --hosted --org <org> --name gauge --url https://app.withgauge.com/mcp --oauth --label v1
592
+ ```
593
+
594
+ The command opens browser consent, waits for completion, then saves `gauge@v1`
595
+ bound to the new connection. Gauge stores and refreshes OAuth credentials; the
596
+ CLI receives only the connection ID. Use `--no-browser` in a remote terminal:
597
+ the URL is printed to stderr and the command still waits. `--oauth-timeout`
598
+ sets a shorter wait (default and maximum 600 seconds).
599
+
600
+ To reuse an existing connection, run `gauge connections list --hosted`, then
601
+ use `--connection <id>` instead of `--oauth`. Only connections belonging to the
602
+ organization and permitting the MCP host can be bound. A connection binding is
603
+ part of the saved version: include it when appending an authenticated version.
604
+ `gauge mcp get <name> --hosted` shows each version's connection ID.
605
+
606
+ If the server requires manual client registration, the command prints the
607
+ callback URL. Retry with `--oauth-client-id <id>` and, if needed,
608
+ `--oauth-client-secret-env <ENV_VAR_NAME>`. The variable's value goes only to
609
+ Gauge's OAuth service, never into the saved MCP spec or command output.
610
+ `--header Authorization` declares a name; it does not configure authentication.
611
+
612
+ After setup, attach the version with `gauge eval edit <id> --hosted --mcp gauge@v1`
613
+ or each case's `config.mcpRefs`. MCP execution is hosted-only. Keep tokens out of
614
+ prompts and Git. The Gauge API must support MCP OAuth setup and connection
615
+ bindings before using these flags. Existing UI-created authenticated servers
616
+ continue to work.
533
617
 
534
618
  ### Website previews
535
619
 
@@ -771,6 +855,33 @@ Every read supports `-o json` for scripting. `GAUGE_API_TOKEN` overrides
771
855
  stored credentials (useful in CI). Exit codes: `0` ok · `1` error ·
772
856
  `2` usage · `3` billing-blocked · `4` needs your input (`optimizations watch`).
773
857
 
858
+ ## Gauge Chat
859
+
860
+ `gauge chat` manages tracked AI-visibility prompts and reads Gauge Chat data.
861
+ Sign in with `gauge auth login`, or use an [API key](../docs/shared-cli-auth.md#api-keys-for-scripts-and-ci)
862
+ for scripts and CI. Choose your [organization](#organizations), then use
863
+ `--category` to select a category or `gauge chat categories use <category>`
864
+ to save a default. If there is only one category, it is selected automatically.
865
+ Chat commands do not use the Agents `--local` and `--hosted` flags.
866
+
867
+ ```sh
868
+ gauge chat topics create "Pricing" --if-exists
869
+ gauge chat prompts add --file prompts.txt --topic pricing --dry-run -o json
870
+ gauge chat query --dataset answers -d brand -m visibility,shareOfVoice --last-days 30
871
+ gauge chat query fields --dataset citations
872
+ gauge chat answers list --since 2026-10-01 --all --max 500 -o json
873
+ ```
874
+
875
+ `prompts add` reads arguments or a file of lines, JSON Lines, or a JSON array,
876
+ sends batches of 500 (previewing every batch before a multi-batch write), and
877
+ reports `created`, `already_exists`, or `would_create` per prompt. List commands
878
+ return `{data, nextCursor}` with `-o json`; `--all` follows cursors up to `--max`.
879
+ Errors carry the API code (`insufficient_scope`, `limit_exceeded`,
880
+ `incompatible_dimension`, `rate_limited`…) and details such as `retryAfter`.
881
+ Invalid requests exit 2 and plan limits exit 3. Query rate metrics are 0-1
882
+ fractions, as in `gauge query`. Rate limits apply per token and organization:
883
+ 600 requests/min and 60 queries/min.
884
+
774
885
  ## Support
775
886
 
776
887
  Questions or problems: [support@withgauge.com](mailto:support@withgauge.com).
@@ -1,2 +1,2 @@
1
- import{runLocal}from"./chunk-NVAONV7U.js";import"./chunk-FCK3MQPV.js";import"./chunk-RB7Q72OV.js";import"./chunk-PSBF4TEK.js";import{randomBytes}from"crypto";import{readFile,rename,rm,writeFile}from"fs/promises";import{dirname,join}from"path";var requestPath=process.argv[2];var directory=dirname(requestPath);var requestLoaded=false;try{const request=JSON.parse(await readFile(requestPath,"utf8"));if(!request||typeof request.batchId!=="string"||typeof request.gaugeDir!=="string"||!request.definition)throw new Error("Invalid batch request");requestLoaded=true;await rm(requestPath);await runLocal(request.definition,[],request.gaugeDir,()=>{},{batchId:request.batchId,credentialSelections:request.credentialSelections,measurement:request.measurement})}catch(error){process.exitCode=1;const detail=requestLoaded?String(error):"Could not read a valid background batch request. Start a new batch.";console.error(detail);const statusPath=join(directory,"status.json");const previous=await readFile(statusPath,"utf8").then(raw=>JSON.parse(raw)).catch(()=>({}));const finishedSessions=previous.finishedSessions??0;const status={...previous,state:"failed",detail,finishedSessions,queuedSessions:0,skippedSessions:Math.max(0,(previous.expectedSessions??0)-finishedSessions),supervisorPid:process.pid};const temporary=`${statusPath}.${randomBytes(6).toString("hex")}.tmp`;await writeFile(temporary,`${JSON.stringify(status,null,2)}
1
+ import{runLocal}from"./chunk-UANK5LHE.js";import"./chunk-QV7MOO3M.js";import"./chunk-RRLAI72I.js";import"./chunk-OIGVDK3F.js";import"./chunk-PSBF4TEK.js";import{randomBytes}from"crypto";import{readFile,rename,rm,writeFile}from"fs/promises";import{dirname,join}from"path";var requestPath=process.argv[2];var directory=dirname(requestPath);var requestLoaded=false;try{const request=JSON.parse(await readFile(requestPath,"utf8"));if(!request||typeof request.batchId!=="string"||typeof request.gaugeDir!=="string"||!request.definition)throw new Error("Invalid batch request");requestLoaded=true;await rm(requestPath);await runLocal(request.definition,[],request.gaugeDir,()=>{},{batchId:request.batchId,credentialSelections:request.credentialSelections,measurement:request.measurement})}catch(error){process.exitCode=1;const detail=requestLoaded?String(error):"Could not read a valid background batch request. Start a new batch.";console.error(detail);const statusPath=join(directory,"status.json");const previous=await readFile(statusPath,"utf8").then(raw=>JSON.parse(raw)).catch(()=>({}));const finishedSessions=previous.finishedSessions??0;const status={...previous,state:"failed",detail,finishedSessions,queuedSessions:0,skippedSessions:Math.max(0,(previous.expectedSessions??0)-finishedSessions),supervisorPid:process.pid};const temporary=`${statusPath}.${randomBytes(6).toString("hex")}.tmp`;await writeFile(temporary,`${JSON.stringify(status,null,2)}
2
2
  `,{mode:384});await rename(temporary,statusPath);await writeFile(join(directory,"error.txt"),detail,{mode:384})}
@@ -0,0 +1 @@
1
+ import{CHAT_API_PREFIX,CHAT_BATCH_SIZE,ChatApiClient,DEFAULT_MAX_ITEMS,addPageOptions,categoryScope,chatPath,chatScope,chunks,createChatApiClient,csv,describeScopes,fetchAll,fetchPages,parsePositive,printPage,resolveChatCategory,resolveChatOrg,resolveTagIds,resolveTopicId,truncate,withCategory}from"./chunk-WJTCHKG6.js";import"./chunk-E24F53BZ.js";import"./chunk-RRLAI72I.js";import"./chunk-OIGVDK3F.js";import"./chunk-PSBF4TEK.js";export{CHAT_API_PREFIX,CHAT_BATCH_SIZE,ChatApiClient,DEFAULT_MAX_ITEMS,addPageOptions,categoryScope,chatPath,chatScope,chunks,createChatApiClient,csv,describeScopes,fetchAll,fetchPages,parsePositive,printPage,resolveChatCategory,resolveChatOrg,resolveTagIds,resolveTopicId,truncate,withCategory};
@@ -0,0 +1 @@
1
+ import{CliError,createClient,resolveToken}from"./chunk-RRLAI72I.js";var UUID_RE=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;var isUuid=value=>UUID_RE.test(value);function credentialKind(token=resolveToken()){if(!token)return"none";if(token.startsWith("gauge_cli_"))return"session";if(token.startsWith("gauge_sk_")||!token.startsWith("gauge_"))return"api_key";return"legacy"}async function fetchDirectory(){if(credentialKind()==="legacy")throw new CliError("usage","This Agents token cannot list or select organizations in this version of the CLI.",{hint:"If GAUGE_API_TOKEN is set, unset it first. Then run `gauge auth login` and `gauge orgs`."});const{chatPath,createChatApiClient}=await import("./chatApi-W6HW3HKD.js");const{data}=await createChatApiClient().get(chatPath("orgs"));if(!Array.isArray(data))throw new CliError("internal","Gauge returned an organization list the CLI could not read.",{hint:"Try `gauge orgs` again. If this continues, contact Gauge support."});return data}async function findOrganization(value){const org=(await fetchDirectory()).find(org2=>org2.id===value||org2.slug===value);if(org)return org;const{chatPath,createChatApiClient}=await import("./chatApi-W6HW3HKD.js");return createChatApiClient().get(chatPath("orgs",value))}async function resolveOrgValue(value){return isUuid(value)?{id:value}:findOrganization(value)}function legacySelection(source){const message=source==="config"?"Your saved organization needs to be selected again after this CLI update.":source==="project"?"This project's organization needs to be selected again after this CLI update.":"The organization in this run file needs to be selected again after this CLI update.";const hint=source==="config"?"Run `gauge orgs` to see your organizations, then `gauge orgs use <slug>` to choose one. If you have only one organization, `gauge auth login` updates your saved choice automatically.":source==="project"?"Run `gauge orgs`, then `gauge orgs use <slug> --project` to update gauge.json.":"Run `gauge orgs`, then rerun your command with --org <slug>. Or remove org from the run file to use your saved organization.";throw new CliError("usage",message,{hint})}async function resolveSelection(value,source){if(source!=="flag"&&!isUuid(value))legacySelection(source);return resolveOrgValue(value)}async function keyOrganization(){const{createChatApiClient}=await import("./chatApi-W6HW3HKD.js");const org=(await createChatApiClient().me()).apiKey?.organization;if(!org)throw new CliError("internal","Gauge could not identify which organization this API key belongs to.",{hint:"Check the key in Settings \u2192 API Keys and try again."});return org}async function createdOrganization(agentsSlug){const{orgs}=await createClient().get("/api/v1/me");const id=orgs.find(org=>org.slug===agentsSlug)?.canonicalGaugeOrganizationId;if(!id)throw new CliError("internal","Your workspace was created, but it is not ready to select in the CLI yet.",{hint:"Run `gauge orgs` to check for it, then `gauge orgs use <slug>`. If it is missing, contact Gauge support; you don't need to create it again."});return findOrganization(id)}async function missingAgentsOrg(org){const match=(await fetchDirectory()).find(o=>o.id===org);if(match?.products?.agents!==false)return void 0;return new CliError("not_found",`${match.name} doesn't use Gauge Agents.`,{hint:"Run `gauge orgs` to see which products each organization uses."})}function productsLabel(products){if(!products)return"unknown";return[products.chat?"Chat":void 0,products.agents?"Agents":void 0].filter(Boolean).join(", ")||"-"}export{isUuid,credentialKind,fetchDirectory,findOrganization,resolveOrgValue,legacySelection,resolveSelection,keyOrganization,createdOrganization,missingAgentsOrg,productsLabel};
@@ -0,0 +1,2 @@
1
+ import{createRequire}from"module";var CLI_VERSION=createRequire(import.meta.url)("../package.json").version;var USER_AGENT=`gauge-cli/${CLI_VERSION}`;import{mkdirSync,readFileSync,writeFileSync}from"fs";import{homedir}from"os";import{join}from"path";var CONFIG_KEYS=["orgId","defaultOrg","apiUrl","authUrl","backend","chatCategory","chatCategoryOrg"];function configDir(){const base=process.env.XDG_CONFIG_HOME||join(homedir(),".config");return join(base,"gauge")}function configPath(){return join(configDir(),"config.json")}function readConfig(){let raw;try{raw=readFileSync(configPath(),"utf8")}catch{return{}}try{const parsed=JSON.parse(raw);return typeof parsed==="object"&&parsed!==null?parsed:{}}catch{return{}}}function writeConfig(config){mkdirSync(configDir(),{recursive:true});writeFileSync(configPath(),`${JSON.stringify(config,null,2)}
2
+ `,"utf8")}function updateConfig(patch){const next={...readConfig(),...patch};for(const key of CONFIG_KEYS){if(next[key]===void 0)delete next[key]}writeConfig(next);return next}export{CLI_VERSION,USER_AGENT,CONFIG_KEYS,configDir,configPath,readConfig,updateConfig};
@@ -0,0 +1,2 @@
1
+ var SESSION_DIR="/gauge/session";var WORKSPACE_DIR=`${SESSION_DIR}/workspace`;var BASE_COMMIT_MARKER="GAUGE_BASE_COMMIT";var PI_AGENT_DIR="/tmp/pi-agent";function shellQuote(value){return`'${value.replace(/'/g,`'\\''`)}'`}function splitModel(model){const slash=model.indexOf("/");if(slash<1||slash===model.length-1)throw new Error(`model must be provider/model, got ${model}`);return{provider:model.slice(0,slash),model:model.slice(slash+1)}}function agentEnv(agent){switch(agent){case"claude-code":return{CLAUDE_CONFIG_DIR:`${SESSION_DIR}/claude`};case"codex":return{CODEX_HOME:`${SESSION_DIR}/codex`};case"pi":return{PI_CODING_AGENT_DIR:PI_AGENT_DIR};case"opencode":return{XDG_DATA_HOME:`${SESSION_DIR}/opencode/data`,XDG_STATE_HOME:`${SESSION_DIR}/opencode/state`,OPENCODE_CONFIG_CONTENT:JSON.stringify({autoupdate:false,permission:{"*":"allow",question:"ask"}})}}}function prepareScript(spec){const lines=["set -eu"];for(const resource of spec.resources){const src=`/gauge/inputs/${resource.name}`;const dest=`${SESSION_DIR}/resources/${resource.name}`;const hex=resource.digest.replace(/^sha256:/,"");lines.push(`echo ${shellQuote(`${hex} ${src}`)} | sha256sum -c --quiet -`,`mkdir -p ${shellQuote(dest)}`,`cp ${shellQuote(src)} ${shellQuote(`${dest}/${resource.filename}`)}`)}lines.push(`mkdir -p ${SESSION_DIR}/home ${SESSION_DIR}/claude ${SESSION_DIR}/codex ${SESSION_DIR}/opencode ${SESSION_DIR}/pi`);if(spec.workspace)lines.push(`git clone --quiet -- ${shellQuote(spec.workspace.repo)} ${WORKSPACE_DIR}`,`cd ${WORKSPACE_DIR}`,`ref=${shellQuote(spec.workspace.ref)}`,`git rev-parse --verify --quiet "$ref^{commit}" >/dev/null || ref="origin/$ref"`,`git checkout --quiet --detach "$ref"`);else lines.push(`git init --quiet ${WORKSPACE_DIR}`,`cd ${WORKSPACE_DIR}`,"git -c user.name=gauge -c user.email=gauge@localhost commit --quiet --allow-empty -m base");lines.push("gauge_base_commit=$(git rev-parse HEAD)");const skillRoot={"claude-code":`${SESSION_DIR}/claude/skills`,codex:`${SESSION_DIR}/codex/skills`,pi:`${PI_AGENT_DIR}/skills`,opencode:`${SESSION_DIR}/home/.config/opencode/skills`};for(const resource of spec.resources){if(resource.kind!=="archive")continue;const root=`${SESSION_DIR}/resources/${resource.name}`;const contents=`${root}/content`;lines.push(`mkdir -p ${shellQuote(contents)}`,`tar --extract --gzip --file ${shellQuote(`${root}/${resource.filename}`)} --directory ${shellQuote(contents)} --no-same-owner --no-same-permissions`);if(resource.use?.type==="skill"){const target=`${skillRoot[spec.agent.kind]}/${resource.name}`;lines.push(`test -f ${shellQuote(`${contents}/SKILL.md`)}`,`mkdir -p ${shellQuote(target)}`,`cp -R ${shellQuote(`${contents}/.`)} ${shellQuote(target)}`)}else if(resource.use?.type==="npm-package"){const args=resource.use.install==="environment"?`--global --prefix ${SESSION_DIR}/npm`:"";lines.push(`npm install --no-audit --no-fund ${args} ${shellQuote(`${contents}/${resource.use.package}`)} >&2`);if(resource.use.install==="environment")lines.push(`case ":$PATH:" in *:${SESSION_DIR}/npm/bin:*) ;; *) export PATH=${SESSION_DIR}/npm/bin:$PATH ;; esac`)}}lines.push(`echo "${BASE_COMMIT_MARKER} $gauge_base_commit"`);return lines.join("\n")}function captureScript(baseCommit){return["set -eu",`cd ${WORKSPACE_DIR}`,"git add --all --intent-to-add .",`git diff --binary ${shellQuote(baseCommit)}`].join("\n")}function harnessCommand(spec){switch(spec.agent.kind){case"claude-code":return["claude","-p","--input-format","stream-json","--output-format","stream-json","--verbose","--model",spec.agent.model,"--dangerously-skip-permissions","--permission-prompt-tool","stdio"];case"codex":return["codex","app-server","--listen","stdio://"];case"pi":{const m=splitModel(spec.agent.model);return["pi","--mode","rpc","--no-approve","--session-dir","/gauge/session/pi","--provider",m.provider,"--model",m.model]}case"opencode":return["opencode","serve","--hostname","127.0.0.1","--port","4096"]}}import{execFile}from"child_process";import{promisify}from"util";var exec=promisify(execFile);var DockerError=class extends Error{args;stderr;constructor(args,stderr){super(`docker ${args[0]} failed: ${stderr.trim()||"unknown error"}`);this.args=args;this.stderr=stderr}};function mountArg(mount){for(const path of[mount.source,mount.target])if(/[,"\n]/.test(path))throw new Error(`unsupported character in mount path: ${path}`);return`type=bind,source=${mount.source},target=${mount.target}${mount.readonly?",readonly":""}`}var isMissing=stderr=>/No such (container|object|volume)/i.test(stderr);var DockerCli=class{binary;constructor(binary="docker"){this.binary=binary}async run(args,includeStderr=false){try{const{stdout,stderr}=await exec(this.binary,args,{maxBuffer:16*1024*1024,timeout:3e4});return includeStderr?stdout+stderr:stdout}catch(error){const stderr=String(error.stderr??error);throw new DockerError(args,stderr)}}async create(spec){const args=["create","--pull","never","--name",spec.name,"--restart","no","--log-driver","json-file","--log-opt",spec.gateway?"max-size=8m":"max-size=1g"];if(spec.interactive)args.push("--interactive");if(spec.user)args.push("--user",spec.user);if(spec.network)args.push("--network",spec.network);if(spec.gateway)args.push("--cap-add","NET_ADMIN","--sysctl","net.ipv6.conf.all.disable_ipv6=1");if(spec.restricted)args.push("--cap-drop","ALL","--security-opt","no-new-privileges");for(const[key,value]of Object.entries(spec.labels))args.push("--label",`${key}=${value}`);for(const[key,value]of Object.entries(spec.env))args.push("--env",`${key}=${value}`);for(const mount of spec.mounts)args.push("--mount",mountArg(mount));args.push(spec.image,...spec.command);try{await this.run(args);return"created"}catch(error){if(error instanceof DockerError&&/Conflict|already in use/i.test(error.stderr))return"exists";throw error}}async assertSessionOwner(name,credentialDir){let inspection;try{inspection=await this.run(["inspect",name])}catch(error){if(error instanceof DockerError&&isMissing(error.stderr))return;throw error}const container=JSON.parse(inspection)[0];if(container?.Config?.Labels?.["sh.gauge.session"]!==name||!container?.Mounts?.some(mount=>mount.Source===`${credentialDir}/workspace`&&mount.Destination==="/gauge/session"))throw new Error("Refusing cleanup: container ownership does not match the Gauge session")}async stop(name,graceS){try{await this.run(["stop","--time",String(graceS),name])}catch(error){if(!(error instanceof DockerError&&isMissing(error.stderr)))throw error}}async start(name){await this.run(["start",name])}async copy(name,path,destination){await this.run(["cp",`${name}:${path}`,destination])}async running(name){return(await this.run(["inspect","--format","{{.State.Running}}",name])).trim()==="true"}async ready(name){const state=JSON.parse(await this.run(["inspect","--format","{{json .State}}",name]));return state.Running===true&&state.Health?.Status==="healthy"}async logs(name){return this.run(["logs","--tail","10000",name],true)}async assertCompanionOwner(name,session,role){let value;try{value=await this.run(["inspect",name])}catch(error){if(error instanceof DockerError&&isMissing(error.stderr))return;throw error}const labels=JSON.parse(value)[0]?.Config?.Labels;if(labels?.["sh.gauge.session"]!==session||labels?.["sh.gauge.role"]!==role)throw new Error("Refusing cleanup: companion container ownership does not match the Gauge session")}async remove(name){try{await this.run(["rm",name])}catch(error){if(!(error instanceof DockerError&&isMissing(error.stderr)))throw error}}};import{createHash}from"crypto";import{chmod,readFile,writeFile}from"fs/promises";import{join}from"path";import{setTimeout as delay}from"timers/promises";function validateGateway(gateway){const body=JSON.stringify(gateway.fixture);if(!gateway.image||Buffer.byteLength(body)>1e6||gateway.fixture.version!==1||gateway.fixture.searches.length)throw new Error("Invalid gateway fixture configuration");if(gateway.digest!==`sha256:${createHash("sha256").update(body).digest("hex")}`)throw new Error("Gateway fixture digest mismatch")}var CA_PATH="/etc/ssl/certs/ca-certificates.crt";var CA_VARIABLES=["SSL_CERT_FILE","REQUESTS_CA_BUNDLE","PIP_CERT","CURL_CA_BUNDLE","GIT_SSL_CAINFO","AWS_CA_BUNDLE","GRPC_DEFAULT_SSL_ROOTS_FILE_PATH","CARGO_HTTP_CAINFO","DENO_CERT"];async function prepareGateway(launch,canceled,docker=new DockerCli){const gateway=launch.spec.gateway;validateGateway(gateway);const check=()=>{if(canceled())throw new Error("Session canceled during gateway preparation")};const name=launch.gatewayName;const labels={"sh.gauge.session":launch.containerName};const fixturePath=join(launch.credentialDir,"fixture.json");await writeFile(fixturePath,JSON.stringify(gateway.fixture),{mode:420});check();if(await docker.create({name,image:gateway.image,gateway:true,labels:{...labels,"sh.gauge.role":"gateway"},mounts:[{type:"bind",source:fixturePath,target:"/gateway/fixture.json",readonly:true}],env:{},command:[]})!=="created")throw new Error("Gateway container already exists");check();await docker.start(name);const deadline=Date.now()+3e4;while(!await docker.ready(name)){check();if(!await docker.running(name)||Date.now()>=deadline)throw new Error("Gateway did not become ready; see gateway.log");await delay(200)}check();const cert=join(launch.credentialDir,"gateway.crt");await docker.copy(name,"/gateway/private/mitmproxy-ca-cert.pem",cert);const trust=launch.trustContainerName;if(await docker.create({name:trust,image:launch.spec.image,network:"none",labels:{...labels,"sh.gauge.role":"trust"},mounts:[],env:{},command:["true"]})!=="created")throw new Error("Trust source container already exists");check();const bundle=join(launch.credentialDir,"ca-bundle.pem");await docker.copy(trust,CA_PATH,bundle);const original=await readFile(bundle,"utf8");const publicCA=await readFile(cert,"utf8");if(!original.includes("BEGIN CERTIFICATE")||!publicCA.includes("BEGIN CERTIFICATE")||publicCA.includes("PRIVATE KEY"))throw new Error("Gateway requires a PEM system trust store and a public CA certificate");await writeFile(bundle,`${original}
2
+ ${publicCA}`);await chmod(bundle,420);await chmod(cert,420);await docker.remove(trust);check();const workload=launch.workload;workload.network=`container:${name}`;workload.restricted=true;workload.mounts.push({type:"bind",source:bundle,target:CA_PATH,readonly:true},{type:"bind",source:cert,target:"/gauge/certs/gateway.crt",readonly:true});workload.env.NODE_EXTRA_CA_CERTS="/gauge/certs/gateway.crt";for(const variable of CA_VARIABLES)workload.env[variable]=CA_PATH}async function cleanupGateway(launch,directory,docker=new DockerCli){if(!launch.gatewayName)return;await docker.assertCompanionOwner(launch.gatewayName,launch.containerName,"gateway");await docker.stop(launch.gatewayName,2);try{await writeFile(join(directory,"gateway.log"),await docker.logs(launch.gatewayName),{mode:384})}catch{}await docker.remove(launch.gatewayName);await docker.assertCompanionOwner(launch.trustContainerName,launch.containerName,"trust");await docker.remove(launch.trustContainerName)}import{cp,lstat,mkdtemp,rename,rm}from"fs/promises";import{join as join2}from"path";async function directoryExists(path){try{if(!(await lstat(path)).isDirectory())throw new Error(`Expected a directory: ${path}`);return true}catch(error){if(error.code==="ENOENT")return false;throw error}}async function preserveWorkspace(directory,runtime){const artifact="recovered-workspace";const destination=join2(directory,artifact);if(await directoryExists(destination))return artifact;const source=join2(runtime,"workspace","workspace");if(!await directoryExists(source))return void 0;try{await rename(source,destination)}catch(error){if(error.code!=="EXDEV")throw error;const staging=await mkdtemp(join2(directory,".workspace-"));try{await cp(source,join2(staging,"repo"),{recursive:true,dereference:false,verbatimSymlinks:true});await rename(join2(staging,"repo"),destination)}finally{await rm(staging,{recursive:true,force:true})}}return artifact}export{WORKSPACE_DIR,BASE_COMMIT_MARKER,PI_AGENT_DIR,shellQuote,splitModel,agentEnv,prepareScript,captureScript,harnessCommand,DockerCli,validateGateway,prepareGateway,cleanupGateway,preserveWorkspace};
@@ -0,0 +1,2 @@
1
+ import{USER_AGENT,configDir,readConfig}from"./chunk-OIGVDK3F.js";import{__export}from"./chunk-PSBF4TEK.js";import{chmodSync,mkdirSync,readFileSync,renameSync,rmSync,writeFileSync}from"fs";import{randomUUID}from"crypto";import{join}from"path";import{setTimeout as sleep}from"timers/promises";function credentialsPath(){return join(configDir(),"credentials")}function readCredentials(){try{const raw=readFileSync(credentialsPath(),"utf8").trim();if(!raw.startsWith("{"))return raw||null;const value=JSON.parse(raw);if(value.version!==1||typeof value.accessToken!=="string"||typeof value.authUrl!=="string"||typeof value.apiUrl!=="string")return null;return value}catch{return null}}function resolveToken(){const fromEnv=process.env.GAUGE_API_TOKEN?.trim();if(fromEnv)return fromEnv;const stored=readCredentials();return typeof stored==="string"?stored:stored?.accessToken??null}function atomicWrite(value){mkdirSync(configDir(),{recursive:true,mode:448});const temporary=`${credentialsPath()}.${randomUUID()}.tmp`;try{writeFileSync(temporary,`${value}
2
+ `,{encoding:"utf8",mode:384,flag:"wx"});chmodSync(temporary,384);renameSync(temporary,credentialsPath())}finally{rmSync(temporary,{force:true})}}function saveToken(token){atomicWrite(token.trim())}function saveCredentials(credentials){atomicWrite(JSON.stringify(credentials))}function clearToken(){rmSync(credentialsPath(),{force:true})}async function withCredentialLock(fn){mkdirSync(configDir(),{recursive:true,mode:448});const path=`${credentialsPath()}.lock`;const deadline=Date.now()+35e3;while(true){try{writeFileSync(path,String(process.pid),{flag:"wx",mode:384});break}catch(error){if(error.code!=="EEXIST")throw error;if(Date.now()>=deadline)throw new Error(`Timed out waiting for credential access. If no Gauge process is running, remove ${path} and retry.`);await sleep(50)}}try{return await fn()}finally{rmSync(path,{force:true})}}import{z}from"zod";var DEFAULT_AUTH_URL="https://app.withgauge.com";function resolveAuthUrl(){return(process.env.GAUGE_AUTH_URL||readConfig().authUrl||DEFAULT_AUTH_URL).replace(/\/+$/,"")}var sharedTokenSchema=z.object({accessToken:z.string().startsWith("gauge_cli_"),refreshToken:z.string().startsWith("gauge_refresh_"),tokenType:z.literal("bearer"),expiresAt:z.iso.datetime({offset:true}),refreshExpiresAt:z.iso.datetime({offset:true})});function storedSharedCredentials(){if(process.env.GAUGE_API_TOKEN?.trim())return null;const stored=readCredentials();return stored&&typeof stored!=="string"?stored:null}function assertTrustedOrigin(baseUrl,credentials){const origin=new URL(baseUrl).origin;if(![credentials.authUrl,credentials.apiUrl].some(value=>new URL(value).origin===origin))throw new Error("Your saved login is for a different Gauge server. Check GAUGE_AUTH_URL and GAUGE_API_URL, then run `gauge auth login` for the server you want to use.")}var RefreshError=class extends Error{constructor(message,unauthorized=false){super(message);this.unauthorized=unauthorized}unauthorized};function uncertainRefresh(){return new RefreshError("Gauge could not renew your login. Run `gauge auth login` to sign in again.",true)}async function refreshCredentials(previous,force=false){return withCredentialLock(async()=>{const current=readCredentials();if(!current||typeof current==="string")throw new Error("Your saved login changed. Run the command again.");if(current.sessionId!==previous.sessionId||current.authUrl!==previous.authUrl||current.apiUrl!==previous.apiUrl)throw new Error("Login changed while this command was running. Retry the command.");if(current.refreshPending)throw uncertainRefresh();if(current.accessToken!==previous.accessToken)return current;if(!current.refreshToken)return current;if(!force&&Date.parse(current.expiresAt??"")>Date.now()+6e4)return current;saveCredentials({...current,refreshPending:true});let response;try{response=await fetch(`${current.authUrl}/api/cli/refresh`,{method:"POST",redirect:"error",signal:AbortSignal.timeout(15e3),headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({refreshToken:current.refreshToken})})}catch{throw uncertainRefresh()}if(response.status===429){saveCredentials(current);throw new RefreshError("Gauge is receiving too many sign-in requests. Wait a moment, then try again.")}if(!response.ok)throw response.status===401?new RefreshError("Your login is no longer valid. Run `gauge auth login` to sign in again.",true):uncertainRefresh();try{const tokens=sharedTokenSchema.parse(await response.json());const next={...current,...tokens};delete next.refreshPending;const latest=readCredentials();if(!latest||typeof latest==="string"||latest.refreshToken!==current.refreshToken)throw new Error("Your saved login changed while Gauge was renewing it. Run the command again.");saveCredentials(next);return next}catch{throw uncertainRefresh()}})}var batteries_exports={};__export(batteries_exports,{BATTERY_AGENTS:()=>BATTERY_AGENTS,BATTERY_CADENCES:()=>BATTERY_CADENCES,BATTERY_MAX_LIMITS:()=>BATTERY_MAX_LIMITS,MAX_SAMPLES_PER_CYCLE:()=>MAX_SAMPLES_PER_CYCLE,batchDetailSchema:()=>batchDetailSchema,batchListQuerySchema:()=>batchListQuerySchema,batchListResponseSchema:()=>batchListResponseSchema,batchRunStatusSchema:()=>batchRunStatusSchema,batchRunSummarySchema:()=>batchRunSummarySchema,batchSummarySchema:()=>batchSummarySchema,batteryAgentSchema:()=>batteryAgentSchema,batteryApplyResponseSchema:()=>batteryApplyResponseSchema,batteryDefaultsSchema:()=>batteryDefaultsSchema,batteryLimitsSchema:()=>batteryLimitsSchema,batteryPlanPromptSchema:()=>batteryPlanPromptSchema,batteryPlanScheduleSchema:()=>batteryPlanScheduleSchema,batteryPlanSchema:()=>batteryPlanSchema,batteryPromptSpecSchema:()=>batteryPromptSpecSchema,batteryScheduleSpecSchema:()=>batteryScheduleSpecSchema,batterySkillRefSchema:()=>batterySkillRefSchema,batterySkillsSchema:()=>batterySkillsSchema,batterySpecSchema:()=>batterySpecSchema});import{z as z3}from"zod";import{z as z2}from"zod";var ERROR_CODES=["unauthorized","forbidden","not_found","invalid_request","insufficient_credits","needs_provider_key","rate_limited","conflict","internal","upgrade_required"];var errorCodeSchema=z2.enum(ERROR_CODES);var upgradeOfferSchema=z2.object({skuId:z2.string(),tierLabel:z2.string(),monthlyAmountCents:z2.number().int(),monthlyCredits:z2.number().int(),billingUrl:z2.string()});var errorEnvelopeSchema=z2.object({error:z2.object({code:errorCodeSchema,message:z2.string(),upgrade:upgradeOfferSchema.optional()})});var listQuerySchema=z2.object({limit:z2.coerce.number().int().min(1).max(200).default(50),cursor:z2.string().optional()});function listResponseSchema(item){return z2.object({items:z2.array(item),nextCursor:z2.string().optional()})}var createdBySchema=z2.object({userId:z2.string().nullable(),email:z2.string().nullable(),name:z2.string().nullable()}).nullable().optional();var BATTERY_AGENTS=["CLAUDE_CODE","CODEX_CLI","CURSOR","PI","OPENCODE","ANTIGRAVITY"];var BATTERY_CADENCES=["NONE"];var batteryAgentSchema=z3.enum(BATTERY_AGENTS);var BATTERY_MAX_LIMITS={maxTurns:30,wallClockS:5400,maxUsd:10};var MAX_SAMPLES_PER_CYCLE=50;var batteryLimitsSchema=z3.object({maxTurns:z3.number().positive().max(BATTERY_MAX_LIMITS.maxTurns).optional(),wallClockS:z3.number().positive().max(BATTERY_MAX_LIMITS.wallClockS).optional(),maxUsd:z3.number().positive().max(BATTERY_MAX_LIMITS.maxUsd).optional()});var batterySkillRefSchema=z3.string().regex(/^[A-Za-z0-9][A-Za-z0-9._-]*@\S+$/,'skills entries must be "name@label" (a bare name is a search, not a reference)');var batterySkillsSchema=z3.array(batterySkillRefSchema);var batteryScheduleSpecSchema=z3.object({repoUrl:z3.string().nullish(),repoRef:z3.string().nullish(),model:z3.string().nullable().optional(),agents:z3.array(batteryAgentSchema).optional(),samplesPerCycle:z3.number().int().min(1).max(MAX_SAMPLES_PER_CYCLE).optional(),cadence:z3.enum(BATTERY_CADENCES).optional(),experiment:z3.string().optional(),limits:batteryLimitsSchema.optional(),skills:batterySkillsSchema.optional()});var batteryDefaultsSchema=z3.object({isBranded:z3.boolean().optional(),topic:z3.string().optional(),tags:z3.array(z3.string()).optional(),model:z3.string().nullable().optional(),agents:z3.array(batteryAgentSchema).optional(),samplesPerCycle:z3.number().int().min(1).max(MAX_SAMPLES_PER_CYCLE).optional(),cadence:z3.enum(BATTERY_CADENCES).optional(),limits:batteryLimitsSchema.optional(),skills:batterySkillsSchema.optional()});var batteryPromptSpecSchema=z3.object({key:z3.string().min(1),text:z3.string().min(1),isBranded:z3.boolean().optional(),topic:z3.string().optional(),tags:z3.array(z3.string()).optional(),experiment:z3.string().optional(),schedules:z3.array(batteryScheduleSpecSchema).optional()});var batterySpecSchema=z3.object({version:z3.literal(1),org:z3.string().optional(),experiment:z3.string().optional(),expand:z3.boolean().optional(),defaults:batteryDefaultsSchema.optional(),prompts:z3.array(batteryPromptSpecSchema).min(1)});var batteryPlanScheduleSchema=z3.object({action:z3.enum(["create","update","none"]),repoUrl:z3.string().nullable(),repoRef:z3.string().nullable(),runs:z3.number().int()});var batteryPlanPromptSchema=z3.object({key:z3.string(),action:z3.enum(["create","update"]),schedules:z3.array(batteryPlanScheduleSchema)});var batteryPlanSchema=z3.object({dryRun:z3.literal(true),org:z3.string(),experiment:z3.string().nullable(),expand:z3.boolean(),prompts:z3.array(batteryPlanPromptSchema),summary:z3.object({promptsToCreate:z3.number().int(),promptsToUpdate:z3.number().int(),schedulesToCreate:z3.number().int(),schedulesToUpdate:z3.number().int(),runsToEnqueue:z3.number().int(),runsByAgent:z3.record(z3.string(),z3.number().int()),estimatedCredits:z3.number().int()})});var batteryApplyResponseSchema=z3.object({batchIds:z3.array(z3.string()),runIds:z3.array(z3.string()),experiment:z3.string().nullable(),blocked:z3.enum(["insufficient_credits","needs_provider_key"]).optional()});var batchRunStatusSchema=z3.enum(["QUEUED","CLAIMED","PROVISIONING","RUNNING","SUCCEEDED","FAILED","TIMED_OUT","CANCELED"]);var batchListQuerySchema=listQuerySchema.extend({promptId:z3.string().optional(),experiment:z3.string().optional()});var batchSummarySchema=z3.object({id:z3.string(),promptId:z3.string(),promptText:z3.string(),experiment:z3.string().nullable(),createdAt:z3.string(),runCounts:z3.record(z3.string(),z3.number().int())});var batchListResponseSchema=listResponseSchema(batchSummarySchema);var batchRunSummarySchema=z3.object({id:z3.string(),agent:z3.string(),model:z3.string(),status:batchRunStatusSchema,createdAt:z3.string(),startedAt:z3.string().nullable(),finishedAt:z3.string().nullable()});var batchDetailSchema=z3.object({id:z3.string(),promptId:z3.string(),promptText:z3.string(),scenario:z3.string(),repoUrl:z3.string().nullable(),repoRef:z3.string().nullable(),experiment:z3.string().nullable(),funding:z3.string(),createdAt:z3.string(),runCounts:z3.record(z3.string(),z3.number().int()),runs:z3.array(batchRunSummarySchema)});var brands_exports={};__export(brands_exports,{BRAND_KINDS:()=>BRAND_KINDS,BRAND_PACKAGE_ECOSYSTEMS:()=>BRAND_PACKAGE_ECOSYSTEMS,brandKindSchema:()=>brandKindSchema,brandListResponseSchema:()=>brandListResponseSchema,brandPackageEcosystemSchema:()=>brandPackageEcosystemSchema,brandPackageInputSchema:()=>brandPackageInputSchema,brandPackageSchema:()=>brandPackageSchema,brandSchema:()=>brandSchema,createBrandBodySchema:()=>createBrandBodySchema,listBrandsQuerySchema:()=>listBrandsQuerySchema,updateBrandBodySchema:()=>updateBrandBodySchema});import{z as z4}from"zod";var BRAND_KINDS=["OWNED","COMPETITOR","OTHER"];var brandKindSchema=z4.enum(BRAND_KINDS);var BRAND_PACKAGE_ECOSYSTEMS=["npm","pypi","cargo","go","rubygems","other"];var brandPackageEcosystemSchema=z4.enum(BRAND_PACKAGE_ECOSYSTEMS);var brandPackageSchema=z4.object({name:z4.string(),ecosystem:z4.string().nullable()});var brandSchema=z4.object({id:z4.string(),name:z4.string(),kind:brandKindSchema,source:z4.string(),aliases:z4.array(z4.string()),websites:z4.array(z4.string()),category:z4.string().nullable(),packages:z4.array(brandPackageSchema),createdAt:z4.string(),updatedAt:z4.string()});var brandPackageInputSchema=z4.object({name:z4.string().min(1),ecosystem:brandPackageEcosystemSchema.nullable().optional()});var createBrandBodySchema=z4.object({name:z4.string().min(1),kind:brandKindSchema,aliases:z4.array(z4.string().min(1)).default([]),websites:z4.array(z4.string().min(1)).default([]),packages:z4.array(brandPackageInputSchema).default([])});var updateBrandBodySchema=z4.object({name:z4.string().min(1).optional(),kind:brandKindSchema.optional(),aliases:z4.array(z4.string().min(1)).optional(),websites:z4.array(z4.string().min(1)).optional(),packages:z4.array(brandPackageInputSchema).optional()});var listBrandsQuerySchema=listQuerySchema.extend({kind:brandKindSchema.optional()});var brandListResponseSchema=listResponseSchema(brandSchema);var feedback_exports={};__export(feedback_exports,{FEEDBACK_KINDS:()=>FEEDBACK_KINDS,createFeedbackBodySchema:()=>createFeedbackBodySchema,createFeedbackResponseSchema:()=>createFeedbackResponseSchema,feedbackKindSchema:()=>feedbackKindSchema});import{z as z5}from"zod";var FEEDBACK_KINDS=["friction","missing","bug","other"];var feedbackKindSchema=z5.enum(FEEDBACK_KINDS);var createFeedbackBodySchema=z5.object({message:z5.string().trim().min(1).max(4e3),kind:feedbackKindSchema.default("other"),command:z5.string().trim().max(500).optional(),org:z5.string().optional()});var createFeedbackResponseSchema=z5.object({id:z5.string()});var keys_exports={};__export(keys_exports,{PROVIDER_TYPES:()=>PROVIDER_TYPES,providerKeyListResponseSchema:()=>providerKeyListResponseSchema,providerKeySchema:()=>providerKeySchema,providerTypeSchema:()=>providerTypeSchema,setProviderKeyBodySchema:()=>setProviderKeyBodySchema,setProviderKeyResponseSchema:()=>setProviderKeyResponseSchema});import{z as z6}from"zod";var PROVIDER_TYPES=["anthropic","openai"];var providerTypeSchema=z6.enum(PROVIDER_TYPES);var setProviderKeyBodySchema=z6.object({key:z6.string().min(1)});var setProviderKeyResponseSchema=z6.object({providerType:providerTypeSchema,last4:z6.string()});var providerKeySchema=z6.object({providerType:providerTypeSchema,last4:z6.string(),createdAt:z6.string(),updatedAt:z6.string()});var providerKeyListResponseSchema=listResponseSchema(providerKeySchema);var runConfig_exports={};__export(runConfig_exports,{ROSTERABLE_AGENTS:()=>ROSTERABLE_AGENTS,SCHEDULABLE_AGENTS:()=>SCHEDULABLE_AGENTS,agentConfigInputSchema:()=>agentConfigInputSchema,agentConfigSchema:()=>agentConfigSchema,assetRefListSchema:()=>assetRefListSchema,assetRefSchema:()=>assetRefSchema,modelUnits:()=>modelUnits,refAssetName:()=>refAssetName,rosterableAgentSchema:()=>rosterableAgentSchema,runsPerCycle:()=>runsPerCycle,schedulableAgentSchema:()=>schedulableAgentSchema,totalRunsPerCycle:()=>totalRunsPerCycle});import{z as z9}from"zod";import{z as z8}from"zod";var INTERVENTION_KINDS=["BASELINE","ADD_SKILL","REMOVE_SKILL","EDIT_SKILL","REWRITE_DOCS","PACKAGE_VERSION","PACKAGE_CONTENT","CLI_VERSION","CLI_INTERVENTION"];import{z as z7}from"zod";var ANALYSIS_SCHEMA_VERSION=1;var evidenceSequenceSchema=z7.number().int().nonnegative().describe("The event's recorded seq in this session attempt. Never infer it from an array index, line number, or turn.");var evidenceRefSchema=z7.discriminatedUnion("kind",[z7.object({kind:z7.literal("trace"),seq:evidenceSequenceSchema}).strict(),z7.object({kind:z7.literal("diff"),path:z7.string().min(1)}).strict()]);var INSTALL_PHASES=["NONE","ATTEMPTED","CONFIGURED","EXERCISED"];var sessionProductSchema=z7.object({name:z7.string(),mentioned:z7.boolean(),considered:z7.boolean(),recommended:z7.boolean(),chosen:z7.boolean(),rejected:z7.boolean(),abandoned:z7.boolean(),installPhase:z7.enum(INSTALL_PHASES),rank:z7.number().int().nullable(),source:z7.enum(["BRIEF","PERSONA","PRIOR","SEARCH","PAGE","REPO","UNKNOWN"]),reason:z7.string(),categories:z7.array(z7.string()),evidence:z7.record(z7.string(),z7.array(evidenceRefSchema))});var sessionObservationSchema=z7.object({id:z7.string(),kind:z7.enum(["FRICTION","OBJECTION","OPPORTUNITY"]),label:z7.string(),detail:z7.string(),subject:z7.string(),basis:z7.enum(["OBSERVED","AGENT_CLAIM","INFERRED"]),effect:z7.enum(["BLOCKED","RECOVERED","EXTRA_WORK","SELECTION_LOSS","UNKNOWN"]),cause:z7.enum(["PRODUCT","CONTENT","ACCESS","ENVIRONMENT","AGENT","UNKNOWN"]),tags:z7.array(z7.string()),evidence:z7.array(evidenceRefSchema)});var sessionAnalysisSchema=z7.object({schemaVersion:z7.literal(ANALYSIS_SCHEMA_VERSION),instructionsVersion:z7.string().min(1),id:z7.string(),runId:z7.string(),attempt:z7.number().int(),createdAt:z7.string(),origin:z7.enum(["MODEL","LEGACY"]),model:z7.string().nullable(),assessability:z7.enum(["ASSESSED","PARTIAL","UNASSESSABLE"]),limitation:z7.string().nullable(),selection:z7.enum(["MEASURED","UNASSESSABLE","NOT_MEASURED"]),directed:z7.boolean(),products:z7.array(sessionProductSchema),observations:z7.array(sessionObservationSchema)});var artifactPathSchema=z7.string().min(1).refine(value=>!value.startsWith("/")&&!value.includes("\\")&&!value.split("/").some(part=>part===".."||part===""),"Evidence paths must be relative paths inside the session artifacts");var portableEvidenceRefSchema=z7.discriminatedUnion("kind",[z7.object({kind:z7.literal("trace"),seq:evidenceSequenceSchema}).strict(),z7.object({kind:z7.literal("diff"),path:artifactPathSchema}).strict(),z7.object({kind:z7.literal("artifact"),artifact:artifactPathSchema.describe("Path relative to the session attempt directory, for example session/events.ndjson."),attempt:z7.number().int().positive().optional().describe("Must match the analysis attempt; defaults to that attempt."),line:z7.number().int().positive().optional().describe("One-based line number in the original artifact, before any filtering."),endLine:z7.number().int().positive().optional().describe("Inclusive last line of the evidence range."),seq:evidenceSequenceSchema.optional()}).strict().superRefine((ref,ctx)=>{if(ref.line===void 0&&ref.seq===void 0)ctx.addIssue({code:"custom",message:"Artifact evidence needs a line or seq"});if(ref.endLine!==void 0&&(ref.line===void 0||ref.endLine<ref.line))ctx.addIssue({code:"custom",path:["endLine"],message:"endLine requires line and must be at least line"})})]);var findingSubmissionSchema=sessionObservationSchema.omit({id:true}).extend({label:z7.string().trim().min(1).max(200),detail:z7.string().trim().min(1).max(1e4),subject:z7.string().trim().min(1).max(200),tags:z7.array(z7.string().trim().min(1).max(64)).max(32).default([]),evidence:z7.array(portableEvidenceRefSchema).min(1).max(100)}).strict();var criterionAnalysisSubmissionSchema=z7.object({criterionId:z7.string().min(1),outcome:z7.enum(["PASS","FAIL","UNASSESSABLE"]),label:z7.string().trim().min(1).max(1e4),evidence:z7.array(portableEvidenceRefSchema).max(100).default([])}).strict().superRefine((result,ctx)=>{if(result.outcome!=="UNASSESSABLE"&&result.evidence.length===0)ctx.addIssue({code:"custom",path:["evidence"],message:"A criterion verdict needs evidence"})});var productSubmissionSchema=sessionProductSchema.extend({name:z7.string().trim().min(1).max(200),evidence:z7.record(z7.string(),z7.array(portableEvidenceRefSchema))}).strict().superRefine((product,ctx)=>{if((product.chosen||product.rejected||product.recommended)&&!product.considered)ctx.addIssue({code:"custom",path:["considered"],message:"A chosen, rejected, or recommended product was considered"});if(product.considered&&!product.mentioned)ctx.addIssue({code:"custom",path:["mentioned"],message:"A considered product was mentioned"});if(!Object.values(product.evidence).some(refs=>refs.length>0))ctx.addIssue({code:"custom",path:["evidence"],message:"A product judgment needs evidence"})});var analysisSubmissionSchema=z7.object({schemaVersion:z7.literal(ANALYSIS_SCHEMA_VERSION),attempt:z7.number().int().positive().default(1),instructionsVersion:z7.string().trim().min(1),analyst:z7.object({name:z7.string().trim().min(1),model:z7.string().trim().min(1).optional()}).strict(),assessability:sessionAnalysisSchema.shape.assessability,limitation:z7.string().trim().min(1).nullable().default(null),selection:sessionAnalysisSchema.shape.selection.default("NOT_MEASURED"),directed:z7.boolean().default(false),findings:z7.array(findingSubmissionSchema).max(1e3).default([]),products:z7.array(productSubmissionSchema).max(1e3).default([]),criteria:z7.array(criterionAnalysisSubmissionSchema).max(1e3).default([])}).strict().superRefine((analysis,ctx)=>{for(const field of["products","criteria"]){const keys=field==="products"?analysis.products.map(product=>product.name.toLowerCase()):analysis.criteria.map(criterion=>criterion.criterionId);if(new Set(keys).size!==keys.length)ctx.addIssue({code:"custom",path:[field],message:`Duplicate ${field} are not allowed`})}if(analysis.assessability!=="ASSESSED"&&analysis.limitation===null)ctx.addIssue({code:"custom",path:["limitation"],message:"Partial or unassessable analysis needs a limitation"})});var RUN_STATUSES=["QUEUED","CLAIMED","PROVISIONING","RUNNING","SUCCEEDED","FAILED","TIMED_OUT","CANCELED"];var runStatusSchema=z8.enum(RUN_STATUSES);var AGENTS=["CLAUDE_CODE","CODEX_CLI","CURSOR","PI","OPENCODE","ANTIGRAVITY"];var agentSchema=z8.enum(AGENTS);var OBSERVED_PROVIDER_SOURCES=["AGENT_STREAM","GATEWAY_RESPONSE","GATEWAY_AUDIT","CANARY"];var observedProviderSourceSchema=z8.enum(OBSERVED_PROVIDER_SOURCES);var selectedProviderSchema=z8.object({slug:z8.string(),displayName:z8.string()});var observedProviderSchema=z8.object({slug:z8.string(),source:observedProviderSourceSchema});var RUN_TRIGGER_SOURCES=["UNKNOWN","UI","API","CADENCE","STAFF","OPERATOR","GITHUB"];var runTriggerSourceSchema=z8.enum(RUN_TRIGGER_SOURCES);var runTriggeredBySchema=z8.object({source:runTriggerSourceSchema,userId:z8.string().nullable(),email:z8.string().nullable()});var runOptimizationTrialSchema=z8.object({optimizationId:z8.string(),trialId:z8.string(),key:z8.string(),title:z8.string(),kind:z8.enum(INTERVENTION_KINDS)});var runCreditsChargedSchema=z8.number().int().nullable();var runStatusFilterSchema=z8.string().transform(raw=>raw.split(",").map(s=>s.trim().toUpperCase()).filter(Boolean)).pipe(z8.array(runStatusSchema).min(1));var runListQuerySchema=listQuerySchema.extend({status:runStatusFilterSchema.optional(),batch:z8.string().optional(),prompt:z8.string().optional(),experiment:z8.string().optional(),evalSet:z8.string().optional(),since:z8.string().optional()});var runListItemSchema=z8.object({id:z8.string(),status:runStatusSchema,agent:agentSchema,model:z8.string(),resolvedModel:z8.string().nullable().optional(),modelDisplayName:z8.string().nullable().optional(),selectedProvider:selectedProviderSchema.nullable().optional(),observedProvider:observedProviderSchema.nullable().optional(),promptId:z8.string(),promptSnippet:z8.string(),batchId:z8.string(),experiment:z8.string().nullable(),evalSetId:z8.string().nullable(),evalRevision:z8.string().optional(),optimizationTrial:runOptimizationTrialSchema.nullable().optional(),turns:z8.number().int().nullable(),usdCost:z8.number().nullable(),creditsCharged:runCreditsChargedSchema.optional(),triggeredBy:runTriggeredBySchema.optional(),durationMs:z8.number().int().nullable(),exitReason:z8.string().nullable(),createdAt:z8.string(),startedAt:z8.string().nullable(),finishedAt:z8.string().nullable(),parentRunId:z8.string().nullable(),fetchForkPlanId:z8.string().nullable()});var runListResponseSchema=listResponseSchema(runListItemSchema);var runSkillSchema=z8.discriminatedUnion("source",[z8.object({source:z8.literal("saved"),skillId:z8.string(),name:z8.string(),label:z8.string()}),z8.object({source:z8.literal("input"),name:z8.string(),commit:z8.string().nullable(),pullNumber:z8.number().int().nullable()})]);var runDetailSchema=runListItemSchema.omit({promptSnippet:true}).extend({promptText:z8.string(),attempt:z8.number().int(),cancelRequested:z8.boolean(),failureReason:z8.string().nullable(),repoUrl:z8.string().nullable(),repoRef:z8.string().nullable(),scenario:z8.string(),funding:z8.string(),usage:z8.unknown().nullable(),hasDiff:z8.boolean(),claimedAt:z8.string().nullable(),criterionResults:z8.array(z8.object({criterionId:z8.string().nullable(),name:z8.string(),passed:z8.boolean().nullable(),reasoning:z8.string().nullable()})),skills:z8.array(runSkillSchema).optional()});var cancelRunResponseSchema=z8.object({id:z8.string(),status:runStatusSchema,cancelRequested:z8.boolean(),note:z8.string().optional()});var regradeRunResponseSchema=z8.object({runId:z8.string(),evalSetId:z8.string().nullable(),criteria:z8.number().int()});var RUN_INSIGHT_STATUSES=["PENDING","RUNNING","COMPLETE","FAILED","SKIPPED"];var runInsightStatusSchema=z8.enum(RUN_INSIGHT_STATUSES);var insightPassSchema=z8.object({status:runInsightStatusSchema,model:z8.string().nullable(),inputTokens:z8.number().int().nullable(),outputTokens:z8.number().int().nullable(),usdCost:z8.number().nullable(),error:z8.string().nullable()});var runDecisionSchema=z8.object({index:z8.number().int(),category:z8.string(),categoryRaw:z8.string().nullable(),chosenName:z8.string(),chosenEcosystem:z8.string().nullable(),chosenPackage:z8.string().nullable(),reversed:z8.boolean(),detail:z8.unknown()});var RUN_INSIGHT_PASS_KINDS=["ANALYSIS","CLI_DISCOVERY","LEVER","EXPERIMENT_DELTA"];var runInsightPassKindSchema=z8.enum(RUN_INSIGHT_PASS_KINDS);var insightPipelinePassSchema=insightPassSchema.extend({kind:runInsightPassKindSchema});var runInsightResponseSchema=z8.object({runId:z8.string(),decision:insightPassSchema,passes:z8.array(insightPipelinePassSchema),decisions:z8.array(runDecisionSchema),experimentDelta:z8.unknown().nullable(),visibilityVerdict:z8.unknown().nullable(),analysis:sessionAnalysisSchema.nullable()});var runEventSchema=z8.looseObject({run_id:z8.string(),seq:z8.number(),turn:z8.number(),ts:z8.string(),type:z8.string()});var runCompareSideSchema=z8.object({runRequest:z8.string().min(1).optional(),batch:z8.array(z8.string().min(1)).min(1).max(100).optional(),evalSet:z8.string().min(1).optional(),evalRevision:z8.string().min(1).optional(),trial:z8.string().min(1).optional(),experiment:z8.string().min(1).optional(),agent:z8.array(agentSchema).min(1).optional(),model:z8.array(z8.string()).min(1).optional(),since:z8.string().optional(),until:z8.string().optional()}).strict().refine(side=>side.runRequest!==void 0||side.batch!==void 0||side.evalSet!==void 0||side.experiment!==void 0||side.trial!==void 0,"Select runs with runRequest, batch, evalSet, experiment, or trial");var runCompareBodySchema=z8.object({baseline:runCompareSideSchema,candidate:runCompareSideSchema}).strict();var runCompareStatsSchema=z8.object({runs:z8.number().int(),finished:z8.number().int(),succeeded:z8.number().int(),judged:z8.number().int(),passed:z8.number().int(),passRate:z8.number().nullable(),medianTurns:z8.number().nullable(),medianUsdCost:z8.number().nullable(),medianDurationMs:z8.number().nullable(),criteria:z8.array(z8.object({name:z8.string(),judged:z8.number().int(),passed:z8.number().int(),passRate:z8.number().nullable()}))});var runCompareDeltaSchema=z8.object({passRate:z8.number().nullable(),medianTurns:z8.number().nullable(),medianUsdCost:z8.number().nullable(),medianDurationMs:z8.number().nullable(),criteria:z8.array(z8.object({name:z8.string(),passRate:z8.number().nullable()}))});var runCompareRowSchema=z8.object({baseline:runCompareStatsSchema,candidate:runCompareStatsSchema,delta:runCompareDeltaSchema});var runCompareMixedRevisionSchema=z8.object({evalSetId:z8.string().nullable(),promptId:z8.string().nullable(),revisions:z8.array(z8.string())});var runCompareTrialSchema=runOptimizationTrialSchema.extend({runs:z8.number().int()});var runCompareResponseSchema=z8.object({overall:runCompareRowSchema,targets:z8.array(runCompareRowSchema.extend({agent:agentSchema,model:z8.string()})),sides:z8.object({baseline:z8.object({evalRevisions:z8.array(z8.string()),mixedRevisions:z8.array(runCompareMixedRevisionSchema).optional(),truncated:z8.boolean(),trials:z8.array(runCompareTrialSchema).optional()}),candidate:z8.object({evalRevisions:z8.array(z8.string()),mixedRevisions:z8.array(runCompareMixedRevisionSchema).optional(),truncated:z8.boolean(),trials:z8.array(runCompareTrialSchema).optional()})})});var SCHEDULABLE_AGENTS=["CLAUDE_CODE","CODEX_CLI","PI","OPENCODE"];var schedulableAgentSchema=z9.enum(SCHEDULABLE_AGENTS);var ROSTERABLE_AGENTS=[...SCHEDULABLE_AGENTS,"ANTIGRAVITY"];var rosterableAgentSchema=z9.enum(ROSTERABLE_AGENTS);var agentConfigSchema=z9.object({agent:agentSchema,models:z9.array(z9.string().min(1))});var agentConfigInputSchema=z9.object({agent:rosterableAgentSchema,models:z9.array(z9.string().min(1)).default([])});var assetRefSchema=z9.string().max(320).regex(/^[A-Za-z0-9][A-Za-z0-9._-]*@[^\s,]+$/,'asset references must be "name@label"');function refAssetName(ref){const at=ref.indexOf("@");return at<0?ref:ref.slice(0,at)}var assetRefListSchema=z9.array(assetRefSchema).superRefine((refs,ctx)=>{const names=new Set;for(const ref of refs){const name=refAssetName(ref);if(names.has(name)){ctx.addIssue({code:"custom",message:`"${ref}" names an asset already referenced \u2014 one version per asset`})}names.add(name)}});function modelUnits(agents){return agents.reduce((n,a)=>n+Math.max(1,a.models.length),0)}function runsPerCycle(owner,samplesPerCycle){return modelUnits(owner.agents)*samplesPerCycle}function totalRunsPerCycle(owners,samplesPerCycle){return owners.reduce((n,o)=>n+runsPerCycle(o,samplesPerCycle),0)}var DEFAULT_API_URL="https://agents.withgauge.com";function exitCodeFor(code){switch(code){case"usage":case"invalid_request":return 2;case"insufficient_credits":case"needs_provider_key":return 3;default:return 1}}var CliError=class extends Error{code;exitCode;hint;docs;needsAccount;apiCode;details;constructor(code,message,options={}){super(message);this.name="CliError";this.code=code;this.exitCode=exitCodeFor(code);this.hint=options.hint;this.docs=options.docs;this.needsAccount=options.needsAccount??false;this.apiCode=options.apiCode;this.details=options.details}};function resolveBaseUrl(){return process.env.GAUGE_API_URL||readConfig().apiUrl||DEFAULT_API_URL}var ApiClient=class{baseUrl;token;shared;authOptional;constructor(options={}){this.baseUrl=(options.baseUrl??resolveBaseUrl()).replace(/\/+$/,"");this.token=options.token!==void 0?options.token:resolveToken();this.authOptional=options.authOptional??false;this.shared=options.token===void 0?storedSharedCredentials():null;if(this.shared)assertTrustedOrigin(this.baseUrl,this.shared)}get signedIn(){return this.token!==null}async get(path,query,signal){return this.request("GET",path,{query,signal})}async post(path,body,query){return this.request("POST",path,{body,query})}async patch(path,body,query){return this.request("PATCH",path,{body,query})}async put(path,body,query){return this.request("PUT",path,{body,query})}async delete(path,query,body){return this.request("DELETE",path,{body,query})}async sse(path,onEvent,options={}){const res=await this.authenticatedFetch(this.url(path,options.query),{method:"GET",headers:this.headers({Accept:"text/event-stream"}),signal:options.signal});if(!res.ok)throw await this.toError(res);if(!res.body){throw new CliError("internal","Event stream had no response body")}const reader=res.body.getReader();const decoder=new TextDecoder;let buffer="";try{while(true){const{done,value}=await reader.read();if(done)break;buffer+=decoder.decode(value,{stream:true});let boundary=buffer.indexOf("\n\n");while(boundary!==-1){const raw=buffer.slice(0,boundary);const event=parseSseFrame(raw);buffer=buffer.slice(boundary+2);if(event)onEvent(event);else if(raw.trimStart().startsWith(":"))options.onComment?.();boundary=buffer.indexOf("\n\n")}}}catch(error){if(error instanceof Error&&error.name==="AbortError")return;throw new CliError("network",`Event stream failed: ${messageOf(error)}`)}}url(path,query){const url=new URL(`${this.baseUrl}${path}`);for(const[key,value]of Object.entries(query??{})){if(Array.isArray(value))for(const item of value)url.searchParams.append(key,item);else if(value!==void 0)url.searchParams.set(key,String(value))}return url}headers(extra){if(!this.token&&!this.authOptional){throw new CliError("unauthorized","Not logged in. Run `gauge auth login` or set GAUGE_API_TOKEN.",{needsAccount:true})}return{...this.token?{Authorization:`Bearer ${this.token}`}:{},"User-Agent":USER_AGENT,Accept:"application/json",...extra}}async authenticatedFetch(url,init){let renewalFailed=false;if(this.shared?.refreshToken){try{const next=await refreshCredentials(this.shared);assertTrustedOrigin(this.baseUrl,next);this.shared=next;this.token=this.shared.accessToken}catch(error){if(error instanceof RefreshError&&error.unauthorized)throw new CliError("unauthorized",messageOf(error));if(!(error instanceof RefreshError)||!(Date.parse(this.shared.expiresAt??"")>Date.now()))throw new CliError("network",messageOf(error));renewalFailed=true}}const send=()=>this.fetch(url,{...init,redirect:"error",headers:{...init.headers,...this.token?{Authorization:`Bearer ${this.token}`}:{}}});const response=await send();if(response.status!==401||!this.shared?.refreshToken||renewalFailed)return response;await response.body?.cancel();try{const next=await refreshCredentials(this.shared,true);assertTrustedOrigin(this.baseUrl,next);this.shared=next;this.token=this.shared.accessToken}catch(error){throw new CliError(error instanceof RefreshError&&error.unauthorized?"unauthorized":"network",messageOf(error))}return send()}async fetch(url,init){try{return await fetch(url,init)}catch(error){if(error instanceof Error&&error.name==="AbortError")throw error;throw new CliError("network",`Request to ${url.host} failed: ${messageOf(error)}`)}}async request(method,path,options={}){const headers=this.headers(options.body===void 0?void 0:{"Content-Type":"application/json"});const res=await this.authenticatedFetch(this.url(path,options.query),{method,headers,body:options.body===void 0?void 0:JSON.stringify(options.body),signal:options.signal});if(!res.ok)throw await this.toError(res);if(res.status===204)return void 0;const text=await res.text();if(!text)return void 0;const contentType=res.headers.get("content-type")??"";if(contentType.includes("application/json")){return JSON.parse(text)}return text}async toError(res){const wait=res.status===429?retryAfterSeconds(res):void 0;const hint=wait===void 0?void 0:`Try again in ${wait} s.`;try{const parsed=errorEnvelopeSchema.safeParse(await res.clone().json());if(parsed.success){const{code,message}=parsed.data.error;if(code==="not_found"&&message.startsWith("Organization not found")){const explained=await explainMissingOrg(res.url);if(explained)return explained}return new CliError(code,message,{hint})}}catch{}const what=res.status===429?"Rate limited by the Gauge API":res.status>=500?"Gauge API server error":"API request failed";return new CliError(codeForStatus(res.status),`${what}: HTTP ${res.status} ${res.statusText}`.trim(),{hint})}};async function explainMissingOrg(url){const match=url&&/\/api\/v1\/orgs\/([^/?#]+)/.exec(new URL(url).pathname);if(!match)return void 0;try{const{missingAgentsOrg}=await import("./orgDirectory-BZW2ZAD3.js");return await missingAgentsOrg(decodeURIComponent(match[1]))}catch{return void 0}}function createClient(options){return new ApiClient(options)}function retryAfterSeconds(res){const value=res.headers.get("retry-after");if(!value)return void 0;const seconds=Number(value);const at=Date.parse(value);if(Number.isFinite(seconds))return Math.max(0,Math.ceil(seconds));if(Number.isNaN(at))return void 0;return Math.max(0,Math.ceil((at-Date.now())/1e3))}function codeForStatus(status){switch(status){case 400:return"invalid_request";case 401:return"unauthorized";case 402:return"insufficient_credits";case 403:return"forbidden";case 404:return"not_found";case 409:return"conflict";case 429:return"rate_limited";default:return"internal"}}function parseSseFrame(frame){let eventName;const dataLines=[];for(const line of frame.split("\n")){if(line.startsWith(":"))continue;if(line.startsWith("event:")){eventName=line.slice("event:".length).trim()}else if(line.startsWith("data:")){dataLines.push(line.slice("data:".length).replace(/^ /,""))}}if(dataLines.length===0)return null;return{event:eventName,data:dataLines.join("\n")}}function messageOf(error){if(error instanceof Error){const cause=error.cause instanceof Error?`: ${error.cause.message}`:"";return`${error.message}${cause}`}return String(error)}export{errorEnvelopeSchema,listResponseSchema,createdBySchema,batteries_exports,brands_exports,feedback_exports,keys_exports,ANALYSIS_SCHEMA_VERSION,analysisSubmissionSchema,agentSchema,runTriggeredBySchema,runCreditsChargedSchema,runEventSchema,agentConfigSchema,agentConfigInputSchema,assetRefListSchema,runConfig_exports,credentialsPath,readCredentials,resolveToken,saveToken,saveCredentials,clearToken,withCredentialLock,resolveAuthUrl,sharedTokenSchema,storedSharedCredentials,exitCodeFor,CliError,resolveBaseUrl,ApiClient,createClient,retryAfterSeconds,codeForStatus};
@@ -0,0 +1,69 @@
1
+ import{DockerCli,PI_AGENT_DIR,agentEnv,cleanupGateway,harnessCommand,prepareScript,preserveWorkspace,shellQuote,splitModel,validateGateway}from"./chunk-QV7MOO3M.js";import{CliError}from"./chunk-RRLAI72I.js";import{configDir}from"./chunk-OIGVDK3F.js";import{spawn as spawn3}from"child_process";import{randomBytes as randomBytes2}from"crypto";import{copyFile,mkdir as mkdir4,writeFile as writeFile3,open as open3,readFile as readFile5,rename as rename2}from"fs/promises";import{existsSync as existsSync3}from"fs";import{join as join7}from"path";import{setTimeout as delay}from"timers/promises";import{spawn}from"child_process";import{randomUUID as randomUUID2}from"crypto";import{chmod,lstat,mkdir as mkdir2,mkdtemp,open as open2,readFile as readFile3,rm as rm2,writeFile}from"fs/promises";import{createConnection}from"net";import{tmpdir}from"os";import{basename,dirname as dirname2,isAbsolute,join as join2,resolve}from"path";import{fileURLToPath}from"url";import{execFile}from"child_process";import{readFile}from"fs/promises";import{promisify}from"util";import{join}from"path";var CredentialError=class extends Error{remedy;constructor(message,remedy){super(message);this.remedy=remedy}};var RENEWAL_MARGIN_S=300;async function readOptional(path){try{return await readFile(path,"utf8")}catch(error){if(error.code==="ENOENT")return void 0;throw error}}async function readClaudeKeychain(){if(process.platform!=="darwin")return void 0;try{const{stdout}=await promisify(execFile)("security",["find-generic-password","-s","Claude Code-credentials","-w"]);return stdout}catch{return void 0}}function jwtExpiry(token){const payload=token.split(".")[1];if(!payload)return void 0;try{const exp=JSON.parse(Buffer.from(payload,"base64url").toString("utf8")).exp;return typeof exp==="number"?new Date(exp*1e3):void 0}catch{return void 0}}function envFile(name,value,variable2){const target=`/run/gauge/${name}`;return{files:[{name,contents:value,target,readonly:true}],exports:[`export ${variable2}="$(cat ${target})"`]}}async function claudeCredential(spec,src,read){switch(spec.auth.mode){case"setup_token":{const token=src.env.CLAUDE_CODE_OAUTH_TOKEN?.trim();if(!token)throw new CredentialError("CLAUDE_CODE_OAUTH_TOKEN is not set.","Set CLAUDE_CODE_OAUTH_TOKEN, or use the Claude Code login on this machine.");return envFile("claude-token",token,"CLAUDE_CODE_OAUTH_TOKEN")}case"api_key":return providerApiKey("anthropic",src);case"access_token":{const{access,expiresAt}=await claudeLogin(spec,src,read);return{...envFile("claude-token",access,"CLAUDE_CODE_OAUTH_TOKEN"),expiresAt}}}}async function claudeLogin(spec,src,read){const configDir2=src.env.CLAUDE_CONFIG_DIR||join(src.homeDir,".claude");const raw=await(src.readKeychain??readClaudeKeychain)()??await read(join(configDir2,".credentials.json"));let login;try{login=raw?JSON.parse(raw).claudeAiOauth:void 0}catch{login=void 0}if(typeof login?.accessToken!=="string"||typeof login.expiresAt!=="number")throw new CredentialError("No Claude Code login found on this machine.","Run `claude` on this machine and log in, or set ANTHROPIC_API_KEY.");const expiresAt=new Date(login.expiresAt);const needed=spec.limits.turnTimeoutS+RENEWAL_MARGIN_S;if(expiresAt.getTime()-src.now.getTime()<needed*1e3)throw new CredentialError(`The Claude Code login expires at ${expiresAt.toISOString()}, too soon for a ${spec.limits.turnTimeoutS}s turn.`,"Run `claude -p ok` once on this machine to renew its login.");return{access:login.accessToken,expiresAt}}async function chatGptLogin(spec,src,read){const home=src.env.CODEX_HOME||join(src.homeDir,".codex");const raw=await read(join(home,"auth.json"));let auth;try{auth=raw?JSON.parse(raw):void 0}catch{throw new CredentialError("The native Codex login file is invalid.","Run `codex login` on this machine to replace it.")}const access=auth?.tokens?.access_token;if(!auth||auth.auth_mode!=="chatgpt"||!access)throw new CredentialError("No ChatGPT login found (Codex's ~/.codex/auth.json).","Run `codex login` on this machine, or select API-key auth.");const expiresAt=jwtExpiry(access);const needed=spec.limits.turnTimeoutS+RENEWAL_MARGIN_S;if(!expiresAt||expiresAt.getTime()-src.now.getTime()<needed*1e3)throw new CredentialError(`The ChatGPT access token ${expiresAt?`expires at ${expiresAt.toISOString()}`:"has no readable expiry"}, too soon for a ${spec.limits.turnTimeoutS}s turn.`,"Run `codex` once on this machine to renew its login.");return{auth,access,accountId:auth.tokens?.account_id,expiresAt}}var API_KEY_ENV={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",fireworks:"FIREWORKS_API_KEY",openrouter:"OPENROUTER_API_KEY"};function providerApiKey(provider,src){const variable2=API_KEY_ENV[provider];if(!variable2)throw new CredentialError(`API-key auth is not supported for provider ${provider}.`,`Use one of: ${Object.keys(API_KEY_ENV).join(", ")}.`);const key=src.env[variable2]?.trim();if(!key)throw new CredentialError(`API-key mode was selected but ${variable2} is not set.`,`Export ${variable2}, or select subscription auth.`);return envFile(variable2.toLowerCase().replace(/_/g,"-"),key,variable2)}function noClaudeSubscription(agent){throw new CredentialError(`Claude subscriptions are supported only in Claude Code, not ${agent}.`,"Use an Anthropic API key, or a ChatGPT login with an openai model.")}async function codexCredential(spec,src,read){switch(spec.auth.mode){case"access_token":{const{auth,expiresAt}=await chatGptLogin(spec,src,read);const stripped={...auth,OPENAI_API_KEY:null,tokens:{...auth.tokens,refresh_token:""}};return{files:[{name:"codex-auth.json",contents:JSON.stringify(stripped),target:"/run/gauge/codex-auth.json",readonly:true}],exports:["cp /run/gauge/codex-auth.json /gauge/session/codex/auth.json","chmod 600 /gauge/session/codex/auth.json"],expiresAt}}case"api_key":{const credential=providerApiKey("openai",src);return{...credential,exports:[...credential.exports,"codex login --with-api-key < /run/gauge/openai-api-key"]}}case"setup_token":throw new CredentialError("setup-token auth applies to Claude only.","Use the existing ChatGPT login or an API key for Codex.")}}async function openCodeCredential(spec,src,read){const{provider}=splitModel(spec.agent.model);switch(spec.auth.mode){case"access_token":{if(provider!=="openai")throw new CredentialError(`A ChatGPT login serves only openai/* models in OpenCode, not ${provider}.`,"Choose an openai/<model>, or select API-key auth.");const{access,accountId,expiresAt}=await chatGptLogin(spec,src,read);const content=JSON.stringify({openai:{type:"oauth",access,refresh:"",expires:expiresAt.getTime(),accountId}});return{...envFile("opencode-auth.json",content,"OPENCODE_AUTH_CONTENT"),expiresAt}}case"api_key":return providerApiKey(provider,src);case"setup_token":return noClaudeSubscription("OpenCode")}}async function piCredential(spec,src,read){const{provider}=splitModel(spec.agent.model);const agentDir=`mkdir -p ${PI_AGENT_DIR}`;switch(spec.auth.mode){case"access_token":{if(provider!=="openai-codex")throw new CredentialError(`A ChatGPT login serves only openai-codex/* models in Pi, not ${provider}.`,"Choose an openai-codex/<model>, or select API-key auth.");const{access,accountId,expiresAt}=await chatGptLogin(spec,src,read);const target="/run/gauge/pi-auth.json";return{files:[{name:"pi-auth.json",contents:JSON.stringify({"openai-codex":{type:"oauth",access,refresh:"",expires:expiresAt.getTime(),accountId}}),target,readonly:true}],exports:[agentDir,`cp ${target} ${PI_AGENT_DIR}/auth.json`,`chmod 600 ${PI_AGENT_DIR}/auth.json`],expiresAt}}case"api_key":{const credential=providerApiKey(provider,src);return{...credential,exports:[agentDir,...credential.exports]}}case"setup_token":return noClaudeSubscription("Pi")}}async function resolveSessionCredential(spec,src){const read=src.readFile??readOptional;switch(spec.agent.kind){case"claude-code":return claudeCredential(spec,src,read);case"codex":return codexCredential(spec,src,read);case"opencode":return openCodeCredential(spec,src,read);case"pi":return piCredential(spec,src,read)}}import{randomUUID}from"crypto";import{mkdir,open,readFile as readFile2,rename,rm}from"fs/promises";import{dirname}from"path";function tempPath(path){return`${path}.tmp-${process.pid}-${randomUUID()}`}async function writeSynced(path,data,mode){const handle=await open(path,"wx",mode);try{await handle.writeFile(data);await handle.sync()}finally{await handle.close()}}async function writeFileAtomic(path,data,mode=420){await mkdir(dirname(path),{recursive:true});const tmp=tempPath(path);try{await writeSynced(tmp,data,mode);await rename(tmp,path)}catch(error){await rm(tmp,{force:true});throw error}}function writeJsonAtomic(path,value){return writeFileAtomic(path,`${JSON.stringify(value,null,2)}
2
+ `)}async function readJson(path){try{return JSON.parse(await readFile2(path,"utf8"))}catch(error){if(error.code==="ENOENT")return void 0;throw error}}var terminal=state=>state==="completed"||state==="failed"||state==="canceled"||state==="interrupted";var SessionNotFoundError=class extends Error{constructor(){super("no supported session in this directory");this.name="SessionNotFoundError"}};var SessionUnavailableError=class extends Error{constructor(message,cause){super(message,{cause});this.name="SessionUnavailableError"}};var socketUnavailable=error=>["ENOENT","ECONNREFUSED"].includes(error?.code??"");function driverExited(status){const pid=status?.driverPid;if(typeof pid!=="number"||!Number.isSafeInteger(pid)||pid<=0)return false;try{process.kill(pid,0);return false}catch(error){return error.code==="ESRCH"}}function validateSpec(spec){if(spec.gateway){validateGateway(spec.gateway);if(process.getuid?.()===65532)throw new Error("Host UID 65532 is reserved for the local gateway")}if(!["claude-code","codex","pi","opencode"].includes(spec.agent.kind))throw new Error("unsupported agent");if(!spec.image||!spec.agent.model||!spec.prompt.text)throw new Error("image, model and prompt are required");if(spec.workspace){const url=new URL(spec.workspace.repo);if(url.protocol!=="https:"||url.hostname!=="github.com"||url.username||url.password||url.search||url.hash||!/^\/[\w.-]+\/[\w.-]+\/?$/.test(url.pathname))throw new Error("workspace must be a public GitHub HTTPS repository URL");if(!spec.workspace.ref||spec.workspace.ref.startsWith("-"))throw new Error("invalid repository ref")}for(const value of[spec.limits.turnTimeoutS,spec.limits.maxTurns])if(!Number.isSafeInteger(value)||value<1)throw new Error("limits must be positive integers");if(spec.limits.responseTimeoutS!==null&&(!Number.isSafeInteger(spec.limits.responseTimeoutS)||spec.limits.responseTimeoutS<1))throw new Error("response timeout must be null or a positive integer");const names=new Set;for(const r of spec.resources){if(!["file","archive"].includes(r.kind))throw new Error("unsupported resource kind");if(!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(r.name)||names.has(r.name))throw new Error("invalid or duplicate resource name");if(!/^[\w.-]+$/.test(r.filename)||r.filename==="."||r.filename==="..")throw new Error("invalid resource filename");if(!/^sha256:[a-f0-9]{64}$/.test(r.digest))throw new Error("invalid resource digest");if(r.use&&r.kind!=="archive")throw new Error("resource use requires an archive");if(r.use&&!["files","skill","npm-package"].includes(r.use.type))throw new Error("unsupported resource use");if(r.use?.type==="npm-package"&&(!/^[\w.-]+\.tgz$/.test(r.use.package)||!["environment","workspace"].includes(r.use.install)))throw new Error("invalid npm input");names.add(r.name)}}async function startSession(options){validateSpec(options.spec);const directory=resolve(options.directory);const credential=await resolveSessionCredential(options.spec,{...options.credentials,now:new Date});await mkdir2(dirname2(directory),{recursive:true});await mkdir2(directory,{mode:448});let credentialDir;let handedOff=false;const containerName=`gauge-session-${randomUUID2()}`;try{const runtime=options.runtimeDirectory??tmpdir();await mkdir2(runtime,{recursive:true});credentialDir=await mkdtemp(join2(runtime,"gauge-"));await chmod(credentialDir,448);await mkdir2(join2(credentialDir,"workspace"),{mode:448});const record={version:2,containerName,credentialDir,socketPath:join2(credentialDir,"control.sock"),spec:options.spec,credentialExpiresAt:credential.expiresAt?.toISOString(),...options.spec.gateway?{gatewayName:`${containerName}-gateway`,trustContainerName:`${containerName}-trust`}:{}};if(Buffer.byteLength(record.socketPath)>100)throw new Error("runtimeDirectory is too long for a Unix socket");await writeJsonAtomic(join2(directory,"session.json"),record);const mounts=[{type:"bind",source:join2(credentialDir,"workspace"),target:"/gauge/session",readonly:false}];for(const file of credential.files){const source=join2(credentialDir,file.name);await writeFile(source,file.contents,{mode:384});mounts.push({type:"bind",source,target:file.target,readonly:file.readonly})}for(const resource of options.spec.resources){if(!options.blobsDirectory)throw new Error("blobsDirectory is required for resources");mounts.push({type:"bind",source:resolve(options.blobsDirectory,resource.digest.slice(7)),target:`/gauge/inputs/${resource.name}`,readonly:true})}const script=[prepareScript(options.spec),...credential.exports,`exec ${harnessCommand(options.spec).map(shellQuote).join(" ")}`].join("\n");record.workload={name:containerName,image:options.spec.image,interactive:true,user:`${process.getuid()}:${process.getgid()}`,labels:{"sh.gauge.session":containerName},mounts,env:{...agentEnv(options.spec.agent.kind),HOME:"/gauge/session/home"},command:["sh","-c",script]};await writeJsonAtomic(join2(directory,"session.json"),record);const initial={state:"preparing",turn:0,updatedAt:new Date().toISOString(),cleanupPending:true};await writeJsonAtomic(join2(directory,"status.json"),{...initial,driverPid:process.pid});const log=await open2(join2(directory,"driver.log"),"a",384);try{const child=spawn(process.execPath,[fileURLToPath(new URL("./worker.js",import.meta.url)),directory],{detached:true,stdio:["ignore",log.fd,log.fd],env:Object.fromEntries(Object.entries(process.env).filter(([key])=>!/(?:API_KEY|AUTH_TOKEN|OAUTH_TOKEN|ACCESS_TOKEN|REFRESH_TOKEN)$/.test(key)))});const spawned=new Promise((yes,no)=>{child.once("spawn",yes);child.once("error",no)});await spawned;handedOff=true;child.unref();await writeJsonAtomic(join2(directory,"status.json"),{...initial,driverPid:child.pid});await writeFile(join2(credentialDir,"handoff"),"ready\n",{mode:384})}finally{await log.close()}const deadline=Date.now()+1e4;while(Date.now()<deadline){try{return await request(record.socketPath,{action:"status"})}catch{const saved=await readJson(join2(directory,"status.json"));if(saved&&terminal(saved.state))return saved;await new Promise(r=>setTimeout(r,50))}}throw new Error("host session process did not start; see driver.log")}catch(error){if(handedOff)throw error;if(credentialDir)await rm2(credentialDir,{recursive:true,force:true});await writeJsonAtomic(join2(directory,"status.json"),{state:"failed",turn:0,updatedAt:new Date().toISOString(),detail:String(error)});throw error}}async function request(socketPath,command2){return new Promise((resolve2,reject)=>{const socket=createConnection(socketPath);let bytes="";socket.setEncoding("utf8");socket.setTimeout(35e3,()=>socket.destroy(new Error("session command timed out; delivery may have succeeded")));socket.on("error",reject);socket.on("connect",()=>socket.write(JSON.stringify(command2)+"\n"));socket.on("data",chunk=>{bytes+=chunk;if(Buffer.byteLength(bytes)>8*1024*1024){socket.destroy(new Error("session response too large"));return}if(bytes.includes("\n")){try{resolve2(JSON.parse(bytes.slice(0,bytes.indexOf("\n"))))}catch(e){reject(e)}socket.end()}});socket.on("end",()=>{if(!bytes.includes("\n"))reject(new Error("session disconnected before acknowledging command"))})})}async function launchRecord(directory){const record=await readJson(join2(directory,"session.json"));if(!record)throw new SessionNotFoundError;if(record.version!==1&&record.version!==2)throw new Error("Unsupported session record version");return record}async function getSessionStatus(directory){const record=await launchRecord(directory);const saved=await readJson(join2(directory,"status.json"));if(saved&&terminal(saved.state))return saved;try{return await request(record.socketPath,{action:"status"})}catch(error){const latest=await readJson(join2(directory,"status.json"));if(latest&&terminal(latest.state))return latest;if(!socketUnavailable(error)||!driverExited(latest))throw new SessionUnavailableError("Session status is unavailable; the driver may still be running. Check status again before taking action.",error);return{...latest,state:"interrupted",turn:latest?.turn??0,updatedAt:new Date().toISOString(),detail:"Host session process is unavailable. Stop this session to clean up; prompts are never replayed automatically."}}}async function command(directory,value){const record=await launchRecord(directory);let receipt;try{receipt=await request(record.socketPath,value)}catch(error){throw new SessionUnavailableError("Session command was not acknowledged; delivery may have succeeded. Check status before retrying.",error)}if(!receipt.accepted)throw new Error(receipt.error??"session rejected command");return receipt}var finishSession=(directory,expectedTurn)=>command(directory,{action:"finish",turn:expectedTurn});async function stopSession(directory){const record=await launchRecord(directory);let saved=await readJson(join2(directory,"status.json"));if(saved&&terminal(saved.state)&&!saved.cleanupPending)return;let receipt;try{receipt=await request(record.socketPath,{action:"cancel"})}catch(error){saved=await readJson(join2(directory,"status.json"));if(saved&&terminal(saved.state)&&!saved.cleanupPending)return;if(!socketUnavailable(error)||!driverExited(saved))throw new SessionUnavailableError("Cancellation was not acknowledged; the driver may still be running. Check status before retrying.",error);await cleanupOrphan(directory,record);return}if(!receipt.accepted)throw new Error(receipt.error??"session rejected cancellation");const deadline=Date.now()+3e4;while(Date.now()<deadline){saved=await readJson(join2(directory,"status.json"));if(saved&&terminal(saved.state)){if(saved.cleanupPending)throw new Error(saved.detail??"Cleanup incomplete; call stopSession again after the driver exits");return}if(driverExited(saved)){await cleanupOrphan(directory,record);return}await new Promise(r=>setTimeout(r,100))}throw new SessionUnavailableError("Cancellation was accepted but cleanup is still pending. Check status; the driver retains ownership of cleanup.")}async function cleanupOrphan(directory,record){if(!/^gauge-session-[a-f0-9-]{36}$/.test(record.containerName)||!isAbsolute(record.credentialDir)||!/^gauge-[A-Za-z0-9]+$/.test(basename(record.credentialDir))||record.socketPath!==join2(record.credentialDir,"control.sock"))throw new Error("Refusing cleanup: invalid Gauge runtime ownership record");if(record.gatewayName&&(record.gatewayName!==`${record.containerName}-gateway`||record.trustContainerName!==`${record.containerName}-trust`))throw new Error("Refusing cleanup: invalid gateway ownership record");try{if(!(await lstat(record.credentialDir)).isDirectory())throw new Error("Refusing cleanup: Gauge runtime is not a directory")}catch(error){if(error.code!=="ENOENT")throw error}const engine=new DockerCli;try{await engine.assertSessionOwner(record.containerName,record.credentialDir);await engine.stop(record.containerName,2);const workspace=await preserveWorkspace(directory,record.credentialDir);await engine.remove(record.containerName);await cleanupGateway(record,directory,engine);await rm2(record.credentialDir,{recursive:true,force:true});const saved=await readJson(join2(directory,"status.json"));await writeJsonAtomic(join2(directory,"status.json"),{...saved,state:saved&&terminal(saved.state)?saved.state:"interrupted",turn:saved?.turn??0,updatedAt:new Date().toISOString(),question:void 0,cleanupPending:false,detail:saved&&terminal(saved.state)&&saved.state!=="interrupted"?saved.detail:"Interrupted session cleaned up explicitly",artifacts:workspace?{...saved?.artifacts,workspace}:saved?.artifacts})}catch(error){const saved=await readJson(join2(directory,"status.json"));await writeJsonAtomic(join2(directory,"status.json"),{...saved,state:saved&&terminal(saved.state)?saved.state:"interrupted",turn:saved?.turn??0,updatedAt:new Date().toISOString(),cleanupPending:true,detail:`Cleanup incomplete: ${String(error)}; retry stopSession after resolving the error`});throw error}}var LOCAL_MCP_HINT="Gauge exposes no local MCP configuration for registered servers. Hosted sessions can discover and call their tools; supplied files cannot test the live server. If hosted testing is acceptable, start with `gauge onboard`, select a workspace and MCP version, configure any required connection, then preview with --hosted. Login and static verification do not establish server reachability or access. Hosted sessions use credits; previews contact Gauge and may upload inputs. Do not issue hosted commands for a local-only request. See `gauge instructions hillclimb`.";function assertLocalCapabilities(config,prefix=""){for(const field of["profileId","marketId","skillRefs","mcpRefs","connectionIds"]){const value=config[field];if(Array.isArray(value)?value.length>0:Boolean(value))throw new CliError("usage",`${prefix}${field} requires hosted Gauge.`,{hint:field==="mcpRefs"?LOCAL_MCP_HINT:"Use --hosted for personas and hosted assets, or select local files and Skills through named inputs in gauge.json."})}if(config.addons&&Object.keys(config.addons).length>0)throw new CliError("usage",`${prefix}addons requires hosted Gauge.`,{hint:"Managed browser addons are unavailable locally. Use --hosted or remove the addon."})}import{existsSync as existsSync2,statSync as statSync2}from"fs";import{lstat as lstat2,readdir,readFile as readFile4,writeFile as writeFile2}from"fs/promises";import{join as join4}from"path";import{randomUUID as randomUUID3}from"crypto";import{chmodSync,existsSync,mkdirSync,readdirSync,readFileSync,statSync}from"fs";import{createRequire}from"module";import{join as join3}from"path";var LOCAL_DATABASE_VERSION=1;var schema=`
3
+ CREATE TABLE measurements (
4
+ id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK(kind IN ('eval','preference')),
5
+ revision INTEGER NOT NULL, definition TEXT NOT NULL,
6
+ created_at TEXT NOT NULL, updated_at TEXT NOT NULL, deleted_at TEXT
7
+ );
8
+ CREATE TABLE measurement_revisions (
9
+ measurement_id TEXT NOT NULL REFERENCES measurements(id), revision INTEGER NOT NULL,
10
+ definition TEXT NOT NULL, created_at TEXT NOT NULL,
11
+ PRIMARY KEY(measurement_id,revision)
12
+ );
13
+ CREATE TABLE launches (
14
+ id TEXT PRIMARY KEY, measurement_id TEXT, measurement_revision INTEGER,
15
+ configuration TEXT NOT NULL, provenance TEXT NOT NULL, created_at TEXT NOT NULL
16
+ );
17
+ CREATE TABLE sessions (
18
+ id TEXT PRIMARY KEY, batch_id TEXT NOT NULL, launch_id TEXT,
19
+ measurement_id TEXT, measurement_kind TEXT, measurement_revision INTEGER,
20
+ case_path TEXT, agent TEXT NOT NULL, model TEXT NOT NULL, attempt INTEGER NOT NULL,
21
+ outcome TEXT NOT NULL, ended_by TEXT, turns INTEGER, created_at TEXT NOT NULL,
22
+ ended_at TEXT, duration_ms REAL, input_tokens INTEGER, output_tokens INTEGER,
23
+ configuration TEXT NOT NULL, provenance TEXT NOT NULL, current_analysis_id TEXT
24
+ );
25
+ CREATE INDEX sessions_measurement ON sessions(measurement_id,created_at);
26
+ CREATE INDEX sessions_batch ON sessions(batch_id);
27
+ CREATE TABLE session_analyses (
28
+ id TEXT PRIMARY KEY, run_id TEXT NOT NULL REFERENCES sessions(id), attempt INTEGER NOT NULL,
29
+ revision INTEGER NOT NULL, schema_version INTEGER NOT NULL, instructions_version TEXT NOT NULL,
30
+ evidence_digest TEXT NOT NULL, payload TEXT NOT NULL, created_at TEXT NOT NULL,
31
+ UNIQUE(run_id,revision)
32
+ );
33
+ CREATE TABLE session_findings (
34
+ id TEXT PRIMARY KEY, analysis_id TEXT NOT NULL REFERENCES session_analyses(id),
35
+ kind TEXT NOT NULL, subject TEXT NOT NULL, label TEXT NOT NULL, detail TEXT NOT NULL,
36
+ basis TEXT NOT NULL, effect TEXT NOT NULL, cause TEXT NOT NULL, tags TEXT NOT NULL, evidence TEXT NOT NULL
37
+ );
38
+ CREATE INDEX findings_analysis ON session_findings(analysis_id);
39
+ CREATE TABLE session_products (
40
+ analysis_id TEXT NOT NULL REFERENCES session_analyses(id), position INTEGER NOT NULL,
41
+ name TEXT NOT NULL, payload TEXT NOT NULL, PRIMARY KEY(analysis_id,position)
42
+ );
43
+ CREATE TABLE criterion_results (
44
+ analysis_id TEXT NOT NULL REFERENCES session_analyses(id), criterion_id TEXT NOT NULL,
45
+ outcome TEXT NOT NULL, label TEXT NOT NULL, evidence TEXT NOT NULL,
46
+ PRIMARY KEY(analysis_id,criterion_id)
47
+ );
48
+ PRAGMA user_version = 1;
49
+ `;var json=value=>JSON.stringify(value);var object=value=>value!==null&&typeof value==="object"&&!Array.isArray(value)?value:{};var parse=value=>JSON.parse(String(value));var LocalStore=class{constructor(gaugeDir,db){this.gaugeDir=gaugeDir;this.db=db}gaugeDir;db;close(){this.db.close()}transaction(fn){this.db.exec("BEGIN IMMEDIATE");try{const value=fn();this.db.exec("COMMIT");return value}catch(error){this.db.exec("ROLLBACK");throw error}}createMeasurement(kind,definition){const normalized=normalizeDefinition(definition);const id=`${kind==="eval"?"ev":"pr"}_${randomUUID3()}`;const now=new Date().toISOString();this.transaction(()=>{this.db.prepare("INSERT INTO measurements VALUES (?,?,?,?,?,?,NULL)").run(id,kind,1,json(normalized),now,now);this.db.prepare("INSERT INTO measurement_revisions VALUES (?,?,?,?)").run(id,1,json(normalized),now)});return this.getMeasurement(id)}getMeasurement(id,kind){const row=this.db.prepare("SELECT * FROM measurements WHERE id = ? AND deleted_at IS NULL").get(id);return row&&(!kind||row.kind===kind)?measurement(row):void 0}requireMeasurement(id,kind){const value=this.getMeasurement(id,kind);if(!value)throw new CliError("usage",`Local ${kind??"measurement"} ${id} was not found in this workspace.`,{hint:"List saved measurements with gauge eval list --local or gauge preference list --local."});return value}listMeasurements(kind){const rows=kind?this.db.prepare("SELECT * FROM measurements WHERE kind = ? AND deleted_at IS NULL ORDER BY created_at DESC,id").all(kind):this.db.prepare("SELECT * FROM measurements WHERE deleted_at IS NULL ORDER BY created_at DESC,id").all();return rows.map(measurement)}getMeasurementRevision(id,revision){const row=this.db.prepare("SELECT definition FROM measurement_revisions WHERE measurement_id = ? AND revision = ?").get(id,revision);return row?parse(row.definition):void 0}updateMeasurement(id,patch){this.transaction(()=>{const previous=this.requireMeasurement(id);const original=this.getMeasurementRevision(id,previous.revision);const definition=normalizeDefinition({...original,...patch});const revision=previous.revision+1;const now=new Date().toISOString();this.db.prepare("INSERT INTO measurement_revisions VALUES (?,?,?,?)").run(id,revision,json(definition),now);this.db.prepare("UPDATE measurements SET definition = ?, revision = ?, updated_at = ? WHERE id = ?").run(json(definition),revision,now,id)});return this.requireMeasurement(id)}deleteMeasurement(id){this.requireMeasurement(id);this.db.prepare("UPDATE measurements SET deleted_at = ? WHERE id = ?").run(new Date().toISOString(),id)}recordLaunch(input){const value={...input,id:input.id??`l_${randomUUID3()}`,createdAt:input.createdAt??new Date().toISOString(),measurementId:input.measurementId??null,measurementRevision:input.measurementRevision??null};if(value.measurementId&&(value.measurementRevision===null||!this.getMeasurementRevision(value.measurementId,value.measurementRevision))){throw new CliError("usage","Launch must reference an existing measurement revision.")}this.db.prepare("INSERT INTO launches VALUES (?,?,?,?,?,?)").run(value.id,value.measurementId,value.measurementRevision,json(value.configuration),json(value.provenance),value.createdAt);return value}getLaunch(id){const row=this.db.prepare("SELECT * FROM launches WHERE id = ?").get(id);return row?{id:String(row.id),measurementId:row.measurement_id,measurementRevision:row.measurement_revision,configuration:parse(row.configuration),provenance:parse(row.provenance),createdAt:String(row.created_at)}:void 0}upsertSession(input){if(!Number.isInteger(input.attempt)||input.attempt<1)throw new CliError("usage","Session attempt must be a positive integer.");this.db.prepare(`INSERT INTO sessions (
50
+ id,batch_id,launch_id,measurement_id,measurement_kind,measurement_revision,case_path,agent,model,attempt,outcome,ended_by,turns,created_at,ended_at,duration_ms,input_tokens,output_tokens,configuration,provenance
51
+ ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
52
+ ON CONFLICT(id) DO UPDATE SET
53
+ batch_id=excluded.batch_id,launch_id=COALESCE(excluded.launch_id,sessions.launch_id),
54
+ measurement_id=COALESCE(excluded.measurement_id,sessions.measurement_id),
55
+ measurement_kind=COALESCE(excluded.measurement_kind,sessions.measurement_kind),
56
+ measurement_revision=COALESCE(excluded.measurement_revision,sessions.measurement_revision),
57
+ case_path=excluded.case_path,agent=excluded.agent,model=excluded.model,
58
+ current_analysis_id=CASE WHEN excluded.attempt=sessions.attempt THEN sessions.current_analysis_id ELSE NULL END,
59
+ attempt=excluded.attempt,outcome=excluded.outcome,ended_by=excluded.ended_by,turns=excluded.turns,
60
+ ended_at=excluded.ended_at,duration_ms=COALESCE(excluded.duration_ms,sessions.duration_ms),
61
+ input_tokens=COALESCE(excluded.input_tokens,sessions.input_tokens),output_tokens=COALESCE(excluded.output_tokens,sessions.output_tokens),
62
+ configuration=CASE WHEN excluded.configuration='{}' THEN sessions.configuration ELSE excluded.configuration END,
63
+ provenance=CASE WHEN excluded.provenance='{}' THEN sessions.provenance ELSE excluded.provenance END
64
+ WHERE excluded.attempt >= sessions.attempt`).run(input.id,input.batchId,input.launchId??null,input.measurementId??null,input.measurementKind??null,input.measurementRevision??null,input.case,input.agent,input.model,input.attempt,input.outcome,input.endedBy,input.turns,input.createdAt,input.endedAt,input.durationMs??null,input.inputTokens??null,input.outputTokens??null,json(input.configuration??{}),json(input.provenance??{}));return this.getSession(input.id)}getSession(id){const row=this.db.prepare("SELECT * FROM sessions WHERE id = ?").get(id);return row?session(row):void 0}listSessions(filter={}){const conditions=[];const values=[];for(const[key,column]of[["measurementId","measurement_id"],["measurementKind","measurement_kind"],["batchId","batch_id"]]){if(filter[key]!==void 0){conditions.push(`${column} = ?`);values.push(filter[key])}}return this.db.prepare(`SELECT * FROM sessions${conditions.length?` WHERE ${conditions.join(" AND ")}`:""} ORDER BY COALESCE(ended_at,created_at) DESC,id`).all(...values).map(session)}repairSessionSummaries(ids){const runs=join3(this.gaugeDir,"runs");if(!existsSync(runs))return{repaired:0,skipped:[]};let repaired=0;const skipped=[];for(const entry of readdirSync(runs,{withFileTypes:true})){if(!entry.isDirectory()||ids&&!ids.includes(entry.name))continue;const attempts=join3(runs,entry.name,"attempts");if(!existsSync(attempts))continue;const n=readdirSync(attempts).map(Number).filter(n2=>Number.isInteger(n2)&&n2>0).sort((a,b)=>b-a)[0];if(n===void 0)continue;try{const directory=join3(attempts,String(n));const spec=object(readJson2(join3(directory,"spec.json")));const attempt=object(spec.attempt);const agent=object(spec.agent);if(attempt.runId!==entry.name||attempt.n!==n||typeof attempt.batchId!=="string"||typeof agent.kind!=="string"||typeof agent.model!=="string")throw new Error("invalid spec.json");const result=object(readJson2(join3(directory,"result.json")));const status=object(readJson2(join3(directory,"session","status.json")));const item=object(spec.case);const association=object(spec.measurement);const previous=this.getSession(entry.name);const createdAt=typeof spec.createdAt==="string"?spec.createdAt:previous?.createdAt??statSync(join3(directory,"spec.json")).mtime.toISOString();this.upsertSession({id:entry.name,batchId:attempt.batchId,launchId:typeof spec.launchId==="string"?spec.launchId:null,measurementId:typeof association.id==="string"?association.id:null,measurementKind:association.kind==="eval"||association.kind==="preference"?association.kind:null,measurementRevision:typeof association.revision==="number"?association.revision:null,case:typeof item.path==="string"?item.path:null,agent:typeof spec.agentName==="string"?spec.agentName:agent.kind,model:agent.model,attempt:n,outcome:String(result.outcome??status.state??"queued"),endedBy:typeof result.endedBy==="string"?result.endedBy:null,turns:typeof result.turns==="number"?result.turns:typeof status.turn==="number"?status.turn:null,createdAt,endedAt:typeof result.endedAt==="string"?result.endedAt:null,durationMs:numberOrNull(result.durationMs),inputTokens:numberOrNull(result.inputTokens),outputTokens:numberOrNull(result.outputTokens),configuration:{...object(spec.configuration),case:item},provenance:object(spec.provenance)});repaired++}catch(error){skipped.push(`${entry.name}: ${error instanceof Error?error.message:String(error)}`)}}return{repaired,skipped}}};function numberOrNull(value){return typeof value==="number"&&Number.isFinite(value)?value:null}function readJson2(path){try{return JSON.parse(readFileSync(path,"utf8"))}catch(error){if(error.code==="ENOENT")return void 0;throw error}}function normalizeDefinition(definition){if(!definition.name?.trim()||!definition.promptText?.trim())throw new CliError("usage","A local measurement requires a nonempty name and promptText.");if(definition.tags&&(!Array.isArray(definition.tags)||definition.tags.some(tag=>typeof tag!=="string"||!tag.trim())))throw new CliError("usage","Measurement tags must be nonempty strings.");for(const key of["id","measurementKind","revision","createdAt","updatedAt"]){if(key in definition)throw new CliError("usage",`Measurement field ${key} is assigned by Gauge.`)}return{...definition,tags:[...new Set(definition.tags??[])]}}function measurement(row){return{...parse(row.definition),id:String(row.id),measurementKind:row.kind,revision:Number(row.revision),createdAt:String(row.created_at),updatedAt:String(row.updated_at)}}function session(row){return{id:String(row.id),batchId:String(row.batch_id),launchId:row.launch_id,measurementId:row.measurement_id,measurementKind:row.measurement_kind,measurementRevision:row.measurement_revision,case:row.case_path,agent:String(row.agent),model:String(row.model),attempt:Number(row.attempt),outcome:String(row.outcome),endedBy:row.ended_by,turns:row.turns,createdAt:String(row.created_at),endedAt:row.ended_at,durationMs:row.duration_ms,inputTokens:row.input_tokens,outputTokens:row.output_tokens,configuration:parse(row.configuration),provenance:parse(row.provenance),currentAnalysisId:row.current_analysis_id,analyzed:row.current_analysis_id!==null}}function openLocalStore(gaugeDir){mkdirSync(gaugeDir,{recursive:true,mode:448});const path=join3(gaugeDir,"gauge.sqlite");const{DatabaseSync:SQLite}=loadSQLite();const db=new SQLite(path);try{chmodSync(path,384);db.exec("PRAGMA busy_timeout = 5000; PRAGMA foreign_keys = ON;");const version=Number(db.prepare("PRAGMA user_version").get().user_version);if(version>LOCAL_DATABASE_VERSION)throw new CliError("usage",`This workspace uses database version ${version}; this CLI supports ${LOCAL_DATABASE_VERSION}. Upgrade Gauge. Your data has not been changed.`);db.exec("PRAGMA journal_mode = WAL;");if(version===0){db.exec("BEGIN IMMEDIATE");try{if(Number(db.prepare("PRAGMA user_version").get().user_version)===0)db.exec(schema);db.exec("COMMIT")}catch(error){db.exec("ROLLBACK");throw error}}return new LocalStore(gaugeDir,db)}catch(error){db.close();throw error}}function loadSQLite(){const emitWarning=process.emitWarning;process.emitWarning=(warning,...args)=>{if(warning==="SQLite is an experimental feature and might change at any time"&&args[0]==="ExperimentalWarning")return;Reflect.apply(emitWarning,process,[warning,...args])};try{return createRequire(import.meta.url)("node:sqlite")}finally{process.emitWarning=emitWarning}}function localId(id){if(!/^[a-zA-Z0-9_-]+$/.test(id))throw new CliError("usage","Invalid local ID");return id}var batchDirectory=(gaugeDir,id)=>join4(gaugeDir,"batches",localId(id));var cancellationRequested=directory=>existsSync2(join4(directory,"cancel"));async function directories(path){try{if(!(await lstat2(path)).isDirectory())return[];return(await readdir(path,{withFileTypes:true})).filter(d=>d.isDirectory()).map(d=>d.name)}catch(error){if(error.code==="ENOENT")return[];throw error}}async function localSessions(gaugeDir){const sessions=[];for(const id of await directories(join4(gaugeDir,"runs"))){const attempts=join4(gaugeDir,"runs",id,"attempts");for(const n of await directories(attempts)){const attempt=join4(attempts,n);try{if(!(await lstat2(join4(attempt,"spec.json"))).isFile())continue;const spec=JSON.parse(await readFile4(join4(attempt,"spec.json"),"utf8"));if(spec.attempt?.runId!==id||String(spec.attempt.n)!==n||typeof spec.attempt.batchId!=="string")continue;const directory=join4(attempt,"session");if(existsSync2(directory)&&!(await lstat2(directory)).isDirectory())continue;sessions.push({id,batchId:spec.attempt.batchId,directory})}catch(error){if(!(error instanceof SyntaxError)&&error.code!=="ENOENT")throw error}}}return sessions}async function resultDetail(directory){try{const result=JSON.parse(await readFile4(join4(directory,"..","result.json"),"utf8"));return typeof result.detail==="string"?result.detail:void 0}catch{return}}function stderrLog(directory,state){if(state!=="failed")return;const path=join4(directory,"agent.stderr.log");try{return statSync2(path).size?path:void 0}catch{return}}async function localStatus(gaugeDir,filter={}){const sessions=(await localSessions(gaugeDir)).filter(s=>(!filter.id||s.id===filter.id)&&(!filter.batch||s.batchId===filter.batch));return Promise.all(sessions.map(async session2=>{try{const status=await getSessionStatus(session2.directory);return{id:session2.id,batchId:session2.batchId,state:status.state,turns:status.turn,cleanupPending:status.cleanupPending??false,detail:status.detail??await resultDetail(session2.directory),stderrLog:stderrLog(session2.directory,status.state)}}catch(error){if(!existsSync2(join4(session2.directory,"session.json"))){try{const result=JSON.parse(await readFile4(join4(session2.directory,"..","result.json"),"utf8"));return{id:session2.id,batchId:session2.batchId,state:result.outcome,turns:result.turns,detail:result.detail,stderrLog:stderrLog(session2.directory,result.outcome)}}catch{}}return{id:session2.id,batchId:session2.batchId,state:"unavailable",detail:String(error),stderrLog:void 0}}}))}async function cancelLocal(gaugeDir,filter){if(Boolean(filter.id)===Boolean(filter.batch))throw new CliError("usage","Pass a run ID or --batch <batch-id>");if(filter.id)localId(filter.id);if(filter.batch)localId(filter.batch);const sessions=(await localSessions(gaugeDir)).filter(s=>filter.id?s.id===filter.id:s.batchId===filter.batch);if(filter.batch){const directory=batchDirectory(gaugeDir,filter.batch);if(existsSync2(directory)){if(!(await lstat2(directory)).isDirectory())throw new CliError("usage","Cannot cancel this batch because its folder is a symbolic link.",{hint:"Run the command from the original Gauge workspace."});await writeFile2(join4(directory,"cancel"),"",{mode:384})}else if(!sessions.length)throw new CliError("not_found","Local batch not found")}else if(!sessions.length)throw new CliError("not_found","Local run not found");for(const session2 of sessions)await writeFile2(join4(session2.directory,"..","cancel"),"",{mode:384});return Promise.all(sessions.map(async session2=>{try{await stopSession(session2.directory);return{id:session2.id,status:"stopped"}}catch(error){return{id:session2.id,status:"pending",detail:String(error)}}}))}async function cleanupLocal(gaugeDir){const results=[];for(const session2 of await localSessions(gaugeDir)){if(!existsSync2(join4(session2.directory,"session.json")))continue;try{const status=await getSessionStatus(session2.directory);if(status.state!=="interrupted"&&!status.cleanupPending)continue;await stopSession(session2.directory);const final=await getSessionStatus(session2.directory);try{await writeFile2(join4(session2.directory,"..","result.json"),JSON.stringify({outcome:final.state,endedBy:"recovery",turns:final.turn,endedAt:final.updatedAt,detail:final.detail})+"\n",{flag:"wx",mode:384})}catch(error){if(error.code!=="EEXIST")throw error}const store=openLocalStore(gaugeDir);try{const{skipped}=store.repairSessionSummaries([session2.id]);if(skipped.length)throw new Error(skipped.join("; "))}finally{store.close()}results.push({id:session2.id,status:"cleaned"})}catch(error){results.push({id:session2.id,status:"error",detail:String(error)})}}return results}async function localBatchStatus(gaugeDir,id){const directory=batchDirectory(gaugeDir,id);const status=JSON.parse(await readFile4(join4(directory,"status.json"),"utf8"));if(status.state==="running"&&Number.isSafeInteger(status.supervisorPid)&&status.supervisorPid>0){try{process.kill(status.supervisorPid,0)}catch(error){if(error.code==="ESRCH")status.state="interrupted"}}return{...status,cancelRequested:cancellationRequested(directory)}}import{fileURLToPath as fileURLToPath2}from"url";import{mkdirSync as mkdirSync2,readFileSync as readFileSync2,renameSync,writeFileSync}from"fs";import{randomBytes}from"crypto";import{homedir}from"os";import{join as join5}from"path";import{z}from"zod";var LOCAL_TARGETS={CLAUDE_CODE:{kind:"claude-code",model:"claude-sonnet-5-5"},CODEX_CLI:{kind:"codex",model:"gpt-6-astra"},PI:{kind:"pi",model:"openai-codex/gpt-6-astra"},OPENCODE:{kind:"opencode",model:"openai/gpt-6-astra"}};var agents=["CLAUDE_CODE","CODEX_CLI","PI","OPENCODE"];var variable=z.string().regex(/^[A-Za-z_][A-Za-z0-9_]*$/);var profileSchema=z.object({agent:z.enum(agents),provider:z.enum(["anthropic","openai","openai-codex","fireworks","openrouter"]),auth:z.enum(["api_key","access_token","setup_token"]),env:variable.optional()}).strict();var storeSchema=z.object({version:z.literal(1),profiles:z.record(z.string(),profileSchema),defaults:z.record(z.string(),z.string())}).strict();var API_KEY_ENV2={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",fireworks:"FIREWORKS_API_KEY",openrouter:"OPENROUTER_API_KEY"};var profilePath=()=>join5(configDir(),"local-profiles.json");function readProfiles(){let raw;try{raw=readFileSync2(profilePath(),"utf8")}catch(error){if(error.code==="ENOENT")return{version:1,profiles:{},defaults:{}};throw error}try{const parsed=storeSchema.safeParse(JSON.parse(raw));if(parsed.success)return parsed.data}catch{}throw new CliError("usage","Invalid local credential profile file.",{hint:`Repair ${profilePath()}.`})}function writeProfiles(store){mkdirSync2(configDir(),{recursive:true,mode:448});const temporary=`${profilePath()}.${randomBytes(6).toString("hex")}.tmp`;writeFileSync(temporary,`${JSON.stringify(storeSchema.parse(store),null,2)}
65
+ `,{mode:384});renameSync(temporary,profilePath())}function validateProfile(value){const parsed=profileSchema.safeParse(value);if(!parsed.success)throw new CliError("usage","Invalid profile. Supply a supported --agent, --provider and --auth, and an environment variable name with --env.");const p=parsed.data;const expectedProvider=p.agent==="CLAUDE_CODE"?"anthropic":p.agent==="CODEX_CLI"?"openai":void 0;if(expectedProvider&&p.provider!==expectedProvider)throw new CliError("usage",`${p.agent} requires provider ${expectedProvider}.`);if(p.auth==="access_token"){const provider=p.agent==="CLAUDE_CODE"?"anthropic":p.agent==="PI"?"openai-codex":"openai";if(p.provider!==provider||p.env)throw new CliError("usage",`Native login for ${p.agent} requires provider ${provider} and no --env.`)}else if(p.auth==="setup_token"){if(p.agent!=="CLAUDE_CODE")throw new CliError("usage","Setup tokens are supported only for CLAUDE_CODE.");p.env??="CLAUDE_CODE_OAUTH_TOKEN"}else{if(!API_KEY_ENV2[p.provider])throw new CliError("usage",`API-key auth is unsupported for ${p.provider}.`);p.env??=API_KEY_ENV2[p.provider]}return p}function setProfile(name,value,replace=false){if(!/^[a-zA-Z0-9][a-zA-Z0-9_.-]{0,63}$/.test(name)||["__proto__","constructor","prototype"].includes(name))throw new CliError("usage","Profile names must be 1\u201364 letters, digits, dots, underscores or hyphens.");const p=validateProfile(value);const store=readProfiles();const previous=Object.hasOwn(store.profiles,name)?store.profiles[name]:void 0;if(previous&&!replace)throw new CliError("usage",`Profile ${name} already exists. Pass --replace to update it.`);if(previous&&previous.agent!==p.agent&&store.defaults[previous.agent]===name)delete store.defaults[previous.agent];store.profiles[name]=p;writeProfiles(store);return store}function requireProfile(store,name){if(!Object.hasOwn(store.profiles,name))throw new CliError("not_found",`Local credential profile ${name} does not exist.`,{hint:"Run gauge local profiles list."});return validateProfile(store.profiles[name])}function credentialKey(agent,model){return`${agent}:${model}`}function providerFor(agent,model){if(agent==="CLAUDE_CODE")return"anthropic";if(agent==="CODEX_CLI")return"openai";const slash=model.indexOf("/");if(slash<1||slash===model.length-1)throw new CliError("usage",`${agent} model ${model} must include provider/model.`);return model.slice(0,slash)}function resolveLocalProfile(agent,model,options={},store=void 0,env=process.env){const key=credentialKey(agent,model);if(options.credentialSelections){const selected=options.credentialSelections[key];if(!selected)throw new CliError("usage",`This batch has no saved login settings for ${agent} ${model}.`,{hint:"Check `gauge local profiles list`, then start a new batch with the profile you want to use."});validateCompatibility(selected,agent,model);return selected}store??=readProfiles();const provider=providerFor(agent,model);const name=options.profile??store.defaults[agent]??null;let p;if(name)p=requireProfile(store,name);else{const apiEnv=API_KEY_ENV2[provider];const nativeProvider=agent==="CLAUDE_CODE"?"anthropic":agent==="PI"?"openai-codex":"openai";const auth=apiEnv&&env[apiEnv]?.trim()||provider!==nativeProvider?"api_key":agent==="CLAUDE_CODE"&&env.CLAUDE_CODE_OAUTH_TOKEN?.trim()?"setup_token":"access_token";p=validateProfile({agent,provider,auth})}validateCompatibility(p,agent,model);const nativeDirectory=p.auth==="access_token"?p.agent==="CLAUDE_CODE"?env.CLAUDE_CONFIG_DIR||join5(homedir(),".claude"):env.CODEX_HOME||join5(homedir(),".codex"):void 0;return{...p,name,source:p.env?`env:${p.env}`:p.agent==="CLAUDE_CODE"?"native:claude":"native:codex",...nativeDirectory?{nativeDirectory}:{}}}function validateCompatibility(p,agent,model){validateProfile(pickProfile(p));if(p.agent!==agent||p.provider!==providerFor(agent,model))throw new CliError("usage",`Credential profile for ${p.agent}/${p.provider} is incompatible with ${agent} ${model}.`,{hint:"Select a compatible --profile or change this harness's default with gauge local profiles use."})}function pickProfile(p){return{agent:p.agent,provider:p.provider,auth:p.auth,...p.env?{env:p.env}:{}}}function profileCredentialSources(profile,env=process.env){const mapped={...env};if(profile.env){if(!env[profile.env]?.trim())throw new CredentialError(`Credential environment variable ${profile.env} is not set.`,`Export ${profile.env} before starting the batch, or update the profile's --env reference.`);const canonical=profile.auth==="setup_token"?"CLAUDE_CODE_OAUTH_TOKEN":API_KEY_ENV2[profile.provider];mapped[canonical]=env[profile.env]}if(profile.nativeDirectory)mapped[profile.agent==="CLAUDE_CODE"?"CLAUDE_CONFIG_DIR":"CODEX_HOME"]=profile.nativeDirectory;return{env:mapped,homeDir:homedir(),now:new Date}}function readinessSpec(agent,model,profile){return{image:"unused",agent:{kind:LOCAL_TARGETS[agent].kind,model},auth:{mode:profile.auth},workspace:null,resources:[],prompt:{text:""},limits:{turnTimeoutS:1800,responseTimeoutS:60,maxTurns:1}}}async function inspectLocalCredential(agent,model,options={},store){let profile;try{profile=resolveLocalProfile(agent,model,options,store);const credential=await resolveSessionCredential(readinessSpec(agent,model,profile),profileCredentialSources(profile));return{agent,model,profile,ready:true,...credential.expiresAt?{expiresAt:credential.expiresAt.toISOString()}:{},verification:"local"}}catch(error){const problem=error instanceof CredentialError?`${error.message} Fix: ${error.remedy}`:error instanceof CliError?error.message:"Could not read the credential source. Repair or renew the native login and retry.";return{agent,model,profile,ready:false,problem,verification:"local"}}}import{execFileSync,spawn as spawn2}from"child_process";var runtime_manifest_default={version:"0.1.2",repository:"ghcr.io/gauge-sh/gauge-runtime",digest:"sha256:4056830a1efcb0116fb70e25a18576e26ad7222a87683d6f03387eb88f596c32",contract:"1",platforms:["linux/amd64","linux/arm64"],revision:"abb55dbfa9ceaa160c7a9685a0fd2b4e68a259aa"};var gateway_manifest_default={version:"0.1.0",repository:"ghcr.io/gauge-sh/gauge-gateway",digest:"sha256:ab5489cfe65feb717a79d7be6bd76be8bf915f3e44a5e832cee313772e590e1c",contract:"gateway-1",platforms:["linux/amd64","linux/arm64"],revision:"cdb3a7985666fbb6287be5b2b041aff327c874d7"};var RUNTIME_MANIFEST=runtime_manifest_default;var RUNTIME_IMAGE=runtime_manifest_default.digest?`${runtime_manifest_default.repository}@${runtime_manifest_default.digest}`:`${runtime_manifest_default.repository}:${runtime_manifest_default.version}`;var GATEWAY_IMAGE=gateway_manifest_default.digest?`${gateway_manifest_default.repository}@${gateway_manifest_default.digest}`:`${gateway_manifest_default.repository}:${gateway_manifest_default.version}`;var dockerRead=args=>execFileSync("docker",args,{encoding:"utf8",stdio:["ignore","pipe","pipe"],timeout:15e3});function inspectLocalRuntime(image=RUNTIME_IMAGE,docker=dockerRead,manifest=RUNTIME_MANIFEST){const result={reference:image,expectedContract:manifest.contract,supportedPlatforms:manifest.platforms,ready:false};const fail=(code,message,hint)=>({...result,problem:{code,message,hint}});let server;try{server=JSON.parse(docker(["version","--format","{{json .Server}}"]));if(!server?.Os||!server.Arch)throw new Error("No Docker server")}catch{return fail("docker_unavailable","Local runs need a running Docker daemon.","Start Docker Desktop (using Linux containers) or the Docker daemon, then retry.")}result.dockerPlatform=`${server.Os}/${server.Arch}`;if(!manifest.platforms.includes(result.dockerPlatform))return fail("platform_unsupported",`Docker ${server.Os}/${server.Arch} is not a qualified runtime platform.`,`Supported native Docker platforms: ${manifest.platforms.join(", ")}. Emulation is not qualified.`);let details;try{[details]=JSON.parse(docker(["image","inspect",image]))}catch{return fail("image_missing",`Local runs need the ${image} Docker image.`,image===RUNTIME_IMAGE?"Run: gauge local runtime pull":`Pull or build the selected runtime image: ${image}`)}if(!details?.Id||!details.Os||!details.Architecture)return fail("image_invalid",`Docker returned incomplete image metadata for ${image}.`,"Check docker image inspect for the selected image.");result.imageId=details.Id;result.repoDigest=details.RepoDigests?.find(digest=>digest.startsWith(`${manifest.repository}@`))??details.RepoDigests?.[0];result.contract=details.Config?.Labels?.["sh.gauge.contract"];result.platform=`${details.Os}/${details.Architecture}`;if(result.contract!==manifest.contract)return fail("contract_unsupported",`Runtime contract ${result.contract??"(missing)"} is unsupported; expected ${manifest.contract}.`,"Use a runtime derived from the supported Gauge image and preserve its contract.");if(!manifest.platforms.includes(result.platform)||server.Os!==details.Os||server.Arch!==details.Architecture)return fail("platform_unsupported",`Runtime ${result.platform} is not supported on Docker ${server.Os}/${server.Arch}.`,`Supported native Docker platforms: ${manifest.platforms.join(", ")}. Emulation is not qualified.`);result.ready=true;return result}function requireLocalRuntime(image=RUNTIME_IMAGE,manifest){const result=inspectLocalRuntime(image,void 0,manifest);if(!result.ready){const problem=result.problem;throw new CliError(problem.code==="image_missing"||problem.code==="docker_unavailable"?"not_found":"usage",problem.message,{hint:problem.hint})}return result}var inspectLocalGateway=(image=GATEWAY_IMAGE,docker)=>inspectLocalRuntime(image,docker,gateway_manifest_default);var requireLocalGateway=(image=GATEWAY_IMAGE)=>requireLocalRuntime(image,gateway_manifest_default);var pullLocalGateway=()=>pullLocalRuntime(GATEWAY_IMAGE,gateway_manifest_default);async function pullLocalRuntime(image=RUNTIME_IMAGE,manifest=RUNTIME_MANIFEST){const before=inspectLocalRuntime(image,void 0,manifest);if(!before.dockerPlatform||!manifest.platforms.includes(before.dockerPlatform)){const problem=before.problem;throw new CliError(problem.code==="docker_unavailable"?"not_found":"usage",problem.message,{hint:problem.hint})}const platform=before.dockerPlatform;await new Promise((resolve2,reject)=>{const child=spawn2("docker",["pull","--platform",platform,image],{stdio:["ignore","pipe","pipe"]});child.stdout.pipe(process.stderr);child.stderr.pipe(process.stderr);child.once("error",()=>reject(new CliError("not_found","Docker could not start.",{hint:"Install and start Docker, then retry."})));child.once("exit",code=>code===0?resolve2():reject(new CliError("network",`Could not pull ${image}.`,{hint:"Check the network and public registry availability, then retry gauge local runtime pull."})))});return requireLocalRuntime(image,manifest)}import{execFile as execFile2}from"child_process";import{randomUUID as randomUUID4}from"crypto";import{mkdtemp as mkdtemp2,mkdir as mkdir3,rm as rm3}from"fs/promises";import{tmpdir as tmpdir2}from"os";import{join as join6}from"path";import{promisify as promisify2}from"util";var exec=promisify2(execFile2);var tools=["sh","bash","git","sha256sum","mkdir","cp","tar","chmod","touch","node","npm","jq","curl"];var harnesses={"claude-code":"claude",codex:"codex",pi:"pi",opencode:"opencode"};var marker="GAUGE_RUNTIME_COMPATIBLE";async function checkLocalRuntimeCompatibility(imageId,agents2){if(!process.getuid||!process.getgid)throw new CliError("usage","Local runtime execution requires a Unix host UID/GID.",{hint:"Run Gauge on macOS or Linux with local Docker running Linux containers, or inside WSL on Windows."});const directory=await mkdtemp2(join6(tmpdir2(),"gauge-runtime-check-"));const name=`gauge-runtime-check-${randomUUID4()}`;try{if(/[,"\n]/.test(directory))throw new CliError("usage","The temporary directory cannot be mounted by Docker.",{hint:"Set TMPDIR to a path without commas, quotes or newlines, then retry."});await mkdir3(join6(directory,"workspace"),{mode:448});const commands=[...new Set(agents2.map(agent=>harnesses[agent]))];const script=["set -eu",...tools.map(tool=>`command -v ${tool} >/dev/null || { echo 'missing tool: ${tool}' >&2; exit 41; }`),"mkdir -p /gauge/session/home /gauge/session/workspace || { echo 'session directory is not writable' >&2; exit 42; }","touch /gauge/session/workspace/probe /tmp/gauge-runtime-probe || { echo 'session directory or /tmp is not writable' >&2; exit 42; }","mkdir -p /gauge/session/npm/bin","printf '#!/bin/sh\\nexit 0\\n' > /gauge/session/npm/bin/gauge-runtime-path-probe","chmod 755 /gauge/session/npm/bin/gauge-runtime-path-probe",...["sh -c","sh -lc","bash -lc"].map(shell=>`${shell} gauge-runtime-path-probe >/dev/null || { echo 'environment-installed tools are missing from PATH in ${shell}; rebuild from the current Gauge runtime or preserve /gauge/session/npm/bin in the system login profile and image PATH' >&2; exit 44; }`),"cd /gauge/session/workspace","git init --quiet && git -c user.name=gauge -c user.email=gauge@localhost commit --quiet --allow-empty -m probe","tar --create --gzip --file /tmp/gauge-probe.tar.gz probe","tar --extract --gzip --file /tmp/gauge-probe.tar.gz --directory /gauge/session/workspace --no-same-owner --no-same-permissions","sha256sum probe | sha256sum -c --quiet -",...commands.map(command2=>`${command2} --version >/dev/null || { echo 'missing or broken harness: ${command2}' >&2; exit 43; }`),`echo ${marker}`].join("\n");let stdout;try{({stdout}=await exec("docker",["run","--rm","--pull","never","--name",name,"--network","none","--user",`${process.getuid()}:${process.getgid()}`,"--env","HOME=/gauge/session/home","--env","CLAUDE_CONFIG_DIR=/gauge/session/claude","--env","CODEX_HOME=/gauge/session/codex","--env","PI_CODING_AGENT_DIR=/tmp/pi-agent","--env","XDG_DATA_HOME=/gauge/session/opencode/data","--env","XDG_STATE_HOME=/gauge/session/opencode/state","--mount",`type=bind,source=${join6(directory,"workspace")},target=/gauge/session`,imageId,"sh","-c",script],{timeout:6e4,maxBuffer:1024*1024}))}catch(error){const stderr=String(error.stderr??"").trim();const detail=stderr?` ${stderr.slice(-2e3)}`:" The probe failed or timed out.";throw new CliError("usage",`Runtime ${imageId} failed its compatibility check.${detail}`,{hint:"Derive from the supported Gauge runtime; preserve required tools, harnesses and the session npm bin in ordinary/login-shell PATH, permit the host UID/GID to write /gauge/session and /tmp, and have ENTRYPOINT execute the supplied command."})}if(!stdout.split(/\r?\n/).includes(marker))throw new CliError("usage",`Runtime ${imageId} did not execute Gauge's compatibility command.`,{hint:'Remove the custom ENTRYPOINT or make it finish with exec "$@". Gauge supplies sh -c and must receive its output.'})}finally{await exec("docker",["rm","--force",name],{timeout:1e4}).catch(()=>{});await rm3(directory,{recursive:true,force:true})}}import{createHash}from"crypto";import{createReadStream}from"fs";import{list}from"tar";import{z as z3}from"zod";import{z as z2}from"zod";var websiteUrlSchema=z2.url().refine(raw=>{const u=new URL(raw);return u.protocol==="https:"&&!u.username&&!u.password&&!u.port&&!u.search&&!u.hash},"Expected an HTTPS documentation URL without credentials, port, query or fragment");var websiteUseSchema=z2.object({type:z2.literal("website"),url:websiteUrlSchema.refine(url=>new URL(url).pathname==="/","Website inputs require an origin without a path")}).strict();var deploymentSourceSchema=z2.object({type:z2.literal("github-deployment"),environment:z2.string().min(1).max(128)}).strict();var commentSourceSchema=z2.object({type:z2.literal("github-pr-comment"),author:z2.string().min(1).max(100).regex(/^[a-zA-Z0-9-]+(?:\[bot\])?$/),bodyIncludes:z2.string().min(1).max(256).describe("Literal marker identifying the provider's preview comment."),pattern:z2.string().min(1).max(2048).refine(pattern=>{try{new RegExp(pattern,"gm");return pattern.includes("(?<url>")&&pattern.includes("(?<sha>")}catch{return false}},"Use a valid regex with named url and sha captures; match only a ready preview").describe("JavaScript regex (gm flags) matching readiness and capturing url and sha (7\u201340 hex characters). Execution is time limited."),failureIncludes:z2.string().min(1).max(256).optional().describe("Literal failure marker in the selected comment; failure never falls back to older comments.")}).strict();var websiteSourceSchema=z2.discriminatedUnion("type",[deploymentSourceSchema,commentSourceSchema]);function withinWebsiteScope(raw,base){const u=new URL(raw),scope=new URL(base);const prefix=scope.pathname.replace(/\/$/,"");return u.origin===scope.origin&&(u.pathname===prefix||u.pathname.startsWith(`${prefix}/`))}var MAX_WEB_FIXTURE_BYTES=1e6;function normalizeWebFixtureUrl(raw){const url=new URL(raw);url.hash="";return url.href}var searchResultSchema=z3.object({title:z3.string().min(1),url:z3.url().transform(normalizeWebFixtureUrl),snippet:z3.string()});var searchFixtureSchema=z3.object({query:z3.string().trim().min(1),results:z3.array(searchResultSchema)});var pageFixtureSchema=z3.object({url:z3.url().transform(normalizeWebFixtureUrl),status:z3.int().min(100).max(599).default(200),contentType:z3.string().trim().min(1).default("text/html"),body:z3.string()});var originProxySchema=z3.object({origin:websiteUrlSchema.refine(raw=>new URL(raw).pathname==="/","Expected an origin").transform(raw=>new URL(raw).origin),upstream:websiteUrlSchema.refine(raw=>new URL(raw).pathname==="/","Expected an origin").transform(raw=>new URL(raw).origin)}).strict();var webFixtureSchema=z3.object({version:z3.literal(1),origins:z3.array(originProxySchema).max(32).optional(),scopes:z3.array(websiteUrlSchema.transform(normalizeWebFixtureUrl)).max(32).optional(),searches:z3.array(searchFixtureSchema).default([]),pages:z3.array(pageFixtureSchema).default([])}).superRefine((fixture,ctx)=>{if(fixture.scopes?.some(scope=>!fixture.pages.some(page=>new URL(page.url).origin===new URL(scope).origin)))ctx.addIssue({code:"custom",path:["scopes"],message:"Each scope must have a captured page on its origin"});const origins=new Set;for(const route of fixture.origins??[]){if(origins.has(route.origin)||route.origin===route.upstream||fixture.pages.some(page=>new URL(page.url).origin===route.origin)||fixture.scopes?.some(scope=>new URL(scope).origin===route.origin))ctx.addIssue({code:"custom",message:"Origin proxy overlaps another fixture route"});origins.add(route.origin)}const queries=new Set;fixture.searches.forEach((entry,index)=>{if(queries.has(entry.query)){ctx.addIssue({code:"custom",path:["searches",index,"query"],message:`duplicate normalized query: ${entry.query}`})}queries.add(entry.query)});const urls=new Set;fixture.pages.forEach((entry,index)=>{if(urls.has(entry.url)){ctx.addIssue({code:"custom",path:["pages",index,"url"],message:`duplicate normalized URL: ${entry.url}`})}urls.add(entry.url)});const bytes=new TextEncoder().encode(JSON.stringify(fixture)).byteLength;if(bytes>MAX_WEB_FIXTURE_BYTES){ctx.addIssue({code:"custom",message:`serialized fixture is ${bytes} bytes; maximum is ${MAX_WEB_FIXTURE_BYTES}`})}});var webFixtureInputSchema=webFixtureSchema.safeExtend({searches:z3.array(z3.never()).max(0).default([]),pages:z3.array(pageFixtureSchema.safeExtend({url:z3.url().refine(raw=>{const url=new URL(raw);return url.protocol==="https:"&&!url.port&&!url.username&&!url.password},"Expected an HTTPS URL without credentials or a custom port").transform(normalizeWebFixtureUrl),status:z3.int().min(200).max(599).default(200),contentType:z3.string().trim().min(1).regex(/^[\x20-\x7e]+$/).default("text/html"),body:z3.string().refine(body=>!/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(body),"Body must be valid Unicode")}).strict().refine(page=>![204,205,304].includes(page.status)||page.body==="","This HTTP status requires an empty body")).default([])}).strict().refine(fixture=>fixture.pages.length>0||(fixture.origins?.length??0)>0,"Web fixture must contain pages or origin mappings");function parseWebFixtureInput(text){if(new TextEncoder().encode(text).byteLength>MAX_WEB_FIXTURE_BYTES)throw new Error(`Web fixture exceeds ${MAX_WEB_FIXTURE_BYTES} bytes`);return webFixtureInputSchema.parse(JSON.parse(text))}function mergeWebFixtures(fixtures){if(!fixtures.length)return void 0;const scopes=[];for(const[index,fixture]of fixtures.entries()){for(const scope of fixture.scopes??[]){if(scopes.some(other=>withinWebsiteScope(scope,other)||withinWebsiteScope(other,scope)))throw new Error("Web fixture scopes overlap in this case");if(fixtures.some((other,otherIndex)=>otherIndex!==index&&other.pages.some(page=>withinWebsiteScope(page.url,scope))))throw new Error("Web fixture scope overlaps another input's pages");scopes.push(scope)}}return webFixtureSchema.parse({version:1,scopes,origins:fixtures.flatMap(f=>f.origins??[]),searches:fixtures.flatMap(f=>f.searches),pages:fixtures.flatMap(f=>f.pages)})}async function readWebFixtureArchive(path,member,digest){const hash=createHash("sha256");for await(const chunk of createReadStream(path))hash.update(chunk);if(`sha256:${hash.digest("hex")}`!==digest)throw new Error("Web fixture archive digest mismatch");const chunks=[];let found=0;let bytes=0;let problem;await list({file:path,strict:true,onReadEntry(entry){if(entry.path.replace(/^\.\//,"")!==member||entry.type!=="File"||entry.size>MAX_WEB_FIXTURE_BYTES){problem=new Error("Web fixture archive must contain exactly its declared JSON file (at most 1 MB)");entry.resume();return}found++;entry.on("data",chunk=>{bytes+=chunk.length;if(bytes>MAX_WEB_FIXTURE_BYTES)problem=new Error("Web fixture exceeds 1 MB");else chunks.push(chunk)})}});if(problem)throw problem;if(found!==1)throw new Error("Web fixture archive must contain exactly one JSON file");return parseWebFixtureInput(Buffer.concat(chunks).toString("utf8"))}async function freezeWebFixtures(definition,archives){const inputs=new Map;for(const resource of definition.resources){if(resource.use.type!=="web-fixture")continue;if(resource.source.type!=="upload")throw new Error(`Web fixture ${resource.name} requires a local file`);const digest=resource.source.sha256;const archive=archives.find(a=>a.sha256===digest);if(!archive)throw new Error(`Missing web fixture archive: ${resource.name}`);inputs.set(resource.name,await readWebFixtureArchive(archive.path,resource.use.path,archive.sha256))}const cases={};for(const item of definition.cases){const fixture=mergeWebFixtures((item.config.inputs??[...inputs.keys()]).flatMap(name=>inputs.get(name)??[]));if(fixture)cases[item.path]={fixture,digest:`sha256:${createHash("sha256").update(JSON.stringify(fixture)).digest("hex")}`}}return cases}var CONCURRENCY=4;var POLL_MS=1e3;var LOCAL_AGENTS=LOCAL_TARGETS;function analysisNext(batchId){return["Next: analyze these runs. Run this command and follow it:"," gauge instructions session-analysis --local",`Analyze only batch ${batchId}.`].join("\n")}async function planLocal(definition,options={}){const{queue}=plan(definition);const warnings=[];const runtime=inspectLocalRuntime(options.runtimeImage);const gateway=definition.resources.some(r=>r.use.type==="web-fixture")?inspectLocalGateway(options.gatewayImage):void 0;if(gateway?.problem)warnings.push(`${gateway.problem.message} ${gateway.problem.hint}`);if(runtime.problem)warnings.push(`${runtime.problem.message} ${runtime.problem.hint}`);const credentials=await credentialReadiness(queue,options);for(const target of credentials)if(!target.ready)warnings.push(`${target.agent} ${target.model}: ${target.problem}`);return{mode:"local",runtime,gateway,runtimeProbe:"not_run",expectedSessions:queue.length,cases:definition.cases.map(item=>{const sessions=new Map;for(const job of queue.filter(job2=>job2.item===item)){const key=`${job.agent}\0${job.model}`;const entry=sessions.get(key)??{agent:job.agent,model:job.model,samples:0};entry.samples++;sessions.set(key,entry)}return{path:item.path,criteria:item.criteria?.length??0,sessions:[...sessions.values()]}}),inputs:definition.resources.map(r=>({name:r.name,use:r.use.type})),warnings,credentials}}function plan(definition){for(const item of definition.cases){assertLocalCapabilities(item.config,`${item.path}: config.`);if(item.config.repoRef&&!item.config.repoUrl)throw new CliError("usage",`${item.path}: config.repoRef requires config.repoUrl.`);for(const name of item.config.inputs??[])if(!definition.resources.some(resource=>resource.name===name))throw new CliError("usage",`${item.path}: input ${name} is missing from this launch.`)}const resources=new Map(definition.resources.filter(resource=>resource.use.type!=="web-fixture").map(resource=>[resource.name,toResourceRef(resource)]));const queue=definition.cases.flatMap(item=>item.config.agents.flatMap(entry=>{const agent=LOCAL_AGENTS[entry.agent];if(!agent)throw new CliError("usage",`${item.path}: agent ${entry.agent} cannot run locally.`,{hint:"Use CLAUDE_CODE, CODEX_CLI, PI, or OPENCODE, or pass --hosted."});const models=entry.models?.length?entry.models:[agent.model];return models.flatMap(model=>Array.from({length:item.config.sampleCount??1},(_,sample)=>({item,agent:entry.agent,model,sample:sample+1})))}));return{resources,queue}}async function prepareLocal(definition,uploads,gaugeDir,options={}){const{queue}=plan(definition);const webFixtures=await freezeWebFixtures(definition,uploads);const gateway=Object.keys(webFixtures).length?requireLocalGateway(options.gatewayImage):void 0;await freezeCredentials(queue,options);const runtime=requireLocalRuntime(options.runtimeImage);await checkLocalRuntimeCompatibility(runtime.imageId,[...new Set(queue.map(job=>LOCAL_AGENTS[job.agent].kind))]);const blobsDirectory=join7(gaugeDir,"blobs");await mkdir4(blobsDirectory,{recursive:true});for(const upload of uploads)await copyFile(upload.path,join7(blobsDirectory,upload.sha256.slice(7)));const batchId=`b_${randomBytes2(6).toString("hex")}`;const directory=batchDirectory(gaugeDir,batchId);await mkdir4(directory,{recursive:true,mode:448});await writeJson(join7(directory,"runtime.json"),runtime);await writeJson(join7(directory,"gateway.json"),{gateway,cases:webFixtures});await writeJson(join7(directory,"status.json"),{batchId,runtime,state:"queued",expectedSessions:plan(definition).queue.length});const store=openLocalStore(gaugeDir);try{store.recordLaunch({id:batchId,measurementId:options.measurement?.id,measurementRevision:options.measurement?.revision,configuration:definition,provenance:definition.source??{}})}finally{store.close()}return batchId}async function detachLocal(definition,uploads,gaugeDir,options={}){const credentialSelections=await freezeCredentials(plan(definition).queue,options);const batchId=await prepareLocal(definition,uploads,gaugeDir,{...options,credentialSelections});const directory=batchDirectory(gaugeDir,batchId);await writeFile3(join7(directory,"request.json"),JSON.stringify({definition,gaugeDir,batchId,credentialSelections,measurement:options.measurement}),{mode:384});const log=await open3(join7(directory,"supervisor.log"),"a",384);try{const bundledWorker=fileURLToPath2(new URL("./batchWorker.js",import.meta.url));const workerArgs=existsSync3(bundledWorker)?[bundledWorker]:[...process.execArgv,fileURLToPath2(new URL("../batchWorker.ts",import.meta.url))];const child=spawn3(process.execPath,[...workerArgs,join7(directory,"request.json")],{detached:true,stdio:["ignore",log.fd,log.fd]});await new Promise((resolve2,reject)=>{child.once("spawn",resolve2);child.once("error",reject)});let exited=false;child.once("exit",()=>{exited=true});const deadline=Date.now()+1e4;while(true){const status=JSON.parse(await readFile5(join7(directory,"status.json"),"utf8"));if(status.state!=="queued")break;if(exited||Date.now()>=deadline){await writeFile3(join7(directory,"cancel"),"",{mode:384});throw new CliError("network",`Background supervisor did not become ready. See ${join7(directory,"supervisor.log")}`)}await delay(50)}child.unref()}finally{await log.close()}return{batchId,status:`gauge run status --batch ${batchId} --local`,wait:`gauge run wait --batch ${batchId} --local`,stop:`gauge run cancel --batch ${batchId} --local`}}async function runLocal(definition,uploads,gaugeDir,onRun=()=>{},options={}){const{resources,queue}=plan(definition);const credentialSelections=await freezeCredentials(queue,options);const batchId=options.batchId??await prepareLocal(definition,uploads,gaugeDir,{...options,credentialSelections});const directory=batchDirectory(gaugeDir,batchId);const runtime=JSON.parse(await readFile5(join7(directory,"runtime.json"),"utf8"));const gatewayPlan=JSON.parse(await readFile5(join7(directory,"gateway.json"),"utf8"));const expectedSessions=queue.length;let interrupted=false;const signal=()=>{interrupted=true;process.exitCode=130};process.on("SIGINT",signal);process.on("SIGTERM",signal);const canceled=()=>interrupted||cancellationRequested(directory);const runs=[];let progress=Promise.resolve();const save=state=>writeJson(join7(directory,"status.json"),{batchId,runtime,state,expectedSessions,finishedSessions:runs.length,queuedSessions:state==="running"?queue.length:0,skippedSessions:state==="running"?0:queue.length,supervisorPid:process.pid});try{await save("running");options.onStart?.(batchId);const worker=async()=>{while(!canceled()){const job=queue.shift();if(!job)break;const run=await runJob(job,{batchId,gaugeDir,blobsDirectory:join7(gaugeDir,"blobs"),resources,credentialSelections,runtime,gateway:gatewayPlan.gateway&&gatewayPlan.cases[job.item.path]?{image:gatewayPlan.gateway.imageId,...gatewayPlan.cases[job.item.path]}:void 0,canceled,measurement:options.measurement,provenance:definition.source??{}});runs.push(run);onRun(run);progress=progress.then(()=>save("running"));await progress}};const settled=await Promise.allSettled(Array.from({length:CONCURRENCY},worker));const failure=settled.find(s=>s.status==="rejected");await save(failure?"failed":canceled()?"canceled":"completed");if(failure?.status==="rejected")throw failure.reason;return{batchId,runs,next:analysisNext(batchId),canceled:canceled()}}finally{process.off("SIGINT",signal);process.off("SIGTERM",signal)}}function toResourceRef(resource){if(resource.source.type!=="upload")throw new CliError("usage",`Input ${resource.name} has no local file; local runs need every input in the working tree`);const base={name:resource.name,digest:resource.source.sha256,filename:`${resource.name}.tar.gz`,kind:"archive"};const use=resource.use;if(use.type==="files")return{...base,use:{type:"files"}};if(use.type==="skill"&&use.root===".")return{...base,use:{type:"skill"}};if(use.type==="npm-package")return{...base,use:{type:"npm-package",package:use.package,install:use.install??"workspace"}};throw new CliError("usage",`Input ${resource.name}: ${use.type==="skill"?"supply the Skill as a directory":`${use.type} inputs run on hosted Gauge only`}`)}async function credentialReadiness(queue,options){const store=options.credentialSelections?{version:1,profiles:{},defaults:{}}:readProfiles();const targets=new Map(queue.map(job=>[credentialKey(job.agent,job.model),job]));return Promise.all([...targets.values()].map(job=>inspectLocalCredential(job.agent,job.model,options,store)))}async function freezeCredentials(queue,options){const readiness=await credentialReadiness(queue,options);const failures=readiness.filter(target=>!target.ready);if(failures.length)throw new CliError("usage",failures.map(target=>`${target.agent} ${target.model}: ${target.problem}`).join("\n"),{hint:"Inspect credentials with gauge local profiles inspect <profile>, or preview every target with gauge run --local --dry-run -o json."});return Object.fromEntries(readiness.map(target=>[credentialKey(target.agent,target.model),target.profile]))}function sessionSpec(job,resources,profile,runtimeImage=RUNTIME_IMAGE){const names=job.item.config.inputs??[...resources.keys()];const selected=names.flatMap(name=>resources.get(name)??[]);const files=selected.filter(r=>r.use?.type==="files");const note=files.length?`
66
+
67
+ Prepared inputs:
68
+ ${files.map(r=>`- ${r.name}: /gauge/session/resources/${r.name}/content`).join("\n")}`:"";const{repoUrl,repoRef}=job.item.config;const agent=LOCAL_AGENTS[job.agent];return{image:runtimeImage,agent:{kind:agent.kind,model:job.model},auth:{mode:profile.auth},workspace:repoUrl?{repo:repoUrl,ref:repoRef??"HEAD"}:null,resources:selected,prompt:{text:job.item.prompt+note},limits:{turnTimeoutS:1800,responseTimeoutS:60,maxTurns:1}}}async function runJob(job,context){const id=`r_${randomBytes2(6).toString("hex")}`;const attemptDir=join7(context.gaugeDir,"runs",id,"attempts","1");const directory=join7(attemptDir,"session");await mkdir4(attemptDir,{recursive:true});const createdAt=new Date().toISOString();const frozenCase={...job.item,criteria:job.item.criteria?.map((criterion,index)=>({...criterion,id:context.measurement?.criterionIds?.[index]??`criterion-${index+1}`}))??null,preference:job.item.preference??null};const configuration={...job.item.config,case:frozenCase};const metadata={attempt:{runId:id,n:1,batchId:context.batchId},createdAt,launchId:context.batchId,measurement:context.measurement??null,provenance:context.provenance,configuration,agent:{kind:LOCAL_AGENTS[job.agent].kind,model:job.model},agentName:job.agent,runtime:context.runtime,case:frozenCase,sample:job.sample};await writeJson(join7(attemptDir,"spec.json"),metadata);const session2={id,batchId:context.batchId,launchId:context.batchId,measurementId:context.measurement?.id??null,measurementKind:context.measurement?.kind??null,measurementRevision:context.measurement?.revision??null,case:job.item.path,agent:job.agent,model:job.model,attempt:1,outcome:"starting",endedBy:null,turns:null,createdAt,endedAt:null,configuration,provenance:context.provenance};persistSession(context.gaugeDir,session2);const summary=(status,endedBy2)=>({id,case:job.item.path,agent:job.agent,model:job.model,sample:job.sample,outcome:status.state,endedBy:endedBy2,turns:status.turn,...status.detail?{detail:status.detail}:{}});const canceled=()=>context.canceled()||cancellationRequested(attemptDir);let result;try{const profile=resolveLocalProfile(job.agent,job.model,{credentialSelections:context.credentialSelections});const spec=sessionSpec(job,context.resources,profile,context.runtime.imageId);spec.gateway=context.gateway;await writeJson(join7(attemptDir,"spec.json"),{...metadata,session:spec,credentialProfile:profile});if(canceled()){result=summary({state:"canceled",turn:0},"user_cancel")}else{await startSession({directory,spec,blobsDirectory:context.blobsDirectory,credentials:profileCredentialSources(profile)});result=await drive(directory,summary,canceled)}}catch(error){let cleanupError;try{await stopSession(directory)}catch(failure){cleanupError=failure}result=summary({state:"failed",turn:0,detail:error instanceof CredentialError?`${error.message} Fix: ${error.remedy}`:error instanceof Error?error.message:String(error)},"error");if(cleanupError&&existsSync3(join7(directory,"session.json")))result.detail+=`; cleanup pending: ${String(cleanupError)}. Run gauge cleanup --local.`}const{outcome,endedBy,turns,detail}=result;const endedAt=new Date().toISOString();const durationMs=Date.parse(endedAt)-Date.parse(createdAt);await writeJson(join7(attemptDir,"result.json"),{outcome,endedBy,turns,endedAt,durationMs,...detail?{detail}:{}});persistSession(context.gaugeDir,{...session2,outcome,endedBy,turns,endedAt,durationMs});return result}function persistSession(gaugeDir,session2){const store=openLocalStore(gaugeDir);try{store.upsertSession(session2)}finally{store.close()}}async function drive(directory,summary,canceled,session2={getSessionStatus,stopSession,finishSession}){let endedBy="session";let stopDetail;for(;;){if(canceled()&&endedBy!=="user_cancel"){endedBy="user_cancel";await session2.stopSession(directory)}const status=await session2.getSessionStatus(directory);if(status.state==="waiting"&&endedBy==="session"){endedBy="supervisor_finish";await session2.finishSession(directory,status.turn)}else if(status.state==="blocked"&&endedBy==="session"){endedBy="supervisor_stop";stopDetail="Stopped: the agent asked a question, and local runs have no user to answer it";await session2.stopSession(directory)}else if(["completed","failed","canceled","interrupted"].includes(status.state)){if(status.cleanupPending||status.state==="interrupted")await session2.stopSession(directory);const final=await session2.getSessionStatus(directory);return summary(stopDetail?{...final,detail:stopDetail}:final,endedBy)}await delay(POLL_MS)}}async function writeJson(path,value){const temporary=`${path}.${randomBytes2(6).toString("hex")}.tmp`;await writeFile3(temporary,`${JSON.stringify(value,null,2)}
69
+ `,{mode:384});await rename2(temporary,path)}export{openLocalStore,localStatus,cancelLocal,cleanupLocal,localBatchStatus,LOCAL_MCP_HINT,assertLocalCapabilities,inspectLocalRuntime,inspectLocalGateway,pullLocalGateway,pullLocalRuntime,LOCAL_TARGETS,readProfiles,writeProfiles,setProfile,requireProfile,inspectLocalCredential,websiteUseSchema,deploymentSourceSchema,commentSourceSchema,websiteSourceSchema,parseWebFixtureInput,mergeWebFixtures,readWebFixtureArchive,freezeWebFixtures,LOCAL_AGENTS,analysisNext,planLocal,detachLocal,runLocal};