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,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
|
|
@@ -114,6 +114,8 @@ export declare class GitTransport implements SyncTransport {
|
|
|
114
114
|
* 的唯一线索。列表接口把它如实回传,让状态自洽(绝不静默失败)。
|
|
115
115
|
*/
|
|
116
116
|
private readErrors;
|
|
117
|
+
/** 存量工作副本的 sparse cone 是否已确认包含 latest/(issue #141;只补一次,不每次同步都重设) */
|
|
118
|
+
private sparseConeCoversLatest;
|
|
117
119
|
private readonly msg;
|
|
118
120
|
/** repoUrl 是否为网络地址(本地目录仓库 == false):决定要不要额外禁 file 传输(SEC-12)。 */
|
|
119
121
|
private readonly networkRepo;
|
|
@@ -140,6 +142,27 @@ export declare class GitTransport implements SyncTransport {
|
|
|
140
142
|
upload(snapshot: SyncSnapshot): Promise<SyncSnapshotMeta>;
|
|
141
143
|
/** upload 的实体;**调用方必须已持有工作副本锁** */
|
|
142
144
|
private uploadLocked;
|
|
145
|
+
/**
|
|
146
|
+
* 空文件类分区的 git 占位文件(git 不跟踪空目录):
|
|
147
|
+
* 占位文件不参与 manifest.sectionHashes(基于传入数据计算);读回时按名+内容过滤。
|
|
148
|
+
*/
|
|
149
|
+
private writeEmptyFileSectionPlaceholders;
|
|
150
|
+
/**
|
|
151
|
+
* 写 `latest/` 派生视图(issue #141):与 `snapshots/<id>/` 逐文件同布局,manifest 里保留**真实快照 id**
|
|
152
|
+
* (反向识别用)。返回本轮是否真的重建 —— false = 远端已有更新的视图,保持不动。
|
|
153
|
+
*
|
|
154
|
+
* 为什么按整份快照重写而不是拷目录:布局只有 layout.ts 一处权威,重写不会漏掉新增/删除的分区与占位文件;
|
|
155
|
+
* 外置分区只写引用文件(字节在共享 blobs/ 里按内容寻址,不重复落盘、不重复上传)。
|
|
156
|
+
*/
|
|
157
|
+
private writeLatestMirror;
|
|
158
|
+
/**
|
|
159
|
+
* 存量工作副本的 sparse-checkout 面是**旧版本**建的(不含 `latest/`):cone 之外的路径 `git add` 会
|
|
160
|
+
* 拒绝入索引("outside of your sparse-checkout definition")⇒ 派生视图永远推不上去。
|
|
161
|
+
* 首次写 latest/ 前把 cone 补齐一次(克隆时 syncDirs() 已含它,新副本不会走到这里)。
|
|
162
|
+
*
|
|
163
|
+
* 非 sparse 副本(老 git 回落全量 clone)无需处理;读不出配置一律按「无需处理」走,绝不因优化失败。
|
|
164
|
+
*/
|
|
165
|
+
private ensureSparseConeCoversLatest;
|
|
143
166
|
/** 下载快照完整载荷。明文 → 读回散文件目录;加密 → 读回密文单文件。不存在的 id 必须抛错(契约)。 */
|
|
144
167
|
download(id: string): Promise<SyncSnapshot>;
|
|
145
168
|
/** download 的实体;**调用方必须已持有工作副本锁** */
|
|
@@ -283,6 +306,12 @@ export declare class GitTransport implements SyncTransport {
|
|
|
283
306
|
* (后续 push 会自己建分支);有分支但名字对不上则拒绝猜,把远端分支名列出来。
|
|
284
307
|
*/
|
|
285
308
|
private adoptRemoteHistory;
|
|
309
|
+
/**
|
|
310
|
+
* adopt 合并冲突里只放行 latest/(issue #141):派生视图两侧内容不同不算真冲突 ——
|
|
311
|
+
* 以远端的版本收敛(合并方向 origin/<branch> 即 theirs),其它路径一律不放行。
|
|
312
|
+
* 返回 false 表示冲突面不止 latest/,调用方必须 abort。
|
|
313
|
+
*/
|
|
314
|
+
private resolveLatestOnlyMergeConflict;
|
|
286
315
|
/**
|
|
287
316
|
* pull --ff-only 同步远端;无本地提交(全新仓库)或无 upstream 时静默跳过。
|
|
288
317
|
* opts.retryable=true(仅 list / download 等**幂等读**路径传):瞬时网络故障走有限指数退避重试。
|
|
@@ -338,7 +367,7 @@ export declare class GitTransport implements SyncTransport {
|
|
|
338
367
|
/** 子目录前缀下的同步布局相对路径('' 前缀 = 历史布局,逐字不变)。 */
|
|
339
368
|
private relDir;
|
|
340
369
|
/**
|
|
341
|
-
*
|
|
370
|
+
* 四个同步根目录(带前缀,含只读派生视图 latest/):sparse 面、add 路径、adopt 判据都从这里取 ——
|
|
342
371
|
* 任何一处漏加前缀都会让「写入 A、读取 B」(issue #131 的失败面)。
|
|
343
372
|
*/
|
|
344
373
|
private syncDirs;
|
|
@@ -357,6 +386,8 @@ export declare class GitTransport implements SyncTransport {
|
|
|
357
386
|
private assertSafeId;
|
|
358
387
|
private snapshotDir;
|
|
359
388
|
private snapshotsDir;
|
|
389
|
+
/** 明文最新快照的只读派生视图目录(<workDir>/latest/;配了前缀则在 <subdir>/latest/) */
|
|
390
|
+
private latestDir;
|
|
360
391
|
/** 加密快照密文单文件目录(<workDir>/snapshots-encrypted/;配了前缀则在 <subdir>/ 下) */
|
|
361
392
|
private encryptedSnapshotsDir;
|
|
362
393
|
/** 加密快照密文单文件路径(<id>.json) */
|
|
@@ -95,6 +95,17 @@ const SNAPSHOTS_REL = 'snapshots';
|
|
|
95
95
|
/** 加密快照的「密文单文件」目录:整个快照 JSON(含密文载荷)以 <id>.json 提交。
|
|
96
96
|
* 加密快照不写散文件目录(密文无法平铺为明文 JSON 分区;远端已存密文)。 */
|
|
97
97
|
const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
|
|
98
|
+
/**
|
|
99
|
+
* 明文最新快照的**只读派生视图**(issue #141):与 `snapshots/<id>/` 逐文件同布局,供人和 CI 直接读,
|
|
100
|
+
* 不必先知道快照 id。(契约:docs/contracts/sync-channel-v1.md)
|
|
101
|
+
*
|
|
102
|
+
* 四条硬约束,改这里之前先读:
|
|
103
|
+
* - **只写不读**:list()/download()/合并/保留策略/冲突判定一律不把它当快照目录,也不依赖它的存在;
|
|
104
|
+
* - **必须与快照进同一个 commit**:push 之后再单独提交一次就多出一次远端写(正是报告者担心的并发窗口);
|
|
105
|
+
* - 加密快照不写(密文没有明文镜像)—— 由引擎显式告警,绝不静默;
|
|
106
|
+
* - 保留策略剪旧不动它:它永远指向最新那份明文快照。
|
|
107
|
+
*/
|
|
108
|
+
const LATEST_REL = 'latest';
|
|
98
109
|
/**
|
|
99
110
|
* 内容寻址 blob 仓在仓库内的相对目录(P1-4)。
|
|
100
111
|
*
|
|
@@ -104,14 +115,20 @@ const SNAPSHOTS_ENCRYPTED_REL = 'snapshots-encrypted';
|
|
|
104
115
|
*/
|
|
105
116
|
const BLOBS_REL = 'blobs';
|
|
106
117
|
/**
|
|
107
|
-
* 同步仓库(git
|
|
118
|
+
* 同步仓库(git 通道)顶层**只可能出现**这四个条目。issue #104 的 `adopt` 模式拿它当安全判据:
|
|
108
119
|
* 远端分支的顶层出现别的名字 = 这不是本插件的同步仓库,合并它等于把别人的仓库内容搬进工作副本。
|
|
120
|
+
* `latest/` 只是承认「这是本插件写的派生视图」(issue #141),读取路径一律不碰它。
|
|
121
|
+
*
|
|
122
|
+
* 老版本客户端不知道 `latest/` ⇒ 只有在换仓库/远端历史重写时的 adopt 会 fail-closed 拒收
|
|
123
|
+
* (docs/known-issues/known-limits.md 有记录)。
|
|
109
124
|
*/
|
|
110
|
-
const ADOPT_ROOT_ALLOW = new Set([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL]);
|
|
125
|
+
const ADOPT_ROOT_ALLOW = new Set([SNAPSHOTS_REL, SNAPSHOTS_ENCRYPTED_REL, BLOBS_REL, LATEST_REL]);
|
|
111
126
|
/**
|
|
112
|
-
*
|
|
127
|
+
* 同步仓库只在这四个顶层目录里写内容 ⇒ partial clone 的 sparse-checkout 面与它同源(issue #122)。
|
|
113
128
|
* 远端仓库比同步内容大几个数量级时(报告者现场:1.8 GB 仓库 / 6 MB 内容),全量 clone 与全量 fetch
|
|
114
|
-
*
|
|
129
|
+
* 都要拉完整对象图,而工作副本真正需要的只有这四个目录。
|
|
130
|
+
* `latest/` 虽是派生物,但它是同一 commit 里的产物:落在 cone 之外 `git add` 会直接拒收入库
|
|
131
|
+
* ("outside of your sparse-checkout definition"),所以必须进 sparse 面。
|
|
115
132
|
*/
|
|
116
133
|
/* 具体的 sparse 面在 `syncDirs()` 里按子目录前缀派生(issue #131),基名与 ADOPT_ROOT_ALLOW 同源。 */
|
|
117
134
|
/**
|
|
@@ -309,6 +326,8 @@ export class GitTransport {
|
|
|
309
326
|
* 的唯一线索。列表接口把它如实回传,让状态自洽(绝不静默失败)。
|
|
310
327
|
*/
|
|
311
328
|
readErrors = [];
|
|
329
|
+
/** 存量工作副本的 sparse cone 是否已确认包含 latest/(issue #141;只补一次,不每次同步都重设) */
|
|
330
|
+
sparseConeCoversLatest = false;
|
|
312
331
|
msg;
|
|
313
332
|
/** repoUrl 是否为网络地址(本地目录仓库 == false):决定要不要额外禁 file 传输(SEC-12)。 */
|
|
314
333
|
networkRepo;
|
|
@@ -447,28 +466,24 @@ export class GitTransport {
|
|
|
447
466
|
await fsx.remove(dir); // 覆盖语义:先清旧目录,避免残留旧文件
|
|
448
467
|
// P1-4:会话等大分区改为内容寻址外置(写进共享 blobs/,快照只留引用)
|
|
449
468
|
await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
|
|
450
|
-
// git
|
|
451
|
-
//
|
|
452
|
-
|
|
453
|
-
const plain = snapshot.sections;
|
|
454
|
-
const blobSections = new Set(BLOB_SECTIONS);
|
|
455
|
-
for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
|
|
456
|
-
// 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
|
|
457
|
-
if (blobSections.has(sid))
|
|
458
|
-
continue;
|
|
459
|
-
const data = plain[sid];
|
|
460
|
-
if (data === undefined || data.files.length > 0)
|
|
461
|
-
continue;
|
|
462
|
-
// joinFs 只接受两个参数,占位路径拼进 prefix 一起传
|
|
463
|
-
await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
|
|
464
|
-
}
|
|
469
|
+
// git 不跟踪空目录:为空文件类分区目录写占位文件,保证远端保留目录
|
|
470
|
+
// (否则空分区(如未安装 skills)上传后目录在远端丢失,B 机全新 clone 下载即失败)。
|
|
471
|
+
await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
|
|
465
472
|
// 同 id 旧密文单文件残留 → 一并清理(形态切换不残留)
|
|
466
473
|
await fsx.remove(this.encryptedSnapshotFile(snapshot.id));
|
|
467
474
|
rel = `${this.relDir(SNAPSHOTS_REL)}/${snapshot.id}`;
|
|
468
475
|
}
|
|
476
|
+
// issue #141:`latest/` 派生视图与快照**进同一个 commit**(绝不在 push 之后补交一次)。
|
|
477
|
+
// 加密快照不写(没有明文镜像,引擎侧显式告警);远端已有更新视图时保持不动(不变量:永远指最新)。
|
|
478
|
+
const latestRel = !encrypted && await this.writeLatestMirror(snapshot, fsx) ? this.relDir(LATEST_REL) : null;
|
|
469
479
|
// blob 仓与快照同一次提交:引用文件与它引用的字节绝不跨 commit 分离
|
|
470
480
|
const hasBlobs = await fsx.exists(this.blobsDir());
|
|
471
|
-
|
|
481
|
+
const addPaths = [rel];
|
|
482
|
+
if (hasBlobs)
|
|
483
|
+
addPaths.push(this.relDir(BLOBS_REL));
|
|
484
|
+
if (latestRel !== null)
|
|
485
|
+
addPaths.push(latestRel);
|
|
486
|
+
await this.runGit(['add', '--', ...addPaths]);
|
|
472
487
|
const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
|
|
473
488
|
if (diff.code !== 0) {
|
|
474
489
|
const verb = existed ? 'update' : 'add';
|
|
@@ -477,6 +492,71 @@ export class GitTransport {
|
|
|
477
492
|
}
|
|
478
493
|
return computeSnapshotMeta(snapshot);
|
|
479
494
|
}
|
|
495
|
+
/**
|
|
496
|
+
* 空文件类分区的 git 占位文件(git 不跟踪空目录):
|
|
497
|
+
* 占位文件不参与 manifest.sectionHashes(基于传入数据计算);读回时按名+内容过滤。
|
|
498
|
+
*/
|
|
499
|
+
async writeEmptyFileSectionPlaceholders(dir, snapshot, fsx) {
|
|
500
|
+
const plain = snapshot.sections;
|
|
501
|
+
const blobSections = new Set(BLOB_SECTIONS);
|
|
502
|
+
for (const [sid, prefix] of Object.entries(SECTION_FILE_PREFIXES)) {
|
|
503
|
+
// 外置分区不写 <prefix>/ 目录(引用文件已在 writeSnapshotToDir 里写好),无需占位文件
|
|
504
|
+
if (blobSections.has(sid))
|
|
505
|
+
continue;
|
|
506
|
+
const data = plain[sid];
|
|
507
|
+
if (data === undefined || data.files.length > 0)
|
|
508
|
+
continue;
|
|
509
|
+
// joinFs 只接受两个参数,占位路径拼进 prefix 一起传
|
|
510
|
+
await fsx.writeFile(joinFs(dir, `${prefix}${SNAPSHOT_KEEP_FILE}`), new TextEncoder().encode(SNAPSHOT_KEEP_CONTENT));
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* 写 `latest/` 派生视图(issue #141):与 `snapshots/<id>/` 逐文件同布局,manifest 里保留**真实快照 id**
|
|
515
|
+
* (反向识别用)。返回本轮是否真的重建 —— false = 远端已有更新的视图,保持不动。
|
|
516
|
+
*
|
|
517
|
+
* 为什么按整份快照重写而不是拷目录:布局只有 layout.ts 一处权威,重写不会漏掉新增/删除的分区与占位文件;
|
|
518
|
+
* 外置分区只写引用文件(字节在共享 blobs/ 里按内容寻址,不重复落盘、不重复上传)。
|
|
519
|
+
*/
|
|
520
|
+
async writeLatestMirror(snapshot, fsx) {
|
|
521
|
+
const dir = this.latestDir();
|
|
522
|
+
const existing = await this.readDirManifest(dir);
|
|
523
|
+
// 不变量:latest/ 永远指向最新那份 —— out-of-order 上传(例如补齐更旧的快照)绝不把它回退成旧的
|
|
524
|
+
if (existing !== null && existing.createdAt > snapshot.createdAt)
|
|
525
|
+
return false;
|
|
526
|
+
// 存量工作副本的 sparse 面是旧版本建的(不含 latest/)⇒ 必须在写文件**之前**补齐 cone,否则 add 拒收
|
|
527
|
+
await this.ensureSparseConeCoversLatest();
|
|
528
|
+
await fsx.remove(dir); // 覆盖语义:先清旧视图,避免残留已下线的分区文件
|
|
529
|
+
await writeSnapshotToDir(snapshot, dir, fsx, { blobs: this.blobSink(fsx), sections: BLOB_SECTIONS });
|
|
530
|
+
await this.writeEmptyFileSectionPlaceholders(dir, snapshot, fsx);
|
|
531
|
+
return true;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* 存量工作副本的 sparse-checkout 面是**旧版本**建的(不含 `latest/`):cone 之外的路径 `git add` 会
|
|
535
|
+
* 拒绝入索引("outside of your sparse-checkout definition")⇒ 派生视图永远推不上去。
|
|
536
|
+
* 首次写 latest/ 前把 cone 补齐一次(克隆时 syncDirs() 已含它,新副本不会走到这里)。
|
|
537
|
+
*
|
|
538
|
+
* 非 sparse 副本(老 git 回落全量 clone)无需处理;读不出配置一律按「无需处理」走,绝不因优化失败。
|
|
539
|
+
*/
|
|
540
|
+
async ensureSparseConeCoversLatest() {
|
|
541
|
+
if (this.sparseConeCoversLatest)
|
|
542
|
+
return;
|
|
543
|
+
const flag = await this.runGit(['config', '--get', 'core.sparseCheckout'], { allowNonZero: true });
|
|
544
|
+
if (flag.code !== 0 || flag.stdout.trim() !== 'true') {
|
|
545
|
+
this.sparseConeCoversLatest = true;
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
const latestLine = `/${this.relDir(LATEST_REL)}/`;
|
|
549
|
+
const gitPath = await this.runGit(['rev-parse', '--git-path', 'info/sparse-checkout'], { allowNonZero: true });
|
|
550
|
+
const relPath = gitPath.code === 0 ? gitPath.stdout.trim() : '';
|
|
551
|
+
const abs = relPath === '' ? '' : path.isAbsolute(relPath) ? relPath : path.join(this.o.workDir, relPath);
|
|
552
|
+
const content = abs === '' ? '' : await fs.readFile(abs, 'utf8').catch(() => '');
|
|
553
|
+
if (content.split(/\r?\n/).some((line) => line.trim() === latestLine)) {
|
|
554
|
+
this.sparseConeCoversLatest = true;
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const res = await this.runGit(['sparse-checkout', 'set', '--cone', ...this.syncDirs()], { allowNonZero: true });
|
|
558
|
+
this.sparseConeCoversLatest = res.code === 0;
|
|
559
|
+
}
|
|
480
560
|
/** 下载快照完整载荷。明文 → 读回散文件目录;加密 → 读回密文单文件。不存在的 id 必须抛错(契约)。 */
|
|
481
561
|
async download(id) {
|
|
482
562
|
return this.withWorkdir(() => this.downloadLocked(id));
|
|
@@ -537,7 +617,13 @@ export class GitTransport {
|
|
|
537
617
|
await fsx.remove(encFile);
|
|
538
618
|
rel = `${this.relDir(SNAPSHOTS_ENCRYPTED_REL)}/${id}.json`;
|
|
539
619
|
}
|
|
540
|
-
|
|
620
|
+
// 不变量:latest/ 绝不指向不存在的快照 —— 删掉的正是它镜像的那份时,同一 commit 里一并移除视图
|
|
621
|
+
// (保留策略永远保住刚 push 的那份,正常裁剪走不到这里;这是用户显式删最新快照的兜底)。
|
|
622
|
+
const mirror = await this.readDirManifest(this.latestDir());
|
|
623
|
+
const dropLatest = mirror !== null && mirror.id === id;
|
|
624
|
+
if (dropLatest)
|
|
625
|
+
await fsx.remove(this.latestDir());
|
|
626
|
+
await this.runGit(dropLatest ? ['add', '-A', '--', rel, this.relDir(LATEST_REL)] : ['add', '-A', '--', rel]);
|
|
541
627
|
const diff = await this.runGit(['diff', '--cached', '--quiet'], { allowNonZero: true });
|
|
542
628
|
if (diff.code !== 0) {
|
|
543
629
|
await this.runGit(['commit', '-m', `sync: delete snapshot ${id}`]);
|
|
@@ -1073,17 +1159,47 @@ export class GitTransport {
|
|
|
1073
1159
|
if (unknownEntries.length > 0) {
|
|
1074
1160
|
throw new GitTransportError(this.msg('sync.git.adoptNotSyncRepo', { entries: unknownEntries.slice(0, 3).join(', ') }), { kind: 'protocol', retryable: false });
|
|
1075
1161
|
}
|
|
1076
|
-
// ③ 合并(不改写任何本地提交);冲突则 abort
|
|
1162
|
+
// ③ 合并(不改写任何本地提交);冲突则 abort 回起点再报错。
|
|
1163
|
+
// latest/ 是派生视图(issue #141):两条历史各有一份内容不同的 latest/ 时是 add/add 冲突,
|
|
1164
|
+
// 但它不该挡住换仓库 —— 派生层以远端为准继续合并,其它任何路径冲突仍整笔 abort。
|
|
1077
1165
|
const mergeRes = await this.runGit(['merge', '--allow-unrelated-histories', '--no-edit', `origin/${branch}`], { allowNonZero: true });
|
|
1078
1166
|
if (mergeRes.code !== 0) {
|
|
1079
|
-
await this.
|
|
1080
|
-
|
|
1167
|
+
const resolved = await this.resolveLatestOnlyMergeConflict();
|
|
1168
|
+
const committed = resolved ? await this.runGit(['commit', '--no-edit'], { allowNonZero: true }) : null;
|
|
1169
|
+
if (committed === null || committed.code !== 0) {
|
|
1170
|
+
await this.runGit(['merge', '--abort'], { allowNonZero: true });
|
|
1171
|
+
throw new GitTransportError(this.msg('sync.git.adoptMergeConflict', { branch }), { kind: 'conflict', retryable: false });
|
|
1172
|
+
}
|
|
1081
1173
|
}
|
|
1082
1174
|
// 让后续 pull --ff-only 有 upstream 可用(忽略失败:分支可能已被删/重命名)
|
|
1083
1175
|
await this.runGit(['branch', '--set-upstream-to', `origin/${branch}`, branch], { allowNonZero: true });
|
|
1084
1176
|
await this.writeLayoutMarker();
|
|
1085
1177
|
this.repoReady = true;
|
|
1086
1178
|
}
|
|
1179
|
+
/**
|
|
1180
|
+
* adopt 合并冲突里只放行 latest/(issue #141):派生视图两侧内容不同不算真冲突 ——
|
|
1181
|
+
* 以远端的版本收敛(合并方向 origin/<branch> 即 theirs),其它路径一律不放行。
|
|
1182
|
+
* 返回 false 表示冲突面不止 latest/,调用方必须 abort。
|
|
1183
|
+
*/
|
|
1184
|
+
async resolveLatestOnlyMergeConflict() {
|
|
1185
|
+
const unmerged = await this.runGit(['diff', '--name-only', '--diff-filter=U'], { allowNonZero: true });
|
|
1186
|
+
if (unmerged.code !== 0)
|
|
1187
|
+
return false;
|
|
1188
|
+
const paths = unmerged.stdout.split('\n').map((line) => line.trim()).filter((line) => line !== '');
|
|
1189
|
+
const latestRel = this.relDir(LATEST_REL);
|
|
1190
|
+
const inLatest = (rel) => rel === latestRel || rel.startsWith(`${latestRel}/`);
|
|
1191
|
+
if (paths.length === 0 || !paths.every(inLatest))
|
|
1192
|
+
return false;
|
|
1193
|
+
for (const rel of paths) {
|
|
1194
|
+
const takeTheirs = await this.runGit(['checkout', '--theirs', '--', rel], { allowNonZero: true });
|
|
1195
|
+
// modify/delete 冲突:theirs 里没有这个文件 = 远端已不再有视图,按删除收敛
|
|
1196
|
+
if (takeTheirs.code !== 0)
|
|
1197
|
+
await this.runGit(['rm', '-f', '--', rel], { allowNonZero: true });
|
|
1198
|
+
else
|
|
1199
|
+
await this.runGit(['add', '--', rel], { allowNonZero: true });
|
|
1200
|
+
}
|
|
1201
|
+
return true;
|
|
1202
|
+
}
|
|
1087
1203
|
/**
|
|
1088
1204
|
* pull --ff-only 同步远端;无本地提交(全新仓库)或无 upstream 时静默跳过。
|
|
1089
1205
|
* opts.retryable=true(仅 list / download 等**幂等读**路径传):瞬时网络故障走有限指数退避重试。
|
|
@@ -1368,11 +1484,11 @@ export class GitTransport {
|
|
|
1368
1484
|
return this.subdir === '' ? base : `${this.subdir}/${base}`;
|
|
1369
1485
|
}
|
|
1370
1486
|
/**
|
|
1371
|
-
*
|
|
1487
|
+
* 四个同步根目录(带前缀,含只读派生视图 latest/):sparse 面、add 路径、adopt 判据都从这里取 ——
|
|
1372
1488
|
* 任何一处漏加前缀都会让「写入 A、读取 B」(issue #131 的失败面)。
|
|
1373
1489
|
*/
|
|
1374
1490
|
syncDirs() {
|
|
1375
|
-
return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL)];
|
|
1491
|
+
return [this.relDir(SNAPSHOTS_REL), this.relDir(SNAPSHOTS_ENCRYPTED_REL), this.relDir(BLOBS_REL), this.relDir(LATEST_REL)];
|
|
1376
1492
|
}
|
|
1377
1493
|
blobsDir() {
|
|
1378
1494
|
return path.join(this.o.workDir, this.relDir(BLOBS_REL));
|
|
@@ -1471,6 +1587,10 @@ export class GitTransport {
|
|
|
1471
1587
|
snapshotsDir() {
|
|
1472
1588
|
return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_REL));
|
|
1473
1589
|
}
|
|
1590
|
+
/** 明文最新快照的只读派生视图目录(<workDir>/latest/;配了前缀则在 <subdir>/latest/) */
|
|
1591
|
+
latestDir() {
|
|
1592
|
+
return joinFs(this.o.workDir, this.relDir(LATEST_REL));
|
|
1593
|
+
}
|
|
1474
1594
|
/** 加密快照密文单文件目录(<workDir>/snapshots-encrypted/;配了前缀则在 <subdir>/ 下) */
|
|
1475
1595
|
encryptedSnapshotsDir() {
|
|
1476
1596
|
return joinFs(this.o.workDir, this.relDir(SNAPSHOTS_ENCRYPTED_REL));
|
|
@@ -98,10 +98,21 @@ export interface DeviceFlowEntry {
|
|
|
98
98
|
/** 过期时刻(epoch ms):now > expiresAt 视为已过期,get 返回 undefined 并清理 */
|
|
99
99
|
expiresAt: number;
|
|
100
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* 同一时刻允许驻留的设备码流上限。正常用户一次只开一个授权流程,这个数只在「反复重开」
|
|
103
|
+
* 或有人刷 start 端点时才会被顶到;有它才有内存上界(过期时间之外的第二重保证)。
|
|
104
|
+
*/
|
|
105
|
+
export declare const DEVICE_FLOW_LIMIT = 32;
|
|
101
106
|
/**
|
|
102
107
|
* 宿主内存设备码登记表(进程生命周期,跨请求共享 —— 与 RunRegistry 同模式)。
|
|
103
108
|
* device_code 的持有者是宿主:UI 只拿 flowId(随机 id),轮询时宿主凭 flowId 取回
|
|
104
|
-
* device_code 去 GitHub 换 token
|
|
109
|
+
* device_code 去 GitHub 换 token。
|
|
110
|
+
*
|
|
111
|
+
* audit-sync sync-P6-6(P3):清理不能只挂在 get() 上 —— 用户开了授权页又直接关掉时,流程
|
|
112
|
+
* 从此不会有人轮询,那条含 device_code 的过期条目会永久驻留。所以**写入路径也清扫**:
|
|
113
|
+
* set() 先淘汰全部过期条目(now > expiresAt),再把总数压到 DEVICE_FLOW_LIMIT 以内
|
|
114
|
+
* (超出时淘汰最早过期的那条 = 最接近失效、最没价值的一条)。get() 的惰性清理保留,
|
|
115
|
+
* 任何时刻表的大小都不超过该上限。
|
|
105
116
|
*/
|
|
106
117
|
export declare class DeviceFlowStore {
|
|
107
118
|
private readonly flows;
|
|
@@ -110,6 +121,11 @@ export declare class DeviceFlowStore {
|
|
|
110
121
|
get size(): number;
|
|
111
122
|
/** 登记一个设备码流;flowId 由调用方生成(随机,不可猜) */
|
|
112
123
|
set(flowId: string, entry: DeviceFlowEntry): void;
|
|
124
|
+
/**
|
|
125
|
+
* 清扫:先删全部过期条目,再在容量满时淘汰最早过期者(保证 set 之后 size <= DEVICE_FLOW_LIMIT)。
|
|
126
|
+
* 先扫过期是关键 —— 否则上限会去淘汰仍在有效期内的流程,把用户的正常授权踢掉。
|
|
127
|
+
*/
|
|
128
|
+
private sweep;
|
|
113
129
|
/** 取回设备码流;过期条目视为不存在并立即清理 */
|
|
114
130
|
get(flowId: string): DeviceFlowEntry | undefined;
|
|
115
131
|
/** 主动移除(取消/完成后清理) */
|
package/lib/sync/github-auth.js
CHANGED
|
@@ -183,10 +183,21 @@ export class GitHubAuthClient {
|
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* 同一时刻允许驻留的设备码流上限。正常用户一次只开一个授权流程,这个数只在「反复重开」
|
|
188
|
+
* 或有人刷 start 端点时才会被顶到;有它才有内存上界(过期时间之外的第二重保证)。
|
|
189
|
+
*/
|
|
190
|
+
export const DEVICE_FLOW_LIMIT = 32;
|
|
186
191
|
/**
|
|
187
192
|
* 宿主内存设备码登记表(进程生命周期,跨请求共享 —— 与 RunRegistry 同模式)。
|
|
188
193
|
* device_code 的持有者是宿主:UI 只拿 flowId(随机 id),轮询时宿主凭 flowId 取回
|
|
189
|
-
* device_code 去 GitHub 换 token
|
|
194
|
+
* device_code 去 GitHub 换 token。
|
|
195
|
+
*
|
|
196
|
+
* audit-sync sync-P6-6(P3):清理不能只挂在 get() 上 —— 用户开了授权页又直接关掉时,流程
|
|
197
|
+
* 从此不会有人轮询,那条含 device_code 的过期条目会永久驻留。所以**写入路径也清扫**:
|
|
198
|
+
* set() 先淘汰全部过期条目(now > expiresAt),再把总数压到 DEVICE_FLOW_LIMIT 以内
|
|
199
|
+
* (超出时淘汰最早过期的那条 = 最接近失效、最没价值的一条)。get() 的惰性清理保留,
|
|
200
|
+
* 任何时刻表的大小都不超过该上限。
|
|
190
201
|
*/
|
|
191
202
|
export class DeviceFlowStore {
|
|
192
203
|
flows = new Map();
|
|
@@ -199,8 +210,33 @@ export class DeviceFlowStore {
|
|
|
199
210
|
}
|
|
200
211
|
/** 登记一个设备码流;flowId 由调用方生成(随机,不可猜) */
|
|
201
212
|
set(flowId, entry) {
|
|
213
|
+
this.sweep();
|
|
202
214
|
this.flows.set(flowId, entry);
|
|
203
215
|
}
|
|
216
|
+
/**
|
|
217
|
+
* 清扫:先删全部过期条目,再在容量满时淘汰最早过期者(保证 set 之后 size <= DEVICE_FLOW_LIMIT)。
|
|
218
|
+
* 先扫过期是关键 —— 否则上限会去淘汰仍在有效期内的流程,把用户的正常授权踢掉。
|
|
219
|
+
*/
|
|
220
|
+
sweep() {
|
|
221
|
+
const now = this.now();
|
|
222
|
+
for (const [flowId, entry] of this.flows) {
|
|
223
|
+
if (now > entry.expiresAt)
|
|
224
|
+
this.flows.delete(flowId);
|
|
225
|
+
}
|
|
226
|
+
while (this.flows.size >= DEVICE_FLOW_LIMIT) {
|
|
227
|
+
let oldestId;
|
|
228
|
+
let oldestAt = Number.POSITIVE_INFINITY;
|
|
229
|
+
for (const [flowId, entry] of this.flows) {
|
|
230
|
+
if (entry.expiresAt < oldestAt) {
|
|
231
|
+
oldestAt = entry.expiresAt;
|
|
232
|
+
oldestId = flowId;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (oldestId === undefined)
|
|
236
|
+
return; // 理论不可达:size >= 1 时必能挑出一条
|
|
237
|
+
this.flows.delete(oldestId);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
204
240
|
/** 取回设备码流;过期条目视为不存在并立即清理 */
|
|
205
241
|
get(flowId) {
|
|
206
242
|
const entry = this.flows.get(flowId);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ConfigAdapter, HostContext, ImportDecisions, ImportPlan, PathMapping, Snapshot, SnapshotStore } from '../core/types.ts';
|
|
2
|
+
import type { ReviewQueueItem } from './review-queue.ts';
|
|
3
|
+
export interface PlanMergeApplyInput {
|
|
4
|
+
/** 本次合成的临时 ZIP(合并结果)。 */
|
|
5
|
+
zipPath: string;
|
|
6
|
+
/** 合并路径的映射(merge() 记下的远端 sourceHome + 用户映射,issue #45)。 */
|
|
7
|
+
pathMappings: PathMapping[];
|
|
8
|
+
/** 本次要应用的分区(autoApply + 伴随分区)。 */
|
|
9
|
+
appliedIds: readonly string[];
|
|
10
|
+
/** 复用宿主 Importer 的规划入口(纯计算,零写入)。 */
|
|
11
|
+
createImportPlan: (zipPath: string, decisions: ImportDecisions) => Promise<ImportPlan>;
|
|
12
|
+
/** 待裁决项的告警文案(i18n 留在调用方,键 `sync.mergeHeldConflicts`)。 */
|
|
13
|
+
heldConflictMessage: (count: number, sections: string[]) => string;
|
|
14
|
+
}
|
|
15
|
+
export interface MergeApplyPlan {
|
|
16
|
+
plan: ImportPlan;
|
|
17
|
+
/** 叶级未裁决项的报告面:只给 id/分区/描述,**不带值**(值可能含敏感叶子)。 */
|
|
18
|
+
heldReview: ReviewQueueItem[];
|
|
19
|
+
/** 人读告警(空数组 = 没有待裁决项)。 */
|
|
20
|
+
heldWarnings: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare function planMergeApply(input: PlanMergeApplyInput): Promise<MergeApplyPlan>;
|
|
23
|
+
export type MergeApplySnapshotResult = {
|
|
24
|
+
ok: true;
|
|
25
|
+
store: SnapshotStore;
|
|
26
|
+
snapshot: Snapshot;
|
|
27
|
+
} | {
|
|
28
|
+
ok: false;
|
|
29
|
+
error: unknown;
|
|
30
|
+
};
|
|
31
|
+
/** 执行前兜底快照(拿到 restoreId 给 UI 一键回滚);失败时把原始错误交回调用方决定文案。 */
|
|
32
|
+
export declare function createMergeApplySnapshot(input: {
|
|
33
|
+
ctx: HostContext;
|
|
34
|
+
plan: ImportPlan;
|
|
35
|
+
zipPath: string;
|
|
36
|
+
storeDir: string;
|
|
37
|
+
adapters: ConfigAdapter[];
|
|
38
|
+
}): Promise<MergeApplySnapshotResult>;
|