Skip to content

Commit 5e6d280

Browse files
committed
feat(profile): 「Agent 眼中的你」自动事实——分卡维护、注入生效、面板可管
对标 ChatGPT 记忆 / DS Bot 每 bot 一份 MEMORY.md 的设计(2026-11-06 需求, 用户拍板:自动+手动都要、按会话身份分卡+default 兜底)。profileFactsEnabled opt-in 默认关,进 LIGHT_MODE_OFF。 - store:profile_facts / profile_fact_journal / profile_fact_runs 三表(SCHEMA 内 IF NOT EXISTS,免费迁移)。输入行严格按 agent_scope 分区:agent 卡只吃 自己 scope 的行、default 只吃 NULL 行、外来行整体排除。 - 领域模块 src/profile-facts.js:维护轮提 add/update/archive 三种 op(上限 12/轮),单事务落库 + 逐 op 账本(before/after + actor);游标取最近 ok 回执、 失败不推进;无增量输入不调模型、连 noop 回执都不落。LLM 缝全复用 dream.js (resolveRoute/runAuditedLlm/streamText),围栏与「数据非指令」与禁设人格 三段硬约束与 profileExtract 同源(同一份 fence 断链)。 - 注入:renderUserSettingsBlock 增可选 factsProvider,顺序 人格 → 手填画像 → 自动事实 → 规则(设计稿⑥延伸);40 行/1200 字符双帽 + 尾注如实报数;寻址 与 persona 同口径(strict 无卡不注入)。注入器与 /persona 预览传同一 provider,同源纪律不变。手填画像神圣:任何路径不写 user_settings(#497)。 - 接线:dream onRun 成功后 fire-and-forget 逐卡维护(service.enqueue 串行、 不计入 dream 失败退避);三条路由 GET /profile-facts(token-write)、 POST /profile-facts/apply(token-write)、POST /profile-facts/run(token); SECURITY.md 双语清单同步(apply 按先例同时入开放读与受门控清单)。 - 面板:画像卡后新增事实管理卡——卡选择器、行内编辑/归档/添加、最近变更、 回执状态行、「立即更新」按钮与五种结局文案;apply/run 成功都刷新注入预览。 事实域切片锁死无 PUT /profile 字面量(#497 镜像锁)。 - 分享包不含事实(v1 明确出界)。 测试:test/profile-facts.test.js 新增 17 例;client.test.js 面板锁;旗标 计数锁 113→114;docs-config-keys 补 CONFIGURATION.md 行。全量 npm test 2827/2826 pass/0 fail/1 skipped;check-sync 95 一致。
1 parent 5d23058 commit 5e6d280

25 files changed

Lines changed: 2508 additions & 31 deletions

‎SECURITY.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ The token is a lock on the **write** surface, not a wall around the whole API.
153153

154154
- **The real boundary is loopback + the DSH host listener.** The host-side routes (`/api/dsh-mneme/*`) are mounted on the DSH port, which binds `127.0.0.1` and is fenced by the host against DNS rebinding and cross-site browser requests. That fence does **not** establish identity: any local process, and any page running in the host's own origin, is inside the perimeter.
155155
- **Authentication is opt-in and fail-open.** `apiToken` defaults to empty; while it is empty, `isAuthorized()` returns true for every request — reads *and* writes. Setting a token gates the write routes and the sensitive reads; it does not gate plain reads.
156-
- **Open reads (no token even when one is configured)**: `GET /`, `/info`, `/version-check`, `/list`, `/recall-stats`, `/search`, `/semantic`, `/semantic/llm-audit`, `/semantic/llm-audit/stats`, `/semantic/consolidation-stats`, `/semantic/graph/*`, `/entities`, `/memories/entities`, `/dream-status`, `/inject-status`, `/inject-preview`, `/pipeline`, `/candidates`, `/llm-providers`, `/config-keys`, plus the `GET`/`HEAD` branches of `/profile`, `/rules`, `/persona`, `/mode`, `/features`, `/commands`, `/nudge`, of the two share-receive routes `/receive` and `/receive/undo`, and of the four chorography read routes `/choro/repos`, `/choro/modules`, `/choro/page`, `/choro/search` (M1: they read repo-local wiki pages and `choro_*` index tables — not memory rows; with `chorographyEnabled` off they uniformly answer `409 disabled`, the #497 mount-anyway-gate-in-logic pattern). Treat them as readable by anything local. `/receive` and `/receive/undo` belong on this list even though they write: under the `token-write` policy, `needsToken()` exempts `GET`/`HEAD` from the token, so the only thing genuinely open on those two paths is a `405` — no read branch exists. This section mirrors the policy declarations rather than anyone's intent, which is why a write route shows up under "open reads"; `dsh-mneme/test/security-doc.test.js` compares both lists against the routes actually mounted, in both directions.
157-
- **Gated routes (when a token is configured)**: every write — `/create`, `/update`, `/delete`, `/import`, `/bootstrap`, `/vector-config`, `/vector-reindex`, `/runtime/provision`, `/maintenance/reclaim`, `/test-model`, `/conflicts/resolve`, `/profile/extract` (POST-only: one host-LLM call that returns a profile **draft**; other methods get 405 before any side effect), `/receive`, `/receive/undo` (the two share-receive routes write into *this* machine's library, which is why they are `token-write`), and the non-`GET` branches of `/profile`, `/rules`, `/persona`, `/mode`, `/features`, `/commands`, `/nudge` — plus six reads: `/export`, `/external-api`, `/conflicts`, `/health`, `/share`, `/memory/provenance`. `/share` is gated because the share pack is a whole-library read delivered over POST, and it answers 405 to GET — no open read path exists. `/memory/provenance` is gated because it returns the **verbatim raw text** of distilled conversation windows (opt-in via `distillRecords.enabled`); chained with the open list route it would otherwise let any local process enumerate ids and then pull that text back, which reaches the same result as `/export` in a different shape.
156+
- **Open reads (no token even when one is configured)**: `GET /`, `/info`, `/version-check`, `/list`, `/recall-stats`, `/search`, `/semantic`, `/semantic/llm-audit`, `/semantic/llm-audit/stats`, `/semantic/consolidation-stats`, `/semantic/graph/*`, `/entities`, `/memories/entities`, `/dream-status`, `/inject-status`, `/inject-preview`, `/pipeline`, `/candidates`, `/llm-providers`, `/config-keys`, plus the `GET`/`HEAD` branches of `/profile`, `/rules`, `/persona`, `/profile-facts`, `/mode`, `/features`, `/commands`, `/nudge`, of the two share-receive routes `/receive` and `/receive/undo`, of `/profile-facts/apply`, and of the four chorography read routes `/choro/repos`, `/choro/modules`, `/choro/page`, `/choro/search` (M1: they read repo-local wiki pages and `choro_*` index tables — not memory rows; with `chorographyEnabled` off they uniformly answer `409 disabled`, the #497 mount-anyway-gate-in-logic pattern). Treat them as readable by anything local. `/receive` and `/receive/undo` belong on this list even though they write: under the `token-write` policy, `needsToken()` exempts `GET`/`HEAD` from the token, so the only thing genuinely open on those two paths is a `405` — no read branch exists. `/profile-facts/apply` is POST-only for the same reason (its only open branch is a `405`). The `GET` branch of `/profile-facts` stays open even while `profileFactsEnabled` is off: facts survive a switch-off and the panel must be able to show them (same reasoning as `GET /profile`). This section mirrors the policy declarations rather than anyone's intent, which is why a write route shows up under "open reads"; `dsh-mneme/test/security-doc.test.js` compares both lists against the routes actually mounted, in both directions.
157+
- **Gated routes (when a token is configured)**: every write — `/create`, `/update`, `/delete`, `/import`, `/bootstrap`, `/vector-config`, `/vector-reindex`, `/runtime/provision`, `/maintenance/reclaim`, `/test-model`, `/conflicts/resolve`, `/profile/extract` (POST-only: one host-LLM call that returns a profile **draft**; other methods get 405 before any side effect), `/profile-facts/apply` (POST-only: per-fact manual corrections from the panel; writes the `profile_facts` tables, never `user_settings`), `/profile-facts/run` (POST-only, `token` policy like `/profile/extract`: one host-LLM maintenance pass with no read branch; 409 while `profileFactsEnabled` is off), `/receive`, `/receive/undo` (the two share-receive routes write into *this* machine's library, which is why they are `token-write`), and the non-`GET` branches of `/profile`, `/rules`, `/persona`, `/profile-facts`, `/mode`, `/features`, `/commands`, `/nudge` — plus six reads: `/export`, `/external-api`, `/conflicts`, `/health`, `/share`, `/memory/provenance`. `/share` is gated because the share pack is a whole-library read delivered over POST, and it answers 405 to GET — no open read path exists. `/memory/provenance` is gated because it returns the **verbatim raw text** of distilled conversation windows (opt-in via `distillRecords.enabled`); chained with the open list route it would otherwise let any local process enumerate ids and then pull that text back, which reaches the same result as `/export` in a different shape.
158158
- **The standalone data plane** (`127.0.0.1:8790`) is the opposite shape: every route except `GET /health` requires its own Bearer token. Pointing `externalApiHost` at a non-loopback address therefore turns it into a plaintext, token-only HTTP surface — an explicit operator decision.
159159
- **Consequence**: if you expose the plugin beyond loopback (reverse proxy, non-loopback bind), set `apiToken` *and* check that the plain reads above are acceptable to expose — they are not covered by it.
160160
- **How this list stays true**: every route declares its own `policy` (`open` / `token` / `token-write`) at registration, and `createRouteRegistrar` throws when a route has none or reuses a path. A new route that forgets its classification **fails to boot** instead of silently opening. The two lists above are compared against the routes actually mounted by `dsh-mneme/test/security-doc.test.js` in both directions, so adding a route without updating this section fails CI rather than quietly drifting — the matrix is derived from code, not from anyone's memory.
@@ -539,8 +539,8 @@ Token 是**写**面上的锁,不是把整个 API 围起来的墙。
539539

540540
- **真正的边界是回环 + 宿主监听器。** 宿主侧路由(`/api/dsh-mneme/*`)挂在 DSH 端口上,该端口绑 `127.0.0.1`,并由宿主挡掉 DNS rebinding 与跨站浏览器请求。这道围栏**不建立身份**:任何本地进程、以及任何跑在宿主同源页面里的脚本,都在这道围栏之内。
541541
- **鉴权是 opt-in 且 fail-open。** `apiToken` 默认为空;为空期间 `isAuthorized()` 对每个请求都返回 true——读和写都放行。配了 token 之后,它守的是写路由与敏感读,不守普通读。
542-
- **开放读(即使配了 token 也不需要)**:`GET /`、`/info`、`/version-check`、`/list`、`/recall-stats`、`/search`、`/semantic`、`/semantic/llm-audit`、`/semantic/llm-audit/stats`、`/semantic/consolidation-stats`、`/semantic/graph/*`、`/entities`、`/memories/entities`、`/dream-status`、`/inject-status`、`/inject-preview`、`/pipeline`、`/candidates`、`/llm-providers`、`/config-keys`,以及 `/profile`、`/rules`、`/persona`、`/mode`、`/features`、`/commands`、`/nudge`、`/receive`、`/receive/undo`、`/choro/repos`、`/choro/modules`、`/choro/page`、`/choro/search` 的 `GET`/`HEAD` 分支。把它们当作「本地任何东西都能读」。`/receive`、`/receive/undo` 明明是写路由也出现在这一串里,这不是笔误:`token-write` 下 `needsToken()` 对 `GET`/`HEAD` 免 token,所以这两条路径上真正「开放」的只有一个 `405`,压根没有可读分支。`/choro/*` 四条(chorography 仓库地志读侧,M1)读的是**仓库里的 MD 页与 choro_* 索引表**,不是记忆行——内容是代码仓库的公开文档面,`chorographyEnabled` 关着时四条统一回 409 disabled(路由挂着、逻辑层挡,#497 先例),不因为开放读而绕过这个闸。这一节照 policy 声明抄、不照意图写,`dsh-mneme/test/security-doc.test.js` 会拿真实装配出来的路由双向比对这两行。
543-
- **受门控的路由(配了 token 之后)**:全部写操作——`/create`、`/update`、`/delete`、`/import`、`/bootstrap`、`/vector-config`、`/vector-reindex`、`/runtime/provision`、`/maintenance/reclaim`、`/test-model`、`/conflicts/resolve`、`/profile/extract`(只收 POST:花一次宿主 LLM 产一段画像**草稿**,其它方法在产生任何副作用之前就 405)、`/receive`、`/receive/undo`(这两条写的是**本机自己的库**,所以归 `token-write`),以及 `/profile`、`/rules`、`/persona`、`/mode`、`/features`、`/commands`、`/nudge` 的非 `GET` 分支——外加六条读:`/export`、`/external-api`、`/conflicts`、`/health`、`/share`(分享包是「用 POST 递出来的全库读取」,GET 一律 405,没有开放的读分支)、`/memory/provenance`(它回的是蒸馏窗口的**逐字会话原文**,并自 #512 起按窗口带出 `distill_run_id` / `run_type` / `trigger_source` / `run_status` 的运行归因;`distillRecords.enabled` 打开才有;否则把它和上面那个开放的列表路由串起来,任何本地进程都能先枚举 id、再按 id 把那些原文捞回去——可达结果与 `/export`(一次拿全库)同量级,只是换了形状)。
542+
- **开放读(即使配了 token 也不需要)**:`GET /`、`/info`、`/version-check`、`/list`、`/recall-stats`、`/search`、`/semantic`、`/semantic/llm-audit`、`/semantic/llm-audit/stats`、`/semantic/consolidation-stats`、`/semantic/graph/*`、`/entities`、`/memories/entities`、`/dream-status`、`/inject-status`、`/inject-preview`、`/pipeline`、`/candidates`、`/llm-providers`、`/config-keys`,以及 `/profile`、`/rules`、`/persona`、`/profile-facts`、`/mode`、`/features`、`/commands`、`/nudge`、`/receive`、`/receive/undo`、`/profile-facts/apply`、`/choro/repos`、`/choro/modules`、`/choro/page`、`/choro/search` 的 `GET`/`HEAD` 分支。把它们当作「本地任何东西都能读」。`/receive`、`/receive/undo` 明明是写路由也出现在这一串里,这不是笔误:`token-write` 下 `needsToken()` 对 `GET`/`HEAD` 免 token,所以这两条路径上真正「开放」的只有一个 `405`,压根没有可读分支;`/profile-facts/apply` 同理只收 POST,开放的唯一分支是一个 `405`。`/profile-facts` 的 `GET` 分支在 `profileFactsEnabled` 关着时同样开放:开关关掉后存量事实还在库里,面板要能展示(与 `GET /profile` 同一理由)。`/choro/*` 四条(chorography 仓库地志读侧,M1)读的是**仓库里的 MD 页与 choro_* 索引表**,不是记忆行——内容是代码仓库的公开文档面,`chorographyEnabled` 关着时四条统一回 409 disabled(路由挂着、逻辑层挡,#497 先例),不因为开放读而绕过这个闸。这一节照 policy 声明抄、不照意图写,`dsh-mneme/test/security-doc.test.js` 会拿真实装配出来的路由双向比对这两行。
543+
- **受门控的路由(配了 token 之后)**:全部写操作——`/create`、`/update`、`/delete`、`/import`、`/bootstrap`、`/vector-config`、`/vector-reindex`、`/runtime/provision`、`/maintenance/reclaim`、`/test-model`、`/conflicts/resolve`、`/profile/extract`(只收 POST:花一次宿主 LLM 产一段画像**草稿**,其它方法在产生任何副作用之前就 405)、`/profile-facts/apply`(只收 POST:面板的逐条人工修正;写的是 profile_facts 三表,绝不碰 user_settings)、`/profile-facts/run`(只收 POST,`token` 政策与 `/profile/extract` 同款:一次宿主 LLM 维护轮,无读分支;`profileFactsEnabled` 关着时 409)、`/receive`、`/receive/undo`(这两条写的是**本机自己的库**,所以归 `token-write`),以及 `/profile`、`/rules`、`/persona`、`/profile-facts`、`/mode`、`/features`、`/commands`、`/nudge` 的非 `GET` 分支——外加六条读:`/export`、`/external-api`、`/conflicts`、`/health`、`/share`(分享包是「用 POST 递出来的全库读取」,GET 一律 405,没有开放的读分支)、`/memory/provenance`(它回的是蒸馏窗口的**逐字会话原文**,并自 #512 起按窗口带出 `distill_run_id` / `run_type` / `trigger_source` / `run_status` 的运行归因;`distillRecords.enabled` 打开才有;否则把它和上面那个开放的列表路由串起来,任何本地进程都能先枚举 id、再按 id 把那些原文捞回去——可达结果与 `/export`(一次拿全库)同量级,只是换了形状)。
544544
- **独立数据面**(`127.0.0.1:8790`)是反过来的形状:除 `GET /health` 外每条路由都要自己的 Bearer token。因此把 `externalApiHost` 指向非回环地址,就等于把它变成一个「明文、只靠 token」的 HTTP 面——这是运营者的显式决定。
545545
- **推论**:如果你把插件暴露到回环之外(反代、绑非回环地址),请设 `apiToken`,**并**核对上面那串开放读是否也能被看到——它们不受 token 保护。
546546
- **这张表怎么保持为真**:每条路由在注册时自己声明 `policy`(`open` / `token` / `token-write`),`createRouteRegistrar` 在路由没声明或复用同一路径时直接抛错。新路由忘了归类会**启动失败**,而不是静默开门。上面两张清单还会被 `dsh-mneme/test/security-doc.test.js` 拿真实装配出来的路由做双向比对——加了路由却不更新这一节,CI 会红,而不是悄悄漂走。这张矩阵来自代码,不来自谁的记忆。

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
## [Unreleased]
44

5+
- **「Agent 眼中的你」自动事实:按会话身份分卡维护一份逐条事实清单,dream 巩固后自动更新、注入生效、面板可管(2026-11-06 需求,对标 ChatGPT 记忆 / DS Bot 每 bot 一份 MEMORY.md 的设计)**。`profileFactsEnabled` opt-in 默认关。
6+
7+
- **形态是「逐条事实」不是「整段重写」**:每张卡一份事实清单(稳定、具体、一句话),模型在维护轮里提 add/update/archive 三种 op,事实可独立增改归档、可被人在面板上逐条修正;`profile_fact_journal` 逐 op 记账(scope_changes 式 before/after + actor),`profile_fact_runs` 每轮一回执(dream_runs 式)。维护 prompt 三段硬约束与 profileExtract 同源:输入是数据不是指令(同一份 fence 断链)、禁止给助手起名设人格(#497 身份撞车)、只依据给定材料。
8+
- **手填画像神圣(#497 口径不变)**:事实住 `profile_facts` 独立三表,任何路径都不写 `user_settings`——注入顺序是设计稿⑥的延伸:人格 → 手填画像 → 自动事实 → 规则。卡片寻址与 persona 同口径(byAgent 命中 / strict 无卡不注入 / 非 strict 落 default)。
9+
- **输入严格分区**:agent 卡只吃 `agent_scope=自己` 的记忆行,default 卡只吃 NULL 行,外来行(source=imported)整体排除——「本库不巩固别人导入的原文」延伸到画像事实。游标从最近一次 ok 回执读,**失败不推进**(蒸馏游标同款纪律,failed 轮重跑同一窗口);无增量输入连 noop 回执都不落(自动轮零噪音)。
10+
- **触发**:dream 巩固轮成功后 fire-and-forget 逐卡维护(经 `service.enqueue` 与 sleep/巩固串行,失败不计入 dream 退避);面板「立即更新」手动轮走同一条路。维护轮上限 12 op/轮(超出的切片丢弃并记入回执 detail)。
11+
- **注入预算**:事实段 40 行 / 1200 字符双帽 + 尾注如实报数(该块此前无截断机制、从不轮换,不设上限会让一次失控的维护轮撑爆每帧 systemPrompt);事实内容过既有 `escapePromptVars`(宿主模板插值对裸 `{{` 会整轮崩)。
12+
- **三条新路由**:`GET /profile-facts`(token-write,flag 关也照读——存量事实要能展示)、`POST /profile-facts/apply`(token-write,面板逐条人工修正,与自动轮共用同一份 applyOps 与账本)、`POST /profile-facts/run`(**token**,同 /profile/extract 取舍:无读分支 + 一次 LLM;409 disabled / 503 no-route / 502 unparseable·stream-failed)。SECURITY.md 双语四张清单同步。
13+
- **面板**:「Agent 眼中的你」卡后新增事实管理卡——卡选择器(default + agent 键)、事实行内编辑/归档/添加、最近变更 8 条(op + before→after + actor 徽章)、最近一轮回执状态行、维护轮五种结局各有一句人话(ok·N 条 / noop / 功能关 / 先去选模型 / 失败可重试);apply 与 run 成功路径都刷新注入预览(事实段是注入文本的一部分)。功能开关进 enhance 组。
14+
- **分享包不含事实**(v1 明确出界):事实是本实例 agent 对本用户的认知,跨库语义未定。
15+
- **验证**:新增 `test/profile-facts.test.js` 17 例(ops 解析形状、跨卡守卫、去重跳过、no-route/noop/unparseable 可区分、失败不推进游标、重跑即 noop、scope 分区与外来行排除、autoPassCandidates、账本裁剪、注入顺序/strict 寻址/关闸零读数/截断尾注/花括号转义、HTTP 面 405/400/401/409/503/502);`test/client.test.js` 新增面板锁(三路由对齐、五种结局 i18n、卡片位置、事实域切片内无 PUT /profile 字面量——#497 的镜像锁);旗标计数锁 113 → **114**;docs-config-keys 锁补 CONFIGURATION.md 行。全量 `npm test` 2827 tests / 2826 pass / 0 fail / 1 skipped;check-sync 95 一致。
16+
517
- **面板跨组件失效广播:改库动作后记忆列表/实体页/状态工作台即时重取,不再「关面板重开才看到变化」**(用户 2026-11-06 反馈:裁决完冲突要关掉插件面板才能看到败者归档落地)。
618

719
- **病灶不是没刷新,是没广播**:全面板此前唯一的跨组件失效机制是 `mneme:conflicts-changed`(一个派发点、三个状态侧监听者,全是状态页小组件);记忆列表与冲突徽章只靠弹层成功回调、手动刷新按钮与 30s 首页合并续命,实体页与归档工作台干脆只有挂载时取一次。tab 切换本就卸载重挂,所以症状集中在同一次打开的面板内。

0 commit comments

Comments
 (0)