@chatpanel/events 0.33.1 → 0.47.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/index.js CHANGED
@@ -38,6 +38,32 @@ export { parseFlowchart, layoutFlowchart, renderFlowchartSvg } from './flowchart
38
38
  export { validateView, validateViewInvocation, viewResult } from './view.js';
39
39
  export { validateWidget, validateWidgetMessage, effectiveGrants, widgetIcon, WIDGET_SURFACES } from './widget.js';
40
40
  export { fuseRRF, planQueries, multiSearch } from './rrf.js';
41
+
42
+ // The compounding layer — subjects a brief can accumulate about, and the deterministic
43
+ // half of the maintenance pass (W0's read-only survey is `surveyCorpus`).
44
+ export {
45
+ SUBJECT_KINDS, DEFAULT_THRESHOLD, MAX_SUBJECTS, MAX_SUBJECT_CHARS,
46
+ normalizeSubject, subjectKey, subjectTokens, isSubjectCandidate,
47
+ aliasMap, resolveSubjects, earnsBrief, rankSubjects,
48
+ REDACTION_TOKEN_TYPES, isRedactionToken, SELF_LABELS, isSelfLabel, stripQualifiers,
49
+ suggestMerges,
50
+ } from './entity.js';
51
+ export {
52
+ BRIEF_STATES, CLAIM_KINDS, MAX_CLAIMS, MAX_CLAIM_REFS, MAX_BRIEF_RECORDS, MAX_BRIEF_CHARS,
53
+ contentHash, briefId, briefToText, briefTerms, briefLinks, checkKnowledgeInvariants, parseBriefText,
54
+ } from './knowledge.js';
55
+ export { deriveBrief, deriveBriefs, driftedRefs } from './knowledge-derive.js';
56
+ export {
57
+ SYNTHESIS_SCHEMA, synthesisPrompt, claimsFromSynthesis,
58
+ MAX_SYNTHESIS_CLAIMS, MAX_EXCERPTS, MAX_EXCERPT_CHARS,
59
+ } from './synthesis.js';
60
+ export { PROPOSAL_STATES, SUPERSEDE_OVERLAP, propose, accept, reject, diffProposal, converge, linkClaims } from './promotion.js';
61
+ export {
62
+ normalizeRecord, normalizeRecords, wikilinksIn, redactedTokensIn, redactionCost,
63
+ wantedPages, orphanRecords,
64
+ duplicateTitles, vocabularyDrift, mentionsFrom, spanningQuestions,
65
+ surveyCorpus, thresholdSweep, formatSurvey, NEAR_TITLE_DISTANCE, SPAN_MIN_TERMS,
66
+ } from './curate.js';
41
67
  export {
42
68
  PDF_MAX_CHARS, linesFromItems, orderLines, paragraphsFromLines,
43
69
  pageTextFromItems, looksScanned, buildPdfDocument,
@@ -130,3 +156,55 @@ export {
130
156
  titleFromParticipants, titleFromDate, deriveMeetingTitle, shouldAutoTitle, isBetterTitleSource,
131
157
  meetingTitlePrompt, parseTitleResponse,
132
158
  } from './titles.js';
159
+
160
+ // The library — one record model (chat / note / meeting / brief) for every client, so
161
+ // "what is the title of this meeting" has one answer rather than one per codebase.
162
+ export {
163
+ RECORD_KINDS, MAX_TITLE_LEN, LibraryError,
164
+ parseRecordId, makeRecordId, isRecordId,
165
+ normalizeStoredRecord, isValidStoredRecord, toSearchRecord, searchTextFor, toIndexEntry,
166
+ deriveTitle, snippetOf, wordCount,
167
+ } from './library.js';
168
+
169
+ // Two-way sync, as a pure plan over two index lists. Shared because the tie-break must not
170
+ // depend on which client is asking, or two clients flap forever.
171
+ export {
172
+ CLOCK_TOLERANCE_MS, SyncError,
173
+ stampOf, decide, planSync, isSettled, forkConflict, advanceBases,
174
+ } from './sync-plan.js';
175
+
176
+ // The encrypted backup wire format — the corpus's only lossless channel between clients.
177
+ export {
178
+ ENCRYPTED_TYPE, COMPRESSIONS, BackupError,
179
+ // Aliased: `vault.js` also exports KDF_ITERATIONS, and the two are genuinely different
180
+ // numbers (the vault derives at 310k, a backup at 250k). Shadowing one with the other
181
+ // would silently re-key every backup this package writes.
182
+ KDF_ITERATIONS as BACKUP_KDF_ITERATIONS,
183
+ encryptBackup, decryptBackup, isEncryptedBackup,
184
+ identityCodec, streamCodec, nodeCodec, bestCodec,
185
+ // `toB64`/`fromB64` are deliberately NOT re-exported: `vault.js` already owns those names
186
+ // here. They remain on the module for callers that import the file directly.
187
+ } from './backup-envelope.js';
188
+
189
+ // The command bar's grammar, so muscle memory transfers between surfaces.
190
+ export {
191
+ OMNI_MODES, OMNI_GRAMMAR,
192
+ parseOmni, extractFilters, resolveSince, wantsModel, isActionable,
193
+ } from './omni.js';
194
+
195
+ // The palette as data, for clients that cannot import a stylesheet.
196
+ export {
197
+ THEMES, LIGHT, DARK, PALETTES, SHAPE, TOKEN_NAMES, TOKEN_ROLES,
198
+ paletteFor, cssVarName, toCssVars, themeStylesheet, resolveTheme,
199
+ } from './theme.js';
200
+
201
+ // Plans, gates and the signed entitlement — one definition, every client. The public
202
+ // verification key lives here so a rotation reaches all of them, instead of being a
203
+ // hand-edit in each (which is what CLAUDE.md currently has to warn about).
204
+ export {
205
+ PLANS, API_BASE, ENDPOINTS, ENTITLEMENT_PUBLIC_JWK, UPGRADE_URL,
206
+ FEATURE_TIER, PRO_FEATURES, TEAM_FEATURES, FREE_LIMITS,
207
+ RECHECK_INTERVAL_MS, EntitlementError,
208
+ checkoutUrl, planOf, planLabel, isPro, isTeam, can, tierFor, withinFreeLimit,
209
+ verifyEntitlement, licenseFromPayload, needsRecheck,
210
+ } from './entitlement.js';
@@ -0,0 +1,267 @@
1
+ // DERIVING briefs — the pass that turns a corpus into the derived layer.
2
+ //
3
+ // Separate from `knowledge.js` (the model) because the costs are not alike: reading a brief
4
+ // needs its shape and its renderer; building one walks every record, resolves entities and
5
+ // runs the deterministic maintenance passes. Only pages ever build. The MV3 service worker
6
+ // only ever reads — it syncs stored briefs onward — so keeping this out of `knowledge.js`
7
+ // keeps it off the worker's cold start entirely, rather than relying on nobody importing it.
8
+ //
9
+ // Everything here is class R: no model, no network, and `now` is injected rather than read,
10
+ // so a rebuild is reproducible. That is what makes invariant I-K2 true — deriveBriefs() is
11
+ // the ONLY way a brief is ever created, so throwing them all away can never lose anything.
12
+
13
+ import { makeRef } from './ref.js';
14
+ import {
15
+ DEFAULT_THRESHOLD, MAX_SUBJECTS, normalizeSubject, rankSubjects, resolveSubjects,
16
+ } from './entity.js';
17
+ import { mentionsFrom, normalizeRecords, wantedPages } from './curate.js';
18
+ import { MAX_CLAIMS, MAX_CLAIM_REFS, MAX_BRIEF_RECORDS, briefId, contentHash } from './knowledge.js';
19
+
20
+ /** Records are addressed `chat:x` / `meeting:y` / `note:z` — the ref kind is the prefix. */
21
+ function refForRecord(rec) {
22
+ const [kind, ...rest] = String(rec.id).split(':');
23
+ const id = rest.join(':') || rec.id;
24
+ const known = kind === 'chat' || kind === 'meeting' || kind === 'note' || kind === 'page';
25
+ return makeRef({ kind: known ? kind : 'result', id: known ? id : rec.id, hash: contentHash(rec.text) });
26
+ }
27
+
28
+ function claim({ id, kind, text, refs, at = 0, confidence = 1 }) {
29
+ return {
30
+ id,
31
+ kind,
32
+ text,
33
+ // I-K1 lives here: a claim is CONSTRUCTED with its refs, and `checkKnowledgeInvariants`
34
+ // refuses one that arrives without them. There is no path that writes a claim first and
35
+ // attaches provenance later, because that path is how provenance goes missing.
36
+ refs: refs.slice(0, MAX_CLAIM_REFS),
37
+ firstSeen: at,
38
+ lastConfirmed: at,
39
+ confidence,
40
+ cls: 'R', // class R — derived, not written. W3's prose claims carry 'C'.
41
+ };
42
+ }
43
+
44
+ const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
45
+ const isoDay = (ms) => (ms ? new Date(ms).toISOString().slice(0, 10) : '');
46
+
47
+ // How many co-occurring subjects a `together` claim names. Past a handful it stops being a
48
+ // statement and becomes a tag cloud.
49
+ const TOGETHER_LIMIT = 5;
50
+
51
+ /**
52
+ * Memories that are ABOUT this subject become its highest-confidence claims.
53
+ *
54
+ * This is how the existing memory layer folds in, and the direction matters. `identity` and
55
+ * `preference` memories are AMBIENT — they ride every turn already, and copying them onto a
56
+ * brief would say them twice. The other three kinds (`project`, `fact`, `reference`) are
57
+ * retrieved rather than ambient, and a retrieved durable statement about a subject is
58
+ * exactly what a claim is — except the user said it themselves, so it outranks anything
59
+ * derived from co-occurrence.
60
+ *
61
+ * The reverse never happens automatically: a brief must not write itself into memory. Memory
62
+ * is bounded because it is ambient; a corpus flowing into it would unbound the one thing in
63
+ * the product that is deliberately small.
64
+ */
65
+ function statedClaims(subject, memories, seq) {
66
+ const out = [];
67
+ const names = [subject.canonical, ...subject.aliases].filter(Boolean);
68
+ for (const mem of memories) {
69
+ if (!mem?.text || !mem.id) continue;
70
+ if (mem.kind === 'identity' || mem.kind === 'preference') continue; // already ambient
71
+ const hay = normalizeSubject(mem.text);
72
+ // Whole-token containment, so "pricing" does not match "repricing" and a one-word
73
+ // subject cannot claim every memory that happens to contain it as a substring.
74
+ const hit = names.some((n) => n && (hay === n || hay.includes(` ${n} `) || hay.startsWith(`${n} `) || hay.endsWith(` ${n}`)));
75
+ if (!hit) continue;
76
+ out.push(claim({
77
+ id: `${seq()}`,
78
+ kind: 'stated',
79
+ text: mem.text,
80
+ refs: [makeRef({ kind: 'memory', id: mem.id, hash: contentHash(mem.text) })],
81
+ at: Number(mem.updatedAt || mem.createdAt) || 0,
82
+ confidence: Number(mem.confidence) || 1,
83
+ }));
84
+ }
85
+ return out;
86
+ }
87
+
88
+ /**
89
+ * One subject + the records that mention it → a brief.
90
+ *
91
+ * Every claim here is a restatement of something the corpus already holds, which is what
92
+ * makes the phase free and what makes I-K3's auto-promotion defensible: none of it is an
93
+ * opinion, so there is nothing for a reviewer to review.
94
+ */
95
+ export function deriveBrief(subject, { records, byId, cooccurring = [], memories = [], wanted = null, now = 0 } = {}) {
96
+ const recs = [...(subject.records instanceof Set ? subject.records : subject.records || [])]
97
+ .map((id) => byId.get(id)).filter(Boolean)
98
+ .sort((a, b) => (a.date || 0) - (b.date || 0));
99
+ if (!recs.length && !wanted) return null;
100
+
101
+ let n = 0;
102
+ const seq = () => { n += 1; return `c${n}`; };
103
+ const claims = [];
104
+
105
+ if (recs.length) {
106
+ const byType = {};
107
+ for (const r of recs) byType[r.type] = (byType[r.type] || 0) + 1;
108
+ const parts = Object.entries(byType).sort((a, b) => b[1] - a[1]).map(([t, c]) => plural(c, t));
109
+ claims.push(claim({
110
+ id: seq(),
111
+ kind: 'presence',
112
+ text: `Appears in ${plural(recs.length, 'record')} — ${parts.join(', ')}.`,
113
+ // The most recent records, because that is what a reader checks first — and the
114
+ // newest is also the one most likely to still resolve.
115
+ refs: recs.slice(-MAX_CLAIM_REFS).reverse().map(refForRecord),
116
+ at: now,
117
+ }));
118
+
119
+ const first = recs.find((r) => r.date);
120
+ const last = [...recs].reverse().find((r) => r.date);
121
+ if (first && last && first !== last) {
122
+ claims.push(claim({
123
+ id: seq(),
124
+ kind: 'timeline',
125
+ text: `Runs from ${isoDay(first.date)} to ${isoDay(last.date)} — ${first.title || 'untitled'} → ${last.title || 'untitled'}.`,
126
+ refs: [refForRecord(first), refForRecord(last)],
127
+ at: now,
128
+ }));
129
+ }
130
+ }
131
+
132
+ if (cooccurring.length) {
133
+ claims.push(claim({
134
+ id: seq(),
135
+ kind: 'together',
136
+ text: `Usually alongside ${cooccurring.slice(0, TOGETHER_LIMIT).map((c) => c.name).join(', ')}.`,
137
+ refs: recs.slice(-MAX_CLAIM_REFS).map(refForRecord),
138
+ at: now,
139
+ }));
140
+ }
141
+
142
+ if (wanted) {
143
+ // A [[link]] that resolves to nothing is the corpus asking for a page: a human already
144
+ // decided the subject was worth naming. Saying so on the brief is more useful than
145
+ // hiding it, because it tells the reader the page is thin BECAUSE nobody wrote the
146
+ // source, not because the derivation failed.
147
+ claims.push(claim({
148
+ id: seq(),
149
+ kind: 'wanted',
150
+ text: `Linked from ${plural(wanted.recordCount, 'record')} but no record carries this title.`,
151
+ refs: recs.slice(0, MAX_CLAIM_REFS).map(refForRecord),
152
+ at: now,
153
+ }));
154
+ }
155
+
156
+ claims.push(...statedClaims(subject, memories, seq));
157
+
158
+ const capped = claims.slice(0, MAX_CLAIMS);
159
+ const dates = recs.map((r) => r.date || 0).filter(Boolean);
160
+ return {
161
+ id: briefId(subject.key),
162
+ key: subject.key,
163
+ kind: subject.kind,
164
+ subject: { name: subject.name, aliases: [...(subject.aliases || [])] },
165
+ // Class R may auto-promote (I-K3): nothing above is an opinion, so there is nothing a
166
+ // reviewer could accept or reject. W3's prose arrives as `proposed` beside it.
167
+ state: 'promoted',
168
+ cls: 'R',
169
+ claims: capped,
170
+ records: recs.slice(-MAX_BRIEF_RECORDS).map((r) => ({ id: r.id, type: r.type, title: r.title, date: r.date })),
171
+ stats: {
172
+ records: recs.length,
173
+ mentions: subject.mentions,
174
+ claims: capped.length,
175
+ first: dates.length ? Math.min(...dates) : 0,
176
+ last: dates.length ? Math.max(...dates) : 0,
177
+ wanted: !!wanted,
178
+ },
179
+ createdAt: now,
180
+ updatedAt: now,
181
+ };
182
+ }
183
+
184
+ /**
185
+ * The whole corpus → the briefs it earns. This IS invariant I-K2: it is the only way a
186
+ * brief is ever created, so throwing them all away can never lose anything.
187
+ *
188
+ * `now` is injected rather than read, so a rebuild is reproducible and testable — the same
189
+ * rule loop.js and schedule.js follow.
190
+ */
191
+ export function deriveBriefs(records = [], {
192
+ memories = [], merges = null, self = '', threshold = DEFAULT_THRESHOLD,
193
+ limit = MAX_SUBJECTS, now = 0,
194
+ } = {}) {
195
+ const recs = normalizeRecords(records);
196
+ const byId = new Map(recs.map((r) => [r.id, r]));
197
+ const mentions = mentionsFrom(recs);
198
+ // `merges` and `self` are corrections the USER made, and they are applied here — as inputs
199
+ // to the pass, never as edits to its output. That is what keeps I-K2 true: a rebuild
200
+ // re-applies them, where a rebuild that erased them would teach the user not to bother.
201
+ const subjects = resolveSubjects(mentions, { merges, self });
202
+ const ranked = rankSubjects(subjects, { threshold, limit });
203
+
204
+ // Which subjects share records with which — the `together` claim, computed once for the
205
+ // whole corpus rather than per subject, so this stays linear in mentions rather than
206
+ // quadratic in subjects.
207
+ const perRecord = new Map();
208
+ for (const s of ranked) {
209
+ for (const id of s.records) {
210
+ if (!perRecord.has(id)) perRecord.set(id, []);
211
+ perRecord.get(id).push(s.key);
212
+ }
213
+ }
214
+ const pairs = new Map();
215
+ for (const keys of perRecord.values()) {
216
+ for (const a of keys) for (const b of keys) {
217
+ if (a === b) continue;
218
+ const m = pairs.get(a) || new Map();
219
+ m.set(b, (m.get(b) || 0) + 1);
220
+ pairs.set(a, m);
221
+ }
222
+ }
223
+ const byKey = new Map(ranked.map((s) => [s.key, s]));
224
+ const wantedByNorm = new Map(wantedPages(recs).map((w) => [w.norm, w]));
225
+
226
+ const out = [];
227
+ for (const s of ranked) {
228
+ const co = [...(pairs.get(s.key) || new Map()).entries()]
229
+ .sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
230
+ .map(([key, count]) => ({ key, count, name: byKey.get(key)?.name || key }))
231
+ .filter((c) => c.count > 1);
232
+ const brief = deriveBrief(s, {
233
+ byId,
234
+ cooccurring: co,
235
+ memories,
236
+ wanted: s.kind === 'title' ? wantedByNorm.get(s.canonical) || null : null,
237
+ now,
238
+ });
239
+ if (brief) out.push(brief);
240
+ }
241
+ return out;
242
+ }
243
+
244
+ /**
245
+ * Which refs no longer resolve to what they cited.
246
+ *
247
+ * This is where `ref.js`'s `drifted` finally earns its existence: a claim saying "cutover
248
+ * moved to Q4, see meeting m_8812" is worthless if m_8812 has since been edited into
249
+ * something else, and a maintenance pass that cannot tell is a maintenance pass that lies.
250
+ */
251
+ export function driftedRefs(brief, records = []) {
252
+ const hashes = new Map();
253
+ for (const r of normalizeRecords(records)) {
254
+ const [kind, ...rest] = r.id.split(':');
255
+ hashes.set(`${kind}:${rest.join(':') || r.id}`, contentHash(r.text));
256
+ }
257
+ const out = [];
258
+ for (const c of brief?.claims || []) {
259
+ for (const ref of c.refs || []) {
260
+ if (ref.kind === 'memory') continue; // memories are not records; they drift by edit, not by hash
261
+ const now = hashes.get(`${ref.kind}:${ref.id}`);
262
+ if (now === undefined) out.push({ claim: c.id, ref, resolution: 'verified-but-unavailable' });
263
+ else if (now !== ref.hash) out.push({ claim: c.id, ref, resolution: 'drifted' });
264
+ }
265
+ }
266
+ return out;
267
+ }
package/knowledge.js ADDED
@@ -0,0 +1,221 @@
1
+ // BRIEFS — the derived layer. A statement about a SUBJECT that accumulates across records.
2
+ //
3
+ // Everything else ChatPanel stores is a record of an event: a chat happened, a call
4
+ // happened, a human wrote a note. Nothing is a synthesis, so every answer is re-derived
5
+ // from scratch, every session, forever — and a multi-agent run's findings die with the run.
6
+ // A brief is the thing that compounds.
7
+ //
8
+ // FOUR INVARIANTS, and they are the whole defence (see docs/knowledge-compounding.md §5.1):
9
+ //
10
+ // I-K1 Every claim cites raw. A claim with no ref is a BUG, not a weak claim — it is
11
+ // refused on write. Derived text can then always be checked against, or rebuilt
12
+ // from, the immutable records under it.
13
+ // I-K2 A brief is REBUILDABLE. Delete every brief and this module reconstructs them from
14
+ // the record store. That makes a brief a projection — the same guarantee replay()
15
+ // gives the event log — and it makes "rebuild all" a cache clear, not data loss.
16
+ // I-K3 Nothing self-promotes. `draft` is free; `promoted` needs a gate. Class-R
17
+ // derivations may auto-promote (a backlink is not an opinion); model-written prose
18
+ // may not. Unreviewed agent writing compounding into confident nonsense is the
19
+ // failure mode that is undetectable six months later.
20
+ // I-K4 Bounded, or it is a second corpus. A brief has a size ceiling and the SET of
21
+ // briefs has a count ceiling driven by evidence — memory.js already made this
22
+ // argument for memory, and the same sentence applies here.
23
+ //
24
+ // This phase (W1) is entirely class R: no model, no network, no clock of its own. Every
25
+ // claim is something the corpus already states — who was present, when, what co-occurs,
26
+ // what the user themselves told us. Prose synthesis arrives in W3, behind the gate, and
27
+ // lands as `proposed` beside these rather than replacing them.
28
+ //
29
+ // WHY THIS FILE IS THE MODEL AND `knowledge-derive.js` IS THE PASS. Reading a brief and
30
+ // BUILDING one have very different costs: reading needs the shape and the renderer, while
31
+ // building walks the whole corpus and needs entity resolution and the maintenance passes.
32
+ // The MV3 service worker only ever reads — it syncs stored briefs to the gateway — so
33
+ // putting both halves in one module would have put 60 KB of derivation on its cold start
34
+ // for code it never runs. The split is what keeps that honest rather than remembered.
35
+
36
+ // From subject-name.js, not entity.js: this module is on the MV3 service worker's graph and
37
+ // needs exactly one string function, where entity.js also carries alias resolution and merge
38
+ // suggestion. Same argument as the knowledge/knowledge-derive split, one level down.
39
+ import { normalizeSubject } from './subject-name.js';
40
+
41
+ /** draft → proposed → promoted → archived. `promotion.js` (W3) owns the transitions. */
42
+ export const BRIEF_STATES = Object.freeze(['draft', 'proposed', 'promoted', 'archived']);
43
+
44
+ /** What a claim is derived FROM. Class R throughout this phase. */
45
+ export const CLAIM_KINDS = Object.freeze(['presence', 'timeline', 'together', 'wanted', 'stated']);
46
+
47
+ // I-K4, made concrete. A brief that grows without bound is a document, and a document
48
+ // needs its own summary, and then nothing has been gained.
49
+ export const MAX_CLAIMS = 12;
50
+ export const MAX_CLAIM_REFS = 8;
51
+ export const MAX_BRIEF_RECORDS = 200;
52
+ export const MAX_BRIEF_CHARS = 4000;
53
+
54
+ // How many co-occurring subjects a `together` claim names. Past a handful it stops being a
55
+ // statement and becomes a tag cloud.
56
+ const TOGETHER_LIMIT = 5;
57
+
58
+ /**
59
+ * A stable, non-cryptographic content hash, computed SYNCHRONOUSLY.
60
+ *
61
+ * Deliberately not SHA-256, which `store.js` uses and which is async: derivation walks
62
+ * every record in the corpus and runs in an MV3 service worker, so a hash per record has to
63
+ * be synchronous or the whole pass becomes a promise storm. The job here is DRIFT
64
+ * DETECTION — "has the record this claim cites changed since the claim was made" — not
65
+ * tamper resistance, and FNV-1a answers that exactly as well while staying pure.
66
+ */
67
+ export function contentHash(text) {
68
+ const s = String(text ?? '');
69
+ let h1 = 0x811c9dc5, h2 = 0x01000193;
70
+ for (let i = 0; i < s.length; i += 1) {
71
+ const c = s.charCodeAt(i);
72
+ h1 = Math.imul(h1 ^ c, 0x01000193) >>> 0;
73
+ h2 = Math.imul(h2 ^ (c + i), 0x85ebca6b) >>> 0;
74
+ }
75
+ return `f${h1.toString(16).padStart(8, '0')}${h2.toString(16).padStart(8, '0')}`;
76
+ }
77
+
78
+ /** `person:alex rivera` → `brief:person-alex-rivera`. Safe as a storage key and a URL hash. */
79
+ export function briefId(subjectKey) {
80
+ const [kind, ...rest] = String(subjectKey || '').split(':');
81
+ const slug = normalizeSubject(rest.join(':')).replace(/\s+/g, '-').replace(/-+/g, '-');
82
+ if (!kind || !slug) return '';
83
+ // The slug is TRUNCATED, so two long subjects can share one, and it is lossy, so two
84
+ // kinds can too. A hash of the CANONICAL key rides along to separate them. Canonical, not
85
+ // raw: "Alex Rivera" and "alex rivera" are one subject and must land on one id, or a
86
+ // rebuild would fork the page in two — the exact failure I-K2 exists to make impossible.
87
+ const canonical = `${kind}:${normalizeSubject(rest.join(':'))}`;
88
+ return `brief:${kind}-${slug.slice(0, 48)}-${contentHash(canonical).slice(1, 7)}`;
89
+ }
90
+
91
+ /** Records are addressed `chat:x` / `meeting:y` / `note:z` — the ref kind is the prefix. */
92
+ function refForRecord(rec) {
93
+ const [kind, ...rest] = String(rec.id).split(':');
94
+ const id = rest.join(':') || rec.id;
95
+ const known = kind === 'chat' || kind === 'meeting' || kind === 'note' || kind === 'page';
96
+ return makeRef({ kind: known ? kind : 'result', id: known ? id : rec.id, hash: contentHash(rec.text) });
97
+ }
98
+
99
+ function claim({ id, kind, text, refs, at = 0, confidence = 1 }) {
100
+ return {
101
+ id,
102
+ kind,
103
+ text,
104
+ // I-K1 lives here: a claim is CONSTRUCTED with its refs, and `checkKnowledgeInvariants`
105
+ // refuses one that arrives without them. There is no path that writes a claim first and
106
+ // attaches provenance later, because that path is how provenance goes missing.
107
+ refs: refs.slice(0, MAX_CLAIM_REFS),
108
+ firstSeen: at,
109
+ lastConfirmed: at,
110
+ confidence,
111
+ cls: 'R', // class R — derived, not written. W3's prose claims carry 'C'.
112
+ };
113
+ }
114
+
115
+ const plural = (n, one, many = `${one}s`) => `${n} ${n === 1 ? one : many}`;
116
+ const isoDay = (ms) => (ms ? new Date(ms).toISOString().slice(0, 10) : '');
117
+
118
+ /**
119
+ * The searchable body. A brief is a SOURCE, ranked by the same engine as everything else
120
+ * (design §7.6 — no second retrieval stack), so it has to render to text like one.
121
+ */
122
+ export function briefToText(brief) {
123
+ if (!brief) return '';
124
+ const L = [`BRIEF: ${brief.subject.name}`];
125
+ if (brief.subject.aliases?.length) L.push(`Also known as: ${brief.subject.aliases.join(', ')}`);
126
+ L.push(`Kind: ${brief.kind}`);
127
+ L.push('');
128
+ for (const c of brief.claims) {
129
+ L.push(`- ${c.supersededBy ? '[superseded] ' : ''}${c.text}`);
130
+ if (c.refs.length) L.push(` (${c.refs.map((r) => `${r.kind}:${r.id}`).join(', ')})`);
131
+ }
132
+ if (brief.records.length) {
133
+ L.push('', 'RECORDS:');
134
+ for (const r of brief.records.slice(-40).reverse()) L.push(`- ${r.type}: ${r.title || 'untitled'}`);
135
+ }
136
+ return L.join('\n').slice(0, MAX_BRIEF_CHARS);
137
+ }
138
+
139
+ /**
140
+ * The inverse of `briefToText` — a brief's claims and refs read back out of the text form.
141
+ *
142
+ * Exists because the warm store holds RECORDS: `{ id, title, type, date, text }`, nothing
143
+ * else. Briefs cross to the gateway as that shape, so an agent asking `get_brief` over MCP
144
+ * can only be handed structure if the text form is stable enough to parse. It is: this
145
+ * module writes both ends, and the claim line (`- text`) followed by its refs
146
+ * (` (kind:id, kind:id)`) is a grammar, not a rendering. Round-trips in the tests.
147
+ *
148
+ * Returns `null` for text that is not a brief, so a caller can tell "not a brief" from
149
+ * "a brief with no claims".
150
+ */
151
+ export function parseBriefText(text) {
152
+ const lines = String(text ?? '').split('\n');
153
+ if (!/^BRIEF: /.test(lines[0] || '')) return null;
154
+ const out = { name: lines[0].slice('BRIEF: '.length).trim(), aliases: [], kind: '', claims: [], records: [] };
155
+ let section = 'head';
156
+ for (let i = 1; i < lines.length; i += 1) {
157
+ const line = lines[i];
158
+ if (section === 'head') {
159
+ if (line.startsWith('Also known as: ')) out.aliases = line.slice(15).split(',').map((a) => a.trim()).filter(Boolean);
160
+ else if (line.startsWith('Kind: ')) out.kind = line.slice(6).trim();
161
+ else if (line === '') section = 'claims';
162
+ continue;
163
+ }
164
+ if (line === 'RECORDS:') { section = 'records'; continue; }
165
+ if (section === 'claims' && line.startsWith('- ')) {
166
+ const claim = { text: line.slice(2), refs: [] };
167
+ const next = lines[i + 1] || '';
168
+ const m = /^ \((.*)\)$/.exec(next);
169
+ if (m) {
170
+ claim.refs = m[1].split(', ').map((r) => {
171
+ const idx = r.indexOf(':');
172
+ return idx > 0 ? { kind: r.slice(0, idx), id: r.slice(idx + 1) } : null;
173
+ }).filter(Boolean);
174
+ i += 1;
175
+ }
176
+ out.claims.push(claim);
177
+ } else if (section === 'records' && line.startsWith('- ')) {
178
+ const idx = line.indexOf(': ');
179
+ out.records.push(idx > 0 ? { type: line.slice(2, idx), title: line.slice(idx + 2) } : { type: '', title: line.slice(2) });
180
+ }
181
+ }
182
+ return out;
183
+ }
184
+
185
+ /** The other briefs this brief's accepted claims link to — the backlink graph's edges. */
186
+ export function briefLinks(brief) {
187
+ const out = new Set();
188
+ for (const c of brief?.claims || []) for (const r of c.refs || []) if (r.kind === 'brief' && r.id) out.add(r.id);
189
+ return [...out];
190
+ }
191
+
192
+ /** Terms the graph and the search index rank a brief by — its subject and its neighbours. */
193
+ export function briefTerms(brief) {
194
+ if (!brief) return [];
195
+ const together = brief.claims.find((c) => c.kind === 'together');
196
+ const names = together ? together.text.replace(/^Usually alongside /, '').replace(/\.$/, '').split(', ') : [];
197
+ return [...new Set([brief.subject.name, ...(brief.subject.aliases || []), ...names])].filter(Boolean);
198
+ }
199
+
200
+ /**
201
+ * The invariants, as a check rather than a promise. Returns the failures; empty means clean.
202
+ * Same shape as `invariants.js checkInvariants()`, for the same reason: an invariant nobody
203
+ * can run is a comment.
204
+ */
205
+ export function checkKnowledgeInvariants(brief) {
206
+ const fail = [];
207
+ if (!brief || typeof brief !== 'object') return [{ invariant: 'I-K1', detail: 'not a brief' }];
208
+ if (!BRIEF_STATES.includes(brief.state)) fail.push({ invariant: 'I-K3', detail: `unknown state ${brief.state}` });
209
+ if (brief.state === 'promoted' && brief.cls === 'C') {
210
+ fail.push({ invariant: 'I-K3', detail: 'model-written prose cannot be promoted without the gate' });
211
+ }
212
+ for (const c of brief.claims || []) {
213
+ if (!c.refs?.length) fail.push({ invariant: 'I-K1', detail: `claim ${c.id} cites nothing` });
214
+ if (!CLAIM_KINDS.includes(c.kind) && c.cls !== 'C') {
215
+ fail.push({ invariant: 'I-K1', detail: `claim ${c.id} has unknown kind ${c.kind}` });
216
+ }
217
+ }
218
+ if ((brief.claims || []).length > MAX_CLAIMS) fail.push({ invariant: 'I-K4', detail: `${brief.claims.length} claims exceeds ${MAX_CLAIMS}` });
219
+ if (briefToText(brief).length >= MAX_BRIEF_CHARS) fail.push({ invariant: 'I-K4', detail: 'brief text is at the ceiling' });
220
+ return fail;
221
+ }