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
573
574
575
576
577
578
579
580
581
582
583
//! Example implementations of basic search strategies as defined in [`search`].
//! See [`BasicStrategyKind`] for the list.
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::hash::Hash;
use indexmap::IndexMap;
use tracing::instrument;
use crate::search;
/// Directed acyclic graph commonly used in source control.
///
/// Implementation of `Graph` that represents the common case of a directed
/// acyclic graph in source control. You can implement this trait instead of
/// `Graph` (as there is a blanket implementation for `Graph`) and also make use
/// of `BasicStrategy`.
pub trait BasicSourceControlGraph: Debug {
/// The type of nodes in the graph. This should be cheap to clone.
type Node: Clone + Debug + Hash + Eq + 'static;
/// An error type.
type Error: Debug + std::error::Error + 'static;
/// Get every node `X` in the graph such that `X == node` or there exists a
/// child of `X` that is an ancestor of `node`.
fn ancestors(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Self::Error>;
/// Get the union of `ancestors(node)` for every node in `nodes`.
#[instrument]
fn ancestors_all(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
let mut ancestors = HashSet::new();
for node in nodes {
ancestors.extend(self.ancestors(node)?);
}
Ok(ancestors)
}
/// Filter `nodes` to only include nodes that are not ancestors of any other
/// node in `nodes`.
fn ancestor_heads(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
let node_to_ancestors: HashMap<Self::Node, HashSet<Self::Node>> = nodes
.iter()
.map(|node| Ok((node.clone(), self.ancestors(node.clone())?)))
.collect::<Result<_, _>>()?;
let heads: HashSet<Self::Node> = nodes
.into_iter()
.filter(|node| {
node_to_ancestors
.iter()
.filter_map(|(other_node, ancestors)| {
if node == other_node {
None
} else {
Some(ancestors)
}
})
.all(|ancestors| !ancestors.contains(node))
})
.collect();
Ok(heads)
}
/// Get every node `X` in the graph such that `X == node` or there exists a
/// parent of `X` that is a descendant of `node`.
fn descendants(&self, node: Self::Node) -> Result<HashSet<Self::Node>, Self::Error>;
/// Filter `nodes` to only include nodes that are not descendants of any
/// other node in `nodes`.
fn descendant_roots(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
let node_to_descendants: HashMap<Self::Node, HashSet<Self::Node>> = nodes
.iter()
.map(|node| Ok((node.clone(), self.descendants(node.clone())?)))
.collect::<Result<_, _>>()?;
let roots: HashSet<Self::Node> = nodes
.into_iter()
.filter(|node| {
node_to_descendants
.iter()
.filter_map(|(other_node, descendants)| {
if node == other_node {
None
} else {
Some(descendants)
}
})
.all(|descendants| !descendants.contains(node))
})
.collect();
Ok(roots)
}
/// Get the union of `descendants(node)` for every node in `nodes`.
#[instrument]
fn descendants_all(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
let mut descendants = HashSet::new();
for node in nodes {
descendants.extend(self.descendants(node)?);
}
Ok(descendants)
}
}
impl<T: BasicSourceControlGraph> search::Graph for T {
type Node = <Self as BasicSourceControlGraph>::Node;
type Error = <Self as BasicSourceControlGraph>::Error;
fn is_ancestor(
&self,
ancestor: Self::Node,
descendant: Self::Node,
) -> Result<bool, Self::Error> {
let ancestors = self.ancestors(descendant)?;
Ok(ancestors.contains(&ancestor))
}
fn simplify_success_bounds(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
self.ancestor_heads(nodes)
}
fn simplify_failure_bounds(
&self,
nodes: HashSet<Self::Node>,
) -> Result<HashSet<Self::Node>, Self::Error> {
self.descendant_roots(nodes)
}
}
/// The possible strategies for searching the graph.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum BasicStrategyKind {
/// Search the nodes in the order that they were provided.
Linear,
/// Search the nodes in the reverse order that they were provided.
LinearReverse,
/// Conduct a binary search on the nodes by partitioning the nodes into two
/// groups of approximately equal size.
///
/// TODO: Partitioning into groups of approximately equal size isn't
/// actually optimal for the DAG case. Really, we want to maximize the
/// information that we gain from each test. The `git bisect` algorithm at
/// <https://git-scm.com/docs/git-bisect-lk2009#_bisection_algorithm_discussed>
/// discusses a metric to find the best partition for the subgraph which
/// remains to be tested.
///
/// See also `git-bisect`'s skip algorithm:
/// <https://git-scm.com/docs/git-bisect-lk2009#_skip_algorithm>. This does
/// *not* use the same skip algorithm, and instead uses a deterministic
/// approach. In order to solve the following problem:
///
/// > sometimes the best bisection points all happened to be in an area
/// > where all the commits are untestable. And in this case the user was
/// > asked to test many untestable commits, which could be very inefficient.
///
/// We instead consider the hypothetical case that the node is a success,
/// and yield further nodes as if it were a success, and then interleave
/// those nodes with the hypothetical failure case.
///
/// Resources:
///
/// - <https://git-scm.com/docs/git-bisect-lk2009#_bisection_algorithm_discussed>
/// - <https://byorgey.wordpress.com/2023/01/01/competitive-programming-in-haskell-better-binary-search/>
/// - <https://julesjacobs.com/notes/binarysearch/binarysearch.pdf>
Binary,
}
/// A set of basic search strategies defined by `BasicStrategyKind`.
#[derive(Clone, Debug)]
pub struct BasicStrategy {
strategy: BasicStrategyKind,
}
impl BasicStrategy {
/// Constructor.
pub fn new(strategy: BasicStrategyKind) -> Self {
Self { strategy }
}
}
impl<G: BasicSourceControlGraph> search::Strategy<G> for BasicStrategy {
type Error = G::Error;
fn midpoint(
&self,
graph: &G,
bounds: &search::Bounds<G::Node>,
statuses: &IndexMap<G::Node, search::Status>,
) -> Result<Option<G::Node>, G::Error> {
let search::Bounds {
success: success_bounds,
failure: failure_bounds,
} = bounds;
let mut nodes_to_search = {
let implied_success_nodes = graph.ancestors_all(success_bounds.clone())?;
let implied_failure_nodes = graph.descendants_all(failure_bounds.clone())?;
statuses
.iter()
.filter_map(|(node, status)| match status {
search::Status::Untested => Some(node.clone()),
search::Status::Success
| search::Status::Failure
| search::Status::Indeterminate => None,
})
.filter(|node| {
!implied_success_nodes.contains(node) && !implied_failure_nodes.contains(node)
})
.collect::<Vec<_>>()
};
let next_to_search: Option<G::Node> = match self.strategy {
BasicStrategyKind::Linear => nodes_to_search.into_iter().next(),
BasicStrategyKind::LinearReverse => nodes_to_search.into_iter().next_back(),
BasicStrategyKind::Binary => {
let middle_index = nodes_to_search.len() / 2;
if middle_index < nodes_to_search.len() {
Some(nodes_to_search.swap_remove(middle_index))
} else {
None
}
}
};
Ok(next_to_search)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::search::Bounds;
use crate::search::EagerSolution;
use crate::search::Search;
use crate::search::Status;
use crate::testing::arb_strategy;
use crate::testing::arb_test_graph_and_nodes;
use crate::testing::TestGraph;
use crate::testing::UsizeGraph;
use itertools::Itertools;
use maplit::hashmap;
use maplit::hashset;
#[test]
fn test_search_stick() {
let graph = UsizeGraph { max: 7 };
let nodes = 0..graph.max;
let linear_strategy = BasicStrategy {
strategy: BasicStrategyKind::Linear,
};
let linear_reverse_strategy = BasicStrategy {
strategy: BasicStrategyKind::LinearReverse,
};
let binary_strategy = BasicStrategy {
strategy: BasicStrategyKind::Binary,
};
let mut search = Search::new(graph.clone(), nodes.clone());
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Default::default(),
next_to_search: vec![0, 1, 2, 3, 4, 5, 6],
}
);
assert_eq!(
search
.search(&linear_reverse_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Default::default(),
next_to_search: vec![6, 5, 4, 3, 2, 1, 0],
}
);
assert_eq!(
search
.search(&binary_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Default::default(),
// Breadth-first search:
// 0 1 2 3 4 5 6
// ^
// ^
// ^
// ^
// ^
// ^
// ^
next_to_search: vec![3, 1, 5, 0, 2, 4, 6],
}
);
search.notify(2, Status::Success).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {2},
failure: hashset! {},
},
next_to_search: vec![3, 4, 5, 6],
}
);
assert_eq!(
search
.search(&binary_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {2},
failure: hashset! {},
},
next_to_search: vec![5, 4, 6, 3],
}
);
search.notify(5, Status::Failure).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {2},
failure: hashset! {5},
},
next_to_search: vec![3, 4],
}
);
assert_eq!(
search
.search(&binary_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {2},
failure: hashset! {5},
},
next_to_search: vec![4, 3],
}
);
search.notify(3, Status::Indeterminate).unwrap();
assert_eq!(
search
.search(&binary_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {2},
failure: hashset! {5},
},
next_to_search: vec![4],
}
);
}
#[test]
fn test_search_inconsistent_notify() {
let graph = UsizeGraph { max: 7 };
let nodes = 0..graph.max;
let mut search = Search::new(graph, nodes);
search.notify(4, Status::Success).unwrap();
insta::assert_debug_snapshot!(search.notify(3, Status::Failure), @r###"
Err(
InconsistentStateTransition {
ancestor_node: 3,
ancestor_status: Failure,
descendant_node: 4,
descendant_status: Success,
},
)
"###);
insta::assert_debug_snapshot!(search.notify(4, Status::Indeterminate), @r###"
Err(
IllegalStateTransition {
node: 4,
from: Success,
to: Indeterminate,
},
)
"###);
search.notify(5, Status::Failure).unwrap();
insta::assert_debug_snapshot!(search.notify(6, Status::Success), @r###"
Err(
InconsistentStateTransition {
ancestor_node: 5,
ancestor_status: Failure,
descendant_node: 6,
descendant_status: Success,
},
)
"###);
}
#[test]
fn test_search_dag() {
let graph = TestGraph {
// a -> b -> e -> f -> g
// c -> d -> -> h
nodes: hashmap! {
'a' => hashset! {'b'},
'b' => hashset! {'e'},
'c' => hashset! {'d'},
'd' => hashset! {'e'},
'e' => hashset! {'f', 'h'},
'f' => hashset! {'g'},
'g' => hashset! {},
'h' => hashset! {},
},
};
let linear_strategy = BasicStrategy {
strategy: BasicStrategyKind::Linear,
};
assert_eq!(graph.descendants('e'), Ok(hashset! {'e', 'f', 'g', 'h'}));
assert_eq!(graph.ancestors('e'), Ok(hashset! {'a', 'b', 'c', 'd', 'e'}));
let mut search = Search::new(graph, 'a'..='h');
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Default::default(),
// Breadth-first search: we start from the roots of the graph in
// parallel and proceed to the heads of the graph.
next_to_search: vec!['a', 'c', 'b', 'd', 'e', 'f', 'h', 'g'],
}
);
search.notify('b', Status::Success).unwrap();
search.notify('g', Status::Failure).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {'b'},
failure: hashset! {'g'},
},
next_to_search: vec!['c', 'd', 'e', 'f', 'h'],
}
);
search.notify('e', Status::Success).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {'e'},
failure: hashset! {'g'},
},
next_to_search: vec!['f', 'h'],
}
);
search.notify('f', Status::Success).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {'f'},
failure: hashset! {'g'},
},
next_to_search: vec!['h'],
}
);
search.notify('h', Status::Success).unwrap();
assert_eq!(
search
.search(&linear_strategy)
.unwrap()
.into_eager()
.unwrap(),
EagerSolution {
bounds: Bounds {
success: hashset! {'f', 'h'},
failure: hashset! {'g'},
},
next_to_search: vec![],
}
);
}
proptest::proptest! {
#[test]
fn test_search_dag_proptest(strategy in arb_strategy(), (graph, failure_nodes) in arb_test_graph_and_nodes()) {
let nodes = graph.nodes.keys().sorted().copied().collect::<Vec<_>>();
let strategy = BasicStrategy {
strategy,
};
let mut search = Search::new(graph.clone(), nodes);
let failure_nodes = graph.descendants_all(failure_nodes.into_iter().collect()).unwrap();
let solution = loop {
let solution = search.search(&strategy).unwrap().into_eager().unwrap();
let Bounds { success, failure } = &solution.bounds;
for success_node in success {
assert!(!failure_nodes.contains(success_node))
}
for failure_node in failure {
assert!(failure_nodes.contains(failure_node));
}
match solution.next_to_search.first() {
Some(node) => {
search.notify(*node, if failure_nodes.contains(node) {
Status::Failure
} else {
Status::Success
}).unwrap();
}
None => break solution,
}
};
let nodes = graph.nodes.keys().copied().collect::<HashSet<_>>();
assert!(solution.bounds.success.is_subset(&nodes));
assert!(solution.bounds.failure.is_subset(&nodes));
assert!(solution.bounds.success.is_disjoint(&solution.bounds.failure));
let all_success_nodes = graph.ancestors_all(solution.bounds.success.clone()).unwrap();
let all_failure_nodes = graph.descendants_all(solution.bounds.failure).unwrap();
assert!(all_success_nodes.is_disjoint(&all_failure_nodes));
assert!(
all_success_nodes.union(&all_failure_nodes).copied().collect::<HashSet<_>>() == nodes,
"all_success_nodes: {all_success_nodes:?}, all_failure_nodes: {all_failure_nodes:?}, nodes: {nodes:?}",
);
}
}
}