@leiyi-works/dsh-session-tmpdir 0.10.1 → 0.10.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.
Files changed (3) hide show
  1. package/README.md +13 -0
  2. package/index.js +37 -6
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -253,3 +253,16 @@ v0.10.0 变更(2026-11-16):**白名单条目的路径可移植 + 全机队
253
253
  3. **界面去掉机器过滤**:面板显示整份共享白名单(不是“本机专属”);**本机没有的路径**标记「本机无此路径」(可能存在于其他机器),不会因此自动删除;可移植路径额外标「可移植」。
254
254
  4. 旧条目(绝对路径、home 下)在加载时自动归一为 `~/...` 并落盘;墓碑/合并/按机器回收逻辑不变。
255
255
  5. 仍无法自动统一:盘符/工作区路径不同、机器独有路径——这类按各自绝对路径判定。
256
+
257
+ v0.10.1 / v0.10.2 变更(2026-11-16):**解释器命令段落不再误判路径**。`node`/`python`/`perl`/`ruby` 出现在命令里时,以前会把整段命令的绝对路径都当成写目标(例如 `node -e "…打印一个不存在的示例路径…"` 被误拦)。现在:
258
+ - 只调用解释器、且同一段里没有重定向/其它写动词/写程序/下载写入时 → 改用代码静态扫描判定(只认“写文件语句 + 附近路径字面量”),字符串里的示例路径不再误伤;
259
+ - `node -e "…writeFileSync('D:/新文件'…)"`、`python -c "open(…,'w')"` 等真实写盘仍照常拦截;写 `_tmp` 放行。
260
+ - 0.10.2 修正了 `$node` 变量形式(前一个字符是 `$`)的识别盲区。
261
+
262
+ v0.10.1 已发布的包存在该盲区,请使用 **0.10.2**。
263
+
264
+ v0.10.3 变更(2026-11-16):
265
+
266
+ 1. **重复条目去重**:同「会话 + 类型(文件/目录)+ 展开后的目标路径」只保留一条(取较新的 `approvedAt`,`lastSeenAt` 取最大)。在加载、合并、保存三处都会去重,解决同一路径在多台机器各自批准后出现多条的情况。
267
+ 2. **墓碑 TTL 30 天 → 180 天**:降低“某台机器离线较久、回来同步时找不到墓碑导致已删条目复活”的概率(180 天内同步过就不会复活;超过仍是“重新问一次审批”,不会误放行)。
268
+ 3. 修复:0.10.0 起条目存的是可移植路径,但 `longterm_approve` 的重复检查仍在用“存储路径 vs 绝对路径”比较,永远不相等 → 同一个路径重复批准会不断追加。改为展开后再比较。
package/index.js CHANGED
@@ -143,7 +143,7 @@ const COMMAND_SEGMENT_SPLIT = /(?:\r?\n|;|&&|\|\||\|)+/
143
143
  * file write. Such a segment is judged by the code static scan (write signals paired with
144
144
  * nearby path literals) instead of blaming every absolute path in the command text.
145
145
  */
146
- const INTERPRETER_SEGMENT_PATTERN = /(?:^|[\\/\s"'=])(?:node(?:\.exe)?|python3?(?:\.exe)?|perl|ruby)\b/i
146
+ const INTERPRETER_SEGMENT_PATTERN = /(?:^|[\\/\s"'=$&])(?:node(?:\.exe)?|python3?(?:\.exe)?|perl|ruby)\b/i
147
147
  /** Create verbs excluding the interpreters above (other real write commands in the same segment). */
148
148
  const OTHER_CREATE_VERB_PATTERN =
149
149
  /\b(?:New-Item|mkdir|md(?=\s)|Copy-Item|Move-Item|Set-Content|Add-Content|Out-File|Expand-Archive|git\s+clone|robocopy|tar|unzip|7z|touch|tee|New-ItemProperty|cp|mv|ln|install|dd|rsync|scp|truncate|sed)\b/i
@@ -1334,8 +1334,8 @@ const APPROVALS_FILE = 'session-tmpdir-approvals.json'
1334
1334
  */
1335
1335
  const APPROVALS_SYNC_DIR = 'tmpdir-guard'
1336
1336
  const APPROVALS_SYNC_FILE = 'approvals.json'
1337
- /** v0.9.9: 删除墓碑 30 天后过期(防止已删除条目被其他机器同步“复活”)。 */
1338
- const APPROVALS_TOMBSTONE_TTL = 30 * 24 * 3600 * 1000
1337
+ /** v0.9.9: 删除墓碑 180 天后过期(v0.10.3 起从 30 天加长,降低离线机器“复活”概率)。 */
1338
+ const APPROVALS_TOMBSTONE_TTL = 180 * 24 * 3600 * 1000
1339
1339
 
1340
1340
  /** Sanitized machine id (hostname) used to tag approvals and guard pruning. */
1341
1341
  export function machineId() {
@@ -1416,6 +1416,36 @@ function normalizeApprovalEntry(entry, fallbackMachine) {
1416
1416
  }
1417
1417
  }
1418
1418
 
1419
+ /** Dedupe key: one logical approval per session + kind + expanded target path. */
1420
+ function approvalDedupeKey(entry) {
1421
+ return [entry.sessionId, entry.kind === 'dir' ? 'dir' : 'file', normalizeForCompare(expandPortableApprovalPath(entry.path))].join('\u0000')
1422
+ }
1423
+
1424
+ /**
1425
+ * v0.10.3: collapse duplicate approvals (same session/kind/expanded path, e.g. the same
1426
+ * file approved on two machines). Keeps the newest `approvedAt` and the max `lastSeenAt`.
1427
+ */
1428
+ export function dedupeApprovalEntries(entries) {
1429
+ const map = new Map()
1430
+ for (const entry of entries ?? []) {
1431
+ const key = approvalDedupeKey(entry)
1432
+ const prev = map.get(key)
1433
+ if (!prev) {
1434
+ map.set(key, { ...entry })
1435
+ continue
1436
+ }
1437
+ const prevSeen = Number(prev.lastSeenAt) || 0
1438
+ const nextSeen = Number(entry.lastSeenAt) || 0
1439
+ const seen = Math.max(prevSeen, nextSeen)
1440
+ if ((Number(entry.approvedAt) || 0) >= (Number(prev.approvedAt) || 0)) {
1441
+ map.set(key, { ...entry, lastSeenAt: seen })
1442
+ } else if (seen !== prevSeen) {
1443
+ prev.lastSeenAt = seen
1444
+ }
1445
+ }
1446
+ return [...map.values()]
1447
+ }
1448
+
1419
1449
  /**
1420
1450
  * Merge two whitelist states (v0.9.9): union by `id` with the newest `approvedAt` winning;
1421
1451
  * a tombstone removes entries approved before it. Used on every save (read-merge-write) so a
@@ -1440,7 +1470,7 @@ export function mergeApprovalsStates(base, other) {
1440
1470
  const stone = tombstones.get(entry.id)
1441
1471
  return !stone || stone.at <= entry.approvedAt
1442
1472
  })
1443
- return { version: 2, entries: kept, tombstones: [...tombstones.values()] }
1473
+ return { version: 2, entries: dedupeApprovalEntries(kept), tombstones: [...tombstones.values()] }
1444
1474
  }
1445
1475
 
1446
1476
  /** Load the whitelist: synced file first, then legacy machine-local files (migration sources). */
@@ -1458,7 +1488,7 @@ export function loadApprovalsState() {
1458
1488
  const tombstones = (Array.isArray(parsed?.tombstones) ? parsed.tombstones : [])
1459
1489
  .filter((stone) => stone && typeof stone.id === 'string')
1460
1490
  .map((stone) => ({ id: stone.id, at: Number(stone.at) || 0 }))
1461
- return { version: 2, entries, tombstones }
1491
+ return { version: 2, entries: dedupeApprovalEntries(entries), tombstones }
1462
1492
  } catch {
1463
1493
  // try the next candidate
1464
1494
  }
@@ -1477,6 +1507,7 @@ export function saveApprovalsState(state) {
1477
1507
  // first write (or corrupt file): keep the in-memory state
1478
1508
  }
1479
1509
  const now = Date.now()
1510
+ merged.entries = dedupeApprovalEntries(merged.entries)
1480
1511
  merged.tombstones = (merged.tombstones ?? []).filter((stone) => now - (Number(stone.at) || 0) < APPROVALS_TOMBSTONE_TTL)
1481
1512
  mkdirSync(dirname(file), { recursive: true })
1482
1513
  writeFileSync(
@@ -1845,7 +1876,7 @@ export function apply(ctx, config) {
1845
1876
  (entry) =>
1846
1877
  entry.sessionId === id &&
1847
1878
  (entry.kind === 'dir' ? 'dir' : 'file') === entryKind &&
1848
- normalizeForCompare(entry.path) === normalizeForCompare(normalized),
1879
+ normalizeForCompare(expandPortableApprovalPath(entry.path)) === normalizeForCompare(normalized),
1849
1880
  )
1850
1881
  if (!known) {
1851
1882
  approvalState.entries.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leiyi-works/dsh-session-tmpdir",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "type": "module",
5
5
  "description": "Per-session task temp folder with a long-term write guard. v0.9 (方案3): creating a new file outside _tmp requires the user's approval (per file by default, per directory with mode=dir), overwriting an existing file is allowed without asking, every write channel is guarded (write/edit, shell including ffmpeg/pandoc/Export-*/IO APIs, publish copies, code runtimes, unknown tools), and the persisted whitelist is shown in a left-sidebar panel「临时目录」 where entries can be removed. Every session also gets _tmp/<session title>/ in its working directory, cleaned 7 days after the session is deleted; the same tick reclaims the attachment store.",
6
6
  "exports": {