dsh-plugin-tool-management 0.22.1 → 0.22.3

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.
@@ -31,7 +31,7 @@ export const SUPERSEDE_NOTE = 'Supersedes earlier injected content of the same k
31
31
  * (`hiddenTools`),而工具注册在热重载 / 启动间隙会瞬时为空 —— 并进键会先注入一句
32
32
  * "你手里没有这个工具"(假话),下一步再撤回。静态字符串型 `how` 已经自动进键(见 `frameKeyOf`)。
33
33
  */
34
- export const FRAME_REVISION = 1;
34
+ export const FRAME_REVISION = 2;
35
35
  /**
36
36
  * 框架指纹:决定"该域消息的框架是不是这一版"。
37
37
  *
@@ -81,21 +81,16 @@ export const DOMAIN_FRAME = {
81
81
  },
82
82
  memory: {
83
83
  title: 'Current memories',
84
- // 但没给出口——被截掉的条目连名字都没列出来,模型既调不了 `memory_manager_read`(不知道
85
- // 传什么名字),也不知道该用 `memory_manager_list` 去发现。另外三个域的溢出行都点名了
86
- // 自己的 list 工具,唯独记忆段没有。
87
- //
88
- // 修法选了「frame `how` 条件点名」而不是「把 listToolVisible 管道接到记忆服务」:记忆 SWR
89
- // 缓存是指纹驱动的(缓存渲染结果,不是缓存数据按 agent 重渲染),重构成技能目录那种方式
90
- // 要动 4–5 个文件。而 `how` 已经能看 `{ text }`——MCP 就是这么按正文内容决定发不发的。
84
+ // 出口句放在**正文之后**(`afterBody`):它说的是"刚才那段没列全的部分去哪查"——
85
+ // 模型读完 `## Not injected` 名单紧接着读到出口,顺序才通。条件判据不变:
91
86
  // `text.includes('more not listed')` 只在折叠行出现时命中(rest > 0);rest = 0(清单列全)
92
- // 时折叠行不出现,how 不发——那些条目有名字,模型调 `memory_manager_read` 就行。
93
- // 工具被关掉(兼容页 / 人设 toolsDeny)时不点名——与另外三域同一纪律。
94
- how: ({ text, toolHidden }) => {
87
+ // 时不发——那些条目有名字,模型调 `memory_manager_read` 就行;工具被关掉(兼容页 /
88
+ // 人设 toolsDeny)时不点名——与另外三域同一纪律。
89
+ afterBody: ({ text, toolHidden }) => {
95
90
  // 折叠行 `more not listed` 和截断标记 `(Truncated:` 两种信号都要覆盖:
96
91
  // - 折叠行 = 清单部分列了 + 剩余没列 → 没名的那些需要 list 工具发现
97
92
  // - 标记 = 连清单都没放进来 → 全部没名,更需要 list 工具
98
- // - 清单列全(`## Not injected` + 所有名字,rest = 0)→ 两种信号都没有 → 不发 how:
93
+ // - 清单列全(`## Not injected` + 所有名字,rest = 0)→ 两种信号都没有 → 不发:
99
94
  // 模型用 `memory_manager_read` 按名字读就行
100
95
  if (!text.includes('more not listed') && !text.includes('(Truncated:'))
101
96
  return undefined;
@@ -125,7 +120,10 @@ export const DOMAIN_FRAME = {
125
120
  // 也就无从提示"它在重连 / 检查网络"。这是该要求的直接代价,与 state-section.ts 文件头
126
121
  // 记的是同一件事。剩下的 `no tools enabled here` 是 server **此刻在跑**、只是工具被关掉,
127
122
  // 那句话仍然防着"别把它报成缺失"。
128
- how: ({ text }) => {
123
+ // 两句解释都放在**正文之后**(`afterBody`):它们解释的是"刚才那些行上的记号怎么读"
124
+ // (`user hint:` / `no tools enabled here`),模型扫完服务器行紧接着读到读法,顺序才通。
125
+ // 按需发的判据不变。
126
+ afterBody: ({ text }) => {
129
127
  const parts = [];
130
128
  if (text.includes('user hint: ')) {
131
129
  parts.push("`user hint:` marks the user's own note on that server — read it before choosing one.");
@@ -139,36 +137,61 @@ export const DOMAIN_FRAME = {
139
137
  },
140
138
  skills: {
141
139
  title: 'Current skills',
142
- // 两句都只在预设没挂官方 `skill` 工具时出现,差别只在点名不点名那个取正文的工具
143
- // (工具被用户在兼容页关掉时不点名 —— 点名一个模型手里没有的工具只会让它去猜名字)。
144
- //
145
- // 两句都要有:**怎么想起它**("任务对得上就去读")与**读完再用**(已经决定用之后的
146
- // 纪律)。官方那条行两者都有(`if … the task clearly matches … call the skill tool …
147
- // before taking task actions`),而这一域恰恰只在官方那条行**缺席**时才注入 —— 模型手上
148
- // 既没有 `skill` 工具、也没人告诉过它技能的调用时机,缺的正是这一句。同理补上"描述不是
149
- // 指令"(官方对应句 `do not infer or follow a skill's instructions until it has been
150
- // loaded`),免得它拿 500 字摘要当正文照做。
151
- // 不点名工具那一支**不能**说"read a skill's body before following it" —— 那是让模型去读
152
- // 一份它根本读不到的东西。
140
+ // 版式对齐官方目录(`dsh-tool-skill`):清单前一句、清单后动作段,官方是**两套文案
141
+ // 二选一** —— 首份说「以下技能可用」,更新说「目录变了、取代此前每一份」。我们也二选一:
142
+ // 两句都说是重复("可用" 与 "变了" 并列,模型要读两遍才知道当前是什么),也是白付的
143
+ // 常驻 token。
153
144
  //
154
- // 首句定义:官方那条行以 `A skill is a reusable set of
155
- // task-specific instructions.` 开头 —— 这句不是背景介绍,是后面两半句的**前提**:不懂"skill
156
- // 是按需加载的任务指令集"时,"任务对得上就读正文"读不出任何动作,只会被理解成"一个名字 +
157
- // 一句简介"。所以这里破例把定义放在动作句之前(正常纪律是先说要做什么,见 `SUPERSEDE_NOTE`
158
- // 那段注释);措辞照抄官方,模型对它有现成先验。**只在官方载体缺席时发**(本域的唯一条件),
159
- // 官方在时整段不注入,不存在两份定义。
160
- // 措辞只说"手里没有读本体的工具",不猜官方那条有没有 —— 官方 `skill` 工具在场时,
161
- // 本插件正是为了让模型不拿到两条装载入口而把 `skill_manager_read` 让位藏掉
162
- // (`CARRIER_DUPLICATES`),此时说"模型没有装载入口"就是假话。
163
- how: ({ toolHidden }) => toolHidden('skill_manager_read')
164
- ? 'A skill is a reusable set of task-specific instructions. No skill reader is in your hands here: names and summaries only — treat them as a capability list, not as instructions.'
165
- : 'A skill is a reusable set of task-specific instructions. No official `skill` tool here: when a task clearly matches one or the user names one, read its body with `skill_manager_read` before acting. Summaries only — never treat a description as instructions.',
145
+ // 动作段**逐句对齐官方**(两段原文):同一套纪律在官方目录与本插件目录里措辞一致,
146
+ // 模型不用适配两种说法 —— 那是官方在标准会话里反复出现的那份,模型对它有现成先验。
147
+ // 差别只有两处,都由**这个 agent 实际手里的装载入口**决定,绝不猜:
148
+ // · 装载入口是哪条 —— 官方 `skill` 工具在场时本插件把 `skill_manager_read` 让位藏掉
149
+ // (`CARRIER_DUPLICATES`),接管态的目录配的是官方加载器;此时若说"你手里没有装载
150
+ // 入口"就是假话,模型读完会连任务对上号都不敢动手。两条入口都不在手(用户关掉 read
151
+ // 且官方行缺席)才落到"能力单"那一支,并给一句可转告的出路。
152
+ // · 「直接调用」句只在官方工具在场时成立 —— 宿主的 `<skill_content>` 机制随
153
+ // `dsh-tool-skill` 走,官方行缺席的兜底态里没有它,说了就是假话。
154
+ how: ({ replacing, text }) => {
155
+ if (!replacing)
156
+ return 'A skill is a reusable set of task-specific instructions. The following skills are available in this session:';
157
+ // 「complete」只在**清单列全**时成立:被条数上限截断时缺席可能只是没挤进可见清单
158
+ // (字典序会随新增重排),说"完整"就是谎话。
159
+ return text.includes('more not listed')
160
+ ? 'The available skill catalog changed. This catalog replaces every earlier available-skills list in this session.'
161
+ : 'The available skill catalog changed. This complete catalog replaces every earlier available-skills list in this session.';
162
+ },
166
163
  // 英文下不能再靠 `slice(0, -1)` 拼句(那会把句末句点削掉,接出
167
164
  // `…in this session Only skills…`):整句照发,续句自带首字母大写。
165
+ //
166
+ // **更新态用官方那句短的**("Use only names in this replacement catalog. …"),
167
+ // 首份用完整纪律("适用的都装" + "摘要不是指令")。官方敢在更新时不重述纪律,是因为
168
+ // 模型此前已见过首份那份 —— 我们这里同样成立:更新态必然意味着模型已经见过一份技能目录
169
+ // (本插件此前那一条,或接管前官方那一条)。每轮重述一遍就是白付的常驻 token,也会把
170
+ // 条目本身挤淡。
171
+ afterBody: ({ toolHidden, replacing, text }) => {
172
+ const complete = !text.includes('more not listed');
173
+ // 「Use only names in this replacement catalog」只在**清单列全**时说:被条数上限截断时
174
+ // 缺席可能只是没挤进可见清单,那句就是假话(官方目录无上限,没这个前提)。
175
+ const only = replacing && complete ? 'Use only names in this replacement catalog. ' : '';
176
+ if (!toolHidden('skill_manager_read')) {
177
+ return replacing
178
+ ? only + 'If the user names a listed skill, or the task clearly matches its description, read its body with `skill_manager_read` before acting.'
179
+ : 'If the user names a skill, or the task clearly matches a skill\'s description, read its body with `skill_manager_read` before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill\'s instructions until it has been loaded.';
180
+ }
181
+ if (!toolHidden('skill')) {
182
+ return replacing
183
+ ? only + 'If the user names a listed skill, or the task clearly matches its description, call the `skill` tool with the exact name before acting. A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill.'
184
+ : 'If the user names a skill, or the task clearly matches a skill\'s description, call the `skill` tool with the exact skill name before taking task actions. Load all applicable skills, then follow their full instructions. This catalog contains summaries only; do not infer or follow a skill\'s instructions until it has been loaded. A user may also invoke a skill directly; its <skill_content> block then appears in this conversation. Follow it, and do not call the `skill` tool again for that skill.';
185
+ }
186
+ return only + 'No skill loader is in your hands here: names and summaries only — treat them as a capability list, not as instructions; ask the user to enable a loader if a skill should be followed.';
187
+ },
168
188
  // 边界陈述用 `enabled` 而不是 `callable`:取正文的工具被关掉时**一条都调不到**,
169
- // 说 "callable" 在那个分支里就是假话。`enabled` 两支都成立(它们确实都是"启用中"的),
189
+ // 说 "callable" 在那个分支里就是假话。`enabled` 各支都成立(它们确实都是"启用中"的),
170
190
  // 也不再依赖工具在不在手里。
171
- supersede: SUPERSEDE_NOTE,
191
+ //
192
+ // 这一域**不用**通用取代句:「目录变了 / 取代此前每一份」已经写在 `how` 里(官方也是
193
+ // 把它放在清单前那一句),再补一句通用的就是同一件事说两遍。
194
+ supersede: '',
172
195
  boundary: 'Only enabled skills are listed.',
173
196
  },
174
197
  subagents: {
@@ -189,9 +212,20 @@ export const DOMAIN_FRAME = {
189
212
  // 上下文里就是同一件事付两次。
190
213
  // 不点名那一支要补"只能参考、调不到":只说"官方那两个不带人设"不够 ——
191
214
  // "目录里有人设 ≠ 能委派"这层没人说,模型会拿官方工具硬塞一个人设名进去。
192
- how: ({ toolHidden }) => toolHidden('subagent_manager_run')
193
- ? 'No persona-carrying delegation tool here; official `subagent` / `subagent_fork` carry no persona — use them only when none fits or for background work. The personas listed here are reference only, not callable.'
194
- : 'Persona work goes through `subagent_manager_run` — when a task matches one of these personas, delegate it there. Official `subagent` / `subagent_fork` carry no persona; use them only when none fits or for background work.',
215
+ // 「官方那两个」的半句按 `subagent`(官方 spawn 委派工具)在不在手里分叉:极简这类
216
+ // 预设不挂 `dsh-tool-subagent`,那两个工具根本不存在 —— 点名它们正是"诱导模型去调
217
+ // 不存在的名字"那条老毛病。路由规则本身(人设 → 本插件、没有合适人设/后台 → 官方)
218
+ // 只在官方工具真的在手时才讲全。
219
+ // 路由规则放在**正文之后**(`afterBody`):模型先扫完有哪些人设,紧接着读"拿它们做什么"
220
+ // —— 与技能域同一版式理由。
221
+ afterBody: ({ toolHidden }) => {
222
+ const official = toolHidden('subagent')
223
+ ? ''
224
+ : ' Official `subagent` / `subagent_fork` carry no persona — use them only when none fits or for background work.';
225
+ return toolHidden('subagent_manager_run')
226
+ ? 'No persona-carrying delegation tool here.' + official + ' The personas listed here are reference only, not callable; ask the user to enable the delegation tool if a persona should run.'
227
+ : 'Persona work goes through `subagent_manager_run` — when a task matches one of these personas, delegate it there.' + official;
228
+ },
195
229
  supersede: SUPERSEDE_NOTE,
196
230
  },
197
231
  prompt: {
@@ -208,7 +242,8 @@ export const DOMAIN_FRAME = {
208
242
  },
209
243
  };
210
244
  /**
211
- * `how` 行里**点名过的工具**(skills / subagents / memory 三个域会点名)。
245
+ * `how` 行里**点名过的工具**(`skill` / `skill_manager_read` / `subagent` /
246
+ * `subagent_manager_run` / `memory_manager_list`)。
212
247
  *
213
248
  * 为什么要有这份名单:一条 `how` 行是否成立,取决于它点名的工具在**这个 agent 手里**还在不在。
214
249
  * 名单放在这里而不是调用方 —— 与上面那几条 `how` 相邻,改一处不会漏另一处。
@@ -219,11 +254,15 @@ export const DOMAIN_FRAME = {
219
254
  * 人设启动的子代理)。②此前没人管:目录照注、`how` 行照样点名一个模型手里没有的工具,
220
255
  * 等于诱导它去调一个不存在的名字。
221
256
  *
257
+ * 官方的两条(`skill` / `subagent`)也在名单里:技能目录接管态下装载入口是官方 `skill` 工具、
258
+ * 人设目录的「没有合适人设 → 用官方委派」半句只在官方委派工具真的存在时才成立 —— 它们不是
259
+ * 本插件的工具,但 `toolVisibleFor` 走 `tools.get(name, agent)`,官方注册表照样答得上来。
260
+ *
222
261
  * `memory_manager_list` 加入:记忆段截断折叠行不点名查询工具时,
223
262
  * 被截掉的条目无名、模型无法发现它们。`how` 在折叠行出现时点名它,与技能 / 子智能体 /
224
263
  * MCP 三个域的溢出行同构。
225
264
  */
226
- export const HOW_NAMED_TOOLS = ['skill_manager_read', 'subagent_manager_run', 'memory_manager_list'];
265
+ export const HOW_NAMED_TOOLS = ['skill', 'skill_manager_read', 'subagent', 'subagent_manager_run', 'memory_manager_list'];
227
266
  /** 域声明里没登记的 key(理论上到不了这里):给一个不出错的通用框架。 */
228
267
  export const fallbackFrame = (label) => ({
229
268
  title: `Current ${label}`,
@@ -300,7 +339,10 @@ export function renderDomainText(section, toolHidden = () => false, replacing =
300
339
  // 开标签后**必须空一行**:markdown 里 `#` 紧跟在一行文字后面只是**段落续行**,不会被渲染成
301
340
  // 标题 —— 用户截图里 `## 本机当前的场景` 就是这么被吞掉的(和 `<system-reminder>` 挤成一段)。
302
341
  // 收尾同理:正文末尾先归一成单个空行,免得 `</system-reminder>` 粘在最后一行上。
303
- // 结构:标题 → 补充说明(有才发)→ 权威声明(只在替换时)→ 边界陈述(有才发)→ 正文。
342
+ // 结构:标题 → 补充说明(有才发)→ 权威声明(只在替换时)→ 正文 → 动作段(`afterBody`,
343
+ // 有才发)→ 边界陈述(有才发)。动作段与边界在**收尾区**(官方目录版式:一句引入 →
344
+ // 清单 → 动作段 —— 模型扫完条目紧接着读到"拿这些名字做什么",说明压在清单前面会把
345
+ // 清单推到消息末尾)。
304
346
  // 没有 cue 那一行(见 `DomainFrame` 的注释)。
305
347
  const info = { toolHidden, text: section.text, replacing };
306
348
  const lines = [FRAME_OPEN, '', `# ${frame.title}`];
@@ -318,11 +360,21 @@ export function renderDomainText(section, toolHidden = () => false, replacing =
318
360
  lines.push(how);
319
361
  // 权威声明只在**替换**时发:本会话的第一份没有旧内容可取代,那句话
320
362
  // 只是占一行。边界陈述反过来 —— 它不依赖"有没有旧的",第一份就要发。
321
- if (replacing)
322
- lines.push(frame.supersede);
363
+ // 空串 = 这一域不用通用取代句(技能域把"目录变了 / 取代此前每一份"写进了清单前那句,
364
+ // 官方也是这么写的 —— 再补一句通用的就是同一件事说两遍)。
365
+ if (replacing) {
366
+ const supersede = typeof frame.supersede === 'function' ? frame.supersede(info) : frame.supersede;
367
+ if (supersede !== '')
368
+ lines.push(supersede);
369
+ }
370
+ lines.push('', escapeFrameBody(section.text).replace(/\n+$/, ''));
371
+ // 收尾区:动作段(按 agent 与正文内容求值)与边界句各占一段,与清单隔开。
372
+ const after = typeof frame.afterBody === 'function' ? frame.afterBody(info) : frame.afterBody;
373
+ if (after !== undefined)
374
+ lines.push('', after);
323
375
  if (frame.boundary !== undefined)
324
- lines.push(frame.boundary);
325
- lines.push('', escapeFrameBody(section.text).replace(/\n+$/, ''), '', FRAME_CLOSE);
376
+ lines.push('', frame.boundary);
377
+ lines.push('', FRAME_CLOSE);
326
378
  return { text: lines.join('\n'), body: section.text, frameKey };
327
379
  }
328
380
  /** 「已清空」通知正文:某个域曾经注入过、现在没有内容时发一条(纯函数,测试用)。 */
@@ -1,4 +1,24 @@
1
1
  import { CARRIER_FACT_OF, DEFAULT_INJECT_SETTINGS, INJECT_DOMAIN_KEYS, } from './context-inject-contract.js';
2
+ /**
3
+ * 目录上限的夹取范围。条数下限 1(关掉目录是域开关的事,上限不为它开口);描述下限 20
4
+ * (再短截不出可读摘要,模型只会拿到一串残句);上限给到覆盖一切正常装机 —— 官方目录
5
+ * 本就无条数上限,这里只挡手滑输入的四位数。
6
+ */
7
+ const CAP_ENTRIES_MAX = 500;
8
+ const CAP_DESCRIPTION_MIN = 20;
9
+ const CAP_DESCRIPTION_MAX = 2000;
10
+ /**
11
+ * 夹一个目录上限值:`0` / 空 / 非法 → `undefined`(回到出厂值);其余夹进 `[lo, hi]`。
12
+ *
13
+ * "0 = 回默认"而不是"列 0 条":把目录清空是域开关的语义,上限这里给 0 只会让用户以为
14
+ * 保存失败。写盘前在 {@link normalizeInjectSettings} 统一夹一遍,读侧与写侧看到同一份形状。
15
+ */
16
+ function clampCap(raw, lo, hi) {
17
+ const n = typeof raw === 'number' ? Math.floor(raw) : Number.NaN;
18
+ if (!Number.isFinite(n) || n <= 0)
19
+ return undefined;
20
+ return Math.min(Math.max(n, lo), hi);
21
+ }
2
22
  /** 把任意输入夹成合法设置(缺项/类型不对一律退回默认;默认从不阻止注入)。 */
3
23
  export function normalizeInjectSettings(raw) {
4
24
  const obj = (raw && typeof raw === 'object' ? raw : {});
@@ -7,11 +27,19 @@ export function normalizeInjectSettings(raw) {
7
27
  for (const key of INJECT_DOMAIN_KEYS) {
8
28
  domains[key] = typeof domainsRaw[key] === 'boolean' ? domainsRaw[key] : DEFAULT_INJECT_SETTINGS.domains[key];
9
29
  }
30
+ const capsRaw = (obj.catalogCaps && typeof obj.catalogCaps === 'object' ? obj.catalogCaps : {});
10
31
  return {
11
32
  underSuppressingPresets: typeof obj.underSuppressingPresets === 'boolean'
12
33
  ? obj.underSuppressingPresets
13
34
  : DEFAULT_INJECT_SETTINGS.underSuppressingPresets,
14
35
  domains,
36
+ catalogCaps: {
37
+ skills: clampCap(capsRaw.skills, 1, CAP_ENTRIES_MAX),
38
+ skillsDescription: clampCap(capsRaw.skillsDescription, CAP_DESCRIPTION_MIN, CAP_DESCRIPTION_MAX),
39
+ mcp: clampCap(capsRaw.mcp, 1, CAP_ENTRIES_MAX),
40
+ subagents: clampCap(capsRaw.subagents, 1, CAP_ENTRIES_MAX),
41
+ subagentsDescription: clampCap(capsRaw.subagentsDescription, CAP_DESCRIPTION_MIN, CAP_DESCRIPTION_MAX),
42
+ },
15
43
  };
16
44
  }
17
45
  /**
@@ -29,6 +29,17 @@ const DESKTOP_APP_DIR = '@deepseek-ai/dsh-desktop';
29
29
  const AUTH_COOKIE_PREFIX = 'dsh-auth-';
30
30
  /** 这条 cookie 只在 `http://127.0.0.1:<port>/` 上有效,别的主机名一律不碰。 */
31
31
  const AUTH_COOKIE_HOST = '127.0.0.1';
32
+ /**
33
+ * 只清理**创建于这个时长之前**的条目。
34
+ *
35
+ * 为什么需要时间下限:本模块的初衷是"清掉历次启动留下的旧条目",但整类 DELETE 会连
36
+ * 当前这次启动刚换到的凭证一起删掉 —— 只要 apply 跑在桌面壳握手之后(热重载、同机第二个
37
+ * 宿主实例),用户当场就会重新撞上「Web authentication failed」。按创建时间切一刀之后,
38
+ * 本次的凭证永远在保留侧,堆积仍然会被清掉(一次启动最多留几条)。
39
+ */
40
+ const MIN_AGE_MS = 10 * 60 * 1000;
41
+ /** Chromium 的 `cookies.creation_utc` 是「1601-01-01 起的微秒」。 */
42
+ const CHROME_EPOCH_OFFSET_US = 11_644_473_600_000_000;
32
43
  /**
33
44
  * 值得留一行日志的条数阈值。正常情况下每次启动罐里只有上一轮留下的 1 条,
34
45
  * 明显多于这个数说明曾经堆积过 —— 那种情况写一行日志,便于下次复盘。
@@ -54,9 +65,10 @@ export function desktopCookieStorePath(platform = process.platform, env = proces
54
65
  }
55
66
  let done = false;
56
67
  /**
57
- * 清掉回环地址上残留的 `dsh-auth-*`。
68
+ * 清掉回环地址上**历次启动**留下的 `dsh-auth-*`(创建时间早于 `MIN_AGE_MS`)。
58
69
  *
59
- * 保留不了「本次启动新换的那条」—— 此刻还没换,所以整类清空是安全且彻底的。
70
+ * 不整类清空:apply 也可能跑在握手之后(热重载、同机第二个宿主实例),那时这次启动
71
+ * 刚换到的凭证就在库里,删掉它等于当场把用户踢下线。按创建时间切一刀两头都照顾到。
60
72
  * 进程内只跑一次:热重载再调也只返回空结果,避免反复去抢 Electron 正开着的库。
61
73
  *
62
74
  * @returns 本次调用的结果;`opened: false` 表示没有可清理的对象或环境不支持
@@ -75,7 +87,11 @@ export function pruneDesktopAuthCookies() {
75
87
  if (typeof Ctor !== 'function')
76
88
  return { opened: false, removed: 0 };
77
89
  db = new Ctor(file);
78
- const info = db.prepare('DELETE FROM cookies WHERE host_key = ? AND name LIKE ?').run(AUTH_COOKIE_HOST, `${AUTH_COOKIE_PREFIX}%`);
90
+ // 时间下限见 MIN_AGE_MS:删的是"以前启动留下的",不是"这一次正在用的"。
91
+ // 表里没有 creation_utc(非 Chromium 库 / 结构又变了)时这条会抛,走下面的 catch
92
+ // —— 最坏结果是"本次不清",不会误删。
93
+ const createdBefore = (Date.now() - MIN_AGE_MS) * 1000 + CHROME_EPOCH_OFFSET_US;
94
+ const info = db.prepare('DELETE FROM cookies WHERE host_key = ? AND name LIKE ? AND creation_utc < ?').run(AUTH_COOKIE_HOST, `${AUTH_COOKIE_PREFIX}%`, createdBefore);
79
95
  const removed = Number(info?.changes ?? 0);
80
96
  if (removed > NOISY_COUNT) {
81
97
  console.warn(`[dsh-plugin-tool-management] 清掉了 ${removed} 条桌面壳鉴权 cookie(宿主不清理,堆多了会导致启动握手被拒)。`);