dsh-mobile-plugin 0.5.82 → 0.5.83

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.
@@ -11,10 +11,6 @@
11
11
  * 一节才有得对账(理由当时只在卡上闪一下,卡一结算就没了)。这份记录只给人看,不参与任何
12
12
  * 判定,所以只留最近若干条、超了挤掉最老的。
13
13
  *
14
- * 第三段是**自动审阅档的判定流水**(`autoReview`):这一档会替人放行,每次判定都要留凭据。
15
- * 这条记录**不写会话日志** —— 宿主读盘时认不出的事件类型会让整份会话打不开,账本才是它该在
16
- * 的地方(见 auto-review.js 的 `record`)。
17
- *
18
14
  * 落盘位置 `<DSH_HOME>/storages/mobile-plugin/approval-rules.json`,照移动端那几份账本的
19
15
  * 同一套「原子写 + 读坏当空」的边界:账本读不动不能把审批拦在门外,写失败也只影响「重启后
20
16
  * 还在不在」。`DSH_MOBILE_APPROVAL_RULES` 只给测试注入路径用。
@@ -34,8 +30,6 @@ const VERSION = 1;
34
30
  const MAX_RULES = 500;
35
31
  /** 拒绝记录上限:只是给人对账用的流水,留最近这些就够了。 */
36
32
  const MAX_DECISIONS = 50;
37
- /** 自动审阅判定上限:同上,只是流水。 */
38
- const MAX_AUTO_REVIEW = 200;
39
33
 
40
34
  function rulesFile() {
41
35
  return process.env.DSH_MOBILE_APPROVAL_RULES
@@ -67,25 +61,7 @@ function normalizeRule(entry) {
67
61
  }
68
62
 
69
63
  function emptyStore() {
70
- return { version: VERSION, updatedAt: 0, rules: [], decisions: [], autoReview: [] };
71
- }
72
-
73
- /** 自动审阅档的三种结论;其余取值当坏记录丢掉。 */
74
- const AUTO_REVIEW_ACTIONS = new Set(['allow', 'reject', 'card']);
75
-
76
- /** 一条自动审阅判定:形状不对的丢掉,读坏的文件不能把页面撑炸。 */
77
- function normalizeAutoReview(entry) {
78
- if (!isRecord(entry) || !AUTO_REVIEW_ACTIONS.has(entry.action)) return null;
79
- return {
80
- sessionId: textOf(entry.sessionId, 200) || null,
81
- action: entry.action,
82
- verdict: textOf(entry.verdict, 40) || null,
83
- decidedBy: textOf(entry.decidedBy, 40) || null,
84
- targetMode: textOf(entry.targetMode, 40) || null,
85
- toolName: textOf(entry.toolName, 120) || null,
86
- callId: textOf(entry.callId, 200) || null,
87
- at: Number.isFinite(entry.at) ? entry.at : Date.now(),
88
- };
64
+ return { version: VERSION, updatedAt: 0, rules: [], decisions: [] };
89
65
  }
90
66
 
91
67
  /** 一条拒绝记录:形状不对的丢掉,读坏的文件不能把页面撑炸。 */
@@ -120,17 +96,11 @@ function readStore() {
120
96
  const decision = normalizeDecision(entry);
121
97
  if (decision) decisions.push(decision);
122
98
  }
123
- const autoReview = [];
124
- for (const entry of Array.isArray(raw.autoReview) ? raw.autoReview : []) {
125
- const decision = normalizeAutoReview(entry);
126
- if (decision) autoReview.push(decision);
127
- }
128
99
  return {
129
100
  version: VERSION,
130
101
  updatedAt: Number.isFinite(raw.updatedAt) ? raw.updatedAt : 0,
131
102
  rules,
132
103
  decisions,
133
- autoReview,
134
104
  };
135
105
  }
136
106
 
@@ -143,9 +113,7 @@ function saveStore(store) {
143
113
  }
144
114
  let decisions = (store.decisions || []).slice();
145
115
  if (decisions.length > MAX_DECISIONS) decisions = decisions.slice(decisions.length - MAX_DECISIONS);
146
- let autoReview = (store.autoReview || []).slice();
147
- if (autoReview.length > MAX_AUTO_REVIEW) autoReview = autoReview.slice(autoReview.length - MAX_AUTO_REVIEW);
148
- const next = { ...store, version: VERSION, updatedAt: Date.now(), rules, decisions, autoReview };
116
+ const next = { ...store, version: VERSION, updatedAt: Date.now(), rules, decisions };
149
117
  try {
150
118
  fs.mkdirSync(path.dirname(file), { recursive: true });
151
119
  const tmp = `${file}.${process.pid}.tmp`;
@@ -228,25 +196,6 @@ function recordDecision({ classKey, reason } = {}) {
228
196
  return saveStore(store);
229
197
  }
230
198
 
231
- /**
232
- * 记一条自动审阅判定。只给人和脚本对账用,不参与任何判定;没有合法 action 的不记,
233
- * 落盘失败也只影响这一条留不留得下来。
234
- */
235
- function recordAutoReview(entry = {}) {
236
- const record = normalizeAutoReview({ ...entry, at: Date.now() });
237
- if (!record) return false;
238
- const store = readStore();
239
- store.autoReview.push(record);
240
- return saveStore(store);
241
- }
242
-
243
- /** 最近的自动审阅判定(新的在前),给对账用。 */
244
- function listAutoReview() {
245
- return readStore().autoReview
246
- .slice()
247
- .sort((a, b) => b.at - a.at);
248
- }
249
-
250
199
  module.exports = {
251
200
  listRules,
252
201
  hasRule,
@@ -254,6 +203,4 @@ module.exports = {
254
203
  removeRule,
255
204
  listDecisions,
256
205
  recordDecision,
257
- listAutoReview,
258
- recordAutoReview,
259
206
  };
@@ -25,8 +25,7 @@
25
25
  * 带沙箱档位(`<base>@<tier>`):同一条命令在「工作区内」与「完全访问」下不是同一类授权。
26
26
  * - **拒绝理由**:手机上写的理由随结算帧下发一份,同时记进 `approval-memory.js` 的最近拒绝
27
27
  * 记录 —— 只写在挂起项上会随结算一起消失,用户事后无从回看。
28
- * 另有「同一轮里相同请求再次出现直接 auto-reject」(`turnState` 记本轮签名)。这套逻辑整体
29
- * 叠在可关的自动审阅档(`auto-review.js`)之后,两者互不影响。
28
+ * 另有「同一轮里相同请求再次出现直接 auto-reject」(`turnState` 记本轮签名)。
30
29
  *
31
30
  * `classLabel`(类别人话)在推卡时随卡下发,供第三颗按钮旁那行说明;撤销接口在写盘失败时
32
31
  * 回非 ok,手机据此保留那一行并提示重试,不把没删成的规则当成已撤销。
@@ -35,7 +34,6 @@
35
34
  const { randomUUID } = require('node:crypto');
36
35
  const { sendJson, readJsonBody } = require('../shared/http-util');
37
36
  const { cancelForwardedRequests } = require('./host-bridge/transport.js');
38
- const autoReview = require('./auto-review.js');
39
37
  const { sessionIdOf } = require('./session-id.js');
40
38
  const facts = require('./approval-facts.js');
41
39
  const approvalMemory = require('./approval-memory.js');
@@ -71,13 +69,6 @@ const bySession = new Map();
71
69
  * 都不带轮次,所以轮次要本桥自己从 `session/event` 的 turn 事件维护。
72
70
  */
73
71
  const turnState = new Map();
74
- /**
75
- * `sessionId\u0000<callId|approvalId>`:这些审批**正在自动判定**(`auto-review.js` 最长要等
76
- * 十几秒)。判定期间 `approval/asked` 已经先建了一条 `bound:false` 的挂起项 —— 它答不动
77
- * (`pendingCardOf` 返回 undefined,点进去没有卡),却会让列表页亮一颗点不开的黄点。所以
78
- * `hasPending` 要把它们摘出去,判定结束(无论结论)在 finally 里清掉。
79
- */
80
- const judging = new Set();
81
72
  /**
82
73
  * 推送出口(`mobile/push-notify.js` 的 `notifyInteraction`)。由装配层经
83
74
  * `installApprovalBridge(ctx, pushNotify)` 或 `setPushNotifier` 注入;没注入就不推。
@@ -87,17 +78,6 @@ let pushNotifier = null;
87
78
  function setPushNotifier(fn) {
88
79
  pushNotifier = typeof fn === 'function' ? fn : null;
89
80
  }
90
- /**
91
- * 自动审阅档开关的取值器(装配层注入)。钩子每次现读 —— 开关是 live 的,改完下一次请求即生效;
92
- * 没注入就当关,行为与没有这一档时逐字节一致。
93
- */
94
- let autoReviewConfig = null;
95
- function setAutoReviewConfig(fn) {
96
- autoReviewConfig = typeof fn === 'function' ? fn : null;
97
- }
98
- function autoReviewEnabled() {
99
- return !!(autoReviewConfig && autoReviewConfig().approvalAutoReview);
100
- }
101
81
 
102
82
  function watcherCount(sessionId) {
103
83
  const set = watchers.get(sessionId);
@@ -287,43 +267,15 @@ function attachSession(sessionId, sink) {
287
267
  };
288
268
  }
289
269
 
290
- /**
291
- * 标上「这一条正在自动判定」,返回要在判定结束的 finally 里撤掉的键。
292
- *
293
- * 键按 `sessionId + callId(或 approvalId)` 铸:`approval/asked` 建的那条挂起项与钩子上的
294
- * request 共用同一个 callId,两边认得出是同一条。
295
- */
296
- function markJudging(sessionId, request) {
297
- const keys = [];
298
- for (const id of [request && request.callId, request && request.approvalId]) {
299
- if (typeof id !== 'string' || !id) continue;
300
- const key = `${sessionId}\u0000${id}`;
301
- judging.add(key);
302
- keys.push(key);
303
- }
304
- return keys;
305
- }
306
-
307
- /** 这条挂起项是不是还在自动判定里(按它会带的两种 id 认)。 */
308
- function isJudging(sessionId, entry) {
309
- if (judging.size === 0) return false;
310
- for (const id of [entry.callId, entry.approvalId]) {
311
- if (typeof id === 'string' && id && judging.has(`${sessionId}\u0000${id}`)) return true;
312
- }
313
- return false;
314
- }
315
-
316
270
  /**
317
271
  * 会话摘要里的待处理标记(0.2.x 的宿主不会再下发这个字段,见 sessions.js 的用法)。
318
272
  *
319
- * 口径:**全都在自动判定中就不算挂起**。判定最长要等十几秒,这期间 `approval/asked` 建的那条
320
- * `bound:false` 项答不动(`pendingCardOf` 给不出卡),算成挂起就只剩一颗点不开的黄点。已绑的
321
- * 待答卡、以及任何不在判定里的项一律照旧算 pending —— 它们是真在等人。
273
+ * 口径:还有没结算的挂起项就算挂起。
322
274
  */
323
275
  function hasPending(sessionId) {
324
276
  const open = openEntries(sessionId);
325
277
  if (open.length === 0) return false;
326
- return open.some((entry) => !isJudging(sessionId, entry));
278
+ return open.length > 0;
327
279
  }
328
280
 
329
281
  /**
@@ -346,9 +298,7 @@ function resetForTest() {
346
298
  entries.clear();
347
299
  bySession.clear();
348
300
  turnState.clear();
349
- judging.clear();
350
301
  pushNotifier = null;
351
- autoReviewConfig = null;
352
302
  }
353
303
 
354
304
 
@@ -766,34 +716,9 @@ async function onApprovalRequest(ctx, request, next) {
766
716
  const sessionId = sessionIdOfRequest(request);
767
717
  if (!sessionId) return next();
768
718
  const session = request.agent && request.agent.session ? request.agent.session : null;
769
- // 事实行与动作类别:在钩子这侧从日志原始参数算(手机线路已裁参数)。自动判定要用它,下半段
770
- // 的「已同意 / 本轮重复」也用同一份,所以在这里算一次。
771
- let prepared = null;
772
- // 自动审阅档(默认关):先做一次受限只读判定。能直接放行 / 拒绝就不建卡;越界才落到推卡。
773
- // 开关关着时这一步整段跳过,行为与没有这一档时一字不差(见 test/approval-bridge.test.cjs)。
774
- if (autoReviewEnabled()) {
775
- prepared = prepareFacts(session, request);
776
- // 判定期间先把这一条标出来:`approval/asked` 早于判定建的那条 `bound:false` 项答不动,
777
- // 不让它在列表页亮一颗点不开的黄点(见 hasPending)。
778
- const judgingKeys = markJudging(sessionId, request);
779
- let decision;
780
- try {
781
- // 「人在不在」只按**这条会话**的订阅算:进程级「最近有没有手机请求」既会误判在场,
782
- // 也会把「手机放着不动」误判成离开(见 auto-review.js 文件头)。
783
- decision = await autoReview.decide(ctx, request, session, isSessionWatched(sessionId), prepared);
784
- autoReview.record(session, decision, request, prepared);
785
- } finally {
786
- for (const key of judgingKeys) judging.delete(key);
787
- }
788
- if (decision.action === 'allow') {
789
- // 自动放行的类别也要记进「本轮已见过」:`approval/asked` 那侧靠它判断「这条会不会被钩子
790
- // 静默放行」,不记就还会建一条永远没人结算的挂起项。
791
- rememberDuplicate(sessionId, prepared);
792
- return 'allowed-once';
793
- }
794
- if (decision.action === 'reject') return 'rejected';
795
- }
796
- if (!prepared) prepared = prepareFacts(session, request);
719
+ // 事实行与动作类别:在钩子这侧从日志原始参数算(手机线路已裁参数)。下半段的
720
+ // 「已同意 / 本轮重复」要用同一份,所以在这里算一次。
721
+ const prepared = prepareFacts(session, request);
797
722
  // 「这一类以后都同意」:同类动作之前被用户放行过,这次直接静默放行,不弹卡也不转发。
798
723
  if (classAlreadyAllowed(prepared)) {
799
724
  rememberDuplicate(sessionId, prepared);
@@ -979,6 +904,5 @@ module.exports = {
979
904
  pendingCardOf,
980
905
  isSessionWatched,
981
906
  setPushNotifier,
982
- setAutoReviewConfig,
983
907
  resetForTest,
984
908
  };
@@ -1,3 +1,4 @@
1
+ // 宿主能力检测与协议兼容桥共用入口,兼容服务由每代的独立上下文持有。
1
2
  'use strict';
2
3
 
3
4
  /**
@@ -56,4 +57,14 @@ function createApiProxy(ctx) {
56
57
  };
57
58
  }
58
59
 
59
- module.exports = { createApiProxy, UNAVAILABLE };
60
+ /** 包存在时先等宿主提供旧协议服务,包缺席时直接使用网关兼容桥。 */
61
+ function runtimeProvidesApiProxy() {
62
+ try {
63
+ require.resolve('@deepseek-ai/dsh-host-apiproxy/package.json');
64
+ return true;
65
+ } catch {
66
+ return false;
67
+ }
68
+ }
69
+
70
+ module.exports = { createApiProxy, runtimeProvidesApiProxy, UNAVAILABLE };
@@ -254,9 +254,9 @@ function createPairHandler(deps) {
254
254
 
255
255
  let issued;
256
256
  try {
257
- // 把电脑侧选定的档位展开成权限位,随凭据一起落库。`issuedExtra` 里本来可能有
258
- // 协议 v2 的密钥材料(sealed 那条路),这里合并进去,不覆盖。
259
- const grantedPermissions = permissions.permissionsForTier(state.pairingTier);
257
+ // 档位已移除:每台新配对的设备一律发全权。`issuedExtra` 里本来可能有协议 v2 的
258
+ // 密钥材料(sealed 那条路),这里合并进去,不覆盖。
259
+ const grantedPermissions = permissions.permissionsForTier('full');
260
260
  issued = credentials.issue(instanceId, device, '', undefined, { ...(issuedExtra || {}), permissions: grantedPermissions });
261
261
  } catch (error) {
262
262
  log('pair issue failed:', error && error.message);
@@ -330,8 +330,8 @@ function createPairHandler(deps) {
330
330
  cookie: `dsh_lan=${cookieValue}`,
331
331
  deviceId: issued.deviceId,
332
332
  deviceCredential: issued.credential,
333
- // 这台设备被授予的权限位(电脑侧档位展开)。App 落进会话记录,设备页据此显示当前档。
334
- permissions: Array.isArray(issued.permissions) ? issued.permissions : permissions.permissionsForTier(state.pairingTier),
333
+ // 这台设备被授予的权限位(一律全权)。App 落进会话记录。
334
+ permissions: Array.isArray(issued.permissions) ? issued.permissions : permissions.permissionsForTier('full'),
335
335
  credentialExpiresAt: issued.expiresAt,
336
336
  // 手机拿它对一次时钟(§8.3(7)):时钟偏太多会被当成重放,而用户完全不知道
337
337
  // 「这个 App 连不上」的原因是手机时间不准。
@@ -1,3 +1,4 @@
1
+ // 手机和面板共用固定端口配置,端口变更仍由监听生命周期处理。
1
2
  // 既有中转配置复用有界健康判据与出网诊断,保留本实例吊销回执收尾。
2
3
  'use strict';
3
4
 
@@ -68,8 +69,8 @@ function configView(state) {
68
69
  name: typeof state.name === 'string' ? state.name : '',
69
70
  lanEnabled: state.lanEnabled === true,
70
71
  lanPort: Number.isInteger(state.lanPort) ? state.lanPort : 18787,
72
+ lanPortFixed: state.lanPortFixed === true,
71
73
  pairingPolicy: pairingApproval.normalize(state.pairingPolicy),
72
- approvalAutoReview: state.approvalAutoReview === true,
73
74
  // 「自动上报网络地址」(总控上报)开关。非 false 一律当开,与宿主 configOf 同一口径。
74
75
  controlReportEnabled: state.controlReportEnabled !== false,
75
76
  preferredLanAddress: typeof state.preferredLanAddress === 'string' ? state.preferredLanAddress : '',
@@ -107,6 +108,10 @@ function configPatch(body) {
107
108
  if (typeof body.lanEnabled !== 'boolean') return { ok: false, message: 'lanEnabled 必须是布尔值' };
108
109
  patch.lanEnabled = body.lanEnabled;
109
110
  }
111
+ if (body.lanPortFixed !== undefined) {
112
+ if (typeof body.lanPortFixed !== 'boolean') return { ok: false, message: 'lanPortFixed 必须是布尔值' };
113
+ patch.lanPortFixed = body.lanPortFixed;
114
+ }
110
115
  if (body.lanPort !== undefined) {
111
116
  if (!Number.isInteger(body.lanPort) || body.lanPort < 1024 || body.lanPort > 65535) {
112
117
  return { ok: false, message: 'lanPort 必须是 1024–65535 之间的整数' };
@@ -126,10 +131,6 @@ function configPatch(body) {
126
131
  }
127
132
  patch.pairingPolicy = body.pairingPolicy;
128
133
  }
129
- if (body.approvalAutoReview !== undefined) {
130
- if (typeof body.approvalAutoReview !== 'boolean') return { ok: false, message: 'approvalAutoReview 必须是布尔值' };
131
- patch.approvalAutoReview = body.approvalAutoReview;
132
- }
133
134
  // 「自动上报网络地址」(总控上报)开关。只认布尔值,别的形状整次拒绝,别把 'false' 静默当成关。
134
135
  if (body.controlReportEnabled !== undefined) {
135
136
  if (typeof body.controlReportEnabled !== 'boolean') return { ok: false, message: 'controlReportEnabled 必须是布尔值' };