@unson/brainbase-mcp 0.50.12 → 0.50.13
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,63 @@
|
|
|
1
|
+
export type JudgmentFrameEvidenceKind = 'philosophy' | 'objective' | 'model';
|
|
2
|
+
export type JudgmentFrameEvidenceRole = 'constraint' | 'criterion' | 'prediction';
|
|
3
|
+
export interface JudgmentFrameReadEvidence {
|
|
4
|
+
catalog_digest: string;
|
|
5
|
+
records: Array<{
|
|
6
|
+
kind: JudgmentFrameEvidenceKind;
|
|
7
|
+
ref: string;
|
|
8
|
+
body_digest: string;
|
|
9
|
+
version: string;
|
|
10
|
+
version_source: string;
|
|
11
|
+
applicability_scope_source: string;
|
|
12
|
+
}>;
|
|
13
|
+
}
|
|
14
|
+
export interface JudgmentFrameApplicationEvidence {
|
|
15
|
+
selections: Array<{
|
|
16
|
+
kind: unknown;
|
|
17
|
+
ref: unknown;
|
|
18
|
+
}>;
|
|
19
|
+
chosen_option_digest: string | null;
|
|
20
|
+
applications: Array<{
|
|
21
|
+
kind: JudgmentFrameEvidenceKind;
|
|
22
|
+
ref: string;
|
|
23
|
+
role: JudgmentFrameEvidenceRole;
|
|
24
|
+
note_digest: string;
|
|
25
|
+
verdict?: string;
|
|
26
|
+
}>;
|
|
27
|
+
non_applicability: Array<{
|
|
28
|
+
kind: JudgmentFrameEvidenceKind;
|
|
29
|
+
reason_digest: string;
|
|
30
|
+
}>;
|
|
31
|
+
application_complete: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** The fields of a Host event this module reads. Other fields are ignored. */
|
|
34
|
+
export interface JudgmentFrameEvidenceEvent {
|
|
35
|
+
event_kind?: unknown;
|
|
36
|
+
success?: unknown;
|
|
37
|
+
safe_metadata?: unknown;
|
|
38
|
+
}
|
|
39
|
+
/** Digest-only evidence of a frame_read result, or null when the result does not answer the request. */
|
|
40
|
+
export declare function frameReadMetadata(value: unknown, input: unknown): JudgmentFrameReadEvidence | null;
|
|
41
|
+
/** Digest-only evidence of a frame_record call: which references the adopted option used, and how. */
|
|
42
|
+
export declare function frameApplicationMetadata(frame: unknown, input: unknown): JudgmentFrameApplicationEvidence;
|
|
43
|
+
/**
|
|
44
|
+
* Whether this turn's events hold a complete frame: a catalog, a body read of
|
|
45
|
+
* every selected reference, then a record that applies them, in that order.
|
|
46
|
+
*
|
|
47
|
+
* The catalog is tens of thousands of characters and rarely changes inside one
|
|
48
|
+
* conversation. frame_read and frame_record rebuild the current catalog on the
|
|
49
|
+
* server and reject a stale digest, so a successful read and record with a
|
|
50
|
+
* digest this conversation has already been shown prove the same list is still
|
|
51
|
+
* current. `sessionCatalogDigests` names those digests; with them, a turn that
|
|
52
|
+
* did not read the catalog again is still complete.
|
|
53
|
+
*/
|
|
54
|
+
export declare function hasCompleteJudgmentFrame(events: readonly JudgmentFrameEvidenceEvent[], { sessionCatalogDigests }?: {
|
|
55
|
+
sessionCatalogDigests?: readonly string[];
|
|
56
|
+
}): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Digests of the catalogs the other turns of one conversation read
|
|
59
|
+
* successfully. The Host passes the directory that holds one `<turn>.events`
|
|
60
|
+
* directory per turn and the current turn's own events directory, which is
|
|
61
|
+
* skipped. An unreadable file or directory contributes nothing.
|
|
62
|
+
*/
|
|
63
|
+
export declare function sessionFrameCatalogDigests(sessionDirectory: string, currentEventsDirectory: string): string[];
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Frame evidence a Host keeps for one turn. It persists references and
|
|
2
|
+
// digests, never Graph bodies, option labels, or the reasons the model wrote.
|
|
3
|
+
import { createHash } from 'node:crypto';
|
|
4
|
+
import { readdirSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { join, resolve } from 'node:path';
|
|
6
|
+
const roles = {
|
|
7
|
+
philosophy: 'constraint', objective: 'criterion', model: 'prediction',
|
|
8
|
+
};
|
|
9
|
+
const kinds = Object.keys(roles);
|
|
10
|
+
const verdicts = ['satisfied', 'violated', 'tension'];
|
|
11
|
+
const text = (value) => typeof value === 'string' && value.trim().length > 0;
|
|
12
|
+
const digest = (value) => `sha256:${createHash('sha256').update(value).digest('hex')}`;
|
|
13
|
+
const validDigest = (value) => /^sha256:[a-f0-9]{64}$/u.test(String(value));
|
|
14
|
+
const key = (item) => `${item?.kind}:${item?.ref}`;
|
|
15
|
+
const lastIndexOf = (events, kind) => {
|
|
16
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
17
|
+
if (events[index]?.success && events[index].event_kind === kind)
|
|
18
|
+
return index;
|
|
19
|
+
}
|
|
20
|
+
return -1;
|
|
21
|
+
};
|
|
22
|
+
/** Digest-only evidence of a frame_read result, or null when the result does not answer the request. */
|
|
23
|
+
export function frameReadMetadata(value, input) {
|
|
24
|
+
const result = value;
|
|
25
|
+
const request = input;
|
|
26
|
+
if (result?.schema_version !== 'brainbase-judgment-frame-read-v1'
|
|
27
|
+
|| !validDigest(result.catalog_digest) || result.catalog_digest !== request?.catalog_digest
|
|
28
|
+
|| !Array.isArray(result.records) || !Array.isArray(request?.selections)
|
|
29
|
+
|| result.records.length === 0 || result.records.length !== request.selections.length
|
|
30
|
+
|| result.records.length > 24)
|
|
31
|
+
return null;
|
|
32
|
+
const requested = new Set(request.selections.map(key));
|
|
33
|
+
if (requested.size !== request.selections.length)
|
|
34
|
+
return null;
|
|
35
|
+
const records = [];
|
|
36
|
+
for (const item of result.records) {
|
|
37
|
+
if (!Object.hasOwn(roles, item?.kind) || !text(item.ref) || !requested.delete(key(item))
|
|
38
|
+
|| !text(item.body) || item.body_digest !== digest(item.body)
|
|
39
|
+
|| !text(item.version) || !text(item.applicability_scope)
|
|
40
|
+
|| !['graph', 'body_digest'].includes(item.version_source)
|
|
41
|
+
|| !['graph', 'unspecified'].includes(item.applicability_scope_source)
|
|
42
|
+
|| (item.version_source === 'body_digest' && item.version !== item.body_digest))
|
|
43
|
+
return null;
|
|
44
|
+
records.push({
|
|
45
|
+
kind: item.kind, ref: item.ref, body_digest: item.body_digest,
|
|
46
|
+
version: item.version, version_source: item.version_source,
|
|
47
|
+
applicability_scope_source: item.applicability_scope_source,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return { catalog_digest: result.catalog_digest, records };
|
|
51
|
+
}
|
|
52
|
+
/** Digest-only evidence of a frame_record call: which references the adopted option used, and how. */
|
|
53
|
+
export function frameApplicationMetadata(frame, input) {
|
|
54
|
+
const recorded = frame;
|
|
55
|
+
const request = input;
|
|
56
|
+
const selections = Array.isArray(request?.selections) ? request.selections : [];
|
|
57
|
+
const chosen = Array.isArray(request?.options)
|
|
58
|
+
? request.options.find((option) => option?.label === request.chosen_option) : null;
|
|
59
|
+
const returned = new Set((Array.isArray(recorded?.selections) ? recorded.selections : []).map(key));
|
|
60
|
+
const selected = new Set(selections.map(key));
|
|
61
|
+
const validSelections = Array.isArray(request?.selections) && selections.length <= 24
|
|
62
|
+
&& selected.size === selections.length && returned.size === selections.length
|
|
63
|
+
&& selections.every((item) => Object.hasOwn(roles, item?.kind) && text(item.ref)
|
|
64
|
+
&& text(item.why) && returned.has(key(item)));
|
|
65
|
+
const kindsAddressed = kinds.every((kind) => selections.some((item) => item?.kind === kind)
|
|
66
|
+
? !text(request?.none?.[kind]) : text(request?.none?.[kind]));
|
|
67
|
+
const usedBy = (item) => (use) => use?.ref === item?.ref
|
|
68
|
+
&& use?.role === roles[item?.kind] && text(use?.note)
|
|
69
|
+
&& (item.kind !== 'philosophy' || verdicts.includes(use.verdict));
|
|
70
|
+
const applied = validSelections && Array.isArray(chosen?.uses)
|
|
71
|
+
&& selections.every((item) => chosen.uses.some(usedBy(item)));
|
|
72
|
+
return {
|
|
73
|
+
selections: selections.map((item) => ({ kind: item?.kind, ref: item?.ref })),
|
|
74
|
+
chosen_option_digest: text(request?.chosen_option) ? digest(request.chosen_option) : null,
|
|
75
|
+
applications: validSelections && applied ? selections.map((item) => {
|
|
76
|
+
const use = chosen.uses.find(usedBy(item));
|
|
77
|
+
return {
|
|
78
|
+
kind: item.kind, ref: item.ref, role: use.role, note_digest: digest(use.note),
|
|
79
|
+
...(item.kind === 'philosophy' ? { verdict: use.verdict } : {}),
|
|
80
|
+
};
|
|
81
|
+
}) : [],
|
|
82
|
+
non_applicability: kinds.filter((kind) => !selections.some((item) => item?.kind === kind)
|
|
83
|
+
&& text(request?.none?.[kind])).map((kind) => ({ kind, reason_digest: digest(request.none[kind]) })),
|
|
84
|
+
application_complete: Boolean(validSelections && kindsAddressed && applied
|
|
85
|
+
&& text(request?.chosen_option) && recorded?.chosen_option === request.chosen_option),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Whether this turn's events hold a complete frame: a catalog, a body read of
|
|
90
|
+
* every selected reference, then a record that applies them, in that order.
|
|
91
|
+
*
|
|
92
|
+
* The catalog is tens of thousands of characters and rarely changes inside one
|
|
93
|
+
* conversation. frame_read and frame_record rebuild the current catalog on the
|
|
94
|
+
* server and reject a stale digest, so a successful read and record with a
|
|
95
|
+
* digest this conversation has already been shown prove the same list is still
|
|
96
|
+
* current. `sessionCatalogDigests` names those digests; with them, a turn that
|
|
97
|
+
* did not read the catalog again is still complete.
|
|
98
|
+
*/
|
|
99
|
+
export function hasCompleteJudgmentFrame(events, { sessionCatalogDigests = [] } = {}) {
|
|
100
|
+
const list = events;
|
|
101
|
+
const catalogIndex = lastIndexOf(list, 'frame_catalog');
|
|
102
|
+
const frameIndex = lastIndexOf(list, 'frame');
|
|
103
|
+
if (frameIndex < 0 || frameIndex <= catalogIndex)
|
|
104
|
+
return false;
|
|
105
|
+
const frame = list[frameIndex].safe_metadata?.judgment_frame;
|
|
106
|
+
if (!validDigest(frame?.catalog_digest) || frame.application_complete !== true)
|
|
107
|
+
return false;
|
|
108
|
+
// A catalog read on this turn is the one the record must match. Without
|
|
109
|
+
// one, only a digest read earlier in the same conversation is accepted.
|
|
110
|
+
const catalogDigest = catalogIndex >= 0
|
|
111
|
+
? list[catalogIndex].safe_metadata?.judgment_frame_catalog?.catalog_digest
|
|
112
|
+
: sessionCatalogDigests.includes(frame.catalog_digest) ? frame.catalog_digest : null;
|
|
113
|
+
if (!validDigest(catalogDigest) || frame.catalog_digest !== catalogDigest)
|
|
114
|
+
return false;
|
|
115
|
+
const reads = new Set(list.slice(catalogIndex + 1, frameIndex)
|
|
116
|
+
.filter((event) => event.success && event.event_kind === 'frame_read'
|
|
117
|
+
&& event.safe_metadata?.judgment_frame_read?.catalog_digest === catalogDigest)
|
|
118
|
+
.flatMap((event) => Array.isArray(event.safe_metadata.judgment_frame_read?.records)
|
|
119
|
+
? event.safe_metadata.judgment_frame_read.records.map(key) : []));
|
|
120
|
+
return Array.isArray(frame.selections) && frame.selections.every((item) => reads.has(key(item)));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Digests of the catalogs the other turns of one conversation read
|
|
124
|
+
* successfully. The Host passes the directory that holds one `<turn>.events`
|
|
125
|
+
* directory per turn and the current turn's own events directory, which is
|
|
126
|
+
* skipped. An unreadable file or directory contributes nothing.
|
|
127
|
+
*/
|
|
128
|
+
export function sessionFrameCatalogDigests(sessionDirectory, currentEventsDirectory) {
|
|
129
|
+
const digests = new Set();
|
|
130
|
+
let turns;
|
|
131
|
+
try {
|
|
132
|
+
turns = readdirSync(sessionDirectory).filter((name) => name.endsWith('.events')).sort();
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
for (const turn of turns) {
|
|
138
|
+
const directory = join(sessionDirectory, turn);
|
|
139
|
+
if (resolve(directory) === resolve(currentEventsDirectory))
|
|
140
|
+
continue;
|
|
141
|
+
let names;
|
|
142
|
+
try {
|
|
143
|
+
names = readdirSync(directory).filter((name) => name.endsWith('.json'));
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
for (const name of names) {
|
|
149
|
+
try {
|
|
150
|
+
const event = JSON.parse(readFileSync(join(directory, name), 'utf8'));
|
|
151
|
+
const value = event?.safe_metadata?.judgment_frame_catalog?.catalog_digest;
|
|
152
|
+
if (event?.success === true && event.event_kind === 'frame_catalog' && validDigest(value))
|
|
153
|
+
digests.add(value);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
// Not a readable event.
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return [...digests].sort();
|
|
161
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unson/brainbase-mcp",
|
|
3
|
-
"version": "0.50.
|
|
3
|
+
"version": "0.50.13",
|
|
4
4
|
"description": "Local-first Brainbase judgment OS that lets supported AI agents reuse a user's approved goals, priorities, decisions, and judgment criteria.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,6 +67,10 @@
|
|
|
67
67
|
"types": "./dist/judgment-node-evidence.d.ts",
|
|
68
68
|
"import": "./dist/judgment-node-evidence.js"
|
|
69
69
|
},
|
|
70
|
+
"./judgment-frame-evidence": {
|
|
71
|
+
"types": "./dist/judgment-frame-evidence.d.ts",
|
|
72
|
+
"import": "./dist/judgment-frame-evidence.js"
|
|
73
|
+
},
|
|
70
74
|
"./judgment-event-adapter": {
|
|
71
75
|
"types": "./dist/judgment-event-adapter.d.ts",
|
|
72
76
|
"import": "./dist/judgment-event-adapter.js"
|
|
@@ -645,5 +649,5 @@
|
|
|
645
649
|
"url": "https://github.com/Unson-LLC/brainbase/issues"
|
|
646
650
|
},
|
|
647
651
|
"homepage": "https://github.com/Unson-LLC/brainbase#readme",
|
|
648
|
-
"gitHead": "
|
|
652
|
+
"gitHead": "6a83008e1e11d2393b708f66f207755241da8956"
|
|
649
653
|
}
|