wezard 3.0.3 → 3.0.4

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 (70) hide show
  1. package/CLAUDE.md +1 -1
  2. package/README.md +271 -193
  3. package/cli/wezard.sh +9 -5
  4. package/config.example.jsonc +26 -17
  5. package/dist/cli/audit.js +42 -25
  6. package/dist/cli/audit.js.map +1 -1
  7. package/dist/cli/ext.js +1 -1
  8. package/dist/cli/ext.js.map +1 -1
  9. package/dist/daemon/approval.js +11 -10
  10. package/dist/daemon/approval.js.map +1 -1
  11. package/dist/daemon/audit.js +12 -0
  12. package/dist/daemon/audit.js.map +1 -1
  13. package/dist/daemon/channel-context.js +2 -2
  14. package/dist/daemon/channel-context.js.map +1 -1
  15. package/dist/daemon/channel-newpost.js +88 -0
  16. package/dist/daemon/channel-newpost.js.map +1 -0
  17. package/dist/daemon/channel-onboarding.js +27 -31
  18. package/dist/daemon/channel-onboarding.js.map +1 -1
  19. package/dist/daemon/channel-posts.js +51 -8
  20. package/dist/daemon/channel-posts.js.map +1 -1
  21. package/dist/daemon/channel-rebind.js +4 -5
  22. package/dist/daemon/channel-rebind.js.map +1 -1
  23. package/dist/daemon/channel.js +74 -35
  24. package/dist/daemon/channel.js.map +1 -1
  25. package/dist/daemon/danger-scrub.js +7 -0
  26. package/dist/daemon/danger-scrub.js.map +1 -1
  27. package/dist/daemon/danger-workspace.js +177 -0
  28. package/dist/daemon/danger-workspace.js.map +1 -0
  29. package/dist/daemon/danger.js +272 -22
  30. package/dist/daemon/danger.js.map +1 -1
  31. package/dist/daemon/handoff.js +27 -10
  32. package/dist/daemon/handoff.js.map +1 -1
  33. package/dist/daemon/inbound.js +26 -5
  34. package/dist/daemon/inbound.js.map +1 -1
  35. package/dist/daemon/index.js +291 -301
  36. package/dist/daemon/index.js.map +1 -1
  37. package/dist/daemon/mirror-bridge.js +34 -5
  38. package/dist/daemon/mirror-bridge.js.map +1 -1
  39. package/dist/daemon/onboarding.js +25 -8
  40. package/dist/daemon/onboarding.js.map +1 -1
  41. package/dist/daemon/room-bind.js +16 -109
  42. package/dist/daemon/room-bind.js.map +1 -1
  43. package/dist/daemon/room-hub.js +39 -6
  44. package/dist/daemon/room-hub.js.map +1 -1
  45. package/dist/daemon/room-watchdog.js +4 -0
  46. package/dist/daemon/room-watchdog.js.map +1 -1
  47. package/dist/daemon/wizard.js +4 -4
  48. package/dist/daemon/wizard.js.map +1 -1
  49. package/dist/mcp/tools.js +8 -16
  50. package/dist/mcp/tools.js.map +1 -1
  51. package/dist/shared/chat-http.js +5 -3
  52. package/dist/shared/chat-http.js.map +1 -1
  53. package/dist/shared/config.js +23 -6
  54. package/dist/shared/config.js.map +1 -1
  55. package/dist/shared/external.js +0 -1
  56. package/dist/shared/external.js.map +1 -1
  57. package/dist/shared/group-members.js +2 -1
  58. package/dist/shared/group-members.js.map +1 -1
  59. package/dist/shared/mention-filter.js +63 -0
  60. package/dist/shared/mention-filter.js.map +1 -0
  61. package/dist/shared/room-view.js +30 -1
  62. package/dist/shared/room-view.js.map +1 -1
  63. package/dist/shared/room.js +10 -3
  64. package/dist/shared/room.js.map +1 -1
  65. package/dist/shared/topic-room.js +23 -0
  66. package/dist/shared/topic-room.js.map +1 -0
  67. package/dist/shared/world.js.map +1 -1
  68. package/hooks/pre-tool-use.sh +2 -0
  69. package/package.json +1 -1
  70. package/web/chat.js +218 -31
@@ -30,9 +30,11 @@ import { applyHot, patchedTop, pinChatKeys, configGet, planSet, renderPlan, tier
30
30
  import { computeUsage, costOf, fmtCost, fmtTokens, sumTotal } from "./usage.js";
31
31
  import { humanWakeTail, wakeInjectOpts, wakeTailPorts } from "./wake-tail.js";
32
32
  import { installInboundRouter } from "./inbound.js";
33
- import { installChannelTransport, installChannelEvents, readChannel, isTopicChat, channelOutboundOf, sendTopicPost, sendTopicReply, sendTopicReplyOrPost } from "./channel.js";
34
- import { channelOf, channelRefOf, learnChannelRoom, postRefOf, postsOfChannel } from "./channel-posts.js";
35
- import { channelMonitorLeaveOps, channelMonitorWanted, channelRoomOf, channelGuideJoinOps, hostJoinOps, planChannelGuideTurn, postRoomBirthOps } from "./channel-onboarding.js";
33
+ import { installChannelTransport, installChannelEvents, readChannel, isTopicChat, channelOutboundOf, sendTopicPost, sendTopicPostRaw, sendTopicReply, sendTopicReplyOrPost } from "./channel.js";
34
+ import { channelNewPost, createSelfPostLog, newPostSeatOps, newPostTopicOfRef } from "./channel-newpost.js";
35
+ import { channelNameOf, channelNameOfBase, channelOf, channelRefOf, knownChannels, postRefOf, postsOfChannel, topicRoomOf, topicRoomsOf } from "./channel-posts.js";
36
+ import { isTopicRoomId, parseTopicRoomId, topicRoomLabel } from "../shared/topic-room.js";
37
+ import { channelKeyOf, channelMonitorLeaveOps, channelMonitorWanted, channelGuideJoinOps, hostJoinOps, planChannelGuideTurn, topicRoomBirthOps } from "./channel-onboarding.js";
36
38
  import { rebindChannel } from "./channel-rebind.js";
37
39
  import { loadMirrorStore } from "./mirror-store.js";
38
40
  import { startMirror, installMirrorEventListener } from "./mirror-bridge.js";
@@ -48,10 +50,10 @@ import { makeClaimStartHandler, makeClaimStatusHandler, makeClaimResetHandler, }
48
50
  import { errText, installResponseTracker } from "./last-response.js";
49
51
  import { BindGateClosed, openRoomStore, resolveHashRef } from "./rooms.js";
50
52
  import { openShortIdLog } from "./short-id-log.js";
51
- import { bindCandidates, planBind, planChannelBackfill, planFork, planUnbind, pendingFirstTouches, sourceRoomOf } from "./room-bind.js";
53
+ import { bindCandidates, planBind, planUnbind } from "./room-bind.js";
52
54
  import { stewardRoute as resolveSteward } from "./steward.js";
53
55
  import { repairStewardInstances, unspawnedStewards } from "./steward-repair.js";
54
- import { createRoomWatchdog, parsePaneIds } from "./room-watchdog.js";
56
+ import { createRoomWatchdog, parsePaneIds, watchedPaneOf } from "./room-watchdog.js";
55
57
  import { pruneLeaveOps, selectRoomPrune } from "./room-prune.js";
56
58
  import { createOverdueScan } from "./room-overdue.js";
57
59
  import { createRoomHub } from "./room-hub.js";
@@ -88,11 +90,11 @@ import { registerPlanRoutes } from "./plan-http.js";
88
90
  import { renderReminder } from "../shared/reminder.js";
89
91
  import { busyOf, createHandoffs, handedOff, handingOff } from "./handoff.js";
90
92
  import { coldGateOf, wakeCostOf, decide, occupies, rankCandidates, routeDiagnostics, renderCandidates, tierFor } from "./route.js";
91
- import { parseWhen, planHasOpenDuty, renderRoomRead } from "../shared/room-view.js";
93
+ import { parseWhen, planHasOpenDuty, renderRoomRead, roomKindOf } from "../shared/room-view.js";
92
94
  import { memberLabel } from "../shared/room-label.js";
93
95
  import { roomFacts, nameTable } from "./room-facts.js";
94
96
  import { waitForIdle } from "./wait-idle.js";
95
- import { applyOnboarding, bindSteward, GUIDE_DESC, guideHandoffOps, guideOf, isGuideManifestation, judgeOnboarding, loadOnboardingStore, needsOnboarding, planOnboardBind, renderGuideOpening, renderGuideRetry, renderOnboardingDone, rewriteStewardBindings } from "./onboarding.js";
97
+ import { applyOnboarding, bindSteward, GUIDE_DESC, guideHandoffOps, guideOf, isGuideManifestation, judgeOnboarding, loadOnboardingStore, needsOnboarding, planOnboardBind, renderGuideOpening, renderGuideRetry, renderOnboardingDone, rewriteStewardBindings, setChannelName } from "./onboarding.js";
96
98
  import { reminderToken, startPlanReminders } from "./plan-reminders.js";
97
99
  const retiredOrchestrationField = (body) => {
98
100
  if (!body || typeof body !== "object" || Array.isArray(body))
@@ -129,6 +131,9 @@ const main = async () => {
129
131
  if (stale.length)
130
132
  console.warn("[wezard] 配置里还有已废弃的 chatPolicy 旧键 (room 模型已移除, 忽略): " + stale.join(", "));
131
133
  }
134
+ // 频道改造 (docs/evolve/channel-topic-rooms.md): rooms.binding.decideTimeoutMin 已随首触接待删除 —— 同样忽略不拒启动, warn 一次。
135
+ if ("decideTimeoutMin" in (cfg.rooms?.binding ?? {}))
136
+ console.warn("[wezard] 配置里还有已废弃的 rooms.binding.decideTimeoutMin (频道帖子每帖一个 room, 没有首触判定, 忽略)");
132
137
  const log = makeLogger({
133
138
  logFile: cfg.daemon.logFile,
134
139
  logLevel: cfg.daemon.logLevel,
@@ -139,12 +144,13 @@ const main = async () => {
139
144
  // wizards / m / notifyChat), 但 startScheduler 在那块外面接线 —— 见文件尾。
140
145
  // 记忆整理者的执行体, 同样只有 mirror 模式给得出 —— 赋值见 scheduledTaskInject 旁边。
141
146
  let stopPlanReminders = () => { };
142
- // 频道入站的三条接线 (room 块在下面才立起来, 先留引用): 人话落 room / 频道首次见到 / 帖子源首触
143
- // (只记事, 不建 room —— 见 docs/room/multi-source-binding.md §4.1)。
147
+ // 频道入站的两条接线 (room 块在下面才立起来, 先留引用): 人话落 room / (频道, 帖子, opk?) → 落哪个 topic room
148
+ // (docs/evolve/channel-topic-rooms.md §3.1)。频道自己没有 room, 「首次见到」只记已知频道名单 (channel.ts)。
144
149
  let channelIngest;
145
- let channelRoomSeen;
146
- let channelPostTouched;
147
- /** 入站源解析 (§4.1): 源已绑 / 帖子源未诞生时, 这句话落哪本账、谁是接待人。恒等路径 = undefined。 */
150
+ let channelRoomFor;
151
+ /** 自己刚发的新帖 post_id (最近 N 个): channel.ts 的 topic_event 命中就不当人话落行 (!asker 之外的双保险)。 */
152
+ const selfPosts = createSelfPostLog();
153
+ /** 入站源解析 (§4.1): 群源已绑 / 频道帖子 (原生 @ 帧) 这句话落哪本账、谁是接待人。恒等路径 = undefined。 */
148
154
  let sourceRouteHook;
149
155
  let stewardRun;
150
156
  let stewardWiring = {};
@@ -169,8 +175,35 @@ const main = async () => {
169
175
  // Restore auto-approve windows persisted across daemon restarts —
170
176
  // otherwise a `wezard reload` silently drops the user's "10min" choice.
171
177
  initAutoWindowPersistence(cfg.daemon.stateDir);
172
- // rolepage 的频道 / 帖子 kind 与归属读时算 (channel-posts), 不落账本。
173
- setChannelRef((room) => channelRefOf(cfg, room));
178
+ // rolepage 的频道 / 帖子 kind 与归属读时算 (channel-posts), 不落账本; host = 频道绑的身份 (wrc.channel.steward → 类)。
179
+ // 各 room 现任 host 的身份 (账本里 ord 最小的 wizard → 它的类); roomStore 在后面才建, 建好后接线。
180
+ let roomHostsOfChannel = () => [];
181
+ const hostOfChannel = (topic) => {
182
+ const store = wizardStore();
183
+ const named = (identity, fallback) => ({ identity, name: store?.classes?.get(identity)?.name ?? fallback });
184
+ // ① 频道表绑定 (类名 → 类; 不是类就是普通身份名)。
185
+ const bound = ((cfg.wrc.channel.steward ?? {})[topic] ?? "").trim();
186
+ if (bound) {
187
+ const cls = store?.classes?.byName(bound);
188
+ return { identity: cls?.id ?? bound, name: cls?.name ?? bound };
189
+ }
190
+ // ② 接待口径的有效身份 (chatPolicy 老绑定 / 频道键的默认会话) —— 与实际接待同一判据, 框上不再写「未绑」而 room 里明明有人接待。
191
+ const eff = stewardIdentityOf(cfg, store, "chat:" + topic);
192
+ if (eff)
193
+ return named(eff, store?.all().find((w) => identityOfRec(w) === eff)?.name ?? eff);
194
+ // ③ 表空、接待也没有: 看各 room 现任 host 的类 (只在这里推断 —— 表一写上就以表为准, 不会随 host 换人闪烁); 取最多的那个。
195
+ const tally = new Map();
196
+ for (const h of roomHostsOfChannel(topic))
197
+ tally.set(h.identity, { ...h, n: (tally.get(h.identity)?.n ?? 0) + 1 });
198
+ const top = [...tally.values()].sort((a, b) => b.n - a.n || a.identity.localeCompare(b.identity))[0];
199
+ return top ? { identity: top.identity, name: top.name } : undefined;
200
+ };
201
+ const channelRefWithHost = (room) => {
202
+ const r = channelRefOf(cfg, room);
203
+ const host = r ? hostOfChannel(r.channel) : undefined;
204
+ return r ? { ...r, ...(host ? { host } : {}) } : undefined;
205
+ };
206
+ setChannelRef(channelRefWithHost);
174
207
  // Replay tool/approval detail records so reload doesn't lose click-to-detail
175
208
  // links from messages already on the user's WeCom timeline.
176
209
  initDetailPersistence(cfg.daemon.stateDir, log.child({ mod: "detail" }));
@@ -332,6 +365,7 @@ const main = async () => {
332
365
  }
333
366
  } }, sourcePath, (w, c) => guardCharter(w, c), { onboarding,
334
367
  sourceRoute: (base) => sourceRouteHook?.(base),
368
+ channelRoom: (base) => { const r = postRefOf(cfg, base); return r?.post ? channelRoomFor?.(r.channel, r.post, base) : undefined; },
335
369
  guideInbound: (a) => (guideInboundHook ? guideInboundHook(a) : Promise.resolve({ ok: false, reason: "room 接线还没就绪" })),
336
370
  guideSettle: (base, bound) => (guideSettleHook ? guideSettleHook(base, bound) : false),
337
371
  getNotice: (base) => (getNoticeHook ? getNoticeHook(base) : undefined),
@@ -339,9 +373,9 @@ const main = async () => {
339
373
  });
340
374
  // 频道(topic)事件层:接在 inbound 之后,让频道消息复用同一套 gate / route / mirror。
341
375
  installChannelEvents(ws.client, cfg, log.child({ mod: "channel" }), {
376
+ selfPost: (p) => selfPosts.has(p),
342
377
  ingest: (e) => (channelIngest ? channelIngest(e) : log.warn({ room: e.room, msgid: e.msgid }, "channel: room wiring not ready, 人话没落账本")),
343
- channelSeen: (t) => channelRoomSeen?.(t),
344
- postTouched: (t, b) => channelPostTouched?.(t, b),
378
+ roomFor: (t, p, opk) => (channelRoomFor ? channelRoomFor(t, p, opk) : topicRoomOf({ cfg, topic: t, post: p, ...(opk ? { opk } : {}), roomOfSource: () => undefined, roomExists: () => false })),
345
379
  });
346
380
  // approval click → finalize 当前 liveStream, 后续 tool/text 落到 standalone。
347
381
  // 规则 2: 用户点击授权那一刻就是"上一段对话"的边界, 截断 stream 让授权后的
@@ -601,8 +635,10 @@ const main = async () => {
601
635
  : { ok: false, reason: r.reason, target, runningCwd: set.runningCwd, pendingCwd: set.pendingCwd });
602
636
  });
603
637
  // 频道换绑 (人 2026-11-23: 「绑定工作区为 cc-wecom」): 频道 = 一条类绑定; 换绑 = 建 / 复用
604
- // 该目录的管家类 + 写 wrc.channel.steward + 频道 room 与各帖子 room 坐上新类的**本 room**
638
+ // 该目录的管家类 + 写 wrc.channel.steward + 该频道每个 topic room 坐上新类的**本 room**
605
639
  // 实例 (旧 host 不离开: leave 会把它手上没交代的件全 canceled)。只动这一个频道。
640
+ // 2026-11-27: 频道自己没有 room —— 不再要求频道账本存在, 也不再往频道里留一条系统行 (每帖一行
641
+ // 会变成每个帖子下的评论); 谁换的、换成什么在 charterGuard 的通知与日志里。
606
642
  http.register("POST /mirror/channel/rebind", async (req, res) => {
607
643
  const body = (await readBody(req));
608
644
  const self = resolveSelf(body);
@@ -611,14 +647,10 @@ const main = async () => {
611
647
  return;
612
648
  }
613
649
  const topicId = (body.topicId ?? "").trim();
614
- // 权限: 频道管家表本来就对 wizard 可写 (config_set 那条), 这里不另设身份闸 —— 只要求
615
- // 频道已有账本 (认得出的频道才谈得上换绑), 并把这次改动写进频道 room (谁换的, 换成什么)。
616
- if (!roomState("chat:" + topicId)) {
617
- json(res, 404, { ok: false, reason: "这个频道还没有 room 账本: " + topicId });
618
- return;
619
- }
650
+ // 权限: 频道管家表本来就对 wizard 可写 (config_set 那条), 这里不另设身份闸 —— rebindChannel
651
+ // 自己要求这是个已认出的频道 (见过它的事件 / 管家表里有它)。
620
652
  const { r: out, note } = guardCharter(() => rebindChannel({
621
- cfg, sourcePath, store: wizards, stateOf: roomState,
653
+ cfg, sourcePath, store: wizards, stateOf: roomState, roomIds: roomStore.ids,
622
654
  write: (room, ops) => { hub.write(room, ops); },
623
655
  topicId, workspace: (body.workspace ?? "").toString(), ...(body.name ? { name: body.name.toString() } : {}),
624
656
  }), (r) => r.ok ? { what: "频道 " + r.topicId + " 换绑到 ." + r.className, self, byHuman: true } : undefined);
@@ -626,21 +658,7 @@ const main = async () => {
626
658
  json(res, 400, out);
627
659
  return;
628
660
  }
629
- // 频道级事实留一条系统行 (不唤醒谁、不开 duty): 人一眼看到 host 换成了谁。
630
- const ownRoom = "chat:" + out.topicId;
631
- const ownSeat = out.rooms.find((f) => f.room === ownRoom)?.seat ?? "";
632
- const said = hub.append(ownRoom, { author: DAEMON, kind: "system", origin: "daemon", sys: "rebind",
633
- text: "[wezard] 本频道的管家已换绑为 ." + out.className + (ownSeat ? " (本 room 的实例 ." + displayName(ownSeat) + ")" : "")
634
- + " —— 各 room 的 host 换成本 room 的新实例; 旧 host 仍留在群里, 只是不再是 host" });
635
- if (!said.ok)
636
- roomLog.warn({ room: ownRoom, reason: said.reason }, "channel: rebind 系统行被拒");
637
- // §7 换绑后同钩子补拉: 帖子 host 实例本来就是本 room 的实例, 这里只保证它们在频道成员表里
638
- // (换绑不动它们; 幂等)。
639
- backfillChannelMembers();
640
- // 频道 room 的管家会话先起 (与频道落定同一口子); 帖子 room 的实例等第一次唤醒再冷启动。
641
- const own = out.rooms.find((f) => f.room === "chat:" + out.topicId);
642
- if (own?.seat)
643
- spawnStewardPane(own.seat);
661
+ // 帖子 room 的实例只落记录、不预起 pane: 第一次被唤醒时泵会冷启动它 (每帖一个常驻 pane 会顶到上限)。
644
662
  roomLog.info({ ...out }, "channel: rebind");
645
663
  json(res, 200, { ...out, charter: await note });
646
664
  });
@@ -743,7 +761,7 @@ const main = async () => {
743
761
  };
744
762
  /** 以 `self` 的名义往 `dest` 群发一条: 头独占一行; 头怎么写 (群默认 wizard 报不报名) 归 ownHead。 */
745
763
  const postAs = (self, dest, text) => notifyChat(dest, headed(ownHead(self), text, "\n\n"));
746
- /** 同 postAs, 但去到的是频道 room (chat:<topic_id>) —— 一律发新帖 (send_post)。
764
+ /** 同 postAs, 但去到的是存档频道 room (chat:<topic_id>) —— 一律发新帖 (send_post)。
747
765
  * 频道 / 帖子直连 send_post / send_reply, 绕过 tracker, 署名收尾 (withBubbleFooter) 得自己补。 */
748
766
  const postAsTopic = (self, dest, text, media) => {
749
767
  const topicId = dest.replace(/^chat:/, "");
@@ -759,7 +777,7 @@ const main = async () => {
759
777
  return "";
760
778
  return "> 回 " + roomNameOf(src.author) + ": " + text.slice(0, 80) + (text.length > 80 ? "…" : "") + "\n\n";
761
779
  };
762
- /** 频道 room 的回话: 评论回原帖; 被企微拒 (没 @ 过 bot 的帖子) 回退发新帖, 正文带引用标注。 */
780
+ /** 存档频道 room 的回话: 评论回原帖 (re 到带 post 的存量行); 被企微拒回退发新帖, 正文带引用标注。 */
763
781
  const postAsChannelReply = (self, dest, post, text, quote, media) => {
764
782
  const topicId = dest.replace(/^chat:/, "");
765
783
  const full = linkTags(dest, withBubbleFooter(headed(ownHead(self), text, "\n\n"), self));
@@ -1270,6 +1288,16 @@ const main = async () => {
1270
1288
  const roomsDir = roomsDirOf(cfg.daemon.stateDir);
1271
1289
  const roomStore = openRoomStore(roomsDir);
1272
1290
  roomOfHashHook = (h) => roomStore.roomByHash(h);
1291
+ roomHostsOfChannel = (topic) => [...new Set([...topicRoomsOf(cfg, topic, roomStore.ids()), ...postsOfChannel(cfg, topic).filter((b) => roomStore.ids().includes(b))])]
1292
+ .flatMap((r) => {
1293
+ const s = roomState(r);
1294
+ const h = s ? roomHost(s) : undefined;
1295
+ const w = h?.kind === "wizard" ? wizards.byUid(h.id) : undefined;
1296
+ if (!w)
1297
+ return [];
1298
+ const identity = identityOfRec(w);
1299
+ return [{ identity, name: wizardStore()?.classes?.get(identity)?.name ?? w.name }];
1300
+ });
1273
1301
  /** 损坏的 room 文件 (RoomBroken) 不挡 boot, 也不参与任何判定 —— 只记一行。 */
1274
1302
  const roomState = (id) => {
1275
1303
  try {
@@ -1369,6 +1397,10 @@ const main = async () => {
1369
1397
  }
1370
1398
  if (room.startsWith("adhoc:"))
1371
1399
  return memberLabel([...(s?.members.values() ?? [])].map((x) => roomNameOf(x.who))) || room;
1400
+ // topic room: 「频道名 · 帖 xxxxxx」(频道名沿用 chat-name 键 chat:<topic_id>; 没起名用 id 前 8 位)。
1401
+ const tr = parseTopicRoomId(room);
1402
+ if (tr)
1403
+ return topicRoomLabel(tr, channelNameOf(cfg, tr.channel));
1372
1404
  return chatNameOf(cfg, room) || room;
1373
1405
  };
1374
1406
  /** 频道 room 判据: 进程内见过的 topic (事件登记) **或** channel-posts 认识且没有根帖的频道。
@@ -1899,7 +1931,8 @@ const main = async () => {
1899
1931
  };
1900
1932
  /** 引导化身的发言只许落在本群 room (白名单里的 chat_say / chat_reply)。 */
1901
1933
  const guideOutsideRoom = (res, self, room) => {
1902
- if (!isGuideManifestation(wizards.get(self)) || room === baseOfKey(self))
1934
+ // 引导化身 chat:<topic_id>#guide 的「本群」= 该频道的 topic room (2026-11-27: 频道没有 room)。
1935
+ if (!isGuideManifestation(wizards.get(self)) || room === baseOfKey(self) || channelKeyOf(cfg, room) === baseOfKey(self))
1903
1936
  return false;
1904
1937
  json(res, 403, { ok: false, reason: "引导会话只在 " + baseOfKey(self) + " 里说话" });
1905
1938
  return true;
@@ -1948,6 +1981,50 @@ const main = async () => {
1948
1981
  json(res, 400, { ok: false, reason: to.error });
1949
1982
  return;
1950
1983
  }
1984
+ // 频道 id 是「开新帖」的虚地址 (频道自己没有 room; 旧频道 room 只读存档): 先发帖, 再开 topic room 记这句。
1985
+ // 全 id `chat:<T>` 与 #hash8 / 聊天名指到存档频道 room 同一口径 (后者经 roomResolve 还原成 id)。
1986
+ const channelOfRef = (base) => channelOf(cfg, base);
1987
+ const newPostTopic = newPostTopicOfRef(b.room, (ref) => roomResolve(ref).room, channelOfRef);
1988
+ if (newPostTopic) {
1989
+ const archive = "chat:" + newPostTopic;
1990
+ if (refuseReception(res, "POST /room/say", self, { rooms: [archive] }))
1991
+ return;
1992
+ // 硬门由 channelNewPost 在 send_post **之前**判 (先发后判 = 外发收不回): monitor / 引导化身一律拒;
1993
+ // 其余须是频道 host 类实例或存档 room 成员。
1994
+ const rec = wizards.get(self);
1995
+ const hostIdentity = stewardIdentityOf(cfg, wizardStore(), archive) ?? "";
1996
+ const out = await channelNewPost(newPostTopic, {
1997
+ caller: {
1998
+ monitor: rec?.kind === "monitor",
1999
+ guide: isGuideManifestation(rec),
2000
+ hostClass: !!rec && !!hostIdentity && identityOfRec(rec) === hostIdentity,
2001
+ archiveMember: !!roomState(archive)?.members.has(whoKey(me)),
2002
+ },
2003
+ send: () => sendTopicPostRaw(ws.client, log.child({ mod: "channel" }), newPostTopic, linkTags(archive, withBubbleFooter(headed(ownHead(self), text, "\n\n"), self))),
2004
+ remember: (post) => selfPosts.add(post),
2005
+ ensureRoom: (room) => {
2006
+ // founder = 频道 host 身份在**本 room**的实例 (与人话诞生的帖子 room 同口径); 频道没绑才退成调用方。
2007
+ // 调用方作为普通成员入座, 账本行仍记它为作者。
2008
+ const s0 = roomState(room);
2009
+ const ops = newPostSeatOps(s0, room, s0 ? undefined : roomStewardOf(room, archive), me, Date.now());
2010
+ if (ops.length)
2011
+ hub.write(room, ops);
2012
+ seatChannelRoom(room, archive); // 兜底: 频道 host 类的本 room 实例 (已绑才有; 只落记录不起 pane)
2013
+ },
2014
+ record: (room) => hub.append(room, { author: me, kind: "say", text, origin: "tool" }, {}, { noMirror: true }),
2015
+ logShape: (o) => roomLog.info(o, "channel: send_post 回包形状 (确认 post_id 字段用)"),
2016
+ });
2017
+ if (!out.ok) {
2018
+ json(res, out.status, { ok: false, reason: out.reason });
2019
+ return;
2020
+ }
2021
+ if (!("room" in out)) {
2022
+ json(res, 200, { ok: true, warning: out.warning });
2023
+ return;
2024
+ }
2025
+ roomReply(res, out.verdict, { post: out.post, ...(out.warning ? { warning: out.warning } : {}) });
2026
+ return;
2027
+ }
1951
2028
  const target = roomTargetOf(me, to?.who, b.room);
1952
2029
  if (target.error || !target.room) {
1953
2030
  json(res, target.status ?? 400, { ok: false, reason: target.error ?? target.reason });
@@ -2218,8 +2295,10 @@ const main = async () => {
2218
2295
  return;
2219
2296
  }
2220
2297
  const name = (b.name ?? "").toString().trim().replace(/\s+/g, " ");
2298
+ // name 给了 (哪怕是空串) 就算「给了」: 空串 = 清掉 title, 回落到频道名 / 首条人话缩略 / 聊天名。
2299
+ const nameGiven = b.name !== undefined && b.name !== null;
2221
2300
  const noticeGiven = b.notice !== undefined && b.notice !== null;
2222
- if (!name && !noticeGiven) {
2301
+ if (!nameGiven && !noticeGiven) {
2223
2302
  json(res, 400, { ok: false, reason: "name 与 notice 至少给一个" });
2224
2303
  return;
2225
2304
  }
@@ -2233,9 +2312,9 @@ const main = async () => {
2233
2312
  json(res, nt.status, { ok: false, reason: nt.reason });
2234
2313
  return;
2235
2314
  }
2236
- if (name)
2315
+ if (nameGiven && (name || s.title))
2237
2316
  hub.write(r.room, [{ op: "name", at: Date.now(), room: r.room, title: name, by: me }]);
2238
- json(res, 200, { ok: true, room: r.room, ...(name ? { name } : {}), ...(nt?.ok ? { notice: nt.notice, changed: nt.changed } : {}) });
2317
+ json(res, 200, { ok: true, room: r.room, ...(nameGiven ? { name, ...(name ? {} : { cleared: true }) } : {}), ...(nt?.ok ? { notice: nt.notice, changed: nt.changed } : {}) });
2239
2318
  });
2240
2319
  http.register("POST /room/invite", async (req, res) => {
2241
2320
  const { self, body } = await readPeerBody(req);
@@ -2706,50 +2785,7 @@ const main = async () => {
2706
2785
  }
2707
2786
  roomLog.info({ room, source, by: me.id }, "room: 源已解绑 (回落恒等, lazy)");
2708
2787
  json(res, 200, { ok: true, room, source,
2709
- text: "已解绑 " + source + ": 它的消息回落恒等 (自己的 room / 频道接待处); 帖子源下一条消息时才会按需诞生帖子 room (lazy)。" });
2710
- });
2711
- // §5.1 出口 3 的显式入口 (room_fork): host 判定「这该是新 room」时立即另起, 不必等超时兜底。
2712
- // 权限 = 该频道 room 的 host; 幂等判据与兜底同一套 (已绑 / 帖子 room 已在 / 引导期都不动)。
2713
- http.register("POST /room/fork", async (req, res) => {
2714
- const { self, body } = await readPeerBody(req);
2715
- if (READ_ROOM_FAIL(res, self))
2716
- return;
2717
- if (refuseMonitor(res, self, "room_fork"))
2718
- return;
2719
- if (refuseReception(res, "POST /room/fork", self))
2720
- return;
2721
- const me = roomSelfOf(self);
2722
- if (!me) {
2723
- json(res, 400, { ok: false, reason: "caller 不在名册上, 没有 uid" });
2724
- return;
2725
- }
2726
- const b = body;
2727
- const target = roomTargetOf(me, undefined, b.room);
2728
- if (target.error || !target.room) {
2729
- json(res, target.status ?? 400, { ok: false, reason: target.error ?? target.reason ?? "room required" });
2730
- return;
2731
- }
2732
- const source = (b.source ?? "").toString().trim() || latestSrcOf(target.room);
2733
- const v = planFork({ cfg, source, actor: me, stateOf: roomState, roomOfSource: (s) => roomStore.roomOfSource(s) });
2734
- if (!v.ok) {
2735
- json(res, v.status, { ok: false, reason: v.reason });
2736
- return;
2737
- }
2738
- let born = false;
2739
- try {
2740
- born = birthPostRoom(v.topicId, source, me);
2741
- }
2742
- catch (e) {
2743
- json(res, 500, { ok: false, reason: errText(e) });
2744
- return;
2745
- }
2746
- if (!born) {
2747
- json(res, 409, { ok: false, reason: "没能另起 (频道还在引导期 / 帖子 room 已在) —— 先完成频道绑定" });
2748
- return;
2749
- }
2750
- roomLog.info({ channel: v.channelRoom, post: source, by: me.id }, "room: 显式另起 (room_fork)");
2751
- json(res, 200, { ok: true, room: source, channel: v.channelRoom,
2752
- text: "已另起帖子 room " + source + ": 这个帖子之后的话落它自己的 room, 新 host 已作为普通成员拉进频道 room (频道 host 身份不变)。" });
2788
+ text: "已解绑 " + source + ": 它的消息回落恒等 —— 群源落回它自己的群 room; 频道帖子源下一条消息起按 (频道, 帖子) 落 `topic:<频道>/<帖子>` room (没有就在那一刻诞生, 由该频道 host 身份在本 room 的实例接待)。" });
2753
2789
  });
2754
2790
  // ── 新群引导的受控绑定 (onboard_bind): 只有引导化身能调 ────────────────
2755
2791
  // 引导的唯一动作面: 校验 (create 只建最后一级 / 父目录必须已在 / 必须在 $HOME 下) 与落盘都在
@@ -2771,10 +2807,12 @@ const main = async () => {
2771
2807
  return;
2772
2808
  }
2773
2809
  const { verdict } = plan;
2774
- const bound = applyOnboarding(wizards, onboarding, base, verdict, (name) => guardCharter(() => bindSteward(cfg, sourcePath, base, name), () => ({ what: verdict.kind === "bind-new" ? "新群引导新建管家 " + name + " (工作区 " + verdict.workspace + ") 并绑到 " + base : "新群引导把 " + name + " 绑为 " + base + " 的管家", byHuman: true })), (t) => identityWorkspaceOf(wizards, t) || ((c) => c.runningCwd || c.defaultCwd)(m.getCwd(t)), plan.name);
2810
+ const bound = applyOnboarding(wizards, onboarding, base, verdict, (name) => guardCharter(() => bindSteward(cfg, sourcePath, base, name), () => ({ what: verdict.kind === "bind-new" ? "新群引导新建管家 " + name + " (工作区 " + verdict.workspace + ") 并绑到 " + base : "新群引导把 " + name + " 绑为 " + base + " 的管家", byHuman: true })), (t) => identityWorkspaceOf(wizards, t) || ((c) => c.runningCwd || c.defaultCwd)(m.getCwd(t)), plan.name,
2811
+ // 频道: 座位 = 引导所在的 topic room (频道键不是 room); 群 = base 本身。
2812
+ ((g) => (g ? guideRoomsOf(base, g).find((r) => !!postRefOf(cfg, r)?.post) : undefined))(roomSelfOf(self)) ?? base);
2775
2813
  roomLog.info({ room: base, ...bound, roomName: plan.roomName ?? "" }, "onboarding: steward bound (guide tool)");
2776
2814
  const handed = guideSettleRoom(base, bound, plan.roomName);
2777
- settleBoundChannel(base, bound);
2815
+ settleBoundChannel(base);
2778
2816
  if (!handed)
2779
2817
  notifyChat(base, renderOnboardingDone(bound)); // 引导不在 room 里 (老流程): 至少回报一句
2780
2818
  void reapGuideAfterTurn(self); // 先回成功: 这一轮的话还要说完, 收尸在轮终之后 (或 60s)
@@ -2865,7 +2903,7 @@ const main = async () => {
2865
2903
  if (roomState(room))
2866
2904
  return room;
2867
2905
  const at = Date.now();
2868
- const kind = room.startsWith("user:") ? "wecom-dm" : room.startsWith("chat:wx") ? "wx" : "wecom-group";
2906
+ const kind = isTopicRoomId(room) ? "topic-post" : room.startsWith("user:") ? "wecom-dm" : room.startsWith("chat:wx") ? "wx" : "wecom-group";
2869
2907
  const ops = [{ op: "open", at, room, kind, by: DAEMON }];
2870
2908
  let founder;
2871
2909
  try {
@@ -2912,17 +2950,20 @@ const main = async () => {
2912
2950
  return;
2913
2951
  if (needsOnboarding(cfg, wizardStore(), room))
2914
2952
  return;
2915
- const channelRoom = channelRoomOf(cfg, room);
2916
- if (channelRoom && !channelMonitorWanted(cfg, wizardStore(), channelRoom))
2953
+ const channelKey = channelKeyOf(cfg, room);
2954
+ if (channelKey && !channelMonitorWanted(cfg, wizardStore(), channelKey))
2917
2955
  return;
2918
2956
  if (!perceptronCandidates(st0).length)
2919
2957
  return;
2920
- const target = keyOf(baseOfKey(room), "monitor");
2958
+ // 粒度 (docs/evolve/channel-topic-rooms.md §4 / §9-① A): 频道帖子 room 共用**一个频道一个** perceptron
2959
+ // (`chat:<topic_id>#monitor`, 入座该频道每个 topic room) —— 身份数与冷启动数不随帖子线性涨; 其余 room 仍各一个。
2960
+ // room_activate 按 re 的消息 id 定 room, 共用一个身份不串账。
2961
+ const target = keyOf(channelKey || baseOfKey(room), "monitor");
2921
2962
  const rec = wizards.get(target);
2922
2963
  wizards.upsert(target, {
2923
2964
  kind: "monitor",
2924
2965
  ...(rec ? {} : { bornAt: Date.now(), identity: target }),
2925
- description: "本 room 的 perceptron: 读账本, 结合成员职责判断最后一条人话该由谁回应 (room_activate)",
2966
+ description: channelKey ? "频道的 perceptron: 坐在该频道每个帖子 room 里, 读账本, 结合成员职责判断各 room 最后一条人话该由谁回应 (room_activate)" : "本 room 的 perceptron: 读账本, 结合成员职责判断最后一条人话该由谁回应 (room_activate)",
2926
2967
  parent: undefined,
2927
2968
  lead: undefined,
2928
2969
  leadOf: undefined,
@@ -2935,50 +2976,50 @@ const main = async () => {
2935
2976
  hub.write(room, [{ op: "join", at: Date.now(), room, who: { kind: "wizard", id: uid }, ord: st.nextOrd, via: "invite", by: DAEMON, role: "monitor" }]);
2936
2977
  roomLog.info({ target, room }, "perceptron: 建 room perceptron (懒建, 首次唤醒冷启动)");
2937
2978
  };
2938
- /** 频道 room 不存在就开一个 (频道管家已绑定就作 founder —— monitor 需要有人可激活)。
2939
- * 未绑定期不建 monitor (channel-onboarding 口径①): 否则每条人话都被 monitor 抢走, 引导就哑了。
2940
- * learnChannelRoom: 未配对过帖子的频道也要被认成频道 —— 引导绑定落 wrc.channel.steward 靠它。 */
2941
- const ensureChannelRoom = (topicId) => {
2942
- const room = "chat:" + topicId;
2943
- learnChannelRoom(cfg, topicId);
2944
- if (!roomState(room)) {
2945
- const at = Date.now();
2946
- const ops = [{ op: "open", at, room, kind: "channel", by: DAEMON }];
2947
- try {
2948
- const stw = resolveSteward(cfg, wizardStore(), room, (t) => ((c) => c.runningCwd || c.defaultCwd)(m.getCwd(t)));
2949
- // 已有化身的才能作 founder (fresh = 记录还没落, 由入站接待自己开)。与 ensureBoundRoom 同一判法。
2950
- const uid = stw.fresh ? undefined : wizards.get(stw.target)?.uid;
2951
- if (uid)
2952
- ops.push({ op: "join", at, room, who: { kind: "wizard", id: uid }, ord: 1, via: "founder", by: DAEMON });
2953
- }
2954
- catch { /* 没绑定: 空频道 room, 引导 (或绑定后的管家) 会进来 */ }
2955
- hub.write(room, ops);
2979
+ /** topic room 不存在就开一个 (docs/evolve/channel-topic-rooms.md §3.1): 频道已绑 host 身份就让它在
2980
+ * **本 room** 的实例作 founder (只落记录, 不起 pane —— 首次唤醒由泵冷启动); 没绑 (引导期) 只开 room,
2981
+ * 人话交引导, 落定时补 host。已存在的 room 里没有 host 而频道已绑 → 顺手补座 (自愈), 幂等。 */
2982
+ const ensureTopicRoom = (room) => {
2983
+ const key = channelKeyOf(cfg, room);
2984
+ const wanted = !!key && channelMonitorWanted(cfg, wizardStore(), key);
2985
+ const s = roomState(room);
2986
+ if (!s) {
2987
+ const founder = wanted ? roomStewardOf(room, key) : undefined;
2988
+ hub.write(room, topicRoomBirthOps(room, founder, Date.now()));
2989
+ roomLog.info({ room, channel: key, founder: founder?.id ?? "" }, "channel: topic room 诞生");
2990
+ return;
2956
2991
  }
2957
- return room;
2992
+ if (wanted && !roomHost(s))
2993
+ seatChannelRoom(room, key);
2958
2994
  };
2959
- /** 频道引导期: 引导化身入座频道 room 并接住这条人话 (人话落频道 room —— 帖子 room 此刻没有
2960
- * host, 落那儿等于丢话)。与群引导的 guideTakeover 同形, 只是没有企微占位气泡 (频道 room
2961
- * 的出站是帖子)。 */
2995
+ /** 事件 → 落哪个 topic room (§3.1): 索引 / 账本现取, 纯函数在 channel-posts.ts。 */
2996
+ channelRoomFor = (topicId, postId, opk) => topicRoomOf({
2997
+ cfg, topic: topicId, post: postId, ...(opk ? { opk } : {}),
2998
+ roomOfSource: (src) => roomStore.roomOfSource(src), roomExists: (r) => !!roomState(r),
2999
+ roomKind: (r) => { const s = roomState(r); return s ? roomKindOf(s) : undefined; },
3000
+ });
3001
+ /** 频道引导期: 引导化身入座人说话的 topic room 并接住这条人话 (人话落那个 room, 一个频道一条引导可同时
3002
+ * 住多个 topic room)。与群引导的 guideTakeover 同形, 只是没有企微占位气泡 (topic room 的出站是评论)。 */
2962
3003
  const takeChannelGuide = (turn) => {
2963
3004
  const rec = wizards.get(turn.guide);
2964
3005
  if (!rec?.uid)
2965
3006
  return { ok: false, reason: "引导会话不在名册上" };
2966
3007
  const at = Date.now();
2967
- const s = roomState(turn.channelRoom);
2968
- // 旧版本给未绑定频道建过 monitor: 先请它退场, 否则人话只听它的, 引导接不到 (口径①)。
3008
+ const s = roomState(turn.room);
3009
+ // 旧版本给未绑定频道建过 perceptron: 先请它退场, 否则人话只听它的, 引导接不到 (口径①)。
2969
3010
  const evict = s ? channelMonitorLeaveOps(s, at) : [];
2970
3011
  if (evict.length) {
2971
- hub.write(turn.channelRoom, evict);
2972
- roomLog.info({ room: turn.channelRoom }, "onboarding: 频道引导期请走 monitor");
3012
+ hub.write(turn.room, evict);
3013
+ roomLog.info({ room: turn.room }, "onboarding: 频道引导期请走 monitor");
2973
3014
  }
2974
- const join = channelGuideJoinOps(roomState(turn.channelRoom), turn.channelRoom, { kind: "wizard", id: rec.uid }, at);
3015
+ const join = channelGuideJoinOps(roomState(turn.room), turn.room, { kind: "wizard", id: rec.uid }, at);
2975
3016
  if (join.length)
2976
- hub.write(turn.channelRoom, join);
2977
- const v = hub.append(turn.channelRoom, turn.human);
3017
+ hub.write(turn.room, join);
3018
+ const v = hub.append(turn.room, turn.human);
2978
3019
  return v.ok ? { ok: true } : { ok: false, reason: v.reason };
2979
3020
  };
2980
3021
  const channelGuideSpawning = new Set();
2981
- /** 未绑定频道的人话: 起 (或续) 引导化身, 人话落频道 room 交给它。会话冷启动在后台做 ——
3022
+ /** 未绑定频道的人话: 起 (或续) 引导化身, 人话落它说话的 topic room 交给它。会话冷启动在后台做 ——
2982
3023
  * 人话先落账 (起 pane 慢不能丢话); 出生窗口里贴不进去 (还没绑定), 泵会退成可重试,
2983
3024
  * 起来后这里踢一脚泵立刻投, 不会写成「没有会话 (已被结束)」。 */
2984
3025
  const channelGuideTurn = (turn) => {
@@ -2987,7 +3028,7 @@ const main = async () => {
2987
3028
  return;
2988
3029
  if (!store.get(turn.guide)) {
2989
3030
  store.upsert(turn.guide, { identity: turn.guide, kind: "worker", description: GUIDE_DESC, workspace: expandHome(cfg.wrc.cwd) });
2990
- roomLog.info({ room: turn.channelRoom, guide: turn.guide }, "onboarding: 频道引导化身落记录");
3031
+ roomLog.info({ room: turn.room, guide: turn.guide }, "onboarding: 频道引导化身落记录");
2991
3032
  }
2992
3033
  if (!m.hasMirrorTarget(turn.guide) && !channelGuideSpawning.has(turn.guide)) {
2993
3034
  channelGuideSpawning.add(turn.guide);
@@ -3006,18 +3047,19 @@ const main = async () => {
3006
3047
  }
3007
3048
  const r = takeChannelGuide(turn);
3008
3049
  if (!r.ok)
3009
- roomLog.warn({ room: turn.channelRoom, guide: turn.guide, reason: r.reason }, "onboarding: 频道引导没接住这条人话");
3050
+ roomLog.warn({ room: turn.room, guide: turn.guide, reason: r.reason }, "onboarding: 频道引导没接住这条人话");
3010
3051
  };
3011
3052
  /** 频道已绑定而引导化身还挂着 (人手改 config / 上个进程死在半路): 收尸 —— 与群 guideGate 开头同款。 */
3012
- const reapStaleChannelGuide = (channelRoom) => {
3013
- const guide = guideOf(wizards, channelRoom);
3053
+ const reapStaleChannelGuide = (channelKey) => {
3054
+ const guide = guideOf(wizards, channelKey);
3014
3055
  if (!guide)
3015
3056
  return;
3016
- roomLog.info({ room: channelRoom, guide }, "onboarding: 频道已绑定, 收掉遗留的引导化身");
3057
+ roomLog.info({ room: channelKey, guide }, "onboarding: 频道已绑定, 收掉遗留的引导化身");
3017
3058
  void m.killPane(guide).catch(() => undefined).then(() => { wizardStore()?.drop(guide); });
3018
3059
  };
3019
- /** 人话落 room: 频道 room / 帖子 room 都先保证存在; mention / post 原样写进行里。
3020
- * 未绑定频道先过引导 (口径②④): 引导接一切人话, 绑定后才轮到 monitor / host。 */
3060
+ /** 人话落 room: 事件已经由 channel.ts 的 topicRoomOf 定好落点 (e.room), 这里保证 room 存在再落行。
3061
+ * 未绑定频道先过引导 (口径②): 引导接一切人话, 绑定后才轮到 perceptron / host。行不带 post / src
3062
+ * —— room 自己就是那个帖子。 */
3021
3063
  channelIngest = (e) => {
3022
3064
  // 帖 / 评论带的图与文件: 与群聊一个口径 —— 图片以 `@path` 随行 (pane 里 CLI 直接 Read),
3023
3065
  // 文件给一行路径; 账本行同时记 files (rolepage 显示 📎, room_read 可查)。
@@ -3044,46 +3086,40 @@ const main = async () => {
3044
3086
  void m.newSession(target, settleName(store, chatNameOf(cfg, target), target) || target, { silent: true, ...(cwd ? { cwd } : {}) })
3045
3087
  .catch(() => undefined);
3046
3088
  },
3047
- }, { author: e.author, text, files: mediaFiles, src: e.room, ...(e.post ? { post: e.post } : {}) });
3089
+ }, { author: e.author, text, files: mediaFiles, src: e.room });
3048
3090
  if (me) {
3049
3091
  if (!me.ok)
3050
3092
  roomLog.warn({ room: meRoomId(e.author), src: e.room, msgid: e.msgid, reason: me.reason }, "channel: .me 人话落行被拒");
3051
3093
  return;
3052
3094
  }
3053
- // 绑定优先于引导: 源已绑进某个 room → 这句话落那本账 (引导只管还没归属的频道人话)。
3054
- const bound = roomStore.roomOfSource(e.room);
3055
- if (!bound) {
3056
- const turn = planChannelGuideTurn({ cfg, store: wizardStore(), onboarding, room: e.room }, e);
3057
- if (turn) {
3058
- channelGuideTurn(turn);
3059
- return;
3060
- }
3095
+ const turn = planChannelGuideTurn({ cfg, store: wizardStore(), onboarding, room: e.room }, e);
3096
+ if (turn) {
3097
+ channelGuideTurn(turn);
3098
+ return;
3061
3099
  }
3062
- const chRoom = channelRoomOf(cfg, e.room);
3063
- if (chRoom) {
3064
- onboarding.finish(chRoom);
3065
- reapStaleChannelGuide(chRoom);
3100
+ const key = channelKeyOf(cfg, e.room);
3101
+ if (key) {
3102
+ onboarding.finish(key);
3103
+ reapStaleChannelGuide(key);
3066
3104
  }
3067
- // §4.1 源 → room: 绑定优先; 帖子源还没诞生 (没绑、帖子 room 也不在) → 频道 room 的接待处,
3068
- // 由 host 判定 (§5.1)。行上的 src 写死入口源 —— 重放不再推断, 出站 (P2) 按它回原帖/原群。
3069
- const room = sourceRoomOf({ source: e.room, roomOfSource: (s) => roomStore.roomOfSource(s), stateOf: roomState, fallback: () => chRoom || undefined });
3070
- const src = e.room !== room ? e.room : undefined;
3071
- if (!roomState(room)) {
3072
- roomLog.warn({ room, src: e.room, msgid: e.msgid }, "channel: room 不存在, 人话没落账本 (自愈: 下一条事件重建)");
3105
+ if (isTopicRoomId(e.room))
3106
+ ensureTopicRoom(e.room);
3107
+ if (!roomState(e.room)) {
3108
+ roomLog.warn({ room: e.room, msgid: e.msgid }, "channel: room 不存在, 人话没落账本 (自愈: 下一条事件重建)");
3073
3109
  return;
3074
3110
  }
3075
- const v = hub.append(room, {
3111
+ const v = hub.append(e.room, {
3076
3112
  author: { kind: "human", id: e.author }, kind: "say", text, origin: "inbound",
3077
- ...(e.post ? { post: e.post } : {}), ...(e.mention ? { mention: e.mention } : {}),
3078
- ...(src ? { src } : {}), ...(mediaFiles.length ? { files: mediaFiles } : {}),
3113
+ ...(e.mention ? { mention: e.mention } : {}), ...(mediaFiles.length ? { files: mediaFiles } : {}),
3079
3114
  });
3080
3115
  if (!v.ok)
3081
- roomLog.warn({ room, src: e.room, msgid: e.msgid, reason: v.reason }, "channel: 人话落行被拒");
3116
+ roomLog.warn({ room: e.room, msgid: e.msgid, reason: v.reason }, "channel: 人话落行被拒");
3082
3117
  };
3083
- /** §4.1 入站源解析 (inbound 的 resolve / send 用): base 是入站发生地 (群 / 帖子群 / 频道)。
3084
- * - 源已绑进 room R → 落 R, 接待 = R 的 host (不是源自己的类绑定化身);
3085
- * - 帖子源已诞生 (帖子 room 在) → 落它;
3086
- * - 帖子源还没诞生 (首触) → 落频道 room, 接待 = 频道 host;
3118
+ /** §4.1 入站源解析 (inbound 的 resolve / send 用): base 是入站发生地 (群 / 帖子群)。
3119
+ * - 频道帖子的原生 @ 帧 (base = 帖子自己的 chat:opk…, 配上对了): 落 topicRoomOf 定的 topic room
3120
+ * (room 不在就开), 接待 = 该 room 的 host; 行不带 src / post。频道还在引导期 → undefined,
3121
+ * 交 inbound 的频道引导 (guideGate);
3122
+ * - 源已绑进 room R (群源, 或存量的帖子源 bind) → 落 R, 接待 = R 的 host (不是源自己的类绑定化身);
3087
3123
  * - 普通群 / 恒等路径 → undefined (现行为, stewardReception)。
3088
3124
  * host 必须是 wizard 且还在名册上; 解析不出就回落恒等 (主流程不因绑定出错)。 */
3089
3125
  sourceRouteHook = (base) => {
@@ -3091,26 +3127,27 @@ const main = async () => {
3091
3127
  return undefined;
3092
3128
  const bound = roomStore.roomOfSource(base);
3093
3129
  const ref = postRefOf(cfg, base);
3094
- const topic = channelOf(cfg, base);
3095
- if (!bound && !ref && !topic)
3096
- return undefined;
3097
- const room = sourceRoomOf({ source: base, roomOfSource: (s) => roomStore.roomOfSource(s), stateOf: roomState,
3098
- fallback: () => (ref ? ensureChannelRoom(ref.channel) : undefined) });
3099
- const st = roomState(room);
3100
- const host = st ? roomHost(st) : undefined;
3101
- const target = host?.kind === "wizard" ? roomWizardOf(host.id)?.target : undefined;
3102
- if (!target)
3130
+ const hostTarget = (room) => {
3131
+ const st = roomState(room);
3132
+ const host = st ? roomHost(st) : undefined;
3133
+ return host?.kind === "wizard" ? roomWizardOf(host.id)?.target : undefined;
3134
+ };
3135
+ if (bound) {
3136
+ const target = hostTarget(bound);
3137
+ if (!target)
3138
+ return undefined;
3139
+ return { room: bound, target, ...(bound !== base ? { src: base } : {}), ...(ref?.post ? { post: ref.post } : {}) };
3140
+ }
3141
+ if (!ref?.post)
3103
3142
  return undefined;
3104
- const src = base !== room ? base : undefined;
3105
- return { room, target, ...(src ? { src } : {}), ...(ref?.post ? { post: ref.post } : {}) };
3106
- };
3107
- channelRoomSeen = (topicId) => { ensureChannelRoom(topicId); };
3108
- /** 帖子源首触 (§4.1): **不建 room、不开实例** —— 人话由 inbound / channelIngest 落频道 room
3109
- * 交 host 判定。这里只保证频道 room 在 (首触的人话要有地方落), 并记一行日志。
3110
- * 帖子 room 的诞生移到 birthPostRoom: host 的 room_bind 之外, 超时兜底 (decideOnFirstTouch) 调。 */
3111
- channelPostTouched = (topicId, postBase) => {
3112
- const channelRoom = ensureChannelRoom(topicId);
3113
- roomLog.info({ room: postBase, channel: channelRoom }, "channel: 帖子源首触 (落频道 room 交 host 判定, 不建帖子 room)");
3143
+ const key = "chat:" + ref.channel;
3144
+ if (!channelMonitorWanted(cfg, wizardStore(), key))
3145
+ return undefined; // 引导期: 人话归引导
3146
+ const room = channelRoomFor(ref.channel, ref.post, base);
3147
+ if (isTopicRoomId(room))
3148
+ ensureTopicRoom(room);
3149
+ const target = hostTarget(room);
3150
+ return target ? { room, target } : undefined;
3114
3151
  };
3115
3152
  /** 定时任务 / plan 提醒的一枪 (cutmap G-1/G-2): kind:"trigger" 行 —— 不镜像, 只唤醒 owner。 */
3116
3153
  const fireTaskTrigger = (target, text, ext) => {
@@ -3138,7 +3175,8 @@ const main = async () => {
3138
3175
  },
3139
3176
  wizardOf: (uid) => {
3140
3177
  const w = roomWizardOf(uid);
3141
- return w ? { target: w.target, name: w.name, paneId: m.sessionInfo(w.target)?.tmuxPane } : undefined;
3178
+ // 只在有活 attach 时交出 pane id: reap / sleep 后 store 里的旧 id 不是「意外消失」, 是睡着 (冷)。
3179
+ return w ? { target: w.target, name: w.name, paneId: watchedPaneOf(m.hasAttach(w.target), m.sessionInfo(w.target)?.tmuxPane) } : undefined;
3142
3180
  },
3143
3181
  isBusy: (target) => m.isBusy(target),
3144
3182
  rooms: roomStore.ids,
@@ -3223,12 +3261,13 @@ const main = async () => {
3223
3261
  if (!rec?.uid)
3224
3262
  return { ok: false, reason: "引导会话不在名册上" };
3225
3263
  const guide = { kind: "wizard", id: rec.uid };
3226
- const room = baseOfKey(a.guide);
3264
+ // 频道原生 @ 帧的引导: 人话落帖子自己的 topic room (a.room), 群引导落群 room (= 引导 key 的 base)。
3265
+ const room = a.room || baseOfKey(a.guide);
3227
3266
  const at = Date.now();
3228
3267
  const s = roomState(room);
3229
3268
  if (!s)
3230
3269
  hub.write(room, [
3231
- { op: "open", at, room, kind: "wecom-group", by: DAEMON },
3270
+ { op: "open", at, room, kind: isTopicRoomId(room) ? "topic-post" : "wecom-group", by: DAEMON },
3232
3271
  { op: "join", at, room, who: guide, ord: 1, via: "founder", by: DAEMON },
3233
3272
  ]);
3234
3273
  else if (!s.members.has(whoKey(guide)))
@@ -3268,125 +3307,78 @@ const main = async () => {
3268
3307
  .catch((e) => roomLog.warn({ target, err: errText(e) }, "room: 管家 pane 起不来 (有唤醒时泵会再试)"))
3269
3308
  .finally(() => stewardSpawning.delete(target));
3270
3309
  };
3271
- /** 引导落定: 管家进 room → 引导名下 duty 全部转给它并唤醒 → 引导离开 → 完成系统行。
3272
- * false = 引导不在这个 room 里 (老流程 / 没走过 room), 调用方自己回一句。 */
3273
- const guideSettleRoom = (base, bound, roomName = "") => {
3274
- const s = roomState(base);
3275
- const guideTarget = guideOf(wizards, base);
3276
- const guide = guideTarget ? roomSelfOf(guideTarget) : undefined;
3277
- const steward = roomSelfOf(bound.target);
3278
- if (!s || !guide || !steward)
3279
- return false;
3280
- // roomMirror 给 daemon 的行补 "[wezard] " 前缀, 完成确认自己已经带了一个。
3281
- const text = renderOnboardingDone(bound).replace(/^\[wezard\]\s*/, "");
3282
- const ops = guideHandoffOps(s, { guide, steward, text, now: Date.now(), ...(roomName ? { roomName } : {}) });
3283
- if (!ops.length)
3284
- return false;
3285
- hub.write(base, ops);
3286
- roomLog.info({ room: base, guide: guideTarget, steward: bound.target, ops: ops.length }, "onboarding: room 交给管家");
3287
- // 管家会话还没起: 泵贴不进没绑定的 pane —— 先起一个 (与频道落定 / boot 兜底同一口子)。
3288
- spawnStewardPane(bound.target);
3289
- return true;
3310
+ /** 引导所在的 room 们 (引导化身是成员的): 群 = [群 room]; 频道键 = 该频道每个 topic room 里引导在座的
3311
+ * (一个频道一条引导可同时住几个 topic room), 加上旧版留下的存档频道 room (若引导还坐在里面)。 */
3312
+ const guideRoomsOf = (base, guide) => {
3313
+ const topic = base === "chat:" + channelOf(cfg, base) ? channelOf(cfg, base) : "";
3314
+ const cands = topic ? [base, ...topicRoomsOf(cfg, topic, roomStore.ids())] : [base];
3315
+ return cands.filter((r) => roomState(r)?.members.has(whoKey(guide)));
3290
3316
  };
3291
- /** 一个频道 room / 帖子 room 的 host 座位 + monitor: host 一律是**本 room 的**实例
3292
- * (人 2026-11-23: 不同的 room 要用不同的实例), 没有就领号新开。幂等 —— 落定与 boot 自愈共用。 */
3293
- const seatChannelRoom = (room, channelRoom) => {
3317
+ /** 一个 room 的 host 座位: host 一律是**本 room 的**实例 (人 2026-11-23: 不同的 room 要用不同的
3318
+ * 实例), 没有就领号新开。幂等 —— 落定与 boot 自愈共用。channelKey = 频道键 (群 / 非频道 room 不走它)。 */
3319
+ const seatChannelRoom = (room, channelKey) => {
3294
3320
  const s = roomState(room);
3295
3321
  if (!s)
3296
3322
  return;
3297
- const who = roomStewardOf(room, channelRoom);
3323
+ const who = roomStewardOf(room, channelKey);
3298
3324
  if (who) {
3299
3325
  const ops = hostJoinOps(s, who, Date.now());
3300
3326
  if (ops.length)
3301
3327
  hub.write(room, ops);
3302
3328
  }
3303
3329
  };
3304
- /** 绑定落在**频道**上 (wrc.channel.steward): 频道 room 与各帖子 room 各坐**自己那个**实例当
3305
- * host, 各起一个 monitor (口径③)。引导期这两样都没建 (ensureChannelRoom / channelPostBorn 的
3306
- * bound 闸), 这里是唯一的补建点 —— 普通群 (channelOf 认不出) = 无操作。 */
3307
- const settleBoundChannel = (base, bound) => {
3308
- const topicId = channelOf(cfg, base);
3309
- if (!topicId)
3310
- return;
3311
- const rooms = [base, ...postsOfChannel(cfg, topicId)];
3312
- for (const room of rooms)
3313
- seatChannelRoom(room, base);
3314
- // 管家实例的 pane: 群落定 (guideSettleRoom) 一直有这一步, 频道这边原来漏了 —— 记录在、
3315
- // pane 没有, monitor 唤醒就是 gone, 人看到「已被结束」(人 2026-11-23 现场)。
3316
- // 帖子 room 的实例不预起 pane: 第一次被唤醒时泵会冷启动它 (每个帖子一个常驻 pane 会顶到上限)。
3317
- spawnStewardPane(bound.target);
3318
- roomLog.info({ room: base, topicId, steward: bound.target, rooms: rooms.length }, "onboarding: 频道交出管家 (每 room 一个实例 + monitor)");
3319
- };
3320
- /** §5.1 出口 3 (另起 / 兜底): 帖子 room 诞生 + 领号新实例, 并把新 host 拉进频道 room。
3321
- * by = 判定者 (超时兜底时 DAEMON)。幂等: 帖子 room 已在 / 源已绑 / 频道还在引导期 = 什么都不做。
3322
- * §7: 频道 host (最早加入序) 身份不动 —— 新成员走普通 join (via spawn, ord = nextOrd, watch 关)。 */
3323
- const birthPostRoom = (topicId, postBase, by) => {
3324
- if (roomStore.roomOfSource(postBase))
3330
+ /** 引导落定: 管家进 room → 引导名下 duty 全部转给它并唤醒 → 引导离开 → 完成系统行。对引导所在的
3331
+ * **每个** room 做一遍 (群 = 一个; 频道 = 它住过的每个 topic room, 管家各用本 room 的实例)。
3332
+ * false = 引导不在任何 room 里 (老流程 / 没走过 room), 调用方自己回一句。 */
3333
+ const guideSettleRoom = (base, bound, roomName = "") => {
3334
+ const guideTarget = guideOf(wizards, base);
3335
+ const guide = guideTarget ? roomSelfOf(guideTarget) : undefined;
3336
+ if (!guide)
3325
3337
  return false;
3326
- const channelRoom = ensureChannelRoom(topicId);
3327
- if (!channelMonitorWanted(cfg, wizardStore(), channelRoom))
3328
- return false; // 引导期不诞生, 人话归引导
3329
- const at = Date.now();
3330
- const founder = roomStewardOf(postBase, channelRoom);
3331
- if (!roomState(postBase)) {
3332
- hub.write(postBase, postRoomBirthOps(postBase, founder, at));
3333
- roomLog.info({ room: postBase, channel: channelRoom, founder, by: by.kind + ":" + by.id }, "channel: 帖子 room 诞生 (另起)");
3334
- }
3335
- // 新 host 同步进频道 room: 频道 host 从此在成员表里看得到每个帖子 host。
3336
- const cs = roomState(channelRoom);
3337
- if (cs && founder) {
3338
- const who = founder;
3339
- if (!cs.members.has(whoKey(who)))
3340
- hub.write(channelRoom, [{ op: "join", at, room: channelRoom, who, ord: cs.nextOrd, via: "spawn", by }]);
3341
- }
3342
- return true;
3343
- };
3344
- /** §5.1 兜底扫描: 首触落频道 room、host 在 decideTimeoutMin 内没 bind 也没另起 → 自动走出口 3。
3345
- * 判据全从账本现算 (room-bind.ts 纯函数), 已绑 / 已诞生下一次自然不再命中; overdue 提醒独立运行。 */
3346
- const decideOnFirstTouch = () => {
3347
- const timeoutMs = (cfg.rooms?.binding?.decideTimeoutMin ?? 10) * 60_000;
3348
- let due = [];
3349
- try {
3350
- due = pendingFirstTouches({ cfg, states: roomStore.ids().map(roomState).filter((x) => !!x),
3351
- roomOfSource: (s) => roomStore.roomOfSource(s), stateOf: roomState, now: Date.now(), timeoutMs });
3352
- }
3353
- catch (e) {
3354
- roomLog.warn({ err: errText(e) }, "binding: 首触兜底扫描失败 (下一轮再来)");
3355
- return;
3356
- }
3357
- for (const d of due) {
3338
+ const channelBase = !!channelOf(cfg, base) && base === "chat:" + channelOf(cfg, base);
3339
+ // roomMirror 给 daemon 的行补 "[wezard] " 前缀, 完成确认自己已经带了一个。
3340
+ const text = renderOnboardingDone(bound).replace(/^\[wezard\]\s*/, "");
3341
+ let handed = false;
3342
+ // 频道没有 room 可起名: 人给的名字落成频道名 (wrc.channel.name.<T>), 频道框标题与各帖子 room 的兜底前缀都跟着它。
3343
+ if (channelBase && roomName) {
3358
3344
  try {
3359
- if (birthPostRoom(d.topicId, d.source, DAEMON))
3360
- roomLog.info({ room: d.source, channel: d.channelRoom, waitedMin: Math.round((Date.now() - d.at) / 60000) }, "binding: 首触超时, 自动另起帖子 room");
3345
+ setChannelName(cfg, sourcePath, channelOf(cfg, base), roomName);
3361
3346
  }
3362
- catch (e) {
3363
- roomLog.warn({ room: d.source, err: errText(e) }, "binding: 兜底另起失败 (下一轮再来)");
3347
+ catch (err) {
3348
+ roomLog.warn({ base, err: errText(err) }, "onboarding: 频道名写盘失败");
3364
3349
  }
3365
3350
  }
3366
- };
3367
- /** §7 boot 补拉: 存量帖子 room 的 host 实例进频道 room。幂等; 只补成员, 不起 pane (cold 等第一次唤醒)。 */
3368
- const backfillChannelMembers = () => {
3369
- let seats = [];
3370
- try {
3371
- seats = planChannelBackfill({ cfg, rooms: roomStore.ids(), stateOf: roomState,
3372
- targetOf: (uid) => roomWizardOf(uid)?.target });
3373
- }
3374
- catch (e) {
3375
- roomLog.warn({ err: errText(e) }, "binding: 频道成员补拉扫描失败");
3376
- return;
3377
- }
3378
- for (const seat of seats) {
3379
- const cs = roomState(seat.channelRoom);
3380
- if (!cs)
3351
+ for (const room of guideRoomsOf(base, guide)) {
3352
+ const s = roomState(room);
3353
+ // 频道: 管家是**本 room** 的实例 (bound.target 只是频道级锚点, 不属于任何 topic room)。
3354
+ const steward = channelBase ? roomStewardOf(room, base) : roomSelfOf(bound.target);
3355
+ if (!s || !steward)
3381
3356
  continue;
3382
- hub.write(seat.channelRoom, [{ op: "join", at: Date.now(), room: seat.channelRoom, who: seat.who, ord: cs.nextOrd, via: "spawn", by: DAEMON }]);
3383
- roomLog.info({ room: seat.channelRoom, host: seat.target }, "binding: boot 补拉帖子 host 进频道 room");
3357
+ // 频道的 roomName 已在上面写成频道名, 不进帖子 room 的 title —— roomName 只给群。
3358
+ const ops = guideHandoffOps(s, { guide, steward, text, now: Date.now(), ...(roomName && !channelBase ? { roomName } : {}) });
3359
+ if (!ops.length)
3360
+ continue;
3361
+ hub.write(room, ops);
3362
+ handed = true;
3363
+ const stewardTarget = channelBase ? roomWizardOf(steward.id)?.target ?? "" : bound.target;
3364
+ roomLog.info({ room, guide: guideTarget, steward: stewardTarget, ops: ops.length }, "onboarding: room 交给管家");
3365
+ // 管家会话还没起: 泵贴不进没绑定的 pane —— 先起一个 (与频道落定 / boot 兜底同一口子)。
3366
+ spawnStewardPane(stewardTarget);
3384
3367
  }
3368
+ return handed;
3369
+ };
3370
+ /** 绑定落在**频道**上 (wrc.channel.steward): 该频道每个 topic room 各坐**自己那个**实例当 host
3371
+ * (perceptron 照旧每 room 懒建)。引导期这两样都没建, 这里是唯一的补建点 —— 普通群 (channelOf 认不出)
3372
+ * = 无操作。频道自己没有 room, 不要求有存档频道 room; 实例不预起 pane (首次唤醒由泵冷启动)。 */
3373
+ const settleBoundChannel = (base) => {
3374
+ const topicId = channelOf(cfg, base);
3375
+ if (!topicId)
3376
+ return;
3377
+ const rooms = topicRoomsOf(cfg, topicId, roomStore.ids());
3378
+ for (const room of rooms)
3379
+ seatChannelRoom(room, "chat:" + topicId);
3380
+ roomLog.info({ room: base, topicId, rooms: rooms.length }, "onboarding: 频道交出管家 (每个 topic room 一个实例)");
3385
3381
  };
3386
- // 首触兜底与看门狗同节拍 (阈值每轮现读, hot)。boot 先等启动那一波过去再扫一次。
3387
- const decideTimer = setInterval(decideOnFirstTouch, (cfg.rooms?.watchdog?.intervalSec ?? 60) * 1000);
3388
- decideTimer.unref?.();
3389
- setTimeout(decideOnFirstTouch, 30_000).unref?.();
3390
3382
  /** 引导收尸 (绑定后): 工具调用已经回成功, 等它这一轮结束 (或 60s 上限) 再 kill + drop ——
3391
3383
  * 半路杀 pane 会把这一轮截断在 CLI 里。它已经 leave 过 room, 所以 drop 不会再产生
3392
3384
  * canceled 行 (leaveRooms 对非成员是空操作)。 */
@@ -3400,12 +3392,11 @@ const main = async () => {
3400
3392
  roomLog.info({ guide, killed: r.ok, reason: r.reason }, "onboarding: guide reaped after bind");
3401
3393
  };
3402
3394
  guideInboundHook = guideTakeover;
3403
- // 群引导的落定也走频道补建: base 是频道 room 时, 绑定后要补 host / monitor (口径③);
3395
+ // 群引导的落定也走频道补建: base 是频道键时, 绑定后每个 topic room 补 host (口径③);
3404
3396
  // 普通群 channelOf 认不出, settleBoundChannel 无操作。settleBoundChannel 幂等。
3405
3397
  guideSettleHook = (base, bound) => {
3406
3398
  const ok = guideSettleRoom(base, bound);
3407
- if (ok)
3408
- settleBoundChannel(base, bound);
3399
+ settleBoundChannel(base);
3409
3400
  return ok;
3410
3401
  };
3411
3402
  // rolepage 的 room 历史路由 (room-ui 的第 4 个接线点): 坏账本按「没有这个 room」处理。
@@ -3489,27 +3480,20 @@ const main = async () => {
3489
3480
  spawnStewardPane(target);
3490
3481
  if (spawn.length)
3491
3482
  classLog.info({ spawn }, "steward panes spawned at boot (记录在, pane 没有)");
3492
- // 已绑定频道的 room 一个 host 都没有 (绑完就崩 / 老数据): 补本 room 的实例 + monitor。
3483
+ // 已绑定频道的 topic room 一个 host 都没有 (绑完就崩 / 老数据): 补本 room 的实例。存档频道
3484
+ // room (chat:<topic_id>, 不再收入站) 不补座。
3493
3485
  for (const room of roomStore.ids()) {
3494
- const topicId = channelOf(cfg, room);
3495
- if (!topicId || !(cfg.wrc.channel.steward ?? {})[topicId])
3486
+ const ref = postRefOf(cfg, room);
3487
+ if (!ref?.post || !(cfg.wrc.channel.steward ?? {})[ref.channel])
3496
3488
  continue;
3497
3489
  const s = roomState(room);
3498
3490
  if (s && !roomHost(s))
3499
- seatChannelRoom(room, "chat:" + topicId);
3491
+ seatChannelRoom(room, "chat:" + ref.channel);
3500
3492
  }
3501
3493
  }
3502
3494
  catch (e) {
3503
3495
  classLog.warn({ err: errText(e) }, "steward instance repair failed (不影响启动)");
3504
3496
  }
3505
- // §7 boot 补拉: 存量帖子 room 的 host 进频道 room (与 settleBoundChannel / channel-rebind 同钩子;
3506
- // 幂等, 已拉过的不重复; 实例不在册 / 引导期频道跳过)。
3507
- try {
3508
- backfillChannelMembers();
3509
- }
3510
- catch (e) {
3511
- classLog.warn({ err: errText(e) }, "binding: 频道成员补拉失败 (不影响启动)");
3512
- }
3513
3497
  // v3 §3: 全局管家身份落位 —— home (defaultChat) 的默认会话就是它的锚点化身。
3514
3498
  // 新聊天 (还没有任何会话) 的裸消息由它接待 (inbound stewardRoute); 未认领的新装机不造幽灵记录。
3515
3499
  const gs = markGlobalSteward(wizards, cfg.defaultChat);
@@ -3784,7 +3768,11 @@ const main = async () => {
3784
3768
  file: x.file,
3785
3769
  };
3786
3770
  }),
3787
- chatNames: Object.fromEntries(listChatNames(cfg).map((c) => [c.base, c.name])),
3771
+ chatNames: {
3772
+ ...Object.fromEntries(listChatNames(cfg).map((c) => [c.base, c.name])),
3773
+ // 频道名表 (wrc.channel.name) 盖过旧标签: 前端 channelName 先读 chatNames['chat:<T>'], svr 也靠它。
3774
+ ...Object.fromEntries(Object.entries(cfg.wrc.channel.name ?? {}).filter(([, n]) => n.trim()).map(([t, n]) => [`chat:${t}`, n.trim()])),
3775
+ },
3788
3776
  // 微信用户的发话人 id 就是它那个群的 chatId: 名字跟着群名走。
3789
3777
  humanNames: Object.fromEntries(wx.list().map((a) => [`human:${a.chatId}`, chatNameOf(cfg, `chat:${a.chatId}`)]).filter(([, n]) => n)),
3790
3778
  weixin: wx.list().map((a) => ({
@@ -3794,11 +3782,13 @@ const main = async () => {
3794
3782
  // room 账本快照 + 名字表: rolepage 关系图 / room 视图的唯一来源 (room-ui)。
3795
3783
  // 坏账本跳过并留日志 —— 一个坏文件不该让整张图消失。
3796
3784
  rooms: roomFacts(roomStore, wizards.all(), {
3797
- chatName: (base) => chatNameOf(cfg, base),
3798
- channelOf: (room) => channelRefOf(cfg, room),
3785
+ chatName: (base) => { const tr = parseTopicRoomId(base); return tr ? topicRoomLabel(tr, channelNameOf(cfg, tr.channel)) : channelNameOfBase(cfg, base) || chatNameOf(cfg, base); },
3786
+ channelOf: channelRefWithHost,
3799
3787
  onBroken: (room, err) => log.warn({ mod: "room", room, err: errText(err) }, "room-facts: 坏账本跳过"),
3800
3788
  }),
3801
3789
  names: nameTable(wizards.all()),
3790
+ // 已知频道 (没有 room 的绑定频道也要有一个框): 种子, /api/world 再并上 rooms 里归并出的。
3791
+ channels: knownChannels(cfg).map((id) => ({ id, name: channelNameOf(cfg, id) || "chat:" + id, ...((h) => (h ? { host: h } : {}))(hostOfChannel(id)) })),
3802
3792
  classes: classes.all().map((c) => ({ id: c.id, name: c.name, role: c.role, workspace: c.workspace })),
3803
3793
  };
3804
3794
  };