gunnar_server/transport.rs
1//! **The join.** `gunnar-ssh`'s [`GitTransport`] seam, implemented over
2//! `gunnar-wire`'s state machines, with `gunnar-wire`'s [`PushGate`] seam
3//! implemented over `gunnar-policy`'s engine.
4//!
5//! Until this file existed, those three crates had each been proven alone:
6//! `gunnar-wire` against stock `git` over pipes, `gunnar-ssh` against a
7//! `BannerTransport`, `gunnar-policy` against crafted push fixtures. Nothing
8//! had joined them, so nothing had proved a real `git clone` over a real SSH
9//! connection. `tests/ssh_end_to_end.rs` does.
10//!
11//! # Async on one side, blocking on the other
12//!
13//! russh hands over a `tokio` duplex stream; `gunnar-wire` wants
14//! `&mut dyn BufRead` and `&mut dyn Write`, deliberately, so it can be driven
15//! by a pipe or a subprocess in tests (the git wire protocol). The stream is therefore
16//! [`tokio::io::split`] into halves — one object cannot be borrowed mutably as
17//! both reader and writer — each wrapped in a [`SyncIoBridge`], and the whole
18//! transfer runs inside [`tokio::task::spawn_blocking`]. `SyncIoBridge` blocks
19//! on the runtime, so running it on a runtime worker would deadlock the reactor
20//! that has to feed it.
21//!
22//! # The protocol version is the client's to choose
23//!
24//! A v2 client announces itself by sending `GIT_PROTOCOL=version=2` as an SSH
25//! **environment request**. `gunnar-ssh` accepts that one variable and no
26//! other (`gunnar_ssh::server::ENV_ALLOWLIST`), and carries the raw value
27//! on [`ServiceRequest::git_protocol`]. [`protocol_version`] turns it
28//! into a [`ProtocolVersion`] with `gunnar-wire`'s own parser, so there is one
29//! implementation of the grammar rather than two.
30//!
31//! A client that sends nothing gets **v0**, which is what `ProtocolVersion`'s
32//! default already means and what `gunnar-wire`'s v0 fallback exists for. That
33//! path is not vestigial: it is what an old client, or one behind a transport
34//! that drops the variable, actually gets, and `tests/ssh_end_to_end.rs`
35//! exercises it explicitly rather than assuming it.
36//!
37//! **What this unblocked, measured 2026-08-03.** While every session was v0,
38//! `gunnar clone` could not clone from `gunnar serve` at all:
39//!
40//! ```text
41//! gunnar clone ssh://git@127.0.0.1:2299/team/project.git
42//! → Could not decode server reply: Currently we require feature
43//! "multi_ack_detailed", which is not supported by the server
44//! ```
45//!
46//! `gunnar-client` fetches through `nornir-git` → `gix`'s blocking transport,
47//! which demands `multi_ack_detailed` in v0. `gunnar_wire::caps` used not to
48//! advertise it, on the grounds that gunnar negotiated in single-ACK mode and
49//! advertising more than is implemented is a bug. **It advertises and
50//! implements it now** (smart HTTP): single-ACK cannot complete a stateless-rpc
51//! negotiation, so smart HTTP forced the mode to be built properly rather than
52//! declined. Delivering `GIT_PROTOCOL` still matters — v2 is the better
53//! protocol and `gix` asks for it by itself — but the v0 fallback is no longer
54//! a capability gunnar refuses `gix` on paper. That fallback is not exercised
55//! here, so treat this paragraph as a statement about the advertisement, which
56//! `gunnar-wire/tests/advertisement.rs` asserts, and not as a measurement of
57//! `gix`'s v0 client.
58//!
59//! `receive-pack` is **not** given the negotiated version, and that is not an
60//! oversight: protocol v2 has no push side. `git` sends `GIT_PROTOCOL` on a
61//! push too and then speaks v0 anyway, so passing it on would be inventing a
62//! protocol neither end implements.
63//!
64//! # This module is the event-stream producer, and the attestation one
65//!
66//! `Events.Watch` had a bus, a filter, a gap marker and its own tests. For
67//! `PushAttestation` it had no producer at all. A stream with nothing behind it
68//! connects, delivers nothing and is indistinguishable from a quiet server.
69//! [`serve_receive_pack`] is where a push becomes events:
70//!
71//! ```text
72//! refs applied ── status reported ──┬── RefUpdated / RefDeleted / PushRejected
73//! └── PushAttestation ──┬── the event bus
74//! └── attestations.jsonl
75//! ```
76//!
77//! Everything is published **after** the report is on the wire. A push must
78//! never fail because nobody was watching, and it must never be *reported* as
79//! having done something the client has not yet been told about.
80//!
81//! ## Why there is no publish callback in `gunnar-wire`
82//!
83//! There does not need to be one. `ReceivePack::serve` already returns a
84//! [`PushOutcome`] carrying every command, its status and the indexed pack,
85//! which is the input an event needs. A `Publisher` trait threaded
86//! through `gunnar-wire` would be a second path to the same facts, live at a
87//! point where the report has *not* yet been written, and a second thing to
88//! keep in step (one implementation). The coupling `gunnar-wire` must not grow is a
89//! dependency on `gunnar-grpc`, and returning a value cannot create one.
90//!
91//! ## The dependency edge push attestation forbids
92//!
93//! gunnar does not link the Build Thing, nornir, skade, arrow or parquet for
94//! any of this. The event is a proto message and the log is a file of JSON
95//! lines; anything wanting a warehouse row subscribes and writes it itself.
96
97use std::collections::HashMap;
98use std::future::Future;
99use std::io::{BufRead, BufReader, BufWriter, Write as _};
100use std::path::{Path, PathBuf};
101use std::pin::Pin;
102use std::sync::atomic::{AtomicU64, Ordering};
103use std::sync::{Arc, Mutex};
104
105use gix_hash::ObjectId;
106use gunnar_core::observe::{ServerMetrics, TARGET_GIT, TARGET_POLICY, TARGET_STORE};
107use gunnar_grpc::EventBus;
108use gunnar_policy::{
109 KeyDirectory, PolicyConfig, PolicyEngine, PushContext, RefUpdate, RepoId, StaticKeyDirectory,
110};
111use gunnar_proto::{event, PushAttestation, PushRejected, RefDeleted, RefUpdated, RepoPath};
112use gunnar_ssh::seam::{Delivery, DuplexStream, GitTransport, ServiceRequest, TransportError};
113use gunnar_ssh::GitService;
114use gunnar_store::git_storage_trait::GitServe;
115use gunnar_wire::{
116 PackReceipt, ProtocolVersion, PushFacts, PushGate, PushOutcome, ReceivePack, RefCommand,
117 RefStatus, Session, UploadPack,
118};
119use tokio_util::io::SyncIoBridge;
120
121use crate::attest::AttestationLog;
122use crate::storage::OpenRepo;
123use crate::stores::ServerStores;
124
125/// The per-repository policy document, read from the repository's git dir.
126///
127/// `gunnar-core` has no storage for branch protection or ref-name policy (see
128/// the report in the crate docs), so the gate configuration comes from a file
129/// beside the repository until it does. When the catalog grows a policy
130/// document, this constant is the only thing that changes.
131pub const POLICY_FILE: &str = "gunnar-policy.json";
132
133/// Which protocol version to answer a request in.
134///
135/// The client chooses, by sending `GIT_PROTOCOL` as an SSH environment request;
136/// `gunnar-ssh` allowlists that one name and carries the raw value here. The
137/// grammar is parsed **once**, by `gunnar-wire`, so a v1 announcement cannot
138/// mean one thing to the SSH layer and another to the wire (one implementation).
139///
140/// A request that carries nothing is v0, and not by a default that happens to
141/// land on v0: that is what `from_git_protocol(None)` says.
142fn protocol_version(request: &ServiceRequest) -> ProtocolVersion {
143 ProtocolVersion::from_git_protocol(request.git_protocol.as_deref())
144}
145
146/// **The push's standing in its namespace's byte ledger, as the receive loop
147/// asks about it.**
148///
149/// Four lines, and they are the whole of the third seam this file holds. The
150/// other two are [`protocol_version`] and [`wire_session`], and this one exists
151/// for the same reason they do: `gunnar-ssh` and `gunnar-wire` deliberately do
152/// not depend on each other, and this crate is the only one that holds both.
153///
154/// The bug it closes is a time-of-check/time-of-use on a quota. Every push was
155/// handed `PlanGate::pack_ceiling`'s number — the namespace's remaining room
156/// plus slack — and held to the whole of it, so N pushes that arrived together
157/// were each admitted against the same measurement and the room was spent N
158/// times. `gunnar_lfs::Mark` makes the number a budget the namespace shares:
159/// the receive loop charges every chunk before it takes it, against everything
160/// admitted since this push measured.
161///
162/// # What happened to the slack
163///
164/// Nothing, and that is deliberate. [`crate::entitlement`] still computes
165/// `remaining + max(cap / 16, 64 MiB)` and this file still passes that number
166/// through untouched; the ledger adds no arithmetic of its own and never
167/// re-adds the slack per connection. What changed is only WHO spends it: the
168/// 6.25 % (floor 64 MiB) that covers the difference between a thin pack's wire
169/// bytes and its stored bytes is now one allowance for the namespace, charged
170/// once, instead of one allowance per simultaneous push. That is the direction
171/// the slack was argued for in the first place — it exists so the push that is
172/// landing is not cut by arithmetic nobody can do in advance, not so that eight
173/// pushes may each have one.
174///
175/// # Why an adapter rather than the trait on `Mark` itself
176///
177/// Because `gunnar-lfs` would then have to name `gunnar_wire::PackLedger`, and
178/// `gunnar-lfs` is a leaf (44 packages) that `gunnar-lfs-serve` ships on its
179/// own; `gunnar-wire` cannot name `gunnar_lfs::Mark` either, being bound by the
180/// no-OS rule and the dependency budget to plumbing. An `Arc<dyn PackLedger>`
181/// built here costs one allocation per push, adds no edge to either crate's
182/// graph, and puts the conversion where every other conversion between those two
183/// already is.
184#[derive(Debug)]
185pub(crate) struct PackMark {
186 pub(crate) mark: gunnar_lfs::Mark,
187}
188
189impl gunnar_wire::PackLedger for PackMark {
190 /// Pure forwarding, on purpose: `room` is the receive loop's number (the
191 /// ceiling plus the one byte that makes it reachable) and the arithmetic is
192 /// the ledger's. Anything computed here would be a third place where a byte
193 /// ceiling is decided.
194 fn admit_upto(&self, want: u64, room: u64) -> u64 {
195 self.mark.admit_upto(want, room)
196 }
197}
198
199/// How much of a conversation this call carries, as `gunnar-wire` names it.
200///
201/// [`Delivery`] is the seam's word and [`Session`] is the wire's, and they are
202/// two enums rather than one because `gunnar-ssh` deliberately does not depend
203/// on `gunnar-wire` (see `ServiceRequest::git_protocol`). This is the whole of
204/// the translation, in one place, so the mapping cannot be made differently in
205/// two.
206///
207/// [`Delivery::Advertisement`] has no `Session` at all: it is not a session,
208/// it is a `GET` that writes the advertisement and reads nothing, and it is
209/// handled before this is consulted.
210fn wire_session(request: &ServiceRequest) -> Session {
211 match request.delivery {
212 Delivery::Session | Delivery::Advertisement => Session::Connected,
213 Delivery::StatelessRpc => Session::StatelessRpc,
214 }
215}
216
217/// **What `gunnar-wire` is configured with, for both directions.**
218///
219/// `gunnar-wire` has had these knobs since it was written, and a
220/// `with_config` on both state machines to receive them. Nothing ever called
221/// either: this module said `UploadPack::new(…)` and `ReceivePack::new(…)`, so
222/// every deployment ran on `Config::default()` and **an operator could not
223/// decline `filter`** — the one capability the type's own doc comment says is
224/// "the one capability an operator plausibly wants to decline".
225///
226/// A configuration surface that cannot affect behaviour is worse than none.
227/// It reads as a control that works: an appliance that had to state it does
228/// not serve partial clones had `Offered { filter: false }` sitting right
229/// there, fully implemented in both protocol versions, and no way at all to
230/// reach it. So the knobs are wired rather than deleted, and this struct is
231/// the one place the two `gunnar_wire::…::Config` types are held (one implementation) — a
232/// second copy of `agent` or `budget` on `ServeConfig` is how a value ends up
233/// honoured on one direction and stubbed on the other.
234#[derive(Debug, Clone)]
235pub struct WireConfig {
236 /// What `git-upload-pack` serves under: `offered`, `want_policy`, `budget`
237 /// and `limits`.
238 pub upload: gunnar_wire::upload_pack::Config,
239 /// What `git-receive-pack` serves under: the connectivity budget.
240 pub receive: gunnar_wire::receive_pack::Config,
241 /// What `git-lfs-transfer` serves under.
242 ///
243 /// Held here and not beside the store for the same reason the two above
244 /// are: the LFS session is a third service on this one listener, and a
245 /// second home for its quota is how 8 GiB comes to be enforced by the SSH
246 /// path and not by the next caller. `gunnar_lfs::Session::with_limits` and
247 /// the three builders behind it had **zero** call sites outside
248 /// `tests/quota.rs` — every deployment ran on `Limits::default()` and the
249 /// numbers in the doc comments were not reachable from any command line.
250 pub lfs: gunnar_lfs::Limits,
251 /// Enumerate the objects each push introduced, so the push attestation can
252 /// name them. **On by default.**
253 ///
254 /// The ids are not a by-product of storing a pack — a pack entry does not
255 /// carry its oid — so answering this waits for the engine's indexer to
256 /// drain, which is real latency on the push path
257 /// (`storage::znippy_engine::ingest` has the measured multiples). Before
258 /// this flag existed the enumeration happened only when a policy gate
259 /// needed it, and an attestation on a repository with no gates therefore
260 /// carried an empty object list that was indistinguishable from a push
261 /// that introduced nothing.
262 ///
263 /// Turning it **off** does not restore that silence: the attestation then
264 /// records the reason the set is absent (`new_object_oids_unknown`), because an
265 /// absence that renders as a value is the defect this flag was added to
266 /// remove, not the behaviour it restores.
267 pub attest_objects: bool,
268}
269
270impl Default for WireConfig {
271 /// Hand-written for one field. `#[derive(Default)]` would make
272 /// [`WireConfig::attest_objects`] `false`, and the safe default here is the
273 /// one that produces a complete attestation — the cheap default is the one
274 /// an operator opts into, never the one they get by forgetting.
275 fn default() -> Self {
276 WireConfig {
277 upload: Default::default(),
278 receive: Default::default(),
279 lfs: Default::default(),
280 attest_objects: true,
281 }
282 }
283}
284
285impl WireConfig {
286 /// The knobs an operator set on `gunnar serve`, translated once.
287 ///
288 /// `None` on every flag means "the operator did not say", which takes
289 /// `gunnar-wire`'s default — the same convention `--max-concurrent-transfers`
290 /// already uses. `0` is a *stated* value and is treated as one:
291 ///
292 /// * on the two byte ceilings it is the documented way to remove them,
293 /// because `Limits`' fields are `Option` and `None` means unbounded;
294 /// * on `--fetch-budget` it is **refused**, because the walk stops when it
295 /// reaches the budget, so `0` would mean a server that answers no fetch
296 /// at all while reading as "no limit". An unbounded-looking flag that
297 /// silently bricks the server is the failure this whole item is about.
298 pub fn from_serve_args(args: &gunnar_common::cli::ServeArgs) -> anyhow::Result<Self> {
299 let mut wire = WireConfig::default();
300 if args.no_attest_objects {
301 wire.attest_objects = false;
302 }
303 if args.no_filter {
304 wire.upload.offered.filter = false;
305 }
306 if let Some(policy) = args.want_policy {
307 wire.upload.want_policy = policy.to_wire();
308 }
309 if let Some(budget) = args.fetch_budget {
310 anyhow::ensure!(
311 budget > 0,
312 "--fetch-budget 0 would refuse every fetch rather than allow every fetch: the \
313 walk stops when it REACHES the budget. Leave the flag off for the default of \
314 {}, or give the number of objects a fetch may visit.",
315 gunnar_wire::DEFAULT_BUDGET
316 );
317 wire.upload.budget = Some(budget);
318 }
319 if let Some(bytes) = args.max_fetch_request_bytes {
320 wire.upload.limits.request_bytes = (bytes > 0).then_some(bytes);
321 }
322 if let Some(bytes) = args.max_fetch_object_bytes {
323 wire.upload.limits.object_bytes = (bytes > 0).then_some(bytes);
324 }
325 if let Some(bytes) = args.max_fetch_walk_bytes {
326 wire.upload.limits.walk_bytes = (bytes > 0).then_some(bytes);
327 }
328 // ── the ceiling that bounds the BOX, not the request ─────────────────
329 //
330 // Built here rather than defaulted in `gunnar-wire`, because a pool is
331 // only a pool if every connection gets the same `Arc`. A default inside
332 // `Config` would give each session a private purse of its own, which
333 // reads exactly like a server-wide ceiling and is not one.
334 //
335 // `for_this_machine` reads `MemTotal`. The alternative — a constant —
336 // would be a memory ceiling chosen without knowing how much memory
337 // there is, which is the mistake `DEFAULT_BUDGET` made in objects.
338 wire.upload.walk_pool = match args.max_walk_bytes_total {
339 Some(0) => None,
340 Some(bytes) => Some(gunnar_wire::WalkPool::new(bytes)),
341 None => Some(gunnar_wire::WalkPool::for_this_machine()),
342 };
343 // The LFS three are `u64`/`usize` and not `Option`, so `0` is a stated
344 // "refuse everything" rather than the "unbounded" the two flags above
345 // mean by it. Same call as `--fetch-budget`: refuse it at startup,
346 // because a flag that reads as "no limit" and bricks the service is the
347 // failure this whole item is about.
348 if let Some(bytes) = args.max_lfs_object_bytes {
349 anyhow::ensure!(
350 bytes > 0,
351 "--max-lfs-object-bytes 0 would refuse every upload, not allow every upload. \
352 Leave the flag off for the default of {} bytes.",
353 gunnar_lfs::DEFAULT_MAX_OBJECT_BYTES
354 );
355 wire.lfs = wire.lfs.max_object_bytes(bytes);
356 }
357 if let Some(objects) = args.max_lfs_batch_objects {
358 anyhow::ensure!(
359 objects > 0,
360 "--max-lfs-batch-objects 0 would refuse every batch. Leave the flag off for the \
361 default of {}.",
362 gunnar_lfs::DEFAULT_MAX_BATCH_OBJECTS
363 );
364 wire.lfs = wire.lfs.max_batch_objects(objects);
365 }
366 if let Some(bytes) = args.max_lfs_drain_bytes {
367 anyhow::ensure!(
368 bytes > 0,
369 "--max-lfs-drain-bytes 0 would drop the channel on the first refusal instead of \
370 draining to the client's flush-pkt, so the refusal itself would not be \
371 delivered. Leave the flag off for the default."
372 );
373 wire.lfs = wire.lfs.max_drain_bytes(bytes);
374 }
375 Ok(wire)
376 }
377
378 /// **The one place an LFS session is built.**
379 ///
380 /// By construction rather than by a guard that watches two call sites
381 /// agree (one implementation). The defect this closes was exactly a second construction
382 /// path: `Session::new(…).serve(…)` in the SSH transport, with
383 /// `with_limits` reachable only from `tests/quota.rs`. A test that built
384 /// its own session could be green with the server on `Limits::default()`
385 /// forever, so the test drives *this* and there is nothing else to drive.
386 pub fn lfs_session<'a>(
387 &self,
388 store: &'a dyn gunnar_lfs::LfsStore,
389 operation: gunnar_lfs::Operation,
390 ) -> gunnar_lfs::Session<'a> {
391 gunnar_lfs::Session::new(store, operation).with_limits(self.lfs)
392 }
393}
394
395/// `gunnar-wire`, wired to the SSH listener.
396pub struct WireTransport {
397 stores: Arc<ServerStores>,
398 bus: Option<Arc<EventBus>>,
399 attestations: AttestationLog,
400 metrics: Arc<ServerMetrics>,
401 wire: Arc<WireConfig>,
402}
403
404impl WireTransport {
405 /// Serve repositories out of `stores`.
406 pub fn new(stores: Arc<ServerStores>) -> Self {
407 let attestations = AttestationLog::under(stores.root());
408 WireTransport {
409 stores,
410 bus: None,
411 attestations,
412 metrics: Arc::new(ServerMetrics::new()),
413 wire: Arc::new(WireConfig::default()),
414 }
415 }
416
417 /// Serve under `wire` rather than `gunnar-wire`'s defaults.
418 ///
419 /// The whole of what makes [`WireConfig`] reachable. Without a call to
420 /// this, every knob on it is a comment.
421 pub fn with_wire_config(mut self, wire: WireConfig) -> Self {
422 self.wire = Arc::new(wire);
423 self
424 }
425
426 /// Count into the **server's** counters rather than into a private set.
427 ///
428 /// `gunnar serve` mints one [`ServerMetrics`] and hands the same `Arc` here
429 /// and to the SSH listener, so `connections_refused` and `pushes_rejected`
430 /// are two readings of one server. A transport built without this still
431 /// counts, into a set only it can see, which is what a unit test wants.
432 pub fn with_metrics(mut self, metrics: Arc<ServerMetrics>) -> Self {
433 self.metrics = metrics;
434 self
435 }
436
437 /// Publish `RefUpdated` / `RefDeleted` / `PushRejected` / `PushAttestation`
438 /// to the event bus.
439 ///
440 /// **The bus is optional; the attestation log is not.** A server started
441 /// with `--no-grpc` has nobody to notify, but the attestation's durable record is not a
442 /// notification. It is the thing the event stream explicitly does not
443 /// promise to keep (see `EventBus::publish`), so the log is written either
444 /// way and only the publish is conditional.
445 pub fn with_bus(mut self, bus: Arc<EventBus>) -> Self {
446 self.bus = Some(bus);
447 self
448 }
449
450 /// Where the attestation's JSON lines land.
451 pub fn attestation_log(&self) -> &AttestationLog {
452 &self.attestations
453 }
454}
455
456/// **The LFS object store, opened the one way** — for the SSH session and for
457/// the HTTP batch API alike (one implementation).
458///
459/// The two surfaces used to be one call apart from having two answers to *where
460/// does this repository keep its LFS objects*: `serve_blocking` reached for
461/// `stores.repo_store(...).git_dir()` and anything else would have had to
462/// re-derive it. It is `<git_dir>/lfs/`, the symlink check in
463/// `ServerStores::repo_dir` is not optional on either path, and the handle the
464/// store lock lives on is the same handle, so a repack and an LFS transfer
465/// exclude each other the same way on both transports.
466impl gunnar_ssh::seam::LfsTransport for WireTransport {
467 fn open_lfs(
468 &self,
469 request: &ServiceRequest,
470 ) -> Result<gunnar_ssh::seam::LfsAccess, TransportError> {
471 // An upload into a repository that does not exist yet (the
472 // first-repository door, 2026-09-28): the repository is made by its
473 // first object, see `ProspectiveLfs`.
474 if request.prospective {
475 return Ok(gunnar_ssh::seam::LfsAccess {
476 store: Arc::new(ProspectiveLfs::new(
477 Arc::clone(&self.stores),
478 request.clone(),
479 )),
480 limits: self.wire.lfs,
481 allowance: request.lfs_allowance.clone(),
482 });
483 }
484 let store = open_repo_store(&self.stores, request)?;
485 let lfs = open_lfs_store(&self.stores, &store, request)?;
486 Ok(gunnar_ssh::seam::LfsAccess {
487 store: Arc::new(lfs),
488 // **`self.wire.lfs`, not `Limits::default()`.** The one place an
489 // operator's `--max-lfs-object-bytes` is held. A separate default
490 // here is exactly how a quota comes to be enforced on SSH and not
491 // on HTTP; see `WireConfig::lfs`.
492 limits: self.wire.lfs,
493 // **The tenant's plan**, decided by the gate chain in
494 // `authorise_service` and carried on the request beside the pack
495 // ceiling. Cloned rather than moved: `open_lfs` takes the request by
496 // reference, and one `Allowance` is two small fields.
497 allowance: request.lfs_allowance.clone(),
498 })
499 }
500}
501
502/// Open the repository behind an **already authorised** request, or say why not
503/// in the sentence the client is owed.
504///
505/// Shared by `serve_blocking` and [`LfsTransport::open_lfs`]. The distinction
506/// between the two failures is the load-bearing part and is why this is one
507/// function: a held store lock is a running `gunnar gc` (or a second gunnar)
508/// and clears on its own, so the answer is "retry"; every other open failure is
509/// a fault an operator has to look at. Answering both with one sentence is how
510/// a two-minute wait becomes a support ticket.
511///
512/// `TransportError::Refused` and not `Protocol`: both call sites reach this
513/// before a single byte has been written, which is what that variant *means*.
514fn open_repo_store(
515 stores: &ServerStores,
516 request: &ServiceRequest,
517) -> Result<Arc<OpenRepo>, TransportError> {
518 stores.repo_store(&request.repo).map_err(|err| {
519 // Opening the repository failed *after* the grant check said yes, so
520 // this is a storage fault and not a decision. It used to be returned
521 // silently: the client saw a dead channel and the server said nothing
522 // at all.
523 tracing::error!(
524 target: TARGET_GIT,
525 error = format!("{err:#}"),
526 "the repository could not be opened for a transfer that was already authorised"
527 );
528 TransportError::Refused(match locked_path(&err) {
529 Some(_) => format!(
530 "the repository {} is locked by maintenance on the server (a repack or a \
531 second gunnar holds its store lock); the transfer was not started, so retry",
532 request.repo.key()
533 ),
534 None => format!(
535 "the repository {} could not be opened on the server; the transfer was not \
536 started. The reason is in the server log",
537 request.repo.key()
538 ),
539 })
540 })
541}
542
543/// The LFS objects under a repository's own git dir. See
544/// [`gunnar_lfs::fs_store`].
545fn open_lfs_store(
546 stores: &ServerStores,
547 store: &OpenRepo,
548 request: &ServiceRequest,
549) -> Result<crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>, TransportError> {
550 let repo = format!("{}/{}", request.repo.namespace, request.repo.name);
551 gunnar_lfs::FsLfsStore::open_or_init(store.git_dir())
552 .map(|fs| {
553 crate::txlog_lfs::LoggedLfs::new(fs, Arc::clone(stores.txlog()), repo)
554 // Held to the data set's room, whole object at a time (cov-13).
555 .with_space(stores.space_ledger())
556 })
557 .map_err(|err| {
558 tracing::error!(
559 target: TARGET_GIT,
560 error = format!("{err}"),
561 "the lfs object store could not be opened for an authorised transfer"
562 );
563 // The same `Refused`, for the same reason, at the twin of the seam
564 // above: this is still before the first byte, and the LFS grammar has
565 // no per-object error line to carry it later.
566 TransportError::Refused(format!(
567 "the lfs object store for {} could not be opened on the server; the \
568 transfer was not started. The reason is in the server log",
569 request.repo.key()
570 ))
571 })
572}
573
574/// **An LFS upload into a repository that does not exist yet** (2026-09-28):
575/// the first-repository door for git-lfs's pre-push hook, which runs after
576/// git took receive-pack's advertisement and BEFORE it sends a command.
577/// MEASURED 2026-09-28: the first `git push` with an LFS object into a new
578/// repository died in that hook — `batch request: exec request failed on
579/// channel 0: exit status 255` — and neither the repository nor the ref was
580/// created, while a precreated repository worked.
581///
582/// `Dispatch::authorise_service` admitted the upload under the SAME
583/// push-to-create rules as the push (`Control::push_create_spec`: namespace
584/// Admin, and server Admin for a namespace that does not exist; everyone
585/// else the fused not-found). Nothing is written until an OBJECT arrives:
586/// until then every object is absent (so the batch asks for all of them),
587/// and a client that only batches, or hangs up, leaves nothing. The first
588/// `begin_write` makes the repository through [`create_prospect`] — the
589/// same writes a push-create makes, the grant asked again — or takes the one
590/// a concurrent upload or push made first. After that it is the ordinary
591/// store.
592///
593/// The repository is the push-create prospect, sha1, for the reason
594/// `push_create_spec` gives; LFS objects are sha256 by their own spec
595/// whatever the repository's hash is.
596pub(crate) struct ProspectiveLfs<S> {
597 stores: S,
598 request: ServiceRequest,
599 made: std::sync::OnceLock<(
600 Arc<OpenRepo>,
601 crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>,
602 )>,
603 making: Mutex<()>,
604}
605
606impl<S: std::ops::Deref<Target = ServerStores> + Send + Sync> ProspectiveLfs<S> {
607 pub(crate) fn new(stores: S, request: ServiceRequest) -> Self {
608 ProspectiveLfs {
609 stores,
610 request,
611 made: std::sync::OnceLock::new(),
612 making: Mutex::new(()),
613 }
614 }
615
616 fn made(&self) -> Option<&crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>> {
617 self.made.get().map(|(_, lfs)| lfs)
618 }
619
620 /// The store, made on first use.
621 fn made_or_create(
622 &self,
623 ) -> gunnar_lfs::Result<&crate::txlog_lfs::LoggedLfs<gunnar_lfs::FsLfsStore>> {
624 if let Some(lfs) = self.made() {
625 return Ok(lfs);
626 }
627 let _one = self.making.lock().unwrap_or_else(|p| p.into_inner());
628 if let Some(lfs) = self.made() {
629 return Ok(lfs);
630 }
631 let stores: &ServerStores = &self.stores;
632 let path = self.request.repo.path();
633 let (repo, store) = create_prospect(stores, &self.request).map_err(|sentence| {
634 if sentence.starts_with("repository not found") {
635 gunnar_lfs::Error::NotFound(sentence)
636 } else {
637 gunnar_lfs::Error::Store(sentence)
638 }
639 })?;
640 let mut admitted = self.request.clone();
641 admitted.repo = repo;
642 admitted.prospective = false;
643 let lfs = open_lfs_store(stores, &store, &admitted)
644 .map_err(|err| gunnar_lfs::Error::Store(err.to_string()))?;
645 tracing::info!(
646 target: TARGET_GIT,
647 event = "git.lfs_transfer.created_repo",
648 repo = %path,
649 principal = %self.request.principal.id,
650 "an LFS upload's first object made the repository (the push-create door)"
651 );
652 Ok(&self.made.get_or_init(|| (store, lfs)).1)
653 }
654}
655
656impl<S: std::ops::Deref<Target = ServerStores> + Send + Sync> gunnar_lfs::LfsStore
657 for ProspectiveLfs<S>
658{
659 fn size_of(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<Option<u64>> {
660 match self.made() {
661 Some(lfs) => lfs.size_of(oid),
662 None => Ok(None),
663 }
664 }
665 fn open(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<Option<gunnar_lfs::LfsObject>> {
666 match self.made() {
667 Some(lfs) => lfs.open(oid),
668 None => Ok(None),
669 }
670 }
671 fn begin_write(
672 &self,
673 oid: &gunnar_lfs::Oid,
674 declared_size: u64,
675 ) -> gunnar_lfs::Result<Box<dyn gunnar_lfs::PendingWrite + '_>> {
676 self.made_or_create()?.begin_write(oid, declared_size)
677 }
678 fn delete(&self, oid: &gunnar_lfs::Oid) -> gunnar_lfs::Result<bool> {
679 match self.made() {
680 Some(lfs) => lfs.delete(oid),
681 None => Ok(false),
682 }
683 }
684}
685
686impl GitTransport for WireTransport {
687 fn serve<'a>(
688 &'a self,
689 request: ServiceRequest,
690 stream: Box<dyn DuplexStream>,
691 ) -> Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>> {
692 let stores = Arc::clone(&self.stores);
693 let bus = self.bus.clone();
694 let attestations = self.attestations.clone();
695 let metrics = Arc::clone(&self.metrics);
696 let wire = Arc::clone(&self.wire);
697 Box::pin(async move {
698 // `spawn_blocking` does NOT carry the current span across (it is a
699 // thread hand-off, not a task spawn), so without this the whole
700 // transfer — every line `gunnar-wire`, the gate and this module
701 // emit — would fall out of `git.transfer` and lose the connection
702 // id, the principal and the repository. Captured here, where the
703 // future is polled inside `gunnar-ssh`'s instrumented task.
704 let parent = tracing::Span::current();
705 // One object cannot be borrowed mutably as both `BufRead` and
706 // `Write`, so the duplex is split before it is bridged.
707 let (read_half, write_half) = tokio::io::split(stream);
708 // ★ stinky row 1 (2026-10-10): the git work hears that its client
709 // is gone. `gunnar-ssh` sets the flag when the session's grace runs
710 // out and then WAITS for this future, so the fan worker is held
711 // until the blocking task below has actually ended; the guard sets
712 // it too if this future is dropped some other way, because a
713 // dropped `JoinHandle` stops nothing.
714 let cancel = request.cancel.clone();
715 let _stop_if_dropped = CancelOnDrop(cancel.clone());
716 let mut reader = Cancellable {
717 inner: SyncIoBridge::new(read_half),
718 cancel: cancel.clone(),
719 };
720 let mut writer = Cancellable {
721 inner: SyncIoBridge::new(write_half),
722 cancel,
723 };
724 // The halves are dropped here, at the end of the blocking task, and
725 // that is now safe: `gunnar-ssh` splits the russh channel instead of
726 // consuming it, so no part of this stream closes the channel when it
727 // goes. A 250 ms `linger` used to hold them open to win a race
728 // against `ChannelCloseOnDrop`; the race is gone and so is the
729 // timer.
730 tokio::task::spawn_blocking(move || {
731 // The blocking thread is this transfer's alone for the whole
732 // duration, so entering the span here cannot leak it into
733 // another task's lines.
734 let _entered = parent.enter();
735 serve_blocking(
736 &Serving {
737 stores: &stores,
738 bus: bus.as_deref(),
739 attestations: &attestations,
740 metrics: &metrics,
741 wire: &wire,
742 },
743 request,
744 &mut reader,
745 &mut writer,
746 )
747 })
748 .await
749 .map_err(|err| {
750 TransportError::Protocol(format!("the git transport task did not finish: {err}"))
751 })?
752 })
753 }
754}
755
756/// Sets the transfer's [`Cancel`](gunnar_ssh::channel::Cancel) when dropped:
757/// a transport future dropped before its blocking task ended tells the task.
758struct CancelOnDrop(gunnar_ssh::channel::Cancel);
759
760impl Drop for CancelOnDrop {
761 fn drop(&mut self) {
762 self.0.cancel();
763 }
764}
765
766/// **The git work's safe points**: every read from and write to the client.
767///
768/// Once the transfer is told to stop, the next read or write fails with
769/// `ConnectionAborted` — not `Interrupted`, which `read_exact`/`write_all`
770/// retry — and the work unwinds from there. The ref updates of a receive-pack
771/// transaction do no stream I/O between them, so a stop never lands inside
772/// one: the transaction lands whole, or (stopped while the pack was still
773/// arriving) not at all.
774struct Cancellable<T> {
775 inner: T,
776 cancel: gunnar_ssh::channel::Cancel,
777}
778
779impl<T> Cancellable<T> {
780 fn check(&self) -> std::io::Result<()> {
781 if self.cancel.is_cancelled() {
782 return Err(told_to_stop());
783 }
784 Ok(())
785 }
786}
787
788fn told_to_stop() -> std::io::Error {
789 std::io::Error::new(
790 std::io::ErrorKind::ConnectionAborted,
791 "the transfer was told to stop: its client's session ended and the transfer outlived the grace",
792 )
793}
794
795impl<T: std::io::Read> std::io::Read for Cancellable<T> {
796 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
797 self.check()?;
798 self.inner.read(buf)
799 }
800}
801
802impl<T: std::io::Write> std::io::Write for Cancellable<T> {
803 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
804 self.check()?;
805 self.inner.write(buf)
806 }
807
808 fn flush(&mut self) -> std::io::Result<()> {
809 self.check()?;
810 self.inner.flush()
811 }
812}
813
814fn wire_error(err: gunnar_wire::Error) -> TransportError {
815 TransportError::Protocol(err.to_string())
816}
817
818/// Counts the bytes that actually cross the wire.
819///
820/// **Measured, not inferred.** "Objects sent" comes out of the negotiation and
821/// says nothing about what a client paid for: a fetch that sends 12 objects can
822/// move 4 KiB or 40 MiB. `bytes_out` is the number an operator answering
823/// *"why was that clone slow"* reaches for first, and the only place it can be
824/// counted honestly is at the stream.
825///
826/// One relaxed atomic add per `read`/`write` call, and both are already
827/// buffered (`BufReader`/`BufWriter` sit on top), so this is a handful of adds
828/// per transfer rather than one per byte.
829struct Metered<T> {
830 inner: T,
831 total: Arc<AtomicU64>,
832}
833
834impl<T> Metered<T> {
835 fn new(inner: T) -> (Metered<T>, Arc<AtomicU64>) {
836 let total = Arc::new(AtomicU64::new(0));
837 (
838 Metered {
839 inner,
840 total: Arc::clone(&total),
841 },
842 total,
843 )
844 }
845}
846
847impl<T: std::io::Read> std::io::Read for Metered<T> {
848 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
849 let n = self.inner.read(buf)?;
850 self.total.fetch_add(n as u64, Ordering::Relaxed);
851 Ok(n)
852 }
853}
854
855impl<T: std::io::Write> std::io::Write for Metered<T> {
856 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
857 let n = self.inner.write(buf)?;
858 self.total.fetch_add(n as u64, Ordering::Relaxed);
859 Ok(n)
860 }
861
862 fn flush(&mut self) -> std::io::Result<()> {
863 self.inner.flush()
864 }
865}
866
867/// **THE counting point for per-namespace flow, both directions.**
868///
869/// It is a guard that records when it is DROPPED rather than a call at the end
870/// of [`serve_blocking`], and that is the whole design. `serve_blocking` has
871/// three service arms, an early return for the abandoned prospect, an early
872/// return for the advertisement, and a `?` on every fallible step; a call at
873/// "the end" would be five calls, and the sixth exit somebody adds next year
874/// would silently stop counting a whole transport. A guard cannot be
875/// forgotten, and it counts the failed transfers too — the bytes a client sent
876/// before it hung up were still received, and a tenant who is failing is
877/// exactly the tenant an operator wants to see.
878///
879/// It reads the SAME two counters [`Metered`] already keeps for
880/// `ServerMetrics`, so a byte is counted once for the box and once for the
881/// tenant off one measurement, and the two can never disagree about how much
882/// moved. Nothing here counts bytes itself.
883///
884/// The three `Cell` fields are what the arm that ran decides:
885///
886/// * `fetched` — an upload-pack SESSION, set after the advertisement's early
887/// return, so `git ls-remote` and a monitoring probe are bytes and not a
888/// customer's fetch;
889/// * `pushed` — a receive-pack session that came back `Ok`;
890/// * the LFS pair — the session's own applied figures, which are a different
891/// number from the wire bytes about the same transfer.
892struct FlowRecording<'a> {
893 flow: &'a gunnar_core::FlowLedger,
894 namespace: String,
895 principal: String,
896 bytes_in: Arc<AtomicU64>,
897 bytes_out: Arc<AtomicU64>,
898 fetched: std::cell::Cell<bool>,
899 pushed: std::cell::Cell<bool>,
900 lfs_bytes_in: std::cell::Cell<u64>,
901 lfs_bytes_out: std::cell::Cell<u64>,
902}
903
904impl Drop for FlowRecording<'_> {
905 fn drop(&mut self) {
906 self.flow.record(
907 &self.namespace,
908 gunnar_core::now_ms(),
909 &gunnar_core::Transfer {
910 bytes_in: self.bytes_in.load(Ordering::Relaxed),
911 bytes_out: self.bytes_out.load(Ordering::Relaxed),
912 pushed: self.pushed.get(),
913 fetched: self.fetched.get(),
914 lfs_bytes_in: self.lfs_bytes_in.get(),
915 lfs_bytes_out: self.lfs_bytes_out.get(),
916 principal: self.principal.clone(),
917 },
918 );
919 }
920}
921
922/// The lock file, when `err` is a store whose exclusive lock is already held.
923///
924/// `gunnar gc` opens the repository the same way a transfer does, and the
925/// `O_EXCL` lock the Arrow log takes is what makes "one writer" true across
926/// processes — so a repack and a transfer on the same repository are mutually
927/// exclusive **by that lock and nothing else**. Whichever loses gets
928/// [`gunnar_store::Error::StoreLocked`] out of `open_or_init`, wrapped in
929/// `anyhow` context by `ServerStores::repo_store`, which is why this walks the
930/// chain rather than matching the top-level error.
931///
932/// Recognising it is not cosmetic: "retry in a moment" and "an operator has to
933/// look at this" are opposite instructions, and the caller cannot tell them
934/// apart from a formatted string without matching on prose.
935fn locked_path(err: &anyhow::Error) -> Option<&std::path::Path> {
936 match err.downcast_ref::<gunnar_store::Error>() {
937 Some(gunnar_store::Error::StoreLocked { path }) => Some(path.as_path()),
938 _ => None,
939 }
940}
941
942/// Everything a transfer needs that belongs to the **server** rather than to
943/// the request.
944///
945/// One borrow instead of five. `serve_blocking` grew an eighth argument when
946/// `WireConfig` was wired through and clippy's `too_many_arguments` caught it,
947/// which is the right moment to notice that five of those arguments are the
948/// same five fields `WireTransport` holds and are passed together everywhere
949/// they are passed at all. Bundling them is what the lint is for; an
950/// `#[allow]` would have kept the shape that made a sixth easy to add.
951struct Serving<'a> {
952 stores: &'a ServerStores,
953 bus: Option<&'a EventBus>,
954 attestations: &'a AttestationLog,
955 metrics: &'a Arc<ServerMetrics>,
956 wire: &'a WireConfig,
957}
958
959fn serve_blocking(
960 serving: &Serving<'_>,
961 request: ServiceRequest,
962 reader: &mut dyn std::io::Read,
963 writer: &mut dyn std::io::Write,
964) -> Result<(), TransportError> {
965 // `bus` and `attestations` are not destructured here: they are used only by
966 // `serve_receive_pack`, which takes the whole `Serving`.
967 let Serving {
968 stores,
969 metrics,
970 wire,
971 ..
972 } = *serving;
973 // `Refused`, not `Protocol`: this is the one point in `serve_blocking` at
974 // which **nothing has been written** — `Metered` is constructed on the next
975 // lines and the state machine has not been entered — so the client can still
976 // be handed a pkt-line, and `TransportError::Refused` is exactly the claim
977 // "no byte has gone out yet". `open_repo_store` is shared with the HTTP LFS
978 // seam so the two transports refuse a locked repository in one sentence and
979 // not two (one implementation).
980 //
981 // A PROSPECTIVE request — the first-repository door — has no store to
982 // open and must not get one here: `repo_store` would create the directory
983 // for a push the client may abandon at the advertisement. That path goes
984 // through `admit_prospective_push` below, once the buffers exist.
985 if request.cancel.is_cancelled() {
986 return Err(TransportError::Io(told_to_stop()));
987 }
988 let store = if request.prospective {
989 None
990 } else {
991 Some(open_repo_store(stores, &request)?)
992 };
993 let (metered_in, bytes_in) = Metered::new(reader);
994 let (metered_out, bytes_out) = Metered::new(writer);
995 // **The per-namespace flow counting point.** Created here, beside the two
996 // counters it reads, and recorded when it is dropped — on every exit from
997 // this function, including the early returns and the failures. See
998 // `FlowRecording`. It is created BEFORE the first-repository door below, so
999 // that a client which took an advertisement of a repository it never
1000 // created still has its bytes charged to the namespace it asked about.
1001 let flow = FlowRecording {
1002 flow: stores.flow().as_ref(),
1003 namespace: request.repo.namespace.clone(),
1004 principal: request
1005 .key_fingerprint
1006 .as_ref()
1007 .map(|f| f.to_string())
1008 .unwrap_or_default(),
1009 bytes_in: Arc::clone(&bytes_in),
1010 bytes_out: Arc::clone(&bytes_out),
1011 fetched: std::cell::Cell::new(false),
1012 pushed: std::cell::Cell::new(false),
1013 lfs_bytes_in: std::cell::Cell::new(0),
1014 lfs_bytes_out: std::cell::Cell::new(0),
1015 };
1016 // **`BufWriter::new` is 8 KiB, and a sideband packet is 65 520.** So the
1017 // default capacity did not buffer the pack at all: every packet flushed the
1018 // 5-byte header as a write of its own and then passed 65 515 bytes straight
1019 // through. Two writes per packet, one of them five bytes — and under this
1020 // transport a write is not a syscall, it is a hand-off across a
1021 // `tokio::io::duplex` from a `spawn_blocking` thread, which is a `block_on`,
1022 // a waker and (whenever the reader is parked) a futex pair.
1023 // `gunnar_wire::TRANSPORT_BUFFER_BYTES` carries the arithmetic and the
1024 // write-counting guard.
1025 let mut input = BufReader::with_capacity(gunnar_wire::TRANSPORT_BUFFER_BYTES, metered_in);
1026 let mut output = BufWriter::with_capacity(gunnar_wire::TRANSPORT_BUFFER_BYTES, metered_out);
1027 let started = std::time::Instant::now();
1028
1029 // ── the first-repository door: advertise, read, check, THEN create ──────
1030 //
1031 // Nothing has been written to the catalog or the disk for a prospective
1032 // request. The client is shown the empty repository it would be pushing
1033 // into; if it hangs up (a sha1 tree that read `object-format=sha256`,
1034 // `git push` with nothing to do) this returns and nothing exists. Only a
1035 // command list that passes the hash check turns into a repository — and
1036 // that create is `ServerStores::create_repo`, the same transactional verb
1037 // every other create uses.
1038 //
1039 // A prospective LFS UPLOAD (git-lfs's pre-push, before receive-pack's
1040 // commands) goes on with no store at all: `ProspectiveLfs` makes the
1041 // repository through the same writes when the first object arrives.
1042 let (request, store, preamble) = match store {
1043 Some(store) => (request, Some(store), None),
1044 None if matches!(request.service, GitService::LfsTransfer(_)) => (request, None, None),
1045 None => match admit_prospective_push(serving, &request, &mut input, &mut output)? {
1046 Some((request, store, preamble)) => (request, Some(store), Some(preamble)),
1047 None => {
1048 output.flush()?;
1049 metrics.add_bytes_out(bytes_out.load(Ordering::Relaxed));
1050 metrics.add_bytes_in(bytes_in.load(Ordering::Relaxed));
1051 tracing::info!(
1052 target: TARGET_GIT,
1053 event = "git.receive_pack.prospect_abandoned",
1054 repo = %request.repo.key(),
1055 elapsed_ms = started.elapsed().as_millis() as u64,
1056 "the client took the advertisement of a repository that does not exist and \
1057 sent no command; nothing was created"
1058 );
1059 return Ok(());
1060 }
1061 },
1062 };
1063
1064 // Only a prospective LFS upload reaches here without a store.
1065 let git_store = || {
1066 store.clone().ok_or_else(|| {
1067 TransportError::Refused(format!(
1068 "repository not found, or you do not have access: {}",
1069 request.repo.path()
1070 ))
1071 })
1072 };
1073 match request.service {
1074 GitService::UploadPack => {
1075 let span = tracing::info_span!(target: TARGET_GIT, "git.upload_pack");
1076 let _entered = span.enter();
1077 let store = git_store()?;
1078 let version = protocol_version(&request);
1079 // ── the znippy engine answers reads ─────────────────────────────────
1080 //
1081 // When `GUNNAR_COLD_TIER=znippy` and the repository has an archive,
1082 // upload-pack serves pack ENTRIES out of that archive rather than
1083 // out of `objects/pack` — no unfold, no file. `packed()` is `Some`
1084 // on both arms, so the entry-copy path is the same code; only where
1085 // the bytes come from changes.
1086 //
1087 // receive-pack is deliberately NOT switched: a push lands on `fs`
1088 // and reaches the archive through the append hook. The archive is
1089 // immutable, and `ZnippyObjects::write` says so rather than
1090 // pretending.
1091 //
1092 // There is no second read side to choose between any more. The
1093 // engine *is* the archive on the znippy arm, so `GUNNAR_COLD_TIER`,
1094 // `cold_objects()` and the `Box<dyn ObjectStore>` that carried a
1095 // `None` for "this binary has no znippy engine" have all gone with
1096 // the old znippy engine crate: one store answers reads and writes, and which
1097 // engine it is was decided at startup.
1098 //
1099 // **One handle, not two.** `UploadPack` used to take an object
1100 // store and a ref store, and an advertisement built from one
1101 // snapshot could promise a pack built from another. `GitServe`
1102 // carries both halves, so the ref list and the pack it promises are
1103 // the same store.
1104 let git = store.serve();
1105 // `hash_kind` is a required argument, never a default: it comes
1106 // from the catalog entry through `OpenRepo`, and answering `sha1`
1107 // for a sha256 repository does not error — it decodes garbage.
1108 //
1109 // `with_config`, not `new`: this call is the whole of what makes
1110 // `--no-filter`, `--want-policy`, `--fetch-budget` and the byte
1111 // ceilings anything other than fields nobody reads.
1112 let upload = UploadPack::new(git, store.hash_kind()).with_config(wire.upload.clone());
1113 // `Delivery::Advertisement` is `GET /info/refs`: write the ref list
1114 // (v0) or the capability list (v2) and stop, reading nothing. The
1115 // same call the connected path makes inline, so the two transports
1116 // cannot advertise differently.
1117 if request.delivery == Delivery::Advertisement {
1118 upload.advertise(version, &mut output).map_err(wire_error)?;
1119 output.flush()?;
1120 let out = bytes_out.load(Ordering::Relaxed);
1121 metrics.add_bytes_out(out);
1122 tracing::info!(
1123 target: TARGET_GIT,
1124 event = "git.upload_pack.advertised",
1125 protocol = version.number(),
1126 bytes_out = out,
1127 elapsed_ms = started.elapsed().as_millis() as u64,
1128 "served the upload-pack advertisement"
1129 );
1130 return Ok(());
1131 }
1132 // Past the advertisement's early return above: this is a SESSION,
1133 // which is what `fetches` counts. A bare `git ls-remote` never
1134 // reaches here.
1135 flow.fetched.set(true);
1136 let summary = upload
1137 .serve_session(version, wire_session(&request), &mut input, &mut output)
1138 .map_err(|err| {
1139 tracing::warn!(
1140 target: TARGET_GIT,
1141 event = "git.upload_pack.failed",
1142 elapsed_ms = started.elapsed().as_millis() as u64,
1143 bytes_out = bytes_out.load(Ordering::Relaxed),
1144 %err,
1145 "git-upload-pack failed"
1146 );
1147 wire_error(err)
1148 })?;
1149 output.flush()?;
1150 let out = bytes_out.load(Ordering::Relaxed);
1151 if summary.objects_sent > 0 {
1152 metrics.add_packs_served(1);
1153 }
1154 metrics.add_objects_sent(summary.objects_sent as u64);
1155 metrics.add_bytes_out(out);
1156 metrics.add_bytes_in(bytes_in.load(Ordering::Relaxed));
1157 // The applied outputs, and the duration. `repo` and `principal` are
1158 // NOT repeated: they are fields of `ssh.connection`, which this span
1159 // descends from.
1160 tracing::info!(
1161 target: TARGET_GIT,
1162 event = "git.upload_pack.served",
1163 // What was actually spoken, taken from the session's own
1164 // summary rather than from the request that asked for it; a v2
1165 // request served as v0 would otherwise log as v2.
1166 protocol = summary.protocol.number(),
1167 ls_refs = summary.ls_refs,
1168 rounds = summary.fetch_rounds,
1169 common = summary.common,
1170 objects = summary.objects_sent,
1171 // **WHICH PATH ANSWERED.** `Selection::answered_from_bitmap`
1172 // exists so a guard can tell a fetch served from the store's
1173 // reachability index from one served by walking, and until now
1174 // nothing anywhere read it: not a log line, not a metric, not a
1175 // test. Its only reader in the constellation was an example
1176 // binary.
1177 //
1178 // That blind spot has already cost something concrete. The
1179 // `--fetch-budget` control charged the ids a WALKER holds, and
1180 // on the store the appliance actually runs there is no walker —
1181 // the index answers whole — so the budget was compared against
1182 // nothing while a red test sat in front of three separate call
1183 // sites and each read it as a policy question. A field nobody can
1184 // observe is a behaviour nobody can assert.
1185 from_index = summary.answered_from_bitmap,
1186 bytes_out = out,
1187 elapsed_ms = started.elapsed().as_millis() as u64,
1188 "served git-upload-pack"
1189 );
1190 }
1191 GitService::ReceivePack => {
1192 let span = tracing::info_span!(target: TARGET_GIT, "git.receive_pack");
1193 let _entered = span.enter();
1194 let store = git_store()?;
1195 let outcome =
1196 serve_receive_pack(serving, &request, &store, preamble, &mut input, &mut output);
1197 let out = bytes_out.load(Ordering::Relaxed);
1198 let inn = bytes_in.load(Ordering::Relaxed);
1199 metrics.add_bytes_out(out);
1200 metrics.add_bytes_in(inn);
1201 // A push is a receive-pack session that CAME BACK. The bytes are
1202 // charged either way — they arrived — but a session that failed is
1203 // not a push, and counting it as one would make a tenant whose every
1204 // push is refused look like a tenant who is working.
1205 flow.pushed.set(outcome.is_ok());
1206 if let Err(err) = &outcome {
1207 tracing::warn!(
1208 target: TARGET_GIT,
1209 event = "git.receive_pack.failed",
1210 elapsed_ms = started.elapsed().as_millis() as u64,
1211 bytes_in = inn,
1212 %err,
1213 "git-receive-pack failed"
1214 );
1215 }
1216 outcome?;
1217 }
1218 // LFS over SSH, git-lfs 3.0's pure-SSH transfer. The grant was already decided
1219 // by `gunnar_ssh::command::parse`, from the same key table and the same
1220 // path sandbox the two verbs above went through: `operation` here is the
1221 // one that demanded Read or Write, not a second decision. It is passed
1222 // straight to the session so there is no second authorisation path.
1223 //
1224 // The objects live under the repository's own git dir, in `lfs/`. See
1225 // `gunnar_lfs::fs_store`.
1226 GitService::LfsTransfer(operation) => {
1227 let span = tracing::info_span!(target: TARGET_GIT, "git.lfs_transfer");
1228 let _entered = span.enter();
1229 let lfs: Box<dyn gunnar_lfs::LfsStore + '_> = match &store {
1230 Some(store) => Box::new(open_lfs_store(stores, store, &request)?),
1231 None => Box::new(ProspectiveLfs::new(stores, request.clone())),
1232 };
1233 let mut session = wire.lfs_session(lfs.as_ref(), operation);
1234 // **The tenant's remaining LFS room**, from the gate chain that ran
1235 // in `authorise_service`. `None` on a community build, on an
1236 // uncapped namespace and on every download; see
1237 // `gunnar_ssh::PlanGate::lfs_allowance`.
1238 if let Some(allowance) = request.lfs_allowance.clone() {
1239 session = session.with_allowance(allowance);
1240 }
1241 // `limits()` and `allowance()` read back off the session, not off
1242 // `wire` and not off the request: what is logged is what the session
1243 // will enforce, so a builder that stopped taking would show here
1244 // rather than in a support ticket.
1245 let effective = session.limits();
1246 tracing::debug!(
1247 target: TARGET_GIT,
1248 max_object_bytes = effective.max_object_bytes,
1249 max_batch_objects = effective.max_batch_objects,
1250 max_drain_bytes = effective.max_drain_bytes,
1251 lfs_allowance_bytes = session.allowance().map(|a| a.bytes),
1252 "lfs session limits"
1253 );
1254 let outcome = session.serve(&mut input, &mut output);
1255 output.flush()?;
1256 let out = bytes_out.load(Ordering::Relaxed);
1257 let inn = bytes_in.load(Ordering::Relaxed);
1258 metrics.add_bytes_out(out);
1259 metrics.add_bytes_in(inn);
1260 let summary = outcome.map_err(|err| {
1261 tracing::warn!(
1262 target: TARGET_GIT,
1263 event = "git.lfs_transfer.failed",
1264 operation = %operation,
1265 elapsed_ms = started.elapsed().as_millis() as u64,
1266 %err,
1267 "git-lfs-transfer failed"
1268 );
1269 TransportError::Protocol(err.to_string())
1270 })?;
1271 // The APPLIED figures, from the session's own count. They are a
1272 // different number from the wire bytes above about the same
1273 // transfer — the wire carries framing and the batch conversation —
1274 // so the flow ledger keeps both rather than one as a subset of the
1275 // other.
1276 flow.lfs_bytes_in.set(summary.bytes_received);
1277 flow.lfs_bytes_out.set(summary.bytes_sent);
1278 // Applied output: objects and bytes that really moved, taken from
1279 // the session's own count rather than from what was asked for.
1280 tracing::info!(
1281 target: TARGET_GIT,
1282 event = "git.lfs_transfer.served",
1283 operation = %operation,
1284 commands = summary.commands,
1285 batches = summary.batches,
1286 batch_objects = summary.batch_objects,
1287 objects_sent = summary.objects_sent,
1288 objects_received = summary.objects_received,
1289 lfs_bytes_out = summary.bytes_sent,
1290 lfs_bytes_in = summary.bytes_received,
1291 clean_quit = summary.clean_quit,
1292 bytes_out = out,
1293 bytes_in = inn,
1294 elapsed_ms = started.elapsed().as_millis() as u64,
1295 "served git-lfs-transfer"
1296 );
1297 }
1298 }
1299 Ok(())
1300}
1301
1302// Eight, because the counters joined the six things a push already needed.
1303// Bundling them into a context struct would be one more type whose
1304// only job is to be unpacked on the first line of this function.
1305#[allow(clippy::too_many_arguments)]
1306/// The door's commit. Advertise the prospect, read the client's commands,
1307/// and — only if there are any and they pass the hash check — write the
1308/// catalog row (the namespace with it, if the admin's push names a new one)
1309/// and make the store. `None` means the client sent nothing and nothing was
1310/// made.
1311///
1312/// A refusal after the preamble (a stale store at the name, a grant revoked
1313/// since the exec, a store the engine cannot make) is answered in git's own
1314/// protocol — an `ERR` packet, which `git push` renders as `remote error:` —
1315/// and leaves nothing: the row is retracted, the namespace too if this call
1316/// conjured it, the directory removed.
1317fn admit_prospective_push(
1318 serving: &Serving<'_>,
1319 request: &ServiceRequest,
1320 input: &mut dyn BufRead,
1321 output: &mut dyn std::io::Write,
1322) -> Result<Option<(ServiceRequest, Arc<OpenRepo>, gunnar_wire::Preamble)>, TransportError> {
1323 let Serving { stores, wire, .. } = *serving;
1324 if request.service != GitService::ReceivePack {
1325 // An LFS upload's door is `ProspectiveLfs`, never this one; anything
1326 // else here is a programming error, refused rather than
1327 // served against a repository that does not exist.
1328 return Err(TransportError::Refused(format!(
1329 "repository not found, or you do not have access: {}",
1330 request.repo.path()
1331 )));
1332 }
1333 let hash_kind: gix_hash::Kind = request.repo.hash_kind.into();
1334 if wire_session(request).advertises_inline() || request.delivery == Delivery::Advertisement {
1335 ReceivePack::write_empty_advertisement(hash_kind, &wire.receive, output)
1336 .map_err(wire_error)?;
1337 if request.delivery == Delivery::Advertisement {
1338 return Ok(None);
1339 }
1340 }
1341 let Some(preamble) = ReceivePack::read_preamble(hash_kind, input).map_err(wire_error)? else {
1342 return Ok(None);
1343 };
1344
1345 // The client means it. Now, and only now, the writes — in the order that
1346 // leaves nothing behind if any of them fails.
1347 match create_prospect(stores, request) {
1348 Ok((repo, store)) => {
1349 let mut admitted = request.clone();
1350 admitted.repo = repo;
1351 admitted.prospective = false;
1352 Ok(Some((admitted, store, preamble)))
1353 }
1354 Err(sentence) => {
1355 let _ = gunnar_wire::err_packet(output, &sentence);
1356 let _ = output.flush();
1357 Err(TransportError::Protocol(sentence))
1358 }
1359 }
1360}
1361
1362/// **The first-repository door's writes**, for a prospective request whose
1363/// client has shown it means it — a receive-pack's commands past the hash
1364/// check, or an LFS upload's first object ([`ProspectiveLfs`]). The stale
1365/// store check, the catalog row (and the namespace, if the admin's push names
1366/// a new one) under [`gunnar_ssh::Control::admit_push_create`]'s re-asked
1367/// grant, then the store — and if the store cannot be made, the row and the
1368/// namespace are taken back and the directory removed. `Err` is the sentence
1369/// the client is owed; nothing is left behind by it.
1370fn create_prospect(
1371 stores: &ServerStores,
1372 request: &ServiceRequest,
1373) -> Result<(gunnar_core::Repo, Arc<OpenRepo>), String> {
1374 let path = request.repo.path();
1375 let control = stores.ssh_control();
1376 // **Made meanwhile, by this same push** (2026-09-28): git takes
1377 // receive-pack's advertisement (the prospect) BEFORE it runs the pre-push
1378 // hook, and git-lfs's upload in that hook makes the repository in
1379 // between. The stale-store check below would answer the push's own
1380 // commands with an ERR. A repository that now EXISTS is served, as the
1381 // lost race in `admit_push_create` is, after the same re-asked grant,
1382 // and only in the hash the client was shown.
1383 match control.resolve(&path) {
1384 Ok(Some(existing)) => {
1385 if existing.hash_kind != request.repo.hash_kind {
1386 return Err(format!(
1387 "{path} was created meanwhile as a {:?} repository, and this push was shown {:?}; nothing was written — push again",
1388 existing.hash_kind, request.repo.hash_kind
1389 ));
1390 }
1391 return match control.push_create_spec(&request.principal, &path) {
1392 Ok(Some(_)) => stores
1393 .repo_store(&existing)
1394 .map(|store| (existing, store))
1395 .map_err(|err| {
1396 format!("the repository {path} could not be opened on the server: {err:#}")
1397 }),
1398 Ok(None) => Err(format!(
1399 "repository not found, or you do not have access: {path}"
1400 )),
1401 Err(err) => Err(format!(
1402 "the gunnar control plane could not read {path}: {err}"
1403 )),
1404 };
1405 }
1406 Ok(None) => {}
1407 Err(err) => {
1408 return Err(format!(
1409 "the gunnar control plane could not read {path}: {err}"
1410 ))
1411 }
1412 }
1413 if let Err(err) = stores.refuse_stale_store(&path, false, request.repo.hash_kind) {
1414 return Err(format!("{err:#}"));
1415 }
1416 let (repo, conjured) = match control.admit_push_create(&request.principal, &path) {
1417 Ok(Some(pair)) => pair,
1418 Ok(None) => {
1419 return Err(format!(
1420 "repository not found, or you do not have access: {path}"
1421 ))
1422 }
1423 Err(err) => {
1424 return Err(format!(
1425 "the gunnar control plane could not create {path}: {err}"
1426 ))
1427 }
1428 };
1429 match stores.repo_store(&repo) {
1430 Ok(store) => Ok((repo, store)),
1431 Err(err) => {
1432 stores.close_repo(&path);
1433 if let Err(undo) = control.retract_push_create(&path, conjured) {
1434 tracing::error!(
1435 target: TARGET_GIT,
1436 repo = %path,
1437 error = %undo,
1438 "retracting the catalog row of a push-create whose store failed"
1439 );
1440 }
1441 if let Ok(dir) = stores.repo_dir(&path) {
1442 let _ = std::fs::remove_dir_all(dir);
1443 }
1444 tracing::error!(
1445 target: TARGET_GIT,
1446 repo = %path,
1447 error = format!("{err:#}"),
1448 "the push-created repository's store could not be made; nothing was kept"
1449 );
1450 Err(format!("the repository {path} could not be created on the server; nothing was kept. The reason is in the server log"))
1451 }
1452 }
1453}
1454
1455/// **A push into an encrypted repository on a build that cannot check it in quarantine**
1456/// (wiring tracer B 2026-10-08): the quarantine is the znippy engine's
1457/// (`encrypted_quarantine`), and without it a refused plaintext pack would be stored and framed
1458/// for the twin before the ref gate refused it. `Some(the line)`: refuse the push by name,
1459/// before a byte is read.
1460pub(crate) fn unchecked_encrypted_push(encrypted: bool, quarantine: bool) -> Option<String> {
1461 (encrypted && !quarantine).then(|| {
1462 "this repository is encrypted, and this Gunnar was built without the check that keeps \
1463 plaintext out of it, so it takes no push into it: push to a Gunnar built with its \
1464 standard storage engine. Nothing was received"
1465 .to_string()
1466 })
1467}
1468
1469fn serve_receive_pack(
1470 serving: &Serving<'_>,
1471 request: &ServiceRequest,
1472 store: &OpenRepo,
1473 preamble: Option<gunnar_wire::Preamble>,
1474 input: &mut dyn BufRead,
1475 output: &mut dyn std::io::Write,
1476) -> Result<(), TransportError> {
1477 let Serving {
1478 stores,
1479 bus,
1480 attestations,
1481 metrics,
1482 wire,
1483 } = *serving;
1484 let started = std::time::Instant::now();
1485 // A build that cannot check an encrypted repository's pack in quarantine takes none.
1486 if let Some(line) =
1487 unchecked_encrypted_push(request.repo.encrypted, cfg!(feature = "engine-znippy"))
1488 {
1489 return Err(TransportError::Refused(line));
1490 }
1491 // **Everything below this line is a validation read.** A receive-pack
1492 // ingests, indexes and then walks every commit and tree it was sent to
1493 // prove the push closes — once each — and serves nobody. Those reads are
1494 // indistinguishable from a client's inside the store, so the caller is the
1495 // only thing that can say which they are, and this is the caller.
1496 //
1497 // Without it the push fills the engine's lazy read-cache for a reader that
1498 // does not exist: MEASURED 6 778 047 872 B on `torvalds/linux`, 31 % of the
1499 // finished store (measured 2026-09-03).
1500 //
1501 // Thread-scoped and dropped at the end of this function, so a concurrent
1502 // clone on another connection still warms the cache normally.
1503 #[cfg(feature = "engine-znippy")]
1504 let _validation_reads = crate::storage::znippy_engine::validation_reads();
1505 // **One handle.** `GitServe` carries the reading contract and, through its
1506 // `GitOps` supertrait, the eleven — so receive-pack advertises, checks
1507 // connectivity and writes references against a single store rather than
1508 // against an object handle and a ref handle that could disagree.
1509 let git = store.serve();
1510 let hash_kind = store.hash_kind();
1511
1512 let gate = PolicyGate::open(store.git_dir(), request, stores, Arc::clone(metrics));
1513
1514 // Opened before the ingest, and that ordering is load-bearing: whether the
1515 // policy has any gate at all is what decides if the ingest pays for an
1516 // object-set enumeration. It used to be built after, when nothing needed
1517 // to know.
1518
1519 // No `objects/pack` is created. An empty one is what a later reader uses
1520 // to decide a repository is packfile-shaped, and this server never writes
1521 // a packfile into it.
1522
1523 // ── who asks for the object set, and why it is not only the gates ──────
1524 //
1525 // `want_objects: !gate.engine.is_empty()` stood here, and it had **one**
1526 // consumer in mind. The push attestation is a second one, and it was not
1527 // counted: on a repository with no policy gates the ingest skipped the
1528 // enumeration and `attestation_for` then wrote `new_object_count: 0` with
1529 // an empty `new_object_oids` — a push of nothing and a push nobody
1530 // enumerated rendering as the same record. The log line thirty lines below
1531 // this already got that distinction right (`objects_unknown`); the
1532 // attestation, which is the durable artefact and the one a verifier reads,
1533 // did not. `tests/push_events.rs` saw it as *"the pushed commit is missing
1534 // from the attestation's object list: []"*.
1535 //
1536 // So the attestation asks too, and by default it gets an answer. That is
1537 // not free — see `znippy_engine::ingest`'s note on why the flag exists at
1538 // all: the ids are not a by-product of storing a pack, and asking for them
1539 // waits for the indexer to drain. An operator who would rather have the
1540 // latency than the object list turns it off with
1541 // `--no-attest-objects`, and the attestation then records **why** the set
1542 // is absent instead of implying it was empty.
1543 let objects = if gate.engine.reads_new_objects() {
1544 // A gate reads the set. (Not `!is_empty()`: the attribution-trailer
1545 // gate is live on every repository by default and decides from the
1546 // references and the object graph, so it must not make every push
1547 // demand an enumeration — the gix engine refuses `Required` outright.) An engine that cannot produce one must refuse
1548 // rather than let the gate decide on nothing.
1549 crate::storage::ObjectSet::Required
1550 } else if wire.attest_objects {
1551 // Only the attestation wants it, and the attestation can say it did not
1552 // get it. An engine that cannot enumerate still serves the push.
1553 crate::storage::ObjectSet::Wanted
1554 } else {
1555 crate::storage::ObjectSet::NotNeeded
1556 };
1557 let ingest = PushIngest {
1558 git_dir: store.git_dir().to_path_buf(),
1559 hash_kind,
1560 engine: stores.engine(),
1561 objects,
1562 // **A push has nobody watching an object count.** `git push` renders the
1563 // remote's progress band, and what it wants there is bytes; there is no
1564 // job record and no console poll on this path. The mirror is the caller
1565 // that has both, so the mirror is the caller that passes a sink.
1566 watching: None,
1567 // The pushed pack is a transaction (the twin): framed before the engine stores it. For a
1568 // first push `request.repo` is exactly the row `admit_push_create` writes first.
1569 frame: Some(PackFrame::of(stores, &request.repo)),
1570 // An encrypted repository's pack is checked before it is kept.
1571 encrypted: request.repo.encrypted.then_some(git),
1572 // Held to the data set's room, chunk by chunk (cov-13).
1573 space: stores.space_ledger(),
1574 };
1575 if gate.engine.is_empty() {
1576 tracing::info!(
1577 target: TARGET_POLICY,
1578 event = "policy.gates.none",
1579 policy = %store.git_dir().join(POLICY_FILE).display(),
1580 "no policy gates are configured for this repository; every well-formed push is \
1581 accepted. This is an empty policy, not a passing one."
1582 );
1583 } else {
1584 tracing::info!(
1585 target: TARGET_POLICY,
1586 event = "policy.gates.live",
1587 gates = ?gate.engine.gate_names(),
1588 "policy gates live for this push"
1589 );
1590 }
1591
1592 // **receive-pack reads from the store it writes into.**
1593 //
1594 // This used to be `&objects` unconditionally, under a comment saying
1595 // receive-pack was "deliberately NOT switched" because a push landed on
1596 // `fs` and reached the tier through the append hook. That is no longer the
1597 // route: the pack goes straight into the tier, so checking connectivity
1598 // against the filesystem would ask an empty repository whether the objects
1599 // it just stored are present — and the answer is a refused push, which is
1600 // exactly what it did the first time this ran.
1601 //
1602 // A pack the ingest stored but the indexer has not absorbed yet is **not**
1603 // a stale answer here: the per-pack `indexed` bit makes an index miss fall back to the journal
1604 // rather than report absent, so the read side sees a push the moment
1605 // `put_pack` returns, which is before anything asks.
1606 //
1607 // `write_objects` and not `cold_objects`: the latter asks whether there is
1608 // anything to *serve*, and answers `None` for an empty repository — which
1609 // is every repository's first push.
1610 // The engine is the read side; there is nothing else to open.
1611
1612 // No `#[allow(unused_mut)]` any more: it was here because the only
1613 // assignment below was `cfg(metered)`, so a community build had a `mut`
1614 // binding it never wrote. The ceiling is assigned on both builds, so the
1615 // `mut` is now load-bearing on both — and an allow that no longer suppresses
1616 // anything is a suppression nobody can see expire.
1617 let mut receive_config = wire.receive.clone();
1618 // The plan's warning (Grace), decided in `authorise_service` and carried
1619 // on the request: the wire prints it on the progress band beside the
1620 // report, so `git push` shows `remote: plan expired …; renew at …`.
1621 #[cfg(feature = "metered")]
1622 {
1623 receive_config.notice = request.plan_notice.clone();
1624 }
1625 // **The mid-upload ceiling**, decided by the same gate chain in
1626 // `authorise_service` and carried on the request beside the notice. Not
1627 // `cfg(metered)`: the field is on `ServiceRequest` unconditionally and a
1628 // community build simply carries `None`, so there is one path here rather
1629 // than a second one that only the paid build compiles (one implementation). The two
1630 // `PackCeiling`s are the deliberate twin `gunnar_ssh::PackCeiling`
1631 // documents — `gunnar-ssh` does not depend on `gunnar-wire` — and this is
1632 // the one seam that holds both and converts.
1633 //
1634 // The third field is the one this seam could not have been given by either
1635 // side alone: `gunnar-ssh` measured the namespace and marked its ledger,
1636 // `gunnar-wire` counts the bytes, and neither may name the other's type.
1637 // See `PackMark` below.
1638 receive_config.pack_ceiling =
1639 request
1640 .pack_ceiling
1641 .as_ref()
1642 .map(|ceiling| gunnar_wire::PackCeiling {
1643 bytes: ceiling.bytes,
1644 refusal: ceiling.refusal.clone(),
1645 ledger: request
1646 .pack_mark
1647 .clone()
1648 .map(|mark| Arc::new(PackMark { mark }) as Arc<dyn gunnar_wire::PackLedger>),
1649 });
1650 let receive = ReceivePack::new(git, hash_kind).with_config(receive_config);
1651 // `GET /info/refs?service=git-receive-pack`: the ref list and its
1652 // capabilities, then stop. No command list, no pack, no gate — the push
1653 // itself arrives as a separate POST.
1654 if request.delivery == Delivery::Advertisement && preamble.is_none() {
1655 receive
1656 .write_ref_advertisement(output)
1657 .map_err(wire_error)?;
1658 output.flush()?;
1659 tracing::info!(
1660 target: TARGET_GIT,
1661 event = "git.receive_pack.advertised",
1662 elapsed_ms = started.elapsed().as_millis() as u64,
1663 "served the receive-pack advertisement"
1664 );
1665 return Ok(());
1666 }
1667 // A preamble already read (the first-repository door) skips the
1668 // advertisement and the command read: that half happened before the
1669 // repository existed.
1670 // **Heimdall first** (feature `heimdall`, `crate::heimdall_gate`): with the
1671 // gate installed, the pushed pack is parsed in a Heimdall job and only a
1672 // pack whose signed result checks out reaches the ingest, exactly as a
1673 // fetched one does in `crate::mirror_fetch`.
1674 #[cfg(feature = "heimdall")]
1675 let checked = stores
1676 .pack_gate()
1677 .map(|gate| gate.checked_as(&ingest, hash_kind));
1678 #[cfg(feature = "heimdall")]
1679 let ingest: &dyn gunnar_wire::PackIngest = match &checked {
1680 Some(c) => c,
1681 None => &ingest,
1682 };
1683 #[cfg(not(feature = "heimdall"))]
1684 let ingest: &dyn gunnar_wire::PackIngest = &ingest;
1685 let outcome = match preamble {
1686 Some(preamble) => receive
1687 .serve_after_preamble(preamble, input, output, ingest, &gate)
1688 .map(Some),
1689 None => receive.serve_session(wire_session(request), input, output, ingest, &gate),
1690 }
1691 .map_err(wire_error)?;
1692 output.flush()?;
1693
1694 let Some(outcome) = outcome else {
1695 tracing::info!(
1696 target: TARGET_GIT,
1697 event = "git.receive_pack.advertisement_only",
1698 elapsed_ms = started.elapsed().as_millis() as u64,
1699 "git-receive-pack: the client took the advertisement and had nothing to push"
1700 );
1701 return Ok(());
1702 };
1703
1704 // The key this session authenticated with. Carried on the request since
1705 // The authenticating key itself, not recovered from the identity table.
1706 let fingerprint = session_key_fingerprint(request);
1707 // The answerable one. See `session_credential` and GUN-014.
1708 let credential = session_credential(request);
1709 let gates = gate.gate_that_refused();
1710 // **THE counting point for a policy refusal**, per namespace and per push.
1711 //
1712 // Once per PUSH and not once per refused reference, deliberately: a push
1713 // that names four protected branches met one door four times, and a console
1714 // that reported four refusals would make one mistake look like a campaign.
1715 // The refs themselves are already in the `git.ref.rejected` lines below and
1716 // in the `PushRejected` events, which is where per-reference detail belongs.
1717 if !gates.is_empty() {
1718 serving.stores.flow().refuse(
1719 &request.repo.namespace,
1720 gunnar_core::now_ms(),
1721 gunnar_core::Refusal::Policy,
1722 );
1723 }
1724
1725 let mut refs_ok = 0usize;
1726 let mut refs_rejected = 0usize;
1727 for (command, status) in outcome.commands.iter().zip(&outcome.statuses) {
1728 match status {
1729 RefStatus::Ok => {
1730 refs_ok += 1;
1731 if command.is_delete() {
1732 metrics.add_refs_deleted(1);
1733 } else {
1734 metrics.add_refs_updated(1);
1735 }
1736 tracing::info!(
1737 target: TARGET_GIT,
1738 event = if command.is_delete() { "git.ref.deleted" } else { "git.ref.updated" },
1739 ref_name = %command.name,
1740 old = %command.old,
1741 new = %command.new,
1742 "ref updated"
1743 )
1744 }
1745 RefStatus::Rejected(reason) => {
1746 refs_rejected += 1;
1747 metrics.add_refs_rejected(1);
1748 tracing::warn!(
1749 target: TARGET_GIT,
1750 event = "git.ref.rejected",
1751 ref_name = %command.name,
1752 old = %command.old,
1753 new = %command.new,
1754 // The gate that said no, when one did. Empty means nothing
1755 // named a gate: a connectivity failure, a lost
1756 // compare-and-swap, or the collateral refusal every other
1757 // reference gets in an `--atomic` push. The event stream publishes the
1758 // same distinction; the log must not blur it.
1759 gate = gates.get(&command.name.to_string()).map(String::as_str).unwrap_or(""),
1760 %reason,
1761 "ref rejected"
1762 )
1763 }
1764 }
1765 if let Some(bus) = bus {
1766 // Only a reference that actually moved has a descent question to
1767 // answer, so the walk is skipped for everything else.
1768 let forced = status.is_ok() && was_forced(git, command);
1769 publish(
1770 bus,
1771 request,
1772 command,
1773 status,
1774 outcome.atomic,
1775 &fingerprint,
1776 &credential,
1777 &gates,
1778 forced,
1779 );
1780 }
1781 }
1782
1783 // A push that moved a reference is the pusher's operation (2026-10-21,
1784 // the admin's user list). In memory, at most once a minute per person.
1785 if refs_ok > 0 {
1786 stores
1787 .activity()
1788 .operated(&request.principal.id, gunnar_core::now_ms());
1789 }
1790
1791 // ── a repository nobody could clone, made clonable ─────────────────────
1792 //
1793 // After the references have moved, and only for a repository whose `HEAD`
1794 // still resolves to nothing. See
1795 // [`ServerStores::adopt_default_branch_if_unborn`] for why a push is
1796 // allowed to decide this and for how narrow the condition is. A failure
1797 // here is logged and not fatal: the push itself succeeded and its
1798 // references are durable, and refusing it afterwards would be a lie about
1799 // what happened.
1800 let created: Vec<String> = outcome
1801 .commands
1802 .iter()
1803 .zip(&outcome.statuses)
1804 .filter(|(command, status)| status.is_ok() && !command.is_delete())
1805 .map(|(command, _)| command.name.to_string())
1806 .collect();
1807 if !created.is_empty() {
1808 if let Err(err) = stores.adopt_default_branch_if_unborn(&request.repo.path(), &created) {
1809 tracing::warn!(
1810 target: TARGET_GIT,
1811 event = "git.default_branch.adoption_failed",
1812 repo = %request.repo.key(),
1813 error = format!("{err:#}"),
1814 "the push landed, but this repository's HEAD names a branch that does not \
1815 exist and adopting a pushed one failed; it cannot be cloned until \
1816 `set-default-branch` is run against it"
1817 );
1818 }
1819 }
1820
1821 // ── the one line per push an operator reads ────────────────────────────
1822 //
1823 // Applied outputs: how many references actually moved, how many were
1824 // refused, what the pack cost and how long the whole thing took. A push
1825 // summary that said only "done" would be indistinguishable from one that
1826 // moved nothing (measured, not asserted).
1827 //
1828 // `objects` is what the push INTRODUCED, and an ingest that could not work
1829 // that out logs the reason rather than a plausible zero, because a push of
1830 // nothing and a push whose contents could not be enumerated are different
1831 // facts.
1832 let objects_pushed: &[ObjectId] = outcome.new_objects.as_deref().unwrap_or(&[]);
1833 let objects_unknown = outcome.new_objects.as_ref().err().map(String::as_str);
1834 // Two different numbers, both worth having: `wire_bytes` is what the client
1835 // sent and is what a `max_pack_bytes` limit is enforced against;
1836 // `pack_bytes` is what landed, which is larger for a thin push because
1837 // completing it writes bases the client did not send.
1838 //
1839 // Neither may fall back to `0`. `PushFacts::pack_bytes` is `Option` on
1840 // purpose — "`Some(0)` for a deletions-only push … `None` only if the count
1841 // is genuinely unavailable" — and `unwrap_or(0)` threw away exactly the
1842 // distinction the type exists to carry. `stat` failing on a pack whose
1843 // bytes are then reported as `0` is the same disease: an absence that
1844 // renders as a value (and the `objects_unknown` field above).
1845 let (wire_bytes, wire_bytes_unknown) = measured(
1846 outcome.pack_bytes,
1847 "the ingest did not report how many packfile bytes were read off the wire",
1848 );
1849 let (pack_bytes, pack_bytes_unknown) = measured_pack_bytes(&outcome);
1850 if refs_rejected == 0 && outcome.unpack.is_ok() {
1851 metrics.add_pushes_accepted(1);
1852 } else {
1853 metrics.add_pushes_rejected(1);
1854 }
1855 tracing::info!(
1856 target: TARGET_GIT,
1857 event = "git.receive_pack.served",
1858 refs_ok,
1859 refs_rejected,
1860 atomic = outcome.atomic,
1861 unpack_ok = outcome.unpack.is_ok(),
1862 objects = objects_pushed.len(),
1863 objects_unknown,
1864 wire_bytes,
1865 wire_bytes_unknown,
1866 pack_bytes,
1867 pack_bytes_unknown,
1868 push_options = outcome.push_options.len(),
1869 elapsed_ms = started.elapsed().as_millis() as u64,
1870 "served git-receive-pack"
1871 );
1872
1873 // ── the znippy engine took the objects at ingest ───────────────────────────
1874 //
1875 // There is no second step here any more, and its absence is the change.
1876 //
1877 // What used to sit at this line read the `.pack` and `.idx` that
1878 // `index_thin_pack` had written and appended them to an archive — so the
1879 // tier was a COPY of a filesystem write that had already happened, and the
1880 // packfile it copied from stayed in `objects/pack` for ever. That is what
1881 // made every push pay for git's on-disk format even on a server whose
1882 // storage is Arrow. `PushIngest::ingest_cold` now stores the bytes as they
1883 // arrive and no packfile is written at all.
1884 //
1885 // The `refs_ok > 0` guard that used to wrap this is gone with it, and its
1886 // reasoning does not transfer: it existed because archiving a
1887 // fully-rejected push would make the tier the only place that push
1888 // survived. Storage is no longer a place a push can survive *separately* —
1889 // it is where the push went — so a rejected ref list now leaves exactly
1890 // what a rejected ref list leaves on any git server: objects nothing names,
1891 // for `gunnar gc` to collect.
1892
1893 // ── push attestation ───────────────────────────────────────────────────
1894 //
1895 // Built from what the push actually did, not from what it asked for: a
1896 // refused reference is not in `ref_updates`, so an attestation can never
1897 // claim a reference moved that did not.
1898 //
1899 // The `Result` goes in whole. Handing `objects_pushed` — which is the `Err`
1900 // arm already flattened to `&[]` for the log line above — is what made the
1901 // record claim an empty object list for every push into a repository with
1902 // no policy gates.
1903 let attested_objects = outcome.new_objects.as_deref().map_err(String::as_str);
1904 if let Some(attestation) = attestation_for(
1905 request,
1906 &outcome,
1907 attested_objects,
1908 &fingerprint,
1909 &credential,
1910 ) {
1911 tracing::info!(
1912 target: TARGET_GIT,
1913 event = "git.attestation",
1914 refs = attestation.ref_updates.len(),
1915 objects = attestation.new_object_count,
1916 // Absent from the line when the set WAS enumerated (`Option`
1917 // records nothing for `None`), so the field's presence is the fact.
1918 oids_unknown = (!attestation.new_object_oids_unknown.is_empty())
1919 .then_some(attestation.new_object_oids_unknown.as_str()),
1920 pack = %attestation.pack_hash,
1921 key_fingerprint = %attestation.key_fingerprint,
1922 // The field an HTTP push can be attributed by. `key_fingerprint`
1923 // above is empty on that transport (GUN-014).
1924 credential = %attestation.credential,
1925 "push attestation"
1926 );
1927 if let Err(err) = attestations.append(&attestation) {
1928 // the attestation's durable record failed, and the push has already been
1929 // reported to the client as done. Saying so loudly is the only
1930 // correct action; failing here would make gunnar and the client
1931 // disagree about what is in the repository.
1932 tracing::error!(
1933 target: TARGET_GIT,
1934 path = %attestations.path().display(),
1935 %err,
1936 "the push attestation could not be appended to the log; the push itself stands"
1937 );
1938 }
1939 if let Some(bus) = bus {
1940 bus.publish(event::Payload::PushAttestation(attestation));
1941 }
1942 }
1943 Ok(())
1944}
1945
1946// ───────────────────────────────────────────────────────────────────────────
1947// Measurements, and the absence of one
1948// ───────────────────────────────────────────────────────────────────────────
1949
1950/// A measurement split into **the number** and **why there isn't one**.
1951///
1952/// `tracing`'s `Value` impl for `Option<T>` records nothing at all for `None`,
1953/// so the numeric field is *absent* from the line when the measurement is,
1954/// rather than present and zero. That is the whole point. A reader who sees
1955/// `pack_bytes=0` has to be able to conclude the push carried no bytes, and
1956/// they cannot if `0` is also what a failed `stat` prints.
1957///
1958/// The same shape as `objects` / `objects_unknown` on the line above it:
1959/// one field for the fact, one for the reason it is missing, never
1960/// a plausible default standing in for either.
1961fn measured(value: Option<u64>, why: &str) -> (Option<u64>, Option<String>) {
1962 match value {
1963 Some(bytes) => (Some(bytes), None),
1964 None => (None, Some(why.to_owned())),
1965 }
1966}
1967
1968/// How many bytes the pack that landed occupies, or why that is unknown.
1969///
1970/// Three different absences used to fold into one `0`:
1971///
1972/// * the push carried no pack at all, because it was deletions only;
1973/// * the pack was indexed without a directory, so nothing was written;
1974/// * `stat` failed on a path that *was* written.
1975///
1976/// Only the first is a zero, and it is a **measured** one. The other two are
1977/// unknowns and now say so, with the path in the reason: an operator chasing a
1978/// pack that is not where the server thinks it is needs the path more than they
1979/// need a number. This is a server log, not a client-facing status line, so the
1980/// path belongs here (the reverse mistake — a server path on the wire to
1981/// `git push` — was fixed separately).
1982fn measured_pack_bytes(outcome: &PushOutcome) -> (Option<u64>, Option<String>) {
1983 let Some(pack) = outcome.pack.as_ref() else {
1984 // Deletions only: nothing was written because nothing was sent.
1985 return (Some(0), None);
1986 };
1987 let Some(path) = pack.data_path.as_deref() else {
1988 // No file — which is the ordinary case, not a degraded one: the znippy
1989 // engine appends the pack verbatim into its archive and writes no
1990 // `.pack`. The store counted what it appended, so the measurement is
1991 // right here and only an engine that reported neither is an unknown.
1992 return match pack.stored_bytes {
1993 Some(bytes) => (Some(bytes), None),
1994 None => (
1995 None,
1996 Some(format!(
1997 "pack {} was indexed without a directory and the store reported no size, so \
1998 there is nothing to measure",
1999 pack.pack_id
2000 )),
2001 ),
2002 };
2003 };
2004 match std::fs::metadata(path) {
2005 Ok(meta) => (Some(meta.len()), None),
2006 Err(err) => (
2007 None,
2008 Some(format!("{} could not be stat'ed: {err}", path.display())),
2009 ),
2010 }
2011}
2012
2013/// How many objects a push introduced, for the attestation record.
2014///
2015/// **The count never depends on the list.** `Ok(ids)` is an enumerated set and
2016/// its length is the count, empty included — a push that introduced nothing is
2017/// a measured zero. `Err(_)` is "the set was never worked out", and the count
2018/// then comes off the stored pack's own 12-byte header, which is known either
2019/// way. Zero only for a push that carried no pack at all.
2020fn attested_object_count(
2021 new_objects: std::result::Result<&[ObjectId], &str>,
2022 outcome: &PushOutcome,
2023) -> u64 {
2024 match new_objects {
2025 Ok(ids) => ids.len() as u64,
2026 Err(_) => outcome
2027 .pack
2028 .as_ref()
2029 .map(|pack| u64::from(pack.num_objects))
2030 .unwrap_or(0),
2031 }
2032}
2033
2034/// The push attestation record for one push, or `None` when nothing moved.
2035///
2036/// A push in which every reference was refused produces `PushRejected` events
2037/// and no attestation: there is no provenance to attest to, and a record with
2038/// an empty `ref_updates` would be indistinguishable from a bug in this
2039/// function.
2040fn attestation_for(
2041 request: &ServiceRequest,
2042 outcome: &PushOutcome,
2043 new_objects: std::result::Result<&[ObjectId], &str>,
2044 fingerprint: &str,
2045 credential: &str,
2046) -> Option<PushAttestation> {
2047 let ref_updates: Vec<gunnar_proto::RefUpdate> = outcome
2048 .commands
2049 .iter()
2050 .zip(&outcome.statuses)
2051 .filter(|(_, status)| status.is_ok())
2052 .map(|(command, _)| gunnar_proto::RefUpdate {
2053 ref_name: command.name.to_string(),
2054 old_oid: command.old.to_string(),
2055 new_oid: command.new.to_string(),
2056 })
2057 .collect();
2058 if ref_updates.is_empty() {
2059 return None;
2060 }
2061
2062 // The pack as gunnar stored it. `pack_hash` is that pack's own trailing
2063 // checksum — the `<id>` in `pack-<id>.pack` — in the repository's hash
2064 // kind, so it is verifiable against the file on disk. It is deliberately
2065 // NOT a hash of the bytes the client sent: `index_thin_pack` completes a
2066 // thin pack from the store before writing it, so the received bytes and
2067 // the stored pack are different objects and only one of them still exists.
2068 //
2069 // **Through `measured_pack_bytes`, not through a `stat` of its own.** This
2070 // used to `std::fs::metadata(data_path).ok()…unwrap_or(0)`, which on an
2071 // engine that writes no packfile is `None.unwrap_or(0)` — so every
2072 // attestation this server produced said the push stored zero bytes. Same
2073 // disease as the empty object list below, in the same record, one field
2074 // along. The znippy store writes no pack FILE (`data_path: None`) but it
2075 // counts the bytes it appends, and that count is exactly what
2076 // `measured_pack_bytes` was taught to prefer over an absent file — one
2077 // function, so the log line and the durable record can never disagree
2078 // about how big a push was.
2079 //
2080 // The last resort is the wire count (`PushOutcome::pack_bytes`), for an
2081 // engine that reports neither a file nor a size: a byte count that could
2082 // not be established at all stays `0` here because the proto has no third
2083 // state for it, and `new_object_oids_unknown` is the only field that can
2084 // say the record is partial. What is gone is the case where the count was
2085 // *available* and thrown away for want of a file.
2086 let (pack_hash, pack_bytes) = match &outcome.pack {
2087 Some(pack) => {
2088 let bytes = measured_pack_bytes(outcome)
2089 .0
2090 .or(outcome.pack_bytes)
2091 .unwrap_or(0);
2092 (pack.pack_id.to_string(), bytes)
2093 }
2094 // A deletions-only push carries no pack at all, and that is a fact
2095 // worth attesting rather than a reason to skip the record.
2096 None => (String::new(), 0),
2097 };
2098 // ── the object set, and the reason there isn't one ──────────────────────
2099 //
2100 // The set is not free: on the znippy engine `oids_in_extent` WAITS for the
2101 // indexer to absorb the pack, and that wait is what the deferred indexer
2102 // exists to avoid paying on the push path (`znippy_engine::ingest`'s "why
2103 // this is a flag" note). So `PushIngest` says how badly it wants the ids
2104 // rather than always asking — `ObjectSet::Required` when a policy gate
2105 // reads them (`gate.engine` non-empty), `ObjectSet::Wanted` when only this
2106 // record does, and `ObjectSet::NotNeeded` under `--no-attest-objects`.
2107 // Where the set is not produced — an operator who bought the latency back,
2108 // or an engine that cannot enumerate for a `Wanted` — the ingest says so
2109 // with `Err(reason)` rather than an empty set.
2110 //
2111 // That `Err` used to be flattened to `&[]` by the caller. The record then
2112 // carried an empty list, and an empty list is what a push of zero objects
2113 // carries too, so the two were indistinguishable — and the fix before this
2114 // one made it worse in the honest direction only halfway: the COUNT became
2115 // the pack's, so a reader saw "3 objects" beside a list of none and no word
2116 // about why. The reason travels now, in its own field, and the record says
2117 // what it knows and no more (measured, and never a plausible value where an absence belongs).
2118 let new_object_count = attested_object_count(new_objects, outcome);
2119 let (new_object_oids, new_object_oids_unknown) = match new_objects {
2120 Ok(ids) => (ids.iter().map(ObjectId::to_string).collect(), String::new()),
2121 Err(reason) => (Vec::new(), reason.to_owned()),
2122 };
2123
2124 let received_at_unix_ms = gunnar_grpc::bus::now_unix_ms();
2125 Some(PushAttestation {
2126 // Unique per push: the stored pack's identity is unique to the bytes,
2127 // and the millisecond disambiguates the same pack pushed twice (and a
2128 // deletions-only push, which has no pack at all).
2129 attestation_id: format!(
2130 "{}@{received_at_unix_ms}/{}",
2131 request.repo.key(),
2132 if pack_hash.is_empty() {
2133 "no-pack"
2134 } else {
2135 &pack_hash
2136 }
2137 ),
2138 repo: Some(RepoPath::new(&request.repo.namespace, &request.repo.name)),
2139 principal_id: request.principal.id.clone(),
2140 principal_name: request.principal.name.clone(),
2141 key_fingerprint: fingerprint.to_owned(),
2142 // Answerable on both transports, unlike the fingerprint above
2143 // (GUN-014). A verifier reading an older record sees an empty string,
2144 // which is what it saw before this field existed.
2145 credential: credential.to_owned(),
2146 hash_kind: crate::control_plane::hash_kind(request.repo.hash_kind) as i32,
2147 ref_updates,
2148 // The list is never a partial one: it is either the set the store
2149 // enumerated or empty beside a reason that says why nobody asked.
2150 // `new_object_oids_unknown` is what a reader honours; a reader that
2151 // ignores it sees a count that still came from the pack's own header
2152 // rather than from a list that was never built.
2153 new_object_count,
2154 new_object_oids,
2155 new_object_oids_unknown,
2156 new_object_bytes: pack_bytes,
2157 pack_hash,
2158 received_at_unix_ms,
2159 server_agent: gunnar_proto::AGENT.to_owned(),
2160 // The no-OS rule: git traffic has exactly one transport, and this module is the
2161 // SSH one. Not read from the request, because there is nothing else it
2162 // could be, and a field that can only ever hold one value should say
2163 // which one rather than sit empty.
2164 transport: "ssh".to_owned(),
2165 })
2166}
2167
2168/// The SHA-256 fingerprint of the key this push authenticated with, when that
2169/// can be known without guessing.
2170///
2171/// The key that authenticated this session, for the attestation and the events.
2172///
2173/// # This used to be a guess, and the guess was usually wrong
2174///
2175/// The cross-crate gap is closed: `gunnar-ssh` computes the fingerprint in
2176/// `auth_publickey`, looks the principal up by it, and now **carries it** on
2177/// [`ServiceRequest`] instead of dropping it.
2178///
2179/// What this replaced read the identity table back and answered only when the
2180/// principal held **exactly one key** — so a machine account with a rotation
2181/// key, or any human with a laptop key and a desktop key, published an empty
2182/// fingerprint on every `RefUpdated`, every `RefDeleted` and every attestation.
2183/// "Which key pushed this" was therefore unanswerable for precisely the
2184/// accounts an audit trail exists for. Reading it back could never have been
2185/// made correct: the table cannot say which of two keys made a connection.
2186///
2187/// # An empty string here does NOT mean "nobody" (GUN-014)
2188///
2189/// It used to say so: *"an empty string means no key, and now means it for one
2190/// reason only — the anonymous, read-only path, which authenticates with no key
2191/// at all. That path cannot push, so it cannot reach an attestation."* That was
2192/// false for smart HTTP. HTTP authenticates with a **token**, so there is no
2193/// key and `ServiceRequest::key_fingerprint` is `None` on every HTTP request
2194/// including `git-receive-pack` — and the empty string went into `RefUpdated`,
2195/// `RefDeleted` and the attestation with nothing else on the record naming who
2196/// pushed. Two HTTP pushes by two different people were the same audit line.
2197///
2198/// The field is still exactly what it says: the SSH key, when there was one.
2199/// The question "which credential" is [`session_credential`]'s.
2200fn session_key_fingerprint(request: &ServiceRequest) -> String {
2201 match &request.key_fingerprint {
2202 Some(fingerprint) => fingerprint.as_str().to_owned(),
2203 None => {
2204 tracing::debug!(
2205 target: TARGET_GIT,
2206 principal = %request.principal.id,
2207 "no key fingerprint on this request: the session authenticated anonymously"
2208 );
2209 String::new()
2210 }
2211 }
2212}
2213
2214/// **Which credential made this request**, as the audit trail names it:
2215/// `key:SHA256:…`, `token:<hash prefix>`, or empty for a caller that presented
2216/// none.
2217///
2218/// The field [`session_key_fingerprint`] could not be (GUN-014). It is derived
2219/// here rather than spelled by each front door so the two transports cannot
2220/// come to write the prefix differently: SSH already carries the fingerprint
2221/// and gets `key:` added to it; HTTP has no key and carries
2222/// [`ServiceRequest::credential`] instead.
2223///
2224/// Empty now genuinely has one meaning — no credential — and the anonymous read
2225/// path is the only thing that produces it, which is what the old note claimed
2226/// and this makes true.
2227fn session_credential(request: &ServiceRequest) -> String {
2228 match (&request.key_fingerprint, &request.credential) {
2229 (Some(fingerprint), _) => format!("key:{}", fingerprint.as_str()),
2230 (None, Some(credential)) => credential.clone(),
2231 (None, None) => String::new(),
2232 }
2233}
2234
2235// `pack_object_ids` stood here: it opened a written `.idx` with
2236// `gix_pack::index::File::at` and collected every oid in it. It went with
2237// `index_thin_pack`, which is the only thing that ever wrote an `.idx` for it
2238// to read — a push is a verbatim `put_pack` into the engine now, and the ids a
2239// push introduced come from `PushReceipt::new_oids` off the engine's own index.
2240//
2241// It carried the last two live bumps of `gunnar_pack::oidprobe`'s
2242// `GIX_PACK_INDEX_OPEN` / `GIX_PACK_INDEX_ITER`, which `bench/examples/
2243// oidlookup-probe.rs` reports. Deleting it changes **no measurement**: the
2244// function had no caller, so those counters were already zero in every run that
2245// could be taken. A counter that reads 0 because the path is gone is the truth;
2246// keeping an uncalled function alive to hold a probe open would be the other
2247// way round — an instrument attached to code that cannot execute.
2248
2249/// Taking a pushed pack into `objects/pack`, and saying which objects it
2250/// **introduced**.
2251///
2252/// # The subtraction, and why it is the only honest source
2253///
2254/// A gate cannot enforce a quota against a set that includes objects the
2255/// repository already held. This used to hand the gate every id in the written
2256/// `.idx`, which for a thin push is the client's objects *plus* the bases
2257/// `index_thin_pack` fetched from the store to complete it — so a
2258/// `max_objects` gate refused pushes it need not have, and no amount of
2259/// re-reading the index afterwards could tell the two apart, because by then
2260/// they are the same file.
2261///
2262/// The distinction exists in exactly one place: the `gix_object::Find` that
2263/// resolution asks for a missing base. Every id it is asked for and finds is,
2264/// by definition, an object the repository already had. So the lookup is
2265/// wrapped, the ids it served are recorded, and
2266///
2267/// ```text
2268/// introduced = everything in the .idx − the bases we handed back
2269/// ```
2270///
2271/// is exact rather than conservative, and needs no second enumeration of
2272/// anything (one implementation).
2273/// **Where a pushed or fetched pack is framed**: the box's transaction log, and the catalog
2274/// key and hash kind the twin's apply opens the repository by (`gunnar_core::txlog`).
2275#[derive(Clone)]
2276pub(crate) struct PackFrame {
2277 log: Arc<gunnar_core::txlog::Log>,
2278 repo: String,
2279 hash: String,
2280}
2281
2282impl PackFrame {
2283 /// The frame for `repo` on `stores`.
2284 pub(crate) fn of(stores: &crate::stores::ServerStores, repo: &gunnar_core::Repo) -> PackFrame {
2285 PackFrame {
2286 log: Arc::clone(stores.txlog()),
2287 repo: repo.key(),
2288 hash: repo.hash_kind.to_string(),
2289 }
2290 }
2291
2292 /// These exact bytes, as a `Pack` frame, durable before this returns.
2293 fn append(&self, bytes: &[u8]) -> anyhow::Result<()> {
2294 let meta = gunnar_core::txlog::Meta::Pack {
2295 repo: self.repo.clone(),
2296 hash: self.hash.clone(),
2297 };
2298 self.log
2299 .append(gunnar_core::txlog::Kind::Pack, &meta, bytes)?;
2300 Ok(())
2301 }
2302}
2303
2304pub(crate) struct PushIngest<'a> {
2305 git_dir: PathBuf,
2306 hash_kind: gix_hash::Kind,
2307 /// Which engine stores this push. Carried on the value rather than read
2308 /// from a global, because the process's engine is a field on
2309 /// [`ServerStores`](crate::stores::ServerStores) now and a push is served
2310 /// out of one of those.
2311 engine: crate::storage::Engine,
2312 /// How badly this push needs the object set it introduced: a policy gate
2313 /// that reads the objects cannot decide without it, the push attestation
2314 /// would rather have it than apologise for it, and an operator can decline
2315 /// it altogether. Asking for it waits on index work the ack path defers on
2316 /// purpose, which is why this is three answers and not a `bool`.
2317 objects: crate::storage::ObjectSet,
2318 /// **Where the running object count goes, or `None`.**
2319 ///
2320 /// The numerator of a transfer's progress, and the reason it is on the
2321 /// *ingest* rather than on `gunnar-wire`'s `FetchProgress` beside the
2322 /// denominator: the count exists only inside the engine's pack walk, and
2323 /// there is exactly one of those (one implementation). `fetch_pack` reads the header and
2324 /// can publish `objects_total` because twelve bytes are all that costs;
2325 /// counting the objects themselves would be a second decoder, which its own
2326 /// module doc refuses. So the two halves of one progress pair are published
2327 /// from two places, and this is the half storage owns.
2328 watching: Option<&'a dyn git_storage_trait::PackProgress>,
2329 /// **The transaction log this pack is a frame in**, or `None` (a test, a tool). Handed
2330 /// to `Engine::ingest_push` as `before_store`: the pack's exact bytes are a `Pack` frame
2331 /// before the engine stores them — this path never passes the `OpenRepo::logged` tee.
2332 frame: Option<PackFrame>,
2333 /// **An encrypted repository's objects, or `None`** (crypt-level2 item 4,
2334 /// 2026-10-06): when set, the pushed pack is checked in quarantine
2335 /// ([`crate::encrypted_quarantine`]) BEFORE it is framed or stored — these
2336 /// are the bases a thin pack deltas against. A refusal keeps nothing.
2337 encrypted: Option<&'a dyn git_storage_trait::GitServe>,
2338 /// **The data set's space ledger**, or `None` (a test, a tool, a server
2339 /// whose doors were never wired): every chunk of the pack is admitted
2340 /// against it BEFORE it is read, and a pack that does not fit beside what
2341 /// is already arriving is refused by name — never failed by a full disk
2342 /// mid-receive (cov-13, [`crate::push_space`]).
2343 space: Option<Arc<gunnar_ssh::SpaceLedger>>,
2344}
2345
2346impl<'a> PushIngest<'a> {
2347 /// **The ingest a MIRROR lands its fetched pack through** — this one, the
2348 /// push's own, and not a second sink.
2349 ///
2350 /// `fetch_pack`'s module doc states the principle it is built on: *a
2351 /// fetched pack and a pushed pack are the same bytes*, so both land through
2352 /// the identical [`gunnar_wire::PackIngest`] and znippy implements it once.
2353 /// A private constructor rather than a public struct because the fields are
2354 /// this module's business; what a mirror needs to say is the four facts
2355 /// below.
2356 pub(crate) fn new(
2357 git_dir: PathBuf,
2358 hash_kind: gix_hash::Kind,
2359 engine: crate::storage::Engine,
2360 objects: crate::storage::ObjectSet,
2361 watching: Option<&'a dyn git_storage_trait::PackProgress>,
2362 frame: Option<PackFrame>,
2363 ) -> Self {
2364 PushIngest {
2365 git_dir,
2366 hash_kind,
2367 engine,
2368 objects,
2369 watching,
2370 frame,
2371 // A mirror's fetched pack is not checked here: the repository it
2372 // lands in is the mirror's own (an encrypted repository is not a
2373 // mirror target in Level 1).
2374 encrypted: None,
2375 space: None,
2376 }
2377 }
2378
2379 /// Hold the pack to the data set's room — `stores.space_ledger()`.
2380 pub(crate) fn with_space(mut self, space: Option<Arc<gunnar_ssh::SpaceLedger>>) -> Self {
2381 self.space = space;
2382 self
2383 }
2384
2385 /// **The push, stored without a packfile.**
2386 ///
2387 /// The bytes go from the wire into the znippy engine's own storage and nothing
2388 /// is written to `objects/pack` — no `.pack`, no `.idx`, no `.keep`. That is
2389 /// the whole point of it, and it is why this does not call
2390 /// [`index_thin_pack`]: `gix_pack::Bundle::write_to_directory` takes a
2391 /// directory, so every route through it puts a git packfile on disk before
2392 /// the tier ever sees the bytes, and the tier's copy is then a second one.
2393 ///
2394 /// # What is given up, said out loud
2395 ///
2396 /// `index_thin_pack` **resolved** the pack: it completed thin deltas against
2397 /// the store and produced a real `.idx`. This does not. The closure check
2398 /// inside `put_pack` refuses a pack whose bases are missing, which is the
2399 /// correctness half, but the pack stays thin and stays verbatim — the whole
2400 /// claim of *verbatim is the truth*. A reader resolves chains on the way
2401 /// out instead.
2402 ///
2403 /// # The `.keep` that is not there
2404 ///
2405 /// `index_thin_pack` wrote one and the caller dropped it once the refs
2406 /// landed, guarding the new pack from a concurrent `gc` in the window
2407 /// between. There is no window here: `put_pack` returns only after the
2408 /// journal row is fsynced, and `gc` reads that journal. The marker's job is
2409 /// done by the ordering rather than by a file.
2410 fn ingest_engine(&self, pack: &mut dyn BufRead) -> gunnar_wire::Result<PackReceipt> {
2411 // ── space admission (cov-13) ─────────────────────────────────────
2412 //
2413 // The promise this pack holds on /data: spool + store, + the log's
2414 // frame when it is framed. Given back copy by copy as each lands (the
2415 // spool at the reader's `consume`, the frame in the hook below, the
2416 // store when this function returns and the hold drops).
2417 let hold = self.space.as_ref().map(|ledger| {
2418 ledger.hold(if self.frame.is_some() {
2419 gunnar_ssh::PUSH_COPIES
2420 } else {
2421 gunnar_ssh::PUSH_COPIES - 1
2422 })
2423 });
2424 let mut gated = None;
2425 let reader: &mut dyn BufRead = match &hold {
2426 Some(hold) => gated.insert(crate::push_space::Gated::new(
2427 pack,
2428 hold,
2429 crate::storage::SPOOL_ABOVE as u64,
2430 )),
2431 None => pack,
2432 };
2433 let hook = |bytes: &[u8]| -> anyhow::Result<()> {
2434 // ★ Before the frame and before the store: an encrypted
2435 // repository's pack is checked in quarantine, and a refusal keeps
2436 // nothing — not on this disk, not in the log the twin replays.
2437 #[cfg(feature = "engine-znippy")]
2438 if let Some(repo) = self.encrypted {
2439 let checked = crate::encrypted_quarantine::check(bytes, self.hash_kind, repo)?;
2440 tracing::info!(
2441 target: TARGET_STORE,
2442 event = "store.encrypted.quarantine_passed",
2443 objects = checked,
2444 "an encrypted repository's pushed pack passed its check in quarantine; it is framed and stored now"
2445 );
2446 }
2447 match &self.frame {
2448 Some(frame) => {
2449 frame.append(bytes)?;
2450 // On the disk now: the frame's promise is `statvfs`'s.
2451 if let Some(hold) = &hold {
2452 hold.realize(bytes.len() as u64);
2453 }
2454 Ok(())
2455 }
2456 None => Ok(()),
2457 }
2458 };
2459 let hooked = self.frame.is_some() || self.encrypted.is_some();
2460 // The mutation gate spans frame through store (`Log::mutation`): a snapshot's freeze
2461 // waits for this pack to be in the engine before it reads the head.
2462 let _framed = self.frame.as_ref().map(|f| f.log.mutation());
2463 let stored = self.engine.ingest_push(
2464 self.git_dir.as_path(),
2465 self.hash_kind,
2466 reader,
2467 self.objects,
2468 self.watching,
2469 hooked.then_some(&hook as &dyn Fn(&[u8]) -> anyhow::Result<()>),
2470 );
2471 // ★ Cut for room: the ledger's line, verbatim, whatever the ingest made
2472 // of the cut stream. Its spool is already gone (the failed read dropped
2473 // it), nothing was framed, nothing stored, and its promise is given back.
2474 let consumed_by_cut = gated
2475 .as_ref()
2476 .map(|g| (g.cut().map(str::to_owned), g.consumed()));
2477 drop(gated);
2478 if let Some((Some(line), sent)) = consumed_by_cut.clone() {
2479 tracing::warn!(
2480 target: TARGET_STORE,
2481 event = "store.push.no_room",
2482 pack_bytes = sent,
2483 refusal = %line,
2484 "a pushed pack was refused for room mid-receive: it did not fit beside the \
2485 pushes already arriving; its spool is deleted and nothing of it was kept"
2486 );
2487 return Err(gunnar_wire::Error::Refused(line));
2488 }
2489 let received = stored
2490 .map_err(|err| {
2491 // ★ The disk filled ANYWAY (a writer outside the ledger): said in
2492 // the ledger's words, never `uring: op failed: Operation canceled`.
2493 if let Some(hold) = &hold {
2494 let freed = consumed_by_cut.as_ref().map_or(0, |(_, sent)| *sent);
2495 if crate::push_space::is_space_failure(&err, || hold.at_the_floor(freed)) {
2496 let line = hold.full_now();
2497 tracing::warn!(
2498 target: TARGET_STORE,
2499 event = "store.push.disk_full",
2500 error = %format!("{err:#}"),
2501 refusal = %line,
2502 "the store failed a pushed pack because the data set filled; told by name"
2503 );
2504 return gunnar_wire::Error::Refused(line);
2505 }
2506 }
2507 #[cfg(feature = "engine-znippy")]
2508 if let Some(refused) = crate::encrypted_quarantine::refusal_in(&err) {
2509 tracing::info!(
2510 target: TARGET_STORE,
2511 event = "store.encrypted.quarantine_refused",
2512 reason = %refused,
2513 "an encrypted repository's pushed pack was refused in quarantine: nothing was framed or stored"
2514 );
2515 return gunnar_wire::Error::Refused(refused);
2516 }
2517 // `{err:#}`, not `{err}`. `anyhow`'s plain `Display` prints the
2518 // OUTERMOST context and nothing under it, so a failure three
2519 // `.context()` calls deep arrived as "storing the pushed pack:
2520 // enumerating the objects this push introduced" — the two
2521 // labels this code added, and never the cause the store gave.
2522 // The alternate form joins the whole chain, which is the only
2523 // version of this line an operator can act on.
2524 gunnar_wire::Error::Store(gunnar_store::Error::backend(format!(
2525 "storing the pushed pack: {err:#}"
2526 )))
2527 })?;
2528
2529 let pack_id = gix_hash::ObjectId::try_from(received.pack_id.as_slice()).map_err(|_| {
2530 gunnar_wire::Error::Store(gunnar_store::Error::backend(format!(
2531 "the stored pack's trailer is {} bytes, which is not a {} object id",
2532 received.pack_id.len(),
2533 self.hash_kind
2534 )))
2535 })?;
2536
2537 tracing::info!(
2538 target: TARGET_STORE,
2539 // Renamed from `store.cold_tier.ingested` 2026-08-14. The cold tier
2540 // was purged and this event outlived it, so every push in the log
2541 // still announced a tier that does not exist. Nothing asserted on
2542 // the old name — only a doc-comment example, corrected with it.
2543 event = "store.znippy.ingested",
2544 objects = received.num_objects,
2545 bytes = received.pack_bytes,
2546 elapsed_ms = received.elapsed_ms,
2547 object_set = received.new_oids.is_some(),
2548 "the push was stored in the znippy-git store; no packfile was written"
2549 );
2550
2551 let new_objects = match received.new_oids {
2552 Some(oids) => oids
2553 .iter()
2554 .map(|raw| {
2555 gix_hash::ObjectId::try_from(raw.as_slice())
2556 .map_err(|_| format!("the store returned a {}-byte object id", raw.len()))
2557 })
2558 .collect::<std::result::Result<Vec<_>, _>>(),
2559 // Not "no objects" — see `ColdReceipt::new_oids`. A gate reading
2560 // this refuses, which is the correct answer to "I was never asked
2561 // to find out".
2562 None => Err(
2563 "the policy has no gate over objects, so the object set was not \
2564 enumerated"
2565 .to_owned(),
2566 ),
2567 };
2568
2569 Ok(PackReceipt {
2570 indexed: gunnar_wire::pack::IndexedPack {
2571 num_objects: received.num_objects,
2572 pack_id,
2573 // There is no index file, and `ObjectId::null` is this tree's
2574 // existing spelling for that — the same value the git-store
2575 // bench server and the store's own end-to-end test use.
2576 index_id: gix_hash::ObjectId::null(self.hash_kind),
2577 pack_version: gunnar_wire::pack::PackVersion::from_u32(received.version),
2578 hash_kind: self.hash_kind,
2579 // The three paths that no longer exist. `None` is not a
2580 // degraded answer here: it is the fact, and `cold_tier_append`
2581 // used to read exactly these two to build its second copy.
2582 index_path: None,
2583 data_path: None,
2584 keep_path: None,
2585 // …but the SIZE is not missing with them. The store counted the
2586 // bytes it appended, and until this line nobody carried the
2587 // number out, so `measured_pack_bytes` fell through to "there
2588 // is nothing on disk to measure" on every push into the engine
2589 // the appliance ships. There is no file; there is a
2590 // measurement. Nothing on disk has to be stat'ed to learn it,
2591 // and on this engine nothing on disk could be.
2592 stored_bytes: Some(received.pack_bytes),
2593 },
2594 new_objects,
2595 })
2596 }
2597}
2598
2599impl gunnar_wire::PackIngest for PushIngest<'_> {
2600 /// Straight into the engine. There is no second arm.
2601 ///
2602 /// What stood beside this ran `index_thin_pack` — `gix_pack::Bundle::
2603 /// write_to_directory` — writing a real `.pack`, `.idx` and `.keep` into
2604 /// `objects/pack`, reading the `.idx` back to learn which objects arrived,
2605 /// and dropping the `.keep` afterwards. Every line of it was the packfile
2606 /// engine's, and the engine is amputated.
2607 fn ingest(&self, pack: &mut dyn BufRead) -> gunnar_wire::Result<PackReceipt> {
2608 self.ingest_engine(pack)
2609 }
2610}
2611
2612/// Did this reference move to a tip that does **not** descend from the one it
2613/// held? The event stream's `RefUpdated.forced`.
2614///
2615/// # This is one ancestry walk, not a second one
2616///
2617/// [`gunnar_policy::ancestry::is_descendant_of`] is the same function
2618/// `BranchProtectionGate` decides a non-fast-forward with, and it is the one
2619/// cross-checked against `git merge-base --is-ancestor` in that crate's tests.
2620/// Re-deriving the answer here — `old != new`, a hex compare, a first-parent
2621/// walk — is the mistake that gate's module docs enumerate, and two
2622/// implementations that disagree would put a UI and a policy on different sides
2623/// of the same push (one implementation).
2624///
2625/// The gate cannot simply be asked, because it only runs where a protection
2626/// rule matches. The audit trail has to answer for **every** reference on every
2627/// repository, including the ones nothing protects, which is the case where
2628/// nobody is watching.
2629///
2630/// # An undecidable walk reports `true`
2631///
2632/// A missing object or an exhausted budget is not evidence of a fast-forward.
2633/// `BranchProtectionGate` refuses on undecided rather than allowing; the same
2634/// judgement here means flagging rather than clearing, with the reason logged at
2635/// `warn`. Reporting `false` for a walk that never finished is exactly the
2636/// wrong-answer this field is being fixed for.
2637fn was_forced(git: &dyn GitServe, command: &RefCommand) -> bool {
2638 // A create has no previous tip, and a delete has no new one. Neither is a
2639 // rewrite of anything.
2640 if command.is_create() || command.is_delete() {
2641 return false;
2642 }
2643 match gunnar_policy::ancestry::is_descendant_of(
2644 git,
2645 &command.new,
2646 &command.old,
2647 gunnar_policy::ancestry::DEFAULT_ANCESTRY_BUDGET,
2648 ) {
2649 Ok(descends) => !descends,
2650 Err(err) => {
2651 tracing::warn!(
2652 target: TARGET_POLICY,
2653 ref_name = %command.name,
2654 old = %command.old,
2655 new = %command.new,
2656 %err,
2657 "the ancestry walk behind RefUpdated.forced could not be completed; the update \
2658 is published as forced, because `not forced` is a claim this could not make"
2659 );
2660 true
2661 }
2662 }
2663}
2664
2665#[allow(clippy::too_many_arguments)]
2666fn publish(
2667 bus: &EventBus,
2668 request: &ServiceRequest,
2669 command: &RefCommand,
2670 status: &RefStatus,
2671 atomic: bool,
2672 fingerprint: &str,
2673 credential: &str,
2674 gates: &HashMap<String, String>,
2675 forced: bool,
2676) {
2677 let repo = Some(RepoPath::new(&request.repo.namespace, &request.repo.name));
2678 let payload = match status {
2679 RefStatus::Ok if command.is_delete() => event::Payload::RefDeleted(RefDeleted {
2680 repo,
2681 ref_name: command.name.to_string(),
2682 old_oid: command.old.to_string(),
2683 principal_id: request.principal.id.clone(),
2684 key_fingerprint: fingerprint.to_owned(),
2685 credential: credential.to_owned(),
2686 }),
2687 RefStatus::Ok => event::Payload::RefUpdated(RefUpdated {
2688 repo,
2689 ref_name: command.name.to_string(),
2690 old_oid: command.old.to_string(),
2691 new_oid: command.new.to_string(),
2692 principal_id: request.principal.id.clone(),
2693 key_fingerprint: fingerprint.to_owned(),
2694 credential: credential.to_owned(),
2695 // Answered by [`was_forced`], not assumed. This was a hardcoded
2696 // `false`, which made every force-push on the server
2697 // indistinguishable from an ordinary one on `Events.Watch`.
2698 forced,
2699 }),
2700 RefStatus::Rejected(reason) => event::Payload::PushRejected(PushRejected {
2701 repo,
2702 ref_name: command.name.to_string(),
2703 // Empty when nothing named a gate: a connectivity failure, a
2704 // compare-and-swap that lost, or the collateral refusal every other
2705 // reference gets in an `atomic` push. Those are real refusals and
2706 // they belong on the stream, but attributing them to a gate that
2707 // never ran would be a lie a UI would repeat.
2708 gate: gates
2709 .get(&command.name.to_string())
2710 .cloned()
2711 .unwrap_or_default(),
2712 reason: reason.clone(),
2713 principal_id: request.principal.id.clone(),
2714 whole_push_rejected: atomic,
2715 }),
2716 };
2717 bus.publish(payload);
2718}
2719
2720// ───────────────────────────────────────────────────────────────────────────
2721// the policy seam
2722// ───────────────────────────────────────────────────────────────────────────
2723
2724/// `gunnar-wire`'s [`PushGate`] over `gunnar-policy`'s [`PolicyEngine`].
2725struct PolicyGate {
2726 engine: PolicyEngine,
2727 /// Why this repository's policy could not be read, if it could not be.
2728 ///
2729 /// `Some` makes [`PolicyGate::check`] refuse **every** reference (GUN-007).
2730 /// An unreadable policy used to become `PolicyConfig::default()`, and an
2731 /// empty engine is let through by the `is_empty()` branch below — so a
2732 /// document with a typo, or a storage fault, quietly turned branch
2733 /// protection and `require_signature` off for the duration. This is the
2734 /// field that keeps "no gates are configured" and "the gates could not be
2735 /// read" from being the same value.
2736 unreadable: Option<String>,
2737 principal: gunnar_policy::Principal,
2738 repo: RepoId,
2739 keys: Box<dyn KeyDirectory>,
2740 metrics: Arc<ServerMetrics>,
2741 policy_path: PathBuf,
2742 /// Reference name → the `Gate::name()` that refused it.
2743 ///
2744 /// `PushGate::check` may only answer `Err(String)`, so the *name* of the
2745 /// gate that said no is lost at that boundary; it survives only inside the
2746 /// `Decision`. The event stream's `PushRejected.gate` needs it, so it is recorded here
2747 /// on the way past rather than parsed back out of the reason string; a
2748 /// reason is written for a human and its shape is free to change.
2749 refused_by: Mutex<HashMap<String, String>>,
2750}
2751
2752/// `gunnar-core`'s principal kind as `gunnar-policy` names it.
2753///
2754/// # The fourth arm, which must never be taken
2755///
2756/// A [`PolicyGate`] is built only for a **push**, and a push needs
2757/// [`Level::Write`](gunnar_core::Level::Write), which
2758/// `gunnar_core::AuthTable::may` refuses to the anonymous principal
2759/// unconditionally, before `Dispatch::authorise` returns and long before this
2760/// runs. So the anonymous arm is unreachable through every route that exists.
2761///
2762/// It is loud rather than plausible. `gunnar-policy` has three kinds and none of
2763/// them means "nobody"; quietly answering `User` would put an anonymous push
2764/// through gates written about humans, and do it silently. If this line is ever
2765/// reached, the ACL has stopped being consulted before the gate. That is a
2766/// security incident, and it reads like one in the log.
2767pub(crate) fn policy_principal_kind(
2768 kind: gunnar_core::PrincipalKind,
2769) -> gunnar_policy::PrincipalKind {
2770 match kind {
2771 gunnar_core::PrincipalKind::User => gunnar_policy::PrincipalKind::User,
2772 gunnar_core::PrincipalKind::Machine => gunnar_policy::PrincipalKind::Machine,
2773 gunnar_core::PrincipalKind::Deploy => gunnar_policy::PrincipalKind::Deploy,
2774 // A group holds no keys and is never authenticated as (punkt 9), so a
2775 // push can never be attributed to one — the same unreachable-by-design
2776 // shape as the anonymous arm below, and loud for the same reason: a
2777 // group at the pre-receive gate means a keyless identity was handed a
2778 // push, which is the ACL being bypassed.
2779 gunnar_core::PrincipalKind::Group => {
2780 tracing::error!(
2781 target: gunnar_core::TARGET_AUDIT,
2782 event = "policy.gate.group_push",
2783 "a GROUP principal reached the pre-receive gate. A group holds no keys and \
2784 cannot authenticate, so this cannot happen unless the grant check has been \
2785 bypassed. Treating it as a machine identity for the gate; investigate \
2786 immediately."
2787 );
2788 debug_assert!(
2789 false,
2790 "a group principal reached the pre-receive gate (punkt 9)"
2791 );
2792 gunnar_policy::PrincipalKind::Machine
2793 }
2794 gunnar_core::PrincipalKind::Anonymous => {
2795 tracing::error!(
2796 target: gunnar_core::TARGET_AUDIT,
2797 event = "policy.gate.anonymous_push",
2798 "an ANONYMOUS principal reached the pre-receive gate. Anonymous access is \
2799 read-only and the ACL refuses it Write, so this cannot happen unless the \
2800 grant check has been bypassed. Treating it as a machine identity for the \
2801 gate; investigate immediately."
2802 );
2803 debug_assert!(false, "an anonymous principal reached the pre-receive gate");
2804 gunnar_policy::PrincipalKind::Machine
2805 }
2806 }
2807}
2808
2809impl PolicyGate {
2810 fn open(
2811 git_dir: &Path,
2812 request: &ServiceRequest,
2813 stores: &ServerStores,
2814 metrics: Arc<ServerMetrics>,
2815 ) -> Self {
2816 let policy_path = git_dir.join(POLICY_FILE);
2817 let (config, unreadable) = match load_policy(stores, &request.repo, &policy_path) {
2818 Ok(config) => (config, None),
2819 // An empty engine on purpose: nothing should be evaluated against
2820 // a policy that could not be read, and `check` refuses before it
2821 // would be.
2822 Err(why) => (PolicyConfig::default(), Some(why)),
2823 };
2824 // An ENCRYPTED repository adds the plaintext check to whatever policy
2825 // it has: the flag is the account admin's, not the policy file's, so
2826 // no policy edit can switch it off.
2827 let mut gates = config.gates();
2828 if request.repo.encrypted {
2829 gates.push(Box::new(gunnar_policy::EncryptedGate::new()));
2830 }
2831 PolicyGate {
2832 engine: PolicyEngine::new(gates),
2833 unreadable,
2834 principal: gunnar_policy::Principal {
2835 id: request.principal.id.clone(),
2836 name: request.principal.name.clone(),
2837 kind: policy_principal_kind(request.principal.kind),
2838 },
2839 repo: RepoId::new(&request.repo.namespace, &request.repo.name),
2840 keys: Box::new(key_directory(stores)),
2841 metrics,
2842 policy_path,
2843 refused_by: Mutex::new(HashMap::new()),
2844 }
2845 }
2846
2847 /// Which gate refused each reference, for the event-stream `PushRejected` events.
2848 ///
2849 /// Read after `ReceivePack::serve` has returned, so every `check` that was
2850 /// going to happen has happened.
2851 fn gate_that_refused(&self) -> HashMap<String, String> {
2852 self.refused_by
2853 .lock()
2854 .expect("the refusing-gate lock")
2855 .clone()
2856 }
2857}
2858
2859impl PushGate for PolicyGate {
2860 fn check(
2861 &self,
2862 command: &RefCommand,
2863 facts: &PushFacts<'_>,
2864 ) -> std::result::Result<(), String> {
2865 // One span per gate evaluation. It is a child of
2866 // `git.receive_pack`, so the connection, principal and repository come
2867 // for free; what is new is the reference and how long deciding it took.
2868 // Branch protection walks ancestry, and an ancestry walk on a large
2869 // repository is one of the two places a push spends unexplained
2870 // seconds.
2871 let span = tracing::debug_span!(
2872 target: TARGET_POLICY,
2873 "policy.gate",
2874 ref_name = %command.name,
2875 );
2876 let _entered = span.enter();
2877 let started = std::time::Instant::now();
2878 // ── the policy could not be READ, which is not "there is none" ─────
2879 //
2880 // First, before `is_empty()`, because `is_empty()` is the branch that
2881 // lets a push through and an unreadable policy produced exactly that
2882 // value (GUN-007). A pre-receive refusal costs pushes and nothing
2883 // else: clone, fetch and browse are untouched, and the operator is
2884 // told which document and why.
2885 if let Some(why) = &self.unreadable {
2886 self.metrics.add_gate_refusals(1);
2887 tracing::error!(
2888 target: TARGET_POLICY,
2889 event = "policy.gate.unreadable",
2890 ref_name = %command.name,
2891 %why,
2892 "the policy for this repository could not be read, so every gate refuses"
2893 );
2894 return Err(format!(
2895 "gunnar could not read the policy for this repository, so it refuses this push \
2896 rather than serving it with no gates at all: {why}. Reads are unaffected; fix \
2897 the document (or `gunnar control policy set-protection`) and push again."
2898 ));
2899 }
2900 if self.engine.is_empty() {
2901 // No gates are configured, so there is nothing to decide. Said out
2902 // loud rather than reached by an engine that happens to return an
2903 // empty decision, because "allowed by every gate" and "there are no
2904 // gates" are different facts.
2905 return Ok(());
2906 }
2907 // A gate that cannot see what was pushed cannot decide, and an
2908 // undecidable gate refuses (the pre-receive gate). This is the branch that keeps a
2909 // configured limit from being silently satisfied by an empty object set.
2910 let new_objects = match facts.new_objects {
2911 Ok(ids) => ids,
2912 // No live gate reads the set, so its absence decides nothing.
2913 // The empty slice is never read: `reads_new_objects()` is exactly
2914 // the question "would any gate look at it".
2915 Err(_) if !self.engine.reads_new_objects() => &[],
2916 Err(reason) => {
2917 self.metrics.add_gate_refusals(1);
2918 tracing::error!(
2919 target: TARGET_POLICY,
2920 event = "policy.gate.undecidable",
2921 %reason,
2922 "the pushed objects could not be enumerated, so every configured gate refuses"
2923 );
2924 return Err(format!(
2925 "gunnar could not enumerate the pushed objects, so the policy gates in {} \
2926 could not decide: {reason}",
2927 self.policy_path.display()
2928 ));
2929 }
2930 };
2931 let updates = [RefUpdate::from_wire(
2932 command.name.clone(),
2933 command.old,
2934 command.new,
2935 )];
2936 let mut ctx = PushContext::new(
2937 &self.principal,
2938 &self.repo,
2939 &updates,
2940 new_objects,
2941 facts.objects,
2942 self.keys.as_ref(),
2943 );
2944 // The byte count reaches the gate. Without it
2945 // `LimitsGate` refuses any push at all whenever `max_pack_bytes` is
2946 // configured, because it will not pretend an unmeasured pack is under a
2947 // limit.
2948 if let Some(bytes) = facts.pack_bytes {
2949 ctx = ctx.with_pack_bytes(bytes);
2950 }
2951 // The push options reach the gate. Unconditionally, including the empty
2952 // slice: `Some(&[])` is "the client sent none" and `None` is "nobody
2953 // looked", and `PushOptionsGate` refuses the second. Before this line
2954 // the whole tree's only consumer of a parsed push option was `.len()`
2955 // in the log field twenty lines below, so `push-options` was advertised
2956 // on the wire and discarded.
2957 ctx = ctx.with_push_options(facts.push_options);
2958 let decision = self.engine.evaluate(&ctx);
2959 let elapsed_ms = started.elapsed().as_millis() as u64;
2960 if decision.is_allowed() {
2961 tracing::debug!(
2962 target: TARGET_POLICY,
2963 event = "policy.gate.allowed",
2964 gates = self.engine.gate_names().len(),
2965 new_objects = new_objects.len(),
2966 elapsed_ms,
2967 "every gate allowed this reference"
2968 );
2969 Ok(())
2970 } else {
2971 // `evaluate` runs every gate, so a reference can be refused by more
2972 // than one. The first in gate order is the one named, and the
2973 // summary leads with the same one, so a UI and the client's `ng`
2974 // line agree about who said no.
2975 self.metrics.add_gate_refusals(1);
2976 if let Some(first) = decision.rejections.first() {
2977 // **`warn!`, and it NAMES THE GATE.** The audit trail's `Verify` asks for
2978 // exactly one error line per refused push naming the gate and
2979 // the reference; before this, a refused push produced a
2980 // `PushRejected` event with an empty `gate` field and no log
2981 // line at all that said who refused it.
2982 tracing::warn!(
2983 target: TARGET_POLICY,
2984 event = "policy.gate.refused",
2985 gate = %first.gate,
2986 reason = %first.reason,
2987 refused_by_gates = decision.rejections.len(),
2988 elapsed_ms,
2989 "a policy gate refused this reference"
2990 );
2991 self.refused_by
2992 .lock()
2993 .expect("the refusing-gate lock")
2994 .insert(command.name.to_string(), first.gate.clone());
2995 } else {
2996 // `is_allowed()` said no and named nobody. That is a bug in the
2997 // engine, not a refusal, and it must not read as a normal one.
2998 tracing::error!(
2999 target: TARGET_POLICY,
3000 event = "policy.gate.refused",
3001 gate = "",
3002 elapsed_ms,
3003 "the policy engine refused a reference without naming a gate"
3004 );
3005 }
3006 Err(decision.summary())
3007 }
3008 }
3009}
3010
3011/// Read the repository's policy document.
3012///
3013/// **`Err` is not an empty policy** (GUN-007). Until 2026-09-11 every failure
3014/// here — a document that did not parse, a control store that could not be
3015/// read, a file that could not be opened — returned `PolicyConfig::default()`,
3016/// and `PolicyGate::check` lets an empty engine through because "no gates are
3017/// configured" is a real and common answer. So a typo in a policy document, or
3018/// a transient storage fault, silently switched branch protection and the
3019/// signature requirement OFF for that push, at `error!` level in a log nobody
3020/// was reading at the time.
3021///
3022/// The old note here said refusing would be worse, because a typo would take
3023/// the repository offline. It does not: this is the PRE-RECEIVE gate, so a
3024/// refusal costs pushes and nothing else — clone, fetch, browse and every read
3025/// are untouched, and the operator is told which document and why. Degraded
3026/// operation must not be indistinguishable from full operation, which is the
3027/// same judgement `ancestry` already makes when a walk cannot finish.
3028///
3029/// `Ok(PolicyConfig::default())` survives for exactly one case and it is a
3030/// fact rather than a fault: nothing is stored and no file exists, so nothing
3031/// has been configured.
3032/// **The control store first, the file beside the repository second.**
3033///
3034/// `POLICY_FILE`'s constant used to carry the note *"When the catalog grows a
3035/// policy document, this constant is the only thing that changes."* The catalog
3036/// has grown one — `gunnar_core::PolicyStore`, at `refs/gunnar/policy` — and
3037/// this is that change. What did **not** change is the file: a repository
3038/// configured before the store existed keeps working, and an operator who edits
3039/// the JSON on disk still gets what they edited, until they write a policy over
3040/// the control plane and the stored one takes over.
3041///
3042/// The order is deliberate and is the only one that can be right. The stored
3043/// document is the one `Policy.SetProtection` writes and the one an operator
3044/// can see over gRPC; if a stale file could shadow it, turning protection on
3045/// through the console would appear to work and change nothing.
3046pub(crate) fn load_policy(
3047 stores: &ServerStores,
3048 repo: &gunnar_core::Repo,
3049 path: &Path,
3050) -> std::result::Result<PolicyConfig, String> {
3051 match stores.policies().get(&repo.path()) {
3052 Ok(Some(document)) => match PolicyConfig::from_json(&document) {
3053 Ok(config) => return Ok(config),
3054 Err(err) => {
3055 tracing::error!(
3056 target: TARGET_POLICY,
3057 repo = %repo.key(),
3058 source = "control-store",
3059 %err,
3060 "the stored policy document did not parse; pushes to this repository are \
3061 refused until it does"
3062 );
3063 return Err(format!("the stored policy document did not parse: {err}"));
3064 }
3065 },
3066 // Nothing stored. The file below is the pre-store shape and is still
3067 // the answer for a repository configured before `PolicyStore` existed.
3068 Ok(None) => {}
3069 Err(err) => {
3070 // NOT a fall-through to the file any more. The file "may be older
3071 // than what an operator configured" — its own words — so serving
3072 // it when the authoritative store is unreachable is a silent
3073 // downgrade to a policy nobody chose.
3074 tracing::error!(
3075 target: TARGET_POLICY,
3076 repo = %repo.key(),
3077 %err,
3078 "the policy store could not be read; pushes to this repository are refused \
3079 rather than gated by a file that may be older than what an operator \
3080 configured"
3081 );
3082 return Err(format!("the policy store could not be read: {err}"));
3083 }
3084 }
3085 let text = match std::fs::read_to_string(path) {
3086 Ok(text) => text,
3087 // The one `Ok(default)`: nothing stored and no file. Nothing has been
3088 // configured, which is a fact and not a fault.
3089 Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
3090 return Ok(PolicyConfig::default())
3091 }
3092 Err(err) => {
3093 tracing::error!(
3094 target: TARGET_POLICY,
3095 path = %path.display(),
3096 %err,
3097 "the policy document could not be read; pushes to this repository are refused"
3098 );
3099 return Err(format!(
3100 "the policy document at {} could not be read: {err}",
3101 path.display()
3102 ));
3103 }
3104 };
3105 match PolicyConfig::from_json(&text) {
3106 Ok(config) => Ok(config),
3107 Err(err) => {
3108 tracing::error!(
3109 target: TARGET_POLICY,
3110 path = %path.display(),
3111 %err,
3112 "the policy document did not parse; pushes to this repository are refused until \
3113 it does"
3114 );
3115 Err(format!(
3116 "the policy document at {} did not parse: {err}",
3117 path.display()
3118 ))
3119 }
3120 }
3121}
3122
3123/// Every key gunnar knows **that has not expired**, for the ssh-sig gate (the pre-receive gate).
3124///
3125/// `signing_keys()` and not `keys()`, and that one word is a defect fixed on
3126/// 2026-08-05: this function used to load every row in the table, so a key past
3127/// its `expires` could no longer authenticate an SSH session and could still
3128/// satisfy `require_signature` on a push. The expiry was enforced on exactly one
3129/// of the two things a key does.
3130pub(crate) fn key_directory(stores: &ServerStores) -> StaticKeyDirectory {
3131 let mut directory = StaticKeyDirectory::new();
3132 let keys = match stores.auth().signing_keys() {
3133 Ok(keys) => keys,
3134 Err(err) => {
3135 tracing::error!(
3136 target: TARGET_POLICY,
3137 %err,
3138 "the identity table could not be read for signature verification"
3139 );
3140 return directory;
3141 }
3142 };
3143 for key in keys {
3144 // The identity table holds two credential kinds in one hash space
3145 // (auth.rs, the mTLS plan): an `x509` row is the bootstrap certificate,
3146 // not an ssh key, and cannot verify an ssh-sig — skipped by name, so
3147 // the WARN below is kept for a key that IS ssh and DID go bad
3148 // (measured 2026-08-29: the band warned at every login).
3149 if !ssh_verifiable_kind(&key.algo) {
3150 tracing::debug!(target: TARGET_POLICY, algo = %key.algo, fingerprint = %key.fingerprint_sha256, "not an ssh key kind; not a signature verifier");
3151 continue;
3152 }
3153 match gunnar_core::ssh_key::PublicKey::from_openssh(&key.openssh) {
3154 Ok(parsed) => {
3155 directory.add(&key.principal_id, parsed);
3156 }
3157 Err(err) => tracing::warn!(
3158 target: TARGET_POLICY,
3159 fingerprint = %key.fingerprint_sha256,
3160 %err,
3161 "a registered key did not parse and cannot verify a signature"
3162 ),
3163 }
3164 }
3165 // GUN-MERGE-SIGN: gunnar's own merge-signing key signs only the merge commits gunnar
3166 // wrote; it counts for every principal and scope, so a branch holding them can be
3167 // pushed and merged where signatures are required. Read, never minted, here.
3168 match stores.merge_signing_public() {
3169 Ok(Some(key)) => {
3170 directory.add_server_key(key);
3171 }
3172 Ok(None) => {}
3173 Err(err) => tracing::error!(
3174 target: TARGET_POLICY,
3175 err = %format!("{err:#}"),
3176 "the merge-signing key could not be read; gunnar's merge commits will not verify"
3177 ),
3178 }
3179 directory
3180}
3181
3182/// Is a registered key's `algo` (the name as OpenSSH writes it) something
3183/// the ssh-sig verifier can hold? `ssh-*`, `ecdsa-*`, `sk-*` are; `x509`
3184/// (the bootstrap certificate stored beside them) is not.
3185pub fn ssh_verifiable_kind(algo: &str) -> bool {
3186 algo.starts_with("ssh-") || algo.starts_with("ecdsa-") || algo.starts_with("sk-")
3187}
3188
3189// ───────────────────────────────────────────────────────────────────────────
3190// Guards
3191// ───────────────────────────────────────────────────────────────────────────
3192
3193#[cfg(test)]
3194mod tests {
3195
3196 /// ★ A build without the quarantine refuses an encrypted repository's push by name, and a
3197 /// plain repository's push is never refused for it (wiring tracer B 2026-10-08).
3198 #[test]
3199 fn a_build_without_the_quarantine_refuses_an_encrypted_push_by_name() {
3200 let line = super::unchecked_encrypted_push(true, false).expect("refused");
3201 assert!(
3202 line.contains("encrypted") && line.contains("this Gunnar"),
3203 "{line}"
3204 );
3205 assert_eq!(
3206 super::unchecked_encrypted_push(true, true),
3207 None,
3208 "the quarantine checks it"
3209 );
3210 assert_eq!(
3211 super::unchecked_encrypted_push(false, false),
3212 None,
3213 "a plain repository"
3214 );
3215 }
3216 use super::*;
3217 use gunnar_wire::pack::{IndexedPack, PackVersion};
3218
3219 #[test]
3220 fn the_signature_directory_takes_ssh_kinds_and_skips_the_x509_row_by_name() {
3221 for ssh in [
3222 "ssh-ed25519",
3223 "ssh-rsa",
3224 "ecdsa-sha2-nistp256",
3225 "sk-ssh-ed25519@openssh.com",
3226 "sk-ecdsa-sha2-nistp256@openssh.com",
3227 ] {
3228 assert!(ssh_verifiable_kind(ssh), "{ssh}");
3229 }
3230 assert!(
3231 !ssh_verifiable_kind("x509"),
3232 "the bootstrap certificate is not tried as an ssh key"
3233 );
3234 assert!(!ssh_verifiable_kind(""));
3235 }
3236
3237 /// A `PushOutcome` carrying whatever pack the caller wants to ask about.
3238 fn outcome_with(pack: Option<IndexedPack>) -> PushOutcome {
3239 PushOutcome {
3240 commands: Vec::new(),
3241 statuses: Vec::new(),
3242 unnameable: Vec::new(),
3243 push_options: Vec::new(),
3244 pack,
3245 pack_bytes: Some(0),
3246 new_objects: Ok(Vec::new()),
3247 atomic: false,
3248 unpack: Ok(()),
3249 }
3250 }
3251
3252 /// `stored_bytes: None` on purpose: these arms are about what
3253 /// `measured_pack_bytes` does when the ENGINE reported no count, which is
3254 /// the only case where stat'ing a path is still the answer.
3255 fn indexed(data_path: Option<std::path::PathBuf>) -> IndexedPack {
3256 indexed_with(data_path, None)
3257 }
3258
3259 fn indexed_with(
3260 data_path: Option<std::path::PathBuf>,
3261 stored_bytes: Option<u64>,
3262 ) -> IndexedPack {
3263 IndexedPack {
3264 num_objects: 1,
3265 pack_id: ObjectId::empty_blob(gix_hash::Kind::Sha1),
3266 index_id: ObjectId::empty_blob(gix_hash::Kind::Sha1),
3267 pack_version: PackVersion::V2,
3268 hash_kind: gix_hash::Kind::Sha1,
3269 index_path: None,
3270 data_path,
3271 keep_path: None,
3272 stored_bytes,
3273 }
3274 }
3275
3276 /// **An engine that writes no packfile still knows how big the pack was.**
3277 ///
3278 /// The znippy store appends the received bytes verbatim and writes no
3279 /// `.pack`, so `data_path` is `None` — and every push into the engine the
3280 /// appliance ships logged `pack_bytes_unknown="pack … was indexed without a
3281 /// directory, so there is nothing on disk to measure"`. That sentence was
3282 /// FALSE: the store counted the bytes it appended
3283 /// (`PushReceipt::pack_bytes`) and nobody carried the number this far. An
3284 /// absence rendered where a measurement exists is the same defect as the
3285 /// reverse, and it is the one this whole family of tests is about.
3286 ///
3287 /// Both directions, or a `stored_bytes` that were ignored would still pass
3288 /// the first line: a store that reports NEITHER a file nor a size is still
3289 /// an honest unknown, and must still say so.
3290 #[test]
3291 fn a_pack_with_no_file_is_measured_by_the_store_that_stored_it() {
3292 let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed_with(None, Some(4096)))));
3293 assert_eq!(
3294 bytes,
3295 Some(4096),
3296 "the store's own count is the measurement"
3297 );
3298 assert_eq!(
3299 why, None,
3300 "a measured pack carries no reason for not being measured"
3301 );
3302
3303 let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed_with(None, None))));
3304 assert_eq!(
3305 bytes, None,
3306 "neither a file nor a count is genuinely unknown"
3307 );
3308 assert!(
3309 why.as_deref().unwrap_or_default().contains("no size"),
3310 "and the reason must say the store reported no size, not merely that there is no \
3311 directory: {why:?}"
3312 );
3313 }
3314
3315 /// **The defect this replaced.** `stat` failing rendered as `pack_bytes=0`,
3316 /// which is indistinguishable from a push that genuinely carried nothing.
3317 ///
3318 /// Seen red: with the body restored to
3319 /// `.and_then(|path| std::fs::metadata(path).ok()).map(len).unwrap_or(0)`
3320 /// this fails with *"a pack whose bytes could not be read reported
3321 /// Some(0)"* — the old code's exact answer.
3322 #[test]
3323 fn a_pack_that_cannot_be_stated_reports_no_number_and_names_the_path() {
3324 let missing = std::path::PathBuf::from("/nonexistent/gunnar/pack-deadbeef.pack");
3325 let outcome = outcome_with(Some(indexed(Some(missing.clone()))));
3326 let (bytes, why) = measured_pack_bytes(&outcome);
3327 assert_eq!(
3328 bytes, None,
3329 "a pack whose bytes could not be read reported {bytes:?}; the only honest answer is \
3330 no number at all, because 0 is a legal pack size for a deletions-only push"
3331 );
3332 let why = why.expect("an unreadable pack must say why it is unreadable");
3333 assert!(
3334 why.contains(missing.to_str().unwrap()),
3335 "the reason must name the path an operator has to go and look at, got {why:?}"
3336 );
3337 }
3338
3339 /// The one arm that IS a zero, and must stay one: a deletions-only push
3340 /// carries no pack, and reporting that as *unknown* would be the mirror
3341 /// mistake.
3342 #[test]
3343 fn a_deletions_only_push_reports_a_measured_zero_and_no_reason() {
3344 let (bytes, why) = measured_pack_bytes(&outcome_with(None));
3345 assert_eq!(
3346 bytes,
3347 Some(0),
3348 "a push with no pack carries zero pack bytes"
3349 );
3350 assert_eq!(why, None, "zero bytes is a measurement, not an absence");
3351 }
3352
3353 /// **Three different answers over the same empty list**, which is the whole
3354 /// reason the count takes a `Result` and not a slice.
3355 ///
3356 /// * not enumerated, 3-object pack → 3. Seen red on the rig's SSE feed
3357 /// ("pushed 0 objects (0 B)" for a push that carried a commit).
3358 /// * not enumerated, no pack at all → 0, because there is nothing to count.
3359 /// * ENUMERATED and empty → 0 even beside a 3-object pack. That is a
3360 /// measured zero: the store said which objects the push introduced and
3361 /// the answer was none. Under the old `is_empty()` fallback this case
3362 /// reported 3, i.e. the record disagreed with the store that had been
3363 /// asked. It is the arm that makes this test unable to pass against the
3364 /// code it replaces.
3365 #[test]
3366 fn the_object_count_reads_the_reason_not_the_length() {
3367 let mut pack = indexed(None);
3368 pack.num_objects = 3;
3369 let three = outcome_with(Some(pack));
3370 let none = outcome_with(None);
3371 let unknown = Err("the policy has no gate over objects");
3372 assert_eq!(attested_object_count(unknown, &three), 3);
3373 assert_eq!(attested_object_count(unknown, &none), 0);
3374 assert_eq!(
3375 attested_object_count(Ok(&[]), &three),
3376 0,
3377 "an enumerated empty set is a MEASURED zero and must not be overruled by the pack"
3378 );
3379 let oid = gix_hash::ObjectId::null(gix_hash::Kind::Sha1);
3380 assert_eq!(attested_object_count(Ok(&[oid, oid]), &none), 2);
3381 }
3382
3383 /// A pack indexed with no directory wrote nothing, so there is nothing to
3384 /// measure — and that is not zero either.
3385 #[test]
3386 fn a_pack_never_written_to_disk_reports_no_number() {
3387 let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed(None))));
3388 assert_eq!(bytes, None);
3389 assert!(
3390 why.expect("must say why").contains("without a directory"),
3391 "the reason must distinguish 'never written' from 'written and unreadable'"
3392 );
3393 }
3394
3395 /// The number is the file's real length, not a plausible one. Seen red by
3396 /// writing 7 bytes and asserting 6.
3397 #[test]
3398 fn a_pack_on_disk_reports_the_bytes_the_file_actually_holds() {
3399 let dir = tempfile::tempdir().expect("a temp dir");
3400 let path = dir.path().join("pack-x.pack");
3401 std::fs::write(&path, b"1234567").expect("write");
3402 let (bytes, why) = measured_pack_bytes(&outcome_with(Some(indexed(Some(path)))));
3403 assert_eq!(bytes, Some(7), "the length must come off the filesystem");
3404 assert_eq!(why, None);
3405 }
3406
3407 /// `wire_bytes` had the same `unwrap_or(0)`. `PushFacts::pack_bytes`
3408 /// documents `None` as "genuinely unavailable" and `Some(0)` as a
3409 /// deletions-only push; the log collapsed both onto `0`.
3410 #[test]
3411 fn an_uncounted_wire_is_not_a_wire_that_carried_nothing() {
3412 let (bytes, why) = measured(None, "the ingest did not report");
3413 assert_eq!(bytes, None, "an uncounted wire must not render as 0 bytes");
3414 assert_eq!(why.as_deref(), Some("the ingest did not report"));
3415
3416 let (bytes, why) = measured(Some(0), "unused");
3417 assert_eq!(bytes, Some(0), "a counted zero stays a zero");
3418 assert_eq!(why, None, "a counted zero carries no reason");
3419 }
3420
3421 /// The ssh-sig gate is handed **only unexpired** keys.
3422 ///
3423 /// The defect, found 2026-08-05: `key_directory` loaded `AuthTable::keys()`,
3424 /// which filters nothing, so a key past its `expires` could no longer open an
3425 /// SSH session and could still satisfy `require_signature` on a push. One
3426 /// credential with two lifetimes.
3427 ///
3428 /// `gunnar-core`'s `an_expired_key_signs_nothing_and_is_still_listed` guards
3429 /// the *predicate*. This guards the **wiring**, which is the half that was
3430 /// actually wrong: the predicate already existed and worked, and this call
3431 /// site simply did not ask it. A guard on the predicate alone would have been
3432 /// green over the whole bug.
3433 ///
3434 /// # Seen red
3435 ///
3436 /// With `signing_keys()` changed back to `keys()`, `all_keys()` has length 1
3437 /// and the first assertion fails. The valid-key half is the control: it fails
3438 /// if `key_directory` returns an empty directory for any other reason —
3439 /// an unreadable table, a key that will not parse — which would otherwise
3440 /// make the first assertion pass for the wrong reason.
3441 /// **Un-ignored 2026-08-10.** The reason it was skipped — *"this crate
3442 /// deliberately carries no storage engine"* — stopped being true when the
3443 /// engine became a feature of this crate with znippy as the default. There
3444 /// is no engine-free build of `gunnar-server` any longer to skip it for.
3445 #[test]
3446 fn the_signature_gate_is_handed_no_expired_key() {
3447 // The known-answer key `gunnar-core`'s own tests use, and a second one, so
3448 // the two principals below hold different key material.
3449 const EXPIRED_KEY: &str = "ssh-ed25519 \
3450 AAAAC3NzaC1lZDI1NTE5AAAAIMW4aqVr75w+3DWorOCbfwl5XCDCq1yCQ6puzU4qzol2 gunnar-kat";
3451 let minted = gunnar_core::mint_ed25519().expect("mint a key");
3452 let live_key = gunnar_core::ssh_key::PrivateKey::from_openssh(&minted)
3453 .expect("parse the minted key")
3454 .public_key()
3455 .to_openssh()
3456 .expect("render the public half");
3457
3458 let dir = tempfile::tempdir().expect("a temp dir");
3459 let stores = ServerStores::open(dir.path()).expect("open the stores");
3460 for (id, key, expires) in [
3461 // Already expired: the unix epoch.
3462 ("stale", EXPIRED_KEY, Some(1_000)),
3463 ("live", live_key.as_str(), None),
3464 ] {
3465 stores
3466 .auth()
3467 .add_principal(
3468 gunnar_core::Principal::new(id, id, gunnar_core::PrincipalKind::Machine)
3469 .expect("a valid id"),
3470 )
3471 .expect("add the principal");
3472 stores
3473 .auth()
3474 .register_key(id, key, expires)
3475 .expect("register the key");
3476 }
3477
3478 let directory = key_directory(&stores);
3479 assert!(
3480 directory.keys_for("stale").is_empty(),
3481 "an expired key must not be offered to the signature gate"
3482 );
3483 // The control. Without it an always-empty directory — an unreadable
3484 // table, a parse failure, a `key_directory` that returns early — passes
3485 // the line above while proving nothing.
3486 assert_eq!(
3487 directory.keys_for("live").len(),
3488 1,
3489 "an unexpired key must still be offered, or this test proves nothing"
3490 );
3491 assert_eq!(
3492 directory.all_keys().len(),
3493 1,
3494 "exactly the unexpired key, and nothing else, reaches the gate"
3495 );
3496 }
3497}