rugu-core 0.0.0-stage → 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +8 -2
- package/package.json +52 -4
- package/src/admin/derive.js +114 -0
- package/src/admin/index.js +377 -0
- package/src/admin/notify.js +26 -0
- package/src/admin/params.js +17 -0
- package/src/admin/purge.js +604 -0
- package/src/admin/retention.js +174 -0
- package/src/admin/review.js +505 -0
- package/src/admin/rules.js +243 -0
- package/src/admin/simulate.js +106 -0
- package/src/admin/suppress.js +384 -0
- package/src/admin/views.js +583 -0
- package/src/api.js +527 -0
- package/src/backup/crypto.js +197 -0
- package/src/backup/export.js +71 -0
- package/src/backup/format.js +255 -0
- package/src/backup/index.js +296 -0
- package/src/backup/packs.js +610 -0
- package/src/backup/restore.js +318 -0
- package/src/backup/sqlite-task.js +403 -0
- package/src/backup/thread.js +48 -0
- package/src/backup/util.js +149 -0
- package/src/config/defaults.js +846 -0
- package/src/consolidate/communities.js +195 -0
- package/src/consolidate/correct.js +482 -0
- package/src/consolidate/generalize.js +270 -0
- package/src/consolidate/growth.js +321 -0
- package/src/consolidate/impressions.js +262 -0
- package/src/consolidate/index.js +154 -0
- package/src/consolidate/lessons.js +171 -0
- package/src/consolidate/memory.js +371 -0
- package/src/consolidate/params.js +60 -0
- package/src/consolidate/reflect.js +191 -0
- package/src/consolidate/self.js +144 -0
- package/src/consolidate/sleep.js +434 -0
- package/src/consolidate/storyline.js +199 -0
- package/src/consolidate/time.js +163 -0
- package/src/consolidate/today.js +229 -0
- package/src/consolidate/util.js +266 -0
- package/src/context/diag.js +181 -0
- package/src/context/index.js +261 -0
- package/src/context/ledger.js +465 -0
- package/src/context/pack.js +197 -0
- package/src/context/people.js +139 -0
- package/src/context/recall.js +1147 -0
- package/src/context/resident.js +382 -0
- package/src/context/text.js +94 -0
- package/src/context/working.js +624 -0
- package/src/contract/enums.js +56 -0
- package/src/contract/errors.js +38 -0
- package/src/contract/ids.js +67 -0
- package/src/contract/index.js +7 -0
- package/src/contract/setting.js +45 -0
- package/src/contract/types.js +369 -0
- package/src/contract/validate.js +604 -0
- package/src/core-types.js +242 -0
- package/src/discretion/egress.js +830 -0
- package/src/discretion/facts.js +539 -0
- package/src/discretion/gate.js +324 -0
- package/src/discretion/glance.js +633 -0
- package/src/discretion/index.js +506 -0
- package/src/discretion/knowledge.js +473 -0
- package/src/discretion/match.js +131 -0
- package/src/discretion/norms.js +771 -0
- package/src/discretion/stage1.js +400 -0
- package/src/discretion/stage2.js +633 -0
- package/src/discretion/told.js +330 -0
- package/src/discretion/util.js +207 -0
- package/src/discretion/working.js +319 -0
- package/src/dynamics/actr.js +216 -0
- package/src/dynamics/index.js +767 -0
- package/src/encode/extract.js +715 -0
- package/src/encode/index.js +568 -0
- package/src/encode/lessons.js +567 -0
- package/src/encode/parse.js +60 -0
- package/src/encode/plans.js +400 -0
- package/src/encode/prompt.js +372 -0
- package/src/encode/reconcile.js +199 -0
- package/src/encode/review.js +177 -0
- package/src/encode/segment.js +235 -0
- package/src/encode/taint.js +64 -0
- package/src/encode/tools.js +513 -0
- package/src/encode/util.js +264 -0
- package/src/encode/verify.js +539 -0
- package/src/encode/write.js +720 -0
- package/src/entities/impressions.js +133 -0
- package/src/entities/index.js +662 -0
- package/src/entities/links.js +213 -0
- package/src/entities/matcher.js +125 -0
- package/src/entities/normalize.js +92 -0
- package/src/entities/own.js +56 -0
- package/src/entities/params.js +22 -0
- package/src/entities/records.js +330 -0
- package/src/entities/resolve.js +878 -0
- package/src/index.js +8 -0
- package/src/intake/hash.js +118 -0
- package/src/intake/index.js +1827 -0
- package/src/intake/media.js +219 -0
- package/src/intake/sanitize.js +118 -0
- package/src/intake/windows.js +271 -0
- package/src/lang/index.js +355 -0
- package/src/lang/text.js +142 -0
- package/src/lang/time.js +405 -0
- package/src/lang/unicode.js +59 -0
- package/src/lang/zh.js +470 -0
- package/src/maintenance.js +83 -0
- package/src/ports/index.js +191 -0
- package/src/retrieve/background.js +180 -0
- package/src/retrieve/cues.js +256 -0
- package/src/retrieve/fuse.js +85 -0
- package/src/retrieve/hydrate.js +171 -0
- package/src/retrieve/index.js +257 -0
- package/src/retrieve/level.js +138 -0
- package/src/retrieve/names.js +56 -0
- package/src/retrieve/params.js +40 -0
- package/src/retrieve/past.js +70 -0
- package/src/retrieve/pipeline.js +801 -0
- package/src/retrieve/prefetch.js +160 -0
- package/src/retrieve/render.js +411 -0
- package/src/retrieve/routes.js +467 -0
- package/src/retrieve/util.js +138 -0
- package/src/retrieve/working.js +640 -0
- package/src/store/db.js +274 -0
- package/src/store/fts.js +127 -0
- package/src/store/index.js +63 -0
- package/src/store/invariants.js +48 -0
- package/src/store/jobs.js +116 -0
- package/src/store/keys.js +52 -0
- package/src/store/kv.js +60 -0
- package/src/store/migrate.js +67 -0
- package/src/store/migrations/001-store.js +81 -0
- package/src/store/migrations/002-intake.js +142 -0
- package/src/store/migrations/003-entities.js +130 -0
- package/src/store/migrations/004-encode.js +189 -0
- package/src/store/migrations/005-dynamics.js +59 -0
- package/src/store/migrations/006-discretion.js +184 -0
- package/src/store/migrations/007-context.js +57 -0
- package/src/store/migrations/008-consolidate.js +60 -0
- package/src/store/migrations/009-worker.js +54 -0
- package/src/store/migrations/010-admin.js +85 -0
- package/src/store/migrations/011-backup.js +42 -0
- package/src/store/migrations/012-discretion.js +19 -0
- package/src/store/migrations/013-discretion.js +44 -0
- package/src/store/migrations/014-discretion.js +16 -0
- package/src/store/migrations/015-intake.js +24 -0
- package/src/store/migrations/016-intake.js +16 -0
- package/src/store/migrations/017-consolidate.js +38 -0
- package/src/store/migrations/index.js +27 -0
- package/src/store/refs.js +59 -0
- package/src/store/vectors.js +121 -0
- package/src/worker/client.js +97 -0
- package/src/worker/engine.js +432 -0
- package/src/worker/graph.js +259 -0
- package/src/worker/index.js +1885 -0
- package/src/worker/maint.js +83 -0
- package/src/worker/probes.js +25 -0
- package/src/worker/quant.js +135 -0
- package/src/worker/thread.js +57 -0
- package/src/worker/vindex.js +183 -0
- package/test/eval/memory-eval.js +400 -0
- package/test/eval/memory-eval.test.js +19 -0
- package/test/fakes/budget.js +45 -0
- package/test/fakes/clock.js +29 -0
- package/test/fakes/embedding.js +106 -0
- package/test/fakes/index.js +9 -0
- package/test/fakes/llm.js +73 -0
- package/test/fakes/logger.js +24 -0
- package/test/fakes/ports.js +27 -0
- package/test/fakes/self-profile.js +19 -0
- package/test/fakes/test-core.js +84 -0
- package/test/fixtures/fake-platform.js +226 -0
- package/test/fixtures/paraphrase-pairs.js +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 QiuYeDx
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# rugu-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[如故(dsh-rugu)](https://github.com/QiuYeDx/dsh-rugu)的记忆核心。一个人设只有一段人生:感知入库、认人认物、情节与事实、激活与遗忘、五路回想、分寸判断。
|
|
4
|
+
|
|
5
|
+
它不认识任何宿主和入口(零宿主依赖,Node ≥ 22.16,`node:sqlite`),由宿主实现 LLM、嵌入、重排、预算这些端口再接上来。一般不需要直接装它:在 DeepSeek Harness 里装 `dsh-rugu` 就会带上。
|
|
6
|
+
|
|
7
|
+
接口说明见仓库的 [docs/实现/模块与接口.md](https://github.com/QiuYeDx/dsh-rugu/blob/main/docs/实现/模块与接口.md)。
|
|
8
|
+
|
|
9
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rugu-core",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "如故的记忆核心:一个人设只有一段人生——感知入库、认人认物、情节与事实、激活与遗忘、五路回想、分寸判断。不认识任何宿主和入口。",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"memory",
|
|
7
|
+
"persona",
|
|
8
|
+
"agent-memory",
|
|
9
|
+
"rugu"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "QiuYeDx",
|
|
13
|
+
"homepage": "https://github.com/QiuYeDx/dsh-rugu#readme",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/QiuYeDx/dsh-rugu.git",
|
|
17
|
+
"directory": "packages/core"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/QiuYeDx/dsh-rugu/issues"
|
|
21
|
+
},
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./src/index.js",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./src/index.js",
|
|
26
|
+
"./contract": "./src/contract/index.js",
|
|
27
|
+
"./testing": "./test/fakes/index.js",
|
|
28
|
+
"./testing/fake-platform": "./test/fixtures/fake-platform.js",
|
|
29
|
+
"./package.json": "./package.json",
|
|
30
|
+
"./testing/memory-eval": "./test/eval/memory-eval.js"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"src",
|
|
34
|
+
"test/fakes",
|
|
35
|
+
"test/fixtures",
|
|
36
|
+
"test/eval",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.16"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@node-rs/jieba": "*"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"@node-rs/jieba": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"test": "node ../../scripts/run-tests.mjs core"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// 派生关系(隐藏的级联、彻底删除的展开、墓碑重删共用)。只读:主库上走 encode.derivedFrom(契约),
|
|
2
|
+
// 解开的备份副本上(或 encode 不可用时)用同样语义的 SQL。
|
|
3
|
+
// - 证据感知:一条记忆是从哪几条感知来的(sources.percept_id;情节另加归进它的感知;窗口是它的成员)。
|
|
4
|
+
// - 窗口成员看 members_json(intake 按(场合,visible_to)分别切窗口,D53:同一段 id 范围里可能是别的窗口)。
|
|
5
|
+
import { refOf } from '../store/refs.js';
|
|
6
|
+
|
|
7
|
+
/** 各主表「这一行是什么时候有的」那一列:墓碑按时间生效(D42),主表 id 不是 AUTOINCREMENT,删掉最新的一行之后新行会拿到同一个 id。 */
|
|
8
|
+
export const CREATED = Object.freeze({
|
|
9
|
+
percept: 'created_at', window: 'created_at', episode: 'created_at', assertion: 'created_at', entity: 'created_at',
|
|
10
|
+
impression: 'built_at', intention: 'created_at', storyline: 'updated_at', community: 'built_at', link: 'at',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
const tableCache = new WeakMap();
|
|
14
|
+
function cacheOf(db) {
|
|
15
|
+
let m = tableCache.get(db);
|
|
16
|
+
if (!m) { m = new Map(); tableCache.set(db, m); }
|
|
17
|
+
return m;
|
|
18
|
+
}
|
|
19
|
+
export function hasTable(db, name) {
|
|
20
|
+
const m = cacheOf(db);
|
|
21
|
+
if (!m.has(name)) m.set(name, !!db.get("SELECT 1 FROM sqlite_schema WHERE name = ? AND type IN ('table')", name));
|
|
22
|
+
return m.get(name);
|
|
23
|
+
}
|
|
24
|
+
export function hasColumn(db, table, col) {
|
|
25
|
+
const key = `${table}.${col}`;
|
|
26
|
+
const m = cacheOf(db);
|
|
27
|
+
if (!m.has(key)) m.set(key, hasTable(db, table) && db.all(`PRAGMA table_info(${table})`).some((c) => c.name === col));
|
|
28
|
+
return m.get(key);
|
|
29
|
+
}
|
|
30
|
+
const parseIds = (s) => {
|
|
31
|
+
try { const v = JSON.parse(s ?? '[]'); return Array.isArray(v) ? v.filter((x) => Number.isSafeInteger(x)) : []; } catch { return []; }
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** 和 encode.derivedFrom 同样语义的 SQL(备份副本上用):别的证据 = 别的感知,或没有感知的导入 / 推断 / 她自己生成的来源。 */
|
|
35
|
+
function derivedSql(db, perceptId) {
|
|
36
|
+
if (!hasTable(db, 'sources')) return [];
|
|
37
|
+
const out = db.all(`SELECT s.owner_kind AS kind, s.owner_id AS id,
|
|
38
|
+
(SELECT count(*) FROM sources s2 WHERE s2.owner_kind = s.owner_kind AND s2.owner_id = s.owner_id
|
|
39
|
+
AND ((s2.percept_id IS NOT NULL AND s2.percept_id <> ?)
|
|
40
|
+
OR (s2.percept_id IS NULL AND s2.mode IN ('imported','inferred','self_generated')))) AS others
|
|
41
|
+
FROM sources s WHERE s.percept_id = ? GROUP BY s.owner_kind, s.owner_id`, perceptId, perceptId)
|
|
42
|
+
.map((r) => ({ ref: refOf(r.kind, r.id), onlySource: Number(r.others) === 0 }));
|
|
43
|
+
if (hasColumn(db, 'percepts', 'episode_id')) {
|
|
44
|
+
const p = db.get('SELECT episode_id FROM percepts WHERE id = ?', perceptId);
|
|
45
|
+
if (p?.episode_id && !out.some((x) => x.ref === refOf('episode', p.episode_id))) {
|
|
46
|
+
const n = Number(db.get('SELECT count(*) AS n FROM percepts WHERE episode_id = ? AND id <> ?', p.episode_id, perceptId).n);
|
|
47
|
+
out.push({ ref: refOf('episode', p.episode_id), onlySource: n === 0 });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 以这条感知为来源的记忆。
|
|
55
|
+
* @param {object} core
|
|
56
|
+
* @param {import('../store/db.js').Store} db
|
|
57
|
+
* @param {number} perceptId
|
|
58
|
+
* @returns {{ref: string, onlySource: boolean}[]}
|
|
59
|
+
*/
|
|
60
|
+
export function derivedOf(core, db, perceptId) {
|
|
61
|
+
if (db === core.store) {
|
|
62
|
+
try { const r = core.mods.encode.derivedFrom(core, perceptId); if (Array.isArray(r)) return r; } catch { /* 退回 SQL */ }
|
|
63
|
+
}
|
|
64
|
+
return derivedSql(db, perceptId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 包含这条感知的原文窗口(看成员;没有成员记录的老窗口按 id 范围算,只在同一个场合里找)。 */
|
|
68
|
+
export function windowsContaining(db, perceptId) {
|
|
69
|
+
if (!hasTable(db, 'raw_windows')) return [];
|
|
70
|
+
const p = hasTable(db, 'percepts') ? db.get('SELECT setting_key FROM percepts WHERE id = ?', perceptId) : null;
|
|
71
|
+
const rows = p
|
|
72
|
+
? db.all('SELECT id, members_json FROM raw_windows WHERE setting_key = ? AND first_percept <= ? AND last_percept >= ?', p.setting_key, perceptId, perceptId)
|
|
73
|
+
: db.all('SELECT id, members_json FROM raw_windows WHERE first_percept <= ? AND last_percept >= ?', perceptId, perceptId);
|
|
74
|
+
const out = [];
|
|
75
|
+
for (const w of rows) {
|
|
76
|
+
const members = parseIds(w.members_json);
|
|
77
|
+
if (members.length ? members.includes(perceptId) : !!p) out.push(w.id);
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 窗口的成员感知。 */
|
|
83
|
+
export function windowMembers(db, windowId) {
|
|
84
|
+
const w = hasTable(db, 'raw_windows') ? db.get('SELECT members_json FROM raw_windows WHERE id = ?', windowId) : null;
|
|
85
|
+
if (!w) return [];
|
|
86
|
+
const members = parseIds(w.members_json);
|
|
87
|
+
if (members.length) return members;
|
|
88
|
+
return hasColumn(db, 'percepts', 'window_id') ? db.all('SELECT id FROM percepts WHERE window_id = ? ORDER BY id', windowId).map((r) => r.id) : [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 一条记忆的证据感知。
|
|
93
|
+
* @param {import('../store/db.js').Store} db
|
|
94
|
+
* @param {{kind: string, id: number}} m
|
|
95
|
+
* @returns {number[]}
|
|
96
|
+
*/
|
|
97
|
+
export function evidencePercepts(db, m) {
|
|
98
|
+
if (m.kind === 'percept') return [m.id];
|
|
99
|
+
if (m.kind === 'window') return windowMembers(db, m.id);
|
|
100
|
+
const ids = new Set();
|
|
101
|
+
if (hasTable(db, 'sources')) {
|
|
102
|
+
for (const r of db.all('SELECT DISTINCT percept_id AS p FROM sources WHERE owner_kind = ? AND owner_id = ? AND percept_id IS NOT NULL', m.kind, m.id)) ids.add(r.p);
|
|
103
|
+
}
|
|
104
|
+
if (m.kind === 'episode' && hasColumn(db, 'percepts', 'episode_id')) {
|
|
105
|
+
for (const r of db.all('SELECT id FROM percepts WHERE episode_id = ?', m.id)) ids.add(r.id);
|
|
106
|
+
}
|
|
107
|
+
return [...ids].sort((a, b) => a - b);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 情节的种类(event / summary / diary / lapse / import)。 */
|
|
111
|
+
export function episodeKind(db, id) {
|
|
112
|
+
if (!hasColumn(db, 'episodes', 'ep_kind')) return null;
|
|
113
|
+
return db.get('SELECT ep_kind FROM episodes WHERE id = ?', id)?.ep_kind ?? null;
|
|
114
|
+
}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// admin 模块(§4.5 AdminApi、§9.3.7「需要你知道」、§12.6 保留期、§12.7 遗忘和删除、§13.1 管理页的数据):治理。
|
|
2
|
+
// - 管理接口只给主人:鉴权由集成层负责(Fetch 路由 + principal.kind === 'owner',§13.1),这里假定调用方就是主人,
|
|
3
|
+
// 但会记下 by(谁操作的)。
|
|
4
|
+
// - 规则是数据不是代码:「别记」、主人的披露规则、按场合的开 / 关,目标是实体、信息类型或场合属性,不是入口名。
|
|
5
|
+
// - 隐藏 / 忘掉统一记在 suppressions(所有记忆单元通用);彻底删除按来源链级联,**先删向量再删主库**,写墓碑,中途崩溃可以安全重跑。
|
|
6
|
+
// - 彻底删除和原文到期删除是唯一允许删除别的模块的表里的行的操作(级联规则见 docs/实现/模块与接口.md §2.3)。
|
|
7
|
+
// - 保留期、成长方式、预算这类设置只能在管理页改(§9.6);缩短保留期要确认语 + 7 天冷静期 + 到期前先备份(§12.6)。
|
|
8
|
+
// 拥有的表:review_queue、rules、suppressions、tombstones、need_to_know。
|
|
9
|
+
// 用到的端口:clock、logger。
|
|
10
|
+
// 契约细节:docs/实现/模块与接口.md §4.12。实现分在 rules / review / suppress / purge / retention / views / simulate 几个文件。
|
|
11
|
+
import * as rules from './rules.js';
|
|
12
|
+
import * as review from './review.js';
|
|
13
|
+
import * as sup from './suppress.js';
|
|
14
|
+
import * as purgeMod from './purge.js';
|
|
15
|
+
import * as retention from './retention.js';
|
|
16
|
+
import * as views from './views.js';
|
|
17
|
+
import * as sim from './simulate.js';
|
|
18
|
+
import { parseRef } from '../store/refs.js';
|
|
19
|
+
import { getMetaJson, setMetaJson } from '../store/kv.js';
|
|
20
|
+
import { ContractError } from '../contract/errors.js';
|
|
21
|
+
|
|
22
|
+
/** @param {import('../core-types.js').CoreContext} core */
|
|
23
|
+
export function init(core) {
|
|
24
|
+
core.state.admin = { rules: new Map(), dnr: null, modes: new Map() };
|
|
25
|
+
}
|
|
26
|
+
/** @param {import('../core-types.js').CoreContext} core */
|
|
27
|
+
export function dispose(core) { /* 无资源 */ }
|
|
28
|
+
|
|
29
|
+
// ── 浏览和模拟 ────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 管理页的列表(记忆、人与物、场合、经历、意图、分寸日志、注入日志……)。q.view 选哪一类,其余是筛选条件和分页游标。
|
|
33
|
+
* 返回的条目只含主人能看的内容(主人看得到全部,包括隐藏的,§9.3.7 第 3 条)。
|
|
34
|
+
* view:memories(kind='episode' 时是情节)/ assertions / episodes / entities / settings / percepts / windows / intentions / discretion /
|
|
35
|
+
* injections / utterances / holds / review / needToKnow / rules / suppressed / forgotten / tombstones / backups / drills。
|
|
36
|
+
* 分页:limit、next(上一页返回的游标)。
|
|
37
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
38
|
+
* @param {{view: string, [k: string]: unknown}} q
|
|
39
|
+
* @returns {import('../contract/types.js').Page}
|
|
40
|
+
*/
|
|
41
|
+
export function list(core, q) { return views.list(core, q); }
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 一条的详情:来源链、流动规范、谁知道、告诉过谁、版本链、同类的其他几次、注入过它的会话。
|
|
45
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
46
|
+
* @param {string} ref
|
|
47
|
+
* @returns {object|null}
|
|
48
|
+
*/
|
|
49
|
+
export function get(core, ref) { return views.get(core, ref); }
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 模拟器(§13.1):「如果 X 在 Y 说了 Z」,只计算召回和分寸,不注入、不写感知、不加痕迹(判定记录照常写,标模拟)。
|
|
53
|
+
* 【补】s.refs:直接判这几条记忆(不经召回);s.mode:'involuntary'(默认)/ 'deliberate'。
|
|
54
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
55
|
+
* @param {{settingId: string, as: import('../contract/types.js').ActorRef, text: string}} s
|
|
56
|
+
* @returns {Promise<import('../contract/types.js').SimResult>}
|
|
57
|
+
*/
|
|
58
|
+
export async function simulate(core, s) { return sim.simulate(core, s); }
|
|
59
|
+
|
|
60
|
+
// ── 审核队列 ──────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 排一条审核(各模块都通过它写 review_queue)。payload 只放引用和结构化字段,不放原文。
|
|
64
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
65
|
+
* @param {{kind: 'forget_request'|'owner_op'|'merge'|'growth'|'held_utterance'|'norm_change'|'setting_rule_relax'|'withdraw',
|
|
66
|
+
* payload: object, reason: string, expiresAt?: number|null}} item
|
|
67
|
+
* @returns {number}
|
|
68
|
+
*/
|
|
69
|
+
export function enqueueReview(core, item) { return review.enqueueReview(core, item); }
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
73
|
+
* @param {{status?: string, kind?: string}} [q]
|
|
74
|
+
* @returns {import('../contract/types.js').ReviewItem[]}
|
|
75
|
+
*/
|
|
76
|
+
export function listReview(core, q) { return review.listReview(core, q ?? {}); }
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 主人的决定,按 kind 交给拥有者执行:forget_request → suppress / restore;merge → entities.link(decided_by='owner');
|
|
80
|
+
* growth → consolidate.applyGrowth(应用不上抛 GROWTH_NOT_APPLIED,留在队列里);held_utterance → discretion.holdDecision;
|
|
81
|
+
* norm_change → discretion.applyNormUpdate;setting_rule_relax → discretion.decideSettingRule(挂起的那一版生效或驳回;
|
|
82
|
+
* 那一版已不在等审核时撤回并抛 REVIEW_WITHDRAWN;discretion 还没有它时批准退回 writeSettingRule(authority='principal'));
|
|
83
|
+
* withdraw → purge。执行完常驻区标脏(挂起的发言除外)。
|
|
84
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
85
|
+
* @param {number} id
|
|
86
|
+
* @param {'approve'|'reject'} decision
|
|
87
|
+
* @param {{by: string}} [ctx]
|
|
88
|
+
* @returns {Promise<void>}
|
|
89
|
+
*/
|
|
90
|
+
export async function decideReview(core, id, decision, ctx) { return review.decideReview(core, id, decision, ctx ?? {}); }
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 过期处理:挂起的发言 14 天没处理 → expired(discretion.holdDecision(token, false));maybe_same **永远不**自动判成 not_same。
|
|
94
|
+
* 顺带撤回已经有答案的审核项(status='withdrawn',不执行决定):权利层已经定了是不是同一个、那一版场合规矩已不在等审核、
|
|
95
|
+
* 要处理的记忆已被彻底删除;再给上次扫到之后才写出来的派生内容补上隐藏(inheritSuppression 的兜底,按 created_at 往后扫,
|
|
96
|
+
* 每张表每次最多 admin.inheritBatch 行)。返回过期的条数。
|
|
97
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
98
|
+
* @param {number} at
|
|
99
|
+
* @returns {number}
|
|
100
|
+
*/
|
|
101
|
+
export function expireReviews(core, at) { return review.expireReviews(core, at); }
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 【补】撤回还在等着的审核项:问题已经由拥有那件事的模块回答了(例:entities 的双向自认把「可能是同一个」定成了同一个,
|
|
105
|
+
* 调 withdrawReviews(core, {kind: 'merge', match: {a, b}}, {reason: 'mutual-claim'}))。不执行任何决定,只标 withdrawn。
|
|
106
|
+
* 在调用方的事务里调用也行。
|
|
107
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
108
|
+
* @param {{kind: 'forget_request'|'owner_op'|'merge'|'growth'|'held_utterance'|'norm_change'|'setting_rule_relax'|'withdraw', match: object}} q
|
|
109
|
+
* match:payload 里要相等的字段(merge 的 a / b 不分先后)
|
|
110
|
+
* @param {{reason?: string, at?: number}} [ctx]
|
|
111
|
+
* @returns {number} 撤回了几条
|
|
112
|
+
*/
|
|
113
|
+
export function withdrawReviews(core, q, ctx) { return review.withdrawReviews(core, q, ctx ?? {}); }
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 待审提示的条数(常驻区,§10.4):审核队列 + 需要你知道 + 最近的成长变化 +(保留期缩短的冷静期倒计时另给)。
|
|
117
|
+
* principalOnly=true 时只算需要主人本人处理的。
|
|
118
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
119
|
+
* @param {boolean} principalOnly
|
|
120
|
+
* @returns {number}
|
|
121
|
+
*/
|
|
122
|
+
export function pendingCount(core, principalOnly) { return review.pendingCount(core, principalOnly); }
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 【补】待审提示的各项(常驻区一行「审核队列有 N 条;需要你知道 N 条;最近有 N 处成长变化」+ 冷静期倒计时)。
|
|
126
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
127
|
+
* @returns {{reviews: number, needToKnow: number, growth: number, retention: {to: string, effectiveAt: number, daysLeft: number}|null}}
|
|
128
|
+
*/
|
|
129
|
+
export function pendingSummary(core) { return review.pendingSummary(core); }
|
|
130
|
+
|
|
131
|
+
// ── 需要你知道(§9.3.7) ──────────────────────────────────────────────────
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 记一条「需要你知道」:T1 想改变她的东西(无权限者的指令或设定、「别告诉你主人」这类请求本身)、T2 伤害 / 违法 / 安全、
|
|
135
|
+
* taint 被污染的、poison 疑似投毒、foreign_write 准备写进规矩 / 意图 / 成长块而来源不是管理者的。各模块发现时调用。
|
|
136
|
+
* 【D118】T1 的那条消息是有管理权限的人自己转给她的(合并转发,转发人有权限)→ 不记,返回 null。
|
|
137
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
138
|
+
* @param {{kind: 'T1'|'T2'|'taint'|'poison'|'foreign_write', ref?: string|null, perceptId?: number|null,
|
|
139
|
+
* sourceEntityId?: number|null, reason: string, severity?: number, at: number}} item
|
|
140
|
+
* @returns {number|null}
|
|
141
|
+
*/
|
|
142
|
+
export function notifyNeedToKnow(core, item) { return review.notifyNeedToKnow(core, item); }
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 「需要你知道」列表(管理页)。【D118】按轮污点只在打开 context.taint.notify 时列;每条带 forwardedBy(转发进来的是谁转的)、
|
|
146
|
+
* growth(原因是 growth-v<N> 时那一版成长块的变化和文字)。
|
|
147
|
+
* @param {import('../core-types.js').CoreContext} core @param {{status?: string}} [q] @returns {object[]}
|
|
148
|
+
*/
|
|
149
|
+
export function listNeedToKnow(core, q) { return review.listNeedToKnow(core, q ?? {}); }
|
|
150
|
+
|
|
151
|
+
/** @param {import('../core-types.js').CoreContext} core @param {number} id */
|
|
152
|
+
export function markNeedToKnowRead(core, id) { review.markNeedToKnowRead(core, id); }
|
|
153
|
+
|
|
154
|
+
// ── 规则 ──────────────────────────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* 加一条规则。do_not_remember 的目标:场合键 / 实体 / 记忆引用;disclosure:{entity | attribute | settingAttrs} + 限制;
|
|
158
|
+
* mode:{settingKey} + 'off'|'live'。只能来自管理页(created_by='owner')或经授权的工具(「别记这里」)。
|
|
159
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
160
|
+
* @param {{kind: 'do_not_remember'|'disclosure'|'mode', target: object, value?: unknown, createdBy: string, note?: string, at: number}} rule
|
|
161
|
+
* @returns {number}
|
|
162
|
+
*/
|
|
163
|
+
export function addRule(core, rule) { return rules.addRule(core, rule); }
|
|
164
|
+
|
|
165
|
+
/** @param {import('../core-types.js').CoreContext} core @param {number} id @param {{at: number}} ctx */
|
|
166
|
+
export function revokeRule(core, id, ctx) { rules.revokeRule(core, id, ctx); }
|
|
167
|
+
|
|
168
|
+
/** @param {import('../core-types.js').CoreContext} core @param {'do_not_remember'|'disclosure'|'mode'} kind @returns {object[]} */
|
|
169
|
+
export function activeRules(core, kind) { return rules.activeRules(core, kind); }
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 这个场合当前的生效档(§10.1、§15.2):min(config.mode.global, 场合覆盖(rules.kind='mode')?? 按场合属性的默认档)。
|
|
173
|
+
* 按属性的默认档:和有管理权限的人一对一 → principalOneOnOne;任何人可见或公开留存 → public;其余 → closed。
|
|
174
|
+
* 每类场合默认都是 live(她在哪儿都记得;说不说由关口①②逐条判断,不按场合一刀切);旧版本的 'shadow' 不再是一档。
|
|
175
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
176
|
+
* @param {string} settingKey
|
|
177
|
+
* @returns {'off'|'live'}
|
|
178
|
+
*/
|
|
179
|
+
export function modeFor(core, settingKey) { return rules.modeFor(core, settingKey); }
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 「别记」规则命中吗(intake 入库前问:场合、说话人)。【补】q.ref:这条被忘掉的记忆(encode 对账时问,别把同样的内容又记回去)。
|
|
183
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
184
|
+
* @param {{settingKey?: string, entityIds?: number[]}} q
|
|
185
|
+
* @returns {boolean}
|
|
186
|
+
*/
|
|
187
|
+
export function doNotRemember(core, q) { return rules.doNotRemember(core, q); }
|
|
188
|
+
|
|
189
|
+
// ── 隐藏、忘掉、彻底删除(§12.7) ───────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 隐藏(对别人隐藏,主人和审核视图仍可见)或忘掉(再加一条指向它的「别记」规则;restore_until = 30 天后)。
|
|
193
|
+
* 派生内容一起对别人隐藏(级联,by='system'、reason='cascade:<ref>';方向从来源到派生,同彻底删除 D55):
|
|
194
|
+
* 一条消息 → 含它的原文窗口、以它为来源的每段叙述(任何一种情节)、只靠它作证据的记忆;一条事实、一段经历、一个原文窗口 →
|
|
195
|
+
* 证据消息汇总出的小结、日记(summary / diary);小结 → 汇总它的日记;日记不带别的。常驻区标脏。
|
|
196
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
197
|
+
* @param {string} ref
|
|
198
|
+
* @param {{state: 'hidden'|'forgotten', by: string, reason?: string, at: number}} s
|
|
199
|
+
*/
|
|
200
|
+
export function suppress(core, ref, s) { sup.suppress(core, ref, s); }
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* 恢复(30 天内)。级联隐藏的派生内容一起恢复,除非还有别的直接隐藏会带给它。常驻区标脏。
|
|
204
|
+
* @param {import('../core-types.js').CoreContext} core @param {string} ref @param {{by: string, at: number}} ctx
|
|
205
|
+
*/
|
|
206
|
+
export function restore(core, ref, ctx) { sup.restore(core, ref, ctx); }
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 【补】解除一条叮嘱(D113,管理页「她在守的叮嘱」):discretion.relaxLimit(规范按主人的权限放宽、第一眼核对过的限制清掉)。
|
|
210
|
+
* 只认 discretion.explicitLimit 认的条目;不是叮嘱的返回 {applied: false}。
|
|
211
|
+
* @param {import('../core-types.js').CoreContext} core @param {string} ref @param {{by: string, at: number}} ctx
|
|
212
|
+
* @returns {{applied: boolean}}
|
|
213
|
+
*/
|
|
214
|
+
export function relaxLimit(core, ref, ctx) {
|
|
215
|
+
const m = parseRef(ref);
|
|
216
|
+
if (!m) throw new ContractError('admin.relaxLimit', [{ path: 'ref', message: '不认识的记忆引用' }]);
|
|
217
|
+
if (!core.mods.discretion.explicitLimit(core, m)) return { applied: false };
|
|
218
|
+
let speaker = null;
|
|
219
|
+
try { speaker = core.mods.entities.principals(core)?.[0] ?? null; } catch { speaker = null; }
|
|
220
|
+
return core.mods.discretion.relaxLimit(core, m, { at: ctx?.at ?? core.now(), speakerEntityId: speaker });
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 【补】新写出来的派生内容(今天到目前为止的小结、日记、原文窗口、提炼出的事实):证据里有被直接隐藏的,就一起对别人隐藏
|
|
225
|
+
* (和 suppress 的级联同一套规则)。写派生内容的模块在写入的事务里调:encode.writeEpisode / writeAssertion / formIntention
|
|
226
|
+
*(有消息作证据的;今天到目前为止的小结也经 writeEpisode 写),consolidate 写日记(证据是小结,不是消息)时再调一次;
|
|
227
|
+
* 原文窗口和没调到的由 tick 的 expireReviews 补上(最多晚一个 tick)。重复调不重复记。返回带来隐藏的那条引用,没有是 null。
|
|
228
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
229
|
+
* @param {string} ref
|
|
230
|
+
* @param {{at?: number}} [ctx]
|
|
231
|
+
* @returns {string|null}
|
|
232
|
+
*/
|
|
233
|
+
export function inheritSuppression(core, ref, ctx) { return sup.inheritSuppression(core, ref, ctx ?? {}); }
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 这条记忆的隐藏状态(retrieve 预过滤、context 渲染前都要查;批量用 suppressedRefs)。
|
|
237
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
238
|
+
* @param {string} ref
|
|
239
|
+
* @returns {'hidden'|'forgotten'|null}
|
|
240
|
+
*/
|
|
241
|
+
export function suppression(core, ref) { return sup.suppression(core, ref); }
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 一批里哪些被隐藏 / 忘掉 / 彻底删除(墓碑)了(召回的预过滤用)。墓碑按时间生效(D42):墓碑之后写进来、碰巧拿到同一个 id 的
|
|
245
|
+
* 新记忆不算。
|
|
246
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
247
|
+
* @param {string[]} refs
|
|
248
|
+
* @returns {Set<string>}
|
|
249
|
+
*/
|
|
250
|
+
export function suppressedRefs(core, refs) { return sup.suppressedRefs(core, refs); }
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 彻底删除(只给主人):按来源链级联(只靠它作证据的派生记忆一起删,有其他证据的重新评估),**先删向量(worker.dropEmbeddings),
|
|
254
|
+
* 再删主库**(记忆本身、FTS 行、规范、谁知道、告诉过谁、痕迹、来源……按 §2.3 的级联表),写墓碑。分批、可中断、可安全重跑。
|
|
255
|
+
* 含被删感知的原文窗口在第一批的事务里交给 intake.dropFromWindows 重新渲染(一条不剩的删掉;删掉的窗口身上挂的行由 admin
|
|
256
|
+
* 清掉并写墓碑)。删完重新定来源被删掉的级联隐藏(重新渲染过的窗口放开;还复述着删掉内容的留着,reason 改成 'purged:<ref>')。
|
|
257
|
+
* 删了人或身份证据时 entities.invalidateNames;常驻区标脏。
|
|
258
|
+
* 删之前先做操作前快照(backup.preOperationSnapshot reason='purge')。alsoBackups=true 时再重写本地的每个包(慢)。
|
|
259
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
260
|
+
* @param {string[]} refs
|
|
261
|
+
* @param {{by: string, alsoBackups?: boolean, at: number, reason?: 'purge'|'withdraw'}} ctx 【补】reason:操作前快照记的原因
|
|
262
|
+
* @returns {Promise<{deleted: Record<string, number>, sessions: string[]}>} sessions:注入过这些记忆的会话(提示「已经注入过的文字收不回」)
|
|
263
|
+
*/
|
|
264
|
+
export async function purge(core, refs, ctx) { return purgeMod.purge(core, refs, ctx); }
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 按墓碑再删一遍(每次启动时、恢复备份后,§12.4.5 第 5 步)。tombstones 不给时用当前库的墓碑表。
|
|
268
|
+
* 先把上次中途停下的彻底删除做完。只删墓碑时间之前就有的行(恢复后新写入的同 id 行是新的记忆)。
|
|
269
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
270
|
+
* @param {{tombstones?: {kind: string, ref: string, at: number}[]}} [opt]
|
|
271
|
+
* @returns {Promise<{deleted: number}>}
|
|
272
|
+
*/
|
|
273
|
+
export async function reapplyTombstones(core, opt) { return purgeMod.reapplyTombstones(core, opt ?? {}); }
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* 【补】在任意一个库(主库或解开的备份副本)上按墓碑删(同步)。backup 重写本地包时用。
|
|
277
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
278
|
+
* @param {import('../store/db.js').Store} db
|
|
279
|
+
* @param {{kind: string, ref: string, at: number}[]} tombstones
|
|
280
|
+
* @returns {{deleted: number}}
|
|
281
|
+
*/
|
|
282
|
+
export function applyTombstonesTo(core, db, tombstones) { return { deleted: purgeMod.applyTombstonesTo(core, db, tombstones).deleted }; }
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 【补】一批 (adapter, ext_key) 里哪些是被彻底删除过的消息:intake 入库前问,恢复后从来源补回、或平台重发同一条消息时不再入库。
|
|
286
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
287
|
+
* @param {string} adapter
|
|
288
|
+
* @param {string[]} extKeys percepts.ext_key(HMAC)
|
|
289
|
+
* @returns {Set<string>}
|
|
290
|
+
*/
|
|
291
|
+
export function tombstonedObs(core, adapter, extKeys) { return purgeMod.tombstonedObs(core, adapter, extKeys); }
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 【补】这条是不是被彻底删除过(kind = owner_kind / 'identity' / 'obs')。记忆单元按时间(D42:同 id 的新行不算)。
|
|
295
|
+
* @param {import('../core-types.js').CoreContext} core @param {string} kind @param {string} ref @returns {boolean}
|
|
296
|
+
*/
|
|
297
|
+
export function isTombstoned(core, kind, ref) { return sup.isTombstoned(core.store, kind, ref); }
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* 按来源撤回(§12.7):某人在某段时间提供的全部内容和由它们派生的记忆 → 先进审核队列,主人批准后 purge。
|
|
301
|
+
* 审核期间先对别人隐藏;驳回就恢复原样。
|
|
302
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
303
|
+
* @param {{entityId: number, from: number, to: number, by: string, at: number}} q
|
|
304
|
+
* @returns {{reviewId: number, refs: number}}
|
|
305
|
+
*/
|
|
306
|
+
export function withdrawBySource(core, q) { return purgeMod.withdrawBySource(core, q); }
|
|
307
|
+
|
|
308
|
+
/** 【补】驳回按来源撤回时恢复审核期间的隐藏(decideReview 调)。 @returns {number} */
|
|
309
|
+
export function undoWithdrawHide(core, reviewId, at) { return purgeMod.undoWithdrawHide(core, reviewId, at); }
|
|
310
|
+
|
|
311
|
+
// ── 原文保留期(§12.6) ───────────────────────────────────────────────────
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* 当前保留期和待生效的改动(冷静期倒计时)。状态存在 meta 'retention.policy' / 'retention.pending'。
|
|
315
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
316
|
+
* @returns {{current: '1y'|'2y'|'5y'|'forever', pending: {to: string, effectiveAt: number, impact: object}|null}}
|
|
317
|
+
*/
|
|
318
|
+
export function retentionState(core) { return /** @type {any} */ (retention.retentionState(core)); }
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 改保留期。延长立即生效;缩短先算影响(会删多少条原文、时间范围、占多少空间),要求 confirmPhrase 和给出的确认语一致,
|
|
322
|
+
* 然后进入 7 天冷静期(期间什么都不删,随时可以取消)。
|
|
323
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
324
|
+
* @param {{to: '1y'|'2y'|'5y'|'forever', confirmPhrase?: string, by: string, at: number}} req
|
|
325
|
+
* @returns {{status: 'applied'|'cooling-off'|'need-confirm', confirmPhrase?: string, impact?: object, effectiveAt?: number}}
|
|
326
|
+
*/
|
|
327
|
+
export function requestRetentionChange(core, req) { return retention.requestRetentionChange(core, req); }
|
|
328
|
+
|
|
329
|
+
/** @param {import('../core-types.js').CoreContext} core @param {{by: string, at: number}} ctx */
|
|
330
|
+
export function cancelRetentionChange(core, ctx) { retention.cancelRetentionChange(core, ctx); }
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* 冷静期满了就生效:先做一次完整备份(backup.createPack kind='pre-op' reason='retention'),再分批调 intake.expireRawText。
|
|
334
|
+
* 平时按当前保留期做例行到期删除(夜间整理第 10 步)。
|
|
335
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
336
|
+
* @param {number} at
|
|
337
|
+
* @returns {Promise<{expired: number, done: boolean}>}
|
|
338
|
+
*/
|
|
339
|
+
export async function applyRetention(core, at) { return retention.applyRetention(core, at); }
|
|
340
|
+
|
|
341
|
+
// ── 「有记忆 / 没记忆」评测(D126) ────────────────────────────────────────────
|
|
342
|
+
|
|
343
|
+
const EVAL_KEY = 'eval.memory.last';
|
|
344
|
+
const evalCase = (c) => ({
|
|
345
|
+
id: String(c?.id ?? '').slice(0, 40), name: String(c?.name ?? '').slice(0, 60),
|
|
346
|
+
// 只看守护的用例:有记忆 / 没记忆两列是 null
|
|
347
|
+
on: c?.on == null ? null : c.on === true, off: c?.off == null ? null : c.off === true, ...(typeof c?.guard === 'boolean' ? { guard: c.guard } : {}),
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* 最近一次「有记忆 / 没记忆」评测的结果(集成层跑完交来的,只有用例名、对错和数字,没有内容)。没跑过返回 null。
|
|
352
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
353
|
+
* @returns {{at: number, version: string, total: number, on: number, off: number, guards: number, guardsHeld: number, ms: number|null,
|
|
354
|
+
* cases: {id: string, name: string, on: boolean|null, off: boolean|null, guard?: boolean}[]}|null}
|
|
355
|
+
*/
|
|
356
|
+
export function evalResult(core) {
|
|
357
|
+
const v = getMetaJson(core.store, EVAL_KEY, null);
|
|
358
|
+
return v && typeof v === 'object' && Array.isArray(v.cases) ? v : null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* 存下一次评测的结果(覆盖上一次)。只留用例名、对错和数字;别的字段丢掉。
|
|
363
|
+
* @param {import('../core-types.js').CoreContext} core
|
|
364
|
+
* @param {{at?: number, version?: string, ms?: number, cases: object[]}} r
|
|
365
|
+
*/
|
|
366
|
+
export function setEvalResult(core, r) {
|
|
367
|
+
if (!r || !Array.isArray(r.cases)) throw new ContractError('evalResult', [{ path: 'cases', message: '应为数组' }]);
|
|
368
|
+
const cases = r.cases.slice(0, 50).map(evalCase).filter((c) => c.id);
|
|
369
|
+
const guards = cases.filter((c) => typeof c.guard === 'boolean');
|
|
370
|
+
const v = {
|
|
371
|
+
at: Number.isFinite(r.at) ? r.at : core.now(), version: String(r.version ?? '').slice(0, 40), ms: Number.isFinite(r.ms) ? Math.round(r.ms) : null,
|
|
372
|
+
total: cases.filter((c) => c.on !== null).length, on: cases.filter((c) => c.on === true).length, off: cases.filter((c) => c.off === true).length,
|
|
373
|
+
guards: guards.length, guardsHeld: guards.filter((c) => c.guard).length, cases,
|
|
374
|
+
};
|
|
375
|
+
setMetaJson(core.store, EVAL_KEY, v);
|
|
376
|
+
return v;
|
|
377
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// 主人改了会影响她上下文的东西之后,通知别的模块(只标脏、只让缓存失效,不抛错,提交之后才做)。
|
|
2
|
+
// - 常驻区(context.markResidentDirty):只标脏,等下一条真实消息或压缩结束才换文本(§10.4 的冻结规则不变);
|
|
3
|
+
// context 不在时,常驻区过了 context.residentStaleMin 也会当脏处理。
|
|
4
|
+
// - 名字表(entities.invalidateNames):彻底删除的级联删了档案、名字或身份证据之后,名字匹配器要重建。
|
|
5
|
+
|
|
6
|
+
function afterCommit(core, fn) {
|
|
7
|
+
const run = () => { try { fn(); } catch { /* 通知失败不影响主人的操作 */ } };
|
|
8
|
+
try { core.store.afterCommit(run); } catch { run(); }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 常驻区标脏。settingKey 不给 = 全部场合。
|
|
13
|
+
* @param {object} core
|
|
14
|
+
* @param {string|null} [settingKey]
|
|
15
|
+
*/
|
|
16
|
+
export function residentDirty(core, settingKey = null) {
|
|
17
|
+
afterCommit(core, () => {
|
|
18
|
+
if (typeof settingKey === 'string' && settingKey) core.mods.context.markResidentDirty(core, settingKey);
|
|
19
|
+
else core.mods.context.markResidentDirty(core);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 名字表被 admin 改过(彻底删除、墓碑重删)。 */
|
|
24
|
+
export function namesChanged(core) {
|
|
25
|
+
afterCommit(core, () => core.mods.entities.invalidateNames(core));
|
|
26
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// admin 读参数:每次读当前快照(热加载);数值只在 config/defaults.js(D25),这里不写第二份。
|
|
2
|
+
import { defaultAt } from '../config/defaults.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 参数快照里的一项('retention.coolingOffDays')。快照由 mergeConfig 合出来,键是全的;只有测试里手搭的 core 缺键时退回 DEFAULTS。
|
|
6
|
+
* @param {object} core
|
|
7
|
+
* @param {string} path
|
|
8
|
+
* @returns {any}
|
|
9
|
+
*/
|
|
10
|
+
export function param(core, path) {
|
|
11
|
+
let v = core.config.get();
|
|
12
|
+
for (const k of path.split('.')) {
|
|
13
|
+
if (v == null || typeof v !== 'object') { v = undefined; break; }
|
|
14
|
+
v = v[k];
|
|
15
|
+
}
|
|
16
|
+
return v ?? defaultAt(path);
|
|
17
|
+
}
|