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