@hmharness/cli 0.21.0 → 0.21.2

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.
@@ -0,0 +1 @@
1
+ export declare function runTaskAblation(write: (s: string) => void, home: () => string, runs: number): Promise<void>;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @hmharness/cli - `hmh cognitive ablate-task` (blueprint §26, working domain)
3
+ *
4
+ * ARC ablation measures the CEILING (models fail there, harness gain is not
5
+ * measurable yet — honest zero). This one measures the WORKING RANGE: on
6
+ * verifiable terminal mini-tasks where the model is competent, does the
7
+ * cognitive context (tool-reliability digest) change task success?
8
+ *
9
+ * arm A (cognitive): normal runner (digest injected when evidence exists)
10
+ * arm B (bare): HMH_NO_COGNITIVE=1 strips the digest
11
+ *
12
+ * Same model, same task text, deterministic exact-match verification.
13
+ * Results append to cognitive/ablation.jsonl (type: 'terminal-task').
14
+ */
15
+ import { spawn } from 'node:child_process';
16
+ import { appendFile, mkdir } from 'node:fs/promises';
17
+ import { join } from 'node:path';
18
+ const GREEN = (s) => `\x1b[32m${s}\x1b[0m`;
19
+ const RED = (s) => `\x1b[31m${s}\x1b[0m`;
20
+ const DIM = (s) => `\x1b[2m${s}\x1b[0m`;
21
+ /** deterministic, verifiable mini-tasks (exact substring must appear) */
22
+ const TASKS = [
23
+ { text: '用 read_file 读取 G:/hmharness/package.json,只回复 "name" 字段的精确值,不要其他文字。', expect: 'hmharness', label: 'field-extract' },
24
+ { text: '用 read_file 读取 G:/hmharness/README.md 的第一行,只回复该行内容。', expect: '#', label: 'first-line' },
25
+ { text: '用 run_command 执行 node -e "console.log(21*2)",只回复输出数字。', expect: '42', label: 'compute' },
26
+ { text: '用 list_dir 列出 G:/hmharness/packages 下名为 kernel 的目录是否存在,只回复 是 或 否。', expect: '是', label: 'exists-check' },
27
+ ];
28
+ function runOnce(task, bare, timeoutMs) {
29
+ return new Promise((resolve) => {
30
+ const child = spawn('npx', ['tsx', 'packages/cli/src/main.ts', '--yes', task], {
31
+ cwd: 'G:/hmharness',
32
+ shell: true,
33
+ env: { ...process.env, ...(bare ? { HMH_NO_COGNITIVE: '1' } : {}) },
34
+ });
35
+ let out = '';
36
+ const timer = setTimeout(() => child.kill(), timeoutMs);
37
+ child.stdout?.on('data', (d) => (out += d.toString()));
38
+ child.stderr?.on('data', (d) => (out += d.toString()));
39
+ child.on('close', (code) => {
40
+ clearTimeout(timer);
41
+ resolve({ ok: code === 0, output: out });
42
+ });
43
+ });
44
+ }
45
+ export async function runTaskAblation(write, home, runs) {
46
+ const homeDir = home();
47
+ write(`Terminal 域消融 · ${TASKS.length} 个可验证任务 × ${runs} 轮 × 双臂(§26 工作域测量)\n`);
48
+ let withOk = 0;
49
+ let bareOk = 0;
50
+ let total = 0;
51
+ const details = [];
52
+ for (let r = 1; r <= runs; r++) {
53
+ for (const t of TASKS) {
54
+ total += 1;
55
+ const a = await runOnce(t.text, false, 240_000);
56
+ const b = await runOnce(t.text, true, 240_000);
57
+ const aHit = a.output.includes(t.expect);
58
+ const bHit = b.output.includes(t.expect);
59
+ withOk += aHit ? 1 : 0;
60
+ bareOk += bHit ? 1 : 0;
61
+ details.push({ label: t.label, with: aHit, bare: bHit });
62
+ write(` ${t.label.padEnd(14)} 认知 ${aHit ? GREEN('✓') : RED('✗')} · 裸 ${bHit ? GREEN('✓') : RED('✗')}\n`);
63
+ }
64
+ }
65
+ const record = {
66
+ at: new Date().toISOString(),
67
+ type: 'terminal-task',
68
+ runs,
69
+ withCognitive: { pass: withOk, total },
70
+ bare: { pass: bareOk, total },
71
+ harnessDelta: Number(((withOk - bareOk) / total).toFixed(3)),
72
+ details,
73
+ };
74
+ await mkdir(join(homeDir, 'cognitive'), { recursive: true }).catch(() => undefined);
75
+ await appendFile(join(homeDir, 'cognitive', 'ablation.jsonl'), JSON.stringify(record) + '\n', 'utf8').catch(() => undefined);
76
+ write(`结论 · 认知层 ${withOk}/${total} vs 裸 ${bareOk}/${total} → harness 结果层净贡献 ${(record.harnessDelta * 100).toFixed(1)}%\n`);
77
+ write(DIM('(ARC 域测上限=诚实零;本域测工作范围=模型胜任区的结果增益)\n'));
78
+ }
@@ -32,4 +32,5 @@ export declare function runArc3Play(write: (s: string) => void, opts: {
32
32
  levelsCompleted: number;
33
33
  levelCount: number;
34
34
  actions: number;
35
+ distinctActions: number;
35
36
  } | null>;
package/dist/arc3-play.js CHANGED
@@ -45,13 +45,15 @@ export async function runArc3Ablation(write, opts, deps) {
45
45
  at: new Date().toISOString(),
46
46
  game: gameId,
47
47
  steps: opts.steps,
48
- withCognitive: { levels: withCognitive.levelsCompleted, actions: withCognitive.actions, trajectoryId: withCognitive.trajectoryId },
49
- bare: { levels: bare.levelsCompleted, actions: bare.actions, trajectoryId: bare.trajectoryId },
48
+ withCognitive: { levels: withCognitive.levelsCompleted, actions: withCognitive.actions, distinctActions: withCognitive.distinctActions, trajectoryId: withCognitive.trajectoryId },
49
+ bare: { levels: bare.levelsCompleted, actions: bare.actions, distinctActions: bare.distinctActions, trajectoryId: bare.trajectoryId },
50
50
  harnessDeltaLevels: withCognitive.levelsCompleted - bare.levelsCompleted,
51
+ harnessDeltaDiversity: withCognitive.distinctActions - bare.distinctActions,
51
52
  };
52
53
  await mkdir(join(home, 'cognitive'), { recursive: true }).catch(() => undefined);
53
54
  await appendFile(join(home, 'cognitive', 'ablation.jsonl'), JSON.stringify(record) + '\n', 'utf8').catch(() => undefined);
54
55
  write(`\n结论 · 认知层 ${withCognitive.levelsCompleted} 关 vs 裸提示词 ${bare.levelsCompleted} 关 → harness 净贡献 ${record.harnessDeltaLevels >= 0 ? '+' : ''}${record.harnessDeltaLevels} 关\n`);
56
+ write(` 动作多样性 认知层 ${withCognitive.distinctActions} 种 vs 裸 ${bare.distinctActions} 种(关卡持平时行为差异信号)\n`);
55
57
  write(`(样本=${opts.steps} 步/臂,单次实验;多次运行后 cognitive/ablation.jsonl 聚合更可靠)\n`);
56
58
  }
57
59
  export async function runArc3Play(write, opts, deps) {
@@ -153,6 +155,7 @@ export async function runArc3Play(write, opts, deps) {
153
155
  const score = await env.evaluate();
154
156
  const traj = rec.finish(Number(score.metrics.levelsCompleted ?? 0) > state0.levelsCompleted);
155
157
  await new TrajectoryStore(home).append(traj);
158
+ const distinctActions = new Set(traj.steps.map((s) => s.action.type)).size;
156
159
  const mem = new CognitiveMemory(home);
157
160
  await mem.load();
158
161
  await mem.write({
@@ -176,5 +179,6 @@ export async function runArc3Play(write, opts, deps) {
176
179
  levelsCompleted: Number(score.metrics.levelsCompleted ?? 0),
177
180
  levelCount: Number(score.metrics.levelCount ?? 0),
178
181
  actions: Number(score.metrics.actions ?? opts.steps),
182
+ distinctActions,
179
183
  };
180
184
  }
package/dist/main.js CHANGED
@@ -1192,9 +1192,44 @@ flags:
1192
1192
  if (sub === 'ablate') {
1193
1193
  // blueprint §26 final question, measured: same model, same game —
1194
1194
  // how much does the cognitive layer contribute vs a bare prompt?
1195
+ const { readFile } = await import('node:fs/promises');
1196
+ const { join: j } = await import('node:path');
1197
+ if (rest.includes('--stats')) {
1198
+ try {
1199
+ const text = await readFile(j(homeDir(), 'cognitive', 'ablation.jsonl'), 'utf8');
1200
+ const rows = text.split('\n').filter((l) => l.trim()).map((l) => JSON.parse(l));
1201
+ if (rows.length === 0) {
1202
+ stdout.write('(暂无消融记录——先跑 hmh cognitive ablate --env=arc3)\n');
1203
+ return;
1204
+ }
1205
+ const avg = (pick) => {
1206
+ const vals = rows.map(pick).filter((v) => Number.isFinite(v));
1207
+ return vals.length ? Number((vals.reduce((s, v) => s + v, 0) / vals.length).toFixed(2)) : Number.NaN;
1208
+ };
1209
+ stdout.write(`消融聚合 · ${rows.length} 次实验 · 平均 ${avg((r) => r.steps)} 步/臂\n`);
1210
+ stdout.write(` 关卡净贡献均值 ${avg((r) => r.harnessDeltaLevels)} · 动作多样性净贡献均值 ${avg((r) => r.harnessDeltaDiversity ?? Number.NaN)}\n`);
1211
+ const pos = rows.filter((r) => r.harnessDeltaLevels > 0).length;
1212
+ const neg = rows.filter((r) => r.harnessDeltaLevels < 0).length;
1213
+ stdout.write(` 关卡贡献分布: 正 ${pos} / 持平 ${rows.length - pos - neg} / 负 ${neg}\n`);
1214
+ stdout.write(DIM(' 关卡持平是常态(ARC 难度);多样性与后续关卡增益随样本累积显现\n'));
1215
+ }
1216
+ catch {
1217
+ stdout.write('(暂无消融记录——先跑 hmh cognitive ablate --env=arc3)\n');
1218
+ }
1219
+ return;
1220
+ }
1221
+ if (rest.includes('--task')) {
1222
+ // §26 in the WORKING domain: verifiable terminal mini-tasks, same
1223
+ // model, digest on vs HMH_NO_COGNITIVE=1 — result-level harness delta
1224
+ const rounds = Number(rest.find((a) => a.startsWith('--runs='))?.slice(7) ?? 1);
1225
+ const { runTaskAblation } = await import("./ablate-task.js");
1226
+ const { homeDir: hd } = await import('@hmharness/kernel');
1227
+ await runTaskAblation((s) => stdout.write(s), hd, Number.isFinite(rounds) ? Math.min(Math.max(rounds, 1), 5) : 1);
1228
+ return;
1229
+ }
1195
1230
  if (!rest.includes('--env=arc3')) {
1196
- stdout.write('用法: hmh cognitive ablate --env=arc3 [--game=<id前缀>] [--steps=N]\n');
1197
- stdout.write(DIM(' 双臂对照: 认知层(世界模型信念+经验教训) vs 裸提示词; 结果落 cognitive/ablation.jsonl\n'));
1231
+ stdout.write('用法: hmh cognitive ablate --env=arc3 [--steps=N] | --task [--runs=N] | --stats\n');
1232
+ stdout.write(DIM(' --env=arc3: ARC 域(上限测量); --task: terminal 域(工作范围测量); 结果落 cognitive/ablation.jsonl\n'));
1198
1233
  return;
1199
1234
  }
1200
1235
  const steps = Number(rest.find((a) => a.startsWith('--steps='))?.slice(8) ?? 5);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hmharness/cli",
3
- "version": "0.21.0",
3
+ "version": "0.21.2",
4
4
  "description": "hmharness command line: one-shot tasks, an interactive REPL, a fullscreen TUI, the web frontend, and direct tool invocation.",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",
@@ -43,17 +43,17 @@
43
43
  "build": "tsc -p tsconfig.build.json"
44
44
  },
45
45
  "dependencies": {
46
- "@hmharness/agent": "0.21.0",
47
- "@hmharness/cognitive": "0.21.0",
48
- "@hmharness/domain-harmony": "0.21.0",
49
- "@hmharness/domain-ops": "0.21.0",
50
- "@hmharness/environments": "0.21.0",
51
- "@hmharness/evaluation": "0.21.0",
52
- "@hmharness/evolution": "0.21.0",
53
- "@hmharness/kernel": "0.21.0",
54
- "@hmharness/observability": "0.21.0",
55
- "@hmharness/sandbox": "0.21.0",
56
- "@hmharness/web": "0.21.0",
46
+ "@hmharness/agent": "0.21.2",
47
+ "@hmharness/cognitive": "0.21.2",
48
+ "@hmharness/domain-harmony": "0.21.2",
49
+ "@hmharness/domain-ops": "0.21.2",
50
+ "@hmharness/environments": "0.21.2",
51
+ "@hmharness/evaluation": "0.21.2",
52
+ "@hmharness/evolution": "0.21.2",
53
+ "@hmharness/kernel": "0.21.2",
54
+ "@hmharness/observability": "0.21.2",
55
+ "@hmharness/sandbox": "0.21.2",
56
+ "@hmharness/web": "0.21.2",
57
57
  "qrcode": "^1.5.4"
58
58
  }
59
59
  }