sage-mem 2.25.0 → 2.26.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.
- package/README.en.md +34 -1
- package/README.md +34 -1
- package/lib/assembly/mirror-mount.js +14 -3
- package/lib/client.js +236 -0
- package/lib/index.js +19 -0
- package/lib/intake/merge.js +193 -0
- package/lib/intake/policy.js +238 -0
- package/lib/intake/store.js +247 -0
- package/lib/mirror/mcp.js +53 -6
- package/lib/mirror/policy.js +17 -6
- package/lib/typert/schemas.js +47 -0
- package/lib/typert.host.js +39 -1
- package/package.json +1 -1
package/README.en.md
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
**Cross-session memory for DeepSeek Harness — plain markdown files, no database, no worker.**
|
|
14
14
|
|
|
15
|
-
**Current release: `2.
|
|
15
|
+
**Current release: `2.26.0`** — the npm badge above is the dynamic source of truth. **Contract baseline: `1.0.0`** — the first stable release, whose public commitments, the capability boundaries and the list of things it explicitly cannot do are frozen in the
|
|
16
16
|
[frozen contract](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md).
|
|
17
17
|
|
|
18
18
|
If it is useful to you, a Star is the most direct feedback.
|
|
@@ -589,6 +589,38 @@ Triggering is a 45 second poll plus a 10 second debounce on directory events, so
|
|
|
589
589
|
|
|
590
590
|
**Out of scope on purpose**: the write side (an `intake/` area and a merger for incoming submissions) belongs to 2.26.0; there is no per-agent tiered permission model (ACLs are useless under one OS user, so visibility replaces permissions); there is no endpoint auth and no cross-machine access - use an SSH port forward if you need that.
|
|
591
591
|
|
|
592
|
+
## Incoming submissions: other agents can submit, only you let them in (2.26.0)
|
|
593
|
+
|
|
594
|
+
The read side solved "they cannot see your memories"; this release solves the other direction: **how what they want to tell you gets in**. The answer is a queue, and you approve it entry by entry.
|
|
595
|
+
|
|
596
|
+
The endpoint gains one tool, `memory_submit` (the only write entry point it has). It does **not** write your library: the submission lands in `<state dir>/intake/agents/<source>/pending/` and shows up in the **Incoming submissions** card on the External access page. It is **off** by default: with `external.acceptSubmissions = 0` submissions are rejected outright - "can read the copy" and "can put things in" are two separate switches.
|
|
597
|
+
|
|
598
|
+
```jsonc
|
|
599
|
+
// roughly what they send to memory_submit
|
|
600
|
+
{ "kind": "new", "source": "claude-code",
|
|
601
|
+
"name": "project_tile-cache", "description": "Tile cache hit-rate findings",
|
|
602
|
+
"type": "project", "tags": ["tiles"], "body": "...markdown..." }
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
Two kinds: `kind=new` is a complete new memory (`type` is limited to `project` / `reference`); `kind=patch` is an edit suggestion for an existing entry. Malformed submissions are rejected **at the entry point** and never reach the queue.
|
|
606
|
+
|
|
607
|
+
### Only a new entry can be approved into the library
|
|
608
|
+
|
|
609
|
+
Each pending item carries a proposal in one of four states: **approvable** (a new name), **name conflict** (the library already has that name; reject it or ask for a rename), **needs a human** (an edit suggestion - you get a snippet to copy over), **not applicable** (a suggestion pointing at a memory that does not exist). Approval is an **exclusive create**: if the name is taken it reports a conflict and overwrites nothing.
|
|
610
|
+
|
|
611
|
+
`kind=patch` **never writes the library automatically** - a half-finished patch would destroy the target file. The panel offers a "Copy suggestion" button that copies just the "suggested" section, not the whole patch.
|
|
612
|
+
|
|
613
|
+
Rejecting **does not delete**: the original stays in `intake/agents/<source>/rejected/`, and the reason goes into the report and the audit log (`intake/audit.jsonl`, one line each).
|
|
614
|
+
|
|
615
|
+
| Setting | Default | Description |
|
|
616
|
+
|---|---|---|
|
|
617
|
+
| `external.acceptSubmissions` | `0` (off) | Whether submissions are accepted. While off `memory_submit` is rejected; anything already queued can still be reviewed |
|
|
618
|
+
| Submission size | body <= 32 KB / whole item <= 64 KB | Larger is rejected at the entry point; the body may not carry its own frontmatter |
|
|
619
|
+
| Allowed types | `project` / `reference` | Other agents cannot write `user_*` or `feedback_*` - those are about you, only the main session writes them |
|
|
620
|
+
| After a rejection | original kept in `rejected/` | Never deleted; an approved original is removed, but its full text is already in the report |
|
|
621
|
+
|
|
622
|
+
**Out of scope on purpose**: model-driven auto-merging, automatic rename deduplication, bulk approval (clicking one at a time is the physical guarantee behind "approve first, write later"); `intake/devices/` (cross-device) belongs to 4.0.0.
|
|
623
|
+
|
|
592
624
|
## Configuration Reference
|
|
593
625
|
|
|
594
626
|
### Environment Variables
|
|
@@ -607,6 +639,7 @@ Triggering is a 45 second poll plus a 10 second debounce on directory events, so
|
|
|
607
639
|
| `SAGE_MEM_VALIDITY` | on | master switch for invalidity filtering and contradiction suppression. **Only `0` / `false` / `off` turn it off** (empty or misspelled values count as on); off restores the 1.9.x injection behaviour |
|
|
608
640
|
| `SAGE_MEM_EXTERNAL` | `0` (off) | external access (read side) master switch: `1` exports the copy and opens the read-only endpoint |
|
|
609
641
|
| `SAGE_MEM_EXTERNAL_PORT` | `7461` (1024-65535) | read-only MCP endpoint port, bound to `127.0.0.1` |
|
|
642
|
+
| `SAGE_MEM_EXTERNAL_SUBMIT` | `0` (off) | whether submissions are accepted (`memory_submit`); a separate switch from the one above |`n| `SAGE_MEM_EXTERNAL_SUBMIT` | `0` (off) | whether submissions are accepted (`memory_submit`); a separate switch from the one above |
|
|
610
643
|
|
|
611
644
|
Value semantics: **out-of-range values are clamped to the boundary, illegal values (including pure whitespace) fall back to the default.**
|
|
612
645
|
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
> File-based cross-session memory for DeepSeek Harness — every memory is a plain markdown file you can open, edit and delete. No database, no worker, no port.
|
|
16
16
|
|
|
17
|
-
**当前发布版本:`2.
|
|
17
|
+
**当前发布版本:`2.26.0`** —— 顶部 npm 徽章是动态权威。**契约基线:`1.0.0`** —— 第一个稳定版,公开承诺、能力边界与「明确做不到」的清单冻结在
|
|
18
18
|
[契约冻结](https://github.com/gezi-wen/sage-mem/blob/main/docs/1.0.0-contract.md)。
|
|
19
19
|
|
|
20
20
|
如果它对你有用,给个 Star 是最直接的反馈。
|
|
@@ -591,6 +591,38 @@ sage-mem 给记忆目录的写入加了**版本守卫**:写入前记下当前
|
|
|
591
591
|
|
|
592
592
|
**明确不做**:写侧(外来提交的 `intake/` 与归并器)留给 2.26.0;不做「按 agent 分三档权限」(单用户同 SID 下 ACL 无效,改用「可见性」);不做端点鉴权和跨机器访问 —— 真要跨机器,走 SSH 端口转发。
|
|
593
593
|
|
|
594
|
+
## 外部来件:别的 agent 能提交,进不进库你说了算(2.26.0)
|
|
595
|
+
|
|
596
|
+
读侧解决「它们看不见你的记忆」,这一版解决反方向:**它们想告诉你的事,怎么进来**。答案是只收进队列,主库由你一条条批。
|
|
597
|
+
|
|
598
|
+
端点多了一个工具 `memory_submit`(全端点唯一的写入口)。它**不会**直接写你的记忆库 —— 提交落进 `<状态目录>/intake/agents/<来源>/pending/`,你在「外部访问」页的**外部来件**卡里逐条审。默认**关**:`external.acceptSubmissions = 0` 时提交直接被拒(「能不能读副本」与「能不能往里塞东西」是两个开关)。
|
|
599
|
+
|
|
600
|
+
```jsonc
|
|
601
|
+
// 它们调用 memory_submit 时大概是这个形状
|
|
602
|
+
{ "kind": "new", "source": "claude-code",
|
|
603
|
+
"name": "project_tile-cache", "description": "瓦片缓存的命中率结论",
|
|
604
|
+
"type": "project", "tags": ["瓦片"], "body": "……正文……" }
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
两种提交:`kind=new` 是一条完整新记忆(`type` 只许 `project` / `reference`);`kind=patch` 是对现有条目的修改建议。格式不对**在入口就拒**,连队列都进不去。
|
|
608
|
+
|
|
609
|
+
### 只有「新条目」能被批准落库
|
|
610
|
+
|
|
611
|
+
面板上每条来件带一个提案,四种:**可批准**(新名字)、**同名冲突**(主库已有同名,只能拒绝或让它改名重提)、**需人工**(修改建议,只给一段「建议」让你复制过去)、**不建议收**(修改建议指向一条不存在的记忆)。批准是**排他创建**:同名文件在你就报冲突,一个字节都不覆盖。
|
|
612
|
+
|
|
613
|
+
`kind=patch` **永远不自动写主库** —— 半成品直接落盘会毁掉目标文件。面板给的是「复制建议」按钮,复制的是「建议改成」那一段,不是整份 patch。
|
|
614
|
+
|
|
615
|
+
拒绝**不删**:原件留在 `intake/agents/<来源>/rejected/`,理由进报告与审计(`intake/audit.jsonl` 一行一条)。
|
|
616
|
+
|
|
617
|
+
| 设置项 | 默认值 | 说明 |
|
|
618
|
+
|---|---|---|
|
|
619
|
+
| `external.acceptSubmissions` | `0`(不收) | 收不收外来提交。关着时 `memory_submit` 直接拒,已收到的照样能审 |
|
|
620
|
+
| 提交体积 | 正文 ≤ 32 KB / 整条 ≤ 64 KB | 超了在入口就拒;正文不许自带 frontmatter |
|
|
621
|
+
| 允许的类型 | `project` / `reference` | 外来 agent 写不了 `user_*` 与 `feedback_*` —— 那是关于你本人的,只有主会话能写 |
|
|
622
|
+
| 拒绝之后 | 原件留 `rejected/` | 不删;批准的原件删掉,但全文已进报告 |
|
|
623
|
+
|
|
624
|
+
**明确不做**:模型参与的自动归并、自动改名去重、批量批准(一条条点,才是「先批后写」的物理保证);`intake/devices/`(跨设备)留给 4.0.0。
|
|
625
|
+
|
|
594
626
|
## 配置参考
|
|
595
627
|
|
|
596
628
|
### 环境变量
|
|
@@ -609,6 +641,7 @@ sage-mem 给记忆目录的写入加了**版本守卫**:写入前记下当前
|
|
|
609
641
|
| `SAGE_MEM_VALIDITY` | 开 | 失效过滤与矛盾抑制的总开关。**只认 `0` / `false` / `off` 为关闭**(空值、拼错都按开启);关掉后回到 1.9.x 的注入行为 |
|
|
610
642
|
| `SAGE_MEM_EXTERNAL` | `0`(关) | 外部访问(读侧)总开关:`1` 才导出副本并开只读端点 |
|
|
611
643
|
| `SAGE_MEM_EXTERNAL_PORT` | `7461`(1024–65535) | 只读 MCP 端点端口,只绑 `127.0.0.1` |
|
|
644
|
+
| `SAGE_MEM_EXTERNAL_SUBMIT` | `0`(不收) | 收不收外来提交(`memory_submit`)。与上一个开关是两件事 |
|
|
612
645
|
|
|
613
646
|
取值语义:**越界夹到边界,非法值(含纯空格)回落默认。**
|
|
614
647
|
|
|
@@ -16,6 +16,8 @@ import { MIRROR_DEBOUNCE_MS } from '../mirror/policy.js'
|
|
|
16
16
|
import { createMirrorExporter } from '../mirror/export.js'
|
|
17
17
|
import { createMirrorMcpServer } from '../mirror/mcp.js'
|
|
18
18
|
import { createDirWatcher } from '../memory/watch.js'
|
|
19
|
+
import { createIntakeStore } from '../intake/store.js'
|
|
20
|
+
import { createIntakeMerger } from '../intake/merge.js'
|
|
19
21
|
import { warnLog } from '../memory/util.js'
|
|
20
22
|
|
|
21
23
|
/**
|
|
@@ -27,10 +29,19 @@ import { warnLog } from '../memory/util.js'
|
|
|
27
29
|
*/
|
|
28
30
|
export function mountMirror(ctx, { memoryDir, stateRoot, readExternal }) {
|
|
29
31
|
const exporter = createMirrorExporter({ ctx, memoryDir, stateRoot })
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
// 2.26.0 写侧:提交只进 intake/,归并器只出提案;两者都不在主库之外自己写主库。
|
|
33
|
+
const store = createIntakeStore({ ctx, stateRoot })
|
|
34
|
+
const merger = createIntakeMerger({ ctx, memoryDir, store })
|
|
32
35
|
/** 当前生效设置(null = 还没读过)。 */
|
|
33
36
|
let external = null
|
|
37
|
+
|
|
38
|
+
const mcp = createMirrorMcpServer({
|
|
39
|
+
ctx,
|
|
40
|
+
exporter,
|
|
41
|
+
// 开关现读现用(与读侧同一个 external 段):关着时 memory_submit 直接拒。
|
|
42
|
+
intake: { store, acceptSubmissions: () => !!(external && external.acceptSubmissions) },
|
|
43
|
+
})
|
|
44
|
+
|
|
34
45
|
/** 端点用的端口(-1 = 没起过)。 */
|
|
35
46
|
let mcpPort = -1
|
|
36
47
|
let watcher = null
|
|
@@ -180,5 +191,5 @@ export function mountMirror(ctx, { memoryDir, stateRoot, readExternal }) {
|
|
|
180
191
|
mcpPort = -1
|
|
181
192
|
}
|
|
182
193
|
|
|
183
|
-
return { refresh, status, runOnce, dispose, exporter }
|
|
194
|
+
return { refresh, status, runOnce, dispose, exporter, intake: { store, merger } }
|
|
184
195
|
}
|
package/lib/client.js
CHANGED
|
@@ -2201,6 +2201,44 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2201
2201
|
result: { mode: "strict", typeSymbol: "sage-mem/types#ExternalStatus", schema: objCodec, create: () => objCodec },
|
|
2202
2202
|
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2203
2203
|
},
|
|
2204
|
+
{
|
|
2205
|
+
// 2.26.0:外部来件(写侧)—— 待处理提交 + 每条现算的提案。只读。
|
|
2206
|
+
id: "sage-mem#memory/externalSubmissions",
|
|
2207
|
+
service: "memory",
|
|
2208
|
+
namespace: "memory",
|
|
2209
|
+
method: "externalSubmissions",
|
|
2210
|
+
invocation: { kind: "direct" },
|
|
2211
|
+
parameters: [],
|
|
2212
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#IntakeOverview", schema: objCodec, create: () => objCodec },
|
|
2213
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2214
|
+
},
|
|
2215
|
+
{
|
|
2216
|
+
// 2.26.0:批准一条 new 提交(只有 create 会被接受;patch 永远不自动写)。
|
|
2217
|
+
id: "sage-mem#memory/intakeApprove",
|
|
2218
|
+
service: "memory",
|
|
2219
|
+
namespace: "memory",
|
|
2220
|
+
method: "intakeApprove",
|
|
2221
|
+
invocation: { kind: "direct" },
|
|
2222
|
+
parameters: [
|
|
2223
|
+
{ name: "id", wire: "id", source: "json", codec: { mode: "strict", typeSymbol: "sage-mem/types#SubId", schema: strSchema, create: () => strSchema } },
|
|
2224
|
+
],
|
|
2225
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#IntakeApproveResult", schema: objCodec, create: () => objCodec },
|
|
2226
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2227
|
+
},
|
|
2228
|
+
{
|
|
2229
|
+
// 2.26.0:拒绝一条(原件留档在 intake/…/rejected/,不删)。
|
|
2230
|
+
id: "sage-mem#memory/intakeReject",
|
|
2231
|
+
service: "memory",
|
|
2232
|
+
namespace: "memory",
|
|
2233
|
+
method: "intakeReject",
|
|
2234
|
+
invocation: { kind: "direct" },
|
|
2235
|
+
parameters: [
|
|
2236
|
+
{ name: "id", wire: "id", source: "json", codec: { mode: "strict", typeSymbol: "sage-mem/types#SubId", schema: strSchema, create: () => strSchema } },
|
|
2237
|
+
{ name: "reason", wire: "reason", source: "json", acceptsUndefined: true, codec: { mode: "strict", typeSymbol: "sage-mem/types#SubReason", schema: strSchema, create: () => strSchema } },
|
|
2238
|
+
],
|
|
2239
|
+
result: { mode: "strict", typeSymbol: "sage-mem/types#IntakeRejectResult", schema: objCodec, create: () => objCodec },
|
|
2240
|
+
sourceLocation: { "file": "lib/index.js", "line": 1, "column": 1 },
|
|
2241
|
+
},
|
|
2204
2242
|
{
|
|
2205
2243
|
id: "sage-mem#memory/readRaw",
|
|
2206
2244
|
service: "memory",
|
|
@@ -2921,6 +2959,20 @@ const StarmapRuntime = (function (require, _react) {
|
|
|
2921
2959
|
".smem-ext-row:hover{background:var(--dsw-alias-bg-l2,transparent);}",
|
|
2922
2960
|
'.smem-ext-row--on{border-left-color:var(--dsw-alias-brand-primary,#4d76e6);}',
|
|
2923
2961
|
".smem-ext-name{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:var(--dsw-alias-label-primary);overflow-wrap:anywhere;}",
|
|
2962
|
+
// 2.26.0「外部来件」:一条提交一块,动作按钮靠右;徽标用颜色区分「可批准 / 需人工 / 冲突」。
|
|
2963
|
+
'.smem-intake-list{display:flex;flex-direction:column;gap:8px;}',
|
|
2964
|
+
'.smem-intake-row{display:flex;flex-direction:column;gap:4px;padding:8px 10px;border:1px solid var(--dsw-alias-border-l1);border-radius:8px;}',
|
|
2965
|
+
'.smem-intake-line{display:flex;align-items:center;gap:8px;flex-wrap:wrap;font-size:12px;}',
|
|
2966
|
+
'.smem-intake-src{font-weight:600;color:var(--dsw-alias-label-primary);}',
|
|
2967
|
+
'.smem-intake-name{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:var(--dsw-alias-label-secondary);overflow-wrap:anywhere;}',
|
|
2968
|
+
'.smem-intake-badge{border:1px solid var(--dsw-alias-border-l2);border-radius:999px;padding:1px 8px;font-size:11px;color:var(--dsw-alias-label-secondary);}',
|
|
2969
|
+
'.smem-intake-badge--create{border-color:var(--dsw-alias-brand-primary,#4d76e6);color:var(--dsw-alias-brand-primary,#4d76e6);}',
|
|
2970
|
+
'.smem-intake-badge--conflict{border-color:var(--dsw-alias-static-red-600,#c62828);color:var(--dsw-alias-static-red-600,#c62828);}',
|
|
2971
|
+
'.smem-intake-why{font-size:11.5px;color:var(--dsw-alias-label-secondary);}',
|
|
2972
|
+
'.smem-intake-excerpt{font-size:12px;color:var(--dsw-alias-label-primary);overflow-wrap:anywhere;}',
|
|
2973
|
+
'.smem-intake-actions{display:flex;gap:6px;flex-wrap:wrap;margin-top:2px;}',
|
|
2974
|
+
'.smem-intake-reports{display:flex;flex-direction:column;gap:8px;margin-top:8px;}',
|
|
2975
|
+
'.smem-intake-report{margin:0;padding:8px;border-radius:6px;background:var(--dsw-alias-bg-l2,transparent);font-size:11.5px;white-space:pre-wrap;overflow-wrap:anywhere;max-height:220px;overflow:auto;}',
|
|
2924
2976
|
].join("\n");
|
|
2925
2977
|
|
|
2926
2978
|
/**
|
|
@@ -4310,6 +4362,30 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
4310
4362
|
"ext.err.noSettings": "宿主侧还没有外部访问接口(插件版本较旧)",
|
|
4311
4363
|
"ext.err.rejected": "保存被拒绝",
|
|
4312
4364
|
"ext.err.port": "端口要填 1024–65535 的整数",
|
|
4365
|
+
// ── 2.26.0 外部来件(写侧)──────────────────────────────────
|
|
4366
|
+
"ext.intake.title": "外部来件",
|
|
4367
|
+
"ext.intake.pending": "待处理 {count} 条",
|
|
4368
|
+
"ext.intake.what1": "别的 agent 提交过来的记忆与修改建议会先落在这里,插件**不会**自动写进主库。",
|
|
4369
|
+
"ext.intake.what2": "「新条目」可以批准落库(排他创建,不覆盖同名);「修改建议」只给一段建议,要你复制过去手工并入 —— 半成品直接写会毁掉目标文件。",
|
|
4370
|
+
"ext.intake.empty": "现在没有待处理的来件。",
|
|
4371
|
+
"ext.intake.approve": "批准并写入",
|
|
4372
|
+
"ext.intake.reject": "拒绝",
|
|
4373
|
+
"ext.intake.rejectReason": "在面板里拒绝",
|
|
4374
|
+
"ext.intake.copySuggestion": "复制建议",
|
|
4375
|
+
"ext.intake.kind.new": "新条目",
|
|
4376
|
+
"ext.intake.kind.patch": "修改建议",
|
|
4377
|
+
"ext.intake.action.create": "可批准",
|
|
4378
|
+
"ext.intake.action.conflict": "同名冲突",
|
|
4379
|
+
"ext.intake.action.manual": "需人工",
|
|
4380
|
+
"ext.intake.action.reject": "不建议收",
|
|
4381
|
+
"ext.intake.notice.approved": "已写入 {file}",
|
|
4382
|
+
"ext.intake.notice.rejected": "已拒绝(原件留在 rejected/ 里)",
|
|
4383
|
+
"ext.intake.notice.copied": "建议已复制,去目标文件里粘上",
|
|
4384
|
+
"ext.intake.reports.show": "最近报告 {count} 份",
|
|
4385
|
+
"ext.intake.reports.hide": "收起报告",
|
|
4386
|
+
"ext.intake.err.noRemote": "宿主侧还没有外部来件接口(插件版本较旧)",
|
|
4387
|
+
"ext.intake.err.rejected": "处理被拒绝",
|
|
4388
|
+
"ext.intake.err.noClipboard": "剪贴板不可用,请手动复制",
|
|
4313
4389
|
"lab.title": "MMR 去重(实验性)",
|
|
4314
4390
|
"lab.switch": "启用 MMR 去重",
|
|
4315
4391
|
"lab.switch.onTitle": "现在开启:注入时优先让不同主题的记忆占名额",
|
|
@@ -5015,6 +5091,29 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
5015
5091
|
"ext.err.noSettings": "The host has no external-access API yet (older plugin version)",
|
|
5016
5092
|
"ext.err.rejected": "The save was rejected",
|
|
5017
5093
|
"ext.err.port": "Port must be an integer between 1024 and 65535",
|
|
5094
|
+
"ext.intake.title": "Incoming submissions",
|
|
5095
|
+
"ext.intake.pending": "{count} pending",
|
|
5096
|
+
"ext.intake.what1": "Memories and edit suggestions submitted by other agents land here first. The plugin **never** writes them into your library on its own.",
|
|
5097
|
+
"ext.intake.what2": "A new entry can be approved into the library (created exclusively, never overwriting an existing name). An edit suggestion only produces a snippet you can copy over by hand - writing a half-finished patch would destroy the target file.",
|
|
5098
|
+
"ext.intake.empty": "Nothing pending right now.",
|
|
5099
|
+
"ext.intake.approve": "Approve and write",
|
|
5100
|
+
"ext.intake.reject": "Reject",
|
|
5101
|
+
"ext.intake.rejectReason": "rejected from the panel",
|
|
5102
|
+
"ext.intake.copySuggestion": "Copy suggestion",
|
|
5103
|
+
"ext.intake.kind.new": "New entry",
|
|
5104
|
+
"ext.intake.kind.patch": "Edit suggestion",
|
|
5105
|
+
"ext.intake.action.create": "Approvable",
|
|
5106
|
+
"ext.intake.action.conflict": "Name conflict",
|
|
5107
|
+
"ext.intake.action.manual": "Needs a human",
|
|
5108
|
+
"ext.intake.action.reject": "Not applicable",
|
|
5109
|
+
"ext.intake.notice.approved": "Written to {file}",
|
|
5110
|
+
"ext.intake.notice.rejected": "Rejected (the original stays in rejected/)",
|
|
5111
|
+
"ext.intake.notice.copied": "Suggestion copied - paste it into the target file",
|
|
5112
|
+
"ext.intake.reports.show": "Last {count} reports",
|
|
5113
|
+
"ext.intake.reports.hide": "Hide reports",
|
|
5114
|
+
"ext.intake.err.noRemote": "The host has no incoming-submissions API yet (older plugin version)",
|
|
5115
|
+
"ext.intake.err.rejected": "The action was rejected",
|
|
5116
|
+
"ext.intake.err.noClipboard": "Clipboard unavailable, please copy manually",
|
|
5018
5117
|
"lab.title": "MMR de-duplication (experimental)",
|
|
5019
5118
|
"lab.switch": "Enable MMR de-duplication",
|
|
5020
5119
|
"lab.switch.onTitle": "On: injection prefers memories on different topics for the same slots",
|
|
@@ -8175,6 +8274,143 @@ h("span", { key: "sep", className: "smem-actions-sep", "aria-hidden": "true" }),
|
|
|
8175
8274
|
: null,
|
|
8176
8275
|
]),
|
|
8177
8276
|
|
|
8277
|
+
// ── 2.26.0 写侧:外部来件 ──
|
|
8278
|
+
h(ExternalIntakeCard, { key: "intake", ctx: props.ctx }),
|
|
8279
|
+
|
|
8280
|
+
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
8281
|
+
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8282
|
+
]);
|
|
8283
|
+
}
|
|
8284
|
+
|
|
8285
|
+
/**
|
|
8286
|
+
* 「外部来件」卡(2.26.0 写侧)—— 别的 agent 提交过来的东西,一条一条等人批。
|
|
8287
|
+
*
|
|
8288
|
+
* 三条不许变的产品规则:
|
|
8289
|
+
* 1. **先批后写**:插件不会自动把任何外来内容写进主库;`patch` 更是永远不自动写,
|
|
8290
|
+
* 只给一段「建议」,由人复制过去。
|
|
8291
|
+
* 2. **提案每次现算**(宿主侧算):主库随时会变,缓存一份提案等于让面板说旧话。
|
|
8292
|
+
* 3. **拒绝不删**:原件留在 `intake/agents/<来源>/rejected/`,理由进报告。
|
|
8293
|
+
*/
|
|
8294
|
+
function ExternalIntakeCard(props) {
|
|
8295
|
+
const remote = stableRemote(props.ctx, "remote.memory");
|
|
8296
|
+
const sOv = react.useState(null); const ov = sOv[0], setOv = sOv[1];
|
|
8297
|
+
const sBusy = react.useState(""); const busy = sBusy[0], setBusy = sBusy[1];
|
|
8298
|
+
const sErr = react.useState(null); const err = sErr[0], setErr = sErr[1];
|
|
8299
|
+
const sNotice = react.useState(""); const notice = sNotice[0], setNotice = sNotice[1];
|
|
8300
|
+
const sReports = react.useState(false); const reportsOpen = sReports[0], setReportsOpen = sReports[1];
|
|
8301
|
+
|
|
8302
|
+
function load() {
|
|
8303
|
+
if (!remote || typeof remote.externalSubmissions !== "function") {
|
|
8304
|
+
setErr(t("ext.intake.err.noRemote"));
|
|
8305
|
+
return;
|
|
8306
|
+
}
|
|
8307
|
+
Promise.resolve(remote.externalSubmissions()).then(function (res) {
|
|
8308
|
+
setOv(unwrap(res) || { pending: 0, items: [], scanError: "", reports: [] });
|
|
8309
|
+
}, function (e) { setErr(String((e && e.message) || e)); });
|
|
8310
|
+
}
|
|
8311
|
+
|
|
8312
|
+
react.useEffect(function () { load(); }, []);
|
|
8313
|
+
|
|
8314
|
+
/** 批准 / 拒绝一条。批准只对 `create` 有效,其余动作宿主会回原因。 */
|
|
8315
|
+
function act(kind, id) {
|
|
8316
|
+
const fn = kind === "approve" ? remote && remote.intakeApprove : remote && remote.intakeReject;
|
|
8317
|
+
if (typeof fn !== "function") { setErr(t("ext.intake.err.noRemote")); return; }
|
|
8318
|
+
setBusy(id); setErr(null); setNotice("");
|
|
8319
|
+
const args = kind === "approve" ? [id] : [id, t("ext.intake.rejectReason")];
|
|
8320
|
+
Promise.resolve(fn.apply(remote, args)).then(function (out) {
|
|
8321
|
+
setBusy("");
|
|
8322
|
+
const r = unwrap(out) || out || {};
|
|
8323
|
+
if (r.ok === false) { setErr(r.error || t("ext.intake.err.rejected")); load(); return; }
|
|
8324
|
+
setNotice(kind === "approve"
|
|
8325
|
+
? t("ext.intake.notice.approved", { file: r.targetFile || "" })
|
|
8326
|
+
: t("ext.intake.notice.rejected"));
|
|
8327
|
+
load();
|
|
8328
|
+
}, function (e) { setBusy(""); setErr(String((e && e.message) || e)); });
|
|
8329
|
+
}
|
|
8330
|
+
|
|
8331
|
+
/** patch 的「复制建议」:只复制「建议改成」那一段,不复制整份 patch。 */
|
|
8332
|
+
function copySuggestion(text) {
|
|
8333
|
+
const clip = typeof navigator !== "undefined" ? navigator.clipboard : null;
|
|
8334
|
+
if (!clip || typeof clip.writeText !== "function") { setErr(t("ext.intake.err.noClipboard")); return; }
|
|
8335
|
+
Promise.resolve(clip.writeText(String(text || ""))).then(function () {
|
|
8336
|
+
setNotice(t("ext.intake.notice.copied"));
|
|
8337
|
+
}, function () { setErr(t("ext.intake.err.noClipboard")); });
|
|
8338
|
+
}
|
|
8339
|
+
|
|
8340
|
+
const items = (ov && ov.items) || [];
|
|
8341
|
+
const reports = (ov && ov.reports) || [];
|
|
8342
|
+
const ACTION_LABEL = {
|
|
8343
|
+
create: t("ext.intake.action.create"),
|
|
8344
|
+
conflict: t("ext.intake.action.conflict"),
|
|
8345
|
+
manual: t("ext.intake.action.manual"),
|
|
8346
|
+
reject: t("ext.intake.action.reject"),
|
|
8347
|
+
};
|
|
8348
|
+
|
|
8349
|
+
return h("div", { className: "smem-intake smem-module-card" }, [
|
|
8350
|
+
h("div", { className: "smem-modcard-head", key: "head" }, [
|
|
8351
|
+
h("span", { className: "smem-modcard-title", key: "t" }, t("ext.intake.title")),
|
|
8352
|
+
h("span", { className: "smem-modcard-status", key: "s" }, t("ext.intake.pending", { count: items.length })),
|
|
8353
|
+
]),
|
|
8354
|
+
|
|
8355
|
+
h("div", { className: "smem-autodream-sec", key: "what" }, [
|
|
8356
|
+
h("div", { className: "smem-autodream-note", key: "p1" }, t("ext.intake.what1")),
|
|
8357
|
+
h("div", { className: "smem-autodream-note", key: "p2" }, t("ext.intake.what2")),
|
|
8358
|
+
]),
|
|
8359
|
+
|
|
8360
|
+
ov && ov.scanError ? h("div", { className: "smem-err", key: "scan" }, "⚠ " + ov.scanError) : null,
|
|
8361
|
+
|
|
8362
|
+
items.length === 0
|
|
8363
|
+
? h("div", { className: "smem-muted", key: "empty" }, t("ext.intake.empty"))
|
|
8364
|
+
: h("div", { className: "smem-intake-list", key: "list" }, items.map(function (it) {
|
|
8365
|
+
const disabled = busy === it.id;
|
|
8366
|
+
const buttons = [];
|
|
8367
|
+
if (it.action === "create") {
|
|
8368
|
+
buttons.push(h("button", {
|
|
8369
|
+
type: "button", className: "smem-btn smem-btn-primary", key: "ok",
|
|
8370
|
+
disabled: disabled || !!ov.scanError,
|
|
8371
|
+
onClick: function () { act("approve", it.id); },
|
|
8372
|
+
}, t("ext.intake.approve")));
|
|
8373
|
+
}
|
|
8374
|
+
if (it.action === "manual" && it.suggestion) {
|
|
8375
|
+
buttons.push(h("button", {
|
|
8376
|
+
type: "button", className: "smem-btn", key: "copy",
|
|
8377
|
+
disabled: disabled,
|
|
8378
|
+
onClick: function () { copySuggestion(it.suggestion); },
|
|
8379
|
+
}, t("ext.intake.copySuggestion")));
|
|
8380
|
+
}
|
|
8381
|
+
buttons.push(h("button", {
|
|
8382
|
+
type: "button", className: "smem-btn", key: "no",
|
|
8383
|
+
disabled: disabled,
|
|
8384
|
+
onClick: function () { act("reject", it.id); },
|
|
8385
|
+
}, t("ext.intake.reject")));
|
|
8386
|
+
|
|
8387
|
+
return h("div", { className: "smem-intake-row", key: it.id }, [
|
|
8388
|
+
h("div", { className: "smem-intake-line", key: "l" }, [
|
|
8389
|
+
h("span", { className: "smem-intake-src", key: "src" }, it.source || "?"),
|
|
8390
|
+
h("span", { className: "smem-muted", key: "kind" }, it.kind === "patch" ? t("ext.intake.kind.patch") : t("ext.intake.kind.new")),
|
|
8391
|
+
h("span", { className: "smem-intake-name", key: "n" }, it.targetFile || it.name || ""),
|
|
8392
|
+
h("span", { className: "smem-intake-badge smem-intake-badge--" + String(it.action || ""), key: "a" }, ACTION_LABEL[it.action] || String(it.action || "")),
|
|
8393
|
+
]),
|
|
8394
|
+
h("div", { className: "smem-intake-why", key: "w" }, it.reason || ""),
|
|
8395
|
+
it.excerpt ? h("div", { className: "smem-intake-excerpt", key: "e" }, it.excerpt) : null,
|
|
8396
|
+
h("div", { className: "smem-intake-actions", key: "b" }, buttons),
|
|
8397
|
+
]);
|
|
8398
|
+
})),
|
|
8399
|
+
|
|
8400
|
+
reports.length > 0
|
|
8401
|
+
? h("div", { className: "smem-autodream-sec", key: "reports" }, [
|
|
8402
|
+
h("button", {
|
|
8403
|
+
type: "button", className: "smem-btn", key: "toggle",
|
|
8404
|
+
onClick: function () { setReportsOpen(!reportsOpen); },
|
|
8405
|
+
}, reportsOpen ? t("ext.intake.reports.hide") : t("ext.intake.reports.show", { count: reports.length })),
|
|
8406
|
+
reportsOpen
|
|
8407
|
+
? h("div", { className: "smem-intake-reports", key: "list" }, reports.map(function (r) {
|
|
8408
|
+
return h("pre", { className: "smem-intake-report", key: r.name }, r.text);
|
|
8409
|
+
}))
|
|
8410
|
+
: null,
|
|
8411
|
+
])
|
|
8412
|
+
: null,
|
|
8413
|
+
|
|
8178
8414
|
err ? h("div", { className: "smem-err", key: "err" }, "⚠ " + err) : null,
|
|
8179
8415
|
notice ? h("div", { className: "smem-ok", key: "ok" }, "✓ " + notice) : null,
|
|
8180
8416
|
]);
|
package/lib/index.js
CHANGED
|
@@ -536,6 +536,21 @@ export class MemoryGateway extends TypertRemoteService {
|
|
|
536
536
|
return this.mirror.status()
|
|
537
537
|
}
|
|
538
538
|
|
|
539
|
+
/** 外部来件(2.26.0):待处理提交 + 每条现算的提案 + 最近报告。只读。 */
|
|
540
|
+
async externalSubmissions() {
|
|
541
|
+
return this.mirror.intake.merger.overview()
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** 批准一条 `new`:排他写主库 + 补索引。`conflict` / `manual` / `patch` 一律不写,回原因。 */
|
|
545
|
+
async intakeApprove(id) {
|
|
546
|
+
return this.mirror.intake.merger.approve(id)
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** 拒绝一条:原件留档 `rejected/`(不删),理由进报告与审计。 */
|
|
550
|
+
async intakeReject(id, reason) {
|
|
551
|
+
return this.mirror.intake.merger.reject(id, reason)
|
|
552
|
+
}
|
|
553
|
+
|
|
539
554
|
/**
|
|
540
555
|
* 原样读保留名文件(`MEMORY.md` / `session-log.md` / 用户声明的保护名)。
|
|
541
556
|
*
|
|
@@ -606,6 +621,10 @@ markRemote(MemoryGateway, 'getSettings', 'getSettings')
|
|
|
606
621
|
markRemote(MemoryGateway, 'setSettings', 'setSettings')
|
|
607
622
|
// 2.25.0:外部访问读侧的运行状态(第 41 条)。只读、无入参 —— 面板不看账,只看出没出问题。
|
|
608
623
|
markRemote(MemoryGateway, 'externalStatus', 'externalStatus')
|
|
624
|
+
// 2.26.0:写侧三条 —— 来件总览 / 批准 / 拒绝。批准是唯一能把外来内容写进主库的入口。
|
|
625
|
+
markRemote(MemoryGateway, 'externalSubmissions', 'externalSubmissions')
|
|
626
|
+
markRemote(MemoryGateway, 'intakeApprove', 'intakeApprove')
|
|
627
|
+
markRemote(MemoryGateway, 'intakeReject', 'intakeReject')
|
|
609
628
|
markRemote(MemoryGateway, 'readRaw', 'readRaw')
|
|
610
629
|
markRemote(MemoryGateway, 'writeRaw', 'writeRaw')
|
|
611
630
|
// 1.7.0:注入调试账(只读;开关关着时返回 { enabled: false, records: [] })
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* intake 层 —— **批准之后**唯一能写主库的地方。
|
|
3
|
+
*
|
|
4
|
+
* 四条纪律:
|
|
5
|
+
* 1. **只有 `create` 能落库**。`patch` 永远不自动写(半成品直接写会毁目标文件),
|
|
6
|
+
* 它只产出一份「建议」,由人复制过去。
|
|
7
|
+
* 2. **排他创建**:写入用 `writeFileExclusive`。写之前查一次主库、写完再看一眼冲突 ——
|
|
8
|
+
* 两步之间被人抢先建了同名文件时报冲突、不覆盖。
|
|
9
|
+
* 3. **判据不在本文件**:合不合法在 `policy.validateSubmission`,该走哪条处置在
|
|
10
|
+
* `policy.proposeFor`。这里只负责「照着提案动手」。
|
|
11
|
+
* 4. **没有第二套写入协议**:落库走既有的 `writeFileExclusive` 与 `ensureIndexEntry`,
|
|
12
|
+
* 不绕过版本守卫,也不另写一份索引逻辑。
|
|
13
|
+
*/
|
|
14
|
+
import { readFile } from 'node:fs/promises'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
import { parseFrontmatter, stripFrontmatterBlock } from '../memory/frontmatter.js'
|
|
17
|
+
import { frontmatterField } from '../memory/frontmatter.js'
|
|
18
|
+
import { ensureIndexEntry } from '../memory/index-file.js'
|
|
19
|
+
import { RESERVED_FILES } from '../memory/config.js'
|
|
20
|
+
import { scanMemoryDirDetailed } from '../memory/scan.js'
|
|
21
|
+
import { warnLog, writeFileExclusive } from '../memory/util.js'
|
|
22
|
+
import { proposeFor } from './policy.js'
|
|
23
|
+
|
|
24
|
+
/** 从落盘的提交 markdown 里还原出「批准时要写什么」。 */
|
|
25
|
+
function payloadOf(raw) {
|
|
26
|
+
const meta = parseFrontmatter(raw)
|
|
27
|
+
return {
|
|
28
|
+
name: meta.name || frontmatterField(raw, 'name'),
|
|
29
|
+
description: meta.description || '',
|
|
30
|
+
type: meta.type || '',
|
|
31
|
+
tags: meta.tags || [],
|
|
32
|
+
body: stripFrontmatterBlock(raw),
|
|
33
|
+
source: frontmatterField(raw, 'source'),
|
|
34
|
+
submittedAt: frontmatterField(raw, 'submitted_at'),
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 生成写进主库的正文:干净 frontmatter + 原正文(外来那层的 `kind` 等字段不搬进来)。 */
|
|
39
|
+
function renderMemoryFile(payload, atISO) {
|
|
40
|
+
const lines = ['---', `name: ${payload.name}`, `description: ${JSON.stringify(payload.description)}`, `type: ${payload.type}`]
|
|
41
|
+
if (payload.tags.length) lines.push(`tags: [${payload.tags.join(', ')}]`)
|
|
42
|
+
lines.push(`source: ${payload.source || 'external'}`)
|
|
43
|
+
lines.push(`imported_at: ${atISO}`)
|
|
44
|
+
lines.push(`updated: ${String(payload.submittedAt || atISO).slice(0, 10)}`, '---', '')
|
|
45
|
+
lines.push(String(payload.body).replace(/^\s+/, '').replace(/\s+$/, ''))
|
|
46
|
+
lines.push('')
|
|
47
|
+
return lines.join('\n')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 一段正文用于列表预览(压平空白、截断)。 */
|
|
51
|
+
function excerptOf(text, max = 200) {
|
|
52
|
+
const flat = String(text ?? '').replace(/\s+/g, ' ').trim()
|
|
53
|
+
return flat.length > max ? `${flat.slice(0, max)}…` : flat
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {object} opts
|
|
58
|
+
* @param {object} opts.ctx — 宿主上下文(`warnLog` 用)
|
|
59
|
+
* @param {string} opts.memoryDir — 主库目录(**本文件唯一写它的地方**)
|
|
60
|
+
* @param {object} opts.store — `createIntakeStore()` 的产物
|
|
61
|
+
*/
|
|
62
|
+
export function createIntakeMerger({ ctx, memoryDir, store }) {
|
|
63
|
+
/** 主库现有文件名(一次扫描,供提案与冲突判定共用)。 */
|
|
64
|
+
async function existingNames() {
|
|
65
|
+
const scanned = await scanMemoryDirDetailed(ctx, '', memoryDir)
|
|
66
|
+
if (scanned.complete === false) return { ok: false, names: [], error: '主库目录读不出来,本轮不判冲突' }
|
|
67
|
+
// 目录不存在也不能当「空库」放行:写进去会失败,而且「盘掉线」与「新装」在这里长得一样。
|
|
68
|
+
if (scanned.missing === true) return { ok: false, names: [], error: '主库目录不存在,本轮不判冲突(也不落库)' }
|
|
69
|
+
return { ok: true, names: scanned.files.map((f) => f.file), error: '' }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 面板要的一份总览:待处理条目 + 每条的提案 + 最近报告。
|
|
74
|
+
* 提案**每次现算**(不落盘):主库随时可能变,缓存提案等于让面板说旧话。
|
|
75
|
+
*/
|
|
76
|
+
async function overview({ reportLimit = 5 } = {}) {
|
|
77
|
+
const [items, scan, reports] = await Promise.all([store.listPending(), existingNames(), store.listReports(reportLimit)])
|
|
78
|
+
const out = items.map((item) => {
|
|
79
|
+
const kind = frontmatterField(item.raw, 'kind') || 'new'
|
|
80
|
+
const body = stripFrontmatterBlock(item.raw)
|
|
81
|
+
const proposal = proposeFor(
|
|
82
|
+
kind === 'patch'
|
|
83
|
+
? { kind: 'patch', name: item.name }
|
|
84
|
+
: { kind: 'new', name: item.name },
|
|
85
|
+
scan.names,
|
|
86
|
+
)
|
|
87
|
+
return {
|
|
88
|
+
id: item.id,
|
|
89
|
+
source: item.source,
|
|
90
|
+
kind,
|
|
91
|
+
name: item.name || '',
|
|
92
|
+
description: item.description || '',
|
|
93
|
+
type: item.type || '',
|
|
94
|
+
submittedAt: item.submittedAt || '',
|
|
95
|
+
action: proposal.action,
|
|
96
|
+
targetFile: proposal.targetFile,
|
|
97
|
+
reason: proposal.reason,
|
|
98
|
+
// 列表只给预览;完整原文在报告里、也在 pending 文件里。
|
|
99
|
+
excerpt: excerptOf(kind === 'patch' ? body.replace(/^## +要改的地方/m, '') : body),
|
|
100
|
+
// patch 的「建议改成」单独给一份:面板上的「复制建议」要的就是这一段(
|
|
101
|
+
// 复制整份 patch 过去反而要人再删两段)。
|
|
102
|
+
suggestion: kind === 'patch' ? String(body.split(/## +建议改成/)[1] ?? '').split(/## +理由/)[0].trim().slice(0, 4000) : '',
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
return {
|
|
106
|
+
pending: out.length,
|
|
107
|
+
items: out,
|
|
108
|
+
scanError: scan.ok ? '' : scan.error,
|
|
109
|
+
reports: reports.map((r) => ({ name: r.name, text: r.text.slice(0, 2000) })),
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 批准一条 `new` 提交:落进主库 + 补索引 + 记账 + 写报告。
|
|
115
|
+
*
|
|
116
|
+
* 失败一律**不删 pending**(人可以再试或改成拒绝),也不留半份文件:
|
|
117
|
+
* 记忆写入与索引补齐是两步,索引失败只告警(记忆已在盘上,索引是可选件)。
|
|
118
|
+
*
|
|
119
|
+
* @param {string} id
|
|
120
|
+
* @returns {Promise<{ok:boolean, action?:string, targetFile?:string, error?:string, report?:string}>}
|
|
121
|
+
*/
|
|
122
|
+
async function approve(id) {
|
|
123
|
+
const item = await store.findPending(id)
|
|
124
|
+
if (!item) return { ok: false, error: '找不到这条待处理提交(可能已被处理)' }
|
|
125
|
+
if (await store.isProcessed(item.id)) return { ok: false, error: '这条提交已经处理过了' }
|
|
126
|
+
|
|
127
|
+
const raw = await readFile(join(store.intakeDir, 'agents', item.agentId, 'pending', `${item.id}.md`), 'utf8').catch(() => null)
|
|
128
|
+
if (raw === null) return { ok: false, error: '读不到提交正文(文件可能已被移走)' }
|
|
129
|
+
// patch **永远不自动写**:它不是一份成品,直接落盘会毁掉目标文件。这里先拦,不靠面板禁用按钮。
|
|
130
|
+
if ((frontmatterField(raw, 'kind') || 'new') === 'patch') {
|
|
131
|
+
return { ok: false, action: 'manual', error: '这是修改建议(patch):插件不会自动写主库,请在面板里复制建议后手工并入' }
|
|
132
|
+
}
|
|
133
|
+
const payload = payloadOf(raw)
|
|
134
|
+
const scan = await existingNames()
|
|
135
|
+
if (!scan.ok) return { ok: false, error: scan.error }
|
|
136
|
+
const proposal = proposeFor({ kind: 'new', name: payload.name }, scan.names)
|
|
137
|
+
if (proposal.action !== 'create') {
|
|
138
|
+
return { ok: false, action: proposal.action, targetFile: proposal.targetFile, error: proposal.reason }
|
|
139
|
+
}
|
|
140
|
+
// 用户声明的保护名(`reserved.json`)在真落盘前再兜一道:policy 只认识两个内置名。
|
|
141
|
+
if (RESERVED_FILES.has(String(proposal.targetFile).toLowerCase())) {
|
|
142
|
+
return { ok: false, action: 'reject', targetFile: proposal.targetFile, error: '目标是保留名(索引 / 流水 / 用户声明的保护名),不接受外来写入' }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const at = Date.now()
|
|
146
|
+
const atISO = new Date(at).toISOString()
|
|
147
|
+
try {
|
|
148
|
+
await writeFileExclusive(join(memoryDir, proposal.targetFile), renderMemoryFile(payload, atISO))
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (err?.code === 'EEXIST') {
|
|
151
|
+
return { ok: false, action: 'conflict', targetFile: proposal.targetFile, error: '目标文件已经存在(两次检查之间新建的),未覆盖' }
|
|
152
|
+
}
|
|
153
|
+
return { ok: false, error: `写主库失败(${err?.code ?? err?.message ?? err})` }
|
|
154
|
+
}
|
|
155
|
+
const indexed = await ensureIndexEntry(memoryDir, proposal.targetFile).catch((err) => {
|
|
156
|
+
warnLog(ctx, `外来提交:补索引失败(${err?.message ?? err})`)
|
|
157
|
+
return { ok: false }
|
|
158
|
+
})
|
|
159
|
+
if (indexed && indexed.ok === false) warnLog(ctx, `外来提交:补索引失败(${indexed.error})`)
|
|
160
|
+
|
|
161
|
+
await store.consume(item.id)
|
|
162
|
+
await store.markProcessed(item.id, { action: 'approve', target: proposal.targetFile, note: payload.source })
|
|
163
|
+
const report = await store.writeReport({
|
|
164
|
+
submission: { kind: 'new', name: payload.name, source: payload.source, body: payload.body },
|
|
165
|
+
proposal,
|
|
166
|
+
result: `已写入主库:${proposal.targetFile}(索引 ${indexed && indexed.added ? '已补' : '未变或无索引'})`,
|
|
167
|
+
id: item.id,
|
|
168
|
+
at,
|
|
169
|
+
})
|
|
170
|
+
await store.audit({ at, source: payload.source, action: 'approve', target: proposal.targetFile, result: 'written' })
|
|
171
|
+
return { ok: true, action: 'create', targetFile: proposal.targetFile, report }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 拒绝一条提交(原件留档在 `rejected/`,不删)。 */
|
|
175
|
+
async function reject(id, reason = '') {
|
|
176
|
+
const item = await store.findPending(id)
|
|
177
|
+
if (!item) return { ok: false, error: '找不到这条待处理提交(可能已被处理)' }
|
|
178
|
+
const kind = frontmatterField(item.raw, 'kind') || 'new'
|
|
179
|
+
const scan = await existingNames()
|
|
180
|
+
const proposal = proposeFor({ kind, name: item.name }, scan.names)
|
|
181
|
+
const result = await store.reject(item.id, reason)
|
|
182
|
+
if (!result.ok) return result
|
|
183
|
+
await store.writeReport({
|
|
184
|
+
submission: { kind, name: item.name, source: item.source, body: stripFrontmatterBlock(item.raw) },
|
|
185
|
+
proposal,
|
|
186
|
+
result: `已拒绝${reason ? `:${reason}` : ''}`,
|
|
187
|
+
id: item.id,
|
|
188
|
+
})
|
|
189
|
+
return { ok: true, id: item.id }
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return { overview, approve, reject, existingNames }
|
|
193
|
+
}
|