1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Exact observation and mutation dependencies for no-scan safe rebasing.
use super::{ExtentSeekTarget, LogicalName};
use crate::cancellation::CancellationToken;
use crate::foundation::{Digest, FileId, GenerationId};
use crate::performance::{OperationFailure, WorkBudget, WorkCounters, WorkError};
use std::collections::BTreeMap;
use std::future::Future;
use thiserror::Error;
/// One exact semantic region whose state can affect a checkout.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum DependencyRegion {
/// Complete path-independent file record, including kind and payload roots.
FileRecord(FileId),
/// Complete canonical metadata object selected by one file record.
Metadata(FileId),
/// Exact logical length of one regular file.
FileLength(FileId),
/// A non-empty bounded logical content range.
ContentRange {
/// Stable file identity.
file_id: FileId,
/// Inclusive logical offset.
offset: u64,
/// Positive byte length.
length: u64,
},
/// One exact sparse seek observation.
SparseSeek {
/// Stable file identity.
file_id: FileId,
/// Inclusive logical starting offset.
offset: u64,
/// Sparse class requested by the caller.
target: ExtentSeekTarget,
},
/// One exact name lookup, including a negative lookup when state is absent.
DirectoryName {
/// Stable directory identity.
directory_id: FileId,
/// Canonical queried name.
name: LogicalName,
},
/// A bounded directory cursor interval used to prevent phantom-safe rebases.
DirectoryRange {
/// Stable directory identity.
directory_id: FileId,
/// Exclusive lower cursor; `None` starts before the first name.
after: Option<LogicalName>,
/// Positive maximum entries requested by the observed page.
maximum_entries: u32,
},
}
impl DependencyRegion {
/// Validates range arithmetic and directory cursor ordering.
///
/// # Errors
///
/// Rejects empty/overflowing content ranges and inverted directory windows.
pub fn validate(&self) -> Result<(), DependencyError> {
match self {
Self::ContentRange { offset, length, .. } => {
if *length == 0 {
return Err(DependencyError::EmptyContentRange);
}
offset
.checked_add(*length)
.ok_or(DependencyError::RangeOverflow)?;
}
Self::DirectoryRange {
maximum_entries: 0, ..
} => return Err(DependencyError::ZeroDirectoryPageLimit),
Self::FileRecord(_)
| Self::Metadata(_)
| Self::FileLength(_)
| Self::SparseSeek { .. }
| Self::DirectoryName { .. }
| Self::DirectoryRange { .. } => {}
}
Ok(())
}
}
/// Canonical semantic state of one exact dependency region.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DependencyState {
/// The exact queried name/record/region does not exist.
Absent,
/// Domain-separated digest of the complete bounded semantic result.
Present(Digest),
}
/// One dependency captured while evaluating or mutating a checkout.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Dependency {
/// Exact semantic region.
pub region: DependencyRegion,
/// State authenticated against the checkout's base generation.
pub expected: DependencyState,
}
/// Whether a conflicting dependency came from observation, mutation, or both.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DependencyUse {
/// A read, stat, listing, or negative lookup observed this region.
Observation,
/// A local mutation uses this region as its compare-before-write base.
Mutation,
/// The same region was both observed and mutated.
ObservationAndMutation,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct CapturedState {
expected: DependencyState,
usage: DependencyUse,
}
/// Validated, sorted, and deduplicated checkout dependency proof.
///
/// Construction pays normalization cost once. Equal-generation refresh can
/// therefore return in constant work regardless of dependency count.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CheckoutDependencies {
captured: BTreeMap<DependencyRegion, CapturedState>,
}
impl CheckoutDependencies {
/// Validates and normalizes exact observations and mutation preconditions.
///
/// # Errors
///
/// Rejects excessive, malformed, or contradictory dependencies.
pub fn new(
observations: impl IntoIterator<Item = Dependency>,
mutations: impl IntoIterator<Item = Dependency>,
maximum_dependencies: u32,
) -> Result<Self, DependencyError> {
if maximum_dependencies == 0 {
return Err(DependencyError::ZeroLimit);
}
let mut captured: BTreeMap<DependencyRegion, CapturedState> = BTreeMap::new();
for (dependency, usage) in observations
.into_iter()
.map(|value| (value, DependencyUse::Observation))
.chain(
mutations
.into_iter()
.map(|value| (value, DependencyUse::Mutation)),
)
{
dependency.region.validate()?;
if let Some(existing) = captured.get_mut(&dependency.region) {
if existing.expected != dependency.expected {
return Err(DependencyError::ContradictoryState);
}
if existing.usage != usage {
existing.usage = DependencyUse::ObservationAndMutation;
}
continue;
}
if u32::try_from(captured.len()).unwrap_or(u32::MAX) >= maximum_dependencies {
return Err(DependencyError::TooManyDependencies {
maximum: maximum_dependencies,
});
}
captured.insert(
dependency.region,
CapturedState {
expected: dependency.expected,
usage,
},
);
}
Ok(Self { captured })
}
/// Number of distinct exact regions in the proof.
#[must_use]
pub fn len(&self) -> usize {
self.captured.len()
}
/// Whether no operation has observed or mutated a region.
#[must_use]
pub fn is_empty(&self) -> bool {
self.captured.is_empty()
}
pub(crate) fn clear(&mut self) {
self.captured.clear();
}
pub(crate) fn clear_mutations(&mut self) {
self.captured.retain(|_, state| match state.usage {
DependencyUse::Observation => true,
DependencyUse::Mutation => false,
DependencyUse::ObservationAndMutation => {
state.usage = DependencyUse::Observation;
true
}
});
}
pub(crate) fn extend_observations(
&mut self,
dependencies: Vec<Dependency>,
maximum_dependencies: u32,
) -> Result<(), DependencyError> {
self.extend(
dependencies,
maximum_dependencies,
DependencyUse::Observation,
)
}
pub(crate) fn extend_mutations(
&mut self,
dependencies: Vec<Dependency>,
maximum_dependencies: u32,
) -> Result<(), DependencyError> {
self.extend(dependencies, maximum_dependencies, DependencyUse::Mutation)
}
fn extend(
&mut self,
dependencies: Vec<Dependency>,
maximum_dependencies: u32,
usage: DependencyUse,
) -> Result<(), DependencyError> {
if maximum_dependencies == 0 {
return Err(DependencyError::ZeroLimit);
}
let mut staged = BTreeMap::new();
for dependency in dependencies {
dependency.region.validate()?;
if let Some(existing) = self.captured.get(&dependency.region)
&& existing.expected != dependency.expected
{
return Err(DependencyError::ContradictoryState);
}
match staged.get(&dependency.region) {
Some(expected) if *expected != dependency.expected => {
return Err(DependencyError::ContradictoryState);
}
Some(_) => {}
None => {
staged.insert(dependency.region, dependency.expected);
}
}
}
let distinct_new = staged
.keys()
.filter(|region| !self.captured.contains_key(*region))
.count();
if self.captured.len().saturating_add(distinct_new)
> usize::try_from(maximum_dependencies).unwrap_or(usize::MAX)
{
return Err(DependencyError::TooManyDependencies {
maximum: maximum_dependencies,
});
}
for (region, expected) in staged {
match self.captured.get_mut(®ion) {
Some(existing) => {
if existing.usage != usage
&& existing.usage != DependencyUse::ObservationAndMutation
{
existing.usage = DependencyUse::ObservationAndMutation;
}
}
None => {
self.captured
.insert(region, CapturedState { expected, usage });
}
}
}
Ok(())
}
}
/// Backend-independent exact-region resolver.
///
/// Implementations must derive `DependencyState` from canonical authenticated
/// generation objects, not from mutable projections or cache presence.
pub trait RebaseProbe {
/// Stable backend-specific failure.
type Error: std::error::Error;
/// Resolves one exact semantic region in one immutable generation.
///
/// # Errors
///
/// Returns a backend-specific failure with exact work performed before the
/// region could be authenticated.
fn probe(
&self,
generation: GenerationId,
region: &DependencyRegion,
budget: WorkBudget,
) -> Result<ProbeReceipt, OperationFailure<Self::Error>>;
}
/// Runtime-neutral nonblocking exact-region resolver.
pub trait AsyncRebaseProbe {
/// Stable backend-specific failure.
type Error: std::error::Error;
/// Resolves one exact semantic region without blocking the calling runtime.
fn probe_async(
&self,
generation: GenerationId,
region: &DependencyRegion,
budget: WorkBudget,
cancellation: &CancellationToken,
) -> impl Future<Output = Result<ProbeReceipt, OperationFailure<Self::Error>>>;
}
/// One exact-region result and the work required to authenticate it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProbeReceipt {
/// Canonical semantic state in the selected generation.
pub value: DependencyState,
/// Exact backend and semantic work.
pub work: WorkCounters,
}
/// One region-specific reason automatic advancement is unsafe.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RebaseConflict {
/// Exact changed region.
pub region: DependencyRegion,
/// How the checkout depended on it.
pub usage: DependencyUse,
/// State captured from the old base.
pub expected: DependencyState,
/// State resolved from the candidate generation.
pub actual: DependencyState,
}
/// Bounded safe-rebase result.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RebaseDecision {
/// Candidate generation can become the new base without replay ambiguity.
Safe {
/// New immutable base.
generation: GenerationId,
},
/// Candidate changed one or more exact dependencies.
Conflicted {
/// Region-specific conflicts, bounded by the caller.
conflicts: Vec<RebaseConflict>,
/// More conflicts existed than the caller admitted returning.
truncated: bool,
},
}
/// Successful decision with exact probe work.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RebaseReceipt {
/// Safe or region-conflicted result.
pub decision: RebaseDecision,
/// Exact semantic and backend work.
pub work: WorkCounters,
}
/// Safe-rebase classification failures.
#[derive(Debug, Error)]
pub enum RebaseError<E: std::error::Error> {
/// Cooperative cancellation occurred before the next region probe.
#[error("safe rebase classification was cancelled")]
Cancelled,
/// Conflict output must have a positive hard bound.
#[error("maximum rebase conflicts must be non-zero")]
ZeroConflictLimit,
/// Exact region resolution failed.
#[error("rebase region probe failed: {0}")]
Probe(E),
/// Work exceeded or overflowed the admitted budget.
#[error(transparent)]
Work(#[from] WorkError),
}
/// Compares only captured regions against a candidate generation.
///
/// Equal roots return with zero work and no probes. Otherwise each distinct
/// dependency is resolved once; untouched volume regions are never enumerated.
///
/// # Errors
///
/// Fails with retained work when conflict bounds, probe execution, or work
/// admission fail.
pub fn classify_rebase<P: RebaseProbe>(
probe: &P,
base: GenerationId,
candidate: GenerationId,
dependencies: &CheckoutDependencies,
maximum_conflicts: u32,
budget: WorkBudget,
) -> Result<RebaseReceipt, OperationFailure<RebaseError<P::Error>>> {
if base == candidate {
return Ok(RebaseReceipt {
decision: RebaseDecision::Safe {
generation: candidate,
},
work: WorkCounters::default(),
});
}
if maximum_conflicts == 0 {
return Err(OperationFailure::before_work(
RebaseError::ZeroConflictLimit,
));
}
let mut work = WorkCounters::default();
let mut conflicts = Vec::new();
let mut truncated = false;
for (region, captured) in &dependencies.captured {
let semantic = WorkCounters {
items_examined: 1,
..WorkCounters::default()
};
work = work
.checked_add(semantic)
.map_err(|error| OperationFailure::new(error.into(), work))?;
let remaining = work
.remaining(budget)
.map_err(|error| OperationFailure::new(error.into(), work))?;
let actual = probe
.probe(candidate, region, remaining)
.map_err(|failure| failure.map_with_prior_work(work, RebaseError::Probe))?;
work = work
.checked_add(actual.work)
.map_err(|error| OperationFailure::new(error.into(), work))?;
work.verify(budget)
.map_err(|error| OperationFailure::new(error.into(), work))?;
if actual.value != captured.expected {
if u32::try_from(conflicts.len()).unwrap_or(u32::MAX) < maximum_conflicts {
conflicts.push(RebaseConflict {
region: region.clone(),
usage: captured.usage,
expected: captured.expected,
actual: actual.value,
});
} else {
truncated = true;
}
}
}
let decision = if conflicts.is_empty() {
RebaseDecision::Safe {
generation: candidate,
}
} else {
RebaseDecision::Conflicted {
conflicts,
truncated,
}
};
Ok(RebaseReceipt { decision, work })
}
/// Nonblocking safe-rebase classification over the same exact dependencies as
/// [`classify_rebase`].
///
/// # Errors
///
/// Fails with retained exact work on cancellation, probe failure, invalid
/// conflict bounds, or work-budget exhaustion.
pub async fn classify_rebase_async<P: AsyncRebaseProbe>(
probe: &P,
base: GenerationId,
candidate: GenerationId,
dependencies: &CheckoutDependencies,
maximum_conflicts: u32,
budget: WorkBudget,
cancellation: &CancellationToken,
) -> Result<RebaseReceipt, OperationFailure<RebaseError<P::Error>>> {
if base == candidate {
return Ok(RebaseReceipt {
decision: RebaseDecision::Safe {
generation: candidate,
},
work: WorkCounters::default(),
});
}
if maximum_conflicts == 0 {
return Err(OperationFailure::before_work(
RebaseError::ZeroConflictLimit,
));
}
let mut work = WorkCounters::default();
let mut conflicts = Vec::new();
let mut truncated = false;
for (region, captured) in &dependencies.captured {
cancellation
.check()
.map_err(|_| OperationFailure::new(RebaseError::Cancelled, work))?;
work = work
.checked_add(WorkCounters {
items_examined: 1,
..WorkCounters::default()
})
.map_err(|error| OperationFailure::new(error.into(), work))?;
let remaining = work
.remaining(budget)
.map_err(|error| OperationFailure::new(error.into(), work))?;
let actual = probe
.probe_async(candidate, region, remaining, cancellation)
.await
.map_err(|failure| failure.map_with_prior_work(work, RebaseError::Probe))?;
work = work
.checked_add(actual.work)
.map_err(|error| OperationFailure::new(error.into(), work))?;
work.verify(budget)
.map_err(|error| OperationFailure::new(error.into(), work))?;
if actual.value != captured.expected {
if u32::try_from(conflicts.len()).unwrap_or(u32::MAX) < maximum_conflicts {
conflicts.push(RebaseConflict {
region: region.clone(),
usage: captured.usage,
expected: captured.expected,
actual: actual.value,
});
} else {
truncated = true;
}
}
}
let decision = if conflicts.is_empty() {
RebaseDecision::Safe {
generation: candidate,
}
} else {
RebaseDecision::Conflicted {
conflicts,
truncated,
}
};
Ok(RebaseReceipt { decision, work })
}
/// Dependency-proof construction errors.
#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
pub enum DependencyError {
/// Dependency admission bound must be non-zero.
#[error("maximum dependencies must be non-zero")]
ZeroLimit,
/// Content ranges must contain at least one byte.
#[error("content dependency range is empty")]
EmptyContentRange,
/// Content range arithmetic overflowed.
#[error("content dependency range overflowed")]
RangeOverflow,
/// Directory listing observations require a positive page bound.
#[error("directory dependency page limit must be non-zero")]
ZeroDirectoryPageLimit,
/// The same exact region was captured with two different base states.
#[error("dependency region has contradictory captured states")]
ContradictoryState,
/// Distinct dependency count exceeded its hard bound.
#[error("dependency count exceeds maximum {maximum}")]
TooManyDependencies {
/// Configured maximum.
maximum: u32,
},
}
#[cfg(test)]
#[path = "tests/rebase.rs"]
mod tests;