@evandu/super-pi 0.1.177 → 0.1.178

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.
Files changed (33) hide show
  1. package/bin/super-pi.js +152 -6
  2. package/dist/client/.super-pi-client-sources.json +3 -3
  3. package/dist/client/assets/{CodeMirrorEditor-DwtVhvhP.js → CodeMirrorEditor-BenB0qCl.js} +2 -2
  4. package/dist/client/assets/{dist-B5HaTMDn.js → dist-9SgjXHKI.js} +1 -1
  5. package/dist/client/assets/{dist-DUlq1w0J.js → dist-B7qg204Z.js} +1 -1
  6. package/dist/client/assets/{dist-aLwW7rgW.js → dist-BBqymG_O.js} +1 -1
  7. package/dist/client/assets/{dist-BfwhoJ2c.js → dist-BEhZCgAe.js} +1 -1
  8. package/dist/client/assets/{dist-CnQpZpXf.js → dist-BTbcZiCW.js} +1 -1
  9. package/dist/client/assets/{dist-CaVt9IMZ.js → dist-BWYKkV9J.js} +1 -1
  10. package/dist/client/assets/{dist-Oz3xTF4n.js → dist-BfRyxOfA.js} +1 -1
  11. package/dist/client/assets/{dist-bRSb7mNW.js → dist-BgVPFmiW.js} +1 -1
  12. package/dist/client/assets/{dist-rsaX54pd.js → dist-Bm5z1I13.js} +1 -1
  13. package/dist/client/assets/{dist-DKu5ZQPe.js → dist-BondndXn.js} +1 -1
  14. package/dist/client/assets/{dist-DjKLhP2P.js → dist-BphpuegD.js} +1 -1
  15. package/dist/client/assets/{dist-G3kXm5bB.js → dist-Bqa4C7ia.js} +1 -1
  16. package/dist/client/assets/{dist-BvjjidDY.js → dist-C-XFV2Lh.js} +1 -1
  17. package/dist/client/assets/{dist-ddEYIZIm.js → dist-CAqvv7Ol.js} +1 -1
  18. package/dist/client/assets/{dist-Bs2J7cm7.js → dist-CP2zo9EH.js} +1 -1
  19. package/dist/client/assets/{dist-C_3vxg18.js → dist-D1W6DFkl.js} +1 -1
  20. package/dist/client/assets/{dist-EJoljryk.js → dist-D754rN4L.js} +1 -1
  21. package/dist/client/assets/{dist-Ce50Y3kx.js → dist-DRcKCYFV.js} +1 -1
  22. package/dist/client/assets/{dist-CxcCl9nU.js → dist-DsYkcVVD.js} +1 -1
  23. package/dist/client/assets/{dist-gOkylN4I.js → dist-Dy29IXwY.js} +1 -1
  24. package/dist/client/assets/{dist-LjIserDZ.js → dist-DynAZB38.js} +1 -1
  25. package/dist/client/assets/{dist-CHLXZD6Z.js → dist-fJbqoI_z.js} +1 -1
  26. package/dist/client/assets/{index-ZnFnQvel.js → index-D5fhfow-.js} +4 -4
  27. package/dist/client/index.html +1 -1
  28. package/dist/server/command-handler.js +17 -2
  29. package/dist/server/http-server.js +18 -2
  30. package/dist/server/index.js +18 -5
  31. package/dist/server/update.d.ts +14 -0
  32. package/dist/server/update.js +32 -2
  33. package/package.json +4 -1
@@ -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-ZnFnQvel.js"></script>
11
+ <script type="module" crossorigin src="/assets/index-D5fhfow-.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
- updater.on("error", (error) => { console.error(`Super Pi: npm install failed: ${error instanceof Error ? error.message : "unknown error"}`); });
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;
@@ -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";
@@ -989,13 +989,29 @@ export function createHttpServer(options) {
989
989
  response.writeHead(403).end("Update requires a loopback or private network POST");
990
990
  return;
991
991
  }
992
+ // ONE UPDATE AT A TIME. The client disables its button during an update, but a second tab
993
+ // (or a UI that failed to disable) can still reach this endpoint, and two npm installs against
994
+ // one global prefix corrupt each other. The slot is claimed BEFORE the 202 so a refused
995
+ // request never looks like one that started.
996
+ if (!claimUpdateSlot()) {
997
+ json(409, { error: "更新已经在进行中,请等它结束" });
998
+ return;
999
+ }
992
1000
  response.writeHead(202, { "content-type": "application/json; charset=utf-8" }).end(JSON.stringify({ status: "updating" }));
993
1001
  // Use npm directly instead of `pi update` to avoid Windows detached+shell exit timing issues.
994
1002
  // npm's exit code correctly reflects whether the update succeeded.
995
1003
  const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
996
1004
  const update = packageUpdateArgs(rootDir);
997
1005
  const child = spawn(npmCmd, update.args, { stdio: "ignore", shell: process.platform === "win32", env: process.env });
1006
+ // EXACTLY ONE RELEASE PER CHILD. A spawn that fails to start emits "error" AND THEN "close",
1007
+ // so a plain release in both handlers would free the slot a second time - and if a new
1008
+ // update had already claimed it in between, that second release would hand the slot away
1009
+ // while the new update was still running.
1010
+ let released = false;
1011
+ const release = () => { if (released)
1012
+ return; released = true; releaseUpdateSlot(); };
998
1013
  child.on("close", (code) => {
1014
+ release();
999
1015
  if (code === 0 || code === null) {
1000
1016
  console.log("Super Pi: npm install completed, triggering restart.");
1001
1017
  const restart = spawn(process.execPath, [join(rootDir, "scripts", "restart-server.mjs")], { cwd: rootDir, detached: true, stdio: "ignore", env: process.env });
@@ -1006,7 +1022,7 @@ export function createHttpServer(options) {
1006
1022
  console.error(`Super Pi: npm install failed with code ${code}`);
1007
1023
  }
1008
1024
  });
1009
- child.on("error", (error) => { console.warn(`Super Pi: update spawn failed: ${error instanceof Error ? error.message : "unknown error"}`); });
1025
+ child.on("error", (error) => { release(); console.warn(`Super Pi: update spawn failed: ${error instanceof Error ? error.message : "unknown error"}`); });
1010
1026
  child.unref();
1011
1027
  return;
1012
1028
  }
@@ -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 default port.
103
- const envPort = Number.parseInt(process.env.SUPER_PI_PORT ?? "3080", 10);
104
- const port = cliArgs.port ?? (Number.isNaN(envPort) ? 3080 : envPort);
105
- const host = cliArgs.host ?? process.env.SUPER_PI_HOST ?? "0.0.0.0";
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");
@@ -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
20
  export declare function packageUpdateArgs(rootDir: 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;
@@ -7,6 +7,16 @@ 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
21
  export function packageUpdateArgs(rootDir) {
12
22
  const marker = `${sep}node_modules${sep}`;
@@ -14,7 +24,27 @@ export function packageUpdateArgs(rootDir) {
14
24
  const index = lower.lastIndexOf(marker);
15
25
  if (index >= 0) {
16
26
  const prefix = rootDir.slice(0, index);
17
- return { prefix, args: ["install", "--prefix", prefix, "@evandu/super-pi"] };
27
+ return { prefix, args: ["install", "-g", "--prefix", prefix, "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", "@evandu/super-pi"] };
18
28
  }
19
- return { args: ["install", "-g", "@evandu/super-pi"] };
29
+ return { args: ["install", "-g", "--fetch-timeout=900000", "--fetch-retries=8", "--fetch-retry-maxtimeout=120000", "@evandu/super-pi"] };
30
+ }
31
+ /**
32
+ * ONE UPDATE AT A TIME.
33
+ *
34
+ * Two `npm install` runs against the same global prefix at once fight each other over the same
35
+ * files, and the second one can report a failure that belongs to the first. The slot is claimed
36
+ * before the install starts and released when it ends, so a second request is refused instead of
37
+ * racing the first.
38
+ */
39
+ let updateInFlight = false;
40
+ /** Claims the update slot. False when an update is already running. */
41
+ export function claimUpdateSlot() {
42
+ if (updateInFlight)
43
+ return false;
44
+ updateInFlight = true;
45
+ return true;
46
+ }
47
+ /** Releases the update slot, so the next update can start. */
48
+ export function releaseUpdateSlot() {
49
+ updateInFlight = false;
20
50
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.177",
3
+ "version": "0.1.178",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -43,6 +43,7 @@
43
43
  "test": "node scripts/test.mjs"
44
44
  },
45
45
  "check:logic": [
46
+ "scripts/update-args.test.mjs",
46
47
  "scripts/test-runner.test.mjs",
47
48
  "scripts/stream-checkpoint.test.mjs",
48
49
  "scripts/stream-transport.test.mjs",
@@ -199,6 +200,7 @@
199
200
  "scripts/extension-load.test.mjs",
200
201
  "scripts/static-client-assets.test.mjs",
201
202
  "scripts/cli.test.mjs",
203
+ "scripts/super-pi-cli-port.test.mjs",
202
204
  "scripts/check-runner.test.mjs",
203
205
  "scripts/gate-count-truth.test.mjs",
204
206
  "scripts/skills-discovery.test.mjs",
@@ -253,6 +255,7 @@
253
255
  "scripts/mutation-lock-coverage.test.mjs"
254
256
  ],
255
257
  "check:browser": [
258
+ "scripts/update-button-browser.test.mjs",
256
259
  "scripts/interrupt-browser.test.mjs",
257
260
  "scripts/revert-browser.test.mjs",
258
261
  "scripts/extension-ui-browser.test.mjs",