Skip to content

Make refused sensitive-switch resets recover on the next key press - #15

Merged
RMANOV merged 3 commits into
masterfrom
claude/beautiful-thompson-aaiqyv
Oct 10, 2026
Merged

RMANOV merged 3 commits into
masterfrom
claude/beautiful-thompson-aaiqyv

Conversation

@RMANOV

@RMANOV RMANOV commented Oct 10, 2026 •

Copy link
Copy Markdown
Owner

Summary

A follow-up to #14, in three commits.

1. de177c9: simplify (no behaviour change)

  • _reset_for_sensitive_switch() -> bool becomes _switch_sensitivity(sensitive) -> None, so one helper owns arm, reset, cleanup and release.
  • The two consecutive if transition_in_flight: blocks are merged, and the guard reuses decision_changed.
  • The reset-refusal test fake is now a simple refusal counter.

2. 53f0d14: make the documented recovery real

Problem. IBus never delivers the same content type to one engine twice in a row:

  • the daemon checks its engine-proxy cache (bus/engineproxy.c bus_engine_proxy_set_content_type);
  • libibus checks the engine's own store, which starts at (0,0) (src/ibusengine.c);
  • focus-in pushes the context's value only when it differs (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_ordinary records 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_event retries 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 structural hard_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.
  • The transition guard now costs one word, not the field.
  • _switch_sensitivity treats any exception except KeyboardInterrupt/SystemExit as a refusal. pyo3's PanicException derives from BaseException and 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.
  • Leak closed: do_focus_out no longer calls core.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.
  • Docstrings corrected: the IBus dedup layers, the false "PASSWORD, then a never-declaring client, stays inert" residual, and the get_content_type() remark.

3. 7d74ce4: review fixes

  • The new native test overwrote ~/.config/smartkey/personal.json. Its wrapper forwarded the debounced save_personal() from do_focus_out to the real core. The wrapper now has a no-op save_personal. With HOME isolated, the full suite writes no file.
  • New tests pin 4 mutants that had survived:
    • a panic in the switch cleanup 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.
  • One existing assertion that passed for the wrong reason now asserts the owed release.
  • Docstring: strict mode keeps SmartKey inert until a declaration arrives, not "one dead keystroke".

Tests

  • IBus-faithful sequences use _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.
  • Locally, with a CI-equivalent venv, the native module built by maturin from 7d74ce4, SMARTKEY_REQUIRE_NATIVE_TESTS=1 and the provenance checks: 646 passed, 15 skipped, 0 failed. ruff reports the same 38 old findings as master, none new.

Review done before merge

  • Design: 3 independent designs, scored by 3 judges (fail-open adversary, IBus fidelity, maintainability).
  • Implementation review, 4 lenses with a skeptic for each finding:
    • A randomized model of IBus delivery to one global engine (3 contexts plus the daemon's fake context), run with both the fake and the real native core. Before this PR it fails 51/800 runs with a commit into a sensitive field (the focus-out flush). After it: 0/800, and 0/300 with the native core.
    • All 12 required mutants are killed.
    • One confirmed finding: the personal.json overwrite, fixed in 7d74ce4.

Residuals (all fail closed)

  • A core whose reset keeps failing keeps SmartKey inert. It retries once per key press (2 core calls) and warns once per run.
  • With 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.
  • The key-path release relies on GDBus dispatching method calls on one connection in FIFO order. I checked this in the GLib sources, not on a live system.

🤖 Generated with Claude Code

https://claude.ai/code/session_01887KpmB4KTMztPQcmXt7SD

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
@RMANOV RMANOV changed the title Simplify the sensitive-switch reset recovery Make refused sensitive-switch resets recover on the next key press Oct 10, 2026
- 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
@RMANOV
RMANOV marked this pull request as ready for review October 10, 2026 20:18
Copilot AI balanced review requested due to automatic review settings October 10, 2026 20:18
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@RMANOV
RMANOV merged commit 08cbc4b into master Oct 10, 2026
8 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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() -> bool into _switch_sensitivity(sensitive) -> None, which keeps the stop armed in both directions, treats any non-KeyboardInterrupt/SystemExit exception (including pyo3 PanicException) as a refusal, and tracks/logs refusal runs.
  • Adds _declared_ordinary state so do_process_key_event retries an owed reset on the next key press (not on releases, phantom keys, or focus-in), and prevents do_focus_out from 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants