Files
gstack/scripts/ship-measure.ts
T
f67c478f05 v1.91.34.0 fix: /autoplan on current Claude Code and long sessions, quieter pre-push and sync guards, gstack-post, gstack-doctor (#3069)
* docs: Oct 7 follow-up wave plan (pre-review)

* docs: Oct 7 follow-up wave plan, reviewed through /autoplan

* docs: Oct 7 wave plan, record final gate decisions (UC1 accepted, UC2 rejected)

* docs: credit #1822 to @Mike-E-Log and #1743 to @habassa5 (v1.64.0.0)

The v1.64.0.0 CHANGELOG entry credited the extension token fix (#1822)
and the polyfill exited promise (#1743) to the wrong handle, and the
extension-sender-auth test header repeated the first slip. Commit history
is not rewritten.

Fixes #3054.

Co-authored-by: Mike Ilog <48659643+Mike-E-Log@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(context-restore): offer newer checkpoints from nested repos and task worktrees

/context-restore could present an older checkpoint as the latest with no
error in two layouts:

1. A session started in a non-git parent saved from a nested repo, so the
   checkpoint went to the nested repo's bucket. Restore in the parent now
   lists checkpoints of other buckets whose project_root lies below the cwd
   (BELOW_CWD; the 200 newest are read, and from $HOME only each project's
   newest). /context-save also leaves a JSON pointer (checkpoint path, bucket,
   project_root) in the starting directory's bucket when $CLAUDE_PROJECT_DIR
   is set and files elsewhere; restore lists those as POINTER, in or out of git.
2. A plan checkpoint on the main tree's branch shadowed a newer checkpoint
   saved in a ticket worktree. When another branch holds a newer checkpoint
   for the same task (a worktree below the main tree, read from the new
   worktree: stamp; equal normalized titles; or a shared #123 / ABC-123
   token in titles or branch names), restore prints NEWER_TASK and the skill
   shows both and proposes the newer one. Generic titles (wip, checkpoint,
   save, notes, todo) never match on title alone, and the worktree rule
   applies only when restoring from the main tree, so sibling worktrees
   still never shadow each other.

The branch-first ordering itself is unchanged.

Refs #3065.

Co-authored-by: Thomas Gernhardt <t.gernhardt@softwareprojekt.net>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(redact-prepush): list each MEDIUM finding with rule, file line and fix

The pre-push hook printed only a MEDIUM count, which nobody could review
without re-running the engine by hand. It now prints one line per finding:
rule id, file and file line, and a fix, never the matched value.

Line numbers come from the hunk headers (combined --cc headers included), so
a finding in a later hunk or past a scan-chunk boundary points at the real
file line. Findings from the per-commit catch-up path name the commit whose
file line it is, so content removed before the pushed tip is still locatable.

Plan items: CEO-7 (summary half), DX-9 (fix lines), ENG-8 (line mapping).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(redact-prepush): stop flagging your own email and already-public authors (#3060)

The hook never passed the engine's selfEmail / repoPublicEmails, so every
push that added the pusher's address, or a commit author's, raised a MEDIUM
pii.email warning for an address the destination already shows.

The hook now reads `git config user.email` and one bounded `git log` of
author and committer addresses, raw and mailmapped (%ae %aE %ce %cE): the
history of every pushed tip (those commits become public with the push),
each remote tip being replaced, and, for a new branch or an unknown remote
tip pushed to a configured remote, that remote's own tracking refs. A URL or
unknown destination never borrows tracking refs, and another remote's
history never counts.

The read stops at 50,000 commits (the newest authors are used) and 5 seconds.
An error or the timeout passes no history and, when an email finding is
listed, prints one line saying suppression was limited for this push. The two
inputs fail independently. HIGH findings and every other rule are untouched.

Controls: a stranger's address, an address only another remote knows, a URL
push next to a matching remote, and a first push of a repo with no history
all still report.

Plan items: A3, CEO-7, CEO-23 (A3 half), DX-9 (partial history), ENG-8
(history scope, mailmap).

Co-authored-by: akmandhania <143462686+akmandhania@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(redact-prepush): per-address allowlist for pii.email (gstack.redact.allowEmail)

`git config --add gstack.redact.allowEmail <address>` allows that address for
MEDIUM pii.email findings only, so a team alias or support address that has
to be in the repo stops warning without reaching for --no-verify. It never
affects HIGH findings or any other rule, and the pii.email fix line names it.

Controls: a different address still reports, and a HIGH secret next to an
allowed address still blocks.

Plan item: DX-17.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(troubleshooting): pre-push pii.email findings, exemptions and allowlist

Plan item: DX-12 (pre-push touchpoint).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(redact-prepush): assemble fixture addresses at runtime

The email fixtures were literals, so this branch's own push listed 47
pii.email findings in its test file. Build them from a helper instead, the
way the neighbouring tests build credential and IP fixtures.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(browse): offer the user's signed-in tab, name silent aside repl failures

Rules (rendered into every browsing skill, kept short):
- Rule 1: before the first openTab, offer the user's tabs on the target
  origin (title and origin only; the rest of listBrowserTabs() stays
  private) and attach only after the user confirms one.
- Rule 4: a second sign-in wall after a confirmed sign-in means the session
  is bound to the tab or URL; offer that tab instead of another sign-in.
- Rule 6 no longer suggests an early return; rule 7 says a fast [ok without
  GSTACK_STEP_OK is an abort.

Cookbook (browse, devex-review): a "Silent failures" list (evaluate returns
only serializable values, no top-level return, locator(sel).press(key), an
empty DOM read is about the selector until the screenshot and the request's
response say otherwise, read toggle state before clicking) and a signed-in
tab recipe that prints only target-origin tabs and never closes the user's tab.

Parity caps for design-consultation, land-and-deploy, qa and the browse
skeleton are raised to the measured sizes, with notes.

Refs #3063, #3064.

Co-authored-by: Thomas Gernhardt <t.gernhardt@softwareprojekt.net>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(codex): run gstack-codex-probe as a command, one subcommand per probe

Generated skills now execute bin/gstack-codex-probe (select-model, check-auth,
show-sandbox, check-sandbox, probe-model, check-version, show-first-use-notice,
run-with-timeout, log-event, log-hang, help) and capture its KEY: value lines,
so no helper state crosses into the calling shell and the preflight works from
bash, zsh or sh. select-model keeps --model > GSTACK_CODEX_MODEL > config.toml
precedence; run-with-timeout supervises with the existing gtimeout/timeout/
watchdog chain and returns 124 on a deadline; an unknown subcommand exits 64.

Sourcing keeps working for skills rendered before the upgrade and prints one
deprecation line naming /gstack-upgrade; it and the zsh self-locate block are
removed in a release on or after 2026-10-21 (DX-18). Parity caps raised with
measured notes for the longer capture lines.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(gstack-post): one argv-only, redaction-scanned write path for PR and issue text

bin/gstack-post posts a PR/MR or issue comment, a review-comment reply, a
PR title or body, a new PR/MR or a new issue from agent-written files. Every
value reaches gh/glab as its own argv element in --flag=<value> form or on
stdin, never through a shell string, and a target is a number or a URL of
this repository on the same host.

The helper owns the redaction scan: it reads each file once, scans those
exact bytes (visibility from config, then gh/glab; the pusher's own email
suppressed) and sends the same bytes. HIGH is refused with exit 1 and no
confirmation can post it. MEDIUM exits 2 with RULE:/LINE:/PART: lines and a
TOKEN: digest of the bytes, destination, operation and findings; only
--confirm <token> for those exact bytes posts, so any edit needs a new
confirmation. gh/glab failures exit 3, usage errors 64. It also carries the
projectCards REST fallback and the title read-back that /ship did by hand.

The logic lives in lib/gstack-post.ts behind an injectable runner, so its
tests run in the Windows free lane; the stub-gh executable test is POSIX.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(autoplan): guard verifies dispatches on Claude Code 2.1.29x (schema strip, unflushed call, named causes)

Cause A: Claude Code journals the model's raw Agent input but hands
PreToolUse the schema-parsed input; with the fork-subagent gate on (the
interactive default in 2.1.292) run_in_background is dropped, so the journal
record never deep-equaled the payload. nativeToolInput now drops only that
documented strip for Agent. Agent input is bound on both paths: keys limited
to prompt, description, subagent_type (general-purpose) and
run_in_background; a model override or any other key is denied.

Cause B: when a ready journal lacks the current call, the guard evaluates
the payload placed after the last journaled record instead of polling for a
record that may arrive only with the tool result. A phase transition counts
a report only when a later journaled record follows it (and, on the journal
path, it is not in the current call's message). One batch rule covers both
paths; a background reviewer keeps the invocation armed until its
completion notice.

Every outcome now has a stable reason code, disposition and anchor
(autoplan/bin/guard-reasons.ts). UC1: Claude-Code-caused states
(unrecognized shape, journal lag, rewritten journal, oversized record,
unchecked-version schema strip) allow with a visible warning; integrity
failures stay denials. No denial advises a bare retry. Every guarded
decision appends one content-free line to analytics/autoplan-guard.jsonl,
trimmed to 1,000 lines past 256 KiB.

Co-authored-by: Jared <182125291+yolo-jared@users.noreply.github.com>
Co-authored-by: crblabs <293123359+crblabs@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(autoplan): publish each phase report with a no-op call; foreground requests are conditional

Phase-close step 6 sends the report in a message whose only tool call is
the Bash no-op ': autoplan-published <phase>', and step 7 makes the next
guarded call in a later message, so the guard sees the report flushed
before the next phase entry regardless of Claude Code's journal timing.

Every site that asked for run_in_background: false now asks for it only
when the Agent schema offers it and says how to recover when a dispatch
runs in the background anyway (wait for its completion notice). Parity
caps rise by the measured bytes.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(ship,spec,review): PR and issue text goes out only through gstack-post

/ship: Step 18 writes the title into an agent-written file. An existing PR's
title is piped from gh/glab through gstack-pr-title-rewrite.sh --stdin (new),
so neither the agent nor a shell string carries it; a new title is written
with the file tool and its version prefix checked in the file. Step 19
composes the body as before and publishes with gstack-post pr-create, or
pr-body then pr-title, which scan the exact bytes they send. The separate
title and body scans, the hand-rolled REST fallback and the title self-check
are gone (the helper does all three). Title preparation moved into the
pr-body section, so the skeleton shrinks.

/spec files its issue with gstack-post issue-create and drops the pre-issue
re-scan; Greptile replies use gstack-post reply and pr-comment.
scripts/resolvers/redact-doc.ts loses its PR and issue sinks, and a test
keeps any template from rendering one again.

Tests that pinned the old scan-then-gh wiring now pin the gstack-post wiring;
the publication tests run the real compose and publish blocks against a stub
gh with the real helper (HIGH in title or body refused, MEDIUM token flow).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* ci: pin Claude Code 2.1.292 in the CI image

2.1.292 is the npm latest users run. Its interactive default turns on the
fork-subagent gate, which strips run_in_background from the Agent schema
and backgrounds reviewers; CI on 2.1.284 never saw the /autoplan guard
break. The image tag is a content hash, so every eval workflow picks up
the new image without a workflow edit.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(brain-sync): one drain lock for every writer to the artifacts repo

gstack-artifacts-init, gstack-brain-restore, gstack-gbrain-source-wireup
and the skill-start merge ran git in $GSTACK_HOME without the drain's lock,
so the drain could never tell an abandoned .git/index.lock from a live one.
They now take the same mkdir lock (bin/gstack-brain-lock.sh): the drain and
the skill-start merge skip when it is held, user-run writers wait for it
(GSTACK_BRAIN_LOCK_WAIT, default 120 s), and a child of the holder (the
drain's daily worktree advance, restore's wireup) inherits it by token.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(brain-sync): hold back only the flagged file, keep coupled artifacts whole

One file with secret-shaped text used to block every push, silently. The
drain now attributes scanner hits to files in one pass over the staged diff
(split on its diff --git headers), holds back each flagged file with the
rest of its coupled group, unstages it, and requires the remaining staged
diff to re-scan clean; anything short of that falls back to a full unstage
and status blocked. Held files stay queued and are re-scanned every drain.

- Status file gains held_count, drainable, last_drain_at, last_push_at and
  held[] (path, scanner rule, dependents, both fixes); new status held.
- Coupled groups come from an explicit map (decisions.jsonl and
  decisions.active.json): a queued member stages the whole group, a group
  stages only when its snapshot matches its log in the index, a skipped
  member stops the whole group.
- --unskip-file undoes --skip-file and queues the path again.
- An index lock older than 10 minutes is removed under the drain lock; a
  younger one, a failed git add, a failed commit (hooks included) keep the
  queue and report status error. A verified empty staged diff is the only
  "nothing to commit". git add runs once, batched.
- The scanner reads bytes, so undecodable content no longer crashes it into
  a false clean; matched text never reaches the status file.
- An empty-queue run with unpushed sync commits stays push_failed.
- Privacy-held duplicate records collapse to one per path; --status counts
  a spool too deep for a shell glob.
- --attention prints skill start's fixed attention lines from the sync
  status table in the usage header.

Co-authored-by: v639dragoon <matthewsbrickley@gmail.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(skill-start): ARTIFACTS_SYNC attention lines for a stuck sync

Skill start now prints one fixed attention line per sync-status-table row
that applies (held, blocked, push_failed, error, unknown, stale-push,
stale-drain), each naming gstack-brain-sync by absolute path. Values from
the status file are clamped (status code to the table, counts to digits,
timestamps to their shape), so the status message, file paths and scanner
output never reach the line. The preamble tells the model to surface the
line in one sentence and continue. Skill-start tests are generated from the
table in bin/gstack-brain-sync, and docs/gbrain-sync-errors.md carries the
same table.

Co-authored-by: v639dragoon <matthewsbrickley@gmail.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(outside-review): severity words in label position count as findings

The Oct 7 /autoplan run's Codex CEO and DX reviews labeled seven and eight
findings High/Medium without [Pn] tags, and the classifier printed
VERDICT: clean / FINDINGS: none for both. lib/outside-review-result.ts now
reads severity words in label position (Severity: High, a leading High: /
High — / [High], **High**, a table cell): critical/high block like P0/P1,
medium/low are advisory. Words in prose (high-level, low-risk, no critical
findings) never count. A review or structured response with neither tags,
labels nor an explicit no-findings statement is unverified, never clean
(DX-11, ENG-10). Design-direction proposals move to a new proposal gate so
they keep needing only their Recommendation line, and review prompts ask
for a severity per finding. Fixtures reproduce the CEO/DX label shapes, the
eng [P1] output, a table and prose negative controls; the stored Codex panel
keeps its verdicts. docs/troubleshooting.md explains how the verdict is read.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat: gstack-doctor readiness report and setup --status Codex row (B5)

bin/gstack-doctor prints one row per check without starting a skill:
install root (the registry rows behind ./setup --status), state root, Bun
against the floor, hook parse check, Codex mode and self-locate via
gstack-codex-probe subcommands, the cached model probe with its age
(--live runs the paid probe-model), artifacts-sync health from the status
file with the status code clamped, the browse bundle, Claude Code version,
the largest session journal and the last five schema-1 guard reason codes.
Rows are ok, warn, not configured or fail; only fail exits non-zero; fix
lines name helpers by absolute install-root path.

./setup --status gains a read-only Codex row (bin/gstack-codex-status.sh,
shared with the doctor) and ends with the doctor's absolute path.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs: bug-report template asks for doctor output; README troubleshooting entry; doctor test in the Windows lane

The template gives the Claude Code doctor path and a lookup for other
hosts and names no bare gstack- command (asserted in
test/gstack-doctor.test.ts). The doctor test runs every script through
explicit bash argv, so it joins the curated Windows free subset.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(troubleshooting): readiness check section points at gstack-doctor

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(question-tuning,plan-tune,gbrain): free text reaches helpers only through agent-written files

Question tuning no longer pipes the question summary through printf: a
registered id needs no summary, and for an unregistered id the agent writes
the summary to .gstack/tmp/qt.txt and passes --summary-file (new). The
helper resolves a relative path against the git top level, removes scratch
files under .gstack/tmp, and asks normally when the file is missing.

/plan-tune tune events carry the user's own words again: they go into a
file the agent writes and reach gstack-question-preference --write through
--free-text-file (new), still behind the injection filter and the
user-origin gate.

docs/gbrain-write-surfaces.md drops its heredoc templates for the
agent-written page file on stdin, matching the per-skill save block, and the
generic save block stops showing a quoted --content argument.

The tuning prose was tightened so every preamble stays within its size cap.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(evals): daily autoplan-schema-canary on the latest Claude Code

A periodic rule case installs the latest published Claude Code with
install scripts off, verifies registry signatures, and runs one headless
session with the fork-subagent gate on whose PreToolUse hook records an
Agent and a Read payload. Each payload must match its journal record under
the guard's own comparison, keep the journal's key set apart from the
documented run_in_background strip, and stay inside the Agent allowlist;
a background Agent's completion notice must keep the shape the guard reads.
evals-periodic.yml runs it daily on its own schedule and concurrency group
(read-only token); the weekly census also includes it. A free test proves
the comparison fails on an injected strip.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(doctor): read the artifacts sync held count from held_count, the field the drain writes

* fix(artifacts sync): the init and restore pre-commit hooks catch Bearer/Basic/Token-prefixed JSON auth headers like the drain does

* feat(greptile-early): detect Greptile, ask once on public repos, open the early draft, wait for the review

bin/gstack-greptile-early backs /ship's early Greptile PR:
- detect: a root .greptile/ folder (which takes precedence), greptile.json,
  or a Greptile comment on the repo in the last 90 days; triggerOnDrafts from
  the effective root config; repo visibility; public-repo consent. It prints
  the signal, that /ship will push and open a draft now, the 10-minute wait
  cap and the absolute gstack-config command that turns it off.
- consent yes|no: the public-repo answer, remembered per repo in the
  project's state.
- open: opens the draft through gstack-post with a body saying /ship opened
  it early, and posts one @greptileai comment through gstack-post when the
  config does not set triggerOnDrafts: true. An open PR is left alone.
- wait: polls every 30 s for a completed Greptile check run, a Greptile
  review of the head commit or a summary naming it; each call returns within
  about 90 s and reports timeout 10 minutes after the PR opened.

gstack-config gains ship_greptile_early (default true, invalid values
rejected): on automatically whenever the repo uses Greptile, off per user.
The logic is in lib/greptile-early.ts behind an injectable runner, so its
fixture-repo tests run in the Windows lane; an end-to-end test drives the
real helper and gstack-post against a stub gh.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* feat(ship): early Greptile PR at Step 6.5, bounded wait at Step 10 (#3020)

When the repo uses Greptile, /ship pushes and opens a draft PR right after
its free tests pass (new lazy section greptile-early, Step 6.5), so Greptile
reviews while Steps 7-9 run. A public repo asks once, remembered per repo;
declining keeps today's order. Step 10 waits for that review with
gstack-greptile-early wait and takes the existing unavailable route on a
timeout, never a claim of zero comments. Step 18 replaces the provisional
title, Step 19 marks the draft ready unless a draft was requested, and a
/ship that stops after Step 6.5 leaves the draft open with one comment
saying why.

ship's union cap rises to 1.5275 with the measured note (B4 +836 bytes,
B2 +4,732 bytes); the step-numbering guard allows 6.5; ship goldens are
refreshed for B4 and B2.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(autoplan): the publication no-op is 'true autoplan-published <phase>'

Measured on Claude Code 2.1.292 in a default-settings interactive session:
a bare ':' Bash command asks for approval ('This command requires
approval'), and after a background reviewer's completion notice starts a
new turn the skill's allowed tools no longer cover it, so the phase-close
no-op prompted the user. 'true' with arguments runs without approval and
prints nothing. The guard only needs a later journaled record, so the
command changes everywhere it is named.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(evals): live autoplan-guard-pty case on the pinned Claude Code

A gate-tier rule case (one trial, haiku) drives an interactive PTY
session with default settings through a project skill that carries
/autoplan's own hook block: init, Phase 1 entry, a CEO reviewer dispatch
that sends run_in_background, a prepared close packet, the report
published with the no-op, and the Phase 2 entry in a later message. It
reads outcomes from the guard decision log (three verified allows) and
fails if the no-op raises a permission card; other cards are approved and
printed.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(autoplan): bounded, append-tolerant owned-journal read (A2)

The publication guard no longer reads the whole parent journal or refuses
it at 32 MiB. lib/claude-owned-journal.ts streams the bytes present at open
to the last complete line (pass one), keeps a content-free index per record
and decides ownership, ancestry and causal order on it with the same checks
and reason codes as before. Pass two reloads in full only what the
evaluation reads (autoplan/bin/guard-journal.ts): every literal init and its
result, the current call, and inside the invocation window (from the latest
human turn at or before the bound chain's first init) guarded and
close-packet Read results, structured Bash results, error results, assistant
text after the first close Read and active-plan mutations, plus the earlier
Read a dedup reply cites even before the window (ENG-4).

- lib/claude-journal-records.ts: per-record event extraction, record
  metadata and the causal-order checks, shared by readPlanCountTranscript
  and the bounded reader. The batch reader keeps its whole-file read and cap.
- Appends after open never fail a read; same device and inode and no
  shrink are required (identity). A second read in one hook invocation must
  hash bytes [0, first size) the same, or it is `rewritten` (UC1 unverified);
  the unrecognized-shape double read uses that prefix hash. `changing` is
  removed (REMOVED_CODES; its anchor stays).
- OWNED_RECORD_MAX_BYTES (32 MiB): only one record over it is too_large.
  OWNED_RETAINED_MAX_BYTES (32 MiB): over it is the new oversized_invocation
  fallback denial. GSTACK_TRANSCRIPT_TEST_MAX_BYTES and
  GSTACK_TRANSCRIPT_TEST_RETAINED_BYTES can only lower them.
- ENG-5: evaluation groups uses and results by tool id once (no per-tool
  rescans).

CEO-25 / ENG-5 spike (120 MiB synthetic, 31,242 records, 4 vCPU Linux,
Bun 1.4.2): a full streaming rescan (parse every line, small index, sha256)
took 254-326 ms at 147-161 MiB peak RSS; a pass-one index cache under the
state root (load cached index, verify a 64 KiB tail hash, parse the tail)
took 26-29 ms at 47 MiB. The rescan meets the 1 s budget, so the cache is
not adopted: it would add trusted persistent state and invalidation for no
budget need. A backward tail read cannot prove competing_root or cycle, so
it was not a candidate.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(autoplan): journal-read benchmark and its dispatch-only workflow (CEO-2, CEO-6)

scripts/measure-journal-read.ts builds two synthetic 120 MiB parent journals
around a real /autoplan invocation (mixed: ~28k records, most bytes inside
the invocation; large-invocation: ~89k records with a current invocation of
many small records), keeps appending while the guard reads, and measures one
hook call in a fresh process (wall ms, peak RSS). The workflow runs it on
ubuntu-latest and macos-latest on workflow_dispatch only.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(evals): autoplan-long-session, a resumed >100 MiB session enters Phase 1

CEO-15 with ENG-6's observer: a real session is compacted, its journal
padded past 100 MiB with owned records before the compact boundary, and
the resumed interactive session runs the scripted phase boundary; the
guard decision log must show a verified Phase 1 allow. A free test proves
that observer on a padded journal through the lower-only bound seams
(the PTY counting reader refuses the same journal), with a sidechain
control that must deny.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* v1.91.34.0: CHANGELOG, TODOS, version; harness decision for test-pr-profile; ship title-step test reads its new section

* docs(changelog): use an example.com address in the allowEmail example

* test: assemble GitHub-token fixtures at runtime in the new brain-sync and skill-start tests (CI credential scan)

* fix: integration repairs across the wave (Step 6.5 fence, printed helper paths on Windows, probe usage without a heredoc) and tests pinned to the moved title and gstack-post contracts

* test(doctor): the healthy fixture builds browse.exe on Windows, the name setup uses there

* test(shared-libs): recognize Claude Code 2.1.292's internal git probe prefix (format.pretty and per-hook disables), with its CI receipt

* fix(ship-measure): report lists v1 measurements as an older format instead of crashing

---------

Co-authored-by: Mike Ilog <48659643+Mike-E-Log@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: Thomas Gernhardt <t.gernhardt@softwareprojekt.net>
Co-authored-by: akmandhania <143462686+akmandhania@users.noreply.github.com>
Co-authored-by: Jared <182125291+yolo-jared@users.noreply.github.com>
Co-authored-by: crblabs <293123359+crblabs@users.noreply.github.com>
Co-authored-by: v639dragoon <matthewsbrickley@gmail.com>
2026-10-07 23:29:05 +00:00

843 lines
52 KiB
TypeScript

#!/usr/bin/env bun
/**
* ship-measure — /ship's measure-then-fix runner for a red eval case or a red
* free-suite shard, and the weekly off-ship qualification sweep. Every trial
* it runs is DIAGNOSTIC: it never changes a recorded verdict, its artifacts
* are labeled `diagnostic` with `verdict: null`, and they live under
* `.context/ship-measure/`, never in the project eval dir that pass-rate
* history and CI uploads read. The lane verdict still comes from the one full
* gate run /ship makes after the case measures at or above the bar
* (docs/TESTING_INTERNALS.md#ship-measure).
*
* Trials per batch (config keys in bin/gstack-config): rule
* ship_measure_rule_trials (10); behavior ship_measure_behavior_panels panels
* of 3 (4, so 12 trials); judge ship_measure_judge_outputs outputs (10), each
* scored by its median 3-sample panel. The decision is the measurement bar in
* scripts/lib/measure-bar.ts: MEETS at 9/10, MEETS-qualified at 8/10 with
* every red in a qualifying class, EXTEND at 7/10 (N more trials on identical
* inputs, decided once on the pooled 2N), else BELOW (a fix round). A batch
* with more than 30% provider-evidence failures is void and is redispatched
* once.
*
* Spend: ship_measure_budget_usd is an estimated ADMISSION budget per red case
* across the baseline, extension, redispatch and every repair round. Before
* each batch the runner reserves the estimated cost of every concurrent trial
* and admits only what fits beside what was already spent; actual costs are
* reconciled after the batch. With no per-trial estimate it asks once
* ("estimate unknown"), then runs one calibration trial alone.
* ship_measure_ask_per_trial_usd asks first above that per-trial estimate.
* ship_measure_max_rounds caps repair rounds.
*
* Usage:
* bun run scripts/ship-measure.ts table
* bun run scripts/ship-measure.ts measure --case ID --round baseline|round-N
* [--kind rule|behavior|judge] [--command 'CMD {case}'] [--cost-per-trial USD]
* [--approved] [--fix TEXT] [--jobs N] [--out DIR]
* bun run scripts/ship-measure.ts classify --case ID --round R --trials 2,5 --class C --evidence TEXT [--out DIR]
* bun run scripts/ship-measure.ts decide --case ID --round R [--out DIR]
* bun run scripts/ship-measure.ts extend --case ID --round R [--command ..] [--cost-per-trial USD] [--jobs N] [--out DIR]
* bun run scripts/ship-measure.ts skip --case ID --reason TEXT [--out DIR]
* bun run scripts/ship-measure.ts report [--out DIR]
* bun run scripts/ship-measure.ts free (--files a,b,... | --shard I) [--reruns N]
* [--concurrency C] [--backend local|ubicloud] [--wall-cap SECS] [--out DIR]
* bun run scripts/ship-measure.ts sweep [--k K] [--cap-usd USD] [--dry-run] [--history-dir DIR]...
* [--prior DIR] [--command 'CMD {case}'] [--jobs N] [--out DIR] (scripts/ship-measure-sweep.ts)
*
* measure, extend, classify and decide exit 0 at MEETS or MEETS-qualified, 1
* at BELOW (a fix round), 2 when it needs the user's approval (nothing ran), 3
* on a named-red stop (budget exhausted, round limit, a void batch twice), 4 on
* a usage, setup or refused-classification error, 5 at needs-classify, 6 at
* EXTEND. free exits 0 when every completed rerun passed, else 1.
*/
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { spawn, spawnSync } from 'node:child_process';
import { EVAL_POLICY } from '../test/helpers/periodic-exclude-data';
import { E2E_KINDS, E2E_TIERS, LLM_JUDGE_TOUCHFILES } from '../test/helpers/touchfiles-data';
import { getClaudeCliVersion, getProjectEvalDir, isFinalizedEvalResultFile } from '../test/helpers/eval-store';
import { parseCliFlags } from './lib/shard-engine';
import { collectPaidTestFiles, paidSelectionEnv } from './lib/paid-select';
import { DEFAULT_JOBS } from './lib/paid-types';
import { privateFreeHome } from './lib/free-home-guard';
import {
FAILURE_CLASSES, barThresholds, classificationProblem, decide, identityMismatch,
type BarDecision, type BarNext, type BarThresholds, type Classification, type FailureClass, type SetSummary, type TrialSet,
} from './lib/measure-bar';
import harnessManifest from './harness-version.json';
import {
TREE_MUTATING, assignFilesToShards, collectFreeTestFiles, fullSuiteJobs, loadFreeTestDurations, packShardsByDuration,
runFreeShard, wallTimeoutForShard, type FreeShardOutcome,
} from './test-free-shards';
const ROOT = path.resolve(import.meta.dir, '..');
export type MeasureKind = 'rule' | 'behavior' | 'judge';
export type RerunBackend = 'local' | 'ubicloud';
export const PANEL = EVAL_POLICY.panel;
export const JUDGE_SAMPLES = EVAL_POLICY.judge.samples;
export interface MeasureConfig {
ruleTrials: number; behaviorPanels: number; judgeOutputs: number;
askPerTrialUsd: number; budgetUsd: number; maxRounds: number; rerunBackend: RerunBackend;
sweepCases: number; sweepBudgetUsd: number;
}
export const MEASURE_DEFAULTS: MeasureConfig = {
ruleTrials: 10, behaviorPanels: 4, judgeOutputs: 10, askPerTrialUsd: 2, budgetUsd: 25, maxRounds: 3, rerunBackend: 'local',
sweepCases: 5, sweepBudgetUsd: 150,
};
const USD_FIELDS = new Set<keyof MeasureConfig>(['askPerTrialUsd', 'budgetUsd', 'sweepBudgetUsd']);
function gstackConfigGet(key: string): string | undefined {
const r = spawnSync(path.join(ROOT, 'bin', 'gstack-config'), ['get', key], { encoding: 'utf8', timeout: 10_000 });
return r.status === 0 ? r.stdout.trim() : undefined;
}
/** The measure controls from gstack config; an empty value takes the default, an invalid one throws. */
export function readMeasureConfig(configGet: (key: string) => string | undefined = gstackConfigGet): MeasureConfig {
const raw: Array<[keyof MeasureConfig, string, string | undefined]> = [
['ruleTrials', 'ship_measure_rule_trials', configGet('ship_measure_rule_trials')],
['behaviorPanels', 'ship_measure_behavior_panels', configGet('ship_measure_behavior_panels')],
['judgeOutputs', 'ship_measure_judge_outputs', configGet('ship_measure_judge_outputs')],
['askPerTrialUsd', 'ship_measure_ask_per_trial_usd', configGet('ship_measure_ask_per_trial_usd')],
['budgetUsd', 'ship_measure_budget_usd', configGet('ship_measure_budget_usd')],
['maxRounds', 'ship_measure_max_rounds', configGet('ship_measure_max_rounds')],
['rerunBackend', 'ship_rerun_backend', configGet('ship_rerun_backend')],
['sweepCases', 'ship_measure_sweep_cases', configGet('ship_measure_sweep_cases')],
['sweepBudgetUsd', 'ship_measure_sweep_budget_usd', configGet('ship_measure_sweep_budget_usd')],
];
const config = { ...MEASURE_DEFAULTS };
for (const [field, key, rawValue] of raw) {
const value = rawValue?.trim();
if (!value) continue;
if (field === 'rerunBackend') {
if (value !== 'local' && value !== 'ubicloud') throw new Error(`${key} '${value}' is not local or ubicloud. Fix: gstack-config set ${key} local`);
config.rerunBackend = value;
continue;
}
const usd = USD_FIELDS.has(field);
if (!(usd ? Number.isFinite(Number(value)) && Number(value) > 0 : /^[1-9][0-9]*$/.test(value))) {
throw new Error(`${key} '${value}' is not a ${usd ? 'positive amount in USD' : 'positive integer'}. Fix: gstack-config set ${key} ${MEASURE_DEFAULTS[field]}`);
}
(config as Record<string, unknown>)[field] = Number(value);
}
return config;
}
export interface KindPlan {
kind: MeasureKind;
/** Behavior trials launch in panels of 3; rule and judge trials one at a time. */
trialsPerUnit: number;
/** Trials per batch (outputs for judge). */
trials: number;
bar: BarThresholds;
pooledBar: BarThresholds;
}
export function kindPlan(kind: MeasureKind, config: MeasureConfig): KindPlan {
const trials = kind === 'behavior' ? config.behaviorPanels * PANEL.n : kind === 'judge' ? config.judgeOutputs : config.ruleTrials;
return { kind, trialsPerUnit: kind === 'behavior' ? PANEL.n : 1, trials, bar: barThresholds(trials), pooledBar: barThresholds(2 * trials) };
}
/** The per-kind table /ship prints before it runs anything. */
export function formatKindTable(config: MeasureConfig): string {
const budget = `$${config.budgetUsd} total; asks above $${config.askPerTrialUsd}/trial or with no estimate`;
const row = (kind: MeasureKind, label: string) => {
const { trials: n, bar, pooledBar } = kindPlan(kind, config);
const unit = kind === 'judge' ? ' outputs' : '';
return `| ${kind} | ${label} | ${bar.strict} of ${n}${unit} | ${bar.qualified} of ${n}, every red qualifying | ${bar.floor} of ${n} (or ${bar.qualified} that does not qualify): ${n} more on identical inputs, then ${pooledBar.strict} of ${2 * n}, or ${pooledBar.qualified} of ${2 * n} qualified | ${budget} |`;
};
return [
'| Kind | Trials per batch | MEETS | MEETS-qualified | EXTEND, then the pooled decision | Budget per red case |',
'|---|---|---|---|---|---|',
row('rule', String(config.ruleTrials)),
row('behavior', `${config.behaviorPanels * PANEL.n} (${config.behaviorPanels} panels of ${PANEL.n})`),
row('judge', `${config.judgeOutputs} outputs, each its median ${JUDGE_SAMPLES}-sample panel`),
'',
'Qualifying reds: provider (affirmative provider or transport evidence only), judge noise at the threshold, a model miss citing its evidence.',
'Never qualifying (a fix round): a timeout, a hang, a regression, a contract violation, any known fixable cause. Below the EXTEND floor: BELOW, a fix round.',
'A batch with more than 30% of trials failing on provider evidence is void and is redispatched once; both batches are reported. There is never a third batch.',
`Repair rounds: at most ${config.maxRounds}. Free-suite rerun backend: ${config.rerunBackend}. Every trial is diagnostic and never changes a recorded verdict.`,
].join('\n');
}
// ─── Trials ────────────────────────────────────────────────────────────────
export interface TrialResult {
passed: boolean;
/** A contract violation fails the measurement at any count. */
contract?: boolean;
/** Billed cost; absent means unknown. */
costUsd?: number;
/** Judge outputs: each sample's pass. */
samples?: boolean[];
failureCause?: string;
failureDetail?: string;
/** The eval store's one-line failure_cause_evidence. */
failureEvidence?: string;
}
export interface TrialRequest { caseId: string; kind: MeasureKind; round: string; trial: number; dir: string }
export type TrialRunner = (request: TrialRequest) => Promise<TrialResult>;
export interface TrialRecord extends TrialResult { trial: number; set: TrialSet; unit: number; batch: number; dir: string }
// ─── One measurement: baseline or a repair round ───────────────────────────
export type MeasureStatus = 'measured' | 'needs_approval' | 'budget_exhausted' | 'round_limit';
export interface Measurement {
schema: 'gstack-ship-measure/2';
label: 'diagnostic';
verdict: null;
case: string;
kind: MeasureKind;
round: string;
fix?: string;
status: MeasureStatus;
reason?: string;
plan: KindPlan;
/** Inputs every pooled batch must share: tree, runner, CLI, model, image, policy and harness versions. */
identity: Record<string, string>;
trials: TrialRecord[];
decision: BarDecision;
next: BarNext;
decisionReason: string;
passes: number;
counted: number;
sets: SetSummary[];
/** The per-trial estimate later batches reserve; null until known. */
costPerTrialUsd: number | null;
/** Reserved before admission; null when a batch ran without an estimate. */
estimatedUsd: number | null;
actualUsd: number;
costUnknownTrials: number;
parallel: number;
}
interface Ledger { case: string; kind: MeasureKind; approved: boolean; spentUsd: number; rounds: string[] }
/** A spend cap shared across cases (the sweep's weekly cap), checked beside each case's own budget. */
export interface SpendPool { label: string; capUsd: number; spentUsd: number }
export interface MeasureOptions {
caseId: string;
kind: MeasureKind;
round: string;
config: MeasureConfig;
runner: TrialRunner;
outDir: string;
parallel: number;
costPerTrialUsd: number | null;
approved: boolean;
fix?: string;
/** Pass-rate history directory diagnostic artifacts must stay out of (default: the project eval dir). */
evalDir?: string;
/** The batch identity (default: measurementIdentity of the working tree). */
identity?: () => Record<string, string>;
pool?: SpendPool;
log?: (line: string) => void;
}
export const caseSlug = (id: string) => id.replace(/[^A-Za-z0-9._-]+/g, '_');
const readJson = <T>(file: string): T | null => { try { return JSON.parse(fs.readFileSync(file, 'utf8')) as T; } catch { return null; } };
const writeJson = (file: string, value: unknown) => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`); };
const inside = (child: string, parent: string) => { const rel = path.relative(path.resolve(parent), path.resolve(child)); return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel)); };
/** Throws when diagnostic artifacts would land where verdict history is read. */
export function assertDiagnosticDir(outDir: string, evalDir: string): void {
if (inside(outDir, evalDir)) throw new Error(`ship-measure: ${outDir} is inside the eval history dir ${evalDir}; diagnostic trials are never recorded as verdicts. Use the default .context/ship-measure.`);
}
function roundNumber(round: string): number {
if (round === 'baseline') return 0;
const match = /^round-([1-9][0-9]*)$/.exec(round);
if (!match) throw new Error(`--round must be baseline or round-N. Received: ${round}`);
return Number(match[1]);
}
/** The git tree of the working tree as it would be committed now (tracked and unignored files), without touching the real index. */
export function frozenTree(cwd: string): string {
const git = (args: string[], env: NodeJS.ProcessEnv = process.env) => spawnSync('git', args, { cwd, env, encoding: 'utf8', timeout: 120_000, maxBuffer: 64 * 1024 * 1024 });
const top = git(['rev-parse', '--show-toplevel']);
if (top.status !== 0) return 'no-git';
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ship-measure-index-'));
try {
const index = path.resolve(cwd, git(['rev-parse', '--git-path', 'index']).stdout.trim());
if (fs.existsSync(index)) fs.copyFileSync(index, path.join(tmp, 'index'));
const env = { ...process.env, GIT_INDEX_FILE: path.join(tmp, 'index') };
const add = git(['-C', top.stdout.trim(), 'add', '-A'], env);
const tree = add.status === 0 ? git(['write-tree'], env) : add;
if (tree.status !== 0) throw new Error(`ship-measure: could not snapshot the working tree: ${tree.stderr.trim()}`);
return tree.stdout.trim();
} finally { fs.rmSync(tmp, { recursive: true, force: true }); }
}
/** Everything a pooled batch must share with the batch it extends. */
export function measurementIdentity(kind: MeasureKind, trials: number, runner: string, cwd = process.cwd(), env: NodeJS.ProcessEnv = process.env): Record<string, string> {
return {
tree: frozenTree(cwd), kind, trials: String(trials), runner, policy: String(EVAL_POLICY.version), harness: String(harnessManifest.version),
claude_cli: getClaudeCliVersion(), model: env.EVALS_MODEL ?? 'default', image: env.GSTACK_CI_IMAGE ?? 'local', bun: Bun.version,
};
}
const roundDirOf = (outDir: string, caseId: string, round: string) => path.join(outDir, caseSlug(caseId), round);
const classificationsPath = (roundDir: string) => path.join(roundDir, 'classifications.jsonl');
export function readClassifications(roundDir: string): Classification[] {
if (!fs.existsSync(classificationsPath(roundDir))) return [];
return fs.readFileSync(classificationsPath(roundDir), 'utf8').split('\n').filter(Boolean).map(line => JSON.parse(line) as Classification);
}
/** Apply the bar to the measurement's trials and its classification records. */
function withDecision(m: Measurement, roundDir: string): Measurement {
const outcome = decide(m.kind, m.plan.trials, m.trials, readClassifications(roundDir));
return { ...m, decision: outcome.decision, next: outcome.next, decisionReason: outcome.reason, passes: outcome.passes, counted: outcome.thresholds.trials, sets: outcome.sets };
}
function loadMeasurement(outDir: string, caseId: string, round: string): { m: Measurement; roundDir: string } {
const roundDir = roundDirOf(outDir, caseId, round);
const m = readJson<Measurement>(path.join(roundDir, 'measurement.json'));
if (!m) throw new Error(`ship-measure: no measurement at ${roundDir}; run measure --case ${caseId} --round ${round} first`);
return { m, roundDir };
}
function saveMeasurement(m: Measurement, roundDir: string, log: (line: string) => void): Measurement {
const decided = withDecision(m, roundDir);
writeJson(path.join(roundDir, 'measurement.json'), decided);
log(formatMeasurementLine(decided));
return decided;
}
export async function measureCase(o: MeasureOptions): Promise<Measurement> {
const log = o.log ?? ((line: string) => console.log(line));
const plan = kindPlan(o.kind, o.config);
assertDiagnosticDir(o.outDir, o.evalDir ?? getProjectEvalDir());
const caseDir = path.join(o.outDir, caseSlug(o.caseId));
const ledgerPath = path.join(caseDir, 'ledger.json');
const ledger: Ledger = readJson<Ledger>(ledgerPath) ?? { case: o.caseId, kind: o.kind, approved: false, spentUsd: 0, rounds: [] };
const base: Measurement = {
schema: 'gstack-ship-measure/2', label: 'diagnostic', verdict: null, case: o.caseId, kind: o.kind, round: o.round,
...(o.fix ? { fix: o.fix } : {}), status: 'measured', plan, identity: {}, trials: [], decision: 'incomplete', next: 'stop', decisionReason: '',
passes: 0, counted: 0, sets: [], costPerTrialUsd: o.costPerTrialUsd, estimatedUsd: 0, actualUsd: 0, costUnknownTrials: 0, parallel: Math.max(1, o.parallel),
};
if (roundNumber(o.round) > o.config.maxRounds) {
return { ...base, status: 'round_limit', reason: `repair-round limit ${o.config.maxRounds} reached; stop with the named-red report` };
}
const estimate = o.costPerTrialUsd;
const ask = estimate === null ? 'estimate unknown' : estimate > o.config.askPerTrialUsd
? `estimated $${estimate.toFixed(2)}/trial is above $${o.config.askPerTrialUsd}/trial` : null;
if (ask && !o.approved && !ledger.approved) {
return { ...base, status: 'needs_approval', reason: `${ask}; ${plan.trials} trials, budget $${o.config.budgetUsd} per red case. Ask once, then rerun with --approved.` };
}
if (ask) ledger.approved = true;
const roundDir = path.join(caseDir, o.round);
if (fs.existsSync(roundDir)) throw new Error(`ship-measure: ${roundDir} already exists; measurements are never overwritten. Use the next round.`);
fs.mkdirSync(roundDir, { recursive: true });
let m: Measurement = { ...base, identity: (o.identity ?? (() => measurementIdentity(o.kind, plan.trials, 'gstack')))() };
m = withDecision(await runSet(o, m, 'initial', roundDir, ledger, log), roundDir);
if (m.status === 'measured' && m.next === 'redispatch') {
log(`[ship-measure] ${o.caseId} ${o.round}: ${m.decisionReason}`);
m = await runSet(o, m, 'redispatch', roundDir, ledger, log);
}
ledger.rounds.push(o.round);
writeJson(ledgerPath, ledger);
return saveMeasurement(m, roundDir, log);
}
export interface ExtendOptions extends Omit<MeasureOptions, 'kind' | 'fix' | 'approved' | 'costPerTrialUsd'> { costPerTrialUsd?: number | null }
/** EXTEND: N more trials on identical inputs (the identity must match field for field), decided once on the pooled 2N. */
export async function extendCase(o: ExtendOptions): Promise<Measurement> {
const log = o.log ?? ((line: string) => console.log(line));
assertDiagnosticDir(o.outDir, o.evalDir ?? getProjectEvalDir());
let { m, roundDir } = loadMeasurement(o.outDir, o.caseId, o.round);
m = withDecision(m, roundDir);
if (m.trials.some(t => t.set === 'extend')) throw new Error(`ship-measure: ${o.caseId} ${o.round} was already extended; there is never a third batch`);
if (m.decision !== 'EXTEND') throw new Error(`ship-measure: ${o.caseId} ${o.round} is ${m.decision}, not EXTEND (${m.decisionReason})`);
const now = (o.identity ?? (() => measurementIdentity(m.kind, m.plan.trials, m.identity.runner ?? 'gstack')))();
const changed = identityMismatch(m.identity, now);
if (changed) throw new Error(`ship-measure: refusing to pool ${o.caseId} ${o.round}: ${changed}. EXTEND needs identical inputs; measure the changed tree as a new round.`);
const ledgerPath = path.join(o.outDir, caseSlug(o.caseId), 'ledger.json');
const ledger = readJson<Ledger>(ledgerPath) ?? { case: o.caseId, kind: m.kind, approved: true, spentUsd: m.actualUsd, rounds: [o.round] };
const opts: MeasureOptions = { ...o, kind: m.kind, approved: true, costPerTrialUsd: o.costPerTrialUsd ?? m.costPerTrialUsd };
m = withDecision(await runSet(opts, { ...m, status: 'measured' }, 'extend', roundDir, ledger, log), roundDir);
if (m.status === 'measured' && m.next === 'redispatch') {
log(`[ship-measure] ${o.caseId} ${o.round}: ${m.decisionReason}`);
m = await runSet(opts, m, 'extend-redispatch', roundDir, ledger, log);
}
writeJson(ledgerPath, ledger);
return saveMeasurement(m, roundDir, log);
}
/** Append one immutable classification record per trial (a later record supersedes, never edits), then re-decide. */
export function classifyTrials(outDir: string, caseId: string, round: string, trials: number[], cls: string, evidence: string, log = (line: string) => console.log(line)): Measurement {
const { m, roundDir } = loadMeasurement(outDir, caseId, round);
for (const trial of trials) {
const problem = classificationProblem(m.trials.find(t => t.trial === trial), m.kind, cls, evidence);
if (problem) throw new Error(`ship-measure: classify t${trial} refused: ${problem}`);
}
const at = new Date().toISOString();
fs.appendFileSync(classificationsPath(roundDir), trials.map(trial => `${JSON.stringify({ trial, class: cls as FailureClass, evidence: evidence.trim(), by: 'agent', at } satisfies Classification)}\n`).join(''));
return saveMeasurement(m, roundDir, log);
}
export function decideRound(outDir: string, caseId: string, round: string, log = (line: string) => console.log(line)): Measurement {
const { m, roundDir } = loadMeasurement(outDir, caseId, round);
return saveMeasurement(m, roundDir, log);
}
/** Run one set of N trials (initial, redispatch, extend or extend-redispatch) under the admission budget. */
async function runSet(o: MeasureOptions, m: Measurement, set: TrialSet, roundDir: string, ledger: Ledger, log: (line: string) => void): Promise<Measurement> {
const plan = m.plan;
const first = m.trials.length;
const trials: TrialRecord[] = [];
const initialEstimate = m.costPerTrialUsd;
let estimate = initialEstimate;
let estimatedUsd = m.estimatedUsd;
let actualUsd = m.actualUsd;
let costUnknownTrials = m.costUnknownTrials;
let knownCosts = m.trials.filter(t => typeof t.costUsd === 'number').length;
let batch = 0;
const parallel = Math.max(1, o.parallel);
const done = (status: MeasureStatus, reason?: string): Measurement => ({
...m, status, ...(reason ? { reason } : {}), trials: [...m.trials, ...trials], estimatedUsd, actualUsd, costUnknownTrials, parallel,
costPerTrialUsd: estimate,
});
while (trials.length < plan.trials) {
const remaining = plan.trials - trials.length;
const headroom = Math.min(o.config.budgetUsd - ledger.spentUsd, o.pool ? o.pool.capUsd - o.pool.spentUsd : Infinity);
let size = Math.min(parallel, remaining);
if (estimate === null) size = knownCosts === 0 && trials.length === 0 ? 1 : size;
else size = Math.min(size, Math.floor((headroom + 1e-9) / estimate));
if (size < 1 || headroom <= 0) {
const cap = o.pool && o.pool.capUsd - o.pool.spentUsd <= o.config.budgetUsd - ledger.spentUsd
? `${o.pool.label} $${o.pool.capUsd}: spent $${o.pool.spentUsd.toFixed(2)}`
: `budget $${o.config.budgetUsd} per red case: spent $${ledger.spentUsd.toFixed(2)}`;
return done('budget_exhausted', `${cap}, next trial reserves $${(estimate ?? 0).toFixed(2)}; ${trials.length} of ${plan.trials} ${set} trials ran`);
}
batch += 1;
estimatedUsd = estimate === null || estimatedUsd === null ? null : estimatedUsd + size * estimate;
log(`[ship-measure] ${o.caseId} ${o.round} ${set}: batch ${batch} admits ${size} trial(s)${estimate === null ? ' (calibration, no estimate)' : `, reserves $${(size * estimate).toFixed(2)}`}; spent $${ledger.spentUsd.toFixed(2)} of $${o.config.budgetUsd}`);
const start = first + trials.length;
const results = await Promise.all(Array.from({ length: size }, async (_, i) => {
const trial = start + i + 1;
const dir = path.join(roundDir, `t${String(trial).padStart(2, '0')}`);
fs.mkdirSync(dir, { recursive: false });
let result: TrialResult;
try { result = await o.runner({ caseId: o.caseId, kind: m.kind, round: o.round, trial, dir }); }
catch (error) { result = { passed: false, failureCause: 'unknown', failureDetail: `runner error: ${(error as Error).message}` }; }
return { ...result, trial, set, unit: Math.ceil((trial - first) / plan.trialsPerUnit), batch, dir } satisfies TrialRecord;
}));
for (const record of results) {
writeJson(path.join(record.dir, 'trial.json'), { label: 'diagnostic', verdict: null, ...record });
trials.push(record);
const charge = typeof record.costUsd === 'number' && Number.isFinite(record.costUsd) ? record.costUsd : estimate;
if (typeof record.costUsd === 'number' && Number.isFinite(record.costUsd)) { actualUsd += record.costUsd; knownCosts += 1; }
else costUnknownTrials += 1;
if (charge !== null) { ledger.spentUsd += charge; if (o.pool) o.pool.spentUsd += charge; }
}
if (initialEstimate === null && knownCosts > 0) estimate = actualUsd / knownCosts;
}
return done('measured');
}
const DECISION_WORDS: Record<BarDecision, string> = {
MEETS: 'MEETS', 'MEETS-qualified': 'MEETS-qualified', EXTEND: 'EXTEND', BELOW: 'BELOW (fix round)',
'needs-classify': 'needs-classify', void: 'void', incomplete: 'incomplete',
};
export function formatMeasurementLine(m: Measurement): string {
const when = m.fix ? ` after fix at ${m.fix}` : m.round === 'baseline' ? ' (baseline)' : '';
const verdict = m.status === 'measured' ? DECISION_WORDS[m.decision] : m.status.replace('_', ' ');
const sets = m.sets.length > 1 ? `; batches ${m.sets.map(s => `${s.set} ${s.passes}/${s.trials}${s.void ? ' void' : ''}`).join(', ')}` : '';
return `[ship-measure] DIAGNOSTIC ${m.case} ${m.round}: observed ${m.passes}/${m.counted} ${m.kind === 'judge' ? 'outputs' : 'trials'}${when}; ${verdict}${m.status === 'measured' ? ` — ${m.decisionReason}` : m.reason ? ` — ${m.reason}` : ''}${sets}; est ${usd(m.estimatedUsd)}, actual ${usd(m.actualUsd)}${m.costUnknownTrials ? ` (+${m.costUnknownTrials} trial(s) cost unknown)` : ''}`;
}
const usd = (value: number | null) => value === null ? 'unknown' : `$${value.toFixed(2)}`;
/** measure/extend/classify/decide exit code for a measurement (see the header). */
export function measurementExit(m: Measurement): number {
if (m.status === 'needs_approval') return 2;
if (m.status !== 'measured') return 3;
return ({ MEETS: 0, 'MEETS-qualified': 0, BELOW: 1, 'needs-classify': 5, EXTEND: 6, void: 3, incomplete: 3 } as const)[m.decision];
}
// ─── Unmeasured skip and the PR-body report ────────────────────────────────
export function recordUnmeasured(outDir: string, caseId: string, reason: string): string {
if (!reason.trim()) throw new Error('skip needs --reason: why this red is infrastructure and is not measured');
const file = path.join(outDir, caseSlug(caseId), 'unmeasured.json');
writeJson(file, { schema: 'gstack-ship-measure/2', label: 'diagnostic', verdict: null, case: caseId, status: 'unmeasured', reason: reason.trim() });
return file;
}
/** The per-case table for the PR body: every measurement, estimated and actual spend, never a pass for an unmeasured case. */
export function formatReport(outDir: string): string {
const rows = ['| Case | Kind | Measurement | Observed | Bar | Decision | Est. spend | Actual spend |', '|---|---|---|---|---|---|---|---|'];
const cases = fs.existsSync(outDir) ? fs.readdirSync(outDir).filter(name => fs.statSync(path.join(outDir, name)).isDirectory()).sort() : [];
const qualified: string[] = [];
for (const name of cases) {
const dir = path.join(outDir, name);
const skip = readJson<{ case: string; reason: string }>(path.join(dir, 'unmeasured.json'));
if (skip) { rows.push(`| ${skip.case} | — | — | — | — | unmeasured (${skip.reason.replace(/\|/g, '/')}); not a pass | — | — |`); continue; }
const ledger = readJson<Ledger>(path.join(dir, 'ledger.json'));
for (const round of ledger?.rounds ?? []) {
const m = readJson<Measurement>(path.join(dir, round, 'measurement.json'));
if (!m) continue;
if (m.schema !== 'gstack-ship-measure/2') {
rows.push(`| ${m.case} | ${m.kind ?? '—'} | ${round} | — | — | older measurement format (${String(m.schema).replace(/\|/g, '/')}); not read, re-measure on this release | — | — |`);
continue;
}
const bar = m.counted > m.plan.trials ? m.plan.pooledBar : m.plan.bar;
const what = m.fix ? `${m.round}: after fix at ${m.fix.replace(/\|/g, '/')}` : m.round;
const batches = m.sets.length > 1 ? ` (${m.sets.map(s => `${s.set} ${s.passes}/${s.trials}${s.void ? ' void' : ''}`).join(', ')})` : '';
const decision = m.status === 'measured' ? DECISION_WORDS[m.decision] : m.status.replace('_', ' ');
rows.push(`| ${m.case} | ${m.kind} | ${what} | observed ${m.passes}/${m.counted}${batches} | ${bar.strict}/${bar.trials} strict, ${bar.qualified}/${bar.trials} qualified | ${decision} | ${usd(m.estimatedUsd)} | ${usd(m.actualUsd)}${m.costUnknownTrials ? ` (${m.costUnknownTrials} unknown)` : ''} |`);
if (m.status === 'measured' && m.decision === 'MEETS-qualified') qualified.push(`- ${m.case} ${m.round}: ${m.decisionReason.replace(/\|/g, '/')}`);
}
}
return [...rows, ...(qualified.length ? ['', 'Qualified MEETS (every red classified into a qualifying class):', ...qualified] : []),
'', 'Diagnostic measurements: they never change a recorded verdict; the lane verdict is the full gate run.'].join('\n');
}
// ─── Trial runners ─────────────────────────────────────────────────────────
const TRIAL_TIMEOUT_MS = 60 * 60_000;
/** Cost, failure fields and judge records a trial's eval dir holds (any finalized eval-store result under it). */
export function readTrialRecords(evalDir: string): Array<Record<string, any>> {
if (!fs.existsSync(evalDir)) return [];
return (fs.readdirSync(evalDir, { recursive: true }) as string[]).filter(isFinalizedEvalResultFile)
.flatMap(name => readJson<{ tests?: unknown[] }>(path.join(evalDir, name))?.tests ?? [])
.filter((t): t is Record<string, any> => !!t && typeof t === 'object');
}
/**
* No-cost proof that gstack's runner selects the case before any paid trial:
* `--list` must name the case's own test file. A selection bug otherwise
* shows up only after money is spent (two such bugs were found in #3033).
*/
export function caseSelectionPreflight(caseId: string, rootDir = ROOT): { ok: boolean; detail: string } {
const isJudge = !Object.hasOwn(E2E_TIERS, caseId);
if (isJudge) {
if (!Object.hasOwn(LLM_JUDGE_TOUCHFILES, caseId)) return { ok: false, detail: `${caseId} is neither an E2E case nor a standalone judge` };
const file = judgeFile(caseId, rootDir);
return fs.existsSync(path.join(rootDir, file)) ? { ok: true, detail: `judge ${caseId} runs from ${file}` } : { ok: false, detail: `judge file ${file} is missing` };
}
const r = spawnSync(process.execPath, ['run', path.join(rootDir, 'scripts/test-paid-shards.ts'), '--tier', E2E_TIERS[caseId]!, '--case', caseId, '--trials', '1', '--list'],
{ cwd: rootDir, encoding: 'utf8', timeout: 60_000 });
const out = `${r.stdout ?? ''}${r.stderr ?? ''}`;
if (r.status !== 0) return { ok: false, detail: `--list exited ${r.status}: ${out.trim().split('\n').slice(-1)[0] ?? ''}` };
const file = new RegExp(`--case ${caseId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}: \\d+ trial\\(s\\) of (\\S+)`).exec(out)?.[1];
const esc = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// A file-mode case lists `<file> (trial 1)`; a name-mode case lists its trial shard `<file>#<id>~t1`.
const listed = file && new RegExp(`^\\s+${esc(file)}(?: \\(trial 1\\)|#${esc(caseId)}~t1)$`, 'm').test(out);
if (!file || !listed || !fs.existsSync(path.join(rootDir, file))) return { ok: false, detail: `--list did not plan a trial of ${caseId}'s test file` };
return { ok: true, detail: `--case ${caseId} selects ${file}` };
}
/**
* The environment every diagnostic trial starts from. Trials always run fresh
* and never publish: with the judge input cache enabled, a trial could reuse a
* stored pass, and its own passes would flow into the gate's reuse.
*/
export function diagnosticBaseEnv(base: NodeJS.ProcessEnv, evalDir: string, trialId: string): NodeJS.ProcessEnv {
const { EVALS_CACHE_DIR: _cacheDir, EVALS_CACHE_RUNTIME_ID: _cacheRuntime, ...inherited } = base;
// Cases that retain native evidence (functional QA, docs faults) require EVALS_RUN_ID, which only CI sets;
// each trial gets its own so parallel trials never share an evidence directory.
return { ...inherited, GSTACK_EVAL_DIR: evalDir, GSTACK_SHIP_MEASURE_LABEL: 'diagnostic', EVALS_JOBS: '1', EVALS_RUN_ID: `${base.EVALS_RUN_ID || 'local'}-measure-${trialId}` };
}
/** True when some junit.xml under evalDir holds an executed, passing testcase and no failed or errored one. */
export function junitExecuted(evalDir: string): boolean {
if (!fs.existsSync(evalDir)) return false;
let executed = 0;
for (const name of fs.readdirSync(evalDir, { recursive: true }) as string[]) {
if (path.basename(name) !== 'junit.xml') continue;
const xml = fs.readFileSync(path.join(evalDir, name), 'utf8');
for (const m of xml.matchAll(/<testcase\b[^>]*?(?:\/>|>([\s\S]*?)<\/testcase>)/g)) {
const body = m[1] ?? '';
if (/<(?:failure|error)\b/.test(body)) return false;
if (!/<skipped\b/.test(body)) executed++;
}
}
return executed > 0;
}
function costOf(records: Array<Record<string, any>>): number | undefined {
if (!records.length || records.some(r => r.cost_known === false || typeof r.cost_usd !== 'number')) return undefined;
return records.reduce((sum, r) => sum + r.cost_usd, 0);
}
/** Runs argv as one trial in its own process group, logging to <dir>/output.log; returns exit code and text. */
async function runTrialProcess(argv: string[], cwd: string, env: NodeJS.ProcessEnv, dir: string, timeoutMs = TRIAL_TIMEOUT_MS): Promise<{ code: number | null; output: string }> {
const logFile = fs.createWriteStream(path.join(dir, 'output.log'));
const chunks: string[] = [];
const child = spawn(argv[0]!, argv.slice(1), { cwd, env, stdio: ['ignore', 'pipe', 'pipe'], detached: process.platform !== 'win32' });
for (const stream of [child.stdout, child.stderr]) stream?.on('data', (chunk: Buffer) => { chunks.push(chunk.toString('utf8')); logFile.write(chunk); });
const timer = setTimeout(() => { try { process.kill(-(child.pid ?? 0), 'SIGKILL'); } catch { child.kill('SIGKILL'); } }, timeoutMs);
const code = await new Promise<number | null>(resolve => { child.on('error', () => resolve(null)); child.on('close', resolve); });
clearTimeout(timer);
await new Promise<void>(resolve => logFile.end(resolve));
return { code, output: chunks.join('') };
}
function parseTrialOutput(output: string): Partial<TrialResult> {
const field = (name: string) => new RegExp(`^\\s*${name}\\s*[:=]\\s*(.+?)\\s*$`, 'm').exec(output)?.[1];
const cost = field('cost_usd');
const samples = field('samples');
return {
...(field('failure_cause') ? { failureCause: field('failure_cause') } : {}),
...(field('failure_detail') ? { failureDetail: field('failure_detail') } : {}),
...(field('failure_evidence') ? { failureEvidence: field('failure_evidence') } : {}),
...(cost !== undefined && Number.isFinite(Number(cost)) ? { costUsd: Number(cost) } : {}),
...(samples ? { samples: samples.split(/[,\s]+/).filter(Boolean).map(s => s === '1' || s === 'pass' || s === 'true') } : {}),
...(/^\s*contract_violation\b/m.test(output) ? { contract: true } : {}),
};
}
/**
* The project's documented single-case command, split on whitespace (no
* shell), with {case} replaced by the case id. Exit 0 is a pass; the trial's
* own lines `cost_usd:`, `failure_cause:`, `failure_detail:`, `samples:` and
* `contract_violation` are read, and so are eval-store records it writes to
* GSTACK_EVAL_DIR.
*/
export function commandRunner(template: string, cwd = process.cwd()): TrialRunner {
const words = template.trim().split(/\s+/).filter(Boolean);
if (!words.length) throw new Error('--command is empty');
return async ({ caseId, trial, dir }) => {
const env = { ...process.env, GSTACK_EVAL_DIR: path.join(dir, 'eval'), GSTACK_SHIP_MEASURE_DIR: dir, GSTACK_SHIP_MEASURE_LABEL: 'diagnostic', GSTACK_SHIP_MEASURE_TRIAL: String(trial) };
const { code, output } = await runTrialProcess(words.map(w => w.replaceAll('{case}', caseId)), cwd, env, dir);
const parsed = parseTrialOutput(output);
const records = readTrialRecords(path.join(dir, 'eval'));
const failed = records.find(r => r.passed !== true);
return {
passed: code === 0, ...parsed,
...(parsed.costUsd === undefined && costOf(records) !== undefined ? { costUsd: costOf(records) } : {}),
...(code !== 0 && !parsed.failureCause && failed?.failure_cause ? { failureCause: String(failed.failure_cause) } : {}),
...(code !== 0 && !parsed.failureDetail && failed?.failure_detail ? { failureDetail: JSON.stringify(failed.failure_detail).slice(0, 300) } : {}),
...(code !== 0 && !parsed.failureEvidence && failed?.failure_cause_evidence ? { failureEvidence: String(failed.failure_cause_evidence).slice(0, 300) } : {}),
};
};
}
/** The one paid file that names a standalone judge id, else a thrown reason. */
export function judgeFile(id: string, rootDir = ROOT): string {
const owners = collectPaidTestFiles(rootDir).filter(file => {
const source = fs.readFileSync(path.join(rootDir, file), 'utf8');
return source.includes(`'${id}'`) || source.includes(`"${id}"`);
});
if (owners.length !== 1) throw new Error(`judge ${id}: ${owners.length ? `named by ${owners.join(', ')}` : 'no paid file names it'}; it needs exactly one`);
return owners[0]!;
}
/**
* gstack's default runner. An E2E case runs one trial through
* `scripts/test-paid-shards.ts --case <id> --trials 1` (the CI panel runner,
* whose exit code is that trial's verdict); a standalone judge runs its file
* with the judge selected alone and passes only on its own passing record.
*/
export function gstackRunner(rootDir = ROOT): TrialRunner {
return async ({ caseId, round, trial, dir }) => {
const evalDir = path.join(dir, 'eval');
const isJudge = !Object.hasOwn(E2E_TIERS, caseId);
if (isJudge && !Object.hasOwn(LLM_JUDGE_TOUCHFILES, caseId)) throw new Error(`${caseId} is neither an E2E case nor a standalone judge`);
const argv = isJudge
? [process.execPath, 'test', path.join(rootDir, judgeFile(caseId, rootDir))]
: [process.execPath, 'run', path.join(rootDir, 'scripts/test-paid-shards.ts'), '--tier', E2E_TIERS[caseId]!, '--case', caseId, '--trials', '1'];
const env = {
...diagnosticBaseEnv(process.env, evalDir, `${caseSlug(caseId)}-${round}-t${trial}-${process.pid}`),
...(isJudge ? { EVALS: '1', EVALS_TIER: 'gate', EVALS_ALL: '1', ...paidSelectionEnv('full', { e2e: [], judges: [caseId] }, 'ship-measure judge') } : {}),
};
const { code, output } = await runTrialProcess(argv, rootDir, env, dir);
const records = readTrialRecords(evalDir);
const own = isJudge ? records.filter(r => r.name === caseId || r.case_id === caseId) : records;
const failed = own.find(r => r.passed !== true);
// An E2E trial passes only with proof the case ran: a JUnit testcase that
// executed and passed (a --case that selected nothing also exits 0).
const passed = code === 0 && (isJudge ? own.length > 0 && !failed : junitExecuted(evalDir));
const cost = costOf(own);
return {
passed, ...(cost !== undefined ? { costUsd: cost } : {}),
...(own.some(r => r.failure_class === 'contract') ? { contract: true } : {}),
...(passed ? {} : {
failureCause: failed?.failure_cause ? String(failed.failure_cause) : own.length ? 'assertion' : 'unknown',
...(failed?.failure_cause_evidence ? { failureEvidence: String(failed.failure_cause_evidence).slice(0, 300) } : {}),
failureDetail: failed?.failure_detail ? JSON.stringify(failed.failure_detail).slice(0, 300)
: own.length ? String(failed?.error ?? '').slice(0, 300) : `no trial record; exit ${code}; ${output.trim().split('\n').slice(-1)[0] ?? ''}`.slice(0, 300),
}),
};
};
}
// ─── Free-suite shard reruns (CEO-1) ───────────────────────────────────────
export interface FreeRerunOutcome { rerun: number; mode: 'baseline' | 'parallel'; status: string; completed: boolean; failingFiles: string[]; elapsedMs: number; log: string }
export interface FreeMeasurement {
schema: 'gstack-ship-measure/1'; label: 'diagnostic'; verdict: null;
files: string[]; reruns: number; completed: number; passed: number; parallel: number; concurrency: number;
backend: 'local'; wallCapMs: number; capHit: boolean; outcomes: FreeRerunOutcome[];
}
export interface FreeRerunOptions {
files: string[];
reruns: number;
/** Within-shard concurrency of the original run (the free lane runs each shard at 1). */
concurrency: number;
wallCapMs: number;
outDir: string;
cores?: number;
runShard?: typeof runFreeShard;
log?: (line: string) => void;
}
/** Local parallelism for free reruns: max(1, floor(cores / shard concurrency)). */
export const freeParallelism = (cores: number, concurrency: number) => Math.max(1, Math.floor(cores / Math.max(1, concurrency)));
/**
* Rerun one shard's exact file list N times with the flaky retry off: rerun 1
* alone as the baseline, the rest in parallel up to the cap, each with its own
* HOME, state root and flake ledger. No rerun starts after the wall cap, and
* one the cap cuts short is not counted as completed.
*/
export async function measureFreeShard(o: FreeRerunOptions): Promise<FreeMeasurement> {
const log = o.log ?? ((line: string) => console.log(line));
const runShard = o.runShard ?? runFreeShard;
const parallel = freeParallelism(o.cores ?? os.availableParallelism(), o.concurrency);
fs.mkdirSync(path.join(o.outDir, 'free'), { recursive: true });
const dir = fs.mkdtempSync(path.join(o.outDir, 'free', 'rerun-'));
const deadline = Date.now() + o.wallCapMs;
const shardWall = wallTimeoutForShard(o.files.length);
const outcomes: FreeRerunOutcome[] = [];
const runOne = async (rerun: number, mode: FreeRerunOutcome['mode']) => {
const runDir = path.join(dir, `r${String(rerun).padStart(2, '0')}`);
const state = path.join(runDir, 'state');
fs.mkdirSync(state, { recursive: true });
const remaining = deadline - Date.now();
const wallTimeoutMs = Math.max(1, Math.min(shardWall, remaining));
const env = { ...process.env, GSTACK_HOME: state, GSTACK_STATE_ROOT: state, GSTACK_FREE_RETRY_FLAKY: '0', GSTACK_FLAKE_LEDGER: path.join(runDir, 'flake-ledger.jsonl') };
const logPath = path.join(runDir, 'shard.log');
const outcome: FreeShardOutcome = await runShard(o.files, rerun, o.reruns, { env, wallTimeoutMs, quiet: true, log: () => {}, logFilePath: logPath, homeGuard: privateFreeHome });
const cut = outcome.status === 'timed-out' && wallTimeoutMs < shardWall;
outcomes.push({ rerun, mode, status: cut ? 'cut by wall cap' : outcome.status, completed: !cut, failingFiles: outcome.failingFiles, elapsedMs: outcome.elapsedMs, log: logPath });
};
if (o.reruns > 0) await runOne(1, 'baseline');
let next = 2;
const worker = async () => {
while (next <= o.reruns && Date.now() < deadline) await runOne(next++, 'parallel');
};
await Promise.all(Array.from({ length: Math.min(parallel, Math.max(0, o.reruns - 1)) }, worker));
outcomes.sort((a, b) => a.rerun - b.rerun);
const done = outcomes.filter(r => r.completed);
const measurement: FreeMeasurement = {
schema: 'gstack-ship-measure/1', label: 'diagnostic', verdict: null, files: o.files, reruns: o.reruns,
completed: done.length, passed: done.filter(r => r.status === 'passed').length, parallel, concurrency: o.concurrency,
backend: 'local', wallCapMs: o.wallCapMs, capHit: done.length < o.reruns, outcomes,
};
writeJson(path.join(dir, 'free-measurement.json'), measurement);
log(`[ship-measure] DIAGNOSTIC free shard (${o.files.length} files): observed ${measurement.passed}/${measurement.completed} passed; ${measurement.completed} of ${o.reruns} completed${measurement.capHit ? ` (wall cap ${Math.round(o.wallCapMs / 1000)}s)` : ''}; rerun 1 alone as baseline, the rest at parallelism ${parallel}; flaky retry off. Captures: ${dir}`);
return measurement;
}
/** The exact file list of local free-suite shard I, as `bun run test` packs it. */
export function localFreeShardFiles(index: number, rootDir = ROOT): string[] {
const files = collectFreeTestFiles(rootDir);
const exclusive = files.filter(f => f in TREE_MUTATING);
const readers = files.filter(f => !(f in TREE_MUTATING));
const jobs = fullSuiteJobs();
const durations = loadFreeTestDurations(rootDir);
const shards = durations ? packShardsByDuration(readers, jobs, durations).shards : assignFilesToShards(readers, jobs);
if (exclusive.length) shards.push(exclusive);
const shard = shards[index - 1];
if (!shard) throw new Error(`--shard ${index}: the local free suite has ${shards.length} shards`);
return shard;
}
// ─── CLI ───────────────────────────────────────────────────────────────────
function positive(raw: string | undefined, flag: string, integer = true): number {
const value = Number(raw);
if (raw === undefined || !(integer ? /^[1-9][0-9]*$/.test(raw) : Number.isFinite(value) && value >= 0)) throw new Error(`${flag} needs a ${integer ? 'positive integer' : 'non-negative number'}. Received: ${raw}`);
return value;
}
export async function main(argv: string[], env: NodeJS.ProcessEnv = process.env): Promise<number> {
const [command, ...rest] = argv;
if (command === 'sweep') return (await import('./ship-measure-sweep')).sweepMain(rest, env);
const flags: Record<string, string> = {};
const value = (name: string) => (next: () => string | undefined) => { const v = next(); if (v === undefined) throw new Error(`${name} needs a value`); flags[name] = v; };
const handlers = Object.fromEntries(['--case', '--round', '--kind', '--command', '--cost-per-trial', '--fix', '--jobs', '--out', '--reason',
'--files', '--shard', '--reruns', '--concurrency', '--backend', '--wall-cap', '--trials', '--class', '--evidence'].map(name => [name, value(name)]));
parseCliFlags(rest, { ...handlers, '--approved': () => { flags['--approved'] = '1'; } }, 'Usage: see the header of scripts/ship-measure.ts');
const config = readMeasureConfig();
const outDir = path.resolve(flags['--out'] ?? path.join(process.cwd(), '.context', 'ship-measure'));
if (command === 'table') { console.log(formatKindTable(config)); return 0; }
if (command === 'report') { console.log(formatReport(outDir)); return 0; }
if (command === 'skip') { console.log(`[ship-measure] ${flags['--case']} labeled unmeasured: ${recordUnmeasured(outDir, flags['--case'] ?? '', flags['--reason'] ?? '')}`); return 0; }
if (command === 'free') return runFreeCli(flags, config, outDir, env);
if (command !== 'measure' && command !== 'classify' && command !== 'decide' && command !== 'extend') {
throw new Error(`Unknown command: ${command ?? '(none)'}. Use table, measure, classify, decide, extend, skip, report, free or sweep.`);
}
const caseId = flags['--case'];
if (!caseId) throw new Error(`${command} needs --case`);
const round = flags['--round'] ?? 'baseline';
if (command === 'classify') {
const trials = (flags['--trials'] ?? '').split(',').map(t => t.trim().replace(/^t/, '')).filter(Boolean).map(t => positive(t, '--trials'));
if (!trials.length || !flags['--class']) throw new Error(`classify needs --trials 2,5 and --class (${FAILURE_CLASSES.join(', ')})`);
return measurementExit(classifyTrials(outDir, caseId, round, trials, flags['--class'], flags['--evidence'] ?? ''));
}
if (command === 'decide') return measurementExit(decideRound(outDir, caseId, round));
const isGstack = path.resolve(process.cwd()) === ROOT;
if (!flags['--command'] && !isGstack) throw new Error('No single-case eval command: pass --command \'<documented command> {case}\' (ask the user once and record the answer).');
const runner = flags['--command'] ? commandRunner(flags['--command']) : gstackRunner();
const runnerId = flags['--command'] ? `command:${flags['--command']}` : 'gstack';
const parallel = flags['--jobs'] ? positive(flags['--jobs'], '--jobs') : Number(env.EVALS_JOBS) || DEFAULT_JOBS;
const costPerTrialUsd = flags['--cost-per-trial'] !== undefined ? positive(flags['--cost-per-trial'], '--cost-per-trial', false) : null;
if (command === 'extend') {
return measurementExit(await extendCase({ caseId, round, config, outDir, runner, parallel, ...(costPerTrialUsd !== null ? { costPerTrialUsd } : {}) }));
}
const kind = (flags['--kind'] ?? E2E_KINDS[caseId] ?? 'rule') as MeasureKind;
if (!['rule', 'behavior', 'judge'].includes(kind)) throw new Error(`--kind must be rule, behavior or judge. Received: ${kind}`);
console.log(formatKindTable(config));
if (!flags['--command']) {
const selected = caseSelectionPreflight(caseId);
if (!selected.ok) throw new Error(`--case ${caseId} does not select its test (no paid call made): ${selected.detail}`);
console.log(`[ship-measure] preflight: ${selected.detail}`);
}
const plan = kindPlan(kind, config);
const m = await measureCase({
caseId, kind, round, config, outDir, fix: flags['--fix'], runner, parallel, costPerTrialUsd, approved: flags['--approved'] === '1',
identity: () => measurementIdentity(kind, plan.trials, runnerId),
});
if (m.status === 'needs_approval') console.log(`[ship-measure] NEEDS APPROVAL ${caseId}: ${m.reason}`);
else if (m.status !== 'measured' || m.next === 'stop') console.log(`[ship-measure] NAMED RED ${caseId}: ${m.reason ?? m.decisionReason}`);
return measurementExit(m);
}
async function runFreeCli(flags: Record<string, string>, config: MeasureConfig, outDir: string, env: NodeJS.ProcessEnv): Promise<number> {
const files = flags['--files'] ? flags['--files'].split(',').map(f => f.trim()).filter(Boolean)
: flags['--shard'] ? localFreeShardFiles(positive(flags['--shard'], '--shard')) : [];
if (!files.length) throw new Error('free needs --files a,b,... (the failing shard\'s exact file list) or --shard I');
const reruns = flags['--reruns'] ? positive(flags['--reruns'], '--reruns') : config.ruleTrials;
const backend = (flags['--backend'] ?? config.rerunBackend) as RerunBackend;
if (backend === 'ubicloud') {
if (!env.UBICLOUD_API_KEY) throw new Error('ship_rerun_backend is ubicloud but UBICLOUD_API_KEY is not set; set it or use --backend local');
const r = spawnSync('bash', [path.join(ROOT, 'scripts/ubicloud/test-free.sh'), '--diagnostic', '--files', files.join(','), '--reruns', String(reruns)],
{ stdio: 'inherit', env, timeout: 30 * 60_000 });
return r.status ?? 1;
}
if (backend !== 'local') throw new Error(`--backend must be local or ubicloud. Received: ${backend}`);
const m = await measureFreeShard({ files, reruns, outDir, concurrency: flags['--concurrency'] ? positive(flags['--concurrency'], '--concurrency') : 1,
wallCapMs: (flags['--wall-cap'] ? positive(flags['--wall-cap'], '--wall-cap') : 600) * 1000 });
return m.completed > 0 && m.passed === m.completed ? 0 : 1;
}
if (import.meta.main) {
main(process.argv.slice(2)).then(code => process.exit(code), (error: Error) => { console.error(`[ship-measure] ${error.message}`); process.exit(4); });
}