@evandu/super-pi 0.1.177 → 0.1.179
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/bin/super-pi.js +153 -7
- package/dist/client/.super-pi-client-sources.json +3 -3
- package/dist/client/assets/{CodeMirrorEditor-DwtVhvhP.js → CodeMirrorEditor-BdY_vez9.js} +2 -2
- package/dist/client/assets/{dist-G3kXm5bB.js → dist-1AZAH0Vw.js} +1 -1
- package/dist/client/assets/{dist-aLwW7rgW.js → dist-BPaSHEMs.js} +1 -1
- package/dist/client/assets/{dist-BfwhoJ2c.js → dist-BXJ0FVm3.js} +1 -1
- package/dist/client/assets/{dist-CxcCl9nU.js → dist-BXrWZekq.js} +1 -1
- package/dist/client/assets/{dist-gOkylN4I.js → dist-B_v-qVKZ.js} +1 -1
- package/dist/client/assets/{dist-BvjjidDY.js → dist-BiQDCO0Z.js} +1 -1
- package/dist/client/assets/{dist-CHLXZD6Z.js → dist-Bxp6Lr31.js} +1 -1
- package/dist/client/assets/{dist-DKu5ZQPe.js → dist-BzicsMSL.js} +1 -1
- package/dist/client/assets/{dist-CaVt9IMZ.js → dist-CE4F-GnZ.js} +1 -1
- package/dist/client/assets/{dist-CnQpZpXf.js → dist-C_5fMTu2.js} +1 -1
- package/dist/client/assets/{dist-Ce50Y3kx.js → dist-CcVJlndc.js} +1 -1
- package/dist/client/assets/{dist-LjIserDZ.js → dist-CxZY8nws.js} +1 -1
- package/dist/client/assets/{dist-rsaX54pd.js → dist-D-AKDchV.js} +1 -1
- package/dist/client/assets/{dist-Bs2J7cm7.js → dist-D7S0Q68i.js} +1 -1
- package/dist/client/assets/{dist-B5HaTMDn.js → dist-DVYPDnbh.js} +1 -1
- package/dist/client/assets/{dist-DjKLhP2P.js → dist-DfY4zXbE.js} +1 -1
- package/dist/client/assets/{dist-DUlq1w0J.js → dist-DjsQFuR8.js} +1 -1
- package/dist/client/assets/{dist-C_3vxg18.js → dist-Dl22kIFG.js} +1 -1
- package/dist/client/assets/{dist-bRSb7mNW.js → dist-RxPniecV.js} +1 -1
- package/dist/client/assets/{dist-ddEYIZIm.js → dist-XnsZBWBb.js} +1 -1
- package/dist/client/assets/{dist-Oz3xTF4n.js → dist-tD_cG-sA.js} +1 -1
- package/dist/client/assets/{dist-EJoljryk.js → dist-zNY48I29.js} +1 -1
- package/dist/client/assets/{index-ZnFnQvel.js → index-Cy4Cb9zB.js} +4 -4
- package/dist/client/index.html +1 -1
- package/dist/server/command-handler.js +17 -2
- package/dist/server/http-server.d.ts +10 -0
- package/dist/server/http-server.js +61 -7
- package/dist/server/index.js +19 -6
- package/dist/server/update.d.ts +15 -1
- package/dist/server/update.js +37 -3
- package/package.json +6 -1
- package/scripts/restart-server.mjs +24 -3
package/dist/client/index.html
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
<link rel="alternate icon" href="/favicon.ico" />
|
|
9
9
|
<link rel="apple-touch-icon" href="/favicon.svg" />
|
|
10
10
|
<title>Super Pi</title>
|
|
11
|
-
<script type="module" crossorigin src="/assets/index-
|
|
11
|
+
<script type="module" crossorigin src="/assets/index-Cy4Cb9zB.js"></script>
|
|
12
12
|
<link rel="stylesheet" crossorigin href="/assets/index-DAKtgLA9.css">
|
|
13
13
|
</head>
|
|
14
14
|
<body>
|
|
@@ -2,7 +2,7 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import { join } from "node:path";
|
|
3
3
|
import { KNOWN_APIS } from "./providers.js";
|
|
4
4
|
import { CHILD_SESSION_LOCK_REASON } from "../shared/child-session-lock.js";
|
|
5
|
-
import { packageUpdateArgs } from "./update.js";
|
|
5
|
+
import { claimUpdateSlot, packageUpdateArgs, releaseUpdateSlot } from "./update.js";
|
|
6
6
|
/**
|
|
7
7
|
* Routes client commands to appropriate application services and converts results or errors.
|
|
8
8
|
*/
|
|
@@ -445,11 +445,26 @@ export class CommandHandler {
|
|
|
445
445
|
// npm install -g picks up the latest published version, then the same restart script the
|
|
446
446
|
// /api/update endpoint uses runs detached. The user sees npm's output stream in their
|
|
447
447
|
// terminal because stdio: inherit is what makes a CLI feel like a CLI.
|
|
448
|
+
//
|
|
449
|
+
// ONE UPDATE AT A TIME. The same slot the /api/update endpoint uses: this command and that
|
|
450
|
+
// endpoint install into the same global prefix, so a slash command that starts while a
|
|
451
|
+
// button-started update is still running would have the two npm processes fight over the
|
|
452
|
+
// same files. The refusal is a THROWN MESSAGE because that is what the client already knows
|
|
453
|
+
// how to show - a toast that says what happened, in the reader's language.
|
|
454
|
+
if (!claimUpdateSlot())
|
|
455
|
+
throw new Error("更新已经在进行中,请等它结束");
|
|
448
456
|
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
449
457
|
const update = packageUpdateArgs(this.rootDir ?? import.meta.dirname);
|
|
450
458
|
const updater = spawn(npmCmd, update.args, { stdio: "inherit", shell: process.platform === "win32", env: process.env });
|
|
451
|
-
|
|
459
|
+
// EXACTLY ONE RELEASE PER CHILD, for the same reason the HTTP endpoint does it: a spawn that
|
|
460
|
+
// fails emits "error" and then "close", and releasing twice would free the slot out from
|
|
461
|
+
// under an update that had already claimed it.
|
|
462
|
+
let released = false;
|
|
463
|
+
const release = () => { if (released)
|
|
464
|
+
return; released = true; releaseUpdateSlot(); };
|
|
465
|
+
updater.on("error", (error) => { release(); console.error(`Super Pi: npm install failed: ${error instanceof Error ? error.message : "unknown error"}`); });
|
|
452
466
|
updater.on("close", (code) => {
|
|
467
|
+
release();
|
|
453
468
|
if (code !== 0) {
|
|
454
469
|
console.error(`Super Pi: npm install exited with code ${code}`);
|
|
455
470
|
return;
|
|
@@ -29,5 +29,15 @@ export declare function isAuthorized(request: {
|
|
|
29
29
|
export interface HttpServerOptions {
|
|
30
30
|
context: ServerContext;
|
|
31
31
|
onShutdown: (reason: string) => Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* THE PORT AND HOST THIS SERVER LISTENS ON.
|
|
34
|
+
*
|
|
35
|
+
* The update endpoint hands them to the restart script, which has to find the running server
|
|
36
|
+
* by port - it cannot know the port on its own, because the CLI passes the port as a command
|
|
37
|
+
* line argument. They are the values the server resolved at startup, so the server that comes
|
|
38
|
+
* back listens on exactly the same address as the one that went away.
|
|
39
|
+
*/
|
|
40
|
+
port?: number;
|
|
41
|
+
host?: string;
|
|
32
42
|
}
|
|
33
43
|
export declare function createHttpServer(options: HttpServerOptions): NodeHttpServer;
|
|
@@ -21,7 +21,7 @@ import { MAX_DOCUMENT_REQUEST_BYTES } from "./application/document-service.js";
|
|
|
21
21
|
import { MAX_FEISHU_REQUEST_BYTES } from "./application/feishu-service.js";
|
|
22
22
|
import { MAX_WEB_REQUEST_BYTES } from "./web.js";
|
|
23
23
|
import { MAX_TOOL_BODY_BYTES } from "./tool-registry.js";
|
|
24
|
-
import { packageUpdateArgs } from "./update.js";
|
|
24
|
+
import { claimUpdateSlot, packageUpdateArgs, releaseUpdateSlot } from "./update.js";
|
|
25
25
|
import { FileIndex } from "./file-index.js";
|
|
26
26
|
import { ensureNameIndex, recordWorkspacePath } from "./file-name-index.js";
|
|
27
27
|
import { resolveSessionCwd, sessionScopeKey, workspacePathFromSession } from "./session-cwd.js";
|
|
@@ -277,7 +277,7 @@ async function readRequestText(request, limitBytes) {
|
|
|
277
277
|
return { text: text + decoder.decode(), tooLarge: false };
|
|
278
278
|
}
|
|
279
279
|
export function createHttpServer(options) {
|
|
280
|
-
const { context, onShutdown } = options;
|
|
280
|
+
const { context, onShutdown, port, host } = options;
|
|
281
281
|
const { store, paths, accessToken, broadcaster, runService, catalogService, execService, toolRegistry, toolRunner } = context;
|
|
282
282
|
const { uploadsDir, clientDir, rootDir } = paths;
|
|
283
283
|
return createServer(async (request, response) => {
|
|
@@ -989,24 +989,78 @@ export function createHttpServer(options) {
|
|
|
989
989
|
response.writeHead(403).end("Update requires a loopback or private network POST");
|
|
990
990
|
return;
|
|
991
991
|
}
|
|
992
|
+
// THE BODY IS OPTIONAL. An old client sends nothing; that still has to work.
|
|
993
|
+
let body = {};
|
|
994
|
+
try {
|
|
995
|
+
const reader = textBodyReader();
|
|
996
|
+
let raw = "";
|
|
997
|
+
for await (const chunk of request) {
|
|
998
|
+
const piece = chunk;
|
|
999
|
+
raw = reader.push(piece);
|
|
1000
|
+
if (raw.length > 4096)
|
|
1001
|
+
break;
|
|
1002
|
+
}
|
|
1003
|
+
raw = reader.finish();
|
|
1004
|
+
body = raw.trim() ? JSON.parse(raw) : {};
|
|
1005
|
+
}
|
|
1006
|
+
catch {
|
|
1007
|
+
body = {};
|
|
1008
|
+
}
|
|
1009
|
+
const wantedVersion = typeof body?.version === "string" && body.version ? body.version : undefined;
|
|
1010
|
+
const restartOnly = body?.restartOnly === true;
|
|
1011
|
+
// ONE UPDATE AT A TIME. The client disables its button during an update, but a second tab
|
|
1012
|
+
// (or a UI that failed to disable) can still reach this endpoint, and two npm installs against
|
|
1013
|
+
// one global prefix corrupt each other. The slot is claimed BEFORE the 202 so a refused
|
|
1014
|
+
// request never looks like one that started.
|
|
1015
|
+
if (!claimUpdateSlot()) {
|
|
1016
|
+
json(409, { error: "更新已经在进行中,请等它结束" });
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
992
1019
|
response.writeHead(202, { "content-type": "application/json; charset=utf-8" }).end(JSON.stringify({ status: "updating" }));
|
|
1020
|
+
// EXACTLY ONE RELEASE PER CHILD. A spawn that fails to start emits "error" AND THEN "close",
|
|
1021
|
+
// so a plain release in both handlers would free the slot a second time - and if a new
|
|
1022
|
+
// update had already claimed it in between, that second release would hand the slot away
|
|
1023
|
+
// while the new update was still running.
|
|
1024
|
+
let released = false;
|
|
1025
|
+
const release = () => { if (released)
|
|
1026
|
+
return; released = true; releaseUpdateSlot(); };
|
|
1027
|
+
// THE PORT AND HOST THE SERVER IS REALLY ON. The restart script has to find this very
|
|
1028
|
+
// server by port to replace it, and it cannot work the port out on its own: the CLI starts
|
|
1029
|
+
// the server with `--port=N` as an argument. Without them the script looked at the default
|
|
1030
|
+
// port, found nothing there, and started a second server next to the running one.
|
|
1031
|
+
const listenPort = port ?? request.socket.localPort;
|
|
1032
|
+
const restartEnv = { ...process.env, SUPER_PI_PORT: String(listenPort) };
|
|
1033
|
+
if (host)
|
|
1034
|
+
restartEnv.SUPER_PI_HOST = host;
|
|
1035
|
+
const restartNow = () => {
|
|
1036
|
+
console.log("Super Pi: restart triggered.");
|
|
1037
|
+
const restart = spawn(process.execPath, [join(rootDir, "scripts", "restart-server.mjs")], { cwd: rootDir, detached: true, stdio: "ignore", env: restartEnv });
|
|
1038
|
+
restart.on("error", (restartError) => console.error(`Super Pi: restart after update failed: ${restartError instanceof Error ? restartError.message : String(restartError)}`));
|
|
1039
|
+
restart.unref();
|
|
1040
|
+
};
|
|
1041
|
+
// RESTART ONLY: the code on disk is already new, the running server is not. Re-downloading
|
|
1042
|
+
// 32.8 MB to restart a process is waste; the second button asks for exactly this.
|
|
1043
|
+
if (restartOnly) {
|
|
1044
|
+
release();
|
|
1045
|
+
restartNow();
|
|
1046
|
+
return;
|
|
1047
|
+
}
|
|
993
1048
|
// Use npm directly instead of `pi update` to avoid Windows detached+shell exit timing issues.
|
|
994
1049
|
// npm's exit code correctly reflects whether the update succeeded.
|
|
995
1050
|
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
996
|
-
const update = packageUpdateArgs(rootDir);
|
|
1051
|
+
const update = packageUpdateArgs(rootDir, wantedVersion);
|
|
997
1052
|
const child = spawn(npmCmd, update.args, { stdio: "ignore", shell: process.platform === "win32", env: process.env });
|
|
998
1053
|
child.on("close", (code) => {
|
|
1054
|
+
release();
|
|
999
1055
|
if (code === 0 || code === null) {
|
|
1000
1056
|
console.log("Super Pi: npm install completed, triggering restart.");
|
|
1001
|
-
|
|
1002
|
-
restart.on("error", (restartError) => console.error(`Super Pi: restart after update failed: ${restartError instanceof Error ? restartError.message : String(restartError)}`));
|
|
1003
|
-
restart.unref();
|
|
1057
|
+
restartNow();
|
|
1004
1058
|
}
|
|
1005
1059
|
else {
|
|
1006
1060
|
console.error(`Super Pi: npm install failed with code ${code}`);
|
|
1007
1061
|
}
|
|
1008
1062
|
});
|
|
1009
|
-
child.on("error", (error) => { console.warn(`Super Pi: update spawn failed: ${error instanceof Error ? error.message : "unknown error"}`); });
|
|
1063
|
+
child.on("error", (error) => { release(); console.warn(`Super Pi: update spawn failed: ${error instanceof Error ? error.message : "unknown error"}`); });
|
|
1010
1064
|
child.unref();
|
|
1011
1065
|
return;
|
|
1012
1066
|
}
|
package/dist/server/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { validateConversationModel } from "./model-filter.js";
|
|
|
2
2
|
import { ActiveMemory } from "./active-memory.js";
|
|
3
3
|
import { KnowledgeBase } from "./knowledge.js";
|
|
4
4
|
import { VectorIndex } from "./knowledge-vectors.js";
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
6
|
import { mkdir } from "node:fs/promises";
|
|
7
7
|
import { homedir } from "node:os";
|
|
8
8
|
import { join, resolve } from "node:path";
|
|
@@ -98,11 +98,24 @@ void (async () => {
|
|
|
98
98
|
}
|
|
99
99
|
})();
|
|
100
100
|
const cliArgs = parseServerArgs(process.argv);
|
|
101
|
+
// THE PORT THE PERSON CHOSE, IN THE ORDER THEY CHOSE IT: the command line, then the
|
|
102
|
+
// environment, then the saved config, then the built-in default. `super-pi set port` writes the
|
|
103
|
+
// saved config, so a deployment that cannot pass a flag still gets the port it asked for.
|
|
104
|
+
const savedSettings = (() => {
|
|
105
|
+
try {
|
|
106
|
+
return JSON.parse(readFileSync(join(dataDir, "config.json"), "utf8")) ?? {};
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return {};
|
|
110
|
+
}
|
|
111
|
+
})();
|
|
101
112
|
// A non-numeric SUPER_PI_PORT would parse to NaN and die later in `listen` with a confusing error;
|
|
102
|
-
// like the other environment variables here, an unusable value falls back to the
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const
|
|
113
|
+
// like the other environment variables here, an unusable value falls back to the next choice.
|
|
114
|
+
const savedPort = Number(savedSettings.port);
|
|
115
|
+
const savedHost = typeof savedSettings.host === "string" && savedSettings.host ? savedSettings.host : undefined;
|
|
116
|
+
const envPort = process.env.SUPER_PI_PORT === undefined ? undefined : Number.parseInt(process.env.SUPER_PI_PORT, 10);
|
|
117
|
+
const port = cliArgs.port ?? (envPort !== undefined && !Number.isNaN(envPort) ? envPort : (Number.isInteger(savedPort) && savedPort > 0 ? savedPort : 3080));
|
|
118
|
+
const host = cliArgs.host ?? process.env.SUPER_PI_HOST ?? savedHost ?? "0.0.0.0";
|
|
106
119
|
const internalHost = (host === "0.0.0.0" || host === "::" || !host) ? "127.0.0.1" : host;
|
|
107
120
|
const agentPromptDir = join(dataDir, "agent-prompts");
|
|
108
121
|
const clientDir = join(rootDir, "dist", "client");
|
|
@@ -396,7 +409,7 @@ async function shutdown(reason) {
|
|
|
396
409
|
process.on("SIGINT", () => { void shutdown("SIGINT").finally(() => process.exit(0)); });
|
|
397
410
|
process.on("SIGTERM", () => { void shutdown("SIGTERM").finally(() => process.exit(0)); });
|
|
398
411
|
// ------------------------------------------------------------- Transports & startup
|
|
399
|
-
const httpServer = createHttpServer({ context: serverContext, onShutdown: shutdown });
|
|
412
|
+
const httpServer = createHttpServer({ context: serverContext, onShutdown: shutdown, port, host });
|
|
400
413
|
const wsServer = new WsServer({
|
|
401
414
|
httpServer,
|
|
402
415
|
broadcaster,
|
package/dist/server/update.d.ts
CHANGED
|
@@ -6,8 +6,22 @@
|
|
|
6
6
|
* copy while this process keeps serving the old one. Installed package roots
|
|
7
7
|
* contain `node_modules/@evandu/super-pi`; walking back to the prefix makes the
|
|
8
8
|
* update target unambiguous. Source checkouts retain the global fallback.
|
|
9
|
+
*
|
|
10
|
+
* `-g` is not optional. Without it npm reads `--prefix <dir>` as a PROJECT
|
|
11
|
+
* directory, recomputes `<dir>/node_modules` as that project's dependency
|
|
12
|
+
* tree, and every package in there that is unrelated to this one counts as
|
|
13
|
+
* extraneous - so npm removes it. A global prefix holds the user's global
|
|
14
|
+
* CLIs (npm itself included), so one update would wipe them all. With `-g`
|
|
15
|
+
* npm adds the package to the global tree and leaves everything else alone.
|
|
16
|
+
*
|
|
17
|
+
* The fetch options are there because the tarball is ~32 MB: npm's default
|
|
18
|
+
* 5 minute timeout fails on a slow line and the update looks broken.
|
|
9
19
|
*/
|
|
10
|
-
export declare function packageUpdateArgs(rootDir: string): {
|
|
20
|
+
export declare function packageUpdateArgs(rootDir: string, version?: string): {
|
|
11
21
|
args: string[];
|
|
12
22
|
prefix?: string;
|
|
13
23
|
};
|
|
24
|
+
/** Claims the update slot. False when an update is already running. */
|
|
25
|
+
export declare function claimUpdateSlot(): boolean;
|
|
26
|
+
/** Releases the update slot, so the next update can start. */
|
|
27
|
+
export declare function releaseUpdateSlot(): void;
|
package/dist/server/update.js
CHANGED
|
@@ -7,14 +7,48 @@ import { sep } from "node:path";
|
|
|
7
7
|
* copy while this process keeps serving the old one. Installed package roots
|
|
8
8
|
* contain `node_modules/@evandu/super-pi`; walking back to the prefix makes the
|
|
9
9
|
* update target unambiguous. Source checkouts retain the global fallback.
|
|
10
|
+
*
|
|
11
|
+
* `-g` is not optional. Without it npm reads `--prefix <dir>` as a PROJECT
|
|
12
|
+
* directory, recomputes `<dir>/node_modules` as that project's dependency
|
|
13
|
+
* tree, and every package in there that is unrelated to this one counts as
|
|
14
|
+
* extraneous - so npm removes it. A global prefix holds the user's global
|
|
15
|
+
* CLIs (npm itself included), so one update would wipe them all. With `-g`
|
|
16
|
+
* npm adds the package to the global tree and leaves everything else alone.
|
|
17
|
+
*
|
|
18
|
+
* The fetch options are there because the tarball is ~32 MB: npm's default
|
|
19
|
+
* 5 minute timeout fails on a slow line and the update looks broken.
|
|
10
20
|
*/
|
|
11
|
-
export function packageUpdateArgs(rootDir) {
|
|
21
|
+
export function packageUpdateArgs(rootDir, version) {
|
|
12
22
|
const marker = `${sep}node_modules${sep}`;
|
|
13
23
|
const lower = rootDir.toLowerCase();
|
|
14
24
|
const index = lower.lastIndexOf(marker);
|
|
25
|
+
// THE VERSION THE USER CONFIRMED. The check reads the registry directly while npm resolves
|
|
26
|
+
// "latest" from its own cache, so the two can disagree: the dialog promises vX and the
|
|
27
|
+
// install silently lands on vY. Pinning the version the dialog showed removes that gap.
|
|
28
|
+
const target = version ? `@evandu/super-pi@${version}` : "@evandu/super-pi";
|
|
15
29
|
if (index >= 0) {
|
|
16
30
|
const prefix = rootDir.slice(0, index);
|
|
17
|
-
return { prefix, args: ["install", "--prefix", prefix, "
|
|
31
|
+
return { prefix, args: ["install", "-g", "--prefix", prefix, "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", target] };
|
|
18
32
|
}
|
|
19
|
-
return { args: ["install", "-g", "
|
|
33
|
+
return { args: ["install", "-g", "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", target] };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* ONE UPDATE AT A TIME.
|
|
37
|
+
*
|
|
38
|
+
* Two `npm install` runs against the same global prefix at once fight each other over the same
|
|
39
|
+
* files, and the second one can report a failure that belongs to the first. The slot is claimed
|
|
40
|
+
* before the install starts and released when it ends, so a second request is refused instead of
|
|
41
|
+
* racing the first.
|
|
42
|
+
*/
|
|
43
|
+
let updateInFlight = false;
|
|
44
|
+
/** Claims the update slot. False when an update is already running. */
|
|
45
|
+
export function claimUpdateSlot() {
|
|
46
|
+
if (updateInFlight)
|
|
47
|
+
return false;
|
|
48
|
+
updateInFlight = true;
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
/** Releases the update slot, so the next update can start. */
|
|
52
|
+
export function releaseUpdateSlot() {
|
|
53
|
+
updateInFlight = false;
|
|
20
54
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evandu/super-pi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.179",
|
|
4
4
|
"description": "Web control plane and companion for Pi",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -43,6 +43,9 @@
|
|
|
43
43
|
"test": "node scripts/test.mjs"
|
|
44
44
|
},
|
|
45
45
|
"check:logic": [
|
|
46
|
+
"scripts/update-args.test.mjs",
|
|
47
|
+
"scripts/update-restart.test.mjs",
|
|
48
|
+
"scripts/update-version-pinning.test.mjs",
|
|
46
49
|
"scripts/test-runner.test.mjs",
|
|
47
50
|
"scripts/stream-checkpoint.test.mjs",
|
|
48
51
|
"scripts/stream-transport.test.mjs",
|
|
@@ -199,6 +202,7 @@
|
|
|
199
202
|
"scripts/extension-load.test.mjs",
|
|
200
203
|
"scripts/static-client-assets.test.mjs",
|
|
201
204
|
"scripts/cli.test.mjs",
|
|
205
|
+
"scripts/super-pi-cli-port.test.mjs",
|
|
202
206
|
"scripts/check-runner.test.mjs",
|
|
203
207
|
"scripts/gate-count-truth.test.mjs",
|
|
204
208
|
"scripts/skills-discovery.test.mjs",
|
|
@@ -253,6 +257,7 @@
|
|
|
253
257
|
"scripts/mutation-lock-coverage.test.mjs"
|
|
254
258
|
],
|
|
255
259
|
"check:browser": [
|
|
260
|
+
"scripts/update-button-browser.test.mjs",
|
|
256
261
|
"scripts/interrupt-browser.test.mjs",
|
|
257
262
|
"scripts/revert-browser.test.mjs",
|
|
258
263
|
"scripts/extension-ui-browser.test.mjs",
|
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
import { spawn, execFile } from "node:child_process";
|
|
2
2
|
import { openSync, readFileSync } from "node:fs";
|
|
3
|
-
import { platform } from "node:os";
|
|
3
|
+
import { homedir, platform } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
4
5
|
import { promisify } from "node:util";
|
|
5
6
|
|
|
6
7
|
const execFileAsync = promisify(execFile);
|
|
7
|
-
|
|
8
|
+
|
|
9
|
+
/** The saved port, from the same settings file the CLI reads. Returns undefined when there is
|
|
10
|
+
* no usable value, so the caller can fall through to the default. */
|
|
11
|
+
function savedPort() {
|
|
12
|
+
try {
|
|
13
|
+
const dir = process.env.SUPER_PI_DATA_DIR ? resolve(process.env.SUPER_PI_DATA_DIR) : join(homedir(), ".super-pi");
|
|
14
|
+
const saved = JSON.parse(readFileSync(join(dir, "config.json"), "utf8"));
|
|
15
|
+
const value = Number(saved?.port);
|
|
16
|
+
return Number.isInteger(value) && value > 0 ? value : undefined;
|
|
17
|
+
} catch { return undefined; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// THE PORT THE SERVER IS REALLY ON. The environment wins, then the saved settings, then the
|
|
21
|
+
// default. Reading the environment ALONE was the bug: the CLI starts the server with `--port=N`
|
|
22
|
+
// as an argument and never sets this variable, so the script looked at 3080 and never found the
|
|
23
|
+
// running server it was supposed to replace.
|
|
24
|
+
const port = Number(process.env.SUPER_PI_PORT ?? savedPort() ?? 3080);
|
|
8
25
|
const entry = "dist/server/index.js";
|
|
9
26
|
const allowedEntries = [entry, "src/server/index.ts"];
|
|
27
|
+
/** Compare paths the way Windows reports them. A command line on Windows contains BACKSLASHES
|
|
28
|
+
* (`dist\server\index.js`) while the entries above use forward slashes, so a plain
|
|
29
|
+
* `cmd.includes(candidate)` never matched and the script refused to kill its own server. */
|
|
30
|
+
const normalisePath = (value) => value.replace(/[\\/]+/g, "/").toLowerCase();
|
|
10
31
|
const waitMs = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
32
|
async function pidsOnPort() {
|
|
12
33
|
if (platform() === "win32") {
|
|
@@ -39,7 +60,7 @@ async function forceKill(pid) {
|
|
|
39
60
|
}
|
|
40
61
|
async function stopConflicts() {
|
|
41
62
|
const pids = await pidsOnPort(); if (!pids.length) { console.log(`Port ${port} is free.`); return true; }
|
|
42
|
-
for (const pid of pids) { const cmd = await commandLine(pid); console.log(`Port ${port} is occupied by PID ${pid}: ${cmd}`); if (!allowedEntries.some((candidate) => cmd.includes(candidate))) { console.error(`Refusing to kill non-super-pi process on port ${port}: ${cmd}`); process.exitCode = 2; return false; } }
|
|
63
|
+
for (const pid of pids) { const cmd = await commandLine(pid); console.log(`Port ${port} is occupied by PID ${pid}: ${cmd}`); if (!allowedEntries.some((candidate) => normalisePath(cmd).includes(normalisePath(candidate)))) { console.error(`Refusing to kill non-super-pi process on port ${port}: ${cmd}`); process.exitCode = 2; return false; } }
|
|
43
64
|
const status = await requestShutdown();
|
|
44
65
|
if (status !== null && status >= 200 && status < 300) {
|
|
45
66
|
console.log(`Graceful shutdown accepted (HTTP ${status}); waiting for port ${port} to release...`);
|