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/adapters/plugins.ts
CHANGED
|
@@ -22,8 +22,11 @@ import { engineMsg } from './messages.ts';
|
|
|
22
22
|
import { stringifyJsonSafe } from '../utils/json.ts';
|
|
23
23
|
import { isPathSafe, normalizePath } from '../utils/paths.ts';
|
|
24
24
|
import { isENOENT } from '../utils/guards.ts';
|
|
25
|
+
import { findPatchKeyHazards } from '../utils/patch-yaml.ts';
|
|
26
|
+
import { patchKeyHazardMessage } from './patch-key-guard.ts';
|
|
25
27
|
import { PLUGIN_PATCH_REF_PREFIX, PLUGIN_PROFILE_MANIFEST_REF } from '../core/backup.ts';
|
|
26
28
|
import { readEffectivePatchLines, resolveWriteLayer, USER_PATCH_FILE } from '../core/patch-layers.ts';
|
|
29
|
+
import { applyPatchSecretGuard } from '../core/patch-secret-guard.ts';
|
|
27
30
|
import { stripMcpEntries } from './mcp.ts';
|
|
28
31
|
import {
|
|
29
32
|
parsePnpmPatchedDependencies, restorePnpmMachinePaths, sanitizePnpmWorkspacePatches, stripPnpmMachinePaths,
|
|
@@ -31,7 +34,7 @@ import {
|
|
|
31
34
|
import type { LocalPluginTarball, PatchLine, PluginEntry, PluginsSection, PnpmPatchFile } from '../schema/types.ts';
|
|
32
35
|
import type {
|
|
33
36
|
ApplyResult, ConfigAdapter, ExportOptions, ExportSection, ExportUnit, HostContext,
|
|
34
|
-
ImportContext, PlanItem, PluginAreaRef, SectionPreview, ValidationResult,
|
|
37
|
+
ImportContext, PlanItem, PluginAreaRef, SectionPreview, SensitiveHit, ValidationResult,
|
|
35
38
|
} from '../core/types.ts';
|
|
36
39
|
import { BUILTIN_PLUGIN_DATA_SOURCES, areasForSection } from '../core/plugin-data-registry.ts';
|
|
37
40
|
import type { PluginDataSource } from '../core/plugin-data-registry.ts';
|
|
@@ -279,6 +282,33 @@ async function measureLocalSources(plugins: PluginEntry[], ctx: HostContext): Pr
|
|
|
279
282
|
}
|
|
280
283
|
return sizes;
|
|
281
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* issue #136 A:读目标机**当前生效**的 patch 行原文(写回守卫的「本机现值」)。
|
|
287
|
+
*
|
|
288
|
+
* 口径与 analyzeImport 判定 Skip/Conflict 时逐字相同(readEffectivePatchLines + lineId 匹配),
|
|
289
|
+
* 这样「计划阶段看到的现值」与「写回阶段比对的现值」不会分叉。
|
|
290
|
+
*
|
|
291
|
+
* 读失败**不得**降级成「没有本机行」:那等于跳过守卫、静默覆盖本机明文(#136 的复现条件),
|
|
292
|
+
* 而且连一条告警都不会有。失败必须显式上抛给 applyItem,由它对这一行返回非致命失败。
|
|
293
|
+
*/
|
|
294
|
+
async function readLocalPatchRaw(
|
|
295
|
+
ctx: ImportContext,
|
|
296
|
+
lineId: string,
|
|
297
|
+
): Promise<{ ok: true; raw: unknown } | { ok: false; reason: string }> {
|
|
298
|
+
try {
|
|
299
|
+
const { lines, failures } = await readEffectivePatchLines(ctx.target.patchFile, ctx.target.profile);
|
|
300
|
+
// 有层读不出来 → 无法证明「本机这一行现在是什么 / 到底有没有」。
|
|
301
|
+
// readEffectivePatchLines 把层读失败收进 failures(不抛错),此前的实现只 catch 抛错、
|
|
302
|
+
// 于是「有层读不出来」被当成「本机没有这一行」放行 —— 守卫被绕过且零告警(#136 的复现条件)。
|
|
303
|
+
// 这里宁可跳过这一行并说明原因,也不按「无本机值」写入。
|
|
304
|
+
if (failures.length > 0) {
|
|
305
|
+
return { ok: false, reason: failures.map((f) => `${f.file}: ${f.reason}`).join('; ') };
|
|
306
|
+
}
|
|
307
|
+
return { ok: true, raw: lines.find((l) => l.lineId === lineId)?.raw };
|
|
308
|
+
} catch (err) {
|
|
309
|
+
return { ok: false, reason: err instanceof Error ? err.message : String(err) };
|
|
310
|
+
}
|
|
311
|
+
}
|
|
282
312
|
|
|
283
313
|
/**
|
|
284
314
|
* 由分区载荷派生单元清单(纯函数、零 I/O)。
|
|
@@ -371,6 +401,46 @@ export class PluginsAdapter implements ConfigAdapter<PluginsSection> {
|
|
|
371
401
|
async export(ctx: HostContext, options: ExportOptions): Promise<ExportSection<PluginsSection>> {
|
|
372
402
|
return this.collect(ctx, options, true);
|
|
373
403
|
}
|
|
404
|
+
/**
|
|
405
|
+
* issue #136 C:脱敏**归属标注** —— 把 SecretScanner 的命中路径归属回具体 patch 行。
|
|
406
|
+
*
|
|
407
|
+
* 为什么必须做:导出侧把命中字段名/值形状的字符串叶子替换成空串(占位符),语义是「需补录」。
|
|
408
|
+
* 目标机只看到空串,无法区分「被剥空」与「用户主动写空」——于是合并/导入落地时照抄空串,
|
|
409
|
+
* 静默抹掉本机同一行里用户手写的明文(行是整行写回的,行内没有粒度)。
|
|
410
|
+
*
|
|
411
|
+
* 归属规则:命中路径 `patch[i].<内部路径>` → 第 i 行的 `redactedPaths`(行内相对,原样保留
|
|
412
|
+
* 扫描器语法,如 `raw.mcp.args.token`);未命中的行不写该字段(缺省 = 未知,写回侧走保守分支)。
|
|
413
|
+
*
|
|
414
|
+
* 纯函数:不改入参;命中不落在 patch 行上(plugins / pnpmWorkspace / localTarballs 等)→ 原样返回。
|
|
415
|
+
*/
|
|
416
|
+
annotateRedactions(sanitized: unknown, hits: SensitiveHit[]): unknown {
|
|
417
|
+
if (hits.length === 0) return sanitized;
|
|
418
|
+
if (sanitized === null || typeof sanitized !== 'object') return sanitized;
|
|
419
|
+
const section = sanitized as PluginsSection;
|
|
420
|
+
if (!Array.isArray(section.patch) || section.patch.length === 0) return sanitized;
|
|
421
|
+
const byLine = new Map<number, Set<string>>();
|
|
422
|
+
for (const hit of hits) {
|
|
423
|
+
const m = /^patch\[(\d+)\]\.(.+)$/.exec(hit.path);
|
|
424
|
+
if (m === null) continue;
|
|
425
|
+
const index = Number(m[1]);
|
|
426
|
+
if (!Number.isInteger(index) || index < 0 || index >= section.patch.length) continue;
|
|
427
|
+
let paths = byLine.get(index);
|
|
428
|
+
if (paths === undefined) {
|
|
429
|
+
paths = new Set<string>();
|
|
430
|
+
byLine.set(index, paths);
|
|
431
|
+
}
|
|
432
|
+
const rel = m[2];
|
|
433
|
+
if (rel === undefined || rel === '') continue;
|
|
434
|
+
paths.add(rel);
|
|
435
|
+
}
|
|
436
|
+
if (byLine.size === 0) return sanitized;
|
|
437
|
+
const patch = section.patch.map((line, i) => {
|
|
438
|
+
const paths = byLine.get(i);
|
|
439
|
+
if (paths === undefined) return line;
|
|
440
|
+
return { ...line, redactedPaths: [...paths].sort() };
|
|
441
|
+
});
|
|
442
|
+
return { ...section, patch };
|
|
443
|
+
}
|
|
374
444
|
|
|
375
445
|
/**
|
|
376
446
|
* 只读预览(`/export-preview` 用):与 `export()` **同口径**,但不做任何本地源打包。
|
|
@@ -877,14 +947,27 @@ export class PluginsAdapter implements ConfigAdapter<PluginsSection> {
|
|
|
877
947
|
// 层身份随计划项走:applyItem 按 pl.file 写回,导入前快照按 target.file 记原行,
|
|
878
948
|
// 两者必须是同一层,否则回滚会把另一层改坏(issue #71)。
|
|
879
949
|
const layer = resolveWriteLayer(pl.file, ctx.target.profile);
|
|
950
|
+
// 与目标一致 ⇒ 一个字节都不会写,键名风险不归我们管(见下方守卫);先判 Skip 以免误报。
|
|
951
|
+
if (tl && isDeepStrictEqual(tl.raw, pl.raw)) {
|
|
952
|
+
items.push({ id, kind: 'Skip', adapter: 'plugins', description: msg('adapter.patchLineSame', { lineId: pl.lineId }), severity: 'info' });
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
// issue #137:写侧的 forceQuotes 只保**值**,键名仍会被 4.x 改判(空键更会让整份文件解析失败)。
|
|
956
|
+
// 命中即降级成本项「预判阻断」—— 本项不写、不触发整笔回滚,其余行照常导入。
|
|
957
|
+
const hazard = findPatchKeyHazards(pl.raw)[0];
|
|
958
|
+
if (hazard) {
|
|
959
|
+
items.push({
|
|
960
|
+
id, kind: 'Error', adapter: 'plugins',
|
|
961
|
+
description: patchKeyHazardMessage(msg, pl.lineId, hazard), severity: 'error',
|
|
962
|
+
});
|
|
963
|
+
continue;
|
|
964
|
+
}
|
|
880
965
|
if (!tl) {
|
|
881
966
|
items.push({
|
|
882
967
|
id, kind: 'Create', adapter: 'plugins',
|
|
883
968
|
description: msg('adapter.patchLineCreate', { lineId: pl.lineId }), severity: 'info',
|
|
884
969
|
target: { adapter: 'plugins', ref: pl.lineId, file: layer },
|
|
885
970
|
});
|
|
886
|
-
} else if (isDeepStrictEqual(tl.raw, pl.raw)) {
|
|
887
|
-
items.push({ id, kind: 'Skip', adapter: 'plugins', description: msg('adapter.patchLineSame', { lineId: pl.lineId }), severity: 'info' });
|
|
888
971
|
} else {
|
|
889
972
|
items.push({
|
|
890
973
|
id, kind: 'Conflict', adapter: 'plugins',
|
|
@@ -1079,11 +1162,48 @@ export class PluginsAdapter implements ConfigAdapter<PluginsSection> {
|
|
|
1079
1162
|
const data = ctx.sections.get('plugins') as PluginsSection | undefined;
|
|
1080
1163
|
const pl = data?.patch.find((p) => p.lineId === ref);
|
|
1081
1164
|
if (!pl) return { ok: false, message: msg('adapter.patchMissing', { ref }) };
|
|
1165
|
+
// issue #136 A:写回层守卫 —— 「被脱敏剥空」的叶子不得覆盖本机非空明文。
|
|
1166
|
+
//
|
|
1167
|
+
// 为什么在写回层做(而不是只靠导出侧标注):协同场景里对方的载荷来自更早版本、第三方工具或
|
|
1168
|
+
// 手工改过,`redactedPaths` 可能缺失;写回是最后一道能同时看见「本机现值」与「将被写下的值」
|
|
1169
|
+
// 的地方,也唯一能区分「被剥空」与「用户主动写空」(显式逐行 useImported 才放行清空)。
|
|
1170
|
+
// 守卫只在 Update/Conflict 上生效;Create 没有本机行可保(新行原样写入)。
|
|
1171
|
+
// 本机现值读不出来 → 无法证明这次写入不会覆盖明文,这一行**拒绝写**。
|
|
1172
|
+
// 非致命(warning):一行读不出来不该拖垮其余分区,也不该把整笔导入回滚。
|
|
1173
|
+
const localRead = item.kind === 'Create'
|
|
1174
|
+
? ({ ok: true as const, raw: undefined })
|
|
1175
|
+
: await readLocalPatchRaw(ctx, ref);
|
|
1176
|
+
if (!localRead.ok) {
|
|
1177
|
+
return {
|
|
1178
|
+
ok: false,
|
|
1179
|
+
warning: true,
|
|
1180
|
+
message: msg('adapter.patchLocalReadFailed', { ref: item.id, reason: localRead.reason }),
|
|
1181
|
+
};
|
|
1182
|
+
}
|
|
1183
|
+
const guard = applyPatchSecretGuard({
|
|
1184
|
+
importedRaw: pl.raw,
|
|
1185
|
+
localRaw: localRead.raw,
|
|
1186
|
+
redactedPaths: pl.redactedPaths,
|
|
1187
|
+
// 显式逐行确认 = 用户对本行选了 useImported(全局 replace 策略不算确认)
|
|
1188
|
+
// #136 A:只有**逐项**确认才算「我要这一行按导入值写」。判定必须是**白名单**:
|
|
1189
|
+
// 分区级裁决(decidedBy: 'section',由合并落地 planMergeApply 注入)只决定分区是否应用,
|
|
1190
|
+
// 不构成对本行的确认;来源缺省(未来新增的注入路径忘了标注)一律按「未确认」处理 ——
|
|
1191
|
+
// 默认放行等于给同类漏洞留后门;翻转成默认拒绝后,忘标注只会少清空、不会误清空。
|
|
1192
|
+
confirmed: item.conflict?.resolution === 'useImported' && item.conflict?.decidedBy === 'item',
|
|
1193
|
+
});
|
|
1082
1194
|
// 层身份按**语义**映射到目标机:来源是别的 profile 层时不能照抄名字(目标机未必有那个
|
|
1083
1195
|
// profile,宿主门面会拒绝),一律落到目标机当前 profile 层(与计划项 target.file 同口径)。
|
|
1084
1196
|
await ctx.target.patchFile.applyPatchChanges(resolveWriteLayer(pl.file, ctx.target.profile), [
|
|
1085
|
-
{ lineId: ref, raw:
|
|
1197
|
+
{ lineId: ref, raw: guard.raw, action: item.kind === 'Create' ? 'insert' : 'update' },
|
|
1086
1198
|
]);
|
|
1199
|
+
if (guard.kept.length > 0) {
|
|
1200
|
+
// 告警只报行 id 与字段路径,绝不带值(值本身就是密钥)
|
|
1201
|
+
// 报告面用**计划项 id**(`patch:<lineId>`)而不是裸 lineId:报告与计划项要能逐条对上
|
|
1202
|
+
return { ok: true, warning: true, needsRestart: true, message: msg('adapter.patchSecretsKept', { ref: item.id, paths: guard.kept.join(', ') }) };
|
|
1203
|
+
}
|
|
1204
|
+
if (guard.cleared.length > 0) {
|
|
1205
|
+
return { ok: true, warning: true, needsRestart: true, message: msg('adapter.patchSecretsCleared', { ref: item.id, paths: guard.cleared.join(', ') }) };
|
|
1206
|
+
}
|
|
1087
1207
|
return { ok: true, needsRestart: true, message: msg('adapter.patchWritten', { ref }) };
|
|
1088
1208
|
}
|
|
1089
1209
|
|
|
@@ -1119,6 +1239,23 @@ export class PluginsAdapter implements ConfigAdapter<PluginsSection> {
|
|
|
1119
1239
|
const pl = raw as unknown;
|
|
1120
1240
|
if (pl === null || typeof pl !== 'object') {
|
|
1121
1241
|
issues.push({ path: `patch[${i}]`, message: msg('adapter.validate.recordObject', { subject: 'patch line' }), severity: 'error' });
|
|
1242
|
+
continue;
|
|
1243
|
+
}
|
|
1244
|
+
// issue #136 C:脱敏归属路径(可选;存在时必须是字符串数组、元素非空,写回守卫按它精确比对)。
|
|
1245
|
+
// **空数组合法**:语义就是「本行没有剥空位置」,守卫按缺省(保守分支)处理。
|
|
1246
|
+
// 不把空数组当形状错误 —— 第三方 exporter 写出 `[]` 时拦下整包导入,代价远大于收益,
|
|
1247
|
+
// 而且凝死一个「字段在但内容空」与「字段缺省」不等价的契约,纯属自找兼容债。
|
|
1248
|
+
const redactedPaths = (pl as { redactedPaths?: unknown }).redactedPaths;
|
|
1249
|
+
if (redactedPaths !== undefined) {
|
|
1250
|
+
if (!Array.isArray(redactedPaths)) {
|
|
1251
|
+
issues.push({ path: `patch[${i}].redactedPaths`, message: msg('adapter.validate.array', { subject: 'patch[].redactedPaths' }), severity: 'error' });
|
|
1252
|
+
} else {
|
|
1253
|
+
for (const [j, p] of redactedPaths.entries()) {
|
|
1254
|
+
if (typeof p !== 'string' || p === '') {
|
|
1255
|
+
issues.push({ path: `patch[${i}].redactedPaths[${j}]`, message: msg('adapter.validate.string', { subject: 'patch[].redactedPaths[]' }), severity: 'error' });
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1122
1259
|
}
|
|
1123
1260
|
}
|
|
1124
1261
|
}
|
package/src/cli/actions.test.ts
CHANGED
|
@@ -15,11 +15,12 @@ import path from 'node:path'
|
|
|
15
15
|
|
|
16
16
|
import {
|
|
17
17
|
checkWriteGates, cleanupDisk, collectVerifyResults, diskUsageDirsOf, exportOfflineBackup, isProcessAlive,
|
|
18
|
-
planSnapshotRestore, readBackups, readDiskUsage, readLockState, readProfiles, readRescueStatus,
|
|
18
|
+
locksDirFromControlRoots, planSnapshotRestore, readBackups, readDiskUsage, readLockState, readProfiles, readRescueStatus,
|
|
19
19
|
readRunningInstances, readSnapshots, recoverStaleEnvironmentLock, repairSessionLogInline, repairSessions,
|
|
20
20
|
sessionRepairRowMessage, sessionVerifyNoteOf, stopProfile, unlockEncryptedBackup, type SessionLogRepairRow,
|
|
21
21
|
} from './actions.ts'
|
|
22
22
|
import type { SessionVerifyResult } from '../utils/session-verify.ts'
|
|
23
|
+
import { EnvironmentLockManager } from '../utils/env-lock.ts'
|
|
23
24
|
import { parseZip } from '../utils/zip.ts'
|
|
24
25
|
import { MANIFEST_FILE } from '../schema/manifest.ts'
|
|
25
26
|
|
|
@@ -574,3 +575,75 @@ test('C1 文案:不可修复项的回执指向字节级离线出口(不再
|
|
|
574
575
|
assert.equal(/sessions repair\s+--apply/.test(message), false, message)
|
|
575
576
|
})
|
|
576
577
|
})
|
|
578
|
+
/* ---------------- 审计 P11-4:环境锁目录必须由控制面根派生(就地修复此前漏传 locksDir) ---------------- */
|
|
579
|
+
|
|
580
|
+
test('P11-4 写门:locksDir 由控制面根派生(第一个真实带锁的候选根优先)', async () => {
|
|
581
|
+
await withTmp(async (tmp) => {
|
|
582
|
+
const first = path.join(tmp, 'a')
|
|
583
|
+
const second = path.join(tmp, 'b')
|
|
584
|
+
await fs.mkdir(path.join(second, 'locks'), { recursive: true })
|
|
585
|
+
await fs.writeFile(path.join(second, 'locks', 'environment.lock'), '{}')
|
|
586
|
+
assert.equal(locksDirFromControlRoots([first, second]), path.join(second, 'locks'), '跳过没锁的候选根,用真正持有锁的那个')
|
|
587
|
+
assert.equal(locksDirFromControlRoots([first]), path.join(first, 'locks'), '都没有锁就退回首个候选(读不到 = 无锁,判据仍归 readLockState)')
|
|
588
|
+
assert.equal(locksDirFromControlRoots([]), undefined, '空候选列表没有可派生的目录')
|
|
589
|
+
})
|
|
590
|
+
})
|
|
591
|
+
|
|
592
|
+
test('P11-10 写门:不传 locksDir 也不再跳过环境锁检查(派生在门内完成,5 个调用点不再各拼参数)', async () => {
|
|
593
|
+
await withTmp(async (tmp) => {
|
|
594
|
+
const dataDir = path.join(tmp, 'dsh-config-manager')
|
|
595
|
+
await fs.mkdir(dataDir, { recursive: true })
|
|
596
|
+
const locksDir = path.join(dataDir, 'locks')
|
|
597
|
+
const mgr = new EnvironmentLockManager({ locksDir, heartbeatIntervalMs: 60_000 })
|
|
598
|
+
const acquired = await mgr.acquire({ op: 'test-hold', target: 'p11-10' })
|
|
599
|
+
assert.equal(acquired.state, 'ACQUIRED')
|
|
600
|
+
try {
|
|
601
|
+
// 只有 controlRoots,没有 locksDir —— 改前这就是「静默不查锁」的形态
|
|
602
|
+
const derived = await checkWriteGates([dataDir], { needsDshStopped: false })
|
|
603
|
+
assert.equal(derived.ok, false, '锁被持有时必须拒绝,不能因少传参数放行')
|
|
604
|
+
assert.equal(derived.ok === false ? derived.code : null, 'stale-lock')
|
|
605
|
+
} finally {
|
|
606
|
+
if (acquired.token !== null) await mgr.release(acquired.token)
|
|
607
|
+
await mgr.stopHeartbeatAndDrain()
|
|
608
|
+
}
|
|
609
|
+
})
|
|
610
|
+
})
|
|
611
|
+
|
|
612
|
+
test('P11-4 写门:真实持有的环境锁 → 就地修复必须拒绝(改前不传 locksDir,锁在也照样改写字节)', async () => {
|
|
613
|
+
await withTmp(async (tmp) => {
|
|
614
|
+
const home = path.join(tmp, 'home')
|
|
615
|
+
const dataDir = path.join(tmp, 'dsh-config-manager')
|
|
616
|
+
await fs.mkdir(home, { recursive: true })
|
|
617
|
+
await fs.mkdir(dataDir, { recursive: true })
|
|
618
|
+
const locksDir = path.join(dataDir, 'locks')
|
|
619
|
+
// 真实持锁(不是手写 ownership 文件):持有者是本进程,状态应为 LOCKED
|
|
620
|
+
const mgr = new EnvironmentLockManager({ locksDir, heartbeatIntervalMs: 60_000 })
|
|
621
|
+
const acquired = await mgr.acquire({ op: 'test-hold', target: 'inline-repair' })
|
|
622
|
+
assert.equal(acquired.state, 'ACQUIRED')
|
|
623
|
+
try {
|
|
624
|
+
const held = await checkWriteGates([dataDir], { needsDshStopped: true, locksDir })
|
|
625
|
+
assert.equal(held.ok, false)
|
|
626
|
+
assert.equal(held.ok === false ? held.code : null, 'stale-lock')
|
|
627
|
+
|
|
628
|
+
// 不显式传 locksDir:必须靠 controlRoots 派生出来,否则锁在也放行(P11-4 的形态)
|
|
629
|
+
const refused = await repairSessionLogInline(
|
|
630
|
+
{ home, dataDir, unitIds: ['proj/session-x'], apply: true },
|
|
631
|
+
[dataDir],
|
|
632
|
+
)
|
|
633
|
+
assert.equal(refused.ok, false, '环境锁被持有仍放行 = P11-4 复现')
|
|
634
|
+
assert.match(refused.error ?? '', /环境锁/)
|
|
635
|
+
assert.equal(refused.rows.length, 0, '被门拒绝时不得逐条执行')
|
|
636
|
+
|
|
637
|
+
// 显式传同一个目录:判据相同,只换入口
|
|
638
|
+
const explicit = await repairSessionLogInline(
|
|
639
|
+
{ home, dataDir, unitIds: ['proj/session-x'], apply: true, locksDir },
|
|
640
|
+
[dataDir],
|
|
641
|
+
)
|
|
642
|
+
assert.equal(explicit.ok, false)
|
|
643
|
+
assert.match(explicit.error ?? '', /环境锁/)
|
|
644
|
+
} finally {
|
|
645
|
+
if (acquired.token !== null) await mgr.release(acquired.token)
|
|
646
|
+
await mgr.stopHeartbeatAndDrain()
|
|
647
|
+
}
|
|
648
|
+
})
|
|
649
|
+
})
|
package/src/cli/actions.ts
CHANGED
|
@@ -20,7 +20,7 @@ import {
|
|
|
20
20
|
EXPORTS_RETENTION_DEFAULT_MS, MARKET_RETENTION_DEFAULT_MS, TMP_RETENTION_DEFAULT_MS,
|
|
21
21
|
} from '../core/cache-cleaner.ts'
|
|
22
22
|
import { scanDiskUsage, type DiskUsageDirs, type DiskUsageReport } from '../core/disk-usage.ts'
|
|
23
|
-
import { readSafeModeMarkerSync
|
|
23
|
+
import { readSafeModeMarkerSync } from '../core/phase3-host.ts'
|
|
24
24
|
import { listSnapshots, type SnapshotMeta } from '../core/restore.ts'
|
|
25
25
|
import { readInstalledVersion, resolveProfileDir } from '../core/plugin-cli.ts'
|
|
26
26
|
import { DshProfileError, DshProfileManager } from '../profiles/dsh-profile-manager.ts'
|
|
@@ -31,75 +31,20 @@ import { readTextSafe } from '../profiles/dsh-profile-io.ts'
|
|
|
31
31
|
import {
|
|
32
32
|
AUTO_BACKUP_PREFIX, DEFAULT_BACKUP_RETENTION, listBackupFiles, type BackupFileMeta,
|
|
33
33
|
} from '../sync/backup-files.ts'
|
|
34
|
-
import { EnvironmentLockManager, OWNERSHIP_FILE } from '../utils/env-lock.ts'
|
|
35
34
|
import { scanSessionHealth, type SessionHealthScanResult } from '../utils/session-health-scan.ts'
|
|
36
35
|
import type { SessionVerifyResult } from '../utils/session-verify.ts'
|
|
37
36
|
import { formatDateStamp } from '../utils/time.ts'
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** 一个在跑的 DSH 实例(来自 `<root>/running/<profile>.json` 心跳)。 */
|
|
52
|
-
export interface RunningInstance {
|
|
53
|
-
name: string
|
|
54
|
-
pid: number
|
|
55
|
-
port: number | null
|
|
56
|
-
startedAt: string
|
|
57
|
-
updatedAt: number
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 读「本机正在跑的 DSH 实例」。
|
|
62
|
-
*
|
|
63
|
-
* **为什么按候选根扫描**:心跳在 `<插件 dataDir>/running/`,而 CLI 的 `--data-dir` 语义是
|
|
64
|
-
* 「快照目录」(缺省 `$DSH_HOME/dsh-config-manager/snapshots`)。旧实现把快照目录当 dataDir 用,
|
|
65
|
-
* 于是缺省路径下**永远找不到心跳** —— 那道「DSH 在跑就别写会话字节」的门形同虚设(fail-open)。
|
|
66
|
-
* 这里改为复用 `resolveControlRoots` 的候选根:显式 `--data-root` 只认它,否则同时看
|
|
67
|
-
* `--data-dir` 本身与它的父目录,再叠加缺省根;多查几处只会更保守。
|
|
68
|
-
*/
|
|
69
|
-
export async function readRunningInstances(controlRoots: readonly string[]): Promise<RunningInstance[]> {
|
|
70
|
-
const alive: RunningInstance[] = []
|
|
71
|
-
const seen = new Set<string>()
|
|
72
|
-
const now = Date.now()
|
|
73
|
-
for (const root of controlRoots) {
|
|
74
|
-
const dir = path.join(root, 'running')
|
|
75
|
-
let names: string[]
|
|
76
|
-
try {
|
|
77
|
-
names = (await fsp.readdir(dir)).filter((n) => n.endsWith('.json'))
|
|
78
|
-
} catch {
|
|
79
|
-
continue // 该候选根没有心跳目录:正常情况,不阻断
|
|
80
|
-
}
|
|
81
|
-
for (const name of names) {
|
|
82
|
-
try {
|
|
83
|
-
const record = parseRuntimeRecord(await fsp.readFile(path.join(dir, name), 'utf8'))
|
|
84
|
-
if (record === null) continue
|
|
85
|
-
if (!isProcessAlive(record.pid) || now - record.updatedAt > RUNTIME_STALE_MS) continue
|
|
86
|
-
const key = record.name + '#' + String(record.pid)
|
|
87
|
-
if (seen.has(key)) continue
|
|
88
|
-
seen.add(key)
|
|
89
|
-
alive.push({
|
|
90
|
-
name: record.name,
|
|
91
|
-
pid: record.pid,
|
|
92
|
-
port: record.port,
|
|
93
|
-
startedAt: record.startedAt,
|
|
94
|
-
updatedAt: record.updatedAt,
|
|
95
|
-
})
|
|
96
|
-
} catch {
|
|
97
|
-
// 单条心跳读不出来不阻断(best-effort)
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return alive
|
|
102
|
-
}
|
|
38
|
+
// 只读事实层与写入门已拆到独立模块(体量棘轮:本文件曾顶格 1574 行),re-export 保持既有调用点不变。
|
|
39
|
+
import {
|
|
40
|
+
isProcessAlive, readLockState, readRunningInstances, readSafeMode,
|
|
41
|
+
type LockReport, type RunningInstance, type SafeModeReport,
|
|
42
|
+
} from './control-state.ts'
|
|
43
|
+
import { checkWriteGates, locksDirFromControlRoots } from './write-gates.ts'
|
|
44
|
+
export { isProcessAlive, readLockState, readRunningInstances, readSafeMode } from './control-state.ts'
|
|
45
|
+
export type { LockReport, RunningInstance, SafeModeReport } from './control-state.ts'
|
|
46
|
+
export { checkWriteGates, SAFE_MODE_REFUSAL_CODE, locksDirFromControlRoots } from './write-gates.ts'
|
|
47
|
+
export type { WriteGateOutcome, WriteGateRefusal, WriteRefusalCode } from './write-gates.ts'
|
|
103
48
|
|
|
104
49
|
/* ------------------------------------------------------------ 备份自检(verify) */
|
|
105
50
|
|
|
@@ -232,69 +177,6 @@ export async function readSessionsHealth(homeDir: string): Promise<SessionsHealt
|
|
|
232
177
|
}
|
|
233
178
|
}
|
|
234
179
|
|
|
235
|
-
/* ------------------------------------------------------------ SAFE MODE / 环境锁 */
|
|
236
|
-
|
|
237
|
-
export interface SafeModeReport {
|
|
238
|
-
/** 候选控制面根 */
|
|
239
|
-
root: string
|
|
240
|
-
/** 标记文件绝对路径(写进界面,便于人工核对) */
|
|
241
|
-
marker: string
|
|
242
|
-
/** clear = 明确没有标记;blocked = 有未结案的 transaction;unknown = 读不出来(按阻断处理) */
|
|
243
|
-
state: ReturnType<typeof readSafeModeMarkerSync>
|
|
244
|
-
/**
|
|
245
|
-
* 该候选根下是否已经存在有效的「控制面目录」(dataDir 或 transactions)。
|
|
246
|
-
*
|
|
247
|
-
* 为什么需要:候选根里绝大多数**本来就不存在**(`--data-dir` 的父目录、缺省根都可能没有),
|
|
248
|
-
* 那些根的 'clear' 只是「这里什么都没建」,把它当成「SAFE MODE 未激活」的正面证据是把
|
|
249
|
-
* 「没查」说成「没问题」。界面只在**有效根**上给绿灯,其余如实说「未建立 / 未发现标记」。
|
|
250
|
-
*/
|
|
251
|
-
established: boolean
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
/** 逐个候选根读 SAFE MODE 标记(fail-closed 的判定仍在 CLI/core;这里只报事实)。 */
|
|
255
|
-
export function readSafeMode(controlRoots: readonly string[]): SafeModeReport[] {
|
|
256
|
-
return controlRoots.map((root) => ({
|
|
257
|
-
root,
|
|
258
|
-
marker: safeModeMarkerPath(root),
|
|
259
|
-
state: readSafeModeMarkerSync(root),
|
|
260
|
-
established: fssync.existsSync(root) || fssync.existsSync(path.join(root, 'transactions')),
|
|
261
|
-
}))
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
export interface LockReport {
|
|
265
|
-
locksDir: string
|
|
266
|
-
/** 锁文件是否存在(不存在 = 没有活动锁) */
|
|
267
|
-
present: boolean
|
|
268
|
-
/** 没有锁时恒为 'NONE';否则为 inspectLockState 的结论 */
|
|
269
|
-
state: string
|
|
270
|
-
detail?: string
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* 读环境锁状态(只读)。
|
|
275
|
-
*
|
|
276
|
-
* 复用宿主/CLI 同一套 `inspectLockState`:**绝不**因为「看起来是残留」就自动回收 ——
|
|
277
|
-
* 回收只有显式命令一条路(`recover-stale-lock`)。
|
|
278
|
-
*/
|
|
279
|
-
export async function readLockState(locksDir: string): Promise<LockReport> {
|
|
280
|
-
const present = await pathExists(path.join(locksDir, OWNERSHIP_FILE))
|
|
281
|
-
if (!present) return { locksDir, present: false, state: 'NONE' }
|
|
282
|
-
const lock = new EnvironmentLockManager({ locksDir, op: 'rescue-console-inspect', target: locksDir, lockVersion: '0.1.0' })
|
|
283
|
-
const inspection = await lock.inspectLockState()
|
|
284
|
-
return inspection.detail === undefined
|
|
285
|
-
? { locksDir, present: true, state: inspection.state }
|
|
286
|
-
: { locksDir, present: true, state: inspection.state, detail: inspection.detail }
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
async function pathExists(target: string): Promise<boolean> {
|
|
290
|
-
try {
|
|
291
|
-
await fsp.stat(target)
|
|
292
|
-
return true
|
|
293
|
-
} catch {
|
|
294
|
-
return false
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
180
|
/* ------------------------------------------------------------ 首页聚合 */
|
|
299
181
|
|
|
300
182
|
/** 救急台首页所需的路径事实(由 CLI 侧解析后注入,保证与各子命令同一口径)。 */
|
|
@@ -389,119 +271,6 @@ function errorText(error: unknown): string {
|
|
|
389
271
|
- 回执如实:完成、失败、跳过分别列出,绝不把「没做」说成「做了」。
|
|
390
272
|
*/
|
|
391
273
|
|
|
392
|
-
export type WriteRefusalCode =
|
|
393
|
-
| 'safe-mode'
|
|
394
|
-
| 'stale-lock'
|
|
395
|
-
| 'dsh-running'
|
|
396
|
-
| 'nothing-to-do'
|
|
397
|
-
| 'io-error'
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* SAFE MODE 拒因码的**单点常量**:`src/cli/index.ts` 的源码守卫(P0-11-07)禁止
|
|
401
|
-
* CLI 自带 `'safe-mode'` 字面量,判据必须复用本常量(否则改一处漏一处)。
|
|
402
|
-
*/
|
|
403
|
-
export const SAFE_MODE_REFUSAL_CODE: WriteRefusalCode = 'safe-mode'
|
|
404
|
-
|
|
405
|
-
export interface WriteGateRefusal {
|
|
406
|
-
ok: false
|
|
407
|
-
code: WriteRefusalCode
|
|
408
|
-
/** 用户可读原因(页面直接展示) */
|
|
409
|
-
reason: string
|
|
410
|
-
/**
|
|
411
|
-
* 拒因的结构化明细(页面据此给出「怎么解除」的可操作指引,而不是只丢一句 reason)。
|
|
412
|
-
*
|
|
413
|
-
* 为什么要有:`dsh-running` 是最常被撞到的一道门,光给「请先关闭 DSH」用户会问
|
|
414
|
-
* 「关哪个、怎么确认关掉了」。这里把**是谁在跑**(心跳台账里的 profile/pid/端口)
|
|
415
|
-
* 如实带出来 —— 只读事实,不猜、不代关进程。
|
|
416
|
-
*/
|
|
417
|
-
detail?: {
|
|
418
|
-
kind: 'dsh-running' | 'stale-lock' | 'safe-mode'
|
|
419
|
-
/** 正在运行的实例(心跳合并视图:本插件启动的 + 手动启动的) */
|
|
420
|
-
instances?: Array<{ name: string; pid: number; port: number | null }>
|
|
421
|
-
/** 环境锁状态(kind = 'stale-lock') */
|
|
422
|
-
lock?: { state: string; locksDir: string }
|
|
423
|
-
/** SAFE MODE 标记文件(kind = 'safe-mode') */
|
|
424
|
-
markers?: string[]
|
|
425
|
-
/** 与 CLI 逐字同源的等价命令(可复制去终端跑) */
|
|
426
|
-
commands: string[]
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
export type WriteGateOutcome = { ok: true } | WriteGateRefusal
|
|
431
|
-
|
|
432
|
-
/**
|
|
433
|
-
* 写动作的前置门(与 CLI `sessions repair --fix` 同一判据)。
|
|
434
|
-
*
|
|
435
|
-
* `needsDshStopped`:**改写/搬动会话字节**的动作(会话布局归位 repairSessions、字节级就地修复
|
|
436
|
-
* repairSessionLogInline)与**快照恢复**(runSnapshotRestore 会覆盖/删除 `$DSH_HOME` 下正在使用的文件)
|
|
437
|
-
* 都要「DSH 已停」这一条;磁盘清理不需要(只碰可重建缓存)。
|
|
438
|
-
*/
|
|
439
|
-
export async function checkWriteGates(
|
|
440
|
-
controlRoots: readonly string[],
|
|
441
|
-
options: { needsDshStopped: boolean; locksDir?: string } = { needsDshStopped: true },
|
|
442
|
-
): Promise<WriteGateOutcome> {
|
|
443
|
-
for (const report of readSafeMode(controlRoots)) {
|
|
444
|
-
if (report.state === 'blocked') {
|
|
445
|
-
return {
|
|
446
|
-
ok: false,
|
|
447
|
-
code: 'safe-mode',
|
|
448
|
-
reason: 'SAFE MODE 已激活(存在未结案的配置 transaction):先处理恢复事项再改磁盘。标记:' + report.marker,
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
if (report.state === 'unknown') {
|
|
452
|
-
return {
|
|
453
|
-
ok: false,
|
|
454
|
-
code: 'safe-mode',
|
|
455
|
-
reason: 'SAFE MODE 标记无法判定(按最保守口径处理):先人工核对标记文件。路径:' + report.marker,
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* ② 环境锁:**存在锁就必须先处理**。
|
|
461
|
-
*
|
|
462
|
-
* 为什么不能跳过:CLI 侧 destructive 入口(restore/reinstall)都要求成功获取 GLOBAL 环境锁,
|
|
463
|
-
* 而残留锁(STALE_LOCK_DETECTED)正是「上次异常退出、别的操作也被挡着」的状态;
|
|
464
|
-
* 这时改磁盘等于在一个已知不一致的状态上继续写。判据与 `recover-stale-lock` 同源(inspectLockState),
|
|
465
|
-
* 所以**不给自动回收**,只如实拒绝并把出口指出来。
|
|
466
|
-
*/
|
|
467
|
-
if (options.locksDir !== undefined) {
|
|
468
|
-
const lock = await readLockState(options.locksDir)
|
|
469
|
-
if (lock.present) {
|
|
470
|
-
const stale = lock.state === 'STALE_LOCK_DETECTED'
|
|
471
|
-
return {
|
|
472
|
-
ok: false,
|
|
473
|
-
code: 'stale-lock',
|
|
474
|
-
reason: (stale
|
|
475
|
-
? '检测到残留的环境锁(持有进程已被确证不存在):先回收它再执行写动作。'
|
|
476
|
-
: '环境锁被持有或状态无法判定(' + lock.state + '):先确认没有其它操作在跑,再执行写动作。')
|
|
477
|
-
+ '锁目录:' + lock.locksDir + ';出口:dsh-config-manager recover-stale-lock(本机救急台的「环境锁」页也可回收)。',
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
if (options.needsDshStopped) {
|
|
482
|
-
const running = await readRunningInstances(controlRoots)
|
|
483
|
-
if (running.length > 0) {
|
|
484
|
-
return {
|
|
485
|
-
ok: false,
|
|
486
|
-
code: 'dsh-running',
|
|
487
|
-
reason: '检测到 DSH 正在运行(' + running.map((i) => i.name + ' pid=' + String(i.pid)).join('、')
|
|
488
|
-
+ '):请先关闭 DSH 再改写会话字节。',
|
|
489
|
-
detail: {
|
|
490
|
-
kind: 'dsh-running',
|
|
491
|
-
instances: running.map((i) => ({ name: i.name, pid: i.pid, port: i.port })),
|
|
492
|
-
commands: [
|
|
493
|
-
// 桌面端独占档案不让普通 CLI 停,如实给出「关窗口」这条唯一通道(见 AGENTS.md 的 desktop 条)
|
|
494
|
-
...running.filter((i) => i.name === 'desktop').map(() => '桌面端(desktop):直接关闭 DSH 桌面窗口;它不接受命令行停止'),
|
|
495
|
-
...running.filter((i) => i.name !== 'desktop').map((i) => 'dsh-config-manager stop ' + i.name + ' # 或在「档案与实例」页点「停止 ' + i.name + '」'),
|
|
496
|
-
'dsh-config-manager web # 停完再回本页点「重新检查」',
|
|
497
|
-
],
|
|
498
|
-
},
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
return { ok: true }
|
|
503
|
-
}
|
|
504
|
-
|
|
505
274
|
/** 会话修复步骤的机器可读形态(页面渲染用;CLI 走它自己的文本输出)。 */
|
|
506
275
|
export interface RepairStep {
|
|
507
276
|
kind: 'move' | 'rewrite-move' | 'quarantine' | 'skip'
|
|
@@ -662,7 +431,12 @@ export function sessionRepairRowMessage(facts: SessionRepairRowFacts): string {
|
|
|
662
431
|
* 而 `session-repair-service` 的 CLI 出入口此前只挂在应用内通道上 —— 离线时用户只能敲命令。
|
|
663
432
|
*/
|
|
664
433
|
export async function repairSessionLogInline(
|
|
665
|
-
options: {
|
|
434
|
+
options: {
|
|
435
|
+
home: string; dataDir: string; unitIds: readonly string[]; apply: boolean
|
|
436
|
+
expects?: Record<string, { size: number; mtimeMs: number }>
|
|
437
|
+
/** 环境锁目录(缺省由 controlRoots 派生;显式给出时优先) */
|
|
438
|
+
locksDir?: string
|
|
439
|
+
},
|
|
666
440
|
controlRoots: readonly string[],
|
|
667
441
|
): Promise<InlineRepairOutcome> {
|
|
668
442
|
const base: InlineRepairOutcome = {
|
|
@@ -675,8 +449,13 @@ export async function repairSessionLogInline(
|
|
|
675
449
|
}
|
|
676
450
|
if (options.unitIds.length === 0) return { ...base, ok: false, error: '没有选中任何会话单元。' }
|
|
677
451
|
if (options.apply) {
|
|
678
|
-
// 与 CLI 写动作共用同一道写入门:改写会话字节必须先停 DSH(+ SAFE MODE +
|
|
679
|
-
|
|
452
|
+
// 与 CLI 写动作共用同一道写入门:改写会话字节必须先停 DSH(+ SAFE MODE + 环境锁)。
|
|
453
|
+
// 环境锁目录由控制面根派生(审计 P11-4:此前不传 locksDir,锁存在时也照样改写字节)。
|
|
454
|
+
const locksDir = options.locksDir ?? locksDirFromControlRoots(controlRoots)
|
|
455
|
+
const gate = await checkWriteGates(controlRoots, {
|
|
456
|
+
needsDshStopped: true,
|
|
457
|
+
...(locksDir !== undefined ? { locksDir } : {}),
|
|
458
|
+
})
|
|
680
459
|
if (!gate.ok) return { ...base, ok: false, error: gate.reason }
|
|
681
460
|
}
|
|
682
461
|
const svc = await import('../utils/session-repair-service.ts')
|
|
@@ -741,9 +520,11 @@ export async function repairSessions(
|
|
|
741
520
|
done: [], errors: [], notices: [],
|
|
742
521
|
}
|
|
743
522
|
if (options.fix) {
|
|
523
|
+
// 环境锁目录同样由根派生:调用点漏传时不再静默跳过环境锁检查(与 repairSessionLogInline 同一口径)
|
|
524
|
+
const locksDir = options.locksDir ?? locksDirFromControlRoots(controlRoots)
|
|
744
525
|
const gate = await checkWriteGates(controlRoots, {
|
|
745
526
|
needsDshStopped: true,
|
|
746
|
-
...(
|
|
527
|
+
...(locksDir !== undefined ? { locksDir } : {}),
|
|
747
528
|
})
|
|
748
529
|
if (!gate.ok) {
|
|
749
530
|
return { ...base, ok: false, error: gate.reason, errors: [gate.reason] }
|