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
package/lib/utils/env-lock.js
CHANGED
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
* 关键不变量(NON-NEGOTIABLE,违反即破坏正确性):
|
|
9
9
|
* - 所有权获取 **必须** `open(lockPath, 'wx')` 独占创建语义;**禁止** exists→write、rm→recreate、
|
|
10
10
|
* 用 Phase 1 `atomicWriteFile(environment.lock)` 获取所有权。
|
|
11
|
-
* - `environment.lock` 是 **immutable ownership record**:创建后直到 release 不被 rename/replace
|
|
11
|
+
* - `environment.lock` 是 **immutable ownership record**:创建后直到 release 不被 rename/replace
|
|
12
|
+
* (唯一例外:release 的 unlink 失败 / acquire 发现自己的残留时,按 instanceId 校验后
|
|
13
|
+
* rename 到 `environment.recovering.*` ——「quarantine 自己」,见 quarantineOwnership)。
|
|
12
14
|
* - heartbeat 走 **独立 sidecar** `environment.heartbeat.<instanceId>`,可安全用 Phase 1 `atomicWriteFile` 更新。
|
|
13
15
|
* - **operation-scoped** `MutationLockToken`:禁止 process-level reentrant(instanceId/handle/reenterCount 判嵌套)。
|
|
14
16
|
* - stale 检测只分类(LOCKED / STALE_LOCK_DETECTED / UNKNOWN_STATE);**acquire 侧绝不自动 unlink/takeover**。
|
|
@@ -59,6 +61,9 @@ export const DEFAULT_LONG_EXPIRED_AFTER_MS = 30 * 60_000;
|
|
|
59
61
|
/** acquire 中「EEXIST 但 ownership 读不到」的竞态重试上限(issue #79):文件在两次读取之间
|
|
60
62
|
* 被释放时重试 open('wx') 即可拿到锁,不在锁间隙里空等 acquireTimeoutMs。 */
|
|
61
63
|
const MAX_FREE_RETRIES = 5;
|
|
64
|
+
/** 长持有告警阈值(缺省 2 分钟):正常 mutation(导出/合并/推送)远短于此,超过即视为可疑,
|
|
65
|
+
* 但**只告警不解锁** —— 活持有者可能真的在跑长任务,强拆会造出双持有(比卡死更难查)。 */
|
|
66
|
+
export const DEFAULT_HOLD_WARN_AFTER_MS = 120_000;
|
|
62
67
|
/**
|
|
63
68
|
* 便捷包装:核心引擎「无父 token → acquire;有有效父 token → reuse(不 reacquire)」。
|
|
64
69
|
* 返回 { token, context, release }:调用方在 finally 中调用 release(仅当本次真正 acquire 才 release)。
|
|
@@ -98,7 +103,9 @@ export async function withMutationLock(port, opts) {
|
|
|
98
103
|
const reason = res.state === 'LOCKED'
|
|
99
104
|
? 'locked'
|
|
100
105
|
: res.state === 'STALE_LOCK_DETECTED'
|
|
101
|
-
|
|
106
|
+
// 残留锁再分两类(issue:真机 release 失败留痕):结构可证(进程已死/PID 复用)与
|
|
107
|
+
// 「进程还活着但心跳长期停止」。后者不能谎称「持有进程已不存在」,必须给出不同的处置指引。
|
|
108
|
+
? (res.staleKind === 'heartbeat-stopped' ? 'stale-degraded' : 'stale')
|
|
102
109
|
: 'unavailable';
|
|
103
110
|
return { context: null, release: async () => { }, reason, detail: res.detail };
|
|
104
111
|
}
|
|
@@ -370,6 +377,14 @@ export class EnvironmentLockManager {
|
|
|
370
377
|
acquireTimeoutMs;
|
|
371
378
|
lockVersion;
|
|
372
379
|
onHeartbeatWriteFailure;
|
|
380
|
+
onReleaseFailure;
|
|
381
|
+
/** 长持有看门狗阈值/回调(见 EnvLockManagerOptions.holdWarnAfterMs) */
|
|
382
|
+
holdWarnAfterMs;
|
|
383
|
+
onHoldExceeded;
|
|
384
|
+
/** 看门狗定时器句柄(与 heartbeatTimer 分开:一个是续期,一个是「该不该还没释放」) */
|
|
385
|
+
holdWatchdogTimer = null;
|
|
386
|
+
holdStartedAt = 0;
|
|
387
|
+
holdOp = '';
|
|
373
388
|
/** 诊断用(acquire 时写 ownership.op/target) */
|
|
374
389
|
defaultOp;
|
|
375
390
|
defaultTarget;
|
|
@@ -382,6 +397,10 @@ export class EnvironmentLockManager {
|
|
|
382
397
|
/** heartbeat 定时器句柄(注入面返回值,类型未知 → 用 null 判「未启动」) */
|
|
383
398
|
heartbeatTimer = null;
|
|
384
399
|
heartbeatSeq = 0;
|
|
400
|
+
/** 在飞 acquire 计数:reclaimOwnLeftover 只在「本 manager 此刻没有别的 acquire 在飞」时动手。
|
|
401
|
+
* 否则 A 刚 open('wx') 成功、还没写上 activeToken 的那一瞬,并发 B 会把 A 的**新锁**当成自己的
|
|
402
|
+
* 残留挪走 —— §11.1-c23 实测会出现两个 ACQUIRED(双持有),那是比卡死更难查的错。 */
|
|
403
|
+
acquireInFlight = 0;
|
|
385
404
|
activeInstanceId;
|
|
386
405
|
/** 串行化的 heartbeat 写链(**最后一次写**的 promise);release 前用于 drain,见 drainHeartbeat */
|
|
387
406
|
pendingHeartbeat = Promise.resolve();
|
|
@@ -411,6 +430,9 @@ export class EnvironmentLockManager {
|
|
|
411
430
|
this.acquireTimeoutMs = opts.acquireTimeoutMs ?? DEFAULT_ACQUIRE_TIMEOUT_MS;
|
|
412
431
|
this.lockVersion = opts.lockVersion ?? '0.1.0';
|
|
413
432
|
this.onHeartbeatWriteFailure = opts.onHeartbeatWriteFailure ?? (() => { });
|
|
433
|
+
this.onReleaseFailure = opts.onReleaseFailure ?? (() => { });
|
|
434
|
+
this.holdWarnAfterMs = opts.holdWarnAfterMs ?? DEFAULT_HOLD_WARN_AFTER_MS;
|
|
435
|
+
this.onHoldExceeded = opts.onHoldExceeded ?? (() => { });
|
|
414
436
|
this.timers = opts.timers ?? defaultTimers;
|
|
415
437
|
this.defaultOp = opts.op ?? 'mutation';
|
|
416
438
|
this.defaultTarget = opts.target ?? 'unknown';
|
|
@@ -442,122 +464,137 @@ export class EnvironmentLockManager {
|
|
|
442
464
|
: null;
|
|
443
465
|
/** 「EEXIST/EPERM 说文件在,inspect 却读不到」= 文件在两次读取之间被释放的竞态(issue #79) */
|
|
444
466
|
let freeRetries = 0;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
return classifyIoError(e, utilsMsg('utils.envLock.mkdirLocksDir', { dir: this.locksDir }), this.io);
|
|
452
|
-
}
|
|
453
|
-
// —— 独占创建所有权文件(唯一所有权获取原语)——
|
|
454
|
-
let handle = null;
|
|
455
|
-
try {
|
|
456
|
-
handle = await this.io.open(this.ownershipPath, 'wx', 0o600);
|
|
457
|
-
}
|
|
458
|
-
catch (e) {
|
|
459
|
-
const code = e.code;
|
|
460
|
-
// EBUSY:Windows sharing violation / 杀软 / 索引器瞬时占用 → 有界重试(Windows P2-3)
|
|
461
|
-
if (code === 'EBUSY') {
|
|
462
|
-
const transient = await this.tryTransientRetry('open');
|
|
463
|
-
if (transient)
|
|
464
|
-
continue;
|
|
465
|
-
return classifyIoError(e, `open('wx') ${this.ownershipPath}`, this.io);
|
|
467
|
+
this.acquireInFlight += 1;
|
|
468
|
+
try {
|
|
469
|
+
for (;;) {
|
|
470
|
+
// —— 先确保 locks 目录存在(mkdir 递归;EPERM 在下面归类为 PERMISSION_ERROR)——
|
|
471
|
+
try {
|
|
472
|
+
await this.io.mkdir(this.locksDir, { recursive: true });
|
|
466
473
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
474
|
+
catch (e) {
|
|
475
|
+
return classifyIoError(e, utilsMsg('utils.envLock.mkdirLocksDir', { dir: this.locksDir }), this.io);
|
|
476
|
+
}
|
|
477
|
+
// —— 独占创建所有权文件(唯一所有权获取原语)——
|
|
478
|
+
let handle = null;
|
|
479
|
+
try {
|
|
480
|
+
handle = await this.io.open(this.ownershipPath, 'wx', 0o600);
|
|
481
|
+
}
|
|
482
|
+
catch (e) {
|
|
483
|
+
const code = e.code;
|
|
484
|
+
// EBUSY:Windows sharing violation / 杀软 / 索引器瞬时占用 → 有界重试(Windows P2-3)
|
|
485
|
+
if (code === 'EBUSY') {
|
|
486
|
+
const transient = await this.tryTransientRetry('open');
|
|
487
|
+
if (transient)
|
|
481
488
|
continue;
|
|
482
|
-
}
|
|
483
|
-
return { state: 'LOCKED', token: null, detail: inspect.detail };
|
|
489
|
+
return classifyIoError(e, `open('wx') ${this.ownershipPath}`, this.io);
|
|
484
490
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
}
|
|
488
|
-
// EPERM/EACCES 等(Windows open('wx') 对存在文件常抛 EPERM 而非 EEXIST):
|
|
489
|
-
// 按 §8.1 分类 —— 先检查 environment.lock 是否确实存在且可读取;
|
|
490
|
-
// 存在 → 按 existing lock inspect(可能正是活跃锁被 Windows 以 EPERM 拒绝);
|
|
491
|
-
// 不存在/无法确认 → LOCK_IO_ERROR / PERMISSION_ERROR(绝不误报「Locked/另一任务在运行」)。
|
|
492
|
-
if (code === 'EPERM' || code === 'EACCES') {
|
|
493
|
-
const exists = await this.statLockExists();
|
|
494
|
-
if (exists === true) {
|
|
491
|
+
if (code === 'EEXIST') {
|
|
492
|
+
// 已存在 → inspect owner
|
|
495
493
|
const inspect = await this.inspectLockState();
|
|
496
|
-
//
|
|
494
|
+
// issue #79:EEXIST 与「ownership 读不到」同时出现 = 上一次占用的释放竞态。
|
|
495
|
+
// 此前直接判 LOCKED,一次无害的竞态就变成「另一个任务正在运行」并可能被记成
|
|
496
|
+
// mutation-locked(真机:连续 16 次跳过,用户侧看不出原因)。重试 open('wx') 即可。
|
|
497
|
+
// 盘上这份 ownership 是本 manager 自己的残留(上一次 release 失败留在盘上)→ 结构可证地
|
|
498
|
+
// 自愈后重试;否则它会以「owner alive degraded」永久占住锁,所有 destructive 操作恒 423。
|
|
499
|
+
if (await this.reclaimOwnLeftover(inspect))
|
|
500
|
+
continue;
|
|
497
501
|
if (inspect.state === 'FREE' && freeRetries < MAX_FREE_RETRIES) {
|
|
498
502
|
freeRetries += 1;
|
|
499
503
|
continue;
|
|
500
504
|
}
|
|
501
|
-
if (inspect.state === 'LOCKED'
|
|
502
|
-
|
|
503
|
-
|
|
505
|
+
if (inspect.state === 'LOCKED') {
|
|
506
|
+
// 等 acquireTimeoutMs 后重试;超时则返回 LOCKED
|
|
507
|
+
if (this.acquireTimeoutMs > 0 && this.now() - started < this.acquireTimeoutMs) {
|
|
508
|
+
await sleep(this.heartbeatIntervalMs / 2);
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
return { state: 'LOCKED', token: null, detail: inspect.detail };
|
|
504
512
|
}
|
|
505
|
-
|
|
513
|
+
// STALE/UNKNOWN/IO 直接返回(不自动删除;destructive 不执行)
|
|
514
|
+
return { state: inspect.state, token: null, detail: inspect.detail, staleKind: inspect.staleKind };
|
|
506
515
|
}
|
|
507
|
-
//
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
516
|
+
// EPERM/EACCES 等(Windows open('wx') 对存在文件常抛 EPERM 而非 EEXIST):
|
|
517
|
+
// 按 §8.1 分类 —— 先检查 environment.lock 是否确实存在且可读取;
|
|
518
|
+
// 存在 → 按 existing lock inspect(可能正是活跃锁被 Windows 以 EPERM 拒绝);
|
|
519
|
+
// 不存在/无法确认 → LOCK_IO_ERROR / PERMISSION_ERROR(绝不误报「Locked/另一任务在运行」)。
|
|
520
|
+
if (code === 'EPERM' || code === 'EACCES') {
|
|
521
|
+
const exists = await this.statLockExists();
|
|
522
|
+
if (exists === true) {
|
|
523
|
+
const inspect = await this.inspectLockState();
|
|
524
|
+
// 同 EEXIST 分支:自己的残留 → 自愈后重试
|
|
525
|
+
if (await this.reclaimOwnLeftover(inspect))
|
|
526
|
+
continue;
|
|
527
|
+
// 同 EEXIST 分支:文件刚被释放的竞态 → 重试独占创建(不是「被占用」)
|
|
528
|
+
if (inspect.state === 'FREE' && freeRetries < MAX_FREE_RETRIES) {
|
|
529
|
+
freeRetries += 1;
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
if (inspect.state === 'LOCKED' && this.acquireTimeoutMs > 0 && this.now() - started < this.acquireTimeoutMs) {
|
|
533
|
+
await sleep(this.heartbeatIntervalMs / 2);
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
return { state: inspect.state, token: null, detail: inspect.detail, staleKind: inspect.staleKind };
|
|
537
|
+
}
|
|
538
|
+
// 无既有锁 → 权限/ACL/文件系统错误(可能目录不可写 / ACL 拒绝),非锁占用
|
|
539
|
+
return exists === false
|
|
540
|
+
? { state: 'PERMISSION_ERROR', token: null, detail: utilsMsg('utils.envLock.openWxPermissionNoExisting', { path: this.ownershipPath, code }) }
|
|
541
|
+
: classifyIoError(e, `open('wx') ${this.ownershipPath}`, this.io);
|
|
542
|
+
}
|
|
543
|
+
return classifyIoError(e, `open('wx') ${this.ownershipPath}`, this.io);
|
|
511
544
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
545
|
+
// —— 独占创建成功:写入 immutable owner(一次性,之后不再替换)——
|
|
546
|
+
try {
|
|
547
|
+
const ownerRecord = {
|
|
548
|
+
schemaVersion: LOCK_SCHEMA_VERSION,
|
|
549
|
+
owner: {
|
|
550
|
+
instanceId: this.activeInstanceId,
|
|
551
|
+
instanceStartedAt: Date.now(),
|
|
552
|
+
pid: process.pid,
|
|
553
|
+
hostname: os.hostname(),
|
|
554
|
+
osProcessStartIdentity: selfOsIdentity,
|
|
555
|
+
},
|
|
556
|
+
op,
|
|
557
|
+
target,
|
|
558
|
+
acquiredAt: this.now(),
|
|
559
|
+
lockVersion: this.lockVersion,
|
|
560
|
+
journalId: null,
|
|
561
|
+
};
|
|
562
|
+
await handle.writeFile(encode(ownerRecord));
|
|
563
|
+
await handle.sync();
|
|
564
|
+
await handle.close();
|
|
565
|
+
handle = null;
|
|
566
|
+
}
|
|
567
|
+
catch (e) {
|
|
568
|
+
// 写入 owner 失败:必须回滚 —— 关闭句柄 + 尽力删除刚创建的 lock(此刻所有权尚未确立,删除自己是安全的)
|
|
569
|
+
if (handle)
|
|
570
|
+
try {
|
|
571
|
+
await handle.close();
|
|
572
|
+
}
|
|
573
|
+
catch { /* ignore */ }
|
|
574
|
+
this.activeToken = null;
|
|
539
575
|
try {
|
|
540
|
-
await
|
|
576
|
+
await this.io.unlink(this.ownershipPath);
|
|
541
577
|
}
|
|
542
|
-
catch { /*
|
|
543
|
-
|
|
544
|
-
try {
|
|
545
|
-
await this.io.unlink(this.ownershipPath);
|
|
578
|
+
catch { /* 清理失败留痕由上层 */ }
|
|
579
|
+
return classifyIoError(e, utilsMsg('utils.envLock.writeOwner', { path: this.ownershipPath }), this.io);
|
|
546
580
|
}
|
|
547
|
-
|
|
548
|
-
|
|
581
|
+
// —— 确立 token + 启动 heartbeat ——
|
|
582
|
+
const token = {
|
|
583
|
+
tokenId: randomHex(16),
|
|
584
|
+
managerId: this.managerId,
|
|
585
|
+
instanceId: this.activeInstanceId,
|
|
586
|
+
acquiredAt: this.now(),
|
|
587
|
+
};
|
|
588
|
+
this.activeToken = token;
|
|
589
|
+
this.heartbeatSeq = 0;
|
|
590
|
+
this.startHeartbeat();
|
|
591
|
+
// 与 heartbeat 同期启动看门狗:它回答的是另一个问题 ——「这次持有是不是早该释放了」
|
|
592
|
+
this.startHoldWatchdog(op, token.tokenId);
|
|
593
|
+
return { state: 'ACQUIRED', token, detail: `op=${op}` };
|
|
549
594
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
managerId: this.managerId,
|
|
554
|
-
instanceId: this.activeInstanceId,
|
|
555
|
-
acquiredAt: this.now(),
|
|
556
|
-
};
|
|
557
|
-
this.activeToken = token;
|
|
558
|
-
this.heartbeatSeq = 0;
|
|
559
|
-
this.startHeartbeat();
|
|
560
|
-
return { state: 'ACQUIRED', token, detail: `op=${op}` };
|
|
595
|
+
}
|
|
596
|
+
finally {
|
|
597
|
+
this.acquireInFlight -= 1;
|
|
561
598
|
}
|
|
562
599
|
}
|
|
563
600
|
/* ------------------------------------------------------------ validate / release */
|
|
@@ -595,6 +632,8 @@ export class EnvironmentLockManager {
|
|
|
595
632
|
throw new EnvironmentLockOwnedByAnotherError(utilsMsg('utils.envLock.releaseForeignToken'));
|
|
596
633
|
}
|
|
597
634
|
const instanceId = this.activeInstanceId;
|
|
635
|
+
// 每一条 release 出口都经过这里(含下面全部抛错分支)→ 看门狗在此统一停掉,不留悬空定时器
|
|
636
|
+
this.stopHoldWatchdog();
|
|
598
637
|
this.stopHeartbeat();
|
|
599
638
|
// —— release 前校验磁盘 ownership record 仍属于自己(防异常恢复/人工修改)——
|
|
600
639
|
const st = await this.readOwnershipState();
|
|
@@ -623,16 +662,47 @@ export class EnvironmentLockManager {
|
|
|
623
662
|
throw new EnvironmentLockOwnedByAnotherError(utilsMsg('utils.envLock.releaseOwnershipMismatch', { diskId: st.rec.owner.instanceId, tokenId: instanceId }));
|
|
624
663
|
}
|
|
625
664
|
// —— matching:unlink ——(acquire 成功后句柄已 close,此处只有 unlink)
|
|
626
|
-
|
|
627
|
-
|
|
665
|
+
// 有界瞬时重试(Windows 上杀软/索引器/搜索服务会短暂持有句柄 → EBUSY/EPERM 自行消失)。
|
|
666
|
+
let unlinkErr = null;
|
|
667
|
+
this.transientRetries = 0;
|
|
668
|
+
for (;;) {
|
|
669
|
+
try {
|
|
670
|
+
await this.io.unlink(this.ownershipPath);
|
|
671
|
+
unlinkErr = null;
|
|
672
|
+
break;
|
|
673
|
+
}
|
|
674
|
+
catch (e) {
|
|
675
|
+
// ENOENT:正式路径已经没了 → 释放目的已达成(不是失败)
|
|
676
|
+
if (isENOENT(e)) {
|
|
677
|
+
unlinkErr = null;
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
unlinkErr = e;
|
|
681
|
+
if (await this.tryTransientRetry('unlink'))
|
|
682
|
+
continue;
|
|
683
|
+
break;
|
|
684
|
+
}
|
|
628
685
|
}
|
|
629
|
-
|
|
630
|
-
//
|
|
631
|
-
//
|
|
686
|
+
if (unlinkErr !== null) {
|
|
687
|
+
// 仍删不掉(真机卡死态的起点):以前这里直接抛错,而调用方普遍 `release().catch(() => {})`
|
|
688
|
+
// → ownership 留在盘上、心跳已停、进程仍活 ⇒ inspectLockState 判「owner alive degraded」
|
|
689
|
+
// ⇒ 永不 STALE ⇒ 所有 destructive 操作恒 423,用户只能手工删锁文件。
|
|
690
|
+
// 现在先把 ownership 挪出正式路径(quarantine)解除阻塞,再如实留痕。
|
|
691
|
+
const quarantined = await this.quarantineOwnership(instanceId);
|
|
692
|
+
if (quarantined) {
|
|
693
|
+
this.activeToken = null;
|
|
694
|
+
await this.drainHeartbeat();
|
|
695
|
+
await this.cleanupHeartbeat(instanceId).catch(() => { });
|
|
696
|
+
this.onReleaseFailure({ err: unlinkErr, quarantined: true });
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
// quarantine 也失败(rename 与 unlink 双双被拒):**保留 activeToken**(仍持有该磁盘 inode),
|
|
700
|
+
// 调用方可重试 release;绝不在此清空 token(否则锁卡死在磁盘而令牌失效)。
|
|
632
701
|
// 同 ownership-lost 分支:抛错前 drain 在途写(定时器已停 → drain 有界),
|
|
633
702
|
// 否则调用方随后的目录清理会与已启动的 tmp→rename 竞争。
|
|
634
703
|
await this.drainHeartbeat();
|
|
635
|
-
|
|
704
|
+
this.onReleaseFailure({ err: unlinkErr, quarantined: false });
|
|
705
|
+
throw new EnvironmentLockIOError(utilsMsg('utils.envLock.releaseUnlinkFailed', { path: this.ownershipPath, reason: unlinkErr instanceof Error ? unlinkErr.message : String(unlinkErr) }), unlinkErr);
|
|
636
706
|
}
|
|
637
707
|
// unlink 成功 → 释放完成:**先清 token 再 drain**,最后清自己的 heartbeat sidecar。
|
|
638
708
|
// 顺序关键:activeToken=null 必须早于 drain —— 否则 interval 可能在 drain 返回之后、cleanup 之前
|
|
@@ -642,6 +712,67 @@ export class EnvironmentLockManager {
|
|
|
642
712
|
await this.drainHeartbeat();
|
|
643
713
|
await this.cleanupHeartbeat(instanceId).catch(() => { });
|
|
644
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* 把正式路径上的 ownership 记录挪到 `environment.recovering.*`(quarantine)。
|
|
717
|
+
* 只用于**确证属于本 manager** 的路径:release 的 unlink 失败、acquire 发现盘上是自己的 instanceId。
|
|
718
|
+
*
|
|
719
|
+
* 为什么用 rename 而不是继续 unlink:unlink 删不掉说明「这个目录项删不动」(Windows 共享冲突 /
|
|
720
|
+
* 杀软句柄),重复同一操作不会变好;rename 只移动目录项、不删 inode,能立刻把正式路径腾空,
|
|
721
|
+
* 让后续 acquire 继续。挪走后二次读校验 captured 的 instanceId 仍是自己的才删副本;
|
|
722
|
+
* 不符(异常:被他人写入)就保留副本可查 —— 阻塞已解除,这是本方法与「直接抛错」的关键区别。
|
|
723
|
+
*/
|
|
724
|
+
async quarantineOwnership(instanceId) {
|
|
725
|
+
const capturedPath = path.join(this.locksDir, `${RECOVERING_PREFIX}${randomHex(8)}`);
|
|
726
|
+
try {
|
|
727
|
+
await this.io.rename(this.ownershipPath, capturedPath);
|
|
728
|
+
}
|
|
729
|
+
catch (e) {
|
|
730
|
+
// ENOENT:正式路径已经空了 → 目的达成(没有可 quarantine 的对象)
|
|
731
|
+
return isENOENT(e);
|
|
732
|
+
}
|
|
733
|
+
let own = false;
|
|
734
|
+
try {
|
|
735
|
+
const captured = readLockJsonSafe(await this.io.readFileText(capturedPath));
|
|
736
|
+
own = captured !== null && typeof captured === 'object' && captured.owner?.instanceId === instanceId;
|
|
737
|
+
}
|
|
738
|
+
catch {
|
|
739
|
+
own = false;
|
|
740
|
+
}
|
|
741
|
+
// 只删确证是自己的副本;否则保留(不删他人数据)
|
|
742
|
+
if (own)
|
|
743
|
+
await this.io.unlink(capturedPath).catch(() => { });
|
|
744
|
+
return true;
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* 回收「本 manager 自己上一次释放留下的残留」。
|
|
748
|
+
*
|
|
749
|
+
* 真机卡死态:release 的 unlink 失败被调用方 `release().catch(() => {})` 静默吞掉 →
|
|
750
|
+
* ownership 留在盘上、心跳已停,而本进程仍活 ⇒ inspectLockState 判 LOCKED(owner alive degraded)
|
|
751
|
+
* ⇒ 永不 STALE、显式回收也拒 ⇒ 所有 destructive 操作永久 423。
|
|
752
|
+
*
|
|
753
|
+
* 判据必须结构可证(否则就成了跨实例误删):盘上 instanceId === 本 manager 的 activeInstanceId
|
|
754
|
+
* (随机 16 字节、只有本 manager 会产生 ⇒ 宿主内不可能是别人的锁),并且本 manager 此刻
|
|
755
|
+
* 没有在途临界区(activeToken === null),或 release 已进入但没走完(heartbeatTimer === null,
|
|
756
|
+
* 即 stopHeartbeat() 已执行)。两者都不成立时一律返回 false。
|
|
757
|
+
*/
|
|
758
|
+
async reclaimOwnLeftover(insp) {
|
|
759
|
+
if (insp.ownerInstanceId !== this.activeInstanceId)
|
|
760
|
+
return false;
|
|
761
|
+
// 有别的 acquire 在飞 = 盘上这份可能是它刚拿到的**新锁**(open 与 token 赋值之间有 await 窗口)
|
|
762
|
+
if (this.acquireInFlight > 1)
|
|
763
|
+
return false;
|
|
764
|
+
if (this.activeToken !== null && this.heartbeatTimer !== null)
|
|
765
|
+
return false;
|
|
766
|
+
this.stopHeartbeat();
|
|
767
|
+
const ok = await this.quarantineOwnership(this.activeInstanceId);
|
|
768
|
+
if (!ok)
|
|
769
|
+
return false;
|
|
770
|
+
this.activeToken = null;
|
|
771
|
+
this.heartbeatSeq = 0;
|
|
772
|
+
await this.drainHeartbeat();
|
|
773
|
+
await this.cleanupHeartbeat(this.activeInstanceId).catch(() => { });
|
|
774
|
+
return true;
|
|
775
|
+
}
|
|
645
776
|
/* ------------------------------------------------------------ heartbeat */
|
|
646
777
|
startHeartbeat() {
|
|
647
778
|
if (this.heartbeatTimer !== null)
|
|
@@ -663,6 +794,56 @@ export class EnvironmentLockManager {
|
|
|
663
794
|
this.heartbeatTimer = null;
|
|
664
795
|
}
|
|
665
796
|
}
|
|
797
|
+
/**
|
|
798
|
+
* 长持有看门狗:持有时间超过阈值时**告警一次**,然后自行停掉。
|
|
799
|
+
*
|
|
800
|
+
* 只做观察,不做解锁。真机 issue(Windows 桌面端):「autosync 成功后 ownership 与 heartbeat
|
|
801
|
+
* 都没释放、heartbeat seq 一直在涨,recover-stale-lock 判定持有者还活着拒绝回收,只能手动删
|
|
802
|
+
* locks 目录或重启桌面端」。那种状态下心跳是「活着」的,任何结构性 stale 判据都不会命中 ——
|
|
803
|
+
* 所以最需要的是**一条能定位到 op 的日志**,而不是又一个自动回收器(强拆活持有者会造出双持有)。
|
|
804
|
+
*
|
|
805
|
+
* 定时器沿用注入面(LockTimerApi 只有 setInterval):命中一次即 clearInterval,等价于一次性。
|
|
806
|
+
*/
|
|
807
|
+
startHoldWatchdog(op, tokenId) {
|
|
808
|
+
this.stopHoldWatchdog();
|
|
809
|
+
if (!(this.holdWarnAfterMs > 0))
|
|
810
|
+
return;
|
|
811
|
+
this.holdStartedAt = this.now();
|
|
812
|
+
this.holdOp = op;
|
|
813
|
+
const timer = this.timers.setInterval(() => {
|
|
814
|
+
// token 已换人/已释放 → 这次持有已结束,看门狗无事可做
|
|
815
|
+
if (this.activeToken === null || this.activeToken.tokenId !== tokenId) {
|
|
816
|
+
this.stopHoldWatchdog();
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
const heldMs = Math.max(this.now() - this.holdStartedAt, 0);
|
|
820
|
+
this.stopHoldWatchdog();
|
|
821
|
+
try {
|
|
822
|
+
this.onHoldExceeded({ op, heldMs, tokenId });
|
|
823
|
+
}
|
|
824
|
+
catch {
|
|
825
|
+
/* 观察者异常绝不影响锁状态(看门狗是观察面,不是控制面) */
|
|
826
|
+
}
|
|
827
|
+
}, Math.max(this.holdWarnAfterMs, 50));
|
|
828
|
+
this.holdWatchdogTimer = timer;
|
|
829
|
+
const handle = timer;
|
|
830
|
+
if (handle !== null && handle !== undefined && typeof handle.unref === 'function')
|
|
831
|
+
handle.unref();
|
|
832
|
+
}
|
|
833
|
+
stopHoldWatchdog() {
|
|
834
|
+
if (this.holdWatchdogTimer !== null) {
|
|
835
|
+
this.timers.clearInterval(this.holdWatchdogTimer);
|
|
836
|
+
this.holdWatchdogTimer = null;
|
|
837
|
+
}
|
|
838
|
+
// 注意:**不清** holdOp/holdStartedAt —— 告警命中后定时器即停,但这次持有的「是谁、持了多久」
|
|
839
|
+
// 仍要继续可读(holdInfo),直到 release 时 activeToken 归零自然失效。
|
|
840
|
+
}
|
|
841
|
+
/** 当前持有的操作与已持有时长(诊断用;未持有 → null)。持有超过阈值时它给出「是哪个 op」。 */
|
|
842
|
+
get holdInfo() {
|
|
843
|
+
if (this.activeToken === null)
|
|
844
|
+
return null;
|
|
845
|
+
return { op: this.holdOp, heldMs: Math.max(this.now() - this.holdStartedAt, 0) };
|
|
846
|
+
}
|
|
666
847
|
/**
|
|
667
848
|
* 等待**当前在途**的 heartbeat 写完成(不改变持有状态、不停定时器)。
|
|
668
849
|
* 首写在 startHeartbeat() 中**同步**入链,故 acquire() 之后调用本方法必然等到首写落盘。
|
|
@@ -682,6 +863,7 @@ export class EnvironmentLockManager {
|
|
|
682
863
|
* 锁的持有状态与磁盘内容由调用方的 release 决定。
|
|
683
864
|
*/
|
|
684
865
|
async stopHeartbeatAndDrain() {
|
|
866
|
+
this.stopHoldWatchdog();
|
|
685
867
|
this.stopHeartbeat();
|
|
686
868
|
await this.drainHeartbeat();
|
|
687
869
|
}
|
|
@@ -862,6 +1044,19 @@ export class EnvironmentLockManager {
|
|
|
862
1044
|
return { state: 'STALE_LOCK_DETECTED', ownerInstanceId: rec.owner.instanceId, detail: utilsMsg('utils.envLock.pidReuse', { pid: rec.owner.pid }) };
|
|
863
1045
|
}
|
|
864
1046
|
// alive 且 identity 相同 → owner alive(heartbeat degraded 保护)
|
|
1047
|
+
// issue #36 对称性(本例 = 真机卡死态):identity 相同只证明「这个 pid 还活着」,不证明它仍在持有锁。
|
|
1048
|
+
// 心跳**长过期**(远超窗口,见 longExpiredAfterMs)说明写方早已停摆(典型:release 的 unlink 失败 +
|
|
1049
|
+
// 调用方静默吞错)——此时判 STALE,让「回收残留锁」可用;acquire 侧依旧不自动摘锁
|
|
1050
|
+
// (isProvablyStale 不变),所以放宽分类不会变成跨实例误删许可。
|
|
1051
|
+
const longExpiredSame = this.longExpiredReason(heartbeat, now);
|
|
1052
|
+
if (longExpiredSame !== null) {
|
|
1053
|
+
return {
|
|
1054
|
+
state: 'STALE_LOCK_DETECTED',
|
|
1055
|
+
ownerInstanceId: rec.owner.instanceId,
|
|
1056
|
+
staleKind: 'heartbeat-stopped',
|
|
1057
|
+
detail: utilsMsg('utils.envLock.ownerAliveHeartbeatStopped', { pid: rec.owner.pid, longExpired: longExpiredSame }),
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
865
1060
|
return { state: 'LOCKED', ownerInstanceId: rec.owner.instanceId, detail: utilsMsg('utils.envLock.ownerAliveDegraded') };
|
|
866
1061
|
}
|
|
867
1062
|
/* ------------------------------------------------------------ recover (explicit) */
|
|
@@ -1026,8 +1221,11 @@ export class EnvironmentLockManager {
|
|
|
1026
1221
|
// recorded identity 与探测不同 → PID reuse → 原 owner 已死 → stale 成立
|
|
1027
1222
|
if (rec.owner.osProcessStartIdentity !== ident.osProcessStartIdentity)
|
|
1028
1223
|
return true;
|
|
1029
|
-
//
|
|
1030
|
-
|
|
1224
|
+
// 同一进程仍存活:常规判定非 stale,但必须与 inspectLockState 用**同一**判据(同 issue #36 的理由,
|
|
1225
|
+
// 见上方无 identity 能力分支):心跳长过期 ⇒ 写方已停摆,显式回收要放行;否则用户点「回收残留锁」
|
|
1226
|
+
// 只会拿到「二次验证失败」,等于没修。
|
|
1227
|
+
const hbSamePid = await this.readHeartbeat(rec.owner.instanceId).catch(() => null);
|
|
1228
|
+
return this.longExpiredReason(hbSamePid, this.now()) !== null;
|
|
1031
1229
|
}
|
|
1032
1230
|
/** 心跳「长过期」判据(issue #36):达到阈值 → 返回可读原因,否则 null。
|
|
1033
1231
|
* heartbeat sidecar 缺失时返回 null(无从判断过期时长)——只对「确实读过 heartbeat
|
|
@@ -1170,6 +1368,8 @@ export const LOCK_BLOCK_MESSAGE = {
|
|
|
1170
1368
|
// issue #31:文案承诺的「事故恢复」入口必须真的能回收残留锁——GUI 已接线
|
|
1171
1369
|
// (GET /recovery/status 的 lock 字段 + POST /recovery/lock/recover),两处入口都真实可达。
|
|
1172
1370
|
get stale() { return utilsMsg('utils.envLock.block.stale'); },
|
|
1371
|
+
// 降级形态:不谎称进程已退出,也不说「稍后重试」——处置路径与 stale 相同(显式回收)。
|
|
1372
|
+
get 'stale-degraded'() { return utilsMsg('utils.envLock.block.staleDegraded'); },
|
|
1173
1373
|
};
|
|
1174
1374
|
/** 分类的**简短**文案(单行、可用于表格单元格/迁移历史摘要/日志前缀)。
|
|
1175
1375
|
* 与 LOCK_BLOCK_MESSAGE 同源同分类:长文案给「需要完整指引」的场景(423 响应/告警日志),
|
|
@@ -1181,7 +1381,28 @@ export const LOCK_BLOCK_BRIEF = {
|
|
|
1181
1381
|
get unavailable() { return utilsMsg('utils.envLock.brief.unavailable'); },
|
|
1182
1382
|
// stale 必须点出「残留」——否则用户会像 issue #31 那样等 9 天(57 次静默跳过)。
|
|
1183
1383
|
get stale() { return utilsMsg('utils.envLock.brief.stale'); },
|
|
1384
|
+
get 'stale-degraded'() { return utilsMsg('utils.envLock.brief.staleDegraded'); },
|
|
1184
1385
|
};
|
|
1386
|
+
/**
|
|
1387
|
+
* 锁分类 → 落进结果对象/历史记录的**机器token**(客户端据此本地化;不许再把所有被挡拍平成一个词)。
|
|
1388
|
+
*
|
|
1389
|
+
* issue:真机现场 SAFE MODE 被拒时 locks 目录是空的,界面却说「环境锁被占用」——
|
|
1390
|
+
* 因为宿主所有被挡路径都写死 'mutation-locked',客户端只能照最坏情况解释,排查方向被带偏。
|
|
1391
|
+
* 现在按分类发 token:**只有真被另一项活任务占着(LOCKED)才叫 locked**。
|
|
1392
|
+
* - 'mutation-safe-mode':被 SAFE MODE 阻断(锁目录通常为空,处置入口是事故恢复)
|
|
1393
|
+
* - 'mutation-lock-stale':残留锁(含心跳降级形态)——重试/重启都不会自愈,必须显式回收
|
|
1394
|
+
* - 'mutation-lock-unavailable':锁不可用(目录 IO/权限/未知,或 acquire 直接抛错)
|
|
1395
|
+
* 旧 token 'mutation-locked' 仍表示「被活跃任务持有」,客户端必须继续认它。
|
|
1396
|
+
*/
|
|
1397
|
+
export function lockSkipToken(reason) {
|
|
1398
|
+
switch (reason) {
|
|
1399
|
+
case 'blocked': return 'mutation-safe-mode';
|
|
1400
|
+
case 'locked': return 'mutation-locked';
|
|
1401
|
+
case 'stale':
|
|
1402
|
+
case 'stale-degraded': return 'mutation-lock-stale';
|
|
1403
|
+
default: return 'mutation-lock-unavailable';
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1185
1406
|
/** destructive 必须成功获取 Environment Lock;否则抛此错(被另一进程/操作持有,或锁不可用)。
|
|
1186
1407
|
* 携带 op 与 reason 供内部日志诊断;.message 恒为用户可读的友好文案(不暴露锁/op/路径)。 */
|
|
1187
1408
|
export class EnvironmentLockUnavailableError extends Error {
|
|
@@ -51,6 +51,7 @@ export declare const zh: {
|
|
|
51
51
|
'utils.envLock.heartbeatExpiredNoIdentity': string;
|
|
52
52
|
'utils.envLock.pidReuse': string;
|
|
53
53
|
'utils.envLock.ownerAliveDegraded': string;
|
|
54
|
+
'utils.envLock.ownerAliveHeartbeatStopped': string;
|
|
54
55
|
'utils.envLock.notStaleRefuseRecovery': string;
|
|
55
56
|
'utils.envLock.ownershipGone': string;
|
|
56
57
|
'utils.envLock.captureRenameFailed': string;
|
|
@@ -69,14 +70,19 @@ export declare const zh: {
|
|
|
69
70
|
'utils.envLock.durationMinutes': string;
|
|
70
71
|
'utils.envLock.durationSeconds': string;
|
|
71
72
|
'utils.envLock.ioPermissionNotLock': string;
|
|
73
|
+
'utils.envLock.heartbeatWriteFailed': string;
|
|
74
|
+
'utils.envLock.releaseFailedQuarantined': string;
|
|
75
|
+
'utils.envLock.releaseFailedStuck': string;
|
|
72
76
|
'utils.envLock.block.locked': string;
|
|
73
77
|
'utils.envLock.block.blocked': string;
|
|
74
78
|
'utils.envLock.block.unavailable': string;
|
|
75
79
|
'utils.envLock.block.stale': string;
|
|
80
|
+
'utils.envLock.block.staleDegraded': string;
|
|
76
81
|
'utils.envLock.brief.locked': string;
|
|
77
82
|
'utils.envLock.brief.blocked': string;
|
|
78
83
|
'utils.envLock.brief.unavailable': string;
|
|
79
84
|
'utils.envLock.brief.stale': string;
|
|
85
|
+
'utils.envLock.brief.staleDegraded': string;
|
|
80
86
|
'utils.proxy.connectTimeout': string;
|
|
81
87
|
'utils.proxy.malformedConnectResponse': string;
|
|
82
88
|
'utils.proxy.refusedConnect': string;
|