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/cli/actions.d.ts
CHANGED
|
@@ -1,30 +1,14 @@
|
|
|
1
1
|
import { type BackupVerifyResult } from '../core/backup-verify.ts';
|
|
2
2
|
import { type DiskUsageDirs, type DiskUsageReport } from '../core/disk-usage.ts';
|
|
3
|
-
import { readSafeModeMarkerSync } from '../core/phase3-host.ts';
|
|
4
3
|
import { type SnapshotMeta } from '../core/restore.ts';
|
|
5
4
|
import { type BackupFileMeta } from '../sync/backup-files.ts';
|
|
6
5
|
import { type SessionHealthScanResult } from '../utils/session-health-scan.ts';
|
|
7
6
|
import type { SessionVerifyResult } from '../utils/session-verify.ts';
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
pid: number;
|
|
14
|
-
port: number | null;
|
|
15
|
-
startedAt: string;
|
|
16
|
-
updatedAt: number;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* 读「本机正在跑的 DSH 实例」。
|
|
20
|
-
*
|
|
21
|
-
* **为什么按候选根扫描**:心跳在 `<插件 dataDir>/running/`,而 CLI 的 `--data-dir` 语义是
|
|
22
|
-
* 「快照目录」(缺省 `$DSH_HOME/dsh-config-manager/snapshots`)。旧实现把快照目录当 dataDir 用,
|
|
23
|
-
* 于是缺省路径下**永远找不到心跳** —— 那道「DSH 在跑就别写会话字节」的门形同虚设(fail-open)。
|
|
24
|
-
* 这里改为复用 `resolveControlRoots` 的候选根:显式 `--data-root` 只认它,否则同时看
|
|
25
|
-
* `--data-dir` 本身与它的父目录,再叠加缺省根;多查几处只会更保守。
|
|
26
|
-
*/
|
|
27
|
-
export declare function readRunningInstances(controlRoots: readonly string[]): Promise<RunningInstance[]>;
|
|
7
|
+
import { type LockReport, type RunningInstance, type SafeModeReport } from './control-state.ts';
|
|
8
|
+
export { isProcessAlive, readLockState, readRunningInstances, readSafeMode } from './control-state.ts';
|
|
9
|
+
export type { LockReport, RunningInstance, SafeModeReport } from './control-state.ts';
|
|
10
|
+
export { checkWriteGates, SAFE_MODE_REFUSAL_CODE, locksDirFromControlRoots } from './write-gates.ts';
|
|
11
|
+
export type { WriteGateOutcome, WriteGateRefusal, WriteRefusalCode } from './write-gates.ts';
|
|
28
12
|
export type VerifyOutcome = {
|
|
29
13
|
ok: true;
|
|
30
14
|
results: BackupVerifyResult[];
|
|
@@ -78,39 +62,6 @@ export type SessionsHealthOutcome = {
|
|
|
78
62
|
};
|
|
79
63
|
/** 只读会话体检(结构档 + 限额内的行档)。scanSessionHealth 本身不写任何字节。 */
|
|
80
64
|
export declare function readSessionsHealth(homeDir: string): Promise<SessionsHealthOutcome>;
|
|
81
|
-
export interface SafeModeReport {
|
|
82
|
-
/** 候选控制面根 */
|
|
83
|
-
root: string;
|
|
84
|
-
/** 标记文件绝对路径(写进界面,便于人工核对) */
|
|
85
|
-
marker: string;
|
|
86
|
-
/** clear = 明确没有标记;blocked = 有未结案的 transaction;unknown = 读不出来(按阻断处理) */
|
|
87
|
-
state: ReturnType<typeof readSafeModeMarkerSync>;
|
|
88
|
-
/**
|
|
89
|
-
* 该候选根下是否已经存在有效的「控制面目录」(dataDir 或 transactions)。
|
|
90
|
-
*
|
|
91
|
-
* 为什么需要:候选根里绝大多数**本来就不存在**(`--data-dir` 的父目录、缺省根都可能没有),
|
|
92
|
-
* 那些根的 'clear' 只是「这里什么都没建」,把它当成「SAFE MODE 未激活」的正面证据是把
|
|
93
|
-
* 「没查」说成「没问题」。界面只在**有效根**上给绿灯,其余如实说「未建立 / 未发现标记」。
|
|
94
|
-
*/
|
|
95
|
-
established: boolean;
|
|
96
|
-
}
|
|
97
|
-
/** 逐个候选根读 SAFE MODE 标记(fail-closed 的判定仍在 CLI/core;这里只报事实)。 */
|
|
98
|
-
export declare function readSafeMode(controlRoots: readonly string[]): SafeModeReport[];
|
|
99
|
-
export interface LockReport {
|
|
100
|
-
locksDir: string;
|
|
101
|
-
/** 锁文件是否存在(不存在 = 没有活动锁) */
|
|
102
|
-
present: boolean;
|
|
103
|
-
/** 没有锁时恒为 'NONE';否则为 inspectLockState 的结论 */
|
|
104
|
-
state: string;
|
|
105
|
-
detail?: string;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* 读环境锁状态(只读)。
|
|
109
|
-
*
|
|
110
|
-
* 复用宿主/CLI 同一套 `inspectLockState`:**绝不**因为「看起来是残留」就自动回收 ——
|
|
111
|
-
* 回收只有显式命令一条路(`recover-stale-lock`)。
|
|
112
|
-
*/
|
|
113
|
-
export declare function readLockState(locksDir: string): Promise<LockReport>;
|
|
114
65
|
/** 救急台首页所需的路径事实(由 CLI 侧解析后注入,保证与各子命令同一口径)。 */
|
|
115
66
|
export interface RescuePaths {
|
|
116
67
|
homeDir: string;
|
|
@@ -141,57 +92,6 @@ export interface RescueStatus {
|
|
|
141
92
|
}
|
|
142
93
|
/** 首页聚合:每一项各自 try/catch,缺一项不影响其余(但失败必须在 errors 里可见)。 */
|
|
143
94
|
export declare function readRescueStatus(paths: RescuePaths, now?: () => Date): Promise<RescueStatus>;
|
|
144
|
-
export type WriteRefusalCode = 'safe-mode' | 'stale-lock' | 'dsh-running' | 'nothing-to-do' | 'io-error';
|
|
145
|
-
/**
|
|
146
|
-
* SAFE MODE 拒因码的**单点常量**:`src/cli/index.ts` 的源码守卫(P0-11-07)禁止
|
|
147
|
-
* CLI 自带 `'safe-mode'` 字面量,判据必须复用本常量(否则改一处漏一处)。
|
|
148
|
-
*/
|
|
149
|
-
export declare const SAFE_MODE_REFUSAL_CODE: WriteRefusalCode;
|
|
150
|
-
export interface WriteGateRefusal {
|
|
151
|
-
ok: false;
|
|
152
|
-
code: WriteRefusalCode;
|
|
153
|
-
/** 用户可读原因(页面直接展示) */
|
|
154
|
-
reason: string;
|
|
155
|
-
/**
|
|
156
|
-
* 拒因的结构化明细(页面据此给出「怎么解除」的可操作指引,而不是只丢一句 reason)。
|
|
157
|
-
*
|
|
158
|
-
* 为什么要有:`dsh-running` 是最常被撞到的一道门,光给「请先关闭 DSH」用户会问
|
|
159
|
-
* 「关哪个、怎么确认关掉了」。这里把**是谁在跑**(心跳台账里的 profile/pid/端口)
|
|
160
|
-
* 如实带出来 —— 只读事实,不猜、不代关进程。
|
|
161
|
-
*/
|
|
162
|
-
detail?: {
|
|
163
|
-
kind: 'dsh-running' | 'stale-lock' | 'safe-mode';
|
|
164
|
-
/** 正在运行的实例(心跳合并视图:本插件启动的 + 手动启动的) */
|
|
165
|
-
instances?: Array<{
|
|
166
|
-
name: string;
|
|
167
|
-
pid: number;
|
|
168
|
-
port: number | null;
|
|
169
|
-
}>;
|
|
170
|
-
/** 环境锁状态(kind = 'stale-lock') */
|
|
171
|
-
lock?: {
|
|
172
|
-
state: string;
|
|
173
|
-
locksDir: string;
|
|
174
|
-
};
|
|
175
|
-
/** SAFE MODE 标记文件(kind = 'safe-mode') */
|
|
176
|
-
markers?: string[];
|
|
177
|
-
/** 与 CLI 逐字同源的等价命令(可复制去终端跑) */
|
|
178
|
-
commands: string[];
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
export type WriteGateOutcome = {
|
|
182
|
-
ok: true;
|
|
183
|
-
} | WriteGateRefusal;
|
|
184
|
-
/**
|
|
185
|
-
* 写动作的前置门(与 CLI `sessions repair --fix` 同一判据)。
|
|
186
|
-
*
|
|
187
|
-
* `needsDshStopped`:**改写/搬动会话字节**的动作(会话布局归位 repairSessions、字节级就地修复
|
|
188
|
-
* repairSessionLogInline)与**快照恢复**(runSnapshotRestore 会覆盖/删除 `$DSH_HOME` 下正在使用的文件)
|
|
189
|
-
* 都要「DSH 已停」这一条;磁盘清理不需要(只碰可重建缓存)。
|
|
190
|
-
*/
|
|
191
|
-
export declare function checkWriteGates(controlRoots: readonly string[], options?: {
|
|
192
|
-
needsDshStopped: boolean;
|
|
193
|
-
locksDir?: string;
|
|
194
|
-
}): Promise<WriteGateOutcome>;
|
|
195
95
|
/** 会话修复步骤的机器可读形态(页面渲染用;CLI 走它自己的文本输出)。 */
|
|
196
96
|
export interface RepairStep {
|
|
197
97
|
kind: 'move' | 'rewrite-move' | 'quarantine' | 'skip';
|
|
@@ -323,6 +223,8 @@ export declare function repairSessionLogInline(options: {
|
|
|
323
223
|
size: number;
|
|
324
224
|
mtimeMs: number;
|
|
325
225
|
}>;
|
|
226
|
+
/** 环境锁目录(缺省由 controlRoots 派生;显式给出时优先) */
|
|
227
|
+
locksDir?: string;
|
|
326
228
|
}, controlRoots: readonly string[]): Promise<InlineRepairOutcome>;
|
|
327
229
|
/**
|
|
328
230
|
* 会话布局修复:`dryRun: true` 时**零写入**(与 `sessions repair` 缺省行为一致)。
|
package/lib/cli/actions.js
CHANGED
|
@@ -17,7 +17,7 @@ import path from 'node:path';
|
|
|
17
17
|
import { verifyBackupZip } from '../core/backup-verify.js';
|
|
18
18
|
import { EXPORTS_RETENTION_DEFAULT_MS, MARKET_RETENTION_DEFAULT_MS, TMP_RETENTION_DEFAULT_MS, } from '../core/cache-cleaner.js';
|
|
19
19
|
import { scanDiskUsage } from '../core/disk-usage.js';
|
|
20
|
-
import { readSafeModeMarkerSync
|
|
20
|
+
import { readSafeModeMarkerSync } from '../core/phase3-host.js';
|
|
21
21
|
import { listSnapshots } from '../core/restore.js';
|
|
22
22
|
import { readInstalledVersion, resolveProfileDir } from '../core/plugin-cli.js';
|
|
23
23
|
import { DshProfileError, DshProfileManager } from '../profiles/dsh-profile-manager.js';
|
|
@@ -26,68 +26,13 @@ import { DshProfileRuntimeRegistry, parseRuntimeRecord, RUNTIME_STALE_MS } from
|
|
|
26
26
|
import { isManagedProfileName } from '../profiles/dsh-profile-shared.js';
|
|
27
27
|
import { readTextSafe } from '../profiles/dsh-profile-io.js';
|
|
28
28
|
import { AUTO_BACKUP_PREFIX, DEFAULT_BACKUP_RETENTION, listBackupFiles, } from '../sync/backup-files.js';
|
|
29
|
-
import { EnvironmentLockManager, OWNERSHIP_FILE } from '../utils/env-lock.js';
|
|
30
29
|
import { scanSessionHealth } from '../utils/session-health-scan.js';
|
|
31
30
|
import { formatDateStamp } from '../utils/time.js';
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
39
|
-
catch (error) {
|
|
40
|
-
return error.code === 'EPERM';
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* 读「本机正在跑的 DSH 实例」。
|
|
45
|
-
*
|
|
46
|
-
* **为什么按候选根扫描**:心跳在 `<插件 dataDir>/running/`,而 CLI 的 `--data-dir` 语义是
|
|
47
|
-
* 「快照目录」(缺省 `$DSH_HOME/dsh-config-manager/snapshots`)。旧实现把快照目录当 dataDir 用,
|
|
48
|
-
* 于是缺省路径下**永远找不到心跳** —— 那道「DSH 在跑就别写会话字节」的门形同虚设(fail-open)。
|
|
49
|
-
* 这里改为复用 `resolveControlRoots` 的候选根:显式 `--data-root` 只认它,否则同时看
|
|
50
|
-
* `--data-dir` 本身与它的父目录,再叠加缺省根;多查几处只会更保守。
|
|
51
|
-
*/
|
|
52
|
-
export async function readRunningInstances(controlRoots) {
|
|
53
|
-
const alive = [];
|
|
54
|
-
const seen = new Set();
|
|
55
|
-
const now = Date.now();
|
|
56
|
-
for (const root of controlRoots) {
|
|
57
|
-
const dir = path.join(root, 'running');
|
|
58
|
-
let names;
|
|
59
|
-
try {
|
|
60
|
-
names = (await fsp.readdir(dir)).filter((n) => n.endsWith('.json'));
|
|
61
|
-
}
|
|
62
|
-
catch {
|
|
63
|
-
continue; // 该候选根没有心跳目录:正常情况,不阻断
|
|
64
|
-
}
|
|
65
|
-
for (const name of names) {
|
|
66
|
-
try {
|
|
67
|
-
const record = parseRuntimeRecord(await fsp.readFile(path.join(dir, name), 'utf8'));
|
|
68
|
-
if (record === null)
|
|
69
|
-
continue;
|
|
70
|
-
if (!isProcessAlive(record.pid) || now - record.updatedAt > RUNTIME_STALE_MS)
|
|
71
|
-
continue;
|
|
72
|
-
const key = record.name + '#' + String(record.pid);
|
|
73
|
-
if (seen.has(key))
|
|
74
|
-
continue;
|
|
75
|
-
seen.add(key);
|
|
76
|
-
alive.push({
|
|
77
|
-
name: record.name,
|
|
78
|
-
pid: record.pid,
|
|
79
|
-
port: record.port,
|
|
80
|
-
startedAt: record.startedAt,
|
|
81
|
-
updatedAt: record.updatedAt,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
// 单条心跳读不出来不阻断(best-effort)
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return alive;
|
|
90
|
-
}
|
|
31
|
+
// 只读事实层与写入门已拆到独立模块(体量棘轮:本文件曾顶格 1574 行),re-export 保持既有调用点不变。
|
|
32
|
+
import { isProcessAlive, readLockState, readRunningInstances, readSafeMode, } from './control-state.js';
|
|
33
|
+
import { checkWriteGates, locksDirFromControlRoots } from './write-gates.js';
|
|
34
|
+
export { isProcessAlive, readLockState, readRunningInstances, readSafeMode } from './control-state.js';
|
|
35
|
+
export { checkWriteGates, SAFE_MODE_REFUSAL_CODE, locksDirFromControlRoots } from './write-gates.js';
|
|
91
36
|
/**
|
|
92
37
|
* 定位待校验目标:显式目标(文件名或路径)优先,否则列出导出目录下全部 *.zip(名称升序)。
|
|
93
38
|
* 与 `dsh-config-manager verify` 同一实现(CLI 与网页共用)。
|
|
@@ -194,40 +139,6 @@ export async function readSessionsHealth(homeDir) {
|
|
|
194
139
|
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
195
140
|
}
|
|
196
141
|
}
|
|
197
|
-
/** 逐个候选根读 SAFE MODE 标记(fail-closed 的判定仍在 CLI/core;这里只报事实)。 */
|
|
198
|
-
export function readSafeMode(controlRoots) {
|
|
199
|
-
return controlRoots.map((root) => ({
|
|
200
|
-
root,
|
|
201
|
-
marker: safeModeMarkerPath(root),
|
|
202
|
-
state: readSafeModeMarkerSync(root),
|
|
203
|
-
established: fssync.existsSync(root) || fssync.existsSync(path.join(root, 'transactions')),
|
|
204
|
-
}));
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* 读环境锁状态(只读)。
|
|
208
|
-
*
|
|
209
|
-
* 复用宿主/CLI 同一套 `inspectLockState`:**绝不**因为「看起来是残留」就自动回收 ——
|
|
210
|
-
* 回收只有显式命令一条路(`recover-stale-lock`)。
|
|
211
|
-
*/
|
|
212
|
-
export async function readLockState(locksDir) {
|
|
213
|
-
const present = await pathExists(path.join(locksDir, OWNERSHIP_FILE));
|
|
214
|
-
if (!present)
|
|
215
|
-
return { locksDir, present: false, state: 'NONE' };
|
|
216
|
-
const lock = new EnvironmentLockManager({ locksDir, op: 'rescue-console-inspect', target: locksDir, lockVersion: '0.1.0' });
|
|
217
|
-
const inspection = await lock.inspectLockState();
|
|
218
|
-
return inspection.detail === undefined
|
|
219
|
-
? { locksDir, present: true, state: inspection.state }
|
|
220
|
-
: { locksDir, present: true, state: inspection.state, detail: inspection.detail };
|
|
221
|
-
}
|
|
222
|
-
async function pathExists(target) {
|
|
223
|
-
try {
|
|
224
|
-
await fsp.stat(target);
|
|
225
|
-
return true;
|
|
226
|
-
}
|
|
227
|
-
catch {
|
|
228
|
-
return false;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
142
|
/** 首页聚合:每一项各自 try/catch,缺一项不影响其余(但失败必须在 errors 里可见)。 */
|
|
232
143
|
export async function readRescueStatus(paths, now = () => new Date()) {
|
|
233
144
|
const errors = [];
|
|
@@ -280,80 +191,6 @@ export async function readRescueStatus(paths, now = () => new Date()) {
|
|
|
280
191
|
function errorText(error) {
|
|
281
192
|
return error instanceof Error ? error.message : String(error);
|
|
282
193
|
}
|
|
283
|
-
/**
|
|
284
|
-
* SAFE MODE 拒因码的**单点常量**:`src/cli/index.ts` 的源码守卫(P0-11-07)禁止
|
|
285
|
-
* CLI 自带 `'safe-mode'` 字面量,判据必须复用本常量(否则改一处漏一处)。
|
|
286
|
-
*/
|
|
287
|
-
export const SAFE_MODE_REFUSAL_CODE = 'safe-mode';
|
|
288
|
-
/**
|
|
289
|
-
* 写动作的前置门(与 CLI `sessions repair --fix` 同一判据)。
|
|
290
|
-
*
|
|
291
|
-
* `needsDshStopped`:**改写/搬动会话字节**的动作(会话布局归位 repairSessions、字节级就地修复
|
|
292
|
-
* repairSessionLogInline)与**快照恢复**(runSnapshotRestore 会覆盖/删除 `$DSH_HOME` 下正在使用的文件)
|
|
293
|
-
* 都要「DSH 已停」这一条;磁盘清理不需要(只碰可重建缓存)。
|
|
294
|
-
*/
|
|
295
|
-
export async function checkWriteGates(controlRoots, options = { needsDshStopped: true }) {
|
|
296
|
-
for (const report of readSafeMode(controlRoots)) {
|
|
297
|
-
if (report.state === 'blocked') {
|
|
298
|
-
return {
|
|
299
|
-
ok: false,
|
|
300
|
-
code: 'safe-mode',
|
|
301
|
-
reason: 'SAFE MODE 已激活(存在未结案的配置 transaction):先处理恢复事项再改磁盘。标记:' + report.marker,
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
if (report.state === 'unknown') {
|
|
305
|
-
return {
|
|
306
|
-
ok: false,
|
|
307
|
-
code: 'safe-mode',
|
|
308
|
-
reason: 'SAFE MODE 标记无法判定(按最保守口径处理):先人工核对标记文件。路径:' + report.marker,
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* ② 环境锁:**存在锁就必须先处理**。
|
|
314
|
-
*
|
|
315
|
-
* 为什么不能跳过:CLI 侧 destructive 入口(restore/reinstall)都要求成功获取 GLOBAL 环境锁,
|
|
316
|
-
* 而残留锁(STALE_LOCK_DETECTED)正是「上次异常退出、别的操作也被挡着」的状态;
|
|
317
|
-
* 这时改磁盘等于在一个已知不一致的状态上继续写。判据与 `recover-stale-lock` 同源(inspectLockState),
|
|
318
|
-
* 所以**不给自动回收**,只如实拒绝并把出口指出来。
|
|
319
|
-
*/
|
|
320
|
-
if (options.locksDir !== undefined) {
|
|
321
|
-
const lock = await readLockState(options.locksDir);
|
|
322
|
-
if (lock.present) {
|
|
323
|
-
const stale = lock.state === 'STALE_LOCK_DETECTED';
|
|
324
|
-
return {
|
|
325
|
-
ok: false,
|
|
326
|
-
code: 'stale-lock',
|
|
327
|
-
reason: (stale
|
|
328
|
-
? '检测到残留的环境锁(持有进程已被确证不存在):先回收它再执行写动作。'
|
|
329
|
-
: '环境锁被持有或状态无法判定(' + lock.state + '):先确认没有其它操作在跑,再执行写动作。')
|
|
330
|
-
+ '锁目录:' + lock.locksDir + ';出口:dsh-config-manager recover-stale-lock(本机救急台的「环境锁」页也可回收)。',
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (options.needsDshStopped) {
|
|
335
|
-
const running = await readRunningInstances(controlRoots);
|
|
336
|
-
if (running.length > 0) {
|
|
337
|
-
return {
|
|
338
|
-
ok: false,
|
|
339
|
-
code: 'dsh-running',
|
|
340
|
-
reason: '检测到 DSH 正在运行(' + running.map((i) => i.name + ' pid=' + String(i.pid)).join('、')
|
|
341
|
-
+ '):请先关闭 DSH 再改写会话字节。',
|
|
342
|
-
detail: {
|
|
343
|
-
kind: 'dsh-running',
|
|
344
|
-
instances: running.map((i) => ({ name: i.name, pid: i.pid, port: i.port })),
|
|
345
|
-
commands: [
|
|
346
|
-
// 桌面端独占档案不让普通 CLI 停,如实给出「关窗口」这条唯一通道(见 AGENTS.md 的 desktop 条)
|
|
347
|
-
...running.filter((i) => i.name === 'desktop').map(() => '桌面端(desktop):直接关闭 DSH 桌面窗口;它不接受命令行停止'),
|
|
348
|
-
...running.filter((i) => i.name !== 'desktop').map((i) => 'dsh-config-manager stop ' + i.name + ' # 或在「档案与实例」页点「停止 ' + i.name + '」'),
|
|
349
|
-
'dsh-config-manager web # 停完再回本页点「重新检查」',
|
|
350
|
-
],
|
|
351
|
-
},
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
return { ok: true };
|
|
356
|
-
}
|
|
357
194
|
/* ----------------------------------------------- 会话修复行的文案(纯函数,可单测) */
|
|
358
195
|
/**
|
|
359
196
|
* 真 codec 复验结论 → 一句人话(**三态**;服务层 utils/session-verify.ts 的定义为准)。
|
|
@@ -440,8 +277,13 @@ export async function repairSessionLogInline(options, controlRoots) {
|
|
|
440
277
|
if (options.unitIds.length === 0)
|
|
441
278
|
return { ...base, ok: false, error: '没有选中任何会话单元。' };
|
|
442
279
|
if (options.apply) {
|
|
443
|
-
// 与 CLI 写动作共用同一道写入门:改写会话字节必须先停 DSH(+ SAFE MODE +
|
|
444
|
-
|
|
280
|
+
// 与 CLI 写动作共用同一道写入门:改写会话字节必须先停 DSH(+ SAFE MODE + 环境锁)。
|
|
281
|
+
// 环境锁目录由控制面根派生(审计 P11-4:此前不传 locksDir,锁存在时也照样改写字节)。
|
|
282
|
+
const locksDir = options.locksDir ?? locksDirFromControlRoots(controlRoots);
|
|
283
|
+
const gate = await checkWriteGates(controlRoots, {
|
|
284
|
+
needsDshStopped: true,
|
|
285
|
+
...(locksDir !== undefined ? { locksDir } : {}),
|
|
286
|
+
});
|
|
445
287
|
if (!gate.ok)
|
|
446
288
|
return { ...base, ok: false, error: gate.reason };
|
|
447
289
|
}
|
|
@@ -503,9 +345,11 @@ export async function repairSessions(options, controlRoots) {
|
|
|
503
345
|
done: [], errors: [], notices: [],
|
|
504
346
|
};
|
|
505
347
|
if (options.fix) {
|
|
348
|
+
// 环境锁目录同样由根派生:调用点漏传时不再静默跳过环境锁检查(与 repairSessionLogInline 同一口径)
|
|
349
|
+
const locksDir = options.locksDir ?? locksDirFromControlRoots(controlRoots);
|
|
506
350
|
const gate = await checkWriteGates(controlRoots, {
|
|
507
351
|
needsDshStopped: true,
|
|
508
|
-
...(
|
|
352
|
+
...(locksDir !== undefined ? { locksDir } : {}),
|
|
509
353
|
});
|
|
510
354
|
if (!gate.ok) {
|
|
511
355
|
return { ...base, ok: false, error: gate.reason, errors: [gate.reason] };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { readSafeModeMarkerSync } from '../core/phase3-host.ts';
|
|
2
|
+
/** 进程是否存活(不发送信号,只探测)。EPERM = 存在但无权限发信号 → 仍算存活。 */
|
|
3
|
+
export declare function isProcessAlive(pid: number): boolean;
|
|
4
|
+
/** 一个在跑的 DSH 实例(来自 `<root>/running/<profile>.json` 心跳)。 */
|
|
5
|
+
export interface RunningInstance {
|
|
6
|
+
name: string;
|
|
7
|
+
pid: number;
|
|
8
|
+
port: number | null;
|
|
9
|
+
startedAt: string;
|
|
10
|
+
updatedAt: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 读「本机正在跑的 DSH 实例」。
|
|
14
|
+
*
|
|
15
|
+
* **为什么按候选根扫描**:心跳在 `<插件 dataDir>/running/`,而 CLI 的 `--data-dir` 语义是
|
|
16
|
+
* 「快照目录」(缺省 `$DSH_HOME/dsh-config-manager/snapshots`)。旧实现把快照目录当 dataDir 用,
|
|
17
|
+
* 于是缺省路径下**永远找不到心跳** —— 那道「DSH 在跑就别写会话字节」的门形同虚设(fail-open)。
|
|
18
|
+
* 这里改为复用 `resolveControlRoots` 的候选根:显式 `--data-root` 只认它,否则同时看
|
|
19
|
+
* `--data-dir` 本身与它的父目录,再叠加缺省根;多查几处只会更保守。
|
|
20
|
+
*/
|
|
21
|
+
export declare function readRunningInstances(controlRoots: readonly string[]): Promise<RunningInstance[]>;
|
|
22
|
+
export interface SafeModeReport {
|
|
23
|
+
/** 候选控制面根 */
|
|
24
|
+
root: string;
|
|
25
|
+
/** 标记文件绝对路径(写进界面,便于人工核对) */
|
|
26
|
+
marker: string;
|
|
27
|
+
/** clear = 明确没有标记;blocked = 有未结案的 transaction;unknown = 读不出来(按阻断处理) */
|
|
28
|
+
state: ReturnType<typeof readSafeModeMarkerSync>;
|
|
29
|
+
/**
|
|
30
|
+
* 该候选根下是否已经存在有效的「控制面目录」(dataDir 或 transactions)。
|
|
31
|
+
*
|
|
32
|
+
* 为什么需要:候选根里绝大多数**本来就不存在**(`--data-dir` 的父目录、缺省根都可能没有),
|
|
33
|
+
* 那些根的 'clear' 只是「这里什么都没建」,把它当成「SAFE MODE 未激活」的正面证据是把
|
|
34
|
+
* 「没查」说成「没问题」。界面只在**有效根**上给绿灯,其余如实说「未建立 / 未发现标记」。
|
|
35
|
+
*/
|
|
36
|
+
established: boolean;
|
|
37
|
+
}
|
|
38
|
+
/** 逐个候选根读 SAFE MODE 标记(fail-closed 的判定仍在 CLI/core;这里只报事实)。 */
|
|
39
|
+
export declare function readSafeMode(controlRoots: readonly string[]): SafeModeReport[];
|
|
40
|
+
export interface LockReport {
|
|
41
|
+
locksDir: string;
|
|
42
|
+
/** 锁文件是否存在(不存在 = 没有活动锁) */
|
|
43
|
+
present: boolean;
|
|
44
|
+
/** 没有锁时恒为 'NONE';否则为 inspectLockState 的结论 */
|
|
45
|
+
state: string;
|
|
46
|
+
detail?: string;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 读环境锁状态(只读)。
|
|
50
|
+
*
|
|
51
|
+
* 复用宿主/CLI 同一套 `inspectLockState`:**绝不**因为「看起来是残留」就自动回收 ——
|
|
52
|
+
* 回收只有显式命令一条路(`recover-stale-lock`)。
|
|
53
|
+
*/
|
|
54
|
+
export declare function readLockState(locksDir: string): Promise<LockReport>;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 救急台与 CLI 共用的**只读事实层**:进程存活、心跳实例、SAFE MODE 标记、环境锁状态。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独成文件:这四件事是「写动作前置门」与首页聚合共同依赖的最小事实集,原先挤在
|
|
5
|
+
* 1600 行的 actions.ts 里(体量棘轮已顶格)。这里**只放读取**,判据仍只由 core/utils 给,
|
|
6
|
+
* 绝不因为「看起来像残留」就自行回收(回收只有显式命令一条路)。
|
|
7
|
+
*/
|
|
8
|
+
import fssync from 'node:fs';
|
|
9
|
+
import fsp from 'node:fs/promises';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { readSafeModeMarkerSync, safeModeMarkerPath } from '../core/phase3-host.js';
|
|
12
|
+
import { parseRuntimeRecord, RUNTIME_STALE_MS } from '../profiles/dsh-profile-runtime.js';
|
|
13
|
+
import { EnvironmentLockManager, OWNERSHIP_FILE } from '../utils/env-lock.js';
|
|
14
|
+
/* ------------------------------------------------------------ 进程/实例事实 */
|
|
15
|
+
/** 进程是否存活(不发送信号,只探测)。EPERM = 存在但无权限发信号 → 仍算存活。 */
|
|
16
|
+
export function isProcessAlive(pid) {
|
|
17
|
+
try {
|
|
18
|
+
process.kill(pid, 0);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return error.code === 'EPERM';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 读「本机正在跑的 DSH 实例」。
|
|
27
|
+
*
|
|
28
|
+
* **为什么按候选根扫描**:心跳在 `<插件 dataDir>/running/`,而 CLI 的 `--data-dir` 语义是
|
|
29
|
+
* 「快照目录」(缺省 `$DSH_HOME/dsh-config-manager/snapshots`)。旧实现把快照目录当 dataDir 用,
|
|
30
|
+
* 于是缺省路径下**永远找不到心跳** —— 那道「DSH 在跑就别写会话字节」的门形同虚设(fail-open)。
|
|
31
|
+
* 这里改为复用 `resolveControlRoots` 的候选根:显式 `--data-root` 只认它,否则同时看
|
|
32
|
+
* `--data-dir` 本身与它的父目录,再叠加缺省根;多查几处只会更保守。
|
|
33
|
+
*/
|
|
34
|
+
export async function readRunningInstances(controlRoots) {
|
|
35
|
+
const alive = [];
|
|
36
|
+
const seen = new Set();
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
for (const root of controlRoots) {
|
|
39
|
+
const dir = path.join(root, 'running');
|
|
40
|
+
let names;
|
|
41
|
+
try {
|
|
42
|
+
names = (await fsp.readdir(dir)).filter((n) => n.endsWith('.json'));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
continue; // 该候选根没有心跳目录:正常情况,不阻断
|
|
46
|
+
}
|
|
47
|
+
for (const name of names) {
|
|
48
|
+
try {
|
|
49
|
+
const record = parseRuntimeRecord(await fsp.readFile(path.join(dir, name), 'utf8'));
|
|
50
|
+
if (record === null)
|
|
51
|
+
continue;
|
|
52
|
+
if (!isProcessAlive(record.pid) || now - record.updatedAt > RUNTIME_STALE_MS)
|
|
53
|
+
continue;
|
|
54
|
+
const key = record.name + '#' + String(record.pid);
|
|
55
|
+
if (seen.has(key))
|
|
56
|
+
continue;
|
|
57
|
+
seen.add(key);
|
|
58
|
+
alive.push({
|
|
59
|
+
name: record.name,
|
|
60
|
+
pid: record.pid,
|
|
61
|
+
port: record.port,
|
|
62
|
+
startedAt: record.startedAt,
|
|
63
|
+
updatedAt: record.updatedAt,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// 单条心跳读不出来不阻断(best-effort)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return alive;
|
|
72
|
+
}
|
|
73
|
+
/** 逐个候选根读 SAFE MODE 标记(fail-closed 的判定仍在 CLI/core;这里只报事实)。 */
|
|
74
|
+
export function readSafeMode(controlRoots) {
|
|
75
|
+
return controlRoots.map((root) => ({
|
|
76
|
+
root,
|
|
77
|
+
marker: safeModeMarkerPath(root),
|
|
78
|
+
state: readSafeModeMarkerSync(root),
|
|
79
|
+
established: fssync.existsSync(root) || fssync.existsSync(path.join(root, 'transactions')),
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 读环境锁状态(只读)。
|
|
84
|
+
*
|
|
85
|
+
* 复用宿主/CLI 同一套 `inspectLockState`:**绝不**因为「看起来是残留」就自动回收 ——
|
|
86
|
+
* 回收只有显式命令一条路(`recover-stale-lock`)。
|
|
87
|
+
*/
|
|
88
|
+
export async function readLockState(locksDir) {
|
|
89
|
+
const present = await pathExists(path.join(locksDir, OWNERSHIP_FILE));
|
|
90
|
+
if (!present)
|
|
91
|
+
return { locksDir, present: false, state: 'NONE' };
|
|
92
|
+
const lock = new EnvironmentLockManager({ locksDir, op: 'rescue-console-inspect', target: locksDir, lockVersion: '0.1.0' });
|
|
93
|
+
const inspection = await lock.inspectLockState();
|
|
94
|
+
return inspection.detail === undefined
|
|
95
|
+
? { locksDir, present: true, state: inspection.state }
|
|
96
|
+
: { locksDir, present: true, state: inspection.state, detail: inspection.detail };
|
|
97
|
+
}
|
|
98
|
+
async function pathExists(target) {
|
|
99
|
+
try {
|
|
100
|
+
await fsp.stat(target);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
//# sourceMappingURL=control-state.js.map
|
package/lib/cli/index.js
CHANGED
|
@@ -715,6 +715,15 @@ async function runReinstall(options, io, env, deps) {
|
|
|
715
715
|
io.error(`拒绝执行:${safeMsg}`);
|
|
716
716
|
return 1;
|
|
717
717
|
}
|
|
718
|
+
// AGENTS「CLI 与救急台同一道写入门」第三道:DSH 未运行。
|
|
719
|
+
// 清空 settings/plugins/data 会删掉运行中实例的 profiles 与 sessions(审计 P11-5)。
|
|
720
|
+
if (plan.wipeConfig) {
|
|
721
|
+
const runningGate = await checkWriteGates(resolveControlRoots({ dataRoot: options.dataRoot, dataDir: options.dataDir }, env), { needsDshStopped: true });
|
|
722
|
+
if (!runningGate.ok) {
|
|
723
|
+
io.error(`拒绝执行:${runningGate.reason}`);
|
|
724
|
+
return 1;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
718
727
|
// 执行(Phase 2 锁:reinstall 卸载/清扫/重装属 GLOBAL destructive,必须成功获取环境锁)
|
|
719
728
|
const lockHome = resolveDshHome(env);
|
|
720
729
|
const lock = new EnvironmentLockManager({
|
|
@@ -773,6 +782,11 @@ async function runReinstall(options, io, env, deps) {
|
|
|
773
782
|
const reason = err instanceof Error ? err.message : String(err);
|
|
774
783
|
failed.push({ label: step.label, command: step.command, reason });
|
|
775
784
|
io.error(`✘ ${step.label} — ${reason}`);
|
|
785
|
+
// 备份步(critical)失败时后面的清空没有抢救副本:中止剩余步骤(审计 P11-7)。
|
|
786
|
+
if (step.critical === true) {
|
|
787
|
+
io.error('✘ 兜底备份失败,已中止后续清空步骤(未删除任何 ~/.dsh 数据)/ backup failed, aborting remaining steps');
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
776
790
|
}
|
|
777
791
|
}
|
|
778
792
|
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type WriteRefusalCode = 'safe-mode' | 'stale-lock' | 'dsh-running' | 'nothing-to-do' | 'io-error';
|
|
2
|
+
/**
|
|
3
|
+
* SAFE MODE 拒因码的**单点常量**:`src/cli/index.ts` 的源码守卫(P0-11-07)禁止
|
|
4
|
+
* CLI 自带 `'safe-mode'` 字面量,判据必须复用本常量(否则改一处漏一处)。
|
|
5
|
+
*/
|
|
6
|
+
export declare const SAFE_MODE_REFUSAL_CODE: WriteRefusalCode;
|
|
7
|
+
export interface WriteGateRefusal {
|
|
8
|
+
ok: false;
|
|
9
|
+
code: WriteRefusalCode;
|
|
10
|
+
/** 用户可读原因(页面直接展示) */
|
|
11
|
+
reason: string;
|
|
12
|
+
/**
|
|
13
|
+
* 拒因的结构化明细(页面据此给出「怎么解除」的可操作指引,而不是只丢一句 reason)。
|
|
14
|
+
*
|
|
15
|
+
* 为什么要有:`dsh-running` 是最常被撞到的一道门,光给「请先关闭 DSH」用户会问
|
|
16
|
+
* 「关哪个、怎么确认关掉了」。这里把**是谁在跑**(心跳台账里的 profile/pid/端口)
|
|
17
|
+
* 如实带出来 —— 只读事实,不猜、不代关进程。
|
|
18
|
+
*/
|
|
19
|
+
detail?: {
|
|
20
|
+
kind: 'dsh-running' | 'stale-lock' | 'safe-mode';
|
|
21
|
+
/** 正在运行的实例(心跳合并视图:本插件启动的 + 手动启动的) */
|
|
22
|
+
instances?: Array<{
|
|
23
|
+
name: string;
|
|
24
|
+
pid: number;
|
|
25
|
+
port: number | null;
|
|
26
|
+
}>;
|
|
27
|
+
/** 环境锁状态(kind = 'stale-lock') */
|
|
28
|
+
lock?: {
|
|
29
|
+
state: string;
|
|
30
|
+
locksDir: string;
|
|
31
|
+
};
|
|
32
|
+
/** SAFE MODE 标记文件(kind = 'safe-mode') */
|
|
33
|
+
markers?: string[];
|
|
34
|
+
/** 与 CLI 逐字同源的等价命令(可复制去终端跑) */
|
|
35
|
+
commands: string[];
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export type WriteGateOutcome = {
|
|
39
|
+
ok: true;
|
|
40
|
+
} | WriteGateRefusal;
|
|
41
|
+
/**
|
|
42
|
+
* 从控制面候选根派生**环境锁目录**(`<root>/locks`)。
|
|
43
|
+
*
|
|
44
|
+
* 为什么由根派生而不是让每个调用点多传一个参数:`checkWriteGates` 的 `locksDir` 是**可选**的,
|
|
45
|
+
* 「少传一个参数」就静默退化成「不查环境锁」。审计 P11-4 正是这么漏掉的:就地修复(repairSessionLogInline)
|
|
46
|
+
* 不传 locksDir,残留/被持有的环境锁存在时照样改写 `.zstd` 会话字节。
|
|
47
|
+
* 派生顺序与 `resolveControlRoots` 同源:第一个真实持有 `locks/environment.lock` 的候选根;
|
|
48
|
+
* 都没有就退回首个候选的 `locks`(读不到即视为无锁,判据仍只由 readLockState 给)。
|
|
49
|
+
*/
|
|
50
|
+
export declare function locksDirFromControlRoots(controlRoots: readonly string[]): string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* 写动作的前置门(与 CLI `sessions repair --fix` 同一判据)。
|
|
53
|
+
*
|
|
54
|
+
* `needsDshStopped`:**改写/搬动会话字节**的动作(会话布局归位 repairSessions、字节级就地修复
|
|
55
|
+
* repairSessionLogInline)与**快照恢复**(runSnapshotRestore 会覆盖/删除 `$DSH_HOME` 下正在使用的文件)
|
|
56
|
+
* 都要「DSH 已停」这一条;磁盘清理不需要(只碰可重建缓存)。
|
|
57
|
+
*/
|
|
58
|
+
export declare function checkWriteGates(controlRoots: readonly string[], options?: {
|
|
59
|
+
needsDshStopped: boolean;
|
|
60
|
+
locksDir?: string;
|
|
61
|
+
}): Promise<WriteGateOutcome>;
|