@stardustlc/dsh-dream 0.4.1 → 0.5.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.
@@ -0,0 +1,87 @@
1
+ import { type Evidence, type EvidenceInput, type Lesson, type LessonInput, type LessonState, type ReviewDecision } from './knowledge.js';
2
+ /** events.jsonl 默认最多回放的行数(有界读取)。 */
3
+ export declare const MAX_REPLAY_EVENTS = 200000;
4
+ /** KnowledgeStore 构造选项(都可省略,保持 FREEZE 的 constructor(knowledgeDir) 可用)。 */
5
+ export interface KnowledgeStoreOptions {
6
+ /** 写盘前是否按 mask.ts 脱敏;默认 true(与插件默认 maskSecrets=true 一致)。 */
7
+ maskSecrets?: boolean;
8
+ /** 回放行数上限;默认 MAX_REPLAY_EVENTS,测试可调小。 */
9
+ maxReplayEvents?: number;
10
+ /** 获取锁的最长等待毫秒数;默认 2000。 */
11
+ lockTimeoutMs?: number;
12
+ /** 锁年龄超过该毫秒数且 pid 不存活才允许接管;默认 30000。 */
13
+ lockStaleMs?: number;
14
+ }
15
+ /**
16
+ * 经验知识存储。无文件时所有读取方法安全返回空,写入方法在首次写入时自建目录。
17
+ */
18
+ export declare class KnowledgeStore {
19
+ private readonly dir;
20
+ private readonly eventsPath;
21
+ private readonly evidencePath;
22
+ private readonly indexPath;
23
+ private readonly lockPath;
24
+ private readonly mask;
25
+ private readonly maxReplayEvents;
26
+ private readonly lockTimeoutMs;
27
+ private readonly lockStaleMs;
28
+ constructor(knowledgeDir: string, options?: KnowledgeStoreOptions);
29
+ /** 全部经验,按 updatedAt 降序(同刻按 id 稳定排序)。 */
30
+ listLessons(): Lesson[];
31
+ /** 全部证据,按 observedAt 升序(同刻按 id 稳定排序)。 */
32
+ listEvidence(): Evidence[];
33
+ /** 按 id 取经验;不存在返回 undefined。 */
34
+ getLesson(id: string): Lesson | undefined;
35
+ /** 追加证据;同 sessionId+recordSeq 或同 sourceHash 视为重复,返回已有记录且不写。 */
36
+ appendEvidence(input: EvidenceInput): {
37
+ evidence: Evidence;
38
+ created: boolean;
39
+ };
40
+ /**
41
+ * 创建候选经验。同 idempotencyKey 幂等;同指纹(文本 + 范围相同)确定性合并,
42
+ * 只把新证据并入已有经验,不新增经验。
43
+ */
44
+ createLesson(input: LessonInput, idempotencyKey: string): {
45
+ lesson: Lesson;
46
+ created: boolean;
47
+ };
48
+ /** 按 patch 更新经验;revision 不符抛 KnowledgeError('revision') 且不写。 */
49
+ updateLesson(id: string, patch: Partial<Lesson>, expectedRevision: number, idempotencyKey: string): Lesson;
50
+ /** 审阅经验:accepted → usable,rejected → rejected,unreviewed → candidate。 */
51
+ reviewLesson(id: string, decision: ReviewDecision, expectedRevision: number, idempotencyKey: string, actor?: string): Lesson;
52
+ /** 状态迁移;不允许的迁移抛 KnowledgeError('invalid') 且不写。 */
53
+ applyTransition(id: string, to: LessonState, expectedRevision: number, idempotencyKey: string): Lesson;
54
+ /** 从事件重建派生索引;不删除、不改写 events.jsonl / evidence.jsonl。 */
55
+ rebuildIndex(): void;
56
+ /** 统计。truncated=true 表示回放被 MAX_REPLAY_EVENTS 截断(不静默)。 */
57
+ stats(): {
58
+ lessons: number;
59
+ evidence: number;
60
+ events: number;
61
+ byState: Record<LessonState, number>;
62
+ truncated: boolean;
63
+ badLines: number;
64
+ };
65
+ private ensureDir;
66
+ private withLock;
67
+ /** 有界读取 JSONL:只保留最后 maxReplayEvents 条非空行,并报告总行数与截断。 */
68
+ private readBoundedLines;
69
+ /** 只回放事件流。 */
70
+ private loadEvents;
71
+ /** 事件流 + evidence.jsonl(两条路径都留痕,按 id 合并去重)。 */
72
+ private loadAll;
73
+ private applyEventLine;
74
+ private appendEvent;
75
+ private appendLine;
76
+ private findEvidenceByNaturalKey;
77
+ /** 在锁内追加/复用一条证据;同天然身份键返回已有记录且不写。 */
78
+ private persistEvidenceLocked;
79
+ /** 在锁内追加 lesson.update 并同步内存与索引。 */
80
+ private appendLessonUpdate;
81
+ private maskLesson;
82
+ private maskEvidence;
83
+ private buildIndexPayload;
84
+ /** 自动更新派生索引:失败降级(权威事件已落盘),不推翻调用结果。 */
85
+ private writeIndex;
86
+ private writeIndexStrict;
87
+ }