@hmharness/cognitive 0.23.43 → 0.23.44
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/skill-compiler.d.ts +91 -0
- package/dist/skill-compiler.js +145 -0
- package/package.json +2 -2
package/dist/skill-compiler.d.ts
CHANGED
|
@@ -94,6 +94,96 @@ export interface DerivedCounterexample {
|
|
|
94
94
|
export declare function deriveCounterexamples(candidate: WorkflowCandidate, holdout: CognitiveTrajectory[], opts?: {
|
|
95
95
|
cap?: number;
|
|
96
96
|
}): DerivedCounterexample[];
|
|
97
|
+
export interface MinedPreconditions {
|
|
98
|
+
/** action types appearing in the support trajectories' prefixes before the
|
|
99
|
+
* gram starts; coverage = fraction of support trajectories whose prefix
|
|
100
|
+
* contains the type */
|
|
101
|
+
preceding: Array<{
|
|
102
|
+
type: string;
|
|
103
|
+
coverage: number;
|
|
104
|
+
}>;
|
|
105
|
+
/** [p10, p90] nearest-rank band of first-containment position / length */
|
|
106
|
+
positionBand?: {
|
|
107
|
+
p10: number;
|
|
108
|
+
p90: number;
|
|
109
|
+
};
|
|
110
|
+
/** goal-description tokens shared across support trajectories */
|
|
111
|
+
goalKeywords: Array<{
|
|
112
|
+
word: string;
|
|
113
|
+
coverage: number;
|
|
114
|
+
}>;
|
|
115
|
+
/** support trajectories actually located (gram found in each) */
|
|
116
|
+
supportUsed: number;
|
|
117
|
+
}
|
|
118
|
+
/** Mine structured preconditions for a workflow candidate from its TRAIN
|
|
119
|
+
* support trajectories (holdout must NEVER reach this function — the P1-2
|
|
120
|
+
* no-look rule). Everything carries coverage; below-threshold signals stay
|
|
121
|
+
* absent rather than diluted. Deterministic: ties break lexicographically. */
|
|
122
|
+
export declare function minePreconditions(candidate: WorkflowCandidate, train: CognitiveTrajectory[], opts?: {
|
|
123
|
+
minCoverage?: number;
|
|
124
|
+
cap?: number;
|
|
125
|
+
minBandSupport?: number;
|
|
126
|
+
}): MinedPreconditions;
|
|
127
|
+
/** Turn mined signals into Conditions with the mechanical check grammar the
|
|
128
|
+
* matcher parses back (see parseStructuredCheck). */
|
|
129
|
+
export declare function preconditionsFromMined(mined: MinedPreconditions): Condition[];
|
|
130
|
+
/** A candidate has a learnable firing context iff at least one preceding
|
|
131
|
+
* type or goal keyword clears the coverage bar — the P1-2 signal gate. */
|
|
132
|
+
export declare function hasContextSignal(mined: MinedPreconditions): boolean;
|
|
133
|
+
/** Arm-B selection: the first candidate (in arm-A's existing order) whose
|
|
134
|
+
* mined preconditions show a stable firing context; if none does, the
|
|
135
|
+
* family is degenerate and B falls back to arm-A's pick, recorded as such. */
|
|
136
|
+
export declare function selectByPreconditionStability(candidates: WorkflowCandidate[], train: CognitiveTrajectory[], opts?: {
|
|
137
|
+
minCoverage?: number;
|
|
138
|
+
}): {
|
|
139
|
+
selected: WorkflowCandidate | undefined;
|
|
140
|
+
signalPool: WorkflowCandidate[];
|
|
141
|
+
degenerate: boolean;
|
|
142
|
+
};
|
|
143
|
+
/** Compile a candidate into a V2 skill WITH its preconditions filled from
|
|
144
|
+
* train (toSkillSpecV2 leaves them [] — the现状 this factor repairs). */
|
|
145
|
+
export declare function compileSkillWithPreconditions(candidate: WorkflowCandidate, train: CognitiveTrajectory[], opts?: {
|
|
146
|
+
provenance?: string;
|
|
147
|
+
environmentScope?: string[];
|
|
148
|
+
minCoverage?: number;
|
|
149
|
+
}): SkillSpecV2;
|
|
150
|
+
type StructuredCheck = {
|
|
151
|
+
kind: 'preceding';
|
|
152
|
+
type: string;
|
|
153
|
+
check: string;
|
|
154
|
+
} | {
|
|
155
|
+
kind: 'position';
|
|
156
|
+
lo: number;
|
|
157
|
+
hi: number;
|
|
158
|
+
check: string;
|
|
159
|
+
} | {
|
|
160
|
+
kind: 'goal';
|
|
161
|
+
word: string;
|
|
162
|
+
check: string;
|
|
163
|
+
};
|
|
164
|
+
/** Parse the frozen check grammar; anything else is reported, not guessed. */
|
|
165
|
+
export declare function parseStructuredCheck(check: string): StructuredCheck | undefined;
|
|
166
|
+
export interface TriggerMatchResult {
|
|
167
|
+
score: number;
|
|
168
|
+
matched: boolean;
|
|
169
|
+
satisfied: number;
|
|
170
|
+
total: number;
|
|
171
|
+
/** structured checks this skill carries; 0 => score is vacuous */
|
|
172
|
+
vacuous: boolean;
|
|
173
|
+
/** check strings outside the frozen grammar, reported never guessed */
|
|
174
|
+
ignored: string[];
|
|
175
|
+
anchor: number;
|
|
176
|
+
components: Array<{
|
|
177
|
+
check: string;
|
|
178
|
+
satisfied: boolean;
|
|
179
|
+
}>;
|
|
180
|
+
}
|
|
181
|
+
/** Precondition-aware trigger scoring on an UNSEEN trajectory. anchor =
|
|
182
|
+
* first contiguous-containment start of the gram; else first occurrence of
|
|
183
|
+
* gram[0]; else -1 (preceding/position unsatisfiable, goal still live). */
|
|
184
|
+
export declare function triggerMatchScore(skill: SkillSpecV2, traj: CognitiveTrajectory, opts?: {
|
|
185
|
+
threshold?: number;
|
|
186
|
+
}): TriggerMatchResult;
|
|
97
187
|
export interface SkillIR {
|
|
98
188
|
skillId: string;
|
|
99
189
|
/** procedure steps as abstract verbs; unmapped positions keep the literal */
|
|
@@ -163,3 +253,4 @@ export declare function mineAntiPatterns(experiences: CognitiveTrajectory[], opt
|
|
|
163
253
|
minFailures?: number;
|
|
164
254
|
nMax?: number;
|
|
165
255
|
}): AntiPattern[];
|
|
256
|
+
export {};
|
package/dist/skill-compiler.js
CHANGED
|
@@ -71,6 +71,151 @@ export function deriveCounterexamples(candidate, holdout, opts = {}) {
|
|
|
71
71
|
}
|
|
72
72
|
return out;
|
|
73
73
|
}
|
|
74
|
+
/** First index where gram occurs CONTIGUOUSLY in seq, or -1. */
|
|
75
|
+
function firstContainment(seq, gram) {
|
|
76
|
+
if (gram.length === 0)
|
|
77
|
+
return -1;
|
|
78
|
+
outer: for (let i = 0; i + gram.length <= seq.length; i++) {
|
|
79
|
+
for (let j = 0; j < gram.length; j++)
|
|
80
|
+
if (seq[i + j] !== gram[j])
|
|
81
|
+
continue outer;
|
|
82
|
+
return i;
|
|
83
|
+
}
|
|
84
|
+
return -1;
|
|
85
|
+
}
|
|
86
|
+
function tokenizeGoal(text) {
|
|
87
|
+
return text.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter((w) => w.length >= 3);
|
|
88
|
+
}
|
|
89
|
+
const successTypes = (t) => t.steps.filter((s) => s.outcome === 'success').map((s) => s.action.type);
|
|
90
|
+
/** Mine structured preconditions for a workflow candidate from its TRAIN
|
|
91
|
+
* support trajectories (holdout must NEVER reach this function — the P1-2
|
|
92
|
+
* no-look rule). Everything carries coverage; below-threshold signals stay
|
|
93
|
+
* absent rather than diluted. Deterministic: ties break lexicographically. */
|
|
94
|
+
export function minePreconditions(candidate, train, opts = {}) {
|
|
95
|
+
const minCoverage = opts.minCoverage ?? 0.6;
|
|
96
|
+
const cap = opts.cap ?? 3;
|
|
97
|
+
const minBandSupport = opts.minBandSupport ?? 4;
|
|
98
|
+
const byId = new Map(train.map((t) => [t.id, t]));
|
|
99
|
+
const support = candidate.trajectoryIds.map((id) => byId.get(id)).filter((t) => !!t);
|
|
100
|
+
const gram = candidate.steps;
|
|
101
|
+
const preCount = new Map();
|
|
102
|
+
const goalCount = new Map();
|
|
103
|
+
const ratios = [];
|
|
104
|
+
let used = 0;
|
|
105
|
+
for (const t of support) {
|
|
106
|
+
const seq = successTypes(t);
|
|
107
|
+
const i = firstContainment(seq, gram);
|
|
108
|
+
if (i < 0)
|
|
109
|
+
continue; // support recorded on this train set but gram not re-locatable: skip honestly
|
|
110
|
+
used++;
|
|
111
|
+
for (const ty of new Set(seq.slice(0, i)))
|
|
112
|
+
preCount.set(ty, (preCount.get(ty) ?? 0) + 1);
|
|
113
|
+
if (seq.length > 0)
|
|
114
|
+
ratios.push(i / seq.length);
|
|
115
|
+
for (const w of new Set(tokenizeGoal(t.goal?.description ?? '')))
|
|
116
|
+
goalCount.set(w, (goalCount.get(w) ?? 0) + 1);
|
|
117
|
+
}
|
|
118
|
+
const keep = (m) => [...m.entries()]
|
|
119
|
+
.filter(([, c]) => used > 0 && c / used >= minCoverage)
|
|
120
|
+
.sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))
|
|
121
|
+
.slice(0, cap);
|
|
122
|
+
return {
|
|
123
|
+
preceding: keep(preCount).map(([type, c]) => ({ type, coverage: Number((c / used).toFixed(2)) })),
|
|
124
|
+
...(used >= minBandSupport && ratios.length > 0
|
|
125
|
+
? {
|
|
126
|
+
positionBand: (() => {
|
|
127
|
+
const s = [...ratios].sort((a, b) => a - b);
|
|
128
|
+
const q = (p) => Number(s[Math.min(s.length - 1, Math.max(0, Math.ceil(p * s.length) - 1))].toFixed(2));
|
|
129
|
+
return { p10: q(0.1), p90: q(0.9) };
|
|
130
|
+
})(),
|
|
131
|
+
}
|
|
132
|
+
: {}),
|
|
133
|
+
goalKeywords: keep(goalCount).map(([word, c]) => ({ word, coverage: Number((c / used).toFixed(2)) })),
|
|
134
|
+
supportUsed: used,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** Turn mined signals into Conditions with the mechanical check grammar the
|
|
138
|
+
* matcher parses back (see parseStructuredCheck). */
|
|
139
|
+
export function preconditionsFromMined(mined) {
|
|
140
|
+
const out = [];
|
|
141
|
+
for (const p of mined.preceding) {
|
|
142
|
+
out.push({ description: `${p.type} seen before the workflow starts (coverage ${p.coverage})`, check: `ctx:preceding:${p.type}` });
|
|
143
|
+
}
|
|
144
|
+
if (mined.positionBand) {
|
|
145
|
+
const { p10, p90 } = mined.positionBand;
|
|
146
|
+
out.push({ description: `workflow starts within relative position [${p10}, ${p90}]`, check: `ctx:position:${p10.toFixed(2)}-${p90.toFixed(2)}` });
|
|
147
|
+
}
|
|
148
|
+
for (const g of mined.goalKeywords) {
|
|
149
|
+
out.push({ description: `goal mentions "${g.word}" (coverage ${g.coverage})`, check: `ctx:goal:${g.word}` });
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
/** A candidate has a learnable firing context iff at least one preceding
|
|
154
|
+
* type or goal keyword clears the coverage bar — the P1-2 signal gate. */
|
|
155
|
+
export function hasContextSignal(mined) {
|
|
156
|
+
return mined.preceding.length > 0 || mined.goalKeywords.length > 0;
|
|
157
|
+
}
|
|
158
|
+
/** Arm-B selection: the first candidate (in arm-A's existing order) whose
|
|
159
|
+
* mined preconditions show a stable firing context; if none does, the
|
|
160
|
+
* family is degenerate and B falls back to arm-A's pick, recorded as such. */
|
|
161
|
+
export function selectByPreconditionStability(candidates, train, opts = {}) {
|
|
162
|
+
const pool = candidates.filter((c) => hasContextSignal(minePreconditions(c, train, opts)));
|
|
163
|
+
return {
|
|
164
|
+
selected: pool[0] ?? candidates[0],
|
|
165
|
+
signalPool: pool,
|
|
166
|
+
degenerate: pool.length === 0 && candidates.length > 0,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** Compile a candidate into a V2 skill WITH its preconditions filled from
|
|
170
|
+
* train (toSkillSpecV2 leaves them [] — the现状 this factor repairs). */
|
|
171
|
+
export function compileSkillWithPreconditions(candidate, train, opts = {}) {
|
|
172
|
+
const skill = toSkillSpecV2(candidate, opts);
|
|
173
|
+
return { ...skill, preconditions: preconditionsFromMined(minePreconditions(candidate, train, { minCoverage: opts.minCoverage })) };
|
|
174
|
+
}
|
|
175
|
+
/** Parse the frozen check grammar; anything else is reported, not guessed. */
|
|
176
|
+
export function parseStructuredCheck(check) {
|
|
177
|
+
if (check.startsWith('ctx:preceding:'))
|
|
178
|
+
return { kind: 'preceding', type: check.slice('ctx:preceding:'.length), check };
|
|
179
|
+
const pos = /^ctx:position:(\d+\.\d+)-(\d+\.\d+)$/.exec(check);
|
|
180
|
+
if (pos)
|
|
181
|
+
return { kind: 'position', lo: Number(pos[1]), hi: Number(pos[2]), check };
|
|
182
|
+
if (check.startsWith('ctx:goal:'))
|
|
183
|
+
return { kind: 'goal', word: check.slice('ctx:goal:'.length), check };
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
/** Precondition-aware trigger scoring on an UNSEEN trajectory. anchor =
|
|
187
|
+
* first contiguous-containment start of the gram; else first occurrence of
|
|
188
|
+
* gram[0]; else -1 (preceding/position unsatisfiable, goal still live). */
|
|
189
|
+
export function triggerMatchScore(skill, traj, opts = {}) {
|
|
190
|
+
const threshold = opts.threshold ?? 0.5;
|
|
191
|
+
const gram = skill.trigger.actionTypes?.length ? skill.trigger.actionTypes : skill.procedure.map((s) => s.ref);
|
|
192
|
+
const seq = successTypes(traj);
|
|
193
|
+
const contained = firstContainment(seq, gram);
|
|
194
|
+
const firstStep = gram[0] !== undefined ? seq.indexOf(gram[0]) : -1;
|
|
195
|
+
const anchor = contained >= 0 ? contained : firstStep;
|
|
196
|
+
const structured = [];
|
|
197
|
+
const ignored = [];
|
|
198
|
+
for (const c of skill.preconditions) {
|
|
199
|
+
const p = parseStructuredCheck(c.check);
|
|
200
|
+
if (p)
|
|
201
|
+
structured.push(p);
|
|
202
|
+
else
|
|
203
|
+
ignored.push(c.check);
|
|
204
|
+
}
|
|
205
|
+
const prefixTypes = new Set(anchor > 0 ? seq.slice(0, anchor) : []);
|
|
206
|
+
const goalTokens = new Set(tokenizeGoal(traj.goal?.description ?? ''));
|
|
207
|
+
const components = structured.map((p) => ({
|
|
208
|
+
check: p.check,
|
|
209
|
+
satisfied: p.kind === 'preceding' ? prefixTypes.has(p.type)
|
|
210
|
+
: p.kind === 'position' ? anchor >= 0 && seq.length > 0 && anchor / seq.length >= p.lo - 0.0055 && anchor / seq.length <= p.hi + 0.0055
|
|
211
|
+
: goalTokens.has(p.word),
|
|
212
|
+
}));
|
|
213
|
+
const envOK = skill.trigger.environmentId === traj.environment.id;
|
|
214
|
+
const satisfied = components.filter((c) => c.satisfied).length;
|
|
215
|
+
const vacuous = structured.length === 0;
|
|
216
|
+
const score = !envOK ? 0 : vacuous ? 1 : satisfied / structured.length;
|
|
217
|
+
return { score, matched: envOK && score >= threshold, satisfied, total: structured.length, vacuous, ignored, anchor, components };
|
|
218
|
+
}
|
|
74
219
|
export function toSkillIR(skill, map) {
|
|
75
220
|
const abstractProcedure = [];
|
|
76
221
|
const unmapped = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hmharness/cognitive",
|
|
3
|
-
"version": "0.23.
|
|
3
|
+
"version": "0.23.44",
|
|
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.
|
|
29
|
+
"@hmharness/kernel": "0.23.44"
|
|
30
30
|
}
|
|
31
31
|
}
|