Files
buzz/desktop/src-tauri/src/commands/project_git_merge_error.rs
Tyler 788b3c002b fix(git): channel binding tooling + author remediation for unbound repos (#3626)
Closes #3527.

Repos announced via vanilla NIP-34 (kind:30617 without a `buzz-channel`
tag) 404 forever: the SEC-005 read gate requires a channel-membership
ACL, and nothing tells the author why or how to fix it. Per the ruling
in the originating thread, this ships **bind/rebind tooling plus a
narrow author-only remediation carve-out** — the shelved owner-circle
approach is intentionally absent.

## Relay
- **`api/git/binding.rs` (new):** shared tri-state binding resolver —
`Bound(uuid)` / `NotBound` / `Broken`. First-tag, fail-closed: a
malformed `buzz-channel` tag is `Broken`, never conflated with "no tag".
Both gates use it.
- **Read gate (`transport.rs`):** a **never-bound** repo read by **its
own announcement author** still returns 404 (status byte-identical to
the generic denial) but the body carries remediation: `run: buzz repos
bind --id <repo> --channel <channel-uuid> — …`. This leaks nothing — the
author announced the repo, and only the author can rebind (30617 is
keyed by `(author, d)`). `Broken` bindings stay generic-denial for
everyone, including the author (revocation shape).
Bound-to-nonexistent-channel stays generic (phase 1; ingest validation
is phase 2).
- **Push gate (`policy.rs`):** unbound denial now returns
`GIT_NO_CHANNEL_BINDING_BODY`. A deploy-skew test pins that the body
carries both the new token (`no_channel_binding`) and the legacy phrase
(`"no channel binding"`) so already-shipped desktops keep matching.
**(Review r1, blocker 2)** `Broken` no longer collapses into "unbound":
it denies 403 `invalid channel binding` for *everyone — including the
announcement owner —* **before** the owner short-circuit, matching the
read gate's fail-closed posture. The remediation token stays
NotBound-only.
- **`ingest.rs`:** side-effect failure `warn!` → `error!` — prod runs
`RUST_LOG=error`, so these failures were invisible during triage.

## Contract
- **`buzz-core/git_perms.rs`:** `GIT_NO_CHANNEL_BINDING_TOKEN` /
`GIT_NO_CHANNEL_BINDING_BODY` consts as the declared cross-component
contract; relay tests and desktop matcher both build on them.

## CLI
- **`buzz repos bind --id <repo> --channel <uuid>`** — rebinds an
existing announcement, preserving other tags.
- **(Review r1, blocker 1)** **`--channel` on `buzz repos create`** —
optional; injects exactly one shape-validated `buzz-channel` tag at
creation via a pure `build_create_announcement` builder, so the primary
create command stops producing repos the relay 404s. UUID
existence/membership stays the relay's authority at git-access time
(same TOCTOU posture as `repos bind`). Overlaps with #3594 (open, head
6bbe38459) — happy to reconcile whichever lands first; this branch also
carries the bind path and tag preservation.

## Desktop
- **Rust:** new `commands/project_git_merge_error.rs` (extracted from
`project_git_workflow.rs` to respect the 1000-line ratchet); maps the
token to a structured `no_channel_binding` error carrying the bind
command.
- **TS:** new `features/projects/lib/projectBranchErrors.ts` + tests —
dual matcher (new token AND legacy spaced phrase);
`ProjectBranchDialogs.tsx` uses it.

## Tests / verification (at head f914c7066, base 581baa625)
- Workspace `cargo test` green; `clippy -D warnings` clean; desktop Rust
1859 pass; TS 3780 pass; tsc/biome/file-size checks pass. Pre-push hooks
re-ran all suites at the pushed head.
- Postgres-gated `sec005_read_gate_tests`: all 6 pass, including
`read_gate_gives_author_of_unbound_repo_remediation_body` — asserts 404
status, `text/plain` content-type, and exact body bytes, distinguishing
remediation from generic denial (a blind `is_err()` can't).
- **New (review r1):** `buzz-cli` emitted-event tests —
`create_with_channel_emits_exactly_one_binding_tag`,
`create_without_channel_emits_no_binding_tag`,
`create_rejects_malformed_channel_uuid` (266/266 pass). Postgres-gated
`push_gate_denies_owner_through_broken_binding` — owner +
malformed-first/valid-second binding → 403 generic body without the
remediation token; never-bound control stays 200, pinning the denial to
`Broken` specifically.
- e2e git tests now bind announcements to a real channel via a
`create_test_channel` helper.

---------

Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
2026-07-29 20:07:55 -04:00

153 lines
5.4 KiB
Rust

//! Structured pull-request merge failures returned across the Tauri boundary.
use serde::Serialize;
/// Machine-readable recovery metadata for a failed pull-request merge.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProjectPullRequestMergeRecovery {
action: String,
target_branch: String,
source_branch: String,
}
/// Structured pull-request merge failure returned across the Tauri boundary.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProjectPullRequestMergeError {
code: String,
message: String,
recovery: Option<ProjectPullRequestMergeRecovery>,
}
impl ProjectPullRequestMergeError {
pub(crate) fn new(code: &str, message: impl Into<String>) -> Self {
Self {
code: code.to_string(),
message: message.into(),
recovery: None,
}
}
fn conflict(target_branch: String, source_branch: String) -> Self {
Self {
code: "merge_conflict".to_string(),
message: "Pull request has merge conflicts.".to_string(),
recovery: Some(ProjectPullRequestMergeRecovery {
action: "open_terminal".to_string(),
target_branch,
source_branch,
}),
}
}
}
impl From<String> for ProjectPullRequestMergeError {
fn from(message: String) -> Self {
// Relay push-policy denial for a repo with no `buzz-channel` binding.
// The stable token is declared in `buzz-core::git_perms`
// (GIT_NO_CHANNEL_BINDING_TOKEN); the relay guarantees the denial body
// starts with it. Push failures reach this conversion as raw
// stderr/`remote:` text, so match the token anywhere in the message.
if message.contains(buzz_core_pkg::git_perms::GIT_NO_CHANNEL_BINDING_TOKEN) {
return Self::new(
buzz_core_pkg::git_perms::GIT_NO_CHANNEL_BINDING_TOKEN,
"This repository is not bound to a channel, so the relay cannot \
authorize pushes. Bind it with: buzz repos bind --id <repo> \
--channel <channel-uuid>",
);
}
Self::new("merge_failed", message)
}
}
pub(crate) fn classify_merge_error(
message: String,
has_conflicts: bool,
target_branch: &str,
source_branch: &str,
) -> ProjectPullRequestMergeError {
if has_conflicts {
ProjectPullRequestMergeError::conflict(target_branch.to_string(), source_branch.to_string())
} else {
ProjectPullRequestMergeError::new(
"merge_failed",
format!("Pull request merge failed: {message}"),
)
}
}
#[cfg(test)]
mod tests {
use super::{classify_merge_error, ProjectPullRequestMergeError};
#[test]
fn merge_conflict_error_has_stable_recovery_metadata() {
let error =
ProjectPullRequestMergeError::conflict("main".to_string(), "feature/demo".to_string());
assert_eq!(error.code, "merge_conflict");
assert_eq!(error.message, "Pull request has merge conflicts.");
let recovery = error.recovery.expect("conflict recovery");
assert_eq!(recovery.action, "open_terminal");
assert_eq!(recovery.target_branch, "main");
assert_eq!(recovery.source_branch, "feature/demo");
}
#[test]
fn merge_conflict_error_serializes_for_tauri_clients() {
let error =
ProjectPullRequestMergeError::conflict("main".to_string(), "feature/demo".to_string());
let value = serde_json::to_value(error).expect("serialize merge conflict");
assert_eq!(value["code"], "merge_conflict");
assert_eq!(value["recovery"]["targetBranch"], "main");
assert_eq!(value["recovery"]["sourceBranch"], "feature/demo");
}
#[test]
fn merge_error_classification_only_recovers_conflicts() {
let conflict = classify_merge_error(
"CONFLICT (content): Merge conflict in src/main.rs".to_string(),
true,
"main",
"feature/demo",
);
assert_eq!(conflict.code, "merge_conflict");
assert!(conflict.recovery.is_some());
let other = classify_merge_error(
"fatal: refusing to merge unrelated histories".to_string(),
false,
"main",
"feature/demo",
);
assert_eq!(other.code, "merge_failed");
assert!(other.recovery.is_none());
}
#[test]
fn no_channel_binding_denial_converts_to_structured_code() {
// The relay's push-policy denial arrives as raw git stderr with
// `remote:` framing; the stable token must be recognized wherever it
// sits in the message.
let remote_stderr = format!(
"remote: {}\nerror: failed to push some refs",
buzz_core_pkg::git_perms::GIT_NO_CHANNEL_BINDING_BODY
);
let error = ProjectPullRequestMergeError::from(remote_stderr);
assert_eq!(
error.code,
buzz_core_pkg::git_perms::GIT_NO_CHANNEL_BINDING_TOKEN
);
assert!(error.message.contains("buzz repos bind"));
assert!(error.recovery.is_none());
// Unrelated push failures keep the generic code and original text.
let generic = ProjectPullRequestMergeError::from("connection reset".to_string());
assert_eq!(generic.code, "merge_failed");
assert_eq!(generic.message, "connection reset");
}
}