@trans-ai/trans-code 0.2.0 → 0.2.1
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/acp/session.ts +2 -3
- package/src/agent/loop.test.ts +50 -1
- package/src/agent/loop.ts +7 -1
- package/src/agent/permission.test.ts +50 -0
- package/src/agent/permission.ts +34 -0
- package/src/agent/tools/bash.ts +2 -0
- package/src/agent/tools/read-only.test.ts +78 -0
- package/src/agent/tools/read-only.ts +181 -0
- package/src/agent/tools/types.ts +2 -0
- package/src/cli.ts +2 -1
- package/src/commands.test.ts +40 -1
- package/src/commands.ts +22 -0
- package/src/screens/REPL.tsx +4 -2
- package/src/version.ts +1 -1
package/package.json
CHANGED
package/src/acp/session.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
import { createModel, getAvailableModels, resolveModel } from '../provider/provider.js';
|
|
10
10
|
import { runAgentLoop, type AgentEvent, type PermissionRequest } from '../agent/loop.js';
|
|
11
11
|
import { createToolSession, type ToolSession } from '../agent/tools/index.js';
|
|
12
|
+
import { getPermissionMode } from '../agent/permission.js';
|
|
12
13
|
import { buildSystemPrompt, loadProjectContext } from '../agent/system-prompt.js';
|
|
13
14
|
import {
|
|
14
15
|
RPC_ERRORS,
|
|
@@ -66,9 +67,7 @@ function promptText(prompt: AcpPromptBlock[]): string {
|
|
|
66
67
|
}
|
|
67
68
|
|
|
68
69
|
function permissionMode(): 'ask' | 'auto' | 'readonly' {
|
|
69
|
-
|
|
70
|
-
if (value === 'auto' || value === 'readonly') return value;
|
|
71
|
-
return 'ask';
|
|
70
|
+
return getPermissionMode();
|
|
72
71
|
}
|
|
73
72
|
|
|
74
73
|
export class AcpSession {
|
package/src/agent/loop.test.ts
CHANGED
|
@@ -33,7 +33,7 @@ const fakeTool: AgentTool = {
|
|
|
33
33
|
description: 'test double',
|
|
34
34
|
schema: (await import('zod')).z.object({}),
|
|
35
35
|
kind: 'other',
|
|
36
|
-
isMutating:
|
|
36
|
+
isMutating: true,
|
|
37
37
|
title: () => 'fake',
|
|
38
38
|
async execute() {
|
|
39
39
|
return { output: 'ok', diff: '+added-line' };
|
|
@@ -61,4 +61,53 @@ describe('runAgentLoop', () => {
|
|
|
61
61
|
'+added-line',
|
|
62
62
|
);
|
|
63
63
|
});
|
|
64
|
+
|
|
65
|
+
test('read-only mutating tools skip the permission request', async () => {
|
|
66
|
+
const events: AgentEvent[] = [];
|
|
67
|
+
let permissionAsked = 0;
|
|
68
|
+
const readOnlyTool: AgentTool = {
|
|
69
|
+
...fakeTool,
|
|
70
|
+
name: 'fake',
|
|
71
|
+
isMutating: true,
|
|
72
|
+
isReadOnly: () => true,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const result = await runAgentLoop({
|
|
76
|
+
model: stubModel(),
|
|
77
|
+
cwd: '/tmp',
|
|
78
|
+
messages: [],
|
|
79
|
+
systemPrompt: 'test',
|
|
80
|
+
tools: [readOnlyTool],
|
|
81
|
+
onEvent: (event) => {
|
|
82
|
+
events.push(event);
|
|
83
|
+
},
|
|
84
|
+
requestPermission: async () => {
|
|
85
|
+
permissionAsked++;
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(result.stopReason).toBe('end_turn');
|
|
91
|
+
expect(permissionAsked).toBe(0);
|
|
92
|
+
const toolResult = events.find((event) => event.type === 'tool_result');
|
|
93
|
+
expect(toolResult && toolResult.type === 'tool_result' ? toolResult.skippedPermission : false)
|
|
94
|
+
.toBe(true);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('mutating tools without isReadOnly still request permission', async () => {
|
|
98
|
+
let permissionAsked = 0;
|
|
99
|
+
await runAgentLoop({
|
|
100
|
+
model: stubModel(),
|
|
101
|
+
cwd: '/tmp',
|
|
102
|
+
messages: [],
|
|
103
|
+
systemPrompt: 'test',
|
|
104
|
+
tools: [fakeTool],
|
|
105
|
+
onEvent: () => {},
|
|
106
|
+
requestPermission: async () => {
|
|
107
|
+
permissionAsked++;
|
|
108
|
+
return true;
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
expect(permissionAsked).toBe(1);
|
|
112
|
+
});
|
|
64
113
|
});
|
package/src/agent/loop.ts
CHANGED
|
@@ -37,6 +37,8 @@ export type AgentEvent =
|
|
|
37
37
|
ok: boolean;
|
|
38
38
|
/** Unified diff for UI rendering; not sent to the model. */
|
|
39
39
|
diff?: string;
|
|
40
|
+
/** True when a mutating tool ran without prompting (read-only input). */
|
|
41
|
+
skippedPermission?: boolean;
|
|
40
42
|
};
|
|
41
43
|
|
|
42
44
|
export interface PermissionRequest {
|
|
@@ -302,10 +304,13 @@ export async function runAgentLoop(
|
|
|
302
304
|
});
|
|
303
305
|
|
|
304
306
|
let result: { ok: boolean; output: string; diff?: string };
|
|
307
|
+
let skippedPermission = false;
|
|
305
308
|
if (!tool) {
|
|
306
309
|
result = { ok: false, output: `Unknown tool: ${call.name}` };
|
|
307
310
|
} else {
|
|
308
|
-
const
|
|
311
|
+
const readOnly = tool.isReadOnly?.(input) === true;
|
|
312
|
+
const needsPermission = tool.isMutating && !readOnly;
|
|
313
|
+
skippedPermission = tool.isMutating && readOnly;
|
|
309
314
|
const allowed =
|
|
310
315
|
!needsPermission || !options.requestPermission
|
|
311
316
|
? true
|
|
@@ -333,6 +338,7 @@ export async function runAgentLoop(
|
|
|
333
338
|
output: result.output,
|
|
334
339
|
ok: result.ok,
|
|
335
340
|
diff: result.diff,
|
|
341
|
+
skippedPermission,
|
|
336
342
|
});
|
|
337
343
|
|
|
338
344
|
messages.push(
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import {
|
|
3
|
+
getPermissionMode,
|
|
4
|
+
PERMISSION_MODES,
|
|
5
|
+
resetPermissionMode,
|
|
6
|
+
setPermissionMode,
|
|
7
|
+
} from './permission.js';
|
|
8
|
+
|
|
9
|
+
const savedEnv: string | undefined = process.env.TRANSCODE_PERMISSION_MODE;
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
if (savedEnv === undefined) delete process.env.TRANSCODE_PERMISSION_MODE;
|
|
12
|
+
else process.env.TRANSCODE_PERMISSION_MODE = savedEnv;
|
|
13
|
+
resetPermissionMode();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('permission mode', () => {
|
|
17
|
+
test('defaults to ask', () => {
|
|
18
|
+
delete process.env.TRANSCODE_PERMISSION_MODE;
|
|
19
|
+
resetPermissionMode();
|
|
20
|
+
expect(getPermissionMode()).toBe('ask');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('initializes from the environment variable', () => {
|
|
24
|
+
process.env.TRANSCODE_PERMISSION_MODE = 'AUTO';
|
|
25
|
+
resetPermissionMode();
|
|
26
|
+
expect(getPermissionMode()).toBe('auto');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('invalid environment value falls back to ask', () => {
|
|
30
|
+
process.env.TRANSCODE_PERMISSION_MODE = 'yolo';
|
|
31
|
+
resetPermissionMode();
|
|
32
|
+
expect(getPermissionMode()).toBe('ask');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('switches at runtime', () => {
|
|
36
|
+
setPermissionMode('auto');
|
|
37
|
+
expect(getPermissionMode()).toBe('auto');
|
|
38
|
+
setPermissionMode('readonly');
|
|
39
|
+
expect(getPermissionMode()).toBe('readonly');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('rejects unknown modes', () => {
|
|
43
|
+
expect(() => setPermissionMode('yolo' as never)).toThrow();
|
|
44
|
+
expect(getPermissionMode()).toBe('ask');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('documents the mode list', () => {
|
|
48
|
+
expect(PERMISSION_MODES).toEqual(['ask', 'auto', 'readonly']);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime-switchable permission mode, shared by the REPL and the ACP server.
|
|
3
|
+
* Initialized from TRANSCODE_PERMISSION_MODE; switchable at runtime via /mode.
|
|
4
|
+
*
|
|
5
|
+
* - ask: mutating tools prompt (read-only bash commands pass silently)
|
|
6
|
+
* - auto: everything is allowed without prompting
|
|
7
|
+
* - readonly: mutating tools are denied; read-only commands still run
|
|
8
|
+
*/
|
|
9
|
+
export type PermissionMode = 'ask' | 'auto' | 'readonly';
|
|
10
|
+
|
|
11
|
+
export const PERMISSION_MODES: readonly PermissionMode[] = ['ask', 'auto', 'readonly'];
|
|
12
|
+
|
|
13
|
+
function parseMode(value: string | undefined): PermissionMode {
|
|
14
|
+
const lower = value?.toLowerCase();
|
|
15
|
+
return PERMISSION_MODES.includes(lower as PermissionMode) ? (lower as PermissionMode) : 'ask';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
let currentMode: PermissionMode = parseMode(process.env.TRANSCODE_PERMISSION_MODE);
|
|
19
|
+
|
|
20
|
+
export function getPermissionMode(): PermissionMode {
|
|
21
|
+
return currentMode;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function setPermissionMode(mode: PermissionMode): void {
|
|
25
|
+
if (!PERMISSION_MODES.includes(mode)) {
|
|
26
|
+
throw new Error(`Unknown permission mode: ${mode}. Valid: ${PERMISSION_MODES.join(', ')}`);
|
|
27
|
+
}
|
|
28
|
+
currentMode = mode;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Re-reads the environment variable (used at startup and in tests). */
|
|
32
|
+
export function resetPermissionMode(): void {
|
|
33
|
+
currentMode = parseMode(process.env.TRANSCODE_PERMISSION_MODE);
|
|
34
|
+
}
|
package/src/agent/tools/bash.ts
CHANGED
|
@@ -6,6 +6,7 @@ import path from 'node:path';
|
|
|
6
6
|
import { z } from 'zod';
|
|
7
7
|
import type { AgentTool } from './types.js';
|
|
8
8
|
import type { ToolSession } from './session.js';
|
|
9
|
+
import { isReadOnlyCommand } from './read-only.js';
|
|
9
10
|
|
|
10
11
|
const MAX_BASH_OUTPUT = 30_000;
|
|
11
12
|
const TRUNCATE_HEAD = 15_000;
|
|
@@ -220,6 +221,7 @@ export const bashTool: AgentTool = {
|
|
|
220
221
|
'Prefer dedicated read/write/edit/glob/grep tools for file operations.',
|
|
221
222
|
kind: 'execute',
|
|
222
223
|
isMutating: true,
|
|
224
|
+
isReadOnly: (input) => isReadOnlyCommand(String(input.command ?? '')),
|
|
223
225
|
schema: z.object({
|
|
224
226
|
command: z.string().describe('Shell command to run'),
|
|
225
227
|
timeout_ms: z.number().int().min(1000).max(600_000).nullable().optional().describe('Timeout in ms'),
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { isReadOnlyCommand } from './read-only.js';
|
|
3
|
+
|
|
4
|
+
describe('isReadOnlyCommand', () => {
|
|
5
|
+
test('simple read-only commands pass', () => {
|
|
6
|
+
expect(isReadOnlyCommand('ls')).toBe(true);
|
|
7
|
+
expect(isReadOnlyCommand('ls -la /tmp')).toBe(true);
|
|
8
|
+
expect(isReadOnlyCommand('git status')).toBe(true);
|
|
9
|
+
expect(isReadOnlyCommand('git log --oneline -3')).toBe(true);
|
|
10
|
+
expect(isReadOnlyCommand('grep -n foo bar.txt')).toBe(true);
|
|
11
|
+
expect(isReadOnlyCommand('sed -n "348,420p" tests/smoke.py')).toBe(true);
|
|
12
|
+
expect(isReadOnlyCommand('pwd')).toBe(true);
|
|
13
|
+
expect(isReadOnlyCommand('cd trans-once && pwd')).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('pipelines and chains pass when every segment is read-only', () => {
|
|
17
|
+
expect(isReadOnlyCommand('cat x | grep y')).toBe(true);
|
|
18
|
+
expect(isReadOnlyCommand('git log --oneline -3; echo "---"; git diff --stat')).toBe(true);
|
|
19
|
+
expect(isReadOnlyCommand('ls && echo done || echo failed')).toBe(true);
|
|
20
|
+
expect(isReadOnlyCommand('git branch -vv && git log --oneline -15')).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('mutating commands fail', () => {
|
|
24
|
+
expect(isReadOnlyCommand('npm install')).toBe(false);
|
|
25
|
+
expect(isReadOnlyCommand('git push origin main')).toBe(false);
|
|
26
|
+
expect(isReadOnlyCommand('git commit -m x')).toBe(false);
|
|
27
|
+
expect(isReadOnlyCommand('node check.ts')).toBe(false);
|
|
28
|
+
expect(isReadOnlyCommand('rm -rf /tmp/x')).toBe(false);
|
|
29
|
+
expect(isReadOnlyCommand('curl https://example.com')).toBe(false);
|
|
30
|
+
expect(isReadOnlyCommand('mkdir newdir')).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test('mixed chains fail when any segment mutates', () => {
|
|
34
|
+
expect(isReadOnlyCommand('ls && npm install')).toBe(false);
|
|
35
|
+
expect(isReadOnlyCommand('git status; git push')).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('output redirection disqualifies', () => {
|
|
39
|
+
expect(isReadOnlyCommand('grep foo bar > out.txt')).toBe(false);
|
|
40
|
+
expect(isReadOnlyCommand('echo hi >> log.txt')).toBe(false);
|
|
41
|
+
expect(isReadOnlyCommand('ls >out')).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('command substitution and heredocs disqualify', () => {
|
|
45
|
+
expect(isReadOnlyCommand('echo $(rm -rf x)')).toBe(false);
|
|
46
|
+
expect(isReadOnlyCommand('echo `rm -rf x`')).toBe(false);
|
|
47
|
+
expect(isReadOnlyCommand('cat <<EOF\nhi\nEOF')).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('risky flags disqualify find and sed', () => {
|
|
51
|
+
expect(isReadOnlyCommand('find . -name "*.tmp" -delete')).toBe(false);
|
|
52
|
+
expect(isReadOnlyCommand('find . -exec rm {} \\;')).toBe(false);
|
|
53
|
+
expect(isReadOnlyCommand('sed -i s/a/b file.txt')).toBe(false);
|
|
54
|
+
expect(isReadOnlyCommand('sed -in s/a/b file.txt')).toBe(false);
|
|
55
|
+
expect(isReadOnlyCommand('sed --in-place s/a/b file.txt')).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('git subcommands outside the read-only set fail', () => {
|
|
59
|
+
expect(isReadOnlyCommand('git branch feature')).toBe(false);
|
|
60
|
+
expect(isReadOnlyCommand('git config user.name x')).toBe(false);
|
|
61
|
+
expect(isReadOnlyCommand('git checkout main')).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('quoted separators and metacharacters are not treated as operators', () => {
|
|
65
|
+
expect(isReadOnlyCommand('echo "a;b | c && d"')).toBe(true);
|
|
66
|
+
expect(isReadOnlyCommand('grep "a > b" file.txt')).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('env assignment prefixes are tolerated', () => {
|
|
70
|
+
expect(isReadOnlyCommand('LC_ALL=C sort big.txt')).toBe(true);
|
|
71
|
+
expect(isReadOnlyCommand('FOO=1 ls')).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('empty or whitespace command is not read-only', () => {
|
|
75
|
+
expect(isReadOnlyCommand('')).toBe(false);
|
|
76
|
+
expect(isReadOnlyCommand(' ')).toBe(false);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pragmatic read-only bash command analyzer. A command is considered
|
|
3
|
+
* read-only when every segment (split on ; | && || & and newlines, outside
|
|
4
|
+
* quotes) starts with a whitelisted command AND carries no risky flags, and
|
|
5
|
+
* the raw command contains no unquoted redirection/substitution/heredoc.
|
|
6
|
+
*
|
|
7
|
+
* Conservative by design: anything unparsed or unusual fails closed (asks
|
|
8
|
+
* for permission). Inspired by Claude Code's readOnlyValidation.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const SAFE_COMMANDS = new Set([
|
|
12
|
+
'ls', 'cat', 'head', 'tail', 'pwd', 'echo', 'printf', 'wc', 'file', 'which',
|
|
13
|
+
'whoami', 'date', 'uname', 'hostname', 'basename', 'dirname', 'realpath',
|
|
14
|
+
'stat', 'du', 'df', 'tree', 'grep', 'rg', 'fgrep', 'egrep', 'awk', 'sort',
|
|
15
|
+
'uniq', 'cut', 'tr', 'diff', 'cmp', 'jq', 'printenv', 'type', 'command',
|
|
16
|
+
'help', 'man', 'true', 'false', 'cd', 'md5sum', 'shasum', 'sha256sum',
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
const GIT_READONLY = new Set([
|
|
20
|
+
'status', 'log', 'diff', 'show', 'rev-parse', 'ls-files', 'blame',
|
|
21
|
+
'describe', 'shortlog', 'reflog', 'grep', 'whatchanged',
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
const FIND_UNSAFE_FLAGS = new Set([
|
|
25
|
+
'-delete', '-exec', '-execdir', '-ok', '-okdir', '-fls', '-fprintf',
|
|
26
|
+
'-fprint', '-fprint0',
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
interface Segment {
|
|
30
|
+
tokens: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Splits a command line into segments on unquoted separators, tracking
|
|
35
|
+
* whether any unquoted disqualifying metacharacter appears. Backslash
|
|
36
|
+
* escapes and single/double quotes are respected.
|
|
37
|
+
*/
|
|
38
|
+
function parseSegments(command: string): { segments: Segment[]; unsafe: boolean } {
|
|
39
|
+
const segments: Segment[] = [];
|
|
40
|
+
let current: Segment = { tokens: [] };
|
|
41
|
+
let token = '';
|
|
42
|
+
let hasToken = false;
|
|
43
|
+
let unsafe = false;
|
|
44
|
+
|
|
45
|
+
const endToken = () => {
|
|
46
|
+
if (hasToken) {
|
|
47
|
+
current.tokens.push(token);
|
|
48
|
+
token = '';
|
|
49
|
+
hasToken = false;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const endSegment = () => {
|
|
53
|
+
endToken();
|
|
54
|
+
if (current.tokens.length > 0) segments.push(current);
|
|
55
|
+
current = { tokens: [] };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
for (let i = 0; i < command.length; i++) {
|
|
59
|
+
const char = command[i]!;
|
|
60
|
+
|
|
61
|
+
if (char === "'") {
|
|
62
|
+
// Single quotes: everything literal until the closing quote.
|
|
63
|
+
const close = command.indexOf("'", i + 1);
|
|
64
|
+
if (close === -1) return { segments, unsafe: true }; // unterminated quote
|
|
65
|
+
token += command.slice(i + 1, close);
|
|
66
|
+
hasToken = true;
|
|
67
|
+
i = close;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (char === '"') {
|
|
72
|
+
const close = command.indexOf('"', i + 1);
|
|
73
|
+
if (close === -1) return { segments, unsafe: true };
|
|
74
|
+
for (let j = i + 1; j < close; j++) {
|
|
75
|
+
if (command[j] === '\\' && j + 1 < close) {
|
|
76
|
+
token += command[j + 1]!;
|
|
77
|
+
j++;
|
|
78
|
+
} else {
|
|
79
|
+
token += command[j]!;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
hasToken = true;
|
|
83
|
+
i = close;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (char === '\\') {
|
|
88
|
+
if (i + 1 >= command.length) return { segments, unsafe: true };
|
|
89
|
+
token += command[i + 1]!;
|
|
90
|
+
hasToken = true;
|
|
91
|
+
i++;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (char === ';' || char === '\n') {
|
|
96
|
+
endSegment();
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (char === '|' || char === '&') {
|
|
101
|
+
if (command[i + 1] === char) i++; // || or &&
|
|
102
|
+
endSegment();
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (char === '>' || char === '<' || char === '`') {
|
|
107
|
+
unsafe = true; // redirection, heredoc, backtick substitution
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (char === '$' && command[i + 1] === '(') {
|
|
112
|
+
unsafe = true; // command substitution
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (char === '#' && !hasToken) {
|
|
117
|
+
// Comment: ignore the rest of the line.
|
|
118
|
+
const nl = command.indexOf('\n', i);
|
|
119
|
+
if (nl === -1) break;
|
|
120
|
+
i = nl - 1;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (/\s/.test(char)) {
|
|
125
|
+
endToken();
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
token += char;
|
|
130
|
+
hasToken = true;
|
|
131
|
+
}
|
|
132
|
+
endSegment();
|
|
133
|
+
|
|
134
|
+
return { segments, unsafe };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function segmentIsReadOnly(tokens: string[]): boolean {
|
|
138
|
+
// Strip leading VAR=value assignments (FOO=1 ls …).
|
|
139
|
+
let start = 0;
|
|
140
|
+
while (start < tokens.length && /^[A-Za-z_][A-Za-z0-9_]*=/.test(tokens[start]!)) {
|
|
141
|
+
start++;
|
|
142
|
+
}
|
|
143
|
+
const rest = tokens.slice(start);
|
|
144
|
+
if (rest.length === 0) return true; // pure assignments are side-effect free
|
|
145
|
+
const base = rest[0]!;
|
|
146
|
+
|
|
147
|
+
if (base === 'git') {
|
|
148
|
+
const sub = rest[1];
|
|
149
|
+
if (!sub) return false;
|
|
150
|
+
if (sub === 'branch') {
|
|
151
|
+
// `git branch -vv` lists; `git branch name` creates. Safe only when
|
|
152
|
+
// every argument is a flag.
|
|
153
|
+
return rest.slice(2).every((arg) => arg.startsWith('-'));
|
|
154
|
+
}
|
|
155
|
+
return GIT_READONLY.has(sub);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (base === 'find') {
|
|
159
|
+
return !rest.some((arg) => FIND_UNSAFE_FLAGS.has(arg));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (base === 'sed') {
|
|
163
|
+
// Unsafe when any short-flag cluster contains `i` (in-place) or a long
|
|
164
|
+
// --in-place style flag is present.
|
|
165
|
+
return !rest.some((arg) => {
|
|
166
|
+
if (arg.startsWith('--')) return arg === '--in-place' || arg.startsWith('--in-place=');
|
|
167
|
+
if (arg.startsWith('-') && arg.length > 1) return arg.includes('i');
|
|
168
|
+
return false;
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return SAFE_COMMANDS.has(base);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** True when the command can run without a permission prompt. */
|
|
176
|
+
export function isReadOnlyCommand(command: string): boolean {
|
|
177
|
+
const { segments, unsafe } = parseSegments(command);
|
|
178
|
+
if (unsafe) return false;
|
|
179
|
+
if (segments.length === 0) return false;
|
|
180
|
+
return segments.every((segment) => segmentIsReadOnly(segment.tokens));
|
|
181
|
+
}
|
package/src/agent/tools/types.ts
CHANGED
|
@@ -33,6 +33,8 @@ export interface AgentTool {
|
|
|
33
33
|
schema: z.ZodObject<z.ZodRawShape>;
|
|
34
34
|
kind: ToolKind;
|
|
35
35
|
isMutating: boolean;
|
|
36
|
+
/** When true for a given input, a mutating tool may skip the permission prompt. */
|
|
37
|
+
isReadOnly?(input: Record<string, unknown>): boolean;
|
|
36
38
|
title(input: Record<string, unknown>): string;
|
|
37
39
|
execute(input: Record<string, unknown>, ctx: ToolContext): Promise<string | ToolOutput>;
|
|
38
40
|
}
|
package/src/cli.ts
CHANGED
|
@@ -12,7 +12,8 @@ Usage:
|
|
|
12
12
|
Environment:
|
|
13
13
|
TRANSCODE_MODEL Default model (registry id or provider:model)
|
|
14
14
|
TRANSCODE_DEFAULT_PROVIDER Provider for bare model names (default: openai)
|
|
15
|
-
TRANSCODE_PERMISSION_MODE ask | auto | readonly (default: ask in ACP mode
|
|
15
|
+
TRANSCODE_PERMISSION_MODE ask | auto | readonly (default: ask in ACP mode;
|
|
16
|
+
switch at runtime in the REPL with /mode)
|
|
16
17
|
ANTHROPIC_API_KEY / OPENAI_API_KEY / DEEPSEEK_API_KEY / ... provider keys
|
|
17
18
|
|
|
18
19
|
Examples:
|
package/src/commands.test.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test';
|
|
1
|
+
import { afterEach, describe, expect, test } from 'bun:test';
|
|
2
2
|
import { findCommand, getCommands } from './commands.js';
|
|
3
|
+
import { getPermissionMode, resetPermissionMode, setPermissionMode } from './agent/permission.js';
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
setPermissionMode('ask');
|
|
7
|
+
resetPermissionMode();
|
|
8
|
+
});
|
|
3
9
|
|
|
4
10
|
describe('init command registration', () => {
|
|
5
11
|
test('is registered so /help lists it', () => {
|
|
@@ -15,3 +21,36 @@ describe('init command registration', () => {
|
|
|
15
21
|
expect(names).toContain('exit');
|
|
16
22
|
});
|
|
17
23
|
});
|
|
24
|
+
|
|
25
|
+
describe('mode command', () => {
|
|
26
|
+
test('is registered', () => {
|
|
27
|
+
const mode = findCommand('mode');
|
|
28
|
+
expect(mode).toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('bare /mode shows the current mode and usage', async () => {
|
|
32
|
+
const mode = findCommand('mode')!;
|
|
33
|
+
const result = await mode.execute([], { modelId: 'deepseek', setModelId: () => {}, clearHistory: () => {} });
|
|
34
|
+
expect(result.output).toContain('ask');
|
|
35
|
+
expect(result.output).toContain('auto');
|
|
36
|
+
expect(result.output).toContain('readonly');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('/mode auto switches the runtime mode', async () => {
|
|
40
|
+
const mode = findCommand('mode')!;
|
|
41
|
+
const ctx = { modelId: 'deepseek', setModelId: () => {}, clearHistory: () => {} };
|
|
42
|
+
await mode.execute(['auto'], ctx);
|
|
43
|
+
expect(getPermissionMode()).toBe('auto');
|
|
44
|
+
|
|
45
|
+
const result = await mode.execute(['readonly'], ctx);
|
|
46
|
+
expect(getPermissionMode()).toBe('readonly');
|
|
47
|
+
expect(result.output).toContain('readonly');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('/mode bogus reports the valid modes without switching', async () => {
|
|
51
|
+
const mode = findCommand('mode')!;
|
|
52
|
+
const result = await mode.execute(['bogus'], { modelId: 'deepseek', setModelId: () => {}, clearHistory: () => {} });
|
|
53
|
+
expect(result.output).toContain('Unknown mode');
|
|
54
|
+
expect(getPermissionMode()).toBe('ask');
|
|
55
|
+
});
|
|
56
|
+
});
|
package/src/commands.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ModelId } from './provider/provider.js';
|
|
2
2
|
import { getAvailableModels } from './provider/provider.js';
|
|
3
|
+
import { getPermissionMode, PERMISSION_MODES, setPermissionMode } from './agent/permission.js';
|
|
3
4
|
|
|
4
5
|
export interface CommandContext {
|
|
5
6
|
modelId: ModelId;
|
|
@@ -46,6 +47,27 @@ const commands: Command[] = [
|
|
|
46
47
|
output: 'Analyzing the project and writing TRANS.md…',
|
|
47
48
|
}),
|
|
48
49
|
},
|
|
50
|
+
{
|
|
51
|
+
name: 'mode',
|
|
52
|
+
description: 'Show or switch permission mode (ask | auto | readonly)',
|
|
53
|
+
execute: (args) => {
|
|
54
|
+
const target = args[0]?.toLowerCase();
|
|
55
|
+
if (!target) {
|
|
56
|
+
return {
|
|
57
|
+
output:
|
|
58
|
+
`Current permission mode: ${getPermissionMode()}\n` +
|
|
59
|
+
`Usage: /mode <${PERMISSION_MODES.join('|')}> — ` +
|
|
60
|
+
'ask prompts for mutating tools (read-only commands pass), ' +
|
|
61
|
+
'auto allows everything, readonly denies mutating tools.',
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (!PERMISSION_MODES.includes(target as never)) {
|
|
65
|
+
return { output: `Unknown mode: ${target}. Valid modes: ${PERMISSION_MODES.join(', ')}` };
|
|
66
|
+
}
|
|
67
|
+
setPermissionMode(target as never);
|
|
68
|
+
return { output: `Permission mode switched to ${target}` };
|
|
69
|
+
},
|
|
70
|
+
},
|
|
49
71
|
{
|
|
50
72
|
name: 'clear',
|
|
51
73
|
description: 'Clear screen',
|
package/src/screens/REPL.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { findCommand, getCommands, type CommandContext } from '../commands.js';
|
|
|
5
5
|
import { getAvailableModels, createProviderWithKey, createProvider, setPreferredModel, type ModelId } from '../provider/provider.js';
|
|
6
6
|
import { runAgentLoop, type PermissionRequest } from '../agent/loop.js';
|
|
7
7
|
import { createToolSession } from '../agent/tools/index.js';
|
|
8
|
+
import { getPermissionMode } from '../agent/permission.js';
|
|
8
9
|
import { walkFiles } from '../agent/tools/fs-utils.js';
|
|
9
10
|
import { applyCompletion, filterFiles, mentionQuery } from './file-completion.js';
|
|
10
11
|
import { renderDiff } from './diff-render.js';
|
|
@@ -120,7 +121,7 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
120
121
|
|
|
121
122
|
const askInTerminal = useCallback(
|
|
122
123
|
(request: PermissionRequest, signal: AbortSignal): Promise<boolean> => {
|
|
123
|
-
const mode =
|
|
124
|
+
const mode = getPermissionMode();
|
|
124
125
|
if (mode === 'auto') return Promise.resolve(true);
|
|
125
126
|
if (mode === 'readonly') return Promise.resolve(false);
|
|
126
127
|
return new Promise<boolean>(resolve => {
|
|
@@ -180,7 +181,8 @@ export default function REPL({ ctx }: REPLProps) {
|
|
|
180
181
|
appendChatOutput(`\n⚙ ${event.title}`);
|
|
181
182
|
lastWasTool = true;
|
|
182
183
|
} else if (event.type === 'tool_result') {
|
|
183
|
-
|
|
184
|
+
const marker = event.skippedPermission ? ' ✓ (read-only)' : event.ok ? ' ✓' : ' ✗';
|
|
185
|
+
appendChatOutput(marker);
|
|
184
186
|
if (event.diff) appendChatOutput(`\n${renderDiff(event.diff)}`);
|
|
185
187
|
lastWasTool = true;
|
|
186
188
|
}
|
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.1';
|