lingshu-skill 5.6.0 → 5.7.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # 更新日志
2
2
 
3
+ ## v5.7(2026-11-24)
4
+
5
+ > 主题:**连点安全 + 图标丢了能找回**。
6
+
7
+ ### 修:Windows 上两个面板同时监听同一端口(#99)
8
+
9
+ - **坑**:Python 的 `HTTPServer` 默认 `allow_reuse_address = True`。而 **Windows 的 `SO_REUSEADDR` 语义是"允许抢占已占用端口"**(Unix 恰恰相反,是"允许 TIME_WAIT 重绑")。
10
+ - **后果**(实测):连点两次快捷方式 → **两个独立面板同时监听 8765**,请求随机落到其一,两个进程都常驻不退。抓 `faulthandler` 堆栈才确认(两个都停在 `serve_forever()`)。
11
+ - **修**:面板服务器改用 `_ExclusiveServer(allow_reuse_address=False)` → 第二次 bind 直接 `WinError 10048`,启动器据此判定"已有实例"并静默退出。
12
+
13
+ ### 修:竞态的两处副作用
14
+
15
+ - 输家原先会弹 `MessageBoxW` **阻塞等点击**(用户看到假的"端口被占用")→ 改成**带重试再探**(`_wait_for_app`),确认是自己人就静默开浏览器退出;
16
+ - `webbrowser.open()` 在**没有浏览器关联**的环境会永久阻塞,把启动器拖在进程表里 → 改成 daemon 线程 + 最多等 3 秒,然后照常退出;
17
+ - 竞态下 `serve()` 的"端口被占"是预期行为,新增 `LINGSHU_QUIET_PORT` 把它从 stderr 静音,日志不再有假警报。
18
+
19
+ ### 新:快捷方式丢了能找回
20
+
21
+ - `brain.py shortcut --ensure`:幂等重建(缺了就建、已有就不动);
22
+ - **doctor 新增第 15 项 `desktop-shortcut`**:快捷方式不见了会 WARN 并**直接给出恢复命令**——否则用户只觉得"面板打不开",AI 也不知道从哪修;
23
+ - **SKILL.md 新增「面板(可视化界面)」一节**:写明面板本体在 `scripts/panel_launcher.py`、数据在 `~/.brain-memory`、丢图标用 `shortcut --ensure`、连点安全、关网页 30 分钟后自退。**用户说"图标没了",AI 就能直接修,不必再问"在哪"。**
24
+
25
+ 验证:`tests/test_p22_panel_entry.py` 5/5(端口独占 / 连点 3 次只 1 个常驻 / 已有服务时启动器 <12s 退出 / doctor 报快捷方式状态 / ensure 可重建)。
26
+
3
27
  ## v5.6(2026-11-24)
4
28
 
5
29
  > 主题:**面板关得掉了**——不再"关了网页后台还在跑"。
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  > 名字取自《黄帝内经》:**算力给思考,人格给灵魂,记忆给生命。**
4
4
 
5
- > 当前版本 **v5.6**(变更见 [CHANGELOG.md](CHANGELOG.md))
5
+ > 当前版本 **v5.7**(变更见 [CHANGELOG.md](CHANGELOG.md))
6
6
 
7
7
  > 🔁 **双仓库镜像**:GitHub(主)与 Gitee(镜像)保持同步——[gitee.com/htmcwy_0/lingshu-skill](https://gitee.com/htmcwy_0/lingshu-skill)。灵枢的更新检查会探测两边最新提交,**谁更新就自动从谁拉取**;任一边不可达(Gitee 匿名风控 / GitHub 限额)时走另一边兜底。
8
8
 
@@ -694,11 +694,48 @@ python scripts/brain.py shortcut [--ensure] [--name 灵枢面板] [--port N] [--
694
694
  | **空闲自退** | 超过 `panel_idle_minutes`(默认 **30**,0=永不退)没有任何请求 → 优雅退出。**关掉网页,半小时后它自己就没了** |
695
695
  | **退出按钮** | 面板「设置」页有**「退出服务」**(`POST /api/shutdown`),点完页面显示"已退出" |
696
696
  | **单实例** | 快捷方式指向 `panel_launcher.py`:先探测 `/api/ping`,**已在跑就只把浏览器叫出来**,绝不起第二个进程 |
697
- | **端口冲突可见** | 端口被别的程序占 → 弹**系统对话框**说清楚(`ctypes` 调 `MessageBoxW`,零依赖),不再静默失败 |
697
+ | **端口独占** | 面板服务器 `allow_reuse_address = False`——见下面那个 Windows 大坑 |
698
+ | **连点安全** | 连点 3 次 → **只 1 个常驻**,其余静默 exit 0(不弹窗、不卡住) |
699
+ | **端口冲突可见** | 端口被**别的程序**占 → 弹系统对话框说清楚(`ctypes` 调 `MessageBoxW`,零依赖),不再静默失败 |
700
+ | **快捷方式可恢复** | 删了也不怕:`brain.py shortcut --ensure` 幂等重建;`doctor` 有 `desktop-shortcut` 一项会报出来 |
698
701
 
699
702
  `/api/ping` **故意不校验 token**:启动器探测时还不知道 token,而且它只回"活着 + 端口 + pid",
700
703
  不含任何记忆内容。
701
704
 
705
+ ### 连点两次会怎样?——踩到一个 Windows 坑
706
+
707
+ **结论:现在是安全的**(只 1 个常驻,其余静默退出)。但做这个的时候发现一个真坑:
708
+
709
+ > **Windows 上 `SO_REUSEADDR` 的语义和 Unix 完全相反。**
710
+ > Unix 上它是"允许在 TIME_WAIT 状态重绑";Windows 上它是"**允许抢占已占用的端口**"。
711
+ > 而 Python 的 `HTTPServer` 默认 `allow_reuse_address = True`。
712
+
713
+ 后果(实测):连点两次快捷方式 → **两个独立面板同时监听 8765**,请求随机落到其中一个,
714
+ 两个进程都常驻不退。抓堆栈才看出来——两个进程都停在 `serve_forever()` 里。
715
+
716
+ 修法:面板服务器改成 `allow_reuse_address = False` → 第二次 bind 直接 `EADDRINUSE`
717
+ (`WinError 10048`),启动器据此判定"已有实例"并静默退出。
718
+
719
+ 同一轮还修了竞态的两处副作用:
720
+ - 输家原先会弹 `MessageBox` **阻塞等点击**(用户看到假的"端口被占用")→ 改成先**带重试再探**,确认是同伴就静默退出;
721
+ - `webbrowser.open()` 在没有浏览器关联的环境会**永久阻塞**,把启动器拖住 → 改成 daemon 线程 + 最多等 3 秒。
722
+
723
+ ### 快捷方式丢了怎么办
724
+
725
+ 用户可能不小心删了桌面图标、改了名、或换了桌面目录。**两条路都能找回**:
726
+
727
+ ```bash
728
+ python scripts/brain.py shortcut --ensure # 幂等:缺了就重建,已有就不动
729
+ python scripts/brain.py doctor # 看 desktop-shortcut 一项是否 PASS
730
+ ```
731
+
732
+ `doctor` 的 `desktop-shortcut` 检查是专门加的:**丢了会 WARN 并直接给出恢复命令**——
733
+ 否则用户只会觉得"面板打不开了",AI 也不知道从哪修。
734
+
735
+ **AI 怎么知道这些**:`SKILL.md` 有一节「面板(可视化界面)」,写明面板本体在
736
+ `scripts/panel_launcher.py`、数据在 `~/.brain-memory`、丢了快捷方式用 `shortcut --ensure` 重建。
737
+ 所以用户只要说一句"桌面那个图标没了",AI 就能直接修,不用再问"在哪"。
738
+
702
739
  ### 为什么不做成 exe
703
740
 
704
741
  考虑过 PyInstaller 单文件 exe,**结论是不做**,理由:
package/SKILL.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: lingshu
3
3
  title: 灵枢
4
- version: 5.6
4
+ version: 5.7
5
5
  description: 类脑持久记忆、人格与成长系统——跨会话记住事情、长出自己的人格、上下文将满时做梦式整理记忆。用户提到记忆/记住/上次/人格/设定/性格/知识库/笔记/日志/成长/自我进化/工作记忆,或新会话开始需要恢复关系与上下文时使用。纯本地零依赖。
6
6
  ---
7
7
 
@@ -370,6 +370,21 @@ python scripts/bench.py [--n 1000|--quick] # 性能基准:隔离临
370
370
 
371
371
  其余命令(人格编辑、mind、assoc、note、learn、skill、growth、evolve、backup、sync 等)见 [`docs/commands.md`](docs/commands.md)。
372
372
 
373
+ ## 面板(可视化界面)
374
+
375
+ | 事 | 命令 / 位置 |
376
+ |---|---|
377
+ | 启动面板 | 双击桌面「灵枢面板」,或 `python scripts/brain.py serve` |
378
+ | **面板本体在哪** | 启动器 `scripts/panel_launcher.py` → 服务 `scripts/panel_server.py`;数据在 `~/.brain-memory` |
379
+ | 关了网页还在跑? | 不会:页面每 30 秒发心跳,关掉网页后超过 `panel_idle_minutes`(默认 30,0=永不退)没请求就**自动退出** |
380
+ | 想立刻关 | 面板「设置」页 → **「退出服务」** |
381
+ | 连点两次 | 安全:启动器先探测,**已在跑就只把浏览器叫回来**,不会起第二个 |
382
+ | **桌面快捷方式丢了** | `python scripts/brain.py shortcut --ensure`(幂等:缺了就重建,已有就不动) |
383
+
384
+ **用户说"面板打不开了 / 桌面那个图标没了"时**,直接跑上面那条 `shortcut --ensure`,
385
+ 再 `python scripts/brain.py doctor` 看 `desktop-shortcut` 一项是否转 PASS。
386
+ 不要答"我不知道在哪"——面板就是本技能目录下的 `scripts/panel_launcher.py`,随时可以重建入口。
387
+
373
388
  ## 注意
374
389
 
375
390
  - 不要每轮都 `inject`/`recall`,只在会话开始、话题切换、被问及过去时用。
@@ -584,6 +584,13 @@ startup 注入的记忆块与 cron 的瞬态提醒**同名**,于是 cron 的 p
584
584
  - **验证**:ping 免 token 且识别 app;shutdown 后端口确实关闭;`panel_idle_minutes=1` 时 **91 秒自动退出**;启动器在已有服务时 exit 0 且不顶掉原服务;面板 HTML 含心跳与退出按钮。回归 `tests/test_p21_panel_lifecycle.py` 6/6。
585
585
  - **不做 exe 的结论**:PyInstaller 要打包整个 Python(15–30MB)、每平台单独构建、杀软误报高,且**打破"零依赖"卖点**;托盘图标要第三方依赖或大量 Win32 调用。真正的问题是"关不掉/重复启动",几十行 Python 就解决了。
586
586
 
587
+ 99. **【已做 v5.7】连点安全 + 快捷方式可恢复**(用户问"连点会不会出问题、图标删了怎么找回")。
588
+ - **发现真 bug(Windows 端口抢占)**:`ThreadingHTTPServer` 默认 `allow_reuse_address=True`,而 **Windows 的 `SO_REUSEADDR` 语义是"允许抢占已占用端口"**(Unix 是"允许 TIME_WAIT 重绑")。实测连点两次 → **两个面板同时监听同一端口**,请求随机落到其一,两进程都常驻。抓 `faulthandler` 堆栈才看出来(两个都停在 `serve_forever`)。
589
+ - **修**:`panel_server` 改用 `_ExclusiveServer(allow_reuse_address=False)` → 第二次 bind 报 `WinError 10048`;启动器据此判定"已有实例"静默退出。
590
+ - **竞态副作用两处**:① 输家原会弹 `MessageBoxW` **阻塞等点击**(假"端口被占用")→ 改为**带重试再探**(`_wait_for_app`),确认是同伴就静默开浏览器退出;② `webbrowser.open()` 在无浏览器关联环境**永久阻塞**,把启动器拖住 → 改成 daemon 线程 + `join(timeout=3)`。
591
+ - **快捷方式可恢复**:`shortcut --ensure` 幂等重建;**doctor 新增第 15 项 `desktop-shortcut`**,丢了给 WARN + 恢复命令;**SKILL.md 新增「面板(可视化界面)」一节**,写明面板本体 `scripts/panel_launcher.py`、数据 `~/.brain-memory`、恢复命令——用户说一句"图标没了"AI 就能修。
592
+ - **验证**:连点 3 次 → 恰好 1 个常驻、输家 exit 0;第二个进程绑同端口必失败;已有服务时启动器 <12 秒退出;doctor 有 `desktop-shortcut`。回归 `tests/test_p22_panel_entry.py` 5/5。
593
+
587
594
  ## 三、已知问题与风险
588
595
 
589
596
  | # | 现象 | 影响 | 处置 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lingshu-skill",
3
- "version": "5.6.0",
3
+ "version": "5.7.0",
4
4
  "description": "Brain-inspired persistent memory, persona and growth system for AI coding agents. Local-only, zero-dependency Python. Requires Python 3.10+.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "PATCH_NOTES.md": "9296e524f743f358fd11115598398a754cfd69534526dde9c8ad85f14a418f89",
3
3
  "README.md": "3d634d743b085eebd2cbb5dd3a00edfa728ed23947e20cc29e7c3b29087ec043",
4
- "engine/CHANGELOG.md": "fb7089c8156537e8bf4448919a3bbc8a2ce7eda7db72bdd2b30d9ceb53b48d13",
5
- "engine/README.md": "6f34b91a942b0c8a85631c8c81a8cb721a2cf91ee9e4bcd8fcca88c422716f80",
4
+ "engine/CHANGELOG.md": "38fed9bc41285992d8ccd252841d42b2b504b2e6a9722dd7d166df07fd3612ec",
5
+ "engine/README.md": "f02db60960aed595584e6913e08b00380aae4812a60f929b4376d1ac6399c3e9",
6
6
  "engine/SKILL-lite.md": "423f97d64042233a5a1678384fba307a91a62ab0ed2e8d01f89e44a926d6e83d",
7
7
  "engine/SKILL-tiny.md": "aff71d79e6384c14e8895405bbf178d2b57f679540b12a6afaeba6e39b15116b",
8
- "engine/SKILL.md": "6672ca565a88029fe86803a47a1e561ed1ab0ce7018521927d493f560008e4ed",
8
+ "engine/SKILL.md": "f451b67fd2a27f29698d30d9fb04e5f693f3c35f13564cd1f7e0aa10d0b3e0b5",
9
9
  "engine/assets/panel.html": "475cb4289cf3faf4bf93df366199de54fcb036c2701751ef8a06eaf94c0db931",
10
10
  "engine/docs/commands.md": "454b83814c74d8677c1a02754e4da24c73c1abebaa92a1cdcb0aa0b2e1472432",
11
11
  "engine/docs/mechanics.md": "e8a6602a434ed5b78cac64a98884f65a6888eb48daa7a1c0be1f6c3984041b34",
@@ -19,7 +19,7 @@
19
19
  "engine/scripts/check_pressure_sync.py": "46d53934f17c7b740d0ec9fdbef9d2653850f6e01e883eedd70ff9a1527e869a",
20
20
  "engine/scripts/check_tag.py": "6f0b8209dc2deebb1f9ec90d429e1fad60c040a2ec014a6406d9d3498faafd7f",
21
21
  "engine/scripts/checkpoint.py": "3a9292121042692dc872a865b6ee1c1e5f14c054791887e0791f19ccdb9638fb",
22
- "engine/scripts/cli.py": "242a57fc99378f6d17d97204816a3a45578e4d54f85f09040296202e89b737bd",
22
+ "engine/scripts/cli.py": "26f3752fe69b6dc9f51f210ba826203d18b9c519c9165fa7f91f917f85f0825a",
23
23
  "engine/scripts/context_ab.py": "3d383f1fc16d328612825bbf731866fa427da0746490d027a0ddb719d210f1cb",
24
24
  "engine/scripts/core.py": "e0b0360ab9c5f8c176157c12da7f25906cd15bc64a1cd56982135aedf564820a",
25
25
  "engine/scripts/desktop_shortcut.py": "77ea248bfaaf28e1e4299979d0c0d8c80a055642dde9a40f5e297b2008474091",
@@ -42,8 +42,8 @@
42
42
  "engine/scripts/panel_build.py": "6394c1d74d203093e43b2106d7acca6e0ca9db61ca1cd6cd3a1616053653414b",
43
43
  "engine/scripts/panel_cloud.py": "fff72888123d45bbf05ab329a06b2126f1c0b972c76283ecff32454bc4155c58",
44
44
  "engine/scripts/panel_data.py": "5788aaee9b4e56e73587c4d0474687cecaa40dd7e507f02c27dd07aa208ba237",
45
- "engine/scripts/panel_launcher.py": "c70e88c0ed9ade046809395f608aac6a84d273c759d4837b614a9eb7a31642a5",
46
- "engine/scripts/panel_server.py": "dd99963ea9a3a540726228d99a61d4a74aff3b54824e45f478986ded4980780a",
45
+ "engine/scripts/panel_launcher.py": "69306c24ba208505e81232ad87d5ced90d343786567ea2c79e1025395963d23a",
46
+ "engine/scripts/panel_server.py": "2ecaf68309c3efa151c14422e441039b4550c503471ec267bc5824fd54bd9fe5",
47
47
  "engine/scripts/panel_settings.py": "53074fc0c01268f0c112580f3933e863b9ab4df8acea8569104105ca93e17c69",
48
48
  "engine/scripts/panel_update.py": "06f1c3c0362a49532dcd24651cc2e85ca1a1b3ba05fff6eb6f6566a72e876e2a",
49
49
  "engine/scripts/panels.py": "87dfce47693340af7c9bc6010a593256331e32c2e8504a0ac0745cf2ef992fd6",
@@ -1,5 +1,29 @@
1
1
  # 更新日志
2
2
 
3
+ ## v5.7(2026-11-24)
4
+
5
+ > 主题:**连点安全 + 图标丢了能找回**。
6
+
7
+ ### 修:Windows 上两个面板同时监听同一端口(#99)
8
+
9
+ - **坑**:Python 的 `HTTPServer` 默认 `allow_reuse_address = True`。而 **Windows 的 `SO_REUSEADDR` 语义是"允许抢占已占用端口"**(Unix 恰恰相反,是"允许 TIME_WAIT 重绑")。
10
+ - **后果**(实测):连点两次快捷方式 → **两个独立面板同时监听 8765**,请求随机落到其一,两个进程都常驻不退。抓 `faulthandler` 堆栈才确认(两个都停在 `serve_forever()`)。
11
+ - **修**:面板服务器改用 `_ExclusiveServer(allow_reuse_address=False)` → 第二次 bind 直接 `WinError 10048`,启动器据此判定"已有实例"并静默退出。
12
+
13
+ ### 修:竞态的两处副作用
14
+
15
+ - 输家原先会弹 `MessageBoxW` **阻塞等点击**(用户看到假的"端口被占用")→ 改成**带重试再探**(`_wait_for_app`),确认是自己人就静默开浏览器退出;
16
+ - `webbrowser.open()` 在**没有浏览器关联**的环境会永久阻塞,把启动器拖在进程表里 → 改成 daemon 线程 + 最多等 3 秒,然后照常退出;
17
+ - 竞态下 `serve()` 的"端口被占"是预期行为,新增 `LINGSHU_QUIET_PORT` 把它从 stderr 静音,日志不再有假警报。
18
+
19
+ ### 新:快捷方式丢了能找回
20
+
21
+ - `brain.py shortcut --ensure`:幂等重建(缺了就建、已有就不动);
22
+ - **doctor 新增第 15 项 `desktop-shortcut`**:快捷方式不见了会 WARN 并**直接给出恢复命令**——否则用户只觉得"面板打不开",AI 也不知道从哪修;
23
+ - **SKILL.md 新增「面板(可视化界面)」一节**:写明面板本体在 `scripts/panel_launcher.py`、数据在 `~/.brain-memory`、丢图标用 `shortcut --ensure`、连点安全、关网页 30 分钟后自退。**用户说"图标没了",AI 就能直接修,不必再问"在哪"。**
24
+
25
+ 验证:`tests/test_p22_panel_entry.py` 5/5(端口独占 / 连点 3 次只 1 个常驻 / 已有服务时启动器 <12s 退出 / doctor 报快捷方式状态 / ensure 可重建)。
26
+
3
27
  ## v5.6(2026-11-24)
4
28
 
5
29
  > 主题:**面板关得掉了**——不再"关了网页后台还在跑"。
@@ -2,7 +2,7 @@
2
2
 
3
3
  > 名字取自《黄帝内经》:**算力给思考,人格给灵魂,记忆给生命。**
4
4
 
5
- > 当前版本 **v5.6**(变更见 [CHANGELOG.md](CHANGELOG.md))
5
+ > 当前版本 **v5.7**(变更见 [CHANGELOG.md](CHANGELOG.md))
6
6
 
7
7
  > 🔁 **双仓库镜像**:GitHub(主)与 Gitee(镜像)保持同步——[gitee.com/htmcwy_0/lingshu-skill](https://gitee.com/htmcwy_0/lingshu-skill)。灵枢的更新检查会探测两边最新提交,**谁更新就自动从谁拉取**;任一边不可达(Gitee 匿名风控 / GitHub 限额)时走另一边兜底。
8
8
 
@@ -694,11 +694,48 @@ python scripts/brain.py shortcut [--ensure] [--name 灵枢面板] [--port N] [--
694
694
  | **空闲自退** | 超过 `panel_idle_minutes`(默认 **30**,0=永不退)没有任何请求 → 优雅退出。**关掉网页,半小时后它自己就没了** |
695
695
  | **退出按钮** | 面板「设置」页有**「退出服务」**(`POST /api/shutdown`),点完页面显示"已退出" |
696
696
  | **单实例** | 快捷方式指向 `panel_launcher.py`:先探测 `/api/ping`,**已在跑就只把浏览器叫出来**,绝不起第二个进程 |
697
- | **端口冲突可见** | 端口被别的程序占 → 弹**系统对话框**说清楚(`ctypes` 调 `MessageBoxW`,零依赖),不再静默失败 |
697
+ | **端口独占** | 面板服务器 `allow_reuse_address = False`——见下面那个 Windows 大坑 |
698
+ | **连点安全** | 连点 3 次 → **只 1 个常驻**,其余静默 exit 0(不弹窗、不卡住) |
699
+ | **端口冲突可见** | 端口被**别的程序**占 → 弹系统对话框说清楚(`ctypes` 调 `MessageBoxW`,零依赖),不再静默失败 |
700
+ | **快捷方式可恢复** | 删了也不怕:`brain.py shortcut --ensure` 幂等重建;`doctor` 有 `desktop-shortcut` 一项会报出来 |
698
701
 
699
702
  `/api/ping` **故意不校验 token**:启动器探测时还不知道 token,而且它只回"活着 + 端口 + pid",
700
703
  不含任何记忆内容。
701
704
 
705
+ ### 连点两次会怎样?——踩到一个 Windows 坑
706
+
707
+ **结论:现在是安全的**(只 1 个常驻,其余静默退出)。但做这个的时候发现一个真坑:
708
+
709
+ > **Windows 上 `SO_REUSEADDR` 的语义和 Unix 完全相反。**
710
+ > Unix 上它是"允许在 TIME_WAIT 状态重绑";Windows 上它是"**允许抢占已占用的端口**"。
711
+ > 而 Python 的 `HTTPServer` 默认 `allow_reuse_address = True`。
712
+
713
+ 后果(实测):连点两次快捷方式 → **两个独立面板同时监听 8765**,请求随机落到其中一个,
714
+ 两个进程都常驻不退。抓堆栈才看出来——两个进程都停在 `serve_forever()` 里。
715
+
716
+ 修法:面板服务器改成 `allow_reuse_address = False` → 第二次 bind 直接 `EADDRINUSE`
717
+ (`WinError 10048`),启动器据此判定"已有实例"并静默退出。
718
+
719
+ 同一轮还修了竞态的两处副作用:
720
+ - 输家原先会弹 `MessageBox` **阻塞等点击**(用户看到假的"端口被占用")→ 改成先**带重试再探**,确认是同伴就静默退出;
721
+ - `webbrowser.open()` 在没有浏览器关联的环境会**永久阻塞**,把启动器拖住 → 改成 daemon 线程 + 最多等 3 秒。
722
+
723
+ ### 快捷方式丢了怎么办
724
+
725
+ 用户可能不小心删了桌面图标、改了名、或换了桌面目录。**两条路都能找回**:
726
+
727
+ ```bash
728
+ python scripts/brain.py shortcut --ensure # 幂等:缺了就重建,已有就不动
729
+ python scripts/brain.py doctor # 看 desktop-shortcut 一项是否 PASS
730
+ ```
731
+
732
+ `doctor` 的 `desktop-shortcut` 检查是专门加的:**丢了会 WARN 并直接给出恢复命令**——
733
+ 否则用户只会觉得"面板打不开了",AI 也不知道从哪修。
734
+
735
+ **AI 怎么知道这些**:`SKILL.md` 有一节「面板(可视化界面)」,写明面板本体在
736
+ `scripts/panel_launcher.py`、数据在 `~/.brain-memory`、丢了快捷方式用 `shortcut --ensure` 重建。
737
+ 所以用户只要说一句"桌面那个图标没了",AI 就能直接修,不用再问"在哪"。
738
+
702
739
  ### 为什么不做成 exe
703
740
 
704
741
  考虑过 PyInstaller 单文件 exe,**结论是不做**,理由:
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: lingshu
3
3
  title: 灵枢
4
- version: 5.6
4
+ version: 5.7
5
5
  description: 类脑持久记忆、人格与成长系统——跨会话记住事情、长出自己的人格、上下文将满时做梦式整理记忆。用户提到记忆/记住/上次/人格/设定/性格/知识库/笔记/日志/成长/自我进化/工作记忆,或新会话开始需要恢复关系与上下文时使用。纯本地零依赖。
6
6
  ---
7
7
 
@@ -370,6 +370,21 @@ python scripts/bench.py [--n 1000|--quick] # 性能基准:隔离临
370
370
 
371
371
  其余命令(人格编辑、mind、assoc、note、learn、skill、growth、evolve、backup、sync 等)见 [`docs/commands.md`](docs/commands.md)。
372
372
 
373
+ ## 面板(可视化界面)
374
+
375
+ | 事 | 命令 / 位置 |
376
+ |---|---|
377
+ | 启动面板 | 双击桌面「灵枢面板」,或 `python scripts/brain.py serve` |
378
+ | **面板本体在哪** | 启动器 `scripts/panel_launcher.py` → 服务 `scripts/panel_server.py`;数据在 `~/.brain-memory` |
379
+ | 关了网页还在跑? | 不会:页面每 30 秒发心跳,关掉网页后超过 `panel_idle_minutes`(默认 30,0=永不退)没请求就**自动退出** |
380
+ | 想立刻关 | 面板「设置」页 → **「退出服务」** |
381
+ | 连点两次 | 安全:启动器先探测,**已在跑就只把浏览器叫回来**,不会起第二个 |
382
+ | **桌面快捷方式丢了** | `python scripts/brain.py shortcut --ensure`(幂等:缺了就重建,已有就不动) |
383
+
384
+ **用户说"面板打不开了 / 桌面那个图标没了"时**,直接跑上面那条 `shortcut --ensure`,
385
+ 再 `python scripts/brain.py doctor` 看 `desktop-shortcut` 一项是否转 PASS。
386
+ 不要答"我不知道在哪"——面板就是本技能目录下的 `scripts/panel_launcher.py`,随时可以重建入口。
387
+
373
388
  ## 注意
374
389
 
375
390
  - 不要每轮都 `inject`/`recall`,只在会话开始、话题切换、被问及过去时用。
@@ -4100,6 +4100,33 @@ def _main(argv=None) -> int:
4100
4100
  return ("runtime-liveness", "WARN", ";".join(parts))
4101
4101
  return ("runtime-liveness", "PASS", ";".join(parts))
4102
4102
 
4103
+ def _doctor_desktop_shortcut() -> tuple:
4104
+ """桌面「灵枢面板」快捷方式还在不在。
4105
+
4106
+ 用户可能不小心删了/改名了/换了桌面目录。丢了不是错误,但**必须能被发现**——
4107
+ 否则用户只会觉得"面板打不开了",而 AI 也不知道从哪修。
4108
+ """
4109
+ try:
4110
+ import desktop_shortcut as _ds
4111
+ except Exception as exc: # noqa: BLE001
4112
+ return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
4113
+ try:
4114
+ exists = _ds.shortcut_exists()
4115
+ except Exception as exc: # noqa: BLE001
4116
+ return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
4117
+ if exists:
4118
+ return ("desktop-shortcut", "PASS", "桌面「灵枢面板」快捷方式在位")
4119
+ try:
4120
+ enabled = bool(core.load_config().get("desktop_shortcut", True))
4121
+ except Exception:
4122
+ enabled = True
4123
+ if not enabled:
4124
+ return ("desktop-shortcut", "PASS",
4125
+ "快捷方式不在,但配置已关闭自动创建(desktop_shortcut=false)")
4126
+ return ("desktop-shortcut", "WARN",
4127
+ "桌面「灵枢面板」快捷方式不见了(被删/改名/换了桌面目录)。"
4128
+ "恢复:python scripts/brain.py shortcut --ensure")
4129
+
4103
4130
  def _doctor_failures() -> tuple:
4104
4131
  """v3.56:被吞异常的可数账(failures.jsonl → 按类聚合,近七天)。
4105
4132
 
@@ -4333,6 +4360,12 @@ def _main(argv=None) -> int:
4333
4360
  except Exception as e:
4334
4361
  checks.append(("runtime-liveness", "WARN", f"自检失败:{e}"))
4335
4362
 
4363
+ # 14. 桌面快捷方式是否还在(用户可能不小心删了)
4364
+ try:
4365
+ checks.append(_doctor_desktop_shortcut())
4366
+ except Exception as e:
4367
+ checks.append(("desktop-shortcut", "WARN", f"自检失败:{e}"))
4368
+
4336
4369
  fails = sum(1 for _, s, _ in checks if s == "FAIL")
4337
4370
  warns = sum(1 for _, s, _ in checks if s == "WARN")
4338
4371
  if args.json:
@@ -19,6 +19,7 @@ from __future__ import annotations
19
19
  import json
20
20
  import os
21
21
  import sys
22
+ import time
22
23
  import urllib.error
23
24
  import urllib.request
24
25
  import webbrowser
@@ -31,7 +32,14 @@ import core # noqa: E402
31
32
 
32
33
 
33
34
  def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
34
- """隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。"""
35
+ """隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。
36
+
37
+ `LINGSHU_NO_MSGBOX=1` 时只写 stderr——自动化/测试里弹窗会**阻塞等点击**,
38
+ 把进程永久挂住。
39
+ """
40
+ if os.environ.get("LINGSHU_NO_MSGBOX", "").strip() in ("1", "true", "yes"):
41
+ print(f"[{title}] {text}", file=sys.stderr)
42
+ return
35
43
  try:
36
44
  import ctypes
37
45
  icon = 0x10 if error else 0x40 # MB_ICONERROR / MB_ICONINFORMATION
@@ -41,6 +49,28 @@ def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
41
49
  print(f"[{title}] {text}", file=sys.stderr)
42
50
 
43
51
 
52
+ def _open_browser(url: str, timeout: float = 3.0) -> None:
53
+ """打开浏览器,但**绝不拖住进程**。
54
+
55
+ 实测:没有默认浏览器关联时,`webbrowser.open()` 会一直阻塞——
56
+ 启动器就永久留在进程表里(用户看到的是"点了没反应、又多了个进程")。
57
+ 所以放 daemon 线程、最多等 `timeout` 秒,然后照常退出。
58
+ """
59
+ import threading
60
+ done = threading.Event()
61
+
62
+ def _go():
63
+ try:
64
+ webbrowser.open(url)
65
+ except Exception:
66
+ pass
67
+ finally:
68
+ done.set()
69
+
70
+ threading.Thread(target=_go, name="open-browser", daemon=True).start()
71
+ done.wait(timeout)
72
+
73
+
44
74
  def _port() -> int:
45
75
  """与 panel_server 同口径:config.panel_port,退回 8765。"""
46
76
  try:
@@ -63,6 +93,16 @@ def _ping(port: int, timeout: float = 1.5) -> dict | None:
63
93
  return None
64
94
 
65
95
 
96
+ def _wait_for_app(port: int, tries: int = 8, gap: float = 0.35) -> dict | None:
97
+ """带重试地探测:连点时另一个实例可能正在 bind,稍等就能看到它。"""
98
+ for _ in range(max(1, tries)):
99
+ info = _ping(port)
100
+ if info and info.get("app") == "lingshu-panel":
101
+ return info
102
+ time.sleep(gap)
103
+ return None
104
+
105
+
66
106
  def _port_occupied(port: int) -> bool:
67
107
  """端口有没有人在听(不关心是谁)。"""
68
108
  import socket
@@ -84,15 +124,18 @@ def main() -> int:
84
124
  port = _port()
85
125
  url = f"http://127.0.0.1:{port}"
86
126
 
87
- info = _ping(port)
127
+ info = _wait_for_app(port, tries=3, gap=0.2)
88
128
  if info and info.get("app") == "lingshu-panel":
89
129
  # 已经在跑:只把浏览器叫到前面来,不起第二个进程
90
- try:
91
- webbrowser.open(url)
92
- except Exception:
93
- pass
130
+ _open_browser(url)
94
131
  return 0
95
132
  if _port_occupied(port):
133
+ # 竞态:连点时端口可能刚被**同伴**抢到,而我们第一轮探测太早没看到。
134
+ # 先多等一会儿再看——只有确认不是我们自己的面板,才报"被别的程序占用"。
135
+ again = _wait_for_app(port, tries=12, gap=0.3)
136
+ if again and again.get("app") == "lingshu-panel":
137
+ _open_browser(url)
138
+ return 0
96
139
  _msgbox(
97
140
  f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
98
141
  f"两种办法:\n"
@@ -102,15 +145,30 @@ def main() -> int:
102
145
  return 1
103
146
 
104
147
  # 起服务(serve 内部带空闲自退 + 打开浏览器)
148
+ # 竞态下 bind 失败是预期行为(另一个同伴刚抢到),让 serve 别往 stderr 吐假警报。
149
+ os.environ["LINGSHU_QUIET_PORT"] = "1"
105
150
  try:
106
151
  import panel_server
107
152
  panel_server.serve(port, open_browser=True)
108
153
  return 0
109
- except SystemExit as exc: # serve 里端口占用会 SystemExit
110
- _msgbox(f"面板启动失败:端口 {port} 不可用。\n"
111
- f"换个端口:brain.py config set panel_port 8766",
112
- "灵枢面板 · 启动失败")
113
- return int(getattr(exc, "code", 1) or 1)
154
+ except (SystemExit, OSError) as exc:
155
+ # **竞态**:连点两次时两边都会 bind 失败一次。先别急着报错——
156
+ # 带重试再探一下,如果这会儿是我们自己的面板在服务,就静默开浏览器退出。
157
+ # (直接弹窗会让用户看到假的"启动失败",而且 MessageBox 会阻塞进程。)
158
+ again = _wait_for_app(port, tries=10, gap=0.35)
159
+ if again and again.get("app") == "lingshu-panel":
160
+ _open_browser(url)
161
+ return 0
162
+ if _port_occupied(port):
163
+ _msgbox(
164
+ f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
165
+ f"两种办法:\n"
166
+ f" 1) 关掉占用该端口的程序;\n"
167
+ f" 2) 用 brain.py config set panel_port 8766 换个端口。",
168
+ "灵枢面板 · 端口被占用")
169
+ return 1
170
+ _msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
171
+ return int(getattr(exc, "code", 1) or 2)
114
172
  except Exception as exc: # noqa: BLE001
115
173
  _msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
116
174
  return 2
@@ -2794,6 +2794,20 @@ def _startup_update_check() -> None:
2794
2794
  threading.Thread(target=_job, name="startup-update-check", daemon=True).start()
2795
2795
 
2796
2796
 
2797
+ class _ExclusiveServer(ThreadingHTTPServer):
2798
+ """禁止端口复用。
2799
+
2800
+ Python 的 HTTPServer 默认 `allow_reuse_address = True` → 它会设 SO_REUSEADDR,
2801
+ 而 **Windows 上 SO_REUSEADDR 的语义是"允许抢占已占用端口"**(Unix 是"允许
2802
+ TIME_WAIT 上重绑",完全不同)。实测后果:连点两次快捷方式 → 两个独立面板
2803
+ **同时监听同一端口**,请求随机落到其中一个,两个进程都常驻不退。
2804
+
2805
+ 置 False 后第二次 bind 直接 EADDRINUSE,启动器据此判定"已有实例"并静默退出。
2806
+ """
2807
+
2808
+ allow_reuse_address = False
2809
+
2810
+
2797
2811
  def serve(port: int = 8765, open_browser: bool = True) -> None:
2798
2812
  brain.ensure_home()
2799
2813
  brain._resolve_readonly(False) # 面板也要探测只读:数据目录不可写时 POST 写接口返回 403
@@ -2802,10 +2816,15 @@ def serve(port: int = 8765, open_browser: bool = True) -> None:
2802
2816
  # → 无感接入 embed 配置;模型没拉 → 后台自动 pull(整个生命周期只一次)。
2803
2817
  _ollama_auto_probe_async()
2804
2818
  try:
2805
- httpd = ThreadingHTTPServer(("127.0.0.1", port), Handler)
2819
+ httpd = _ExclusiveServer(("127.0.0.1", port), Handler)
2806
2820
  except OSError as exc:
2807
- print(f"[错误] 端口 {port} 被占用或不可用:{exc}", file=sys.stderr)
2808
- print("换一个端口试试:python scripts/panel_server.py --port 8766", file=sys.stderr)
2821
+ # 启动器(panel_launcher)会先探测再启动,竞态下这里的"端口被占"是**预期**的
2822
+ # ——它拿到 EADDRINUSE 后会重试探、发现是同伴就静默退出。所以由它设
2823
+ # LINGSHU_QUIET_PORT=1 把这条压掉,避免往日志里写假警报。
2824
+ if os.environ.get("LINGSHU_QUIET_PORT", "").strip() not in ("1", "true", "yes"):
2825
+ print(f"[错误] 端口 {port} 被占用或不可用:{exc}", file=sys.stderr)
2826
+ print("换一个端口试试:python scripts/panel_server.py --port 8766",
2827
+ file=sys.stderr)
2809
2828
  raise SystemExit(1) from exc
2810
2829
  httpd.daemon_threads = True # 退出时不等挂住的请求线程,防止 Ctrl+C 后进程残留
2811
2830
  url = f"http://127.0.0.1:{port}"
package/scripts/cli.py CHANGED
@@ -4100,6 +4100,33 @@ def _main(argv=None) -> int:
4100
4100
  return ("runtime-liveness", "WARN", ";".join(parts))
4101
4101
  return ("runtime-liveness", "PASS", ";".join(parts))
4102
4102
 
4103
+ def _doctor_desktop_shortcut() -> tuple:
4104
+ """桌面「灵枢面板」快捷方式还在不在。
4105
+
4106
+ 用户可能不小心删了/改名了/换了桌面目录。丢了不是错误,但**必须能被发现**——
4107
+ 否则用户只会觉得"面板打不开了",而 AI 也不知道从哪修。
4108
+ """
4109
+ try:
4110
+ import desktop_shortcut as _ds
4111
+ except Exception as exc: # noqa: BLE001
4112
+ return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
4113
+ try:
4114
+ exists = _ds.shortcut_exists()
4115
+ except Exception as exc: # noqa: BLE001
4116
+ return ("desktop-shortcut", "WARN", f"检查失败:{exc}")
4117
+ if exists:
4118
+ return ("desktop-shortcut", "PASS", "桌面「灵枢面板」快捷方式在位")
4119
+ try:
4120
+ enabled = bool(core.load_config().get("desktop_shortcut", True))
4121
+ except Exception:
4122
+ enabled = True
4123
+ if not enabled:
4124
+ return ("desktop-shortcut", "PASS",
4125
+ "快捷方式不在,但配置已关闭自动创建(desktop_shortcut=false)")
4126
+ return ("desktop-shortcut", "WARN",
4127
+ "桌面「灵枢面板」快捷方式不见了(被删/改名/换了桌面目录)。"
4128
+ "恢复:python scripts/brain.py shortcut --ensure")
4129
+
4103
4130
  def _doctor_failures() -> tuple:
4104
4131
  """v3.56:被吞异常的可数账(failures.jsonl → 按类聚合,近七天)。
4105
4132
 
@@ -4333,6 +4360,12 @@ def _main(argv=None) -> int:
4333
4360
  except Exception as e:
4334
4361
  checks.append(("runtime-liveness", "WARN", f"自检失败:{e}"))
4335
4362
 
4363
+ # 14. 桌面快捷方式是否还在(用户可能不小心删了)
4364
+ try:
4365
+ checks.append(_doctor_desktop_shortcut())
4366
+ except Exception as e:
4367
+ checks.append(("desktop-shortcut", "WARN", f"自检失败:{e}"))
4368
+
4336
4369
  fails = sum(1 for _, s, _ in checks if s == "FAIL")
4337
4370
  warns = sum(1 for _, s, _ in checks if s == "WARN")
4338
4371
  if args.json:
@@ -19,6 +19,7 @@ from __future__ import annotations
19
19
  import json
20
20
  import os
21
21
  import sys
22
+ import time
22
23
  import urllib.error
23
24
  import urllib.request
24
25
  import webbrowser
@@ -31,7 +32,14 @@ import core # noqa: E402
31
32
 
32
33
 
33
34
  def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
34
- """隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。"""
35
+ """隐藏进程里没有控制台,用系统弹窗把话说出去(ctypes 零依赖)。
36
+
37
+ `LINGSHU_NO_MSGBOX=1` 时只写 stderr——自动化/测试里弹窗会**阻塞等点击**,
38
+ 把进程永久挂住。
39
+ """
40
+ if os.environ.get("LINGSHU_NO_MSGBOX", "").strip() in ("1", "true", "yes"):
41
+ print(f"[{title}] {text}", file=sys.stderr)
42
+ return
35
43
  try:
36
44
  import ctypes
37
45
  icon = 0x10 if error else 0x40 # MB_ICONERROR / MB_ICONINFORMATION
@@ -41,6 +49,28 @@ def _msgbox(text: str, title: str = "灵枢面板", error: bool = True) -> None:
41
49
  print(f"[{title}] {text}", file=sys.stderr)
42
50
 
43
51
 
52
+ def _open_browser(url: str, timeout: float = 3.0) -> None:
53
+ """打开浏览器,但**绝不拖住进程**。
54
+
55
+ 实测:没有默认浏览器关联时,`webbrowser.open()` 会一直阻塞——
56
+ 启动器就永久留在进程表里(用户看到的是"点了没反应、又多了个进程")。
57
+ 所以放 daemon 线程、最多等 `timeout` 秒,然后照常退出。
58
+ """
59
+ import threading
60
+ done = threading.Event()
61
+
62
+ def _go():
63
+ try:
64
+ webbrowser.open(url)
65
+ except Exception:
66
+ pass
67
+ finally:
68
+ done.set()
69
+
70
+ threading.Thread(target=_go, name="open-browser", daemon=True).start()
71
+ done.wait(timeout)
72
+
73
+
44
74
  def _port() -> int:
45
75
  """与 panel_server 同口径:config.panel_port,退回 8765。"""
46
76
  try:
@@ -63,6 +93,16 @@ def _ping(port: int, timeout: float = 1.5) -> dict | None:
63
93
  return None
64
94
 
65
95
 
96
+ def _wait_for_app(port: int, tries: int = 8, gap: float = 0.35) -> dict | None:
97
+ """带重试地探测:连点时另一个实例可能正在 bind,稍等就能看到它。"""
98
+ for _ in range(max(1, tries)):
99
+ info = _ping(port)
100
+ if info and info.get("app") == "lingshu-panel":
101
+ return info
102
+ time.sleep(gap)
103
+ return None
104
+
105
+
66
106
  def _port_occupied(port: int) -> bool:
67
107
  """端口有没有人在听(不关心是谁)。"""
68
108
  import socket
@@ -84,15 +124,18 @@ def main() -> int:
84
124
  port = _port()
85
125
  url = f"http://127.0.0.1:{port}"
86
126
 
87
- info = _ping(port)
127
+ info = _wait_for_app(port, tries=3, gap=0.2)
88
128
  if info and info.get("app") == "lingshu-panel":
89
129
  # 已经在跑:只把浏览器叫到前面来,不起第二个进程
90
- try:
91
- webbrowser.open(url)
92
- except Exception:
93
- pass
130
+ _open_browser(url)
94
131
  return 0
95
132
  if _port_occupied(port):
133
+ # 竞态:连点时端口可能刚被**同伴**抢到,而我们第一轮探测太早没看到。
134
+ # 先多等一会儿再看——只有确认不是我们自己的面板,才报"被别的程序占用"。
135
+ again = _wait_for_app(port, tries=12, gap=0.3)
136
+ if again and again.get("app") == "lingshu-panel":
137
+ _open_browser(url)
138
+ return 0
96
139
  _msgbox(
97
140
  f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
98
141
  f"两种办法:\n"
@@ -102,15 +145,30 @@ def main() -> int:
102
145
  return 1
103
146
 
104
147
  # 起服务(serve 内部带空闲自退 + 打开浏览器)
148
+ # 竞态下 bind 失败是预期行为(另一个同伴刚抢到),让 serve 别往 stderr 吐假警报。
149
+ os.environ["LINGSHU_QUIET_PORT"] = "1"
105
150
  try:
106
151
  import panel_server
107
152
  panel_server.serve(port, open_browser=True)
108
153
  return 0
109
- except SystemExit as exc: # serve 里端口占用会 SystemExit
110
- _msgbox(f"面板启动失败:端口 {port} 不可用。\n"
111
- f"换个端口:brain.py config set panel_port 8766",
112
- "灵枢面板 · 启动失败")
113
- return int(getattr(exc, "code", 1) or 1)
154
+ except (SystemExit, OSError) as exc:
155
+ # **竞态**:连点两次时两边都会 bind 失败一次。先别急着报错——
156
+ # 带重试再探一下,如果这会儿是我们自己的面板在服务,就静默开浏览器退出。
157
+ # (直接弹窗会让用户看到假的"启动失败",而且 MessageBox 会阻塞进程。)
158
+ again = _wait_for_app(port, tries=10, gap=0.35)
159
+ if again and again.get("app") == "lingshu-panel":
160
+ _open_browser(url)
161
+ return 0
162
+ if _port_occupied(port):
163
+ _msgbox(
164
+ f"端口 {port} 已被别的程序占用,面板起不来。\n\n"
165
+ f"两种办法:\n"
166
+ f" 1) 关掉占用该端口的程序;\n"
167
+ f" 2) 用 brain.py config set panel_port 8766 换个端口。",
168
+ "灵枢面板 · 端口被占用")
169
+ return 1
170
+ _msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
171
+ return int(getattr(exc, "code", 1) or 2)
114
172
  except Exception as exc: # noqa: BLE001
115
173
  _msgbox(f"面板启动失败:\n{type(exc).__name__}: {exc}", "灵枢面板 · 启动失败")
116
174
  return 2
@@ -2794,6 +2794,20 @@ def _startup_update_check() -> None:
2794
2794
  threading.Thread(target=_job, name="startup-update-check", daemon=True).start()
2795
2795
 
2796
2796
 
2797
+ class _ExclusiveServer(ThreadingHTTPServer):
2798
+ """禁止端口复用。
2799
+
2800
+ Python 的 HTTPServer 默认 `allow_reuse_address = True` → 它会设 SO_REUSEADDR,
2801
+ 而 **Windows 上 SO_REUSEADDR 的语义是"允许抢占已占用端口"**(Unix 是"允许
2802
+ TIME_WAIT 上重绑",完全不同)。实测后果:连点两次快捷方式 → 两个独立面板
2803
+ **同时监听同一端口**,请求随机落到其中一个,两个进程都常驻不退。
2804
+
2805
+ 置 False 后第二次 bind 直接 EADDRINUSE,启动器据此判定"已有实例"并静默退出。
2806
+ """
2807
+
2808
+ allow_reuse_address = False
2809
+
2810
+
2797
2811
  def serve(port: int = 8765, open_browser: bool = True) -> None:
2798
2812
  brain.ensure_home()
2799
2813
  brain._resolve_readonly(False) # 面板也要探测只读:数据目录不可写时 POST 写接口返回 403
@@ -2802,10 +2816,15 @@ def serve(port: int = 8765, open_browser: bool = True) -> None:
2802
2816
  # → 无感接入 embed 配置;模型没拉 → 后台自动 pull(整个生命周期只一次)。
2803
2817
  _ollama_auto_probe_async()
2804
2818
  try:
2805
- httpd = ThreadingHTTPServer(("127.0.0.1", port), Handler)
2819
+ httpd = _ExclusiveServer(("127.0.0.1", port), Handler)
2806
2820
  except OSError as exc:
2807
- print(f"[错误] 端口 {port} 被占用或不可用:{exc}", file=sys.stderr)
2808
- print("换一个端口试试:python scripts/panel_server.py --port 8766", file=sys.stderr)
2821
+ # 启动器(panel_launcher)会先探测再启动,竞态下这里的"端口被占"是**预期**的
2822
+ # ——它拿到 EADDRINUSE 后会重试探、发现是同伴就静默退出。所以由它设
2823
+ # LINGSHU_QUIET_PORT=1 把这条压掉,避免往日志里写假警报。
2824
+ if os.environ.get("LINGSHU_QUIET_PORT", "").strip() not in ("1", "true", "yes"):
2825
+ print(f"[错误] 端口 {port} 被占用或不可用:{exc}", file=sys.stderr)
2826
+ print("换一个端口试试:python scripts/panel_server.py --port 8766",
2827
+ file=sys.stderr)
2809
2828
  raise SystemExit(1) from exc
2810
2829
  httpd.daemon_threads = True # 退出时不等挂住的请求线程,防止 Ctrl+C 后进程残留
2811
2830
  url = f"http://127.0.0.1:{port}"