dsh-config-manager 0.4.3 → 0.6.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/README.md +10 -0
- package/README.zh-CN.md +10 -0
- package/lib/adapters/mcp.d.ts +9 -0
- package/lib/adapters/mcp.js +37 -0
- package/lib/adapters/plugins.js +10 -2
- package/lib/adapters/pnpm-workspace.js +1 -1
- package/lib/adapters/self.d.ts +2 -0
- package/lib/adapters/self.js +18 -2
- package/lib/adapters/workspaces.js +1 -1
- package/lib/cli/restore-resume.js +1 -1
- package/lib/cli/web/page.js +2 -2
- package/lib/client.d.ts +17 -41
- package/lib/client.js +946 -761
- package/lib/core/backup.d.ts +11 -0
- package/lib/core/backup.js +27 -0
- package/lib/core/env-lock-lifecycle.d.ts +17 -3
- package/lib/core/env-lock-lifecycle.js +38 -0
- package/lib/core/external-failure.d.ts +9 -0
- package/lib/core/external-failure.js +50 -0
- package/lib/core/local-plugin-host.d.ts +9 -7
- package/lib/core/local-plugin-host.js +109 -15
- package/lib/core/local-plugin-pack.d.ts +20 -0
- package/lib/core/local-plugin-pack.js +18 -3
- package/lib/core/mcp-connector-store.d.ts +13 -0
- package/lib/core/mcp-connector-store.js +16 -1
- package/lib/core/messages-core.d.ts +45 -0
- package/lib/core/messages-core.js +92 -0
- package/lib/core/messages.d.ts +5 -0
- package/lib/core/messages.js +14 -0
- package/lib/core/migration-history.d.ts +40 -0
- package/lib/core/migration-history.js +55 -0
- package/lib/core/model-tool-plans.d.ts +66 -0
- package/lib/core/model-tool-plans.js +128 -0
- package/lib/core/model-tools.d.ts +96 -8
- package/lib/core/model-tools.js +568 -31
- package/lib/core/phase3-host.js +10 -2
- package/lib/core/plugin-cli.d.ts +5 -0
- package/lib/core/plugin-cli.js +11 -3
- package/lib/core/plugin-data-registry.d.ts +1 -1
- package/lib/core/plugin-data-registry.js +1 -1
- package/lib/core/restore-plan-fingerprint.d.ts +2 -0
- package/lib/core/restore-plan-fingerprint.js +18 -0
- package/lib/core/restore-resume.d.ts +1 -1
- package/lib/core/restore-resume.js +1 -1
- package/lib/core/restore.d.ts +5 -0
- package/lib/core/restore.js +4 -1
- package/lib/core/types.d.ts +12 -0
- package/lib/foreign/claude-sessions.d.ts +1 -1
- package/lib/foreign/claude-sessions.js +1 -1
- package/lib/foreign/registry.js +1 -1
- package/lib/foreign/types.d.ts +1 -1
- package/lib/index.d.ts +19 -15
- package/lib/index.js +72 -80
- package/lib/market/git-file-writer.js +1 -1
- package/lib/market/github-repos.js +1 -1
- package/lib/market/market-config.js +1 -1
- package/lib/market/messages-market.js +1 -1
- package/lib/market/my-repo.js +1 -1
- package/lib/market/prepare.js +1 -1
- package/lib/market/reader.js +2 -2
- package/lib/market/repo-url.d.ts +1 -1
- package/lib/market/security.js +2 -2
- package/lib/market/star-cache.d.ts +1 -1
- package/lib/market/types.d.ts +1 -1
- package/lib/market/types.js +1 -1
- package/lib/market/url.js +1 -1
- package/lib/profiles/dsh-profile-manager.d.ts +1 -1
- package/lib/profiles/dsh-profile-manager.js +26 -7
- package/lib/profiles/dsh-profile-shared.d.ts +3 -1
- package/lib/routes/backup.js +17 -9
- package/lib/routes/index.d.ts +1 -1
- package/lib/routes/kit.d.ts +2 -2
- package/lib/routes/market.d.ts +1 -1
- package/lib/routes/market.js +3 -60
- package/lib/routes/model-tools-deps.d.ts +41 -0
- package/lib/routes/model-tools-deps.js +18 -0
- package/lib/routes/recovery.js +5 -3
- package/lib/routes/route-support.d.ts +18 -26
- package/lib/routes/route-support.js +19 -32
- package/lib/routes/snapshots.d.ts +1 -1
- package/lib/routes/snapshots.js +4 -40
- package/lib/routes/sync.js +40 -28
- package/lib/schema/index.d.ts +2 -2
- package/lib/schema/index.js +2 -2
- package/lib/schema/section-registry.d.ts +5 -3
- package/lib/schema/section-registry.js +7 -3
- package/lib/security/secret-scanner.js +1 -1
- package/lib/sync/apply-bundle.d.ts +50 -0
- package/lib/sync/apply-bundle.js +60 -0
- package/lib/sync/autosync-scheduler.d.ts +9 -0
- package/lib/sync/autosync-scheduler.js +118 -89
- package/lib/sync/backup-scheduler.js +5 -4
- package/lib/sync/blob-store.d.ts +2 -2
- package/lib/sync/git/git-transport.d.ts +12 -0
- package/lib/sync/git/git-transport.js +50 -2
- package/lib/sync/merge.d.ts +2 -0
- package/lib/sync/merge.js +28 -0
- package/lib/sync/messages.d.ts +4 -0
- package/lib/sync/messages.js +9 -2
- package/lib/sync/s3/s3-transport.d.ts +1 -1
- package/lib/sync/s3/s3-transport.js +1 -1
- package/lib/sync/sync-engine.d.ts +4 -4
- package/lib/sync/sync-engine.js +25 -26
- package/lib/sync/sync-scope-boot.d.ts +34 -0
- package/lib/sync/sync-scope-boot.js +71 -0
- package/lib/sync/sync-scope.d.ts +103 -0
- package/lib/sync/sync-scope.js +229 -0
- package/lib/sync/sync-selection.d.ts +17 -4
- package/lib/sync/sync-selection.js +40 -14
- package/lib/sync/transport.d.ts +4 -1
- package/lib/sync/transport.js +6 -0
- package/lib/sync/webdav/webdav-transport.js +1 -1
- package/lib/sync/workspace-merge.d.ts +36 -0
- package/lib/sync/workspace-merge.js +152 -0
- package/lib/ui/backup-schedule.d.ts +13 -2
- package/lib/ui/backup-schedule.js +3 -0
- package/lib/ui/snapshots-view.d.ts +16 -0
- package/lib/ui/snapshots-view.js +21 -0
- package/lib/ui/sync-repository-picker.d.ts +21 -0
- package/lib/ui/sync-repository-picker.js +18 -0
- package/lib/utils/env-lock.d.ts +96 -0
- package/lib/utils/env-lock.js +332 -111
- package/lib/utils/messages-utils.d.ts +6 -0
- package/lib/utils/messages-utils.js +17 -1
- package/lib/utils/patch-yaml.js +1 -1
- package/lib/utils/remove-tree.d.ts +7 -0
- package/lib/utils/remove-tree.js +265 -0
- package/lib/utils/zip.js +1 -1
- package/package.json +3 -2
- package/src/adapters/mcp.test.ts +28 -0
- package/src/adapters/mcp.ts +33 -0
- package/src/adapters/plugins.test.ts +49 -0
- package/src/adapters/plugins.ts +9 -1
- package/src/adapters/pnpm-workspace.ts +1 -1
- package/src/adapters/self.test.ts +39 -1
- package/src/adapters/self.ts +20 -2
- package/src/adapters/workspaces.ts +1 -1
- package/src/cli/restore-resume.ts +1 -1
- package/src/cli/web/page.ts +2 -2
- package/src/client/about/AboutPanel.tsx +1 -1
- package/src/client/about/about-view.test.ts +1 -1
- package/src/client/about/about-view.ts +1 -1
- package/src/client/api.ts +1 -6
- package/src/client/chat/chat-entry-view.test.ts +1 -1
- package/src/client/chat/chat-slots.ts +1 -1
- package/src/client/chat/rollback-diff-tab.ts +1 -1
- package/src/client/client-types.ts +1 -1
- package/src/client/common/routes.ts +1 -5
- package/src/client/config-manager.module.css +21 -1
- package/src/client/css-scale-guard.test.ts +1 -1
- package/src/client/environment/EnvironmentPanel.tsx +2 -2
- package/src/client/home/HomePanel.tsx +3 -0
- package/src/client/library/ArtifactRow.tsx +14 -2
- package/src/client/library/LibraryPanel.tsx +44 -0
- package/src/client/locales/overview.ts +10 -2
- package/src/client/locales/profiles.ts +2 -0
- package/src/client/locales/sessions.ts +3 -0
- package/src/client/market/MyConfigsView.tsx +1 -1
- package/src/client/market/market-api.ts +2 -14
- package/src/client/market/market-locales.ts +1 -1
- package/src/client/market/market-view.ts +3 -3
- package/src/client/market/my-configs-api.ts +1 -1
- package/src/client/market/my-configs-view.ts +1 -1
- package/src/client/recovery/recovery-locales.test.ts +1 -1
- package/src/client/run-store.ts +1 -1
- package/src/client/sync/ChannelConfigDialog.tsx +32 -1
- package/src/client/sync/SyncPanel.tsx +11 -30
- package/src/client/sync/SyncRepositoryPicker.tsx +34 -13
- package/src/client/sync/SyncSectionPickerDialog.tsx +0 -5
- package/src/client/sync/SyncSettingsView.test.ts +32 -0
- package/src/client/sync/config-save-state.test.ts +99 -0
- package/src/client/sync/config-save-state.ts +69 -0
- package/src/client/sync/history-model.test.ts +28 -7
- package/src/client/sync/history-model.ts +6 -3
- package/src/client/sync/sync-locales.ts +14 -4
- package/src/client/sync/sync-view.ts +1 -1
- package/src/core/analyzer-cache.test.ts +1 -1
- package/src/core/backup.test.ts +45 -1
- package/src/core/backup.ts +25 -0
- package/src/core/disk-usage.test.ts +2 -0
- package/src/core/engine-locale.test.ts +4 -0
- package/src/core/env-lock-lifecycle.ts +33 -3
- package/src/core/external-failure.test.ts +60 -0
- package/src/core/external-failure.ts +54 -0
- package/src/core/integrity-extra-entries.test.ts +1 -1
- package/src/core/local-plugin-host.test.ts +134 -0
- package/src/core/local-plugin-host.ts +122 -17
- package/src/core/local-plugin-pack.ts +39 -3
- package/src/core/mcp-connector-store.ts +26 -1
- package/src/core/messages-core.ts +94 -0
- package/src/core/messages.ts +14 -0
- package/src/core/migration-history.test.ts +65 -0
- package/src/core/migration-history.ts +70 -0
- package/src/core/model-tool-plans.test.ts +111 -0
- package/src/core/model-tool-plans.ts +170 -0
- package/src/core/model-tools.test.ts +212 -12
- package/src/core/model-tools.ts +710 -38
- package/src/core/phase3-host.ts +10 -2
- package/src/core/phase3-production-integration.test.ts +43 -0
- package/src/core/plugin-cli.fs.test.ts +30 -3
- package/src/core/plugin-cli.ts +11 -3
- package/src/core/plugin-data-registry.ts +1 -1
- package/src/core/restore-plan-fingerprint.ts +19 -0
- package/src/core/restore-resume.ts +1 -1
- package/src/core/restore.ts +9 -1
- package/src/core/types.ts +12 -0
- package/src/foreign/claude-sessions.ts +3 -3
- package/src/foreign/registry.ts +1 -1
- package/src/foreign/types.ts +1 -1
- package/src/index.ts +102 -111
- package/src/market/git-file-writer.ts +1 -1
- package/src/market/github-repos.test.ts +1 -1
- package/src/market/github-repos.ts +1 -1
- package/src/market/market-config.ts +1 -1
- package/src/market/messages-market.ts +1 -1
- package/src/market/my-repo.test.ts +1 -1
- package/src/market/my-repo.ts +1 -1
- package/src/market/prepare.test.ts +1 -1
- package/src/market/prepare.ts +1 -1
- package/src/market/reader.ts +2 -2
- package/src/market/repo-url.ts +1 -1
- package/src/market/security.ts +2 -2
- package/src/market/star-cache.ts +1 -1
- package/src/market/types.ts +1 -1
- package/src/market/url.test.ts +1 -1
- package/src/market/url.ts +1 -1
- package/src/profiles/dsh-profile-copy-rollback.test.ts +1 -1
- package/src/profiles/dsh-profile-manager.test.ts +20 -0
- package/src/profiles/dsh-profile-manager.ts +25 -7
- package/src/profiles/dsh-profile-shared.ts +2 -0
- package/src/routes/backup.ts +16 -9
- package/src/routes/index.ts +1 -1
- package/src/routes/kit.ts +2 -2
- package/src/routes/market.ts +2 -59
- package/src/routes/model-tools-deps.ts +56 -0
- package/src/routes/recovery.ts +5 -3
- package/src/routes/route-support.ts +49 -36
- package/src/routes/snapshots.ts +4 -40
- package/src/routes/sync.ts +39 -27
- package/src/schema/index.ts +2 -2
- package/src/schema/manifest-schema.test.ts +2 -2
- package/src/schema/registry.test.ts +1 -1
- package/src/schema/section-registry.ts +9 -4
- package/src/security/redaction.test.ts +1 -1
- package/src/security/secret-scanner.test.ts +2 -2
- package/src/security/secret-scanner.ts +1 -1
- package/src/sync/apply-bundle.ts +104 -0
- package/src/sync/autosync-scheduler.test.ts +180 -6
- package/src/sync/autosync-scheduler.ts +132 -92
- package/src/sync/backup-scheduler.test.ts +11 -4
- package/src/sync/backup-scheduler.ts +5 -4
- package/src/sync/blob-store.ts +2 -2
- package/src/sync/git/git-transport.test.ts +62 -0
- package/src/sync/git/git-transport.ts +54 -3
- package/src/sync/merge.ts +29 -0
- package/src/sync/messages.ts +9 -2
- package/src/sync/s3/s3-transport.ts +1 -1
- package/src/sync/sync-engine.test.ts +238 -2
- package/src/sync/sync-engine.ts +25 -26
- package/src/sync/sync-preview-push.test.ts +2 -1
- package/src/sync/sync-scope-boot.ts +87 -0
- package/src/sync/sync-scope.test.ts +154 -0
- package/src/sync/sync-scope.ts +256 -0
- package/src/sync/sync-selection.test.ts +76 -2
- package/src/sync/sync-selection.ts +40 -13
- package/src/sync/transport.test.ts +21 -1
- package/src/sync/transport.ts +12 -0
- package/src/sync/webdav/webdav-delete-index-unreadable.test.ts +2 -2
- package/src/sync/webdav/webdav-transport.ts +1 -1
- package/src/sync/workspace-merge.test.ts +110 -0
- package/src/sync/workspace-merge.ts +180 -0
- package/src/ui/backup-schedule.test.ts +5 -0
- package/src/ui/backup-schedule.ts +21 -2
- package/src/ui/session-issue-labels.ts +1 -1
- package/src/ui/snapshots-view.test.ts +32 -0
- package/src/ui/snapshots-view.ts +34 -0
- package/src/ui/sync-repository-picker.test.ts +34 -1
- package/src/ui/sync-repository-picker.ts +24 -0
- package/src/utils/env-lock.test.ts +242 -9
- package/src/utils/env-lock.ts +247 -13
- package/src/utils/messages-utils.ts +17 -1
- package/src/utils/patch-yaml.ts +1 -1
- package/src/utils/remove-tree.test.ts +120 -0
- package/src/utils/remove-tree.ts +239 -0
- package/src/utils/zip.ts +1 -1
- package/src/client/CLIENT_DEPENDENCIES.md +0 -94
|
@@ -9,7 +9,7 @@ import assert from 'node:assert/strict'
|
|
|
9
9
|
|
|
10
10
|
import {
|
|
11
11
|
formatRepoUpdatedAt, normalizeRepoUrl, repoCreateBody, repoNameError, repoPickerOptions,
|
|
12
|
-
repoPickerRepos, repoPickerValueFor, repoShortName, REPO_NAME_MAX_LENGTH,
|
|
12
|
+
repoPickerRepos, repoPickerValueFor, repoShortName, shouldAutoLoadRepos, REPO_NAME_MAX_LENGTH,
|
|
13
13
|
REPO_PICKER_CREATE_VALUE, type RepoPickerRepo,
|
|
14
14
|
} from './sync-repository-picker.ts'
|
|
15
15
|
|
|
@@ -128,3 +128,36 @@ test('formatRepoUpdatedAt:空串→空串,非法→原样,合法→本地
|
|
|
128
128
|
assert.ok(shown.includes('2026'), '应包含年份,实际:' + shown)
|
|
129
129
|
assert.ok(!shown.includes('Invalid'), '绝不显示 Invalid Date,实际:' + shown)
|
|
130
130
|
})
|
|
131
|
+
|
|
132
|
+
/* ---------- 仓库列表的自动加载判定(2026-11-08 用户反馈:登录成功后列表不出现、必须手点下拉) */
|
|
133
|
+
|
|
134
|
+
/** 基线:弹窗打开、未登录、地址为空、什么都没加载过 */
|
|
135
|
+
const autoBase = {
|
|
136
|
+
open: true,
|
|
137
|
+
signedIn: null as boolean | null,
|
|
138
|
+
loaded: false,
|
|
139
|
+
loading: false,
|
|
140
|
+
failed: false,
|
|
141
|
+
repoUrl: '',
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
test('shouldAutoLoadRepos:登录成功(signedIn true)即自动加载 —— 用户不必再点开下拉', () => {
|
|
145
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true }), true)
|
|
146
|
+
// 已登录 + 表单里已有地址同样自动加载(保持既有行为)
|
|
147
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true, repoUrl: 'https://github.com/u/r.git' }), true)
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('shouldAutoLoadRepos:未登录且地址为空 → 不加载(必然 401,白跑网络)', () => {
|
|
151
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: null }), false)
|
|
152
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: false }), false)
|
|
153
|
+
// 未登录但地址已填:照旧自动拉(既有行为,可能与手填的 token 一起生效)
|
|
154
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: false, repoUrl: ' https://github.com/u/r.git ' }), true)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('shouldAutoLoadRepos:弹窗关闭 / 已加载 / 加载中 / 失败后 → 一律不重复发请求', () => {
|
|
158
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true, open: false }), false)
|
|
159
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true, loaded: true }), false)
|
|
160
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true, loading: true }), false)
|
|
161
|
+
// 失败不自动重试:否则每次重渲染都会打一次必然失败的请求(重试走「刷新列表」或登录态复位)
|
|
162
|
+
assert.equal(shouldAutoLoadRepos({ ...autoBase, signedIn: true, failed: true }), false)
|
|
163
|
+
})
|
|
@@ -178,3 +178,27 @@ export function formatRepoUpdatedAt(iso: string, locale?: string): string {
|
|
|
178
178
|
const date = new Date(ms)
|
|
179
179
|
return locale === undefined ? date.toLocaleString() : date.toLocaleString(locale)
|
|
180
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* 仓库列表是否该**自动加载**(不要求用户先点开下拉)。
|
|
183
|
+
*
|
|
184
|
+
* 用户反馈(2026-11-08):GitHub 登录成功后界面没有任何提示,列表也不会自己出现 ——
|
|
185
|
+
* 必须手动点一下「从我的仓库中选择」才知道成功了。这里把「该拉列表」的判定收成一条纯函数,
|
|
186
|
+
* 三个触发点共用:弹窗打开、登录状态由「未登录」变「已登录」、表单里已经有地址。
|
|
187
|
+
*
|
|
188
|
+
* 规则:
|
|
189
|
+
* - 弹窗没打开 / 已加载过 / 正在加载 → 不重复发请求;
|
|
190
|
+
* - `failed` 为真 → 不自动重试(否则每次重渲染都会打一次必然失败的请求;重试由「刷新列表」或
|
|
191
|
+
* 登录状态变化后的复位负责)。
|
|
192
|
+
* - 未登录且地址为空 → 保持惰性(此时拉列表必然 401,白跑一次网络)。
|
|
193
|
+
*/
|
|
194
|
+
export function shouldAutoLoadRepos(input: {
|
|
195
|
+
open: boolean
|
|
196
|
+
signedIn: boolean | null
|
|
197
|
+
loaded: boolean
|
|
198
|
+
loading: boolean
|
|
199
|
+
failed: boolean
|
|
200
|
+
repoUrl: string
|
|
201
|
+
}): boolean {
|
|
202
|
+
if (!input.open || input.loaded || input.loading || input.failed) return false
|
|
203
|
+
return input.signedIn === true || input.repoUrl.trim() !== ''
|
|
204
|
+
}
|
|
@@ -36,6 +36,8 @@ import {
|
|
|
36
36
|
OWNERSHIP_FILE,
|
|
37
37
|
HEARTBEAT_PREFIX,
|
|
38
38
|
RECOVERING_PREFIX,
|
|
39
|
+
LOCK_BLOCK_MESSAGE,
|
|
40
|
+
lockSkipToken,
|
|
39
41
|
type LockOwnershipRecord,
|
|
40
42
|
type ProcessIdentityProbe,
|
|
41
43
|
type EnvLockManagerOptions,
|
|
@@ -113,12 +115,15 @@ function makeIo(): {
|
|
|
113
115
|
failOpenWhen(p: (p: string) => boolean, code?: string): void;
|
|
114
116
|
failUnlinkWhen(p: (p: string) => boolean): void;
|
|
115
117
|
failReadWhen(p: (p: string) => boolean): void;
|
|
118
|
+
/** 注入 rename 故障(quarantine / recover 的捕获 rename 都在这里) */
|
|
119
|
+
failRenameWhen(p: (p: string) => boolean): void;
|
|
116
120
|
hookRename(fn: (a: string, b: string) => void | Promise<void>): void;
|
|
117
121
|
clear(): void;
|
|
118
122
|
} {
|
|
119
123
|
const openFaults: Array<{ p: (p: string) => boolean; code?: string }> = [];
|
|
120
124
|
const unlinkFaults: Array<(p: string) => boolean> = [];
|
|
121
125
|
const readFaults: Array<(p: string) => boolean> = [];
|
|
126
|
+
const renameFaults: Array<(p: string) => boolean> = [];
|
|
122
127
|
const renameHooks: Array<(a: string, b: string) => void | Promise<void>> = [];
|
|
123
128
|
const io: import('./env-lock.ts').EnvLockIo = {
|
|
124
129
|
async mkdir(d, o) { await fs.mkdir(d, o); },
|
|
@@ -130,7 +135,15 @@ function makeIo(): {
|
|
|
130
135
|
}
|
|
131
136
|
return fs.open(p, flag as never, mode) as never;
|
|
132
137
|
},
|
|
133
|
-
async rename(a, b) {
|
|
138
|
+
async rename(a, b) {
|
|
139
|
+
for (const f of renameFaults) if (f(a)) {
|
|
140
|
+
const e = new Error('injected rename fault') as NodeJS.ErrnoException;
|
|
141
|
+
e.code = 'EPERM';
|
|
142
|
+
throw e;
|
|
143
|
+
}
|
|
144
|
+
await fs.rename(a, b);
|
|
145
|
+
for (const h of renameHooks) await h(a, b);
|
|
146
|
+
},
|
|
134
147
|
async unlink(p) {
|
|
135
148
|
for (const f of unlinkFaults) if (f(p)) {
|
|
136
149
|
const e = new Error('injected unlink fault') as NodeJS.ErrnoException;
|
|
@@ -152,8 +165,9 @@ function makeIo(): {
|
|
|
152
165
|
failOpenWhen(p, code) { openFaults.push({ p, code }); },
|
|
153
166
|
failUnlinkWhen(p) { unlinkFaults.push(p); },
|
|
154
167
|
failReadWhen(p) { readFaults.push(p); },
|
|
168
|
+
failRenameWhen(p) { renameFaults.push(p); },
|
|
155
169
|
hookRename(fn) { renameHooks.push(fn); },
|
|
156
|
-
clear() { openFaults.length = 0; unlinkFaults.length = 0; readFaults.length = 0; },
|
|
170
|
+
clear() { openFaults.length = 0; unlinkFaults.length = 0; readFaults.length = 0; renameFaults.length = 0; },
|
|
157
171
|
};
|
|
158
172
|
}
|
|
159
173
|
|
|
@@ -335,24 +349,32 @@ process.exit(1); // 期望被拒:非 0 指示「未获得锁」;父进程根
|
|
|
335
349
|
await parent.release(pres.token!);
|
|
336
350
|
});
|
|
337
351
|
|
|
338
|
-
test('§11.1-c4 release:
|
|
352
|
+
test('§11.1-c4 release:unlink 失败先 quarantine 腾空正式路径(不抛);rename 也失败才抛错并保留 activeToken', async (t) => {
|
|
339
353
|
const locksDir = tmp(t);
|
|
340
354
|
const ctl = makeIo();
|
|
341
|
-
const mgr = lockManager(t, { locksDir, io: ctl.io });
|
|
355
|
+
const mgr = lockManager(t, { locksDir, io: ctl.io, heartbeatIntervalMs: 40 });
|
|
342
356
|
const res = await mgr.acquire({ op: 'u' });
|
|
343
357
|
assert.equal(res.state, 'ACQUIRED');
|
|
344
|
-
|
|
345
|
-
//
|
|
358
|
+
// ① 只有 unlink 失败(rename 可用)→ 把记录挪走:正式路径腾空,release 不抛。
|
|
359
|
+
// 真机事故正是「unlink 失败 + 调用方吞错」把锁永久留在盘上,所以这条路径不允许再把锁留在原地。
|
|
346
360
|
ctl.failUnlinkWhen((p) => p === mgr.ownershipPath);
|
|
361
|
+
await mgr.release(res.token!);
|
|
362
|
+
assert.equal(mgr.isHolding, false, 'quarantine 成功即视为已释放');
|
|
363
|
+
assert.equal(fssync.existsSync(mgr.ownershipPath), false, '正式路径必须腾空(否则下一个 acquire 只是继承僵尸锁)');
|
|
364
|
+
|
|
365
|
+
// ② unlink + rename 双失败 → 抛错并保留 activeToken(仍持有该 inode,可重试)
|
|
366
|
+
const res2 = await mgr.acquire({ op: 'u' });
|
|
367
|
+
assert.equal(res2.state, 'ACQUIRED');
|
|
368
|
+
ctl.failRenameWhen((p) => p === mgr.ownershipPath);
|
|
347
369
|
await assert.rejects(
|
|
348
|
-
() => mgr.release(token),
|
|
370
|
+
() => mgr.release(res2.token!),
|
|
349
371
|
(e) => e instanceof EnvironmentLockIOError,
|
|
350
372
|
);
|
|
351
|
-
assert.equal(mgr.isHolding, true, '
|
|
373
|
+
assert.equal(mgr.isHolding, true, '双失败后必须保留 activeToken(可重试,不卡死磁盘锁)');
|
|
352
374
|
assert.ok(fssync.existsSync(mgr.ownershipPath), '锁文件仍在磁盘(未被误删)');
|
|
353
375
|
// 恢复后重试 release 成功
|
|
354
376
|
ctl.clear();
|
|
355
|
-
await mgr.release(token);
|
|
377
|
+
await mgr.release(res2.token!);
|
|
356
378
|
assert.equal(mgr.isHolding, false);
|
|
357
379
|
assert.equal(fssync.existsSync(mgr.ownershipPath), false, 'release 后 ownership 被删除');
|
|
358
380
|
});
|
|
@@ -1415,3 +1437,214 @@ test('§11.7-g 孤儿心跳清理:只删「非持有者且心跳过期」的 s
|
|
|
1415
1437
|
});
|
|
1416
1438
|
|
|
1417
1439
|
|
|
1440
|
+
/* ------------------------------------------------- §11.8 release 失败留痕与自愈(真机 423 卡死态) */
|
|
1441
|
+
|
|
1442
|
+
/*
|
|
1443
|
+
* 真机现场(v0.4.3):autosync 取锁后 release 的 unlink 失败(Windows 句柄冲突),调用方
|
|
1444
|
+
* `release().catch(() => {})` 把错误吞掉 → ownership 留在盘上、心跳停在 seq=3,而进程仍活
|
|
1445
|
+
* ⇒ inspectLockState 判 LOCKED(owner alive degraded) ⇒ 永不 STALE、显式回收也拒
|
|
1446
|
+
* ⇒ 档案删除等 destructive 操作**永久** 423「另一个任务正在运行」。
|
|
1447
|
+
* 这一组锁定修法的三条边:① 自己删不掉时把记录挪走(quarantine)而不是留原地;
|
|
1448
|
+
* ② 盘上是自己的残留时 acquire 能结构可证地自愈;③ 进程存活但心跳长过期要能被显式回收。
|
|
1449
|
+
*/
|
|
1450
|
+
|
|
1451
|
+
test('§11.8-a release 的 unlink 反复失败 → quarantine 腾空正式路径(不抛),随后 acquire 仍可用', async (t) => {
|
|
1452
|
+
const locksDir = tmp(t);
|
|
1453
|
+
const io = makeIo();
|
|
1454
|
+
const m = lockManager(t, { locksDir, io: io.io, heartbeatIntervalMs: 40 });
|
|
1455
|
+
const acq = await m.acquire({ op: 'import' });
|
|
1456
|
+
assert.equal(acq.state, 'ACQUIRED');
|
|
1457
|
+
await waitHeartbeat(m);
|
|
1458
|
+
|
|
1459
|
+
// 模拟「unlink 怎么都删不掉」:只有正式 ownership 路径失败,recovering 副本可删
|
|
1460
|
+
io.failUnlinkWhen((p) => path.basename(p) === OWNERSHIP_FILE);
|
|
1461
|
+
await m.release(acq.token!);
|
|
1462
|
+
assert.equal(m.isHolding, false, 'quarantine 成功即视为已释放');
|
|
1463
|
+
assert.equal(
|
|
1464
|
+
fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)),
|
|
1465
|
+
false,
|
|
1466
|
+
'正式路径必须腾空 —— 否则下一次 acquire 只会继承同一个僵尸锁',
|
|
1467
|
+
);
|
|
1468
|
+
|
|
1469
|
+
const again = await m.acquire({ op: 'import' });
|
|
1470
|
+
assert.equal(again.state, 'ACQUIRED', '释放失败不得让这个 manager 永久失去写能力');
|
|
1471
|
+
await m.release(again.token!);
|
|
1472
|
+
});
|
|
1473
|
+
|
|
1474
|
+
test('§11.8-b unlink 与 rename 双失败 → release 抛错并保留 token,故障消失后 acquire 自愈回收自己的残留', async (t) => {
|
|
1475
|
+
const locksDir = tmp(t);
|
|
1476
|
+
const io = makeIo();
|
|
1477
|
+
const reports: Array<{ quarantined: boolean }> = [];
|
|
1478
|
+
const m = lockManager(t, {
|
|
1479
|
+
locksDir,
|
|
1480
|
+
io: io.io,
|
|
1481
|
+
heartbeatIntervalMs: 40,
|
|
1482
|
+
onReleaseFailure: (b) => { reports.push({ quarantined: b.quarantined }); },
|
|
1483
|
+
});
|
|
1484
|
+
const acq = await m.acquire({ op: 'import' });
|
|
1485
|
+
assert.equal(acq.state, 'ACQUIRED');
|
|
1486
|
+
await waitHeartbeat(m);
|
|
1487
|
+
|
|
1488
|
+
io.failUnlinkWhen((p) => path.basename(p) === OWNERSHIP_FILE);
|
|
1489
|
+
io.failRenameWhen((p) => path.basename(p) === OWNERSHIP_FILE); // quarantine 的 source 就是它
|
|
1490
|
+
await assert.rejects(() => m.release(acq.token!), (e: unknown) => e instanceof EnvironmentLockIOError);
|
|
1491
|
+
assert.equal(m.isHolding, true, 'unlink/quarantine 全失败 → 保留 token(调用方可重试 release)');
|
|
1492
|
+
assert.deepEqual(reports, [{ quarantined: false }], '失败必须留痕,不许静默');
|
|
1493
|
+
|
|
1494
|
+
// 故障消失:同一个 manager 的下一次 acquire 必须结构可证地回收自己的残留(盘上 instanceId === 自己)
|
|
1495
|
+
io.clear();
|
|
1496
|
+
const again = await m.acquire({ op: 'import' });
|
|
1497
|
+
assert.equal(again.state, 'ACQUIRED', '自己的残留必须自愈,否则每轮 destructive 都是 423');
|
|
1498
|
+
await m.release(again.token!);
|
|
1499
|
+
assert.equal(fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)), false);
|
|
1500
|
+
assert.equal(m.isHolding, false);
|
|
1501
|
+
});
|
|
1502
|
+
|
|
1503
|
+
test('§11.8-c 进程存活且身份一致但心跳长过期 → 判 stale(heartbeat-stopped),显式回收可用', async (t) => {
|
|
1504
|
+
const locksDir = tmp(t);
|
|
1505
|
+
const clk = makeClock();
|
|
1506
|
+
const p = makeProbe();
|
|
1507
|
+
p.respond(() => ({ alive: true, osProcessStartIdentity: 'identity-A' }));
|
|
1508
|
+
await seedOwnership(locksDir, { instanceId: 'zomb1', pid: 999_010, osIdentity: 'identity-A', op: 'autosync' });
|
|
1509
|
+
await seedHeartbeat(locksDir, 'zomb1', clk.clock() - 120_000);
|
|
1510
|
+
const m = lockManager(t, { locksDir, now: clk.clock, probe: p.probe, staleAfterMs: 1000, longExpiredAfterMs: 5_000 });
|
|
1511
|
+
|
|
1512
|
+
const insp = await m.inspectLockState();
|
|
1513
|
+
assert.equal(insp.state, 'STALE_LOCK_DETECTED', '心跳长过期 = 写方已停摆,不能永远说「另一个任务在运行」');
|
|
1514
|
+
assert.equal(insp.staleKind, 'heartbeat-stopped');
|
|
1515
|
+
|
|
1516
|
+
const blocked = await withMutationLock(m, { op: 'profile-delete' });
|
|
1517
|
+
assert.equal(blocked.context, null);
|
|
1518
|
+
assert.equal(blocked.reason, 'stale-degraded', '文案必须区别于「持有进程已不存在」');
|
|
1519
|
+
assert.notEqual(LOCK_BLOCK_MESSAGE['stale-degraded'], LOCK_BLOCK_MESSAGE.stale);
|
|
1520
|
+
assert.match(LOCK_BLOCK_MESSAGE['stale-degraded'], /回收/);
|
|
1521
|
+
|
|
1522
|
+
const rec = await m.recoverStaleLock();
|
|
1523
|
+
assert.equal(rec.ok, true, '二次验证必须与 inspect 同判据,否则用户点「回收残留锁」只会拿到「失败」');
|
|
1524
|
+
assert.deepEqual(fssync.readdirSync(locksDir), []);
|
|
1525
|
+
});
|
|
1526
|
+
|
|
1527
|
+
test('§11.8-d 心跳未达长过期 + 进程存活 → 仍 LOCKED,acquire 与 recover 都不放行(不放开误删)', async (t) => {
|
|
1528
|
+
const locksDir = tmp(t);
|
|
1529
|
+
const clk = makeClock();
|
|
1530
|
+
const p = makeProbe();
|
|
1531
|
+
p.respond(() => ({ alive: true, osProcessStartIdentity: 'identity-A' }));
|
|
1532
|
+
await seedOwnership(locksDir, { instanceId: 'live1', pid: 999_011, osIdentity: 'identity-A' });
|
|
1533
|
+
await seedHeartbeat(locksDir, 'live1', clk.clock() - 2_000);
|
|
1534
|
+
const m = lockManager(t, { locksDir, now: clk.clock, probe: p.probe, staleAfterMs: 1000, longExpiredAfterMs: 60_000 });
|
|
1535
|
+
|
|
1536
|
+
const insp = await m.inspectLockState();
|
|
1537
|
+
assert.equal(insp.state, 'LOCKED', '活锁就是活锁:心跳过期但没长过期,不能判残留');
|
|
1538
|
+
assert.equal(insp.staleKind, undefined);
|
|
1539
|
+
assert.equal((await withMutationLock(m, { op: 'profile-delete' })).reason, 'locked');
|
|
1540
|
+
assert.equal((await m.recoverStaleLock()).ok, false);
|
|
1541
|
+
assert.ok(fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)), '拒绝后现场必须原样保留');
|
|
1542
|
+
});
|
|
1543
|
+
|
|
1544
|
+
/* ------------------------------------------- §11.9 长持有看门狗(issue:锁与心跳都不释放) */
|
|
1545
|
+
|
|
1546
|
+
/**
|
|
1547
|
+
* 手动 tick 的假定时器(只实现 LockTimerApi 的两个方法)。
|
|
1548
|
+
* 与真实定时器不同:到点由测试显式 fire(ms),不依赖墙钟与事件循环负载。
|
|
1549
|
+
*/
|
|
1550
|
+
function manualTimers(): {
|
|
1551
|
+
api: { setInterval(fn: () => void | Promise<void>, ms: number): unknown; clearInterval(handle: unknown): void };
|
|
1552
|
+
/** 触发所有 ms 匹配且未被清除的定时器一次;返回触发个数 */
|
|
1553
|
+
fire(ms: number): number;
|
|
1554
|
+
/** 仍存活的定时器个数(含 heartbeat) */
|
|
1555
|
+
live(): number;
|
|
1556
|
+
} {
|
|
1557
|
+
const handles: Array<{ id: object; fn: () => void | Promise<void>; ms: number; cleared: boolean }> = [];
|
|
1558
|
+
return {
|
|
1559
|
+
api: {
|
|
1560
|
+
setInterval(fn, ms) { const h = { id: {}, fn, ms, cleared: false }; handles.push(h); return h.id; },
|
|
1561
|
+
clearInterval(handle) { const h = handles.find((x) => x.id === handle); if (h !== undefined) h.cleared = true; },
|
|
1562
|
+
},
|
|
1563
|
+
fire(ms) {
|
|
1564
|
+
let n = 0;
|
|
1565
|
+
for (const h of handles.slice()) {
|
|
1566
|
+
if (h.cleared || h.ms !== ms) continue;
|
|
1567
|
+
h.cleared = true;
|
|
1568
|
+
n += 1;
|
|
1569
|
+
void h.fn();
|
|
1570
|
+
}
|
|
1571
|
+
return n;
|
|
1572
|
+
},
|
|
1573
|
+
live() { return handles.filter((h) => !h.cleared).length; },
|
|
1574
|
+
};
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
test('§11.9-a 长持有看门狗:超过阈值只告警一次(点名 op 与持有时长),绝不强拆活持有者', async (t) => {
|
|
1578
|
+
const locksDir = tmp(t);
|
|
1579
|
+
const timers = manualTimers();
|
|
1580
|
+
let clock = 1_000_000;
|
|
1581
|
+
const seen: Array<{ op: string; heldMs: number; tokenId: string }> = [];
|
|
1582
|
+
const mgr = lockManager(t, {
|
|
1583
|
+
locksDir,
|
|
1584
|
+
timers: timers.api,
|
|
1585
|
+
now: () => clock,
|
|
1586
|
+
heartbeatIntervalMs: 1000,
|
|
1587
|
+
holdWarnAfterMs: 5000,
|
|
1588
|
+
onHoldExceeded: (info) => { seen.push(info); },
|
|
1589
|
+
});
|
|
1590
|
+
const res = await mgr.acquire({ op: 'autosync' });
|
|
1591
|
+
assert.equal(res.state, 'ACQUIRED');
|
|
1592
|
+
assert.equal(seen.length, 0, '刚 acquire 不得告警');
|
|
1593
|
+
clock += 5000;
|
|
1594
|
+
assert.equal(timers.fire(5000), 1, '看门狗必须在阈值到点触发');
|
|
1595
|
+
assert.equal(seen.length, 1);
|
|
1596
|
+
assert.equal(seen[0]!.op, 'autosync', '告警必须点名是哪个 op 卡住了');
|
|
1597
|
+
assert.ok(seen[0]!.heldMs >= 5000, '告警必须给出已持有时长: ' + seen[0]!.heldMs);
|
|
1598
|
+
// 只观察、不解锁:活持有者可能真在跑长任务,强拆会造出双持有(比卡死更难查)
|
|
1599
|
+
assert.equal(mgr.isHolding, true, '看门狗绝不释放锁');
|
|
1600
|
+
assert.notEqual(await readOwnership(locksDir), null, 'ownership 仍在盘上');
|
|
1601
|
+
assert.deepEqual(mgr.holdInfo, { op: 'autosync', heldMs: 5000 }, 'holdInfo 给出「是哪个 op 持有」');
|
|
1602
|
+
// 触发后自行停掉:不得每 tick 重复告警
|
|
1603
|
+
assert.equal(timers.fire(5000), 0, '看门狗命中一次即停');
|
|
1604
|
+
assert.equal(seen.length, 1);
|
|
1605
|
+
await mgr.release(res.token!);
|
|
1606
|
+
});
|
|
1607
|
+
|
|
1608
|
+
test('§11.9-b release 之后看门狗随之失效(停表 + holdInfo 为空)', async (t) => {
|
|
1609
|
+
const locksDir = tmp(t);
|
|
1610
|
+
const timers = manualTimers();
|
|
1611
|
+
let clock = 2_000_000;
|
|
1612
|
+
const seen: unknown[] = [];
|
|
1613
|
+
const mgr = lockManager(t, {
|
|
1614
|
+
locksDir, timers: timers.api, now: () => clock,
|
|
1615
|
+
heartbeatIntervalMs: 1000, holdWarnAfterMs: 5000,
|
|
1616
|
+
onHoldExceeded: (i) => { seen.push(i); },
|
|
1617
|
+
});
|
|
1618
|
+
const res = await mgr.acquire({ op: 'backup' });
|
|
1619
|
+
assert.equal(timers.live(), 2, 'acquire 后应有 heartbeat + 看门狗两只定时器');
|
|
1620
|
+
await mgr.release(res.token!);
|
|
1621
|
+
assert.equal(timers.live(), 0, 'release 必须停掉全部定时器(含看门狗)');
|
|
1622
|
+
assert.equal(mgr.holdInfo, null);
|
|
1623
|
+
clock += 60_000;
|
|
1624
|
+
assert.equal(timers.fire(5000), 0);
|
|
1625
|
+
assert.equal(seen.length, 0, '已释放的持有不得再告警');
|
|
1626
|
+
});
|
|
1627
|
+
|
|
1628
|
+
test('§11.9-c holdWarnAfterMs=0 关闭看门狗(只留 heartbeat,供不需要告警的宿主)', async (t) => {
|
|
1629
|
+
const locksDir = tmp(t);
|
|
1630
|
+
const timers = manualTimers();
|
|
1631
|
+
const mgr = lockManager(t, { locksDir, timers: timers.api, heartbeatIntervalMs: 1000, holdWarnAfterMs: 0 });
|
|
1632
|
+
const res = await mgr.acquire({ op: 'export' });
|
|
1633
|
+
assert.equal(timers.live(), 1, '关闭时只剩 heartbeat');
|
|
1634
|
+
assert.equal(timers.fire(5000), 0);
|
|
1635
|
+
await mgr.release(res.token!);
|
|
1636
|
+
});
|
|
1637
|
+
|
|
1638
|
+
test('§11.9-d lockSkipToken:锁分类 → 稳定机器 token(只有真被活任务占用才叫 mutation-locked)', () => {
|
|
1639
|
+
// issue:此前所有被挡路径都发 'mutation-locked',客户端只能写「被占用或残留」,
|
|
1640
|
+
// 真机 locks 目录为空时这句文案把排查方向带偏。分档后一类一个 token。
|
|
1641
|
+
assert.equal(lockSkipToken('blocked'), 'mutation-safe-mode');
|
|
1642
|
+
assert.equal(lockSkipToken('locked'), 'mutation-locked');
|
|
1643
|
+
assert.equal(lockSkipToken('stale'), 'mutation-lock-stale');
|
|
1644
|
+
assert.equal(lockSkipToken('stale-degraded'), 'mutation-lock-stale');
|
|
1645
|
+
assert.equal(lockSkipToken('unavailable'), 'mutation-lock-unavailable');
|
|
1646
|
+
// acquire 直接抛错(未拿到分类)→ 不可用,而不是谎称被占用
|
|
1647
|
+
assert.equal(lockSkipToken(undefined), 'mutation-lock-unavailable');
|
|
1648
|
+
});
|
|
1649
|
+
|
|
1650
|
+
|