Bug with `context.apply()`

@cowlicks -- looks like the new `context.apply()` method doesn't work with keys generated by `_generate_key()`.  I've added a minimal test that shows the bug here.  Please investigate.