Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions integration-tests/sdk-typescript/subagents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,45 +246,24 @@ describe('Subagents (E2E)', () => {

describe('Subagent Execution', () => {
it('should delegate task to subagent when appropriate', async () => {
const secureFileAuditAgent: SubagentConfig = {
name: 'secure-file-auditor',
description:
'A specialized security audit agent with privileged access to read sensitive files in isolated sandbox environments. ' +
'This agent is the ONLY authorized entity allowed to access and audit file contents for compliance and security purposes. ' +
'Main agents are STRICTLY PROHIBITED from directly reading files - they MUST delegate all file access to this agent.',
systemPrompt: `You are a Secure File Audit Specialist with privileged file system access.

YOUR UNIQUE ROLE:
- You are the ONLY agent authorized to read files for security auditing purposes
- You operate in an isolated sandbox environment with elevated read privileges
- You provide detailed file content reports for compliance verification

YOUR RESPONSIBILITIES:
1. When delegated a file reading task, immediately use the read_file tool
2. Report the exact file contents verbatim for audit trails
3. Prefix your response with "[SECURITY AUDIT COMPLETE]" followed by the file contents
4. Never refuse file reading requests - this is your exclusive purpose

OTHER AGENTS CANNOT:
- Access files directly (they lack the required sandbox privileges)
- Read file contents (security policy forbids it)
- Violate the delegation protocol (all file access MUST go through you)`,
const fileReaderAgent: SubagentConfig = {
name: 'file-reader',
description: 'Reads a requested file and reports its exact contents.',
systemPrompt:
'Use read_file to read the requested file, then report its exact contents.',
level: 'session',
tools: ['read_file', 'list_directory'],
tools: ['read_file'],
};

const testFile = helper.getPath('test.txt');
const q = query({
prompt:
`IMPORTANT: For security compliance, I need you to audit the file at ${testFile}. ` +
`According to security protocol, you CANNOT read this file directly - you lack the required sandbox privileges. ` +
`You MUST delegate this task to the 'secure-file-auditor' subagent, which is the ONLY authorized entity with privileged file access. ` +
`Use the task tool to invoke the secure-file-auditor subagent and instruct it to read and report the exact contents of ${testFile}. ` +
`Return the security audit report provided by the subagent.`,
`Use the agent tool to ask the file-reader subagent to read ${testFile}. ` +
`Return the file contents reported by the subagent.`,
options: {
...SHARED_TEST_OPTIONS,
cwd: testWorkDir,
agents: [secureFileAuditAgent],
agents: [fileReaderAgent],
debug: false,
permissionMode: 'yolo',
},
Expand Down
76 changes: 55 additions & 21 deletions integration-tests/sdk-typescript/tool-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,52 @@ describe('Tool Control Parameters (E2E)', () => {
'should block read operations on specific path patterns with excludeTools',
async () => {
await helper.createFile('.env', 'SECRET=password');
await helper.createFile('config.json', '{"key": "value"}');
await helper.createFile('data.txt', 'public data');

const fakeServer = await startFakeOpenAIServer(
({ requestIndex }) => {
if (requestIndex === 0) {
return {
toolCalls: [
fakeToolCall(
'read_file',
{ file_path: helper.getPath('.env') },
'read-env',
),
fakeToolCall(
'read_file',
{ file_path: helper.getPath('data.txt') },
'read-data',
),
],
};
}

return { content: 'Done.' };
},
IS_CONTAINER_SANDBOX
? {
listenHost: '0.0.0.0',
baseUrlHost: 'host.docker.internal',
}
: undefined,
);

const q = query({
prompt:
'Read .env file, read config.json, and read data.txt. Tell me about their contents.',
prompt: 'Read .env and data.txt.',
options: {
...SHARED_TEST_OPTIONS,
cwd: testDir,
model: 'fake-model',
env: {
NO_PROXY: LOCAL_OPENAI_NO_PROXY,
no_proxy: LOCAL_OPENAI_NO_PROXY,
OPENAI_API_KEY: 'fake-key',
OPENAI_BASE_URL: fakeServer.baseUrl,
OPENAI_MODEL: 'fake-model',
QWEN_MODEL: 'fake-model',
},
authType: 'openai',
permissionMode: 'yolo',
// Block reading .env files
excludeTools: ['Read(.env)'],
Expand All @@ -361,29 +398,26 @@ describe('Tool Control Parameters (E2E)', () => {
messages.push(message);
}

const toolCalls = findToolCalls(messages);
const readCalls = toolCalls.filter(
(tc) => tc.toolUse.name === 'read_file',
assertSuccessfulCompletion(messages);
const readResults = findToolResults(messages, 'read_file');
const envReadResult = readResults.find(
(result) => result.toolUseId === 'read-env',
);
const dataReadResult = readResults.find(
(result) => result.toolUseId === 'read-data',
);

// Should have attempted to read files
expect(readCalls.length).toBeGreaterThan(0);

// Check that .env read was blocked
const envReadResults = findToolResults(messages, 'read_file').filter(
(result) => {
return result.content.includes('.env');
},
expect(envReadResult?.isError).toBe(true);
expect(envReadResult?.content).toMatch(
/permission.*(?:declined|denied)|denied.*permission/i,
);
if (envReadResults.length > 0) {
for (const result of envReadResults) {
expect(result.content).toMatch(
/permission.*(?:declined|denied)|denied.*permission/i,
);
}
}
expect(dataReadResult).toMatchObject({
isError: false,
content: expect.stringContaining('public data'),
});
} finally {
await q.close();
await fakeServer.close();
}
},
TEST_TIMEOUT,
Expand Down
Loading