csm-cli 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {csm_cli-0.1.0 → csm_cli-0.2.0}/.gitignore +3 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/CHANGELOG.md +5 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/PKG-INFO +12 -2
- {csm_cli-0.1.0 → csm_cli-0.2.0}/README.md +11 -1
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/__init__.py +1 -1
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/cli.py +32 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/fmt.py +21 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/scanner.py +125 -2
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/ui.py +16 -1
- csm_cli-0.2.0/test_csm.py +116 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/test_scanner.py +62 -5
- csm_cli-0.1.0/test_csm.py +0 -55
- {csm_cli-0.1.0 → csm_cli-0.2.0}/LICENSE +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/pyproject.toml +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/__main__.py +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/serve.py +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/web.html +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/src/csm_cli/web.py +0 -0
- {csm_cli-0.1.0 → csm_cli-0.2.0}/test_fmt.py +0 -0
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.2.0 (2026-11-22)
|
|
4
|
+
|
|
5
|
+
- TUI `g` 跳转:定位会话所在终端并直达——tmux 按 tty 反查 pane(csm 在 tmux 内 `switch-client` 瞬移、在外 `execvp` 让位 `attach`)、iTerm2/Terminal.app AppleScript 聚焦、全 miss 降级提示(`fmt.fallback_location_hint`)
|
|
6
|
+
- 修复 tty 口径漂移:`current_terminal_tty()` 快路径返回的 `os.ttyname` 带 `/dev/` 前缀,与 ps 快照的无前缀口径不一致,导致直接在终端运行 csm 时 ◉本终端标记与双开保护的本终端豁免恒不命中;现统一归一化为 ps 口径
|
|
7
|
+
|
|
3
8
|
## 0.1.0 (2026-11-22)
|
|
4
9
|
|
|
5
10
|
首个正式发布。四轮功能迭代汇总:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: csm-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Claude Code 会话管理器:一屏浏览/过滤/收藏全部会话,回车 resume;TUI + 本地 Web 界面
|
|
5
5
|
Project-URL: Homepage, https://github.com/cainli/csm
|
|
6
6
|
Project-URL: Issues, https://github.com/cainli/csm/issues
|
|
@@ -51,6 +51,7 @@ Description-Content-Type: text/markdown
|
|
|
51
51
|
| 对话流预览(Markdown 渲染 + 工具调用折叠) | ✅ Tab 最近 50 条 | ✅ 完整分页 | — |
|
|
52
52
|
| **Edit/Write/MultiEdit 行级 diff**(红删绿增) | — | ✅ 完整级别 | — |
|
|
53
53
|
| **双开保护**(活会话在他终端 → 确认拦截) | ✅ | — | — |
|
|
54
|
+
| **跳转 `g`**(直达会话所在终端:tmux switch-client/attach · iTerm2/Terminal 聚焦) | ✅ | — | — |
|
|
54
55
|
| 复制 resume 命令(终端/tmux/headless 三变体) | — | ✅ | — |
|
|
55
56
|
| SDK 子进程会话标记(`sdk-cli` → SDK) | ✅ | ✅ | ✅ |
|
|
56
57
|
| **Web 后台守护**(`csm serve start/stop/status`,launchd 开机自启) | — | ✅ | — |
|
|
@@ -118,7 +119,7 @@ launchd 模式日志在 `~/.claude/csm/launchd.{out,err}.log`;停 launchd 管
|
|
|
118
119
|
## TUI
|
|
119
120
|
|
|
120
121
|
```
|
|
121
|
-
csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星标 Tab预览 ^L清空 ^R重扫 esc退出
|
|
122
|
+
csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星标 g跳转 Tab预览 ^L清空 ^R重扫 esc退出
|
|
122
123
|
◉★ 11:20 ●busy 修复登录超时问题 #线上 claude-tools/fleet-features 12 757f8670-…
|
|
123
124
|
10:29 ●idle Orca 多 Agent 开发环境 swain 8 892a5d70-…
|
|
124
125
|
```
|
|
@@ -132,6 +133,7 @@ csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星
|
|
|
132
133
|
| 直接打字 | 过滤(标题/首输入/项目路径/SID,多词 AND;`#标签` 精确筛) | 同左 |
|
|
133
134
|
| `1`-`6` | **分类过滤**:全部/运行中/本终端/收藏/已关闭/单轮 | — |
|
|
134
135
|
| `s` | **星标循环**:无 → ⭐ → ★ → 无(收藏永久置顶) | — |
|
|
136
|
+
| `g` | **跳转**到会话所在终端(见下) | — |
|
|
135
137
|
| `Tab` | **进入会话预览**(最近 50 条消息流) | 返回列表(保持原选择) |
|
|
136
138
|
| `Enter` | resume 选中会话(先 cd 到原项目目录;见他终端运行会触发双开确认) | resume 该会话 |
|
|
137
139
|
| `Ctrl-L` / `Ctrl-R` | 清空过滤 / 重新扫描 | 清空 / 刷新预览 |
|
|
@@ -158,6 +160,14 @@ csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星
|
|
|
158
160
|
|
|
159
161
|
resume 一个正在**其他终端**运行的会话时,csm 警告并要求确认——两处同时写入同一会话文件(jsonl)可能导致内容交叉错乱。本终端运行的会话不拦;tty 无法识别时宁可误拦。
|
|
160
162
|
|
|
163
|
+
### 跳转 `g`:会话在哪,就去哪
|
|
164
|
+
|
|
165
|
+
双开保护的另一半答案:不另起一份,而是**直达会话正在运行的终端**。按目标环境自动选择(全部只读查询):
|
|
166
|
+
|
|
167
|
+
- 会话在 **tmux**(任意 session):按 tty 反查 pane。csm 自己也在 tmux → `switch-client` 瞬移(csm 存活,随时切回列表);不在 → `execvp` 让位给 `tmux attach`(resume 同款哲学)
|
|
168
|
+
- 会话在 **iTerm2 / Terminal.app**:AppleScript 按 tty 找到 tab/窗口并置前
|
|
169
|
+
- 都不是:降级提示 tty(`fmt.fallback_location_hint`)
|
|
170
|
+
|
|
161
171
|
### `--new`:快速发射
|
|
162
172
|
|
|
163
173
|
`csm --new ~/dev/workspace/swain` 直接在该目录起新会话。与 resume 同为 `execvp` 接管——csm 始终不持有会话进程。
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
| 对话流预览(Markdown 渲染 + 工具调用折叠) | ✅ Tab 最近 50 条 | ✅ 完整分页 | — |
|
|
22
22
|
| **Edit/Write/MultiEdit 行级 diff**(红删绿增) | — | ✅ 完整级别 | — |
|
|
23
23
|
| **双开保护**(活会话在他终端 → 确认拦截) | ✅ | — | — |
|
|
24
|
+
| **跳转 `g`**(直达会话所在终端:tmux switch-client/attach · iTerm2/Terminal 聚焦) | ✅ | — | — |
|
|
24
25
|
| 复制 resume 命令(终端/tmux/headless 三变体) | — | ✅ | — |
|
|
25
26
|
| SDK 子进程会话标记(`sdk-cli` → SDK) | ✅ | ✅ | ✅ |
|
|
26
27
|
| **Web 后台守护**(`csm serve start/stop/status`,launchd 开机自启) | — | ✅ | — |
|
|
@@ -88,7 +89,7 @@ launchd 模式日志在 `~/.claude/csm/launchd.{out,err}.log`;停 launchd 管
|
|
|
88
89
|
## TUI
|
|
89
90
|
|
|
90
91
|
```
|
|
91
|
-
csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星标 Tab预览 ^L清空 ^R重扫 esc退出
|
|
92
|
+
csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星标 g跳转 Tab预览 ^L清空 ^R重扫 esc退出
|
|
92
93
|
◉★ 11:20 ●busy 修复登录超时问题 #线上 claude-tools/fleet-features 12 757f8670-…
|
|
93
94
|
10:29 ●idle Orca 多 Agent 开发环境 swain 8 892a5d70-…
|
|
94
95
|
```
|
|
@@ -102,6 +103,7 @@ csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星
|
|
|
102
103
|
| 直接打字 | 过滤(标题/首输入/项目路径/SID,多词 AND;`#标签` 精确筛) | 同左 |
|
|
103
104
|
| `1`-`6` | **分类过滤**:全部/运行中/本终端/收藏/已关闭/单轮 | — |
|
|
104
105
|
| `s` | **星标循环**:无 → ⭐ → ★ → 无(收藏永久置顶) | — |
|
|
106
|
+
| `g` | **跳转**到会话所在终端(见下) | — |
|
|
105
107
|
| `Tab` | **进入会话预览**(最近 50 条消息流) | 返回列表(保持原选择) |
|
|
106
108
|
| `Enter` | resume 选中会话(先 cd 到原项目目录;见他终端运行会触发双开确认) | resume 该会话 |
|
|
107
109
|
| `Ctrl-L` / `Ctrl-R` | 清空过滤 / 重新扫描 | 清空 / 刷新预览 |
|
|
@@ -128,6 +130,14 @@ csm — 77 会话 (22 运行中) ★3 ⭐7 过滤:全部 68/77 1-6过滤 s星
|
|
|
128
130
|
|
|
129
131
|
resume 一个正在**其他终端**运行的会话时,csm 警告并要求确认——两处同时写入同一会话文件(jsonl)可能导致内容交叉错乱。本终端运行的会话不拦;tty 无法识别时宁可误拦。
|
|
130
132
|
|
|
133
|
+
### 跳转 `g`:会话在哪,就去哪
|
|
134
|
+
|
|
135
|
+
双开保护的另一半答案:不另起一份,而是**直达会话正在运行的终端**。按目标环境自动选择(全部只读查询):
|
|
136
|
+
|
|
137
|
+
- 会话在 **tmux**(任意 session):按 tty 反查 pane。csm 自己也在 tmux → `switch-client` 瞬移(csm 存活,随时切回列表);不在 → `execvp` 让位给 `tmux attach`(resume 同款哲学)
|
|
138
|
+
- 会话在 **iTerm2 / Terminal.app**:AppleScript 按 tty 找到 tab/窗口并置前
|
|
139
|
+
- 都不是:降级提示 tty(`fmt.fallback_location_hint`)
|
|
140
|
+
|
|
131
141
|
### `--new`:快速发射
|
|
132
142
|
|
|
133
143
|
`csm --new ~/dev/workspace/swain` 直接在该目录起新会话。与 resume 同为 `execvp` 接管——csm 始终不持有会话进程。
|
|
@@ -186,6 +186,38 @@ def resume(entry: scanner.SessionEntry, no_cd: bool = False) -> None:
|
|
|
186
186
|
os.execvp("claude", ["claude", "--resume", entry.session_id])
|
|
187
187
|
|
|
188
188
|
|
|
189
|
+
def jump_to_terminal(entry: scanner.SessionEntry) -> bool:
|
|
190
|
+
"""跳到会话所在终端:tmux switch-client / execvp attach / GUI 聚焦 / 降级打印。
|
|
191
|
+
|
|
192
|
+
返回 True = 已处理(switch-client / 聚焦成功,csm 存活可继续);
|
|
193
|
+
False = 未跳转(原因已打印到 stderr)。attach 分支 execvp 不返回。
|
|
194
|
+
与 resume 同款让位哲学:csm 从不持有会话或终端,送达即退。
|
|
195
|
+
"""
|
|
196
|
+
import subprocess
|
|
197
|
+
|
|
198
|
+
if not entry.tty:
|
|
199
|
+
print("该会话未在运行或所在终端未知,无法跳转", file=sys.stderr)
|
|
200
|
+
return False
|
|
201
|
+
target = scanner.locate_tty_in_tmux(entry.tty)
|
|
202
|
+
if target is not None:
|
|
203
|
+
if scanner.in_tmux():
|
|
204
|
+
# switch-client 只切当前 client 的显示,csm 存活,用户可切回列表
|
|
205
|
+
try:
|
|
206
|
+
subprocess.run(["tmux", "switch-client", "-t", target],
|
|
207
|
+
check=True, timeout=2)
|
|
208
|
+
except (OSError, subprocess.SubprocessError) as exc:
|
|
209
|
+
print(f"跳转失败(tmux switch-client -t {target}): {exc}", file=sys.stderr)
|
|
210
|
+
return False
|
|
211
|
+
return True
|
|
212
|
+
# csm 不在 tmux:让位给 attach(resume 同款 execvp),attach 最细到 window
|
|
213
|
+
os.execvp("tmux", ["tmux", "attach-session", "-t", target.rsplit(".", 1)[0]])
|
|
214
|
+
if scanner.focus_gui_terminal(entry.tty):
|
|
215
|
+
return True
|
|
216
|
+
from csm_cli import fmt # 惰性:fmt 依赖 prompt_toolkit,--list 主路径不加载
|
|
217
|
+
print(fmt.fallback_location_hint(entry.tty), file=sys.stderr)
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
|
|
189
221
|
def resolve_new_target(path: str) -> Path | None:
|
|
190
222
|
"""--new 的目标目录解析:展开 ~;不存在或不是目录返回 None(可测纯函数)。"""
|
|
191
223
|
target = Path(path).expanduser()
|
|
@@ -113,3 +113,24 @@ def parse_query(text: str) -> tuple[list[str], list[str]]:
|
|
|
113
113
|
else:
|
|
114
114
|
plain.append(tok)
|
|
115
115
|
return plain, tags
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
# ---------------------------------------------------------------------------
|
|
119
|
+
# 终端定位降级提示
|
|
120
|
+
# ---------------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def fallback_location_hint(tty: str) -> str:
|
|
124
|
+
"""会话运行终端定位全部失败(非 tmux、非 iTerm2/Terminal.app)时的提示文案。
|
|
125
|
+
|
|
126
|
+
与 rel_time()/fuzzy_score() 不同:调用方(跳转键)没有再降级的分支,
|
|
127
|
+
本函数必须始终返回可用字符串。
|
|
128
|
+
|
|
129
|
+
TODO(user): 当前是最小实现(只报 tty)。这行文案是产品决策——你来定信息密度:
|
|
130
|
+
- 只报 tty(现状):最短,但用户拿到 ttys012 干不了什么
|
|
131
|
+
- 附排查命令:如 `ps -t ttys012 -o pid,command`(列出该终端在跑什么),
|
|
132
|
+
用户复制即查——但 TUI 的 _notice 是单行 2 秒提示,塞得下吗?
|
|
133
|
+
- 提示性文案:说明仅支持 tmux/iTerm2/Terminal.app 自动跳转
|
|
134
|
+
tty 为 ps 口径(无 /dev/ 前缀);命令若需要,写成 /dev/{tty} 更符合习惯。
|
|
135
|
+
"""
|
|
136
|
+
return f"未定位到运行终端({tty}),仅支持 tmux/iTerm2/Terminal.app 跳转"
|
|
@@ -480,8 +480,13 @@ def ps_snapshot() -> dict[int, PsInfo]:
|
|
|
480
480
|
return table
|
|
481
481
|
|
|
482
482
|
|
|
483
|
+
def _norm_tty(name: str) -> str:
|
|
484
|
+
"""统一 tty 口径为 ps 形式(ttys012):tmux pane_tty / os.ttyname / iTerm 都带 /dev/ 前缀。"""
|
|
485
|
+
return name.removeprefix("/dev/")
|
|
486
|
+
|
|
487
|
+
|
|
483
488
|
def current_terminal_tty(table: dict[int, PsInfo] | None = None) -> str | None:
|
|
484
|
-
"""当前终端 tty
|
|
489
|
+
"""当前终端 tty(ps 口径,无 /dev/ 前缀——与 PsInfo.tty 直接可比)。
|
|
485
490
|
|
|
486
491
|
快路径 os.ttyname(0/1/2)(用户直接在终端跑 csm 时零开销命中);
|
|
487
492
|
失败(被工具嵌套调用、stdin 非 tty)则沿 PPID 链上溯 ps 快照,
|
|
@@ -489,7 +494,7 @@ def current_terminal_tty(table: dict[int, PsInfo] | None = None) -> str | None:
|
|
|
489
494
|
"""
|
|
490
495
|
for fd in (0, 1, 2):
|
|
491
496
|
try:
|
|
492
|
-
return os.ttyname(fd)
|
|
497
|
+
return _norm_tty(os.ttyname(fd))
|
|
493
498
|
except OSError:
|
|
494
499
|
continue
|
|
495
500
|
table = table if table is not None else ps_snapshot()
|
|
@@ -506,6 +511,124 @@ def current_terminal_tty(table: dict[int, PsInfo] | None = None) -> str | None:
|
|
|
506
511
|
return None
|
|
507
512
|
|
|
508
513
|
|
|
514
|
+
# ---------------------------------------------------------------------------
|
|
515
|
+
# 终端定位(跳转到会话所在终端)
|
|
516
|
+
# ---------------------------------------------------------------------------
|
|
517
|
+
|
|
518
|
+
_TMUX_TARGET_RE = re.compile(r"^\S+:\d+\.\d+$") # session:window.pane
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
def parse_tmux_panes(stdout: str) -> dict[str, str]:
|
|
522
|
+
"""解析 `tmux list-panes -a -F '#{pane_tty} #{session_name}:#{window_index}.#{pane_index}'`。
|
|
523
|
+
|
|
524
|
+
返回 归一化 tty → "session:window.pane"。坏行静默跳过(与坏 JSON 行同口径)。
|
|
525
|
+
"""
|
|
526
|
+
table: dict[str, str] = {}
|
|
527
|
+
for line in stdout.splitlines():
|
|
528
|
+
parts = line.split(None, 1)
|
|
529
|
+
if len(parts) != 2:
|
|
530
|
+
continue
|
|
531
|
+
tty, target = parts[0].strip(), parts[1].strip()
|
|
532
|
+
if not tty or not _TMUX_TARGET_RE.match(target):
|
|
533
|
+
continue
|
|
534
|
+
table[_norm_tty(tty)] = target
|
|
535
|
+
return table
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
def locate_tty_in_tmux(tty: str, timeout: float = 2.0) -> str | None:
|
|
539
|
+
"""tty 出现在任意 tmux pane → 返回 "session:window.pane" target;否则 None。
|
|
540
|
+
|
|
541
|
+
与 csm 自身是否在 tmux 无关(目标会话可以在别的 tmux client 里)。
|
|
542
|
+
tmux 缺失 / server 未起 / 超时 / 未命中均 None,调用方按序降级。
|
|
543
|
+
"""
|
|
544
|
+
import shutil
|
|
545
|
+
import subprocess
|
|
546
|
+
|
|
547
|
+
if shutil.which("tmux") is None:
|
|
548
|
+
return None
|
|
549
|
+
try:
|
|
550
|
+
proc = subprocess.run(
|
|
551
|
+
["tmux", "list-panes", "-a", "-F",
|
|
552
|
+
"#{pane_tty} #{session_name}:#{window_index}.#{pane_index}"],
|
|
553
|
+
capture_output=True, text=True, timeout=timeout,
|
|
554
|
+
)
|
|
555
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
556
|
+
return None
|
|
557
|
+
if proc.returncode != 0:
|
|
558
|
+
return None # server 未起等
|
|
559
|
+
return parse_tmux_panes(proc.stdout).get(_norm_tty(tty))
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
def in_tmux() -> bool:
|
|
563
|
+
"""csm 自身是否运行在 tmux 内($TMUX 仅由 tmux 为其 client 进程设置)。"""
|
|
564
|
+
return bool(os.environ.get("TMUX"))
|
|
565
|
+
|
|
566
|
+
|
|
567
|
+
def _iterm2_focus_script(tty: str) -> str:
|
|
568
|
+
"""iTerm2 AppleScript:按 tty 找 session,置前其 tab 与 window。"""
|
|
569
|
+
return (
|
|
570
|
+
'tell application "iTerm2"\n'
|
|
571
|
+
" repeat with w in windows\n"
|
|
572
|
+
" repeat with t in tabs of w\n"
|
|
573
|
+
" repeat with s in sessions of t\n"
|
|
574
|
+
f' if (tty of s) is "/dev/{tty}" then\n'
|
|
575
|
+
" select t\n"
|
|
576
|
+
" select w\n"
|
|
577
|
+
" activate\n"
|
|
578
|
+
" return true\n"
|
|
579
|
+
" end if\n"
|
|
580
|
+
" end repeat\n"
|
|
581
|
+
" end repeat\n"
|
|
582
|
+
" end repeat\n"
|
|
583
|
+
"end tell\n"
|
|
584
|
+
"return false"
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
def _terminal_focus_script(tty: str) -> str:
|
|
589
|
+
"""Terminal.app AppleScript:按 tty 找 tab,选中并置前其窗口。"""
|
|
590
|
+
return (
|
|
591
|
+
'tell application "Terminal"\n'
|
|
592
|
+
" repeat with w in windows\n"
|
|
593
|
+
" repeat with t in tabs of w\n"
|
|
594
|
+
f' if (tty of t) is "/dev/{tty}" then\n'
|
|
595
|
+
" set selected tab of w to t\n"
|
|
596
|
+
" set frontmost of w to true\n"
|
|
597
|
+
" activate\n"
|
|
598
|
+
" return true\n"
|
|
599
|
+
" end if\n"
|
|
600
|
+
" end repeat\n"
|
|
601
|
+
" end repeat\n"
|
|
602
|
+
"end tell\n"
|
|
603
|
+
"return false"
|
|
604
|
+
)
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
def focus_gui_terminal(tty: str, timeout: float = 3.0) -> bool:
|
|
608
|
+
"""iTerm2 → Terminal.app 依次尝试按 tty 聚焦对应窗口/标签;命中返回 True。
|
|
609
|
+
|
|
610
|
+
非 macOS / 无 osascript / 超时 / 应用未运行均 False(调用方降级打印)。
|
|
611
|
+
tty 参数为 ps 口径,脚本内已补 /dev/ 前缀。
|
|
612
|
+
"""
|
|
613
|
+
import platform
|
|
614
|
+
import shutil
|
|
615
|
+
import subprocess
|
|
616
|
+
|
|
617
|
+
if platform.system() != "Darwin" or shutil.which("osascript") is None:
|
|
618
|
+
return False
|
|
619
|
+
for script in (_iterm2_focus_script(tty), _terminal_focus_script(tty)):
|
|
620
|
+
try:
|
|
621
|
+
proc = subprocess.run(
|
|
622
|
+
["osascript", "-e", script],
|
|
623
|
+
capture_output=True, text=True, timeout=timeout,
|
|
624
|
+
)
|
|
625
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
626
|
+
continue
|
|
627
|
+
if proc.returncode == 0 and proc.stdout.strip() == "true":
|
|
628
|
+
return True
|
|
629
|
+
return False
|
|
630
|
+
|
|
631
|
+
|
|
509
632
|
# ---------------------------------------------------------------------------
|
|
510
633
|
# 活跃注册表
|
|
511
634
|
# ---------------------------------------------------------------------------
|
|
@@ -323,6 +323,21 @@ class SessionPicker:
|
|
|
323
323
|
# 选择必须跟随行跳转(s 是三态循环,行跳到置顶段后才能连按)
|
|
324
324
|
self._refilter_keep_sid(e.session_id)
|
|
325
325
|
|
|
326
|
+
@kb.add("g", filter=self._in_list & _empty_query)
|
|
327
|
+
def _jump(event: KeyPressEvent) -> None:
|
|
328
|
+
if not self._filtered_idx:
|
|
329
|
+
return
|
|
330
|
+
e = self.entries[self._filtered_idx[self.selected]]
|
|
331
|
+
if e.running_status is None:
|
|
332
|
+
self._notice("会话未运行,无终端可跳")
|
|
333
|
+
return
|
|
334
|
+
if e.tty is None:
|
|
335
|
+
self._notice("运行终端未知(tty 不可识别)")
|
|
336
|
+
return
|
|
337
|
+
from csm_cli import cli # 惰性:TUI 路径才加载入口层
|
|
338
|
+
if cli.jump_to_terminal(e):
|
|
339
|
+
self._notice(f"已跳转到 {e.tty}")
|
|
340
|
+
|
|
326
341
|
for digit, name in zip("123456", scanner.FILTER_NAMES):
|
|
327
342
|
@kb.add(digit, filter=self._in_list & _empty_query)
|
|
328
343
|
def _switch(event: KeyPressEvent, name: str = name) -> None:
|
|
@@ -478,7 +493,7 @@ class SessionPicker:
|
|
|
478
493
|
f" ⭐{sum(1 for e in self.entries if e.star == 1)}"),
|
|
479
494
|
("class:dim", f" 过滤:{_FILTER_LABELS.get(self.filter_name, self.filter_name)}"),
|
|
480
495
|
("", f" {matched}/{total}"),
|
|
481
|
-
("class:dim", " 1-6过滤 s星标 Tab预览 ^L清空 ^R重扫 esc退出"),
|
|
496
|
+
("class:dim", " 1-6过滤 s星标 g跳转 Tab预览 ^L清空 ^R重扫 esc退出"),
|
|
482
497
|
]
|
|
483
498
|
if starred == 0:
|
|
484
499
|
# 无收藏时省略星标统计
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"""csm 入口层单测:resume 双开保护判定 / --new 目标解析(不 execvp、不碰真实 ~/.claude)。"""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
|
|
7
|
+
from csm_cli import cli as csm
|
|
8
|
+
from csm_cli import scanner as s
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _entry(sid="sid-1"):
|
|
12
|
+
return s.SessionEntry(session_id=sid, file_path="x", mtime=1, size=1)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _reg(sid="sid-1", pid=123, status="busy", tty="ttys007"):
|
|
16
|
+
return {sid: s.RunningInfo(pid=pid, status=status, name=None, tty=tty)}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# ---------------------------------------------------------------- 双开保护
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_conflict_other_terminal():
|
|
23
|
+
"""会话活在他终端 → 警告文案含终端/pid/状态。"""
|
|
24
|
+
warn = csm._resume_conflict(_entry(), _reg(), "/dev/ttys001")
|
|
25
|
+
assert warn is not None
|
|
26
|
+
assert "ttys007" in warn and "123" in warn and "busy" in warn
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_conflict_same_terminal_ok():
|
|
30
|
+
"""本终端在跑(csm 被嵌套调用)→ 不拦(两侧均为 ps 口径)。"""
|
|
31
|
+
assert csm._resume_conflict(_entry(), _reg(tty="ttys001"), "ttys001") is None
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def test_conflict_not_running_ok():
|
|
35
|
+
"""registry 无记录(已关闭/kill -9 残留已被判活过滤)→ 不拦。"""
|
|
36
|
+
assert csm._resume_conflict(_entry(), {}, None) is None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def test_conflict_unknown_tty_still_warns():
|
|
40
|
+
"""tty 未知(ps 快照失败)→ 无法证明在本终端,仍警告。"""
|
|
41
|
+
warn = csm._resume_conflict(_entry(), _reg(tty=None), "ttys001")
|
|
42
|
+
assert warn is not None and "tty 未知" in warn
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
# ---------------------------------------------------------------- 跳转 g
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _running_entry(sid="sid-1", tty="ttys007"):
|
|
49
|
+
e = _entry(sid)
|
|
50
|
+
e.running_status, e.tty = "busy", tty
|
|
51
|
+
return e
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_jump_requires_tty(capsys):
|
|
55
|
+
assert csm.jump_to_terminal(_entry()) is False # 未运行 / tty 未知
|
|
56
|
+
assert "无法跳转" in capsys.readouterr().err
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_jump_switch_client(monkeypatch):
|
|
60
|
+
"""tmux 命中 + csm 在 tmux → switch-client(csm 存活,不 execvp)。"""
|
|
61
|
+
import subprocess as sp
|
|
62
|
+
|
|
63
|
+
calls = []
|
|
64
|
+
|
|
65
|
+
def fake_run(cmd, **kw):
|
|
66
|
+
calls.append(cmd)
|
|
67
|
+
|
|
68
|
+
class _P:
|
|
69
|
+
returncode = 0
|
|
70
|
+
return _P()
|
|
71
|
+
|
|
72
|
+
monkeypatch.setattr(s, "locate_tty_in_tmux", lambda t: "main:3.0")
|
|
73
|
+
monkeypatch.setattr(s, "in_tmux", lambda: True)
|
|
74
|
+
monkeypatch.setattr(sp, "run", fake_run)
|
|
75
|
+
assert csm.jump_to_terminal(_running_entry()) is True
|
|
76
|
+
assert calls[0] == ["tmux", "switch-client", "-t", "main:3.0"]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_jump_attach_when_outside_tmux(monkeypatch):
|
|
80
|
+
"""tmux 命中 + csm 不在 tmux → execvp 让位 attach,粒度收窄到 window。"""
|
|
81
|
+
execed = []
|
|
82
|
+
monkeypatch.setattr(s, "locate_tty_in_tmux", lambda t: "main:3.0")
|
|
83
|
+
monkeypatch.setattr(s, "in_tmux", lambda: False)
|
|
84
|
+
monkeypatch.setattr(s, "focus_gui_terminal", lambda t: False) # execvp 后不可达,防御
|
|
85
|
+
monkeypatch.setattr(os, "execvp", lambda name, argv: execed.append((name, argv)))
|
|
86
|
+
csm.jump_to_terminal(_running_entry())
|
|
87
|
+
assert execed[0] == ("tmux", ["tmux", "attach-session", "-t", "main:3"])
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_jump_gui_fallback_chain(monkeypatch):
|
|
91
|
+
"""tmux 未命中 → GUI 聚焦成功。"""
|
|
92
|
+
monkeypatch.setattr(s, "locate_tty_in_tmux", lambda t: None)
|
|
93
|
+
monkeypatch.setattr(s, "focus_gui_terminal", lambda t: True)
|
|
94
|
+
assert csm.jump_to_terminal(_running_entry()) is True
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def test_jump_all_miss_prints_hint(monkeypatch, capsys):
|
|
98
|
+
"""全部定位失败 → 打印降级提示(含 tty)。"""
|
|
99
|
+
monkeypatch.setattr(s, "locate_tty_in_tmux", lambda t: None)
|
|
100
|
+
monkeypatch.setattr(s, "focus_gui_terminal", lambda t: False)
|
|
101
|
+
assert csm.jump_to_terminal(_running_entry(tty="ttys042")) is False
|
|
102
|
+
assert "ttys042" in capsys.readouterr().err
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
# ---------------------------------------------------------------- --new
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_resolve_new_target(tmp_path):
|
|
109
|
+
d = tmp_path / "proj"
|
|
110
|
+
d.mkdir()
|
|
111
|
+
got = csm.resolve_new_target(str(d))
|
|
112
|
+
assert got is not None and got.is_dir()
|
|
113
|
+
assert csm.resolve_new_target(str(tmp_path / "nope")) is None # 不存在
|
|
114
|
+
f = tmp_path / "file"
|
|
115
|
+
f.write_text("x")
|
|
116
|
+
assert csm.resolve_new_target(str(f)) is None # 不是目录
|
|
@@ -520,10 +520,10 @@ def test_apply_registry_clears_stale(fake_root):
|
|
|
520
520
|
|
|
521
521
|
|
|
522
522
|
def test_current_tty_paths(monkeypatch):
|
|
523
|
-
# 快路径:ttyname
|
|
523
|
+
# 快路径:ttyname 命中(os.ttyname 带 /dev/ 前缀,统一归一化为 ps 口径)
|
|
524
524
|
monkeypatch.setattr(os, "ttyname", lambda fd: "/dev/ttys009")
|
|
525
|
-
assert s.current_terminal_tty() == "
|
|
526
|
-
# 慢路径:ttyname 全失败 → PPID
|
|
525
|
+
assert s.current_terminal_tty() == "ttys009"
|
|
526
|
+
# 慢路径:ttyname 全失败 → PPID 链上溯(ps 表内的 tty 本就是无前缀口径)
|
|
527
527
|
def boom(fd):
|
|
528
528
|
raise OSError("not a tty")
|
|
529
529
|
|
|
@@ -532,12 +532,69 @@ def test_current_tty_paths(monkeypatch):
|
|
|
532
532
|
parent = os.getppid()
|
|
533
533
|
table = {
|
|
534
534
|
me: s.PsInfo(ppid=parent, tty=None, lstart=None, command="python3"),
|
|
535
|
-
parent: s.PsInfo(ppid=1, tty="
|
|
535
|
+
parent: s.PsInfo(ppid=1, tty="ttys021", lstart=None, command="claude"),
|
|
536
536
|
}
|
|
537
|
-
assert s.current_terminal_tty(table) == "
|
|
537
|
+
assert s.current_terminal_tty(table) == "ttys021"
|
|
538
538
|
assert s.current_terminal_tty({}) is None # 无表可用 → None
|
|
539
539
|
|
|
540
540
|
|
|
541
|
+
def test_is_current_fast_path_tty_match(monkeypatch):
|
|
542
|
+
"""回归:os.ttyname 带 /dev/ 前缀而 ps 的 tty 无前缀,曾致快路径识别的
|
|
543
|
+
tty 永不命中 is_current(◉本终端/双开豁免失效)。归一化后必须命中。"""
|
|
544
|
+
monkeypatch.setattr(os, "ttyname", lambda fd: "/dev/ttys009")
|
|
545
|
+
e = s.SessionEntry(session_id="s1", file_path="x", mtime=1, size=1)
|
|
546
|
+
reg = {"s1": s.RunningInfo(pid=9, status="busy", name=None, tty="ttys009")}
|
|
547
|
+
s.apply_registry([e], reg, current_tty=s.current_terminal_tty())
|
|
548
|
+
assert e.is_current is True
|
|
549
|
+
|
|
550
|
+
|
|
551
|
+
# ---------------------------------------------------------------- 终端定位
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def test_parse_tmux_panes():
|
|
555
|
+
out = (
|
|
556
|
+
"/dev/ttys001 main:0.0\n"
|
|
557
|
+
"/dev/ttys002 dev:1.2\n"
|
|
558
|
+
"\n"
|
|
559
|
+
"garbage\n"
|
|
560
|
+
"/dev/ttys003 wt-only\n" # 非 session:win.pane 格式 → 跳过
|
|
561
|
+
"/dev/ttys004 a:b.c\n" # 非数字 index → 跳过
|
|
562
|
+
)
|
|
563
|
+
assert s.parse_tmux_panes(out) == {"ttys001": "main:0.0", "ttys002": "dev:1.2"}
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
def test_locate_tty_in_tmux(monkeypatch):
|
|
567
|
+
import subprocess as sp
|
|
568
|
+
|
|
569
|
+
class FakeProc:
|
|
570
|
+
returncode = 0
|
|
571
|
+
stdout = "/dev/ttys001 main:3.0\n/dev/ttys002 dev:0.1\n"
|
|
572
|
+
|
|
573
|
+
monkeypatch.setattr(sp, "run", lambda *a, **k: FakeProc())
|
|
574
|
+
assert s.locate_tty_in_tmux("ttys001") == "main:3.0" # ps 口径
|
|
575
|
+
assert s.locate_tty_in_tmux("/dev/ttys002") == "dev:0.1" # 带前缀也归一
|
|
576
|
+
assert s.locate_tty_in_tmux("ttys099") is None # 未命中
|
|
577
|
+
|
|
578
|
+
|
|
579
|
+
def test_locate_tty_no_tmux_binary(monkeypatch):
|
|
580
|
+
import shutil
|
|
581
|
+
|
|
582
|
+
monkeypatch.setattr(shutil, "which", lambda _b: None)
|
|
583
|
+
assert s.locate_tty_in_tmux("ttys001") is None
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
def test_in_tmux(monkeypatch):
|
|
587
|
+
monkeypatch.delenv("TMUX", raising=False)
|
|
588
|
+
assert s.in_tmux() is False
|
|
589
|
+
monkeypatch.setenv("TMUX", "/tmp/tmux-501/default,123,0")
|
|
590
|
+
assert s.in_tmux() is True
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def test_focus_gui_scripts_contain_tty():
|
|
594
|
+
assert '"/dev/ttys012"' in s._iterm2_focus_script("ttys012")
|
|
595
|
+
assert '"/dev/ttys012"' in s._terminal_focus_script("ttys012")
|
|
596
|
+
|
|
597
|
+
|
|
541
598
|
# ---------------------------------------------------------------- entrypoint
|
|
542
599
|
|
|
543
600
|
|
csm_cli-0.1.0/test_csm.py
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"""csm 入口层单测:resume 双开保护判定 / --new 目标解析(不 execvp、不碰真实 ~/.claude)。"""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
from csm_cli import cli as csm
|
|
6
|
-
from csm_cli import scanner as s
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def _entry(sid="sid-1"):
|
|
10
|
-
return s.SessionEntry(session_id=sid, file_path="x", mtime=1, size=1)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def _reg(sid="sid-1", pid=123, status="busy", tty="ttys007"):
|
|
14
|
-
return {sid: s.RunningInfo(pid=pid, status=status, name=None, tty=tty)}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
# ---------------------------------------------------------------- 双开保护
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def test_conflict_other_terminal():
|
|
21
|
-
"""会话活在他终端 → 警告文案含终端/pid/状态。"""
|
|
22
|
-
warn = csm._resume_conflict(_entry(), _reg(), "/dev/ttys001")
|
|
23
|
-
assert warn is not None
|
|
24
|
-
assert "ttys007" in warn and "123" in warn and "busy" in warn
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
def test_conflict_same_terminal_ok():
|
|
28
|
-
"""本终端在跑(csm 被嵌套调用)→ 不拦。"""
|
|
29
|
-
assert csm._resume_conflict(_entry(), _reg(tty="/dev/ttys001"),
|
|
30
|
-
"/dev/ttys001") is None
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
def test_conflict_not_running_ok():
|
|
34
|
-
"""registry 无记录(已关闭/kill -9 残留已被判活过滤)→ 不拦。"""
|
|
35
|
-
assert csm._resume_conflict(_entry(), {}, None) is None
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def test_conflict_unknown_tty_still_warns():
|
|
39
|
-
"""tty 未知(ps 快照失败)→ 无法证明在本终端,仍警告。"""
|
|
40
|
-
warn = csm._resume_conflict(_entry(), _reg(tty=None), "/dev/ttys001")
|
|
41
|
-
assert warn is not None and "tty 未知" in warn
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
# ---------------------------------------------------------------- --new
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def test_resolve_new_target(tmp_path):
|
|
48
|
-
d = tmp_path / "proj"
|
|
49
|
-
d.mkdir()
|
|
50
|
-
got = csm.resolve_new_target(str(d))
|
|
51
|
-
assert got is not None and got.is_dir()
|
|
52
|
-
assert csm.resolve_new_target(str(tmp_path / "nope")) is None # 不存在
|
|
53
|
-
f = tmp_path / "file"
|
|
54
|
-
f.write_text("x")
|
|
55
|
-
assert csm.resolve_new_target(str(f)) is None # 不是目录
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|