Repository navigation
Description
The host-triggered source-mode switch can load stale Markdown while a code edit is waiting for delayed synchronization. Switching back to preview discards the latest text, and a subsequent save emits the old content.
Validated on main at d50e1fd1ad080fadf416098a582fe0a277d1ad43 (package version 0.2.1), 2026-09-16 UTC. Environment: macOS 26.5.2, arm64.
Reproduction at the webview boundary
Start with:
```
alpha
```- Click the code, move to the end, and type
updated. - Immediately deliver the normal host message
{ type: 'toggleSourceMode' }without first moving focus to the toolbar. - Inspect the source textarea, switch back with the same host message, then save.
Expected: source mode and the later save contain alpha updated.
Observed: source mode contains alpha; switching back removes the visible edit, and the save payload still contains alpha after waiting for the pending timers.
| Route/control | Real-time trials | Result |
|---|---|---|
| Immediate host source-mode message after typing | 3/3 | Latest edit lost |
| Wait 1.8 seconds, then send the host message | 3/3 | Edit preserved |
| Immediate click on the source toolbar button | 3/3 | Edit preserved |
| Save before clicking the source toolbar button | 3/3 | Edit preserved |
A separate controlled-clock run reproduced the pending-synchronization case. The three immediate host trials above used real timers.
Cause and suggested scope
The registered Toggle Source Mode command calls a provider method that posts this exact message.
toggleSourceMode changes isSourceMode and assigns the cached markdown to the textarea before committing pending visual edits. Synchronization is delayed. The blur flush is guarded by !isSourceMode, which also explains why changing focus through the toolbar can mask the failure.
Capture current visual content before changing modes and invalidate stale queued callbacks. Reuse the existing synchronization/save contracts. Add tests for the host route, immediate source edits after switching, delayed callbacks, undo/redo, and active inline equations; changing the toolbar alone would miss the failing route.
Validation boundary
Production webview HTML/CSS/JavaScript ran in Chromium with real keyboard input and the production host-message listener. VS Code API messaging was mocked. The command-to-message mapping was verified in source; the installed VS Code keyboard shortcut itself was not exercised. This report uses synthetic content only.
Activity