Skip to content

修复工作目录选择器「远程路径自动补全」逐字符卡顿 - #42

Merged
flymysql merged 2 commits into
flymysql:mainfrom
GDWhisper:fix/picker-remote-path-autocomplete
Sep 28, 2026
Merged

flymysql merged 2 commits into
flymysql:mainfrom
GDWhisper:fix/picker-remote-path-autocomplete

Conversation

@GDWhisper

Copy link
Copy Markdown
Contributor

Closes #41

问题。 在工作目录选择器里输入远程路径时补全很慢:每敲一个字符都会断开重连一次 SSH、并重复列一遍同一个目录,真实网络下每个字符要卡 0.5–2 秒。

改了什么

  • 输入过程中保持 SSH 连接不断开,同一个目录只列一次——在某个目录里连续输入时,第一个字符之后补全基本秒出
  • 切换机器的语义不变,但现在会真正断开上一台机器的连接(自审时发现的正确性问题,一并修掉)
  • 明确的「连接」动作(设置页测试连接、rw_connect)总是重新拨号,不再复用可能已经半死的旧连接
  • 慢网络下更稳:过期的响应不会覆盖新的补全建议;同一目录的并发查询合并成一次请求;重新打开选择器一定看到最新的目录树
  • /dsh-remote/ls 响应新增一个附加小字段 machine(host/port/username),让选择器能察觉「当前机器被换掉了」

界面无变化;没有删除或改动任何既有接口(ls 只是新增字段)。

测试。 新增回归测试覆盖连接生命周期(test/apply-active-machine.test.js)和补全逻辑(test/picker-suggest.test.js),两组测试都在修复前验证过会红。全套 200/201 通过——唯一失败的 test/i18n.test.js 在 main 上就已失败,与本次无关。

范围。 两个提交:性能修复,然后是自审后的正确性修复,仅此而已。

…plete

The picker posted /dsh-remote/current before every /ls, and that route
re-applied the machine via pool.setTarget which unconditionally closed the
live SSH connection and reset the platform cache — every keystroke paid a
full reconnect + re-detect + readdir (the slow autocomplete bug).

- SshPool.setTarget: an unchanged target is now a no-op (keep connection)
- setCurrent: idempotent for the already-current machine (no registry
  rewrite, no credential re-read)
- picker client: POST /current only on machine switch (ensureCurrent),
  TTL-cached dir listings with in-flight dedup (fetchDirList), stale
  suggestion responses dropped via a request sequence number
- regression tests: test/settarget-noop.test.js, test/current-idempotent.test.js
  (verified red against pre-fix code, green with the fix)
Self-review of the autocomplete change found the setTarget no-op dead on
its primary path: applyMachine mutates the shared config (which IS
pool.config) BEFORE setTarget reads it, so machine switches kept the old
SSH client (wrong-host hazard). Plus client-side cache/switch defects from
the same change.

- setTarget gains an explicit 'previous' diff baseline; applyActiveMachine/
  clearActiveMachine snapshot the target fields before mutating the shared
  config (the critical regression)
- /connect and rw_connect adopt invalidate() as the forced-reconnect escape
  hatch so a probe never trusts a half-open cached client
- lastAppliedMachine is maintained inside applyActiveMachine so boot restore
  and rw_connect keep the invariant
- ensureCurrent: shared in-flight promise, rejects on failure, nothing cached
  after a failed switch
- /ls echoes its machine identity; the picker validates it and re-switches
  once on mismatch (external machine switches no longer poison the cache)
- picker reopen fully resets fetch state incl. in-flight map and seq;
  per-key fetch generation fences superseded cache writes; the seq-guarded
  pipeline is extracted to suggestFrom; continueSuggest fetches fresh
- invalidate() now fires onCloseHook like close() (forwards detach on both
  paths; stale close listeners can't tear down re-attached forwards)
- tests: apply-active-machine.test.js (production call shape, red 5/6 ->
  green 6/6), picker-suggest.test.js (12 extraction-based tests over the
  shipped client logic), red-green verified per finding
@flymysql
flymysql merged commit d3175e4 into flymysql:main Sep 28, 2026
1 check passed
flymysql pushed a commit that referenced this pull request Sep 28, 2026
…itch cannot redirect a pick

PR #42 removed the per-keystroke POST /current that used to keep the active
pool pinned to the picker's machine (that reconnect was the issue #41 bug).
With its client cache a cache-hit re-type issues NO host request at all, so
nothing re-applied the picker's machine before commitPath -- and /mirror
carried no identity, so the mirror was built from whichever machine happened
to be active.

Verified against a real host: with the picker showing jimmycppliu's tree on
9.134.186.191:36000 and an external 'set as current' to a second machine on
the same box, the mirror came out with the second machine's identity
(machineId=root). Pre-#42 masked this by accident: the pre-fix commit path
always re-posted /current first, so the probe landed on the picker's machine.

- /mirror and /home accept an optional machineId naming the machine the caller
  is showing; the host re-asserts it (setCurrent) BEFORE probing.
- An unknown id is REFUSED (404) rather than silently falling back to the
  active machine -- falling back is exactly the hazard.
- An absent id keeps the previous machine-scoped behaviour, so older clients
  and the settings page's workspace flow are unaffected.
- The picker sends machineId on both requests.

test/mirror-machine-identity.test.js covers all four orderings at route level
plus the client-side call shape; red-green verified (4/5 fail without the fix).
@flymysql

Copy link
Copy Markdown
Owner

Merged — with one follow-up needed (and now applied)

Thanks @GDWhisper, this is a good fix. Reviewed, verified against a real SSH host, and squash-merged as d3175e4.

Performance claim independently reproduced (real host, real SFTP, real routes; driving the real DirPicker helpers extracted from lib/client.js — not a mock):

pre-fix (main) this PR
warm keystroke → suggestions 1099 ms 31 ms
host requests per warm keystroke 2 (reconnect + readdir) 0
re-typing the same path 1146 ms 31 ms

The red-green claim also checks out: on pre-fix lib/ the new tests give 6 failures across settarget-noop / current-idempotent / apply-active-machine / picker-suggest; all green with your changes. Your self-review follow-up was genuinely necessary, not cosmetic — I confirmed the intermediate commit c2a404b really did have the dead no-op (applyActiveMachine mutating the shared config before setTarget read it), reproduced as 5 failures in apply-active-machine.test.js.

One issue your self-review didn't cover: a concurrent machine switch can redirect a pick

The PR removes the per-keystroke POST /current — and that was, unintentionally, the thing keeping the active pool pinned to the picker's machine. Combined with the new client cache, a cache-hit re-type issues no host request at all, so nothing re-applied the picker's machine before commitPath. /mirror carried no identity, so it used whichever machine was active at that moment.

The /ls identity echo doesn't cover this: the cache is keyed by machine id and a cache hit never validates anything.

Reproduced on a real host (9.134.186.191:36000), with the picker showing machine A's tree:

after typing on A: memo="m1" cacheKeys=1 suggest=["/home/jimmycppliu/QQMail"]
external switch → {activeUser: "root"}          # settings page / rw_connect elsewhere
after external switch: 0 host calls  (cache hit — no echo check ran)
commit → mirror identity: {"username":"root"}    # ✘ wrong machine, user never saw its tree

Pre-#42 masked this by accident: the old commitPath always re-posted /current first.

Follow-up committed to main (6df376e)

/mirror and /home now accept an optional machineId naming the machine the caller is showing, and the host re-asserts it before probing:

  • named machine → re-applied, so the commit lands on the intended identity
  • unknown id → refused (404), never a silent fall back to the active machine (falling back is the hazard)
  • absent id → unchanged machine-scoped behaviour, so older clients and the settings page's workspace flow are unaffected

The picker now sends machineId on both requests. Covered by test/mirror-machine-identity.test.js (4 route-level orderings + the client call shape); red-green verified 4/5 fail without it. All 209 tests pass, check.mjs and node --check clean, CI green on main.

I did not touch your PR branch — the fix rides a separate commit on top so the review stays readable. Nothing is released yet (npm is still 0.8.22); if you'd prefer this folded into the PR rather than a follow-up commit, say so and I'll restructure before the next release.

One more thing worth noting: rw_stat/rw_read_file and friends resolve their machine from the session's mirror meta, so they were never affected by this — the window was specific to the machine-scoped picker flow. That's also why the wrong identity is a real hazard rather than cosmetic: the mirror records it durably in .dsh-remote-meta.json, and every later session cwd-bound to that mirror resolves that machine.

flymysql pushed a commit that referenced this pull request Sep 28, 2026
…#41, PR #42) + machine identity hardening

Measured on a real host (9.134.186.191:36000, real SSH/SFTP with the real
DirPicker logic extracted from lib/client.js): warm keystroke -> suggestions
1099ms -> 31ms (35x), host requests per keystroke 2 -> 0.

Also ships the follow-up hardening for the wrong-host window PR #42 opened:
/mirror and /home accept an optional machineId naming the machine the caller
is showing, re-asserted by the host before probing; an unknown id is refused
(404) rather than silently falling back to the active machine.
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.

远程路径自动补全:每输入一个字符都重连 SSH,补全极慢

2 participants