@hmharness/evolution 0.23.50 → 0.23.52

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/bench.d.ts CHANGED
@@ -24,6 +24,16 @@ export interface BenchResult {
24
24
  detail: string;
25
25
  }
26
26
  export declare function matchExpect(output: string, expect: string[]): boolean;
27
+ /** Strip JS regex-literal delimiters: `/^\d+$/i` -> `^\d+$`.
28
+ * .task data files write expect-regex with slashes (bench/cases/*.task,
29
+ * HarmonyBench exports); callers hand the value to `new RegExp(...)`, which
30
+ * treats the slashes as literal characters — so `/^\d+$/` matched the
31
+ * literal text "^\d+$" and never a digit. 13 of 35 gate cases were
32
+ * unpassable for this reason (2026-11-25; three-arm v1/v2 root cause).
33
+ * Flags are dropped on purpose: the assertions are case-sensitive.
34
+ * A bare pattern (no leading slash) is returned untouched, and a pattern
35
+ * whose first slash is never closed is treated as literal. */
36
+ export declare function normalizeRegex(pattern: string): string;
27
37
  /** Fence convention: when the reply is a fenced block, assertions test the
28
38
  * fence INNER content (day-55 finding: models preserve literals verbatim
29
39
  * inside code fences while normalizing them in prose - the fence is the
package/dist/bench.js CHANGED
@@ -24,6 +24,34 @@ export function matchExpect(output, expect) {
24
24
  const lower = output.toLowerCase();
25
25
  return expect.every((e) => lower.includes(e.toLowerCase()));
26
26
  }
27
+ /** Strip JS regex-literal delimiters: `/^\d+$/i` -> `^\d+$`.
28
+ * .task data files write expect-regex with slashes (bench/cases/*.task,
29
+ * HarmonyBench exports); callers hand the value to `new RegExp(...)`, which
30
+ * treats the slashes as literal characters — so `/^\d+$/` matched the
31
+ * literal text "^\d+$" and never a digit. 13 of 35 gate cases were
32
+ * unpassable for this reason (2026-11-25; three-arm v1/v2 root cause).
33
+ * Flags are dropped on purpose: the assertions are case-sensitive.
34
+ * A bare pattern (no leading slash) is returned untouched, and a pattern
35
+ * whose first slash is never closed is treated as literal. */
36
+ export function normalizeRegex(pattern) {
37
+ if (pattern.length < 2 || pattern[0] !== '/')
38
+ return pattern;
39
+ let escaped = false;
40
+ for (let i = 1; i < pattern.length; i++) {
41
+ const ch = pattern[i];
42
+ if (escaped) {
43
+ escaped = false;
44
+ continue;
45
+ }
46
+ if (ch === '\\') {
47
+ escaped = true;
48
+ continue;
49
+ }
50
+ if (ch === '/')
51
+ return pattern.slice(1, i);
52
+ }
53
+ return pattern;
54
+ }
27
55
  /** Fence convention: when the reply is a fenced block, assertions test the
28
56
  * fence INNER content (day-55 finding: models preserve literals verbatim
29
57
  * inside code fences while normalizing them in prose - the fence is the
@@ -41,7 +69,9 @@ export function matchCase(output, c) {
41
69
  }
42
70
  if (c.expectRegex !== undefined) {
43
71
  try {
44
- if (!new RegExp(c.expectRegex).test(body))
72
+ // defense in depth: case objects built by hand (not via listCases)
73
+ // may still carry the delimiters
74
+ if (!new RegExp(normalizeRegex(c.expectRegex)).test(body))
45
75
  return { pass: false, detail: `regex mismatch: /${c.expectRegex.slice(0, 60)}/` };
46
76
  }
47
77
  catch {
@@ -87,7 +117,9 @@ export async function listCases(home) {
87
117
  prompt,
88
118
  expect: list('expect') ?? [],
89
119
  expectExact: pick('expect-exact')?.replace(/^"|"$/g, ''),
90
- expectRegex: pick('expect-regex'),
120
+ // normalized at the parse boundary so every consumer (matchCase,
121
+ // three-arm judgeOutput, experiment drivers) gets a bare pattern
122
+ expectRegex: pick('expect-regex') !== undefined ? normalizeRegex(pick('expect-regex')) : undefined,
91
123
  expectNone: list('expect-none'),
92
124
  expectAny: list('expect-any'),
93
125
  tools: pick('tools') === 'loop',
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @hmharness/evolution - case-runner
3
+ *
4
+ * The ONE place that turns a bench case + skills prompt into model messages.
5
+ *
6
+ * Root cause #2 (2026-11-25): `runEvolution` calls `runCase(case, skillsPrompt)`
7
+ * - the baseline receives the active-skills block, the candidate receives
8
+ * active + draft. Three call sites existed and two of them (the agent
9
+ * background loop and the TUI) silently dropped the second parameter, so in
10
+ * production both arms got IDENTICAL messages and the promotion gate compared
11
+ * A against A. The published evolution log shows the fingerprint: 125 of 127
12
+ * promotions had baseline pass rate == candidate pass rate.
13
+ *
14
+ * This module makes the injection impossible to forget: the skills prompt is
15
+ * a required parameter of `caseRunnerMessages` and always lands in the
16
+ * system message. Every `runEvolution` call site builds its runner from here
17
+ * (the CLI's own `makeCaseRunner` was the correct reference implementation -
18
+ * it had fixed the same defect on "day-50" - so this is that fix, shared).
19
+ */
20
+ export type ChatMessage = {
21
+ role: 'system' | 'user';
22
+ content: string;
23
+ };
24
+ /** Messages for a non-tool bench case. An empty skills prompt yields the
25
+ * user message alone, which keeps the control arm byte-identical to the
26
+ * pre-skills behavior. */
27
+ export declare function caseRunnerMessages(prompt: string, skillsPrompt: string): ChatMessage[];
28
+ export interface CaseRunnerDeps {
29
+ /** One model turn. Receives exactly what `caseRunnerMessages` built.
30
+ * `content` may be null - that is the kernel's ChatResponse contract
31
+ * (`content: string | null`), so callers can pass `chat` straight through. */
32
+ chat: (messages: ChatMessage[]) => Promise<{
33
+ message: {
34
+ content?: string | null;
35
+ };
36
+ }>;
37
+ }
38
+ /** A `runEvolution`-compatible `runCase` for the plain (non-tool) bench cases.
39
+ * Returns the function itself - the exact shape `runEvolution` expects, so a
40
+ * call site reads `runCase: buildCaseRunner({ chat })`.
41
+ * Tool cases (`c.tools`) need a live agent loop, so they are the caller's
42
+ * concern: route them before calling this and pass the skills prompt into
43
+ * the loop's system prompt yourself (the agent runner shows the pattern). */
44
+ export declare function buildCaseRunner(deps: CaseRunnerDeps): (c: {
45
+ prompt: string;
46
+ tools: boolean;
47
+ }, skillsPrompt: string) => Promise<string>;
@@ -0,0 +1,43 @@
1
+ /**
2
+ * @hmharness/evolution - case-runner
3
+ *
4
+ * The ONE place that turns a bench case + skills prompt into model messages.
5
+ *
6
+ * Root cause #2 (2026-11-25): `runEvolution` calls `runCase(case, skillsPrompt)`
7
+ * - the baseline receives the active-skills block, the candidate receives
8
+ * active + draft. Three call sites existed and two of them (the agent
9
+ * background loop and the TUI) silently dropped the second parameter, so in
10
+ * production both arms got IDENTICAL messages and the promotion gate compared
11
+ * A against A. The published evolution log shows the fingerprint: 125 of 127
12
+ * promotions had baseline pass rate == candidate pass rate.
13
+ *
14
+ * This module makes the injection impossible to forget: the skills prompt is
15
+ * a required parameter of `caseRunnerMessages` and always lands in the
16
+ * system message. Every `runEvolution` call site builds its runner from here
17
+ * (the CLI's own `makeCaseRunner` was the correct reference implementation -
18
+ * it had fixed the same defect on "day-50" - so this is that fix, shared).
19
+ */
20
+ /** Messages for a non-tool bench case. An empty skills prompt yields the
21
+ * user message alone, which keeps the control arm byte-identical to the
22
+ * pre-skills behavior. */
23
+ export function caseRunnerMessages(prompt, skillsPrompt) {
24
+ if (skillsPrompt) {
25
+ return [
26
+ { role: 'system', content: skillsPrompt },
27
+ { role: 'user', content: prompt },
28
+ ];
29
+ }
30
+ return [{ role: 'user', content: prompt }];
31
+ }
32
+ /** A `runEvolution`-compatible `runCase` for the plain (non-tool) bench cases.
33
+ * Returns the function itself - the exact shape `runEvolution` expects, so a
34
+ * call site reads `runCase: buildCaseRunner({ chat })`.
35
+ * Tool cases (`c.tools`) need a live agent loop, so they are the caller's
36
+ * concern: route them before calling this and pass the skills prompt into
37
+ * the loop's system prompt yourself (the agent runner shows the pattern). */
38
+ export function buildCaseRunner(deps) {
39
+ return async (c, skillsPrompt) => {
40
+ const r = await deps.chat(caseRunnerMessages(c.prompt, skillsPrompt));
41
+ return r.message.content ?? '';
42
+ };
43
+ }
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export * from './sequential.js';
11
11
  export * from './insights.js';
12
12
  export * from './skills.js';
13
13
  export * from './bench.js';
14
+ export * from './case-runner.js';
14
15
  export * from './evolve.js';
15
16
  export * from './patches.js';
16
17
  export * from './impact.js';
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ export * from "./sequential.js";
11
11
  export * from "./insights.js";
12
12
  export * from "./skills.js";
13
13
  export * from "./bench.js";
14
+ export * from "./case-runner.js";
14
15
  export * from "./evolve.js";
15
16
  export * from "./patches.js";
16
17
  export * from "./impact.js";
@@ -2,7 +2,15 @@ export interface Insight {
2
2
  time: string;
3
3
  session: string;
4
4
  task: string;
5
- outcome: 'ok' | 'turn-budget' | 'error';
5
+ /** How the task's agent loop stopped. `ok` = the model produced a final
6
+ * answer. The rest mirror the kernel's `LoopResult.reason` (2026-11-25;
7
+ * before that the agent re-derived a two-valued 'ok' | 'turn-budget' from
8
+ * the turn count, so a loop stopped by the token valve or an interrupt
9
+ * was logged as a success). 'turn-budget' is kept because it is what
10
+ * historical records on disk contain.
11
+ * NOTE: `ok` is "finished with an answer", not "the answer is correct" -
12
+ * correctness is not observable from inside the loop. */
13
+ outcome: 'ok' | 'turn-budget' | 'error' | 'turn-valve' | 'token-valve' | 'idle' | 'interrupted';
6
14
  turns: number;
7
15
  toolUses: number;
8
16
  toolsUsed: string[];
package/dist/skills.d.ts CHANGED
@@ -25,6 +25,12 @@ export declare function listDrafts(home: string): Promise<SkillEntry[]>;
25
25
  /** Canary-state skills: promoted through the bench gates but still under
26
26
  * impact evaluation - injected into a sample of sessions, watermarked. */
27
27
  export declare function listCanary(home: string): Promise<SkillEntry[]>;
28
+ /** One line per skill. The line carries the skill's sub-folder as a short tag
29
+ * (`- name [canary]: description`); the system prompt supplies the
30
+ * "tag → path" legend under the real HMH_HOME. Before 2026-11-25 the line had
31
+ * no location at all: SKILL.md files sit in three different folders and the
32
+ * model had to guess — in the session history it read a skill body in 1.9% of
33
+ * the sessions that had skills injected (23 of 77 reads failed on the path). */
28
34
  export declare function skillsToPrompt(entries: SkillEntry[]): string;
29
35
  /** Write (or overwrite) a draft; drafts are cheap and reversible by deletion. */
30
36
  export declare function writeDraft(home: string, name: string, skillMd: string): Promise<string>;
@@ -52,3 +58,11 @@ export declare function rollbackSkill(home: string, name: string): Promise<boole
52
58
  /** Demote an active skill back to draft without deleting anything. */
53
59
  export declare function unpromoteSkill(home: string, name: string): Promise<boolean>;
54
60
  export declare function deleteDraft(home: string, name: string): Promise<boolean>;
61
+ /** One-line description of a SKILL.md: the frontmatter `description`, else the
62
+ * first non-heading non-empty line of the BODY. Line endings are normalised
63
+ * first: 65 of 107 skills in a real library had CRLF frontmatter, which the
64
+ * LF-only regex missed, so their "description" came out as the frontmatter's
65
+ * "name: <skill>" line and the model could not tell what they were for.
66
+ * Frontmatter without a description falls through to the body, never to
67
+ * another frontmatter field. Exported for tests. */
68
+ export declare function parseDescription(text: string): string;
package/dist/skills.js CHANGED
@@ -169,10 +169,31 @@ async function scan(dir, exclude, state) {
169
169
  }
170
170
  return entries;
171
171
  }
172
+ /** Sub-folder of the skills root a SKILL.md lives in, or '' for the top-level
173
+ * layout: skills\<name>\SKILL.md → '', skills\active\<name>\SKILL.md → 'active'.
174
+ * Derived from the file path: top-level and active/ entries both carry
175
+ * state 'active', so the state alone cannot tell the model where to read. */
176
+ function folderTag(file) {
177
+ const norm = file.replace(/\\/g, '/');
178
+ const i = norm.lastIndexOf('/skills/');
179
+ if (i < 0)
180
+ return '';
181
+ const parts = norm.slice(i + '/skills/'.length).split('/');
182
+ return parts.length > 2 ? parts[0] : '';
183
+ }
184
+ /** One line per skill. The line carries the skill's sub-folder as a short tag
185
+ * (`- name [canary]: description`); the system prompt supplies the
186
+ * "tag → path" legend under the real HMH_HOME. Before 2026-11-25 the line had
187
+ * no location at all: SKILL.md files sit in three different folders and the
188
+ * model had to guess — in the session history it read a skill body in 1.9% of
189
+ * the sessions that had skills injected (23 of 77 reads failed on the path). */
172
190
  export function skillsToPrompt(entries) {
173
191
  if (entries.length === 0)
174
192
  return '';
175
- return entries.map((s) => `- ${s.name}: ${s.description || '(no description)'}`).join('\n');
193
+ return entries.map((s) => {
194
+ const tag = folderTag(s.file);
195
+ return `- ${s.name}${tag ? ` [${tag}]` : ''}: ${s.description || '(no description)'}`;
196
+ }).join('\n');
176
197
  }
177
198
  /** Write (or overwrite) a draft; drafts are cheap and reversible by deletion. */
178
199
  export async function writeDraft(home, name, skillMd) {
@@ -304,15 +325,39 @@ export async function deleteDraft(home, name) {
304
325
  function sanitize(name) {
305
326
  return name.replace(/[^a-zA-Z0-9_-]/g, '-').slice(0, 60) || 'skill';
306
327
  }
307
- function parseDescription(text) {
308
- // frontmatter "description:" line, or first non-heading non-empty line
309
- const fm = text.match(/^---\n([\s\S]*?)\n---/);
310
- if (fm) {
311
- const m = fm[1].match(/^description:\s*(.+)$/m);
312
- if (m)
313
- return m[1].trim().slice(0, 120);
328
+ /** The frontmatter `description`: inline (optionally quoted) or a folded/literal block scalar. '' when absent. */
329
+ function frontmatterDescription(fm) {
330
+ const lines = fm.split('\n');
331
+ const i = lines.findIndex((l) => /^description:/.test(l));
332
+ if (i < 0)
333
+ return '';
334
+ const inline = lines[i].replace(/^description:\s*/, '').trim();
335
+ if (!/^[>|][+-]?$/.test(inline))
336
+ return inline.replace(/^["']|["']$/g, '').trim();
337
+ // block scalar: every following indented line belongs to it
338
+ const folded = [];
339
+ for (const l of lines.slice(i + 1)) {
340
+ if (!/^\s/.test(l) || !l.trim())
341
+ break;
342
+ folded.push(l.trim());
314
343
  }
315
- for (const line of text.split('\n')) {
344
+ return folded.join(' ');
345
+ }
346
+ /** One-line description of a SKILL.md: the frontmatter `description`, else the
347
+ * first non-heading non-empty line of the BODY. Line endings are normalised
348
+ * first: 65 of 107 skills in a real library had CRLF frontmatter, which the
349
+ * LF-only regex missed, so their "description" came out as the frontmatter's
350
+ * "name: <skill>" line and the model could not tell what they were for.
351
+ * Frontmatter without a description falls through to the body, never to
352
+ * another frontmatter field. Exported for tests. */
353
+ export function parseDescription(text) {
354
+ const lf = text.replace(/\r\n/g, '\n');
355
+ const fm = lf.match(/^---\n([\s\S]*?)\n---(?:\n|$)/);
356
+ const body = fm ? lf.slice(fm[0].length) : lf;
357
+ const fromFm = fm ? frontmatterDescription(fm[1]) : '';
358
+ if (fromFm)
359
+ return fromFm.trim().slice(0, 120);
360
+ for (const line of body.split('\n')) {
316
361
  const t = line.trim();
317
362
  if (t && !t.startsWith('#') && !t.startsWith('---'))
318
363
  return t.slice(0, 120);
package/dist/three-arm.js CHANGED
@@ -16,6 +16,7 @@
16
16
  * analysis cannot tune them after seeing results (pre-registration rule).
17
17
  */
18
18
  import { createHash } from 'node:crypto';
19
+ import { normalizeRegex } from "./bench.js";
19
20
  /* ---------------- pre-registered constants (frozen before any run) ---------------- */
20
21
  export const EXPERIMENT_ID = 'three-arm-main-experiment-2026-10-27';
21
22
  /** Family quotas for the train subset. Families are the first '-' segment of
@@ -126,7 +127,10 @@ export function judgeOutput(output, t) {
126
127
  }
127
128
  if (a.expectRegex !== undefined) {
128
129
  try {
129
- if (!new RegExp(a.expectRegex).test(body))
130
+ // normalizeRegex: .task files carry JS-literal delimiters (/^\d+$/);
131
+ // without it the judge compared against the literal text "^\d+$" and
132
+ // 13 gate cases were unpassable (2026-11-25 root cause)
133
+ if (!new RegExp(normalizeRegex(a.expectRegex)).test(body))
130
134
  return { pass: false, detail: `regex mismatch: /${a.expectRegex.slice(0, 60)}/`, falseCompletion: DONE_RE.test(body) };
131
135
  }
132
136
  catch {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/evolution",
3
- "version": "0.23.50",
3
+ "version": "0.23.52",
4
4
  "description": "hmharness evolution subsystem: persistent memory, insight capture, skill library, and the bench that gives evolution its fitness signal. First-class kernel citizen, not a plugin.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -15,8 +15,8 @@
15
15
  "build": "tsc -p tsconfig.build.json"
16
16
  },
17
17
  "dependencies": {
18
- "@hmharness/cognitive": "0.23.50",
19
- "@hmharness/kernel": "0.23.50"
18
+ "@hmharness/cognitive": "0.23.52",
19
+ "@hmharness/kernel": "0.23.52"
20
20
  },
21
21
  "files": [
22
22
  "dist"