Repository: HypothesisWorks/hypothesis

Issue:
Not clearing out examples found during shrinking from the primary key
Historically we saved all interesting examples in the database key. Now we're supposed to save examples found during shrinking under the secondary key and remove them from the primary key. Apparently that isn't working correctly. The following test (extracted from some other work I'm doing) demonstrates this:

```python
def test_clears_out_its_database_on_shrinking():
    key = b'key'
    db = InMemoryExampleDatabase()

    def f(data):
        if data.draw_bits(8) >= 127:
            data.mark_interesting()

    runner = ConjectureRunner(
        f, settings=settings(database=db, max_examples=256), database_key=key)

    for n in hrange(256):
        db.save(runner.secondary_key, hbytes([n]))
    runner.run()

    assert len(list(db.fetch(key))) == 1
```

When I ran this it fails with 17 examples in the list, but I expect that depends on the random seed.


Discussion:

Resolving patch:
diff --git a/src/hypothesis/internal/conjecture/engine.py b/src/hypothesis/internal/conjecture/engine.py
--- a/src/hypothesis/internal/conjecture/engine.py
+++ b/src/hypothesis/internal/conjecture/engine.py
@@ -249,6 +249,7 @@ def test_function(self, data):
                     changed = True
 
             if changed:
+                self.save_buffer(data.buffer)
                 self.interesting_examples[key] = data
                 self.shrunk_examples.discard(key)
                 if last_data_is_interesting and not first_call:
@@ -353,10 +354,9 @@ def record_for_health_check(self, data):
                 state.valid_examples, draw_time, state.invalid_examples,
                 state.overrun_examples), HealthCheck.too_slow,)
 
-    def save_buffer(self, buffer, key=None):
+    def save_buffer(self, buffer):
         if self.settings.database is not None:
-            if key is None:
-                key = self.database_key
+            key = self.database_key
             if key is None:
                 return
             self.settings.database.save(key, hbytes(buffer))
@@ -375,15 +375,6 @@ def covering_key(self):
         return b'.'.join((self.database_key, b'coverage'))
 
     def note_details(self, data):
-        if data.status == Status.INTERESTING:
-            if (
-                self.last_data is None or
-                self.last_data.status != Status.INTERESTING or
-                self.last_data.interesting_origin == data.interesting_origin
-            ):
-                self.save_buffer(data.buffer)
-            else:
-                self.save_buffer(data.buffer, self.secondary_key)
         runtime = max(data.finish_time - data.start_time, 0.0)
         self.all_runtimes.append(runtime)
         self.all_drawtimes.extend(data.draw_times)


Test patch:
diff --git a/tests/cover/test_conjecture_engine.py b/tests/cover/test_conjecture_engine.py
--- a/tests/cover/test_conjecture_engine.py
+++ b/tests/cover/test_conjecture_engine.py
@@ -706,6 +706,7 @@ def f(data):
     assert len(runner.interesting_examples) == 1
     for b in db.fetch(runner.secondary_key):
         assert b[0] >= 127
+    assert len(list(db.fetch(runner.database_key))) == 1
 
 
 def test_saves_negated_examples_in_covering():