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
|
@@ -50,6 +50,7 @@ import { engineMsg } from './messages.js';
|
|
|
50
50
|
import { readAutosyncConfig, writeAutosyncConfig, clampRemoteCheckMs, DEFAULT_AUTOSYNC_REALTIME, DEFAULT_AUTOSYNC_REMOTE_CHECK_MS, DEFAULT_AUTOSYNC_AUTO_APPLY_TIER, DEFAULT_AUTOSYNC_CONFLICT_POLICY, } from './autosync-config.js';
|
|
51
51
|
import { readSyncConfigFor, isGitConfig, isWebDavConfig, isS3Config, isGistConfig, SYNC_CHANNELS } from './sync-config.js';
|
|
52
52
|
import { readSyncHistory, appendAutosyncEntry, markAutosyncNotified } from './sync-history.js';
|
|
53
|
+
import { repeatHistorySignature } from './autosync-repeat.js';
|
|
53
54
|
import { classifyMergePlan } from './risk.js';
|
|
54
55
|
import { SyncWatcher } from './sync-watcher.js';
|
|
55
56
|
import { buildWatchTargets } from './watch-scope.js';
|
|
@@ -143,6 +144,14 @@ export class AutoSyncScheduler {
|
|
|
143
144
|
reportedConfigIssues = new Set();
|
|
144
145
|
stopped = false;
|
|
145
146
|
running = false;
|
|
147
|
+
/**
|
|
148
|
+
* P1(2026-11-14 真机事故):上一轮真正落盘的历史签名,见 autosync-repeat.ts。
|
|
149
|
+
* 与本轮签名相同 → 这一轮不追加历史(同一结论连续播报把 200 条历史刷满、首页变成
|
|
150
|
+
* 每 1 分钟一条的噪音);但 lastRunAt / lastRunStatus / 跳过计数仍逐轮刷新。
|
|
151
|
+
*/
|
|
152
|
+
repeatSignature = null;
|
|
153
|
+
/** 本轮历史是否被压掉 —— writeFinalConfig 据此保留上一条真记录的 lastRunHistoryId。 */
|
|
154
|
+
historyDeduped = false;
|
|
146
155
|
constructor(opts) {
|
|
147
156
|
this.syncDir = opts.syncDir;
|
|
148
157
|
this.host = opts.host;
|
|
@@ -174,6 +183,16 @@ export class AutoSyncScheduler {
|
|
|
174
183
|
* 原样透传(readSyncHistory 逐条 cast),所以运行时可稳定往返;界面按「字段有无」渲染。
|
|
175
184
|
*/
|
|
176
185
|
async appendHistory(channel, entry) {
|
|
186
|
+
const signature = repeatHistorySignature(channel, entry);
|
|
187
|
+
if (signature !== null && signature === this.repeatSignature) {
|
|
188
|
+
// 同一状态的第 N 轮(典型:远端快照一直比基线新,60s 轮询每轮都撞同一份冲突)。
|
|
189
|
+
// 不写 sync-history、也不写首页统一历史;但这一轮仍要刷新「上次运行」,
|
|
190
|
+
// 否则界面会停在几十分钟前的 lastRunAt,用户以为自动同步停了。
|
|
191
|
+
this.historyDeduped = true;
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.repeatSignature = signature;
|
|
195
|
+
this.historyDeduped = false;
|
|
177
196
|
await this.appendHistoryFn({ ...entry, transport: channel });
|
|
178
197
|
}
|
|
179
198
|
/**
|
|
@@ -252,36 +271,47 @@ export class AutoSyncScheduler {
|
|
|
252
271
|
this.host.log.error(`自动同步触发失败(${channel}/${reason})`, { error: err instanceof Error ? err.message : String(err) });
|
|
253
272
|
});
|
|
254
273
|
}
|
|
255
|
-
/**
|
|
274
|
+
/**
|
|
275
|
+
* 兜底间隔定时器:每个 enabled 通道按各自 interval 排一次性定时器,触发后**只重排自己**。
|
|
276
|
+
*
|
|
277
|
+
* 早期实现是「清掉全部通道再统一重排」:webdav 5m 每跑一轮就把 git 30m 的定时器清掉重排,
|
|
278
|
+
* 30m 永远等不到 → 纯定时器模式下慢通道永久饥饿(审计 P5-1,虚拟时钟 180min 实测 git 只跑 1 次)。
|
|
279
|
+
*/
|
|
256
280
|
scheduleIntervalTimers() {
|
|
257
281
|
for (const timer of this.timers.values())
|
|
258
282
|
this.clearTimer(timer);
|
|
259
283
|
this.timers.clear();
|
|
260
284
|
// 通道清单唯一来源 = SYNC_CHANNELS(此前同一数组在本文件写了两遍:漏改一处即某通道永不排期)
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
285
|
+
for (const channel of SYNC_CHANNELS)
|
|
286
|
+
this.scheduleChannelTimer(channel);
|
|
287
|
+
}
|
|
288
|
+
/** 单个通道的一次性 interval 排期(触发后只重排这一个通道)。 */
|
|
289
|
+
scheduleChannelTimer(channel) {
|
|
290
|
+
const existing = this.timers.get(channel);
|
|
291
|
+
if (existing !== undefined)
|
|
292
|
+
this.clearTimer(existing);
|
|
293
|
+
this.timers.delete(channel);
|
|
294
|
+
void this.readConfig(channel).then((cfg) => {
|
|
295
|
+
if (this.stopped || !cfg.enabled)
|
|
296
|
+
return;
|
|
297
|
+
const ms = intervalToMs(cfg.interval);
|
|
298
|
+
const timer = this.setTimer(() => {
|
|
299
|
+
// 一次性定时器:触发后立即从表移除,避免 reload() 清理到已失效的句柄
|
|
300
|
+
this.timers.delete(channel);
|
|
301
|
+
if (this.stopped)
|
|
265
302
|
return;
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
.
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (!this.stopped)
|
|
279
|
-
this.scheduleIntervalTimers();
|
|
280
|
-
});
|
|
281
|
-
}, ms);
|
|
282
|
-
this.timers.set(channel, timer);
|
|
283
|
-
}).catch(() => { });
|
|
284
|
-
}
|
|
303
|
+
void this.runOnce(channel, { reason: 'interval' })
|
|
304
|
+
.catch((err) => {
|
|
305
|
+
this.host.log.error(`自动同步定时触发失败(${channel})`, { error: err instanceof Error ? err.message : String(err) });
|
|
306
|
+
})
|
|
307
|
+
.then(() => {
|
|
308
|
+
// 只重排自己的 interval 定时器(远端轮询有自己的节奏,别的通道也不得被本通道重置)
|
|
309
|
+
if (!this.stopped)
|
|
310
|
+
this.scheduleChannelTimer(channel);
|
|
311
|
+
});
|
|
312
|
+
}, ms);
|
|
313
|
+
this.timers.set(channel, timer);
|
|
314
|
+
}).catch(() => { });
|
|
285
315
|
}
|
|
286
316
|
/**
|
|
287
317
|
* realtime 通道的远端轮询:按 remoteCheckMs(缺省 60s)排一次性定时器。
|
|
@@ -514,7 +544,12 @@ export class AutoSyncScheduler {
|
|
|
514
544
|
try {
|
|
515
545
|
// 锁内重跑一次检测:预检到写入之间可能被别的进程推进(远端新快照 / 本机配置改动),
|
|
516
546
|
// 拿过期结论去写等于覆盖别人的成果。无锁环境(测试)直接用预检结果 —— 同一份数据,没有竞态。
|
|
517
|
-
|
|
547
|
+
// P0(2026-11-14 真机事故):锁内重跑必须与预检同口径传 localCheck。
|
|
548
|
+
// 此前这里漏传 → 60s 远端轮询在锁内又跑了一遍全量本地导出(plugins 的 npm pack,
|
|
549
|
+
// 实测单插件 44.7s/轮),预检省下的开销全被这一步还回去。
|
|
550
|
+
const phase = releaseLock === null
|
|
551
|
+
? preflight
|
|
552
|
+
: await this.detectPhase(channel, cfg, nowIso, historyId, { localCheck: opts.reason !== 'remote-poll' });
|
|
518
553
|
if (phase.kind === 'skip')
|
|
519
554
|
return phase.result;
|
|
520
555
|
const { engine, remoteNew, localDirty, firstRun } = phase;
|
|
@@ -528,6 +563,12 @@ export class AutoSyncScheduler {
|
|
|
528
563
|
* 真机现场「推本机配置当备份」这条链路被远端冲突连续阻塞,用户却只看到 skipped/conflict。
|
|
529
564
|
*/
|
|
530
565
|
let conflictedSections = [];
|
|
566
|
+
//
|
|
567
|
+
// issue #136:Phase B 的「成功但没全写进去」必须能被看见 —— 旧实现只看 applyReport.ok,
|
|
568
|
+
// ok 时把 warnings / 待裁决项整包丢掉,于是「patch 行被剥空→守卫保留本机」与
|
|
569
|
+
// 「叶级冲突被扣下」都表现为一轮干净的成功。
|
|
570
|
+
let downlinkWarnings = [];
|
|
571
|
+
let heldLeafSections = [];
|
|
531
572
|
// Phase A: pull 合并(下载)—— 仅当远端有新快照才拉取(§3.2)。
|
|
532
573
|
if (remoteNew) {
|
|
533
574
|
let mergePlan;
|
|
@@ -585,6 +626,8 @@ export class AutoSyncScheduler {
|
|
|
585
626
|
})).result
|
|
586
627
|
: await rawApply();
|
|
587
628
|
appliedSections = applyReport.applied;
|
|
629
|
+
downlinkWarnings = applyReport.warnings;
|
|
630
|
+
heldLeafSections = [...new Set(applyReport.review.map((r) => r.sectionId))];
|
|
588
631
|
if (!applyReport.ok) {
|
|
589
632
|
const error = applyReport.warnings.join('; ') || engineMsg('sync.autosyncApplyMergeFailed');
|
|
590
633
|
const result = {
|
|
@@ -636,12 +679,18 @@ export class AutoSyncScheduler {
|
|
|
636
679
|
// 绝不记 failed(那会累加连续失败计数并对用户发通知,把「需要用『检查远程合并』处理」
|
|
637
680
|
// 谎报成「同步坏了」);错误摘要保留引擎给出的说明(用户能看见到底为什么没推上去)。
|
|
638
681
|
const message = pushReport.message ?? engineMsg('sync.autosyncPushFailed');
|
|
682
|
+
// issue #136:下行自身的告警/待裁决与「推送被冲突挡下」合并进同一条记录,
|
|
683
|
+
// 不因上行受阻就把下行的事实抹掉。
|
|
684
|
+
const blockedConflicted = [...new Set([...conflictedSections, ...heldLeafSections])];
|
|
685
|
+
const blockedDegraded = downlinkWarnings.length > 0 || heldLeafSections.length > 0;
|
|
639
686
|
const conflictResult = {
|
|
640
|
-
status:
|
|
687
|
+
status: blockedConflicted.length > 0 || blockedDegraded ? 'partial' : 'skipped',
|
|
641
688
|
direction: appliedSections.length ? 'both' : 'push',
|
|
642
689
|
skipReason: 'conflict', error: message, historyId,
|
|
643
690
|
appliedSections,
|
|
644
|
-
...(
|
|
691
|
+
...(blockedConflicted.length > 0 ? { conflictedSections: blockedConflicted } : {}),
|
|
692
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
693
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
645
694
|
consecutiveFailures: 0,
|
|
646
695
|
...forcedMark,
|
|
647
696
|
};
|
|
@@ -649,10 +698,14 @@ export class AutoSyncScheduler {
|
|
|
649
698
|
direction: appliedSections.length ? 'both' : 'push', status: conflictResult.status,
|
|
650
699
|
skipReason: 'conflict', error: message,
|
|
651
700
|
appliedSections,
|
|
652
|
-
...(
|
|
701
|
+
...(blockedConflicted.length > 0 ? { conflictedSections: blockedConflicted } : {}),
|
|
702
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
703
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
653
704
|
createdAt: nowIso, failureCountAtRun: 0,
|
|
654
705
|
...forcedMark,
|
|
655
706
|
});
|
|
707
|
+
// 冲突分区落成待裁决(P2):首页与同步页据此给出裁决入口。
|
|
708
|
+
await this.recordPendingMerge(engine, conflictedSections);
|
|
656
709
|
await this.writeFinalConfig(cfg, conflictResult, nowIso, historyId, channel);
|
|
657
710
|
return conflictResult;
|
|
658
711
|
}
|
|
@@ -692,21 +745,38 @@ export class AutoSyncScheduler {
|
|
|
692
745
|
// 如实记一条 skipReason,让历史与首页能说清「这次推上去之前先修好了基线」,
|
|
693
746
|
// 而不是让用户以为又发生了一次莫名冲突。冲突原因优先(真有冲突时更该被看见)。
|
|
694
747
|
const adoptedReason = pushReport.adoptedBase !== undefined ? 'baseline-adopted' : undefined;
|
|
748
|
+
// issue #136:下行有告警 / 有待裁决叶级项 → 终态只能是 partial。
|
|
749
|
+
// 「推送成功」不能替下行的未完成背书:用户会从 success 得出「两端一样了」,而实际上
|
|
750
|
+
// 有些叶子被有意扣下(保留本机 / 等待裁决)。
|
|
751
|
+
// 两类降级必须分开说:只有「叶级冲突被扣下」才是冲突待裁决;
|
|
752
|
+
// 纯告警(插件没装上、undo 有残留等)与冲突无关,套上 'conflict' 会让
|
|
753
|
+
// 界面上出现「跳过原因:冲突」「另有 N 个分区待确认」,而 conflictedSections 是空的。
|
|
754
|
+
const heldDegraded = heldLeafSections.length > 0;
|
|
755
|
+
const degraded = downlinkWarnings.length > 0 || heldDegraded;
|
|
756
|
+
const finalPartial = partial || degraded;
|
|
757
|
+
const finalSkipReason = skipReason
|
|
758
|
+
?? (heldDegraded ? 'conflict' : degraded ? 'warning' : undefined);
|
|
759
|
+
// 扣下的叶级冲突所在分区也要出现在「待处理分区」里(与分区级冲突合并去重)
|
|
760
|
+
const finalConflicted = [...new Set([...reportedConflicted, ...heldLeafSections])];
|
|
695
761
|
const result = {
|
|
696
|
-
status:
|
|
762
|
+
status: finalPartial ? 'partial' : 'success',
|
|
697
763
|
direction: appliedSections.length ? 'both' : 'push',
|
|
698
764
|
appliedSections, pushedSnapshotId: pushReport.snapshotId, historyId,
|
|
699
|
-
...(
|
|
700
|
-
...(
|
|
765
|
+
...(finalSkipReason !== undefined || adoptedReason !== undefined ? { skipReason: finalSkipReason ?? adoptedReason } : {}),
|
|
766
|
+
...(finalConflicted.length > 0 ? { conflictedSections: finalConflicted } : {}),
|
|
767
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
768
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
701
769
|
consecutiveFailures: 0,
|
|
702
770
|
...forcedMark,
|
|
703
771
|
};
|
|
704
772
|
await this.appendHistory(channel, {
|
|
705
773
|
direction: appliedSections.length ? 'both' : 'push',
|
|
706
|
-
status:
|
|
774
|
+
status: finalPartial ? 'partial' : 'success',
|
|
707
775
|
appliedSections, pushedSnapshotId: pushReport.snapshotId,
|
|
708
|
-
...(
|
|
709
|
-
...(
|
|
776
|
+
...(finalSkipReason !== undefined || adoptedReason !== undefined ? { skipReason: finalSkipReason ?? adoptedReason } : {}),
|
|
777
|
+
...(finalConflicted.length > 0 ? { conflictedSections: finalConflicted } : {}),
|
|
778
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
779
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
710
780
|
createdAt: nowIso, failureCountAtRun: 0,
|
|
711
781
|
...forcedMark,
|
|
712
782
|
});
|
|
@@ -732,28 +802,45 @@ export class AutoSyncScheduler {
|
|
|
732
802
|
}
|
|
733
803
|
// 本轮下行被冲突挡下、且没有可上行内容(startup 变体 / 本地无改动)→ 如实 skipped/conflict。
|
|
734
804
|
if (conflictedSections.length > 0) {
|
|
805
|
+
// issue #136:被扣下的叶级冲突也算「待处理分区」(与分区级冲突同列)。
|
|
806
|
+
const skipConflicted = [...new Set([...conflictedSections, ...heldLeafSections])];
|
|
735
807
|
const conflictResult = {
|
|
736
808
|
status: 'skipped', direction: 'pull', skipReason: 'conflict',
|
|
737
|
-
conflictedSections, historyId, consecutiveFailures: 0,
|
|
809
|
+
conflictedSections: skipConflicted, historyId, consecutiveFailures: 0,
|
|
810
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
811
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
738
812
|
};
|
|
739
813
|
await this.appendHistory(channel, {
|
|
740
814
|
direction: 'pull', status: 'skipped', skipReason: 'conflict',
|
|
741
|
-
conflictedSections, createdAt: nowIso, failureCountAtRun: 0,
|
|
815
|
+
conflictedSections: skipConflicted, createdAt: nowIso, failureCountAtRun: 0,
|
|
816
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
817
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
742
818
|
});
|
|
819
|
+
await this.recordPendingMerge(engine, conflictedSections);
|
|
743
820
|
await this.writeFinalConfig(cfg, conflictResult, nowIso, historyId, channel);
|
|
744
821
|
return conflictResult;
|
|
745
822
|
}
|
|
746
823
|
// startup 变体 / 仅远端合并:只做 pull 合并(不上传),或远端无新生且本地无改动(已在上方 upToDate 短路上)。
|
|
824
|
+
// issue #136:纯下行的告警/待裁决同样算「没全做完」—— 终态不得写成 success。
|
|
825
|
+
const degraded = downlinkWarnings.length > 0 || heldLeafSections.length > 0;
|
|
826
|
+
const pulledConflicted = [...new Set([...deferredSections, ...heldLeafSections])];
|
|
827
|
+
const pullSkipReason = !degraded && appliedSections.length === 0
|
|
828
|
+
? 'unchanged'
|
|
829
|
+
: degraded ? 'conflict' : undefined;
|
|
747
830
|
const result = {
|
|
748
|
-
status: 'success', direction: 'pull', appliedSections,
|
|
749
|
-
...(
|
|
750
|
-
...(
|
|
831
|
+
status: degraded ? 'partial' : 'success', direction: 'pull', appliedSections,
|
|
832
|
+
...(pullSkipReason !== undefined ? { skipReason: pullSkipReason } : {}),
|
|
833
|
+
...(pulledConflicted.length > 0 ? { conflictedSections: pulledConflicted } : {}),
|
|
834
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
835
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
751
836
|
historyId, consecutiveFailures: 0,
|
|
752
837
|
};
|
|
753
838
|
await this.appendHistory(channel, {
|
|
754
|
-
direction: 'pull', status: 'success', appliedSections,
|
|
755
|
-
...(
|
|
756
|
-
...(
|
|
839
|
+
direction: 'pull', status: degraded ? 'partial' : 'success', appliedSections,
|
|
840
|
+
...(pullSkipReason !== undefined ? { skipReason: pullSkipReason } : {}),
|
|
841
|
+
...(pulledConflicted.length > 0 ? { conflictedSections: pulledConflicted } : {}),
|
|
842
|
+
...(downlinkWarnings.length > 0 ? { warnings: downlinkWarnings } : {}),
|
|
843
|
+
...(heldLeafSections.length > 0 ? { heldSections: heldLeafSections } : {}),
|
|
757
844
|
createdAt: nowIso, failureCountAtRun: 0,
|
|
758
845
|
});
|
|
759
846
|
await this.writeFinalConfig(cfg, result, nowIso, historyId, channel);
|
|
@@ -893,6 +980,26 @@ export class AutoSyncScheduler {
|
|
|
893
980
|
}
|
|
894
981
|
return { kind: 'work', engine, remoteNew, localDirty, firstRun };
|
|
895
982
|
}
|
|
983
|
+
/**
|
|
984
|
+
* P2(2026-11-14 真机事故):把「本轮被冲突挡下的分区」写进 sync-state.pendingMerge。
|
|
985
|
+
*
|
|
986
|
+
* 过去自动同步只在历史里留一句「冲突」,sync-state 里没有可挂入口的持久对象 ——
|
|
987
|
+
* 首页「待裁决」徽章于是只有颜色没有去处,用户点不动、也不知道该去哪裁决。
|
|
988
|
+
* 这里由引擎按**本轮实际合并过的远端快照 id** 落 pendingMerge(同远端快照连续冲突保留首次 since),
|
|
989
|
+
* 界面据 /sync/status 的 pendingMerge 显示待裁决分区,首页那一行据此跳到同步页。
|
|
990
|
+
*
|
|
991
|
+
* 尽力而为:写待裁决失败绝不能把「远端有冲突」这一事实变成 failed(那会累加连续失败计数)。
|
|
992
|
+
*/
|
|
993
|
+
async recordPendingMerge(engine, sections) {
|
|
994
|
+
if (sections.length === 0)
|
|
995
|
+
return;
|
|
996
|
+
try {
|
|
997
|
+
await engine.recordPendingMerge(sections);
|
|
998
|
+
}
|
|
999
|
+
catch (err) {
|
|
1000
|
+
this.host.log.warn(`自动同步待裁决分区未能落盘:${err instanceof Error ? err.message : String(err)}`);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
896
1003
|
async writeFinalConfig(base, result, nowIso, historyId, channel) {
|
|
897
1004
|
const skipReason = result.status === 'failed' ? undefined : result.skipReason;
|
|
898
1005
|
const countsConflicts = skipReason === 'conflict' || skipReason === 'conflict-kept-local';
|
|
@@ -904,7 +1011,8 @@ export class AutoSyncScheduler {
|
|
|
904
1011
|
lastRunAt: nowIso,
|
|
905
1012
|
lastRunStatus: result.status,
|
|
906
1013
|
consecutiveFailures: result.consecutiveFailures,
|
|
907
|
-
|
|
1014
|
+
// 历史被压掉的轮次没有新记录 id —— 保留上一条真记录的 id,绝不指向不存在的条目。
|
|
1015
|
+
lastRunHistoryId: this.historyDeduped ? (base.lastRunHistoryId ?? historyId) : historyId,
|
|
908
1016
|
lastRunMessage: result.error ?? '',
|
|
909
1017
|
lastRunSkipReason: skipReason,
|
|
910
1018
|
lastRunSkipCount: skipCount,
|
|
@@ -28,6 +28,13 @@ export async function adoptOwnRemoteHead(opts) {
|
|
|
28
28
|
continue;
|
|
29
29
|
state.sections[sid] = { hash, updatedAt: meta.createdAt };
|
|
30
30
|
}
|
|
31
|
+
// issue #140(G-44):被收养的快照自己就是「远端当前观测」——它的 sections 记进独立字段。
|
|
32
|
+
// 空对象(加密快照)⇒ 删除观测:判定层因此回落 id 比较,绝不拿 state.sections 顶上。
|
|
33
|
+
const remoteHashes = { ...meta.sections };
|
|
34
|
+
if (Object.keys(remoteHashes).length > 0)
|
|
35
|
+
state.lastRemoteSections = remoteHashes;
|
|
36
|
+
else
|
|
37
|
+
delete state.lastRemoteSections;
|
|
31
38
|
state.transport = opts.transport;
|
|
32
39
|
await saveSyncState(opts.stateDir, state, opts.fsx);
|
|
33
40
|
opts.warnings.push(opts.msg(ancestorKnown ? 'sync.pushBaselineAdopted' : 'sync.pushBaselineAdoptedNoAncestor', {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* m-sync-flow:写基线的唯一实现(本地祖先副本 + sync-state)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独成模块:这段逻辑是「祖先副本布局 + 远端指针 + 每分区 hash + 裁剪」的完整落盘决策,
|
|
5
|
+
* 不需要引擎的任何其他状态(只吃快照目录、通道身份与时间);留在 SyncEngine 里会顶穿体量棘轮。
|
|
6
|
+
*
|
|
7
|
+
* **两个 id 语义严格分开(P0-7),不要再混用同一个字段**:
|
|
8
|
+
* - `snapshotId`:本次基线的**本地**快照对象 id,同时是本地祖先副本的目录名(写入 `ancestorId`);
|
|
9
|
+
* - `opts.remoteSnapshotId`:该基线在**远端**对应的快照 id(写入 `lastSnapshotId`)。缺省 = snapshotId
|
|
10
|
+
* (push 路径两者天然相同);合并/应用路径**必须显式传**,否则 lastSnapshotId 会退化成「一个远端
|
|
11
|
+
* 并不存在的本地 id」→ hasNewRemoteSnapshot() 恒判「远端有新生」,每轮白跑 list + download + merge。
|
|
12
|
+
* 传入 `''` 表示「远端 id 未知」——宁可留空,也不要把本地 id 冒充成远端 id。
|
|
13
|
+
*
|
|
14
|
+
* **issue #140 / G-44**:`opts.remoteSections` 是**远端观测**(远端快照自己携带的分区哈希),
|
|
15
|
+
* 与传入的 `sections`(本机观测,用来算 hasLocalChanges 的基线)是两个不同来源。
|
|
16
|
+
* 早先的事故正是「一参两用」:拿本机 hash 当远端现状,判定就永远自洽、永远看不出远端真的变了。
|
|
17
|
+
*/
|
|
18
|
+
import type { Platform, SectionData, SectionId } from '../schema/types.ts';
|
|
19
|
+
import type { MsgFunc } from '../core/messages.ts';
|
|
20
|
+
import type { SnapshotFs } from './fs.ts';
|
|
21
|
+
import type { PendingMerge, SectionHashes } from './sync-state.ts';
|
|
22
|
+
export interface RecordBaselineOptions {
|
|
23
|
+
writeAncestor?: boolean;
|
|
24
|
+
/** 提供即把祖先副本写成**密文**(密码仅本次内存使用;issue #107) */
|
|
25
|
+
ancestorPassword?: string;
|
|
26
|
+
remoteSnapshotId?: string;
|
|
27
|
+
/** 保留当前祖先副本与 ancestorId(缺省 false = 既有行为逐字不变) */
|
|
28
|
+
preserveAncestor?: boolean;
|
|
29
|
+
/** 待裁决冲突记录:null = 清除;缺省 = 不动该字段 */
|
|
30
|
+
pendingMerge?: PendingMerge | null;
|
|
31
|
+
/** P1-5:本轮**实际带走**的会话单元(提供即覆盖;缺省 = 保留原记录) */
|
|
32
|
+
sessionUnits?: string[];
|
|
33
|
+
/** P1-5:本轮累积的删除墓碑(提供即覆盖;缺省 = 保留原记录) */
|
|
34
|
+
deletedSessions?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* issue #140:本次基线对应的**远端观测**(远端快照实际携带的分区哈希,G-44)。
|
|
37
|
+
*
|
|
38
|
+
* - 提供且非空 ⇒ 原样记录(只允许来自「真的看到了远端 sections」的调用点);
|
|
39
|
+
* - 提供但为空(加密快照)或未提供 ⇒ 指针若发生变化则**删除**既有观测(未知 ⇒ 判定回落 id);
|
|
40
|
+
* - 指针未变(preserveAncestor 的待裁决冲突)⇒ 保留既有观测(远端没动,观测仍成立)。
|
|
41
|
+
* 绝不允许由传入的 `sections`(本机观测)推导。
|
|
42
|
+
*/
|
|
43
|
+
remoteSections?: SectionHashes;
|
|
44
|
+
}
|
|
45
|
+
/** 落盘所需的环境(引擎逐字段透传;此处不持有引擎实例)。 */
|
|
46
|
+
export interface RecordBaselineEnv {
|
|
47
|
+
fsx: SnapshotFs;
|
|
48
|
+
stateDir: string;
|
|
49
|
+
localSnapshotsDir?: string;
|
|
50
|
+
dshVersion: string;
|
|
51
|
+
platform: Platform;
|
|
52
|
+
transport: {
|
|
53
|
+
type: string;
|
|
54
|
+
ref: string;
|
|
55
|
+
};
|
|
56
|
+
now: () => Date;
|
|
57
|
+
msg: MsgFunc;
|
|
58
|
+
}
|
|
59
|
+
export interface RecordBaselineInput {
|
|
60
|
+
snapshotId: string;
|
|
61
|
+
/** 基线**恒为明文**(加密快照的基线 hash 也用明文算,供 hasLocalChanges 比较)。 */
|
|
62
|
+
sections: Partial<Record<SectionId, SectionData>>;
|
|
63
|
+
nowIso?: string;
|
|
64
|
+
opts?: RecordBaselineOptions;
|
|
65
|
+
env: RecordBaselineEnv;
|
|
66
|
+
}
|
|
67
|
+
export declare function recordBaseline(input: RecordBaselineInput): Promise<void>;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { CURRENT_SCHEMA_VERSION } from '../schema/versions.js';
|
|
2
|
+
import { DEFAULT_ANCESTOR_KEEP, pruneAncestors, writeAncestor, writeEncryptedAncestor } from './ancestor.js';
|
|
3
|
+
import { encryptSectionsPayload } from './snapshot-crypto.js';
|
|
4
|
+
import { hashSection, loadSyncState, saveSyncState } from './sync-state.js';
|
|
5
|
+
export async function recordBaseline(input) {
|
|
6
|
+
const { snapshotId, sections, env } = input;
|
|
7
|
+
const opts = input.opts ?? {};
|
|
8
|
+
const ts = input.nowIso ?? env.now().toISOString();
|
|
9
|
+
const preserveAncestor = opts.preserveAncestor === true;
|
|
10
|
+
const ancestorPassword = opts.ancestorPassword;
|
|
11
|
+
const encryptedAncestor = ancestorPassword !== undefined && ancestorPassword !== '';
|
|
12
|
+
const writesAncestorCopy = !preserveAncestor && env.localSnapshotsDir !== undefined && opts.writeAncestor !== false;
|
|
13
|
+
// 1) 写本地祖先副本(如有 localSnapshotsDir)。
|
|
14
|
+
// ancestorPassword 提供 → 落**密文**祖先副本(issue #107:加密通道也能做三方合并);
|
|
15
|
+
// 否则落明文散文件布局(既有行为逐字不变)。
|
|
16
|
+
if (writesAncestorCopy && env.localSnapshotsDir !== undefined) {
|
|
17
|
+
const manifest = {
|
|
18
|
+
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
19
|
+
dshVersion: env.dshVersion,
|
|
20
|
+
platform: env.platform,
|
|
21
|
+
sectionIds: Object.keys(sections),
|
|
22
|
+
containsSecrets: false,
|
|
23
|
+
// 记录触发通道(git/webdav),供同步历史展示「由哪个通道触发」
|
|
24
|
+
transport: env.transport.type,
|
|
25
|
+
...(encryptedAncestor ? { encrypted: true } : {}),
|
|
26
|
+
};
|
|
27
|
+
if (encryptedAncestor) {
|
|
28
|
+
const encryptedSections = await encryptSectionsPayload(sections, ancestorPassword);
|
|
29
|
+
await writeEncryptedAncestor(env.localSnapshotsDir, { id: snapshotId, createdAt: ts, manifest, sections: encryptedSections }, env.fsx);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
await writeAncestor(env.localSnapshotsDir, { id: snapshotId, createdAt: ts, manifest, sections }, env.fsx);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// 2) 更新 sync-state:远端指针 + 本地祖先目录名 + 每分区 hash/updatedAt + lastSyncAt + transport
|
|
36
|
+
const state = await loadSyncState(env.stateDir, env.fsx, env.msg);
|
|
37
|
+
const previousPointer = state.lastSnapshotId;
|
|
38
|
+
state.lastSnapshotId = opts.remoteSnapshotId ?? snapshotId;
|
|
39
|
+
// issue #140(G-44):远端观测只认调用方显式给出的「远端快照 sections」。
|
|
40
|
+
// 指针变了却拿不到观测 ⇒ 删除(下一轮判定回落 id 比较),绝不沿用上一份观测冒充现状。
|
|
41
|
+
if (opts.remoteSections !== undefined) {
|
|
42
|
+
if (Object.keys(opts.remoteSections).length > 0)
|
|
43
|
+
state.lastRemoteSections = { ...opts.remoteSections };
|
|
44
|
+
else
|
|
45
|
+
delete state.lastRemoteSections;
|
|
46
|
+
}
|
|
47
|
+
else if (state.lastSnapshotId !== previousPointer) {
|
|
48
|
+
delete state.lastRemoteSections;
|
|
49
|
+
}
|
|
50
|
+
// preserveAncestor:祖先目录名一并保持不动(既不是 snapshotId,也不是 '')
|
|
51
|
+
if (!preserveAncestor)
|
|
52
|
+
state.ancestorId = writesAncestorCopy ? snapshotId : '';
|
|
53
|
+
state.lastSyncAt = ts;
|
|
54
|
+
state.transport = { type: env.transport.type, ref: env.transport.ref };
|
|
55
|
+
// 基线纪律:null = 清除(全部裁决完);undefined = 不动该字段(既有调用方零感知)
|
|
56
|
+
if (opts.pendingMerge === null)
|
|
57
|
+
delete state.pendingMerge;
|
|
58
|
+
else if (opts.pendingMerge !== undefined)
|
|
59
|
+
state.pendingMerge = opts.pendingMerge;
|
|
60
|
+
// P1-5:只在调用方明确提供时覆盖(否则保留既有记录,绝不因某条路径不跟踪而清空)
|
|
61
|
+
if (opts.sessionUnits !== undefined)
|
|
62
|
+
state.sessionUnits = [...opts.sessionUnits];
|
|
63
|
+
if (opts.deletedSessions !== undefined)
|
|
64
|
+
state.deletedSessions = [...opts.deletedSessions];
|
|
65
|
+
for (const [sid, data] of Object.entries(sections)) {
|
|
66
|
+
state.sections[sid] = { hash: hashSection(data), updatedAt: ts };
|
|
67
|
+
}
|
|
68
|
+
await saveSyncState(env.stateDir, state, env.fsx);
|
|
69
|
+
// 3) 裁剪祖先副本(最近 N 个);preserveAncestor 时本轮没有新副本,不参与裁剪
|
|
70
|
+
if (!preserveAncestor && env.localSnapshotsDir !== undefined) {
|
|
71
|
+
await pruneAncestors(env.localSnapshotsDir, DEFAULT_ANCESTOR_KEEP, env.fsx);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=baseline-record.js.map
|
package/lib/sync/blob-store.js
CHANGED
|
@@ -47,6 +47,19 @@ export async function refsToSection(refs, sink) {
|
|
|
47
47
|
if (data === null) {
|
|
48
48
|
throw new Error(engineMsg('sync.blobMissing', { hash: ref.blobHash, path: ref.relativePath }));
|
|
49
49
|
}
|
|
50
|
+
// 内容寻址仓的引用自带 sha256(+ 字节数),取回后必须自证(审计 P6-3)。
|
|
51
|
+
// 注释承诺「远端仓被清理坏/被手工删过」必须硬失败,但此前只覆盖「删了」:
|
|
52
|
+
// 内容被改、半截写、base64 传输丢字符都会畅通无阻地把错误字节当成会话原样写回,
|
|
53
|
+
// 而「静默损坏」比「硬失败」更糟 —— 用户看不到任何错误,只发现对话内容变了。
|
|
54
|
+
const actualHash = sha256Hex(data);
|
|
55
|
+
if (actualHash !== ref.blobHash || (ref.sizeBytes !== undefined && data.byteLength !== ref.sizeBytes)) {
|
|
56
|
+
throw new Error(engineMsg('sync.blobCorrupt', {
|
|
57
|
+
hash: ref.blobHash,
|
|
58
|
+
path: ref.relativePath,
|
|
59
|
+
actual: actualHash,
|
|
60
|
+
size: String(data.byteLength),
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
50
63
|
files.push({ relativePath: ref.relativePath, data, contentHash: ref.blobHash });
|
|
51
64
|
}
|
|
52
65
|
return { version: 1, files };
|
|
@@ -0,0 +1,39 @@
|
|
|
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 type { SectionHashes } from './sync-state.ts';
|
|
23
|
+
/** 判定结论的三种取值(`unknown` 不是失败,是「没有可用观测」) */
|
|
24
|
+
export type ContentDiffReason = 'same-content' | 'content-changed' | 'unknown';
|
|
25
|
+
export interface ContentDiffResult {
|
|
26
|
+
/** `unknown` 恒为 false —— 调用方必须自己决定回落策略,绝不能把它当「变了」或「没变」 */
|
|
27
|
+
changed: boolean;
|
|
28
|
+
reason: ContentDiffReason;
|
|
29
|
+
}
|
|
30
|
+
/** 分区导出载荷 → 分区哈希(推送上行 / 本地观测共用;与 sync-state 记的基线同口径) */
|
|
31
|
+
export declare function hashSections(sections: Partial<Record<SectionId, SectionData>>): SectionHashes;
|
|
32
|
+
/**
|
|
33
|
+
* 远端快照内容 vs 对照观测(`local` 优先,缺省用 `baseline`)。
|
|
34
|
+
*
|
|
35
|
+
* @param remote 远端快照元数据(transport.list() 的元素;`undefined` = 远端没有快照/列表读不到)
|
|
36
|
+
* @param baseline 上次记录的**远端**分区哈希(sync-state.lastRemoteSections)
|
|
37
|
+
* @param local 本次刚导出的本地分区哈希(推送路径用;拉取路径不传)
|
|
38
|
+
*/
|
|
39
|
+
export declare function diffSnapshotContent(remote: SyncSnapshotMeta | undefined | null, baseline: SectionHashes | undefined | null, local?: SectionHashes | null): ContentDiffResult;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { hashSection } from './sync-state.js';
|
|
2
|
+
/** 分区导出载荷 → 分区哈希(推送上行 / 本地观测共用;与 sync-state 记的基线同口径) */
|
|
3
|
+
export function hashSections(sections) {
|
|
4
|
+
const out = {};
|
|
5
|
+
for (const [sid, data] of Object.entries(sections)) {
|
|
6
|
+
if (data === undefined)
|
|
7
|
+
continue;
|
|
8
|
+
out[sid] = hashSection(data);
|
|
9
|
+
}
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
/** 是否「有观测」(undefined / 空对象都算没有:空集合无法证伪任何内容) */
|
|
13
|
+
function hasObservation(hashes) {
|
|
14
|
+
return hashes !== undefined && hashes !== null && Object.keys(hashes).length > 0;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 远端快照内容 vs 对照观测(`local` 优先,缺省用 `baseline`)。
|
|
18
|
+
*
|
|
19
|
+
* @param remote 远端快照元数据(transport.list() 的元素;`undefined` = 远端没有快照/列表读不到)
|
|
20
|
+
* @param baseline 上次记录的**远端**分区哈希(sync-state.lastRemoteSections)
|
|
21
|
+
* @param local 本次刚导出的本地分区哈希(推送路径用;拉取路径不传)
|
|
22
|
+
*/
|
|
23
|
+
export function diffSnapshotContent(remote, baseline, local) {
|
|
24
|
+
const reference = local ?? baseline;
|
|
25
|
+
if (remote === undefined || remote === null)
|
|
26
|
+
return { changed: false, reason: 'unknown' };
|
|
27
|
+
const remoteHashes = remote.sections;
|
|
28
|
+
// 加密快照的 sections 恒为空对象(transport.ts:53-55)→ 无从比较,回落 id。
|
|
29
|
+
if (!hasObservation(remoteHashes))
|
|
30
|
+
return { changed: false, reason: 'unknown' };
|
|
31
|
+
if (!hasObservation(reference))
|
|
32
|
+
return { changed: false, reason: 'unknown' };
|
|
33
|
+
// 键集对称:任一侧缺键都算内容变了(新增/收窄分区的推送必须产生新快照)。
|
|
34
|
+
for (const sid of Object.keys(remoteHashes)) {
|
|
35
|
+
const recorded = reference[sid];
|
|
36
|
+
if (recorded === undefined || recorded !== remoteHashes[sid]) {
|
|
37
|
+
return { changed: true, reason: 'content-changed' };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const sid of Object.keys(reference)) {
|
|
41
|
+
if (remoteHashes[sid] === undefined)
|
|
42
|
+
return { changed: true, reason: 'content-changed' };
|
|
43
|
+
}
|
|
44
|
+
return { changed: false, reason: 'same-content' };
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=content-diff.js.map
|