Skip to content

fix(session): restore pane cwd when OSC 7 reports the local hostname - #537

Merged
tw93 merged 2 commits into
tw93:mainfrom
dufu1991:fix/session-restore-local-hostname-cwd
Aug 22, 2026
Merged

fix(session): restore pane cwd when OSC 7 reports the local hostname#537
tw93 merged 2 commits into
tw93:mainfrom
dufu1991:fix/session-restore-local-hostname-cwd

Conversation

@dufu1991

Copy link
Copy Markdown
Contributor

Problem

Session restore reopens tabs/splits correctly, but every pane lands in $HOME. last_session.json saves each pane's working_dir, but cwd_from_working_dir() converts it with url::Url::to_file_path(), which on unix refuses any host other than empty or localhost (verified against the url crate source: None | Some(Host::Domain("localhost")) => None, _ => return Err(())).

OSC 7 is conventionally reported as file://$HOST$PWD — WezTerm's own shell integration does exactly this — and the snapshot stores the URL verbatim. Verified live on macOS: emitting \033]7;file://<hostname>.local/tmp/x\033\\ into a pane makes kaku cli list report exactly that URL, and every entry in a real last_session.json shows the same shape (file://df-macbook-pro-14.local/Users/...). All of them fail to_file_path() -> cwd_for_restore() returns None -> panes spawn at the default cwd.

The ssh-domain branch of cwd_for_restore() already special-cases this to_file_path() limitation for remote hosts; local panes hit the same wall when the OSC 7 host is the local hostname.

Fix

In cwd_from_working_dir(), treat a file:// URL whose host identifies this machine as local: strip the host before calling to_file_path().

  • Accept both the full hostname and its first label (zsh $HOST, hostname, and scutil --get LocalHostName disagree on whether the domain part is included).
  • Compare case-insensitively: the URL parser lowercases the host, while macOS hostnames are often mixed case.
  • Tolerate a trailing dot (FQDN form).
  • Any other host still fails conversion, preserving the existing guarantee that a leftover remote cwd cannot leak into a local spawn.

Tests

  • Extended restore_cwd_local_domain_requires_local_url with a localhost case; the remote-host rejection case is unchanged.
  • Added restore_cwd_local_domain_accepts_local_hostname: real hostname (uppercased to prove case-insensitivity), short hostname, and trailing-dot FQDN. The hostname is read at runtime, so the test is machine-independent.

Verification

  • make fmt-check passes.
  • cargo test --locked -p kaku-gui session_restore covers the new and existing restore-cwd tests (cold full-workspace build is slow on the author's machine; the checks workflow covers fmt/check/tests on the PR).

中文摘要

问题:会话恢复后所有 pane 都打开在 $HOME。快照里保存的 working_dir 是带本机真实主机名的 file:// URL(OSC 7 的惯例格式 file://$HOST$PWD),而恢复端 cwd_from_working_dir() 调用的 url::Url::to_file_path() 在 unix 上拒绝任何非空、非 localhost 的 host,导致路径转换失败、pane 以默认目录启动。ssh 分支之前已经针对这个限制做过处理,但本地 pane 在 host 是本机主机名时撞上了同一堵墙。

修复:转换前把"本机主机名"(全名或首段、忽略大小写、容忍 FQDN 尾点)识别为本地并剥离 host;其他 host 依旧拒绝,保留"远程路径不得泄漏进本地 spawn"的防护。附带覆盖本机主机名三种形态的单元测试,并通过 make fmt-check

cwd_from_working_dir() converted the saved working_dir URL with
url::Url::to_file_path(), which on unix refuses any host other than
empty or "localhost". OSC 7 is conventionally reported as
file://$HOST$PWD with the real local hostname, so every pane's saved
cwd failed conversion and restored panes spawned in $HOME.

Treat file URLs whose host matches this machine's hostname (full or
first label, case-insensitive, optional trailing dot) as local by
stripping the host before conversion. Any other host still fails, so
a remote cwd cannot leak into a local spawn.
@vercel

vercel Bot commented Aug 17, 2026

Copy link
Copy Markdown

@dufu1991 is attempting to deploy a commit to the Faberon Team on Vercel.

A member of the Team first needs to authorize it.

Rewrite a verified local hostname to localhost so the url crate can convert the saved file URL back into a path. Share the exact hostname policy with AI context handling and reject ambiguous short-name aliases from nested SSH sessions.
@tw93
tw93 merged commit a377cff into tw93:main Aug 22, 2026
7 of 8 checks passed
@tw93

tw93 commented Aug 22, 2026

Copy link
Copy Markdown
Owner

@dufu1991 Thanks for tracking down the OSC 7 restore case and adding the initial coverage. I tightened hostname validation so a same-named host inside a regular SSH session is not treated as local, and the fix is now merged.

It is available in the latest Nightly: https://github.com/tw93/Kaku/releases/download/nightly/Kaku-nightly.dmg

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.

2 participants