@remnic/belief-ledger 9.3.596

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.md ADDED
@@ -0,0 +1,70 @@
1
+ # @remnic/belief-ledger
2
+
3
+ A Remnic library for a personal belief and prediction ledger. It stores claims,
4
+ predictions, and opinions as structured Remnic facts, retrieves prior claims that
5
+ may conflict, asks a host LLM to classify the relationship, and tracks prediction
6
+ calibration over time.
7
+
8
+ This package is intentionally host-neutral:
9
+
10
+ - It depends on `@remnic/core` public APIs.
11
+ - It does not import OpenClaw, Hermes, or direct OpenAI clients.
12
+ - Host adapters provide LLM access through `LedgerLlmAdapter`, or wrap Remnic's
13
+ existing `FallbackLlmClient` with `createFallbackLlmLedgerAdapter`.
14
+
15
+ ## Basic Usage
16
+
17
+ ```ts
18
+ import { StorageManager } from "@remnic/core";
19
+ import {
20
+ BeliefLedger,
21
+ RemnicLedgerStore,
22
+ createFallbackLlmLedgerAdapter,
23
+ } from "@remnic/belief-ledger";
24
+
25
+ const storage = new StorageManager("/path/to/remnic-memory");
26
+ const store = new RemnicLedgerStore(storage);
27
+ const llm = createFallbackLlmLedgerAdapter(fallbackLlmClient);
28
+
29
+ const ledger = new BeliefLedger({ store, llm });
30
+
31
+ const result = await ledger.capture({
32
+ text: "I think local-first memory tools will beat cloud-only memory tools by 2027.",
33
+ });
34
+
35
+ if (result.challenge) {
36
+ console.log(result.challenge.question);
37
+ }
38
+ ```
39
+
40
+ ## What Gets Stored
41
+
42
+ Claims are written as Remnic `fact` memories with:
43
+
44
+ - `tags`: `belief-ledger`, claim kind, status, and optional domain.
45
+ - `entityRef`: the first extracted entity when one exists.
46
+ - `structuredAttributes`: stance, confidence, domain, entities, deadline,
47
+ evidence links, status, prediction outcome, and Brier score.
48
+
49
+ That confirms issue #869's upstream requirements through the public core
50
+ surface: custom metadata, entity-scoped retrieval inputs, and supersession via
51
+ `StorageManager.supersedeMemory`.
52
+
53
+ ## Host LLM Access
54
+
55
+ The package asks for a `LedgerLlmAdapter` rather than an API key. OpenClaw,
56
+ Hermes, CLI, or app adapters can route the calls through their normal Remnic LLM
57
+ path. For OpenClaw-backed Remnic runtimes, pass the existing gateway-backed
58
+ `FallbackLlmClient` to `createFallbackLlmLedgerAdapter`.
59
+
60
+ ## Core Flows
61
+
62
+ - `capture`: extract a structured claim, persist it, retrieve prior claims,
63
+ judge contradictions, and draft a Socratic challenge when needed.
64
+ - `crossExamine`: run retrieval and LLM judging against an existing claim.
65
+ - `supersede`, `split`, `resolve`, `snooze`, `ignore`: typed ledger actions
66
+ suitable for host tool/function surfaces.
67
+ - `scoreDuePredictions`: find predictions past deadline, grade them from a
68
+ verdict source or return a user-verdict prompt.
69
+ - `reflect`: compute calibration summaries, Brier score, flipped claims, and
70
+ dormant topics.
@@ -0,0 +1,319 @@
1
+ import { MemoryFile, StorageManager, FallbackLlmOptions, FallbackLlmClient, MemoryFrontmatter } from '@remnic/core';
2
+
3
+ type LedgerClaimKind = "claim" | "prediction" | "opinion";
4
+ type LedgerStance = "for" | "against" | "uncertain" | "neutral";
5
+ type LedgerClaimStatus = "active" | "superseded" | "resolved" | "snoozed" | "ignored";
6
+ type LedgerJudgeClassification = "contradiction" | "evolution" | "refinement" | "unrelated";
7
+ type LedgerPredictionVerdict = "true" | "false" | "mixed" | "unknown";
8
+ interface LedgerTimeWindow {
9
+ start?: string;
10
+ end?: string;
11
+ }
12
+ interface LedgerClaimScope {
13
+ entities: string[];
14
+ domain?: string;
15
+ timeWindow?: LedgerTimeWindow;
16
+ }
17
+ interface LedgerResolution {
18
+ verdict: LedgerPredictionVerdict;
19
+ actualConfidence: number;
20
+ resolvedAt: string;
21
+ source?: string;
22
+ notes?: string;
23
+ brierScore?: number;
24
+ }
25
+ interface LedgerClaim {
26
+ id: string;
27
+ memoryId: string;
28
+ statement: string;
29
+ kind: LedgerClaimKind;
30
+ stance: LedgerStance;
31
+ confidence: number;
32
+ scope: LedgerClaimScope;
33
+ deadline?: string;
34
+ evidenceLinks: string[];
35
+ status: LedgerClaimStatus;
36
+ createdAt: string;
37
+ updatedAt: string;
38
+ supersedes?: string;
39
+ supersededBy?: string;
40
+ parentIds: string[];
41
+ snoozedUntil?: string;
42
+ ignoredAt?: string;
43
+ ignoredReason?: string;
44
+ resolution?: LedgerResolution;
45
+ sourceText?: string;
46
+ sourceMemory?: MemoryFile;
47
+ }
48
+ interface LedgerClaimDraft {
49
+ statement: string;
50
+ kind?: LedgerClaimKind;
51
+ stance: LedgerStance;
52
+ confidence: number;
53
+ scope?: Partial<LedgerClaimScope>;
54
+ deadline?: string;
55
+ evidenceLinks?: string[];
56
+ }
57
+ interface LedgerCaptureInput {
58
+ text: string;
59
+ now?: string;
60
+ source?: string;
61
+ sessionKey?: string;
62
+ }
63
+ interface LedgerExtractionRequest extends LedgerCaptureInput {
64
+ now: string;
65
+ }
66
+ interface LedgerJudgeRequest {
67
+ current: LedgerClaim;
68
+ prior: LedgerClaim;
69
+ }
70
+ interface LedgerJudgeResult {
71
+ priorClaimId: string;
72
+ classification: LedgerJudgeClassification;
73
+ confidence: number;
74
+ rationale: string;
75
+ }
76
+ interface LedgerChallengeRequest {
77
+ current: LedgerClaim;
78
+ contradictions: Array<{
79
+ claim: LedgerClaim;
80
+ judgment: LedgerJudgeResult;
81
+ }>;
82
+ }
83
+ interface LedgerChallenge {
84
+ question: string;
85
+ priorClaimIds: string[];
86
+ suggestedActions: Array<"supersede" | "split" | "resolve" | "ignore">;
87
+ }
88
+ interface LedgerPredictionGradeRequest {
89
+ claim: LedgerClaim;
90
+ verdictSource?: string;
91
+ now: string;
92
+ }
93
+ interface LedgerPredictionGrade {
94
+ verdict: LedgerPredictionVerdict;
95
+ actualConfidence: number;
96
+ rationale: string;
97
+ source?: string;
98
+ }
99
+ interface LedgerLlmAdapter {
100
+ extractClaim(request: LedgerExtractionRequest): Promise<LedgerClaimDraft>;
101
+ judgeClaimPair(request: LedgerJudgeRequest): Promise<LedgerJudgeResult>;
102
+ draftSocraticChallenge(request: LedgerChallengeRequest): Promise<LedgerChallenge>;
103
+ gradePrediction?(request: LedgerPredictionGradeRequest): Promise<LedgerPredictionGrade | null>;
104
+ }
105
+ interface LedgerRetrievalCandidate {
106
+ claim: LedgerClaim;
107
+ score: number;
108
+ reasons: string[];
109
+ }
110
+ interface LedgerSemanticRetrievalAdapter {
111
+ scoreClaims(request: {
112
+ query: LedgerClaim;
113
+ candidates: LedgerClaim[];
114
+ limit: number;
115
+ }): Promise<Array<{
116
+ claimId: string;
117
+ score: number;
118
+ }>>;
119
+ }
120
+ interface LedgerRerankerAdapter {
121
+ rerank(request: {
122
+ query: LedgerClaim;
123
+ candidates: LedgerRetrievalCandidate[];
124
+ limit: number;
125
+ }): Promise<LedgerRetrievalCandidate[]>;
126
+ }
127
+ interface LedgerRetrievalOptions {
128
+ limit?: number;
129
+ includeStatuses?: LedgerClaimStatus[];
130
+ now?: string;
131
+ semantic?: LedgerSemanticRetrievalAdapter;
132
+ reranker?: LedgerRerankerAdapter;
133
+ }
134
+ interface LedgerCrossExaminationStats {
135
+ candidatesConsidered: number;
136
+ judged: number;
137
+ contradictions: number;
138
+ unrelated: number;
139
+ observableFalsePositiveRate: number;
140
+ }
141
+ interface LedgerCrossExaminationResult {
142
+ claim: LedgerClaim;
143
+ candidates: LedgerRetrievalCandidate[];
144
+ judgments: LedgerJudgeResult[];
145
+ challenge?: LedgerChallenge;
146
+ stats: LedgerCrossExaminationStats;
147
+ }
148
+ interface LedgerCaptureResult extends LedgerCrossExaminationResult {
149
+ claim: LedgerClaim;
150
+ }
151
+ interface LedgerStore {
152
+ createClaim(input: Omit<LedgerClaim, "id" | "memoryId" | "sourceMemory">): Promise<LedgerClaim>;
153
+ getClaim(id: string): Promise<LedgerClaim | null>;
154
+ listClaims(filter?: {
155
+ statuses?: LedgerClaimStatus[];
156
+ kinds?: LedgerClaimKind[];
157
+ }): Promise<LedgerClaim[]>;
158
+ updateClaim(id: string, patch: Partial<LedgerClaim>): Promise<LedgerClaim>;
159
+ supersedeClaim(priorId: string, newId: string, reason: string): Promise<boolean>;
160
+ }
161
+ interface LedgerScoreDuePredictionsOptions {
162
+ now?: string;
163
+ verdictSources?: Record<string, string>;
164
+ limit?: number;
165
+ }
166
+ interface LedgerPredictionScoreResult {
167
+ claim: LedgerClaim;
168
+ status: "resolved" | "needs_user_verdict" | "skipped";
169
+ resolution?: LedgerResolution;
170
+ prompt?: string;
171
+ reason?: string;
172
+ }
173
+ interface LedgerReflectionOptions {
174
+ now?: string;
175
+ dormantAfterDays?: number;
176
+ }
177
+ interface LedgerCalibrationBin {
178
+ minConfidence: number;
179
+ maxConfidence: number;
180
+ count: number;
181
+ meanPredictedConfidence: number;
182
+ meanActualConfidence: number;
183
+ calibrationError: number;
184
+ }
185
+ interface LedgerDomainCalibration {
186
+ domain: string;
187
+ count: number;
188
+ brierScore: number;
189
+ meanPredictedConfidence: number;
190
+ meanActualConfidence: number;
191
+ tendency: "overconfident" | "underconfident" | "well_calibrated";
192
+ }
193
+ interface LedgerFlippedClaim {
194
+ rootClaimId: string;
195
+ claimIds: string[];
196
+ statements: string[];
197
+ flipCount: number;
198
+ }
199
+ interface LedgerDormantTopic {
200
+ topic: string;
201
+ lastClaimAt: string;
202
+ daysSilent: number;
203
+ claimCount: number;
204
+ }
205
+ interface LedgerReflectionReport {
206
+ generatedAt: string;
207
+ totalClaims: number;
208
+ activeClaims: number;
209
+ resolvedPredictions: number;
210
+ brierScore?: number;
211
+ calibrationBins: LedgerCalibrationBin[];
212
+ domains: LedgerDomainCalibration[];
213
+ flippedClaims: LedgerFlippedClaim[];
214
+ dormantTopics: LedgerDormantTopic[];
215
+ }
216
+
217
+ interface RemnicLedgerStoreOptions {
218
+ now?: () => Date;
219
+ source?: string;
220
+ writeEntityLinks?: boolean;
221
+ }
222
+ declare class RemnicLedgerStore implements LedgerStore {
223
+ private readonly storage;
224
+ private readonly now;
225
+ private readonly source;
226
+ private readonly shouldWriteEntityLinks;
227
+ constructor(storage: StorageManager, options?: RemnicLedgerStoreOptions);
228
+ createClaim(input: Omit<LedgerClaim, "id" | "memoryId" | "sourceMemory">): Promise<LedgerClaim>;
229
+ getClaim(id: string): Promise<LedgerClaim | null>;
230
+ listClaims(filter?: {
231
+ statuses?: LedgerClaimStatus[];
232
+ kinds?: LedgerClaimKind[];
233
+ }): Promise<LedgerClaim[]>;
234
+ updateClaim(id: string, patch: Partial<LedgerClaim>): Promise<LedgerClaim>;
235
+ supersedeClaim(priorId: string, newId: string, reason: string): Promise<boolean>;
236
+ private getLedgerMemoryById;
237
+ private readAllLedgerCandidateMemories;
238
+ private writeClaimMemory;
239
+ private restoreClaimBestEffort;
240
+ private writeSupersessionCorrectionBestEffort;
241
+ private claimFromMemorySafely;
242
+ private writeEntityLinksBestEffort;
243
+ private writeEntityLinks;
244
+ }
245
+
246
+ interface BeliefLedgerOptions {
247
+ store: LedgerStore;
248
+ llm: LedgerLlmAdapter;
249
+ retrieval?: LedgerRetrievalOptions;
250
+ now?: () => Date;
251
+ }
252
+ interface BeliefLedgerFromStorageOptions extends RemnicLedgerStoreOptions {
253
+ llm: LedgerLlmAdapter;
254
+ retrieval?: LedgerRetrievalOptions;
255
+ }
256
+ interface ResolveClaimInput {
257
+ verdict: "true" | "false" | "mixed" | "unknown";
258
+ actualConfidence: number;
259
+ source?: string;
260
+ notes?: string;
261
+ resolvedAt?: string;
262
+ }
263
+ declare class BeliefLedger {
264
+ private readonly store;
265
+ private readonly llm;
266
+ private readonly retrieval;
267
+ private readonly now;
268
+ constructor(options: BeliefLedgerOptions);
269
+ static fromStorage(storage: StorageManager, options: BeliefLedgerFromStorageOptions): BeliefLedger;
270
+ capture(input: LedgerCaptureInput): Promise<LedgerCaptureResult>;
271
+ crossExamine(claimOrId: LedgerClaim | string, options?: LedgerRetrievalOptions): Promise<LedgerCrossExaminationResult>;
272
+ supersede(priorId: string, newId: string, reason: string): Promise<boolean>;
273
+ split(priorId: string, parts: LedgerClaimDraft[], reason?: string): Promise<LedgerClaim[]>;
274
+ resolve(claimId: string, input: ResolveClaimInput): Promise<LedgerClaim>;
275
+ snooze(claimId: string, until: string): Promise<LedgerClaim>;
276
+ ignore(claimId: string, reason?: string): Promise<LedgerClaim>;
277
+ scoreDuePredictions(options?: LedgerScoreDuePredictionsOptions): Promise<LedgerPredictionScoreResult[]>;
278
+ reflect(options?: LedgerReflectionOptions): Promise<LedgerReflectionReport>;
279
+ private requireClaim;
280
+ }
281
+
282
+ interface FallbackLlmLedgerAdapterOptions extends FallbackLlmOptions {
283
+ agentId?: string;
284
+ }
285
+ declare function createFallbackLlmLedgerAdapter(client: FallbackLlmClient, options?: FallbackLlmLedgerAdapterOptions): LedgerLlmAdapter;
286
+
287
+ declare function retrievePriorClaims(query: LedgerClaim, store: LedgerStore, options?: LedgerRetrievalOptions): Promise<LedgerRetrievalCandidate[]>;
288
+
289
+ declare function buildReflectionReport(claims: LedgerClaim[], options?: LedgerReflectionOptions): LedgerReflectionReport;
290
+
291
+ declare const LEDGER_TAG = "belief-ledger";
292
+ declare const LEDGER_SCHEMA_VERSION = "1";
293
+ type StringRecord = Record<string, string>;
294
+ declare function normalizeClaimDraft(draft: LedgerClaimDraft, options: {
295
+ now: string;
296
+ sourceText?: string;
297
+ }): Omit<LedgerClaim, "id" | "memoryId" | "sourceMemory">;
298
+ declare function normalizeJudgeResult(raw: unknown, priorClaimId: string): LedgerJudgeResult;
299
+ declare function normalizeChallenge(raw: unknown, priorClaimIds: string[]): LedgerChallenge;
300
+ declare function normalizePredictionGrade(raw: unknown): LedgerPredictionGrade;
301
+ declare function createResolution(input: {
302
+ verdict: LedgerPredictionVerdict;
303
+ actualConfidence: number;
304
+ resolvedAt: string;
305
+ source?: string;
306
+ notes?: string;
307
+ predictedConfidence: number;
308
+ }): LedgerResolution;
309
+ declare function computeBrierScore(predictedConfidence: number, actualConfidence: number): number;
310
+ declare function roundMetric(value: number): number;
311
+ declare function claimToStructuredAttributes(claim: LedgerClaim): StringRecord;
312
+ declare function claimTags(claim: LedgerClaim): string[];
313
+ declare function serializeClaimBody(claim: Pick<LedgerClaim, "statement" | "kind" | "stance" | "confidence" | "scope" | "deadline" | "evidenceLinks" | "status" | "resolution">): string;
314
+ declare function claimFromMemory(memory: MemoryFile): LedgerClaim | null;
315
+ declare function memoryFrontmatterPatchForClaim(claim: LedgerClaim): Partial<MemoryFrontmatter>;
316
+ declare function remnicMemoryStatusForClaim(claim: Pick<LedgerClaim, "status">): NonNullable<MemoryFrontmatter["status"]>;
317
+ declare function normalizeIsoTimestamp(field: string, value: unknown): string;
318
+
319
+ export { BeliefLedger, type BeliefLedgerFromStorageOptions, type BeliefLedgerOptions, type FallbackLlmLedgerAdapterOptions, LEDGER_SCHEMA_VERSION, LEDGER_TAG, type LedgerCalibrationBin, type LedgerCaptureInput, type LedgerCaptureResult, type LedgerChallenge, type LedgerChallengeRequest, type LedgerClaim, type LedgerClaimDraft, type LedgerClaimKind, type LedgerClaimScope, type LedgerClaimStatus, type LedgerCrossExaminationResult, type LedgerCrossExaminationStats, type LedgerDomainCalibration, type LedgerDormantTopic, type LedgerFlippedClaim, type LedgerJudgeClassification, type LedgerJudgeRequest, type LedgerJudgeResult, type LedgerLlmAdapter, type LedgerPredictionGrade, type LedgerPredictionGradeRequest, type LedgerPredictionScoreResult, type LedgerPredictionVerdict, type LedgerReflectionOptions, type LedgerReflectionReport, type LedgerRerankerAdapter, type LedgerResolution, type LedgerRetrievalCandidate, type LedgerRetrievalOptions, type LedgerScoreDuePredictionsOptions, type LedgerSemanticRetrievalAdapter, type LedgerStance, type LedgerStore, type LedgerTimeWindow, RemnicLedgerStore, type RemnicLedgerStoreOptions, type ResolveClaimInput, buildReflectionReport, claimFromMemory, claimTags, claimToStructuredAttributes, computeBrierScore, createFallbackLlmLedgerAdapter, createResolution, memoryFrontmatterPatchForClaim, normalizeChallenge, normalizeClaimDraft, normalizeIsoTimestamp, normalizeJudgeResult, normalizePredictionGrade, remnicMemoryStatusForClaim, retrievePriorClaims, roundMetric, serializeClaimBody };