dsh-config-manager 0.8.2 → 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 +104 -49
- package/lib/client.js +12536 -12006
- 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/session-ir.js +50 -9
- package/lib/foreign/session-read.d.ts +8 -0
- package/lib/foreign/session-read.js +26 -0
- 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/i18n.d.ts +2 -1
- package/lib/ui/i18n.js +4 -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 +7 -6
- 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/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 +2 -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/kernel.ts +9 -2
- package/src/foreign/read-claude-plugins.ts +32 -11
- package/src/foreign/read-dsh.ts +33 -2
- 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/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/i18n.ts +4 -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,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #136 B:合并落地的「两趟计划 + 待裁决面」,以及执行前的兜底快照。
|
|
3
|
+
*
|
|
4
|
+
* **旧实现的坑**:`strategy: 'replace'` + 空 `resolutions` 规划。冲突只在外部分流到
|
|
5
|
+
* 分区级,而被放行分区里的叶级差异项在计划里是 `Conflict`;`replace` 把它们**不分来源**
|
|
6
|
+
* 统一翻成 `Update` → 落盘,等于「未裁决也按远端覆盖」,且报告 review 恒空 ——
|
|
7
|
+
* 用户既没同意,事后也无从知道。
|
|
8
|
+
*
|
|
9
|
+
* **现口径**:
|
|
10
|
+
* - **分区级裁决 → 逐项 resolutions**:本次要应用的分区(`appliedIds`:autoApply + 会话
|
|
11
|
+
* 伴随的 workspaces)其叶级差异已由分区裁决(useRemote/useMerged/keepLocal)决定,于是
|
|
12
|
+
* 显式给这些项 `useImported`(合并路径的 ZIP 载荷本身就是裁决结果 —— merge 分区的
|
|
13
|
+
* merged 已含双方改动,keepLocal 分区的 merged 就是本机值)。
|
|
14
|
+
* - **没有分区裁决覆盖的项保持 `Conflict`**:不写盘(`planItemWritesTarget()`,
|
|
15
|
+
* `src/core/backup.ts` 拦下),并列进报告的待裁决面 —— 绝不由全局策略暗中落盘。
|
|
16
|
+
* - **两趟计划的理由**:resolutions 要按 **item id** 给,而 id 只有计划才知道(各适配器
|
|
17
|
+
* 自己算)。第一趟用 `merge` + 空 resolutions 只做「探测」,零写入;只有探测出
|
|
18
|
+
* 「可裁决的 Conflict」时才补一遍 resolutions 重算 —— 无差异的常规合并仍然只有一趟。
|
|
19
|
+
*/
|
|
20
|
+
import { createSnapshot, FileSnapshotStore } from '../core/backup.ts';
|
|
21
|
+
import type {
|
|
22
|
+
ConfigAdapter,
|
|
23
|
+
HostContext,
|
|
24
|
+
ImportDecisions,
|
|
25
|
+
ImportPlan,
|
|
26
|
+
PathMapping,
|
|
27
|
+
Snapshot,
|
|
28
|
+
SnapshotStore,
|
|
29
|
+
} from '../core/types.ts';
|
|
30
|
+
import type { ReviewQueueItem } from './review-queue.ts';
|
|
31
|
+
|
|
32
|
+
export interface PlanMergeApplyInput {
|
|
33
|
+
/** 本次合成的临时 ZIP(合并结果)。 */
|
|
34
|
+
zipPath: string;
|
|
35
|
+
/** 合并路径的映射(merge() 记下的远端 sourceHome + 用户映射,issue #45)。 */
|
|
36
|
+
pathMappings: PathMapping[];
|
|
37
|
+
/** 本次要应用的分区(autoApply + 伴随分区)。 */
|
|
38
|
+
appliedIds: readonly string[];
|
|
39
|
+
/** 复用宿主 Importer 的规划入口(纯计算,零写入)。 */
|
|
40
|
+
createImportPlan: (zipPath: string, decisions: ImportDecisions) => Promise<ImportPlan>;
|
|
41
|
+
/** 待裁决项的告警文案(i18n 留在调用方,键 `sync.mergeHeldConflicts`)。 */
|
|
42
|
+
heldConflictMessage: (count: number, sections: string[]) => string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface MergeApplyPlan {
|
|
46
|
+
plan: ImportPlan;
|
|
47
|
+
/** 叶级未裁决项的报告面:只给 id/分区/描述,**不带值**(值可能含敏感叶子)。 */
|
|
48
|
+
heldReview: ReviewQueueItem[];
|
|
49
|
+
/** 人读告警(空数组 = 没有待裁决项)。 */
|
|
50
|
+
heldWarnings: string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function planMergeApply(input: PlanMergeApplyInput): Promise<MergeApplyPlan> {
|
|
54
|
+
const probePlan = await input.createImportPlan(input.zipPath, {
|
|
55
|
+
strategy: 'merge',
|
|
56
|
+
resolutions: {},
|
|
57
|
+
pathMappings: input.pathMappings,
|
|
58
|
+
});
|
|
59
|
+
const decidedSections = new Set<string>(input.appliedIds);
|
|
60
|
+
const decidable = probePlan.items.filter((i) => i.kind === 'Conflict' && decidedSections.has(i.adapter));
|
|
61
|
+
let plan = probePlan;
|
|
62
|
+
if (decidable.length > 0) {
|
|
63
|
+
const injected = new Set(decidable.map((i) => i.id));
|
|
64
|
+
const decidedPlan = await input.createImportPlan(input.zipPath, {
|
|
65
|
+
strategy: 'merge',
|
|
66
|
+
resolutions: Object.fromEntries([...injected].map((id) => [id, 'useImported' as const])),
|
|
67
|
+
pathMappings: input.pathMappings,
|
|
68
|
+
});
|
|
69
|
+
// #136 A:这些 `useImported` 是**分区级**裁决换来的,不是人对这一行的确认 ——
|
|
70
|
+
// 标成 `decidedBy: 'section'`,写回守卫据此只保留被剥空叶子(其余照常落地)。
|
|
71
|
+
// 漏标就会让自动同步(零 UI)把脱敏剥离的空值当用户主动清空写下去。
|
|
72
|
+
plan = {
|
|
73
|
+
...decidedPlan,
|
|
74
|
+
items: decidedPlan.items.map((i) => (i.conflict && injected.has(i.id)
|
|
75
|
+
? { ...i, conflict: { ...i.conflict, decidedBy: 'section' as const } }
|
|
76
|
+
: i)),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const held = plan.items.filter((i) => i.kind === 'Conflict');
|
|
80
|
+
const heldReview: ReviewQueueItem[] = held.map((i) => ({
|
|
81
|
+
id: i.id,
|
|
82
|
+
sectionId: i.adapter,
|
|
83
|
+
kind: 'key',
|
|
84
|
+
description: i.description,
|
|
85
|
+
}));
|
|
86
|
+
const heldWarnings = held.length === 0
|
|
87
|
+
? []
|
|
88
|
+
: [input.heldConflictMessage(held.length, [...new Set(held.map((i) => i.adapter))])];
|
|
89
|
+
return { plan, heldReview, heldWarnings };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export type MergeApplySnapshotResult =
|
|
93
|
+
| { ok: true; store: SnapshotStore; snapshot: Snapshot }
|
|
94
|
+
| { ok: false; error: unknown };
|
|
95
|
+
|
|
96
|
+
/** 执行前兜底快照(拿到 restoreId 给 UI 一键回滚);失败时把原始错误交回调用方决定文案。 */
|
|
97
|
+
export async function createMergeApplySnapshot(input: {
|
|
98
|
+
ctx: HostContext;
|
|
99
|
+
plan: ImportPlan;
|
|
100
|
+
zipPath: string;
|
|
101
|
+
storeDir: string;
|
|
102
|
+
adapters: ConfigAdapter[];
|
|
103
|
+
}): Promise<MergeApplySnapshotResult> {
|
|
104
|
+
const store: SnapshotStore = new FileSnapshotStore({ dir: input.storeDir });
|
|
105
|
+
try {
|
|
106
|
+
const snapshot = await createSnapshot({
|
|
107
|
+
ctx: input.ctx,
|
|
108
|
+
plan: input.plan,
|
|
109
|
+
sourceZip: input.zipPath,
|
|
110
|
+
store,
|
|
111
|
+
adapters: input.adapters,
|
|
112
|
+
});
|
|
113
|
+
return { ok: true, store, snapshot };
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return { ok: false, error };
|
|
116
|
+
}
|
|
117
|
+
}
|
package/src/sync/messages.ts
CHANGED
|
@@ -42,6 +42,7 @@ export const zh = {
|
|
|
42
42
|
'sync.layoutFileSectionDirMissing': '快照缺少文件分区目录 {prefix}({section})',
|
|
43
43
|
// ---------- 内容寻址 blob 仓 ----------
|
|
44
44
|
'sync.blobMissing': '内容的 blob 缺失({hash},{path}):远端仓不完整,拒绝降级为空分区',
|
|
45
|
+
'sync.blobCorrupt': '内容的 blob 校验不符({path}:期望 {hash},实得 {actual},{size} 字节):远端仓已损坏,拒绝写回错误内容',
|
|
45
46
|
'sync.webdav.blobHashInvalid': '内容哈希形状非法,拒绝写入 blob 仓: {hash}',
|
|
46
47
|
// ---------- 按档案分作用域(sync-scope) ----------
|
|
47
48
|
'sync.scopeDirCreateFailed': '无法创建档案同步目录 {dir}:{reason}',
|
|
@@ -127,6 +128,7 @@ export const en: Record<keyof typeof zh, string> = {
|
|
|
127
128
|
'sync.layoutBlobRefsNoSink': 'Section {section} of the snapshot uses content-addressed external form, but the current channel provides no blob store',
|
|
128
129
|
'sync.layoutFileSectionDirMissing': 'Snapshot is missing the file section directory {prefix} ({section})',
|
|
129
130
|
'sync.blobMissing': 'The blob for {path} is missing ({hash}): the remote store is incomplete, refusing to degrade to an empty section',
|
|
131
|
+
'sync.blobCorrupt': 'The blob for {path} failed its hash check (expected {hash}, got {actual}, {size} bytes): the remote store is corrupted, refusing to write back wrong content',
|
|
130
132
|
'sync.webdav.blobHashInvalid': 'Malformed content hash; refusing to write to the blob store: {hash}',
|
|
131
133
|
'sync.scopeDirCreateFailed': 'Cannot create the profile sync directory {dir}: {reason}',
|
|
132
134
|
'sync.scopeAdoptFailed': 'Failed to adopt {file}: {reason}',
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 待裁决(pendingMerge)的纯状态写入 —— 2026-11-14 真机事故 P2 的落点。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独一层:自动同步撞冲突时**刻意不写本地**(scheduler 跳过 Phase B),那条路径需要的
|
|
5
|
+
* 只是「基线纪律的纯状态写入」—— 不要 SyncApplyPlan、不构造临时 ZIP、不碰本机配置、不动祖先副本,
|
|
6
|
+
* 与「没有应用任何分区」时的既有口径逐字一致。从 sync-engine 拆出(体量棘轮,只许下调)。
|
|
7
|
+
*/
|
|
8
|
+
import { loadSyncState, saveSyncState } from './sync-state.ts';
|
|
9
|
+
import type { PendingMerge, SyncState } from './sync-state.ts';
|
|
10
|
+
import type { SnapshotFs } from './fs.ts';
|
|
11
|
+
import type { MsgFunc } from '../core/messages.ts';
|
|
12
|
+
import type { SectionId } from '../schema/types.ts';
|
|
13
|
+
|
|
14
|
+
/** 落盘依赖(与 SyncEngine 的 stateDir / fsx / msg 同源)。 */
|
|
15
|
+
export interface PendingMergeDeps {
|
|
16
|
+
stateDir: string
|
|
17
|
+
fsx: SnapshotFs
|
|
18
|
+
msg: MsgFunc
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 组装待裁决记录:同一远端快照上的连续待裁决**保留首次时间**(界面据此讲「挂了多久」)。 */
|
|
22
|
+
export function pendingMergeFor(
|
|
23
|
+
state: SyncState,
|
|
24
|
+
remoteSnapshotId: string,
|
|
25
|
+
sections: SectionId[],
|
|
26
|
+
nowIso: string,
|
|
27
|
+
): PendingMerge {
|
|
28
|
+
const since = state.pendingMerge !== undefined && state.pendingMerge.remoteSnapshotId === remoteSnapshotId
|
|
29
|
+
? state.pendingMerge.since
|
|
30
|
+
: nowIso;
|
|
31
|
+
return { remoteSnapshotId, sections, since };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 基线纪律的**纯状态**写入(不写祖先副本、不碰远端):仅在本机没有任何配置写入时使用。
|
|
36
|
+
*
|
|
37
|
+
* - pending 提供 → 写 pendingMerge;undefined → 清除该字段(「全部裁决完」);
|
|
38
|
+
* - advanceRemoteId 提供 → 把远端指针推进到该快照 id(缺省 = 保持不变)。
|
|
39
|
+
*
|
|
40
|
+
* 为什么不走 recordBaseline:那条路径会重建祖先副本并只带本次应用的分区 —— 在「没有应用任何分区」
|
|
41
|
+
* 或「仍有未裁决冲突」时都会把共同基线改坏(下一轮所有分区都退化成「无祖先 → 冲突」)。
|
|
42
|
+
*/
|
|
43
|
+
export async function writeMergeBaseline(
|
|
44
|
+
deps: PendingMergeDeps,
|
|
45
|
+
opts: { pending?: PendingMerge; advanceRemoteId?: string },
|
|
46
|
+
nowIso: string,
|
|
47
|
+
): Promise<void> {
|
|
48
|
+
const state = await loadSyncState(deps.stateDir, deps.fsx, deps.msg);
|
|
49
|
+
if (opts.pending === undefined) delete state.pendingMerge;
|
|
50
|
+
else state.pendingMerge = opts.pending;
|
|
51
|
+
if (opts.advanceRemoteId !== undefined) {
|
|
52
|
+
state.lastSnapshotId = opts.advanceRemoteId;
|
|
53
|
+
state.lastSyncAt = nowIso;
|
|
54
|
+
}
|
|
55
|
+
await saveSyncState(deps.stateDir, state, deps.fsx);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 把本轮被冲突挡下的分区落成「待裁决」(自动同步冲突分支调用)。
|
|
60
|
+
*
|
|
61
|
+
* - 远端快照 id 取自最近一次 merge();未知(null)或分区为空 → 返回 null 且**不写**:没有远端 id 的
|
|
62
|
+
* 待裁决无法被后续轮次认成「同一件事」,写进去只会让 since 每轮重置,界面永远说不清「挂了多久」。
|
|
63
|
+
* - since 由 pendingMergeFor 决定:同一远端快照上连续冲突保留首次时间。
|
|
64
|
+
* - 清理归推送成功侧(recordBaseline 带 pendingMerge:null):本机真推上去了 = 冲突已有结论。
|
|
65
|
+
*/
|
|
66
|
+
export async function recordPendingMerge(
|
|
67
|
+
deps: PendingMergeDeps,
|
|
68
|
+
remoteSnapshotId: string | null,
|
|
69
|
+
sections: readonly SectionId[],
|
|
70
|
+
nowIso: string,
|
|
71
|
+
): Promise<PendingMerge | null> {
|
|
72
|
+
if (remoteSnapshotId === null || sections.length === 0) return null;
|
|
73
|
+
const stateBefore = await loadSyncState(deps.stateDir, deps.fsx, deps.msg);
|
|
74
|
+
const pending = pendingMergeFor(stateBefore, remoteSnapshotId, [...sections], nowIso);
|
|
75
|
+
await writeMergeBaseline(deps, { pending }, nowIso);
|
|
76
|
+
return pending;
|
|
77
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* audit-sync sync-P6-1 / sync-P6-4(P3)的回归钉子(S3 兼容通道,与 WebDAV 同型同口径):
|
|
3
|
+
*
|
|
4
|
+
* P6-1:delete 必须先条件写索引、后删对象。旧顺序在索引 PUT 冲突时留下「索引指向已删对象」的条目,
|
|
5
|
+
* list 不校验存在性 ⇒ 别的设备 pull 到该 id 即 notfound 硬失败。最坏情况改为「孤儿对象」:
|
|
6
|
+
* 索引里不存在、list 看不到、pull 不会命中,重跑 delete 可补删。
|
|
7
|
+
*
|
|
8
|
+
* P6-4:blob 索引的「读失败」与「确实没有索引」必须分开(404 才是空);读失败一律中止,
|
|
9
|
+
* 绝不用一次整体 PUT 把已登记条目覆盖掉。
|
|
10
|
+
*/
|
|
11
|
+
import test from 'node:test'
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
|
|
14
|
+
import { S3Transport, S3TransportError } from './s3-transport.ts'
|
|
15
|
+
import type { S3RequestFn, S3Response, S3TransportOptions } from './s3-transport.ts'
|
|
16
|
+
import type { SyncSnapshot } from '../transport.ts'
|
|
17
|
+
import { s3Msg } from './messages.ts'
|
|
18
|
+
|
|
19
|
+
const ENDPOINT = 'https://minio.example.com:9000'
|
|
20
|
+
const BUCKET = 'demo-bucket'
|
|
21
|
+
const PREFIX = 'dsh-config-manager'
|
|
22
|
+
const ACCESS_KEY_ID = 'AKIAIOSFODNN7EXAMPLE'
|
|
23
|
+
const SECRET = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
|
|
24
|
+
const BASE_OBJECT = ENDPOINT + '/' + BUCKET + '/' + PREFIX
|
|
25
|
+
const BLOBS_PREFIX = BASE_OBJECT + '/blobs/'
|
|
26
|
+
const INDEX_URL = BASE_OBJECT + '/index.json'
|
|
27
|
+
const BLOBS_INDEX_URL = BASE_OBJECT + '/blobs-index.json'
|
|
28
|
+
const SNAPSHOT_URL = BASE_OBJECT + '/snap-001.json'
|
|
29
|
+
const OLD_HASH = 'a'.repeat(64)
|
|
30
|
+
const NEW_HASH = 'b'.repeat(64)
|
|
31
|
+
|
|
32
|
+
interface S3Call { method: string; url: string; headers: Record<string, string>; body?: string }
|
|
33
|
+
|
|
34
|
+
function res(status: number, bodyText = '', headers?: Record<string, string>): S3Response {
|
|
35
|
+
return {
|
|
36
|
+
status,
|
|
37
|
+
ok: status >= 200 && status < 300,
|
|
38
|
+
...(headers !== undefined ? { headers } : {}),
|
|
39
|
+
async text() { return bodyText; },
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function record(calls: S3Call[], handler: (m: S3Call) => S3Response): S3RequestFn {
|
|
44
|
+
return async (method, url, opts = {}) => {
|
|
45
|
+
const rec: S3Call = { method, url, headers: opts.headers ?? {}, ...(opts.body !== undefined ? { body: opts.body } : {}) };
|
|
46
|
+
calls.push(rec);
|
|
47
|
+
return handler(rec);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function makeOptions(request: S3RequestFn): S3TransportOptions {
|
|
52
|
+
return {
|
|
53
|
+
provider: 'minio',
|
|
54
|
+
endpoint: ENDPOINT,
|
|
55
|
+
region: 'us-east-1',
|
|
56
|
+
bucket: BUCKET,
|
|
57
|
+
accessKeyId: ACCESS_KEY_ID,
|
|
58
|
+
credentials: { getSecretAccessKey: async () => SECRET },
|
|
59
|
+
pathStyle: true,
|
|
60
|
+
request,
|
|
61
|
+
now: () => new Date('2026-10-18T10:00:00.000Z'),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const META = {
|
|
66
|
+
id: 'snap-001',
|
|
67
|
+
createdAt: '2026-10-18T09:00:00.000Z',
|
|
68
|
+
sections: {},
|
|
69
|
+
manifest: { schemaVersion: 1, dshVersion: '1', platform: 'win32', sectionIds: [], containsSecrets: false },
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function sessionsSnapshot(): SyncSnapshot {
|
|
73
|
+
return {
|
|
74
|
+
id: 'snap-001',
|
|
75
|
+
createdAt: '2026-10-18T10:00:00.000Z',
|
|
76
|
+
manifest: {
|
|
77
|
+
schemaVersion: 1,
|
|
78
|
+
dshVersion: '1.2.3',
|
|
79
|
+
platform: 'win32',
|
|
80
|
+
sectionIds: ['sessions'],
|
|
81
|
+
containsSecrets: false,
|
|
82
|
+
},
|
|
83
|
+
sections: {
|
|
84
|
+
sessions: {
|
|
85
|
+
version: 1,
|
|
86
|
+
files: [
|
|
87
|
+
{ relativePath: 's1.jsonl', data: new TextEncoder().encode('one'), contentHash: OLD_HASH },
|
|
88
|
+
{ relativePath: 's2.jsonl', data: new TextEncoder().encode('two'), contentHash: NEW_HASH },
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
} as unknown as SyncSnapshot;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const callsOf = (calls: S3Call[], method: string, url: string): S3Call[] =>
|
|
96
|
+
calls.filter((c) => c.method === method && c.url === url);
|
|
97
|
+
|
|
98
|
+
/* ---------------- P6-1:删除顺序 ---------------- */
|
|
99
|
+
|
|
100
|
+
test('P6-1(删除路径):索引写回 412 → 快照对象零 DELETE(绝不留「索引指向已删对象」)', async () => {
|
|
101
|
+
const calls: S3Call[] = [];
|
|
102
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
103
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, JSON.stringify([META]), { etag: '"v1-del"' });
|
|
104
|
+
if (m.method === 'PUT' && m.url === INDEX_URL) return res(412, 'precondition failed');
|
|
105
|
+
if (m.method === 'DELETE' && m.url === SNAPSHOT_URL) return res(204, '');
|
|
106
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
107
|
+
})));
|
|
108
|
+
|
|
109
|
+
await assert.rejects(
|
|
110
|
+
() => t.delete('snap-001'),
|
|
111
|
+
(err: unknown) => {
|
|
112
|
+
assert.ok(err instanceof S3TransportError);
|
|
113
|
+
assert.equal(err.kind, 'conflict');
|
|
114
|
+
assert.equal(err.message, s3Msg('sync.s3.concurrentWrite', { status: '412', url: INDEX_URL }));
|
|
115
|
+
return true;
|
|
116
|
+
},
|
|
117
|
+
);
|
|
118
|
+
assert.equal(callsOf(calls, 'DELETE', SNAPSHOT_URL).length, 0, '索引没写成功就绝不许删对象');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('P6-1(删除路径):成功路径顺序 = 先条件写索引(if-match),后 DELETE 快照对象', async () => {
|
|
122
|
+
const calls: S3Call[] = [];
|
|
123
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
124
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, JSON.stringify([META]), { etag: '"v1-del"' });
|
|
125
|
+
if (m.method === 'PUT' && m.url === INDEX_URL) return res(200, '');
|
|
126
|
+
if (m.method === 'DELETE' && m.url === SNAPSHOT_URL) return res(204, '');
|
|
127
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) return res(404, '');
|
|
128
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
129
|
+
})));
|
|
130
|
+
|
|
131
|
+
await t.delete('snap-001');
|
|
132
|
+
|
|
133
|
+
const puts = callsOf(calls, 'PUT', INDEX_URL);
|
|
134
|
+
assert.equal(puts.length, 1);
|
|
135
|
+
assert.equal(puts[0]!.headers['if-match'], '"v1-del"', '索引写回仍是条件写');
|
|
136
|
+
assert.equal(puts[0]!.body, '[]');
|
|
137
|
+
const putAt = calls.findIndex((c) => c.method === 'PUT' && c.url === INDEX_URL);
|
|
138
|
+
const delAt = calls.findIndex((c) => c.method === 'DELETE' && c.url === SNAPSHOT_URL);
|
|
139
|
+
assert.ok(putAt >= 0 && delAt >= 0);
|
|
140
|
+
assert.ok(putAt < delAt, '索引必须先落盘,再删对象(P6-1)');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('P6-1(删除路径):索引无该 id 且对象存在 → 只删对象,不写回索引', async () => {
|
|
144
|
+
const calls: S3Call[] = [];
|
|
145
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
146
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, '[]', { etag: '"v1-del"' });
|
|
147
|
+
if (m.method === 'DELETE' && m.url === SNAPSHOT_URL) return res(204, '');
|
|
148
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) return res(404, '');
|
|
149
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
150
|
+
})));
|
|
151
|
+
|
|
152
|
+
await t.delete('snap-001');
|
|
153
|
+
|
|
154
|
+
assert.equal(callsOf(calls, 'PUT', INDEX_URL).length, 0, '索引无变化就不写回(N1③ 口径)');
|
|
155
|
+
assert.equal(callsOf(calls, 'DELETE', SNAPSHOT_URL).length, 1, '孤儿对象仍按用户意图删掉');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/* ---------------- P6-4:blob 索引三态读 ---------------- */
|
|
159
|
+
|
|
160
|
+
test('P6-4:blob 索引 GET 500 → 上传可重试失败,快照与 blobs-index 零写入', async () => {
|
|
161
|
+
const calls: S3Call[] = [];
|
|
162
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
163
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, '[]', { etag: '"v1"' });
|
|
164
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) return res(503, 'backend down');
|
|
165
|
+
if (m.method === 'PUT' && m.url === SNAPSHOT_URL) return res(200, '');
|
|
166
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
167
|
+
})));
|
|
168
|
+
|
|
169
|
+
await assert.rejects(
|
|
170
|
+
() => t.upload(sessionsSnapshot()),
|
|
171
|
+
(err: unknown) => {
|
|
172
|
+
assert.ok(err instanceof S3TransportError);
|
|
173
|
+
assert.equal(err.kind, 'server');
|
|
174
|
+
assert.equal(err.retryable, true);
|
|
175
|
+
return true;
|
|
176
|
+
},
|
|
177
|
+
);
|
|
178
|
+
assert.equal(callsOf(calls, 'PUT', SNAPSHOT_URL).length, 0, '读不出来就零写入');
|
|
179
|
+
assert.equal(callsOf(calls, 'PUT', BLOBS_INDEX_URL).length, 0, '绝不整体覆盖');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('P6-4:blob 索引 200 + 坏 JSON → protocol 错误,绝不覆盖成只有本次条目', async () => {
|
|
183
|
+
const calls: S3Call[] = [];
|
|
184
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
185
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, '[]', { etag: '"v1"' });
|
|
186
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) return res(200, '{not-json');
|
|
187
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
188
|
+
})));
|
|
189
|
+
|
|
190
|
+
await assert.rejects(
|
|
191
|
+
() => t.upload(sessionsSnapshot()),
|
|
192
|
+
(err: unknown) => {
|
|
193
|
+
assert.ok(err instanceof S3TransportError);
|
|
194
|
+
assert.equal(err.kind, 'protocol');
|
|
195
|
+
assert.equal(err.retryable, false);
|
|
196
|
+
return true;
|
|
197
|
+
},
|
|
198
|
+
);
|
|
199
|
+
assert.equal(callsOf(calls, 'PUT', BLOBS_INDEX_URL).length, 0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('P6-4:写回前再读 blobs-index 失败 → 绝不 PUT(已登记条目不得被覆盖掉)', async () => {
|
|
203
|
+
const calls: S3Call[] = [];
|
|
204
|
+
let blobIdxReads = 0;
|
|
205
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
206
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, '[]', { etag: '"v1"' });
|
|
207
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) {
|
|
208
|
+
blobIdxReads += 1;
|
|
209
|
+
return blobIdxReads === 1 ? res(200, JSON.stringify({ [OLD_HASH]: 1 })) : res(500, 'flaky');
|
|
210
|
+
}
|
|
211
|
+
if (m.method === 'PUT' && m.url === SNAPSHOT_URL) return res(200, '');
|
|
212
|
+
if (m.method === 'PUT' && m.url === INDEX_URL) return res(200, '');
|
|
213
|
+
if (m.method === 'PUT' && m.url.startsWith(BLOBS_PREFIX)) return res(200, '');
|
|
214
|
+
if (m.method === 'PUT' && m.url === BLOBS_INDEX_URL) return res(200, '');
|
|
215
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
216
|
+
})));
|
|
217
|
+
|
|
218
|
+
await assert.rejects(
|
|
219
|
+
() => t.upload(sessionsSnapshot()),
|
|
220
|
+
(err: unknown) => {
|
|
221
|
+
assert.ok(err instanceof S3TransportError);
|
|
222
|
+
assert.equal(err.kind, 'server');
|
|
223
|
+
return true;
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
assert.equal(blobIdxReads, 2, '跳过判定一次 + 合并写回一次');
|
|
227
|
+
assert.equal(callsOf(calls, 'PUT', BLOBS_INDEX_URL).length, 0, '读失败时绝不整体 PUT(P6-4 的核心)');
|
|
228
|
+
assert.equal(callsOf(calls, 'PUT', SNAPSHOT_URL).length, 1, '快照已落盘:本次 blob 漏登记只占空间,不误删');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('P6-4:blob 索引 404(确实没有索引)→ 照常上传并新建索引', async () => {
|
|
232
|
+
const calls: S3Call[] = [];
|
|
233
|
+
const t = new S3Transport(makeOptions(record(calls, (m) => {
|
|
234
|
+
if (m.method === 'GET' && m.url === INDEX_URL) return res(200, '[]', { etag: '"v1"' });
|
|
235
|
+
if (m.method === 'GET' && m.url === BLOBS_INDEX_URL) return res(404, '');
|
|
236
|
+
if (m.method === 'PUT' && m.url === SNAPSHOT_URL) return res(200, '');
|
|
237
|
+
if (m.method === 'PUT' && m.url === INDEX_URL) return res(200, '');
|
|
238
|
+
if (m.method === 'PUT' && m.url === BLOBS_INDEX_URL) return res(200, '');
|
|
239
|
+
if (m.method === 'PUT' && m.url.startsWith(BLOBS_PREFIX)) return res(200, '');
|
|
240
|
+
return res(500, 'unexpected ' + m.method + ' ' + m.url);
|
|
241
|
+
})));
|
|
242
|
+
|
|
243
|
+
await t.upload(sessionsSnapshot());
|
|
244
|
+
|
|
245
|
+
const puts = callsOf(calls, 'PUT', BLOBS_INDEX_URL);
|
|
246
|
+
assert.equal(puts.length, 1, '缺资源 = 空是安全的,必须照常建索引');
|
|
247
|
+
const written = JSON.parse(puts[0]!.body ?? '{}') as Record<string, number>;
|
|
248
|
+
assert.deepEqual(Object.keys(written).sort(), [OLD_HASH, NEW_HASH].sort());
|
|
249
|
+
});
|
|
@@ -36,7 +36,7 @@ import {
|
|
|
36
36
|
import type { BlobRefsSection, BlobSink } from '../blob-store.ts';
|
|
37
37
|
import {
|
|
38
38
|
classifyHttpStatus, classifyNetworkErrorText, computeSnapshotMeta, DEFAULT_SYNC_TIMEOUT_MS,
|
|
39
|
-
isEncryptedSections, sectionsEqual, SyncTransportError, withSyncRetry,
|
|
39
|
+
isEncryptedSections, isPreconditionFailedStatus, sectionsEqual, SyncTransportError, withSyncRetry,
|
|
40
40
|
} from '../transport.ts';
|
|
41
41
|
import type {
|
|
42
42
|
SyncProbeResult, SyncRetryOptions, SyncSnapshot, SyncSnapshotMeta, SyncTransport, SyncTransportErrorOptions,
|
|
@@ -85,15 +85,6 @@ function conditionalHeaders(
|
|
|
85
85
|
return guarded ? { 'x-oss-forbid-overwrite': 'true' } : { 'if-none-match': '*' };
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
/**
|
|
89
|
-
* 条件写失败判定:412 恒是;409 仅在**确实带了条件写头**时才算
|
|
90
|
-
* (无条件的 409 另有含义,不吞;条件写头的存在由调用方显式告知)。
|
|
91
|
-
*/
|
|
92
|
-
function isConcurrentWrite(status: number, guarded: boolean): boolean {
|
|
93
|
-
if (status === 412) return true;
|
|
94
|
-
return status === 409 && guarded;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
88
|
/** 请求选项:headers / body / 覆盖默认超时(ms;0 = 不超时) */
|
|
98
89
|
export interface S3RequestOptions {
|
|
99
90
|
headers?: Record<string, string>;
|
|
@@ -372,27 +363,43 @@ export class S3Transport implements SyncTransport {
|
|
|
372
363
|
// audit-sync sync-N1(P0):与 WebDAV 同型 —— 写路径**不接受 404 = 空集合**。索引真缺失时
|
|
373
364
|
// 「远端一条快照都没有」是缺信息而非事实,沿用空索引会覆盖 index.json 并用空引用集回收 blob。
|
|
374
365
|
const { entries: idx, missing: indexMissing, etag: idxEtag } = await this.readIndexDetailed(secret);
|
|
375
|
-
const
|
|
376
|
-
if (
|
|
377
|
-
|
|
366
|
+
const key = snapshotKey(this.target.prefix, id);
|
|
367
|
+
if (indexMissing) {
|
|
368
|
+
// audit-sync sync-N1(P0):索引真缺失(404)= 缺信息而非「集合为空」→ **不写回索引、不触发 GC**,
|
|
369
|
+
// 其余快照与其引用的 blob 原样保留(与 WebDAV 同型)。目标快照文件按用户意图删除(404 视为成功)。
|
|
370
|
+
const del = await this.send('DELETE', key, { secret });
|
|
371
|
+
if (!del.res.ok && del.res.status !== 404) {
|
|
372
|
+
throw new S3TransportError(await this.failText('DELETE', del.url, del.res, secret), classifyHttpStatus(del.res.status));
|
|
373
|
+
}
|
|
374
|
+
return;
|
|
378
375
|
}
|
|
379
|
-
// audit-sync sync-N1(P0):索引真缺失(404)= 缺信息而非「集合为空」→ **不写回索引、不触发 GC**,
|
|
380
|
-
// 其余快照与其引用的 blob 原样保留(与 WebDAV 同型)。目标快照文件已按用户意图删除。
|
|
381
|
-
if (indexMissing) return;
|
|
382
376
|
const remaining = idx.filter((m) => m.id !== id);
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
377
|
+
const entryPresent = remaining.length !== idx.length;
|
|
378
|
+
// audit-sync sync-P6-1(P3):与 WebDAV 同型 —— **先写索引、后删对象**。
|
|
379
|
+
// 反序(旧实现先 DELETE 对象)的伤害:索引 PUT 408/412/409 冲突时本次删除已抛错中止,但对象没了
|
|
380
|
+
// ⇒ 索引留下指向不存在对象的条目;list() 不校验存在性,别的设备 pull 到该 id 即 notfound 硬失败。
|
|
381
|
+
// 调整顺序后的**最坏情况**:索引摘除成功而对象 DELETE 失败 ⇒ 远端留一个索引里不存在、list() 看不到、
|
|
382
|
+
// pull 不会命中的孤儿对象(只占空间);重跑 delete 会把对象补删(404 视为成功)。
|
|
383
|
+
if (entryPresent) {
|
|
384
|
+
const condHeaders = conditionalHeaders(idxEtag, false, this.target.conditionalWrite);
|
|
385
|
+
await this.reassertIndexEtag(idxEtag, secret);
|
|
386
|
+
const putIdx = await this.send('PUT', indexKey(this.target.prefix), {
|
|
387
|
+
body: JSON.stringify(remaining),
|
|
388
|
+
secret,
|
|
389
|
+
headers: condHeaders,
|
|
390
|
+
});
|
|
391
|
+
const idxFailure = await this.conditionalFailure('PUT', putIdx.url, putIdx.res, Object.keys(condHeaders).length > 0, secret);
|
|
392
|
+
if (idxFailure !== undefined) throw idxFailure;
|
|
393
|
+
if (!putIdx.res.ok) {
|
|
394
|
+
throw new S3TransportError(await this.failText('PUT', putIdx.url, putIdx.res, secret), classifyHttpStatus(putIdx.res.status));
|
|
395
|
+
}
|
|
395
396
|
}
|
|
397
|
+
// 索引已摘除(或本来就无该 id)后才动对象:404 = 不存在,视为成功
|
|
398
|
+
const del = await this.send('DELETE', key, { secret });
|
|
399
|
+
if (!del.res.ok && del.res.status !== 404) {
|
|
400
|
+
throw new S3TransportError(await this.failText('DELETE', del.url, del.res, secret), classifyHttpStatus(del.res.status));
|
|
401
|
+
}
|
|
402
|
+
if (!entryPresent && !del.res.ok) return; // 索引无该 id 且对象本就不存在 → 静默成功,无 GC 可做
|
|
396
403
|
await this.gcBlobStore(secret, remaining).catch(() => undefined);
|
|
397
404
|
}
|
|
398
405
|
|
|
@@ -435,7 +442,7 @@ export class S3Transport implements SyncTransport {
|
|
|
435
442
|
}
|
|
436
443
|
|
|
437
444
|
/**
|
|
438
|
-
* 条件写失败分类(issue #109 第二层复核):`
|
|
445
|
+
* 条件写失败分类(issue #109 第二层复核):`isPreconditionFailedStatus` 只看「状态码像不像」,
|
|
439
446
|
* 这里再问报文一句确认「对象确实已存在」。OSS 的 **409 不止 FileAlreadyExists** ——
|
|
440
447
|
* FileImmutable(Bucket 数据处于保护状态,删改都会被拒)同为 409,一律按并发写报
|
|
441
448
|
* 「别的设备改过索引」就是假话。报文读不出时仍按并发写处理(宁可让用户重跑一次,
|
|
@@ -448,7 +455,8 @@ export class S3Transport implements SyncTransport {
|
|
|
448
455
|
guarded: boolean,
|
|
449
456
|
secret: string,
|
|
450
457
|
): Promise<S3TransportError | undefined> {
|
|
451
|
-
|
|
458
|
+
// 判定与 WebDAV 通道共用 transport.ts 的唯一实现(412 恒算;409 仅在真的带了条件头时算)
|
|
459
|
+
if (!isPreconditionFailedStatus(res.status, { guarded })) return undefined;
|
|
452
460
|
if (res.status !== 409) return this.concurrentWrite(url, res.status);
|
|
453
461
|
// 只有 forbid-overwrite 方言的 409 有两种含义(FileAlreadyExists / FileImmutable);
|
|
454
462
|
// etag 方言的 409 由各家自定语义,报文不可当作判据 —— 沿用「带条件头的 409 = 并发写」。
|
|
@@ -671,7 +679,6 @@ export class S3Transport implements SyncTransport {
|
|
|
671
679
|
|
|
672
680
|
/* ---------------- P1-4:内容寻址 blob 仓 ---------------- */
|
|
673
681
|
|
|
674
|
-
/** 读取 blob 索引(哈希 → 写入时间 ms);缺失/损坏 → 空(GC 只会「少删」,不会误删) */
|
|
675
682
|
/**
|
|
676
683
|
* 远端是否已存在历史内容(t66)。判据 = **blob 仓索引对象存在**:它是本通道唯一的、
|
|
677
684
|
* 无需列举整个桶就能读到的「这里以前写过」证据(本实现未用 ListObjects)。
|
|
@@ -700,21 +707,48 @@ export class S3Transport implements SyncTransport {
|
|
|
700
707
|
}
|
|
701
708
|
}
|
|
702
709
|
|
|
710
|
+
/** 读取 blob 索引(哈希 → 写入时间 ms);只读失败即抛,绝不回落成空索引,见 readBlobIndexDetailed。 */
|
|
703
711
|
private async readBlobIndex(secret: string): Promise<Record<string, number>> {
|
|
712
|
+
return (await this.readBlobIndexDetailed(secret)).index;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/**
|
|
716
|
+
* audit-sync sync-P6-4(P3):与 WebDAV 同型 —— 「读失败」与「确实没有索引」必须分开。
|
|
717
|
+
*
|
|
718
|
+
* 旧实现把非 404 与坏 JSON 一律当成空索引,紧接着 appendBlobIndex 读→合并→整体 PUT 覆盖,
|
|
719
|
+
* 一次 5xx 抖动就把远端已登记条目全丢掉(GC 从此看不见这些 blob,等于远端漏账)。三态语义:
|
|
720
|
+
* - 404 → missing = true:桶里确实没有 blob 索引,按空处理是安全的(不会被覆盖成假集合);
|
|
721
|
+
* - 其它非 2xx(5xx / 403 / …)→ 抛错(可重试):读不出来就**不许**整体覆盖;
|
|
722
|
+
* - 200 但坏 JSON / 非对象 → 抛 protocol(不可重试):索引损坏是远端状态问题,覆盖写会抹掉残存记录。
|
|
723
|
+
*
|
|
724
|
+
* 最坏情况(可重试失败):本可跳过的 blob 被重传一遍,或本次 blob 漏登记(只占空间,不误删)。
|
|
725
|
+
*/
|
|
726
|
+
private async readBlobIndexDetailed(secret: string): Promise<{ index: Record<string, number>; missing: boolean }> {
|
|
704
727
|
const sent = await this.send('GET', blobIndexKey(this.target.prefix), { secret });
|
|
705
|
-
if (sent.res.status === 404) return {};
|
|
706
|
-
if (!sent.res.ok)
|
|
728
|
+
if (sent.res.status === 404) return { index: {}, missing: true };
|
|
729
|
+
if (!sent.res.ok) {
|
|
730
|
+
throw new S3TransportError(await this.failText('GET', sent.url, sent.res, secret), classifyHttpStatus(sent.res.status));
|
|
731
|
+
}
|
|
732
|
+
let parsed: unknown;
|
|
707
733
|
try {
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
734
|
+
parsed = parseJsonSafe(await sent.res.text());
|
|
735
|
+
} catch (err) {
|
|
736
|
+
throw new S3TransportError(
|
|
737
|
+
this.o.msg('sync.s3.indexInvalid', { url: sent.url, err: this.mask(String((err as Error)?.message ?? ''), secret) }),
|
|
738
|
+
{ kind: 'protocol', retryable: false },
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
742
|
+
throw new S3TransportError(
|
|
743
|
+
this.o.msg('sync.s3.indexInvalid', { url: sent.url, err: 'not an object' }),
|
|
744
|
+
{ kind: 'protocol', retryable: false },
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
const out: Record<string, number> = {};
|
|
748
|
+
for (const [hash, at] of Object.entries(parsed as Record<string, unknown>)) {
|
|
749
|
+
if (BLOB_HASH_RE.test(hash) && typeof at === 'number' && Number.isFinite(at)) out[hash] = at;
|
|
717
750
|
}
|
|
751
|
+
return { index: out, missing: false };
|
|
718
752
|
}
|
|
719
753
|
|
|
720
754
|
private async writeBlobIndex(secret: string, index: Record<string, number>): Promise<void> {
|