sage-mem 2.33.3 → 3.0.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.
@@ -188,6 +188,11 @@ export function isSafeSyncName(name) {
188
188
  if (n === '' || n.length > 200) return false
189
189
  if (n.includes('/') || n.includes('\\') || n.includes('\0')) return false
190
190
  if (n === '.' || n === '..') return false
191
+ /**
192
+ * 扩展名判定,**不是名字比较**:上面三行已经拒掉了 `/` `\` `\0` 与 `.` / `..`,
193
+ * 所以这里的 `.toLowerCase()` 与 `nameKey` 同义(本文件是零 import 的纯策略层,
194
+ * 不为了字面统一去拉 `memory/naming.js` —— 它会经 `memory/config.js` 在 import 时读盘)。
195
+ */
191
196
  if (!n.toLowerCase().endsWith('.md')) return false
192
197
  return true
193
198
  }
@@ -509,6 +514,218 @@ function outcomeTail(result) {
509
514
  return ''
510
515
  }
511
516
 
517
+ // ── 推送(3.0.0)────────────────────────────────────────────────────
518
+ //
519
+ // 与拉取严格对称:**同一套分类、同一张三态表、同一批纪律**,只是动作方向相反。
520
+ // 这里不新增任何分类 —— 如果推送需要新的类别,说明分类器本身错了,该回去改它。
521
+
522
+ /** 一个文件在这次推送里能有的四种去向。 */
523
+ export const PUSH_ACTIONS = ['upload', 'overwrite', 'conflict', 'skip']
524
+
525
+ /**
526
+ * 类别 → 推送动作(**唯一真相**)。
527
+ *
528
+ * 与 `PULL_ACTION_OF` 逐行对照着读:凡是拉取「搬进来」的,推送就「不处理」(那是对方的事);
529
+ * 凡是拉取「不处理」的,推送也不处理。只有两列真正反过来:
530
+ *
531
+ * | 类别 | 拉取 | 推送 |
532
+ * |---|---|---|
533
+ * | `local-new` | skip | **upload** |
534
+ * | `local-changed` | skip | **overwrite** |
535
+ * | `remote-new` | import | skip |
536
+ * | `remote-changed` | overwrite | skip |
537
+ *
538
+ * 三条共同纪律(两个方向都不许破例):
539
+ * 1. **删除永不自动执行**。本地删了不推删除,远端删了也不替它删 —— 删除是唯一
540
+ * 没有「留两份」兜底的破坏性动作,它必须由人明确做。
541
+ * 2. **写之前核 revision**。拉取写本地要核本地 sha,推送写远端要核**远端** sha
542
+ * (清单是检查那一刻的事实,上传时可能已经变了)。
543
+ * 3. **`inconsistent` 只报不猜**。
544
+ *
545
+ * `local-changed-remote-deleted` 推向 `skip` 而不是 conflict:远端那份正文已经不存在,
546
+ * 凑不出「两份」。要「按远端删掉本地」还是「把本地推回去」,是人的决定,两个动作都不该自动做。
547
+ */
548
+ export const PUSH_ACTION_OF = Object.freeze({
549
+ same: 'skip',
550
+ 'remote-new': 'skip',
551
+ 'local-new': 'upload',
552
+ 'remote-changed': 'skip',
553
+ 'local-changed': 'overwrite',
554
+ 'both-changed': 'conflict',
555
+ 'both-new-diverged': 'conflict',
556
+ 'local-deleted-remote-changed': 'skip',
557
+ 'local-changed-remote-deleted': 'skip',
558
+ 'remote-deleted': 'skip',
559
+ 'local-deleted': 'skip',
560
+ inconsistent: 'skip',
561
+ })
562
+
563
+ /**
564
+ * 把分类结果折成「每个文件一行、带去向」的推送清单(纯函数)。
565
+ *
566
+ * 与 `planPull` 一样返回**全部**条目(含 `skip`):报告要把「看见了但没动它」的也念出来,
567
+ * 否则「本地删掉的那个没推出去」会变成一个看不见的洞。
568
+ *
569
+ * @param {object} result — `classifySync()` 的返回值
570
+ * @returns {Array<{name:string, class:string, action:string, local?:string, remote?:string, base?:string}>}
571
+ */
572
+ export function planPush(result) {
573
+ return (result?.entries ?? []).map((e) => ({
574
+ name: e.name,
575
+ class: e.class,
576
+ action: PUSH_ACTION_OF[e.class] ?? 'skip',
577
+ local: e.local,
578
+ remote: e.remote,
579
+ base: e.base,
580
+ }))
581
+ }
582
+
583
+ /** 按去向计数(报告抬头与工具回执共用)。 */
584
+ export function pushSummary(plan = []) {
585
+ const out = { upload: 0, overwrite: 0, conflict: 0, skip: 0, total: 0 }
586
+ for (const item of plan) {
587
+ const a = PUSH_ACTIONS.includes(item.action) ? item.action : 'skip'
588
+ out[a] += 1
589
+ out.total += 1
590
+ }
591
+ return out
592
+ }
593
+
594
+ /**
595
+ * 渲染推送报告。
596
+ *
597
+ * 与拉取报告同一套措辞纪律,多一节「门」:推送前必须先说清**凭什么**认为远端是自己的库
598
+ * (状态目录 / 设备身份 / 是否已确认)。这一节不能省 —— 用户点「推送」时脑子里想的
599
+ * 是「把我的记忆同步过去」,而插件必须让他看见「我确认过那是谁」。
600
+ *
601
+ * @param {object} input
602
+ * @param {Array} input.plan — `planPush()` 的结果
603
+ * @param {string} input.host
604
+ * @param {string} input.remotePath
605
+ * @param {string} [input.at]
606
+ * @param {boolean} [input.apply=false]
607
+ * @param {boolean} [input.hasBase=true]
608
+ * @param {object} [input.gate] — `gateVerdict()` 的返回值(可缺省:预演前的早退路径)
609
+ * @param {string} [input.gateLabel] — 门结论的中文短句(由 gate.js 提供,避免两处各写一份)
610
+ * @param {string} [input.gateHint] — 门结论对应的「该怎么办」(同上,只有一份)
611
+ * @param {Array<{name:string, status:string, code?:string, error?:string, conflictId?:string}>} [input.results]
612
+ * @param {number} [input.bytes] — 将要 / 已经上传的总字节
613
+ * @param {number} [input.skippedRemote]
614
+ * @param {number} [input.skippedLocal]
615
+ * @returns {string}
616
+ */
617
+ export function renderPushReport({
618
+ plan = [],
619
+ host,
620
+ remotePath,
621
+ at,
622
+ apply = false,
623
+ hasBase = true,
624
+ gate = null,
625
+ gateLabel = '',
626
+ gateHint = '',
627
+ results = [],
628
+ bytes = 0,
629
+ skippedRemote = 0,
630
+ skippedLocal = 0,
631
+ }) {
632
+ const summary = pushSummary(plan)
633
+ const busy = summary.total - summary.skip
634
+ const byName = new Map(results.map((r) => [r.name, r]))
635
+ const lines = []
636
+
637
+ lines.push(`# 跨设备同步 · 推送报告(3.0.0)· ${apply ? '已执行' : '预演(dry-run)'}`)
638
+ lines.push('')
639
+ lines.push(`- 远端:\`${host}\` 的 \`${remotePath}\``)
640
+ if (at) lines.push(`- 生成时间:${at}`)
641
+ lines.push(`- 基线:${hasBase ? '有(上次同步的哈希表)' : '**无 —— 首次同步:只做排他新建,不覆盖远端任何已有文件**'}`)
642
+ lines.push(`- 扫描:本地 ${plan.length} 个参与比对;远端清单跳过 ${skippedRemote} 条,本地跳过 ${skippedLocal} 个`)
643
+ if (gate) {
644
+ const who = gate.deviceId
645
+ ? `(\`${gate.deviceId}\`${gate.deviceLabel ? ` · ${gate.deviceLabel}` : ''})`
646
+ : ''
647
+ lines.push(`- 写入门:${gate.allow ? '✅' : '⛔️'} ${gateLabel || gate.reason}${who}`)
648
+ }
649
+ lines.push('')
650
+
651
+ // 硬闸拦下时**不念计划**:计划是基于「远端可能是我的库」算出来的,而被拦下正是因为这个前提
652
+ // 不成立。把一份「会传 12 个文件」的清单摆在一个拒绝回执里,读起来像是在建议照做。
653
+ if (gate && gate.allow !== true && gate.needConfirm !== true) {
654
+ lines.push('**推送被写入门拦下,什么都没做。**')
655
+ lines.push('')
656
+ if (gateHint) {
657
+ lines.push(`> ${gateHint}`)
658
+ lines.push('')
659
+ }
660
+ return `${lines.join('\n')}\n`
661
+ }
662
+
663
+ if (busy === 0) {
664
+ lines.push('**没有需要推送的文件。**')
665
+ if (summary.skip > 0) {
666
+ lines.push('')
667
+ lines.push(`> ${summary.skip} 个文件有差异但不该由推送处理(远端新增/改动是拉取的事,删除永不自动执行)。`)
668
+ }
669
+ lines.push('')
670
+ lines.push(apply ? '> 本次运行没有修改任何文件。' : '> 这是预演。加 `apply: true` 才会真的动手。')
671
+ return `${lines.join('\n')}\n`
672
+ }
673
+
674
+ lines.push(
675
+ `共 **${busy}** 个文件可以处理:新建 **${summary.upload}** / 覆盖 **${summary.overwrite}** / 留两份待裁决 **${summary.conflict}**`
676
+ + `(合计 ${bytes} 字节);另有 **${summary.skip}** 个差异项不处理。`,
677
+ )
678
+ lines.push('')
679
+
680
+ if (gate && gate.needConfirm === true) {
681
+ lines.push('> ⛔️ 写入门还要一次**手工确认**:认过上面的远端身份之后,带 `confirm: true` 再来一次。')
682
+ lines.push('> 预演不会留下任何痕迹 —— 只有带 `confirm` 的真推送才会把「这台设备是可信的」记进基线。')
683
+ lines.push('')
684
+ }
685
+
686
+ const section = (title, action, note) => {
687
+ const names = plan.filter((p) => p.action === action).map((p) => p.name)
688
+ if (names.length === 0) return
689
+ lines.push(`## ${title}(${names.length})`)
690
+ lines.push('')
691
+ if (note) {
692
+ lines.push(`> ${note}`)
693
+ lines.push('')
694
+ }
695
+ for (const name of names) lines.push(`- \`${name}\`${outcomeTail(byName.get(name))}`)
696
+ lines.push('')
697
+ }
698
+
699
+ section('新建(远端没有这个文件)', 'upload', '> 远端用**排他创建**:撞名一律转成冲突,不会覆盖任何已有文件。')
700
+ section(
701
+ '覆盖(远端等于基线,没动过)',
702
+ 'overwrite',
703
+ apply
704
+ ? '> 写入前已核对远端当前 revision;不一致的那几条没有覆盖。覆盖前旧版已备份到远端 `<状态根>/backup/`。'
705
+ : '> 写入前会核对远端当前 revision;不一致就不覆盖。旧版会先备份到远端 `<状态根>/backup/`。',
706
+ )
707
+ section(
708
+ '留两份待裁决(两侧都动过)',
709
+ 'conflict',
710
+ '⚠️ 这些文件**不会**写到远端。远端那份正文已取回并存进冲突记录,到「冲突」页签里裁决。',
711
+ )
712
+
713
+ const skipped = plan.filter((p) => p.action === 'skip' && p.class !== 'same')
714
+ if (skipped.length > 0) {
715
+ lines.push(`## 未处理(${skipped.length})`)
716
+ lines.push('')
717
+ lines.push('> 按设计不由推送处理:远端新增/改动归拉取;**删除永不自动执行**(两个方向都不)。')
718
+ lines.push('')
719
+ for (const p of skipped) lines.push(`- \`${p.name}\`(${CLASS_LABEL[p.class] ?? p.class})`)
720
+ lines.push('')
721
+ }
722
+
723
+ lines.push(apply
724
+ ? '> 本次运行只动了上面列出的文件;删除、索引重建、归档都没有发生。'
725
+ : '> 这是预演,**远端一个字节都没收,本地一个字节都没改**。确认清单无误后加 `apply: true`。')
726
+ return `${lines.join('\n')}\n`
727
+ }
728
+
512
729
  // ── 设置面(与 `mirror/policy.js` 的 resolveExternal / parseExternalPatch 对称)──
513
730
  //
514
731
  // 为什么放在 policy 里而不是 settings.js:**校验判据只有一份**。
package/lib/sync/pull.js CHANGED
@@ -184,6 +184,12 @@ export async function runSyncPull({
184
184
  actualVersion: actualSha ?? '',
185
185
  note: `跨设备同步:${CLASS_LABEL[className] ?? className}${note ? `|${note}` : ''}`,
186
186
  tool: 'memory_sync_pull',
187
+ // 3.0.0:这条冲突是**跨设备同步**造的,且能指着具体是哪台远端 / 哪个目录 ——
188
+ // 面板靠它把「跨设备差异」与本地冲突分开渲染。`kind` 分不开这两种:
189
+ // 这里的 `external_change` 与本地 autodream 撞上的那一类是同名同义的。
190
+ origin: 'sync',
191
+ host,
192
+ remotePath,
187
193
  at,
188
194
  })
189
195
  // 队列里已经有「同一件事」就不再建第二条。判据走 `sameConflict`(同一判据只许一份实现):