@sparkelf/dsh-knowledge-base 0.1.36 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +53 -0
  2. package/lib/client.js +2463 -1385
  3. package/lib/client.js.map +1 -1
  4. package/lib/index.js +18210 -7883
  5. package/lib/types/client/KnowledgeMemory.d.ts +127 -0
  6. package/lib/types/client/graph/OriginBadges.d.ts +55 -0
  7. package/lib/types/client/locales.d.ts +64 -0
  8. package/lib/types/ftsIndex.d.ts +32 -0
  9. package/lib/types/graphCanvas.d.ts +1 -1
  10. package/lib/types/graphJsonProjection.d.ts +71 -0
  11. package/lib/types/graphMigration.d.ts +118 -0
  12. package/lib/types/graphModel.d.ts +194 -0
  13. package/lib/types/graphPolicy.d.ts +68 -0
  14. package/lib/types/graphTools.d.ts +30 -5
  15. package/lib/types/graphTypes.d.ts +24 -0
  16. package/lib/types/guardianSession.d.ts +135 -0
  17. package/lib/types/index.d.ts +135 -15
  18. package/lib/types/knowledgeTools.d.ts +50 -2
  19. package/lib/types/memoryBindings.d.ts +119 -0
  20. package/lib/types/memoryContext.d.ts +83 -0
  21. package/lib/types/memoryGraphBootstrap.d.ts +62 -0
  22. package/lib/types/memoryGraphWriter.d.ts +101 -0
  23. package/lib/types/memoryGuardian.d.ts +123 -0
  24. package/lib/types/memoryGuardianTool.d.ts +56 -0
  25. package/lib/types/memoryPolicy.d.ts +106 -0
  26. package/lib/types/memoryRecordTool.d.ts +39 -0
  27. package/lib/types/memoryRoutes.d.ts +85 -0
  28. package/lib/types/memorySettings.d.ts +40 -0
  29. package/lib/types/memorySkillPromotion.d.ts +62 -0
  30. package/lib/types/memoryStore.d.ts +297 -0
  31. package/lib/types/memoryTools.d.ts +71 -0
  32. package/lib/types/memoryTriggers.d.ts +65 -0
  33. package/lib/types/memoryTypes.d.ts +241 -0
  34. package/lib/types/summaryIndex.d.ts +112 -0
  35. package/lib/types/summaryWriteback.d.ts +87 -0
  36. package/lib/types/views.d.ts +25 -0
  37. package/package.json +1 -1
  38. package/skills/knowledge-graph/SKILL.md +37 -8
  39. package/skills/memory-extract/SKILL.md +43 -0
  40. package/skills/memory-guardian/SKILL.md +97 -0
  41. package/skills/memory-sop-evaluate/SKILL.md +43 -0
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Incremental change detection and summary refresh for the knowledge tree.
3
+ *
4
+ * The design fixes one rule that keeps this module small: the AI never decides
5
+ * what an edit affects. A changed document makes the document and its fixed
6
+ * parent chain dirty; only dirty documents are re-summarised, and directory
7
+ * summaries are folded from the documents summarised in that pass, in stable
8
+ * path order.
9
+ *
10
+ * ```text
11
+ * Git diff / mtime+hash
12
+ * → ChangeSnapshot
13
+ * → diffSnapshots
14
+ * → ChangeSet (changed / added / removed / dirtyDirectories)
15
+ * → refreshDirtySummaries (only dirty documents reach summarize())
16
+ * ```
17
+ *
18
+ * The module owns no storage. `summarize` and `writeIndex` are injected, so the
19
+ * existing `kb_docs.summary` path (or an INDEX.md writer) remains the one owner
20
+ * of persistence. A removed document is dropped by calling `writeIndex(path,
21
+ * '')`; an empty summary is the "delete this entry" signal.
22
+ *
23
+ * Scope: the snapshot tracks TEXT DOCUMENTS (`.md`, `.markdown`, `.txt`,
24
+ * `.text`). Binary attachments are deliberately outside the summary index; the
25
+ * directory and file names the design reserves for graph/media state
26
+ * (`.graphs`, `.kg`, `.archive`, `_trash`, any other dot directory) are skipped
27
+ * recursively.
28
+ *
29
+ * @module @sparkelf/dsh-knowledge-base/summary-index
30
+ */
31
+ /** One file's content identity and modification instant. */
32
+ export interface ChangeSnapshot {
33
+ files: Record<string, {
34
+ mtimeMs: number;
35
+ sha256: string;
36
+ }>;
37
+ }
38
+ /** The difference between two snapshots, plus the directory chain to refresh. */
39
+ export interface ChangeSet {
40
+ changed: string[];
41
+ added: string[];
42
+ removed: string[];
43
+ dirtyDirectories: string[];
44
+ }
45
+ /**
46
+ * Whether a path is a document the summary index tracks.
47
+ *
48
+ * Exported so the wiring layer can apply the same definition when it maps paths
49
+ * to `kb_docs` rows instead of guessing from a MIME type.
50
+ */
51
+ export declare function isSummaryDocumentPath(relPath: string): boolean;
52
+ /**
53
+ * Hash every text document under `root` and record its modification instant.
54
+ *
55
+ * Output keys are sorted, so two scans of the same tree produce equal snapshots
56
+ * and callers can compare them without a second ordering pass. A missing root
57
+ * yields an empty snapshot: the plugin may load before a workspace provisions
58
+ * its tree. Symbolic links are not followed, which keeps a link cycle from
59
+ * turning a scan into an infinite walk.
60
+ */
61
+ export declare function scanKnowledgeTree(root: string): Promise<ChangeSnapshot>;
62
+ /**
63
+ * Compare two snapshots.
64
+ *
65
+ * Content identity is the sha256, not the mtime: touching a file without
66
+ * changing a byte is not a change and must not spend a summarisation call. The
67
+ * mtime exists so a scanner can skip re-hashing an untouched file; it is kept in
68
+ * the snapshot for that optimisation and for display.
69
+ *
70
+ * `dirtyDirectories` is the deterministic union of the fixed parent chain of
71
+ * every changed, added and removed document, sorted.
72
+ */
73
+ export declare function diffSnapshots(previous: ChangeSnapshot, next: ChangeSnapshot): ChangeSet;
74
+ /**
75
+ * The fixed ancestor chain of a document, root-first.
76
+ *
77
+ * `a/b/c.md` answers `['a', 'a/b']`; a document at the root answers `[]`. The
78
+ * chain never includes the document itself and never depends on what currently
79
+ * exists on disk, which is what makes "which directories are dirty" a pure
80
+ * function of the changed path.
81
+ *
82
+ * Invalid input (absolute path, `..`, NUL) answers `[]` instead of throwing:
83
+ * there is no ancestor chain to refresh outside the root.
84
+ */
85
+ export declare function parentChain(relPath: string): string[];
86
+ /**
87
+ * Re-summarise the changed/added documents, drop removed ones and rebuild the
88
+ * dirty directory aggregates.
89
+ *
90
+ * Only `changeSet.changed` and `changeSet.added` documents reach `summarize()`,
91
+ * each exactly once, in sorted path order. Removed documents are not
92
+ * summarised; they are dropped by calling `writeIndex(path, '')`.
93
+ *
94
+ * Directory aggregates are folded from the summaries produced in THIS pass,
95
+ * using the deterministic child order (`- <relPath>: <summary>`). That is the
96
+ * only summary source the injected API exposes; a directory with no freshly
97
+ * summarised child is not rewritten, so a deletion alone cannot blank an
98
+ * aggregate that still describes unchanged siblings. A caller that stores full
99
+ * child summaries should merge them at the sink.
100
+ *
101
+ * @returns `updated` - document paths summarised and written, sorted.
102
+ * `directories` - directory paths whose aggregate was written, sorted.
103
+ */
104
+ export declare function refreshDirtySummaries(input: {
105
+ root: string;
106
+ changeSet: ChangeSet;
107
+ summarize: (relPath: string, source: string) => Promise<string>;
108
+ writeIndex: (relPath: string, summary: string) => Promise<void>;
109
+ }): Promise<{
110
+ updated: string[];
111
+ directories: string[];
112
+ }>;
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Persist incremental summary refreshes into the existing knowledge index.
3
+ *
4
+ * `summaryIndex.ts` decides WHICH documents changed. This module is the write
5
+ * half: it reads the changed/added documents, rebuilds a deterministic extractive
6
+ * summary from the already-indexed chunks, updates the existing `kb_docs` row
7
+ * (`summary`, `summary_kind='extractive'`, `summary_version`), and refreshes the
8
+ * generated `index/md/<docId>.INDEX.md` when that file already exists. Removed
9
+ * documents have their summary cleared. The scan snapshot is written last and
10
+ * atomically, so a failed run is simply retried from the previous baseline.
11
+ *
12
+ * ## Why the index must be current
13
+ *
14
+ * The summary is built from the chunks SQLite already holds, with the exact
15
+ * `chunkText` + `buildDocumentOutline` functions the indexer uses, so the text
16
+ * matches what `kb_search` can cite. Before using a document this module verifies
17
+ * that the stored chunks are byte-identical to a fresh chunking of the source. A
18
+ * changed document whose row/chunks are stale (or missing entirely) is reported as
19
+ * `pending` and left OUT of the snapshot, so the next run retries it after
20
+ * `syncIndex` catches up. Nothing is guessed from a stale index.
21
+ *
22
+ * The expected call order in the plugin is:
23
+ *
24
+ * ```ts
25
+ * await syncIndex({ actualRoot, indexPath })
26
+ * await applySummaryWriteback({ root: actualRoot, indexPath })
27
+ * ```
28
+ *
29
+ * ## Boundaries
30
+ *
31
+ * - It does not create the SQLite schema. `kb_docs`/`kb_chunks` and `kb_index_meta`
32
+ * are owned by `ftsIndex.ts`; a missing `kb_docs` table is a wiring error.
33
+ * - It writes only the three summary columns. Section rows, TOC and the summary
34
+ * FTS table stay owned by `ftsIndex`/`kb_outline_save`, so a model-authored
35
+ * outline is never half-replaced by an extractive one.
36
+ * - An existing `summary_kind='llm'` summary is preserved, matching the indexer's
37
+ * own rule; extractive output never downgrades model-authored text.
38
+ *
39
+ * @module @sparkelf/dsh-knowledge-base/summary-writeback
40
+ */
41
+ /** Inputs for one incremental summary write-back pass. */
42
+ export interface SummaryWritebackOptions {
43
+ /** Authoritative knowledge-tree root; the same `actualRoot` given to `syncIndex`. */
44
+ root: string;
45
+ /** SQLite index file created/owned by `ftsIndex` (`kb.sqlite`). */
46
+ indexPath: string;
47
+ /** Where the scan baseline lives; defaults to `<index dir>/summary-snapshot.json`. */
48
+ snapshotPath?: string;
49
+ /**
50
+ * Optional replacement summarizer for the changed/added documents. When omitted,
51
+ * the deterministic extractive summary from the indexed chunks is used. The
52
+ * source text is the current file content, so a caller may plug in a model here.
53
+ */
54
+ summarize?: (relPath: string, source: string) => Promise<string>;
55
+ }
56
+ /** What one write-back pass did; all path arrays are sorted. */
57
+ export interface SummaryWritebackResult {
58
+ /** Documents whose `kb_docs.summary` was rewritten. */
59
+ updated: string[];
60
+ /** Documents that disappeared from the tree; their summary was cleared. */
61
+ removed: string[];
62
+ /** Changed/added documents the index has not caught up with; retried next run. */
63
+ pending: string[];
64
+ /** Documents left untouched because a model-authored (`llm`) summary owns them. */
65
+ preserved: string[];
66
+ /** Absolute `INDEX.md` paths regenerated (only files that already existed). */
67
+ indexFiles: string[];
68
+ /** Absolute snapshot path used for this pass. */
69
+ snapshotPath: string;
70
+ /** False when the baseline was already current and nothing was written. */
71
+ snapshotWritten: boolean;
72
+ }
73
+ /** The default snapshot path for one index; exported for callers that inspect it. */
74
+ export declare function summarySnapshotPathFor(indexPath: string): string;
75
+ /**
76
+ * Run one incremental summary write-back pass.
77
+ *
78
+ * Only changed/added documents are read; only removed documents are cleared.
79
+ * Unchanged documents cost one hash in `scanKnowledgeTree` and nothing else. A
80
+ * second pass with an unchanged tree performs no database writes and no summary
81
+ * generation, and leaves the snapshot alone.
82
+ *
83
+ * @throws when the index file/table is missing, the source cannot be read, or a
84
+ * database/INDEX write fails. In every failure case the snapshot is NOT
85
+ * advanced, so the same changes are retried on the next pass.
86
+ */
87
+ export declare function applySummaryWriteback(options: SummaryWritebackOptions): Promise<SummaryWritebackResult>;
@@ -33,6 +33,17 @@ export interface ViewDelta {
33
33
  unlinked: string[];
34
34
  /** Paths left alone because the view already matched. */
35
35
  unchanged: number;
36
+ /**
37
+ * View paths that existed only in the view and were imported into the
38
+ * authoritative tree instead of being deleted.
39
+ *
40
+ * A file can land in a view without going through this plugin (a shell `cp`, an
41
+ * editor's save-as). The view is the reader's only writable surface, so treating
42
+ * such a file as an orphan would silently destroy the user's document on the next
43
+ * sync. `adoptOrphan` decides, per path, whether it is imported; these are the
44
+ * paths where that succeeded.
45
+ */
46
+ adopted?: string[];
36
47
  /**
37
48
  * Directories removed because a revoke emptied them.
38
49
  *
@@ -107,6 +118,20 @@ export interface ViewSyncOptions extends ViewOptions {
107
118
  * a permission decision, and must not delete the reader's links.
108
119
  */
109
120
  trustResolvedSet?: boolean;
121
+ /**
122
+ * Import one view-only file into the authoritative tree before it is unlinked.
123
+ *
124
+ * Called only for a regular file the resolved set does not name, and only when the
125
+ * prune pass is otherwise about to delete it. Returning true means the file now
126
+ * lives at the authoritative path (and, ideally, that the view entry was relinked
127
+ * to it), so the view keeps it. Returning false falls through to the ordinary
128
+ * unlink, which stays the answer for a file the caller may not write or may not
129
+ * import.
130
+ *
131
+ * The callback owns permission and destination decisions: `syncView` only knows
132
+ * about files and links, not about DataOps grants or bucket layouts.
133
+ */
134
+ adoptOrphan?: (relPath: string, fullPath: string) => Promise<boolean>;
110
135
  }
111
136
  /**
112
137
  * The three top-level buckets every view exposes, in display order.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sparkelf/dsh-knowledge-base",
3
- "version": "0.1.36",
3
+ "version": "0.4.3",
4
4
  "description": "DataOps-backed knowledge base for DeepSeek Harness: a left-sidebar destination, Settings configuration, and index tooling over a mounted NAS view.",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -22,6 +22,18 @@ description: 读写团队知识库里的知识图谱:主题卡、关系文档
22
22
  - 主题文件夹名可读、可改;关系引用 `index.md` 里的 `id`,不引用文件夹名,所以改名不断边。
23
23
  - 一个图谱一条 grant,覆盖主题文档与 `.kg`;写操作走 `kg_add` / `kg_set` / `kg_del`,不要用 shell 直接写文件。
24
24
 
25
+ 当前图谱模型同时支持 graph-json(新建图谱默认用它):
26
+
27
+ ```text
28
+ <scope>/.graphs/<graph-id>/graph.json # 拓扑与元数据:nodes/edges + name/scope/visibility
29
+ <scope>/.graphs/<graph-id>/links/<edge-id>.md # 可选的关系正文
30
+ <scope>/<分类>/<文档>.md # 节点正文,仍是普通知识库文档
31
+ ```
32
+
33
+ - 节点属性(type/authority/origin/reviewStatus/tags)写在节点文档的 front matter,`graph.json` 只存 `{id,path}` 与边。
34
+ - 图谱页把每个节点投影成一张卡片;空图谱也会出现在图谱树里,加上第一个节点后画布就有内容。
35
+ - `.graphs/` 是隐藏目录,不要用文件树或 shell 直接编辑;新建用 `kg_add kind=graph`,改拓扑用 `kg_add` / `kg_set` / `kg_del`。
36
+
25
37
  ## 2. 主题卡与关系文档
26
38
 
27
39
  主题卡好例子:
@@ -70,18 +82,35 @@ updated: 2026-10-31
70
82
 
71
83
  | 动作 | 做什么 |
72
84
  |---|---|
73
- | `kg_add` | `kind: topic` 新增一条线(文件夹 + 卡);`kind: link` 新增一条关系 |
85
+ | `kg_add` | `kind: graph` 新建 graph-json 图谱;`kind: topic` 新增一条线(文件夹 + 卡);`kind: link` 新增一条关系;`kind: node` / `kind: edge` 给 graph-json 图谱加节点/关系 |
74
86
  | `kg_get` | 不带 `graph`/`id` 返回全部图谱总览;带 `graph` 返回该图谱;带 `graph`+`id` 返回卡、文档、关系 |
75
- | `kg_set` | 改一个主题的 `status` / `depth` / `title` / `domain` / 摘要 / 正文 |
87
+ | `kg_set` | 改一个主题的 `status` / `depth` / `title` / `domain` / 摘要 / 正文;graph-json 图谱改节点/关系字段 |
76
88
  | `kg_del` | 把主题移到 `<graph>/_trash/`;有关联关系时需 `force: true` 一并移走,**不真删** |
77
89
 
78
- ### 五个剧本
90
+ ### 新建图谱(graph-json)
91
+
92
+ `.graphs/<id>/graph.json` 是当前图谱模型。新建图谱**不需要先有可见文件夹**,也不要手工拼路径或写 shell:
93
+
94
+ 1. `kg_add {"kind":"graph","scope":"personal","id":"nenghui-governance","name":"能汇制度图谱"}`
95
+ 返回完整 manifest 草稿(`schemaVersion/id/name/scope/visibility/nodes/edges`),此时不写盘。
96
+ 2. 把草稿给用户看;确认后同参数再加 `"confirm":true`,写入 `<scope>/.graphs/<id>/graph.json`。
97
+ 写成功后图谱页/`kg_get` 概览立刻能看到它(`format: "graph-json"`)。
98
+ 3. 继续加内容:`kg_add {"graph":"personal/.graphs/nenghui-governance","kind":"node","id":"policy-1","path":"制度/采购制度.md","title":"采购制度","type":"policy"}`,
99
+ 同样是先草稿、再 `confirm:true`。节点正文是普通 `.md` 文档,图谱只记录 `{id,path}` 拓扑。
100
+
101
+ - `scope`:`personal`(默认,写调用者自己的个人范围)、`department`(复用已有部门图谱的部门路径;首次可显式传 `department/<部门链>`)、`global`(通常需要管理员写权限)。权限由 Host 的 `canWrite` 判定,越权返回 `forbidden`。
102
+ - `id` 只能是小写字母、数字、连字符;同一范围内重复 id **或重名**都会返回 `exists`。
103
+ - `kg_add` / `kg_get` 报 `no readable graph matches ...` 时,按错误里的提示先用 `kind:"graph"` 建图;不要用 `find`/`map` 或 shell 绕开工具。
104
+ - 新建图谱的 `status` 是 `draft`;发布/审核是后续动作,不要为了“让它出现”改 manifest。
105
+
106
+ ### 六个剧本
79
107
 
80
- 1. **新建一条线**:`kg_get` 看当前图谱 → `kg_add {kind:"topic"}` 草稿 → 用户确认 → `confirm:true`。
81
- 2. **接续**:`kg_get {graph,id}` 读卡与关系 → 更新正文/摘要 → `kg_set` 草稿 → 确认。
82
- 3. **寻址**:`kg_get` 不带 id 看总览 → 按名称找 graph → `kg_get {graph,id}` 读一条线;不要用 `find`/`map` 另造工具。
83
- 4. **沉淀**:把一次会话的结论写成文档(`kb_file_write` 到主题文件夹),再用 `kg_set` 更新卡摘要;图谱不复制文档内容。
84
- 5. **复盘**:`kg_get` 总览 → 对每条线读 `kg_get {graph,id}` → 把 `status`/`depth` 调整回真实状态;`kg_del` 只移 `_trash`,不删。
108
+ 1. **新建图谱**:`kg_get` 总览里没有目标图谱(或 `kg_add` 报 `no readable graph matches`)→ `kg_add {kind:"graph"}` 草稿 → 用户确认 → `confirm:true` 建出 `.graphs/<id>/graph.json`。
109
+ 2. **新建一条线**:`kg_get` 看当前图谱 → `kg_add {kind:"topic"}` 草稿 → 用户确认 → `confirm:true`。
110
+ 3. **接续**:`kg_get {graph,id}` 读卡与关系 → 更新正文/摘要 → `kg_set` 草稿 → 确认。
111
+ 4. **寻址**:`kg_get` 不带 id 看总览 → 按名称找 graph → `kg_get {graph,id}` 读一条线;不要用 `find`/`map` 另造工具。
112
+ 5. **沉淀**:把一次会话的结论写成文档(`kb_file_write` 到主题文件夹),再用 `kg_set` 更新卡摘要;图谱不复制文档内容。
113
+ 6. **复盘**:`kg_get` 总览 → 对每条线读 `kg_get {graph,id}` → 把 `status`/`depth` 调整回真实状态;`kg_del` 只移 `_trash`,不删。
85
114
 
86
115
  ## 4. 扫历史会话(shell,不配工具)
87
116
 
@@ -0,0 +1,43 @@
1
+ ---
2
+ name: memory-extract
3
+ description: 记忆抽取子代理的工作准则:读一次会话的新增消息和工具轨迹,只提取可长期复用的偏好、踩坑、情景、事实与 SOP 候选,给出证据并避开秘密与闲聊。
4
+ ---
5
+
6
+ # 记忆抽取子代理
7
+
8
+ 你负责从一段真实的会话轨迹里挑出值得长期记住的内容。你只提候选,不最终写记忆;合并、纠偏和遗忘由守护会话决定。
9
+
10
+ ## 读什么
11
+
12
+ 读分配给你的会话在本次范围内的新增消息和工具轨迹。工具轨迹包括调用意图、参数、返回结果和失败重试,不要只看最后的成功消息。压缩摘要可以作为索引,但证据要回到原文。
13
+
14
+ ## 只提五类
15
+
16
+ - **偏好**:用户的软习惯、输出风格、默认值、常用做法。是“倾向”,不是硬约束。
17
+ - **踩坑**:实际执行中遇到的错误或异常,连同上下文、解决办法和适用条件。以后遇到同样错误能直接省时间。
18
+ - **情景**:什么时间、什么场景、用户要求做什么、结果如何。任务过程和决策时刻值得留,但不要把整段过程复述一遍。
19
+ - **事实**:用户明确要求、明确拍板或明确澄清的约束。必须是用户说清楚的,不要把你自己的推断写成事实。
20
+ - **SOP**:可以重复使用的做法,包含触发条件、步骤、工具选择和成功标准。做法只在这一次偶然奏效、或依赖特殊环境的,不要提成 SOP。
21
+
22
+ 判断标准只有一个:**它会不会改变下一次的行为**。闲聊、寒暄、一次性的查询结果、已经写在部门制度里的公共知识,都不算。
23
+
24
+ ## 每条候选要带什么
25
+
26
+ 用一小段话说清:它是什么、为什么值得留、在哪个消息或工具结果里能看到证据、适用于什么场景。证据要能定位到原文,不要写“根据对话”。没有证据的候选不要提。
27
+
28
+ 同一个主题合并成一条,不要按消息逐条罗列。发现它和已有记忆可能重复时,指出来,让守护会话去重。
29
+
30
+ ## 不做什么
31
+
32
+ - 不保存密钥、Token、密码或凭据值。如果遇到,只记“哪份凭据、什么用途、用于哪个系统”,实际值归凭据管理器。
33
+ - 不把联系方式、身份、账户细节等敏感内容当普通记忆;拿不准就标成 private 或 sensitive,让守护会话确认。
34
+ - 不把用户的玩笑、假设、临时演练当成事实。
35
+ - 不替用户做决定,也不把“AI 建议”写成用户的明确要求。
36
+
37
+ ## 怎么交付
38
+
39
+ 可以写一份临时详细报告,但回给守护会话的只要一小段结论:提了哪几类、每类最关键的几条、哪些拿不准。守护会话读完后会删除临时报告,所以不要把唯一证据只留在报告里而不写进结论。
40
+
41
+ 宁少勿滥。一条真正会改变下次行为的记忆,比十条看起来很完整的复述更有价值。
42
+
43
+ 每个候选带一个不超过 20 字的短标题,只写结论,不写工具名、路径、会话 id 或长解释;详细证据放在正文。
@@ -0,0 +1,97 @@
1
+ ---
2
+ name: memory-guardian
3
+ description: 记忆守护会话的唯一写入者准则:串行处理空闲、压缩与 dream 消息,派抽取和 SOP 评估子代理,维护日文档与偏好、踩坑、事实、SOP,评估并晋升 SOP,处理隐私与 dream Git 恢复点。
4
+ ---
5
+
6
+ # 记忆守护会话
7
+
8
+ 你是这个用户唯一的记忆写入者。普通会话只留下消息和工具轨迹;记忆的抽取、合并、纠偏、遗忘和晋升都由你完成。不要把写记忆的工作退回给普通会话。
9
+
10
+ ## 收到什么
11
+
12
+ - **空闲**:某个会话静默了一段时间,可能积累了值得记住的内容。
13
+ - **压缩**:某个会话开始压缩。原始消息仍然存在,压缩摘要会标出被遮蔽的范围;判断依据以原始轨迹为准。
14
+ - **dream**:每日一次的整理与评估。每天确认这个每日任务只存在一份,按标题去重。
15
+
16
+ 三类消息按到达顺序串行处理。上一轮没有收尾时,不要开始下一轮;宁可晚一点整理,也不要把同一天的记忆写乱。
17
+
18
+ ## 一轮怎么处理
19
+
20
+ ### 1. 先确定范围
21
+
22
+ 枚举今天真正活跃的会话:上次 dream 之后有新增消息或工具事件的会话。读它们的新增消息和工具轨迹,不要只读压缩摘要,也不要凭标题猜内容。
23
+
24
+ ### 2. 派两个子代理
25
+
26
+ - **抽取子代理**:按五类记忆整理候选。派它时让它按「记忆抽取」技能工作。
27
+ - **SOP 评估子代理**:判断现有 SOP 今天是否被使用、效果如何、该怎么改。派它时让它按「SOP 评估」技能工作。
28
+
29
+ 两个子代理可以并行。它们可以把详细分析写进临时报告,但只回你一句结论。你读完报告、完成合并和决策后,立刻删除临时文件。临时报告可能含敏感内容,只留在本机、仅 owner 可见,不导出、不进版本库。
30
+
31
+ ### 3. 合并五类记忆
32
+
33
+ - **偏好**:用户的软习惯、风格和默认值。新偏好覆盖旧偏好时,留下替代关系。
34
+ - **踩坑**:实际遇到的错误、上下文、解决办法和适用条件。按错误特征去重合并。
35
+ - **情景**:什么时间、什么场景、用户要求做什么、结果如何。可以从中提炼偏好、事实或 SOP。
36
+ - **事实**:用户明确要求、明确拍板或明确澄清的约束。高置信,但部门制度与红线优先。
37
+ - **SOP**:做某件事的可复用做法,独立生命周期。
38
+
39
+ 只留下会改变下次行为的内容。闲聊、一次性操作、没有证据的猜测不进入记忆。部门 canonical 和红线永远压过个人记忆;冲突时标记冲突,不要悄悄覆盖。
40
+
41
+ ### 4. 去重、纠偏、遗忘
42
+
43
+ 同一个偏好只留当前有效版本;同一个坑按错误特征合并;用户纠正过的内容以新说法为准,并记录被替代的旧说法。用户要求删除、偏好已过期、长期低价值的记忆,要真的遗忘,并写清原因。删除不是隐藏。
44
+
45
+ ### 5. 写法
46
+
47
+ 每条记忆都要有一个**短标题**和一个正文:
48
+
49
+ - 标题不超过 20 字,只写结论,例如「结论先行」「文件路径写错」;
50
+ - 标题里不写工具名、路径、字段名、会话 id、SOP 这类内部词;
51
+ - 详细说明、证据、来源放到正文/详情里,标题不承担解释;
52
+ - 来源只写用户能懂的场景名,如「驾驶舱下钻」;不写 session id。
53
+
54
+ ### 6. 更新记忆
55
+
56
+ - **日文档**:当天唯一的新增写入目标,按时间追加原始抽取和审计信息,记录每条记忆的来源会话、证据、被替代或被晋升的去向。
57
+ - **偏好、踩坑、事实**:由 dream 维护成当前有效快照,不是抽取时直接改写;同一时刻只保留一处当前版本。
58
+ - **SOP**:一个 SOP 一份草稿,保留触发条件、步骤、工具选择和成功标准。每次新情景、新踩坑或用户纠正,都可以修订草稿。
59
+
60
+ ### 7. 评估并晋升 SOP
61
+
62
+ 评估靠读轨迹和原文证据,靠你对“这套做法是否真的解决问题”的判断,不用使用次数、成功次数或任何机械计数器。
63
+
64
+ - 有效且可复用:沉淀为技能,写清来源 SOP 和晋升时间;技能写成功后,再删除 SOP。
65
+ - 证据不足:保留在 testing,下次继续观察。
66
+ - 新证据推翻:修订步骤或触发条件。
67
+ - 已过时:drop,并保留原因。
68
+ - 拿不准:保留。晋升没有硬阈值,但“今天没被用到”不等于失败。
69
+
70
+ 同名技能已存在时更新它,不要新建第二份。
71
+
72
+ ### 8. 审计与 Git 恢复点
73
+
74
+ 每一轮都写审计:抽取了哪些会话、合并了什么、纠正了什么、遗忘了什么、晋升或删除了哪个 SOP、证据是什么。
75
+
76
+ dream 用 Git 做恢复点,没有 snapshot 工具,Git 就是恢复点,只操作个人记忆 scope 目录:
77
+
78
+ 1. dream 开始前,在个人 scope 仓库里提交当前状态,并记下这个 commit:
79
+ `git -C <个人记忆 scope 目录> add -A`
80
+ `git -C <个人记忆 scope 目录> commit -m "dream: pre <timestamp>"`
81
+ 2. 本轮 dream 失败,回到开始前:`git -C <个人记忆 scope 目录> reset --hard <pre-commit>`。
82
+ 3. dream 完成并写完全部记忆后,再提交一次:`git -C <个人记忆 scope 目录> commit -m "dream: post <timestamp>"`。
83
+
84
+ 只提交个人 scope。个人守护会话绝不提交部门或全局仓库;那些仓库不属于你的恢复点。日常的单次写入不需要各自提交。
85
+
86
+ ## 隐私规则
87
+
88
+ - **normal**:普通个人记忆,可以进入个人图谱;可内化时才提炼进部门资产。
89
+ - **private**:仅 owner。不进部门提炼、不进共享摘要、不导出。
90
+ - **sensitive**:联系方式、身份、账户细节等。仅 owner,日志和遥测里要脱敏,禁止导出或共享。
91
+ - **secret**:密钥、Token、密码。记忆里只写“哪份凭据、什么用途、用于哪个系统”,绝不保存实际值;值由凭据管理器保管。
92
+
93
+ 拿不准时按 private 处理。抽取子代理可以提出隐私判断,但最终由你确认。不要把 private 或 sensitive 内容写进日文档的共享可见部分,也不要让它们进入跨用户检索。
94
+
95
+ ## 边界
96
+
97
+ 你是标准会话,不是新的 agent 类型。你的约束来自这份技能和审计记录:不建队列、不建调度器、不靠机械计数,也不把记忆分散到多个守护会话。一个用户只有一个守护会话;状态丢失时按标题找回,找不到再创建。
@@ -0,0 +1,43 @@
1
+ ---
2
+ name: memory-sop-evaluate
3
+ description: SOP 评估子代理的工作准则:对照活跃 SOP 与当天会话轨迹,判断是否被使用、是否有效、证据是什么,给出修订、晋升、保留或淘汰建议。
4
+ ---
5
+
6
+ # SOP 评估子代理
7
+
8
+ 你负责回答一个问题:**这些 SOP 今天到底有没有解决问题,下一步该拿它怎么办。** 你不写记忆,也不做晋升动作;你只给守护会话一份有证据的判断。
9
+
10
+ ## 输入
11
+
12
+ - 当前活跃的 SOP 草稿:每份的触发条件、步骤、工具选择和成功标准。
13
+ - 当天相关会话的新增消息和工具轨迹。压缩摘要只能当索引,判断要回到原文。
14
+
15
+ ## 对每份 SOP 回答四件事
16
+
17
+ 1. **有没有被用到**:今天的任务里是否出现了符合触发条件的场景?是明确照做了,还是只沾了一点边?没有被用到也要说清楚,但不要因此判定它失败。
18
+ 2. **有没有效果**:按 SOP 做完之后,任务是否顺利推进?中途出现了哪些偏差、返工、额外解释或失败重试?用户是否纠正过步骤?
19
+ 3. **证据在哪**:引用具体的消息或工具结果,说明哪一步有效、哪一步卡住。不要用“大概”“感觉”,也不要编造原文没有的细节。
20
+ 4. **下一步怎么办**:给出一个明确建议——修订、晋升、保留或淘汰,并说明理由。拿不准就保留。
21
+
22
+ ## 怎么判断,不怎么做
23
+
24
+ - 读轨迹做整体判断,**不要数使用次数、成功次数,也不要设评分阈值**。一次高质量的成功加上清晰证据,可能比十次机械重复更值得晋升。
25
+ - “今天没被用到”不等于无效;也可能只是没遇到那个场景。只有被证据推翻、或场景已经消失时,才建议淘汰。
26
+ - 不要因为 SOP 写得漂亮就建议晋升。看它是否真的在真实任务里复现过,步骤是否稳定,换一个相似场景还能不能用。
27
+ - 用户明确纠正过的步骤,优先于 SOP 原文;把纠正写进建议里。
28
+ - 部门制度、canonical 和红线优先。SOP 与它们冲突时,建议修订或淘汰,不要建议晋升一个会覆盖红线的做法。
29
+
30
+ ## 什么时候值得晋升为技能
31
+
32
+ - 触发条件清楚,下一次遇到同类任务能认出来。
33
+ - 步骤和工具选择稳定,不依赖某一次的特殊环境。
34
+ - 有跨场景的真实成功证据,成功标准可判断。
35
+ - 失败或偏差有解释,且已经被修订吸收。
36
+
37
+ 四条都站得住,才建议晋升。任何一条拿不准,就保留在 testing,等下一次证据。
38
+
39
+ ## 怎么交付
40
+
41
+ 可以写一份临时详细报告,按 SOP 逐条列出判断和证据。回给守护会话的结论要短:每份 SOP 一句话——用了没有、效果如何、建议是什么、最关键的证据是哪条。守护会话会读报告、做最终决定,然后删除临时文件。
42
+
43
+ 不要给机械分数,不要输出使用次数表,也不要把报告写成实施规格。**判断和原文证据**才是你的产出。