@hncode/hncode 0.8.1 → 0.8.3
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/README.md +18 -0
- package/README_CN.md +14 -0
- package/bin/hncode +54 -8
- package/package.json +1 -1
- package/src/agent-task.js +356 -337
- package/src/agent.js +1465 -1405
- package/src/animate.js +56 -0
- package/src/colors.js +615 -593
- package/src/config.js +50 -7
- package/src/context-bar.js +280 -278
- package/src/doctor.js +4 -3
- package/src/file-tree.js +4 -3
- package/src/highlight.js +20 -6
- package/src/i18n.js +1432 -389
- package/src/image-row.js +11 -2
- package/src/index.js +218 -6
- package/src/keybindings.js +2 -1
- package/src/llm.js +146 -21
- package/src/log-file.js +112 -0
- package/src/markdown.js +646 -567
- package/src/mcp-server.js +409 -0
- package/src/mcp.js +279 -29
- package/src/memory-profile.js +19 -16
- package/src/migrate-index.js +175 -0
- package/src/migrate.js +392 -0
- package/src/panels.js +9 -7
- package/src/plan.js +7 -5
- package/src/plugin.js +37 -21
- package/src/registry-browser.js +17 -15
- package/src/rows-plan-bg.js +3 -2
- package/src/rows-status.js +20 -6
- package/src/rows-tool-result.js +242 -208
- package/src/screen-paint.js +153 -128
- package/src/session-recover.js +317 -0
- package/src/session.js +203 -13
- package/src/settings-schema.js +9 -0
- package/src/swarm-progress.js +6 -2
- package/src/tasks-browser.js +37 -36
- package/src/team-panel.js +12 -11
- package/src/term.js +57 -21
- package/src/text.js +254 -149
- package/src/tool-line.js +7 -2
- package/src/tool-result.js +400 -327
- package/src/tools/agent.js +7 -2
- package/src/tools/ask-user-question.js +1 -2
- package/src/tools/bash.js +446 -434
- package/src/tools/edit.js +653 -665
- package/src/tools/goal.js +130 -93
- package/src/tools/read.js +141 -133
- package/src/tools/tasks.js +214 -214
- package/src/tools/utils.js +143 -104
- package/src/tui.js +20887 -19942
package/src/log-file.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// A diagnostic log that cannot grow without limit.
|
|
2
|
+
//
|
|
3
|
+
// WHY THIS EXISTS
|
|
4
|
+
// ---------------
|
|
5
|
+
// Every log in this program appended forever: `paint.log` reached 174 MB, `keys.log`
|
|
6
|
+
// 7 MB, and `session-errors.log` grows by a line per failed save. On a machine with a
|
|
7
|
+
// small disk that is the difference between working and not, and none of it is ever
|
|
8
|
+
// read — a diagnostic log is consulted after a problem, not during one.
|
|
9
|
+
//
|
|
10
|
+
// ROTATION IS BY SIZE, CHECKED ON THE FILE'S OWN LENGTH.
|
|
11
|
+
//
|
|
12
|
+
// The check has to be cheap enough to sit on a paint path: `statSync` measured 33 µs
|
|
13
|
+
// here. That is paid per traced frame only when tracing is on, which it is not by
|
|
14
|
+
// default; on the exit and save paths it is paid once per event.
|
|
15
|
+
//
|
|
16
|
+
// The rotated file keeps ONE generation, `<name>.1`. A debug log is read immediately
|
|
17
|
+
// after the event that wrote it, so a second generation would only preserve the moment
|
|
18
|
+
// before the one being investigated, at twice the disk.
|
|
19
|
+
//
|
|
20
|
+
// A CONCURRENT WRITER LOSES NOTHING. The append is a single `appendFileSync`, which is
|
|
21
|
+
// O_APPEND and therefore atomic per record, so two hncode instances sharing one log
|
|
22
|
+
// interleave records rather than overwrite each other. Only the ROTATION can race, and
|
|
23
|
+
// the loser's rename fails harmlessly — that record is still appended, one rotation late.
|
|
24
|
+
|
|
25
|
+
import fs from 'node:fs';
|
|
26
|
+
import path from 'node:path';
|
|
27
|
+
|
|
28
|
+
/** Default ceiling for a diagnostic log. */
|
|
29
|
+
export const DEFAULT_MAX_BYTES = 5 * 1024 * 1024;
|
|
30
|
+
|
|
31
|
+
// The size is remembered between writes so a burst costs one `statSync` instead of one
|
|
32
|
+
// per record. Another process growing the same file makes the value stale, which delays
|
|
33
|
+
// a rotation by that growth — for a shared file the ceiling is best-effort, never a
|
|
34
|
+
// correctness rule.
|
|
35
|
+
const sizeCache = new Map(); // file -> bytes
|
|
36
|
+
|
|
37
|
+
/** Current size of `file`, from the cache when this process wrote it last. */
|
|
38
|
+
function currentSize(file) {
|
|
39
|
+
const known = sizeCache.get(file);
|
|
40
|
+
if (known !== undefined) return known;
|
|
41
|
+
try { return fs.statSync(file).size; } catch { return 0; }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Rotate `file` to `file.1` when it is at or past `maxBytes`, keeping one generation.
|
|
46
|
+
*
|
|
47
|
+
* Exported for a caller that wants the check on its own, for content it did not build as
|
|
48
|
+
* a single string.
|
|
49
|
+
*/
|
|
50
|
+
export function rotateIfLarge(file, maxBytes = DEFAULT_MAX_BYTES) {
|
|
51
|
+
if (currentSize(file) < maxBytes) return false;
|
|
52
|
+
try {
|
|
53
|
+
fs.renameSync(file, `${file}.1`);
|
|
54
|
+
sizeCache.set(file, 0);
|
|
55
|
+
return true;
|
|
56
|
+
} catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Append `text` to `file`, rotating first when the record would pass `maxBytes`.
|
|
63
|
+
*
|
|
64
|
+
* A record larger than the whole ceiling becomes the ONLY content of the rotated file.
|
|
65
|
+
* Truncating it instead would cut the stack trace that made it worth recording, and that
|
|
66
|
+
* one record is the reason the log is worth having.
|
|
67
|
+
*
|
|
68
|
+
* Rotation happens BEFORE the append, so the live file never grows past the ceiling and
|
|
69
|
+
* always exists. Rotating afterwards left no live file at all until the next write.
|
|
70
|
+
*
|
|
71
|
+
* Returns true when the bytes landed. NEVER THROWS: a diagnostic log is not worth the
|
|
72
|
+
* failure it would add to the path that was doing the logging.
|
|
73
|
+
*/
|
|
74
|
+
export function appendLog(file, text, { maxBytes = DEFAULT_MAX_BYTES } = {}) {
|
|
75
|
+
if (text == null) return false;
|
|
76
|
+
const data = String(text);
|
|
77
|
+
const grow = Buffer.byteLength(data);
|
|
78
|
+
try {
|
|
79
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
80
|
+
if (grow >= maxBytes) {
|
|
81
|
+
// The oversized record becomes the rotated file in one step, and the live log is
|
|
82
|
+
// left present but empty: a reader must never find it MISSING, which would look
|
|
83
|
+
// like logging was broken rather than rotated.
|
|
84
|
+
fs.writeFileSync(`${file}.1`, data, 'utf8');
|
|
85
|
+
fs.writeFileSync(file, '', 'utf8');
|
|
86
|
+
sizeCache.set(file, 0);
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
let size = currentSize(file);
|
|
90
|
+
if (size > 0 && size + grow > maxBytes) {
|
|
91
|
+
try {
|
|
92
|
+
fs.renameSync(file, `${file}.1`);
|
|
93
|
+
size = 0;
|
|
94
|
+
sizeCache.set(file, 0);
|
|
95
|
+
} catch {
|
|
96
|
+
// Another reader holds the file open. Keep the record and let a later attempt
|
|
97
|
+
// rotate; losing the write to win the rotation would be the wrong trade.
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
fs.appendFileSync(file, data, 'utf8');
|
|
101
|
+
sizeCache.set(file, size + grow);
|
|
102
|
+
return true;
|
|
103
|
+
} catch {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Forget the cached size for one file, or for all of them when no file is given. */
|
|
109
|
+
export function forgetLogSize(file) {
|
|
110
|
+
if (file === undefined) { sizeCache.clear(); return; }
|
|
111
|
+
sizeCache.delete(file);
|
|
112
|
+
}
|