mslxdff 0.1.42 → 0.1.45

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/mslxdff.js CHANGED
@@ -8,13 +8,15 @@ import { DEFAULT_PORT, defaultStateFile } from "../src/state.js";
8
8
  import { createRouter } from "../src/routes.js";
9
9
  import { createUpstreamClient } from "../src/upstream.js";
10
10
  import { createModelsService } from "../src/models.js";
11
- import { loadToken, refreshToken, setPort, getPort, loadGroupsJoined, saveGroupsJoined, loadModelErrors } from "../src/state.js";
11
+ import { loadToken, refreshToken, setPort, getPort, loadGroupsJoined, saveGroupsJoined, loadModelErrors, savePreferredModel } from "../src/state.js";
12
+ import { getPreferredModel } from "../src/auto.js";
12
13
  import { startDaemon, stopDaemon, writePid, pidFile, logFile, readPid, readPidVersion, isPidAlive } from "../src/daemon.js";
13
14
  import { createAutoSelector } from "../src/auto.js";
14
15
  import { createPeersService } from "../src/peers.js";
15
16
  import { createEventBus } from "../src/events.js";
16
17
  import { createGroupsService, createBansService, refreshGroupMembers, syncPeersFromMembers } from "../src/groups.js";
17
18
  import { logDir, recentCalls, lastError, appendCall, appendError, appendEvent, recentEvents, eventsFile, callsFile, errorsFile } from "../src/logs.js";
19
+ import { loadPlugins, runHook, pluginsDir, resolvePluginDirs } from "../src/plugins.js";
18
20
 
19
21
  const logs = { appendCall, appendError, appendEvent };
20
22
 
@@ -108,6 +110,28 @@ if (args.includes("-log") || args.includes("--log") || args.includes("-logs") ||
108
110
  process.exit(0);
109
111
  }
110
112
 
113
+ // -plugins: list plugins in the plugins dirs (and their hooks) without starting the daemon
114
+ if (args.includes("-plugins") || args.includes("--plugins")) {
115
+ const pkgRoot = dirname(dirname(fileURLToPath(import.meta.url)));
116
+ const dirs = resolvePluginDirs({ pkgRoot });
117
+ const labels = ["official (bundled)", "user"];
118
+ if (dirs.length === 1) labels[0] = "dir";
119
+ console.log(`plugin dirs:`);
120
+ dirs.forEach((d, i) => console.log(` [${labels[i] || `dir${i + 1}`}] ${d}${existsSync(d) ? "" : " (not created yet)"}`));
121
+ const { plugins, errors } = await loadPlugins({ dirs });
122
+ if (!plugins.length && !errors.length) {
123
+ console.log("(no plugins — drop *.mjs files into a dir above, see docs/plugins.md)");
124
+ }
125
+ for (const p of plugins) {
126
+ const hooks = Object.keys(p.hooks || {});
127
+ const src = p.file.startsWith(pkgRoot) ? "official" : "user";
128
+ console.log(` ${p.name}${p.version ? `@${p.version}` : ""} [${hooks.join(", ") || "no hooks"}] (${src})`);
129
+ if (p.description) console.log(` ${p.description}`);
130
+ }
131
+ for (const e of errors) console.log(` load error: ${e.file} — ${e.error}`);
132
+ process.exit(0);
133
+ }
134
+
111
135
  if (args.includes("-status") || args.includes("--status") || args.includes("-s")) {
112
136
  await printStatus();
113
137
  process.exit(0);
@@ -156,18 +180,24 @@ if (args.includes("-model") || args.includes("-models")) {
156
180
  }
157
181
  process.exit(0);
158
182
  }
183
+ if (sub === "set" && args[idx + 2]) {
184
+ const id = args[idx + 2];
185
+ savePreferredModel(id);
186
+ console.log(`default model set to: ${id} (daemon hot-reloads on next request)`);
187
+ process.exit(0);
188
+ }
159
189
  if (sub !== undefined && sub !== "list") {
160
- console.error("usage: mslxdff -model list | mslxdff -model status | mslxdff -model refresh");
190
+ console.error("usage: mslxdff -models (interactive picker) | mslxdff -model list | mslxdff -model set <id> | mslxdff -model status | mslxdff -model refresh");
161
191
  process.exit(1);
162
192
  }
163
193
  const cacheFile = join(logDir(), "models.json");
164
194
  try {
195
+ let ids = [];
196
+ let cachedAt = null;
165
197
  const cached = readModelsCache(cacheFile);
166
198
  if (cached) {
167
- const ids = (cached.data || []).map((m) => m.id).filter(Boolean);
168
- const at = cached.cachedAt ? ` (cached ${new Date(cached.cachedAt).toISOString().slice(0, 16).replace("T", " ")})` : "";
169
- console.log(`${ids.length} free model(s)${at}:`);
170
- for (const id of ids) console.log(` ${id}`);
199
+ ids = (cached.data || []).map((m) => m.id).filter(Boolean);
200
+ cachedAt = cached.cachedAt || null;
171
201
  } else {
172
202
  const models = createModelsService({
173
203
  baseUrl: process.env.UPSTREAM_BASE_URL || "https://opencode.ai",
@@ -176,10 +206,37 @@ if (args.includes("-model") || args.includes("-models")) {
176
206
  cacheFile,
177
207
  });
178
208
  const list = await models.get();
179
- const ids = (list.data || []).map((m) => m.id).filter(Boolean);
180
- console.log(`${ids.length} free model(s):`);
181
- for (const id of ids) console.log(` ${id}`);
209
+ ids = (list.data || []).map((m) => m.id).filter(Boolean);
182
210
  }
211
+ if (!ids.length) {
212
+ console.log("no models available — try: mslxdff -model refresh");
213
+ process.exit(0);
214
+ }
215
+ // TTY:交互式箭头选择默认模型;非 TTY(管道/脚本):保持纯列表
216
+ if (process.stdin.isTTY && process.stdout.isTTY) {
217
+ const statuses = loadModelErrors();
218
+ const current = getPreferredModel();
219
+ const items = ids.map((id) => {
220
+ const e = statuses[id];
221
+ return {
222
+ id,
223
+ status: typeof e === "number" ? "error" : e?.status || "normal",
224
+ current: id === current,
225
+ };
226
+ });
227
+ const picked = await pickInteractive(items, Math.max(0, items.findIndex((x) => x.current)));
228
+ if (!picked) {
229
+ console.log("cancelled — default model unchanged");
230
+ process.exit(0);
231
+ }
232
+ savePreferredModel(picked);
233
+ console.log(`default model set to: ${picked} (daemon hot-reloads on next request)`);
234
+ process.exit(0);
235
+ }
236
+ const at = cachedAt ? ` (cached ${new Date(cachedAt).toISOString().slice(0, 16).replace("T", " ")})` : "";
237
+ console.log(`${ids.length} free model(s)${at}:`);
238
+ for (const id of ids) console.log(` ${id}`);
239
+ console.log(`\ninteractive pick needs a TTY; set directly with: mslxdff -model set <id>`);
183
240
  } catch (err) {
184
241
  console.error(`could not fetch models: ${String(err?.message || err)}`);
185
242
  process.exit(1);
@@ -187,6 +244,46 @@ if (args.includes("-model") || args.includes("-models")) {
187
244
  process.exit(0);
188
245
  }
189
246
 
247
+ // 交互式选择器:↑/↓ 移动,Enter 确认,q/Esc 取消;ANSI 原地重绘
248
+ async function pickInteractive(items, startCursor = 0) {
249
+ const { renderChooser, renderChooserHelp, parseKey } = await import("../src/chooser.js");
250
+ let cursor = Math.min(Math.max(startCursor, 0), items.length - 1);
251
+ const draw = () => {
252
+ const lines = [...renderChooser(items, cursor), ...renderChooserHelp()];
253
+ process.stdout.write("\x1b[G\x1b[J" + lines.join("\n"));
254
+ };
255
+ draw();
256
+ return new Promise((resolve) => {
257
+ const wasRaw = process.stdin.isRaw;
258
+ process.stdin.setRawMode(true);
259
+ process.stdin.resume();
260
+ process.stdin.setEncoding("utf8");
261
+ const cleanup = () => {
262
+ process.stdin.removeListener("data", onData);
263
+ process.stdin.setRawMode(false);
264
+ process.stdin.pause();
265
+ process.stdout.write("\n");
266
+ };
267
+ const onData = (chunk) => {
268
+ const key = parseKey(String(chunk));
269
+ if (key === "up") {
270
+ cursor = (cursor - 1 + items.length) % items.length;
271
+ draw();
272
+ } else if (key === "down") {
273
+ cursor = (cursor + 1) % items.length;
274
+ draw();
275
+ } else if (key === "enter") {
276
+ cleanup();
277
+ resolve(items[cursor].id);
278
+ } else if (key === "cancel") {
279
+ cleanup();
280
+ resolve(null);
281
+ }
282
+ };
283
+ process.stdin.on("data", onData);
284
+ });
285
+ }
286
+
190
287
  // -debug: stop the background daemon and run the server in THIS terminal
191
288
  // (foreground), printing every event to stdout in real time via the in-memory
192
289
  // event bus — no filesystem polling. Ctrl+C / SIGTERM restarts the daemon in
@@ -648,7 +745,36 @@ if (!process.env.MSLXDFF_DAEMON) {
648
745
  }
649
746
 
650
747
  const { token, created } = await loadToken();
651
- const upstream = createUpstreamClient({});
748
+ // 插件系统:加载(在 upstream 创建前,插件可整体替换上游实现)
749
+ // 双目录:<安装目录>/plugins/(官方插件,随包分发)+ ~/.config/mslxdff/plugins/(用户自定义,升级不丢)
750
+ const pkgRoot = dirname(dirname(fileURLToPath(import.meta.url)));
751
+ const pluginDirs = resolvePluginDirs({ pkgRoot });
752
+ const { plugins: loadedPlugins, errors: pluginErrors } = await loadPlugins({ dirs: pluginDirs });
753
+ for (const e of pluginErrors) {
754
+ console.log(`plugin load failed: ${e.file} — ${e.error}`);
755
+ appendEvent({ ts: Date.now(), type: "plugin-load-error", file: e.file, error: e.error });
756
+ }
757
+ if (loadedPlugins.length) {
758
+ console.log(`plugins loaded (${loadedPlugins.length}): ${loadedPlugins.map((p) => `${p.name}${p.version ? `@${p.version}` : ""}`).join(", ")}`);
759
+ appendEvent({ ts: Date.now(), type: "plugins-loaded", plugins: loadedPlugins.map((p) => ({ name: p.name, version: p.version })) });
760
+ }
761
+ const upstreamHooks = loadedPlugins.length
762
+ ? (name, ctx) => runHook(loadedPlugins, name, ctx)
763
+ : null;
764
+ // 插件可提供 createUpstream(ctx) 整体替换上游(接任意 provider);取第一个声明者
765
+ const providerPlugin = loadedPlugins.find((p) => typeof p.createUpstream === "function");
766
+ let upstream;
767
+ if (providerPlugin) {
768
+ try {
769
+ upstream = await providerPlugin.createUpstream({ baseUrl: process.env.UPSTREAM_BASE_URL || "https://opencode.ai", authToken: process.env.UPSTREAM_AUTH_TOKEN || "public", env: process.env });
770
+ console.log(`upstream provider replaced by plugin: ${providerPlugin.name}`);
771
+ appendEvent({ ts: Date.now(), type: "plugin-upstream-active", plugin: providerPlugin.name });
772
+ } catch (err) {
773
+ console.log(`plugin upstream (${providerPlugin.name}) failed: ${errMsg(err)} — falling back to default`);
774
+ appendEvent({ ts: Date.now(), type: "plugin-upstream-error", plugin: providerPlugin.name, error: errMsg(err) });
775
+ }
776
+ }
777
+ if (!upstream) upstream = createUpstreamClient({ hooks: upstreamHooks });
652
778
  const baseUrl = process.env.UPSTREAM_BASE_URL || "https://opencode.ai";
653
779
  const models = createModelsService({
654
780
  baseUrl,
@@ -673,9 +799,16 @@ const bans = createBansService({ windowMs: banWindowMs(), threshold: banThreshol
673
799
 
674
800
  const isDebug = process.env.MSLXDFF_DEBUG === "1";
675
801
  const bus = createEventBus();
676
- const router = createRouter({ token, upstream, models, auto, logs, peers, maxHops: maxHopsValue(), groups, bans, bus });
802
+ const router = createRouter({ token, upstream, models, auto, logs, peers, maxHops: maxHopsValue(), groups, bans, bus, plugins: loadedPlugins });
677
803
  const listenHost = effectiveHost();
678
- const srv = startServer({ router, signals: !isDebug, host: listenHost });
804
+ const srv = startServer({
805
+ router,
806
+ signals: !isDebug,
807
+ host: listenHost,
808
+ onBeforeClose: loadedPlugins.length
809
+ ? () => runHook(loadedPlugins, "server:stop", { version: VERSION }).then(() => {})
810
+ : undefined,
811
+ });
679
812
 
680
813
  // -debug: push every event straight to this terminal.
681
814
  if (isDebug) {
@@ -703,6 +836,20 @@ if (isDebug) {
703
836
 
704
837
  await srv.ready();
705
838
 
839
+ // 插件 hook:server:start — 服务就绪后触发(只观察)
840
+ if (loadedPlugins.length) {
841
+ runHook(loadedPlugins, "server:start", { port: srv.server.address()?.port, host: listenHost, version: VERSION }).catch(() => {});
842
+ // 插件 onEvent(evt) — 订阅全部事件流(fire-and-forget,错误隔离)
843
+ const eventPlugins = loadedPlugins.filter((p) => typeof p.onEvent === "function");
844
+ if (eventPlugins.length) {
845
+ bus.subscribe((e) => {
846
+ for (const p of eventPlugins) {
847
+ try { p.onEvent(e); } catch {}
848
+ }
849
+ });
850
+ }
851
+ }
852
+
706
853
  // 上游 Keep-Alive 预热:首条 TCP+TLS 暖好,100ms 后异步触发,不阻塞 ready
707
854
  setTimeout(() => {
708
855
  upstream.preheat().then((r) => {
@@ -1018,7 +1165,9 @@ Usage:
1018
1165
  mslxdff -d start as a background daemon
1019
1166
  mslxdff -status show current status (daemon, models, recent calls, last error)
1020
1167
  mslxdff -log [N] show last N events (default 10, e.g. -log 100)
1021
- mslxdff -model list list the free models this proxy serves (cached)
1168
+ mslxdff -models interactive picker: ↑/↓ select a model, Enter sets it as the default (non-TTY: plain list)
1169
+ mslxdff -model list list the free models this proxy serves (cached)
1170
+ mslxdff -model set <id> set the default (preferred) model without the interactive picker
1022
1171
  mslxdff -model status show per-model health status (normal/limit/error)
1023
1172
  mslxdff -model refresh force-refresh the model cache from the upstream
1024
1173
  mslxdff -debug live-follow the daemon event stream (requests, errors, peer forwards)
@@ -0,0 +1,14 @@
1
+ # ADR-0001: Inject a reasoning_content placeholder on outbound assistant messages
2
+
3
+ The Zen upstream's thinking-mode models (deepseek-family at minimum) return
4
+ `400 "The reasoning_content in the thinking mode must be passed back"` when a
5
+ multi-turn request echoes an assistant message without its `reasoning_content`.
6
+ Clients speaking plain OpenAI format never send that field, so the proxy writes
7
+ a `" "` placeholder into assistant messages before forwarding. Scope is `all`
8
+ for deepseek-family models and `tool_calls` for kimi-family models; messages
9
+ that already carry non-empty `reasoning_content` are left untouched.
10
+
11
+ The alternative — telling clients to manage `reasoning_content` themselves —
12
+ would break standard OpenAI-compatible clients, so the proxy eats this
13
+ compatibility cost instead. Matches `/root/9router` v0.5.45
14
+ `open-sse/utils/reasoningContentInjector.js`.
@@ -0,0 +1,12 @@
1
+ # ADR-0002: /v1/models exposes only free models, matched by suffix or whitelist
2
+
3
+ The upstream `/zen/v1/models` list contains ~60 models; exposing them all would
4
+ pollute clients with paid models this proxy can't serve for free. `/v1/models`
5
+ therefore filters to: `id` ending in `-free`, OR the explicit whitelist entry
6
+ `big-pickle`. The whitelist exists because `big-pickle` is a free model without
7
+ the `-free` suffix, and a suffix-only filter would silently drop it.
8
+
9
+ A plain `endsWith("-free")` filter was considered and rejected for exactly that
10
+ reason. Matches `/root/9router` v0.5.45
11
+ `src/app/api/providers/suggested-models/filters.js`
12
+ (`KNOWN_FREE_OPENCODE_MODELS = ["big-pickle"]`).
@@ -0,0 +1,10 @@
1
+ # ADR-0003: Zero-state, no-DB, no-account local proxy
2
+
3
+ Status: partially superseded by [ADR-0004](./0004-bearer-token.md) — the
4
+ no-auth clause below is replaced; the zero-DB / no-account / no-cloud principles
5
+ stand.
6
+
7
+ By design this proxy holds no database, no token store, no account rotation,
8
+ and no cloud sync. A single static bearer token is the only credential, kept
9
+ in a 0600 state file (see ADR-0004); everything else is stateless per-process
10
+ memory at most.
@@ -0,0 +1,18 @@
1
+ # ADR-0004: Single static bearer token, persisted in a 0600 state file
2
+
3
+ The proxy requires a bearer token on `/v1/*` so an accidentally-exposed port
4
+ isn't an open relay. There is no account system: the token is a random
5
+ `crypto` 32-byte value (hex), generated once on first run, persisted to a
6
+ state file (default `~/.config/mslxdff/state.json`, `0600`, path overridable
7
+ via `MSLXDFF_STATE_FILE`), and printed to stdout on creation. Rotate with
8
+ `mslxdff -refresh-token`, which regenerates, rewrites the file, prints the
9
+ new token, and exits (does not start the server).
10
+
11
+ Auth is enforced with a constant-time string compare on
12
+ `Authorization: Bearer <token>`; mismatches get `401` with `WWW-Authenticate`.
13
+ `/health` stays public (no token). Tokens never appear in logs.
14
+
15
+ Alternatives rejected: a fixed default token (same key on every install),
16
+ per-user accounts (needs a DB — that's the 9Router provisioning surface we
17
+ rejected in ADR-0003), and unauthenticated local-only binding (fragile;
18
+ a proxy relay deserves an explicit secret even on localhost).
@@ -0,0 +1,53 @@
1
+ # ADR-0005: Peer mesh for same-model failover across machines
2
+
3
+ > **Update (group model):** peers are now configured through named groups, and the
4
+ > group name doubles as the join password (supersedes the random join key). CLI:
5
+ > `-creategroup <name>` on the leader (no address needed — the first joiner seeds
6
+ > the leader's own entry from the address it connects from), `-addtogroup <leader-host> <name>`
7
+ > elsewhere. Every member (leader included) re-registers with the leader on a timer
8
+ > (`MSLXDFF_GROUP_SYNC_MS`, default 60s) and rebuilds its local peer list from the
9
+ > freshest member map, so membership changes propagate to all nodes automatically.
10
+ > The `-peer` commands are removed; peers are an internal mechanism. Wrong group
11
+ > names/tokens are counted per source IP: `MSLXDFF_BAN_THRESHOLD` (default 5)
12
+ > failures ban the IP for `MSLXDFF_BAN_WINDOW_MS` (default 48h); `-resetban [ip]`
13
+ > clears bans.
14
+
15
+ A single mslxdff instance depends on one upstream quota; when that upstream
16
+ starts rate-limiting or failing for a model, the only local fallback is
17
+ switching to a *different* model. That changes the model out from under the
18
+ client. To keep the requested model working while the local path recovers,
19
+ multiple instances can be joined into a group: when the local upstream
20
+ fails for model X, the instance forwards the request (still model X) to a
21
+ group member that runs its own mslxdff and has its own upstream quota.
22
+
23
+ ## Design
24
+
25
+ - **Peers are plain mslxdff instances.** Each peer is identified by
26
+ `{ url, token }` (its bearer token, per ADR-0004). Configured with
27
+ `mslxdff -peer add <token> <url> [name]`, removed with `-peer remove`,
28
+ listed with `-peer list`; persisted in the state file. No new protocol —
29
+ forwarding is a normal authenticated `POST /v1/chat/completions` to the
30
+ peer, reusing the existing API surface.
31
+ - **Local-first routing.** A request always tries the local upstream first.
32
+ Only on local failure (network error or HTTP ≥ 400) does it iterate peers
33
+ for the *same model*, round-robin over currently-available peers.
34
+ - **Model lock.** Forwarded requests carry `x-mslxdff-model-lock: <model>`
35
+ so the receiving peer uses exactly that model — it must not re-select or
36
+ fall back to another model, keeping "same model, different machine" true.
37
+ - **Hop bound.** Forwarded requests carry `x-mslxdff-hops` (incremented each
38
+ hop). A peer receiving hops ≥ `maxHops` (default 3, `MSLXDFF_MAX_HOPS`)
39
+ stops forwarding further, bounding mesh depth and preventing loops.
40
+ - **Peer cooldown.** A peer that fails a request enters a cooldown window
41
+ (default 30s, `MSLXDFF_PEER_COOLDOWN_MS`), during which it is skipped by
42
+ the round-robin, so the mesh rotates to healthy peers instead of
43
+ hammering a down one. Mirrors the model cooldown in ADR-0001.
44
+
45
+ ## Why not alternatives
46
+
47
+ - **Point the whole proxy at another machine** (client-side failover): the
48
+ client can't detect per-model upstream failure, and every request pays the
49
+ cross-machine latency even when local is healthy.
50
+ - **Different model per machine**: violates the requirement of keeping the
51
+ same model, and hides the model-change from the client.
52
+ - **Central coordinator / service discovery**: overkill for a handful of
53
+ private instances; static peer config is zero-config and debuggable.
@@ -0,0 +1,103 @@
1
+ # ADR-0006: 宽带动态IP成员(broadband)经 Leader 中继共享配额
2
+
3
+ > **状态**:规划完成,待实现(基于 v0.1.33)。用户确认:`--broadband` 为唯一语义,不设 `--relay` 别名;默认 `127.0.0.1` 监听。
4
+
5
+ ## 1. 背景与问题
6
+
7
+ 现有组模型(ADR-0005)假设组员均为公网 VPS,`A->D` 直连 `http://D公网IP:8989/v1/chat/completions` 用 `D` 的出口 IP 打 `opencode.ai`,实现 `IP级免费池` 分散。家庭宽带 `D` 有公网 IP 但:
8
+
9
+ 1. **入站不可达**:无端口映射/CGNAT,`probeHealth GET http://D:8989/health` 恒 `fail`,`peer-race` 直接跳过,`D` 的配额永不被用。
10
+ 2. **IP 动态**:`refreshGroupMembers` 透传旧 `myUrl`,IP 变更后 60s 同步期内全组仍用旧 IP。
11
+ 3. **语义缺失**:组内无法区分 `static(VPS直连)` 与 `broadband(家庭中继)`,`-group list` 无标识。
12
+
13
+ 需求:家庭 `D` 以 `broadband` 类型加入,无需公网入站,经 `Leader` 中继仍用 `D` 的家庭出口打上游,且全程可观测。
14
+
15
+ ## 2. 决策
16
+
17
+ 新增成员类型 `kind: "broadband"`(默认 `kind: "static"` 兼容老数据),`broadband` 隐含 `relay` 中继:
18
+
19
+ * **加入**:`mslxdff -addtogroup <leader-host> <group> --broadband`(唯一旗标,不设 `--relay`)。
20
+ * **监听**:`--broadband` 下默认 `listen 127.0.0.1:8989`,仅本机 `WorkBuddy` 可用;不加该旗标的为 `static` 走 `0.0.0.0`。
21
+ * **共享**:`D --WS--> Leader` 常驻出站,`A(429) -> Leader -> D -> opencode.ai -> D -> Leader -> A`,上游仍见 `D` 家庭 IP。
22
+ * **展示**:`-help` 新增用法行,`-group list / -status / -log` 标注 `[broadband] via leader Xs ago ip=...`。
23
+
24
+ ## 3. 设计
25
+
26
+ ### 3.1 成员模型
27
+
28
+ `state.json groups[name].members[id]` 扩展:
29
+
30
+ ```json
31
+ "home-D": {
32
+ "url": "relay://home-D",
33
+ "token": "...",
34
+ "kind": "broadband",
35
+ "publicIp": "183.14.22.78",
36
+ "lastSeen": 1724212345678,
37
+ "status": {"upstreamOk": true, "latencyMs": 4200}
38
+ }
39
+ ```
40
+
41
+ * `static`:`url: http://IP:8989`,参与 `probeHealth`。
42
+ * `broadband`:`url: relay://id`,不探活,只看 `lastSeen`(`>90s` 标 `cooling`)+ `status`,`rankModels` 同 `slow` 5m 冷却共用。
43
+
44
+ ### 3.2 连接与心跳
45
+
46
+ **D 端(家庭)**
47
+ * 解析 `--broadband`,建 `WS wss://Leader/v1/groups/relay/connect?group=my@mslxd`,`Authorization: Bearer <token>`。
48
+ * `hello {group, token, kind:"broadband", version}` → Leader 回 `welcome {memberId}`。
49
+ * `heartbeat 30s {status:{upstreamOk, models, load}}`,`publicIp` 由 Leader 的 `clientIp(req)` 填,不靠 `ifconfig.me`。
50
+ * 断线指数退避 `1s/2s/4s...` 重连,IP 变更导致 `TCP RST` 自动重建即完成 IP 更新。
51
+
52
+ **Leader 端(VPS)**
53
+ * `GET /v1/groups/relay/connect` 升级 WS,鉴权 `membersForToken`,存 `relayConns[group][id]=ws`,`ws.remoteIp=clientIp`。
54
+ * 每条 `heartbeat` 对比 `remoteIp` vs `members[id].publicIp`,变则 `saveGroups` + `evt relay-ip-change` 入 `events.log`。
55
+ * `lastSeen >90s` 或 `WS断开` 立即标 `cooling`,`A` 下次 `candidatesFor` 自动避开。
56
+
57
+ ### 3.3 转发路径
58
+
59
+ ```
60
+ 用户 -> A: POST /v1/chat/completions {model: deepseek, stream:true}
61
+ A -> opencode.ai (用A IP) 429
62
+ A选 candidatesFor -> [B(static), home-D(broadband via Leader), ...] 按 latency EMA
63
+ A -> Leader: POST /v1/groups/relay/forward {target: home-D, body, hops:1, reqId}
64
+ Leader -> D (WS): {reqId, body:{model,messages}}
65
+ D -> opencode.ai (用D家庭IP) 200 SSE chunk*
66
+ D --WS {reqId, data:chunk}--> Leader --HTTP chunk--> A --SSE--> 用户
67
+ A断开 -> Leader --WS {abort reqId}--> D controller.abort()
68
+ ```
69
+
70
+ * `hops` 每跳 +1,`MAX_HOPS=3` 防环;`broadband` 节点自身 `429` 时可直接 `fetch` 到 VPS `static` 节点(出站直连,无需中继)。
71
+ * 多请求复用一条 WS,`reqId` 复用现有 `relay` 日志的 `reqId/detail` 串联。
72
+
73
+ ### 3.4 可观测
74
+
75
+ * `-help`:` -addtogroup <host> <name> [--broadband] 宽带动态IP成员(经Leader中继,无需公网入站,默认127.0.0.1)`
76
+ * `-group list`:`3. relay://home-D [broadband] ok via leader 12s ago ip=183.14.22.78`
77
+ * `-log [N]`:`relay-ip-change / relay-forward / relay-heartbeat / client-abort` 均带 `reqId/detail`。
78
+
79
+ ## 4. 实现清单
80
+
81
+ | 文件 | 改动 |
82
+ |---|---|
83
+ | `bin/mslxdff.js` | `-addtogroup` 解析 `--broadband`,`WS` 客户端+心跳+重连,`listen` 在 `broadband` 下绑 `127.0.0.1`,`printHelp` 新增行,`groupSyncTimer` 对 `broadband` 组 30s |
84
+ | `src/groups.js` | 成员结构 `kind/publicIp/lastSeen/status`,`addGroupMember/upsertMember/syncPeersFromMembers` 支持 `broadband`,`refreshGroupMembers` 透传 `kind` |
85
+ | `src/peers.js` | 新增 `relayVia` 字段,`add({relayVia, kind}) / isRelay`,`broadband` 不进 `ordered()` 直连池 |
86
+ | `src/routes.js` | 新增 `WS /v1/groups/relay/connect` + `POST /v1/groups/relay/forward`,`forwardToPeer` 中继分支,`hops` 处理 |
87
+ | `src/state.js` | 持久化 `kind/publicIp/lastSeen`,新增 `load/save` 兼容 |
88
+ | `src/logs.js` | `relay-*` 事件类型 |
89
+
90
+ 依赖:`ws`(或 Node 22 原生 `WebSocket`,服务端需 `upgrade` 处理)。
91
+
92
+ ## 5. 验证
93
+
94
+ 1. **IP变更**:`D` 拨号重拨,`WS` 重连,`events.log` 出现 `relay-ip-change old->new`,`-group list` IP 更新,`A` 经 `Leader` 仍命中 `D`。
95
+ 2. **配额共享**:`A` 指定 `deepseek 429`,`-log` 显示 `peer-race via=relay-leader target=home-D`,`D` 的 `upstream-done 200` 且 `detail.exitReason:normal`,回包完整。
96
+ 3. **本地可用**:`D` 本机 `curl http://127.0.0.1:8989/v1/chat/completions` 200,外网 `curl http://家庭公网IP:8989` 超时(符合预期)。
97
+ 4. **`-help` / `-group list`** 均含 `broadband` 标识。
98
+
99
+ ## 6. 风险与取舍
100
+
101
+ * `Leader` 单点中继增加 `10-30ms` 延迟,可接受;`Leader` 宕则 `broadband` 配额暂不可用(`static` 组员仍直连可用)。
102
+ * 多并发复用单 `WS` 需 `reqId` 复用与背压控制,首版可限并发 3(复用 `PEER_RACE_LIMIT`)。
103
+ * 不设 `--relay` 别名,术语统一为 `broadband`,内部变量 `relay` 仅作实现名。
@@ -0,0 +1,51 @@
1
+ # Domain Docs
2
+
3
+ How the engineering skills should consume this repo's domain documentation when exploring the codebase.
4
+
5
+ ## Before exploring, read these
6
+
7
+ - **`CONTEXT.md`** at the repo root, or
8
+ - **`CONTEXT-MAP.md`** at the repo root if it exists — it points at one `CONTEXT.md` per context. Read each one relevant to the topic.
9
+ - **`docs/adr/`** — read ADRs that touch the area you're about to work in. In multi-context repos, also check `src/<context>/docs/adr/` for context-scoped decisions.
10
+
11
+ If any of these files don't exist, **proceed silently**. Don't flag their absence; don't suggest creating them upfront. The `/domain-modeling` skill (reached via `/grill-with-docs` and `/improve-codebase-architecture`) creates them lazily when terms or decisions actually get resolved.
12
+
13
+ ## File structure
14
+
15
+ Single-context repo (most repos):
16
+
17
+ ```
18
+ /
19
+ ├── CONTEXT.md
20
+ ├── docs/adr/
21
+ │ ├── 0001-event-sourced-orders.md
22
+ │ └── 0002-postgres-for-write-model.md
23
+ └── src/
24
+ ```
25
+
26
+ Multi-context repo (presence of `CONTEXT-MAP.md` at the root):
27
+
28
+ ```
29
+ /
30
+ ├── CONTEXT-MAP.md
31
+ ├── docs/adr/ ← system-wide decisions
32
+ └── src/
33
+ ├── ordering/
34
+ │ ├── CONTEXT.md
35
+ │ └── docs/adr/ ← context-specific decisions
36
+ └── billing/
37
+ ├── CONTEXT.md
38
+ └── docs/adr/
39
+ ```
40
+
41
+ ## Use the glossary's vocabulary
42
+
43
+ When your output names a domain concept (in an issue title, a refactor proposal, a hypothesis, a test name), use the term as defined in `CONTEXT.md`. Don't drift to synonyms the glossary explicitly avoids.
44
+
45
+ If the concept you need isn't in the glossary yet, that's a signal — either you're inventing language the project doesn't use (reconsider) or there's a real gap (note it for `/domain-modeling`).
46
+
47
+ ## Flag ADR conflicts
48
+
49
+ If your output contradicts an existing ADR, surface it explicitly rather than silently overriding:
50
+
51
+ > _Contradicts ADR-0007 (event-sourced orders) — but worth reopening because…_
@@ -0,0 +1,30 @@
1
+ # Issue tracker: Local Markdown
2
+
3
+ Issues and specs (you may know a spec as a PRD) for this repo live as markdown files in `.scratch/`.
4
+
5
+ ## Conventions
6
+
7
+ - One feature per directory: `.scratch/<feature-slug>/`
8
+ - The spec is `.scratch/<feature-slug>/spec.md`
9
+ - Implementation issues are one file per ticket at `.scratch/<feature-slug>/issues/<NN>-<slug>.md`, numbered from `01` — never a single combined tickets file
10
+ - Triage state is recorded as a `Status:` line near the top of each issue file (see `triage-labels.md` for the role strings)
11
+ - Comments and conversation history append to the bottom of the file under a `## Comments` heading
12
+
13
+ ## When a skill says "publish to the issue tracker"
14
+
15
+ Create a new file under `.scratch/<feature-slug>/` (creating the directory if needed).
16
+
17
+ ## When a skill says "fetch the relevant ticket"
18
+
19
+ Read the file at the referenced path. The user will normally pass the path or the issue number directly.
20
+
21
+ ## Wayfinding operations
22
+
23
+ Used by `/wayfinder`. The **map** is a file with one **child** file per ticket.
24
+
25
+ - **Map**: `.scratch/<effort>/map.md` — the Notes / Decisions-so-far / Fog body.
26
+ - **Child ticket**: `.scratch/<effort>/issues/NN-<slug>.md`, numbered from `01`, with the question in the body. A `Type:` line records the ticket type (`research`/`prototype`/`grilling`/`task`); a `Status:` line records `claimed`/`resolved`.
27
+ - **Blocking**: a `Blocked by: NN, NN` line near the top. A ticket is unblocked when every file it lists is `resolved`.
28
+ - **Frontier**: scan `.scratch/<effort>/issues/` for files that are open, unblocked, and unclaimed; first by number wins.
29
+ - **Claim**: set `Status: claimed` and save before any work.
30
+ - **Resolve**: append the answer under an `## Answer` heading, set `Status: resolved`, then append a context pointer (gist + link) to the map's Decisions-so-far in `map.md`.
@@ -0,0 +1,15 @@
1
+ # Triage Labels
2
+
3
+ The skills speak in terms of five canonical triage roles. This file maps those roles to the actual label strings used in this repo's issue tracker.
4
+
5
+ | Label in mattpocock/skills | Label in our tracker | Meaning |
6
+ | -------------------------- | -------------------- | ---------------------------------------- |
7
+ | `needs-triage` | `needs-triage` | Maintainer needs to evaluate this issue |
8
+ | `needs-info` | `needs-info` | Waiting on reporter for more information |
9
+ | `ready-for-agent` | `ready-for-agent` | Fully specified, ready for an AFK agent |
10
+ | `ready-for-human` | `ready-for-human` | Requires human implementation |
11
+ | `wontfix` | `wontfix` | Will not be actioned |
12
+
13
+ When a skill mentions a role (e.g. "apply the AFK-ready triage label"), use the corresponding label string from this table.
14
+
15
+ Edit the right-hand column to match whatever vocabulary you actually use.