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/lib/core/backup.d.ts
CHANGED
|
@@ -278,8 +278,4 @@ export interface SnapshotVerifyResult {
|
|
|
278
278
|
ok: boolean;
|
|
279
279
|
reason?: string;
|
|
280
280
|
}
|
|
281
|
-
/**
|
|
282
|
-
* 从磁盘重读并验证快照(F1):id 合法 + snapshot.json 存在 + manifest 存在 + blob hashes 匹配 +
|
|
283
|
-
* metadataHash 匹配 + 必要 blobs 存在 + 路径安全。不信任内存对象。
|
|
284
|
-
*/
|
|
285
281
|
export declare function verifySnapshot(snapshotsDir: string, id: string): Promise<SnapshotVerifyResult>;
|
package/lib/core/backup.js
CHANGED
|
@@ -832,9 +832,25 @@ export function computeMetadataHash(snapshot) {
|
|
|
832
832
|
* 从磁盘重读并验证快照(F1):id 合法 + snapshot.json 存在 + manifest 存在 + blob hashes 匹配 +
|
|
833
833
|
* metadataHash 匹配 + 必要 blobs 存在 + 路径安全。不信任内存对象。
|
|
834
834
|
*/
|
|
835
|
+
/**
|
|
836
|
+
* `candidate` 是否严格落在 `root` 目录**内部**(等于 root 自身不算)。
|
|
837
|
+
*
|
|
838
|
+
* 判据走 `path.relative` 的**路径段**语义,绝不用裸字符串前缀:`snapshots/snap1` 与
|
|
839
|
+
* `snapshots/snap10` 共享字符串前缀,字符串比较会把兄弟目录里的 blob 当成本快照内的文件,
|
|
840
|
+
* 从而放过 `../snap10/blob.bin` 这类键(审计 P2-5)。两者入参都先 resolve,Windows 的
|
|
841
|
+
* 反斜杠与 `..` 各变体在比较前已被规范化。
|
|
842
|
+
*/
|
|
843
|
+
function isInsideDir(root, candidate) {
|
|
844
|
+
const rel = path.relative(root, candidate);
|
|
845
|
+
if (rel === '' || path.isAbsolute(rel))
|
|
846
|
+
return false;
|
|
847
|
+
return rel !== '..' && !rel.startsWith(`..${path.sep}`);
|
|
848
|
+
}
|
|
835
849
|
export async function verifySnapshot(snapshotsDir, id) {
|
|
836
|
-
|
|
837
|
-
|
|
850
|
+
// snapshotsRoot 与 dir 一律先 resolve:越界判定要求两侧同一坐标系(否则相对路径/分隔符各说各话)
|
|
851
|
+
const snapshotsRoot = path.resolve(snapshotsDir);
|
|
852
|
+
const dir = path.resolve(snapshotsRoot, id);
|
|
853
|
+
if (!isInsideDir(snapshotsRoot, dir)) {
|
|
838
854
|
return { ok: false, reason: engineMsg('core.backup.snapshotPathEscaped') };
|
|
839
855
|
}
|
|
840
856
|
let raw;
|
|
@@ -872,8 +888,9 @@ export async function verifySnapshot(snapshotsDir, id) {
|
|
|
872
888
|
}
|
|
873
889
|
// blob hashes 匹配 + 存在
|
|
874
890
|
for (const [blobPath, expectedHash] of Object.entries(manifest.blobHashes)) {
|
|
875
|
-
|
|
876
|
-
|
|
891
|
+
// 按路径段判定:`../snap10/blob.bin` 解析后落在兄弟快照目录里,必须在这里被拦住(审计 P2-5)
|
|
892
|
+
const target = path.resolve(dir, blobPath);
|
|
893
|
+
if (!isInsideDir(dir, target))
|
|
877
894
|
return { ok: false, reason: engineMsg('core.backup.blobPathEscaped', { path: blobPath }) };
|
|
878
895
|
let data;
|
|
879
896
|
try {
|
|
@@ -80,7 +80,7 @@ export type RescueEnterResult = {
|
|
|
80
80
|
state: RescueState;
|
|
81
81
|
} | {
|
|
82
82
|
ok: false;
|
|
83
|
-
code: 'already-active' | 'backup-failed' | 'package-json-invalid' | 'write-failed';
|
|
83
|
+
code: 'already-active' | 'other-profile-active' | 'backup-failed' | 'package-json-invalid' | 'write-failed';
|
|
84
84
|
message: string;
|
|
85
85
|
};
|
|
86
86
|
export type RescueExitResult = {
|
package/lib/core/boot-rescue.js
CHANGED
|
@@ -430,7 +430,18 @@ export async function enterRescueMode(opts) {
|
|
|
430
430
|
message: `rescue mode is already active for ${rel.profilePatch} (enteredAt=${current.enteredAt}); nothing was modified`,
|
|
431
431
|
};
|
|
432
432
|
}
|
|
433
|
-
//
|
|
433
|
+
// 指纹不匹配但状态仍是 active 且属于**另一个 profile**:home patch 是共享文件且已被置空,
|
|
434
|
+
// 备份文件名不含 profile(见 BACKUP_* 常量)——再进会覆盖它的备份与状态,退出后另一个
|
|
435
|
+
// profile 的 home patch / profile patch 原件永久丢失(审计 P3-1)。此处 fail-closed 拒绝,
|
|
436
|
+
// 让调用方先为原 profile 退出救援模式。
|
|
437
|
+
if (current !== null && current.active === true && current.profile !== profile) {
|
|
438
|
+
return {
|
|
439
|
+
ok: false,
|
|
440
|
+
code: 'other-profile-active',
|
|
441
|
+
message: `rescue mode is already active for profile ${current.profile} (enteredAt=${current.enteredAt}): exit that profile first, entering ${profile} would overwrite its backups`,
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
// 同 profile 的失效残留(换机器/重建 home 导致指纹变化)或状态损坏 → 允许覆盖重进
|
|
434
445
|
// —— 2. 读原件(读失败 → backup-failed;备份先于任何目标写入) ——
|
|
435
446
|
let origProfilePatch;
|
|
436
447
|
let origHomePatch;
|
package/lib/core/boot-safety.js
CHANGED
|
@@ -70,6 +70,35 @@ function writeBundles(pkg, bundles) {
|
|
|
70
70
|
function errorText(err) {
|
|
71
71
|
return err instanceof Error ? err.message : String(err);
|
|
72
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* 写回后的读回校验:profile package.json 的 bundles 已不含待剔除条目(且全为字符串)→ true。
|
|
75
|
+
* 为什么需要它:「writeText 没抛错」只证明字节发出去了,不证明盘面变成了我们要的形状
|
|
76
|
+
* (审计 P3-2:drop 集合用 describeEntry 生成的展示名,filter 与真实条目永不相等,写回后
|
|
77
|
+
* 非字符串条目仍在,却仍报 fixed:true/repaired)。读不到 / 解析失败 / 结构异常一律 false ——
|
|
78
|
+
* 修没修成只看盘面,宁可报 unfixed 让用户手动处理,也不假装启动风险已消除。
|
|
79
|
+
*/
|
|
80
|
+
async function bundlesPrunedOnDisk(readText, rel, drop) {
|
|
81
|
+
let text;
|
|
82
|
+
try {
|
|
83
|
+
text = await readText(rel);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (text === null)
|
|
89
|
+
return false;
|
|
90
|
+
let parsed;
|
|
91
|
+
try {
|
|
92
|
+
parsed = JSON.parse(text);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const bundles = readBundles(parsed);
|
|
98
|
+
if (!Array.isArray(bundles))
|
|
99
|
+
return false;
|
|
100
|
+
return bundles.every((b) => typeof b === 'string' && !drop.has(b));
|
|
101
|
+
}
|
|
73
102
|
/**
|
|
74
103
|
* 执行启动自洽审计。**绝不抛错**:任何单项失败转成 issue / unchecked。
|
|
75
104
|
*/
|
|
@@ -117,11 +146,17 @@ export async function auditBootSafety(deps) {
|
|
|
117
146
|
try {
|
|
118
147
|
// 只剔除「不可解析且非核心」的条目:其余条目(含解析不出的核心项)原样保留
|
|
119
148
|
const drop = new Set(prunable.map((p) => p.name));
|
|
120
|
-
const
|
|
149
|
+
const rawBundles = Array.isArray(bundles) ? bundles : [];
|
|
150
|
+
// 非字符串条目一律剔除,且必须按**真实类型**判定:pruned 里它的 name 是
|
|
151
|
+
// describeEntry(entry) 生成的展示名(42 → '42'),拿展示名去比数组里的真实条目
|
|
152
|
+
// 永不命中,写回后条目仍在(审计 P3-2)。非字符串不可能是合法 bundle 名。
|
|
153
|
+
const next = rawBundles.filter((b) => typeof b === 'string' && !drop.has(b));
|
|
121
154
|
if (!writeBundles(pkg, next))
|
|
122
155
|
throw new Error(engineMsg('core.bootSafety.bundlesNotArray'));
|
|
123
156
|
await deps.writeText(pkgRel, JSON.stringify(pkg, null, 2) + '\n');
|
|
124
|
-
written =
|
|
157
|
+
written = await bundlesPrunedOnDisk(deps.readText, pkgRel, drop);
|
|
158
|
+
if (!written)
|
|
159
|
+
failReason = 'bundles-still-present';
|
|
125
160
|
}
|
|
126
161
|
catch (err) {
|
|
127
162
|
failReason = errorText(err);
|
|
@@ -7,7 +7,8 @@ export function applyItemResolution(item, decisions, msg) {
|
|
|
7
7
|
return { ...item, kind: 'Skip', severity: 'info', detail: `${item.detail ?? ''}${msg('import.conflictKeepCurrent')}` };
|
|
8
8
|
}
|
|
9
9
|
if (resolution === 'useImported') {
|
|
10
|
-
|
|
10
|
+
// decidedBy: 'item' —— 来源是用户对该计划项的逐项决策(写回守卫据此允许清空被剥空叶子)
|
|
11
|
+
return { ...item, kind: 'Update', severity: 'info', conflict: { itemId: item.id, resolution, decidedBy: 'item' } };
|
|
11
12
|
}
|
|
12
13
|
// review / 未决策:按全局策略兜底
|
|
13
14
|
if (decisions.strategy === 'skipExisting') {
|
package/lib/core/exporter.js
CHANGED
|
@@ -16,9 +16,11 @@ import { stringifyJsonSafe } from '../utils/json.js';
|
|
|
16
16
|
import { buildManifest, CHECKSUMS_FILE, MANIFEST_FILE, EXPORTER_NAME } from '../schema/manifest.js';
|
|
17
17
|
import { SECTION_JSON_PATHS, SECTION_FILE_PREFIXES, isFileSection } from '../schema/config.js';
|
|
18
18
|
import { DEFAULT_SENSITIVE_RELS, refreshVault } from '../security/vault.js';
|
|
19
|
+
import { isSensitiveHomeRel } from '../utils/paths.js';
|
|
19
20
|
import { writeZip } from '../utils/zip.js';
|
|
20
21
|
import { formatDateOnly } from '../utils/time.js';
|
|
21
22
|
import { msgOf } from './messages.js';
|
|
23
|
+
import { sanitizeSectionData } from './section-sanitize.js';
|
|
22
24
|
import { engineMsg } from './messages-core.js';
|
|
23
25
|
import { declareBundledSessionsInWorkspaces, normalizeSessionLimit, sessionIdKey } from './session-select.js';
|
|
24
26
|
// issue #45 连带匹配的第二判据:工作区 path 的 cwd 目录键(DSH 的 sessionIds 覆盖率很低,见 coupleSessionWorkspaces)
|
|
@@ -316,7 +318,9 @@ export class Exporter {
|
|
|
316
318
|
// 只做**文本级扫描 + 告警**,绝不改写/剥离内容(见 scanFileSectionText)。
|
|
317
319
|
let sanitized = section.data;
|
|
318
320
|
if (!isFileSection(adapter.id)) {
|
|
319
|
-
|
|
321
|
+
// issue #136 C:脱敏 + 归属标注走唯一入口(与同步侧五个出口同一份字节)。
|
|
322
|
+
// 归属信息随载荷走(plugins 的 patch 行 `redactedPaths`),报告统计仍用全量命中。
|
|
323
|
+
const scanned = sanitizeSectionData(adapter, this.scanner, section.data);
|
|
320
324
|
redactedHits.push(...scanned.hits);
|
|
321
325
|
sanitized = scanned.sanitized;
|
|
322
326
|
}
|
|
@@ -381,9 +385,26 @@ export class Exporter {
|
|
|
381
385
|
if (isFileSection(section.sectionId)) {
|
|
382
386
|
const prefix = SECTION_FILE_PREFIXES[section.sectionId];
|
|
383
387
|
const files = section.data.files ?? [];
|
|
388
|
+
// 打包侧兜底(审计 P2-1 P0):第三方插件数据分区的根是**系统主目录**,收集侧已按同一判据
|
|
389
|
+
// 剔除凭据文件;这里再判一次,防止「收集侧漏判 / 消费者手工构造 section」把整文件秘密写进 ZIP。
|
|
390
|
+
// 判据唯一来源 = utils/paths.ts 的 isSensitiveHomeRel。
|
|
391
|
+
const guardCredentials = section.sectionId === 'pluginData' || section.sectionId === 'pluginAssets';
|
|
392
|
+
let droppedSensitive = 0;
|
|
393
|
+
let droppedSample = '';
|
|
384
394
|
for (const file of files) {
|
|
395
|
+
if (guardCredentials && isSensitiveHomeRel(file.relativePath)) {
|
|
396
|
+
droppedSensitive += 1;
|
|
397
|
+
if (droppedSample === '')
|
|
398
|
+
droppedSample = file.relativePath;
|
|
399
|
+
continue;
|
|
400
|
+
}
|
|
385
401
|
entries.push({ name: `${prefix}${file.relativePath}`, data: file.data });
|
|
386
402
|
}
|
|
403
|
+
if (droppedSensitive > 0) {
|
|
404
|
+
warnings.push(this.msg('export.pluginDataSensitiveSkipped', {
|
|
405
|
+
count: String(droppedSensitive), sample: droppedSample,
|
|
406
|
+
}));
|
|
407
|
+
}
|
|
387
408
|
continue;
|
|
388
409
|
}
|
|
389
410
|
const jsonPath = SECTION_JSON_PATHS[section.sectionId];
|
package/lib/core/journal.d.ts
CHANGED
|
@@ -143,6 +143,25 @@ export interface JournalIo {
|
|
|
143
143
|
}): Promise<void>;
|
|
144
144
|
exists(p: string): Promise<boolean>;
|
|
145
145
|
}
|
|
146
|
+
/** 引用集合的取证式扫描结果(P3-5)。`complete === false` 时 referenced 只是下界。 */
|
|
147
|
+
export interface JournalReferenceScan {
|
|
148
|
+
/** 可解析 journal 引用的 snapshotId(不完整时是下界) */
|
|
149
|
+
referenced: Set<string>;
|
|
150
|
+
/** 读不到/解析不了的 journal 位置(相对 transactions 的 posix 形式) */
|
|
151
|
+
unreadable: string[];
|
|
152
|
+
/** true = 三个目录都读到、每条 journal 都可解析,referenced 可当完整集合使用 */
|
|
153
|
+
complete: boolean;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 引用集合读不完整(P3-5):有 journal 但读不出/解析不了,或 journal 目录读不到。
|
|
157
|
+
* 调用方必须 fail-closed —— 快照删除(409 recovery-reference-unreadable)与自动 prune
|
|
158
|
+
* (放弃本轮清理)都依赖「读不到 ≠ 没有引用」。`unreadable` 是相对路径,供日志定位。
|
|
159
|
+
*/
|
|
160
|
+
export declare class JournalReferenceUnreadableError extends Error {
|
|
161
|
+
readonly code = "journal-reference-unreadable";
|
|
162
|
+
readonly unreadable: string[];
|
|
163
|
+
constructor(unreadable: string[]);
|
|
164
|
+
}
|
|
146
165
|
export interface JournalStoreOptions {
|
|
147
166
|
transactionsDir: string;
|
|
148
167
|
io?: JournalIo;
|
|
@@ -223,7 +242,7 @@ export declare class JournalStore {
|
|
|
223
242
|
/**
|
|
224
243
|
* 收集已被「可恢复 / 未收敛 journal」引用的 snapshotId(Phase 4 F3 prune 保护):
|
|
225
244
|
* 扫描 active/ + quarantine/ + completed/ 下的 journal,凡「快照仍可能被 recovery/人工恢复用到」
|
|
226
|
-
* 且 snapshotId 合法 →
|
|
245
|
+
* 且 snapshotId 合法 → 收集。该集合用于 FileSnapshotStore.prune 豁免 ——
|
|
227
246
|
* 引用的 recovery snapshot 绝不可被自动淘汰。
|
|
228
247
|
*
|
|
229
248
|
* 什么算「已消费」(不保护):
|
|
@@ -232,8 +251,21 @@ export declare class JournalStore {
|
|
|
232
251
|
* 审计 P0-3:**半回滚态**(ROLLED_BACK + full === false)与 NEEDS_ATTENTION / ROLLING_BACK /
|
|
233
252
|
* RECOVERING 仍需 recovery 或人工恢复 —— 它们会被规整进 completed/,所以 completed/ 必须一起扫,
|
|
234
253
|
* 否则「已回滚但没回滚干净」的回滚点会在 10 次快照后被静默淘汰(而它正是用户唯一的退路)。
|
|
254
|
+
*
|
|
255
|
+
* 审计 P3-5:**读不到 ≠ 没有引用**。旧实现把目录读不到折成空、把单条读不出/解析不了折成跳过,
|
|
256
|
+
* 于是「有 journal 但读不出」与「没有 journal」在上层无法区分:deleteSnapshot 的
|
|
257
|
+
* recovery-reference-unreadable 保守分支永远不可达,快照会被真删。这里改为:
|
|
258
|
+
* - 目录 ENOENT(真没有)→ 空;其余目录错误 → 记进 unreadable(不折叠);
|
|
259
|
+
* - 单条 journal 读不出(非 ENOENT)/解析不了 → 记进 unreadable;
|
|
260
|
+
* - 有任何 unreadable ⇒ 集合不完整,listReferencedSnapshotIds 显式抛错(调用方 fail-closed)。
|
|
261
|
+
* 需要区分「具体哪条读不出」的诊断面用 scanSnapshotReferences()。
|
|
235
262
|
*/
|
|
236
263
|
listReferencedSnapshotIds(): Promise<Set<string>>;
|
|
264
|
+
/** 引用集合的**取证式**扫描:既给出可用的 snapshotId,也如实给出读不出的位置(P3-5)。
|
|
265
|
+
* `complete === false` 时 `referenced` 只是下界,调用方必须按 fail-closed 处理。 */
|
|
266
|
+
scanSnapshotReferences(): Promise<JournalReferenceScan>;
|
|
267
|
+
/** 诊断用位置标签(相对 transactions 的 posix 形式;绝不带绝对路径进日志/响应)。 */
|
|
268
|
+
private referenceLabel;
|
|
237
269
|
/** 祖先布局核对链(SAFE MODE 判定用):<transactionsDir> → 其父目录。 */
|
|
238
270
|
private safeModeLayoutRoots;
|
|
239
271
|
/**
|
|
@@ -250,9 +282,27 @@ export declare class JournalStore {
|
|
|
250
282
|
readSafeMode(): Promise<boolean>;
|
|
251
283
|
/** 写/清 SAFE MODE 标记(atomic)。 */
|
|
252
284
|
writeSafeMode(blocked: boolean): Promise<void>;
|
|
253
|
-
/**
|
|
254
|
-
|
|
255
|
-
|
|
285
|
+
/**
|
|
286
|
+
* 保留策略:completed 保留 N,recovery-history 保留 M;**按真实时间删最旧**,幂等。
|
|
287
|
+
*
|
|
288
|
+
* 排序键(唯一来源,注释即实现):
|
|
289
|
+
* - completed/:journal 记录内的 `updatedAt`(落盘/状态推进时刻)。刻意不用 mtime ——
|
|
290
|
+
* completed 文件名是 randomBytes UUID,与时间无关;而复制/恢复会重写 mtime,
|
|
291
|
+
* 旧实现按文件名字典序「删最旧」,实际可能删掉最新的那条(审计 P3-7)。
|
|
292
|
+
* - recovery-history/:文件名前缀的 epoch 毫秒(appendRecoveryHistory 的命名口径;
|
|
293
|
+
* 该目录没有记录内时间字段)。
|
|
294
|
+
*
|
|
295
|
+
* 判不了新旧的文件(读不出 / 解析不了 / 文件名带不出时间)**既不计入上限也不删**:
|
|
296
|
+
* 宁可暂时超出上限,也不拿一个未知年龄的 journal 当「最旧」删掉。
|
|
297
|
+
*/
|
|
298
|
+
retention(completedLimit?: number, historyLimit?: number): Promise<{
|
|
299
|
+
completed: number;
|
|
300
|
+
history: number;
|
|
301
|
+
}>;
|
|
302
|
+
/** completed journal 的时间键:记录内 updatedAt;读不出/解析不了 → null(判不了新旧)。 */
|
|
303
|
+
private completedJournalAt;
|
|
304
|
+
/** 按时间键升序删除超出上限的最旧项;键为 null 的项不参与计数、绝不被删。返回删除条数。 */
|
|
305
|
+
private pruneOldestByAge;
|
|
256
306
|
}
|
|
257
307
|
/**
|
|
258
308
|
* durable SAFE MODE 标记的**路径 + 内容判据 + 三态分类**(唯一来源)。
|
package/lib/core/journal.js
CHANGED
|
@@ -111,23 +111,37 @@ export function transitionJournalState(j, to) {
|
|
|
111
111
|
}
|
|
112
112
|
return { ...j, state: to, updatedAt: new Date().toISOString() };
|
|
113
113
|
}
|
|
114
|
+
/** 只有「确实不存在」才算空目录;权限/路径类型(EACCES / ENOTDIR)等错误必须抛给调用方(P3-5)。 */
|
|
115
|
+
function isEnoent(err) {
|
|
116
|
+
return err?.code === 'ENOENT';
|
|
117
|
+
}
|
|
114
118
|
const defaultIo = {
|
|
115
119
|
async mkdir(d, o) { await fs.mkdir(d, o); },
|
|
116
120
|
async readFileText(p) { return (await fs.readFile(p, 'utf8')).toString(); },
|
|
117
121
|
async writeAll(t, c) { await atomicWriteFile(t, c, { mode: 0o600, symlink: 'reject' }); },
|
|
118
122
|
async rename(a, b) { await fs.rename(a, b); },
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
123
|
+
// P3-5:旧实现 catch 一切 → 读不出的目录被当作空目录,上层(尤其快照引用集合)把
|
|
124
|
+
// 「读不到 ≠ 没有引用」误读成「没有引用」。ENOENT 才是「真没有」。
|
|
125
|
+
async readdirNames(d) {
|
|
126
|
+
try {
|
|
127
|
+
return await fs.readdir(d);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
if (isEnoent(err))
|
|
131
|
+
return [];
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
async readdirEntries(d) {
|
|
136
|
+
try {
|
|
137
|
+
return await fs.readdir(d, { withFileTypes: true });
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
if (isEnoent(err))
|
|
141
|
+
return [];
|
|
142
|
+
throw err;
|
|
143
|
+
}
|
|
144
|
+
},
|
|
131
145
|
async lstat(p) { try {
|
|
132
146
|
return await fs.lstat(p);
|
|
133
147
|
}
|
|
@@ -149,6 +163,20 @@ const defaultIo = {
|
|
|
149
163
|
return false;
|
|
150
164
|
} },
|
|
151
165
|
};
|
|
166
|
+
/**
|
|
167
|
+
* 引用集合读不完整(P3-5):有 journal 但读不出/解析不了,或 journal 目录读不到。
|
|
168
|
+
* 调用方必须 fail-closed —— 快照删除(409 recovery-reference-unreadable)与自动 prune
|
|
169
|
+
* (放弃本轮清理)都依赖「读不到 ≠ 没有引用」。`unreadable` 是相对路径,供日志定位。
|
|
170
|
+
*/
|
|
171
|
+
export class JournalReferenceUnreadableError extends Error {
|
|
172
|
+
code = 'journal-reference-unreadable';
|
|
173
|
+
unreadable;
|
|
174
|
+
constructor(unreadable) {
|
|
175
|
+
super('journal reference set is unreadable (unreadable is not the same as no reference): ' + unreadable.join(', '));
|
|
176
|
+
this.name = 'JournalReferenceUnreadableError';
|
|
177
|
+
this.unreadable = unreadable;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
152
180
|
/** journal 严格 UUID 校验(防文件名穿越)。 */
|
|
153
181
|
export function isValidOperationId(id) {
|
|
154
182
|
return typeof id === 'string' && VALID_OPERATION_ID_RE.test(id);
|
|
@@ -159,6 +187,18 @@ export function isJournalBasename(name) {
|
|
|
159
187
|
return false;
|
|
160
188
|
return !name.startsWith(TMP_PREFIX);
|
|
161
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* recovery-history 文件名的时间键:`<epochMs>-<randomHex>.<marker>.json`(appendRecoveryHistory 的命名口径)。
|
|
192
|
+
* 该目录没有记录内时间字段,文件名前缀就是唯一时间来源;带不出 epoch 毫秒 → null
|
|
193
|
+
*(retention 判不了新旧,不参与删除)。
|
|
194
|
+
*/
|
|
195
|
+
function historyEntryAt(name) {
|
|
196
|
+
const m = /^(\d+)-/.exec(name);
|
|
197
|
+
if (m === null)
|
|
198
|
+
return null;
|
|
199
|
+
const at = Number(m[1]);
|
|
200
|
+
return Number.isSafeInteger(at) ? at : null;
|
|
201
|
+
}
|
|
162
202
|
/**
|
|
163
203
|
* 原子写半成品判据(**有意保守宽判**,t65 / t60-O3)。
|
|
164
204
|
*
|
|
@@ -389,7 +429,7 @@ export class JournalStore {
|
|
|
389
429
|
/**
|
|
390
430
|
* 收集已被「可恢复 / 未收敛 journal」引用的 snapshotId(Phase 4 F3 prune 保护):
|
|
391
431
|
* 扫描 active/ + quarantine/ + completed/ 下的 journal,凡「快照仍可能被 recovery/人工恢复用到」
|
|
392
|
-
* 且 snapshotId 合法 →
|
|
432
|
+
* 且 snapshotId 合法 → 收集。该集合用于 FileSnapshotStore.prune 豁免 ——
|
|
393
433
|
* 引用的 recovery snapshot 绝不可被自动淘汰。
|
|
394
434
|
*
|
|
395
435
|
* 什么算「已消费」(不保护):
|
|
@@ -398,29 +438,71 @@ export class JournalStore {
|
|
|
398
438
|
* 审计 P0-3:**半回滚态**(ROLLED_BACK + full === false)与 NEEDS_ATTENTION / ROLLING_BACK /
|
|
399
439
|
* RECOVERING 仍需 recovery 或人工恢复 —— 它们会被规整进 completed/,所以 completed/ 必须一起扫,
|
|
400
440
|
* 否则「已回滚但没回滚干净」的回滚点会在 10 次快照后被静默淘汰(而它正是用户唯一的退路)。
|
|
441
|
+
*
|
|
442
|
+
* 审计 P3-5:**读不到 ≠ 没有引用**。旧实现把目录读不到折成空、把单条读不出/解析不了折成跳过,
|
|
443
|
+
* 于是「有 journal 但读不出」与「没有 journal」在上层无法区分:deleteSnapshot 的
|
|
444
|
+
* recovery-reference-unreadable 保守分支永远不可达,快照会被真删。这里改为:
|
|
445
|
+
* - 目录 ENOENT(真没有)→ 空;其余目录错误 → 记进 unreadable(不折叠);
|
|
446
|
+
* - 单条 journal 读不出(非 ENOENT)/解析不了 → 记进 unreadable;
|
|
447
|
+
* - 有任何 unreadable ⇒ 集合不完整,listReferencedSnapshotIds 显式抛错(调用方 fail-closed)。
|
|
448
|
+
* 需要区分「具体哪条读不出」的诊断面用 scanSnapshotReferences()。
|
|
401
449
|
*/
|
|
402
450
|
async listReferencedSnapshotIds() {
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
451
|
+
const scan = await this.scanSnapshotReferences();
|
|
452
|
+
if (!scan.complete)
|
|
453
|
+
throw new JournalReferenceUnreadableError(scan.unreadable);
|
|
454
|
+
return scan.referenced;
|
|
455
|
+
}
|
|
456
|
+
/** 引用集合的**取证式**扫描:既给出可用的 snapshotId,也如实给出读不出的位置(P3-5)。
|
|
457
|
+
* `complete === false` 时 `referenced` 只是下界,调用方必须按 fail-closed 处理。 */
|
|
458
|
+
async scanSnapshotReferences() {
|
|
459
|
+
const referenced = new Set();
|
|
460
|
+
const unreadable = [];
|
|
461
|
+
for (const dir of [this.activeDir(), this.quarantineDir(), this.completedDir()]) {
|
|
462
|
+
let names;
|
|
463
|
+
try {
|
|
464
|
+
names = await this.io.readdirNames(dir);
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
if (isEnoent(err))
|
|
468
|
+
continue; // 目录确实不存在 = 没有 journal
|
|
469
|
+
unreadable.push(this.referenceLabel(dir));
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
for (const name of names) {
|
|
407
473
|
if (!isJournalBasename(name))
|
|
408
474
|
continue;
|
|
409
|
-
const
|
|
410
|
-
|
|
475
|
+
const file = path.join(dir, name);
|
|
476
|
+
let text;
|
|
477
|
+
try {
|
|
478
|
+
text = await this.io.readFileText(file);
|
|
479
|
+
}
|
|
480
|
+
catch (err) {
|
|
481
|
+
if (isEnoent(err))
|
|
482
|
+
continue; // 扫描与读取之间被移走/删除:此刻已没有这条 journal
|
|
483
|
+
unreadable.push(this.referenceLabel(file));
|
|
411
484
|
continue;
|
|
485
|
+
}
|
|
412
486
|
const j = parseSafe(text);
|
|
413
|
-
if (j === null)
|
|
487
|
+
if (j === null) {
|
|
488
|
+
// 解析不了 = 无法证明它没引用某个快照(可能是更高 schemaVersion 的 journal)→ 不完整
|
|
489
|
+
unreadable.push(this.referenceLabel(file));
|
|
414
490
|
continue;
|
|
491
|
+
}
|
|
415
492
|
if (j.state === 'COMMITTED')
|
|
416
493
|
continue; // 已消费,不保护
|
|
417
494
|
if (j.state === 'ROLLED_BACK' && j.rollback.full === true)
|
|
418
495
|
continue; // 完整回滚:已回到导入前状态
|
|
419
496
|
if (typeof j.snapshotId === 'string' && j.snapshotId !== '')
|
|
420
|
-
|
|
497
|
+
referenced.add(j.snapshotId);
|
|
421
498
|
}
|
|
422
499
|
}
|
|
423
|
-
return
|
|
500
|
+
return { referenced, unreadable, complete: unreadable.length === 0 };
|
|
501
|
+
}
|
|
502
|
+
/** 诊断用位置标签(相对 transactions 的 posix 形式;绝不带绝对路径进日志/响应)。 */
|
|
503
|
+
referenceLabel(p) {
|
|
504
|
+
const rel = path.relative(this.transactionsDir, p);
|
|
505
|
+
return (rel === '' ? '.' : rel).split(path.sep).join('/');
|
|
424
506
|
}
|
|
425
507
|
// ---------- SAFE MODE ----------
|
|
426
508
|
/** 祖先布局核对链(SAFE MODE 判定用):<transactionsDir> → 其父目录。 */
|
|
@@ -455,22 +537,63 @@ export class JournalStore {
|
|
|
455
537
|
await this.io.writeAll(p, `${JSON.stringify({ blocked: true, at: new Date().toISOString() }, null, 2)}\n`);
|
|
456
538
|
}
|
|
457
539
|
// ---------- retention ----------
|
|
458
|
-
/**
|
|
540
|
+
/**
|
|
541
|
+
* 保留策略:completed 保留 N,recovery-history 保留 M;**按真实时间删最旧**,幂等。
|
|
542
|
+
*
|
|
543
|
+
* 排序键(唯一来源,注释即实现):
|
|
544
|
+
* - completed/:journal 记录内的 `updatedAt`(落盘/状态推进时刻)。刻意不用 mtime ——
|
|
545
|
+
* completed 文件名是 randomBytes UUID,与时间无关;而复制/恢复会重写 mtime,
|
|
546
|
+
* 旧实现按文件名字典序「删最旧」,实际可能删掉最新的那条(审计 P3-7)。
|
|
547
|
+
* - recovery-history/:文件名前缀的 epoch 毫秒(appendRecoveryHistory 的命名口径;
|
|
548
|
+
* 该目录没有记录内时间字段)。
|
|
549
|
+
*
|
|
550
|
+
* 判不了新旧的文件(读不出 / 解析不了 / 文件名带不出时间)**既不计入上限也不删**:
|
|
551
|
+
* 宁可暂时超出上限,也不拿一个未知年龄的 journal 当「最旧」删掉。
|
|
552
|
+
*/
|
|
459
553
|
async retention(completedLimit = 50, historyLimit = 200) {
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
await this.
|
|
463
|
-
const
|
|
464
|
-
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
554
|
+
const completedNames = (await this.io.readdirNames(this.completedDir())).filter(isJournalBasename);
|
|
555
|
+
const completed = await this.pruneOldestByAge(this.completedDir(), completedNames, completedLimit, (name) => this.completedJournalAt(name));
|
|
556
|
+
const historyNames = await this.io.readdirNames(this.recoveryHistoryDir());
|
|
557
|
+
const history = await this.pruneOldestByAge(this.recoveryHistoryDir(), historyNames, historyLimit, async (name) => historyEntryAt(name));
|
|
558
|
+
return { completed, history };
|
|
559
|
+
}
|
|
560
|
+
/** completed journal 的时间键:记录内 updatedAt;读不出/解析不了 → null(判不了新旧)。 */
|
|
561
|
+
async completedJournalAt(name) {
|
|
562
|
+
try {
|
|
563
|
+
const j = parseSafe(await this.io.readFileText(path.join(this.completedDir(), name)));
|
|
564
|
+
if (j === null)
|
|
565
|
+
return null;
|
|
566
|
+
const at = Date.parse(j.updatedAt);
|
|
567
|
+
return Number.isFinite(at) ? at : null;
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
return null; // 读不出 / 并发消失:判不了新旧就不删
|
|
472
571
|
}
|
|
473
572
|
}
|
|
573
|
+
/** 按时间键升序删除超出上限的最旧项;键为 null 的项不参与计数、绝不被删。返回删除条数。 */
|
|
574
|
+
async pruneOldestByAge(dir, names, limit, atOf) {
|
|
575
|
+
const keyed = [];
|
|
576
|
+
for (const name of names) {
|
|
577
|
+
const at = await atOf(name);
|
|
578
|
+
if (at !== null)
|
|
579
|
+
keyed.push({ name, at });
|
|
580
|
+
}
|
|
581
|
+
if (keyed.length <= limit)
|
|
582
|
+
return 0;
|
|
583
|
+
// 同一时刻用文件名做稳定次序,保证幂等(不依赖 readdir 顺序)
|
|
584
|
+
keyed.sort((a, b) => a.at - b.at || a.name.localeCompare(b.name));
|
|
585
|
+
let removed = 0;
|
|
586
|
+
for (const item of keyed.slice(0, keyed.length - limit)) {
|
|
587
|
+
try {
|
|
588
|
+
await this.io.rm(path.join(dir, item.name), { force: true });
|
|
589
|
+
removed += 1;
|
|
590
|
+
}
|
|
591
|
+
catch {
|
|
592
|
+
/* 单条删失败不中断其余,留待下一次 */
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return removed;
|
|
596
|
+
}
|
|
474
597
|
}
|
|
475
598
|
/** durable SAFE MODE 标记的规范路径:`<dataDir>/transactions/safe-mode`。
|
|
476
599
|
* host 同步探测与 CLI 离线安全门必须走这一处定义,不得在别处再写一份字面量(审计 P0-11)。 */
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import type { ChildProcess } from 'node:child_process';
|
|
1
3
|
import { parsePackOutput } from './local-plugin-pack.ts';
|
|
2
4
|
import type { PackExec, PackLocalPluginsResult } from './local-plugin-pack.ts';
|
|
3
5
|
import type { HostContext } from './types.ts';
|
|
@@ -15,6 +17,33 @@ export type LocalPluginPackHook = (plugins: readonly {
|
|
|
15
17
|
version: string;
|
|
16
18
|
spec?: string;
|
|
17
19
|
}[], ctx: Pick<HostContext, 'profile' | 'profileDir'>) => Promise<PackLocalPluginsResult>;
|
|
20
|
+
/**
|
|
21
|
+
* 默认 exec:把 `npm pack` 作为子进程执行并收集 stdout/stderr。
|
|
22
|
+
*
|
|
23
|
+
* **刻意不用 `execFile(..., { shell: true })`**:该组合会触发 Node 的 DEP0190
|
|
24
|
+
* (`shell:true` + args 数组只做拼接不做转义,存在注入面)。仓库既有解法是
|
|
25
|
+
* 在 Windows 上显式走 `cmd.exe /d /s /c "<转义后的命令行>"`(见 core/plugin-cli.ts
|
|
26
|
+
* 的 spawnShim + quoteCmdArg),本模块复用同一做法。
|
|
27
|
+
*
|
|
28
|
+
* 失败**不 reject**(把退出码交回调用方判定),只有 spawn 本身失败才 reject——
|
|
29
|
+
* 这样「npm 报错」与「npm 不存在」能被分开处理(前者是插件问题,后者是环境问题)。
|
|
30
|
+
*/
|
|
31
|
+
/** createPackExec 的注入面(测试用:假子进程 + 记录「整树终止」发生了没有) */
|
|
32
|
+
export interface PackExecDeps {
|
|
33
|
+
spawn?: typeof spawn;
|
|
34
|
+
/** 终止整棵进程树(缺省 core/plugin-cli.ts 的 killTree:Windows taskkill /t /f,POSIX 杀进程组) */
|
|
35
|
+
killTree?: (child: ChildProcess) => void;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 造一个真实 `npm pack` 执行器;默认导出实例给生产用,测试可注入 spawn / killTree。
|
|
39
|
+
*
|
|
40
|
+
* 超时为什么要整棵树:调用方只 `child.kill()` 时,Windows 死掉的只是 cmd.exe 包装进程,
|
|
41
|
+
* npm/pnpm 孙进程成孤儿继续写 tarball,而 `finally` 已经 rm 掉 packDir(审计 P2-3:
|
|
42
|
+
* 孤儿随后在已删除目录里写、或写出一份没人收的产物)。killTree 与 plugin-cli 的
|
|
43
|
+
* `dsh plugin` 子进程用同一实现(Windows taskkill /T /F;POSIX 负 pid 杀进程组 ——
|
|
44
|
+
* 因此 POSIX 上必须 `detached: true` 让子进程自成进程组,否则 -pid 会打到自己的组)。
|
|
45
|
+
*/
|
|
46
|
+
export declare function createPackExec(deps?: PackExecDeps): PackExec;
|
|
18
47
|
export interface LocalPluginPackHookOptions {
|
|
19
48
|
/** $DSH_HOME 绝对路径 */
|
|
20
49
|
homeDir: string;
|