Skip to content

v1.91.206.0 feat(pregate): user-owned policy file and local lanes (port of #3286) - #3398

Merged
capy-ai[bot] merged 39 commits into
mainfrom
gsta73/pregate-user-policy-3286
Oct 25, 2026
Merged

capy-ai[bot] merged 39 commits into
mainfrom
gsta73/pregate-user-policy-3286

Conversation

@garrytan

@garrytan garrytan commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

What changed

gstack-pregate with a user-owned policy, before and after

Before (#3380 head, without this PR): in a repo that keeps .gstack/ out of git, pregate falls back to the defaults, so the windows.yml/windows-shard lane stays a requires-remote obligation. After: <state root>/projects/<slug>/pregate.json is the active policy, and its local list lets the run pass here. Same fixture, same command.

a state root linked into the checkout, before and after the security fix

Before (#3286 as contributed): the state root links into the checkout, and the policy file there links back out to a permissive file. That file was read as the user policy, so a branch could pick which policy governs it. After: a path that passes through the checkout at any link hop is ignored, and the defaults keep the windows obligation.

This ports #3286 by @arlophoenix. Their 13 commits carry the whole feature and come first, merged with their history and authorship kept (their branch had merged upstream twice, so a cherry-pick of the series conflicted; the merge commit credits them as co-author):

  • User-owned policy (lib/pregate/context.ts, config.ts). When the base branch has no .gstack/pregate.json, gstack-pregate run reads <state root>/projects/<slug>/pregate.json. It never does under --policy-from head, never for a relative path or one inside the checkout, and never with a slug or state root chosen by the checkout's .env. An unreadable file, a dangling link on its path, sealed or price_file refuses rather than falling back to the defaults. A base copy git can't read refuses unless git ls-tree proves it's absent.
  • local lanes. <workflow.yml>/<job> lanes that a local run satisfies run here instead of becoming requires-remote. Under 8 cores, the small-machine remote obligation still applies.
  • Receipts (lib/pregate/index.ts). The record names the user file and its sha256. verify goes PREGATE_STALE when the file is edited, deleted, or appears after a run under the defaults.

Our commits on top close the two findings from a security-focused /review of the containment code:

  • Link chains through the checkout (high, d65304a). userConfigRejection checked the lexical path and the final realpath only. Now every hop of the symlink resolution is checked, so a state root linked into the checkout, or one link hop inside it, gets the path ignored even when the last hop points back outside. The new test covers both shapes and keeps a direct outside link as the control. It failed on the contributed code (null instead of a rejection) and passes now.
  • A dangling parent after a defaults run (medium, 393b60c). A fresh run refuses a user policy path with a dangling parent link, but verify checked only the leaf, so an old defaults pass stayed publishable. The loader and verify now share one absence check (userPolicyAbsent). The new test failed (ok: true) before the fix and passes after; the plain-absent path still verifies.
  • TODO(#3380) (27a8513) on the two cargo-lane cases that v1.91.197.0 fix(pregate): Rust crate lanes and nested JS package runners (#3344, port of #3346) #3380 skips on Windows, as Garry asked.
  • Docs and changelog. docs/pregate.md, the pregate result-code entries in docs/troubleshooting.md, and an additive [Unreleased] entry.

Retargeted to main after #3380 merged (c739dfa). main was merged in at ca65514: the only conflicts were CHANGELOG (resolved additively) and the TODO lines in test/pregate-rust.test.ts (this branch's side kept). Apart from the [Unreleased] heading, the diff against main matches the stacked diff exactly.

Why

On a shared repo where gstack is one contributor's tool, .gstack/ stays out of git, so pregate ran on the defaults. Every platform lane the machine can't run (an ubuntu-latest suite on a Mac, say) became a requires-remote obligation that never cleared, even when that suite runs the same here. The user file fixes that without letting a PR choose its own policy, and the two fixes close the remaining paths a branch had to influence which file is read.

Closes #3286. Thanks @arlophoenix for splitting #3193 as asked, and for the careful containment and refusal work the review built on.

Approvals

Date Asked Words Covers Pending
2026-10-24 Garry, via GStack Minder "Do it": touch only the pregate result-code entries of docs/troubleshooting.md, leave the GSTA-68/69/77 passages docs/troubleshooting.md hunks nothing
2026-10-24 Garry, via GStack Minder per-case skipIf(win32) accepted; add a TODO linking #3380 on each skip in this PR test/pregate-rust.test.ts comments nothing
2026-10-24 none needed standing approval: merge when green, through a merge train (CREW rule 6) this PR nothing

Evidence

  • Focused: test/pregate-*.test.ts (22 files), module-size ratchet, bun-entry isolation, state-root ratchet and spawnSync tripwire gave 212 pass and 2 fail. Both failures are pregate-regate cases at the 5 s timeout on a 4-core machine. The same file fails the same way (4 cases) on v1.91.197.0 fix(pregate): Rust crate lanes and nested JS package runners (#3344, port of #3346) #3380's head without this PR, and passes on the remote gate below. bun run typecheck and typecheck:test are clean.
  • Remote free tier (gstack-pregate --stage tests --remote, Ubicloud) at ca65514, tree 264d57e37d9a: 15496 tests, 0 failed, $0.05. The macOS and Windows obligations were cleared with Free Tests 40169208710 and Windows Free Tests 40169208698, and CI is green (48 passed, 0 failed).
  • Local pregate: regen, strays, literals, timing and guards pass. secrets warns on 9 MEDIUM internal.hostname matches: .env.local-style dotenv names and the .local example in the docstrings, not hostnames.
  • Gate integrity: no threshold, budget, timeout, trial count, case kind or skip changed. The user file can relax lanes only where the base has no policy at all, and only from outside the repository. Both fixes make that stricter. No new dependency, no network call, no workflow change.

Open in Capy

arlophoenix and others added 30 commits October 18, 2026 14:14
A repo that keeps .gstack/ out of git can hold its pregate policy at
<GSTACK_HOME>/projects/<slug>/pregate.json. gstack-pregate run reads it only
under --policy-from base when the base carries no .gstack/pregate.json, so a
reviewed base copy always wins and a PR still cannot reach the policy. A
missing file falls back; any other read error, and a non-null sealed or
price_file (read only from the git copies), refuse with PREGATE_CONFIG_INVALID
and a fix that names the file.

A new `local` key lists <workflow.yml>/<job> lanes a local run satisfies: a
platform lane named there runs here (tier unit) instead of becoming a
requires-remote obligation. Lane ids share one LANE_ID check with selection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tive policy

With no .gstack/pregate.json on the base, an edit to that path is never read,
so the no-lane, runner, stall_timeout_s, remote_gate and strays fixes now name
ctx.configFile: the user-owned path when it is the active policy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Ignore a user policy path that is relative or inside the checkout, fingerprint
the active user file into check inputs, name the user file in its JSON errors
and the refusal lead, add --explain lines for an absent user file and an
unknown local id, and document the state-root location and slug lookup.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A state root under an in-repo directory named ..something passed the
inside-the-repo check, and realpath kept the caller's casing on macOS.
The unknown-local explain line now says workflow lane, and the state-root
chain in explain and docs names every source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…, refuses

Any git show failure read as no base policy, which now hands the run to
the unreviewed user file; it is absent only when cat-file says so, and a
spawn error (stdout cut at the buffer limit) counts as a failure. A user
pregate.json that is a dangling symlink refuses instead of reading as
absent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…angling parent links

cat-file -e checks the blob, so a missing or corrupt object still read as
no base copy; ls-tree reads the tree entry, and any failure refuses. The
dangling-symlink check now covers every path component, not just the file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…sals

Guards, remote_gate, stall timeouts and lane fixes name the user-owned file
when it is the active policy; the trust boundary says run alone reads it;
the dangling-symlink and unreadable-base-copy refusals are documented, and
state-root lists the bucket's pregate.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-checks it

Bun loads .env from the working directory, so a branch could set
GSTACK_PROJECT_SLUG or a state root and choose which pregate.json governs
it. The shebang now passes --no-env-file, scriptInvocation carries that
flag over when it launches the bin as bun <script>, and a bare bun launch
beside any dotenv file refuses (POLICY_SOURCE_UNAVAILABLE).

A recorded pass also outlived its policy: pregate.json now records the
user file's digest (or, under the defaults, the path that was absent), and
verify and the ship receipt treat an edited, deleted or newly present user
file as PREGATE_STALE, reading both paths from the record.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…olicy

# Conflicts:
#	bin/gstack-pregate
#	lib/pregate/checks/lanes.ts
#	lib/pregate/config.ts
#	lib/pregate/context.ts
#	lib/script-invocation.ts
…wording

The added '(or .gstack/pregate.json)' broke describeRefusal's rewrite to
'the reviewed pin files cannot be read', which pregate-wtree-refusal pins.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ne test

On a Windows host the windows lane is native, so it never became
requires-remote and the test failed on the Windows shards.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
lanes reported PREGATE_NO_LANE for every touched .rs file and ran a nested
pnpm+vitest package's tests as `bun test` from the repo root.

- Rust: a .rs file of a crate (nearest Cargo.toml with [package]) is a test
  under <crate>/tests/ or when it holds a #[cfg(test)]; every .rs file of a
  crate and its Cargo.toml select the crate's tests and those of every crate
  depending on it (lib/pregate/rust.ts). Selected tests run per crate as
  `cargo test -p <name>` from the workspace root, judged on the summed
  `test result:` lines (doc-tests included); zero tests stays ZERO-RUN and a
  missing cargo is incomplete, like a missing Dart SDK.
- JS/TS: a test owned by a nested package.json that declares vitest or jest
  runs from that package with package-relative paths, through the package
  manager its packageManager field or nearest lockfile names (pnpm exec,
  yarn, bun x, else npx). Root detection and a declared runner are unchanged.
- init notes that Rust tests run per crate; docs/pregate.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The line scan of Cargo.toml missed dependents (a false green) and picked the
wrong workspace:
- an inherited renamed dependency (`base = { workspace = true }` with
  `[workspace.dependencies] base = { package = "alpha", path = ... }`);
- dotted keys (`alpha.path = "../a"`), `[dependencies.<name>]` tables and
  target-specific tables, matched by key name only;
- a crate the workspace excludes ran `cargo test -p` from the workspace root.

Manifests are now parsed with Bun.TOML. A dependency resolves by path
(relative to the manifest declaring it, or to the workspace root when
inherited), else by package name. A crate runs from the workspace it is a
member of under Cargo's rules (package.workspace, the nearest [workspace],
members globs, exclude, literal members, path dependencies of members),
else from its own directory. Nothing runs cargo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… beside a real cargo

windows-free-shard (5) failed 'a touched src file runs its crate...' and
'zero tests is ZERO-RUN...' deterministically: the shim PATH was joined with
':' so Git Bash never saw the shim dir, and the runner's real cargo ran
against the fixture (exit 1, no test result lines). On POSIX hosts where
Bun and cargo share a directory (/usr/local/bin on Capy's image), the lane
puts Bun's directory first and the real cargo wins the same way.

Join with path.delimiter, add cargo.cmd for Bun.which on Windows, and on
POSIX put the shim first again in the lane's own bash through BASH_ENV.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
The crate name is read from the repo's Cargo.toml and was interpolated into
'cargo test -p <name>', which the lane runs through bash. A name like
'evil;touch pwned' ran the second command. Quote it like every file and cwd
the lane already passes; a new end-to-end case checks the name reaches cargo
as one argument and runs nothing.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
'a touched file in a nested pnpm+vitest package runs vitest there' ran the
host's real pnpm wherever Bun and pnpm share a directory (/usr/local/bin on
Capy's image): the lane puts Bun's directory first, so pnpm installed vitest
from the network, dirtied the fixture tree and the check came back
incomplete. Put the shim first again in the lane's bash through BASH_ENV,
as the Rust fixture does.

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

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

Port review (outside voice): quoting alone was not enough, because the lane
expands {files} after the runner string is built, so a crate named
'evil{files}' spliced the quoted file list into the quoted name and a file
name like tests/$(cmd).rs then ran. A name outside Cargo's grammar
(letters, digits, _ and -) now gets no runner: its files are unclaimed and
the lane is incomplete, as Cargo itself would refuse the manifest. A valid
name, Unicode included, still runs quoted.

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

Port review (outside voice): members globs went through ship-policy's
matchesAny, which escapes [...] classes (crates/[ab] matched nothing, so the
crate was no member, lost its inherited [workspace.dependencies] path and its
dependents' tests were never selected: a false green) and backtracks
exponentially on a hostile glob such as '*a*a...*ab' (over 5 s for one crate,
before any runner deadline). A small dynamic-programming matcher handles *,
?, [..] with negation and ranges, and ** across components, in
O(pattern x path).

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

Port review (outside voice): only #[cfg(test)] modules and tests/ files
counted, so a dependent crate whose tests are plain #[test] or #[tokio::test]
functions in src never ran when its dependency changed (a false green). The
cfg scan also went quadratic on a run of unclosed '#[cfg(' (about 3 s on
120 KB). Test attributes now count, and the cfg predicate and its strings are
length-bounded so the scan stays linear.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…d crate names; changelog for the port review fixes

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…t every crate past a size cap

Second port review round (outside voice): the matcher counted UTF-16 units, so
'crates/?' missed a crate directory named with a supplementary character and
dropped its inherited dependencies (a false green); and each unmatched '['
rescanned the rest of the glob (5 s for one crate with a million of them).
Tokenize by code point with a precomputed next-']' table, and past 4096
characters of members globs admit every crate under the workspace: a wrong
member fails loudly in cargo, a dropped one is silent. The new supervised
probe kills a super-linear matcher at 30 s instead of hanging the file.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…closable one counts as a test

Second port review round (outside voice): the length bound from the first
round dropped a valid cfg(any(test, feature = "<257 chars>")), so its crate's
tests were no longer selected (a false green). Read each attribute once to
its closing ']' and resume after it, which keeps the scan linear without a
bound; an attribute the scan cannot close (an unterminated string, no ']')
counts as a test, which selects more, never fewer.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…nbounded linear cfg scan)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…ded and its tests unclaimed

Third port review round (outside voice): admitting every crate past the cap
made a non-member run as 'cargo test -p <name>' from the workspace root,
where a same-named member answered and its passing tests stood in for the
orphan's failing one (a false green). Keep the conservative membership for
dependency edges, but leave such a crate's workspace undecided (null):
planRuns leaves its files unclaimed and the lane incomplete.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…ows' own Path key

windows-free-shard (5) still failed both end-to-end cases at fe99b20 with
the same symptom (the runner's real cargo, exit 1, no test result lines).
runBin merges process.env, whose Windows key is 'Path', with the shim's
'PATH', so the child got two path variables and read the original. Write the
shim under the key process.env actually uses, as test/helpers/unreachable-gbrain.ts
and sync-gbrain-readiness-fixture.ts do.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
garrytan and others added 6 commits October 24, 2026 22:40
windows-free-shard (5) still failed both end-to-end cargo-lane cases after the
delimiter, cargo.cmd and Path-key repairs: the lane's Git Bash exits 1 with an
empty log, and that log is not visible from CI. Skip the two cases that run the
bash stand-in on win32 (test.skipIf, as other POSIX-shim pregate fixtures are
handled) and drop the unproven cargo.cmd and Path-key changes. Selection,
planning and the hostile-name case still run on Windows.

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

Ports arlophoenix's branch as is (13 commits, their two upstream merges
included). It merges cleanly onto main and onto #3380.

Co-authored-by: arlophoenix <[email protected]>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
…e checkout

userConfigRejection checked the lexical path and the final realpath only. A
state root linked into the checkout, or a link hop inside it, let a branch pick
which existing out-of-repo policy governs it. Every hop of the resolution chain
is now checked. Found by the security review of #3286.

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

A fresh run refuses a user policy path with a dangling parent link, but verify
looked only at the leaf, so the parent's ENOENT read as continued absence and
an old defaults pass stayed publishable. The loader and verify now share one
absence check. Found by the security review of #3286.

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

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@capy-ai
capy-ai Bot changed the base branch from gsta73/pregate-rust-3344 to main October 25, 2026 06:26
@trunk-io

trunk-io Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

😎 This pull request was merged.

garrytan and others added 3 commits October 25, 2026 06:27
…olicy-3286

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

# Conflicts:
#	CHANGELOG.md
#	test/pregate-rust.test.ts
…olicy-3286

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

# Conflicts:
#	CHANGELOG.md
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@github-actions github-actions Bot changed the title feat(pregate): user-owned policy file and local lanes (port of #3286) v1.91.206.0 feat(pregate): user-owned policy file and local lanes (port of #3286) Oct 25, 2026
@capy-ai
capy-ai Bot merged commit 669e59d into main Oct 25, 2026
63 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants