@hncode/hncode 0.8.0 → 0.8.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.
- package/README.md +18 -0
- package/README_CN.md +14 -0
- package/package.json +1 -1
- package/src/agent-task.js +354 -337
- package/src/agent.js +19 -3
- package/src/animate.js +35 -0
- package/src/clipboard.js +54 -5
- package/src/config.js +25 -7
- package/src/file-tree.js +4 -3
- package/src/highlight.js +20 -6
- package/src/i18n.js +825 -0
- package/src/image-row.js +11 -2
- package/src/index.js +730 -610
- package/src/llm.js +146 -21
- package/src/log-file.js +112 -0
- package/src/markdown.js +619 -567
- package/src/migrate-index.js +175 -0
- package/src/migrate.js +392 -0
- package/src/panels.js +9 -7
- package/src/rows-plan-bg.js +3 -2
- package/src/rows-status.js +20 -6
- package/src/rows-tool-result.js +35 -8
- package/src/screen-paint.js +153 -128
- package/src/session-recover.js +317 -0
- package/src/session.js +203 -13
- package/src/swarm-progress.js +5 -1
- package/src/tasks-browser.js +9 -8
- package/src/team-panel.js +12 -11
- package/src/text.js +192 -149
- package/src/tool-line.js +7 -2
- package/src/tool-result.js +400 -327
- package/src/tools/ask-user-question.js +1 -2
- package/src/tools/bash.js +23 -11
- package/src/tools/edit.js +640 -665
- package/src/tools/goal.js +130 -93
- package/src/tools/read.js +141 -133
- package/src/tools/shell.js +28 -2
- package/src/tools/tasks.js +214 -214
- package/src/tools/utils.js +143 -104
- package/src/tui.js +20689 -19943
package/src/tools/goal.js
CHANGED
|
@@ -1,93 +1,130 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The Goal tool: the agent's own objective across turns.
|
|
3
|
-
*
|
|
4
|
-
* A turn ends when the model stops. Some work does not fit in one turn — a refactor across a dozen files, a
|
|
5
|
-
* migration, a suite that needs several rounds — and without a goal the agent simply stops halfway and waits
|
|
6
|
-
* to be told to continue. The goal makes that explicit: a line the agent wrote for itself, shown in the status
|
|
7
|
-
* panel, and the turn loop restarts the agent while one is set.
|
|
8
|
-
*
|
|
9
|
-
* ONE STORE, NOT TWO. The objective lives in the TUI's `state.objective` — the same field `/goal` writes — and
|
|
10
|
-
* this module only borrows it through a listener. A second copy here would drift the moment the user typed
|
|
11
|
-
* `/goal cancel`, and the loop would restart against an objective nobody could see.
|
|
12
|
-
*
|
|
13
|
-
* THREE ACTIONS:
|
|
14
|
-
*
|
|
15
|
-
* set Start or replace the objective.
|
|
16
|
-
* status Read it back, so a resumed turn can re-read what it was doing.
|
|
17
|
-
* clear Finish. Nothing restarts after this, which is why it has to be deliberate.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
/** Read the goal. Installed by startTUI, backed by `state.objective`. */
|
|
21
|
-
let readGoal = () => '';
|
|
22
|
-
/** Write the goal. Installed by startTUI. */
|
|
23
|
-
let writeGoal = () => {};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
*
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The Goal tool: the agent's own objective across turns.
|
|
3
|
+
*
|
|
4
|
+
* A turn ends when the model stops. Some work does not fit in one turn — a refactor across a dozen files, a
|
|
5
|
+
* migration, a suite that needs several rounds — and without a goal the agent simply stops halfway and waits
|
|
6
|
+
* to be told to continue. The goal makes that explicit: a line the agent wrote for itself, shown in the status
|
|
7
|
+
* panel, and the turn loop restarts the agent while one is set.
|
|
8
|
+
*
|
|
9
|
+
* ONE STORE, NOT TWO. The objective lives in the TUI's `state.objective` — the same field `/goal` writes — and
|
|
10
|
+
* this module only borrows it through a listener. A second copy here would drift the moment the user typed
|
|
11
|
+
* `/goal cancel`, and the loop would restart against an objective nobody could see.
|
|
12
|
+
*
|
|
13
|
+
* THREE ACTIONS:
|
|
14
|
+
*
|
|
15
|
+
* set Start or replace the objective.
|
|
16
|
+
* status Read it back, so a resumed turn can re-read what it was doing.
|
|
17
|
+
* clear Finish. Nothing restarts after this, which is why it has to be deliberate.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** Read the goal. Installed by startTUI, backed by `state.objective`. */
|
|
21
|
+
let readGoal = () => '';
|
|
22
|
+
/** Write the goal. Installed by startTUI. */
|
|
23
|
+
let writeGoal = () => {};
|
|
24
|
+
/**
|
|
25
|
+
* Who set the goal: 'user' or 'agent'.
|
|
26
|
+
*
|
|
27
|
+
* THE DISTINCTION IS THE POINT. The user's `/goal set` and the agent's `Goal {action:'set'}` write the same field, so
|
|
28
|
+
* without this the agent could silently REPLACE the objective the person gave it — and worse, `Goal clear` could wipe an
|
|
29
|
+
* objective the user was still working towards, which stops the loop for a goal nobody has met.
|
|
30
|
+
*
|
|
31
|
+
* A goal the USER set is not the agent's to change. The tool refuses both `set` and `clear` against one.
|
|
32
|
+
*/
|
|
33
|
+
/** Set by installGoalStore; reports who owns the objective currently stored. */
|
|
34
|
+
let readOwner = () => 'agent';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {{get: () => string, set: (goal: string, owner?: 'user'|'agent') => void, owner?: () => string}} api
|
|
38
|
+
*/
|
|
39
|
+
export function installGoalStore(api) {
|
|
40
|
+
if (api && typeof api.get === 'function') readGoal = api.get;
|
|
41
|
+
if (api && typeof api.set === 'function') writeGoal = api.set;
|
|
42
|
+
if (api && typeof api.owner === 'function') readOwner = api.owner;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** The current objective, or ''. */
|
|
46
|
+
export function getGoal() {
|
|
47
|
+
try { return String(readGoal() || '').trim(); } catch { return ''; }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Who set the current objective. */
|
|
51
|
+
export function getGoalOwner() {
|
|
52
|
+
try { return String(readOwner() || 'agent'); } catch { return 'agent'; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const spec = {
|
|
56
|
+
name: 'Goal',
|
|
57
|
+
description: [
|
|
58
|
+
'Set, read or clear the objective you are working towards across turns.',
|
|
59
|
+
'',
|
|
60
|
+
'A turn ends when you stop responding. While a goal is set the agent restarts you with it quoted back, so',
|
|
61
|
+
'use it for work that genuinely needs several turns — not for something you can finish now.',
|
|
62
|
+
'',
|
|
63
|
+
'- set: start or replace it. Write a finishable outcome ("all callers of X migrated and the suite passes"),',
|
|
64
|
+
' not an activity ("work on X").',
|
|
65
|
+
'- status: read it back.',
|
|
66
|
+
'- clear: you are done. Call this the moment the goal is met; otherwise the loop restarts you again.',
|
|
67
|
+
'',
|
|
68
|
+
'The user can set the same goal with /goal and can pause it. Never clear a goal you have not met.',
|
|
69
|
+
].join('\n'),
|
|
70
|
+
parameters: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {
|
|
73
|
+
action: {
|
|
74
|
+
type: 'string',
|
|
75
|
+
enum: ['set', 'status', 'clear'],
|
|
76
|
+
description: 'set = start or replace, status = read back, clear = finished.',
|
|
77
|
+
},
|
|
78
|
+
goal: {
|
|
79
|
+
type: 'string',
|
|
80
|
+
description: 'The objective. Required for `set`; a finishable outcome, not an activity.',
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
required: ['action'],
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {{action: string, goal?: string}} args
|
|
88
|
+
* @returns {string}
|
|
89
|
+
*/
|
|
90
|
+
execute(args) {
|
|
91
|
+
const action = String((args && args.action) || '').toLowerCase();
|
|
92
|
+
const owner = getGoalOwner();
|
|
93
|
+
if (action === 'set') {
|
|
94
|
+
const goal = String((args && args.goal) || '').trim();
|
|
95
|
+
if (!goal) return 'Error: `set` needs a goal — the outcome you are working towards.';
|
|
96
|
+
// THE USER'S GOAL IS NOT YOURS TO REPLACE.
|
|
97
|
+
//
|
|
98
|
+
// `/goal set` and this tool write the same field, so without this check a `set` silently overwrote whatever the
|
|
99
|
+
// person had asked for — and the loop then restarted against an objective they never wrote and could not see.
|
|
100
|
+
// Refused rather than merged: two objectives are not one goal.
|
|
101
|
+
const current = getGoal();
|
|
102
|
+
if (current && owner === 'user') {
|
|
103
|
+
return `Refused: the user set this goal and it is still active.\n\nGoal: ${current}\n\n`
|
|
104
|
+
+ 'Work towards it. Do not replace it — the user changes it with /goal, and only they can clear it.';
|
|
105
|
+
}
|
|
106
|
+
try { writeGoal(goal, 'agent'); } catch { return 'Error: could not store the goal.'; }
|
|
107
|
+
return `Goal set: ${goal}\nThe turn loop restarts you with this until you call Goal with action clear.`;
|
|
108
|
+
}
|
|
109
|
+
if (action === 'status') {
|
|
110
|
+
const goal = getGoal();
|
|
111
|
+
return goal ? `Goal: ${goal}${owner === 'user' ? ' (set by the user)' : ''}` : 'No goal is set.';
|
|
112
|
+
}
|
|
113
|
+
if (action === 'clear') {
|
|
114
|
+
const goal = getGoal();
|
|
115
|
+
if (!goal) return 'No goal to clear.';
|
|
116
|
+
// CLEARING SOMEONE ELSE'S GOAL STOPS A LOOP THAT WAS NOT YOURS.
|
|
117
|
+
//
|
|
118
|
+
// The user set an objective and is waiting for it. Reporting it done resets `state.objective`, so the turn loop
|
|
119
|
+
// stops restarting — the work is abandoned mid-way and nothing says so. Tell the user what was finished instead;
|
|
120
|
+
// they decide whether the goal is met.
|
|
121
|
+
if (owner === 'user') {
|
|
122
|
+
return `Not cleared: this goal was set by the user, so only they can finish it.\n\nGoal: ${goal}\n\n`
|
|
123
|
+
+ 'Report what you completed and stop. The user will clear the goal, or tell you to continue.';
|
|
124
|
+
}
|
|
125
|
+
try { writeGoal('', 'agent'); } catch { return 'Error: could not clear the goal.'; }
|
|
126
|
+
return `Goal cleared: ${goal}\nNothing will restart this turn now.`;
|
|
127
|
+
}
|
|
128
|
+
return `Error: unknown action "${args && args.action}". Use set, status or clear.`;
|
|
129
|
+
},
|
|
130
|
+
};
|
package/src/tools/read.js
CHANGED
|
@@ -1,133 +1,141 @@
|
|
|
1
|
-
// Read tool — mirrors the hncode Read tool schema & behavior.
|
|
2
|
-
|
|
3
|
-
import fs from 'node:fs';
|
|
4
|
-
import { resolvePath, isProbablyTextFile } from './utils.js';
|
|
5
|
-
|
|
6
|
-
// Normalize newlines so a text file's hash is stable regardless of CRLF/LF.
|
|
7
|
-
export function normalizeText(s) {
|
|
8
|
-
return String(s).split(/\r\n|\r|\n/).join('\n');
|
|
9
|
-
}
|
|
10
|
-
// FNV-1a-ish 32-bit hash of a string (cheap, deterministic).
|
|
11
|
-
export function hashStr(s) {
|
|
12
|
-
const str = normalizeText(s);
|
|
13
|
-
let h = 0x811c9dc5;
|
|
14
|
-
for (let i = 0; i < str.length; i++) {
|
|
15
|
-
h ^= str.charCodeAt(i);
|
|
16
|
-
h = (h * 0x01000193) >>> 0;
|
|
17
|
-
}
|
|
18
|
-
return h.toString(36);
|
|
19
|
-
}
|
|
20
|
-
// Hash the exact <start..end> 1-based line slice of `lines`.
|
|
21
|
-
function hashLines(lines, start, end) {
|
|
22
|
-
const slice = (lines.slice(start - 1, end) || []).join('\n');
|
|
23
|
-
return hashStr(slice);
|
|
24
|
-
}
|
|
25
|
-
// Public alias so edit.js (and the line-range path) share ONE implementation.
|
|
26
|
-
export const hashRegion = hashLines;
|
|
27
|
-
|
|
28
|
-
// Reset the Read snapshot pool after files were rewound on disk. The pool holds
|
|
29
|
-
// per-file content hashes plus the line regions the model has "seen"; once /undo
|
|
30
|
-
// has put an older version back, every hash in it describes content that is no
|
|
31
|
-
// longer there. Keeping them made the NEXT Edit fail with a staleness rejection
|
|
32
|
-
// against a file the model had just watched being restored.
|
|
33
|
-
export function forgetReadPool(ctx) {
|
|
34
|
-
if (ctx && ctx.readPool && typeof ctx.readPool.clear === 'function') ctx.readPool.clear();
|
|
35
|
-
}
|
|
36
|
-
export const spec = {
|
|
37
|
-
name: 'Read',
|
|
38
|
-
description: 'Read a text file. With no line arguments it returns the whole file; pass start_line and '
|
|
39
|
-
+ 'end_line for a range. BOTH BOUNDS ARE INCLUSIVE: start_line=3, end_line=5 returns THREE lines — 3, 4 '
|
|
40
|
-
+ 'and 5. A negative start_line counts from the end (-100 = the last 100 lines). Every returned line is '
|
|
41
|
-
+ 'prefixed with its 1-based number.',
|
|
42
|
-
parameters: {
|
|
43
|
-
type: 'object',
|
|
44
|
-
properties: {
|
|
45
|
-
path: { type: 'string', description: 'Path to the text file.' },
|
|
46
|
-
start_line: {
|
|
47
|
-
type: 'integer',
|
|
48
|
-
description: '1-based FIRST line to return, INCLUSIVE — it is a line that is returned, not the line '
|
|
49
|
-
+ 'before the first one. Omit to start at the first line, or pass a negative value to count from the '
|
|
50
|
-
+ 'end (-100 = last 100 lines).',
|
|
51
|
-
},
|
|
52
|
-
end_line: { type: 'integer', minimum: 1, description: '1-based LAST line to return, INCLUSIVE — it is a line that is returned, not the line after the last one. start_line=3, end_line=5 returns lines 3, 4 and 5. Omit to read to the end of the file.' },
|
|
53
|
-
},
|
|
54
|
-
required: ['path'],
|
|
55
|
-
},
|
|
56
|
-
async execute(args, ctx) {
|
|
57
|
-
// A missing `path` used to reach resolvePath(undefined), which resolves to the
|
|
58
|
-
// workspace ROOT — so the failure surfaced as an EISDIR from fs.readFileSync
|
|
59
|
-
// instead of "you forgot the path". Cheap to check, and it tells the model
|
|
60
|
-
// exactly what to fix.
|
|
61
|
-
if (typeof args.path !== 'string' || !args.path.trim()) {
|
|
62
|
-
return 'Error: `path` is required and must be a non-empty string.';
|
|
63
|
-
}
|
|
64
|
-
let p;
|
|
65
|
-
try { p = resolvePath(args.path, ctx); } catch (e) { return e.message; }
|
|
66
|
-
let buf;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
//
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
1
|
+
// Read tool — mirrors the hncode Read tool schema & behavior.
|
|
2
|
+
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import { resolvePath, isProbablyTextFile, similarFileHint } from './utils.js';
|
|
5
|
+
|
|
6
|
+
// Normalize newlines so a text file's hash is stable regardless of CRLF/LF.
|
|
7
|
+
export function normalizeText(s) {
|
|
8
|
+
return String(s).split(/\r\n|\r|\n/).join('\n');
|
|
9
|
+
}
|
|
10
|
+
// FNV-1a-ish 32-bit hash of a string (cheap, deterministic).
|
|
11
|
+
export function hashStr(s) {
|
|
12
|
+
const str = normalizeText(s);
|
|
13
|
+
let h = 0x811c9dc5;
|
|
14
|
+
for (let i = 0; i < str.length; i++) {
|
|
15
|
+
h ^= str.charCodeAt(i);
|
|
16
|
+
h = (h * 0x01000193) >>> 0;
|
|
17
|
+
}
|
|
18
|
+
return h.toString(36);
|
|
19
|
+
}
|
|
20
|
+
// Hash the exact <start..end> 1-based line slice of `lines`.
|
|
21
|
+
function hashLines(lines, start, end) {
|
|
22
|
+
const slice = (lines.slice(start - 1, end) || []).join('\n');
|
|
23
|
+
return hashStr(slice);
|
|
24
|
+
}
|
|
25
|
+
// Public alias so edit.js (and the line-range path) share ONE implementation.
|
|
26
|
+
export const hashRegion = hashLines;
|
|
27
|
+
|
|
28
|
+
// Reset the Read snapshot pool after files were rewound on disk. The pool holds
|
|
29
|
+
// per-file content hashes plus the line regions the model has "seen"; once /undo
|
|
30
|
+
// has put an older version back, every hash in it describes content that is no
|
|
31
|
+
// longer there. Keeping them made the NEXT Edit fail with a staleness rejection
|
|
32
|
+
// against a file the model had just watched being restored.
|
|
33
|
+
export function forgetReadPool(ctx) {
|
|
34
|
+
if (ctx && ctx.readPool && typeof ctx.readPool.clear === 'function') ctx.readPool.clear();
|
|
35
|
+
}
|
|
36
|
+
export const spec = {
|
|
37
|
+
name: 'Read',
|
|
38
|
+
description: 'Read a text file. With no line arguments it returns the whole file; pass start_line and '
|
|
39
|
+
+ 'end_line for a range. BOTH BOUNDS ARE INCLUSIVE: start_line=3, end_line=5 returns THREE lines — 3, 4 '
|
|
40
|
+
+ 'and 5. A negative start_line counts from the end (-100 = the last 100 lines). Every returned line is '
|
|
41
|
+
+ 'prefixed with its 1-based number.',
|
|
42
|
+
parameters: {
|
|
43
|
+
type: 'object',
|
|
44
|
+
properties: {
|
|
45
|
+
path: { type: 'string', description: 'Path to the text file.' },
|
|
46
|
+
start_line: {
|
|
47
|
+
type: 'integer',
|
|
48
|
+
description: '1-based FIRST line to return, INCLUSIVE — it is a line that is returned, not the line '
|
|
49
|
+
+ 'before the first one. Omit to start at the first line, or pass a negative value to count from the '
|
|
50
|
+
+ 'end (-100 = last 100 lines).',
|
|
51
|
+
},
|
|
52
|
+
end_line: { type: 'integer', minimum: 1, description: '1-based LAST line to return, INCLUSIVE — it is a line that is returned, not the line after the last one. start_line=3, end_line=5 returns lines 3, 4 and 5. Omit to read to the end of the file.' },
|
|
53
|
+
},
|
|
54
|
+
required: ['path'],
|
|
55
|
+
},
|
|
56
|
+
async execute(args, ctx) {
|
|
57
|
+
// A missing `path` used to reach resolvePath(undefined), which resolves to the
|
|
58
|
+
// workspace ROOT — so the failure surfaced as an EISDIR from fs.readFileSync
|
|
59
|
+
// instead of "you forgot the path". Cheap to check, and it tells the model
|
|
60
|
+
// exactly what to fix.
|
|
61
|
+
if (typeof args.path !== 'string' || !args.path.trim()) {
|
|
62
|
+
return 'Error: `path` is required and must be a non-empty string.';
|
|
63
|
+
}
|
|
64
|
+
let p;
|
|
65
|
+
try { p = resolvePath(args.path, ctx); } catch (e) { return e.message; }
|
|
66
|
+
let buf;
|
|
67
|
+
// A MISS TYPED PATH IS THE CHEAPEST MISTAKE TO FIX AND THE MOST EXPENSIVE TO PAY FOR.
|
|
68
|
+
// `ENOENT` alone sends the model into a directory listing to discover that `pakage.json` was meant to be
|
|
69
|
+
// `package.json` — a whole extra round trip for one character. The hint names what is actually there.
|
|
70
|
+
try {
|
|
71
|
+
buf = fs.readFileSync(p);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
const hint = e && e.code === 'ENOENT' ? similarFileHint(args.path, p) : '';
|
|
74
|
+
return `Error reading ${args.path}: ${e.message}${hint}`;
|
|
75
|
+
}
|
|
76
|
+
if (!isProbablyTextFile(buf)) {
|
|
77
|
+
return `Cannot read: ${args.path} appears to be a binary file (use a tool suited for binary).`;
|
|
78
|
+
}
|
|
79
|
+
let st = null;
|
|
80
|
+
try { st = fs.statSync(p); } catch { /* stat is advisory only */ }
|
|
81
|
+
// NO truncation: the caller asked for the file, so the whole file is returned
|
|
82
|
+
// (line ranges still work via line_offset / n_lines). The old 1000-line and
|
|
83
|
+
// 128 KB caps silently cut the content, which also corrupted the
|
|
84
|
+
// staleness hashes recorded below because they were computed from the
|
|
85
|
+
// TRUNCATED text rather than the file on disk.
|
|
86
|
+
const text = buf.toString('utf8');
|
|
87
|
+
// A FILE ENDING IN A NEWLINE HAS ONE FEWER LINE THAN `split` REPORTS.
|
|
88
|
+
//
|
|
89
|
+
// `"a\nb\n".split("\n")` is `["a", "b", ""]` — the trailing newline leaves an empty element, and
|
|
90
|
+
// counting it made a ten-line file report `lines 1-11 of 11`. The numbers on each line were right;
|
|
91
|
+
// the TOTAL was one too high, so a caller planning an edit at the end of the file aimed at a row that
|
|
92
|
+
// does not exist.
|
|
93
|
+
//
|
|
94
|
+
// Only the element the trailing newline produced is dropped: a file that genuinely ends with a blank
|
|
95
|
+
// line (`"a\n\n"`) keeps it, because that is real content.
|
|
96
|
+
const lines = normalizeText(text).split("\n");
|
|
97
|
+
if (lines.length > 1 && lines[lines.length - 1] === "") lines.pop();
|
|
98
|
+
const total = lines.length;
|
|
99
|
+
// Default: every line. `start_line` / `end_line` narrow the window, and either may be omitted.
|
|
100
|
+
let start = args.start_line !== undefined ? Number(args.start_line) : 1;
|
|
101
|
+
if (start < 0) start = Math.max(1, total + 1 + start);
|
|
102
|
+
start = Math.max(1, Math.min(start, total));
|
|
103
|
+
const end = args.end_line !== undefined
|
|
104
|
+
? Math.min(Math.max(start, Number(args.end_line)), total)
|
|
105
|
+
: total;
|
|
106
|
+
let out = '';
|
|
107
|
+
out += `${args.path} (lines ${start}-${end} of ${total})\n`;
|
|
108
|
+
// THE NUMBER IS LEFT-ALIGNED AND PADDED ON THE RIGHT, not the other way round. `padStart` put the padding in
|
|
109
|
+
// FRONT, so `1` arrived as `" 1"` and the gutter read as right-aligned. The tab after it is what aligns the
|
|
110
|
+
// code, so all a number needs is to start in column 0.
|
|
111
|
+
for (let i = start; i <= end; i++) out += `${i}\t${lines[i - 1]}\n`;
|
|
112
|
+
// No trailing "truncated" banner: without an explicit end_line every line is returned, so the only way
|
|
113
|
+
// to stop early is a deliberate range request — the header already states which lines those were.
|
|
114
|
+
|
|
115
|
+
// --- Record a hash fingerprint so a later Edit can verify the AI is
|
|
116
|
+
// editing against the same content it read (snippet-staleness guard). ---
|
|
117
|
+
// Store the FULL file's normalized content hash plus this call's line
|
|
118
|
+
// range + region hash. Edit compares old_string's location against these.
|
|
119
|
+
if (ctx) {
|
|
120
|
+
const pool = (ctx.readPool = ctx.readPool || new Map());
|
|
121
|
+
let entry = pool.get(p);
|
|
122
|
+
if (!entry) {
|
|
123
|
+
entry = {
|
|
124
|
+
mtimeMs: st ? st.mtimeMs : 0,
|
|
125
|
+
size: st ? st.size : 0,
|
|
126
|
+
fileHash: hashStr(text),
|
|
127
|
+
lines: total, // current total line count at read time
|
|
128
|
+
regions: [], // {start, end, hash} of each read slice
|
|
129
|
+
};
|
|
130
|
+
pool.set(p, entry);
|
|
131
|
+
}
|
|
132
|
+
entry.mtimeMs = st ? st.mtimeMs : entry.mtimeMs;
|
|
133
|
+
entry.size = st ? st.size : entry.size;
|
|
134
|
+
entry.fileHash = hashStr(text);
|
|
135
|
+
entry.lines = total;
|
|
136
|
+
// Record this call's exact region (only the lines actually returned).
|
|
137
|
+
entry.regions.push({ start, end, hash: hashLines(lines, start, end) });
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
},
|
|
141
|
+
};
|
package/src/tools/shell.js
CHANGED
|
@@ -69,6 +69,25 @@ function whichSync(name) {
|
|
|
69
69
|
* cmd's, not PowerShell's.
|
|
70
70
|
*/
|
|
71
71
|
export function detectShell() {
|
|
72
|
+
// POSIX FIRST, AND ON ITS OWN PATH.
|
|
73
|
+
//
|
|
74
|
+
// The PowerShell probe used to run on EVERY platform, so on Linux or macOS it looked for `pwsh`, then
|
|
75
|
+
// `powershell`, then `cmd.exe`, found none of them, and reported "install PowerShell 7+" — advice that is wrong on
|
|
76
|
+
// a machine whose shell has been there all along. A Unix box is never looking for PowerShell and a Windows box is
|
|
77
|
+
// never looking for bash, so the two branches do not overlap at all.
|
|
78
|
+
if (process.platform !== 'win32') {
|
|
79
|
+
// `$SHELL` is the user's own choice and is what a terminal would use. `/bin/sh` is the floor: it is on every
|
|
80
|
+
// POSIX system, so the tool survives a stripped PATH the same way `cmd.exe` does on Windows.
|
|
81
|
+
const fromEnv = process.env.SHELL ? whichSync(process.env.SHELL) : null;
|
|
82
|
+
if (fromEnv) return { path: fromEnv, name: fromEnv.split('/').pop(), edition: 'posix' };
|
|
83
|
+
for (const name of ['bash', 'zsh', 'sh']) {
|
|
84
|
+
const path = whichSync(name);
|
|
85
|
+
if (path) return { path, name, edition: 'posix' };
|
|
86
|
+
}
|
|
87
|
+
const sh = fs.existsSync('/bin/sh') ? '/bin/sh' : null;
|
|
88
|
+
return sh ? { path: sh, name: 'sh', edition: 'posix' } : null;
|
|
89
|
+
}
|
|
90
|
+
|
|
72
91
|
for (const name of SHELL_CANDIDATES) {
|
|
73
92
|
const path = whichSync(name);
|
|
74
93
|
if (path) {
|
|
@@ -128,8 +147,9 @@ export function shellSpawnArgs(command, opts = {}) {
|
|
|
128
147
|
const shell = getShell();
|
|
129
148
|
if (!shell) {
|
|
130
149
|
throw new Error(
|
|
131
|
-
|
|
132
|
-
|
|
150
|
+
process.platform === 'win32'
|
|
151
|
+
? 'No shell found (tried pwsh, powershell, and cmd.exe). Install PowerShell 7+ or add it to PATH, then retry.'
|
|
152
|
+
: 'No shell found (tried $SHELL, bash, zsh and sh). Install a POSIX shell, then retry.',
|
|
133
153
|
);
|
|
134
154
|
}
|
|
135
155
|
if (shell.edition === 'cmd') {
|
|
@@ -138,6 +158,12 @@ export function shellSpawnArgs(command, opts = {}) {
|
|
|
138
158
|
// quoting and we do not wrap it ourselves.
|
|
139
159
|
return { bin: shell.path, args: ['/d', '/s', '/c', command], shell };
|
|
140
160
|
}
|
|
161
|
+
if (shell.edition === 'posix') {
|
|
162
|
+
// `-c` takes the command as one string, which is what every POSIX shell expects. There is no `-NoProfile`
|
|
163
|
+
// equivalent and none is wanted: a user's rc file is part of their environment, and the Windows flags exist to
|
|
164
|
+
// suppress a PowerShell banner that has no counterpart here.
|
|
165
|
+
return { bin: shell.path, args: ['-c', command], shell };
|
|
166
|
+
}
|
|
141
167
|
const args = ['-NoProfile'];
|
|
142
168
|
if (!opts.interactive) args.push('-NonInteractive');
|
|
143
169
|
args.push('-Command', command);
|