@hmharness/cognitive 0.23.27 → 0.23.29

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.
@@ -25,8 +25,11 @@
25
25
  */
26
26
  import type { Candidate, EvalResult, LearningDataset, LearningPlan, LearningTarget } from './continual.js';
27
27
  import type { CognitiveLedger } from './ledger.js';
28
+ import { type WorkflowCandidate } from './skill-compiler.js';
28
29
  export type TargetTrainer = (plan: LearningPlan, dataset: LearningDataset) => Promise<Candidate>;
29
- export type TargetEvaluator = (candidate: Candidate, holdout: LearningDataset) => Promise<EvalResult>;
30
+ /** train rides along: deterministic-rebuild evaluators (world_model) need
31
+ * the exact training set; evaluators that don't care ignore the 3rd arg. */
32
+ export type TargetEvaluator = (candidate: Candidate, holdout: LearningDataset, train: LearningDataset) => Promise<EvalResult>;
30
33
  export type TargetRollback = (candidate: Candidate) => Promise<void>;
31
34
  export interface PromotionPolicy {
32
35
  /** minimum evidence metric before promotion is even considered */
@@ -75,3 +78,28 @@ export declare class LearningTargetRegistry {
75
78
  runCycle(plan: LearningPlan, train: LearningDataset): Promise<CycleOutcome>;
76
79
  private emit;
77
80
  }
81
+ export interface SkillCandidate extends Candidate {
82
+ workflows: WorkflowCandidate[];
83
+ }
84
+ export declare function registerSkillTarget(registry: LearningTargetRegistry, opts: {
85
+ /** disjoint-from-train holdout source - the caller owns the split */
86
+ holdout: () => Promise<LearningDataset>;
87
+ mining?: {
88
+ minSupport?: number;
89
+ nMin?: number;
90
+ nMax?: number;
91
+ };
92
+ policy?: Partial<PromotionPolicy>;
93
+ lineageSource?: string;
94
+ }): void;
95
+ export interface WorldModelCandidate extends Candidate {
96
+ environmentId: string;
97
+ beliefs: number;
98
+ corrections: number;
99
+ }
100
+ export declare function registerWorldModelTarget(registry: LearningTargetRegistry, opts: {
101
+ environmentId: string;
102
+ holdout: () => Promise<LearningDataset>;
103
+ policy?: Partial<PromotionPolicy>;
104
+ lineageSource?: string;
105
+ }): void;
@@ -1,4 +1,7 @@
1
1
  import { evidenceThreshold } from "./continual.js";
2
+ import { mineWorkflows } from "./skill-compiler.js";
3
+ import { WorldModel } from "./world-model.js";
4
+ import { replayIntoWorldModel } from "./analysis.js";
2
5
  const REQUIRED_FIELDS = ['trainer', 'evaluator', 'holdout', 'promotionPolicy', 'rollback', 'lineage'];
3
6
  /** Only vocabulary kinds are mirrored - see the module doc. */
4
7
  const LEDGER_MAP = {
@@ -51,7 +54,7 @@ export class LearningTargetRegistry {
51
54
  let evalResult;
52
55
  if (reg.promotionPolicy.requireHoldout) {
53
56
  const hold = await reg.holdout();
54
- evalResult = await reg.evaluator(candidate, hold);
57
+ evalResult = await reg.evaluator(candidate, hold, train);
55
58
  candidate = { ...candidate, status: 'evaluated', evalResult };
56
59
  }
57
60
  const evidence = evalResult?.metrics.evidence ?? 0;
@@ -77,3 +80,101 @@ export class LearningTargetRegistry {
77
80
  catch { /* the ledger must never break the cycle it observes */ }
78
81
  }
79
82
  }
83
+ function containsGram(seq, gram) {
84
+ outer: for (let i = 0; i + gram.length <= seq.length; i++) {
85
+ for (let j = 0; j < gram.length; j++)
86
+ if (seq[i + j] !== gram[j])
87
+ continue outer;
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+ function successSequence(t) {
93
+ return t.steps.filter((s) => s.outcome === 'success').map((s) => s.action.type);
94
+ }
95
+ export function registerSkillTarget(registry, opts) {
96
+ registry.register({
97
+ target: 'skill',
98
+ trainer: async (plan, dataset) => ({
99
+ id: `skill-${plan.opportunityId}-${Math.random().toString(36).slice(2, 8)}`,
100
+ plan,
101
+ createdAt: new Date().toISOString(),
102
+ status: 'draft',
103
+ workflows: mineWorkflows(dataset.trajectories, opts.mining ?? {}),
104
+ }),
105
+ evaluator: async (candidate, holdout) => {
106
+ const top = candidate.workflows[0];
107
+ const holdoutSuccesses = holdout.trajectories.filter((t) => t.metrics.success);
108
+ if (!top) {
109
+ return { candidateId: candidate.id, pass: false, metrics: { evidence: 0, holdoutSuccesses: holdoutSuccesses.length }, holdoutSize: holdout.trajectories.length };
110
+ }
111
+ const containing = holdoutSuccesses.filter((t) => containsGram(successSequence(t), top.steps)).length;
112
+ const evidence = holdoutSuccesses.length > 0 ? Number((containing / holdoutSuccesses.length).toFixed(3)) : 0;
113
+ const pass = holdoutSuccesses.length >= 2 && evidence >= 0.5;
114
+ return {
115
+ candidateId: candidate.id,
116
+ pass,
117
+ metrics: { evidence, holdoutSuccesses: holdoutSuccesses.length, workflowSupport: top.support },
118
+ holdoutSize: holdout.trajectories.length,
119
+ };
120
+ },
121
+ holdout: opts.holdout,
122
+ promotionPolicy: { minEvidence: 0.5, requireHoldout: true, canaryShare: 0.2, ...opts.policy },
123
+ rollback: async () => undefined, // skill rollback is host-side promotion bookkeeping; v0 records intent via the ledger chain
124
+ lineage: { registeredAt: new Date().toISOString(), version: 1, source: opts.lineageSource ?? 'skill-compiler.mineWorkflows' },
125
+ });
126
+ }
127
+ function brierScoreOnly(wm, holdout, environmentId) {
128
+ let checked = 0;
129
+ let sum = 0;
130
+ for (const traj of holdout) {
131
+ if (traj.environment.id !== environmentId)
132
+ continue;
133
+ for (const step of traj.steps) {
134
+ const p = wm.predict({ action: step.action });
135
+ const binary = step.outcome === 'success' ? 1 : 0;
136
+ sum += (p.confidence - binary) ** 2;
137
+ checked++;
138
+ }
139
+ }
140
+ return { checked, brier: checked > 0 ? Number((sum / checked).toFixed(4)) : 0 };
141
+ }
142
+ export function registerWorldModelTarget(registry, opts) {
143
+ registry.register({
144
+ target: 'world_model',
145
+ trainer: async (plan, dataset) => {
146
+ const wm = replayIntoWorldModel(dataset.trajectories, opts.environmentId);
147
+ const st = wm.worldState;
148
+ const corrections = st.beliefs.reduce((s, b) => s + (b.corrections?.length ?? 0), 0);
149
+ return {
150
+ id: `wm-${plan.opportunityId}-${Math.random().toString(36).slice(2, 8)}`,
151
+ plan,
152
+ createdAt: new Date().toISOString(),
153
+ status: 'draft',
154
+ environmentId: opts.environmentId,
155
+ beliefs: st.beliefs.filter((b) => b.id.startsWith('act:')).length,
156
+ corrections,
157
+ };
158
+ },
159
+ evaluator: async (candidate, holdout, train) => {
160
+ // deterministic rebuild from the SAME train set the trainer used - the
161
+ // evaluator receives the train dataset by contract, so no smuggling
162
+ const trained = replayIntoWorldModel(train.trajectories, opts.environmentId);
163
+ const fresh = new WorldModel(opts.environmentId);
164
+ const t = brierScoreOnly(trained, holdout.trajectories, opts.environmentId);
165
+ const f = brierScoreOnly(fresh, holdout.trajectories, opts.environmentId);
166
+ const evidence = f.brier > 0 ? Number(((f.brier - t.brier) / f.brier).toFixed(3)) : 0;
167
+ const pass = t.checked >= 10 && t.brier < f.brier && evidence >= 0.5;
168
+ return {
169
+ candidateId: candidate.id,
170
+ pass,
171
+ metrics: { evidence, brierTrained: t.brier, brierFresh: f.brier, checked: t.checked },
172
+ holdoutSize: holdout.trajectories.length,
173
+ };
174
+ },
175
+ holdout: opts.holdout,
176
+ promotionPolicy: { minEvidence: 0.5, requireHoldout: true, canaryShare: 0.2, ...opts.policy },
177
+ rollback: async () => undefined, // world-model rollback = dropping the candidate model; the base model is never mutated in-place
178
+ lineage: { registeredAt: new Date().toISOString(), version: 1, source: opts.lineageSource ?? 'analysis.replayIntoWorldModel' },
179
+ });
180
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/cognitive",
3
- "version": "0.23.27",
3
+ "version": "0.23.29",
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.23.27"
29
+ "@hmharness/kernel": "0.23.29"
30
30
  }
31
31
  }