@trans-ai/trans-code 0.2.1 → 0.2.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/package.json +1 -1
- package/src/agent/system-prompt.test.ts +13 -1
- package/src/agent/system-prompt.ts +3 -0
- package/src/agent/tools/bash.test.ts +11 -0
- package/src/agent/tools/bash.ts +2 -1
- package/src/agent/tools/read-only.test.ts +19 -0
- package/src/agent/tools/read-only.ts +33 -2
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -2,7 +2,19 @@ import { afterEach, describe, expect, test } from 'bun:test';
|
|
|
2
2
|
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import path from 'node:path';
|
|
5
|
-
import { INIT_PROMPT, loadProjectContext } from './system-prompt.js';
|
|
5
|
+
import { INIT_PROMPT, buildSystemPrompt, loadProjectContext } from './system-prompt.js';
|
|
6
|
+
|
|
7
|
+
describe('buildSystemPrompt', () => {
|
|
8
|
+
test('steers file inspection away from whole-file cat/sed dumps', () => {
|
|
9
|
+
const prompt = buildSystemPrompt(
|
|
10
|
+
{ cwd: '/tmp', projectFiles: [] },
|
|
11
|
+
{ modelName: 'test-model' },
|
|
12
|
+
);
|
|
13
|
+
expect(prompt).toContain('wc -l');
|
|
14
|
+
expect(prompt).toContain('offset');
|
|
15
|
+
expect(prompt.toLowerCase()).toContain('do not cat');
|
|
16
|
+
});
|
|
17
|
+
});
|
|
6
18
|
|
|
7
19
|
const cleanups: Array<() => void> = [];
|
|
8
20
|
afterEach(() => {
|
|
@@ -89,6 +89,9 @@ export function buildSystemPrompt(
|
|
|
89
89
|
'## How to work',
|
|
90
90
|
'- Inspect the repository before changing it. Read the relevant files first.',
|
|
91
91
|
'- Prefer read_file, glob and grep for exploration; use bash for builds, tests and git.',
|
|
92
|
+
'- Inspect file contents with read_file, not cat/sed: it caps lines and paginates. ' +
|
|
93
|
+
'Check unknown file sizes with wc -l first and read large files in offset/limit chunks. ' +
|
|
94
|
+
'Do not cat entire files into the conversation.',
|
|
92
95
|
'- The bash working directory persists across calls; you do not need to cd again each time.',
|
|
93
96
|
'- Use edit_file for targeted changes and write_file for new files.',
|
|
94
97
|
'- Keep changes focused on the task. Do not undo unrelated user work.',
|
|
@@ -6,6 +6,17 @@ import { bashTool } from './bash.js';
|
|
|
6
6
|
import { createToolSession, type ToolSession } from './session.js';
|
|
7
7
|
import type { ToolContext } from './types.js';
|
|
8
8
|
|
|
9
|
+
describe('bash tool wiring', () => {
|
|
10
|
+
test('description steers file viewing to read_file', () => {
|
|
11
|
+
expect(bashTool.description).toContain('read_file');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('isReadOnly is wired to the analyzer', () => {
|
|
15
|
+
expect(bashTool.isReadOnly?.({ command: 'cat config.py 2>/dev/null' })).toBe(true);
|
|
16
|
+
expect(bashTool.isReadOnly?.({ command: 'npm install' })).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
9
20
|
const cleanups: Array<() => void> = [];
|
|
10
21
|
const savedEnv: Record<string, string | undefined> = {};
|
|
11
22
|
function setEnv(key: string, value: string | undefined): void {
|
package/src/agent/tools/bash.ts
CHANGED
|
@@ -218,7 +218,8 @@ export const bashTool: AgentTool = {
|
|
|
218
218
|
description:
|
|
219
219
|
'Run a shell command in the workspace using the user shell. Working directory changes (cd) ' +
|
|
220
220
|
'persist across calls. Returns stdout, stderr and the exit code. ' +
|
|
221
|
-
'
|
|
221
|
+
'For viewing file contents use read_file (line-capped, paginates with offset/limit) — ' +
|
|
222
|
+
'cat/sed dumps of entire files waste context.',
|
|
222
223
|
kind: 'execute',
|
|
223
224
|
isMutating: true,
|
|
224
225
|
isReadOnly: (input) => isReadOnlyCommand(String(input.command ?? '')),
|
|
@@ -75,4 +75,23 @@ describe('isReadOnlyCommand', () => {
|
|
|
75
75
|
expect(isReadOnlyCommand('')).toBe(false);
|
|
76
76
|
expect(isReadOnlyCommand(' ')).toBe(false);
|
|
77
77
|
});
|
|
78
|
+
|
|
79
|
+
test('redirects to /dev/null and fd duplication are harmless', () => {
|
|
80
|
+
expect(isReadOnlyCommand('cat config.py 2>/dev/null')).toBe(true);
|
|
81
|
+
expect(isReadOnlyCommand('ls missing 2>/dev/null; echo done')).toBe(true);
|
|
82
|
+
expect(isReadOnlyCommand('grep foo bar >/dev/null')).toBe(true);
|
|
83
|
+
expect(isReadOnlyCommand('echo hi 2>>/dev/null')).toBe(true);
|
|
84
|
+
expect(isReadOnlyCommand('grep foo bar 2>&1 | wc -l')).toBe(true);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('redirects to real files still disqualify', () => {
|
|
88
|
+
expect(isReadOnlyCommand('grep foo bar > out.txt')).toBe(false);
|
|
89
|
+
expect(isReadOnlyCommand('grep foo bar >> out.txt')).toBe(false);
|
|
90
|
+
expect(isReadOnlyCommand('grep foo bar 2> err.txt')).toBe(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('input redirection is read-only, process substitution is not', () => {
|
|
94
|
+
expect(isReadOnlyCommand('sort < input.txt')).toBe(true);
|
|
95
|
+
expect(isReadOnlyCommand('cat <(ls)')).toBe(false);
|
|
96
|
+
});
|
|
78
97
|
});
|
|
@@ -103,8 +103,39 @@ function parseSegments(command: string): { segments: Segment[]; unsafe: boolean
|
|
|
103
103
|
continue;
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
if (char === '>'
|
|
107
|
-
|
|
106
|
+
if (char === '>') {
|
|
107
|
+
// Output redirection: harmless when the target is /dev/null or an fd
|
|
108
|
+
// duplication (2>&1); anything else writes a file and disqualifies.
|
|
109
|
+
let j = i + 1;
|
|
110
|
+
if (command[j] === '>') j++; // >>
|
|
111
|
+
let k = j;
|
|
112
|
+
while (k < command.length && /\s/.test(command[k]!)) k++;
|
|
113
|
+
if (command[k] === '&' && /[0-9]/.test(command[k + 1] ?? '')) {
|
|
114
|
+
i = k + 1; // >&N — safe, consumed
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
let t = k;
|
|
118
|
+
if (command[t] === '"' || command[t] === "'") t++;
|
|
119
|
+
if (command.startsWith('/dev/null', t)) {
|
|
120
|
+
i = t + 8; // >/dev/null — safe, consumed
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
unsafe = true;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (char === '<') {
|
|
128
|
+
// Heredocs (<<) and process substitution (<() are disqualifying;
|
|
129
|
+
// a plain input redirect (< file) only reads.
|
|
130
|
+
if (command[i + 1] === '<' || command[i + 1] === '(' || command[i + 1] === '>') {
|
|
131
|
+
unsafe = true;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
continue; // target tokenizes as a regular word
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (char === '`') {
|
|
138
|
+
unsafe = true; // backtick substitution
|
|
108
139
|
continue;
|
|
109
140
|
}
|
|
110
141
|
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Auto-generated from package.json by scripts/sync-version.ts (npm version hook)
|
|
2
|
-
export const VERSION = '0.2.
|
|
2
|
+
export const VERSION = '0.2.2';
|