dsh-config-manager 0.8.1 → 0.9.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.
- package/lib/adapters/mcp.js +42 -13
- package/lib/adapters/messages.d.ts +1 -0
- package/lib/adapters/messages.js +2 -0
- package/lib/adapters/patch-key-guard.d.ts +23 -0
- package/lib/adapters/patch-key-guard.js +13 -0
- package/lib/adapters/plugin-data.d.ts +19 -5
- package/lib/adapters/plugin-data.js +76 -22
- package/lib/adapters/plugins.d.ts +14 -1
- package/lib/adapters/plugins.js +147 -4
- package/lib/cli/actions.d.ts +7 -105
- package/lib/cli/actions.js +16 -172
- package/lib/cli/control-state.d.ts +54 -0
- package/lib/cli/control-state.js +107 -0
- package/lib/cli/index.js +14 -0
- package/lib/cli/write-gates.d.ts +61 -0
- package/lib/cli/write-gates.js +110 -0
- package/lib/client.d.ts +122 -50
- package/lib/client.js +12676 -12091
- package/lib/core/backup.d.ts +0 -4
- package/lib/core/backup.js +21 -4
- package/lib/core/boot-rescue.d.ts +1 -1
- package/lib/core/boot-rescue.js +12 -1
- package/lib/core/boot-safety.js +37 -2
- package/lib/core/conflict-decisions.js +2 -1
- package/lib/core/exporter.js +22 -1
- package/lib/core/journal.d.ts +54 -4
- package/lib/core/journal.js +158 -35
- package/lib/core/local-plugin-host.d.ts +29 -0
- package/lib/core/local-plugin-host.js +55 -48
- package/lib/core/messages.d.ts +7 -0
- package/lib/core/messages.js +14 -0
- package/lib/core/migration-history.js +16 -9
- package/lib/core/model-tools.d.ts +2 -2
- package/lib/core/model-tools.js +14 -2
- package/lib/core/patch-secret-guard.d.ts +49 -0
- package/lib/core/patch-secret-guard.js +164 -0
- package/lib/core/reconcile.d.ts +8 -0
- package/lib/core/reconcile.js +18 -3
- package/lib/core/reinstall.d.ts +2 -0
- package/lib/core/reinstall.js +6 -2
- package/lib/core/restore.js +31 -6
- package/lib/core/section-sanitize.d.ts +32 -0
- package/lib/core/section-sanitize.js +13 -0
- package/lib/core/snapshot-diff.d.ts +30 -0
- package/lib/core/snapshot-diff.js +56 -20
- package/lib/core/startup-barrier.js +8 -2
- package/lib/core/types.d.ts +22 -0
- package/lib/core/update-check.js +51 -2
- package/lib/foreign/kernel.js +9 -2
- package/lib/foreign/read-claude-plugins.js +29 -11
- package/lib/foreign/read-dsh.js +31 -2
- package/lib/foreign/read-trae.d.ts +11 -0
- package/lib/foreign/read-trae.js +124 -4
- package/lib/foreign/session-ir.js +50 -9
- package/lib/foreign/session-read.d.ts +8 -0
- package/lib/foreign/session-read.js +26 -0
- package/lib/foreign/session-source.d.ts +12 -4
- package/lib/foreign/session-source.js +5 -3
- package/lib/foreign/trae-sqlcipher.d.ts +127 -0
- package/lib/foreign/trae-sqlcipher.js +519 -0
- package/lib/foreign/trae.js +3 -1
- package/lib/foreign/truth-table.js +3 -2
- package/lib/foreign/types.d.ts +9 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/profiles/dsh-profile-io.d.ts +20 -0
- package/lib/profiles/dsh-profile-io.js +27 -0
- package/lib/profiles/dsh-profile-manager.d.ts +27 -0
- package/lib/profiles/dsh-profile-manager.js +45 -4
- package/lib/profiles/dsh-profile-runtime.d.ts +5 -1
- package/lib/profiles/dsh-profile-runtime.js +16 -3
- package/lib/profiles/process-control.d.ts +18 -1
- package/lib/profiles/process-control.js +25 -1
- package/lib/routes/profiles.js +2 -1
- package/lib/routes/sync.js +13 -1
- package/lib/schema/types.d.ts +11 -0
- package/lib/security/redaction.js +22 -6
- package/lib/sync/autosync-repeat.d.ts +27 -0
- package/lib/sync/autosync-repeat.js +23 -0
- package/lib/sync/autosync-scheduler.d.ts +34 -1
- package/lib/sync/autosync-scheduler.js +151 -43
- package/lib/sync/baseline-heal.js +7 -0
- package/lib/sync/baseline-record.d.ts +67 -0
- package/lib/sync/baseline-record.js +74 -0
- package/lib/sync/blob-store.js +13 -0
- package/lib/sync/content-diff.d.ts +39 -0
- package/lib/sync/content-diff.js +46 -0
- package/lib/sync/git/git-transport.d.ts +32 -1
- package/lib/sync/git/git-transport.js +146 -26
- package/lib/sync/github-auth.d.ts +17 -1
- package/lib/sync/github-auth.js +37 -1
- package/lib/sync/merge-apply-plan.d.ts +38 -0
- package/lib/sync/merge-apply-plan.js +76 -0
- package/lib/sync/messages.d.ts +1 -0
- package/lib/sync/messages.js +2 -0
- package/lib/sync/pending-merge.d.ts +34 -0
- package/lib/sync/pending-merge.js +53 -0
- package/lib/sync/s3/s3-transport.d.ts +14 -2
- package/lib/sync/s3/s3-transport.js +72 -48
- package/lib/sync/sync-engine.d.ts +23 -40
- package/lib/sync/sync-engine.js +115 -132
- package/lib/sync/sync-history.d.ts +7 -0
- package/lib/sync/sync-state.d.ts +28 -0
- package/lib/sync/sync-state.js +20 -0
- package/lib/sync/transport.d.ts +12 -3
- package/lib/sync/transport.js +13 -4
- package/lib/sync/webdav/webdav-transport.d.ts +19 -2
- package/lib/sync/webdav/webdav-transport.js +81 -40
- package/lib/ui/commands.js +5 -11
- package/lib/ui/dsh-profiles-view.d.ts +8 -3
- package/lib/ui/dsh-profiles-view.js +9 -5
- package/lib/ui/foreign-view.d.ts +19 -0
- package/lib/ui/foreign-view.js +24 -0
- package/lib/ui/i18n.d.ts +7 -1
- package/lib/ui/i18n.js +17 -2
- package/lib/ui/menu-placement.js +1 -1
- package/lib/ui/migration-consult-view.d.ts +11 -0
- package/lib/ui/migration-consult-view.js +15 -0
- package/lib/ui/overview-view.d.ts +7 -0
- package/lib/ui/overview-view.js +3 -0
- package/lib/ui/report.d.ts +9 -0
- package/lib/ui/report.js +17 -4
- package/lib/ui/selection-model.d.ts +8 -0
- package/lib/ui/selection-model.js +16 -0
- package/lib/utils/patch-yaml.d.ts +39 -1
- package/lib/utils/patch-yaml.js +82 -2
- package/lib/utils/paths.d.ts +13 -0
- package/lib/utils/paths.js +35 -0
- package/lib/utils/workdir-lock.js +18 -3
- package/package.json +2 -1
- package/src/adapters/mcp.test.ts +29 -0
- package/src/adapters/mcp.ts +43 -12
- package/src/adapters/messages.ts +2 -0
- package/src/adapters/patch-key-guard.ts +29 -0
- package/src/adapters/plugin-data.test.ts +126 -0
- package/src/adapters/plugin-data.ts +97 -26
- package/src/adapters/plugins.test.ts +288 -0
- package/src/adapters/plugins.ts +141 -4
- package/src/cli/actions.test.ts +74 -1
- package/src/cli/actions.ts +27 -246
- package/src/cli/control-state.ts +143 -0
- package/src/cli/index.ts +18 -0
- package/src/cli/web/web.test.ts +27 -0
- package/src/cli/write-gates.ts +149 -0
- package/src/client/about/AboutPanel.tsx +7 -6
- package/src/client/about/release-notes-view.test.ts +17 -0
- package/src/client/about/release-notes-view.ts +49 -24
- package/src/client/chat/ChatEntryView.tsx +2 -2
- package/src/client/chat/rollback-entry-view.ts +6 -1
- package/src/client/common/CommandPalette.tsx +6 -3
- package/src/client/common/ConfirmDialog.tsx +29 -12
- package/src/client/common/ContentPicker.tsx +13 -12
- package/src/client/common/Modal.tsx +12 -5
- package/src/client/common/MoreMenu.tsx +4 -1
- package/src/client/common/RunsCenter.tsx +103 -32
- package/src/client/common/Select.tsx +2 -2
- package/src/client/common/floating-bounds.ts +1 -1
- package/src/client/common/floating-units.test.ts +1 -2
- package/src/client/common/plan-text-redaction.test.ts +188 -3
- package/src/client/common/runs-center-guard.test.ts +60 -0
- package/src/client/common/runs-center-view.test.ts +128 -0
- package/src/client/common/runs-center-view.ts +96 -0
- package/src/client/common/section-title-action.test.ts +66 -0
- package/src/client/common/ui.tsx +93 -30
- package/src/client/config-manager.module.css +1507 -272
- package/src/client/consult/ConsultCard.tsx +7 -6
- package/src/client/environment/EnvironmentPanel.tsx +20 -21
- package/src/client/environment/maintenance/DiskUsageCard.tsx +65 -91
- package/src/client/environment/profiles-incomplete-ui.test.ts +8 -8
- package/src/client/export/ExportView.tsx +7 -16
- package/src/client/history/HistoryPanel.tsx +88 -28
- package/src/client/history/history-locales.ts +3 -3
- package/src/client/home/BackupScheduleCard.tsx +35 -30
- package/src/client/home/HomePanel.tsx +45 -10
- package/src/client/import/ConflictList.tsx +13 -13
- package/src/client/import/ForeignImportView.tsx +32 -13
- package/src/client/import/ImportWizardView.tsx +2 -3
- package/src/client/import/PathMappingForm.tsx +4 -4
- package/src/client/import/PathPickerDialog.tsx +2 -1
- package/src/client/import/foreign-import-view-guard.test.ts +1 -1
- package/src/client/import/import-wizard-steps.tsx +51 -39
- package/src/client/import/use-import-wizard-controller.test.ts +129 -0
- package/src/client/import/use-import-wizard-controller.ts +116 -22
- package/src/client/library/ArtifactRow.tsx +2 -2
- package/src/client/library/BackupInspectView.tsx +19 -15
- package/src/client/library/LibraryActions.tsx +56 -16
- package/src/client/library/LibraryPanel.tsx +7 -7
- package/src/client/library/RestorePlanView.tsx +13 -8
- package/src/client/locales/about.ts +0 -2
- package/src/client/locales/backup.ts +10 -12
- package/src/client/locales/common.ts +2 -0
- package/src/client/locales/export.ts +5 -12
- package/src/client/locales/foreign.ts +11 -2
- package/src/client/locales/import.ts +3 -2
- package/src/client/locales/overview.ts +4 -0
- package/src/client/locales/profiles.ts +32 -4
- package/src/client/locales/restore.ts +0 -2
- package/src/client/locales/sessions.ts +0 -2
- package/src/client/locales/shell.ts +2 -2
- package/src/client/market/MarketPanel.tsx +38 -20
- package/src/client/market/MyConfigsList.tsx +34 -32
- package/src/client/market/MyConfigsLoginCard.tsx +12 -15
- package/src/client/market/MyConfigsView.tsx +5 -5
- package/src/client/market/MyConfigsWizard.tsx +7 -12
- package/src/client/market/market-locales.ts +0 -6
- package/src/client/market/my-configs-split.test.ts +2 -1
- package/src/client/nav-menu-clip-guard.test.ts +52 -0
- package/src/client/recovery/IncompleteCopiesSection.tsx +1 -3
- package/src/client/recovery/RecoveryPanel.tsx +31 -16
- package/src/client/recovery/RollbackCard.tsx +58 -16
- package/src/client/recovery/RollbackFileChanges.tsx +40 -27
- package/src/client/recovery/SessionHealthCard.tsx +2 -2
- package/src/client/recovery/SessionHealthDialog.tsx +5 -7
- package/src/client/recovery/SessionLayoutSection.tsx +3 -5
- package/src/client/recovery/recovery-locales.ts +12 -8
- package/src/client/run-store.test.ts +30 -0
- package/src/client/run-store.ts +21 -1
- package/src/client/sync/AddChannelCard.tsx +65 -0
- package/src/client/sync/AutosyncCard.tsx +182 -108
- package/src/client/sync/ChannelClearConfirmDialog.tsx +5 -1
- package/src/client/sync/ChannelConfigDialog.tsx +27 -48
- package/src/client/sync/MergePreviewView.tsx +3 -5
- package/src/client/sync/PushReportSections.tsx +35 -0
- package/src/client/sync/SyncChannelEntryCard.tsx +33 -24
- package/src/client/sync/SyncConfirmView.tsx +3 -3
- package/src/client/sync/SyncLogList.tsx +16 -5
- package/src/client/sync/SyncPanel.tsx +153 -159
- package/src/client/sync/SyncProbeRow.tsx +5 -7
- package/src/client/sync/SyncRepositoryPicker.tsx +17 -4
- package/src/client/sync/SyncSecuritySection.tsx +157 -0
- package/src/client/sync/SyncSettingsDialog.tsx +304 -0
- package/src/client/sync/SyncSettingsView.test.ts +26 -8
- package/src/client/sync/WorkDirCard.tsx +2 -4
- package/src/client/sync/config-clear-guard.test.ts +94 -0
- package/src/client/sync/config-clear-guard.ts +98 -0
- package/src/client/sync/config-dirty.test.ts +91 -0
- package/src/client/sync/config-dirty.ts +88 -0
- package/src/client/sync/history-model.test.ts +2 -0
- package/src/client/sync/history-model.ts +3 -0
- package/src/client/sync/merge-request.ts +20 -0
- package/src/client/sync/sync-api.ts +12 -0
- package/src/client/sync/sync-locales.ts +87 -47
- package/src/client/sync/sync-panel-clear.test.ts +44 -0
- package/src/client/sync/sync-view.test.ts +20 -0
- package/src/client/sync/sync-view.ts +42 -4
- package/src/core/backup.test.ts +61 -1
- package/src/core/backup.ts +21 -4
- package/src/core/boot-rescue.test.ts +35 -0
- package/src/core/boot-rescue.ts +13 -2
- package/src/core/boot-safety.test.ts +25 -0
- package/src/core/boot-safety.ts +37 -2
- package/src/core/conflict-decisions.ts +2 -1
- package/src/core/exporter.ts +21 -1
- package/src/core/journal.test.ts +183 -0
- package/src/core/journal.ts +163 -25
- package/src/core/local-plugin-host.test.ts +68 -2
- package/src/core/local-plugin-host.ts +31 -4
- package/src/core/messages.ts +14 -0
- package/src/core/migration-history.test.ts +27 -0
- package/src/core/migration-history.ts +12 -4
- package/src/core/model-tools.test.ts +67 -0
- package/src/core/model-tools.ts +16 -4
- package/src/core/patch-secret-guard.test.ts +110 -0
- package/src/core/patch-secret-guard.ts +176 -0
- package/src/core/reconcile.ts +32 -4
- package/src/core/reinstall.ts +8 -2
- package/src/core/restore.ts +32 -7
- package/src/core/section-sanitize.ts +37 -0
- package/src/core/snapshot-diff.test.ts +73 -1
- package/src/core/snapshot-diff.ts +66 -21
- package/src/core/startup-barrier.test.ts +64 -0
- package/src/core/startup-barrier.ts +8 -2
- package/src/core/types.ts +27 -1
- package/src/core/update-check.test.ts +40 -0
- package/src/core/update-check.ts +53 -2
- package/src/foreign/claude-code.test.ts +5 -0
- package/src/foreign/claude-plugins.test.ts +57 -0
- package/src/foreign/claude-sessions-bytes.test.ts +7 -1
- package/src/foreign/dsh.test.ts +23 -0
- package/src/foreign/file-budget.test.ts +7 -1
- package/src/foreign/kernel.ts +9 -2
- package/src/foreign/read-claude-plugins.ts +32 -11
- package/src/foreign/read-dsh.ts +33 -2
- package/src/foreign/read-trae.ts +117 -5
- package/src/foreign/session-ir.test.ts +67 -0
- package/src/foreign/session-ir.ts +54 -14
- package/src/foreign/session-read.ts +23 -0
- package/src/foreign/session-source.ts +15 -5
- package/src/foreign/trae-sqlcipher.test.ts +360 -0
- package/src/foreign/trae-sqlcipher.ts +564 -0
- package/src/foreign/trae.test.ts +188 -0
- package/src/foreign/trae.ts +3 -1
- package/src/foreign/truth-table.ts +4 -2
- package/src/foreign/types.ts +10 -1
- package/src/index.ts +1 -1
- package/src/profiles/dsh-profile-copy-rollback.test.ts +58 -4
- package/src/profiles/dsh-profile-io.ts +29 -0
- package/src/profiles/dsh-profile-manager.test.ts +30 -0
- package/src/profiles/dsh-profile-manager.ts +53 -5
- package/src/profiles/dsh-profile-runtime.test.ts +29 -7
- package/src/profiles/dsh-profile-runtime.ts +17 -3
- package/src/profiles/process-control.test.ts +97 -0
- package/src/profiles/process-control.ts +28 -1
- package/src/routes/profiles.ts +2 -1
- package/src/routes/sync.ts +13 -1
- package/src/schema/types.ts +16 -1
- package/src/security/redaction.test.ts +31 -0
- package/src/security/redaction.ts +23 -6
- package/src/sync/autosync-repeat.test.ts +58 -0
- package/src/sync/autosync-repeat.ts +55 -0
- package/src/sync/autosync-scheduler.test.ts +417 -0
- package/src/sync/autosync-scheduler.ts +154 -41
- package/src/sync/baseline-heal.ts +6 -0
- package/src/sync/baseline-record.ts +135 -0
- package/src/sync/blob-store.test.ts +16 -0
- package/src/sync/blob-store.ts +13 -0
- package/src/sync/content-diff.test.ts +95 -0
- package/src/sync/content-diff.ts +79 -0
- package/src/sync/git/git-transport.test.ts +151 -4
- package/src/sync/git/git-transport.ts +143 -27
- package/src/sync/github-auth.test.ts +26 -1
- package/src/sync/github-auth.ts +37 -1
- package/src/sync/merge-apply-plan.ts +117 -0
- package/src/sync/messages.ts +2 -0
- package/src/sync/pending-merge.ts +77 -0
- package/src/sync/s3/s3-delete-and-blob-index.test.ts +249 -0
- package/src/sync/s3/s3-transport.ts +76 -42
- package/src/sync/sync-config.test.ts +39 -12
- package/src/sync/sync-device-identity.test.ts +3 -0
- package/src/sync/sync-engine.test.ts +465 -12
- package/src/sync/sync-engine.ts +130 -135
- package/src/sync/sync-history.ts +7 -0
- package/src/sync/sync-state.ts +48 -0
- package/src/sync/transport.test.ts +13 -1
- package/src/sync/transport.ts +12 -4
- package/src/sync/webdav/webdav-delete-and-blob-index.test.ts +249 -0
- package/src/sync/webdav/webdav-transport.ts +86 -34
- package/src/ui/commands.test.ts +11 -0
- package/src/ui/commands.ts +5 -14
- package/src/ui/dsh-profiles-view.test.ts +16 -0
- package/src/ui/dsh-profiles-view.ts +11 -5
- package/src/ui/foreign-view.test.ts +36 -0
- package/src/ui/foreign-view.ts +46 -0
- package/src/ui/i18n.ts +17 -2
- package/src/ui/menu-placement.ts +1 -1
- package/src/ui/migration-consult-view.test.ts +20 -1
- package/src/ui/migration-consult-view.ts +17 -0
- package/src/ui/overview-view.ts +10 -0
- package/src/ui/report.test.ts +25 -1
- package/src/ui/report.ts +15 -4
- package/src/ui/selection-model.test.ts +15 -1
- package/src/ui/selection-model.ts +18 -0
- package/src/utils/js-yaml4.d.ts +35 -0
- package/src/utils/patch-yaml.test.ts +207 -1
- package/src/utils/patch-yaml.ts +98 -2
- package/src/utils/paths.test.ts +14 -1
- package/src/utils/paths.ts +35 -0
- package/src/utils/workdir-lock.test.ts +69 -0
- package/src/utils/workdir-lock.ts +17 -3
- package/lib/ui/path-mapping.d.ts +0 -38
- package/lib/ui/path-mapping.js +0 -75
- package/src/client/common/InfoHint.tsx +0 -289
- package/src/client/common/info-hint-guard.test.ts +0 -1119
- package/src/client/sync/DecryptPasswordCard.tsx +0 -48
- package/src/client/sync/SecurityOptionsCard.tsx +0 -97
- package/src/client/sync/SyncSectionPickerDialog.tsx +0 -213
- package/src/ui/path-mapping.test.ts +0 -67
- package/src/ui/path-mapping.ts +0 -103
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* issue #140:同步内容的**唯一**判定点(拉取与推送共用)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:id 只是「哪一次推送」,与「内容是否相同」是两件事。
|
|
5
|
+
* - 拉取侧只比 id ⇒ 远端每次新 id(哪怕分区哈希逐字相同)都会被判成「有新生」,白跑
|
|
6
|
+
* download + 三方合并;合并后内容没变,下一轮又来一次。
|
|
7
|
+
* - 推送侧每次生成新 id ⇒ 内容没变也 upload、也建目录、也推进远端保留链。
|
|
8
|
+
*
|
|
9
|
+
* 判据(三条,缺一不可):
|
|
10
|
+
* 1. 只比**分区哈希集合**,不比 manifest(createdAt/deviceId/dshVersion 天然不同);
|
|
11
|
+
* 2. 键集也必须相等:远端有而对照侧没有的分区算「变了」(否则「多推了一个分区」会被漏判);
|
|
12
|
+
* 3. 任一观测缺失 ⇒ `unknown`,由调用方**回落**到 id 比较 —— 绝不把「读不到」猜成「没变」
|
|
13
|
+
* (加密快照的 sections 恒为空对象,就是这条的主要来源)。
|
|
14
|
+
*
|
|
15
|
+
* G-44(docs/known-issues/known-gaps.md):对照侧必须是「上次记录下来的**远端**观测」
|
|
16
|
+
* (sync-state.lastRemoteSections),或本次调用自己刚导出的本地载荷。
|
|
17
|
+
* 绝不允许拿 sync-state.sections(本机文件 hash,可能含本机未推送的改动)当远端观测 ——
|
|
18
|
+
* 一参两用会让「本机改过、远端没改」被判成「远端变过」,拉取侧每轮空转。
|
|
19
|
+
*/
|
|
20
|
+
import type { SectionData, SectionId } from '../schema/types.ts';
|
|
21
|
+
import type { SyncSnapshotMeta } from './transport.ts';
|
|
22
|
+
import { hashSection } from './sync-state.ts';
|
|
23
|
+
import type { SectionHashes } from './sync-state.ts';
|
|
24
|
+
|
|
25
|
+
/** 判定结论的三种取值(`unknown` 不是失败,是「没有可用观测」) */
|
|
26
|
+
export type ContentDiffReason = 'same-content' | 'content-changed' | 'unknown';
|
|
27
|
+
|
|
28
|
+
export interface ContentDiffResult {
|
|
29
|
+
/** `unknown` 恒为 false —— 调用方必须自己决定回落策略,绝不能把它当「变了」或「没变」 */
|
|
30
|
+
changed: boolean;
|
|
31
|
+
reason: ContentDiffReason;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** 分区导出载荷 → 分区哈希(推送上行 / 本地观测共用;与 sync-state 记的基线同口径) */
|
|
35
|
+
export function hashSections(sections: Partial<Record<SectionId, SectionData>>): SectionHashes {
|
|
36
|
+
const out: SectionHashes = {};
|
|
37
|
+
for (const [sid, data] of Object.entries(sections)) {
|
|
38
|
+
if (data === undefined) continue;
|
|
39
|
+
out[sid as SectionId] = hashSection(data);
|
|
40
|
+
}
|
|
41
|
+
return out;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 是否「有观测」(undefined / 空对象都算没有:空集合无法证伪任何内容) */
|
|
45
|
+
function hasObservation(hashes: SectionHashes | undefined | null): hashes is SectionHashes {
|
|
46
|
+
return hashes !== undefined && hashes !== null && Object.keys(hashes).length > 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 远端快照内容 vs 对照观测(`local` 优先,缺省用 `baseline`)。
|
|
51
|
+
*
|
|
52
|
+
* @param remote 远端快照元数据(transport.list() 的元素;`undefined` = 远端没有快照/列表读不到)
|
|
53
|
+
* @param baseline 上次记录的**远端**分区哈希(sync-state.lastRemoteSections)
|
|
54
|
+
* @param local 本次刚导出的本地分区哈希(推送路径用;拉取路径不传)
|
|
55
|
+
*/
|
|
56
|
+
export function diffSnapshotContent(
|
|
57
|
+
remote: SyncSnapshotMeta | undefined | null,
|
|
58
|
+
baseline: SectionHashes | undefined | null,
|
|
59
|
+
local?: SectionHashes | null,
|
|
60
|
+
): ContentDiffResult {
|
|
61
|
+
const reference = local ?? baseline;
|
|
62
|
+
if (remote === undefined || remote === null) return { changed: false, reason: 'unknown' };
|
|
63
|
+
const remoteHashes = remote.sections;
|
|
64
|
+
// 加密快照的 sections 恒为空对象(transport.ts:53-55)→ 无从比较,回落 id。
|
|
65
|
+
if (!hasObservation(remoteHashes)) return { changed: false, reason: 'unknown' };
|
|
66
|
+
if (!hasObservation(reference)) return { changed: false, reason: 'unknown' };
|
|
67
|
+
|
|
68
|
+
// 键集对称:任一侧缺键都算内容变了(新增/收窄分区的推送必须产生新快照)。
|
|
69
|
+
for (const sid of Object.keys(remoteHashes) as SectionId[]) {
|
|
70
|
+
const recorded = reference[sid];
|
|
71
|
+
if (recorded === undefined || recorded !== remoteHashes[sid]) {
|
|
72
|
+
return { changed: true, reason: 'content-changed' };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
for (const sid of Object.keys(reference) as SectionId[]) {
|
|
76
|
+
if (remoteHashes[sid] === undefined) return { changed: true, reason: 'content-changed' };
|
|
77
|
+
}
|
|
78
|
+
return { changed: false, reason: 'same-content' };
|
|
79
|
+
}
|
|
@@ -718,6 +718,153 @@ test('集成:同内容重复 upload → 第二次不产生新 commit(git 天
|
|
|
718
718
|
assert.equal(listed.length, 1, '远端仍只有 1 条快照(同 id 覆盖语义)');
|
|
719
719
|
});
|
|
720
720
|
|
|
721
|
+
/** 递归读目录树(相对路径 → 文本内容):用于断言 latest/ 与快照目录逐文件一致 */
|
|
722
|
+
async function readTree(root: string): Promise<Map<string, string>> {
|
|
723
|
+
const out = new Map<string, string>();
|
|
724
|
+
const walk = async (dir: string, prefix: string): Promise<void> => {
|
|
725
|
+
for (const name of (await fs.readdir(dir)).sort()) {
|
|
726
|
+
const full = path.join(dir, name);
|
|
727
|
+
const rel = prefix === '' ? name : prefix + '/' + name;
|
|
728
|
+
const stat = await fs.stat(full);
|
|
729
|
+
if (stat.isDirectory()) await walk(full, rel);
|
|
730
|
+
else out.set(rel, await fs.readFile(full, 'utf8'));
|
|
731
|
+
}
|
|
732
|
+
};
|
|
733
|
+
await walk(root, '');
|
|
734
|
+
return out;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
test('issue #141:明文 upload 在同一次 commit 内派生 latest/,与 snapshots/<id>/ 逐文件一致且 list 不计入', async (t) => {
|
|
738
|
+
const bare = await makeBareRepo(t);
|
|
739
|
+
const workDir = await makeTempDir(t);
|
|
740
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
741
|
+
await transport.upload(sampleSnapshot());
|
|
742
|
+
|
|
743
|
+
const snapTree = await readTree(path.join(workDir, 'snapshots', 'snap-001'));
|
|
744
|
+
const latestTree = await readTree(path.join(workDir, 'latest'));
|
|
745
|
+
assert.deepEqual([...latestTree.keys()].sort(), [...snapTree.keys()].sort(), 'latest/ 与快照目录的文件清单必须一致');
|
|
746
|
+
for (const [rel, content] of snapTree) {
|
|
747
|
+
assert.equal(latestTree.get(rel), content, 'latest/' + rel + ' 与快照内容不一致');
|
|
748
|
+
}
|
|
749
|
+
// manifest 保留真实快照 id(反向识别用),不是 'latest'
|
|
750
|
+
const manifest = JSON.parse(await fs.readFile(path.join(workDir, 'latest', 'manifest.json'), 'utf8')) as { id: string };
|
|
751
|
+
assert.equal(manifest.id, 'snap-001');
|
|
752
|
+
|
|
753
|
+
// 只写不读:list 仍只有 1 条(顶层多一个目录天然被忽略)
|
|
754
|
+
assert.deepEqual((await transport.list()).map((m) => m.id), ['snap-001']);
|
|
755
|
+
|
|
756
|
+
// 同一次 push:latest/ 与 snapshots/ 必须落在同一个提交里
|
|
757
|
+
const log = await runRealGit(['log', '--format=%s', '--all'], bare);
|
|
758
|
+
assert.equal(log.stdout.trim().split('\n').filter(Boolean).length, 1, 'latest/ 绝不允许单独占一个 commit');
|
|
759
|
+
const tree = await runRealGit(['ls-tree', '-r', '--name-only', 'HEAD'], bare);
|
|
760
|
+
assert.ok(tree.stdout.includes('latest/manifest.json'), '远端缺 latest/manifest.json: ' + tree.stdout);
|
|
761
|
+
assert.ok(tree.stdout.includes('snapshots/snap-001/manifest.json'));
|
|
762
|
+
});
|
|
763
|
+
|
|
764
|
+
test('issue #141:latest/ 建成后紧接着再 push 同一内容 → 视图不重建、不产生新 commit', async (t) => {
|
|
765
|
+
const bare = await makeBareRepo(t);
|
|
766
|
+
const workDir = await makeTempDir(t);
|
|
767
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
768
|
+
await transport.upload(sampleSnapshot());
|
|
769
|
+
const before = await readTree(path.join(workDir, 'latest'));
|
|
770
|
+
|
|
771
|
+
await transport.upload(sampleSnapshot());
|
|
772
|
+
const log = await runRealGit(['log', '--format=%s', '--all'], bare);
|
|
773
|
+
assert.equal(log.stdout.trim().split('\n').filter(Boolean).length, 1, '内容无变化 → 不得产生新 commit');
|
|
774
|
+
assert.deepEqual(await readTree(path.join(workDir, 'latest')), before, 'latest/ 应保持指向既有快照');
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
test('issue #141:加密快照不写 latest/(没有明文镜像,也不造密文副本)', async (t) => {
|
|
778
|
+
const bare = await makeBareRepo(t);
|
|
779
|
+
const workDir = await makeTempDir(t);
|
|
780
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
781
|
+
await transport.upload(await encryptedSnapshot());
|
|
782
|
+
|
|
783
|
+
const hasLatest = await fs.stat(path.join(workDir, 'latest')).then(() => true).catch(() => false);
|
|
784
|
+
assert.equal(hasLatest, false, '加密快照不得生成 latest/ 明文视图');
|
|
785
|
+
const tree = await runRealGit(['ls-tree', '-r', '--name-only', 'HEAD'], bare);
|
|
786
|
+
assert.ok(!tree.stdout.includes('latest/'), '远端不得出现 latest/: ' + tree.stdout);
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
test('issue #141:latest/ 永远指向最新那份 —— 后推一份更旧的快照不回退视图', async (t) => {
|
|
790
|
+
const bare = await makeBareRepo(t);
|
|
791
|
+
const workDir = await makeTempDir(t);
|
|
792
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
793
|
+
await transport.upload(sampleSnapshot({ id: 'snap-new', createdAt: '2026-08-16T14:00:00.000Z' }));
|
|
794
|
+
await transport.upload(sampleSnapshot({ id: 'snap-old', createdAt: '2026-08-16T12:00:00.000Z' }));
|
|
795
|
+
|
|
796
|
+
const manifest = JSON.parse(await fs.readFile(path.join(workDir, 'latest', 'manifest.json'), 'utf8')) as { id: string };
|
|
797
|
+
assert.equal(manifest.id, 'snap-new', 'out-of-order 上传不得把 latest/ 回退成更旧的快照');
|
|
798
|
+
// 两份都还在(保留策略不受影响),latest/ 只镜像最新那份
|
|
799
|
+
assert.deepEqual((await transport.list()).map((m) => m.id).sort(), ['snap-new', 'snap-old']);
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
test('issue #141:用户手工删掉远端 latest/ 后 list/download 不报错,下一次 push 重建视图', async (t) => {
|
|
803
|
+
const bare = await makeBareRepo(t);
|
|
804
|
+
const workDir = await makeTempDir(t);
|
|
805
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
806
|
+
await transport.upload(sampleSnapshot());
|
|
807
|
+
|
|
808
|
+
// 另开一个工作副本模拟「有人在远端删了 latest/ 并推送」
|
|
809
|
+
const otherRoot = await makeTempDir(t);
|
|
810
|
+
const other = path.join(otherRoot, 'clone');
|
|
811
|
+
const cloned = await runRealGit(['clone', bare, other], otherRoot);
|
|
812
|
+
assert.equal(cloned.code, 0, 'clone 失败: ' + cloned.stderr);
|
|
813
|
+
await runRealGit(['config', 'user.name', 'other'], other);
|
|
814
|
+
await runRealGit(['config', 'user.email', 'other@local'], other);
|
|
815
|
+
const removed = await runRealGit(['rm', '-r', '-q', 'latest'], other);
|
|
816
|
+
assert.equal(removed.code, 0, 'git rm 失败: ' + removed.stderr);
|
|
817
|
+
await runRealGit(['commit', '-m', 'drop latest'], other);
|
|
818
|
+
const pushed = await runRealGit(['push', 'origin', 'HEAD'], other);
|
|
819
|
+
assert.equal(pushed.code, 0, 'push 失败: ' + pushed.stderr);
|
|
820
|
+
|
|
821
|
+
// 允许远端没有 latest/:读路径完全不受影响
|
|
822
|
+
assert.deepEqual((await transport.list()).map((m) => m.id), ['snap-001']);
|
|
823
|
+
assert.deepEqual(await transport.download('snap-001'), sampleSnapshot());
|
|
824
|
+
|
|
825
|
+
// 派生视图可重建:再推同样的内容会把 latest/ 补回来(且这确实是一次真实提交)
|
|
826
|
+
await transport.upload(sampleSnapshot());
|
|
827
|
+
const tree = await runRealGit(['ls-tree', '-r', '--name-only', 'HEAD'], bare);
|
|
828
|
+
assert.ok(tree.stdout.includes('latest/manifest.json'), 'latest/ 应可重建: ' + tree.stdout);
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
test('issue #141:存量工作副本的 sparse cone 不含 latest/ 时,首次 push 自动补齐 cone 后仍能推上去', async (t) => {
|
|
832
|
+
const bare = await makeBareRepo(t);
|
|
833
|
+
const workDir = await makeTempDir(t);
|
|
834
|
+
await runRealGit(['init'], workDir);
|
|
835
|
+
await runRealGit(['config', 'user.name', 'test'], workDir);
|
|
836
|
+
await runRealGit(['config', 'user.email', 'test@local'], workDir);
|
|
837
|
+
await runRealGit(['config', 'core.sparseCheckout', 'true'], workDir);
|
|
838
|
+
await fs.mkdir(path.join(workDir, '.git', 'info'), { recursive: true });
|
|
839
|
+
// 旧版本建的 cone:只有三个快照面
|
|
840
|
+
await fs.writeFile(
|
|
841
|
+
path.join(workDir, '.git', 'info', 'sparse-checkout'),
|
|
842
|
+
'/snapshots/\n/snapshots-encrypted/\n/blobs/\n',
|
|
843
|
+
);
|
|
844
|
+
|
|
845
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
846
|
+
await transport.upload(sampleSnapshot());
|
|
847
|
+
|
|
848
|
+
const cone = await fs.readFile(path.join(workDir, '.git', 'info', 'sparse-checkout'), 'utf8');
|
|
849
|
+
assert.ok(cone.includes('/latest/'), 'cone 必须补齐 latest/,否则 git add 会拒收: ' + cone);
|
|
850
|
+
const tree = await runRealGit(['ls-tree', '-r', '--name-only', 'HEAD'], bare);
|
|
851
|
+
assert.ok(tree.stdout.includes('latest/manifest.json'), 'cone 补齐后 latest/ 应推上去: ' + tree.stdout);
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
test('issue #141:删除被 latest/ 镜像的那份快照 → 视图随同一提交移除,不留悬空视图', async (t) => {
|
|
855
|
+
const bare = await makeBareRepo(t);
|
|
856
|
+
const workDir = await makeTempDir(t);
|
|
857
|
+
const transport = new GitTransport({ repoUrl: bare, workDir, credentials: { getToken: async () => TEST_TOKEN } });
|
|
858
|
+
await transport.upload(sampleSnapshot());
|
|
859
|
+
await transport.delete('snap-001');
|
|
860
|
+
|
|
861
|
+
const hasLatest = await fs.stat(path.join(workDir, 'latest')).then(() => true).catch(() => false);
|
|
862
|
+
assert.equal(hasLatest, false, '被删快照的视图不得残留在工作副本');
|
|
863
|
+
const tree = await runRealGit(['ls-tree', '-r', '--name-only', 'HEAD'], bare);
|
|
864
|
+
assert.ok(!tree.stdout.includes('latest/'), '远端不得留下指向已删快照的 latest/: ' + tree.stdout);
|
|
865
|
+
assert.deepEqual(await transport.list(), []);
|
|
866
|
+
});
|
|
867
|
+
|
|
721
868
|
test('集成:token 不泄漏到快照文件内容与 commit message(真实 git 全链路)', async (t) => {
|
|
722
869
|
const bare = await makeBareRepo(t);
|
|
723
870
|
const workDir = await makeTempDir(t);
|
|
@@ -1118,7 +1265,7 @@ test('issue #122:网络子命令一律带低速上限,本地子命令不带'
|
|
|
1118
1265
|
for (const c of local) assert.ok(!c.args.join(' ').includes('lowSpeedLimit'), '本地命令不该带 http 配置: ' + c.args.join(' '));
|
|
1119
1266
|
});
|
|
1120
1267
|
|
|
1121
|
-
test('issue #122:首次 clone 走 partial clone + sparse-checkout,只 materialize
|
|
1268
|
+
test('issue #122:首次 clone 走 partial clone + sparse-checkout,只 materialize 四个同步目录(含 latest 派生视图)', async (t) => {
|
|
1122
1269
|
const dir = await makeTempDir(t); // 空目录 → 走 clone
|
|
1123
1270
|
const calls: CallRecord[] = [];
|
|
1124
1271
|
const transport = new GitTransport(makeOptions({ workDir: dir, exec: mockExec(calls) }));
|
|
@@ -1132,7 +1279,7 @@ test('issue #122:首次 clone 走 partial clone + sparse-checkout,只 materi
|
|
|
1132
1279
|
assert.ok(sparse !== undefined, '必须设置 sparse-checkout');
|
|
1133
1280
|
assert.equal(
|
|
1134
1281
|
stripProtocolGuards(sparse.args.join(' ')),
|
|
1135
|
-
'sparse-checkout set --cone snapshots snapshots-encrypted blobs',
|
|
1282
|
+
'sparse-checkout set --cone snapshots snapshots-encrypted blobs latest',
|
|
1136
1283
|
'sparse 面必须与同步目录同源: ' + sparse.args.join(' '),
|
|
1137
1284
|
);
|
|
1138
1285
|
assert.equal(calls.filter((c) => c.args.includes('checkout')).length, 1, 'sparse 之后必须 checkout 一次');
|
|
@@ -1956,10 +2103,10 @@ test('issue #131 集成:子目录前缀 —— 上传/列表/下载/删除都
|
|
|
1956
2103
|
assert.equal(root.code, 0, 'ls-tree 失败: ' + root.stderr);
|
|
1957
2104
|
assert.deepEqual(root.stdout.split('\n').filter(Boolean), [subdir], '仓库根必须只有子目录前缀');
|
|
1958
2105
|
|
|
1959
|
-
// 前缀下的同步目录(无外置 blob
|
|
2106
|
+
// 前缀下的同步目录(无外置 blob 时建 snapshots + 派生视图 latest;issue #141)
|
|
1960
2107
|
const inner = await runRealGit(['-c', 'core.quotePath=false', 'ls-tree', '--name-only', 'HEAD:' + subdir], bare);
|
|
1961
2108
|
assert.equal(inner.code, 0, 'ls-tree 子目录失败: ' + inner.stderr);
|
|
1962
|
-
assert.deepEqual(inner.stdout.split('\n').filter(Boolean), ['snapshots'], '子目录下必须是同步目录');
|
|
2109
|
+
assert.deepEqual(inner.stdout.split('\n').filter(Boolean), ['latest', 'snapshots'], '子目录下必须是同步目录');
|
|
1963
2110
|
|
|
1964
2111
|
// 工作副本同样落在前缀下
|
|
1965
2112
|
assert.ok(await fs.stat(path.join(workDir, subdir, 'snapshots', 'snap-001', 'manifest.json')));
|
|
@@ -189,6 +189,17 @@ const SNAPSHOTS_REL = 'snapshots';
|
|
|
189
189
|
/** 加密快照的「密文单文件」目录:整个快照 JSON(含密文载荷)以 <id>.json 提交。
|
|
190
190
|
* 加密快照不写散文件目录(密文无法平铺为明文 JSON 分区;远端已存密文)。 */
|
|
191
191
|
const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
|
|
192
|
+
/**
|
|
193
|
+
* 明文最新快照的**只读派生视图**(issue #141):与 `snapshots/<id>/` 逐文件同布局,供人和 CI 直接读,
|
|
194
|
+
* 不必先知道快照 id。(契约:docs/contracts/sync-channel-v1.md)
|
|
195
|
+
*
|
|
196
|
+
* 四条硬约束,改这里之前先读:
|
|
197
|
+
* - **只写不读**:list()/download()/合并/保留策略/冲突判定一律不把它当快照目录,也不依赖它的存在;
|
|
198
|
+
* - **必须与快照进同一个 commit**:push 之后再单独提交一次就多出一次远端写(正是报告者担心的并发窗口);
|
|
199
|
+
* - 加密快照不写(密文没有明文镜像)—— 由引擎显式告警,绝不静默;
|
|
200
|
+
* - 保留策略剪旧不动它:它永远指向最新那份明文快照。
|
|
201
|
+
*/
|
|
202
|
+
const LATEST_REL = 'latest';
|
|
192
203
|
/**
|
|
193
204
|
* 内容寻址 blob 仓在仓库内的相对目录(P1-4)。
|
|
194
205
|
*
|
|
@@ -198,14 +209,20 @@ const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
|
|
|
198
209
|
*/
|
|
199
210
|
const BLOBS_REL = 'blobs';
|
|
200
211
|
/**
|
|
201
|
-
* 同步仓库(git
|
|
212
|
+
* 同步仓库(git 通道)顶层**只可能出现**这四个条目。issue #104 的 `adopt` 模式拿它当安全判据:
|
|
202
213
|
* 远端分支的顶层出现别的名字 = 这不是本插件的同步仓库,合并它等于把别人的仓库内容搬进工作副本。
|
|
214
|
+
* `latest/` 只是承认「这是本插件写的派生视图」(issue #141),读取路径一律不碰它。
|
|
215
|
+
*
|
|
216
|
+
* 老版本客户端不知道 `latest/` ⇒ 只有在换仓库/远端历史重写时的 adopt 会 fail-closed 拒收
|
|
217
|
+
* (docs/known-issues/known-limits.md 有记录)。
|
|
203
218
|
*/
|
|
204
|
-
const ADOPT_ROOT_ALLOW = new Set<string>([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL]);
|
|
219
|
+
const ADOPT_ROOT_ALLOW = new Set<string>([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL, LATEST_REL]);
|
|
205
220
|
/**
|
|
206
|
-
*
|
|
221
|
+
* 同步仓库只在这四个顶层目录里写内容 ⇒ partial clone 的 sparse-checkout 面与它同源(issue #122)。
|
|
207
222
|
* 远端仓库比同步内容大几个数量级时(报告者现场:1.8 GB 仓库 / 6 MB 内容),全量 clone 与全量 fetch
|
|
208
|
-
*
|
|
223
|
+
* 都要拉完整对象图,而工作副本真正需要的只有这四个目录。
|
|
224
|
+
* `latest/` 虽是派生物,但它是同一 commit 里的产物:落在 cone 之外 `git add` 会直接拒收入库
|
|
225
|
+
* ("outside of your sparse-checkout definition"),所以必须进 sparse 面。
|
|
209
226
|
*/
|
|
210
227
|
/* 具体的 sparse 面在 `syncDirs()` 里按子目录前缀派生(issue #131),基名与 ADOPT_ROOT_ALLOW 同源。 */
|
|
211
228
|
/**
|
|
@@ -407,6 +424,8 @@ export class GitTransport implements SyncTransport {
|
|
|
407
424
|
* 的唯一线索。列表接口把它如实回传,让状态自洽(绝不静默失败)。
|
|
408
425
|
*/
|
|
409
426
|
private readErrors: { file: string; reason: string }[] = [];
|
|
427
|
+
/** 存量工作副本的 sparse cone 是否已确认包含 latest/(issue #141;只补一次,不每次同步都重设) */
|
|
428
|
+
private sparseConeCoversLatest = false;
|
|
410
429
|
private readonly msg: MsgFunc;
|
|
411
430
|
/** repoUrl 是否为网络地址(本地目录仓库 == false):决定要不要额外禁 file 传输(SEC-12)。 */
|
|
412
431
|
private readonly networkRepo: boolean;
|
|
@@ -544,26 +563,22 @@ export class GitTransport implements SyncTransport {
|
|
|
544
563
|
if (await fsx.exists(dir)) await fsx.remove(dir); // 覆盖语义:先清旧目录,避免残留旧文件
|
|
545
564
|
// P1-4:会话等大分区改为内容寻址外置(写进共享 blobs/,快照只留引用)
|
|
546
565
|
await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
|
|
547
|
-
// git
|
|
548
|
-
//
|
|
549
|
-
|
|
550
|
-
const plain = snapshot.sections as Partial<Record<SectionId, SectionData>>;
|
|
551
|
-
const blobSections = new Set<string>(BLOB_SECTIONS);
|
|
552
|
-
for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
|
|
553
|
-
// 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
|
|
554
|
-
if (blobSections.has(sid)) continue;
|
|
555
|
-
const data = plain[sid as SectionId] as FilesSection | undefined;
|
|
556
|
-
if (data === undefined || data.files.length > 0) continue;
|
|
557
|
-
// joinFs 只接受两个参数,占位路径拼进 prefix 一起传
|
|
558
|
-
await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
|
|
559
|
-
}
|
|
566
|
+
// git 不跟踪空目录:为空文件类分区目录写占位文件,保证远端保留目录
|
|
567
|
+
// (否则空分区(如未安装 skills)上传后目录在远端丢失,B 机全新 clone 下载即失败)。
|
|
568
|
+
await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
|
|
560
569
|
// 同 id 旧密文单文件残留 → 一并清理(形态切换不残留)
|
|
561
570
|
await fsx.remove(this.encryptedSnapshotFile(snapshot.id));
|
|
562
571
|
rel = `${this.relDir(SNAPSHOTS_REL)}/${snapshot.id}`;
|
|
563
572
|
}
|
|
573
|
+
// issue #141:`latest/` 派生视图与快照**进同一个 commit**(绝不在 push 之后补交一次)。
|
|
574
|
+
// 加密快照不写(没有明文镜像,引擎侧显式告警);远端已有更新视图时保持不动(不变量:永远指最新)。
|
|
575
|
+
const latestRel = !encrypted && await this.writeLatestMirror(snapshot, fsx) ? this.relDir(LATEST_REL) : null;
|
|
564
576
|
// blob 仓与快照同一次提交:引用文件与它引用的字节绝不跨 commit 分离
|
|
565
577
|
const hasBlobs = await fsx.exists(this.blobsDir());
|
|
566
|
-
|
|
578
|
+
const addPaths = [rel];
|
|
579
|
+
if (hasBlobs) addPaths.push(this.relDir(BLOBS_REL));
|
|
580
|
+
if (latestRel !== null) addPaths.push(latestRel);
|
|
581
|
+
await this.runGit(['add', '--', ...addPaths]);
|
|
567
582
|
const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
|
|
568
583
|
if (diff.code !== 0) {
|
|
569
584
|
const verb = existed ? 'update' : 'add';
|
|
@@ -573,6 +588,70 @@ export class GitTransport implements SyncTransport {
|
|
|
573
588
|
return computeSnapshotMeta(snapshot);
|
|
574
589
|
}
|
|
575
590
|
|
|
591
|
+
/**
|
|
592
|
+
* 空文件类分区的 git 占位文件(git 不跟踪空目录):
|
|
593
|
+
* 占位文件不参与 manifest.sectionHashes(基于传入数据计算);读回时按名+内容过滤。
|
|
594
|
+
*/
|
|
595
|
+
private async writeEmptyFileSectionPlaceholders(dir: string, snapshot: SyncSnapshot, fsx: SnapshotFs): Promise<void> {
|
|
596
|
+
const plain = snapshot.sections as Partial<Record<SectionId, SectionData>>;
|
|
597
|
+
const blobSections = new Set<string>(BLOB_SECTIONS);
|
|
598
|
+
for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
|
|
599
|
+
// 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
|
|
600
|
+
if (blobSections.has(sid)) continue;
|
|
601
|
+
const data = plain[sid as SectionId] as FilesSection | undefined;
|
|
602
|
+
if (data === undefined || data.files.length > 0) continue;
|
|
603
|
+
// joinFs 只接受两个参数,占位路径拼进 prefix 一起传
|
|
604
|
+
await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* 写 `latest/` 派生视图(issue #141):与 `snapshots/<id>/` 逐文件同布局,manifest 里保留**真实快照 id**
|
|
610
|
+
* (反向识别用)。返回本轮是否真的重建 —— false = 远端已有更新的视图,保持不动。
|
|
611
|
+
*
|
|
612
|
+
* 为什么按整份快照重写而不是拷目录:布局只有 layout.ts 一处权威,重写不会漏掉新增/删除的分区与占位文件;
|
|
613
|
+
* 外置分区只写引用文件(字节在共享 blobs/ 里按内容寻址,不重复落盘、不重复上传)。
|
|
614
|
+
*/
|
|
615
|
+
private async writeLatestMirror(snapshot: SyncSnapshot, fsx: SnapshotFs): Promise<boolean> {
|
|
616
|
+
const dir = this.latestDir();
|
|
617
|
+
const existing = await this.readDirManifest(dir);
|
|
618
|
+
// 不变量:latest/ 永远指向最新那份 —— out-of-order 上传(例如补齐更旧的快照)绝不把它回退成旧的
|
|
619
|
+
if (existing !== null && existing.createdAt > snapshot.createdAt) return false;
|
|
620
|
+
// 存量工作副本的 sparse 面是旧版本建的(不含 latest/)⇒ 必须在写文件**之前**补齐 cone,否则 add 拒收
|
|
621
|
+
await this.ensureSparseConeCoversLatest();
|
|
622
|
+
await fsx.remove(dir); // 覆盖语义:先清旧视图,避免残留已下线的分区文件
|
|
623
|
+
await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
|
|
624
|
+
await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* 存量工作副本的 sparse-checkout 面是**旧版本**建的(不含 `latest/`):cone 之外的路径 `git add` 会
|
|
630
|
+
* 拒绝入索引("outside of your sparse-checkout definition")⇒ 派生视图永远推不上去。
|
|
631
|
+
* 首次写 latest/ 前把 cone 补齐一次(克隆时 syncDirs() 已含它,新副本不会走到这里)。
|
|
632
|
+
*
|
|
633
|
+
* 非 sparse 副本(老 git 回落全量 clone)无需处理;读不出配置一律按「无需处理」走,绝不因优化失败。
|
|
634
|
+
*/
|
|
635
|
+
private async ensureSparseConeCoversLatest(): Promise<void> {
|
|
636
|
+
if (this.sparseConeCoversLatest) return;
|
|
637
|
+
const flag = await this.runGit(['config', '--get', 'core.sparseCheckout'], { allowNonZero: true });
|
|
638
|
+
if (flag.code !== 0 || flag.stdout.trim() !== 'true') {
|
|
639
|
+
this.sparseConeCoversLatest = true;
|
|
640
|
+
return;
|
|
641
|
+
}
|
|
642
|
+
const latestLine = `/${this.relDir(LATEST_REL)}/`;
|
|
643
|
+
const gitPath = await this.runGit(['rev-parse', '--git-path', 'info/sparse-checkout'], { allowNonZero: true });
|
|
644
|
+
const relPath = gitPath.code === 0 ? gitPath.stdout.trim() : '';
|
|
645
|
+
const abs = relPath === '' ? '' : path.isAbsolute(relPath) ? relPath : path.join(this.o.workDir, relPath);
|
|
646
|
+
const content = abs === '' ? '' : await fs.readFile(abs, 'utf8').catch(() => '');
|
|
647
|
+
if (content.split(/\r?\n/).some((line) => line.trim() === latestLine)) {
|
|
648
|
+
this.sparseConeCoversLatest = true;
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
const res = await this.runGit(['sparse-checkout', 'set', '--cone', ...this.syncDirs()], { allowNonZero: true });
|
|
652
|
+
this.sparseConeCoversLatest = res.code === 0;
|
|
653
|
+
}
|
|
654
|
+
|
|
576
655
|
/** 下载快照完整载荷。明文 → 读回散文件目录;加密 → 读回密文单文件。不存在的 id 必须抛错(契约)。 */
|
|
577
656
|
async download(id: string): Promise<SyncSnapshot> {
|
|
578
657
|
return this.withWorkdir(() => this.downloadLocked(id));
|
|
@@ -633,7 +712,12 @@ export class GitTransport implements SyncTransport {
|
|
|
633
712
|
await fsx.remove(encFile);
|
|
634
713
|
rel = `${this.relDir(SNAPSHOTS_ENCRYPTED_REL)}/${id}.json`;
|
|
635
714
|
}
|
|
636
|
-
|
|
715
|
+
// 不变量:latest/ 绝不指向不存在的快照 —— 删掉的正是它镜像的那份时,同一 commit 里一并移除视图
|
|
716
|
+
// (保留策略永远保住刚 push 的那份,正常裁剪走不到这里;这是用户显式删最新快照的兜底)。
|
|
717
|
+
const mirror = await this.readDirManifest(this.latestDir());
|
|
718
|
+
const dropLatest = mirror !== null && mirror.id === id;
|
|
719
|
+
if (dropLatest) await fsx.remove(this.latestDir());
|
|
720
|
+
await this.runGit(dropLatest ? ['add', '-A', '--', rel, this.relDir(LATEST_REL)] : ['add', '-A', '--', rel]);
|
|
637
721
|
const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
|
|
638
722
|
if (diff.code !== 0) {
|
|
639
723
|
await this.runGit(['commit', '-m', `sync: delete snapshot ${id}`]);
|
|
@@ -1195,14 +1279,20 @@ export class GitTransport implements SyncTransport {
|
|
|
1195
1279
|
{ kind: 'protocol', retryable: false },
|
|
1196
1280
|
);
|
|
1197
1281
|
}
|
|
1198
|
-
// ③ 合并(不改写任何本地提交);冲突则 abort
|
|
1282
|
+
// ③ 合并(不改写任何本地提交);冲突则 abort 回起点再报错。
|
|
1283
|
+
// latest/ 是派生视图(issue #141):两条历史各有一份内容不同的 latest/ 时是 add/add 冲突,
|
|
1284
|
+
// 但它不该挡住换仓库 —— 派生层以远端为准继续合并,其它任何路径冲突仍整笔 abort。
|
|
1199
1285
|
const mergeRes = await this.runGit(['merge', '--allow-unrelated-histories', '--no-edit', `origin/${branch}`], { allowNonZero: true });
|
|
1200
1286
|
if (mergeRes.code !== 0) {
|
|
1201
|
-
await this.
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1287
|
+
const resolved = await this.resolveLatestOnlyMergeConflict();
|
|
1288
|
+
const committed = resolved ? await this.runGit(['commit', '--no-edit'], { allowNonZero: true }) : null;
|
|
1289
|
+
if (committed === null || committed.code !== 0) {
|
|
1290
|
+
await this.runGit(['merge', '--abort'], { allowNonZero: true });
|
|
1291
|
+
throw new GitTransportError(
|
|
1292
|
+
this.msg('sync.git.adoptMergeConflict', { branch }),
|
|
1293
|
+
{ kind: 'conflict', retryable: false },
|
|
1294
|
+
);
|
|
1295
|
+
}
|
|
1206
1296
|
}
|
|
1207
1297
|
// 让后续 pull --ff-only 有 upstream 可用(忽略失败:分支可能已被删/重命名)
|
|
1208
1298
|
await this.runGit(['branch', '--set-upstream-to', `origin/${branch}`, branch], { allowNonZero: true });
|
|
@@ -1210,6 +1300,27 @@ export class GitTransport implements SyncTransport {
|
|
|
1210
1300
|
this.repoReady = true;
|
|
1211
1301
|
}
|
|
1212
1302
|
|
|
1303
|
+
/**
|
|
1304
|
+
* adopt 合并冲突里只放行 latest/(issue #141):派生视图两侧内容不同不算真冲突 ——
|
|
1305
|
+
* 以远端的版本收敛(合并方向 origin/<branch> 即 theirs),其它路径一律不放行。
|
|
1306
|
+
* 返回 false 表示冲突面不止 latest/,调用方必须 abort。
|
|
1307
|
+
*/
|
|
1308
|
+
private async resolveLatestOnlyMergeConflict(): Promise<boolean> {
|
|
1309
|
+
const unmerged = await this.runGit(['diff', '--name-only', '--diff-filter=U'], { allowNonZero: true });
|
|
1310
|
+
if (unmerged.code !== 0) return false;
|
|
1311
|
+
const paths = unmerged.stdout.split('\n').map((line) => line.trim()).filter((line) => line !== '');
|
|
1312
|
+
const latestRel = this.relDir(LATEST_REL);
|
|
1313
|
+
const inLatest = (rel: string): boolean => rel === latestRel || rel.startsWith(`${latestRel}/`);
|
|
1314
|
+
if (paths.length === 0 || !paths.every(inLatest)) return false;
|
|
1315
|
+
for (const rel of paths) {
|
|
1316
|
+
const takeTheirs = await this.runGit(['checkout', '--theirs', '--', rel], { allowNonZero: true });
|
|
1317
|
+
// modify/delete 冲突:theirs 里没有这个文件 = 远端已不再有视图,按删除收敛
|
|
1318
|
+
if (takeTheirs.code !== 0) await this.runGit(['rm', '-f', '--', rel], { allowNonZero: true });
|
|
1319
|
+
else await this.runGit(['add', '--', rel], { allowNonZero: true });
|
|
1320
|
+
}
|
|
1321
|
+
return true;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1213
1324
|
/**
|
|
1214
1325
|
* pull --ff-only 同步远端;无本地提交(全新仓库)或无 upstream 时静默跳过。
|
|
1215
1326
|
* opts.retryable=true(仅 list / download 等**幂等读**路径传):瞬时网络故障走有限指数退避重试。
|
|
@@ -1496,11 +1607,11 @@ export class GitTransport implements SyncTransport {
|
|
|
1496
1607
|
}
|
|
1497
1608
|
|
|
1498
1609
|
/**
|
|
1499
|
-
*
|
|
1610
|
+
* 四个同步根目录(带前缀,含只读派生视图 latest/):sparse 面、add 路径、adopt 判据都从这里取 ——
|
|
1500
1611
|
* 任何一处漏加前缀都会让「写入 A、读取 B」(issue #131 的失败面)。
|
|
1501
1612
|
*/
|
|
1502
1613
|
private syncDirs(): string[] {
|
|
1503
|
-
return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL)];
|
|
1614
|
+
return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL), this.relDir(LATEST_REL)];
|
|
1504
1615
|
}
|
|
1505
1616
|
|
|
1506
1617
|
private blobsDir(): string {
|
|
@@ -1593,6 +1704,11 @@ export class GitTransport implements SyncTransport {
|
|
|
1593
1704
|
return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_REL));
|
|
1594
1705
|
}
|
|
1595
1706
|
|
|
1707
|
+
/** 明文最新快照的只读派生视图目录(<workDir>/latest/;配了前缀则在 <subdir>/latest/) */
|
|
1708
|
+
private latestDir(): string {
|
|
1709
|
+
return joinFs(this.o.workDir, this.relDir(LATEST_REL));
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1596
1712
|
/** 加密快照密文单文件目录(<workDir>/snapshots-encrypted/;配了前缀则在 <subdir>/ 下) */
|
|
1597
1713
|
private encryptedSnapshotsDir(): string {
|
|
1598
1714
|
return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_ENCRYPTED_REL));
|
|
@@ -14,7 +14,7 @@ import assert from 'node:assert/strict';
|
|
|
14
14
|
|
|
15
15
|
import {
|
|
16
16
|
GITHUB_ACCESS_TOKEN_URL, GITHUB_DEVICE_CODE_URL, GITHUB_DEVICE_FLOW_SCOPE,
|
|
17
|
-
DeviceFlowStore, GitHubAuthClient, GitHubAuthError,
|
|
17
|
+
DEVICE_FLOW_LIMIT, DeviceFlowStore, GitHubAuthClient, GitHubAuthError,
|
|
18
18
|
} from './github-auth.ts';
|
|
19
19
|
import type { DeviceFlowEntry } from './github-auth.ts';
|
|
20
20
|
|
|
@@ -278,3 +278,28 @@ test('github-auth: DeviceFlowStore delete 移除条目;未过期条目可正
|
|
|
278
278
|
store.set('flow-2', entry({ expiresAt: 1000 }));
|
|
279
279
|
assert.equal(store.get('flow-2')?.clientId, 'client-123');
|
|
280
280
|
});
|
|
281
|
+
|
|
282
|
+
test('github-auth: DeviceFlowStore 只 set 不 get 的过期条目 → 下次 set 时被清扫(不永久驻留 device_code)', () => {
|
|
283
|
+
let now = 0;
|
|
284
|
+
const store = new DeviceFlowStore(() => now);
|
|
285
|
+
store.set('flow-1', entry({ expiresAt: 100 }));
|
|
286
|
+
// 用户开了授权页直接关掉:这条流程从此没人轮询 ⇒ 只有 set 路径能清理它
|
|
287
|
+
now = 500;
|
|
288
|
+
store.set('flow-2', entry({ expiresAt: 10_000 }));
|
|
289
|
+
assert.equal(store.size, 1, '过期条目必须在写入路径被淘汰,而不是永远留在表里');
|
|
290
|
+
assert.equal(store.get('flow-1'), undefined);
|
|
291
|
+
assert.equal(store.get('flow-2')?.deviceCode, 'device-abc-123');
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('github-auth: DeviceFlowStore 有效条目超上限 → 淘汰最早过期者并保持 size <= DEVICE_FLOW_LIMIT', () => {
|
|
295
|
+
const store = new DeviceFlowStore(() => 0);
|
|
296
|
+
for (let i = 0; i < DEVICE_FLOW_LIMIT; i += 1) {
|
|
297
|
+
store.set('flow-' + i, entry({ expiresAt: 1000 + i }));
|
|
298
|
+
}
|
|
299
|
+
assert.equal(store.size, DEVICE_FLOW_LIMIT);
|
|
300
|
+
store.set('flow-new', entry({ expiresAt: 999_999 }));
|
|
301
|
+
assert.equal(store.size, DEVICE_FLOW_LIMIT, '上限是硬上界:反复开流程不能无限涨');
|
|
302
|
+
assert.equal(store.get('flow-0'), undefined, '先被淘汰的是最早过期的那条(最接近失效)');
|
|
303
|
+
assert.equal(store.get('flow-1')?.deviceCode, 'device-abc-123', '仍在有效期内的其他流程不受影响');
|
|
304
|
+
assert.equal(store.get('flow-new')?.deviceCode, 'device-abc-123');
|
|
305
|
+
});
|
package/src/sync/github-auth.ts
CHANGED
|
@@ -268,10 +268,22 @@ export interface DeviceFlowEntry {
|
|
|
268
268
|
expiresAt: number;
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
+
/**
|
|
272
|
+
* 同一时刻允许驻留的设备码流上限。正常用户一次只开一个授权流程,这个数只在「反复重开」
|
|
273
|
+
* 或有人刷 start 端点时才会被顶到;有它才有内存上界(过期时间之外的第二重保证)。
|
|
274
|
+
*/
|
|
275
|
+
export const DEVICE_FLOW_LIMIT = 32;
|
|
276
|
+
|
|
271
277
|
/**
|
|
272
278
|
* 宿主内存设备码登记表(进程生命周期,跨请求共享 —— 与 RunRegistry 同模式)。
|
|
273
279
|
* device_code 的持有者是宿主:UI 只拿 flowId(随机 id),轮询时宿主凭 flowId 取回
|
|
274
|
-
* device_code 去 GitHub 换 token
|
|
280
|
+
* device_code 去 GitHub 换 token。
|
|
281
|
+
*
|
|
282
|
+
* audit-sync sync-P6-6(P3):清理不能只挂在 get() 上 —— 用户开了授权页又直接关掉时,流程
|
|
283
|
+
* 从此不会有人轮询,那条含 device_code 的过期条目会永久驻留。所以**写入路径也清扫**:
|
|
284
|
+
* set() 先淘汰全部过期条目(now > expiresAt),再把总数压到 DEVICE_FLOW_LIMIT 以内
|
|
285
|
+
* (超出时淘汰最早过期的那条 = 最接近失效、最没价值的一条)。get() 的惰性清理保留,
|
|
286
|
+
* 任何时刻表的大小都不超过该上限。
|
|
275
287
|
*/
|
|
276
288
|
export class DeviceFlowStore {
|
|
277
289
|
private readonly flows = new Map<string, DeviceFlowEntry>();
|
|
@@ -287,9 +299,33 @@ export class DeviceFlowStore {
|
|
|
287
299
|
|
|
288
300
|
/** 登记一个设备码流;flowId 由调用方生成(随机,不可猜) */
|
|
289
301
|
set(flowId: string, entry: DeviceFlowEntry): void {
|
|
302
|
+
this.sweep();
|
|
290
303
|
this.flows.set(flowId, entry);
|
|
291
304
|
}
|
|
292
305
|
|
|
306
|
+
/**
|
|
307
|
+
* 清扫:先删全部过期条目,再在容量满时淘汰最早过期者(保证 set 之后 size <= DEVICE_FLOW_LIMIT)。
|
|
308
|
+
* 先扫过期是关键 —— 否则上限会去淘汰仍在有效期内的流程,把用户的正常授权踢掉。
|
|
309
|
+
*/
|
|
310
|
+
private sweep(): void {
|
|
311
|
+
const now = this.now();
|
|
312
|
+
for (const [flowId, entry] of this.flows) {
|
|
313
|
+
if (now > entry.expiresAt) this.flows.delete(flowId);
|
|
314
|
+
}
|
|
315
|
+
while (this.flows.size >= DEVICE_FLOW_LIMIT) {
|
|
316
|
+
let oldestId: string | undefined;
|
|
317
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
318
|
+
for (const [flowId, entry] of this.flows) {
|
|
319
|
+
if (entry.expiresAt < oldestAt) {
|
|
320
|
+
oldestAt = entry.expiresAt;
|
|
321
|
+
oldestId = flowId;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (oldestId === undefined) return; // 理论不可达:size >= 1 时必能挑出一条
|
|
325
|
+
this.flows.delete(oldestId);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
293
329
|
/** 取回设备码流;过期条目视为不存在并立即清理 */
|
|
294
330
|
get(flowId: string): DeviceFlowEntry | undefined {
|
|
295
331
|
const entry = this.flows.get(flowId);
|