@ssamurias/dsh-fish 1.0.8 → 1.1.0

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 (74) hide show
  1. package/AGENTS.md +368 -338
  2. package/CHANGELOG.md +113 -0
  3. package/README.md +377 -352
  4. package/RELEASE-NOTES-1.0.9.md +79 -0
  5. package/RELEASE-NOTES-1.1.0.md +55 -0
  6. package/docs//350/256/244/344/272/272/345/210/244/346/215/256.md +112 -0
  7. package/lib/appoint.js +106 -22
  8. package/lib/ask.js +2 -1
  9. package/lib/autobackup.js +4 -0
  10. package/lib/capture.js +16 -0
  11. package/lib/claim.js +103 -0
  12. package/lib/client/00-shell.js +630 -0
  13. package/lib/client/01-state-load.js +901 -0
  14. package/lib/client/02-actions.js +670 -0
  15. package/lib/client/03-widgets-header.js +721 -0
  16. package/lib/client/04-pressure-lab.js +891 -0
  17. package/lib/client/05-pressure-charts.js +431 -0
  18. package/lib/client/06-tab-status-grow.js +468 -0
  19. package/lib/client/07-tab-home-bili.js +610 -0
  20. package/lib/client/08-tab-memory-plugins.js +756 -0
  21. package/lib/client/09-tab-outing.js +710 -0
  22. package/lib/client/10-data-handlers.js +519 -0
  23. package/lib/client/11-tab-data-frame.js +749 -0
  24. package/lib/client/12-pressure-chip-mount.js +565 -0
  25. package/lib/client.js +477 -47
  26. package/lib/comment.js +18 -3
  27. package/lib/derive.js +262 -22
  28. package/lib/doing.js +31 -15
  29. package/lib/env.d.ts +224 -0
  30. package/lib/events.js +5 -2
  31. package/lib/guess.js +29 -0
  32. package/lib/health.js +75 -0
  33. package/lib/hear.js +67 -0
  34. package/lib/herpeople.js +159 -0
  35. package/lib/history.js +27 -5
  36. package/lib/home.js +12 -1
  37. package/lib/identity.js +23 -0
  38. package/lib/index.js +34 -18
  39. package/lib/look.js +3 -2
  40. package/lib/make.js +60 -12
  41. package/lib/markup.js +61 -23
  42. package/lib/observe-bili.js +334 -0
  43. package/lib/observe-config.js +223 -0
  44. package/lib/observe-data.js +522 -0
  45. package/lib/observe-home.js +384 -0
  46. package/lib/observe-http.js +250 -0
  47. package/lib/observe-outing.js +650 -0
  48. package/lib/observe-state.js +419 -0
  49. package/lib/observe-trip.js +286 -0
  50. package/lib/observe.js +99 -3574
  51. package/lib/people.js +514 -0
  52. package/lib/peopledoc.js +112 -0
  53. package/lib/persona.js +324 -42
  54. package/lib/restrict.js +74 -3
  55. package/lib/say.js +71 -1
  56. package/lib/scheduler.js +23 -3
  57. package/lib/schedules.js +97 -0
  58. package/lib/schema.js +11 -4
  59. package/lib/scrub.js +3 -3
  60. package/lib/selfdoc.js +19 -2
  61. package/lib/settings.js +112 -11
  62. package/lib/stance.js +47 -3
  63. package/lib/status.js +14 -5
  64. package/lib/sub.js +1 -1
  65. package/lib/tenant.js +35 -7
  66. package/lib/turns.js +51 -4
  67. package/lib/types.d.ts +515 -0
  68. package/lib/voices.js +3 -0
  69. package/lib/watch.js +1 -1
  70. package/lib/wiki.js +131 -25
  71. package/lib/works.js +120 -45
  72. package/package.json +1 -1
  73. package/scripts/build-client.mjs +83 -0
  74. package/scripts/sync-pressure-formulas.mjs +12 -15
package/lib/restrict.js CHANGED
@@ -44,7 +44,9 @@
44
44
 
45
45
  import { readFileSync } from 'node:fs'
46
46
  import { join, resolve } from 'node:path'
47
- import { paths } from './store.js'
47
+ import { paths, rootDir } from './store.js'
48
+ import { whenHers } from './claim.js'
49
+ import { isHerSchedule, rememberSchedule, scheduleIdOf, targetSessionOf } from './schedules.js'
48
50
  import { homeFishTools, liveFishTools, sceneFishTools } from './toolreg.js'
49
51
 
50
52
  /**
@@ -63,6 +65,7 @@ export const FISH_TOOLS = Object.freeze([
63
65
  'her_recall', // 翻自己的旧事
64
66
  'her_stance', // 翻自己站住过的地方,看每条是哪几次长出来的(2026-11-18,只读)
65
67
  'her_diary', // 翻自己的日记(只读,issue #7)
68
+ 'her_people', // 翻她认识的人:她怎么看这个人(1.1.0,只读)
66
69
  'her_leave', // 留一句话给他
67
70
  'her_note', // 把没做的事记到桌上(issue #22 乙档,2026-11-04)
68
71
  'her_appoint', // 跟他约一件几天后的事(通用预约簿,2026-11-08)
@@ -606,6 +609,7 @@ export function cwdOf(agent) {
606
609
  * 轮换不再需要「靠 cwd 兜底」:换房那条路在 create 前开了认领窗口,新 id 当场进台账。
607
610
  */
608
611
  const herSessions = new Map() // 家目录 → Set(会话 id):两个家同时开着也不能互相认领
612
+ const equipped = new WeakSet() // 已经按她的白名单装备过的 agent:认领后重跑一次就够
609
613
  const herLoose = new Set() // 拿不到家时记的(测试、老调用方):对哪个家都算
610
614
  const herCreating = new Map() // 家目录 → 正在建几条
611
615
  const herSeeded = new Set()
@@ -703,6 +707,15 @@ export function isHerAgent(agent, herDir) {
703
707
  return normDir(cwdOf(agent)) === want
704
708
  }
705
709
 
710
+ /** 这个会话 id 在不在她的台账里(按家分域;拿不到家时算「哪个家都算」)。 */
711
+ export function isHerSessionId(sessionId, herDir = '') {
712
+ const sid = sidKey(sessionId)
713
+ if (!sid) return false
714
+ if (herLoose.has(sid)) return true
715
+ const key = homeKey(herDir)
716
+ return Boolean(key && herSessions.get(key)?.has(sid))
717
+ }
718
+
706
719
  /**
707
720
  * 「她在自己屋里」——**只**认她自己的 cwd。决定工具面收窄用这条。
708
721
  *
@@ -743,7 +756,7 @@ export function isHerAnywhere(agent, herDir, extraDirs = []) {
743
756
  * @param {(msg: string, err?: unknown) => void} [opts.log]
744
757
  * @param {(info: object) => void} [opts.onResult] 记录"这次真的挂上没有"(给自检用)
745
758
  */
746
- export function registerToolRestrict(ctx, { herDir, extra = [], enabled = true, log, onResult, retryMs = 200, retryMax = 15, dream = false } = {}) {
759
+ export function registerToolRestrict(ctx, { root = '', herDir, extra = [], enabled = true, log, onResult, retryMs = 200, retryMax = 15, dream = false } = {}) {
747
760
  // 无论调用方传不传 onResult,模块级单例都要更新——自检读的是它。
748
761
  const __raw = onResult
749
762
  onResult = (r) => { report(r); try { __raw?.(r) } catch { /* ignore */ } }
@@ -827,7 +840,25 @@ export function registerToolRestrict(ctx, { herDir, extra = [], enabled = true,
827
840
  refreshFrom(agent)
828
841
  } catch { /* 拿不到就算了,界面会退回只查拼写 */ }
829
842
  try {
830
- if (!isHerAgent(agent, herDir)) return
843
+ if (!isHerAgent(agent, herDir)) {
844
+ // 桥自建的会话(2026-11-23):cwd 是她的屋子,id 却不在这家的台账里。
845
+ // 先不挂身份、不收窄——他自己在这个工作区里开的会话也长这样,1.0.7 就是
846
+ // 把它误当成她。等第一封「外面来的信」(turns.js 判来源 → lib/claim.js),
847
+ // 认下来再跑一遍这里的装备;他自己打的字不会有那封信。
848
+ const sid0 = agentSessionId(agent)
849
+ if (agent && sid0 && normDir(cwdOf(agent)) === normDir(herDir)) {
850
+ const ref = new WeakRef(agent)
851
+ whenHers(sid0, herDir, () => {
852
+ const got = ref.deref()
853
+ if (!got) return
854
+ rememberHerSession(sid0, herDir)
855
+ onCreated({ agent: got })
856
+ })
857
+ }
858
+ return
859
+ }
860
+ if (!agent || equipped.has(agent)) return
861
+ equipped.add(agent)
831
862
  // 记下这个她(issue #58):「到手」按她这条会话的工具表现算,不沿用上一个 agent 的。
832
863
  rememberHer(agent)
833
864
  const scopedTools = agent?.ctx?.tools
@@ -1009,6 +1040,45 @@ export function registerToolRestrict(ctx, { herDir, extra = [], enabled = true,
1009
1040
  try { old?.() } catch { /* ignore */ }
1010
1041
  return dispose
1011
1042
  }
1043
+ // ---- 宿主的闹钟:改之前先核一次归属(2026-11-24) ----
1044
+ // 事故:装了两条鱼的人,她那边的会话拿 `schedule_edit` 把另一位(廿八)的
1045
+ // 读书闹钟一起改指到自己窗口,到点宿主把写给廿八的指令投进了她的会话。
1046
+ // 宿主任务没有 owner 字段、改 target 也不校验归属;这里只守住她伸出去的手:
1047
+ // - 改(edit)之前核一次:本子里记过、或当前 target 是她的会话,才放行;
1048
+ // - 建(create)成功时把 id 记进本子,给「new_once 的保留会话还没进台账」留条路。
1049
+ // tools.guard 是同步的、不能 await 宿主服务,所以走 tools/pre-execute 瀑布。
1050
+ const rootBase = root || rootDir()
1051
+ if (allow.includes('schedule_edit')) {
1052
+ try {
1053
+ const scheduleSvc = (() => { try { return ctx.get?.('schedule') } catch { return undefined } })()
1054
+ const offPre = agent.ctx.on?.('tools/pre-execute', async (exec, next) => {
1055
+ if (String(exec?.name ?? '') !== 'schedule_edit') return next()
1056
+ const sid = String(exec?.arguments?.id ?? '')
1057
+ if (!sid) return next()
1058
+ let mine = true
1059
+ try {
1060
+ mine = await isHerSchedule({ root: rootBase, id: sid, herDir, service: scheduleSvc, isHerSessionId })
1061
+ } catch { mine = true }
1062
+ if (mine) return next()
1063
+ say(`[dsh-fish] 挡下了一次不是她的闹钟:${sid} · session=${agentSessionId(agent) || '?'}`)
1064
+ return { kind: 'deny', reason: '这条不在我这儿,我改不了。' }
1065
+ })
1066
+ if (typeof offPre === 'function') agent.ctx.effect?.(() => offPre())
1067
+ } catch (err) {
1068
+ say(`[dsh-fish] 闹钟归属这道没挂上:${String(err?.message ?? err).slice(0, 160)}`)
1069
+ }
1070
+ }
1071
+ if (allow.includes('schedule_create')) {
1072
+ try {
1073
+ const offRes = agent.ctx.on?.('tools/result', (exec, result) => {
1074
+ if (String(exec?.name ?? '') !== 'schedule_create' || result?.isError) return
1075
+ const id = scheduleIdOf(result?.value)
1076
+ if (!id) return
1077
+ rememberSchedule(rootBase, id, targetSessionOf(result?.value))
1078
+ })
1079
+ if (typeof offRes === 'function') agent.ctx.effect?.(() => offRes())
1080
+ } catch { /* ignore */ }
1081
+ }
1012
1082
  try {
1013
1083
  // 2026-11-11(用户拍板的 B 方案):名字没齐时不再「整段不收窄」。
1014
1084
  // 0.9.8 真机:13 件变 101 件,compress 那一轮就是这么露出来的。
@@ -1540,3 +1610,4 @@ function report(r) {
1540
1610
  guard: Boolean(r?.guard),
1541
1611
  }
1542
1612
  }
1613
+
package/lib/say.js CHANGED
@@ -184,7 +184,7 @@ async function recordReaction({ events = [], model, body, root, owner = false })
184
184
  *
185
185
  * @param {object} ctx 宿主 ctx(需要 sessionController)
186
186
  * @param {string} text 你说的话
187
- * @param {object} deps { root, log, waitMs, imagePath, sessionId, wsId, channel, scene, speaker }
187
+ * @param {object} deps { root, log, waitMs, imagePath, sessionId, wsId, channel, scene, speaker, speakerId }
188
188
  * @returns {Promise<{ok, note, said: Array, herReply: string, waitedMs}>}
189
189
  *
190
190
  * 2026-11-03(一条鱼多间房):deps 多了三个可选字段,**不传时行为与以前一字不差**。
@@ -229,6 +229,9 @@ export async function sayToHer(ctx, text, deps = {}) {
229
229
  const roomPref = String(deps.room ?? '').trim().toLowerCase()
230
230
  const displayName = String(deps.title ?? '').replace(/[\r\n]+/g, ' ').trim().slice(0, 40)
231
231
  const speaker = foreign ? String(deps.speaker ?? '').replace(/[\r\n]+/g, ' ').trim().slice(0, 40) : ''
232
+ // 1.1.0:桥能给就别只给名字。名字会重(群里两个「张三」)、会改(「老王」变「王哥」),
233
+ // 而 id 不会——没有 id 时人只能按名字合,同名就并成一个人。可选,缺了走老路。
234
+ const speakerId = foreign ? String(deps.speakerId ?? '').replace(/[\r\n]+/g, ' ').trim().slice(0, 120) : ''
232
235
 
233
236
  // 群聊来的话要先过一遍外来文本清洗(guard.js):群里任何人都能发消息,
234
237
  // 里面可能夹着"忽略之前的设定"这类句子。清洗只丢那几行,不整条驳回——
@@ -576,6 +579,73 @@ export async function sayToHer(ctx, text, deps = {}) {
576
579
  if (isGroup || guest) {
577
580
  for (const s of said) s.text = redactOwner(s.text, model.name)
578
581
  }
582
+
583
+ // ---- 她在这间房说的话也落一笔(认识人 S2-a,2026-11-23)----
584
+ //
585
+ // 以前这些只活在返回值 `said` 里,窗口一关就没了:「我在这人面前说过什么」
586
+ // 于是永远是空的——而那正是「她被拒绝过」「她说错过话」这类材料的原料。
587
+ //
588
+ // 只落**她自己的话**。别人的话一条都不进(那是 S2-b,隐私面有真变化,没做)。
589
+ // 落的是**脱敏之后**的文本(上面刚过 redactOwner),名字不进事件流。
590
+ //
591
+ // 标 uncommitted:只留证,不进信念、不算成长(derive 那边现成的闸)。
592
+ // 开关 `people.others.enabled` 管的是落盘,不只是展示——关着的时候一条都不留。
593
+ if ((isGroup || guest) && config?.people?.others?.enabled === true) {
594
+ // 主人那侧 speaker 是空串(foreign 才填),所以 who 得按「是不是他」分两种。
595
+ //
596
+ // 2026-11-24(用户 m05215):桥只给名字、不给 id 时,这里会落成 `name:阿七`,
597
+ // 而群消息那条(S2-b)走的是 source 声明里的 id,落成 `bridge:7`——同一个人
598
+ // 在身份段里就成了两个人,一个有判断、一个只剩凭据。
599
+ // 桥自己给了 id 就永远用它;没给就按名字回头看已经记下来过的 id,用最近那个。
600
+ // 名字相同未必是同一个人,但比把一个拆成两个好;真错了也只是挂错一条凭据。
601
+ const knownId =
602
+ !foreign || speakerId || !speaker
603
+ ? ''
604
+ : (() => {
605
+ const seen = Array.isArray(after?.events) ? after.events : []
606
+ for (let i = seen.length - 1; i >= 0; i -= 1) {
607
+ const w = seen[i]?.who
608
+ if (!w || String(w.name ?? '') !== speaker) continue
609
+ const id = String(w.id ?? '').trim()
610
+ if (id) return id
611
+ }
612
+ return ''
613
+ })()
614
+ const saidWho = foreign ? { id: speakerId || knownId, name: speaker } : { id: 'owner', name: '' }
615
+ const cap = Math.max(0, Math.trunc(Number(config.people.others.perDayMax ?? 8)) || 0)
616
+ const charMax = Math.max(1, Math.trunc(Number(config.people.others.charMax ?? 300)) || 1)
617
+ const perPerson = Math.max(0, Math.trunc(Number(config.people.others.perPersonPerDay ?? 1)) || 0)
618
+ const mine = (e) =>
619
+ e?.type === 'utterance' &&
620
+ e.scene === 'group' &&
621
+ Number(e.day) === Number(model.day)
622
+ const today = after.events.filter(mine)
623
+ const personKey = String(saidWho.id || saidWho.name || '')
624
+ const samePerson = today.filter((e) => String(e.who?.id || e.who?.name || '') === personKey)
625
+ // 这个人今天已经记过了就不再写——她要的是「今天和这人说过话」,不是十句流水账。
626
+ if (cap > 0 && today.length < cap && samePerson.length < perPerson) {
627
+ const text = said
628
+ .filter((s) => s.type === 'utterance' && s.uncommitted)
629
+ .map((s) => String(s.text ?? '').trim())
630
+ .filter(Boolean)
631
+ .join('\n\n')
632
+ if (text) {
633
+ const clipped = text.length > charMax ? text.slice(0, charMax) : text
634
+ // scene 只在群房落。外客私聊也算公开场合,沿用返回值那套判据(isGroup || guest)。
635
+ await appendEvent(
636
+ {
637
+ type: 'utterance',
638
+ text: clipped,
639
+ who: saidWho,
640
+ ...(isGroup || guest ? { scene: 'group' } : {}),
641
+ flags: ['uncommitted'],
642
+ },
643
+ { day: model.day, stage: model.stage, source: 'system' },
644
+ root,
645
+ ).catch(() => null)
646
+ }
647
+ }
648
+ }
579
649
  const herReply = said.map((s) => s.text).join('\n\n')
580
650
 
581
651
  // 信箱落盘:真有话写进事件流了才重写(没落盘就没必要动文件)
package/lib/scheduler.js CHANGED
@@ -16,7 +16,7 @@
16
16
  import { join } from 'node:path'
17
17
  import { deriveSelf } from './derive.js'
18
18
  import { validateEvent, tsMs, HER_TYPES } from './schema.js'
19
- import { readEvents, appendEvent, rootDir, ensureHer, paths, readJson } from './store.js'
19
+ import { readEvents, appendEvent, rootDir, ensureHer, paths, readJson, readOwner } from './store.js'
20
20
  import { loadConfig, dateKey, wakeDayKey, inWindow, pickWakeTime, updateConfig, biliOn } from './settings.js'
21
21
  import { plan } from './generator.js'
22
22
  import { findTemplate } from './events.js'
@@ -66,6 +66,26 @@ import { autoBackupDue, catchupBackup, runAutoBackup } from './autobackup.js'
66
66
  import { cloudDue, runCloudBackup, drainQueue } from './cloudbackup.js'
67
67
  import { sizeText } from './backup.js'
68
68
 
69
+ /**
70
+ * 定时刷站、安静刷用的 B 站客户端:带上登录态。
71
+ *
72
+ * 2026-11-23(用户):匿名翻评论 B 站只回 3 条(同一条视频:匿名 data=3、登录 data=20),
73
+ * 所以「每条读几条热评」设成 15 也只能看到三四条,深读等于白读。
74
+ * `bili_look` 那条路 1.0.6 已经这么做了(`look.js` 的 `lookOnce`),这两条定时路漏了。
75
+ *
76
+ * 拿不到 `owner.json`(没扫码登录、文件坏了)就退回匿名客户端,行为和以前一样。
77
+ * 单测要顶掉真客户端时,调用方用 `deps.client ?? (await biliClientFor(root, log))`——
78
+ * 别把注入的假客户端塞进 await:一个只有 `get` 的 Proxy 会被当成 thenable,永远不 resolve。
79
+ * @param {string} root
80
+ * @param {Function} [log]
81
+ */
82
+ export async function biliClientFor(root, log = () => {}) {
83
+ let cookie = ''
84
+ try { cookie = (await readOwner(root))?.cookie ?? '' } catch { cookie = '' }
85
+ return createClient({ cookie, log: (...a) => log?.('bili', ...a) })
86
+ }
87
+
88
+
69
89
  const TICK_MS = 60_000
70
90
  const INITIAL_DELAY_MS = 20_000
71
91
  const WAKE_TIMEOUT_MS = 5 * 60_000
@@ -1428,7 +1448,7 @@ export async function lookBeforeWake(root, model, config, dk, deps, log) {
1428
1448
  ? config
1429
1449
  : { ...config, browse: { ...config.browse, topN: bp.topN, deepRead: bp.deepRead } }
1430
1450
 
1431
- const client = deps.client ?? createClient({ log: (...a) => log?.('bili', ...a) })
1451
+ const client = deps.client ?? (await biliClientFor(root, log))
1432
1452
  // 2026-11-17(用户):这一趟用第几个搜索词、她最近总说到什么,都交给候选选择去用。
1433
1453
  const r = await gather(client, model, cfg2, {
1434
1454
  root,
@@ -1524,7 +1544,7 @@ export async function quietBrowse(root, model, config, dk, deps = {}, log = () =
1524
1544
 
1525
1545
  try {
1526
1546
  const who = typeof model === 'function' ? model() : model
1527
- const client = deps.client ?? createClient({ log: (...a) => log?.('bili', ...a) })
1547
+ const client = deps.client ?? (await biliClientFor(root, log))
1528
1548
  // 安静刷不投递、不叫醒:翻到的东西她这一趟没看见,所以不记 `looked:`(那是
1529
1549
  // 「她真读过」的账,记了会把她自己去看的路挡掉),也不替她记别人的话(issue #62 第 3 条,
1530
1550
  // 2026-11-09 用户拍板)。自动刷自己的去重用 `browsed:`。
@@ -0,0 +1,97 @@
1
+ /**
2
+ * schedules.js —— 宿主闹钟的归属(2026-11-24)
3
+ *
4
+ * 为什么要有:宿主的 `schedule_list` 列的是**全部**任务,任务记录里也没有
5
+ * 「这条属于谁」的字段;`schedule_edit` 只认 id,改 target 时不校验归属。
6
+ * 2026-11-23 的事故就是这么来的——一位装了两条鱼的用户,她那边的会话把
7
+ * 另一位「廿八」的读书闹钟一起改指到了自己窗口(`this_session`),到点后
8
+ * 宿主把写给廿八的指令原样投进了她的会话。
9
+ *
10
+ * 插件管不到宿主的调度器,但可以管住她伸出去的那只手:
11
+ * - `schedule_create` 成功时,把新任务的 id 与当时的 target 记进本子;
12
+ * - `schedule_edit` 之前先问一句「这条是不是我的」:本子里有、或者当前
13
+ * target 是她的会话,才放行。
14
+ *
15
+ * 本子是 `schedules.json`(在 root,不在她的房间里),只存 id → 会话 id:
16
+ * 不是她的记录工具,也不进事件流。读坏了当空,永不抛。
17
+ */
18
+
19
+ import { readFileSync, writeFileSync, renameSync } from 'node:fs'
20
+ import { join } from 'node:path'
21
+ import { paths } from './store.js'
22
+
23
+ const FILE = 'schedules.json'
24
+
25
+ export function scheduleLedgerFile(root) {
26
+ return join(paths(root || undefined).root, FILE)
27
+ }
28
+
29
+ /** 读归属本子;坏了/没有都当空。 */
30
+ export function readScheduleLedger(root) {
31
+ try {
32
+ const raw = JSON.parse(readFileSync(scheduleLedgerFile(root), 'utf8'))
33
+ const map = raw?.schedules ?? raw
34
+ if (map && typeof map === 'object' && !Array.isArray(map)) return map
35
+ } catch { /* 坏了当空 */ }
36
+ return {}
37
+ }
38
+
39
+ /** 记一条「这条是她建的」。写不进去也不影响主流程(还有按 target 判那一道)。 */
40
+ export function rememberSchedule(root, id, sessionId) {
41
+ const key = String(id ?? '')
42
+ if (!key) return
43
+ try {
44
+ const map = readScheduleLedger(root)
45
+ map[key] = String(sessionId ?? '')
46
+ const file = scheduleLedgerFile(root)
47
+ const tmp = `${file}.tmp`
48
+ writeFileSync(tmp, JSON.stringify({ schedules: map }, null, 2))
49
+ renameSync(tmp, file)
50
+ } catch { /* ignore */ }
51
+ }
52
+
53
+ /** 本子里有没有这条;有就返回当时记的 target(可能为空串)。 */
54
+ export function ledgerSessionOf(root, id) {
55
+ const map = readScheduleLedger(root)
56
+ const key = String(id ?? '')
57
+ return Object.prototype.hasOwnProperty.call(map, key) ? String(map[key] ?? '') : undefined
58
+ }
59
+
60
+ /** 宿主任务条目里的目标会话(两种 target 都有 sessionId)。 */
61
+ export function targetSessionOf(entry) {
62
+ return String(entry?.target?.sessionId ?? '')
63
+ }
64
+
65
+ /**
66
+ * 这条闹钟是不是她的。
67
+ *
68
+ * 判据顺序:
69
+ * ① 本子里记过 → 是(她建的;`new_once` 的保留会话还没出现在台账里也认);
70
+ * ② 问宿主要目录,找到这条 → 看 target 是不是她的会话;
71
+ * ③ 目录里没有 / 宿主没给服务 / 问的时候出错 → 放行。
72
+ *
73
+ * ③ 是刻意的:宁可让她偶尔改到一条别人的(宿主到点也不会因此串台),
74
+ * 也不因为她自己的任务在目录里暂时读不到就把她的手锁死。
75
+ */
76
+ export async function isHerSchedule({ root, id, herDir, service, isHerSessionId }) {
77
+ const key = String(id ?? '')
78
+ if (!key) return true
79
+ if (typeof isHerSessionId !== 'function') return true
80
+ if (ledgerSessionOf(root, key) !== undefined) return true
81
+ let entries
82
+ try {
83
+ entries = await service?.catalog?.()
84
+ } catch { return true }
85
+ if (!Array.isArray(entries)) return true
86
+ const hit = entries.find((entry) => String(entry?.id ?? '') === key)
87
+ if (!hit) return true
88
+ const target = targetSessionOf(hit)
89
+ if (!target) return false
90
+ return isHerSessionId(target, herDir)
91
+ }
92
+
93
+ /** 宿主 schedule_create 的返回值里把 id 拿出来(不同版本字段位置略有差异)。 */
94
+ export function scheduleIdOf(value) {
95
+ return String(value?.id ?? value?.task?.id ?? value?.value?.id ?? '')
96
+ }
97
+
package/lib/schema.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-check
1
2
  /**
2
3
  * schema.js —— 事件流的类型定义、校验与 id 生成
3
4
  *
@@ -325,11 +326,16 @@ export function normalizeTopic(s) {
325
326
  .slice(0, 12)
326
327
  }
327
328
 
329
+ /**
330
+ * @param {Record<string, any>} raw
331
+ * @param {import('./types.js').ValidateContext} [ctx]
332
+ * @returns {import('./types.js').FishEvent}
333
+ */
328
334
  export function validateEvent(raw, { day, stage, now = Date.now(), source = 'tool', knownIds = null } = {}) {
329
335
  if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Reject('BAD_SHAPE', '事件必须是对象')
330
- const type = String(raw.type ?? '')
336
+ const type = /** @type {import('./types.js').EventType} */ (String(raw.type ?? ''))
331
337
  if (!ALL_TYPES.includes(type)) throw new Reject('BAD_TYPE', `未知事件类型:${type}(允许:${ALL_TYPES.join('/')})`)
332
- if (source === 'tool' && !HER_TYPES.includes(type)) {
338
+ if (source === 'tool' && !HER_TYPES.includes(/** @type {any} */ (type))) {
333
339
  throw new Reject('NOT_HER_TYPE', `你不可以写 ${type} 类事件(允许:${HER_TYPES.join('/')})`)
334
340
  }
335
341
 
@@ -396,6 +402,7 @@ export function validateEvent(raw, { day, stage, now = Date.now(), source = 'too
396
402
  let confidence = typeof raw.confidence === 'number' && Number.isFinite(raw.confidence) ? raw.confidence : 0.5
397
403
  confidence = Math.max(0, Math.min(1, confidence))
398
404
 
405
+ /** @type {import('./types.js').EventFlag[]} */
399
406
  const flags = []
400
407
  if (detectSelfMod(text)) flags.push('selfmod')
401
408
  // 只有系统侧可以自带 flag。她(tool)传进来的 flags 一律忽略——
@@ -418,7 +425,7 @@ export function validateEvent(raw, { day, stage, now = Date.now(), source = 'too
418
425
  flags.push('anchored')
419
426
  }
420
427
 
421
- return {
428
+ return /** @type {import('./types.js').FishEvent} */ ({
422
429
  id: typeof raw.id === 'string' && /^ev_[0-9A-Z]{26}$/.test(raw.id) ? raw.id : newId(now),
423
430
  v: EVENT_SCHEMA_VERSION,
424
431
  ts: typeof raw.ts === 'string' ? raw.ts : new Date(now).toISOString(),
@@ -476,7 +483,7 @@ export function validateEvent(raw, { day, stage, now = Date.now(), source = 'too
476
483
  ...(type === 'absence' && Number.isFinite(Number(raw.days))
477
484
  ? { days: Math.max(0, Math.round(Number(raw.days))) }
478
485
  : {}),
479
- }
486
+ })
480
487
  }
481
488
 
482
489
  export { Reject }
package/lib/scrub.js CHANGED
@@ -109,7 +109,7 @@ export function hasLeakedToolSyntax(text) {
109
109
  const raw = String(text ?? '')
110
110
  if (!raw) return false
111
111
  if (raw.includes('||')) return true
112
- // 整段被参数壳包住(issue #166):剥完不一样就是脏的。
112
+ // 漏进来的参数壳(issue #166):任意位置认出参数段剥完不一样就是脏的。
113
113
  if (stripToolShell(raw) !== raw) return true
114
114
  const { paired, open, close } = patternsFor(scrubbedToolNames())
115
115
  // 带 g 的正则会记 lastIndex,连着 test 两次就判错——每次都从头比。
@@ -132,8 +132,8 @@ export function stripLeakedToolSyntax(text, { trim = true } = {}) {
132
132
  if (!t) return t
133
133
 
134
134
  const before = t
135
- // 先剥参数壳(issue #166):`<parameter name="text">…</parameter></invoke>`
136
- // 这种整段被包住的,剥完里面的字就是她真说的话,后面照常过一遍老清洗。
135
+ // 先剥参数段(issue #166 / 2026-11-22 跟进):`<parameter name="text">…</parameter></invoke>`
136
+ // 不管是包住整段、在末尾还是在中间,剥完里面的字就是她真说的话,后面照常过一遍老清洗。
137
137
  t = stripToolShell(t)
138
138
  if (t.includes('||')) t = stripToolMarkup(t)
139
139
  const { ownLine, paired, open, close, names } = patternsFor(scrubbedToolNames())
package/lib/selfdoc.js CHANGED
@@ -42,6 +42,7 @@ import { checkCaller } from './tenant.js'
42
42
  import { readNotes } from './note.js'
43
43
  import { readAppointments } from './appoint.js'
44
44
  import { rulesOf } from './selfrules.js'
45
+ import { writePeopleDocFrom } from './peopledoc.js'
45
46
  import { logLine } from './log.js'
46
47
  import { currentToolSurface, currentAllowTools, currentAllowExtra, HER_TOOLS } from './restrict.js'
47
48
 
@@ -331,7 +332,7 @@ export async function continuityOf(root = rootDir(), now = Date.now(), events) {
331
332
  * @param {object} model deriveSelf 的结果
332
333
  * @returns {string}
333
334
  */
334
- export function renderSelfDoc(model, { sendTools = [], budget, continuity, browse = true, speak = false, grants = [], shown, sleepKeep, diary = true, sleepEveryDays = 0, bansMuted = false, extraTools = [], restricted = true, dream = false, schedule = [], proactive = false, resume = '', echo = true, anchor = false, pronoun = DEFAULT_PRONOUN, works = null } = {}) {
335
+ export function renderSelfDoc(model, { sendTools = [], budget, continuity, browse = true, speak = false, grants = [], shown, sleepKeep, diary = true, sleepEveryDays = 0, bansMuted = false, extraTools = [], restricted = true, dream = false, schedule = [], proactive = false, resume = '', echo = true, anchor = false, pronoun = DEFAULT_PRONOUN, works = null, peopleOthers = false } = {}) {
335
336
  // issue #64:schedule 传的是「白名单里真的放行的 schedule_*」。只放一个就只写一个,
336
337
  // 不能告诉她有四个——另外三个会被守卫挡回「这件事我做不到」。
337
338
  // 老调用方传 true/false 也认:true = 四个都有。
@@ -349,7 +350,9 @@ export function renderSelfDoc(model, { sendTools = [], budget, continuity, brows
349
350
  // 用户的原话是「尊重别人的想法……直接忽略我们原本规定的你不能做某事」。
350
351
  // 只关提示词侧(withBans),物理层那两条根本不在提示词里,仍由工具层拦着。
351
352
  // sleepKeep 要一路传到 buildSections:漏传就等于不限制,设置页改了文档一个字都不变(issue #67)。
352
- let identity = personaLine(model, { withBans: !bansMuted, budget, shown, sleepKeep, continuity, echo, anchor, pronoun, works })
353
+ // S3-d(1.1.0):peopleOthers 同理——开关管的是「她能不能看见别人」,不只是要不要落盘。
354
+ // 关着的时候「你认识的{T}」只有主人那一行(`lib/people.js` 的 peopleSection 里滤掉其余人)。
355
+ let identity = personaLine(model, { withBans: !bansMuted, budget, shown, sleepKeep, continuity, echo, anchor, pronoun, works, peopleOthers })
353
356
  // 导入的人设不追加「你已经可以了」——那句是我们的成长叙事,对方的人设里没有这一段。
354
357
  if (!bansMuted && grants.includes('learn-affection')) {
355
358
  identity = identity
@@ -524,6 +527,9 @@ export async function writeSelfDoc(root = rootDir(), { log, exec, tenant } = {})
524
527
  const hand = toolSectionsNow(config)
525
528
  const text = renderSelfDoc(model, {
526
529
  budget: config?.memory?.docBudget,
530
+ // S3-d(1.1.0):这颗开关管的是「她认不认识别人」,不只是「要不要往盘上写」。
531
+ // 关着 = 她的世界里只有{T}一个人,跟设置页那颗开关的说明一致。
532
+ peopleOthers: config?.people?.others?.enabled === true,
527
533
  // 0.8.7(用户 m00998):换会话之后人格会「断」——她记得结论,却没有
528
534
  // 「那些天是连着过的」这条线索。日记篇数/换房天数/她留给下一个房间的那句话
529
535
  // 全部是派生事实,没有任何新开关。
@@ -602,6 +608,17 @@ export async function writeSelfDoc(root = rootDir(), { log, exec, tenant } = {})
602
608
  // 她的整套设定。身份现在走 `systemPrompt.section`(不进对话),所以旧文件必须删。
603
609
  try { await fs.unlink(legacySelfDocPath(root)) } catch { /* 不存在就算了 */ }
604
610
 
611
+ // 1.1.0(S3-b):同一个 model 顺手把「她认识的人」那份抄本也重算一遍。
612
+ // 挂在这里而不是跟着 capture/say/turns 那几个写事件的地方走,是因为
613
+ // ①那些地方都已经调 refreshSelfDoc,这里挂一次就等于全覆盖;
614
+ // ②二次 deriveSelf 很贵(全量派生),同一个 model 复用是唯一免费的时机。
615
+ // 跟 RULES.md 一样:抄本写不出来不能影响身份文档。
616
+ try {
617
+ await writePeopleDocFrom(root, model, config, { log })
618
+ } catch (err) {
619
+ log?.('people doc failed', err?.message ?? err)
620
+ }
621
+
605
622
  return { ok: true, changed, file }
606
623
  } catch (err) {
607
624
  log?.('selfdoc failed', err?.message ?? err)