Merge remote-tracking branch 'origin/main' into codex/fail-closed-run-event-persistence

# Conflicts:
#	lib/components/fabro-workflow/src/operations/start.rs
This commit is contained in:
Bryan Helmkamp 2026-08-21 19:49:44 -04:00
commit d80dde2320
No known key found for this signature in database
60 changed files with 4991 additions and 523 deletions

77
Cargo.lock generated
View file

@ -1870,7 +1870,7 @@ checksum = "d7a1e2f27636f116493b8b860f5546edb47c8d8f8ea73e1d2a20be88e28d1fea"
[[package]]
name = "daytona-api-client"
version = "0.1.0"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=73c9c458dd1a1d096afd3521175637af82afd8d8#73c9c458dd1a1d096afd3521175637af82afd8d8"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=be2c7b7272740d47c023cac8abc9f63c1a51a511#be2c7b7272740d47c023cac8abc9f63c1a51a511"
dependencies = [
"reqwest 0.13.2",
"reqwest-middleware",
@ -1884,7 +1884,7 @@ dependencies = [
[[package]]
name = "daytona-sdk"
version = "0.1.0"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=73c9c458dd1a1d096afd3521175637af82afd8d8#73c9c458dd1a1d096afd3521175637af82afd8d8"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=be2c7b7272740d47c023cac8abc9f63c1a51a511#be2c7b7272740d47c023cac8abc9f63c1a51a511"
dependencies = [
"daytona-api-client",
"daytona-toolbox-client",
@ -1904,13 +1904,15 @@ dependencies = [
[[package]]
name = "daytona-toolbox-client"
version = "0.1.0"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=73c9c458dd1a1d096afd3521175637af82afd8d8#73c9c458dd1a1d096afd3521175637af82afd8d8"
source = "git+https://github.com/brynary/daytona-sdk-rust?rev=be2c7b7272740d47c023cac8abc9f63c1a51a511#be2c7b7272740d47c023cac8abc9f63c1a51a511"
dependencies = [
"reqwest 0.13.2",
"reqwest-middleware",
"serde",
"serde_json",
"serde_repr",
"tokio",
"tokio-util",
"url",
]
@ -2072,7 +2074,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -2199,7 +2201,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -2663,6 +2665,7 @@ dependencies = [
"fabro-static",
"fabro-test",
"fabro-types",
"futures",
"jsonwebtoken",
"serde",
"serde_json",
@ -4388,6 +4391,22 @@ dependencies = [
"webpki-roots 1.0.6",
]
[[package]]
name = "hyper-tls"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0"
dependencies = [
"bytes",
"http-body-util",
"hyper",
"hyper-util",
"native-tls",
"tokio",
"tokio-native-tls",
"tower-service",
]
[[package]]
name = "hyper-util"
version = "0.1.20"
@ -4440,7 +4459,7 @@ dependencies = [
"js-sys",
"log",
"wasm-bindgen",
"windows-core 0.62.2",
"windows-core 0.61.2",
]
[[package]]
@ -5315,6 +5334,23 @@ dependencies = [
"getrandom 0.2.17",
]
[[package]]
name = "native-tls"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "465500e14ea162429d264d44189adc38b199b62b1c21eea9f69e4b73cb03bbf2"
dependencies = [
"libc",
"log",
"openssl",
"openssl-probe 0.2.1",
"openssl-sys",
"schannel",
"security-framework",
"security-framework-sys",
"tempfile",
]
[[package]]
name = "new_debug_unreachable"
version = "1.0.6"
@ -5389,7 +5425,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -6386,7 +6422,7 @@ dependencies = [
"once_cell",
"socket2",
"tracing",
"windows-sys 0.60.2",
"windows-sys 0.59.0",
]
[[package]]
@ -6671,11 +6707,13 @@ dependencies = [
"http-body-util",
"hyper",
"hyper-rustls",
"hyper-tls",
"hyper-util",
"js-sys",
"log",
"mime",
"mime_guess",
"native-tls",
"percent-encoding",
"pin-project-lite",
"quinn",
@ -6687,6 +6725,7 @@ dependencies = [
"serde_urlencoded",
"sync_wrapper",
"tokio",
"tokio-native-tls",
"tokio-rustls",
"tokio-util",
"tower",
@ -6860,7 +6899,7 @@ dependencies = [
"errno 0.3.14",
"libc",
"linux-raw-sys",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -6919,7 +6958,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -7443,7 +7482,7 @@ version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
"errno 0.3.14",
"errno 0.2.8",
"libc",
]
@ -8033,7 +8072,7 @@ dependencies = [
"getrandom 0.4.1",
"once_cell",
"rustix",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -8079,7 +8118,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874"
dependencies = [
"rustix",
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]
@ -8231,6 +8270,16 @@ dependencies = [
"syn 2.0.117",
]
[[package]]
name = "tokio-native-tls"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2"
dependencies = [
"native-tls",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.26.4"
@ -9132,7 +9181,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.61.2",
"windows-sys 0.59.0",
]
[[package]]

View file

@ -97,8 +97,8 @@ twin-openai = { path = "test/twin/openai" }
twin-github = { path = "test/twin/github" }
tokio-tungstenite = { version = "0.26", features = ["rustls-tls-webpki-roots"] }
futures-util = "0.3"
daytona-sdk = { git = "https://github.com/brynary/daytona-sdk-rust", rev = "73c9c458dd1a1d096afd3521175637af82afd8d8", package = "daytona-sdk" }
daytona-api-client = { git = "https://github.com/brynary/daytona-sdk-rust", rev = "73c9c458dd1a1d096afd3521175637af82afd8d8", package = "daytona-api-client" }
daytona-sdk = { git = "https://github.com/brynary/daytona-sdk-rust", rev = "be2c7b7272740d47c023cac8abc9f63c1a51a511", package = "daytona-sdk" }
daytona-api-client = { git = "https://github.com/brynary/daytona-sdk-rust", rev = "be2c7b7272740d47c023cac8abc9f63c1a51a511", package = "daytona-api-client" }
sentry = { version = "0.35", default-features = false, features = ["backtrace", "contexts", "ureq", "rustls"] }
fork = "0.2"
exec = "0.3"

View file

@ -14447,6 +14447,17 @@ components:
type: object
additionalProperties:
type: string
additional_repositories:
type: array
description: |
Additional GitHub repositories, beyond the implicit run origin,
that the minted GITHUB_TOKEN must cover. Each entry is a full
`owner/repository` slug; every repository must share one owner
with the run origin. Omitted when empty; settings persisted
before this field existed deserialize to an empty set.
items:
type: string
uniqueItems: true
RunGoal:
oneOf:
@ -14633,6 +14644,12 @@ components:
properties:
enabled:
type: boolean
depth:
type: integer
format: int32
minimum: 0
default: 100
description: Git history depth. Set to 0 to clone full history.
RunBranchSettings:
type: object

View file

@ -0,0 +1,20 @@
---
title: "Additional GitHub repositories"
date: "2026-08-21"
---
## One token for the whole repository set
A run can now declare additional GitHub repositories that its stages may access through the managed `GITHUB_TOKEN`:
```toml
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
```
The run origin stays implicit, and Fabro mints one installation token scoped to the origin plus every declared repository with the shared permission map. Inside stages, `gh` commands, raw GitHub API calls, plain Git over HTTPS, and the common SSH URL spellings (`git@github.com:owner/repo` and `ssh://git@github.com/owner/repo`) all work against the declared set — the SSH forms are transparently rewritten to authenticated HTTPS with no secret placed in Git configuration.
Every repository must share one owner and be reachable by the origin's GitHub App installation. Preflight resolves each repository's installation, mints the scoped token once, and probes every repository with `git ls-remote`, naming the exact repository when something is not accessible; run initialization enforces the same checks. A declared-but-inaccessible repository fails the run before its first stage.
Declaring additional repositories requires `contents = "read"` or `contents = "write"`. With `contents = "write"`, any stage can push to any declared repository — declare the smallest set and weakest permissions that work. See [Additional repositories](/integrations/github#additional-repositories) for details, including layering rules and `GH_TOKEN` precedence.

View file

@ -294,6 +294,13 @@
"tab": "Changelog",
"icon": "clock-rotate-left",
"groups": [
{
"group": "August 2026",
"icon": "clock-rotate-left",
"pages": [
"changelog/2026-08-21"
]
},
{
"group": "July 2026",
"icon": "clock-rotate-left",

View file

@ -258,7 +258,7 @@ memory = "4GB"
mode = "block"
```
Docker and Daytona are clone-based providers. When a run has a GitHub origin, Fabro clones it into the provider workspace. Set `[run.clone] enabled = false` to start with an empty workspace. Docker and Daytona ignore `cwd`; use the provider-owned workspace layout and `run.working_dir` for repository-relative commands.
Docker and Daytona are clone-based providers. When a run has a GitHub origin, Fabro clones it into the provider workspace with a history depth of 100. Set `[run.clone] enabled = false` to start with an empty workspace. Set `[run.clone] depth = 0` to clone full history. Docker and Daytona ignore `cwd`; use the provider-owned workspace layout and `run.working_dir` for repository-relative commands.
The image must provide `/bin/bash`; Fabro evaluates every sandbox command with it and has no `sh` fallback. Commands run in a **non-login** shell, so login profiles (`/etc/profile.d/*.sh`, `~/.bash_profile`, and `nvm`/`rbenv`/`sdkman` initializers) are not sourced — put anything they set into the Dockerfile's `ENV` instead. Fabro verifies Bash during initialization and again on resume, and fails with remediation rather than reporting the sandbox ready.

View file

@ -241,10 +241,16 @@ Configure whether clone-based sandboxes clone the run's GitHub origin before exe
```toml title="run.toml"
[run.clone]
enabled = true
depth = 100
```
Set `enabled = false` to start Docker and Daytona runs with an empty provider workspace. Use [prepare steps](#runprepare) to clone or create any files the workflow needs.
| Field | Description |
|---|---|
| `enabled` | When `false`, Fabro skips the repository clone. Defaults to `true`. |
| `depth` | Git history depth for Docker and Daytona. Defaults to `100`. Set it to `0` to clone full history. |
### `[run.run_branch]`
Configure Fabro's managed `fabro/run/<id>` checkpoint branch.
@ -363,6 +369,24 @@ Only requested permissions are included. The upper bound is the permission set g
This table follows the normal settings precedence order. A higher-precedence layer can set `permissions = {}` to clear inherited permissions and run without a GitHub token.
### `[run.integrations.github].additional_repositories`
Declare extra GitHub repositories, beyond the implicit run origin, that the minted `GITHUB_TOKEN` must cover. The one `permissions` map applies to the origin and every declared repository.
```toml title="run.toml"
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
```
Each entry is a full `owner/repository` slug. Every repository in the effective set must share one owner and be reachable by the origin repository's GitHub App installation. A non-empty list requires `contents = "read"` or `contents = "write"`. Malformed slugs, case-insensitive duplicates, cross-owner sets, and sets larger than 499 entries fail configuration validation with indexed error paths such as `run.integrations.github.additional_repositories[1]`.
Unlike permissions-only configuration, declared additional repositories are a hard requirement: missing credentials, a missing origin, or an inaccessible declared repository fails preflight and run initialization with the repository named.
The higher-precedence list replaces the lower one wholesale — no union and no `...` splice — and `additional_repositories = []` explicitly clears an inherited list. `additional_repositories` and `permissions` resolve independently; if layering leaves repositories declared while permissions were cleared, resolution reports the invalid combination instead of dropping either field.
See [Additional repositories](/integrations/github#additional-repositories) for what works inside stages (`gh`, GitHub API, plain Git over HTTPS and the common SSH spellings) and for the security boundary.
### `[run.notifications]`
Define named notification routes for run events. Slack lifecycle notifications are configured here, not in server config.

View file

@ -141,6 +141,15 @@ provider = "daytona"
enabled = false
```
Daytona clones 100 commits by default. To keep only the newest commit, set a smaller clone depth:
```toml title="run.toml"
[run.clone]
depth = 1
```
Set `depth = 0` to clone the full repository history.
If the clone fails without GitHub access configured, Fabro suggests running the setup flow:
```

View file

@ -229,7 +229,7 @@ For public repositories, the clone works without credentials. The token is still
### GITHUB_TOKEN injection
When any settings layer declares `[run.integrations.github.permissions]`, Fabro prepares a scoped GitHub App token source and exposes it as the `GITHUB_TOKEN` environment variable in sandbox command and agent execution. Agents running inside the sandbox can use this token for GitHub API calls, cloning additional private repos, or pushing to branches. The GitHub CLI (`gh`) reads `GITHUB_TOKEN` automatically, so command stages can run `gh pr list`, `gh issue create`, and similar commands without an explicit `gh auth login`.
When any settings layer declares `[run.integrations.github.permissions]`, Fabro prepares a scoped GitHub App token source and exposes it as the `GITHUB_TOKEN` environment variable in sandbox command and agent execution. Agents running inside the sandbox can use this token for GitHub API calls and pushes within the granted permissions. The GitHub CLI (`gh`) reads `GITHUB_TOKEN` automatically, so command stages can run `gh pr list`, `gh issue create`, and similar commands without an explicit `gh auth login`.
```toml title="workflow.toml"
[run.integrations.github.permissions]
@ -239,6 +239,46 @@ pull_requests = "write"
Only the listed permissions are requested — the token is scoped to the minimum access needed. If the GitHub App isn't configured or the repository lacks an installation, the run logs a warning and continues without the token.
In App mode, the token covers only the run's origin repository unless the run declares [additional repositories](#additional-repositories). Injecting `GITHUB_TOKEN` alone does not make other private repositories reachable.
### Additional repositories
A run can declare extra GitHub repositories that its stages may access through the same `GITHUB_TOKEN`:
```toml title="workflow.toml"
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
```
The run origin stays implicit — never list it. Each entry is a full `owner/repository` slug (no scheme, host, ref, or extra path component). Fabro mints **one** installation token scoped to the origin plus every declared repository, with the one shared `permissions` map applying to all of them.
What works against every declared repository, within the granted permissions:
- **`gh` CLI and raw GitHub API calls** through `GITHUB_TOKEN`.
- **Plain Git over HTTPS** (`git clone https://github.com/owner/repo`), through a secret-free credential helper that reads `$GITHUB_TOKEN` at invocation time.
- **The common SSH spellings** `git@github.com:owner/repo[.git]` and `ssh://git@github.com/owner/repo[.git]`, through per-repository SSH-to-HTTPS rewrites injected into the stage environment.
Fabro does not clone additional repositories for you; a workflow that needs one on disk adds its own clone step (`git clone https://github.com/owner/repo` or `gh repo clone owner/repo`).
Requirements and validation:
- Every repository in the effective set must share **one owner** and be reachable by the origin repository's GitHub App installation, because one App installation covers one account. Cross-owner declarations fail configuration validation; a same-owner repository outside the installation fails preflight and run initialization with the repository named.
- A non-empty `additional_repositories` requires `contents = "read"` or `contents = "write"` in the permission map.
- Malformed slugs, duplicates (repository identity is case-insensitive), and sets larger than 499 entries fail configuration validation with indexed error paths.
- Unlike permissions-only configuration, declared additional repositories are a hard requirement: missing GitHub credentials, a missing origin, or an inaccessible declared repository fails preflight and run initialization instead of continuing without the token.
- Layering: the higher-precedence `additional_repositories` list replaces the lower one wholesale (no union, no `...` splice), and `additional_repositories = []` explicitly clears an inherited list. `permissions` keeps its existing whole-map replacement behavior. If layering leaves repositories declared with permissions cleared, configuration resolution reports the invalid combination.
Behavior notes:
- **Token strategy (PAT):** the configured PAT is used as-is. The repository list drives validation and preflight probes, but it cannot narrow the PAT's inherent GitHub scope — App mode remains the least-authority option.
- **`GH_TOKEN` precedence:** `gh` checks `GH_TOKEN` before `GITHUB_TOKEN`. If the resolved run environment defines `GH_TOKEN`, `gh` uses it instead of the managed token; Fabro never sets or removes `GH_TOKEN`, and preflight warns when additional repositories are declared alongside one.
- **SSH rewrites match by prefix.** With `owner/repo` declared, the SSH spelling of `owner/repo-other` is also rewritten to HTTPS. The scoped token is invalid for undeclared repositories at GitHub, so authority is unchanged — but a private undeclared repository fails with a GitHub authorization error instead of a missing-credential or SSH error.
#### Security boundary
Workflow authors may name any repository reachable by the server's GitHub App installation; Fabro applies no second server-side repository intersection. The token is scoped server-side to exactly the declared set — a request to an undeclared repository fails at GitHub, and Fabro never mints an unscoped installation-wide token. With `contents = "write"`, **any stage can push to any declared repository**. Declare the smallest repository set and the weakest permissions that work.
Installation Access Tokens are short-lived. Fabro refreshes its own credentials before checkpoint pushes. For ACP/CLI agent turns launched with GitHub App push credentials, Fabro also re-mints the token and rewrites the sandbox's `origin` URL before the ACP process starts, then every 45 minutes for the lifetime of that turn. Refresh failures are logged and do not fail the stage.
`FABRO_PUSH_CRED_REFRESH_AHEAD` defaults to enabled; set it to `0`, `false`, `off`, `no`, or an empty value to disable both turn-entry and background refresh. `FABRO_PUSH_CRED_REFRESH_INTERVAL_SECONDS` overrides the background interval, and `0` disables only the background loop. This refresh loop is ACP-specific; command and native/API agent stages do not run it. Reconnected sandboxes for resumed or parked runs currently lack the App credentials needed for ACP refresh, so the refresh is skipped there.

View file

@ -34,7 +34,7 @@ Files that omit `_version` are treated as version `1`. The legacy top-level `ver
| Scope | Examples |
|---|---|
| CLI-only | `[cli.target]`, `[cli.auth]`, `[cli.exec]`, `[cli.output]`, `[cli.updates]`, `[cli.logging]` |
| Shared run defaults | `[run.model]`, `[run.environment]`, `[environments.<slug>]`, `[run.checkpoint]`, `[run.inputs]`, `[run.prepare]`, `[run.pull_request]`, `[run.integrations.github.permissions]`, `[run.hooks]`, `[run.agent.mcps]` |
| Shared run defaults | `[run.model]`, `[run.environment]`, `[environments.<slug>]`, `[run.checkpoint]`, `[run.inputs]`, `[run.prepare]`, `[run.pull_request]`, `[run.integrations.github]`, `[run.hooks]`, `[run.agent.mcps]` |
| Shared LLM catalog | `[llm.providers.<id>]`, provider-scoped `[llm.providers.<id>.models.<slug>]` offerings, limits, features, controls, and costs |
| Server-only | `[server.listen]`, `[server.api]`, `[server.web]`, `[server.auth]`, `[server.storage]`, `[server.artifacts]`, `[server.slatedb]`, `[server.scheduler]`, `[server.logging]`, `[server.integrations]` |

View file

@ -161,13 +161,13 @@ pub(crate) async fn execute(
artifact_sink,
run_control: Some(run_control),
github_app,
github_permissions: run_spec
github_integration: run_spec
.settings
.run
.integrations
.github
.resolve_permissions()
.context("failed to resolve github permissions")?,
.resolve_integration()
.context("failed to resolve github integration")?,
vault,
catalog,
on_node: None,
@ -1762,7 +1762,10 @@ mod tests {
.parse::<EnvironmentProvider>()
.expect("test provider should parse");
run.integrations = RunIntegrationsSettings {
github: RunIntegrationsGithubSettings { permissions },
github: RunIntegrationsGithubSettings {
permissions,
..RunIntegrationsGithubSettings::default()
},
};
run
}

View file

@ -924,6 +924,7 @@ fn attach_json_errors_without_prompting_for_human_input() {
"skip_git_hooks": false
},
"clone": {
"depth": 100,
"enabled": true
},
"environment": {

View file

@ -152,7 +152,8 @@ fn inspect_resolves_selector_via_server_endpoint() {
"commit_timeout_ms": 30000
},
"clone": {
"enabled": true
"enabled": true,
"depth": 100
},
"run_branch": {
"enabled": true,

View file

@ -12,8 +12,7 @@ use fabro_util::error::collect_chain;
use tokio::{fs, task};
use crate::git_checkout::{
GitCheckoutError, GitRepoCache, WorktreePrepareInput, github_metadata_url,
resolve_git_auth_config,
GitCheckoutError, GitRepoCache, WorktreePrepareInput, resolve_git_auth_config,
};
#[derive(Debug, Clone, PartialEq, Eq)]
@ -199,7 +198,7 @@ fn build_manifest_from_checkout(
let mut manifest = built.manifest;
manifest.git = Some(GitContext {
origin_url: github_metadata_url(&git_context.repo),
origin_url: git_context.repo.https_url(),
branch: git_context.ref_selector,
sha: Some(git_context.checked_out_sha),
dirty: DirtyStatus::Clean,

View file

@ -165,11 +165,9 @@ async fn bare_clone_may_be_corrupt(bare_dir: &Path) -> bool {
}
fn github_clone_url(repo: &GitHubRepositorySlug) -> String {
format!("https://github.com/{}/{}.git", repo.owner(), repo.repo())
}
pub(crate) fn github_metadata_url(repo: &GitHubRepositorySlug) -> String {
format!("https://github.com/{}/{}", repo.owner(), repo.repo())
let mut url = repo.https_url();
url.push_str(".git");
url
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -478,10 +476,7 @@ mod tests {
github_clone_url(&repo),
"https://github.com/fabro-sh/fabro.git"
);
assert_eq!(
github_metadata_url(&repo),
"https://github.com/fabro-sh/fabro"
);
assert_eq!(repo.https_url(), "https://github.com/fabro-sh/fabro");
assert!(!github_clone_url(&repo).contains('@'));
}
@ -491,10 +486,7 @@ mod tests {
assert_eq!(repo.owner(), "owner");
assert_eq!(repo.repo(), ".github");
assert_eq!(
github_metadata_url(&repo),
"https://github.com/owner/.github"
);
assert_eq!(repo.https_url(), "https://github.com/owner/.github");
}
#[test]

View file

@ -12,6 +12,7 @@ use fabro_config::{
CliLayer, CliOutputLayer, EnvironmentLayer, MergeMap, RunLayer, SettingsLayer,
WorkflowSettingsBuilder, parse_input_overrides, parse_labels, project,
};
use fabro_github::token_source::{InstallationTokenSource, ResolvedToken, TokenSnapshot};
use fabro_graphviz::graph::{Graph, is_llm_handler_type};
use fabro_graphviz::render::apply_direction;
use fabro_llm::model_test::{ModelTestStatus, run_basic_model_probe};
@ -669,11 +670,11 @@ pub(crate) fn effective_sandbox_provider(settings: &RunNamespace) -> SandboxProv
}
fn resolve_daytona_config(settings: &RunNamespace) -> DaytonaConfig {
daytona_config_from_environment(&settings.environment, !settings.clone.enabled)
daytona_config_from_environment(&settings.environment, &settings.clone)
}
fn resolve_docker_config(settings: &RunNamespace) -> DockerSandboxOptions {
docker_config_from_environment(&settings.environment, !settings.clone.enabled)
docker_config_from_environment(&settings.environment, &settings.clone)
}
#[derive(Clone, Debug, PartialEq, Eq)]
@ -872,6 +873,18 @@ async fn check_git_remote_ref(
command.arg(branch);
}
run_ls_remote(command)
.await
.map_err(|message| redact_auth_url(&message, auth_url.as_ref()))
}
/// Run a prepared `git ls-remote` invocation with a 10s timeout, reducing a
/// failure to its most useful message: stderr, then stdout, then the exit
/// status.
async fn run_ls_remote(mut command: Command) -> std::result::Result<(), String> {
// Dropping a timed-out `Command::output` future does not stop the child
// unless kill-on-drop is enabled.
command.kill_on_drop(true);
let output = time::timeout(Duration::from_secs(10), command.output())
.await
.map_err(|_| "git ls-remote timed out after 10s".to_string())?
@ -883,14 +896,13 @@ async fn check_git_remote_ref(
let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
let message = if !stderr.is_empty() {
Err(if !stderr.is_empty() {
stderr
} else if !stdout.is_empty() {
stdout
} else {
format!("git ls-remote exited with status {}", output.status)
};
Err(redact_auth_url(&message, auth_url.as_ref()))
})
}
fn preflight_sandbox_spec(
@ -1200,32 +1212,233 @@ async fn run_github_token_check(
resolved_run: &RunNamespace,
github_app: Option<fabro_github::GitHubCredentials>,
) -> bool {
if !resolved_run.integrations.github.is_token_requested() {
run_github_token_check_with(
checks,
prepared,
resolved_run,
github_app,
mint_scoped_github_token,
probe_github_repository,
)
.await
}
async fn run_github_token_check_with<M, MFut, P, PFut>(
checks: &mut Vec<CheckResult>,
prepared: &PreparedManifest,
resolved_run: &RunNamespace,
github_app: Option<fabro_github::GitHubCredentials>,
mint_scoped_token: M,
probe_repository: P,
) -> bool
where
M: FnOnce(fabro_github::GitHubRepositoryAccess, fabro_github::GitHubCredentials) -> MFut,
MFut: Future<Output = std::result::Result<ResolvedToken, String>>,
P: Fn(fabro_types::GitHubRepositorySlug, ResolvedToken) -> PFut,
PFut: Future<Output = std::result::Result<(), String>>,
{
let github = &resolved_run.integrations.github;
if !github.is_token_requested() {
return true;
}
// Resolve InterpString permission values eagerly for token minting and
// for display in the preflight report.
let github_permissions = match resolved_run.integrations.github.resolve_permissions() {
Ok(permissions) => permissions,
let integration = match github.resolve_integration() {
Ok(integration) => integration,
Err(err) => {
checks.push(CheckResult {
name: "GitHub Token".into(),
status: CheckStatus::Error,
summary: "invalid permissions".into(),
details: vec![],
remediation: Some(format!("Failed to resolve GitHub permissions: {err}")),
});
return false;
return fail_github_token_check(
checks,
Vec::new(),
"invalid permissions",
format!("Failed to resolve GitHub permissions: {err}"),
);
}
};
let perm_details = github_permissions
let perm_details = integration
.permissions
.iter()
.map(|(key, value)| CheckDetail::new(format!("{key}: {value}")))
.collect::<Vec<_>>();
if !integration.has_additional_repositories() {
// Primary-only behavior is unchanged: a mint check when credentials
// and an origin exist, a warning otherwise, and no Git-content probe
// (permissions-only workflows may request non-contents permissions).
return check_primary_only_github_token(
checks,
prepared,
github_app,
&integration.permissions,
perm_details,
)
.await;
}
// `gh` checks GH_TOKEN before GITHUB_TOKEN, so a user-defined GH_TOKEN
// bypasses the managed scoped token for gh commands. Warn without
// failing; the value is the workflow author's responsibility.
if resolved_run.environment.env.contains_key(EnvVars::GH_TOKEN) {
checks.push(CheckResult {
name: "GH_TOKEN Override".into(),
status: CheckStatus::Warning,
summary: "gh will not use the managed token".into(),
details: vec![],
remediation: Some(
"The resolved run environment defines GH_TOKEN, which the gh CLI prefers over \
the managed GITHUB_TOKEN; gh commands will not use the token scoped to the \
declared repositories."
.to_string(),
),
});
}
let Some(origin_url) = prepared
.git
.as_ref()
.map(|git| git.origin_url.trim())
.filter(|url| !url.is_empty())
else {
return fail_github_token_check(
checks,
perm_details,
"missing origin",
"run.integrations.github.additional_repositories requires a GitHub run origin, but \
this run has no repository origin URL"
.to_string(),
);
};
let Some(creds) = github_app else {
return fail_github_token_check(
checks,
perm_details,
"missing credentials",
"run.integrations.github.additional_repositories requires GitHub credentials, but \
none are configured on the server"
.to_string(),
);
};
// The same validated access value runtime initialization constructs, so
// preflight and runtime cannot disagree about the effective set.
let access = match fabro_github::GitHubRepositoryAccess::new(
Some(origin_url),
&integration.additional_repositories,
integration.permissions.clone(),
) {
Ok(Some(access)) => access,
// `new` returns `Ok(None)` only when nothing is declared, and the
// declared set is non-empty here. Fail closed instead of panicking.
Ok(None) => {
return fail_github_token_check(
checks,
perm_details,
"missing origin",
"run.integrations.github.additional_repositories requires a GitHub run origin, \
but this run has no repository origin URL"
.to_string(),
);
}
Err(err) => {
return fail_github_token_check(
checks,
perm_details,
"invalid repository set",
format!("{err:#}"),
);
}
};
// One mint scoped to the whole effective set. In App mode the minter
// first resolves every repository's installation so a failure names the
// repository the App cannot see.
let token = match mint_scoped_token(access.clone(), creds).await {
Ok(token) => token,
Err(err) => {
return fail_github_token_check(checks, perm_details, "failed", err);
}
};
checks.push(CheckResult {
name: "GitHub Token".into(),
status: CheckStatus::Pass,
summary: "minted".into(),
details: perm_details.clone(),
remediation: None,
});
// Probe every effective repository with bounded concurrency, then report
// in deterministic primary-first order. Possession of a scoped token is
// not proof of access; the probe also verifies PAT/static credentials.
let targets: Vec<fabro_types::GitHubRepositorySlug> =
access.targets().into_iter().cloned().collect();
let probe_repository = &probe_repository;
let mut results: Vec<(usize, CheckResult)> =
stream::iter(targets.into_iter().enumerate().map(|(index, slug)| {
let token = token.clone();
let perm_details = perm_details.clone();
async move {
let check = match probe_repository(slug.clone(), token).await {
Ok(()) => CheckResult {
name: format!("GitHub Repository ({slug})"),
status: CheckStatus::Pass,
summary: "reachable".into(),
details: perm_details,
remediation: None,
},
Err(err) => CheckResult {
name: format!("GitHub Repository ({slug})"),
status: CheckStatus::Error,
summary: "failed".into(),
details: perm_details,
remediation: Some(format!("Failed to verify repository access: {err}")),
},
};
(index, check)
}
}))
.buffer_unordered(REPOSITORY_PROBE_CONCURRENCY)
.collect()
.await;
results.sort_by_key(|(index, _)| *index);
let mut ok = true;
for (_, check) in results {
if check.status != CheckStatus::Pass {
ok = false;
}
checks.push(check);
}
ok
}
/// Report one "GitHub Token" preflight failure and fail the check.
fn fail_github_token_check(
checks: &mut Vec<CheckResult>,
perm_details: Vec<CheckDetail>,
summary: &str,
remediation: String,
) -> bool {
checks.push(CheckResult {
name: "GitHub Token".into(),
status: CheckStatus::Error,
summary: summary.into(),
details: perm_details,
remediation: Some(remediation),
});
false
}
/// Bounded concurrency for per-repository `git ls-remote` probes.
const REPOSITORY_PROBE_CONCURRENCY: usize = 4;
async fn check_primary_only_github_token(
checks: &mut Vec<CheckResult>,
prepared: &PreparedManifest,
github_app: Option<fabro_github::GitHubCredentials>,
permissions: &HashMap<String, String>,
perm_details: Vec<CheckDetail>,
) -> bool {
if let (Some(creds), Some(git)) = (&github_app, prepared.git.as_ref()) {
match mint_github_token(creds, &git.origin_url, &github_permissions).await {
match mint_github_token(creds, &git.origin_url, permissions).await {
Ok(_) => {
checks.push(CheckResult {
name: "GitHub Token".into(),
@ -1259,6 +1472,62 @@ async fn run_github_token_check(
}
}
/// Production minter for the multi-repository path. The source owns the
/// effective set. In App mode its first resolve checks every repository's
/// installation before minting the scoped token.
async fn mint_scoped_github_token(
access: fabro_github::GitHubRepositoryAccess,
creds: fabro_github::GitHubCredentials,
) -> std::result::Result<ResolvedToken, String> {
let source =
InstallationTokenSource::for_access(&creds, &access).map_err(|err| format!("{err:#}"))?;
source.resolve().await.map_err(|err| format!("{err:#}"))
}
/// Production per-repository probe: a non-interactive
/// `git ls-remote <https-url> HEAD` authenticated through
/// [`fabro_github::GITHUB_CREDENTIAL_HELPER`] reading `GITHUB_TOKEN` from
/// the child process environment, so the token never appears in the URL,
/// argv, or rendered errors — exactly what the runtime `git_bridge`
/// configures in `fabro-workflow`.
async fn probe_github_repository(
slug: fabro_types::GitHubRepositorySlug,
token: ResolvedToken,
) -> std::result::Result<(), String> {
let url = slug.https_url();
probe_with_replication_retry(token.snapshot, || run_probe_ls_remote(&url, &token)).await
}
/// Retry auth-shaped failures with the SAME token: replication of a given
/// token only makes progress, while re-minting would restart the replication
/// clock. The sandbox git retry executor owns attempt limits,
/// classification, and pacing.
async fn probe_with_replication_retry<F, Fut>(
snapshot: TokenSnapshot,
mut run: F,
) -> std::result::Result<(), String>
where
F: FnMut() -> Fut,
Fut: Future<Output = std::result::Result<(), String>>,
{
let credential_context = fabro_sandbox::CredentialContext::from_snapshot(Some(&snapshot));
fabro_sandbox::retry_git_operation(
SandboxProviderKind::Local,
"repository probe",
&fabro_sandbox::RetryPlan::repository_probe(),
|_attempt| run(),
|message| fabro_sandbox::classify_failure(message, credential_context),
)
.await
}
async fn run_probe_ls_remote(url: &str, token: &ResolvedToken) -> std::result::Result<(), String> {
let mut command = Command::new("git");
fabro_github::apply_probe_git_env(&mut command, token.token.expose());
command.args(["ls-remote", url, "HEAD"]);
run_ls_remote(command).await
}
async fn mint_github_token(
creds: &fabro_github::GitHubCredentials,
origin_url: &str,
@ -2974,4 +3243,336 @@ dockerfile = { path = "Dockerfile" }
);
}
}
mod github_additional_repository_checks {
//! Seam-injected tests for the declared-additional-repositories
//! preflight path: one scoped mint, per-repository probes with
//! deterministic primary-first reporting, GH_TOKEN warning, and the
//! replication-lag retry policy.
use std::sync::Mutex as StdMutex;
use std::sync::atomic::{AtomicU64, Ordering};
use fabro_github::token_source::{
ResolvedToken, SecretString, TokenProvenance, TokenSnapshot,
};
use fabro_types::settings::run::RunIntegrationsGithubSettings;
use super::*;
fn static_token(secret: &str) -> ResolvedToken {
ResolvedToken {
token: SecretString::new(secret.to_string()),
snapshot: TokenSnapshot {
generation: 0,
provenance: TokenProvenance::Static,
},
refresh_failed: false,
}
}
fn fresh_minted_snapshot() -> TokenSnapshot {
let now = chrono::Utc::now();
TokenSnapshot {
generation: 1,
provenance: TokenProvenance::Minted {
minted_at: now,
expires_at: now + chrono::Duration::minutes(60),
},
}
}
fn declared(origin: &str, additional: &[&str]) -> (PreparedManifest, RunNamespace) {
let (prepared, mut resolved) = prepared_and_resolved_for_sandbox(
SandboxProviderKind::Local,
true,
Some(git_context(origin, "main")),
);
resolved.integrations.github = RunIntegrationsGithubSettings {
permissions: HashMap::from([(
"contents".to_string(),
InterpString::parse("read"),
)]),
additional_repositories: additional
.iter()
.map(|value| value.parse().expect("test slug should parse"))
.collect(),
};
(prepared, resolved)
}
fn pat_creds() -> fabro_github::GitHubCredentials {
fabro_github::GitHubCredentials::Pat("ghp_test".to_string())
}
#[tokio::test(start_paused = true)]
async fn reports_each_repository_primary_first_despite_probe_completion_order() {
let (prepared, resolved) = declared("https://github.com/acme/widgets", &[
"acme/zeta",
"acme/alpha",
]);
let minted = Arc::new(StdMutex::new(Vec::new()));
let minted_for_seam = Arc::clone(&minted);
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
Some(pat_creds()),
move |access, _creds| {
minted_for_seam.lock().unwrap().push(access);
async { Ok(static_token("scoped-token")) }
},
|slug, _token| async move {
// Invert completion order: the primary finishes last.
let delay = match slug.repo() {
"widgets" => 30,
"alpha" => 20,
_ => 10,
};
time::sleep(Duration::from_millis(delay)).await;
Ok(())
},
)
.await;
assert!(ok);
// One mint listing every repository with the shared permissions.
let minted = minted.lock().unwrap();
assert_eq!(minted.len(), 1);
assert_eq!(minted[0].repository_names(), vec![
"widgets", "alpha", "zeta"
]);
assert_eq!(
minted[0].permissions().get("contents").map(String::as_str),
Some("read")
);
let names: Vec<&str> = checks.iter().map(|check| check.name.as_str()).collect();
assert_eq!(names, vec![
"GitHub Token",
"GitHub Repository (acme/widgets)",
"GitHub Repository (acme/alpha)",
"GitHub Repository (acme/zeta)",
]);
assert!(checks.iter().all(|check| check.status == CheckStatus::Pass));
// The token never reaches check output.
for check in &checks {
let rendered = format!("{check:?}");
assert!(!rendered.contains("scoped-token"), "{rendered}");
}
}
#[tokio::test]
async fn installation_resolution_failure_names_only_the_inaccessible_repository() {
let (prepared, resolved) =
declared("https://github.com/acme/widgets", &["acme/keystone"]);
let probes = Arc::new(AtomicU64::new(0));
let probes_for_seam = Arc::clone(&probes);
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
Some(pat_creds()),
|_access, _creds| async {
Err(
"the GitHub App installation cannot see repository acme/keystone; add \
it to the installation's repository access"
.to_string(),
)
},
move |_slug, _token| {
probes_for_seam.fetch_add(1, Ordering::SeqCst);
async { Ok(()) }
},
)
.await;
assert!(!ok);
assert_eq!(
probes.load(Ordering::SeqCst),
0,
"no probes after a failed mint"
);
assert_eq!(checks.last().unwrap().name, "GitHub Token");
assert_eq!(checks.last().unwrap().status, CheckStatus::Error);
let remediation = checks.last().unwrap().remediation.as_deref().unwrap();
assert!(remediation.contains("acme/keystone"), "{remediation}");
assert!(!remediation.contains("acme/widgets"), "{remediation}");
}
#[tokio::test]
async fn successful_mint_with_failed_probe_still_fails() {
let (prepared, resolved) =
declared("https://github.com/acme/widgets", &["acme/keystone"]);
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
Some(pat_creds()),
|_access, _creds| async { Ok(static_token("scoped-token")) },
|slug, _token| async move {
if slug.repo() == "keystone" {
Err("remote: Repository not found.".to_string())
} else {
Ok(())
}
},
)
.await;
assert!(!ok);
let keystone = checks
.iter()
.find(|check| check.name == "GitHub Repository (acme/keystone)")
.expect("keystone probe result should be reported");
assert_eq!(keystone.status, CheckStatus::Error);
assert!(
!keystone
.remediation
.as_deref()
.unwrap_or_default()
.contains("scoped-token")
);
let widgets = checks
.iter()
.find(|check| check.name == "GitHub Repository (acme/widgets)")
.expect("primary probe result should be reported");
assert_eq!(widgets.status, CheckStatus::Pass);
}
#[tokio::test]
async fn resolved_gh_token_warns_without_failing() {
let (prepared, mut resolved) =
declared("https://github.com/acme/widgets", &["acme/keystone"]);
resolved
.environment
.env
.insert("GH_TOKEN".to_string(), InterpString::parse("user-token"));
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
Some(pat_creds()),
|_access, _creds| async { Ok(static_token("scoped-token")) },
|_slug, _token| async { Ok(()) },
)
.await;
assert!(ok, "a GH_TOKEN override warns but does not fail preflight");
let warning = checks
.iter()
.find(|check| check.name == "GH_TOKEN Override")
.expect("GH_TOKEN warning should be reported");
assert_eq!(warning.status, CheckStatus::Warning);
}
#[tokio::test]
async fn missing_origin_fails_for_declared_repositories() {
let (prepared, resolved) = {
let (mut prepared, resolved) =
declared("https://github.com/acme/widgets", &["acme/keystone"]);
prepared.git = None;
(prepared, resolved)
};
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
Some(pat_creds()),
|_access, _creds| async { Ok(static_token("scoped-token")) },
|_slug, _token| async { Ok(()) },
)
.await;
assert!(!ok);
let check = checks.last().unwrap();
assert_eq!(check.summary, "missing origin");
assert!(
check
.remediation
.as_deref()
.unwrap_or_default()
.contains("requires a GitHub run origin"),
"{:?}",
check.remediation
);
}
#[tokio::test]
async fn missing_credentials_fail_for_declared_repositories() {
let (prepared, resolved) =
declared("https://github.com/acme/widgets", &["acme/keystone"]);
let mut checks = Vec::new();
let ok = run_github_token_check_with(
&mut checks,
&prepared,
&resolved,
None,
|_access, _creds| async { Ok(static_token("scoped-token")) },
|_slug, _token| async { Ok(()) },
)
.await;
assert!(!ok);
assert_eq!(checks.last().unwrap().summary, "missing credentials");
}
#[tokio::test(start_paused = true)]
async fn replication_lag_failure_retries_with_the_same_token_and_succeeds() {
let attempts = Arc::new(AtomicU64::new(0));
let attempts_for_run = Arc::clone(&attempts);
let result = probe_with_replication_retry(fresh_minted_snapshot(), move || {
let attempts = Arc::clone(&attempts_for_run);
async move {
if attempts.fetch_add(1, Ordering::SeqCst) == 0 {
Err("remote: Repository not found.".to_string())
} else {
Ok(())
}
}
})
.await;
assert!(result.is_ok());
assert_eq!(attempts.load(Ordering::SeqCst), 2);
}
#[tokio::test(start_paused = true)]
async fn static_credential_auth_failures_do_not_retry() {
let attempts = Arc::new(AtomicU64::new(0));
let attempts_for_run = Arc::clone(&attempts);
let static_snapshot = TokenSnapshot {
generation: 0,
provenance: TokenProvenance::Static,
};
let result = probe_with_replication_retry(static_snapshot, move || {
let attempts = Arc::clone(&attempts_for_run);
async move {
attempts.fetch_add(1, Ordering::SeqCst);
Err("remote: Repository not found.".to_string())
}
})
.await;
assert!(result.is_err());
assert_eq!(
attempts.load(Ordering::SeqCst),
1,
"a 404 with a static credential cannot become valid by waiting"
);
}
}
}

View file

@ -4081,15 +4081,15 @@ async fn execute_run_in_process(state: Arc<AppState>, run_id: RunId) {
return;
}
};
let github_permissions = match persisted
let github_integration = match persisted
.run_spec()
.settings
.run
.integrations
.github
.resolve_permissions()
.resolve_integration()
{
Ok(permissions) => permissions,
Ok(integration) => integration,
Err(err) => {
tracing::error!(
run_id = %run_id,
@ -4131,7 +4131,7 @@ async fn execute_run_in_process(state: Arc<AppState>, run_id: RunId) {
artifact_sink: Some(ArtifactSink::Store(state.artifact_store.clone())),
run_control: None,
github_app,
github_permissions,
github_integration,
vault: Arc::new(AsyncRwLock::new(vault.into_vault())),
catalog: state.catalog(),
on_node: None,

View file

@ -25,6 +25,7 @@ fabro-http.workspace = true
fabro-redact.workspace = true
fabro-static.workspace = true
fabro-types = { path = "../../foundation/fabro-types" }
futures.workspace = true
jsonwebtoken.workspace = true
chrono.workspace = true
tracing.workspace = true

View file

@ -0,0 +1,587 @@
//! The validated effective repository set for one run's GitHub access.
//!
//! [`GitHubRepositoryAccess`] is the single value both server preflight and
//! workflow initialization construct from the run origin, the declared
//! additional repositories, and the resolved shared permissions — so the two
//! paths cannot disagree about which repositories a run's `GITHUB_TOKEN`
//! covers. It carries no token or key material.
use std::collections::{BTreeSet, HashMap};
use anyhow::{Context as _, bail};
use fabro_types::GitHubRepositorySlug;
use fabro_types::settings::run::RunIntegrationsGithubSettings;
use futures::stream::{self, StreamExt as _};
use crate::{GitHubAppCredentials, HttpClient, InstallationLookup, InstallationToken};
/// Keep GitHub installation lookups bounded while avoiding one network round
/// trip at a time for large declared repository sets.
const INSTALLATION_LOOKUP_CONCURRENCY: usize = 4;
/// The validated effective repository set for a run: the primary origin
/// repository plus zero or more distinct additional repositories, all with
/// one shared owner, and the shared permission map that scopes the token.
///
/// Secret-free by construction: `Debug` may render everywhere the run
/// pipeline logs.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitHubRepositoryAccess {
primary: GitHubRepositorySlug,
/// Sorted, deduplicated, primary excluded.
additional: Vec<GitHubRepositorySlug>,
permissions: HashMap<String, String>,
}
impl GitHubRepositoryAccess {
/// Build the effective access request.
///
/// Returns `Ok(None)` when no origin URL is available and no additional
/// repositories are declared — the legacy "nothing to scope" state whose
/// handling stays with the caller. Every declared-additional invariant is
/// enforced here:
///
/// - a declared additional set requires a GitHub origin,
/// - the additional set cannot contain the primary repository,
/// - every additional repository shares the primary's owner
/// (case-insensitive) because one App installation covers one account,
/// - a declared additional set requires interpolated permissions with
/// `contents = "read"` or `contents = "write"`.
pub fn new(
origin_url: Option<&str>,
additional_repositories: &BTreeSet<GitHubRepositorySlug>,
permissions: HashMap<String, String>,
) -> anyhow::Result<Option<Self>> {
let origin_url = origin_url.map(str::trim).filter(|url| !url.is_empty());
let Some(origin_url) = origin_url else {
if additional_repositories.is_empty() {
return Ok(None);
}
bail!(
"run.integrations.github.additional_repositories requires a GitHub run origin; \
this run has no repository origin URL"
);
};
let normalized = crate::normalize_repo_origin_url(origin_url);
let (owner, repo) = crate::parse_github_owner_repo(&normalized)
.context("parsing GitHub origin for repository access")?;
let Some(primary) = GitHubRepositorySlug::try_new(&format!("{owner}/{repo}")) else {
bail!("run origin does not name a valid GitHub `owner/repository`: {owner}/{repo}");
};
if !additional_repositories.is_empty() {
validate_additional_permissions(&permissions)?;
}
let mut additional = Vec::with_capacity(additional_repositories.len());
for slug in additional_repositories {
if *slug == primary {
bail!(
"run.integrations.github.additional_repositories must not repeat the run \
origin repository `{primary}` the origin is always included"
);
}
if !slug.same_owner(&primary) {
bail!(
"additional repository `{slug}` has owner `{}` but the run origin `{primary}` \
has owner `{}`; all repositories must share one owner because one GitHub App \
installation covers one account",
slug.owner(),
primary.owner()
);
}
additional.push(slug.clone());
}
Ok(Some(Self {
primary,
additional,
permissions,
}))
}
#[must_use]
pub fn primary(&self) -> &GitHubRepositorySlug {
&self.primary
}
/// Every repository in the effective set, primary first, then the
/// additional repositories in their deterministic sorted order.
#[must_use]
pub fn targets(&self) -> Vec<&GitHubRepositorySlug> {
std::iter::once(&self.primary)
.chain(self.additional.iter())
.collect()
}
/// Project each validated slug to its repository-name component for the
/// installation-token mint request, which accepts names within the
/// selected installation. Every target shares the primary's owner, so the
/// projection loses nothing.
#[must_use]
pub fn repository_names(&self) -> Vec<String> {
self.targets()
.into_iter()
.map(|slug| slug.repo().to_string())
.collect()
}
#[must_use]
pub fn owner(&self) -> &str {
self.primary.owner()
}
#[must_use]
pub fn permissions(&self) -> &HashMap<String, String> {
&self.permissions
}
pub fn permissions_json(&self) -> anyhow::Result<serde_json::Value> {
serde_json::to_value(&self.permissions).context("serializing GitHub permissions")
}
#[must_use]
pub fn has_additional_repositories(&self) -> bool {
!self.additional.is_empty()
}
/// Resolve every target's App installation and require one shared
/// installation ID, so a repository the App cannot see — or one that
/// resolves to a different installation — is named before any token is
/// minted. Targets are checked in deterministic primary-first order.
async fn resolve_shared_installation_with_jwt(
&self,
client: &impl HttpClient,
jwt: &str,
base_url: &str,
) -> anyhow::Result<u64> {
let targets: Vec<GitHubRepositorySlug> = self.targets().into_iter().cloned().collect();
let mut lookups = stream::iter(targets.into_iter().map(|slug| async move {
let lookup =
crate::lookup_installation(client, jwt, base_url, slug.owner(), slug.repo())
.await
.with_context(|| format!("looking up the GitHub App installation for {slug}"));
(slug, lookup)
}))
.buffered(INSTALLATION_LOOKUP_CONCURRENCY);
let mut shared: Option<(u64, GitHubRepositorySlug)> = None;
while let Some((slug, lookup)) = lookups.next().await {
let lookup = lookup?;
let id = match lookup {
InstallationLookup::Found(id) => id,
InstallationLookup::NotFound => bail!(
"the GitHub App installation cannot see repository {slug}; add it to the \
installation's repository access"
),
InstallationLookup::Failed(status) => bail!(
"unexpected status {status} looking up the GitHub App installation for {slug}"
),
};
match &shared {
None => shared = Some((id, slug)),
Some((shared_id, first)) if *shared_id != id => bail!(
"repository {slug} belongs to GitHub App installation {id} but {first} \
belongs to installation {shared_id}; all repositories must share one \
installation"
),
Some(_) => {}
}
}
let (id, _) = shared.expect("the effective repository set always contains the primary");
Ok(id)
}
/// Resolve every target to one installation, then mint one token scoped
/// to this exact repository set without looking up the primary twice.
pub(crate) async fn mint_installation_token(
&self,
creds: &GitHubAppCredentials,
client: &impl HttpClient,
base_url: &str,
) -> anyhow::Result<InstallationToken> {
let jwt = crate::sign_app_jwt(&creds.app_id, &creds.private_key_pem)?;
let installation_id = self
.resolve_shared_installation_with_jwt(client, &jwt, base_url)
.await
.context("resolving the shared GitHub App installation")?;
crate::mint_installation_token_for_id_with_jwt(
client,
&jwt,
installation_id,
&self.repository_names(),
base_url,
self.permissions_json()?,
)
.await
}
}
/// A non-empty additional set needs a token that can reach repository
/// contents. Configuration resolution already checked literal values; this
/// is the runtime re-check after `{{ vars.* }}` interpolation.
fn validate_additional_permissions(permissions: &HashMap<String, String>) -> anyhow::Result<()> {
let Some(contents) = permissions.get("contents") else {
bail!(
"run.integrations.github.additional_repositories requires the `contents` permission \
(`read` or `write`)"
);
};
if !RunIntegrationsGithubSettings::contents_permission_allows_repository_access(contents) {
bail!(
"run.integrations.github.additional_repositories requires `contents = \"read\"` or \
`contents = \"write\"`, got `{contents}`"
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn slugs(values: &[&str]) -> BTreeSet<GitHubRepositorySlug> {
values
.iter()
.map(|value| value.parse().expect("test slug should parse"))
.collect()
}
fn contents_read() -> HashMap<String, String> {
HashMap::from([("contents".to_string(), "read".to_string())])
}
fn access(
origin: &str,
additional: &[&str],
permissions: HashMap<String, String>,
) -> anyhow::Result<Option<GitHubRepositoryAccess>> {
GitHubRepositoryAccess::new(Some(origin), &slugs(additional), permissions)
}
#[test]
fn https_and_both_ssh_origin_forms_normalize_to_the_same_primary() {
let origins = [
"https://github.com/fabro-sh/fabro.git",
"git@github.com:fabro-sh/fabro.git",
"ssh://git@github.com/fabro-sh/fabro.git",
"https://github.com/fabro-sh/fabro",
];
for origin in origins {
let access = access(origin, &[], HashMap::new())
.expect(origin)
.expect("origin should produce an access value");
assert_eq!(access.primary().to_string(), "fabro-sh/fabro", "{origin}");
}
}
#[test]
fn no_origin_and_no_additional_repositories_is_none() {
let access = GitHubRepositoryAccess::new(None, &BTreeSet::new(), HashMap::new()).unwrap();
assert!(access.is_none());
let blank =
GitHubRepositoryAccess::new(Some(" "), &BTreeSet::new(), HashMap::new()).unwrap();
assert!(blank.is_none());
}
#[test]
fn additional_repositories_require_an_origin() {
let err =
GitHubRepositoryAccess::new(None, &slugs(&["fabro-sh/keystone"]), contents_read())
.unwrap_err();
assert!(
err.to_string().contains("requires a GitHub run origin"),
"{err:#}"
);
}
#[test]
fn additional_repositories_require_a_github_origin() {
let err = access(
"https://gitlab.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
contents_read(),
)
.unwrap_err();
assert!(err.to_string().contains("repository access"), "{err:#}");
}
#[test]
fn rejects_primary_duplication_regardless_of_url_spelling_or_case() {
let origins = [
"https://github.com/Fabro-SH/Fabro.git",
"git@github.com:fabro-sh/fabro.git",
"ssh://git@github.com/fabro-sh/fabro",
];
for origin in origins {
let err = access(origin, &["fabro-sh/FABRO"], contents_read()).unwrap_err();
assert!(
err.to_string().contains("must not repeat the run origin"),
"{origin}: {err:#}"
);
}
}
#[test]
fn rejects_an_additional_repository_with_a_different_owner() {
let err = access(
"https://github.com/fabro-sh/fabro",
&["lithoscomputer/conveyor"],
contents_read(),
)
.unwrap_err();
let message = err.to_string();
assert!(message.contains("lithoscomputer/conveyor"), "{message}");
assert!(message.contains("share one owner"), "{message}");
}
#[test]
fn rejects_a_declared_set_without_a_contents_permission() {
let missing = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
HashMap::new(),
)
.unwrap_err();
assert!(
missing.to_string().contains("`contents` permission"),
"{missing:#}"
);
let wrong_level = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
HashMap::from([("contents".to_string(), "admin".to_string())]),
)
.unwrap_err();
assert!(
wrong_level.to_string().contains("got `admin`"),
"{wrong_level:#}"
);
}
#[test]
fn targets_retain_every_full_slug_exactly_once_primary_first() {
let access = access(
"git@github.com:fabro-sh/fabro.git",
&["fabro-sh/keystone", "fabro-sh/arc"],
contents_read(),
)
.unwrap()
.unwrap();
let targets: Vec<String> = access.targets().iter().map(ToString::to_string).collect();
assert_eq!(targets, vec![
"fabro-sh/fabro",
"fabro-sh/arc",
"fabro-sh/keystone",
]);
assert_eq!(access.repository_names(), vec!["fabro", "arc", "keystone"]);
assert_eq!(access.owner(), "fabro-sh");
assert!(access.has_additional_repositories());
}
#[test]
fn debug_output_contains_only_repositories_and_permissions() {
let access = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/arc"],
contents_read(),
)
.unwrap()
.unwrap();
let rendered = format!("{access:?}");
assert!(rendered.contains("fabro-sh"), "{rendered}");
assert!(rendered.contains("contents"), "{rendered}");
// The value carries no token or key material by construction; its
// fields are exactly the repository slugs and the permission map.
assert!(!rendered.to_lowercase().contains("token"), "{rendered}");
assert!(!rendered.to_lowercase().contains("key"), "{rendered}");
}
#[tokio::test]
async fn resolve_shared_installation_names_the_invisible_repository() {
use crate::HttpMethod;
use crate::tests_mock::{MockHttpClient, test_rsa_key};
let mock = MockHttpClient::new()
.on(
HttpMethod::Get,
"/repos/fabro-sh/fabro/installation",
200,
r#"{"id": 7}"#,
)
.on(
HttpMethod::Get,
"/repos/fabro-sh/keystone/installation",
404,
"{}",
);
let creds = GitHubAppCredentials {
app_id: "test".to_string(),
private_key_pem: test_rsa_key().to_string(),
slug: None,
};
let access = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
contents_read(),
)
.unwrap()
.unwrap();
let err = access
.resolve_shared_installation_with_jwt(
&mock,
&crate::sign_app_jwt(&creds.app_id, &creds.private_key_pem).unwrap(),
"",
)
.await
.unwrap_err();
let message = err.to_string();
assert!(message.contains("fabro-sh/keystone"), "{message}");
assert!(message.contains("cannot see"), "{message}");
}
#[tokio::test]
async fn resolve_shared_installation_requires_one_installation_id() {
use crate::HttpMethod;
use crate::tests_mock::{MockHttpClient, test_rsa_key};
let mock = MockHttpClient::new()
.on(
HttpMethod::Get,
"/repos/fabro-sh/fabro/installation",
200,
r#"{"id": 7}"#,
)
.on(
HttpMethod::Get,
"/repos/fabro-sh/keystone/installation",
200,
r#"{"id": 8}"#,
);
let creds = GitHubAppCredentials {
app_id: "test".to_string(),
private_key_pem: test_rsa_key().to_string(),
slug: None,
};
let access = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
contents_read(),
)
.unwrap()
.unwrap();
let err = access
.resolve_shared_installation_with_jwt(
&mock,
&crate::sign_app_jwt(&creds.app_id, &creds.private_key_pem).unwrap(),
"",
)
.await
.unwrap_err();
let message = err.to_string();
assert!(message.contains("installation 8"), "{message}");
assert!(message.contains("fabro-sh/keystone"), "{message}");
}
#[tokio::test]
async fn resolve_shared_installation_returns_the_shared_id() {
use crate::HttpMethod;
use crate::tests_mock::{MockHttpClient, test_rsa_key};
let mock = MockHttpClient::new()
.on(
HttpMethod::Get,
"/repos/fabro-sh/fabro/installation",
200,
r#"{"id": 7}"#,
)
.on(
HttpMethod::Get,
"/repos/fabro-sh/keystone/installation",
200,
r#"{"id": 7}"#,
);
let creds = GitHubAppCredentials {
app_id: "test".to_string(),
private_key_pem: test_rsa_key().to_string(),
slug: None,
};
let access = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
contents_read(),
)
.unwrap()
.unwrap();
let id = access
.resolve_shared_installation_with_jwt(
&mock,
&crate::sign_app_jwt(&creds.app_id, &creds.private_key_pem).unwrap(),
"",
)
.await
.unwrap();
assert_eq!(id, 7);
}
#[tokio::test]
async fn access_mint_reuses_the_resolved_installation_id() {
use crate::HttpMethod;
use crate::tests_mock::{MockHttpClient, test_rsa_key};
let mock = MockHttpClient::new()
.on(
HttpMethod::Get,
"/repos/fabro-sh/fabro/installation",
200,
r#"{"id": 7}"#,
)
.on(
HttpMethod::Get,
"/repos/fabro-sh/keystone/installation",
200,
r#"{"id": 7}"#,
)
.on(
HttpMethod::Post,
"/app/installations/7/access_tokens",
201,
r#"{"token":"scoped","expires_at":"2099-01-01T00:00:00Z"}"#,
)
.with_req_body(
r#"{"repositories":["fabro","keystone"],"permissions":{"contents":"read"}}"#,
);
let creds = GitHubAppCredentials {
app_id: "test".to_string(),
private_key_pem: test_rsa_key().to_string(),
slug: None,
};
let access = access(
"https://github.com/fabro-sh/fabro",
&["fabro-sh/keystone"],
contents_read(),
)
.unwrap()
.unwrap();
let token = access
.mint_installation_token(&creds, &mock, "")
.await
.unwrap();
assert_eq!(token.token, "scoped");
assert_eq!(
mock.request_count(),
3,
"each repository should be looked up once before the mint"
);
}
}

View file

@ -9,13 +9,45 @@ use fabro_types::settings::run::MergeStrategy;
use serde::Deserialize;
use tokio::process::Command;
pub mod access;
pub mod token_source;
#[cfg(any(test, feature = "test-support"))]
pub mod test_support;
#[cfg(test)]
pub(crate) mod tests_mock;
pub use access::GitHubRepositoryAccess;
pub const GITHUB_API_BASE_URL: &str = "https://api.github.com";
/// Git config key that routes github.com HTTPS credentials through
/// [`GITHUB_CREDENTIAL_HELPER`].
pub const GITHUB_CREDENTIAL_HELPER_KEY: &str = "credential.https://github.com.helper";
/// Secret-free git credential helper: reads `$GITHUB_TOKEN` from the
/// invoking git process's environment at invocation time, so the token never
/// lands in git configuration, argv, or rendered errors. Non-`get`
/// operations (`store`, `erase`) are ignored. The one definition shared by
/// the runtime git bridge, server preflight probes, and the live contract
/// test, so the probes always exercise exactly what the bridge configures.
pub const GITHUB_CREDENTIAL_HELPER: &str = r#"!f() { if [ "$1" = get ]; then echo username=x-access-token; echo "password=$GITHUB_TOKEN"; fi; }; f"#;
/// Configure a `git` invocation to authenticate to github.com through
/// [`GITHUB_CREDENTIAL_HELPER`] with `token`, isolated from user/system git
/// configuration and terminal prompts. The token is passed only through the
/// child process environment.
pub fn apply_probe_git_env(command: &mut Command, token: &str) {
command
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env(EnvVars::GITHUB_TOKEN, token)
.env("GIT_CONFIG_COUNT", "1")
.env("GIT_CONFIG_KEY_0", GITHUB_CREDENTIAL_HELPER_KEY)
.env("GIT_CONFIG_VALUE_0", GITHUB_CREDENTIAL_HELPER);
}
/// Returns the GitHub API base URL, allowing override via `GITHUB_BASE_URL` env
/// var.
#[expect(
@ -151,6 +183,29 @@ impl GitHubAppCredentials {
base_url: &str,
permissions: serde_json::Value,
install_url: Option<&str>,
) -> anyhow::Result<InstallationToken> {
self.mint_installation_token_for_repositories(
client,
owner,
&[repo.to_string()],
base_url,
permissions,
install_url,
)
.await
}
/// Mint one installation token scoped to every repository in
/// `repository_names` (names within `owner`'s installation, primary
/// first) with the shared `permissions`.
pub async fn mint_installation_token_for_repositories(
&self,
client: &impl HttpClient,
owner: &str,
repository_names: &[String],
base_url: &str,
permissions: serde_json::Value,
install_url: Option<&str>,
) -> anyhow::Result<InstallationToken> {
let jwt = sign_app_jwt(&self.app_id, &self.private_key_pem)?;
let default_install_url = self.installation_url(owner);
@ -159,7 +214,7 @@ impl GitHubAppCredentials {
client,
&jwt,
owner,
repo,
repository_names,
base_url,
permissions,
install_url,
@ -475,47 +530,88 @@ pub async fn create_installation_access_token_with_permissions_and_install_url(
permissions: serde_json::Value,
install_url: Option<&str>,
) -> anyhow::Result<String> {
mint_installation_token_with_jwt(client, jwt, owner, repo, base_url, permissions, install_url)
.await
.map(|token| token.token)
mint_installation_token_with_jwt(
client,
jwt,
owner,
&[repo.to_string()],
base_url,
permissions,
install_url,
)
.await
.map(|token| token.token)
}
/// Outcome of one GitHub App installation lookup
/// (`GET /repos/{owner}/{repo}/installation`).
///
/// Status interpretation stays with callers because their user guidance
/// differs: the mint path speaks about the owner's App installation, the
/// multi-repository resolution names the specific repository.
pub(crate) enum InstallationLookup {
Found(u64),
/// 404: the App cannot see the repository (or is not installed at all).
NotFound,
/// Any other non-200 status.
Failed(u16),
}
/// Look up the App installation covering `owner/repo` and parse its id.
/// Transport and parse failures carry no caller-specific context; callers
/// attach their own.
pub(crate) async fn lookup_installation(
client: &impl HttpClient,
jwt: &str,
base_url: &str,
owner: &str,
repo: &str,
) -> anyhow::Result<InstallationLookup> {
#[derive(Deserialize)]
struct Installation {
id: u64,
}
let endpoint = format!("{base_url}/repos/{owner}/{repo}/installation");
let auth = format!("Bearer {jwt}");
let resp = client
.request(HttpMethod::Get, &endpoint, &github_headers(&auth), None)
.await?;
match resp.status {
200 => {
let installation: Installation = resp
.json()
.context("Failed to parse installation response")?;
Ok(InstallationLookup::Found(installation.id))
}
404 => Ok(InstallationLookup::NotFound),
status => Ok(InstallationLookup::Failed(status)),
}
}
async fn mint_installation_token_with_jwt(
client: &impl HttpClient,
jwt: &str,
owner: &str,
repo: &str,
repos: &[String],
base_url: &str,
permissions: serde_json::Value,
install_url: Option<&str>,
) -> anyhow::Result<InstallationToken> {
#[derive(Deserialize)]
struct Installation {
id: u64,
}
let Some(primary_repo) = repos.first() else {
bail!("installation token mint requires at least one repository");
};
#[derive(Deserialize)]
struct AccessToken {
token: String,
expires_at: DateTime<Utc>,
}
// Step 1: Find the installation for this repo
let installation_endpoint = format!("{base_url}/repos/{owner}/{repo}/installation");
let auth = format!("Bearer {jwt}");
let resp = client
.request(
HttpMethod::Get,
&installation_endpoint,
&github_headers(&auth),
None,
)
// Step 1: Find the installation via the primary repository. Multi-
// repository callers resolve every repository's installation up front
// (`GitHubRepositoryAccess::resolve_shared_installation`), so the
// primary stands for the whole set here.
let installation_id = match lookup_installation(client, jwt, base_url, owner, primary_repo)
.await
.context("Failed to look up GitHub App installation")?;
match resp.status {
200 => {}
404 => {
.context("Failed to look up GitHub App installation")?
{
InstallationLookup::Found(id) => id,
InstallationLookup::NotFound => {
let install_url = install_url.map_or_else(
|| format!("https://github.com/organizations/{owner}/settings/installations"),
str::to_string,
@ -525,37 +621,54 @@ async fn mint_installation_token_with_jwt(
Install it at {install_url}"
);
}
403 => {
InstallationLookup::Failed(403) => {
bail!(
"GitHub App installation is suspended. \
Re-enable it in your organization's GitHub App settings."
);
}
401 => {
InstallationLookup::Failed(401) => {
bail!(
"GitHub App authentication failed. \
Check that app_id and GITHUB_APP_PRIVATE_KEY are correct."
);
}
_ => {
bail!(
"Unexpected status {} looking up GitHub App installation",
resp.status
);
InstallationLookup::Failed(status) => {
bail!("Unexpected status {status} looking up GitHub App installation");
}
};
mint_installation_token_for_id_with_jwt(
client,
jwt,
installation_id,
repos,
base_url,
permissions,
)
.await
}
/// Create a repository-scoped token for an installation already resolved by
/// the caller.
pub(crate) async fn mint_installation_token_for_id_with_jwt(
client: &impl HttpClient,
jwt: &str,
installation_id: u64,
repos: &[String],
base_url: &str,
permissions: serde_json::Value,
) -> anyhow::Result<InstallationToken> {
#[derive(Deserialize)]
struct AccessToken {
token: String,
expires_at: DateTime<Utc>,
}
let installation: Installation = resp
.json()
.context("Failed to parse installation response")?;
// Step 2: Create a scoped access token
let token_url = format!(
"{base_url}/app/installations/{}/access_tokens",
installation.id
);
let auth = format!("Bearer {jwt}");
let token_url = format!("{base_url}/app/installations/{installation_id}/access_tokens");
let body = serde_json::json!({
"repositories": [repo],
"repositories": repos,
"permissions": permissions,
});
@ -573,8 +686,9 @@ async fn mint_installation_token_with_jwt(
201 => {}
422 => {
bail!(
"GitHub App does not have access to repository {repo}. \
Update the installation's repository permissions to include it."
"GitHub App does not have access to every requested repository ({}). \
Update the installation's repository permissions to include them.",
repos.join(", ")
);
}
401 => {
@ -1065,25 +1179,24 @@ pub async fn check_app_installed(
repo: &str,
base_url: &str,
) -> anyhow::Result<bool> {
let url = format!("{base_url}/repos/{owner}/{repo}/installation");
let auth = format!("Bearer {jwt}");
let resp = client
.request(HttpMethod::Get, &url, &github_headers(&auth), None)
let lookup = lookup_installation(client, jwt, base_url, owner, repo)
.await
.context("Failed to check GitHub App installation")?;
match resp.status {
200 => Ok(true),
404 => Ok(false),
401 => bail!(
match lookup {
InstallationLookup::Found(_) => Ok(true),
InstallationLookup::NotFound => Ok(false),
InstallationLookup::Failed(401) => bail!(
"GitHub App authentication failed. \
Check that app_id and GITHUB_APP_PRIVATE_KEY are correct."
),
403 => bail!(
InstallationLookup::Failed(403) => bail!(
"GitHub App installation is suspended. \
Re-enable it in your organization's GitHub App settings."
),
status => bail!("Unexpected status {status} checking GitHub App installation"),
InstallationLookup::Failed(status) => {
bail!("Unexpected status {status} checking GitHub App installation")
}
}
}
@ -1715,7 +1828,7 @@ mod tests {
// -----------------------------------------------------------------------
fn test_rsa_key() -> &'static str {
include_str!("testdata/rsa_private.pem")
tests_mock::test_rsa_key()
}
#[test]
@ -1769,89 +1882,7 @@ mod tests {
// MockHttpClient
// -----------------------------------------------------------------------
struct MockRoute {
method: HttpMethod,
path: String,
status: u16,
response_body: String,
assert_header: Option<(String, MockHeaderCheck)>,
assert_body_json: Option<serde_json::Value>,
}
enum MockHeaderCheck {
Equals(String),
}
struct MockHttpClient {
routes: Vec<MockRoute>,
}
impl MockHttpClient {
fn new() -> Self {
Self { routes: vec![] }
}
fn on(mut self, method: HttpMethod, path: &str, status: u16, body: &str) -> Self {
self.routes.push(MockRoute {
method,
path: path.to_string(),
status,
response_body: body.to_string(),
assert_header: None,
assert_body_json: None,
});
self
}
fn with_req_header(mut self, name: &str, value: &str) -> Self {
self.routes.last_mut().unwrap().assert_header =
Some((name.to_string(), MockHeaderCheck::Equals(value.to_string())));
self
}
fn with_req_body(mut self, json_str: &str) -> Self {
self.routes.last_mut().unwrap().assert_body_json =
Some(serde_json::from_str(json_str).unwrap());
self
}
}
impl HttpClient for MockHttpClient {
async fn request(
&self,
method: HttpMethod,
url: &str,
headers: &[(&str, &str)],
body: Option<&serde_json::Value>,
) -> anyhow::Result<HttpResponse> {
for route in &self.routes {
if method == route.method && url.ends_with(&route.path) {
if let Some((name, MockHeaderCheck::Equals(expected))) = &route.assert_header {
let (_, v) = headers
.iter()
.find(|(k, _)| *k == name.as_str())
.unwrap_or_else(|| {
panic!("Expected header '{name}' not found in request to {url}")
});
assert_eq!(*v, expected.as_str(), "Header '{name}' mismatch for {url}");
}
if let Some(expected_body) = &route.assert_body_json {
let actual = body.expect("Expected request body");
assert_eq!(actual, expected_body, "Request body mismatch for {url}");
}
return Ok(HttpResponse::new(route.status, route.response_body.clone()));
}
}
panic!(
"No mock route for {:?} {url}\nRegistered routes: {:?}",
method,
self.routes
.iter()
.map(|r| format!("{:?} {}", r.method, r.path))
.collect::<Vec<_>>()
);
}
}
use crate::tests_mock::{self, MockHttpClient};
// -----------------------------------------------------------------------
// create_installation_access_token — success
@ -1901,6 +1932,62 @@ mod tests {
);
}
/// The multi-repository mint sends one request listing every projected
/// repository name exactly once, primary first, with the shared
/// permissions; the installation lookup uses the primary repository.
#[tokio::test]
async fn multi_repository_mint_lists_every_repository_name_once() {
let access = GitHubRepositoryAccess::new(
Some("git@github.com:owner/repo.git"),
&[
"owner/keystone".parse().unwrap(),
"owner/arc".parse().unwrap(),
]
.into_iter()
.collect(),
std::collections::HashMap::from([("contents".to_string(), "read".to_string())]),
)
.unwrap()
.expect("origin should produce an access value");
let mock = MockHttpClient::new()
.on(
HttpMethod::Get,
"/repos/owner/repo/installation",
200,
r#"{"id": 123}"#,
)
.on(
HttpMethod::Post,
"/app/installations/123/access_tokens",
201,
r#"{"token": "ghs_multi", "expires_at": "2026-01-01T12:00:00Z"}"#,
)
.with_req_body(
r#"{"permissions":{"contents":"read"},"repositories":["repo","arc","keystone"]}"#,
);
let creds = GitHubAppCredentials {
app_id: "test".to_string(),
private_key_pem: test_rsa_key().to_string(),
slug: None,
};
let token = creds
.mint_installation_token_for_repositories(
&mock,
access.owner(),
&access.repository_names(),
"",
access.permissions_json().unwrap(),
None,
)
.await
.unwrap();
assert_eq!(token.token, "ghs_multi");
}
#[tokio::test]
async fn create_iat_requests_only_contents_write() {
let mock = MockHttpClient::new()

View file

@ -0,0 +1,104 @@
//! Crate-internal test doubles shared by the `lib.rs` and `access` test
//! modules: a scripted [`HttpClient`] and a throwaway RSA key for JWT
//! signing.
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::{HttpClient, HttpMethod, HttpResponse};
pub(crate) fn test_rsa_key() -> &'static str {
include_str!("testdata/rsa_private.pem")
}
pub(crate) struct MockRoute {
method: HttpMethod,
path: String,
status: u16,
response_body: String,
assert_header: Option<(String, MockHeaderCheck)>,
assert_body_json: Option<serde_json::Value>,
}
pub(crate) enum MockHeaderCheck {
Equals(String),
}
pub(crate) struct MockHttpClient {
routes: Vec<MockRoute>,
request_count: AtomicUsize,
}
impl MockHttpClient {
pub(crate) fn new() -> Self {
Self {
routes: vec![],
request_count: AtomicUsize::new(0),
}
}
pub(crate) fn on(mut self, method: HttpMethod, path: &str, status: u16, body: &str) -> Self {
self.routes.push(MockRoute {
method,
path: path.to_string(),
status,
response_body: body.to_string(),
assert_header: None,
assert_body_json: None,
});
self
}
pub(crate) fn with_req_header(mut self, name: &str, value: &str) -> Self {
self.routes.last_mut().unwrap().assert_header =
Some((name.to_string(), MockHeaderCheck::Equals(value.to_string())));
self
}
pub(crate) fn with_req_body(mut self, json_str: &str) -> Self {
self.routes.last_mut().unwrap().assert_body_json =
Some(serde_json::from_str(json_str).unwrap());
self
}
pub(crate) fn request_count(&self) -> usize {
self.request_count.load(Ordering::SeqCst)
}
}
impl HttpClient for MockHttpClient {
async fn request(
&self,
method: HttpMethod,
url: &str,
headers: &[(&str, &str)],
body: Option<&serde_json::Value>,
) -> anyhow::Result<HttpResponse> {
self.request_count.fetch_add(1, Ordering::SeqCst);
for route in &self.routes {
if method == route.method && url.ends_with(&route.path) {
if let Some((name, MockHeaderCheck::Equals(expected))) = &route.assert_header {
let (_, v) = headers
.iter()
.find(|(k, _)| *k == name.as_str())
.unwrap_or_else(|| {
panic!("Expected header '{name}' not found in request to {url}")
});
assert_eq!(*v, expected.as_str(), "Header '{name}' mismatch for {url}");
}
if let Some(expected_body) = &route.assert_body_json {
let actual = body.expect("Expected request body");
assert_eq!(actual, expected_body, "Request body mismatch for {url}");
}
return Ok(HttpResponse::new(route.status, route.response_body.clone()));
}
}
panic!(
"No mock route for {:?} {url}\nRegistered routes: {:?}",
method,
self.routes
.iter()
.map(|r| format!("{:?} {}", r.method, r.path))
.collect::<Vec<_>>()
);
}
}

View file

@ -15,7 +15,7 @@ use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Context as _;
use anyhow::{Context as _, bail};
use chrono::{DateTime, Utc};
use tokio::sync::Mutex;
@ -146,29 +146,54 @@ pub(crate) trait InstallationTokenMinter: Send + Sync {
async fn mint(&self) -> anyhow::Result<InstallationToken>;
}
/// Repository scope an App-backed source owns.
enum AppTokenScope {
/// A single-repository source that resolves its installation during each
/// mint.
Repository {
owner: String,
repo: String,
permissions: serde_json::Value,
},
/// A validated declared set. Each mint resolves every target to one App
/// installation before creating the token.
Access(crate::GitHubRepositoryAccess),
}
/// Real minter backed by GitHub App credentials.
struct AppTokenMinter {
creds: GitHubAppCredentials,
http: fabro_http::HttpClient,
owner: String,
repo: String,
base_url: String,
permissions: serde_json::Value,
creds: GitHubAppCredentials,
http: fabro_http::HttpClient,
base_url: String,
scope: AppTokenScope,
}
#[async_trait::async_trait]
impl InstallationTokenMinter for AppTokenMinter {
async fn mint(&self) -> anyhow::Result<InstallationToken> {
self.creds
.mint_installation_token(
&self.http,
&self.owner,
&self.repo,
&self.base_url,
self.permissions.clone(),
None,
)
.await
match &self.scope {
AppTokenScope::Repository {
owner,
repo,
permissions,
} => {
self.creds
.mint_installation_token(
&self.http,
owner,
repo,
&self.base_url,
permissions.clone(),
None,
)
.await
}
AppTokenScope::Access(access) => {
access
.mint_installation_token(&self.creds, &self.http, &self.base_url)
.await
}
}
}
}
@ -220,6 +245,17 @@ pub struct InstallationTokenSource {
state: SourceState,
}
fn repository_set_display(owner: &str, repos: &[String]) -> anyhow::Result<String> {
match repos {
[primary] => Ok(format!("{owner}/{primary}")),
[primary, additional @ ..] => Ok(format!(
"{owner}/{primary} (+{} additional)",
additional.len()
)),
[] => bail!("token source requires at least one repository"),
}
}
impl InstallationTokenSource {
/// Build a source for `creds` against the repository in `origin_url`.
///
@ -244,6 +280,32 @@ impl InstallationTokenSource {
permissions: serde_json::Value,
) -> anyhow::Result<Arc<Self>> {
let repo_display = format!("{owner}/{repo}");
Self::with_app_scope(creds, repo_display, AppTokenScope::Repository {
owner,
repo,
permissions,
})
}
/// Build a source for a validated effective repository set. Minted
/// tokens are scoped to every repository in the set with the shared
/// permissions. App-backed sources also resolve every repository to one
/// shared installation before each mint. Caching, refresh margin, and
/// single-flight behavior are identical to the single-repository source.
pub fn for_access(
creds: &GitHubCredentials,
access: &crate::GitHubRepositoryAccess,
) -> anyhow::Result<Arc<Self>> {
let repository_names = access.repository_names();
let repo_display = repository_set_display(access.owner(), &repository_names)?;
Self::with_app_scope(creds, repo_display, AppTokenScope::Access(access.clone()))
}
fn with_app_scope(
creds: &GitHubCredentials,
repo_display: String,
scope: AppTokenScope,
) -> anyhow::Result<Arc<Self>> {
let state = match creds {
GitHubCredentials::Pat(token) => SourceState::Pat(SecretString::new(token.clone())),
GitHubCredentials::Installation(token) => SourceState::Installation(token.clone()),
@ -255,10 +317,8 @@ impl InstallationTokenSource {
minter: Box::new(AppTokenMinter {
creds: app.clone(),
http,
owner,
repo,
base_url: crate::github_api_base_url(),
permissions,
scope,
}),
cache: Mutex::new(None),
}
@ -502,6 +562,31 @@ mod tests {
assert!(!source.mints_installation_tokens());
}
/// A source built from a validated multi-repository access value uses the
/// same state machine as the single-repository constructor: static
/// credentials pass through, and App credentials share the cache
/// machinery exercised by the `with_minter` tests below.
#[tokio::test]
async fn for_access_source_resolves_like_the_single_repository_source() {
let access = crate::GitHubRepositoryAccess::new(
Some("https://github.com/owner/repo.git"),
&["owner/keystone".parse().unwrap()].into_iter().collect(),
std::collections::HashMap::from([("contents".to_string(), "read".to_string())]),
)
.unwrap()
.expect("origin should produce an access value");
let source = InstallationTokenSource::for_access(
&GitHubCredentials::Pat("ghp_pat".to_string()),
&access,
)
.unwrap();
let resolved = source.resolve().await.unwrap();
assert_eq!(resolved.token.expose(), "ghp_pat");
assert!(resolved.snapshot.is_static());
}
#[tokio::test]
async fn static_installation_token_resolves_until_expiry() {
let valid = InstallationTokenSource::for_origin(

View file

@ -0,0 +1,122 @@
//! Opt-in live GitHub App test for additional-repository access.
//!
//! Verifies against the real GitHub API that one installation token scoped
//! to the primary repository plus one declared additional repository grants
//! Git read access to both. Runs only in live mode with these variables set
//! (it skips clearly otherwise):
//!
//! - `FABRO_TEST_GITHUB_APP_ID` — GitHub App id
//! - `GITHUB_APP_PRIVATE_KEY` — App private key (PEM, or base64-encoded PEM)
//! - `FABRO_TEST_GITHUB_ORIGIN` — HTTPS origin URL of the primary repository
//! - `FABRO_TEST_GITHUB_ADDITIONAL_REPO` — an `owner/repository` slug the
//! installation can see, ideally private, sharing the origin's owner
//!
//! The repositories come from the environment so no private slug is baked
//! into durable test output, and the minted token is only ever passed to
//! `git` through the child process environment.
use std::collections::BTreeSet;
use std::process::Stdio;
use std::time::Duration;
use fabro_github::token_source::InstallationTokenSource;
use fabro_github::{GitHubAppCredentials, GitHubCredentials, GitHubRepositoryAccess};
use fabro_types::GitHubRepositorySlug;
use tokio::process::Command;
use tokio::time::sleep;
fn env_var(name: &str) -> String {
#[expect(
clippy::disallowed_methods,
reason = "live e2e configuration comes from the process environment by design"
)]
std::env::var(name).unwrap_or_else(|_| panic!("{name} must be set for this live test"))
}
async fn ls_remote_with_token(slug: &GitHubRepositorySlug, token: &str) -> bool {
let url = slug.https_url();
let mut command = Command::new("git");
fabro_github::apply_probe_git_env(&mut command, token);
let output = command
.args(["ls-remote", &url, "HEAD"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()
.await
.expect("git should run");
output.status.success()
}
#[fabro_macros::e2e_test(
live("FABRO_TEST_GITHUB_APP_ID"),
live("GITHUB_APP_PRIVATE_KEY"),
live("FABRO_TEST_GITHUB_ORIGIN"),
live("FABRO_TEST_GITHUB_ADDITIONAL_REPO")
)]
async fn scoped_token_reaches_the_declared_additional_repository() {
let app_id = env_var("FABRO_TEST_GITHUB_APP_ID");
let app = GitHubAppCredentials::from_env(Some(&app_id))
.expect("GITHUB_APP_PRIVATE_KEY should decode as PEM or base64 PEM")
.expect("GITHUB_APP_PRIVATE_KEY must be set for this live test");
let origin = env_var("FABRO_TEST_GITHUB_ORIGIN");
let additional: GitHubRepositorySlug = env_var("FABRO_TEST_GITHUB_ADDITIONAL_REPO")
.parse()
.expect("FABRO_TEST_GITHUB_ADDITIONAL_REPO must be an owner/repository slug");
let additional_set: BTreeSet<GitHubRepositorySlug> = [additional.clone()].into_iter().collect();
let access = GitHubRepositoryAccess::new(
Some(&origin),
&additional_set,
std::collections::HashMap::from([("contents".to_string(), "read".to_string())]),
)
.expect("access request should validate")
.expect("origin should produce an access value");
// The production choreography: every target resolves to one shared
// installation, then one mint scoped to the whole effective set.
let creds = GitHubCredentials::App(app.clone());
let source =
InstallationTokenSource::for_access(&creds, &access).expect("token source should build");
let resolved = source.resolve().await.expect("scoped mint should succeed");
let token = resolved.token.expose();
// The one token reads both the primary and the additional repository.
// A freshly minted token can hit GitHub's replication lag, so retry a
// few times with the same token before failing.
for slug in access.targets() {
let mut reachable = false;
for _ in 0..3 {
if ls_remote_with_token(slug, token).await {
reachable = true;
break;
}
sleep(Duration::from_secs(2)).await;
}
assert!(
reachable,
"scoped token should read every declared repository"
);
}
// Negative scope check: a token minted for the primary alone must not
// read the additional repository (proves server-side scoping, not just
// possession of a token).
let primary_only = GitHubRepositoryAccess::new(
Some(&origin),
&BTreeSet::new(),
std::collections::HashMap::from([("contents".to_string(), "read".to_string())]),
)
.expect("primary-only access should validate")
.expect("origin should produce an access value");
let narrow_source =
InstallationTokenSource::for_access(&GitHubCredentials::App(app), &primary_only)
.expect("primary-only token source should build");
let narrow = narrow_source
.resolve()
.await
.expect("primary-only mint should succeed");
assert!(
!ls_remote_with_token(&additional, narrow.token.expose()).await,
"a primary-only token must not read the additional repository"
);
}

View file

@ -63,7 +63,9 @@ pub(super) fn decode_response(
let wire_usage = api_resp.usage.as_ref();
let usage = wire_usage.map_or_else(TokenCounts::default, ApiUsage::token_counts);
let cost_usd = wire_usage.and_then(|u| u.cost);
let cost_usd = wire_usage
.and_then(|usage| usage.cost)
.or_else(|| api_resp.cost.as_ref().and_then(|cost| cost.usd));
let cost_source = translate::authoritative_cost_source(cost_usd);
Ok(Response {

View file

@ -29,8 +29,8 @@ pub(super) struct StreamState {
/// True after `finish_events()` has run (guards against duplicates).
finished: bool,
rate_limit: Option<RateLimitInfo>,
/// In-band USD cost from the usage chunk (OpenRouter), surfaced as
/// authoritative on the final response.
/// In-band USD cost from the response, surfaced as authoritative on the
/// final response.
cost_usd: Option<f64>,
}
@ -72,9 +72,13 @@ impl StreamState {
// Capture usage if present (often in a dedicated chunk).
if let Some(usage) = &chunk.usage {
self.usage = usage.token_counts();
// Keep a previously seen cost when a later usage chunk omits it.
self.cost_usd = usage.cost.or(self.cost_usd);
}
let cost_usd = chunk
.usage
.as_ref()
.and_then(|usage| usage.cost)
.or_else(|| chunk.cost.as_ref().and_then(|cost| cost.usd));
self.cost_usd = cost_usd.or(self.cost_usd);
let choices = chunk.choices.as_mut()?;
let choice = choices.first_mut()?;

View file

@ -125,6 +125,12 @@ pub(super) struct ApiResponse {
pub model: String,
pub choices: Vec<ApiChoice>,
pub usage: Option<ApiUsage>,
pub cost: Option<ApiCost>,
}
#[derive(serde::Deserialize)]
pub(super) struct ApiCost {
pub usd: Option<f64>,
}
#[derive(serde::Deserialize)]
@ -377,6 +383,7 @@ pub(super) struct StreamChunk {
pub model: Option<String>,
pub choices: Option<Vec<StreamChoice>>,
pub usage: Option<ApiUsage>,
pub cost: Option<ApiCost>,
}
#[derive(serde::Deserialize)]

View file

@ -795,6 +795,31 @@ async fn decode_usage_openrouter_cost_and_cache_write() {
fabro_test::fabro_json_snapshot!(response);
}
/// Venice reports authoritative USD cost in a top-level object rather than
/// the OpenRouter `usage.cost` field.
#[tokio::test]
async fn decode_usage_venice_top_level_cost() {
let response = decode_response(serde_json::json!({
"id": "chatcmpl_venice_test",
"object": "chat.completion",
"created": CREATED_TS,
"model": MODEL,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "ok"},
"finish_reason": "stop"
}],
"cost": {"usd": 0.00042, "diem": 0.0},
"usage": {
"prompt_tokens": 12,
"completion_tokens": 2,
"total_tokens": 14
}
}))
.await;
fabro_test::fabro_json_snapshot!(response);
}
// ---------------------------------------------------------------------------
// Stream
// ---------------------------------------------------------------------------
@ -841,6 +866,20 @@ async fn stream_usage_openrouter_cost() {
fabro_test::fabro_json_snapshot!(events);
}
/// Venice streams authoritative USD cost in a top-level object on the usage
/// chunk.
#[tokio::test]
async fn stream_usage_venice_top_level_cost() {
let sse = support::sse_data_transcript(&[
r#"{"id":"chatcmpl_venice_stream","object":"chat.completion.chunk","created":1700000000,"model":"test-model","choices":[{"index":0,"delta":{"role":"assistant","content":"Hi"},"finish_reason":null}]}"#,
r#"{"id":"chatcmpl_venice_stream","object":"chat.completion.chunk","created":1700000000,"model":"test-model","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}"#,
r#"{"id":"chatcmpl_venice_stream","object":"chat.completion.chunk","created":1700000000,"model":"test-model","choices":[],"cost":{"usd":0.00031,"diem":0.0},"usage":{"prompt_tokens":12,"completion_tokens":2,"total_tokens":14}}"#,
"[DONE]",
]);
let (_capture, events) = stream_capture(&base_request(MODEL), &sse).await;
fabro_test::fabro_json_snapshot!(events);
}
#[tokio::test]
async fn stream_tool_call_deltas() {
let sse = support::sse_data_transcript(&[

View file

@ -0,0 +1,55 @@
---
source: lib/components/fabro-llm/tests/it/wire/openai_compatible.rs
expression: rendered
---
{
"id": "chatcmpl_venice_test",
"model": "test-model",
"provider": "openai-compatible",
"message": {
"role": "assistant",
"content": [
{
"kind": "text",
"data": "ok"
}
]
},
"finish_reason": "stop",
"usage": {
"input_tokens": 12,
"output_tokens": 2,
"reasoning_tokens": 0,
"cache_read_tokens": 0,
"cache_write_tokens": 0
},
"raw": {
"id": "chatcmpl_venice_test",
"object": "chat.completion",
"created": 1700000000,
"model": "test-model",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "ok"
},
"finish_reason": "stop"
}
],
"cost": {
"usd": 0.00042,
"diem": 0.0
},
"usage": {
"prompt_tokens": 12,
"completion_tokens": 2,
"total_tokens": 14
}
},
"warnings": [],
"rate_limit": null,
"cost_usd": 0.00042,
"cost_source": "authoritative"
}

View file

@ -0,0 +1,60 @@
---
source: lib/components/fabro-llm/tests/it/wire/openai_compatible.rs
expression: rendered
---
[
{
"type": "stream_start"
},
{
"type": "text_start",
"text_id": null
},
{
"type": "text_delta",
"delta": "Hi",
"text_id": null
},
{
"type": "text_end",
"text_id": null
},
{
"type": "finish",
"finish_reason": "stop",
"usage": {
"input_tokens": 12,
"output_tokens": 2,
"reasoning_tokens": 0,
"cache_read_tokens": 0,
"cache_write_tokens": 0
},
"response": {
"id": "chatcmpl_venice_stream",
"model": "test-model",
"provider": "openai-compatible",
"message": {
"role": "assistant",
"content": [
{
"kind": "text",
"data": "Hi"
}
]
},
"finish_reason": "stop",
"usage": {
"input_tokens": 12,
"output_tokens": 2,
"reasoning_tokens": 0,
"cache_read_tokens": 0,
"cache_write_tokens": 0
},
"raw": null,
"warnings": [],
"rate_limit": null,
"cost_usd": 0.00031,
"cost_source": "authoritative"
}
}
]

View file

@ -91,10 +91,11 @@ pub(crate) fn exact_fetch_command(
checkout_path: &str,
fetch_source: &str,
commit_sha: &str,
depth: usize,
depth: Option<usize>,
) -> String {
let depth_arg = depth_argument(depth);
format!(
"{git} -C {} fetch --depth {depth} --no-tags {} -- {}",
"{git} -C {} fetch{depth_arg} --no-tags {} -- {}",
sandbox::shell_quote(checkout_path),
sandbox::shell_quote(fetch_source),
sandbox::shell_quote(commit_sha),
@ -102,6 +103,12 @@ pub(crate) fn exact_fetch_command(
)
}
/// Leading-space ` --depth N` fragment for a Git command, or empty when
/// `depth` is `None` to fetch full history.
pub(crate) fn depth_argument(depth: Option<usize>) -> String {
depth.map_or_else(String::new, |depth| format!(" --depth {depth}"))
}
/// Point the admitted branch at `revision` and attach HEAD to it.
///
/// The checkout attaches to a real branch instead of detaching so callers that
@ -474,7 +481,7 @@ mod tests {
"/repos/acme's widgets",
"https://token@example.com/acme/widgets.git?x=a b",
sha,
10,
Some(10),
);
let checkout =
exact_checkout_verify_command("/repos/acme's widgets", "feature/a b", "FETCH_HEAD");
@ -493,6 +500,19 @@ mod tests {
);
}
#[test]
fn exact_fetch_omits_depth_for_full_history() {
assert_eq!(
exact_fetch_command(
"/repos/acme/widgets",
"origin",
"0123456789abcdef0123456789abcdef01234567",
None,
),
"git -c maintenance.auto=0 -c gc.auto=0 -C /repos/acme/widgets fetch --no-tags origin -- 0123456789abcdef0123456789abcdef01234567"
);
}
#[test]
fn exact_checkout_verification_rejects_invalid_or_mismatched_head() {
let expected = "0123456789abcdef0123456789abcdef01234567";
@ -554,7 +574,7 @@ mod tests {
);
run_shell(
temp.path(),
&exact_fetch_command(checkout_path, remote_path, &admitted_sha, 10),
&exact_fetch_command(checkout_path, remote_path, &admitted_sha, Some(10)),
);
let checked_out_sha = run_shell(
temp.path(),

View file

@ -18,6 +18,9 @@ pub struct DaytonaSettings {
pub labels: Option<HashMap<String, String>>,
pub snapshot: Option<DaytonaSnapshotSettings>,
pub network: Option<DaytonaNetwork>,
/// Git history depth for the repository clone; `None` clones full
/// history.
pub clone_depth: Option<i32>,
#[serde(default)]
pub skip_clone: bool,
}

View file

@ -101,30 +101,16 @@ const DAYTONA_STATE_CHANGE_POLL_INTERVAL: Duration = Duration::from_secs(1);
/// leaked by a dead worker. An explicit `0` disables auto-stop entirely.
const DEFAULT_AUTO_STOP_INTERVAL_MINUTES: i32 = 120;
fn daytona_git_clone_options(
branch: Option<String>,
commit_id: Option<String>,
username: Option<String>,
password: Option<String>,
) -> GitCloneOptions {
GitCloneOptions {
branch,
commit_id,
username,
password,
}
}
pub(crate) fn daytona_not_found(err: &DaytonaError) -> bool {
matches!(err, DaytonaError::NotFound { .. }) || err.status_code() == Some(404)
}
/// Permissions a Daytona API key needs for Fabro's snapshot and sandbox flow.
pub const REQUIRED_DAYTONA_PERMISSIONS: &[Permissions] = &[
Permissions::WriteColonSnapshots,
Permissions::DeleteColonSnapshots,
Permissions::WriteColonSandboxes,
Permissions::DeleteColonSandboxes,
Permissions::WRITE_SNAPSHOTS,
Permissions::DELETE_SNAPSHOTS,
Permissions::WRITE_SANDBOXES,
Permissions::DELETE_SANDBOXES,
];
pub use crate::config::{
@ -258,10 +244,10 @@ fn join_perms(perms: &[Permissions]) -> String {
fn perm_wire_str(permission: Permissions) -> &'static str {
match permission {
Permissions::WriteColonSnapshots => "write:snapshots",
Permissions::DeleteColonSnapshots => "delete:snapshots",
Permissions::WriteColonSandboxes => "write:sandboxes",
Permissions::DeleteColonSandboxes => "delete:sandboxes",
Permissions::WRITE_SNAPSHOTS => "write:snapshots",
Permissions::DELETE_SNAPSHOTS => "delete:snapshots",
Permissions::WRITE_SANDBOXES => "write:sandboxes",
Permissions::DELETE_SANDBOXES => "delete:sandboxes",
_ => "unknown",
}
}
@ -1612,7 +1598,7 @@ impl Sandbox for DaytonaSandbox {
})?;
let clone_plan = git_retry::RetryPlan::clone_default(None);
let clone_result = git_retry::retry_clone(
let clone_result = git_retry::retry_git_operation(
SandboxProviderKind::Daytona,
"clone",
&clone_plan,
@ -1620,12 +1606,14 @@ impl Sandbox for DaytonaSandbox {
let git_svc = &git_svc;
let origin = origin_url.as_str();
let target = layout.primary_repo_path.as_str();
let options = daytona_git_clone_options(
branch.clone(),
commit_sha.clone(),
username.clone(),
password.clone(),
);
let options = GitCloneOptions {
branch: branch.clone(),
commit_id: commit_sha.clone(),
username: username.clone(),
password: password.clone(),
depth: self.config.clone_depth,
..GitCloneOptions::default()
};
async move { git_svc.clone(origin, target, options).await }
},
|err: &DaytonaError| classify_clone_failure(err, clone_credential_context),
@ -3138,24 +3126,6 @@ mod tests {
assert!(!error.to_string().contains("Daytona client"));
}
#[test]
fn exact_checkout_uses_daytona_branch_and_commit_options() {
let options = daytona_git_clone_options(
Some("feature/work".to_string()),
Some("0123456789abcdef0123456789abcdef01234567".to_string()),
Some("x-access-token".to_string()),
Some("secret".to_string()),
);
assert_eq!(options.branch.as_deref(), Some("feature/work"));
assert_eq!(
options.commit_id.as_deref(),
Some("0123456789abcdef0123456789abcdef01234567")
);
assert_eq!(options.username.as_deref(), Some("x-access-token"));
assert_eq!(options.password.as_deref(), Some("secret"));
}
fn mock_sandbox_body(sandbox_id: &str) -> serde_json::Value {
serde_json::json!({
"id": sandbox_id,
@ -3171,6 +3141,7 @@ mod tests {
"gpu": 0.0,
"memory": 4.0,
"disk": 20.0,
"toolboxProxyUrl": "https://proxy.example.com/toolbox",
"state": "started"
})
}
@ -3488,6 +3459,7 @@ mod tests {
"size": null,
"entrypoint": null,
"errorReason": null,
"sourceSandboxId": null,
"lastUsedAt": null,
"createdAt": "2026-05-01T00:00:00Z",
"updatedAt": "2026-05-01T00:00:00Z"
@ -3509,6 +3481,7 @@ mod tests {
"gpu": 0.0,
"memory": 4.0,
"disk": 20.0,
"toolboxProxyUrl": "https://proxy.example.com/toolbox",
"state": state.to_string()
})
}
@ -3519,6 +3492,7 @@ mod tests {
assert!(config.snapshot.is_none());
assert!(config.auto_stop_interval.is_none());
assert!(config.labels.is_none());
assert!(config.clone_depth.is_none());
}
#[test]
@ -4195,10 +4169,7 @@ mod tests {
fn missing_display_uses_daytona_wire_scope_names() {
let check = DaytonaKeyCheck {
key_name: "delete-only".to_string(),
missing: vec![
Permissions::WriteColonSnapshots,
Permissions::WriteColonSandboxes,
],
missing: vec![Permissions::WRITE_SNAPSHOTS, Permissions::WRITE_SANDBOXES],
};
assert_eq!(check.missing_display(), "write:snapshots, write:sandboxes");
@ -4337,6 +4308,7 @@ mod tests {
"gpu": 0.0,
"memory": 4.0,
"disk": 20.0,
"toolboxProxyUrl": "https://proxy.example.com/toolbox",
"state": "started"
}));
})

View file

@ -672,18 +672,22 @@ pub(crate) mod daytona {
DaytonaState::Creating
| DaytonaState::PendingBuild
| DaytonaState::BuildingSnapshot
| DaytonaState::PullingSnapshot => SandboxState::Provisioning,
DaytonaState::Starting => SandboxState::Starting,
DaytonaState::Started => SandboxState::Running,
DaytonaState::Stopping | DaytonaState::Archiving => SandboxState::Stopping,
| DaytonaState::PullingSnapshot
| DaytonaState::Forking => SandboxState::Provisioning,
DaytonaState::Starting | DaytonaState::Resuming => SandboxState::Starting,
DaytonaState::Started | DaytonaState::Snapshotting => SandboxState::Running,
DaytonaState::Stopping | DaytonaState::Archiving | DaytonaState::Pausing => {
SandboxState::Stopping
}
DaytonaState::Stopped => SandboxState::Stopped,
DaytonaState::Paused => SandboxState::Paused,
DaytonaState::Restoring => SandboxState::Restoring,
DaytonaState::Resizing => SandboxState::Resizing,
DaytonaState::Archived => SandboxState::Archived,
DaytonaState::Destroying => SandboxState::Deleting,
DaytonaState::Destroyed => SandboxState::Deleted,
DaytonaState::Error | DaytonaState::BuildFailed => SandboxState::Error,
DaytonaState::Unknown => SandboxState::Unknown,
DaytonaState::Unknown | DaytonaState::UnknownDefaultOpenApi => SandboxState::Unknown,
}
}
@ -755,6 +759,22 @@ pub(crate) mod daytona {
);
}
#[test]
fn pause_states_normalize_to_fabro_states() {
assert_eq!(
normalize_daytona_state(DaytonaState::Pausing),
SandboxState::Stopping
);
assert_eq!(
normalize_daytona_state(DaytonaState::Paused),
SandboxState::Paused
);
assert_eq!(
normalize_daytona_state(DaytonaState::Resuming),
SandboxState::Starting
);
}
#[test]
fn gibibytes_to_bytes_converts_positive_values() {
assert_eq!(gibibytes_to_bytes(2.0), Some(2 * 1024 * 1024 * 1024));

View file

@ -18,6 +18,7 @@ use bollard::image::CreateImageOptions;
use bollard::models::{ContainerInspectResponse, HostConfig};
use fabro_github::GitHubCredentials;
use fabro_github::token_source::InstallationTokenSource;
use fabro_types::settings::run::RunCloneSettings;
use fabro_types::{CommandOutputStream, CommandTermination, RunId, SandboxProviderKind};
use fabro_util::time::elapsed_ms;
use futures::StreamExt;
@ -48,7 +49,7 @@ const DOCKER_BASH_REQUIREMENT: &str = "Docker sandboxes require /bin/bash for ev
pub(crate) const WORKING_DIRECTORY: &str = "/workspace";
pub(crate) const REPOS_ROOT: &str = "/repos";
const GIT_CLONE_DEPTH: usize = 10;
const DEFAULT_GIT_CLONE_DEPTH: usize = RunCloneSettings::DEFAULT_DEPTH.unsigned_abs() as usize;
const GIT_CLONE_TIMEOUT: Duration = Duration::from_mins(5);
#[cfg(test)]
const EXEC_STOP_POLL_SLEEP_SECONDS: &str = "0.005";
@ -121,6 +122,9 @@ pub struct DockerSandboxOptions {
pub auto_pull: bool,
/// Additional `KEY=VALUE` environment variables for the container.
pub env_vars: Vec<String>,
/// Maximum Git history depth fetched during clone; `None` fetches full
/// history.
pub clone_depth: Option<usize>,
/// Create an empty workspace instead of cloning even when an origin exists.
pub skip_clone: bool,
}
@ -134,6 +138,7 @@ impl Default for DockerSandboxOptions {
cpu_quota: None,
auto_pull: true,
env_vars: Vec::new(),
clone_depth: Some(DEFAULT_GIT_CLONE_DEPTH),
skip_clone: false,
}
}
@ -811,7 +816,7 @@ impl DockerSandbox {
auth_url: Option<&fabro_redact::DisplaySafeUrl>,
) -> Result<(), DockerCloneFailure> {
let plan = git_retry::RetryPlan::clone_default(Some(clone_deadline));
git_retry::retry_clone(
git_retry::retry_git_operation(
SandboxProviderKind::Docker,
op,
&plan,
@ -954,7 +959,7 @@ impl DockerSandbox {
&layout.primary_repo_path,
"origin",
expected_sha,
GIT_CLONE_DEPTH,
self.config.clone_depth,
);
if let Err(failure) = self
.retry_git_transfer(
@ -992,8 +997,12 @@ impl DockerSandbox {
return Err(self.report_clone_failure(&origin_url, error));
}
} else {
let command =
git_clone_command(clone_url, branch.as_deref(), &layout.primary_repo_path);
let command = git_clone_command(
clone_url,
branch.as_deref(),
&layout.primary_repo_path,
self.config.clone_depth,
);
if let Err(failure) = self
.retry_git_transfer(
&command,
@ -1529,15 +1538,19 @@ async fn cache_docker_stdio_completion(
}
}
fn git_clone_command(clone_url: &str, branch: Option<&str>, checkout_path: &str) -> String {
fn git_clone_command(
clone_url: &str,
branch: Option<&str>,
checkout_path: &str,
depth: Option<usize>,
) -> String {
let mut command = format!("{} clone", sandbox::GIT);
if let Some(branch) = branch {
command.push_str(" --branch ");
command.push_str(&shell_quote(branch));
command.push_str(" --single-branch");
}
command.push_str(" --depth ");
command.push_str(&GIT_CLONE_DEPTH.to_string());
command.push_str(&clone_source::depth_argument(depth));
command.push_str(" --no-tags");
command.push_str(" -- ");
command.push_str(&shell_quote(clone_url));
@ -2556,19 +2569,21 @@ mod tests {
let options = DockerSandboxOptions::default();
assert_eq!(options.image, "buildpack-deps:noble");
assert_eq!(options.network_mode.as_deref(), Some("bridge"));
assert_eq!(options.clone_depth, Some(DEFAULT_GIT_CLONE_DEPTH));
assert!(!options.skip_clone);
}
#[test]
fn clone_command_uses_depth_ten_without_tags_for_branch_clone() {
fn clone_command_uses_configured_depth_without_tags_for_branch_clone() {
let command = git_clone_command(
"https://github.com/fabro-sh/fabro",
Some("main"),
"/repos/fabro-sh/fabro",
Some(1),
);
assert_eq!(
command,
"git -c maintenance.auto=0 -c gc.auto=0 clone --branch main --single-branch --depth 10 --no-tags -- https://github.com/fabro-sh/fabro /repos/fabro-sh/fabro"
"git -c maintenance.auto=0 -c gc.auto=0 clone --branch main --single-branch --depth 1 --no-tags -- https://github.com/fabro-sh/fabro /repos/fabro-sh/fabro"
);
}
@ -2578,10 +2593,25 @@ mod tests {
"https://github.com/fabro-sh/fabro",
None,
"/repos/fabro-sh/fabro",
Some(DEFAULT_GIT_CLONE_DEPTH),
);
assert_eq!(
command,
"git -c maintenance.auto=0 -c gc.auto=0 clone --depth 10 --no-tags -- https://github.com/fabro-sh/fabro /repos/fabro-sh/fabro"
"git -c maintenance.auto=0 -c gc.auto=0 clone --depth 100 --no-tags -- https://github.com/fabro-sh/fabro /repos/fabro-sh/fabro"
);
}
#[test]
fn clone_command_omits_depth_for_full_clone() {
let command = git_clone_command(
"https://github.com/fabro-sh/fabro",
Some("main"),
"/repos/fabro-sh/fabro",
None,
);
assert_eq!(
command,
"git -c maintenance.auto=0 -c gc.auto=0 clone --branch main --single-branch --no-tags -- https://github.com/fabro-sh/fabro /repos/fabro-sh/fabro"
);
}

View file

@ -8,7 +8,9 @@ use std::path::{Path, PathBuf};
use fabro_types::settings::ResolveError;
#[cfg(feature = "daytona")]
use fabro_types::settings::run::DockerfileSource as ResolvedDockerfileSource;
use fabro_types::settings::run::{EnvironmentNetworkMode, RunEnvironmentSettings};
use fabro_types::settings::run::{
EnvironmentNetworkMode, RunCloneSettings, RunEnvironmentSettings,
};
#[cfg(feature = "daytona")]
use crate::config::{
@ -23,19 +25,16 @@ use crate::docker::DockerSandboxOptions;
#[must_use]
pub fn daytona_config_from_environment(
settings: &RunEnvironmentSettings,
skip_clone: bool,
clone: &RunCloneSettings,
) -> DaytonaConfig {
DaytonaConfig {
auto_stop_interval: settings
.lifecycle
.auto_stop
.map(|duration| duration_to_minutes_i32(duration.as_std())),
labels: (!settings.labels.is_empty()).then(|| settings.labels.clone()),
snapshot: settings
.image
.dockerfile
.as_ref()
.map(|dockerfile| DaytonaSnapshotSettings {
labels: (!settings.labels.is_empty()).then(|| settings.labels.clone()),
snapshot: settings.image.dockerfile.as_ref().map(|dockerfile| {
DaytonaSnapshotSettings {
cpu: settings.resources.cpu,
memory: settings
.resources
@ -53,15 +52,17 @@ pub fn daytona_config_from_environment(
SandboxDockerfileSource::Path { path: path.clone() }
}
}),
}),
network: Some(match settings.network.mode {
}
}),
network: Some(match settings.network.mode {
EnvironmentNetworkMode::Block => DaytonaNetwork::Block,
EnvironmentNetworkMode::AllowAll => DaytonaNetwork::AllowAll,
EnvironmentNetworkMode::CidrAllowList => {
DaytonaNetwork::AllowList(settings.network.allow.clone())
}
}),
skip_clone,
clone_depth: clone.depth_limit(),
skip_clone: !clone.enabled,
}
}
@ -69,7 +70,7 @@ pub fn daytona_config_from_environment(
#[must_use]
pub fn docker_config_from_environment(
settings: &RunEnvironmentSettings,
skip_clone: bool,
clone: &RunCloneSettings,
) -> DockerSandboxOptions {
// No vault is available on this path (server preflight / manifest), so a
// `{{ secrets.* }}` value keeps its source form. Nothing else is left to
@ -84,25 +85,23 @@ pub fn docker_config_from_environment(
.iter()
.map(|(key, value)| (key.clone(), value.as_source()))
.collect();
docker_config_from_environment_env(settings, skip_clone, env)
docker_config_from_environment_env(settings, clone, env)
}
#[cfg(feature = "docker")]
pub fn docker_config_from_environment_with_secrets(
settings: &RunEnvironmentSettings,
skip_clone: bool,
clone: &RunCloneSettings,
secrets_lookup: impl FnMut(&str) -> Option<String>,
) -> Result<DockerSandboxOptions, ResolveError> {
let env = settings.resolve_env(secrets_lookup)?;
Ok(docker_config_from_environment_env(
settings, skip_clone, env,
))
Ok(docker_config_from_environment_env(settings, clone, env))
}
#[cfg(feature = "docker")]
fn docker_config_from_environment_env(
settings: &RunEnvironmentSettings,
skip_clone: bool,
clone: &RunCloneSettings,
env: std::collections::HashMap<String, String>,
) -> DockerSandboxOptions {
let mut env_vars = env
@ -133,7 +132,10 @@ fn docker_config_from_environment_env(
.cpu
.map(|cpu| i64::from(cpu).saturating_mul(100_000)),
env_vars,
skip_clone,
clone_depth: clone
.depth_limit()
.and_then(|depth| usize::try_from(depth).ok()),
skip_clone: !clone.enabled,
..DockerSandboxOptions::default()
}
}

View file

@ -199,7 +199,7 @@ pub fn classify_failure(message: &str, cred: CredentialContext) -> Option<GitRet
/// GitHub's guidance for token replication is to wait a few seconds and retry
/// with the same token. Sub-second delays land inside the same replication
/// window and spend an attempt for nothing.
fn clone_backoff() -> BackoffPolicy {
fn replication_backoff() -> BackoffPolicy {
BackoffPolicy {
initial_delay: Duration::from_secs(3),
factor: 3.0,
@ -229,6 +229,13 @@ pub struct RetryPlan {
}
impl RetryPlan {
/// Host-side repository probes use the same attempt count and pacing as
/// clone operations against a freshly minted token.
#[must_use]
pub fn repository_probe() -> Self {
Self::clone_default(None)
}
/// The clone policy both providers already trust: 3 attempts, 3s/9s
/// backoff, no plan-level bounds. Docker supplies its existing absolute
/// five-minute deadline through `outer_deadline`; Daytona supplies none.
@ -236,7 +243,7 @@ impl RetryPlan {
pub fn clone_default(outer_deadline: Option<time::Instant>) -> Self {
Self {
max_attempts: 3,
backoff: clone_backoff(),
backoff: replication_backoff(),
max_elapsed: None,
per_attempt_timeout: None,
outer_deadline,
@ -249,7 +256,7 @@ impl RetryPlan {
pub fn checkpoint_push() -> Self {
Self {
max_attempts: 3,
backoff: clone_backoff(),
backoff: replication_backoff(),
max_elapsed: Some(Duration::from_secs(90)),
per_attempt_timeout: Some(Duration::from_mins(1)),
outer_deadline: None,
@ -316,13 +323,13 @@ impl RetryPlan {
}
}
/// Run a clone operation, repeating it while the failure looks transient.
/// Run a git operation, repeating it while the failure looks transient.
///
/// `attempt` receives the 1-based attempt number. `classify` decides whether
/// an error is worth repeating; `None` returns it to the caller untouched.
/// A retry starts only when its backoff fits before the plan's effective
/// deadline. The final error is returned as-is.
pub(crate) async fn retry_clone<T, E, Attempt, Fut, Classify>(
pub async fn retry_git_operation<T, E, Attempt, Fut, Classify>(
provider: SandboxProviderKind,
op: &str,
plan: &RetryPlan,
@ -595,7 +602,7 @@ mod tests {
async fn first_success_runs_one_attempt() {
let attempts = Attempts::default();
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"clone",
&RetryPlan::clone_default(None),
@ -615,7 +622,7 @@ mod tests {
async fn retries_until_a_later_attempt_succeeds() {
let attempts = Attempts::default();
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"clone",
&RetryPlan::clone_default(None),
@ -641,7 +648,7 @@ mod tests {
async fn exhausted_attempts_return_the_final_error() {
let attempts = Attempts::default();
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"clone",
&RetryPlan::clone_default(None),
@ -665,7 +672,7 @@ mod tests {
async fn unretryable_failure_stops_immediately() {
let attempts = Attempts::default();
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"clone",
&RetryPlan::clone_default(None),
@ -692,7 +699,7 @@ mod tests {
let attempts = Attempts::default();
let deadline = time::Instant::now() + Duration::from_secs(2);
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"clone",
&RetryPlan::clone_default(Some(deadline)),
@ -715,7 +722,7 @@ mod tests {
async fn unbounded_plan_runs_all_attempts() {
let attempts = Attempts::default();
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Daytona,
"clone",
&RetryPlan::clone_default(None),
@ -736,13 +743,13 @@ mod tests {
let attempts = Attempts::default();
let plan = RetryPlan {
max_attempts: 5,
backoff: clone_backoff(),
backoff: replication_backoff(),
max_elapsed: Some(Duration::from_secs(4)),
per_attempt_timeout: None,
outer_deadline: None,
};
let result = retry_clone(
let result = retry_git_operation(
SandboxProviderKind::Docker,
"push",
&plan,

View file

@ -43,7 +43,9 @@ pub use fabro_github::token_source::{
InstallationTokenSource, ResolvedToken, TokenProvenance, TokenSnapshot,
};
pub use fabro_types::{RunSandboxInstance, SandboxProviderKind};
pub use git_retry::{CredentialContext, GitRetryReason, RetryPlan, classify_failure};
pub use git_retry::{
CredentialContext, GitRetryReason, RetryPlan, classify_failure, retry_git_operation,
};
pub use local::LocalSandbox;
#[cfg(feature = "daytona")]
pub use provider::daytona::DaytonaSandboxProvider;

View file

@ -793,7 +793,7 @@ mod tests {
_dir: tempfile::TempDir,
source: Database,
source_db: slatedb::Db,
sqlite: fabro_db::Database,
sqlite: sqlx::SqlitePool,
target: BlobStore,
}
@ -809,7 +809,8 @@ mod tests {
let dir = tempfile::tempdir()?;
let sqlite = fabro_db::Database::connect(dir.path().join("fabro.sqlite3")).await?;
sqlite.migrate().await?;
let target = BlobStore::new(sqlite.clone_pool());
let sqlite = sqlite.clone_pool();
let target = BlobStore::new(sqlite.clone());
Ok(Self {
_dir: dir,
source,
@ -819,6 +820,19 @@ mod tests {
})
}
async fn new_with_single_sqlite_connection() -> TestResult<Self> {
let mut context = Self::new().await?;
let options = context.sqlite.connect_options().as_ref().clone();
context.sqlite.close().await;
let sqlite = SqlitePoolOptions::new()
.max_connections(1)
.connect_with(options)
.await?;
context.target = BlobStore::new(sqlite.clone());
context.sqlite = sqlite;
Ok(context)
}
async fn put_blob(&self, bytes: &[u8]) -> TestResult<BlobHash> {
let hash = BlobHash::new(bytes);
let key = SlateKey::new("blobs").with("sha256").with(hash);
@ -842,7 +856,7 @@ mod tests {
async fn destination_rows(&self) -> TestResult<i64> {
Ok(sqlx::query_scalar("SELECT COUNT(*) FROM blobs")
.fetch_one(self.sqlite.pool())
.fetch_one(&self.sqlite)
.await?)
}
@ -850,7 +864,7 @@ mod tests {
sqlx::query("INSERT INTO blobs (hash, data) VALUES (?, ?)")
.bind(hash.to_string())
.bind(bytes)
.execute(self.sqlite.pool())
.execute(&self.sqlite)
.await?;
Ok(())
}
@ -858,20 +872,20 @@ mod tests {
async fn delete_destination(&self, hash: BlobHash) -> TestResult<()> {
sqlx::query("DELETE FROM blobs WHERE hash = ?")
.bind(hash.to_string())
.execute(self.sqlite.pool())
.execute(&self.sqlite)
.await?;
Ok(())
}
async fn set_automatic_checkpoint(&self, pages: i64) -> TestResult<()> {
let mut connection = self.sqlite.pool().acquire().await?;
let mut connection = self.sqlite.acquire().await?;
let statement = sqlx::AssertSqlSafe(format!("PRAGMA wal_autocheckpoint = {pages}"));
sqlx::query(statement).execute(&mut *connection).await?;
Ok(())
}
async fn automatic_checkpoint(&self) -> TestResult<i64> {
let mut connection = self.sqlite.pool().acquire().await?;
let mut connection = self.sqlite.acquire().await?;
Ok(sqlx::query_scalar("PRAGMA wal_autocheckpoint")
.fetch_one(&mut *connection)
.await?)
@ -1341,13 +1355,13 @@ mod tests {
#[tokio::test]
async fn automatic_checkpoint_setting_is_restored_after_success_and_failure() -> TestResult<()>
{
let success = TestContext::new().await?;
let success = TestContext::new_with_single_sqlite_connection().await?;
success.set_automatic_checkpoint(37).await?;
success.put_blob(b"success").await?;
success.import().await?;
assert_eq!(success.automatic_checkpoint().await?, 37);
let failure = TestContext::new().await?;
let failure = TestContext::new_with_single_sqlite_connection().await?;
failure.set_automatic_checkpoint(41).await?;
let mut invalid_key = legacy_prefix();
invalid_key.extend_from_slice(&[b'z'; 64]);

View file

@ -0,0 +1,438 @@
//! Secret-free Git bridging environment for additional-repository access.
//!
//! When a run declares additional GitHub repositories, every resolved
//! command/tool/ACP environment receives `GIT_CONFIG_COUNT` /
//! `GIT_CONFIG_KEY_n` / `GIT_CONFIG_VALUE_n` entries that make plain Git
//! commands work against the declared set through the managed
//! `GITHUB_TOKEN`:
//!
//! - a credential helper for `https://github.com` that reads `$GITHUB_TOKEN`
//! from the invoking Git process's environment at invocation time, so token
//! refresh flows through per-stage environment resolution with no bridging
//! update;
//! - per-repository `url.<https>.insteadOf` rewrites for the
//! `git@github.com:owner/repo[.git]` and
//! `ssh://git@github.com/owner/repo[.git]` SSH spellings of each effective
//! repository.
//!
//! None of the values contain a secret; the token lives only in
//! `GITHUB_TOKEN`.
//!
//! The credential helper is host-scoped to `https://github.com`, not
//! path-scoped. This is safe because the token is scoped server-side to the
//! declared repository set and is only ever offered to github.com. It does
//! change one failure mode for *undeclared* repositories: public HTTPS
//! clones are unaffected (Git tries unauthenticated first), while private
//! undeclared HTTPS repositories fail with a GitHub authorization error
//! instead of a missing-credential error. Both fail; only the diagnostic
//! differs.
//!
//! `insteadOf` matches by string prefix, not exactly: a rule for
//! `owner/repo` also matches `owner/repo-other`. An undeclared repository
//! that shares a declared prefix is therefore rewritten to HTTPS; the scoped
//! token is invalid for it at GitHub, so authority is unchanged, but its Git
//! transport changes from SSH to HTTPS.
use std::collections::HashMap;
use fabro_github::{GITHUB_CREDENTIAL_HELPER, GITHUB_CREDENTIAL_HELPER_KEY};
use fabro_types::GitHubRepositorySlug;
use crate::error::Error;
/// Section base for the effective repositories' HTTPS routes.
const GITHUB_HTTPS_BASE: &str = "https://github.com/";
/// Merge the bridging entries into `env` for the effective repository set
/// (primary first). Appends after any valid user-provided `GIT_CONFIG_COUNT`
/// overlay without overwriting it, and fails with a configuration error when
/// the user overlay is malformed rather than silently replacing it.
pub(crate) fn merge_git_bridge_env(
env: &mut HashMap<String, String>,
targets: &[&GitHubRepositorySlug],
) -> Result<(), Error> {
let start = user_git_config_count(env)?;
let entries = bridge_entries(targets, GITHUB_HTTPS_BASE);
let total = start + entries.len();
for (offset, (key, value)) in entries.into_iter().enumerate() {
let index = start + offset;
env.insert(format!("GIT_CONFIG_KEY_{index}"), key);
env.insert(format!("GIT_CONFIG_VALUE_{index}"), value);
}
env.insert("GIT_CONFIG_COUNT".to_string(), total.to_string());
// Fail instead of hanging when access is missing or invalid; a user who
// explicitly configured prompting keeps their value.
env.entry("GIT_TERMINAL_PROMPT".to_string())
.or_insert_with(|| "0".to_string());
Ok(())
}
/// The bridge's Git config entries in order: the credential helper, then two
/// SSH-to-HTTPS rewrites per repository. `https_base` is
/// [`GITHUB_HTTPS_BASE`] in production; contract tests substitute a local
/// `file://` root to prove real Git applies the generated entries without
/// touching the network.
fn bridge_entries(targets: &[&GitHubRepositorySlug], https_base: &str) -> Vec<(String, String)> {
let mut entries = Vec::with_capacity(1 + targets.len() * 2);
entries.push((
GITHUB_CREDENTIAL_HELPER_KEY.to_string(),
GITHUB_CREDENTIAL_HELPER.to_string(),
));
for slug in targets {
let owner = slug.owner();
let repo = slug.repo();
let https = format!("{https_base}{owner}/{repo}");
// One prefix rule per SSH spelling covers both the bare and `.git`
// suffixed forms.
entries.push((
format!("url.{https}.insteadOf"),
format!("git@github.com:{owner}/{repo}"),
));
entries.push((
format!("url.{https}.insteadOf"),
format!("ssh://git@github.com/{owner}/{repo}"),
));
}
entries
}
/// Validate and measure a user-provided `GIT_CONFIG_COUNT` overlay so the
/// bridge appends after it. Orphaned `GIT_CONFIG_KEY_n` entries without a
/// count are inert to Git and are treated as absent.
fn user_git_config_count(env: &HashMap<String, String>) -> Result<usize, Error> {
let Some(raw) = env.get("GIT_CONFIG_COUNT") else {
return Ok(0);
};
let count: usize = raw.trim().parse().map_err(|_| {
Error::Precondition(format!(
"environment variable GIT_CONFIG_COUNT must be a non-negative integer to combine \
with Fabro's Git bridging entries, got `{raw}`"
))
})?;
for index in 0..count {
let key = format!("GIT_CONFIG_KEY_{index}");
let value = format!("GIT_CONFIG_VALUE_{index}");
if !env.contains_key(&key) || !env.contains_key(&value) {
return Err(Error::Precondition(format!(
"GIT_CONFIG_COUNT is {count} but {key} or {value} is missing; fix the indexed \
Git config overlay so Fabro can append its bridging entries after it"
)));
}
}
Ok(count)
}
#[cfg(test)]
#[expect(
clippy::disallowed_methods,
clippy::disallowed_types,
reason = "contract tests drive the installed git binary synchronously in non-async tests"
)]
mod tests {
use std::path::Path;
use std::process::Command;
use super::*;
fn slug(value: &str) -> GitHubRepositorySlug {
value.parse().expect("test slug should parse")
}
fn bridged_env(
base_env: HashMap<String, String>,
targets: &[&GitHubRepositorySlug],
) -> HashMap<String, String> {
let mut env = base_env;
merge_git_bridge_env(&mut env, targets).expect("bridge entries should merge");
env
}
/// Run `git` with ONLY the bridge-relevant environment: the inherited
/// user/system/global Git config is disabled so assertions observe just
/// the generated entries.
fn git(args: &[&str], env: &HashMap<String, String>, cwd: &Path) -> std::process::Output {
let mut command = Command::new("git");
command
.args(args)
.current_dir(cwd)
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_ASKPASS", "true");
for (key, value) in env {
command.env(key, value);
}
command.output().expect("git should run")
}
/// Create a bare fixture answering both the bare and `.git`-suffixed
/// routes, the way GitHub serves both HTTPS spellings.
fn init_bare_fixture(root: &Path, owner_repo: &str) -> String {
let fixture = root.join(format!("{owner_repo}.git"));
std::fs::create_dir_all(&fixture).unwrap();
let init = Command::new("git")
.args(["init", "--bare", "--initial-branch=main"])
.arg(&fixture)
.output()
.expect("git init should run");
assert!(init.status.success(), "{init:?}");
#[cfg(unix)]
std::os::unix::fs::symlink(&fixture, root.join(owner_repo)).unwrap();
format!("file://{}/", root.display())
}
#[test]
fn no_targets_means_no_bridge_call_and_empty_env_stays_empty() {
// The caller only bridges when the additional set is non-empty; the
// pure entry builder is still total for the primary-only case.
assert_eq!(bridge_entries(&[], GITHUB_HTTPS_BASE).len(), 1);
let env: HashMap<String, String> = HashMap::new();
assert!(!env.contains_key("GIT_CONFIG_COUNT"));
}
#[test]
fn merges_helper_rewrites_count_and_terminal_prompt() {
let keystone = slug("fabro-sh/keystone");
let fabro = slug("fabro-sh/fabro");
let env = bridged_env(HashMap::new(), &[&fabro, &keystone]);
assert_eq!(env.get("GIT_CONFIG_COUNT").map(String::as_str), Some("5"));
assert_eq!(
env.get("GIT_CONFIG_KEY_0").map(String::as_str),
Some("credential.https://github.com.helper")
);
assert_eq!(
env.get("GIT_CONFIG_KEY_1").map(String::as_str),
Some("url.https://github.com/fabro-sh/fabro.insteadOf")
);
assert_eq!(
env.get("GIT_CONFIG_VALUE_1").map(String::as_str),
Some("git@github.com:fabro-sh/fabro")
);
assert_eq!(
env.get("GIT_CONFIG_VALUE_2").map(String::as_str),
Some("ssh://git@github.com/fabro-sh/fabro")
);
assert_eq!(
env.get("GIT_TERMINAL_PROMPT").map(String::as_str),
Some("0")
);
// No secrets anywhere in the generated values.
for (key, value) in &env {
assert!(!value.contains("ghs_"), "{key}={value}");
}
}
#[test]
fn respects_an_explicit_user_terminal_prompt() {
let keystone = slug("fabro-sh/keystone");
let env = bridged_env(
HashMap::from([("GIT_TERMINAL_PROMPT".to_string(), "1".to_string())]),
&[&keystone],
);
assert_eq!(
env.get("GIT_TERMINAL_PROMPT").map(String::as_str),
Some("1")
);
}
#[test]
fn appends_after_a_valid_user_git_config_overlay() {
let keystone = slug("fabro-sh/keystone");
let env = bridged_env(
HashMap::from([
("GIT_CONFIG_COUNT".to_string(), "1".to_string()),
("GIT_CONFIG_KEY_0".to_string(), "user.name".to_string()),
("GIT_CONFIG_VALUE_0".to_string(), "Overlay User".to_string()),
]),
&[&keystone],
);
assert_eq!(env.get("GIT_CONFIG_COUNT").map(String::as_str), Some("4"));
assert_eq!(
env.get("GIT_CONFIG_KEY_0").map(String::as_str),
Some("user.name"),
"user entry must survive at its original index"
);
assert_eq!(
env.get("GIT_CONFIG_KEY_1").map(String::as_str),
Some("credential.https://github.com.helper")
);
// Real Git sees both the user's entry and the appended bridge entry.
let dir = tempfile::tempdir().unwrap();
let output = git(&["config", "--list"], &env, dir.path());
assert!(output.status.success(), "{output:?}");
let listed = String::from_utf8_lossy(&output.stdout);
assert!(listed.contains("user.name=Overlay User"), "{listed}");
assert!(
listed.contains("credential.https://github.com.helper"),
"{listed}"
);
}
#[test]
fn rejects_a_malformed_user_git_config_overlay() {
let keystone = slug("fabro-sh/keystone");
let mut non_numeric = HashMap::from([("GIT_CONFIG_COUNT".to_string(), "two".to_string())]);
let err = merge_git_bridge_env(&mut non_numeric, &[&keystone]).unwrap_err();
assert!(err.to_string().contains("GIT_CONFIG_COUNT"), "{err}");
let mut missing_index = HashMap::from([
("GIT_CONFIG_COUNT".to_string(), "2".to_string()),
("GIT_CONFIG_KEY_0".to_string(), "user.name".to_string()),
("GIT_CONFIG_VALUE_0".to_string(), "Overlay".to_string()),
]);
let err = merge_git_bridge_env(&mut missing_index, &[&keystone]).unwrap_err();
assert!(err.to_string().contains("GIT_CONFIG_KEY_1"), "{err}");
}
/// With the bridge active, `git credential fill` for github.com resolves
/// through the generated helper and reads `$GITHUB_TOKEN` from the
/// invoking process environment at invocation time.
#[test]
fn credential_helper_reads_github_token_at_invocation_time() {
use std::io::Write as _;
let keystone = slug("fabro-sh/keystone");
let mut env = bridged_env(HashMap::new(), &[&keystone]);
env.insert("GITHUB_TOKEN".to_string(), "test-token-value".to_string());
let dir = tempfile::tempdir().unwrap();
let mut command = Command::new("git");
command
.args(["credential", "fill"])
.current_dir(dir.path())
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped());
for (key, value) in &env {
command.env(key, value);
}
let mut child = command.spawn().expect("git credential fill should spawn");
child
.stdin
.as_mut()
.unwrap()
.write_all(b"protocol=https\nhost=github.com\npath=fabro-sh/keystone\n\n")
.unwrap();
let output = child.wait_with_output().unwrap();
assert!(output.status.success(), "{output:?}");
let filled = String::from_utf8_lossy(&output.stdout);
assert!(filled.contains("username=x-access-token"), "{filled}");
assert!(filled.contains("password=test-token-value"), "{filled}");
}
/// Real Git applies the generated `insteadOf` rewrites: the exact SSH
/// spellings of a declared repository resolve to their HTTPS-analog
/// route (a local `file://` fixture here, so no network is involved),
/// while `GIT_SSH_COMMAND=false` proves SSH is never attempted.
#[test]
fn declared_ssh_urls_rewrite_to_the_https_route() {
let root = tempfile::tempdir().unwrap();
let base = init_bare_fixture(root.path(), "fabro-sh/keystone");
let keystone = slug("fabro-sh/keystone");
let mut env: HashMap<String, String> = HashMap::new();
for (offset, (key, value)) in bridge_entries(&[&keystone], &base).into_iter().enumerate() {
env.insert(format!("GIT_CONFIG_KEY_{offset}"), key);
env.insert(format!("GIT_CONFIG_VALUE_{offset}"), value);
}
env.insert("GIT_CONFIG_COUNT".to_string(), "3".to_string());
env.insert("GIT_SSH_COMMAND".to_string(), "false".to_string());
for url in [
"ssh://git@github.com/fabro-sh/keystone.git",
"ssh://git@github.com/fabro-sh/keystone",
"git@github.com:fabro-sh/keystone.git",
"git@github.com:fabro-sh/keystone",
] {
let output = git(&["ls-remote", url], &env, root.path());
assert!(
output.status.success(),
"{url} should rewrite to the fixture route: {}",
String::from_utf8_lossy(&output.stderr)
);
}
}
/// An undeclared SSH URL that shares no declared prefix is not
/// rewritten: Git still routes it to SSH, where the scripted
/// `GIT_SSH_COMMAND=false` fails immediately without network access.
#[test]
fn undeclared_ssh_urls_are_not_rewritten() {
let root = tempfile::tempdir().unwrap();
let base = init_bare_fixture(root.path(), "fabro-sh/keystone");
let keystone = slug("fabro-sh/keystone");
let mut env: HashMap<String, String> = HashMap::new();
for (offset, (key, value)) in bridge_entries(&[&keystone], &base).into_iter().enumerate() {
env.insert(format!("GIT_CONFIG_KEY_{offset}"), key);
env.insert(format!("GIT_CONFIG_VALUE_{offset}"), value);
}
env.insert("GIT_CONFIG_COUNT".to_string(), "3".to_string());
env.insert("GIT_SSH_COMMAND".to_string(), "false".to_string());
env.insert("GITHUB_TOKEN".to_string(), "test-token-value".to_string());
let output = git(
&["ls-remote", "git@github.com:fabro-sh/undeclared"],
&env,
root.path(),
);
assert!(!output.status.success(), "{output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
// Not rewritten: the failure never mentions the local HTTPS-analog
// fixture route, so Git still chose the SSH transport.
assert!(
!stderr.contains(&root.path().display().to_string()),
"undeclared URL must not be rewritten to the fixture route: {stderr}"
);
assert!(!stderr.contains("test-token-value"), "{stderr}");
}
/// Prefix collision: with `fabro-sh/keystone` declared, both SSH
/// spellings of `fabro-sh/keystone-other` are rewritten to the HTTPS
/// route (prefix match), where access fails — at GitHub this is an
/// authorization error for the scoped token — and no token leaks into
/// the output.
#[test]
fn prefix_colliding_undeclared_repositories_rewrite_and_fail_without_token_leak() {
let root = tempfile::tempdir().unwrap();
let base = init_bare_fixture(root.path(), "fabro-sh/keystone");
let keystone = slug("fabro-sh/keystone");
let mut env: HashMap<String, String> = HashMap::new();
for (offset, (key, value)) in bridge_entries(&[&keystone], &base).into_iter().enumerate() {
env.insert(format!("GIT_CONFIG_KEY_{offset}"), key);
env.insert(format!("GIT_CONFIG_VALUE_{offset}"), value);
}
env.insert("GIT_CONFIG_COUNT".to_string(), "3".to_string());
env.insert("GIT_SSH_COMMAND".to_string(), "false".to_string());
env.insert("GITHUB_TOKEN".to_string(), "test-token-value".to_string());
for url in [
"git@github.com:fabro-sh/keystone-other",
"ssh://git@github.com/fabro-sh/keystone-other.git",
] {
let output = git(&["ls-remote", url], &env, root.path());
assert!(!output.status.success(), "{url}: {output:?}");
let stderr = String::from_utf8_lossy(&output.stderr);
// The failure names the (missing) HTTPS-analog fixture route,
// proving the prefix rule rewrote the URL away from SSH.
assert!(
stderr.contains("keystone-other"),
"{url} must be rewritten away from SSH, got: {stderr}"
);
assert!(
stderr.contains(&root.path().display().to_string()),
"{url} must land on the rewritten route, got: {stderr}"
);
assert!(!stderr.contains("test-token-value"), "{stderr}");
}
}
}

View file

@ -473,8 +473,9 @@ impl AgentAcpBackend {
emitter.notice(
RunNoticeLevel::Info,
RunNoticeCode::GithubTokenRefreshLimited,
"ACP agent stages receive workflow env at process launch; stages running beyond \
token expiry may need to be retried.",
"ACP agent stages receive workflow env at process launch; GITHUB_TOKEN access to \
every declared repository expires together, so stages running beyond token \
expiry may need to be retried.",
);
}
provider

View file

@ -293,6 +293,7 @@ pub mod error;
pub mod event;
pub mod file_resolver;
pub mod git;
pub(crate) mod git_bridge;
pub(crate) mod graph;
pub mod handler;
mod hook_context;

View file

@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::future::Future;
use std::path::Path;
use std::sync::{Arc, Mutex};
@ -18,7 +18,7 @@ use fabro_sandbox::{DockerSandboxOptions, SandboxSpec};
use fabro_static::EnvVars;
use fabro_types::settings::run::{
ApprovalMode, McpServerSettings as ResolvedMcpServerSettings, PullRequestSettings,
ResolvedMcpEntry, RunMode, RunNamespace as ResolvedRunSettings,
ResolvedGithubIntegration, ResolvedMcpEntry, RunMode, RunNamespace as ResolvedRunSettings,
RunPrepareSettings as ResolvedRunPrepareSettings,
};
use fabro_types::{ManifestPath, RunId, RunRunnableSource, SandboxProviderKind};
@ -107,9 +107,10 @@ pub struct StartServices {
pub artifact_sink: Option<ArtifactSink>,
pub run_control: Option<Arc<RunControlState>>,
pub github_app: Option<fabro_github::GitHubCredentials>,
/// Server-resolved GitHub integration permissions to inject into the
/// sandbox env. Empty when github integration has no permissions.
pub github_permissions: HashMap<String, String>,
/// The resolved GitHub integration request (interpolated permissions
/// plus declared additional repositories) to inject into the sandbox
/// env. Empty when the github integration requests no token.
pub github_integration: ResolvedGithubIntegration,
pub vault: Arc<AsyncRwLock<Vault>>,
pub catalog: Arc<Catalog>,
pub on_node: crate::OnNodeCallback,
@ -493,11 +494,13 @@ impl RunSession {
.environment
.resolve_env(secret_lookup)
.map_err(|err| Error::engine_with_source("failed to resolve run environment", err))?;
let github_permissions: Option<HashMap<String, String>> =
(!services.github_permissions.is_empty()).then(|| services.github_permissions.clone());
let github_integration = services
.github_integration
.is_token_requested()
.then(|| services.github_integration.clone());
let sandbox_env = SandboxEnvSpec {
toml_env,
github_permissions,
github_integration,
origin_url: record.repo_origin_url().map(str::to_string),
};
@ -632,7 +635,7 @@ fn resolve_sandbox_provider(settings: &ResolvedRunSettings) -> SandboxProviderKi
}
fn resolve_daytona_config(settings: &ResolvedRunSettings) -> DaytonaConfig {
daytona_config_from_environment(&settings.environment, !settings.clone.enabled)
daytona_config_from_environment(&settings.environment, &settings.clone)
}
fn resolve_docker_config(
@ -641,7 +644,7 @@ fn resolve_docker_config(
) -> Result<DockerSandboxOptions, Error> {
docker_config_from_environment_with_secrets(
&settings.environment,
!settings.clone.enabled,
&settings.clone,
secrets_lookup,
)
.map_err(|err| Error::engine_with_source("failed to resolve Docker environment config", err))
@ -1387,6 +1390,7 @@ reasoning = false
let settings = settings_from_run_layer(RunLayer {
clone: Some(RunCloneLayer {
enabled: Some(false),
depth: Some(1),
}),
..RunLayer::default()
});
@ -1397,6 +1401,45 @@ reasoning = false
.skip_clone
);
assert!(resolve_daytona_config(&settings.run).skip_clone);
assert_eq!(resolve_daytona_config(&settings.run).clone_depth, Some(1));
assert_eq!(
resolve_docker_config(&settings.run, |_| None)
.unwrap()
.clone_depth,
Some(1)
);
}
#[test]
fn zero_clone_depth_requests_full_history_from_clone_providers() {
let settings = settings_from_run_layer(RunLayer {
clone: Some(RunCloneLayer {
enabled: None,
depth: Some(0),
}),
..RunLayer::default()
});
assert_eq!(resolve_daytona_config(&settings.run).clone_depth, None);
assert_eq!(
resolve_docker_config(&settings.run, |_| None)
.unwrap()
.clone_depth,
None
);
}
#[test]
fn clone_providers_default_to_depth_100() {
let settings = settings_from_run_layer(RunLayer::default());
assert_eq!(resolve_daytona_config(&settings.run).clone_depth, Some(100));
assert_eq!(
resolve_docker_config(&settings.run, |_| None)
.unwrap()
.clone_depth,
Some(100)
);
}
#[test]
@ -1827,7 +1870,7 @@ reasoning = false
artifact_sink: None,
run_control: None,
github_app: None,
github_permissions: HashMap::new(),
github_integration: ResolvedGithubIntegration::default(),
vault: Arc::new(AsyncRwLock::new(start_vault(&[]))),
catalog: test_catalog(),
on_node: None,

View file

@ -287,7 +287,7 @@ async fn execute_test_run_with_options(
hooks: HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -349,7 +349,7 @@ async fn execute_runs_start_to_exit_and_returns_final_context() {
hooks: HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -488,7 +488,7 @@ async fn resumed_in_flight_node_starts_a_new_stage_execution() {
hooks: HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -599,7 +599,7 @@ async fn run_with_lifecycle(
hooks: HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),

View file

@ -24,6 +24,7 @@ use super::types::{InitOptions, Initialized, LlmSpec, Persisted, SandboxEnvSpec}
use crate::error::Error;
use crate::event::{Event, RunNoticeCode, RunNoticeLevel};
use crate::git::GitAuthor;
use crate::git_bridge;
use crate::handler::llm::{AgentAcpBackend, AgentApiBackend, BackendRouter, routing};
use crate::handler::{HandlerRegistry, default_registry};
#[cfg(test)]
@ -37,10 +38,14 @@ use crate::services::{
use crate::stage_execution::{StageExecutionSeed, StageExecutionTracker};
use crate::steering_hub::SteeringHub;
type BuiltSandboxEnv = (
HashMap<String, String>,
Option<Arc<InstallationTokenSource>>,
);
struct BuiltSandboxEnv {
env: HashMap<String, String>,
github_token: Option<Arc<InstallationTokenSource>>,
/// The validated effective repository set behind `github_token`.
/// Present only in App mode or when additional repositories are
/// declared; drives the eager access validation at initialization.
github_access: Option<fabro_github::GitHubRepositoryAccess>,
}
async fn run_hooks(
hook_runner: Option<&HookRunner>,
@ -91,41 +96,113 @@ fn build_sandbox_env(
spec: &SandboxEnvSpec,
github_app: Option<&fabro_github::GitHubCredentials>,
) -> Result<BuiltSandboxEnv, Error> {
let env = spec.toml_env.clone();
let mut env = spec.toml_env.clone();
let Some(permissions) = spec.github_permissions.as_ref().filter(|p| !p.is_empty()) else {
return Ok((env, None));
let no_token = |env| BuiltSandboxEnv {
env,
github_token: None,
github_access: None,
};
let Some(integration) = spec
.github_integration
.as_ref()
.filter(|integration| integration.is_token_requested())
else {
return Ok(no_token(env));
};
let declares_additional = integration.has_additional_repositories();
let Some(creds) = github_app else {
return Ok((env, None));
if declares_additional {
// Legacy permissions-only configuration stays best-effort, but a
// declared additional set is an explicit access requirement.
return Err(Error::Precondition(
"run.integrations.github.additional_repositories requires GitHub credentials, \
but none are configured"
.to_string(),
));
}
return Ok(no_token(env));
};
let source = match creds {
fabro_github::GitHubCredentials::Pat(token) => {
Some(InstallationTokenSource::pat(token.clone()))
}
fabro_github::GitHubCredentials::Installation(token) => {
Some(InstallationTokenSource::installation(token.clone()))
}
fabro_github::GitHubCredentials::App(_) => {
let Some(origin_url) = spec.origin_url.as_deref() else {
return Ok((env, None));
};
let https_url = fabro_github::ssh_url_to_https(origin_url);
let (owner, repo) = fabro_github::parse_github_owner_repo(&https_url)
.map_err(|err| Error::engine_with_anyhow("Failed to parse GitHub origin", err))?;
let permissions = serde_json::to_value(permissions).map_err(|err| {
Error::engine_with_source("Failed to serialize GitHub permissions", err)
})?;
Some(
InstallationTokenSource::for_repository(creds, owner, repo, permissions).map_err(
|err| Error::engine_with_anyhow("Failed to build GitHub token source", err),
)?,
// Validate the effective repository set whenever it matters: App mode
// scopes the mint to it, and any declared additional set must hold its
// invariants regardless of credential kind. Legacy PAT/static
// permissions-only runs skip it to preserve their origin-agnostic
// behavior.
let github_access =
if declares_additional || matches!(creds, fabro_github::GitHubCredentials::App(_)) {
fabro_github::GitHubRepositoryAccess::new(
spec.origin_url.as_deref(),
&integration.additional_repositories,
integration.permissions.clone(),
)
}
.map_err(|err| {
Error::engine_with_anyhow("Failed to validate GitHub repository access", err)
})?
} else {
None
};
let github_token = match github_access.as_ref() {
Some(access) => Some(InstallationTokenSource::for_access(creds, access).map_err(
|err| Error::engine_with_anyhow("Failed to build GitHub token source", err),
)?),
None => match creds {
fabro_github::GitHubCredentials::Pat(token) => {
Some(InstallationTokenSource::pat(token.clone()))
}
fabro_github::GitHubCredentials::Installation(token) => {
Some(InstallationTokenSource::installation(token.clone()))
}
// No origin URL and nothing declared: keep the legacy App-mode
// best-effort skip.
fabro_github::GitHubCredentials::App(_) => None,
},
};
Ok((env, source))
if declares_additional {
let access = github_access
.as_ref()
.expect("access is always constructed when additional repositories are declared");
git_bridge::merge_git_bridge_env(&mut env, &access.targets())?;
}
Ok(BuiltSandboxEnv {
env,
github_token,
github_access,
})
}
/// When additional repositories are declared, resolve their token before the
/// first workflow stage. App-backed sources first check that every target is
/// on one installation. Static credentials resolve locally; the first Git
/// operation remains their access check. Legacy permissions-only runs skip
/// eager resolution.
async fn resolve_declared_repository_token(built: &BuiltSandboxEnv) -> Result<(), Error> {
let Some(_) = built
.github_access
.as_ref()
.filter(|access| access.has_additional_repositories())
else {
return Ok(());
};
// `build_sandbox_env` guarantees a token source whenever additional
// repositories are declared; fail closed if that ever breaks.
let Some(source) = built.github_token.as_ref() else {
return Err(Error::Precondition(
"run.integrations.github.additional_repositories requires GitHub credentials, but \
none are configured"
.to_string(),
));
};
source.resolve().await.map_err(|err| {
Error::engine_with_anyhow(
"Failed to resolve the GitHub token for the declared repository set",
err,
)
})?;
Ok(())
}
async fn build_registry(
@ -443,10 +520,16 @@ pub async fn initialize(
});
}
let (base_env, github_token) = build_sandbox_env(
let built_env = build_sandbox_env(
&options.sandbox_env,
options.run_options.github_app.as_ref(),
)?;
resolve_declared_repository_token(&built_env).await?;
let BuiltSandboxEnv {
env: base_env,
github_token,
github_access: _,
} = built_env;
let tool_env_provider = Arc::new(WorkflowToolEnvProvider {
base_env: base_env.clone(),
github_token: github_token.clone(),
@ -849,7 +932,7 @@ mod tests {
hooks: fabro_hooks::HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -979,7 +1062,7 @@ mod tests {
let initialized = initialize(persisted, InitOptions {
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::from([("TEST_KEY".to_string(), "value".to_string())]),
github_permissions: None,
github_integration: None,
origin_url: None,
},
..test_init_options(
@ -1288,7 +1371,7 @@ mod tests {
hooks: fabro_hooks::HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault,
@ -1391,7 +1474,7 @@ mod tests {
hooks: fabro_hooks::HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -1533,7 +1616,7 @@ mod tests {
hooks: fabro_hooks::HookSettings { hooks: vec![] },
sandbox_env: SandboxEnvSpec {
toml_env: HashMap::new(),
github_permissions: None,
github_integration: None,
origin_url: None,
},
vault: auth_test_support::empty_vault(),
@ -1549,4 +1632,169 @@ mod tests {
assert!(matches!(result, Err(Error::Cancelled)));
}
mod github_integration_env {
//! Focused tests for `build_sandbox_env` /
//! `resolve_declared_repository_token` around declared additional
//! repositories. Installation-resolution failure naming is covered
//! by `fabro_github::access` tests; these prove the initialization
//! wiring: hard errors for declared sets, best-effort behavior for
//! legacy permissions-only configuration.
use fabro_github::test_support::{InstallationTokenMinter, installation_token_source};
use fabro_github::{GitHubAppCredentials, GitHubCredentials, InstallationToken};
use fabro_types::settings::run::ResolvedGithubIntegration;
use super::*;
fn integration(additional: &[&str]) -> ResolvedGithubIntegration {
ResolvedGithubIntegration {
permissions: HashMap::from([(
"contents".to_string(),
"read".to_string(),
)]),
additional_repositories: additional
.iter()
.map(|value| value.parse().expect("test slug should parse"))
.collect(),
}
}
fn spec(
origin: Option<&str>,
github_integration: Option<ResolvedGithubIntegration>,
) -> SandboxEnvSpec {
SandboxEnvSpec {
toml_env: HashMap::new(),
github_integration,
origin_url: origin.map(str::to_string),
}
}
#[test]
fn declared_additional_repositories_require_credentials() {
let spec = spec(
Some("https://github.com/fabro-sh/fabro"),
Some(integration(&["fabro-sh/keystone"])),
);
let Err(err) = build_sandbox_env(&spec, None) else {
panic!("declared additional repositories without credentials must fail");
};
assert!(
err.to_string().contains("requires GitHub credentials"),
"{err}"
);
}
#[test]
fn declared_additional_repositories_require_an_origin() {
let spec = spec(None, Some(integration(&["fabro-sh/keystone"])));
let creds = GitHubCredentials::Pat("ghp_x".to_string());
let Err(err) = build_sandbox_env(&spec, Some(&creds)) else {
panic!("declared additional repositories without an origin must fail");
};
assert!(
err.to_string().contains("GitHub repository access"),
"{err}"
);
}
#[test]
fn declared_repositories_inject_bridge_entries_and_keep_the_pat_source() {
let spec = spec(
Some("https://github.com/fabro-sh/fabro"),
Some(integration(&["fabro-sh/keystone"])),
);
let creds = GitHubCredentials::Pat("ghp_x".to_string());
let built = build_sandbox_env(&spec, Some(&creds)).unwrap();
assert!(built.github_token.is_some());
let access = built.github_access.expect("access should be constructed");
assert!(access.has_additional_repositories());
// Helper entry plus two SSH rewrites for each of the two
// effective repositories (origin + declared additional).
assert_eq!(
built.env.get("GIT_CONFIG_COUNT").map(String::as_str),
Some("5")
);
assert_eq!(
built.env.get("GIT_CONFIG_KEY_0").map(String::as_str),
Some("credential.https://github.com.helper")
);
assert_eq!(
built.env.get("GIT_TERMINAL_PROMPT").map(String::as_str),
Some("0")
);
}
#[test]
fn legacy_permissions_only_configuration_stays_best_effort() {
// No credentials: no error, no token source, no bridge entries.
let no_creds = spec(
Some("https://github.com/fabro-sh/fabro"),
Some(integration(&[])),
);
let built = build_sandbox_env(&no_creds, None).unwrap();
assert!(built.github_token.is_none());
assert!(!built.env.contains_key("GIT_CONFIG_COUNT"));
// App credentials without an origin: legacy best-effort skip.
let creds = GitHubCredentials::App(GitHubAppCredentials {
app_id: "1".to_string(),
private_key_pem: "unused".to_string(),
slug: None,
});
let no_origin = spec(None, Some(integration(&[])));
let built = build_sandbox_env(&no_origin, Some(&creds)).unwrap();
assert!(built.github_token.is_none());
assert!(built.github_access.is_none());
}
struct FailingMinter;
#[async_trait::async_trait]
impl InstallationTokenMinter for FailingMinter {
async fn mint(&self) -> anyhow::Result<InstallationToken> {
Err(anyhow::anyhow!("scripted mint failure"))
}
}
#[tokio::test]
async fn eager_validation_fails_when_the_declared_token_cannot_resolve() {
let access = fabro_github::GitHubRepositoryAccess::new(
Some("https://github.com/fabro-sh/fabro"),
&["fabro-sh/keystone".parse().unwrap()].into_iter().collect(),
HashMap::from([("contents".to_string(), "read".to_string())]),
)
.unwrap();
let built = BuiltSandboxEnv {
env: HashMap::new(),
github_token: Some(installation_token_source(
"fabro-sh/fabro (+1 additional)",
Arc::new(FailingMinter),
)),
github_access: access,
};
let err = resolve_declared_repository_token(&built).await.unwrap_err();
let message = err.to_string();
assert!(message.contains("declared repository set"), "{message}");
}
#[tokio::test]
async fn eager_validation_skips_legacy_permissions_only_runs() {
let built = BuiltSandboxEnv {
env: HashMap::new(),
github_token: Some(installation_token_source(
"fabro-sh/fabro",
Arc::new(FailingMinter),
)),
github_access: None,
};
resolve_declared_repository_token(&built)
.await
.expect("legacy permissions-only runs must not resolve eagerly");
}
}
}

View file

@ -8,7 +8,9 @@ use fabro_mcp::config::McpServerSettings;
use fabro_model::{Catalog, ProviderId};
use fabro_sandbox::SandboxSpec;
use fabro_template::TemplateContext;
use fabro_types::settings::run::{PullRequestSettings, RunModelControls};
use fabro_types::settings::run::{
PullRequestSettings, ResolvedGithubIntegration, RunModelControls,
};
use fabro_types::{ManifestPath, RunId, RunProjection};
use fabro_validate::{Diagnostic, Severity};
use fabro_vault::Vault;
@ -246,7 +248,10 @@ pub struct LlmSpec {
#[derive(Clone)]
pub struct SandboxEnvSpec {
pub toml_env: HashMap<String, String>,
pub github_permissions: Option<HashMap<String, String>>,
/// The resolved GitHub integration request (interpolated permissions
/// plus declared additional repositories). `None` when the run requests
/// no `GITHUB_TOKEN`.
pub github_integration: Option<ResolvedGithubIntegration>,
pub origin_url: Option<String>,
}

View file

@ -208,6 +208,19 @@ fn main() {
("DiffSummary", "fabro_types::DiffSummary", &[]),
("RepositoryRef", "fabro_types::RepositoryRef", &[]),
("WorkflowSettings", "fabro_types::WorkflowSettings", &[]),
// Run-level GitHub integration settings reuse the canonical resolved
// types instead of generating parallel API DTOs; the wire shape is
// identical (InterpString serializes as its source string).
(
"RunIntegrationsSettings",
"fabro_types::settings::run::RunIntegrationsSettings",
&[],
),
(
"RunIntegrationsGithubSettings",
"fabro_types::settings::run::RunIntegrationsGithubSettings",
&[],
),
("ServerSettings", "fabro_types::ServerSettings", &[]),
(
"ServerNamespace",

View file

@ -25,7 +25,10 @@ pub mod types {
ReasoningEffortFeature, Speed as BillingSpeed, TokenCounts as CompletionUsage,
};
pub use fabro_types::run_event::AgentSessionActivatedProps;
pub use fabro_types::settings::run::{McpHttpProtocol, RunModelControls, RunModelSettings};
pub use fabro_types::settings::run::{
McpHttpProtocol, RunIntegrationsGithubSettings, RunIntegrationsSettings, RunModelControls,
RunModelSettings,
};
pub use fabro_types::settings::server::{
GithubIntegrationSettings, GithubIntegrationStrategy, IntegrationWebhooksSettings,
LogDestination, ObjectStoreSettings, ServerApiSettings, ServerArtifactsSettings,

View file

@ -1,8 +1,10 @@
//! JSON parity test for `RunIntegrationsGithubSettings`.
//! JSON parity and type-identity tests for `RunIntegrationsGithubSettings`.
//!
//! Asserts that the API-side generated `RunIntegrationsGithubSettings` and
//! the canonical Rust resolved type round-trip through the same JSON shape.
//! Covers both the populated and empty-permissions cases.
//! Asserts that the API-side `RunIntegrationsGithubSettings` is the
//! canonical Rust resolved type (via `with_replacement` in `build.rs`) and
//! that both names round-trip through the same JSON shape. Covers the
//! populated and empty permissions cases as well as populated and empty
//! `additional_repositories` sets.
use fabro_api::types::{
RunIntegrationsGithubSettings as ApiRunIntegrationsGithubSettings,
@ -11,6 +13,22 @@ use fabro_api::types::{
use fabro_types::settings::run::{RunIntegrationsGithubSettings, RunIntegrationsSettings};
use serde_json::json;
/// Type-identity witnesses: the generated API names are the canonical Rust
/// types, not parallel DTOs. Compiles only when they are the same type.
#[expect(dead_code, reason = "compile-time type-identity witness")]
fn github_settings_type_identity(
value: ApiRunIntegrationsGithubSettings,
) -> RunIntegrationsGithubSettings {
value
}
#[expect(dead_code, reason = "compile-time type-identity witness")]
fn integrations_settings_type_identity(
value: ApiRunIntegrationsSettings,
) -> RunIntegrationsSettings {
value
}
#[test]
fn run_integrations_github_settings_round_trips_with_permissions() {
let json_value = json!({
@ -44,6 +62,43 @@ fn run_integrations_github_settings_round_trips_empty_permissions() {
assert_eq!(serde_json::to_value(&canonical).unwrap(), json_value);
}
#[test]
fn run_integrations_github_settings_round_trips_additional_repositories() {
let json_value = json!({
"permissions": { "contents": "read" },
"additional_repositories": ["fabro-sh/arc", "fabro-sh/keystone"],
});
let api: ApiRunIntegrationsGithubSettings =
serde_json::from_value(json_value.clone()).expect("api type should parse repositories");
let canonical: RunIntegrationsGithubSettings =
serde_json::from_value(json_value.clone()).expect("canonical type should parse");
assert_eq!(serde_json::to_value(&api).unwrap(), json_value);
assert_eq!(serde_json::to_value(&canonical).unwrap(), json_value);
}
#[test]
fn run_integrations_github_settings_omits_an_empty_repository_set() {
// An absent field and an explicit empty array both deserialize to the
// empty set, and the empty set serializes back with the field omitted —
// keeping single-repository settings byte-identical to older releases.
let empty_array = json!({
"permissions": {},
"additional_repositories": [],
});
let omitted = json!({ "permissions": {} });
let api: ApiRunIntegrationsGithubSettings =
serde_json::from_value(empty_array).expect("api type should parse an empty array");
let canonical: RunIntegrationsGithubSettings =
serde_json::from_value(omitted.clone()).expect("canonical type should parse");
assert!(api.additional_repositories.is_empty());
assert_eq!(serde_json::to_value(&api).unwrap(), omitted);
assert_eq!(serde_json::to_value(&canonical).unwrap(), omitted);
}
#[test]
fn run_integrations_settings_round_trips() {
let json_value = json!({

View file

@ -88,13 +88,23 @@ pub struct RunIntegrationsLayer {
#[serde(deny_unknown_fields)]
pub struct RunIntegrationsGithubLayer {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub permissions: Option<HashMap<String, InterpString>>,
pub permissions: Option<HashMap<String, InterpString>>,
/// Extra `owner/repository` slugs the minted `GITHUB_TOKEN` must cover in
/// addition to the implicit run origin. Kept as raw strings in this
/// sparse layer; slug validation happens at resolve time so diagnostics
/// can carry indexed paths. The higher-precedence list replaces the lower
/// one wholesale (`Some(vec![])` is an explicit clear); no `...` splice.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub additional_repositories: Option<Vec<String>>,
}
impl Combine for RunIntegrationsGithubLayer {
fn combine(self, other: Self) -> Self {
Self {
permissions: self.permissions.or(other.permissions),
permissions: self.permissions.or(other.permissions),
additional_repositories: self
.additional_repositories
.or(other.additional_repositories),
}
}
}
@ -303,6 +313,8 @@ pub struct RunCheckpointLayer {
pub struct RunCloneLayer {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub depth: Option<i32>,
}
/// `[run.run_branch]` — Fabro-managed checkpoint branch policy.

View file

@ -1,6 +1,8 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use fabro_types::GitHubRepositorySlug;
use fabro_types::settings::InterpString;
use fabro_types::settings::interp::ResolveCtx;
use fabro_types::settings::run::{
ArtifactsSettings, GitAuthorSettings, HookDefinition, HookType, InterviewProviderSettings,
McpServerSettings, McpTransport, MergeStrategy, NotificationProviderSettings,
@ -17,9 +19,9 @@ use crate::{
EnvironmentLayer, HookAgentMarker, HookEntry, HookTlsMode, InterviewProviderLayer,
InterviewsLayer, McpEntryLayer, MergeMap, ModelRefOrSplice, NotificationProviderLayer,
NotificationRouteLayer, RunAgentLayer, RunArtifactsLayer, RunCheckpointLayer, RunCloneLayer,
RunExecutionLayer, RunGitLayer, RunGoalLayer, RunIntegrationsLayer, RunLayer,
RunMetaBranchLayer, RunModelLayer, RunPrepareLayer, RunPullRequestLayer, RunRunBranchLayer,
RunScmLayer, StickyMap, StringOrSplice,
RunExecutionLayer, RunGitLayer, RunGoalLayer, RunIntegrationsGithubLayer, RunIntegrationsLayer,
RunLayer, RunMetaBranchLayer, RunModelLayer, RunPrepareLayer, RunPullRequestLayer,
RunRunBranchLayer, RunScmLayer, StickyMap, StringOrSplice,
};
pub fn resolve_run(
@ -28,7 +30,7 @@ pub fn resolve_run(
mcp_server_catalog: &HashMap<String, McpServerSettings>,
errors: &mut Vec<ResolveError>,
) -> RunNamespace {
let clone = resolve_clone(layer.clone.as_ref());
let clone = resolve_clone(layer.clone.as_ref(), errors);
let run_branch = resolve_run_branch(layer.run_branch.as_ref());
let mut meta_branch = resolve_meta_branch(layer.meta_branch.as_ref());
if !run_branch.enabled {
@ -85,23 +87,140 @@ pub fn resolve_run(
scm: resolve_scm(layer.scm.as_ref()),
pull_request,
artifacts: resolve_artifacts(layer.artifacts.as_ref(), errors),
integrations: resolve_integrations(layer.integrations.as_ref()),
integrations: resolve_integrations(layer.integrations.as_ref(), errors),
}
}
fn resolve_integrations(layer: Option<&RunIntegrationsLayer>) -> RunIntegrationsSettings {
fn resolve_integrations(
layer: Option<&RunIntegrationsLayer>,
errors: &mut Vec<ResolveError>,
) -> RunIntegrationsSettings {
let github = layer
.and_then(|integrations| integrations.github.as_ref())
.map(|github| RunIntegrationsGithubSettings {
// Collapse `Option<HashMap<...>>` -> `HashMap<...>`: both `None`
// and `Some({})` resolve to an empty map (no token requested).
// The presence distinction is only meaningful at merge time.
permissions: github.permissions.clone().unwrap_or_default(),
})
.map(|github| resolve_integrations_github(github, errors))
.unwrap_or_default();
RunIntegrationsSettings { github }
}
/// GitHub caps one installation token at 500 repositories; the implicit run
/// origin takes one slot.
const MAX_ADDITIONAL_REPOSITORIES: usize = 499;
fn resolve_integrations_github(
github: &RunIntegrationsGithubLayer,
errors: &mut Vec<ResolveError>,
) -> RunIntegrationsGithubSettings {
// Collapse `Option<HashMap<...>>` -> `HashMap<...>`: both `None`
// and `Some({})` resolve to an empty map (no token requested).
// The presence distinction is only meaningful at merge time. The same
// collapse applies to `additional_repositories` (`Some(vec![])` is an
// explicit clear that resolves to the empty set).
let permissions = github.permissions.clone().unwrap_or_default();
let raw_repositories = github
.additional_repositories
.as_deref()
.unwrap_or_default();
if raw_repositories.len() > MAX_ADDITIONAL_REPOSITORIES {
errors.push(ResolveError::Invalid {
path: "run.integrations.github.additional_repositories".to_string(),
reason: format!(
"at most {MAX_ADDITIONAL_REPOSITORIES} additional repositories are supported (the \
run origin takes the remaining slot of GitHub's 500-repository token limit), got \
{}",
raw_repositories.len()
),
});
}
let mut additional_repositories: BTreeSet<GitHubRepositorySlug> = BTreeSet::new();
for (index, value) in raw_repositories.iter().enumerate() {
let path = format!("run.integrations.github.additional_repositories[{index}]");
let Ok(slug) = value.parse::<GitHubRepositorySlug>() else {
errors.push(ResolveError::Invalid {
path,
reason: format!(
"`{value}` is not a full GitHub `owner/repository` slug (no scheme, host, \
ref, or extra path component)"
),
});
continue;
};
if let Some(existing) = additional_repositories.get(&slug) {
errors.push(ResolveError::Invalid {
path,
reason: format!(
"`{value}` duplicates `{existing}` (repository identity is case-insensitive)"
),
});
continue;
}
if let Some(first) = additional_repositories.first() {
if !first.same_owner(&slug) {
errors.push(ResolveError::Invalid {
path,
reason: format!(
"`{value}` has owner `{}` but `{first}` has owner `{}`; all repositories \
must share one owner because one GitHub App installation covers one \
account",
slug.owner(),
first.owner()
),
});
continue;
}
}
additional_repositories.insert(slug);
}
if !additional_repositories.is_empty() {
validate_additional_repository_permissions(&permissions, errors);
}
RunIntegrationsGithubSettings {
permissions,
additional_repositories,
}
}
/// A non-empty additional-repository set needs a token that can reach
/// repository contents. Only a literal `contents` value is checked here; a
/// templated value is re-checked after interpolation at the runtime boundary.
fn validate_additional_repository_permissions(
permissions: &HashMap<String, InterpString>,
errors: &mut Vec<ResolveError>,
) {
if permissions.is_empty() {
errors.push(ResolveError::Invalid {
path: "run.integrations.github.additional_repositories".to_string(),
reason: "additional repositories require [run.integrations.github.permissions] with \
a `contents` permission; a higher layer may have cleared the permissions"
.to_string(),
});
return;
}
let Some(contents) = permissions.get("contents") else {
errors.push(ResolveError::Invalid {
path: "run.integrations.github.permissions".to_string(),
reason: "additional repositories require the `contents` permission (`read` or \
`write`)"
.to_string(),
});
return;
};
if let Ok(literal) = contents.resolve_with(&mut ResolveCtx::new()) {
if !RunIntegrationsGithubSettings::contents_permission_allows_repository_access(&literal) {
errors.push(ResolveError::Invalid {
path: "run.integrations.github.permissions.contents".to_string(),
reason: format!(
"additional repositories require `contents = \"read\"` or `contents = \
\"write\"`, got `{literal}`"
),
});
}
}
}
fn resolve_goal(goal: Option<&RunGoalLayer>) -> Option<RunGoal> {
match goal? {
RunGoalLayer::Inline(value) => Some(RunGoal::Inline(value.clone())),
@ -244,9 +363,24 @@ fn resolve_checkpoint(checkpoint: Option<&RunCheckpointLayer>) -> RunCheckpointS
}
}
fn resolve_clone(clone: Option<&RunCloneLayer>) -> RunCloneSettings {
fn resolve_clone(
clone: Option<&RunCloneLayer>,
errors: &mut Vec<ResolveError>,
) -> RunCloneSettings {
let mut depth = clone
.and_then(|clone| clone.depth)
.unwrap_or(RunCloneSettings::DEFAULT_DEPTH);
if depth < 0 {
errors.push(ResolveError::Invalid {
path: "run.clone.depth".to_string(),
reason: "depth must be at least 0".to_string(),
});
depth = RunCloneSettings::DEFAULT_DEPTH;
}
RunCloneSettings {
enabled: clone.and_then(|clone| clone.enabled).unwrap_or(true),
depth,
}
}

View file

@ -126,6 +126,7 @@ fn resolves_run_defaults_from_empty_settings() {
assert!(!settings.environment.lifecycle.preserve);
assert!(settings.environment.lifecycle.stop_on_terminal);
assert!(settings.clone.enabled);
assert_eq!(settings.clone.depth, 100);
assert!(settings.run_branch.enabled);
assert!(settings.run_branch.push);
assert!(settings.meta_branch.enabled);
@ -296,6 +297,7 @@ _version = 1
[run.clone]
enabled = false
depth = 1
[run.run_branch]
enabled = true
@ -310,12 +312,52 @@ push = false
.run;
assert!(!settings.clone.enabled);
assert_eq!(settings.clone.depth, 1);
assert!(settings.run_branch.enabled);
assert!(!settings.run_branch.push);
assert!(settings.meta_branch.enabled);
assert!(!settings.meta_branch.push);
}
#[test]
fn zero_clone_depth_requests_full_history() {
let settings = super::workflow_settings_from_toml(
r"
_version = 1
[run.clone]
depth = 0
",
)
.expect("zero clone depth should resolve")
.run;
assert_eq!(settings.clone.depth, 0);
}
#[test]
fn rejects_negative_clone_depth() {
let error = super::workflow_settings_from_toml(
r"
_version = 1
[run.clone]
depth = -1
",
)
.expect_err("negative clone depth should not resolve");
let message = error.to_string();
assert!(
message.contains("run.clone.depth"),
"unexpected error: {message}"
);
assert!(
message.contains("at least 0"),
"unexpected error: {message}"
);
}
#[test]
fn disabling_run_branch_forces_meta_branch_off() {
let settings = super::workflow_settings_from_toml(
@ -879,6 +921,447 @@ issues = "{{ env.GH_PERM_LEVEL }}"
}
}
mod run_integrations_github_additional_repositories {
//! Layer + resolver tests for
//! `[run.integrations.github].additional_repositories`.
//!
//! The list replaces wholesale across layers (`[]` is an explicit clear),
//! resolves independently from `permissions`, and validates each entry as
//! a full `owner/repository` slug with indexed error paths.
use crate::SettingsLayer;
use crate::layers::Combine;
fn parse_settings(source: &str) -> SettingsLayer {
source
.parse::<SettingsLayer>()
.expect("fixture should parse via SettingsLayer")
}
fn invalid_paths_and_reasons(error: crate::Error) -> Vec<(String, String)> {
let errors = match error {
crate::Error::Resolve { errors, .. } => errors,
other => panic!("expected structured resolve errors, got {other:#}"),
};
errors
.into_iter()
.map(|error| match error {
crate::ResolveError::Invalid { path, reason } => (path, reason),
other => panic!("expected invalid-value error, got {other}"),
})
.collect()
}
fn resolved_repositories(settings: &fabro_types::WorkflowSettings) -> Vec<String> {
settings
.run
.integrations
.github
.additional_repositories
.iter()
.map(ToString::to_string)
.collect()
}
#[test]
fn resolves_one_and_multiple_repositories() {
let one = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
"#,
)
.expect("one additional repository should resolve");
assert_eq!(resolved_repositories(&one), vec!["fabro-sh/keystone"]);
assert!(one.run.integrations.github.has_additional_repositories());
let many = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone", "fabro-sh/arc"]
permissions = { contents = "write" }
"#,
)
.expect("multiple additional repositories should resolve");
assert_eq!(resolved_repositories(&many), vec![
"fabro-sh/arc",
"fabro-sh/keystone",
]);
}
#[test]
fn rejects_malformed_slugs_with_indexed_paths() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = [
"fabro-sh/keystone",
"https://github.com/fabro-sh/arc",
"git@github.com:fabro-sh/arc.git",
"fabro-sh/arc@main",
"not-a-slug",
]
permissions = { contents = "read" }
"#,
)
.expect_err("malformed slugs should not resolve");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec![
"run.integrations.github.additional_repositories[1]",
"run.integrations.github.additional_repositories[2]",
"run.integrations.github.additional_repositories[3]",
"run.integrations.github.additional_repositories[4]",
]
);
assert!(
invalid[0].1.contains("owner/repository"),
"reason should explain the slug grammar: {}",
invalid[0].1
);
}
#[test]
fn rejects_duplicate_and_case_variant_duplicate_slugs() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone", "Fabro-SH/Keystone"]
permissions = { contents = "read" }
"#,
)
.expect_err("case-variant duplicate slugs should not resolve");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec!["run.integrations.github.additional_repositories[1]"]
);
assert!(
invalid[0].1.contains("case-insensitive"),
"reason should mention case-insensitive identity: {}",
invalid[0].1
);
}
#[test]
fn rejects_cross_owner_additional_repositories() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone", "lithoscomputer/conveyor"]
permissions = { contents = "read" }
"#,
)
.expect_err("cross-owner additional repositories should not resolve");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec!["run.integrations.github.additional_repositories[1]"]
);
assert!(
invalid[0].1.contains("share one owner"),
"reason should explain the single-owner requirement: {}",
invalid[0].1
);
}
#[test]
fn rejects_more_than_the_installation_token_repository_limit() {
let repositories = (0..500)
.map(|index| format!("\"owner/repo-{index}\""))
.collect::<Vec<_>>()
.join(", ");
let error = super::workflow_settings_from_toml(&format!(
r#"
_version = 1
[run.integrations.github]
additional_repositories = [{repositories}]
permissions = {{ contents = "read" }}
"#,
))
.expect_err("500 additional repositories should not resolve");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid[0].0,
"run.integrations.github.additional_repositories"
);
assert!(invalid[0].1.contains("499"), "{}", invalid[0].1);
}
#[test]
fn rejects_additional_repositories_without_permissions() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
"#,
)
.expect_err("additional repositories without permissions should not resolve");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec!["run.integrations.github.additional_repositories"]
);
}
#[test]
fn rejects_additional_repositories_without_the_contents_permission() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { issues = "read" }
"#,
)
.expect_err("additional repositories require the contents permission");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec!["run.integrations.github.permissions"]
);
}
#[test]
fn rejects_a_literal_contents_permission_that_is_not_read_or_write() {
let error = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "admin" }
"#,
)
.expect_err("literal contents permission must be read or write");
let invalid = invalid_paths_and_reasons(error);
assert_eq!(
invalid
.iter()
.map(|(path, _)| path.as_str())
.collect::<Vec<_>>(),
vec!["run.integrations.github.permissions.contents"]
);
}
#[test]
fn defers_a_templated_contents_permission_to_the_runtime_boundary() {
let settings = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "{{ vars.GH_CONTENTS }}" }
"#,
)
.expect("templated contents permission resolves; the value is re-checked at runtime");
assert_eq!(resolved_repositories(&settings), vec!["fabro-sh/keystone"]);
}
#[test]
fn higher_layer_replaces_the_repository_list_wholesale() {
let workflow = parse_settings(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
"#,
);
let user = parse_settings(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/arc", "fabro-sh/widgets"]
permissions = { contents = "read" }
"#,
);
let merged = workflow.combine(user);
let resolved =
super::workflow_settings_from_layer(merged).expect("merged settings should resolve");
// The lists never union: the higher layer's single entry wins, while
// the permission map inherits independently from the lower layer.
assert_eq!(resolved_repositories(&resolved), vec!["fabro-sh/keystone"]);
assert_eq!(
resolved.run.integrations.github.permissions.len(),
1,
"permissions should inherit from the lower layer"
);
}
#[test]
fn absent_higher_layer_inherits_the_lower_repository_list() {
let workflow = parse_settings("_version = 1\n");
let user = parse_settings(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
"#,
);
let merged = workflow.combine(user);
let resolved =
super::workflow_settings_from_layer(merged).expect("merged settings should resolve");
assert_eq!(resolved_repositories(&resolved), vec!["fabro-sh/keystone"]);
}
#[test]
fn empty_higher_layer_list_clears_inherited_repositories() {
let workflow = parse_settings(
r"
_version = 1
[run.integrations.github]
additional_repositories = []
",
);
let user = parse_settings(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
"#,
);
let merged = workflow.combine(user);
let resolved =
super::workflow_settings_from_layer(merged).expect("merged settings should resolve");
assert!(
resolved
.run
.integrations
.github
.additional_repositories
.is_empty(),
"explicit [] should clear the inherited repository list"
);
// Permissions survive the repository clear: each field resolves
// independently.
assert!(resolved.run.integrations.github.is_token_requested());
}
#[test]
fn rejects_repositories_that_survive_a_cross_layer_permission_clear() {
let workflow = parse_settings(
r"
_version = 1
[run.integrations.github]
permissions = {}
",
);
let user = parse_settings(
r#"
_version = 1
[run.integrations.github]
additional_repositories = ["fabro-sh/keystone"]
permissions = { contents = "read" }
"#,
);
let merged = workflow.combine(user);
let error = super::workflow_settings_from_layer(merged)
.map(|_| ())
.expect_err("repositories with cleared permissions should not resolve");
let message = error.to_string();
assert!(
message.contains("additional_repositories"),
"error should name the invalid combination: {message}"
);
}
#[test]
fn permissions_only_and_fully_empty_shapes_are_preserved() {
let permissions_only = super::workflow_settings_from_toml(
r#"
_version = 1
[run.integrations.github.permissions]
issues = "read"
"#,
)
.expect("permissions-only settings should resolve");
assert!(
permissions_only
.run
.integrations
.github
.additional_repositories
.is_empty()
);
assert!(
permissions_only
.run
.integrations
.github
.is_token_requested()
);
let empty = super::workflow_settings_from_toml("_version = 1\n")
.expect("empty settings should resolve");
assert!(
empty
.run
.integrations
.github
.additional_repositories
.is_empty()
);
assert!(!empty.run.integrations.github.is_token_requested());
}
}
mod run_agent {
use crate::SettingsLayer;
use crate::layers::Combine;

View file

@ -8,7 +8,7 @@ use clap::Args;
use quick_xml::events::{BytesStart, Event};
use quick_xml::reader::Reader;
use super::{PlannedCommand, capture_command, command, workspace_root};
use super::{PlannedCommand, command, workspace_root};
const TOOL_CONFIG_RELATIVE: &str = "target/bench-tests/nextest-tool.toml";
const TOOL_CONFIG_BODY: &str = "\
@ -67,7 +67,7 @@ pub(crate) fn bench_tests(args: BenchTestsArgs) -> Result<()> {
}
let root = workspace_root();
let git_sha = resolve_head_sha(&root)?;
let git_sha = super::resolve_git_revision(&root, "HEAD")?;
let tool_config = ensure_tool_config(&root)?;
let junit_path = root
.join("target")
@ -180,21 +180,6 @@ fn ensure_tool_config(root: &Path) -> Result<PathBuf> {
Ok(path)
}
fn resolve_head_sha(root: &Path) -> Result<String> {
let cmd = PlannedCommand::new("git").arg("rev-parse").arg("HEAD");
let output = capture_command(root, &cmd)?;
if !output.status.success() {
bail!(
"git rev-parse HEAD failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8(output.stdout)
.context("git rev-parse HEAD returned non-UTF-8")?
.trim()
.to_string())
}
#[expect(
clippy::disallowed_methods,
reason = "fabro-dev opens the CSV file synchronously; bench-tests is a CLI tool, not a Tokio runtime"

View file

@ -148,6 +148,24 @@ pub(crate) fn capture_command(cwd: &Path, planned: &PlannedCommand) -> Result<Ou
.with_context(|| format!("running {}", planned.to_shell_line()))
}
pub(crate) fn resolve_git_revision(root: &Path, revision: &str) -> Result<String> {
let command = PlannedCommand::new("git")
.arg("rev-parse")
.arg("--verify")
.arg(revision);
let output = capture_command(root, &command)?;
if !output.status.success() {
anyhow::bail!(
"git rev-parse --verify {revision} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
}
Ok(String::from_utf8(output.stdout)
.with_context(|| format!("git rev-parse --verify {revision} returned non-UTF-8"))?
.trim()
.to_string())
}
#[expect(
clippy::disallowed_methods,
reason = "dev CLI sanitizes inherited Cargo build-script env before spawning nested cargo"

View file

@ -8,6 +8,9 @@ use super::{PlannedCommand, capture_command, run_command, spa_refresh, workspace
const RELEASE_EPOCH: &str = "2026-01-01";
const RELEASE_TEST_SEGMENT_WRITE_KEY: &str = "fake-for-local-smoke";
const MAX_PUSH_ATTEMPTS: u32 = 4;
const RELEASE_BRANCH: &str = "main";
const RELEASE_REMOTE: &str = "origin";
#[derive(Debug, Args)]
pub(crate) struct ReleaseArgs {
@ -36,6 +39,55 @@ struct ReleasePlan {
root: PathBuf,
}
struct ReleaseVersions {
current: String,
next: String,
}
impl ReleaseVersions {
fn tag(&self) -> String {
format!("v{}", self.next)
}
}
struct RemoteReleaseState {
main: String,
tag: Option<String>,
}
#[derive(Debug, PartialEq, Eq)]
enum PushFailureDisposition {
Published,
Retryable,
Unchanged,
Inconsistent,
}
impl RemoteReleaseState {
fn classify_push_failure(
&self,
remote_main_before: &str,
release_head: &str,
local_contains_remote_before: bool,
) -> PushFailureDisposition {
let main_is_release = self.main == release_head;
let tag_is_release = self.tag.as_deref() == Some(release_head);
if main_is_release && tag_is_release {
PushFailureDisposition::Published
} else if main_is_release || tag_is_release {
PushFailureDisposition::Inconsistent
} else if !local_contains_remote_before
|| self.main != remote_main_before
|| self.tag.is_some()
{
PushFailureDisposition::Retryable
} else {
PushFailureDisposition::Unchanged
}
}
}
#[expect(
clippy::print_stdout,
reason = "dev release command reports progress and dry-run commands directly"
@ -52,64 +104,21 @@ pub(crate) fn release(args: ReleaseArgs) -> Result<()> {
};
let cargo_toml = plan.root.join("Cargo.toml");
let current_version = read_current_version(&cargo_toml)?;
println!("Current version: {current_version}");
let base_version = plan.next_base_version()?;
let new_version = plan.compute_release_version(&base_version)?;
let tag = format!("v{new_version}");
println!("Releasing {new_version} (tag {tag})");
let versions = plan.compute_versions(&cargo_toml)?;
let tag = versions.tag();
println!("Current version: {}", versions.current);
println!("Releasing {} (tag {tag})", versions.next);
if plan.dry_run {
plan.print_dry_run(&current_version, &new_version, &tag);
plan.print_dry_run(&versions);
return Ok(());
}
plan.ensure_main_branch()?;
plan.ensure_clean_worktree()?;
spa_refresh::spa_refresh_root(&plan.root)?;
plan.verify_release_tests()?;
update_version(&cargo_toml, &current_version, &new_version)?;
println!("Updated {}", cargo_toml.display());
run_command(
&plan.root,
&PlannedCommand::new("cargo")
.arg("update")
.arg("--workspace"),
)?;
println!("Updated Cargo.lock");
run_command(
&plan.root,
&PlannedCommand::new("git")
.arg("add")
.arg("Cargo.toml")
.arg("Cargo.lock"),
)?;
run_command(
&plan.root,
&PlannedCommand::new("git")
.arg("commit")
.arg("-m")
.arg(format!("Bump version to {new_version}")),
)?;
run_command(
&plan.root,
&PlannedCommand::new("git")
.arg("tag")
.arg("-a")
.arg(&tag)
.arg("-m")
.arg(&tag),
)?;
run_command(
&plan.root,
&PlannedCommand::new("git")
.arg("push")
.arg("origin")
.arg("main")
.arg(&tag),
)?;
let tag = plan.commit_tag_and_push(&cargo_toml, versions)?;
println!();
println!("Released {tag}");
@ -156,6 +165,347 @@ impl ReleasePlan {
}
}
fn compute_versions(&self, cargo_toml: &Path) -> Result<ReleaseVersions> {
let current = read_current_version(cargo_toml)?;
let base_version = self.next_base_version()?;
let next = self.compute_release_version(&base_version)?;
Ok(ReleaseVersions { current, next })
}
/// Commits the version bump, tags it, and pushes `main` plus the tag
/// atomically. When the push is rejected because origin/main moved while
/// the release ran, rebuilds the bump commit and tag on the fresh tip
/// and retries.
#[expect(
clippy::print_stdout,
reason = "dev release command reports push retry progress directly"
)]
fn commit_tag_and_push(
&self,
cargo_toml: &Path,
mut versions: ReleaseVersions,
) -> Result<String> {
let mut attempt = 1;
loop {
self.ensure_main_branch()?;
self.ensure_clean_worktree()
.context("working tree changed while the release was running")?;
let start_head = super::resolve_git_revision(&self.root, "HEAD")?;
let remote_tracking_ref = Self::remote_tracking_ref();
let remote_main_before =
super::resolve_git_revision(&self.root, &remote_tracking_ref).with_context(|| {
format!(
"release requires a local {} tracking ref; fetch {RELEASE_REMOTE} and retry",
Self::remote_branch()
)
})?;
let local_contains_remote_before =
self.commit_is_ancestor(&remote_main_before, &start_head)?;
let tag = versions.tag();
self.create_bump_commit_and_tag(cargo_toml, &versions)?;
let release_head = super::resolve_git_revision(&self.root, "HEAD")?;
let Err(push_error) = self.push_main_and_tag(&tag) else {
return Ok(tag);
};
let disposition = self
.push_failure_disposition(
&tag,
&remote_main_before,
&release_head,
local_contains_remote_before,
)
.context("push failed and origin could not be verified")?;
match disposition {
PushFailureDisposition::Published => {
println!(
"Push reported an error, but {RELEASE_REMOTE} contains {RELEASE_BRANCH} \
and {tag} at {release_head}"
);
return Ok(tag);
}
PushFailureDisposition::Unchanged => return Err(push_error),
PushFailureDisposition::Inconsistent => {
return Err(push_error.context(
"origin contains only part of the release; inspect the remote refs before \
retrying",
));
}
PushFailureDisposition::Retryable => {
if attempt == MAX_PUSH_ATTEMPTS {
return Err(push_error);
}
}
}
let remote_branch = Self::remote_branch();
println!(
"Origin changed during push attempt {attempt} of {MAX_PUSH_ATTEMPTS}; rebuilding \
the release on the latest {remote_branch}"
);
self.resync_with_origin_main(&tag, &start_head, &release_head)?;
versions = self.compute_versions(cargo_toml)?;
println!("Retrying as {} (tag {})", versions.next, versions.tag());
attempt += 1;
}
}
#[expect(
clippy::print_stdout,
reason = "dev release command reports version bump progress directly"
)]
fn create_bump_commit_and_tag(
&self,
cargo_toml: &Path,
versions: &ReleaseVersions,
) -> Result<()> {
update_version(cargo_toml, &versions.current, &versions.next)?;
println!("Updated {}", cargo_toml.display());
let [cargo_update, git_add, git_commit, git_tag] = Self::bump_commands(versions);
run_command(&self.root, &cargo_update)?;
println!("Updated Cargo.lock");
for command in [git_add, git_commit, git_tag] {
run_command(&self.root, &command)?;
}
Ok(())
}
fn bump_commands(versions: &ReleaseVersions) -> [PlannedCommand; 4] {
let tag = versions.tag();
[
PlannedCommand::new("cargo")
.arg("update")
.arg("--workspace"),
PlannedCommand::new("git")
.arg("add")
.arg("Cargo.toml")
.arg("Cargo.lock"),
PlannedCommand::new("git")
.arg("commit")
.arg("-m")
.arg(format!("Bump version to {}", versions.next)),
PlannedCommand::new("git")
.arg("tag")
.arg("-a")
.arg(&tag)
.arg("-m")
.arg(tag),
]
}
fn push_main_and_tag(&self, tag: &str) -> Result<()> {
run_command(&self.root, &Self::push_command(tag))
}
fn push_failure_disposition(
&self,
tag: &str,
remote_main_before: &str,
release_head: &str,
local_contains_remote_before: bool,
) -> Result<PushFailureDisposition> {
Ok(self.remote_release_state(tag)?.classify_push_failure(
remote_main_before,
release_head,
local_contains_remote_before,
))
}
fn commit_is_ancestor(&self, ancestor: &str, descendant: &str) -> Result<bool> {
let output = capture_command(
&self.root,
&PlannedCommand::new("git")
.arg("merge-base")
.arg("--is-ancestor")
.arg(ancestor)
.arg(descendant),
)?;
if output.status.success() {
return Ok(true);
}
if output.status.code() == Some(1) {
return Ok(false);
}
bail!(
"failed to compare git commits {ancestor} and {descendant}: {}",
String::from_utf8_lossy(&output.stderr).trim()
)
}
fn remote_release_state(&self, tag: &str) -> Result<RemoteReleaseState> {
let branch_ref = Self::branch_ref();
let tag_ref = Self::tag_ref(tag);
let peeled_tag_ref = format!("{tag_ref}^{{}}");
let output = capture_command(
&self.root,
&PlannedCommand::new("git")
.arg("ls-remote")
.arg(RELEASE_REMOTE)
.arg(&branch_ref)
.arg(&tag_ref)
.arg(&peeled_tag_ref),
)?;
if !output.status.success() {
bail!(
"failed to inspect release refs on {RELEASE_REMOTE}: {}",
String::from_utf8_lossy(&output.stderr).trim()
);
}
let stdout =
String::from_utf8(output.stdout).context("git ls-remote returned non-UTF-8 output")?;
let mut main = None;
let mut tag_object = None;
let mut peeled_tag = None;
for line in stdout.lines() {
let (object_id, reference) = line
.split_once('\t')
.with_context(|| format!("unexpected git ls-remote output: {line}"))?;
if reference == branch_ref {
main = Some(object_id.to_string());
} else if reference == tag_ref {
tag_object = Some(object_id.to_string());
} else if reference == peeled_tag_ref {
peeled_tag = Some(object_id.to_string());
}
}
let remote_branch = Self::remote_branch();
let main = main
.with_context(|| format!("{remote_branch} was not present in git ls-remote output"))?;
Ok(RemoteReleaseState {
main,
tag: peeled_tag.or(tag_object),
})
}
/// Drops the bump commit and tag this run created, then fast-forwards
/// onto the updated origin/main. `--keep` preserves worktree edits, and
/// `--ff-only` refuses to discard commits that did not come from origin.
fn resync_with_origin_main(
&self,
tag: &str,
start_head: &str,
release_head: &str,
) -> Result<()> {
self.ensure_main_branch()?;
let current_head = super::resolve_git_revision(&self.root, "HEAD")?;
if current_head != release_head {
bail!(
"local {RELEASE_BRANCH} changed while the release was running; refusing to move \
it from {current_head} back to {start_head}"
);
}
let tag_ref = Self::tag_ref(tag);
let tag_object = super::resolve_git_revision(&self.root, &tag_ref)
.with_context(|| format!("local release tag {tag} changed while the release ran"))?;
let tag_commit = super::resolve_git_revision(&self.root, &format!("{tag_ref}^{{commit}}"))
.with_context(|| format!("local release tag {tag} changed while the release ran"))?;
if tag_commit != release_head {
bail!(
"local release tag {tag} changed while the release was running; refusing to \
delete it"
);
}
run_command(
&self.root,
&PlannedCommand::new("git")
.arg("reset")
.arg("--keep")
.arg(start_head),
)
.context(
"working tree changed while the release was running; local edits were preserved",
)?;
run_command(
&self.root,
&PlannedCommand::new("git")
.arg("update-ref")
.arg("-d")
.arg(&tag_ref)
.arg(tag_object),
)
.with_context(|| format!("local release tag {tag} changed while the release ran"))?;
self.ensure_clean_worktree()
.context("working tree changed while the release was running")?;
run_command(
&self.root,
&PlannedCommand::new("git")
.arg("fetch")
.arg("--tags")
.arg(RELEASE_REMOTE)
.arg(RELEASE_BRANCH),
)?;
let remote_branch = Self::remote_branch();
run_command(
&self.root,
&PlannedCommand::new("git")
.arg("merge")
.arg("--ff-only")
.arg(&remote_branch),
)
.with_context(|| {
format!(
"local {RELEASE_BRANCH} has diverged from {remote_branch}; reconcile manually and \
rerun the release"
)
})?;
self.ensure_clean_worktree()
.context("working tree changed while the release was running")
}
fn branch_ref() -> String {
format!("refs/heads/{RELEASE_BRANCH}")
}
fn remote_branch() -> String {
format!("{RELEASE_REMOTE}/{RELEASE_BRANCH}")
}
fn remote_tracking_ref() -> String {
format!("refs/remotes/{RELEASE_REMOTE}/{RELEASE_BRANCH}")
}
fn tag_ref(tag: &str) -> String {
format!("refs/tags/{tag}")
}
fn push_command(tag: &str) -> PlannedCommand {
PlannedCommand::new("git")
.arg("push")
.arg("--atomic")
.arg(RELEASE_REMOTE)
.arg(RELEASE_BRANCH)
.arg(tag)
}
fn ensure_main_branch(&self) -> Result<()> {
let output = capture_command(
&self.root,
&PlannedCommand::new("git")
.arg("symbolic-ref")
.arg("--quiet")
.arg("HEAD"),
)?;
if !output.status.success() {
bail!("release must run from {RELEASE_BRANCH}; HEAD is not a local branch");
}
let head_ref = String::from_utf8(output.stdout)
.context("git symbolic-ref returned non-UTF-8 output")?;
let head_ref = head_ref.trim();
if head_ref != Self::branch_ref() {
let branch = head_ref.strip_prefix("refs/heads/").unwrap_or(head_ref);
bail!("release must run from {RELEASE_BRANCH}; current branch is {branch}");
}
Ok(())
}
fn ensure_clean_worktree(&self) -> Result<()> {
let output = capture_command(
&self.root,
@ -195,7 +545,7 @@ impl ReleasePlan {
clippy::print_stdout,
reason = "dev release command reports dry-run commands directly"
)]
fn print_dry_run(&self, current_version: &str, new_version: &str, tag: &str) {
fn print_dry_run(&self, versions: &ReleaseVersions) {
println!("DRY RUN: would refresh SPA assets:");
println!("{}", Self::spa_refresh_command().to_shell_line());
@ -206,31 +556,15 @@ impl ReleasePlan {
println!("{}", Self::release_tests_command().to_shell_line());
}
println!("DRY RUN: would update Cargo.toml version {current_version} -> {new_version}");
for command in [
PlannedCommand::new("cargo")
.arg("update")
.arg("--workspace"),
PlannedCommand::new("git")
.arg("add")
.arg("Cargo.toml")
.arg("Cargo.lock"),
PlannedCommand::new("git")
.arg("commit")
.arg("-m")
.arg(format!("Bump version to {new_version}")),
PlannedCommand::new("git")
.arg("tag")
.arg("-a")
.arg(tag)
.arg("-m")
.arg(tag),
PlannedCommand::new("git")
.arg("push")
.arg("origin")
.arg("main")
.arg(tag),
] {
println!(
"DRY RUN: would update Cargo.toml version {} -> {}",
versions.current, versions.next
);
let tag = versions.tag();
for command in Self::bump_commands(versions)
.into_iter()
.chain(std::iter::once(Self::push_command(&tag)))
{
println!("{}", command.to_shell_line());
}
}
@ -321,3 +655,396 @@ fn workspace_package_version<'a>(
)
})
}
#[cfg(test)]
mod tests {
use super::*;
const WORKSPACE_MANIFEST: &str = r#"[workspace]
members = ["app"]
[workspace.package]
version = "0.1.0"
"#;
const MEMBER_MANIFEST: &str = r#"[package]
name = "app"
edition = "2021"
version.workspace = true
"#;
fn git(root: &Path, args: &[&str]) -> String {
let mut command = PlannedCommand::new("git");
for arg in args {
command = command.arg(*arg);
}
let output = capture_command(root, &command).expect("git should spawn");
assert!(
output.status.success(),
"git {args:?} failed\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
String::from_utf8_lossy(&output.stdout).trim().to_string()
}
fn configure_identity(repo: &Path) {
git(repo, &["config", "user.name", "Release Test"]);
git(repo, &["config", "user.email", "release-test@example.com"]);
}
/// A fixture with a bare `origin`, a `work` clone releases run from, and
/// an `other` clone that simulates concurrent pushes.
struct RaceFixture {
_dir: tempfile::TempDir,
origin: PathBuf,
work: PathBuf,
other: PathBuf,
}
#[expect(
clippy::disallowed_methods,
reason = "release tests build git fixture repositories synchronously"
)]
fn race_fixture() -> RaceFixture {
let dir = tempfile::tempdir().expect("creating fixture");
let origin = dir.path().join("origin.git");
let work = dir.path().join("work");
let other = dir.path().join("other");
std::fs::create_dir(&origin).expect("creating origin dir");
git(&origin, &["init", "--bare", "-b", "main"]);
std::fs::create_dir(&work).expect("creating work dir");
git(&work, &["init", "-b", "main"]);
configure_identity(&work);
std::fs::write(work.join("Cargo.toml"), WORKSPACE_MANIFEST).expect("writing manifest");
std::fs::create_dir_all(work.join("app/src")).expect("creating member dirs");
std::fs::write(work.join("app/Cargo.toml"), MEMBER_MANIFEST)
.expect("writing member manifest");
std::fs::write(work.join("app/src/lib.rs"), "").expect("writing member lib");
git(&work, &["add", "."]);
git(&work, &["commit", "-m", "initial"]);
git(&work, &[
"remote",
"add",
"origin",
origin.to_str().expect("origin path should be utf-8"),
]);
git(&work, &["push", "-u", "origin", "main"]);
git(dir.path(), &[
"clone",
origin.to_str().expect("origin path should be utf-8"),
"other",
]);
configure_identity(&other);
RaceFixture {
_dir: dir,
origin,
work,
other,
}
}
#[expect(
clippy::disallowed_methods,
reason = "release tests write fixture files synchronously"
)]
fn write_file(path: &Path, contents: &str) {
std::fs::write(path, contents).expect("writing fixture file");
}
fn nightly_plan(root: &Path) -> ReleasePlan {
ReleasePlan {
nightly: true,
release_date: NaiveDate::from_ymd_opt(2026, 1, 1).expect("valid release date"),
dry_run: false,
skip_tests: true,
root: root.to_path_buf(),
}
}
fn push_concurrent_commit(fixture: &RaceFixture) {
write_file(&fixture.other.join("README.md"), "concurrent\n");
git(&fixture.other, &["add", "README.md"]);
git(&fixture.other, &["commit", "-m", "concurrent work"]);
git(&fixture.other, &["push", RELEASE_REMOTE, RELEASE_BRANCH]);
}
#[test]
fn failed_push_check_recognizes_a_published_release() {
let fixture = race_fixture();
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = versions.tag();
let start_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving initial HEAD");
plan.create_bump_commit_and_tag(&cargo_toml, &versions)
.expect("creating release commit and tag");
let release_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving release HEAD");
git(&fixture.work, &[
"push",
"--atomic",
RELEASE_REMOTE,
RELEASE_BRANCH,
tag.as_str(),
]);
assert_eq!(
plan.push_failure_disposition(&tag, &start_head, &release_head, true)
.expect("inspecting published refs"),
PushFailureDisposition::Published
);
}
#[test]
fn push_failure_classification_rejects_an_unchanged_remote() {
let state = RemoteReleaseState {
main: "start".to_string(),
tag: None,
};
assert_eq!(
state.classify_push_failure("start", "release", true),
PushFailureDisposition::Unchanged
);
}
#[test]
fn release_requires_the_main_branch() {
let fixture = race_fixture();
git(&fixture.work, &["switch", "-c", "feature"]);
let error = nightly_plan(&fixture.work)
.ensure_main_branch()
.expect_err("a release from another branch should fail");
assert!(
format!("{error:#}").contains("release must run from main; current branch is feature"),
"error should identify the required and current branches: {error:#}"
);
}
#[test]
fn resync_preserves_worktree_edits() {
let fixture = race_fixture();
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = versions.tag();
let start_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving initial HEAD");
plan.create_bump_commit_and_tag(&cargo_toml, &versions)
.expect("creating release commit and tag");
let release_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving release HEAD");
write_file(&fixture.work.join("app/src/lib.rs"), "user edit\n");
let error = plan
.resync_with_origin_main(&tag, &start_head, &release_head)
.expect_err("resync should stop when the worktree changes");
assert!(
format!("{error:#}").contains("working tree changed while the release was running"),
"error should explain why resync stopped: {error:#}"
);
let diff = git(&fixture.work, &["diff", "--", "app/src/lib.rs"]);
assert!(
diff.contains("+user edit"),
"resync should preserve the worktree edit:\n{diff}"
);
}
#[test]
fn resync_preserves_a_commit_created_after_the_release_commit() {
let fixture = race_fixture();
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = versions.tag();
let start_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving initial HEAD");
plan.create_bump_commit_and_tag(&cargo_toml, &versions)
.expect("creating release commit and tag");
let release_head = super::super::resolve_git_revision(&fixture.work, "HEAD")
.expect("resolving release HEAD");
write_file(&fixture.work.join("local.txt"), "concurrent local work\n");
git(&fixture.work, &["add", "local.txt"]);
git(&fixture.work, &["commit", "-m", "concurrent local work"]);
let error = plan
.resync_with_origin_main(&tag, &start_head, &release_head)
.expect_err("resync should stop when local main changes");
assert!(
format!("{error:#}").contains("local main changed while the release was running"),
"error should explain why resync stopped: {error:#}"
);
let subject = git(&fixture.work, &["log", "-1", "--format=%s"]);
assert_eq!(subject, "concurrent local work");
git(&fixture.work, &[
"rev-parse",
"--verify",
&ReleasePlan::tag_ref(&tag),
]);
}
#[test]
fn push_does_not_retry_when_remote_refs_are_unchanged() {
let fixture = race_fixture();
write_file(&fixture.work.join("local.txt"), "unpushed local work\n");
git(&fixture.work, &["add", "local.txt"]);
git(&fixture.work, &["commit", "-m", "unpushed local work"]);
let missing_push_remote = fixture
.origin
.parent()
.expect("origin should have a parent")
.join("missing.git");
git(&fixture.work, &[
"remote",
"set-url",
"--push",
RELEASE_REMOTE,
missing_push_remote
.to_str()
.expect("push remote path should be utf-8"),
]);
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
plan.commit_tag_and_push(&cargo_toml, versions)
.expect_err("an unrelated push failure should be returned");
let subjects = git(&fixture.work, &["log", "--format=%s"]);
assert_eq!(subjects.lines().collect::<Vec<_>>(), [
"Bump version to 0.100.0-nightly.0",
"unpushed local work",
"initial"
]);
}
#[test]
fn push_rebuilds_bump_commit_when_origin_main_moves() {
let fixture = race_fixture();
push_concurrent_commit(&fixture);
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = plan
.commit_tag_and_push(&cargo_toml, versions)
.expect("push should rescue itself when origin/main moves");
assert_eq!(tag, "v0.100.0-nightly.0");
let subjects = git(&fixture.origin, &["log", "--format=%s", "main"]);
assert_eq!(subjects.lines().collect::<Vec<_>>(), [
"Bump version to 0.100.0-nightly.0",
"concurrent work",
"initial"
]);
git(&fixture.origin, &[
"rev-parse",
"--verify",
"refs/tags/v0.100.0-nightly.0",
]);
}
#[test]
fn push_rebuilds_when_origin_main_was_fetched_but_not_merged() {
let fixture = race_fixture();
push_concurrent_commit(&fixture);
git(&fixture.work, &["fetch", RELEASE_REMOTE, RELEASE_BRANCH]);
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = plan
.commit_tag_and_push(&cargo_toml, versions)
.expect("push should rescue a local main behind its tracking branch");
assert_eq!(tag, "v0.100.0-nightly.0");
let subjects = git(&fixture.origin, &["log", "--format=%s", RELEASE_BRANCH]);
assert_eq!(subjects.lines().collect::<Vec<_>>(), [
"Bump version to 0.100.0-nightly.0",
"concurrent work",
"initial"
]);
}
#[test]
fn push_recomputes_version_when_tag_is_taken() {
let fixture = race_fixture();
git(&fixture.other, &[
"tag",
"-a",
"v0.100.0-nightly.0",
"-m",
"v0.100.0-nightly.0",
]);
git(&fixture.other, &["push", "origin", "v0.100.0-nightly.0"]);
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let tag = plan
.commit_tag_and_push(&cargo_toml, versions)
.expect("push should rescue itself when the tag is taken");
assert_eq!(tag, "v0.100.0-nightly.1");
git(&fixture.origin, &[
"rev-parse",
"--verify",
"refs/tags/v0.100.0-nightly.1",
]);
let subject = git(&fixture.origin, &["log", "-1", "--format=%s", "main"]);
assert_eq!(subject, "Bump version to 0.100.0-nightly.1");
}
#[test]
fn push_preserves_unpushed_local_commits_on_divergence() {
let fixture = race_fixture();
write_file(&fixture.work.join("local.txt"), "local\n");
git(&fixture.work, &["add", "local.txt"]);
git(&fixture.work, &["commit", "-m", "unpushed local work"]);
push_concurrent_commit(&fixture);
let plan = nightly_plan(&fixture.work);
let cargo_toml = fixture.work.join("Cargo.toml");
let versions = plan
.compute_versions(&cargo_toml)
.expect("computing versions");
let error = plan
.commit_tag_and_push(&cargo_toml, versions)
.expect_err("diverged local main should fail instead of being reset away");
assert!(
format!("{error:#}").contains("diverged"),
"error should explain the divergence: {error:#}"
);
let subject = git(&fixture.work, &["log", "-1", "--format=%s"]);
assert_eq!(subject, "unpushed local work");
}
}

View file

@ -34,7 +34,7 @@ members = []
version = "0.1.0"
"#,
);
git(fixture.path(), &["init"]);
git(fixture.path(), &["init", "-b", "main"]);
git(fixture.path(), &["config", "user.name", "Release Test"]);
git(fixture.path(), &[
"config",
@ -107,6 +107,10 @@ fn dry_run_computes_stable_version_from_date() {
stdout.contains("git tag -a v0.100.0 -m v0.100.0"),
"dry-run should print release tag command:\n{stdout}"
);
assert!(
stdout.contains("git push --atomic origin main v0.100.0"),
"dry-run should print the atomic push command:\n{stdout}"
);
assert!(
stdout.contains(
"unset GH_TOKEN GITHUB_TOKEN && SEGMENT_WRITE_KEY=fake-for-local-smoke cargo nextest run --locked"

View file

@ -1,3 +1,8 @@
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@ -25,8 +30,10 @@ impl RepositoryRef {
///
/// Construction enforces GitHub's owner and repository name syntax on the
/// exact submitted bytes; no trimming, case folding, or other normalization
/// is performed.
#[derive(Debug, Clone, PartialEq, Eq)]
/// is performed. The original spelling is preserved for `Display` and
/// serialization, while identity (`Eq`, `Ord`, `Hash`) is case-insensitive
/// to match GitHub's treatment of owner and repository names.
#[derive(Debug, Clone)]
pub struct GitHubRepositorySlug {
owner: String,
repo: String,
@ -57,6 +64,105 @@ impl GitHubRepositorySlug {
pub fn repo(&self) -> &str {
&self.repo
}
/// Canonical credential-free HTTPS URL for this GitHub repository.
#[must_use]
pub fn https_url(&self) -> String {
format!("https://github.com/{self}")
}
/// Whether `other` names the same repository owner, ignoring ASCII case.
/// Owner and repository names are validated ASCII, so ASCII folding is
/// exact.
#[must_use]
pub fn same_owner(&self, other: &Self) -> bool {
self.owner.eq_ignore_ascii_case(&other.owner)
}
}
/// Case-folded bytes for identity comparisons without allocating; owner and
/// repository names are validated ASCII, so ASCII folding is exact.
fn folded_bytes(value: &str) -> impl Iterator<Item = u8> + '_ {
value.bytes().map(|byte| byte.to_ascii_lowercase())
}
impl PartialEq for GitHubRepositorySlug {
fn eq(&self, other: &Self) -> bool {
self.owner.eq_ignore_ascii_case(&other.owner) && self.repo.eq_ignore_ascii_case(&other.repo)
}
}
impl Eq for GitHubRepositorySlug {}
impl PartialOrd for GitHubRepositorySlug {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for GitHubRepositorySlug {
fn cmp(&self, other: &Self) -> Ordering {
folded_bytes(&self.owner)
.cmp(folded_bytes(&other.owner))
.then_with(|| folded_bytes(&self.repo).cmp(folded_bytes(&other.repo)))
}
}
impl Hash for GitHubRepositorySlug {
fn hash<H: Hasher>(&self, state: &mut H) {
for byte in folded_bytes(&self.owner) {
state.write_u8(byte);
}
// `/` cannot appear in a validated owner, so the folded
// `owner/repo` encoding stays unambiguous.
state.write_u8(b'/');
for byte in folded_bytes(&self.repo) {
state.write_u8(byte);
}
}
}
impl fmt::Display for GitHubRepositorySlug {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.owner, self.repo)
}
}
/// Parse failure for [`GitHubRepositorySlug`]. The offending input is not
/// echoed back because config surfaces already attach the value and its path.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error(
"expected a GitHub `owner/repository` slug with no scheme, host, ref, or extra path component"
)]
pub struct GitHubRepositorySlugError;
impl FromStr for GitHubRepositorySlug {
type Err = GitHubRepositorySlugError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::try_new(value).ok_or(GitHubRepositorySlugError)
}
}
impl Serialize for GitHubRepositorySlug {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for GitHubRepositorySlug {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
use serde::de::Error as _;
let value = String::deserialize(deserializer)?;
value.parse().map_err(D::Error::custom)
}
}
fn valid_github_owner(value: &str) -> bool {
@ -234,6 +340,77 @@ mod tests {
assert!(GitHubRepositorySlug::try_new(&over_repo).is_none());
}
#[test]
fn slug_identity_is_case_insensitive_but_display_preserves_case() {
let mixed: GitHubRepositorySlug = "Fabro-SH/Keystone".parse().unwrap();
let lower: GitHubRepositorySlug = "fabro-sh/keystone".parse().unwrap();
assert_eq!(mixed, lower);
assert_eq!(mixed.cmp(&lower), std::cmp::Ordering::Equal);
assert!(mixed.same_owner(&lower));
assert_eq!(mixed.to_string(), "Fabro-SH/Keystone");
let mut hashes = std::collections::HashSet::new();
hashes.insert(mixed.clone());
assert!(
!hashes.insert(lower.clone()),
"case variants share identity"
);
let mut ordered = std::collections::BTreeSet::new();
ordered.insert(mixed);
assert!(!ordered.insert(lower), "case variants share ordering");
}
#[test]
fn slug_https_url_preserves_spelling_and_has_no_credentials() {
let slug: GitHubRepositorySlug = "Fabro-SH/Keystone".parse().unwrap();
assert_eq!(slug.https_url(), "https://github.com/Fabro-SH/Keystone");
assert!(!slug.https_url().contains('@'));
}
#[test]
fn slug_ordering_sorts_by_canonical_form() {
let mut slugs: Vec<GitHubRepositorySlug> = ["owner/Zeta", "Owner/alpha", "owner/Beta"]
.iter()
.map(|value| value.parse().unwrap())
.collect();
slugs.sort();
let rendered: Vec<String> = slugs.iter().map(ToString::to_string).collect();
assert_eq!(rendered, ["Owner/alpha", "owner/Beta", "owner/Zeta"]);
}
#[test]
fn slug_from_str_rejects_urls_and_hosts() {
let cases = [
"https://github.com/owner/repo",
"git@github.com:owner/repo.git",
"ssh://git@github.com/owner/repo",
"github.com/owner/repo",
"owner/repo@main",
"owner/repo#ref",
" owner/repo",
"owner/repo ",
];
for input in cases {
assert!(input.parse::<GitHubRepositorySlug>().is_err(), "{input}");
}
}
#[test]
fn slug_serde_round_trips_as_a_string() {
let slug: GitHubRepositorySlug = "Fabro-SH/Keystone".parse().unwrap();
let json = serde_json::to_string(&slug).unwrap();
assert_eq!(json, "\"Fabro-SH/Keystone\"");
let parsed: GitHubRepositorySlug = serde_json::from_str(&json).unwrap();
assert_eq!(parsed, slug);
let err = serde_json::from_str::<GitHubRepositorySlug>("\"not a slug\"").unwrap_err();
assert!(err.to_string().contains("owner/repository"), "{err}");
}
#[test]
fn valid_ref_selectors_are_accepted() {
let max = "a".repeat(255);

View file

@ -6,7 +6,7 @@
//! notifications, interviews, agent knobs, hooks, SCM targeting, pull-request
//! behavior, and artifact collection.
use std::collections::{BTreeMap, HashMap};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::PathBuf;
use std::time::Duration as StdDuration;
@ -603,9 +603,19 @@ pub struct RunIntegrationsSettings {
/// presence-vs-clear distinction is only meaningful at the layer-merge
/// stage; the resolved form collapses both `None` and `Some({})` into an
/// empty map.
///
/// `additional_repositories` lists repositories, beyond the implicit run
/// origin, that the minted `GITHUB_TOKEN` must cover. Configuration
/// resolution guarantees a non-empty set comes with a non-empty permission
/// map that includes `contents`; runs persisted before the field existed
/// deserialize to an empty set via the serde default.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RunIntegrationsGithubSettings {
pub permissions: HashMap<String, InterpString>,
pub permissions: HashMap<String, InterpString>,
/// Omitted when empty so settings serialized by this release stay
/// byte-identical to earlier releases for single-repository runs.
#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
pub additional_repositories: BTreeSet<crate::GitHubRepositorySlug>,
}
impl RunIntegrationsGithubSettings {
@ -616,17 +626,68 @@ impl RunIntegrationsGithubSettings {
!self.permissions.is_empty()
}
/// Whether the run declares additional repositories beyond the origin.
pub fn has_additional_repositories(&self) -> bool {
!self.additional_repositories.is_empty()
}
/// Whether a resolved `contents` permission level lets the token reach
/// repository contents — the level a declared `additional_repositories`
/// set requires. The one definition shared by config-time validation and
/// the runtime re-check after interpolation, so the accepted levels
/// cannot drift between the two layers.
#[must_use]
pub fn contents_permission_allows_repository_access(value: &str) -> bool {
value == "read" || value == "write"
}
/// Resolve every `permissions` value. `{{ vars.* }}` is substituted
/// server-side at run creation, so values are literal by this point; a
/// still-unresolved token fails closed rather than reaching the GitHub API
/// as literal text.
pub fn resolve_permissions(&self) -> Result<HashMap<String, String>, ResolveError> {
fn resolve_permissions(&self) -> Result<HashMap<String, String>, ResolveError> {
let mut ctx = ResolveCtx::new();
self.permissions
.iter()
.map(|(name, value)| Ok((name.clone(), value.resolve_with(&mut ctx)?)))
.collect()
}
/// Resolve the whole runtime integration request: interpolated
/// permissions plus the declared additional repositories, produced
/// together so consumers cannot pick up one without the other.
pub fn resolve_integration(&self) -> Result<ResolvedGithubIntegration, ResolveError> {
Ok(ResolvedGithubIntegration {
permissions: self.resolve_permissions()?,
additional_repositories: self.additional_repositories.clone(),
})
}
}
/// The resolved runtime GitHub integration request for one run: interpolated
/// permission values plus the declared additional repositories.
///
/// This is the single value carried from run materialization into workflow
/// startup, replacing parallel permission/repository collections that could
/// drift apart.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ResolvedGithubIntegration {
pub permissions: HashMap<String, String>,
pub additional_repositories: BTreeSet<crate::GitHubRepositorySlug>,
}
impl ResolvedGithubIntegration {
/// Mirrors [`RunIntegrationsGithubSettings::is_token_requested`] for the
/// resolved form.
#[must_use]
pub fn is_token_requested(&self) -> bool {
!self.permissions.is_empty()
}
#[must_use]
pub fn has_additional_repositories(&self) -> bool {
!self.additional_repositories.is_empty()
}
}
#[cfg(test)]
@ -635,10 +696,11 @@ mod run_integrations_github_tests {
fn settings(permissions: &[(&str, &str)]) -> RunIntegrationsGithubSettings {
RunIntegrationsGithubSettings {
permissions: permissions
permissions: permissions
.iter()
.map(|(k, v)| ((*k).to_string(), InterpString::parse(v)))
.collect(),
additional_repositories: std::collections::BTreeSet::new(),
}
}
@ -670,6 +732,53 @@ mod run_integrations_github_tests {
fn resolve_permissions_is_empty_for_empty_settings() {
assert!(settings(&[]).resolve_permissions().unwrap().is_empty());
}
#[test]
fn settings_without_additional_repositories_field_deserialize_to_empty_set() {
// Persisted run.created events from releases before
// `additional_repositories` existed omit the field entirely.
let parsed: RunIntegrationsGithubSettings = serde_json::from_value(serde_json::json!({
"permissions": { "contents": "read" }
}))
.expect("legacy settings should deserialize");
assert!(parsed.additional_repositories.is_empty());
assert!(!parsed.has_additional_repositories());
}
#[test]
fn resolve_integration_carries_permissions_and_repositories_together() {
let mut s = settings(&[("contents", "read")]);
s.additional_repositories
.insert("fabro-sh/keystone".parse().unwrap());
let resolved = s.resolve_integration().unwrap();
assert!(resolved.is_token_requested());
assert!(resolved.has_additional_repositories());
assert_eq!(
resolved.permissions.get("contents"),
Some(&"read".to_string())
);
assert_eq!(
resolved
.additional_repositories
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>(),
vec!["fabro-sh/keystone"]
);
}
#[test]
fn resolve_integration_fails_on_an_unresolved_permission_token() {
let mut s = settings(&[("contents", "{{ env.GH_PERM_LEVEL }}")]);
s.additional_repositories
.insert("fabro-sh/keystone".parse().unwrap());
let err = s.resolve_integration().unwrap_err();
assert_eq!(err.namespace, Namespace::Env);
}
}
/// The resolved source of a run goal.
@ -961,14 +1070,32 @@ impl Default for RunCheckpointSettings {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunCloneSettings {
pub enabled: bool,
#[serde(default = "default_clone_depth")]
pub depth: i32,
}
impl RunCloneSettings {
pub const DEFAULT_DEPTH: i32 = 100;
/// Git history depth to fetch, or `None` to fetch full history.
pub fn depth_limit(&self) -> Option<i32> {
(self.depth > 0).then_some(self.depth)
}
}
impl Default for RunCloneSettings {
fn default() -> Self {
Self { enabled: true }
Self {
enabled: true,
depth: Self::DEFAULT_DEPTH,
}
}
}
fn default_clone_depth() -> i32 {
RunCloneSettings::DEFAULT_DEPTH
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunBranchSettings {
pub enabled: bool,

View file

@ -16,4 +16,8 @@
export interface RunCloneSettings {
'enabled': boolean;
/**
* Git history depth. Set to 0 to clone full history.
*/
'depth'?: number;
}

View file

@ -16,4 +16,8 @@
export interface RunIntegrationsGithubSettings {
'permissions': { [key: string]: string; };
/**
* Additional GitHub repositories, beyond the implicit run origin, that the minted GITHUB_TOKEN must cover. Each entry is a full `owner/repository` slug; every repository must share one owner with the run origin. Omitted when empty; settings persisted before this field existed deserialize to an empty set.
*/
'additional_repositories'?: Array<string>;
}

View file

@ -39,7 +39,7 @@ export interface RunSpec {
'graph_source'?: string | null;
'workflow_slug'?: string | null;
/**
* SHA-256 identity of validated canonical workflow-version bytes.
* SHA-256 identity of validated canonical workflow-version bytes. Hex input is case-insensitive; Fabro emits the canonical lowercase form.
*/
'workflow_version_id'?: string | null;
'automation'?: AutomationRef | null;