@yanzc/claude-pro 0.7.8 → 0.9.0

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/src/tools/bash.js CHANGED
@@ -3,12 +3,14 @@
3
3
  import fs from 'node:fs';
4
4
  import os from 'node:os';
5
5
  import path from 'node:path';
6
- import { findShell, killTree, spawnShell, watchShell } from '../utils/shell.js';
6
+ import { findShell, killTree, killTreeSync, spawnShell, watchShell } from '../utils/shell.js';
7
7
  import { stripAnsi } from '../ui/ansi.js';
8
+ import { OutputDecoder } from '../utils/encoding.js';
8
9
  import { toolResultsDir } from '../session.js';
9
10
 
10
11
  const background = new Map(); // id -> {child, command, output, readPos, status, exitCode, startedAt}
11
12
  let bgCounter = 0;
13
+ const PROGRESS_CHARS = 8192; // the end of the output the live view gets while a command runs
12
14
 
13
15
  function truncateOutput(text, max) {
14
16
  if (text.length <= max) return text;
@@ -53,7 +55,7 @@ function childEnv() {
53
55
  }
54
56
 
55
57
  export function shutdownBackgroundShells() {
56
- for (const bg of background.values()) if (bg.status === 'running') killTree(bg.child);
58
+ for (const bg of background.values()) if (bg.status === 'running') killTreeSync(bg.child); // the program is exiting
57
59
  }
58
60
 
59
61
  export function listBackgroundShells() {
@@ -106,12 +108,13 @@ When you commit (the system prompt says when): run git status, git diff and git
106
108
  const id = `bash_${++bgCounter}`;
107
109
  const bg = { child, command, description: String(input.description || ''), output: '', readPos: 0, status: 'running', exitCode: null, startedAt: Date.now() };
108
110
  background.set(id, bg);
109
- // Decode as a stream: a multi-byte character (CJK text) can be split between two chunks.
110
- child.stdout.setEncoding('utf8');
111
- child.stderr.setEncoding('utf8');
112
- const onData = (d) => appendOutput(bg, stripAnsi(d));
113
- child.stdout.on('data', onData);
114
- child.stderr.on('data', onData);
111
+ // Line by line, UTF-8 or the console code page (GBK here), never cutting a character (see OutputDecoder).
112
+ for (const stream of [child.stdout, child.stderr]) {
113
+ const decoder = new OutputDecoder();
114
+ const take = (text) => { if (text) appendOutput(bg, stripAnsi(text)); };
115
+ stream.on('data', (chunk) => take(decoder.write(chunk)));
116
+ stream.on('end', () => take(decoder.end()));
117
+ }
115
118
  child.on('close', (code) => { bg.status = bg.status === 'killed' ? 'killed' : 'completed'; bg.exitCode = code; });
116
119
  child.on('error', (e) => { bg.status = 'failed'; bg.output += `\n${e.message}`; });
117
120
  return { content: `Command running in background with ID: ${id}`, display: { summary: `Running in background (${id})` } };
@@ -148,7 +151,8 @@ When you commit (the system prompt says when): run git status, git diff and git
148
151
  const saved = saveFullOutput(ctx.projectRoot, text);
149
152
  const size = text.length;
150
153
  text = truncateOutput(text, config.bashMaxOutput);
151
- if (saved) text += `\n\n(Output was ${size} characters and has been truncated. The full output is saved at ${saved}; use Read with offset/limit or Grep on that file to see the rest.)`;
154
+ const what = droppedHead ? `The last ${size} characters are saved at ${saved} (the start of the output, past 10 MB, was not kept)` : `The full output is saved at ${saved}`;
155
+ if (saved) text += `\n\n(Output was ${droppedHead ? 'over 10,000,000' : size} characters and has been truncated. ${what}; use Read with offset/limit or Grep on that file to see the rest.)`;
152
156
  }
153
157
  if (err) text += `${text ? '\n' : ''}${err.message}`;
154
158
  if (timedOut) text += `${text ? '\n' : ''}Command timed out after ${timeout}ms`;
@@ -156,11 +160,16 @@ When you commit (the system prompt says when): run git status, git diff and git
156
160
  if (code && code !== 0 && !timedOut) text += `${text ? '\n' : ''}Exit code ${code}`;
157
161
  resolve({ isError, content: text || '(no output)', display: { output: text, exitCode: code } });
158
162
  };
163
+ // The live view shows the last lines: it gets the end of the output and the line count, not all of it
164
+ // stripped again on every chunk (quadratic on a noisy build, with up to 10 MB re-split every frame).
165
+ let lines = 0;
166
+ let droppedHead = false;
159
167
  watchShell(child, {
160
168
  onOutput: (text) => {
161
169
  output += text;
162
- if (output.length > 10_000_000) output = output.slice(-5_000_000);
163
- ctx.onProgress?.(stripAnsi(output));
170
+ for (let i = text.indexOf('\n'); i >= 0; i = text.indexOf('\n', i + 1)) lines++;
171
+ if (output.length > 10_000_000) { output = output.slice(-5_000_000); droppedHead = true; }
172
+ ctx.onProgress?.(stripAnsi(output.slice(-PROGRESS_CHARS)), { lines, all: () => stripAnsi(output) });
164
173
  },
165
174
  done: finish,
166
175
  });
@@ -185,7 +194,7 @@ export const BashOutputTool = {
185
194
  required: ['bash_id'],
186
195
  },
187
196
  summarize(input) { return input.bash_id; },
188
- async call(input) {
197
+ async call(input, ctx = {}) {
189
198
  const bg = background.get(input.bash_id);
190
199
  if (!bg) return { isError: true, content: `No background shell with id ${input.bash_id}. Known: ${[...background.keys()].join(', ') || 'none'}` };
191
200
  let fresh = bg.output.slice(bg.readPos);
@@ -196,7 +205,14 @@ export const BashOutputTool = {
196
205
  fresh = fresh.split('\n').filter((l) => re.test(l)).join('\n');
197
206
  } catch (e) { return { isError: true, content: `Invalid filter regex: ${e.message}` }; }
198
207
  }
199
- fresh = truncateOutput(fresh.replace(/\r\n/g, '\n'), 30_000);
208
+ fresh = fresh.replace(/\r\n/g, '\n');
209
+ if (fresh.length > 30_000) {
210
+ // The read position has moved past all of it: the middle that is cut here would be gone for good.
211
+ const saved = ctx.projectRoot ? saveFullOutput(ctx.projectRoot, fresh) : null;
212
+ const size = fresh.length;
213
+ fresh = truncateOutput(fresh, 30_000);
214
+ if (saved) fresh += `\n\n(The new output was ${size} characters and has been truncated. All of it is saved at ${saved}; use Read with offset/limit or Grep on that file to see the rest.)`;
215
+ }
200
216
  const status = `<status>${bg.status}</status>${bg.exitCode !== null ? `\n<exit_code>${bg.exitCode}</exit_code>` : ''}`;
201
217
  return { content: `${status}\n<output>\n${fresh || '(no new output)'}\n</output>`, display: { summary: `${bg.status}${fresh ? `, ${fresh.split('\n').length} new lines` : ', no new output'}` } };
202
218
  },
@@ -216,7 +232,7 @@ export const KillShellTool = {
216
232
  if (!bg) return { isError: true, content: `No background shell with id ${input.shell_id}` };
217
233
  if (bg.status !== 'running') return { content: `Shell ${input.shell_id} is not running (status: ${bg.status})` };
218
234
  bg.status = 'killed';
219
- killTree(bg.child);
235
+ await killTree(bg.child);
220
236
  return { content: `Successfully killed shell: ${input.shell_id} (${bg.command})` };
221
237
  },
222
238
  };