a4phone 1.5.1 → 1.5.2

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/README.md CHANGED
@@ -169,6 +169,24 @@ command = "a4p hook codex"
169
169
 
170
170
  > 前提:`dsh web` 正在运行且已挂载新版 `dsh-hook` 插件(`a4p setup` 自动挂载,插件代码更新后需重启 `dsh web`)。未检测到 `dsh web` 时续聊会快速失败并给出提示。
171
171
 
172
+ ### ⚠️ 待办:DSH workspace 记账归组缺陷(临时外部方案,关注 DSH 官方修复)
173
+
174
+ **背景**:DSH 存在一个**已知缺陷**——workspace 注册表的会话记账只在"首次启动"时用 header 归组历史目录,此后产生的会话(旧版 TUI、`dsh headless`、部分启动入口等)只写会话日志、不主动记账,于是永久落在"未分组"。DSH 官方已将其列为**已知待办**(`@deepseek-ai/dsh-workspace` README 的 "Known Limitations and Deferred Work")。
175
+
176
+ **a4phone 的临时方案**:在 DSH 插件(`dsh/lib/index.js`)中加入"孤儿会话自愈",在每次插件启动时把已持久化但未记账的会话按 `header.cwd` 归入对应项目(必要时自动新建 workspace 记录)。该方案:
177
+
178
+ - **幂等**:只处理未记账的会话,重复运行不重复归组;
179
+ - **安全**:失败只记日志,不影响插件启动与其他功能;
180
+ - **临时**:仅作为 DSH 官方修复记账 bug 之前的过渡措施。
181
+
182
+ **迭代检查项(每次功能迭代 / 代码提交前)**:
183
+
184
+ 1. 检查 DSH 官方(`@deepseek-ai/dsh-workspace`)是否已修复 workspace 记账归组;
185
+ 2. 若已修复,评估并**移除**本插件中的"孤儿会话自愈"模块(`config.healWorkspaces` 开关可先关闭再删代码)——它是与 a4phone 核心功能无关的冗余设计,不应长期保留;
186
+ 3. 若未修复,保留本方案并在文档更新"已知待办"状态。
187
+
188
+ **责任划分**:根因是 DSH 核心 workspace 记账缺陷(DSH 官方应修);dsh-tui / 旧入口只是"不主动记账"的设计使然,非其锅;a4phone 是体验受损方而非制造者。
189
+
172
190
  ## 原理
173
191
 
174
192
  DSH 插件与 Hook(Claude Code / Codex)拦截事件后,通过 ntfy.sh 推送带按钮的通知到手机;手机点选或发送文字后,决策经响应话题回传并注入会话。`Stop` 事件同时把 AI 最后输出从会话记录中抽取出来推送手机。
package/dsh/lib/index.js CHANGED
@@ -34,7 +34,8 @@ const name = 'dsh-hook';
34
34
  // 监听 tools/execute 与 approval/request 需要注入这两个服务;
35
35
  // session/event 直接 ctx.on 即可(与 dsh-session-telemetry 一致)。
36
36
  // agents 供续聊服务解析目标会话(ctx.agents.get / roots)。
37
- const inject = ['tools', 'approval', 'agents'];
37
+ // workspaceRegistry / sessionPersistence 供“孤儿会话自愈”(临时性外部兜底)按 cwd 归组。
38
+ const inject = ['tools', 'approval', 'agents', 'workspaceRegistry', 'sessionPersistence'];
38
39
 
39
40
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
40
41
 
@@ -68,10 +69,58 @@ function record(ctx, logDir, hook, payload) {
68
69
  process.stderr.write(`[dsh-hook] ${line}\n`);
69
70
  }
70
71
 
72
+ /**
73
+ * 孤儿会话自愈(临时性外部兜底方案):把“已持久化但未计入任何 workspace 项目”的会话,
74
+ * 按其 header.cwd 归入对应项目(必要时自动新建 workspace 记录)。
75
+ *
76
+ * 【定位:临时方案,非根因修复】
77
+ * 根因是 DSH 核心 workspace 注册表的记账缺口:首次启动时用 header 归组历史目录,
78
+ * 此后产生的会话(旧版 TUI、headless、部分启动入口等)只写会话日志、不主动记账,
79
+ * 于是永久落在“未分组”。DSH 官方已将此列为已知待办
80
+ * (@deepseek-ai/dsh-workspace README 的 "Known Limitations and Deferred Work")。
81
+ *
82
+ * 本方案由 a4phone 在 DSH 插件中提供,作为 DSH 官方修复该 bug 之前的过渡措施:
83
+ * 在每次启动时把缺口补上(幂等,只处理未记账的会话;失败只记日志不影响启动)。
84
+ *
85
+ * 【退出策略】
86
+ * 一旦 DSH 官方修复了 workspace 记账(让所有会话在产生时即被正确归组),
87
+ * 本模块应被评估移除——它属于与 a4phone 核心功能无关的冗余设计,
88
+ * 不应长期留在代码中。请见 README 中 "### 待办:DSH workspace 记账归组缺陷" 一节。
89
+ *
90
+ * @param ctx cordis 插件上下文
91
+ */
92
+ async function healUngroupedSessions(ctx) {
93
+ const registry = ctx.get('workspaceRegistry');
94
+ const persistence = ctx.get('sessionPersistence');
95
+ if (registry === undefined || persistence === undefined) return;
96
+ let headers;
97
+ try {
98
+ headers = await persistence.list();
99
+ } catch (error) {
100
+ ctx.logger.warn(`dsh-hook: 会话自愈读取持久化列表失败: ${String(error)}`);
101
+ return;
102
+ }
103
+ const accounted = new Set();
104
+ for (const workspace of registry.list()) {
105
+ for (const id of workspace.sessionIds) accounted.add(id);
106
+ }
107
+ for (const header of headers) {
108
+ if (header.cwd === undefined || accounted.has(header.id)) continue;
109
+ try {
110
+ const workspace = await registry.resolveByPath(header.cwd) ?? await registry.create(header.cwd);
111
+ await workspace.attachSession(header.id);
112
+ accounted.add(header.id);
113
+ ctx.logger.info(`dsh-hook: 已把未分组会话 ${header.id} 归入项目 ${workspace.title}`);
114
+ } catch (error) {
115
+ ctx.logger.warn(`dsh-hook: 会话 ${header.id} 归位失败,保持未分组: ${error instanceof Error ? error.message : String(error)}`);
116
+ }
117
+ }
118
+ }
119
+
71
120
  /**
72
121
  * 插件入口:注册三个 hook 的事件监听(日志 + 手机交互)。
73
122
  * @param ctx cordis 插件上下文
74
- * @param config 插件配置 { logDir?, taskComplete?, permissionRequest?, questionAsked?, phone? }
123
+ * @param config 插件配置 { logDir?, taskComplete?, permissionRequest?, questionAsked?, phone?, healWorkspaces? }
75
124
  */
76
125
  function apply(ctx, config = {}) {
77
126
  // 默认日志目录移到 ~/.a4phone/dsh-logs(a4phone 状态目录),不再写插件目录
@@ -200,6 +249,17 @@ function apply(ctx, config = {}) {
200
249
  // AI 回复同样写入会话(桌面实时可见),并经 a4p 推回手机。
201
250
  const stopResume = enableResume ? startResumeService(ctx) : () => {};
202
251
 
252
+ // ── 孤儿会话自愈(临时性外部兜底:待 DSH 官方修复记账后移除)──────────
253
+ // 等待 loader 就绪后,把已持久化但未记账的会话按 cwd 归入项目;
254
+ // 幂等、只补缺口,可经 config.healWorkspaces: false 关闭。
255
+ if (config.healWorkspaces !== false) {
256
+ const loader = ctx.get('loader');
257
+ const ready = loader?.await === undefined ? Promise.resolve() : loader.await();
258
+ void ready
259
+ .then(() => healUngroupedSessions(ctx))
260
+ .catch((error) => ctx.logger.warn(`dsh-hook: 孤儿会话自愈失败: ${String(error)}`));
261
+ }
262
+
203
263
  ctx.logger.info('dsh-hook: 已挂载三个 hook(task-complete / permission-request / question-asked)与续聊服务');
204
264
 
205
265
  // 返回卸载函数:插件卸载(HMR / dsh web 退出)时停止轮询
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "a4phone",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "Claude Code / Codex / DSH 远程手机交互:任务完成通知(含 AI 最后输出)、提问/权限请求手机点选或文字作答、远程续聊(ntfy.sh,含 DSH 会话续聊)",
5
5
  "type": "module",
6
6
  "bin": {