Repository navigation
Make refused sensitive-switch resets recover on the next key press - #15
Conversation
Let one helper own the whole sensitivity transition instead of splitting it between the helper and its only caller. `_switch_sensitivity(target)` arms the hard stop, resets the core, runs the adapter cleanup, and only then releases the stop if the reset succeeded. The old bool return and the arm/gate steps at the call site are gone. Behaviour is unchanged. Also merge the two consecutive `if transition_in_flight:` blocks, reuse `decision_changed`, and reduce the reset-refusal test fake to a simple refusal counter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01887KpmB4KTMztPQcmXt7SD
IBus never delivers the same content type to one engine twice in a row: the daemon checks its engine-proxy cache, libibus checks the engine's own store, and focus-in pushes the context's value only when it differs. So the recovery path from the previous fix (a repeated ordinary declaration retries the refused core reset) could never happen, and SmartKey stayed inert for the rest of the field. The transition guard had the same problem. - Record whether the latest declaration itself was ordinary (`_declared_ordinary`, taken before the transition-guard override and withdrawn first on every declaration). While the hard stop is still armed, that release is owed. - Retry the owed reset on the next real key press (not releases, not phantom keycode-240 events). IBus orders that key after every declaration it pushed for the focused field. On success the stop is released, a structural `hard_stop` trace event is emitted, and the key is processed normally. Focus-in is never a release point. - The transition guard now costs one word, not the field. - Treat any exception except KeyboardInterrupt/SystemExit as a refusal: pyo3's PanicException derives from BaseException and used to skip the adapter cleanup. Warn once per run of refusals (type name only), debug after, and log the recovery. - Do not call core.focus_lost() while the hard stop is armed. After a refused reset on entering a sensitive field, it committed and learned the previous field's word. - Correct the docstrings and tests that relied on IBus re-sending an identical content type. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01887KpmB4KTMztPQcmXt7SD
- The native stale-word test wrapped the real core with a forwarding __getattr__, so the debounced save in do_focus_out reached save_personal() and overwrote ~/.config/smartkey/personal.json with an empty test profile. Give the wrapper a no-op save_personal. - Add tests for mutants that survived review: a panic in the switch cleanup (hide_preedit_text / set_surrounding_text) neither escapes nor skips it; KeyboardInterrupt and SystemExit propagate with the stop armed on the entering, leaving and key-press paths; a new run of refusals warns again after a recovery. - test_content_type_change_mid_composition_fails_safe: assert the owed release instead of a final key that now passed for the wrong reason. - Docstring: strict SMARTKEY_SENSITIVE_UNTIL_DECLARED mode keeps SmartKey inert until a declaration arrives, not "one dead keystroke"; a never-declaring client's context stays at FREE_FORM/NONE. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01887KpmB4KTMztPQcmXt7SD
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
🔵 Needs a closer look
It modifies a security-critical sensitive-input kill switch whose correctness depends on subtle fail-closed invariants and IBus/D-Bus delivery-ordering assumptions, warranting final human review.
0 open findings
What changed in this PR
This PR is a follow-up to #14 that makes SmartKey's IBus sensitive-field "hard stop" recover correctly when a native-core reset is refused. The key insight is that IBus never re-delivers an identical content type to a single global engine, so the previous design's recovery path (retrying on a repeated ordinary declaration) could never fire—leaving SmartKey permanently inert in a field after a refused reset, and leaking the previous field's word via focus_lost() on focus-out. The fix defers the owed reset to the next real key press and stops flushing the core while the hard stop is armed.
Changes:
- Refactors
_reset_for_sensitive_switch() -> boolinto_switch_sensitivity(sensitive) -> None, which keeps the stop armed in both directions, treats any non-KeyboardInterrupt/SystemExitexception (including pyo3PanicException) as a refusal, and tracks/logs refusal runs. - Adds
_declared_ordinarystate sodo_process_key_eventretries an owed reset on the next key press (not on releases, phantom keys, or focus-in), and preventsdo_focus_outfrom flushing a dirty core while sensitive (the password-leak fix). - Rewrites docstrings/comments to describe the real IBus dedup layers and adds extensive IBus-faithful tests (including one against the real native core).
| File | Description |
|---|---|
ibus/smartkey_engine.py |
Core logic: _switch_sensitivity refactor, _UNCATCHABLE fail-closed handling, owed-release retry on key press, and the focus-out no-flush leak fix. |
ibus/test_input_safety.py |
New/updated IBus-faithful tests for refused-reset recovery, transition-guard recovery, panic/interpreter-exit handling, and the focus-out leak, plus a native-core seam test. |
ibus/test_space_accept.py |
Updates the sticky-declaration lifecycle test to assert the hard stop never flushes the core on focus-out. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Summary
A follow-up to #14, in three commits.
1.
de177c9: simplify (no behaviour change)_reset_for_sensitive_switch() -> boolbecomes_switch_sensitivity(sensitive) -> None, so one helper owns arm, reset, cleanup and release.if transition_in_flight:blocks are merged, and the guard reusesdecision_changed.2.
53f0d14: make the documented recovery realProblem. IBus never delivers the same content type to one engine twice in a row:
bus/engineproxy.cbus_engine_proxy_set_content_type);src/ibusengine.c);bus/inputcontext.c).Global-engine mode is hard-coded on, so one engine object serves every context. I checked this against the IBus 1.5.30, 1.5.34 and main sources.
So the recovery path from #14, where a repeated ordinary declaration retries a refused core reset, can't happen. After a refused reset SmartKey stayed inert for the rest of the field. The transition guard had the same problem even with a healthy core.
Fix
_declared_ordinaryrecords whether the latest declaration itself was ordinary. It is taken before the transition-guard override, and every declaration withdraws it first. While the hard stop is still armed, that release is owed.do_process_key_eventretries the owed reset on the next real key press: not on releases, and not on phantom keycode-240 events. IBus orders that key after every declaration it pushed for the focused field. On success the stop is released, a structuralhard_stop{seq:0, transition:"released", cause:"deferred_reset"}trace event is emitted, and the key is processed normally. Focus-in is never a release point, because IBus sends FocusIn before the new field's content type._switch_sensitivitytreats any exception exceptKeyboardInterrupt/SystemExitas a refusal. pyo3'sPanicExceptionderives fromBaseExceptionand used to skip the adapter cleanup. The first refusal in a row logs a warning with the exception type only; repeats log at debug level; the recovery logs at info.do_focus_outno longer callscore.focus_lost()while the hard stop is armed. After a refused reset on entering a sensitive field, the core still held the previous field's word, and focus-out committed and learned it.get_content_type()remark.3.
7d74ce4: review fixes~/.config/smartkey/personal.json. Its wrapper forwarded the debouncedsave_personal()fromdo_focus_outto the real core. The wrapper now has a no-opsave_personal. With HOME isolated, the full suite writes no file.KeyboardInterruptandSystemExitpropagate with the stop armed on the entering, leaving and key-press paths;Tests
_IBusContentTypeFeed, which never delivers an identical value twice in a row. They cover the refused-reset recovery, the transition-guard recovery, and the cases where a key release, a phantom key or focus-in must not retry. Also covered: a later sensitive declaration cancels the owed release; a declaration that fails half-way withdraws it; strict mode is never released by the key path; focus-out never flushes a dirty core (with a positive control); panic and interpreter-exit handling. One test runs against the real native core.7d74ce4,SMARTKEY_REQUIRE_NATIVE_TESTS=1and the provenance checks: 646 passed, 15 skipped, 0 failed.ruffreports the same 38 old findings asmaster, none new.Review done before merge
personal.jsonoverwrite, fixed in7d74ce4.Residuals (all fail closed)
SMARTKEY_SENSITIVE_UNTIL_DECLARED=1, a FREE_FORM/NONE field is never declared to a fresh engine, so the stop holds until some other value and then an ordinary one are delivered.🤖 Generated with Claude Code
https://claude.ai/code/session_01887KpmB4KTMztPQcmXt7SD