Skip to content

Commit e2747d5

Browse files
takemi-ohamaclaude
andcommitted
perf(editor): ssh host 自動検出を mtime 降順・最初の一致で打ち切り
_detect_ssh_host_from_vscode が全 entries.json を読んでいたのを、mtime 降順で 新しいファイルから順に読み最初の ssh-remote 一致で即 return する実装へ変更。 History が巨大でも全読み込みを避け devbase up の遅延を防ぐ (gemini 指摘 / major)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 2b74562 commit e2747d5

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

lib/devbase/editor/opener.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -342,24 +342,28 @@ def _detect_ssh_host_from_vscode(vscode_server_dir: str) -> Optional[str]:
342342
history = os.path.join(vscode_server_dir, "data", "User", "History")
343343
if not os.path.isdir(history):
344344
return None
345-
best: dict = {} # host -> 最新 mtime
345+
# entries.json 候補を mtime 降順で集め、**新しい方から 1 ファイルずつ読み、最初に
346+
# ssh-remote ホストが見つかった時点で即 return** する (History が数千ファイルに
347+
# 膨れても全読み込みを避け、devbase up の遅延を防ぐ)。mtime 収集は stat のみで安価。
348+
candidates = []
346349
for root, _dirs, files in os.walk(history):
347-
for name in files:
348-
if name != "entries.json": # resource authority は entries.json に載る
349-
continue
350-
path = os.path.join(root, name)
351-
try:
352-
mtime = os.path.getmtime(path)
353-
with open(path, encoding="utf-8", errors="ignore") as f:
354-
text = f.read()
355-
except OSError:
356-
continue
357-
for host in _SSH_REMOTE_RE.findall(text):
358-
if host and (host not in best or mtime > best[host]):
359-
best[host] = mtime
360-
if not best:
361-
return None
362-
return max(best, key=best.get)
350+
if "entries.json" not in files: # resource authority は entries.json に載る
351+
continue
352+
path = os.path.join(root, "entries.json")
353+
try:
354+
candidates.append((os.path.getmtime(path), path))
355+
except OSError:
356+
continue
357+
for _mtime, path in sorted(candidates, key=lambda t: t[0], reverse=True):
358+
try:
359+
with open(path, encoding="utf-8", errors="ignore") as f:
360+
text = f.read()
361+
except OSError:
362+
continue
363+
match = _SSH_REMOTE_RE.search(text)
364+
if match:
365+
return match.group(1)
366+
return None
363367

364368

365369
def resolve_editor_ssh_host(environ=None,

0 commit comments

Comments
 (0)