pi-hub-server 0.3.2 → 0.4.1

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.
@@ -1,42 +1,94 @@
1
1
  /**
2
- * 会话锁 —— 只是 `~/pi-plugins/pi-session-lock/core.mjs` 的转发层。
2
+ * 会话锁 —— 优先用 `pi-session-lock` 插件的唯一实现,找不到就降级到内置最小实现。
3
3
  *
4
- * 唯一实现(含判定口径)在 plugin 里;pi 侧扩展与进程外调用方(pi-hub)共用它,
5
- * 避免两份算法漂移。找不到就直接报错:宁可 hub 起不来,也不要无锁写会话文件。
4
+ * 加载期**不再 throw**:全新机器上 `pi-hub doctor` / `pi-hub serve` 必须能起来
5
+ * (开箱即用是硬指标;旧实现 `throw new Error("pi-session-lock core not found")` 让
6
+ * 所有子命令连 import 都过不去)。降级不等于静默:`lockSource` 由 serve banner、
7
+ * `pi-hub doctor` 与 `/v1/health` 打出来,`corePath` 指到实际加载的那份。
8
+ *
9
+ * 解析顺序(显式优先,其次各处常见安装位置):
10
+ * 1. `PI_HUB_SESSION_LOCK_CORE`(显式指定,gate S6 用它验证 plugin 分支)
11
+ * 2. 自身 node_modules —— `require.resolve`,覆盖 `npm i -g pi-session-lock`(全局时是本包的
12
+ * 嵌套依赖)与仓内 `npm i pi-session-lock` 两种装法
13
+ * 3. 兄弟全局目录 `<prefix>/lib/node_modules/pi-session-lock/core.mjs`(两个包各自全局装)
14
+ * 4. `~/pi-plugins/pi-session-lock/core.mjs`(插件仓直接 clone 的装法)
15
+ * 5. `~/.pi/agent/npm/node_modules/pi-session-lock/core.mjs`(pi 自己的 npm 布局)
6
16
  */
7
17
  import fs from "node:fs";
8
18
  import path from "node:path";
9
- import { pathToFileURL } from "node:url";
19
+ import { createRequire } from "node:module";
20
+ import { fileURLToPath, pathToFileURL } from "node:url";
10
21
  import { HOME } from "./config.mjs";
22
+ import * as fallbackLock from "./session-lock-fallback.mjs";
23
+
24
+ const HERE = path.dirname(fileURLToPath(import.meta.url));
11
25
 
12
- const candidates = [
13
- process.env.PI_HUB_SESSION_LOCK_CORE,
14
- path.join(HOME, "pi-plugins", "pi-session-lock", "core.mjs"),
15
- path.join(HOME, ".pi", "agent", "npm", "node_modules", "pi-session-lock", "core.mjs"),
16
- ].filter(Boolean);
26
+ /** 插件必须给出的函数(= 本模块导出的全部读写面);缺一个就当没装(降级要显式,不能半残着跑)。 */
27
+ const REQUIRED = ["identity", "lockDirFor", "peek", "claim", "release", "classify", "readOwner"];
28
+
29
+ function resolveInstalled() {
30
+ const require = createRequire(import.meta.url);
31
+ // 插件的 exports 表里 "." 与 "./core" 都指向 core.mjs;两种写法都试,避免依赖表细节。
32
+ for (const specifier of ["pi-session-lock/core", "pi-session-lock"]) {
33
+ try {
34
+ return require.resolve(specifier);
35
+ } catch {
36
+ // 换下一个写法
37
+ }
38
+ }
39
+ return null;
40
+ }
41
+
42
+ function candidates() {
43
+ return [
44
+ process.env.PI_HUB_SESSION_LOCK_CORE,
45
+ resolveInstalled(),
46
+ path.join(HERE, "..", "node_modules", "pi-session-lock", "core.mjs"),
47
+ path.join(HERE, "..", "..", "pi-session-lock", "core.mjs"),
48
+ path.join(HOME, "pi-plugins", "pi-session-lock", "core.mjs"),
49
+ path.join(HOME, ".pi", "agent", "npm", "node_modules", "pi-session-lock", "core.mjs"),
50
+ ].filter(Boolean);
51
+ }
17
52
 
18
- const found = candidates.find((p) => {
53
+ function exists(p) {
19
54
  try {
20
55
  return fs.existsSync(p);
21
56
  } catch {
22
57
  return false;
23
58
  }
24
- });
59
+ }
60
+
61
+ const foundPath = candidates().find(exists) ?? null;
25
62
 
26
- if (!found) {
27
- throw new Error(
28
- `pi-session-lock core not found (looked in: ${candidates.join(", ")}).\n` +
29
- `Install the plugin at ~/pi-plugins/pi-session-lock or set PI_HUB_SESSION_LOCK_CORE.`,
30
- );
63
+ async function loadPlugin() {
64
+ if (!foundPath) return null;
65
+ let mod;
66
+ try {
67
+ mod = await import(pathToFileURL(foundPath).href);
68
+ } catch (err) {
69
+ console.error(`pi-session-lock(${foundPath})加载失败,降级到内置实现:${err.message}`);
70
+ return null;
71
+ }
72
+ const missing = REQUIRED.filter((name) => typeof mod[name] !== "function");
73
+ if (missing.length) {
74
+ console.error(`pi-session-lock(${foundPath})缺少 ${missing.join(", ")},降级到内置实现`);
75
+ return null;
76
+ }
77
+ return mod;
31
78
  }
32
79
 
33
- const core = await import(pathToFileURL(found).href);
80
+ const plugin = await loadPlugin();
81
+ const impl = plugin ?? fallbackLock;
82
+
83
+ /** 实际使用的锁实现来源:"plugin" | "fallback"(serve banner / doctor / /v1/health 都打它)。 */
84
+ export const lockSource = plugin ? "plugin" : "fallback";
85
+ /** 插件 core.mjs 的绝对路径;降级时为 null(内置实现的路径就是本仓 src/session-lock-fallback.mjs)。 */
86
+ export const corePath = plugin ? foundPath : null;
34
87
 
35
- export const corePath = found;
36
- export const identity = core.identity;
37
- export const lockDirFor = core.lockDirFor;
38
- export const peek = core.peek;
39
- export const claim = core.claim;
40
- export const release = core.release;
41
- export const classify = core.classify;
42
- export const readOwner = core.readOwner;
88
+ export const identity = impl.identity;
89
+ export const lockDirFor = impl.lockDirFor;
90
+ export const peek = impl.peek;
91
+ export const claim = impl.claim;
92
+ export const release = impl.release;
93
+ export const classify = impl.classify;
94
+ export const readOwner = impl.readOwner;
@@ -14,6 +14,24 @@ Environment=PATH=%h/.pi/agent/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/
14
14
  Environment=PI_HUB_PORT=6768
15
15
  # 只监听局域网/校园网地址时可改成具体 IP
16
16
  Environment=PI_HUB_HOST=0.0.0.0
17
+ # 入站来源网段白名单(先于鉴权):默认已是 回环+RFC1918+CGNAT+链路本地,
18
+ # 即绑 0.0.0.0 也只是对私有网/Tailscale 可达,公网可路由来源一律 403。
19
+ # 前面有反代并自己负责 TLS/访问控制时再放开:
20
+ #Environment=PI_HUB_ALLOW_CIDRS=*
21
+ #
22
+ # 有 TLS 的推荐组合(Tailscale Serve 当 HTTPS 入口,hub 只听回环):
23
+ # tailscale serve --bg http://127.0.0.1:6768 # 需先在 tailnet 控制台启用 HTTPS Certificates
24
+ # 然后下面两行同时启用(服务地址改成 https://<节点名>.<tailnet>.ts.net/)
25
+ #Environment=PI_HUB_HOST=127.0.0.1
26
+ #Environment=PI_HUB_ALLOW_CIDRS=127.0.0.0/8,::1/128 # Serve 从本机回环转发进来
27
+ #
28
+ # 还要从**公网**(客户端不装 Tailscale)访问时,把同一入口改成 Funnel,并打开 xff 采信:
29
+ # tailscale funnel --bg http://127.0.0.1:6768 # 公网 HTTPS;关闭:tailscale funnel --https=443 off
30
+ #Environment=PI_HUB_HOST=127.0.0.1
31
+ #Environment=PI_HUB_TRUST_FORWARDED_FOR=1
32
+ #Environment=PI_HUB_PROXY_CIDRS=127.0.0.1/32 # 反代/Funnel 跑在本机就是这个
33
+ # 公网模式下建议同时开 TOTP(PI_HUB_TOTP_SECRET=`,pi-hub totp setup` 生成);
34
+ # 配对批准与一次性码生成仍然只在主机本机可用(带代理头的请求一律 403 local_only)。
17
35
  # 每个 pi 子进程 200-300MB,按机器内存调
18
36
  Environment=PI_HUB_MAX_LIVE=5
19
37
  Environment=PI_HUB_IDLE_MS=600000