@hmharness/cognitive 0.19.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/dist/benchmark.d.ts +80 -0
- package/dist/benchmark.js +85 -0
- package/dist/continual.d.ts +90 -0
- package/dist/continual.js +142 -0
- package/dist/evolution2.d.ts +93 -0
- package/dist/evolution2.js +181 -0
- package/dist/exploration.d.ts +106 -0
- package/dist/exploration.js +182 -0
- package/dist/goal.d.ts +97 -0
- package/dist/goal.js +133 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/memory.d.ts +64 -0
- package/dist/memory.js +200 -0
- package/dist/multi-agent.d.ts +118 -0
- package/dist/multi-agent.js +119 -0
- package/dist/protocol.d.ts +155 -0
- package/dist/protocol.js +103 -0
- package/dist/registry.d.ts +75 -0
- package/dist/registry.js +195 -0
- package/dist/rlm.d.ts +88 -0
- package/dist/rlm.js +139 -0
- package/dist/skill-compiler.d.ts +81 -0
- package/dist/skill-compiler.js +124 -0
- package/dist/status.d.ts +20 -0
- package/dist/status.js +76 -0
- package/dist/trajectory.d.ts +54 -0
- package/dist/trajectory.js +112 -0
- package/dist/world-model.d.ts +146 -0
- package/dist/world-model.js +219 -0
- package/package.json +31 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { stableHash } from "./protocol.js";
|
|
2
|
+
/** WM-002. Transition journal with retrieval by action type / outcome. */
|
|
3
|
+
export class TransitionStore {
|
|
4
|
+
items = [];
|
|
5
|
+
append(t) {
|
|
6
|
+
this.items.push(t);
|
|
7
|
+
if (this.items.length > 10_000)
|
|
8
|
+
this.items.splice(0, this.items.length - 10_000);
|
|
9
|
+
}
|
|
10
|
+
all() {
|
|
11
|
+
return this.items;
|
|
12
|
+
}
|
|
13
|
+
byActionType(type) {
|
|
14
|
+
return this.items.filter((t) => t.action.type === type);
|
|
15
|
+
}
|
|
16
|
+
byOutcome(outcome) {
|
|
17
|
+
return this.items.filter((t) => t.outcome === outcome);
|
|
18
|
+
}
|
|
19
|
+
/** rough episodic retrieval: most recent N transitions of same action type */
|
|
20
|
+
similar(actionType, n = 5) {
|
|
21
|
+
return this.items.filter((t) => t.action.type === actionType).slice(-n);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class WorldModel {
|
|
25
|
+
state;
|
|
26
|
+
transitions = new TransitionStore();
|
|
27
|
+
predictions = new Map();
|
|
28
|
+
seq = 0;
|
|
29
|
+
constructor(environmentId) {
|
|
30
|
+
this.state = {
|
|
31
|
+
environmentId,
|
|
32
|
+
version: 1,
|
|
33
|
+
entities: [],
|
|
34
|
+
variables: {},
|
|
35
|
+
beliefs: [],
|
|
36
|
+
uncertainty: { byAction: {} },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
get worldState() {
|
|
40
|
+
return JSON.parse(JSON.stringify(this.state));
|
|
41
|
+
}
|
|
42
|
+
/** WM-001/006. Apply a transition: update variables/entities, per-action
|
|
43
|
+
* success-rate beliefs, and resolve any prediction bound to the action. */
|
|
44
|
+
update(input) {
|
|
45
|
+
this.transitions.append(input);
|
|
46
|
+
const after = input.stateAfter;
|
|
47
|
+
if (after) {
|
|
48
|
+
this.state.entities = after.entities;
|
|
49
|
+
this.state.variables = after.variables;
|
|
50
|
+
}
|
|
51
|
+
// belief per action type: exponential moving success rate
|
|
52
|
+
const key = input.action.type;
|
|
53
|
+
const prior = this.state.beliefs.find((b) => b.id === `act:${key}`);
|
|
54
|
+
const rate = input.outcome === 'success' ? 1 : input.outcome === 'failure' ? 0 : 0.5;
|
|
55
|
+
if (!prior) {
|
|
56
|
+
this.state.beliefs.push({
|
|
57
|
+
id: `act:${key}`,
|
|
58
|
+
claim: `action ${key} succeeds ~${rate * 100}%`,
|
|
59
|
+
confidence: 0.3, // low until evidence accumulates
|
|
60
|
+
evidenceCount: 1,
|
|
61
|
+
lastConfirmedAt: new Date().toISOString(),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const n = prior.evidenceCount + 1;
|
|
66
|
+
const blended = (prior.confidence * (n - 1) + rate) / n;
|
|
67
|
+
prior.confidence = Number(blended.toFixed(4));
|
|
68
|
+
prior.evidenceCount = n;
|
|
69
|
+
prior.claim = `action ${key} succeeds ~${Math.round(blended * 100)}% (n=${n})`;
|
|
70
|
+
prior.lastConfirmedAt = new Date().toISOString();
|
|
71
|
+
}
|
|
72
|
+
this.state.uncertainty.byAction[key] = Number((1 - (this.state.beliefs.find((b) => b.id === `act:${key}`)?.confidence ?? 0)).toFixed(4));
|
|
73
|
+
this.state.version += 1;
|
|
74
|
+
// WM-005: resolve the prediction bound to this action, if any
|
|
75
|
+
if (input.predictionId) {
|
|
76
|
+
const p = this.predictions.get(input.predictionId);
|
|
77
|
+
if (p && !p.resolvedAt) {
|
|
78
|
+
p.resolvedAt = new Date().toISOString();
|
|
79
|
+
p.actual = input.outcome;
|
|
80
|
+
p.error = Number(Math.abs(p.confidence - (input.outcome === 'success' ? 1 : 0)).toFixed(4));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return this.worldState;
|
|
84
|
+
}
|
|
85
|
+
/** WM-003/004. Predict the outcome of an action from the belief table.
|
|
86
|
+
* The model is allowed to say "I don't know": unseen action types return
|
|
87
|
+
* confidence 0 and knownUnknown=true — planners MUST treat that as a
|
|
88
|
+
* signal to explore, not as 0% success. */
|
|
89
|
+
predict(query) {
|
|
90
|
+
const key = query.action.type;
|
|
91
|
+
const belief = this.state.beliefs.find((b) => b.id === `act:${key}`);
|
|
92
|
+
const id = `pred-${++this.seq}`;
|
|
93
|
+
const prediction = {
|
|
94
|
+
id,
|
|
95
|
+
claim: belief
|
|
96
|
+
? belief.claim
|
|
97
|
+
: `no evidence about action ${key} yet (unknown)`,
|
|
98
|
+
confidence: belief ? belief.confidence : 0,
|
|
99
|
+
actionType: key,
|
|
100
|
+
createdAt: new Date().toISOString(),
|
|
101
|
+
};
|
|
102
|
+
this.predictions.set(id, prediction);
|
|
103
|
+
return prediction;
|
|
104
|
+
}
|
|
105
|
+
/** planner gate: which action types are reliable enough to plan with */
|
|
106
|
+
plannerConfidence(threshold = 0.6) {
|
|
107
|
+
const trusted = [];
|
|
108
|
+
const untrusted = [];
|
|
109
|
+
const unknown = [];
|
|
110
|
+
for (const b of this.state.beliefs) {
|
|
111
|
+
if (!b.id.startsWith('act:'))
|
|
112
|
+
continue;
|
|
113
|
+
const type = b.id.slice(4);
|
|
114
|
+
if (b.evidenceCount < 2 || b.confidence < threshold)
|
|
115
|
+
untrusted.push(type);
|
|
116
|
+
else
|
|
117
|
+
trusted.push(type);
|
|
118
|
+
}
|
|
119
|
+
for (const t of this.transitions.all()) {
|
|
120
|
+
const seen = new Set([...trusted, ...untrusted]);
|
|
121
|
+
if (!seen.has(t.action.type) && !unknown.includes(t.action.type))
|
|
122
|
+
unknown.push(t.action.type);
|
|
123
|
+
}
|
|
124
|
+
return { trusted, untrusted, unknown };
|
|
125
|
+
}
|
|
126
|
+
explainPrediction(id) {
|
|
127
|
+
const p = this.predictions.get(id);
|
|
128
|
+
if (!p)
|
|
129
|
+
throw new Error(`unknown prediction ${id}`);
|
|
130
|
+
const supporting = this.transitions.byActionType(p.actionType).length;
|
|
131
|
+
const clusterKey = p.error !== undefined && p.error > 0.5 ? `${p.actionType}/systematic-miss` : `${p.actionType}/ok`;
|
|
132
|
+
return {
|
|
133
|
+
predictionId: p.id,
|
|
134
|
+
claim: p.claim,
|
|
135
|
+
confidence: p.confidence,
|
|
136
|
+
actual: p.actual,
|
|
137
|
+
error: p.error,
|
|
138
|
+
supportingTransitions: supporting,
|
|
139
|
+
cluster: clusterKey,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** WM-005. Cluster resolved-but-wrong predictions by action type. */
|
|
143
|
+
errorClusters() {
|
|
144
|
+
const byCluster = new Map();
|
|
145
|
+
for (const p of this.predictions.values()) {
|
|
146
|
+
if (!p.resolvedAt || p.error === undefined)
|
|
147
|
+
continue;
|
|
148
|
+
const bucket = p.actual === 'success' ? 'predicted-fail-but-succeeded' : 'predicted-success-but-failed';
|
|
149
|
+
const key = `${p.actionType}/${bucket}`;
|
|
150
|
+
const c = byCluster.get(key) ?? { misses: 0, total: 0, samples: [] };
|
|
151
|
+
c.total += 1;
|
|
152
|
+
if (p.error > 0.5) {
|
|
153
|
+
c.misses += 1;
|
|
154
|
+
c.samples.push(this.explainPrediction(p.id));
|
|
155
|
+
}
|
|
156
|
+
byCluster.set(key, c);
|
|
157
|
+
}
|
|
158
|
+
return [...byCluster.entries()].map(([cluster, c]) => ({
|
|
159
|
+
cluster,
|
|
160
|
+
misses: c.misses,
|
|
161
|
+
total: c.total,
|
|
162
|
+
sampleErrors: c.samples.slice(0, 5).map((s) => ({ claim: s.claim, actual: String(s.actual), confidence: s.confidence })),
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
/** WM-007/008. Turn error clusters into explicit correction rules.
|
|
166
|
+
* Beliefs are NOT silently rewritten — rules are attached as corrections
|
|
167
|
+
* so provenance survives (blueprint: 可解释修订). */
|
|
168
|
+
revise(evidence) {
|
|
169
|
+
const rulesAdded = [];
|
|
170
|
+
for (const ev of evidence) {
|
|
171
|
+
if (ev.misses < 2)
|
|
172
|
+
continue; // need repetition, not noise
|
|
173
|
+
const [actionType, bucket] = ev.cluster.split('/');
|
|
174
|
+
const rule = bucket === 'predicted-success-but-failed'
|
|
175
|
+
? `${actionType} fails more often than the success-rate belief implies; discount its confidence before planning`
|
|
176
|
+
: `${actionType} succeeds more often than believed; treat pessimism here as stale evidence`;
|
|
177
|
+
const pattern = ev.cluster;
|
|
178
|
+
const belief = this.state.beliefs.find((b) => b.id === `act:${actionType}`);
|
|
179
|
+
if (belief) {
|
|
180
|
+
belief.corrections = [...(belief.corrections ?? []), { pattern, rule, fromCluster: ev.cluster }];
|
|
181
|
+
}
|
|
182
|
+
rulesAdded.push({ pattern, rule });
|
|
183
|
+
}
|
|
184
|
+
this.state.version += 1;
|
|
185
|
+
return {
|
|
186
|
+
id: `rev-${stableHash(rulesAdded).slice(0, 8)}`,
|
|
187
|
+
createdAt: new Date().toISOString(),
|
|
188
|
+
clustersAddressed: evidence.map((e) => e.cluster),
|
|
189
|
+
rulesAdded,
|
|
190
|
+
beliefsAdjusted: rulesAdded.length,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
confidence(stateOrPredictionId) {
|
|
194
|
+
const p = this.predictions.get(stateOrPredictionId);
|
|
195
|
+
if (p)
|
|
196
|
+
return p.confidence;
|
|
197
|
+
const hash = stableHash(this.state);
|
|
198
|
+
if (stateOrPredictionId === hash) {
|
|
199
|
+
const beliefs = this.state.beliefs;
|
|
200
|
+
if (beliefs.length === 0)
|
|
201
|
+
return 0;
|
|
202
|
+
return Number((beliefs.reduce((s, b) => s + b.confidence, 0) / beliefs.length).toFixed(4));
|
|
203
|
+
}
|
|
204
|
+
throw new Error(`unknown id ${stateOrPredictionId}`);
|
|
205
|
+
}
|
|
206
|
+
/** rolling calibration for the dashboard (mean |error| over resolved) */
|
|
207
|
+
calibration() {
|
|
208
|
+
const resolved = [...this.predictions.values()].filter((p) => p.error !== undefined);
|
|
209
|
+
if (resolved.length === 0)
|
|
210
|
+
return { resolved: 0, meanError: undefined };
|
|
211
|
+
return {
|
|
212
|
+
resolved: resolved.length,
|
|
213
|
+
meanError: Number((resolved.reduce((s, p) => s + (p.error ?? 0), 0) / resolved.length).toFixed(4)),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
get transitionsStore() {
|
|
217
|
+
return this.transitions;
|
|
218
|
+
}
|
|
219
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmharness/cognitive",
|
|
3
|
+
"version": "0.19.0",
|
|
4
|
+
"description": "hmharness Cognitive OS core: Environment protocol, World Model, Goal system, Exploration, RLM runtime, five-layer memory, Skill Compiler, Continual Learning control plane, Evolution 2.0 governance, Multi-Agent topology, Transfer lab and GeneralBench schema. Cognitive strategy layer per the HMH Cognitive OS blueprint (2026-09).",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"types": "dist/index.d.ts",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc -p tsconfig.build.json"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/swsgbl/hmharness.git"
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=22"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@hmharness/kernel": "0.19.0"
|
|
30
|
+
}
|
|
31
|
+
}
|