@hmharness/cognitive 0.19.3 → 0.19.4
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/explore-runner.d.ts +37 -0
- package/dist/explore-runner.js +89 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hmharness/cognitive - exploration runner (blueprint M4 / EXP-001..009)
|
|
3
|
+
*
|
|
4
|
+
* The live entry point for the Exploration Engine: run a budgeted
|
|
5
|
+
* uncertainty-seeking session against a real environment, seeded from the
|
|
6
|
+
* world model built so far, and land the outcome as a cognitive trajectory
|
|
7
|
+
* + episodic index entry — exploration feeds the same flywheel as tasks do.
|
|
8
|
+
*
|
|
9
|
+
* Loop per action: policy selects (uncertainty x info-gain x goal-relevance
|
|
10
|
+
* - risk, under budget) -> env.act -> world-model predict-then-update ->
|
|
11
|
+
* trajectory step. Unknown affordances get probed first (that is the point).
|
|
12
|
+
*/
|
|
13
|
+
import type { Environment } from './index.js';
|
|
14
|
+
import { type ExplorationResult } from './exploration.js';
|
|
15
|
+
export interface ExploreOptions {
|
|
16
|
+
environmentId?: string;
|
|
17
|
+
maxActions?: number;
|
|
18
|
+
maxCost?: number;
|
|
19
|
+
riskTolerance?: number;
|
|
20
|
+
goalKeywords?: string[];
|
|
21
|
+
/** the concrete environment to explore (host wires the adapter) */
|
|
22
|
+
env?: Environment;
|
|
23
|
+
}
|
|
24
|
+
export interface ExploreSummary {
|
|
25
|
+
result: ExplorationResult;
|
|
26
|
+
trajectoryId: string;
|
|
27
|
+
hypotheses: Array<{
|
|
28
|
+
claim: string;
|
|
29
|
+
status: string;
|
|
30
|
+
}>;
|
|
31
|
+
worldModelBeliefs: Array<{
|
|
32
|
+
actionType: string;
|
|
33
|
+
confidence: number;
|
|
34
|
+
evidenceCount: number;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
export declare function runExploration(home: string, opts?: ExploreOptions): Promise<ExploreSummary>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { TrajectoryStore, TrajectoryRecorder, MemoryEnvironment } from "./index.js";
|
|
2
|
+
import { ExplorationEngine, UcbExplorationPolicy, HypothesisRegistry } from "./exploration.js";
|
|
3
|
+
import { loadTrajectories, replayIntoWorldModel } from "./analysis.js";
|
|
4
|
+
import { CognitiveMemory } from "./memory.js";
|
|
5
|
+
export async function runExploration(home, opts = {}) {
|
|
6
|
+
const environmentId = opts.environmentId ?? 'terminal';
|
|
7
|
+
const maxActions = opts.maxActions ?? 6;
|
|
8
|
+
const maxCost = opts.maxCost ?? 20;
|
|
9
|
+
const riskTolerance = opts.riskTolerance ?? 0.6;
|
|
10
|
+
let env = opts.env;
|
|
11
|
+
if (!env) {
|
|
12
|
+
if (environmentId === 'memory') {
|
|
13
|
+
env = new MemoryEnvironment('memory');
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
// layering: cognitive must NOT import @hmharness/environments (it
|
|
17
|
+
// depends on us). The CLI/host wires the concrete environment.
|
|
18
|
+
throw new Error(`environment '${environmentId}' requires opts.env — the host wires the concrete adapter (layering rule: cognitive never imports environments)`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// seed the world model from history so exploration PROBES what is unknown
|
|
22
|
+
const history = await loadTrajectories(home);
|
|
23
|
+
const wm = replayIntoWorldModel(history, environmentId === 'memory' ? 'memory' : environmentId);
|
|
24
|
+
const hypotheses = new HypothesisRegistry();
|
|
25
|
+
hypotheses.register({
|
|
26
|
+
claim: 'unseen affordances can be exercised successfully',
|
|
27
|
+
expectedEvidence: 'probing an unknown action succeeds',
|
|
28
|
+
refutingEvidence: 'probing unknown actions fails or is denied',
|
|
29
|
+
});
|
|
30
|
+
const rec = new TrajectoryRecorder(`trj-explore-${Date.now().toString(36)}`, `explore-${environmentId}`, { id: env.id, version: env.version }, { id: 'goal-explore', description: 'reduce uncertainty about the environment affordances' });
|
|
31
|
+
const obs = await env.reset({});
|
|
32
|
+
const engine = new ExplorationEngine(new UcbExplorationPolicy());
|
|
33
|
+
const result = await engine.run({
|
|
34
|
+
maxActions,
|
|
35
|
+
maxCost,
|
|
36
|
+
riskTolerance,
|
|
37
|
+
ctx: { observation: obs, worldModel: wm, hypotheses, goalKeywords: opts.goalKeywords },
|
|
38
|
+
act: async (action) => {
|
|
39
|
+
const prediction = wm.predict({ action });
|
|
40
|
+
const started = Date.now();
|
|
41
|
+
const r = await env.act(action);
|
|
42
|
+
const after = await env.observe().catch(() => obs);
|
|
43
|
+
wm.update({
|
|
44
|
+
stateBefore: wm.worldState,
|
|
45
|
+
action,
|
|
46
|
+
observation: after,
|
|
47
|
+
outcome: r.outcome === 'success' ? 'success' : r.outcome === 'failure' ? 'failure' : 'unknown',
|
|
48
|
+
predictionId: prediction.id,
|
|
49
|
+
});
|
|
50
|
+
rec.record({
|
|
51
|
+
action,
|
|
52
|
+
outcome: r.outcome,
|
|
53
|
+
evidence: [`exploration:${action.type}`],
|
|
54
|
+
prediction: { claim: prediction.claim, confidence: prediction.confidence },
|
|
55
|
+
durationMs: Date.now() - started,
|
|
56
|
+
});
|
|
57
|
+
return { outcome: r.outcome };
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
await env.close().catch(() => undefined);
|
|
61
|
+
const traj = rec.finish(result.actionsTaken > 0);
|
|
62
|
+
await store0(home, traj);
|
|
63
|
+
const beliefs = wm.worldState.beliefs
|
|
64
|
+
.filter((b) => b.id.startsWith('act:'))
|
|
65
|
+
.map((b) => ({ actionType: b.id.slice(4), confidence: b.confidence, evidenceCount: b.evidenceCount }));
|
|
66
|
+
return {
|
|
67
|
+
result,
|
|
68
|
+
trajectoryId: traj.id,
|
|
69
|
+
hypotheses: hypotheses.all().map((h) => ({ claim: h.claim, status: h.status })),
|
|
70
|
+
worldModelBeliefs: beliefs,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async function store0(home, traj) {
|
|
74
|
+
const store = new TrajectoryStore(home);
|
|
75
|
+
await store.append(traj);
|
|
76
|
+
const mem = new CognitiveMemory(home);
|
|
77
|
+
await mem.load();
|
|
78
|
+
await mem.write({
|
|
79
|
+
layer: 'episodic',
|
|
80
|
+
content: `EXPLORE ${traj.environment.id}: ${traj.metrics.actions} actions, ${traj.metrics.recoveryCount} recoveries`,
|
|
81
|
+
payload: { trajectoryId: traj.id },
|
|
82
|
+
source: 'exploration-run',
|
|
83
|
+
provenance: `trajectory:${traj.id}`,
|
|
84
|
+
confidence: 0.7,
|
|
85
|
+
environment: traj.environment.id,
|
|
86
|
+
session: traj.sessionId,
|
|
87
|
+
tags: ['explore', traj.environment.id],
|
|
88
|
+
}).catch(() => undefined);
|
|
89
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/cognitive",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.4",
|
|
4
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
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
"node": ">=22"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@hmharness/kernel": "0.19.
|
|
29
|
+
"@hmharness/kernel": "0.19.4"
|
|
30
30
|
}
|
|
31
31
|
}
|