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
package/src/core/model-tools.ts
CHANGED
|
@@ -80,8 +80,8 @@ export interface ModelToolsDeps {
|
|
|
80
80
|
syncDir: string
|
|
81
81
|
/**
|
|
82
82
|
* SyncEngine 工厂(git/webdav 按配置分支构造传输;与 host 路由同一来源)。
|
|
83
|
-
* D5:agent 侧按工具决定 optInSections
|
|
84
|
-
*
|
|
83
|
+
* D5:agent 侧按工具决定 optInSections —— 推送 / 拉取 / 预览 / 合并一律带用户放行的 'auto' 档,
|
|
84
|
+
* 再剔除 sessions(与 docs/architecture/agent-tools.md 的工具面口径同一份事实)。
|
|
85
85
|
*/
|
|
86
86
|
makeSyncEngine: (cfg: SyncConfig, opts?: { optInSections?: readonly SectionId[] }) => SyncEngine
|
|
87
87
|
/** 插件版本(manifest.exporter.version) */
|
|
@@ -163,13 +163,20 @@ async function resolveSyncTarget(
|
|
|
163
163
|
return { cfg, channel: ch }
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
-
/**
|
|
166
|
+
/**
|
|
167
|
+
* 解析同步引擎(推送 / 拉取)。
|
|
168
|
+
* D5:与 preview/apply 同一口径 —— 引擎构造期必须拿到用户放行的 'auto' 档可选分区,
|
|
169
|
+
* 否则显式传 mcp 会在 pushTargets 里被判成非 portable 并 skipNonPortable(issue #138 ④)。
|
|
170
|
+
*/
|
|
167
171
|
async function resolveEngine(
|
|
168
172
|
deps: ModelToolsDeps,
|
|
169
173
|
channel?: SyncTransportType,
|
|
170
174
|
): Promise<{ engine: SyncEngine; channel: SyncTransportType }> {
|
|
171
175
|
const target = await resolveSyncTarget(deps, channel)
|
|
172
|
-
return {
|
|
176
|
+
return {
|
|
177
|
+
engine: deps.makeSyncEngine(target.cfg, { optInSections: agentOptInSections(deps, target.channel) }),
|
|
178
|
+
channel: target.channel,
|
|
179
|
+
}
|
|
173
180
|
}
|
|
174
181
|
|
|
175
182
|
/* ------------------------------------------------------------ 结构化结论与摘要 */
|
|
@@ -594,6 +601,11 @@ export function createModelTools(deps: ModelToolsDeps) {
|
|
|
594
601
|
// 否则模型会把它当成「推送成功但内容为空」继续往下走。
|
|
595
602
|
...(report.conflict === true ? { conflict: true } : {}),
|
|
596
603
|
...(report.adoptedBase === undefined ? {} : { adoptedBase: report.adoptedBase }),
|
|
604
|
+
// issue #140:内容与远端最新一致 ⇒ 引擎什么都没写(不 upload、不建目录)。
|
|
605
|
+
// 不透传的话模型会把「ok:true + snapshotId」读成「又推了一份新快照」。
|
|
606
|
+
...(report.skipped === 'unchanged'
|
|
607
|
+
? { skipped: 'unchanged' as const, adoptedSnapshotId: report.adoptedSnapshotId ?? report.snapshotId }
|
|
608
|
+
: {}),
|
|
597
609
|
...(report.message === undefined ? {} : { message: report.message }),
|
|
598
610
|
...(encrypt ? { passwordSource: stored === undefined ? 'argument' : 'stored' } : {}),
|
|
599
611
|
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* issue #136 A/C:patch 行写回守卫的纯逻辑测试。
|
|
3
|
+
*
|
|
4
|
+
* 三条必须钉住的事实:
|
|
5
|
+
* - 载荷声明了「被剥空」的路径 + 本机同叶子非空 → 保留本机值,绝不写空;
|
|
6
|
+
* - 载荷给的是**真实非空新值** → 照常写入(守卫不得挡住远端新配置);
|
|
7
|
+
* - 只有逐行显式确认(confirmed)才允许清空;旧载荷(无声明路径)走保守分支。
|
|
8
|
+
*/
|
|
9
|
+
import test from 'node:test';
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import { applyPatchSecretGuard, __test } from './patch-secret-guard.ts';
|
|
12
|
+
|
|
13
|
+
test('#136 守卫:声明路径 + 导入空 + 本机非空 → 保留本机值(不写空)', () => {
|
|
14
|
+
const r = applyPatchSecretGuard({
|
|
15
|
+
importedRaw: { id: 'gate', config: { apiKey: '' } },
|
|
16
|
+
localRaw: { id: 'gate', config: { apiKey: 'sk-local-plain' } },
|
|
17
|
+
redactedPaths: ['raw.config.apiKey'],
|
|
18
|
+
confirmed: false,
|
|
19
|
+
});
|
|
20
|
+
assert.deepEqual(r.raw, { id: 'gate', config: { apiKey: 'sk-local-plain' } }, '被剥空的叶子必须换回本机值');
|
|
21
|
+
assert.deepEqual(r.kept, ['raw.config.apiKey'], '报告只带路径,不带值');
|
|
22
|
+
assert.deepEqual(r.cleared, []);
|
|
23
|
+
assert.equal(r.conservative, false, '有声明路径 → 非保守分支');
|
|
24
|
+
// 报告面(kept/cleared)只有路径:这是能直接进事务报告/日志的部分(raw 本身是写盘载荷,必然含本机值)
|
|
25
|
+
assert.equal(JSON.stringify({ kept: r.kept, cleared: r.cleared }).includes('sk-local-plain'), false, '报告里不得出现值本身');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('#136 守卫:声明路径 + 导入真实非空新值 → 照常写入(不回归远端更新)', () => {
|
|
29
|
+
const r = applyPatchSecretGuard({
|
|
30
|
+
importedRaw: { id: 'gate', config: { apiKey: 'sk-remote-new' } },
|
|
31
|
+
localRaw: { id: 'gate', config: { apiKey: 'sk-local-old' } },
|
|
32
|
+
redactedPaths: ['raw.config.apiKey'],
|
|
33
|
+
confirmed: false,
|
|
34
|
+
});
|
|
35
|
+
assert.deepEqual(r.raw, { id: 'gate', config: { apiKey: 'sk-remote-new' } }, '远端真值必须落地');
|
|
36
|
+
assert.deepEqual(r.kept, []);
|
|
37
|
+
assert.deepEqual(r.cleared, []);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('#136 守卫:逐行显式确认(confirmed)→ 允许清空,并把「已清空」写进结果', () => {
|
|
41
|
+
const r = applyPatchSecretGuard({
|
|
42
|
+
importedRaw: { id: 'gate', config: { apiKey: '' } },
|
|
43
|
+
localRaw: { id: 'gate', config: { apiKey: 'sk-local-plain' } },
|
|
44
|
+
redactedPaths: ['raw.config.apiKey'],
|
|
45
|
+
confirmed: true,
|
|
46
|
+
});
|
|
47
|
+
assert.deepEqual(r.raw, { id: 'gate', config: { apiKey: '' } }, '确认后按导入值写空');
|
|
48
|
+
assert.deepEqual(r.cleared, ['raw.config.apiKey']);
|
|
49
|
+
assert.deepEqual(r.kept, []);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('#136 守卫:旧载荷(无 redactedPaths)→ 保守分支:空串 + 本机非空即保留 + warning 路径', () => {
|
|
53
|
+
const r = applyPatchSecretGuard({
|
|
54
|
+
importedRaw: { id: 'gate', nested: { token: '' }, keep: 'same' },
|
|
55
|
+
localRaw: { id: 'gate', nested: { token: 'plain-token' }, keep: 'same' },
|
|
56
|
+
confirmed: false,
|
|
57
|
+
});
|
|
58
|
+
assert.equal(r.conservative, true, '缺字段 = 未知 → 保守');
|
|
59
|
+
assert.deepEqual(r.raw, { id: 'gate', nested: { token: 'plain-token' }, keep: 'same' });
|
|
60
|
+
assert.deepEqual(r.kept, ['raw.nested.token']);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('#136 守卫:旧载荷保守分支也允许在显式确认后清空(不把「用户主动清空」永久锁死)', () => {
|
|
64
|
+
const r = applyPatchSecretGuard({
|
|
65
|
+
importedRaw: { id: 'gate', token: '' },
|
|
66
|
+
localRaw: { id: 'gate', token: 'plain-token' },
|
|
67
|
+
confirmed: true,
|
|
68
|
+
});
|
|
69
|
+
assert.equal(r.conservative, true);
|
|
70
|
+
assert.deepEqual(r.raw, { id: 'gate', token: '' });
|
|
71
|
+
assert.deepEqual(r.cleared, ['raw.token']);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('#136 守卫:本机没有这一行(Create)→ 原样返回,不凭空造保护', () => {
|
|
75
|
+
const imported = { id: 'new-line', token: '' };
|
|
76
|
+
const r = applyPatchSecretGuard({ importedRaw: imported, localRaw: undefined, confirmed: false });
|
|
77
|
+
assert.deepEqual(r.raw, imported);
|
|
78
|
+
assert.deepEqual(r.kept, []);
|
|
79
|
+
assert.equal(r.conservative, false);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('#136 守卫:数组路径(扫描器语法 a.b[0].c)逐段解析与展示', () => {
|
|
83
|
+
assert.deepEqual(__test.parsePath('raw.mcp.args[2].token'), ['raw', 'mcp', 'args', 2, 'token']);
|
|
84
|
+
assert.equal(__test.formatPath(['raw', 'mcp', 'args', 2, 'token']), 'raw.mcp.args[2].token');
|
|
85
|
+
const r = applyPatchSecretGuard({
|
|
86
|
+
importedRaw: { mcp: { args: [{ token: 'a' }, { token: '' }] } },
|
|
87
|
+
localRaw: { mcp: { args: [{ token: 'a' }, { token: 'local-b' }] } },
|
|
88
|
+
redactedPaths: ['raw.mcp.args[1].token'],
|
|
89
|
+
confirmed: false,
|
|
90
|
+
});
|
|
91
|
+
assert.equal((r.raw as { mcp: { args: { token: string }[] } }).mcp.args[1]?.token, 'local-b');
|
|
92
|
+
assert.deepEqual(r.kept, ['raw.mcp.args[1].token']);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('#136 守卫:语法不认识的声明路径 → 宁可放过(不误判、不崩)', () => {
|
|
96
|
+
const imported = { a: '' };
|
|
97
|
+
const r = applyPatchSecretGuard({
|
|
98
|
+
importedRaw: imported, localRaw: { a: 'local' }, redactedPaths: ['[[bad'], confirmed: false,
|
|
99
|
+
});
|
|
100
|
+
assert.deepEqual(r.raw, imported);
|
|
101
|
+
assert.deepEqual(r.kept, []);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('#136 守卫:本机同叶子为空 → 无保护对象(导入空仍写空,值确实一致)', () => {
|
|
105
|
+
const r = applyPatchSecretGuard({
|
|
106
|
+
importedRaw: { token: '' }, localRaw: { token: '' }, redactedPaths: ['raw.token'], confirmed: false,
|
|
107
|
+
});
|
|
108
|
+
assert.deepEqual(r.raw, { token: '' });
|
|
109
|
+
assert.deepEqual(r.kept, []);
|
|
110
|
+
});
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* patch 行写回守卫(issue #136 A + C)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:导出侧 SecretScanner 会把命中字段名/值形状的字符串叶子替换成空串(占位符),语义是
|
|
5
|
+
* 「需补录」。同步/导入落盘时,若照抄这个空串,就会**静默**抹掉本机同一行里用户手写的明文
|
|
6
|
+
* —— 行是整行写回的,行内没有粒度。
|
|
7
|
+
*
|
|
8
|
+
* 本模块是那条「最后一个能拒绝写入」的纯逻辑:比对导入值与本机当前行,把「被脱敏剥空」的
|
|
9
|
+
* 叶子换回本机值;只有调用方显式声明逐行确认(`confirmed`)才允许真的写空。
|
|
10
|
+
*
|
|
11
|
+
* 两条分支(issue #136 C 的载荷字段决定):
|
|
12
|
+
* - `redactedPaths` 存在(新载荷):只保护**声明过的**路径,精确、可解释;
|
|
13
|
+
* - 缺省(旧载荷 / 未知):保守口径 —— 导入值为空串且本机同叶子非空即保留本机值。
|
|
14
|
+
* 绝不反过来假定「空值一定来自用户主动清空」(旧载荷没有能力区分剥空与真空)。
|
|
15
|
+
*
|
|
16
|
+
* 纯函数:不读盘、不打日志、不抛错;返回值不含任何值本身(只有路径),可直接进报告。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** 路径段(对象键或数组下标)。 */
|
|
20
|
+
type Segment = string | number;
|
|
21
|
+
|
|
22
|
+
export interface PatchSecretGuardInput {
|
|
23
|
+
/** 备份/远端载荷里的整行值(可能含被剥空的叶子)。 */
|
|
24
|
+
importedRaw: unknown;
|
|
25
|
+
/** 本机当前同一行的值;`undefined` = 本机没有这一行(Create 语义,无可保护对象)。 */
|
|
26
|
+
localRaw: unknown;
|
|
27
|
+
/** 载荷声明的剥空路径(行内相对,如 `raw.mcp.args.token`);缺省 = 未知(旧载荷)。 */
|
|
28
|
+
redactedPaths?: readonly string[] | undefined;
|
|
29
|
+
/** 调用方已拿到对该行的**逐行显式确认** → 允许写空。 */
|
|
30
|
+
confirmed: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface PatchSecretGuardResult {
|
|
34
|
+
/** 实际应写入的行值(受保护叶子已换回本机值)。 */
|
|
35
|
+
raw: unknown;
|
|
36
|
+
/** 被保留的本机值路径(行内相对,展示用)。 */
|
|
37
|
+
kept: string[];
|
|
38
|
+
/** 逐行确认后真的写空的路径(同一份事实进报告)。 */
|
|
39
|
+
cleared: string[];
|
|
40
|
+
/** true = 载荷没带剥空路径,走的是保守推断分支。 */
|
|
41
|
+
conservative: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 解析扫描器路径语法(`a.b[0].c`)→ 路径段;语法不认识返回 null(宁可放过,不误判)。 */
|
|
45
|
+
function parsePath(path: string): Segment[] | null {
|
|
46
|
+
const segs: Segment[] = [];
|
|
47
|
+
let rest = path;
|
|
48
|
+
while (rest !== '') {
|
|
49
|
+
const m = /^([^.[\]]+)((?:\[\d+\])*)/.exec(rest);
|
|
50
|
+
if (m === null) return null;
|
|
51
|
+
const head = m[1];
|
|
52
|
+
const tail = m[2] ?? '';
|
|
53
|
+
if (head === undefined) return null;
|
|
54
|
+
segs.push(head);
|
|
55
|
+
for (const hit of tail.matchAll(/\[(\d+)\]/g)) {
|
|
56
|
+
const idx = hit[1];
|
|
57
|
+
if (idx !== undefined) segs.push(Number(idx));
|
|
58
|
+
}
|
|
59
|
+
rest = rest.slice(m[0].length);
|
|
60
|
+
if (rest === '') break;
|
|
61
|
+
if (!rest.startsWith('.')) return null;
|
|
62
|
+
rest = rest.slice(1);
|
|
63
|
+
}
|
|
64
|
+
return segs.length > 0 ? segs : null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 路径段 → 展示用相对路径(与扫描器语法同形,如 `raw.mcp.args.token`)。 */
|
|
68
|
+
function formatPath(segs: readonly Segment[]): string {
|
|
69
|
+
let out = '';
|
|
70
|
+
for (const seg of segs) {
|
|
71
|
+
if (typeof seg === 'number') out += `[${seg}]`;
|
|
72
|
+
else out += out === '' ? seg : `.${seg}`;
|
|
73
|
+
}
|
|
74
|
+
return out;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function getAt(root: unknown, segs: readonly Segment[]): unknown {
|
|
78
|
+
let cur = root;
|
|
79
|
+
for (const seg of segs) {
|
|
80
|
+
if (typeof seg === 'number') {
|
|
81
|
+
if (!Array.isArray(cur)) return undefined;
|
|
82
|
+
cur = cur[seg];
|
|
83
|
+
} else {
|
|
84
|
+
if (cur === null || typeof cur !== 'object' || Array.isArray(cur)) return undefined;
|
|
85
|
+
cur = (cur as Record<string, unknown>)[seg];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return cur;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** 深拷贝后按路径写回(只在确有保护项时调用,避免无谓的整行拷贝)。 */
|
|
92
|
+
function setAt(root: unknown, segs: readonly Segment[], value: unknown): unknown {
|
|
93
|
+
if (segs.length === 0) return value;
|
|
94
|
+
const clone = structuredClone(root);
|
|
95
|
+
let cur: Record<string, unknown> | unknown[] = clone as Record<string, unknown>;
|
|
96
|
+
for (let i = 0; i < segs.length - 1; i++) {
|
|
97
|
+
const next = Array.isArray(cur)
|
|
98
|
+
? (cur as unknown[])[segs[i] as number]
|
|
99
|
+
: (cur as Record<string, unknown>)[segs[i] as string];
|
|
100
|
+
if (next === null || typeof next !== 'object') return clone; // 形状不符 → 不猜,原样返回
|
|
101
|
+
cur = next as Record<string, unknown>;
|
|
102
|
+
}
|
|
103
|
+
const last = segs[segs.length - 1];
|
|
104
|
+
if (Array.isArray(cur)) (cur as unknown[])[last as number] = value;
|
|
105
|
+
else (cur as Record<string, unknown>)[last as string] = value;
|
|
106
|
+
return clone;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 本机值是否「非空」:只有非空字符串算(脱敏占位符恒为空串,故此判据与载荷同口径)。 */
|
|
110
|
+
function isNonEmptyLocal(value: unknown): value is string {
|
|
111
|
+
return typeof value === 'string' && value !== '';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 保守分支:逐叶子找出「导入空串 + 本机非空」的路径(旧载荷没有剥空信息时的唯一线索)。 */
|
|
115
|
+
function collectSuspects(imported: unknown, local: unknown, segs: Segment[], out: Segment[][]): void {
|
|
116
|
+
if (imported === null || typeof imported !== 'object') {
|
|
117
|
+
if (imported === '' && isNonEmptyLocal(local)) out.push([...segs]);
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (Array.isArray(imported)) {
|
|
121
|
+
for (let i = 0; i < imported.length; i++) {
|
|
122
|
+
collectSuspects(imported[i], Array.isArray(local) ? local[i] : undefined, [...segs, i], out);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const obj = imported as Record<string, unknown>;
|
|
127
|
+
const localObj = local !== null && typeof local === 'object' && !Array.isArray(local)
|
|
128
|
+
? (local as Record<string, unknown>)
|
|
129
|
+
: undefined;
|
|
130
|
+
for (const [k, v] of Object.entries(obj)) collectSuspects(v, localObj?.[k], [...segs, k], out);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function applyPatchSecretGuard(input: PatchSecretGuardInput): PatchSecretGuardResult {
|
|
134
|
+
const { importedRaw, localRaw, redactedPaths, confirmed } = input;
|
|
135
|
+
// 本机没有这一行 → 没有可覆盖的东西(Create 语义),原样返回。
|
|
136
|
+
if (localRaw === undefined) return { raw: importedRaw, kept: [], cleared: [], conservative: false };
|
|
137
|
+
|
|
138
|
+
const conservative = redactedPaths === undefined || redactedPaths.length === 0;
|
|
139
|
+
/** 候选:相对路径段(不含 `raw` 前缀;空数组 = 整行值本身)。 */
|
|
140
|
+
const candidates: { segs: Segment[]; declared: boolean }[] = [];
|
|
141
|
+
if (conservative) {
|
|
142
|
+
const found: Segment[][] = [];
|
|
143
|
+
collectSuspects(importedRaw, localRaw, [], found);
|
|
144
|
+
for (const segs of found) candidates.push({ segs, declared: false });
|
|
145
|
+
} else {
|
|
146
|
+
for (const path of redactedPaths) {
|
|
147
|
+
const segs = parsePath(path);
|
|
148
|
+
if (segs === null) continue;
|
|
149
|
+
// 载荷里的路径是相对**行对象**(`{ file, lineId, raw }`)的;行内相对只剩 `raw` 之后的部分。
|
|
150
|
+
const rel = segs[0] === 'raw' ? segs.slice(1) : segs;
|
|
151
|
+
candidates.push({ segs: rel, declared: true });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const kept: string[] = [];
|
|
156
|
+
const cleared: string[] = [];
|
|
157
|
+
let raw = importedRaw;
|
|
158
|
+
for (const cand of candidates) {
|
|
159
|
+
const localVal = getAt(localRaw, cand.segs);
|
|
160
|
+
if (!isNonEmptyLocal(localVal)) continue;
|
|
161
|
+
const importedVal = getAt(importedRaw, cand.segs);
|
|
162
|
+
// 声明路径上出现了非空导入值 = 载荷自相矛盾(或值形状命中后被上游改回):以导入值为准,
|
|
163
|
+
// 绝不因为声明了路径就把远端真实新值挡掉(issue #136 验收 3)。
|
|
164
|
+
if (importedVal === '' || importedVal === undefined) {
|
|
165
|
+
const rel = formatPath(['raw', ...cand.segs]);
|
|
166
|
+
if (confirmed) cleared.push(rel);
|
|
167
|
+
else {
|
|
168
|
+
kept.push(rel);
|
|
169
|
+
raw = setAt(raw, cand.segs, localVal);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return { raw, kept, cleared, conservative };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export const __test = { parsePath, formatPath };
|
package/src/core/reconcile.ts
CHANGED
|
@@ -89,6 +89,12 @@ export interface ReconcileOutcome {
|
|
|
89
89
|
safeModeRequired: boolean;
|
|
90
90
|
/** unresolved(未到 terminal)的 incomplete op id(RECOVERY_REQUIRED 候选)。 */
|
|
91
91
|
unresolved: string[];
|
|
92
|
+
/**
|
|
93
|
+
* 「owner 仍活着(live)但 journal 本身还没到 terminal」的 op id(审计 P3-8)。
|
|
94
|
+
* 与 unresolved 的区别:它们不该进 recovery(执行方还在跑),但也**不是已收敛**。
|
|
95
|
+
* 锁被另一实例持有时启动分类据此产出 LOCKED_LIVE,而不是把「确实在跑」误报成 NORMAL。
|
|
96
|
+
*/
|
|
97
|
+
liveIncomplete: string[];
|
|
92
98
|
}
|
|
93
99
|
|
|
94
100
|
/**
|
|
@@ -154,6 +160,7 @@ export async function reconcileActive(
|
|
|
154
160
|
): Promise<ReconcileOutcome> {
|
|
155
161
|
const decisions: ReconcileDecision[] = [];
|
|
156
162
|
const unresolved: string[] = [];
|
|
163
|
+
const liveIncomplete: string[] = [];
|
|
157
164
|
let safeModeRequired = false;
|
|
158
165
|
|
|
159
166
|
const activeIds = await store.scanActive();
|
|
@@ -162,8 +169,9 @@ export async function reconcileActive(
|
|
|
162
169
|
decisions.push(outcome.decision);
|
|
163
170
|
if (outcome.safeMode) safeModeRequired = true;
|
|
164
171
|
if (outcome.unresolved) unresolved.push(operationId);
|
|
172
|
+
if (outcome.liveIncomplete === true) liveIncomplete.push(operationId);
|
|
165
173
|
}
|
|
166
|
-
return { decisions, safeModeRequired, unresolved };
|
|
174
|
+
return { decisions, safeModeRequired, unresolved, liveIncomplete };
|
|
167
175
|
}
|
|
168
176
|
|
|
169
177
|
async function reconcileOne(
|
|
@@ -172,7 +180,13 @@ async function reconcileOne(
|
|
|
172
180
|
env: ReconcileEnv,
|
|
173
181
|
opts: ReconcileOptions,
|
|
174
182
|
operationId: string,
|
|
175
|
-
): Promise<{
|
|
183
|
+
): Promise<{
|
|
184
|
+
decision: ReconcileDecision;
|
|
185
|
+
safeMode: boolean;
|
|
186
|
+
unresolved: boolean;
|
|
187
|
+
/** live owner 但 journal 未到 terminal(仅 live 分支会置 true;审计 P3-8) */
|
|
188
|
+
liveIncomplete?: boolean;
|
|
189
|
+
}> {
|
|
176
190
|
const j = await store.loadActive(operationId);
|
|
177
191
|
if (j === null) {
|
|
178
192
|
// 存在但 parse/校验失败 → corrupt → quarantine
|
|
@@ -188,7 +202,14 @@ async function reconcileOne(
|
|
|
188
202
|
|
|
189
203
|
// live owner 检查
|
|
190
204
|
if (await env.isLiveOwner(j)) {
|
|
191
|
-
|
|
205
|
+
// P3-8:live 只说明「有人还在跑」,不等于这条 journal 已收敛。未到 terminal 的 live journal
|
|
206
|
+
// 必须如实上报(liveIncomplete),否则 LOCKED 下的启动分类会把「确实在跑」误判成 NORMAL。
|
|
207
|
+
return {
|
|
208
|
+
decision: { operationId, kind: 'live', reason: engineMsg('core.reconcile.ownerLive'), snapshotId: j.snapshotId },
|
|
209
|
+
safeMode: false,
|
|
210
|
+
unresolved: false,
|
|
211
|
+
liveIncomplete: !isTerminalState(j.state),
|
|
212
|
+
};
|
|
192
213
|
}
|
|
193
214
|
|
|
194
215
|
// P1-A:ownership binding 强制校验(显式 recovery 捕获的 crashed ownership 证据)。
|
|
@@ -530,6 +551,8 @@ export interface StartupInspection {
|
|
|
530
551
|
recoveryRequired: boolean;
|
|
531
552
|
decisions: ReconcileDecision[];
|
|
532
553
|
unresolved: string[];
|
|
554
|
+
/** live owner 持有但 journal 未到 terminal 的 op id(P3-8:LOCKED_LIVE 的判据) */
|
|
555
|
+
liveIncomplete: string[];
|
|
533
556
|
/**
|
|
534
557
|
* durable SAFE MODE 标记的三态判定(t37):'blocked' / 'clear' / **'unknown'**。
|
|
535
558
|
* 单独暴露的原因:`safeModeRequired` 是聚合结论(还可能因 corrupt journal / needs-attention 为 true),
|
|
@@ -576,6 +599,11 @@ export async function inspectStartup(
|
|
|
576
599
|
const durableSafe = durableSafeState !== 'clear';
|
|
577
600
|
const safeModeRequired = outcome.safeModeRequired || recoveryRequired || durableSafe;
|
|
578
601
|
return {
|
|
579
|
-
safeModeRequired,
|
|
602
|
+
safeModeRequired,
|
|
603
|
+
recoveryRequired,
|
|
604
|
+
decisions: outcome.decisions,
|
|
605
|
+
unresolved: outcome.unresolved,
|
|
606
|
+
liveIncomplete: outcome.liveIncomplete,
|
|
607
|
+
durableSafeState,
|
|
580
608
|
};
|
|
581
609
|
}
|
package/src/core/reinstall.ts
CHANGED
|
@@ -172,9 +172,11 @@ export function copyDirCommand(
|
|
|
172
172
|
const s = isWindows(platform) ? psQuote(src) : shellQuote(src)
|
|
173
173
|
const d = isWindows(platform) ? psQuote(dest) : shellQuote(dest)
|
|
174
174
|
if (isWindows(platform)) {
|
|
175
|
-
|
|
175
|
+
// 删旧目标可失败(本来不存在);复制失败必须让命令退出码非零 —— 备份是删除前唯一兜底。
|
|
176
|
+
return `Remove-Item -Recurse -Force ${d} -ErrorAction SilentlyContinue; Copy-Item -Recurse ${s} ${d} -ErrorAction Stop`
|
|
176
177
|
}
|
|
177
|
-
|
|
178
|
+
// 同上:不吞 stderr、不用 `; true` 抹平退出码(审计 P11-7)。
|
|
179
|
+
return `rm -rf ${d} && cp -r ${s} ${d}`
|
|
178
180
|
}
|
|
179
181
|
|
|
180
182
|
/* ------------------------------------------------------------ 计划 */
|
|
@@ -186,6 +188,8 @@ export interface ReinstallStep {
|
|
|
186
188
|
command: string
|
|
187
189
|
/** 是否属于危险(触及 ~/.dsh 数据)动作 */
|
|
188
190
|
dangerous: boolean
|
|
191
|
+
/** 失败即中止后续所有步骤(仅备份步:它失败时后面的清空无兜底,必须停) */
|
|
192
|
+
critical?: boolean
|
|
189
193
|
}
|
|
190
194
|
|
|
191
195
|
export interface ReinstallPlan {
|
|
@@ -267,6 +271,8 @@ export async function buildReinstallPlan(
|
|
|
267
271
|
label: engineMsg('core.reinstall.stepBackupHome'),
|
|
268
272
|
command: copyDirCommand(dshHome, `${dshHome}.reinstall-backup`, platform),
|
|
269
273
|
dangerous: true,
|
|
274
|
+
// 备份失败 → 后面的 settings/profiles/sessions/credentials 清空没有抢救副本,必须中止。
|
|
275
|
+
critical: true,
|
|
270
276
|
})
|
|
271
277
|
}
|
|
272
278
|
if (selection.has('settings')) {
|
package/src/core/restore.ts
CHANGED
|
@@ -42,7 +42,7 @@ import { parseJsonSafe } from '../utils/json.ts';
|
|
|
42
42
|
import { isPathInside } from '../utils/paths.ts';
|
|
43
43
|
import { atomicCopyFile, atomicWriteFile } from '../utils/atomic-write.ts';
|
|
44
44
|
import type { SectionId } from '../schema/types.ts';
|
|
45
|
-
import type { HostContext, Snapshot, SnapshotStatus } from './types.ts';
|
|
45
|
+
import type { HostContext, Snapshot, SnapshotEntry, SnapshotStatus } from './types.ts';
|
|
46
46
|
|
|
47
47
|
/* ---------------------------------------------------------------- 类型 */
|
|
48
48
|
|
|
@@ -224,6 +224,19 @@ async function loadSnapshot(snapshotDir: string, msg: MsgFunc): Promise<Snapshot
|
|
|
224
224
|
return parsed as Snapshot;
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
+
/**
|
|
228
|
+
* `snapshot.entries` 兜底(审计 P3-3):旧 / 截断 / 手改的 snapshot.json 可能整段缺 entries
|
|
229
|
+
* (或写成非数组),直接 `for…of` 会抛裸 TypeError(`snapshot.entries is not iterable`)。
|
|
230
|
+
*
|
|
231
|
+
* 为什么按空集合而不是拒绝:快照可信度已在计划期按 verdict 过门(CORRUPT/INVALID 拒绝、
|
|
232
|
+
* LEGACY 放行),字段缺失属旧快照常态;同文件 `hostFileBackups ?? []` / `pluginRemovals ?? []`
|
|
233
|
+
* / `expectedSessionRefs(entries ?? [])` 都是这个口径 —— 旧快照诚实跳过,绝不把「字段缺失」
|
|
234
|
+
* 升级成类型错误。
|
|
235
|
+
*/
|
|
236
|
+
function snapshotEntries(snapshot: Snapshot): SnapshotEntry[] {
|
|
237
|
+
return Array.isArray(snapshot.entries) ? snapshot.entries : [];
|
|
238
|
+
}
|
|
239
|
+
|
|
227
240
|
async function fileExists(p: string): Promise<boolean> {
|
|
228
241
|
try {
|
|
229
242
|
await fs.access(p);
|
|
@@ -352,7 +365,12 @@ function createTargetSafetyChecker(homeDir: string): (absTarget: string) => stri
|
|
|
352
365
|
try {
|
|
353
366
|
if (fssync.lstatSync(absTarget).isSymbolicLink()) return 'target-symlink';
|
|
354
367
|
} catch (err) {
|
|
355
|
-
|
|
368
|
+
// ENOTDIR 与 ENOENT 同义:路径上某一段是普通文件(POSIX 的 lstat 抛 ENOTDIR,
|
|
369
|
+
// Windows 抛 ERROR_PATH_NOT_FOUND ⇒ ENOENT)。目标本身既不存在,也就不可能是链接
|
|
370
|
+
// ⇒ 交给下面的父级投影判定;真越界仍会被 isPathInside 拒。
|
|
371
|
+
// 判据 → docs/evidence/recovery-console.md;CI 三平台复现见 tests/core/restore.test.ts R-ghost-unreadable。
|
|
372
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
373
|
+
if (code !== 'ENOENT' && code !== 'ENOTDIR') return 'target-unreadable';
|
|
356
374
|
}
|
|
357
375
|
const parentReal = projectedParentReal(path.dirname(absTarget));
|
|
358
376
|
if (parentReal === null) return 'parent-unresolvable';
|
|
@@ -766,7 +784,7 @@ export async function planRestore(opts: RestoreOptions): Promise<RestorePlan> {
|
|
|
766
784
|
let patchLineCount = 0;
|
|
767
785
|
let workspaceCount = 0;
|
|
768
786
|
let credentialAbsentCount = 0;
|
|
769
|
-
for (const entry of snapshot
|
|
787
|
+
for (const entry of snapshotEntries(snapshot)) {
|
|
770
788
|
switch (entry.kind) {
|
|
771
789
|
case 'file': {
|
|
772
790
|
const rel = toRelPath(homeDir, resolveFileAbs(homeDir, entry.adapter, entry.ref));
|
|
@@ -1128,15 +1146,22 @@ export async function restore(opts: RestoreOptions): Promise<RestoreReport> {
|
|
|
1128
1146
|
/* ------------------------------------------------------------ 幽灵会话(F5) */
|
|
1129
1147
|
|
|
1130
1148
|
/**
|
|
1131
|
-
* 磁盘 sessions 目录实际相对路径清单(相对 <homeDir>/sessions
|
|
1149
|
+
* 磁盘 sessions 目录实际相对路径清单(相对 <homeDir>/sessions;**只有 ENOENT** 视为空)。
|
|
1132
1150
|
* 用 node fs 递归列举(离线引擎不依赖 HostContext),分隔符由 sessionKeyOf 归一。
|
|
1151
|
+
*
|
|
1152
|
+
* 「读不到」≠「没有」(AGENTS.md 文件类分区条;同 listSnapshots 的 EACCES/ENOTDIR 处理):
|
|
1153
|
+
* EACCES / EPERM / ENOTDIR(sessions 被普通文件占住)若也降级成空集合,幽灵判定会把快照里
|
|
1154
|
+
* **每一个**期望会话都报成「备份有记录、磁盘无文件,需人工清理」(审计 P3-4)。
|
|
1155
|
+
* 故只在 ENOENT(确实没有会话分区)时返回空;其余错误原样上抛,由调用方记入 failed
|
|
1156
|
+
* —— 报告成「未知」,绝不断言磁盘上没有这些会话。
|
|
1133
1157
|
*/
|
|
1134
1158
|
async function listSessionEntries(homeDir: string): Promise<string[]> {
|
|
1135
1159
|
const base = path.join(homeDir, 'sessions');
|
|
1136
1160
|
try {
|
|
1137
1161
|
return await fs.readdir(base, { recursive: true });
|
|
1138
|
-
} catch {
|
|
1139
|
-
return [];
|
|
1162
|
+
} catch (err) {
|
|
1163
|
+
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return [];
|
|
1164
|
+
throw err;
|
|
1140
1165
|
}
|
|
1141
1166
|
}
|
|
1142
1167
|
|
|
@@ -1149,7 +1174,7 @@ async function listSessionEntries(homeDir: string): Promise<string[]> {
|
|
|
1149
1174
|
*/
|
|
1150
1175
|
async function sweepRestoredSessions(snapshotDir: string, homeDir: string, msg: MsgFunc): Promise<string[]> {
|
|
1151
1176
|
const snapshot = await loadSnapshot(snapshotDir, msg);
|
|
1152
|
-
const expected = expectedSessionRefs(snapshot
|
|
1177
|
+
const expected = expectedSessionRefs(snapshotEntries(snapshot));
|
|
1153
1178
|
if (expected.length === 0) return [];
|
|
1154
1179
|
return sweepGhostSessions(expected, await listSessionEntries(homeDir));
|
|
1155
1180
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 分区载荷脱敏的**唯一入口**(issue #136 C)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么必须只有一处:同一分区数据有五个出口 —— 导出、push 载荷、push 预览、
|
|
5
|
+
* `hasLocalChanges` 的基线比对、合并落地的本地侧与「落地后重新导出记基线」。
|
|
6
|
+
* 这些字节必须**逐字相同**(参见 AGENTS.md「本地有改动与导出载荷必须同一份字节」):
|
|
7
|
+
* 任何一处只做 `scanAndRedact` 而不做归属标注,就会让「被剥空路径」在那一侧消失,
|
|
8
|
+
* 与另一侧算出的 hash 分叉,表现为每轮同步都真推一次 / 合并判冲突。
|
|
9
|
+
*
|
|
10
|
+
* 归属标注钩子见 `ConfigAdapter.annotateRedactions`(只有 plugins 分区实现:把 hit 的
|
|
11
|
+
* `patch[i].raw.x` 归属回第 i 行的 `redactedPaths`)。
|
|
12
|
+
*/
|
|
13
|
+
import type { ConfigAdapter, SensitiveHit } from './types.ts';
|
|
14
|
+
|
|
15
|
+
/** 扫描器最小契约(`SecretScanner` 结构性满足;此处只依赖用到的那一个方法)。 */
|
|
16
|
+
export interface SectionScanner {
|
|
17
|
+
scanAndRedact(data: unknown): { sanitized: unknown; hits: SensitiveHit[] };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 可参与脱敏归属标注的适配器(只要求用到的那部分形状)。 */
|
|
21
|
+
type AnnotatingAdapter = Pick<ConfigAdapter, 'annotateRedactions'>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 扫描 + 剥离 + (如实现)归属标注。
|
|
25
|
+
*
|
|
26
|
+
* @param adapter 分区适配器;未实现 `annotateRedactions` 时行为与直接 `scanAndRedact` 逐字相同。
|
|
27
|
+
*/
|
|
28
|
+
export function sanitizeSectionData(
|
|
29
|
+
adapter: AnnotatingAdapter | undefined,
|
|
30
|
+
scanner: SectionScanner,
|
|
31
|
+
data: unknown,
|
|
32
|
+
): { sanitized: unknown; hits: SensitiveHit[] } {
|
|
33
|
+
const scanned = scanner.scanAndRedact(data);
|
|
34
|
+
const annotate = adapter?.annotateRedactions;
|
|
35
|
+
if (annotate === undefined) return { sanitized: scanned.sanitized, hits: scanned.hits };
|
|
36
|
+
return { sanitized: annotate.call(adapter, scanned.sanitized, scanned.hits), hits: scanned.hits };
|
|
37
|
+
}
|
|
@@ -8,7 +8,8 @@ import fs from 'node:fs/promises';
|
|
|
8
8
|
import os from 'node:os';
|
|
9
9
|
import path from 'node:path';
|
|
10
10
|
|
|
11
|
-
import { SNAPSHOT_DIFF_LIMITS, snapshotFileDiff, summarizeRestoreChanges } from './snapshot-diff.ts';
|
|
11
|
+
import { SNAPSHOT_DIFF_LIMITS, readSide, snapshotFileDiff, summarizeRestoreChanges } from './snapshot-diff.ts';
|
|
12
|
+
import type { SideReadIo } from './snapshot-diff.ts';
|
|
12
13
|
import type { RestorePlan } from './restore.ts';
|
|
13
14
|
|
|
14
15
|
interface Fixture {
|
|
@@ -239,3 +240,74 @@ test('S-10 目录当文件:unreadable(不抛错、不递归)', async () =>
|
|
|
239
240
|
await fx.cleanup();
|
|
240
241
|
}
|
|
241
242
|
});
|
|
243
|
+
|
|
244
|
+
/* ---------------- P2-6:「存在但读不出」不得被判成不存在(审计回归) ---------------- */
|
|
245
|
+
|
|
246
|
+
/** 造一个只带 errno code 的错误(真机上只有 EACCES 一类能触发该分支,Windows 无 ACL 夹具可造) */
|
|
247
|
+
function errnoError(code: string): Error {
|
|
248
|
+
return Object.assign(new Error(`${code}: simulated`), { code });
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
test('P2-6: stat 成功但 readFile 抛 EACCES ⇒ 存在但读不出(与目录分支同形态,绝不判成不存在)', async () => {
|
|
252
|
+
const fx = await makeFixture();
|
|
253
|
+
try {
|
|
254
|
+
await fx.write('locked.yaml', 'a: 1\n');
|
|
255
|
+
const lockedAbs = path.join(fx.homeDir, 'locked.yaml');
|
|
256
|
+
const maxBytes = SNAPSHOT_DIFF_LIMITS.maxStatSideBytes;
|
|
257
|
+
|
|
258
|
+
const readable = await readSide(lockedAbs, maxBytes);
|
|
259
|
+
assert.equal(readable.exists, true);
|
|
260
|
+
assert.equal(readable.unreadable, false);
|
|
261
|
+
assert.equal(readable.text, 'a: 1\n');
|
|
262
|
+
|
|
263
|
+
// 既有形态:路径是目录(同函数非普通文件分支)——「读不出」必须与它逐字段一致
|
|
264
|
+
await fs.mkdir(path.join(fx.homeDir, 'adir'), { recursive: true });
|
|
265
|
+
const asDir = await readSide(path.join(fx.homeDir, 'adir'), maxBytes);
|
|
266
|
+
|
|
267
|
+
const deniedIo: SideReadIo = {
|
|
268
|
+
stat: async () => ({ isFile: () => true, size: 6 }),
|
|
269
|
+
readFile: async () => { throw errnoError('EACCES'); },
|
|
270
|
+
};
|
|
271
|
+
const denied = await readSide(lockedAbs, maxBytes, deniedIo);
|
|
272
|
+
// bytes 是各来源自己的 stat 体积(目录 vs 6 字节文件),形态比的是 exists/unreadable/text 这些标志
|
|
273
|
+
assert.deepEqual(
|
|
274
|
+
{ ...denied, bytes: 0 }, { ...asDir, bytes: 0 },
|
|
275
|
+
'「存在但读不出」必须沿用既有不可读形态(exists:true + unreadable:true + text:null)',
|
|
276
|
+
);
|
|
277
|
+
assert.equal(denied.exists, true, '判成不存在会让恢复预览把只读文件显示成「新增」');
|
|
278
|
+
} finally {
|
|
279
|
+
await fx.cleanup();
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test('P2-6: 只有 ENOENT 才算不存在(stat 侧 EACCES = 读不出;stat 后 read 报 ENOENT = 真的没了)', async () => {
|
|
284
|
+
const fx = await makeFixture();
|
|
285
|
+
try {
|
|
286
|
+
const abs = path.join(fx.homeDir, 'gone.yaml');
|
|
287
|
+
const maxBytes = SNAPSHOT_DIFF_LIMITS.maxStatSideBytes;
|
|
288
|
+
|
|
289
|
+
// 真·不存在
|
|
290
|
+
const missing = await readSide(abs, maxBytes);
|
|
291
|
+
assert.equal(missing.exists, false);
|
|
292
|
+
|
|
293
|
+
// stat 拿不到状态(EACCES)= 路径在、只是读不了
|
|
294
|
+
const statDenied: SideReadIo = {
|
|
295
|
+
stat: async () => { throw errnoError('EACCES'); },
|
|
296
|
+
readFile: async () => Buffer.from('x'),
|
|
297
|
+
};
|
|
298
|
+
const fromStatDenied = await readSide(abs, maxBytes, statDenied);
|
|
299
|
+
assert.equal(fromStatDenied.exists, true);
|
|
300
|
+
assert.equal(fromStatDenied.unreadable, true);
|
|
301
|
+
|
|
302
|
+
// stat 与 read 之间被删掉(read 报 ENOENT)= 当时确实不存在
|
|
303
|
+
const vanished: SideReadIo = {
|
|
304
|
+
stat: async () => ({ isFile: () => true, size: 10 }),
|
|
305
|
+
readFile: async () => { throw errnoError('ENOENT'); },
|
|
306
|
+
};
|
|
307
|
+
const fromVanished = await readSide(abs, maxBytes, vanished);
|
|
308
|
+
assert.equal(fromVanished.exists, false);
|
|
309
|
+
assert.equal(fromVanished.unreadable, false);
|
|
310
|
+
} finally {
|
|
311
|
+
await fx.cleanup();
|
|
312
|
+
}
|
|
313
|
+
});
|