pi-wechat-session-hub 0.1.0 → 0.1.2
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/CHANGELOG.md +95 -0
- package/README.md +420 -365
- package/docs/handoff-next-session.md +4 -4
- package/docs/implementation-spec.md +150 -147
- package/extensions/daemon-entry.mjs +31 -0
- package/extensions/wechat-session-hub.ts +12 -12
- package/package.json +72 -67
- package/scripts/probe-wt-capability.mjs +26 -5
- package/scripts/release-preflight.mjs +229 -0
- package/src/gateway/command-parser.ts +25 -9
- package/src/gateway/command-router.ts +1790 -1449
- package/src/gateway/durable-message-queue.ts +1154 -1043
- package/src/gateway/gateway-control-ipc.ts +705 -687
- package/src/gateway/turn-health.ts +305 -0
- package/src/gateway/ui-worker-extension-bridge.ts +1092 -1070
- package/src/gateway/ui-worker-ipc.ts +1517 -1483
- package/src/gateway/wechat-ilink/api-client.ts +386 -390
- package/src/runtime/gateway-lifecycle-operation.ts +67 -2
- package/src/runtime/gateway-service-controller.ts +341 -339
- package/src/runtime/ui-worker-manager.ts +1216 -1186
- package/src/runtime/windows-logon-autostart-manager.ts +401 -0
- package/src/runtime/windows-logon-task-manager.ts +0 -212
|
@@ -34,6 +34,9 @@ export async function watchGatewayLifecycleOperation(
|
|
|
34
34
|
}
|
|
35
35
|
const operationDirectory = await ensureOperationDirectory(managerDirectory);
|
|
36
36
|
const receiptPath = join(operationDirectory, `${operationId}.json`);
|
|
37
|
+
// persistent: false 让监听器不单独持有事件循环。等待期间的存活由 wait()
|
|
38
|
+
// 安装的非 unref 超时定时器保证, 两端平台行为一致; close() 同时清理
|
|
39
|
+
// 定时器与监听器, 因此不会让测试或 CLI 长期挂住进程。
|
|
37
40
|
const watcher = watch(operationDirectory, { persistent: false });
|
|
38
41
|
let cachedReceipt: GatewayLifecycleOperationReceipt | undefined;
|
|
39
42
|
let readError: Error | undefined;
|
|
@@ -113,7 +116,9 @@ export async function watchGatewayLifecycleOperation(
|
|
|
113
116
|
clearWait();
|
|
114
117
|
reject?.(new Error("等待 Gateway 重启完成超时。"));
|
|
115
118
|
}, timeoutMs);
|
|
116
|
-
|
|
119
|
+
// 超时定时器不调用 unref(): 等待期间的存活由 close() 控制,
|
|
120
|
+
// 由监听器与定时器共同保证 Node 不会在 Promise 决之前退出。
|
|
121
|
+
// 调用方结束时会主动 close(), 因此不会长期占用事件循环。
|
|
117
122
|
void readReceipt();
|
|
118
123
|
});
|
|
119
124
|
},
|
|
@@ -159,13 +164,73 @@ export async function writeGatewayLifecycleOperationReceipt(
|
|
|
159
164
|
mode: 0o600,
|
|
160
165
|
});
|
|
161
166
|
try {
|
|
162
|
-
await
|
|
167
|
+
await replaceWithRetry(temporaryPath, receiptPath);
|
|
163
168
|
} catch (error) {
|
|
164
169
|
await unlink(temporaryPath).catch(() => undefined);
|
|
170
|
+
if (isWindowsRenameConflict(error)) {
|
|
171
|
+
throw new Error(
|
|
172
|
+
"写入 Gateway 生命周期回执失败: 目标文件持续被其他进程占用, 已重试仍无法替换。",
|
|
173
|
+
{ cause: error },
|
|
174
|
+
);
|
|
175
|
+
}
|
|
165
176
|
throw error;
|
|
166
177
|
}
|
|
167
178
|
}
|
|
168
179
|
|
|
180
|
+
/**
|
|
181
|
+
* 原子替换目标文件, Windows 占用类错误按退避重试。
|
|
182
|
+
*
|
|
183
|
+
* Windows 上杀毒软件与索引器会短暂持有文件句柄, 使成功落盘的临时文件
|
|
184
|
+
* 在替换阶段瞬时失败. 重试次数少且间隔短, 不改变调用方的超时预期。
|
|
185
|
+
* 采用递归推进而非循环内等待, 避免在循环体内阻塞。
|
|
186
|
+
*
|
|
187
|
+
* 退避定时器不调用 `unref()`: 替换完成前进程必须保持存活,
|
|
188
|
+
* 否则调用方可能在回执落盘前失去事件循环。单个退避最长 200ms, 影响可忽略。
|
|
189
|
+
*
|
|
190
|
+
* @param temporaryPath 已写入的临时文件路径。
|
|
191
|
+
* @param targetPath 目标回执文件路径。
|
|
192
|
+
* @param attempt 当前重试序号, 从 0 开始。
|
|
193
|
+
* @returns 替换完成后正常结束。
|
|
194
|
+
* @throws {unknown} 非占用类错误或重试耗尽时抛出最后一次错误。
|
|
195
|
+
*/
|
|
196
|
+
async function replaceWithRetry(
|
|
197
|
+
temporaryPath: string,
|
|
198
|
+
targetPath: string,
|
|
199
|
+
attempt = 0,
|
|
200
|
+
): Promise<void> {
|
|
201
|
+
const retryDelaysMs = [10, 25, 50, 100, 200];
|
|
202
|
+
try {
|
|
203
|
+
await rename(temporaryPath, targetPath);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
const delay = retryDelaysMs[attempt];
|
|
206
|
+
if (!isWindowsRenameConflict(error) || delay === undefined) {
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
await new Promise<void>((resolveDelay) => {
|
|
210
|
+
setTimeout(resolveDelay, delay);
|
|
211
|
+
});
|
|
212
|
+
return replaceWithRetry(temporaryPath, targetPath, attempt + 1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 判断是否为 Windows 上常见的原子替换失败。
|
|
218
|
+
*
|
|
219
|
+
* Windows 不允许替换被其他进程以不共享方式打开的文件, 杀毒软件、
|
|
220
|
+
* Windows Search 索引器等会短暂持有句柄, 使 `rename` 返回 EPERM/EACCES/EBUSY。
|
|
221
|
+
* 这些错误是可重试的瞬时错误, 与权限配置错误不同。
|
|
222
|
+
*
|
|
223
|
+
* @param error 捕获的错误。
|
|
224
|
+
* @returns 属可重试的 Windows 占用类错误时返回 true。
|
|
225
|
+
*/
|
|
226
|
+
function isWindowsRenameConflict(error: unknown): boolean {
|
|
227
|
+
if (process.platform !== "win32") {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
const code = (error as NodeJS.ErrnoException | undefined)?.code;
|
|
231
|
+
return code === "EPERM" || code === "EACCES" || code === "EBUSY";
|
|
232
|
+
}
|
|
233
|
+
|
|
169
234
|
/**
|
|
170
235
|
* Gateway 启动就绪后完成上次 daemon 重启遗留的 pending 操作.
|
|
171
236
|
*
|