chatccc 0.2.280 → 0.2.281
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 +2 -0
- package/dist/src/index.js +26 -8
- package/dist/src/orchestrator.js +127 -60
- package/dist/src/shared.js +34 -17
- package/dist/src/startup-lifecycle.js +43 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -446,6 +446,8 @@ Codex 的默认模型和推理强度可继续由 `~/.codex/config.toml` 管理
|
|
|
446
446
|
`/update`、`/update safe` 与短别名 `/updatesf` 会把飞书消息或按钮事件 ID 原子写入 `~/.chatccc/state/update-command-guard.json`。同一 ID 跨重启重投时会静默忽略;用户主动发送的新更新指令因事件 ID 不同,仍可执行。该保护不改变普通消息与重启指令的处理方式。
|
|
447
447
|
|
|
448
448
|
`/restart safe`(短别名 `/restartsf`)与 `/update safe`(短别名 `/updatesf`)会先建立全局准入门禁:指令到达前已经运行或进入单会话缓存队列的工作会继续完成,之后到达的新普通任务会被提示在维护完成后重发。维护任务持久化到 `~/.chatccc/state/safe-maintenance.json`,进程意外退出后可继续排空;依赖安装、会话收尾、自动恢复和 Agent Teams 执行也计入等待条件。内存缓存随重启自然重建,磁盘会话、看板、图片等持久数据不会被清理。
|
|
449
|
+
|
|
450
|
+
ChatCCC 的内部重启和更新使用跨平台父子进程握手:替代进程完成启动预检后通知父进程退出,再等待旧监听端口实际释放并接管 PID;替代进程未就绪或握手超时时,父进程会保留并继续服务。
|
|
449
451
|
|
|
450
452
|
> **模型切换**:`/model` 查看当前会话 Agent 的可选模型清单,`/model <名称>` 模糊匹配切换,`/model clear` 恢复默认。可选模型来自当前 Agent 的配置:Claude 使用 `claude.model` / `claude.subagentModel`;Cursor、Codex、CCC Agent 和 DSH 使用各自的 `model` / `alternativeModel`。
|
|
451
453
|
|
package/dist/src/index.js
CHANGED
|
@@ -27,7 +27,7 @@ import WebSocket from "ws";
|
|
|
27
27
|
import { appendStartupTrace, attachRelayWebSocket, ensureSingleInstance, freeRelayListenPort, installCrashLogging, installEpipeGuard, waitForPortFree } from "./shared.js";
|
|
28
28
|
import { createUiRouter, setExtraApiHandler, setReloadConfigHook, startSetupMode } from "./web-ui.js";
|
|
29
29
|
import { configureAgentTeamMainAgent } from "./agent-team/main-agent-bootstrap.js";
|
|
30
|
-
import { buildWebUiUrl, createServiceLifecycleGuard, INTERNAL_RESTART_ENV_VAR, openWebUiInDefaultBrowser, shouldAutoOpenWebUi, } from "./startup-lifecycle.js";
|
|
30
|
+
import { buildWebUiUrl, createServiceLifecycleGuard, announceInternalRestartReady, INTERNAL_RESTART_ENV_VAR, openWebUiInDefaultBrowser, shouldAutoOpenWebUi, } from "./startup-lifecycle.js";
|
|
31
31
|
import { buildPlatformStartupPlan } from "./platform-startup.js";
|
|
32
32
|
import { makeTraceId, logTrace } from "./trace.js";
|
|
33
33
|
import { CHATCCC_PORT, config, APP_ID, APP_SECRET, FEISHU_ENABLED, FEISHU_PLATFORM_TYPE, ILINK_ENABLED, ILINK_REUSE_TOKEN_ON_START, BASE_URL, LOCAL_RELAY_URL, PID_FILE, PROJECT_ROOT, USE_LOCAL, USE_SIMULATE, appendChatLog, fileLog, reportEnvironmentVariableReadout, maskAppId, resolveDefaultAgentTool, toolDisplayName, ts, } from "./config.js";
|
|
@@ -609,6 +609,23 @@ async function main() {
|
|
|
609
609
|
printServiceDidNotStart("config.json 的 port 字段配置无效(须为 1–65535 的整数)");
|
|
610
610
|
process.exit(1);
|
|
611
611
|
}
|
|
612
|
+
const isInternalRestart = process.env[INTERNAL_RESTART_ENV_VAR] === "1";
|
|
613
|
+
if (isInternalRestart) {
|
|
614
|
+
const announced = await announceInternalRestartReady();
|
|
615
|
+
appendStartupTrace("restart-handoff: replacement preflight ready", {
|
|
616
|
+
announced,
|
|
617
|
+
port: CHATCCC_PORT,
|
|
618
|
+
});
|
|
619
|
+
const portReleased = await waitForPortFree(CHATCCC_PORT, 30_000);
|
|
620
|
+
appendStartupTrace("restart-handoff: parent port wait finished", {
|
|
621
|
+
port: CHATCCC_PORT,
|
|
622
|
+
portReleased,
|
|
623
|
+
});
|
|
624
|
+
if (!portReleased) {
|
|
625
|
+
printServiceDidNotStart(`内部重启交接超时:端口 ${CHATCCC_PORT} 在 30 秒内未释放`);
|
|
626
|
+
process.exit(1);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
612
629
|
console.log(`\n[启动 1/7] 单实例:按 PID 文件清理旧 ChatCCC 进程`);
|
|
613
630
|
console.log(` PID 文件: ${PID_FILE}`);
|
|
614
631
|
appendStartupTrace("main: before ensureSingleInstance", { PID_FILE, CHATCCC_PORT });
|
|
@@ -680,14 +697,15 @@ async function main() {
|
|
|
680
697
|
// 启动 HTTP server(同时挂 UI router,供 dashboard / setup / agent image/file 使用)
|
|
681
698
|
appendStartupTrace("main: before freeRelayListenPort", { CHATCCC_PORT });
|
|
682
699
|
const killed = freeRelayListenPort(CHATCCC_PORT);
|
|
683
|
-
const isInternalRestart = process.env[INTERNAL_RESTART_ENV_VAR] === "1";
|
|
684
700
|
appendStartupTrace("main: after freeRelayListenPort", { CHATCCC_PORT, killed, isInternalRestart });
|
|
685
|
-
//
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
701
|
+
// 普通启动清理过旧实例后,也必须确认端口真正释放再继续监听。
|
|
702
|
+
if (killed > 0) {
|
|
703
|
+
const portReleased = await waitForPortFree(CHATCCC_PORT, 5_000);
|
|
704
|
+
appendStartupTrace("main: post-kill port wait finished", { CHATCCC_PORT, portReleased });
|
|
705
|
+
if (!portReleased) {
|
|
706
|
+
printServiceDidNotStart(`旧进程退出后端口 ${CHATCCC_PORT} 在 5 秒内仍未释放`);
|
|
707
|
+
process.exit(1);
|
|
708
|
+
}
|
|
691
709
|
}
|
|
692
710
|
const httpServer = createServer(createUiRouter());
|
|
693
711
|
await listenWithRetry(httpServer, CHATCCC_PORT, "127.0.0.1").catch((err) => {
|
package/dist/src/orchestrator.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { execSync, spawn } from "node:child_process";
|
|
8
8
|
import { readdir, stat } from "node:fs/promises";
|
|
9
|
-
import { appendFileSync, closeSync,
|
|
9
|
+
import { appendFileSync, closeSync, mkdirSync, openSync } from "node:fs";
|
|
10
10
|
import { join, resolve, dirname } from "node:path";
|
|
11
11
|
import { homedir } from "node:os";
|
|
12
12
|
import { config as deepCccConfig } from "../deepccc-agent/src/config.js";
|
|
@@ -25,7 +25,7 @@ import { applySharedPrefix } from "./shared-prefix.js";
|
|
|
25
25
|
import { normalizeSessionDisplayTitle, sessionChatName, sessionDisplayTitleFromPrompt, } from "./session-name.js";
|
|
26
26
|
import { reloadRuntimeConfig } from "./runtime-reload.js";
|
|
27
27
|
import { acquireUpdateCommandGuard } from "./update-command-guard.js";
|
|
28
|
-
import { createInternalRestartEnv, INTERNAL_RESTART_ENV_VAR } from "./startup-lifecycle.js";
|
|
28
|
+
import { createInternalRestartEnv, INTERNAL_RESTART_ENV_VAR, INTERNAL_RESTART_READY_MESSAGE, } from "./startup-lifecycle.js";
|
|
29
29
|
import { resolveChatCccRuntimeSpawnSpec } from "./runtime-entry.js";
|
|
30
30
|
import { engineManager } from "./engines/engine-specs.js";
|
|
31
31
|
import { beginSafeMaintenanceTrackedWork, isSafeMaintenanceAdmissionClosed, safeMaintenanceCoordinator, } from "./safe-maintenance.js";
|
|
@@ -678,41 +678,18 @@ function syncUpdateAndRestart(options = {}) {
|
|
|
678
678
|
appendStartupTrace("update: safe update aborted before restart", {});
|
|
679
679
|
return undefined;
|
|
680
680
|
}
|
|
681
|
-
// 2.
|
|
682
|
-
|
|
683
|
-
const
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
appendStartupTrace("update: spawn begin", { npmPrefix: npmPrefix || "(empty)", binPath });
|
|
687
|
-
// 3. spawn new chatccc:优先 node + 全局包入口绝对路径(不依赖 PATH/shell),
|
|
688
|
-
// 避免继承环境 PATH 异常时秒退;失败时回退到 binPath(走 shell)。
|
|
681
|
+
// 2. Spawn the updated runtime directly through Node. Reusing the restart
|
|
682
|
+
// launcher guarantees an IPC handoff channel and avoids shell/PATH variance.
|
|
683
|
+
const spawnSpec = buildRestartSpawnSpec(PROJECT_ROOT);
|
|
684
|
+
updLog(`runtime path: ${spawnSpec.command} ${spawnSpec.args.join(" ")}`);
|
|
685
|
+
appendStartupTrace("update: spawn begin", { runtimeEntry: spawnSpec.args[0] });
|
|
689
686
|
try {
|
|
690
|
-
|
|
691
|
-
if (npmPrefix) {
|
|
692
|
-
const entry = join(npmPrefix, "node_modules", "chatccc", "bin", "chatccc.mjs");
|
|
693
|
-
if (existsSync(entry)) {
|
|
694
|
-
spawnSpec = { command: process.execPath, args: [entry] };
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
const child = spawnSpec
|
|
698
|
-
? spawn(spawnSpec.command, spawnSpec.args, {
|
|
699
|
-
detached: true,
|
|
700
|
-
stdio: "ignore",
|
|
701
|
-
shell: false,
|
|
702
|
-
env: createInternalRestartEnv(),
|
|
703
|
-
})
|
|
704
|
-
: spawn(binPath, [], {
|
|
705
|
-
detached: true,
|
|
706
|
-
stdio: "ignore",
|
|
707
|
-
shell: true,
|
|
708
|
-
env: createInternalRestartEnv(),
|
|
709
|
-
});
|
|
687
|
+
const child = spawnRestartChild({ projectRoot: PROJECT_ROOT });
|
|
710
688
|
child.unref();
|
|
711
|
-
|
|
712
|
-
updLog(`spawn new chatccc OK, childPid=${child.pid}, bin=${spawnedAs}`);
|
|
689
|
+
updLog(`spawn new chatccc OK, childPid=${child.pid}, bin=${spawnSpec.command} ${spawnSpec.args.join(" ")}`);
|
|
713
690
|
appendStartupTrace("update: spawn OK", {
|
|
714
691
|
childPid: child.pid,
|
|
715
|
-
binPath: spawnSpec
|
|
692
|
+
binPath: spawnSpec.args[0],
|
|
716
693
|
});
|
|
717
694
|
return child;
|
|
718
695
|
}
|
|
@@ -726,8 +703,8 @@ function syncUpdateAndRestart(options = {}) {
|
|
|
726
703
|
// ---------------------------------------------------------------------------
|
|
727
704
|
// /restart — 自重启子进程(不经过 npx/npm,避免 PATH 注入秒退;防空窗兜底)
|
|
728
705
|
// ---------------------------------------------------------------------------
|
|
729
|
-
/**
|
|
730
|
-
export const RESTART_CHILD_READY_MS =
|
|
706
|
+
/** 父进程等待替代进程通过 IPC 完成启动预检的最长时间(毫秒)。 */
|
|
707
|
+
export const RESTART_CHILD_READY_MS = 15_000;
|
|
731
708
|
/**
|
|
732
709
|
* 构建自重启的 spawn 参数:发布包直接运行编译后的 JavaScript;只有尚未
|
|
733
710
|
* build 的开发工作区才使用本地 tsx CLI。两种情况都不经过 npx/npm。
|
|
@@ -735,12 +712,64 @@ export const RESTART_CHILD_READY_MS = 3000;
|
|
|
735
712
|
export function buildRestartSpawnSpec(projectRoot = PROJECT_ROOT) {
|
|
736
713
|
return resolveChatCccRuntimeSpawnSpec(projectRoot);
|
|
737
714
|
}
|
|
715
|
+
const restartHandoffMonitors = new WeakMap();
|
|
716
|
+
function getOrCreateRestartHandoffMonitor(child) {
|
|
717
|
+
const existing = restartHandoffMonitors.get(child);
|
|
718
|
+
if (existing)
|
|
719
|
+
return existing;
|
|
720
|
+
if (typeof child.on !== "function") {
|
|
721
|
+
const unavailable = { promise: Promise.resolve({ kind: "no_ipc" }), cancel() { } };
|
|
722
|
+
restartHandoffMonitors.set(child, unavailable);
|
|
723
|
+
return unavailable;
|
|
724
|
+
}
|
|
725
|
+
const addListener = child.on.bind(child);
|
|
726
|
+
const removeListener = typeof child.off === "function"
|
|
727
|
+
? child.off.bind(child)
|
|
728
|
+
: undefined;
|
|
729
|
+
let resolveOutcome = () => { };
|
|
730
|
+
let settled = false;
|
|
731
|
+
const cleanup = () => {
|
|
732
|
+
removeListener?.("message", onMessage);
|
|
733
|
+
removeListener?.("exit", onExit);
|
|
734
|
+
removeListener?.("error", onError);
|
|
735
|
+
};
|
|
736
|
+
const settle = (outcome) => {
|
|
737
|
+
if (settled)
|
|
738
|
+
return;
|
|
739
|
+
settled = true;
|
|
740
|
+
cleanup();
|
|
741
|
+
resolveOutcome(outcome);
|
|
742
|
+
};
|
|
743
|
+
const onMessage = (message) => {
|
|
744
|
+
const ready = message;
|
|
745
|
+
if (!ready || ready.type !== INTERNAL_RESTART_READY_MESSAGE)
|
|
746
|
+
return;
|
|
747
|
+
if (typeof ready.pid === "number" && child.pid !== undefined && ready.pid !== child.pid)
|
|
748
|
+
return;
|
|
749
|
+
if (ready.parentPid !== process.pid)
|
|
750
|
+
return;
|
|
751
|
+
settle({ kind: "ready" });
|
|
752
|
+
};
|
|
753
|
+
const onExit = () => settle({ kind: "exit" });
|
|
754
|
+
const onError = (error) => settle({ kind: "error", error });
|
|
755
|
+
const promise = new Promise((resolve) => {
|
|
756
|
+
resolveOutcome = resolve;
|
|
757
|
+
});
|
|
758
|
+
const monitor = { promise, cancel: cleanup };
|
|
759
|
+
restartHandoffMonitors.set(child, monitor);
|
|
760
|
+
addListener("message", onMessage);
|
|
761
|
+
addListener("exit", onExit);
|
|
762
|
+
addListener("error", onError);
|
|
763
|
+
if (child.exitCode !== null || child.signalCode !== null)
|
|
764
|
+
settle({ kind: "exit" });
|
|
765
|
+
return monitor;
|
|
766
|
+
}
|
|
738
767
|
/**
|
|
739
768
|
* spawn 自重启子进程。
|
|
740
769
|
*
|
|
741
770
|
* stdout/stderr 按启动方式分流:
|
|
742
771
|
* - **终端(TTY)场景**(用户从 cmd/PowerShell/node.exe 窗口启动):stdio 用
|
|
743
|
-
*
|
|
772
|
+
* 前三个 stdio 直接继承终端句柄,第四个通道保留给 IPC 握手;restart 后窗口日志不中断。
|
|
744
773
|
* 终端句柄的生命周期不随父进程退出而关闭,因此不存在 EPIPE 风险。
|
|
745
774
|
* - **非终端场景**(守护进程/黑匣子等管道或文件启动):stderr 重定向到磁盘日志
|
|
746
775
|
* 文件(restart-*.log),子进程继承文件句柄,父进程退出不影响写入。
|
|
@@ -756,7 +785,7 @@ export function spawnRestartChild(deps = {}) {
|
|
|
756
785
|
const isTty = deps.isTty ?? (() => process.stdout.isTTY === true || process.stderr.isTTY === true);
|
|
757
786
|
const { command, args } = buildRestartSpawnSpec(projectRoot);
|
|
758
787
|
let stderrFd;
|
|
759
|
-
const stdio = ["ignore", "ignore", "pipe"];
|
|
788
|
+
const stdio = ["ignore", "ignore", "pipe", "ipc"];
|
|
760
789
|
const tty = isTty();
|
|
761
790
|
if (tty) {
|
|
762
791
|
// 终端场景:全部 inherit(含 stdin)。注意 stdin 不能是 "ignore":
|
|
@@ -793,8 +822,11 @@ export function spawnRestartChild(deps = {}) {
|
|
|
793
822
|
detached: true,
|
|
794
823
|
stdio,
|
|
795
824
|
shell: false,
|
|
796
|
-
env: createInternalRestartEnv(),
|
|
825
|
+
env: createInternalRestartEnv(process.env, process.pid),
|
|
797
826
|
});
|
|
827
|
+
// Attach before returning so an extremely fast replacement cannot emit its
|
|
828
|
+
// IPC readiness message between spawnRestartChild() and the caller's wait.
|
|
829
|
+
getOrCreateRestartHandoffMonitor(child);
|
|
798
830
|
// 子进程已继承 stderr 文件句柄;父进程关闭自己的副本,避免"子进程早退、
|
|
799
831
|
// 父进程留下继续服务"时 fd 泄漏。
|
|
800
832
|
if (stderrFd !== undefined) {
|
|
@@ -817,24 +849,61 @@ export function spawnRestartChild(deps = {}) {
|
|
|
817
849
|
}
|
|
818
850
|
/**
|
|
819
851
|
* 决定父进程是否应退出(防空窗兜底):
|
|
820
|
-
* -
|
|
821
|
-
* -
|
|
852
|
+
* - 替代进程通过 IPC 明确完成预检 → 返回 true,父进程退出并交出端口;
|
|
853
|
+
* - 替代进程退出、报错或握手超时 → 返回 false,父进程继续服务。
|
|
822
854
|
*/
|
|
823
|
-
export async function decideRestartParentExit(child, timeoutMs,
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
855
|
+
export async function decideRestartParentExit(child, timeoutMs, _pollMs = 500, trace = appendStartupTrace) {
|
|
856
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
857
|
+
trace("restart: child died during window, keeping parent", {
|
|
858
|
+
childPid: child.pid,
|
|
859
|
+
exitCode: child.exitCode,
|
|
860
|
+
signalCode: child.signalCode,
|
|
861
|
+
});
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
const monitor = getOrCreateRestartHandoffMonitor(child);
|
|
865
|
+
let timeout;
|
|
866
|
+
const timeoutOutcome = new Promise((resolve) => {
|
|
867
|
+
timeout = setTimeout(() => resolve({ kind: "no_ipc" }), timeoutMs);
|
|
868
|
+
timeout.unref?.();
|
|
869
|
+
});
|
|
870
|
+
const outcome = await Promise.race([monitor.promise, timeoutOutcome]);
|
|
871
|
+
if (timeout)
|
|
872
|
+
clearTimeout(timeout);
|
|
873
|
+
monitor.cancel();
|
|
874
|
+
if (outcome.kind === "ready") {
|
|
875
|
+
if (child.connected && typeof child.disconnect === "function") {
|
|
876
|
+
try {
|
|
877
|
+
child.disconnect();
|
|
878
|
+
}
|
|
879
|
+
catch { /* child may disconnect first */ }
|
|
880
|
+
}
|
|
881
|
+
trace("restart: child handoff ready, parent exiting", { childPid: child.pid });
|
|
882
|
+
return true;
|
|
883
|
+
}
|
|
884
|
+
if (outcome.kind === "exit") {
|
|
885
|
+
trace("restart: child died during window, keeping parent", {
|
|
886
|
+
childPid: child.pid,
|
|
887
|
+
exitCode: child.exitCode,
|
|
888
|
+
signalCode: child.signalCode,
|
|
889
|
+
});
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
if (outcome.kind === "error") {
|
|
893
|
+
trace("restart: child handoff error, keeping parent", {
|
|
894
|
+
childPid: child.pid,
|
|
895
|
+
error: outcome.error.message,
|
|
896
|
+
});
|
|
897
|
+
return false;
|
|
898
|
+
}
|
|
899
|
+
if (typeof child.kill === "function") {
|
|
900
|
+
try {
|
|
901
|
+
child.kill();
|
|
833
902
|
}
|
|
834
|
-
|
|
903
|
+
catch { /* best effort */ }
|
|
835
904
|
}
|
|
836
|
-
trace("restart: child
|
|
837
|
-
return
|
|
905
|
+
trace("restart: child handoff timeout, keeping parent", { childPid: child.pid });
|
|
906
|
+
return false;
|
|
838
907
|
}
|
|
839
908
|
const safeMaintenancePlatforms = new Map();
|
|
840
909
|
export function configureSafeMaintenanceRuntime(platforms) {
|
|
@@ -1051,8 +1120,8 @@ async function handleCommandInternal(platform, text, chatId, openId, msgTimestam
|
|
|
1051
1120
|
appendStartupTrace("restart: spawn begin", { fromPid: process.pid });
|
|
1052
1121
|
const child = spawnRestartChild();
|
|
1053
1122
|
child.unref();
|
|
1054
|
-
//
|
|
1055
|
-
//
|
|
1123
|
+
// 只有替代进程通过 IPC 明确完成预检才退出父进程;超时或早退时父进程
|
|
1124
|
+
// 继续服务,并保留替代进程 stderr 日志供排查。
|
|
1056
1125
|
void decideRestartParentExit(child, RESTART_CHILD_READY_MS).then((shouldExit) => {
|
|
1057
1126
|
if (!shouldExit)
|
|
1058
1127
|
return;
|
|
@@ -1100,10 +1169,9 @@ async function handleCommandInternal(platform, text, chatId, openId, msgTimestam
|
|
|
1100
1169
|
await platform.sendText(chatId, "正在更新并重启,请稍候...").catch(() => { });
|
|
1101
1170
|
logTrace(tid, "DONE", { outcome: "update" });
|
|
1102
1171
|
appendStartupTrace("update: sync update begin", { fromPid: process.pid });
|
|
1103
|
-
const child = syncUpdateAndRestart();
|
|
1172
|
+
const child = syncUpdateAndRestart({ spawnOnUpdateFailure: false });
|
|
1104
1173
|
if (child) {
|
|
1105
|
-
//
|
|
1106
|
-
// 服务(防空窗)。
|
|
1174
|
+
// 只有替代进程通过 IPC 明确完成预检才退出父进程;否则父进程继续服务。
|
|
1107
1175
|
void decideRestartParentExit(child, RESTART_CHILD_READY_MS).then((shouldExit) => {
|
|
1108
1176
|
if (!shouldExit)
|
|
1109
1177
|
return;
|
|
@@ -1112,8 +1180,7 @@ async function handleCommandInternal(platform, text, chatId, openId, msgTimestam
|
|
|
1112
1180
|
});
|
|
1113
1181
|
}
|
|
1114
1182
|
else {
|
|
1115
|
-
|
|
1116
|
-
setTimeout(() => process.exit(0), 2000);
|
|
1183
|
+
appendStartupTrace("update: replacement unavailable, parent stays alive", {});
|
|
1117
1184
|
}
|
|
1118
1185
|
return;
|
|
1119
1186
|
}
|
package/dist/src/shared.js
CHANGED
|
@@ -164,25 +164,42 @@ export function freeRelayListenPort(port) {
|
|
|
164
164
|
return killed;
|
|
165
165
|
}
|
|
166
166
|
/**
|
|
167
|
-
*
|
|
168
|
-
*
|
|
167
|
+
* 跨平台轮询等待端口释放。既用于 Windows taskkill 后的延迟释放,也用于
|
|
168
|
+
* Linux/macOS 内部重启时等待父进程交出监听端口。
|
|
169
169
|
*/
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const pids = collectListeningPidsOnPortWindows(port, out);
|
|
178
|
-
if (pids.length === 0)
|
|
170
|
+
async function canBindLoopbackPort(port) {
|
|
171
|
+
const probe = createServer();
|
|
172
|
+
probe.unref();
|
|
173
|
+
return new Promise((resolve) => {
|
|
174
|
+
let settled = false;
|
|
175
|
+
const finish = (available) => {
|
|
176
|
+
if (settled)
|
|
179
177
|
return;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
178
|
+
settled = true;
|
|
179
|
+
probe.removeAllListeners();
|
|
180
|
+
if (probe.listening) {
|
|
181
|
+
probe.close(() => resolve(available));
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
resolve(available);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
probe.once("error", () => finish(false));
|
|
188
|
+
probe.once("listening", () => finish(true));
|
|
189
|
+
probe.listen(port, "127.0.0.1");
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/** Wait until the loopback port can actually be bound on every supported OS. */
|
|
193
|
+
export async function waitForPortFree(port, timeoutMs = 3000, pollMs = 200) {
|
|
194
|
+
const deadline = Date.now() + timeoutMs;
|
|
195
|
+
do {
|
|
196
|
+
if (await canBindLoopbackPort(port))
|
|
197
|
+
return true;
|
|
198
|
+
if (Date.now() >= deadline)
|
|
199
|
+
break;
|
|
200
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
201
|
+
} while (Date.now() <= deadline);
|
|
202
|
+
return false;
|
|
186
203
|
}
|
|
187
204
|
// ---------------------------------------------------------------------------
|
|
188
205
|
// 单实例保证:PID 文件互斥(端口占用在 freeRelayListenPort + 监听前处理)
|
|
@@ -147,12 +147,54 @@ export function createServiceLifecycleGuard(options = {}) {
|
|
|
147
147
|
* 会自然穿过 cmd/bash/npx 这几层启动器,因此也适用于 Windows 与 Linux。
|
|
148
148
|
*/
|
|
149
149
|
export const INTERNAL_RESTART_ENV_VAR = "CHATCCC_INTERNAL_RESTART";
|
|
150
|
-
export
|
|
150
|
+
export const INTERNAL_RESTART_PARENT_PID_ENV_VAR = "CHATCCC_RESTART_PARENT_PID";
|
|
151
|
+
export const INTERNAL_RESTART_READY_MESSAGE = "chatccc:restart-handoff-ready";
|
|
152
|
+
export function createInternalRestartEnv(inherited = process.env, parentPid = process.pid) {
|
|
151
153
|
return {
|
|
152
154
|
...inherited,
|
|
153
155
|
[INTERNAL_RESTART_ENV_VAR]: "1",
|
|
156
|
+
[INTERNAL_RESTART_PARENT_PID_ENV_VAR]: String(parentPid),
|
|
154
157
|
};
|
|
155
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Tell the current parent that the replacement runtime loaded successfully and
|
|
161
|
+
* is ready to wait for the listening port. Older parents do not provide IPC;
|
|
162
|
+
* returning false preserves the port-wait fallback used during an upgrade from
|
|
163
|
+
* a pre-handoff ChatCCC version.
|
|
164
|
+
*/
|
|
165
|
+
export async function announceInternalRestartReady(options = {}) {
|
|
166
|
+
const env = options.env ?? process.env;
|
|
167
|
+
if (env[INTERNAL_RESTART_ENV_VAR] !== "1")
|
|
168
|
+
return false;
|
|
169
|
+
const send = options.send ?? (typeof process.send === "function"
|
|
170
|
+
? process.send.bind(process)
|
|
171
|
+
: undefined);
|
|
172
|
+
if (!send)
|
|
173
|
+
return false;
|
|
174
|
+
const pid = options.pid ?? process.pid;
|
|
175
|
+
const parentPid = Number(env[INTERNAL_RESTART_PARENT_PID_ENV_VAR]);
|
|
176
|
+
if (!Number.isInteger(parentPid) || parentPid <= 0)
|
|
177
|
+
return false;
|
|
178
|
+
const timeoutMs = options.timeoutMs ?? 2_000;
|
|
179
|
+
return new Promise((resolve) => {
|
|
180
|
+
let settled = false;
|
|
181
|
+
const finish = (ok) => {
|
|
182
|
+
if (settled)
|
|
183
|
+
return;
|
|
184
|
+
settled = true;
|
|
185
|
+
clearTimeout(timer);
|
|
186
|
+
resolve(ok);
|
|
187
|
+
};
|
|
188
|
+
const timer = setTimeout(() => finish(false), timeoutMs);
|
|
189
|
+
timer.unref?.();
|
|
190
|
+
try {
|
|
191
|
+
send({ type: INTERNAL_RESTART_READY_MESSAGE, pid, parentPid }, (error) => finish(!error));
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
finish(false);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
}
|
|
156
198
|
export function shouldAutoOpenWebUi(options = {}) {
|
|
157
199
|
const env = options.env ?? process.env;
|
|
158
200
|
return options.openOnStart !== false && env[INTERNAL_RESTART_ENV_VAR] !== "1";
|