@hmharness/cli 0.14.9 → 0.14.10
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/main.js +91 -0
- package/dist/tui.d.ts +22 -12
- package/dist/tui.js +49 -26
- package/package.json +1 -1
package/dist/main.js
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import readline from 'node:readline/promises';
|
|
25
25
|
import { stdin, stdout } from 'node:process';
|
|
26
|
+
import { readFile } from 'node:fs/promises';
|
|
26
27
|
import { join } from 'node:path';
|
|
27
28
|
import { stopWebDaemon, startWebDaemon, hmhWebUp, readWebPid } from "./web-daemon.js";
|
|
28
29
|
import { chat, homeDir, resolveProvider, initHome, latestSession, listProviders, listSessions, loadConfig, loadTranscript, mcpServerTools, Registry, runLoop, setChatRoute, setLocale, } from '@hmharness/kernel';
|
|
@@ -984,6 +985,96 @@ flags:
|
|
|
984
985
|
}
|
|
985
986
|
return;
|
|
986
987
|
}
|
|
988
|
+
if (cmd === 'reward') {
|
|
989
|
+
// V3 RL phase (ADR-0010): learned reward model over the human labels
|
|
990
|
+
await initHome();
|
|
991
|
+
const home = homeDir();
|
|
992
|
+
const E = await import('@hmharness/evolution');
|
|
993
|
+
const sub = rest[0] ?? 'report';
|
|
994
|
+
const readJsonl = async (f) => {
|
|
995
|
+
try {
|
|
996
|
+
return (await readFile(f, 'utf8')).trim().split('\n').filter(Boolean).map((l) => JSON.parse(l));
|
|
997
|
+
}
|
|
998
|
+
catch {
|
|
999
|
+
return [];
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
const labels = (await readJsonl(join(home, 'evolution', 'reward-human-labels.jsonl')));
|
|
1003
|
+
const insights = (await readJsonl(join(home, 'insights', 'insights.jsonl')));
|
|
1004
|
+
if (sub === 'fit') {
|
|
1005
|
+
if (labels.length < 10) {
|
|
1006
|
+
stdout.write('need >=10 human labels to fit (have ' + labels.length + ')\n');
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
const m = E.fitRewardModel(labels, insights);
|
|
1010
|
+
await E.saveRewardModel(home, m);
|
|
1011
|
+
stdout.write('reward model fitted on ' + m.nSamples + ' joined labels\n');
|
|
1012
|
+
stdout.write(' weights[outcome, failRate, turns, tools, bias] = ' + m.w.map((v) => v.toFixed(3)).join(', ') + '\n');
|
|
1013
|
+
stdout.write(' train RMSE ' + m.trainRmse + (m.holdoutRmse !== undefined ? ' · holdout RMSE ' + m.holdoutRmse : ' · (no holdout yet)') + '\n');
|
|
1014
|
+
stdout.write(' saved: ' + join(home, 'evolution', 'reward-model.json') + '\n');
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
if (sub === 'report') {
|
|
1018
|
+
const m = await E.loadRewardModel(home);
|
|
1019
|
+
if (!m) {
|
|
1020
|
+
stdout.write('no fitted reward model - run: hmh reward fit\n');
|
|
1021
|
+
return;
|
|
1022
|
+
}
|
|
1023
|
+
stdout.write('reward model @ ' + m.trainedAt + '\n');
|
|
1024
|
+
stdout.write(' samples ' + m.nSamples + ' · train RMSE ' + m.trainRmse + (m.holdoutRmse !== undefined ? ' · holdout RMSE ' + m.holdoutRmse : '') + '\n');
|
|
1025
|
+
stdout.write(' weights [outcome, failRate, turns, tools, bias] = ' + m.w.map((v) => v.toFixed(3)).join(', ') + '\n');
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
1028
|
+
if (sub === 'export-dpo') {
|
|
1029
|
+
const { findSessionFile, readSessionHead, loadTranscript } = await import('@hmharness/kernel');
|
|
1030
|
+
const taskOf = async (session) => {
|
|
1031
|
+
try {
|
|
1032
|
+
const f = await findSessionFile(home, session);
|
|
1033
|
+
if (!f)
|
|
1034
|
+
return '';
|
|
1035
|
+
const h = await readSessionHead(f);
|
|
1036
|
+
return h?.firstUser ?? '';
|
|
1037
|
+
}
|
|
1038
|
+
catch {
|
|
1039
|
+
return '';
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
const answerOf = async (session) => {
|
|
1043
|
+
try {
|
|
1044
|
+
const f = await findSessionFile(home, session);
|
|
1045
|
+
if (!f)
|
|
1046
|
+
return '';
|
|
1047
|
+
const tr = await loadTranscript(f);
|
|
1048
|
+
if (!tr)
|
|
1049
|
+
return '';
|
|
1050
|
+
for (let i = tr.messages.length - 1; i >= 0; i--) {
|
|
1051
|
+
const m = tr.messages[i];
|
|
1052
|
+
if (m.role === 'assistant' && typeof m.content === 'string' && m.content.trim())
|
|
1053
|
+
return m.content;
|
|
1054
|
+
}
|
|
1055
|
+
return '';
|
|
1056
|
+
}
|
|
1057
|
+
catch {
|
|
1058
|
+
return '';
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
const sessions = [...new Set(labels.map((l) => l.session))];
|
|
1062
|
+
const tasks = new Map();
|
|
1063
|
+
const answers = new Map();
|
|
1064
|
+
for (const s2 of sessions) {
|
|
1065
|
+
tasks.set(s2, await taskOf(s2));
|
|
1066
|
+
answers.set(s2, await answerOf(s2));
|
|
1067
|
+
}
|
|
1068
|
+
const pairs = E.exportDpoPairs(labels, insights, (x2) => tasks.get(x2) ?? '', (x2) => answers.get(x2) ?? '');
|
|
1069
|
+
const out = join(home, 'evolution', 'dpo-pairs.jsonl');
|
|
1070
|
+
const { writeFile: wf } = await import('node:fs/promises');
|
|
1071
|
+
await wf(out, pairs.map((p) => JSON.stringify(p)).join('\n') + (pairs.length ? '\n' : ''), 'utf8');
|
|
1072
|
+
stdout.write('DPO pairs: ' + pairs.length + ' -> ' + out + '\n');
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
stdout.write('usage: hmh reward fit | report | export-dpo\n');
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
987
1078
|
if (cmd === 'dataset') {
|
|
988
1079
|
// V2 M10: trajectory -> versioned dataset (filter/dedupe/reward/split)
|
|
989
1080
|
await initHome();
|
package/dist/tui.d.ts
CHANGED
|
@@ -54,6 +54,16 @@ export declare const KEYMAP_DEFAULTS: Record<string, string>;
|
|
|
54
54
|
* wheel-down (+1), anything else (clicks, drags, plain keys) is 0.
|
|
55
55
|
*/
|
|
56
56
|
export declare function parseWheel(data: string): number;
|
|
57
|
+
/**
|
|
58
|
+
* SGR reports can split across stdin chunks (fast wheel bursts fragment in
|
|
59
|
+
* some terminals). If the buffer ends inside a report - a trailing
|
|
60
|
+
* '\x1b[<...' with no terminating M/m yet - slice it off as `pending` for
|
|
61
|
+
* the next chunk and hand back only the complete part.
|
|
62
|
+
*/
|
|
63
|
+
export declare function splitMouseReport(data: string): {
|
|
64
|
+
data: string;
|
|
65
|
+
pending: string;
|
|
66
|
+
};
|
|
57
67
|
export declare class TuiRuntime {
|
|
58
68
|
private entries;
|
|
59
69
|
private dirty;
|
|
@@ -87,15 +97,19 @@ export declare class TuiRuntime {
|
|
|
87
97
|
private modeTag;
|
|
88
98
|
/** rows for the `/model ` picker (configured providers first, set by driver) */
|
|
89
99
|
private modelChoices;
|
|
90
|
-
/** Wheel/click handling:
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
* selection
|
|
97
|
-
*
|
|
100
|
+
/** Wheel/click handling: SGR mouse reporting is ALWAYS on (enabled with
|
|
101
|
+
* ?1000h+?1006h at startup). Cross-terminal wheel support has to come
|
|
102
|
+
* from explicit reports: only some terminals translate the wheel to
|
|
103
|
+
* arrow keys on the alternate screen, so the wheel would silently do
|
|
104
|
+
* nothing on the others (conhost, several Linux terminals, the
|
|
105
|
+
* KaihongOS terminal). The wheel drives the transcript (or the open
|
|
106
|
+
* palette's selection); clicks only pick rows while a palette is open.
|
|
107
|
+
* Native select/copy stays available via Shift+click, the standard
|
|
108
|
+
* bypass in mainstream terminals. */
|
|
98
109
|
private mouseReported;
|
|
110
|
+
/** tail of an SGR mouse report split across stdin chunks (fast wheel
|
|
111
|
+
* bursts); prepended to the next chunk so parseWheel sees the sequence */
|
|
112
|
+
private mousePending;
|
|
99
113
|
/** screen row of each visible palette item (SGR click hit-testing); the
|
|
100
114
|
* render loop records row = frame.length (1-based) as it pushes rows */
|
|
101
115
|
private paletteClickRows;
|
|
@@ -171,10 +185,6 @@ export declare class TuiRuntime {
|
|
|
171
185
|
frameText: string;
|
|
172
186
|
};
|
|
173
187
|
setModeTag(tag: string): void;
|
|
174
|
-
/** Reporting is on ONLY while a palette is open. The palette is a modal:
|
|
175
|
-
* while it shows, clicks choose its rows and the wheel drives its
|
|
176
|
-
* selection; drag-select resumes the moment it closes. */
|
|
177
|
-
private syncMouseReporting;
|
|
178
188
|
/** The palette data source: `/model ` opens the model picker, otherwise
|
|
179
189
|
* slash commands. (/resume submits straight through to the driver, which
|
|
180
190
|
* opens the Codex-style full-frame picker - resume-picker.ts.) Rows are
|
package/dist/tui.js
CHANGED
|
@@ -228,6 +228,21 @@ export function parseWheel(data) {
|
|
|
228
228
|
return 1;
|
|
229
229
|
return 0;
|
|
230
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* SGR reports can split across stdin chunks (fast wheel bursts fragment in
|
|
233
|
+
* some terminals). If the buffer ends inside a report - a trailing
|
|
234
|
+
* '\x1b[<...' with no terminating M/m yet - slice it off as `pending` for
|
|
235
|
+
* the next chunk and hand back only the complete part.
|
|
236
|
+
*/
|
|
237
|
+
export function splitMouseReport(data) {
|
|
238
|
+
const cut = data.lastIndexOf('\x1b[<');
|
|
239
|
+
if (cut === -1)
|
|
240
|
+
return { data, pending: '' };
|
|
241
|
+
const tail = data.slice(cut);
|
|
242
|
+
return /^\x1b\[<\d+(;\d+)*[Mm]$/.test(tail)
|
|
243
|
+
? { data, pending: '' }
|
|
244
|
+
: { data: data.slice(0, cut), pending: tail };
|
|
245
|
+
}
|
|
231
246
|
export class TuiRuntime {
|
|
232
247
|
entries = [];
|
|
233
248
|
dirty = true;
|
|
@@ -261,15 +276,19 @@ export class TuiRuntime {
|
|
|
261
276
|
modeTag = '';
|
|
262
277
|
/** rows for the `/model ` picker (configured providers first, set by driver) */
|
|
263
278
|
modelChoices = [];
|
|
264
|
-
/** Wheel/click handling:
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
* selection
|
|
271
|
-
*
|
|
272
|
-
|
|
279
|
+
/** Wheel/click handling: SGR mouse reporting is ALWAYS on (enabled with
|
|
280
|
+
* ?1000h+?1006h at startup). Cross-terminal wheel support has to come
|
|
281
|
+
* from explicit reports: only some terminals translate the wheel to
|
|
282
|
+
* arrow keys on the alternate screen, so the wheel would silently do
|
|
283
|
+
* nothing on the others (conhost, several Linux terminals, the
|
|
284
|
+
* KaihongOS terminal). The wheel drives the transcript (or the open
|
|
285
|
+
* palette's selection); clicks only pick rows while a palette is open.
|
|
286
|
+
* Native select/copy stays available via Shift+click, the standard
|
|
287
|
+
* bypass in mainstream terminals. */
|
|
288
|
+
mouseReported = true;
|
|
289
|
+
/** tail of an SGR mouse report split across stdin chunks (fast wheel
|
|
290
|
+
* bursts); prepended to the next chunk so parseWheel sees the sequence */
|
|
291
|
+
mousePending = '';
|
|
273
292
|
/** screen row of each visible palette item (SGR click hit-testing); the
|
|
274
293
|
* render loop records row = frame.length (1-based) as it pushes rows */
|
|
275
294
|
paletteClickRows = [];
|
|
@@ -296,8 +315,10 @@ export class TuiRuntime {
|
|
|
296
315
|
constructor() {
|
|
297
316
|
// ?1l forces DECCKM OFF so arrow keys arrive as CSI (\x1b[A) even if a
|
|
298
317
|
// previous program left the terminal in application cursor mode - in
|
|
299
|
-
// that mode arrows arrive as SS3 (\x1bOA) and would be silently dropped
|
|
300
|
-
|
|
318
|
+
// that mode arrows arrive as SS3 (\x1bOA) and would be silently dropped.
|
|
319
|
+
// SGR mouse reporting on from the first frame: the wheel must scroll on
|
|
320
|
+
// every terminal, not only the ones that map wheel->arrows themselves.
|
|
321
|
+
stdout.write('\x1b[?1049h\x1b[?25l\x1b[2J\x1b[?1l\x1b[?1000h\x1b[?1006h');
|
|
301
322
|
stdin.setRawMode?.(true);
|
|
302
323
|
stdin.resume();
|
|
303
324
|
stdin.setEncoding('utf8');
|
|
@@ -545,16 +566,6 @@ export class TuiRuntime {
|
|
|
545
566
|
this.modeTag = tag;
|
|
546
567
|
this.dirty = true;
|
|
547
568
|
}
|
|
548
|
-
/** Reporting is on ONLY while a palette is open. The palette is a modal:
|
|
549
|
-
* while it shows, clicks choose its rows and the wheel drives its
|
|
550
|
-
* selection; drag-select resumes the moment it closes. */
|
|
551
|
-
syncMouseReporting() {
|
|
552
|
-
const want = this.panelItems(this.input).length > 0;
|
|
553
|
-
if (want === this.mouseReported)
|
|
554
|
-
return;
|
|
555
|
-
this.mouseReported = want;
|
|
556
|
-
stdout.write(want ? '\x1b[?1000h\x1b[?1006h' : '\x1b[?1000l\x1b[?1006l');
|
|
557
|
-
}
|
|
558
569
|
/** The palette data source: `/model ` opens the model picker, otherwise
|
|
559
570
|
* slash commands. (/resume submits straight through to the driver, which
|
|
560
571
|
* opens the Codex-style full-frame picker - resume-picker.ts.) Rows are
|
|
@@ -1032,6 +1043,18 @@ export class TuiRuntime {
|
|
|
1032
1043
|
return n;
|
|
1033
1044
|
}
|
|
1034
1045
|
onKey(data) {
|
|
1046
|
+
// reassemble an SGR mouse report that arrived split across stdin chunks
|
|
1047
|
+
// (fast wheel bursts fragment in some terminals) before anything parses
|
|
1048
|
+
if (this.mousePending) {
|
|
1049
|
+
data = this.mousePending + data;
|
|
1050
|
+
this.mousePending = '';
|
|
1051
|
+
}
|
|
1052
|
+
const split = splitMouseReport(data);
|
|
1053
|
+
if (split.pending)
|
|
1054
|
+
this.mousePending = split.pending;
|
|
1055
|
+
if (!split.data)
|
|
1056
|
+
return;
|
|
1057
|
+
data = split.data;
|
|
1035
1058
|
// SS3 application-mode arrows (\x1bOA…H): terminals left in DECCKM by a
|
|
1036
1059
|
// previous program send these; normalize to the CSI forms this UI
|
|
1037
1060
|
// matches so navigation never silently dies
|
|
@@ -1060,9 +1083,10 @@ export class TuiRuntime {
|
|
|
1060
1083
|
this.atPalKey(data);
|
|
1061
1084
|
return;
|
|
1062
1085
|
}
|
|
1063
|
-
// wheel-only mouse routing: 64 = wheel-up, 65 = wheel-down.
|
|
1064
|
-
//
|
|
1065
|
-
//
|
|
1086
|
+
// wheel-only mouse routing: 64 = wheel-up, 65 = wheel-down. Reporting
|
|
1087
|
+
// is always on, so every other report - click, drag, release - is just
|
|
1088
|
+
// swallowed below; the terminal's Shift+click native selection bypasses
|
|
1089
|
+
// the app entirely and keeps working.
|
|
1066
1090
|
const wheel = parseWheel(data);
|
|
1067
1091
|
if (wheel !== 0) {
|
|
1068
1092
|
// an open palette takes the wheel: it moves the selection (the list
|
|
@@ -1371,9 +1395,8 @@ export class TuiRuntime {
|
|
|
1371
1395
|
this.flushFrame(frame, H);
|
|
1372
1396
|
return;
|
|
1373
1397
|
}
|
|
1374
|
-
// modal mouse reporting follows the palette (see syncMouseReporting);
|
|
1375
1398
|
// click rows are re-recorded every frame because screen positions move
|
|
1376
|
-
|
|
1399
|
+
// (mouse reporting is always on - see the mouseReported field note)
|
|
1377
1400
|
this.paletteClickRows.length = 0;
|
|
1378
1401
|
const spin = '⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏'[this.spinnerFrame] ?? ' ';
|
|
1379
1402
|
// Header = pure identity strip: logo · model · cwd · skills (+ mode tag
|
package/package.json
CHANGED