File size: 10,323 Bytes
c6d079d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import fs from 'node:fs';
import path from 'node:path';

import { describe, expect } from 'vitest';

import { AGENT_TOOL_NAME } from '@google/gemini-cli-core';
import { evalTest, TEST_AGENTS, TestRig } from './test-helper.js';

const INDEX_TS = 'export const add = (a: number, b: number) => a + b;\n';

/**
 * Helper to verify that a specific subagent was successfully invoked via the unified tool.
 */
async function expectSubagentCall(rig: TestRig, agentName: string) {
  await rig.expectToolCallSuccess(
    [AGENT_TOOL_NAME],
    undefined,
    (args: string) => {
      try {
        const parsed = JSON.parse(args);
        return parsed.agent_name === agentName;
      } catch {
        return false;
      }
    },
  );
}

/**
 * Helper to check if a subagent (either via unified tool or direct name) was called.
 */
function isSubagentCalled(toolLogs: any[], agentName: string): boolean {
  return toolLogs.some((l) => {
    if (l.toolRequest.name === AGENT_TOOL_NAME) {
      try {
        const args = JSON.parse(l.toolRequest.args);
        return args.agent_name === agentName;
      } catch {
        return false;
      }
    }
    return l.toolRequest.name === agentName;
  });
}

// A minimal package.json is used to provide a realistic workspace anchor.
// This prevents the agent from making incorrect assumptions about the environment
// and helps it properly navigate or act as if it is in a standard Node.js project.
const MOCK_PACKAGE_JSON = JSON.stringify(
  {
    name: 'subagent-eval-project',
    version: '1.0.0',
    type: 'module',
  },
  null,
  2,
);

function readProjectFile(
  rig: { testDir: string | null },
  relativePath: string,
): string {
  return fs.readFileSync(path.join(rig.testDir!, relativePath), 'utf8');
}

describe('subagent eval test cases', () => {
  /**
   * Checks whether the outer agent reliably utilizes an expert subagent to
   * accomplish a task when one is available.
   *
   * Note that the test is intentionally crafted to avoid the word "document"
   * or "docs". We want to see the outer agent make the connection even when
   * the prompt indirectly implies need of expertise.
   *
   * This tests the system prompt's subagent specific clauses.
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should delegate to user provided agent with relevant expertise',
    params: {
      settings: {
        experimental: {
          enableAgents: true,
        },
      },
    },
    prompt: 'Please update README.md with a description of this library.',
    files: {
      ...TEST_AGENTS.DOCS_AGENT.asFile(),
      'index.ts': INDEX_TS,
      'README.md': 'TODO: update the README.\n',
    },
    assert: async (rig, _result) => {
      await expectSubagentCall(rig, TEST_AGENTS.DOCS_AGENT.name);
    },
  });

  /**
   * Checks that the outer agent does not over-delegate trivial work when
   * subagents are available. This helps catch orchestration overuse.
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should avoid delegating trivial direct edit work',
    params: {
      settings: {
        experimental: {
          enableAgents: true,
          agents: {
            overrides: {
              generalist: { enabled: true },
            },
          },
        },
      },
    },
    prompt:
      'Rename the exported function in index.ts from add to sum and update the file directly.',
    files: {
      ...TEST_AGENTS.DOCS_AGENT.asFile(),
      'index.ts': INDEX_TS,
    },
    assert: async (rig, _result) => {
      const updatedIndex = readProjectFile(rig, 'index.ts');
      const toolLogs = rig.readToolLogs() as Array<{
        toolRequest: { name: string };
      }>;

      expect(updatedIndex).toContain('export const sum =');
      expect(isSubagentCalled(toolLogs, TEST_AGENTS.DOCS_AGENT.name)).toBe(
        false,
      );
      expect(isSubagentCalled(toolLogs, 'generalist')).toBe(false);
    },
  });

  /**
   * Checks that the outer agent prefers a more relevant specialist over a
   * broad generalist when both are available.
   *
   * This is meant to codify the "overusing Generalist" failure mode.
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should prefer relevant specialist over generalist',
    params: {
      settings: {
        experimental: {
          enableAgents: true,
          agents: {
            overrides: {
              generalist: { enabled: true },
            },
          },
        },
      },
    },
    prompt: 'Please add a small test file that verifies add(1, 2) returns 3.',
    files: {
      ...TEST_AGENTS.TESTING_AGENT.asFile(),
      'index.ts': INDEX_TS,
      'package.json': MOCK_PACKAGE_JSON,
    },
    assert: async (rig, _result) => {
      const toolLogs = rig.readToolLogs() as Array<{
        toolRequest: { name: string; args: string };
      }>;

      await expectSubagentCall(rig, TEST_AGENTS.TESTING_AGENT.name);
      expect(isSubagentCalled(toolLogs, 'generalist')).toBe(false);
    },
  });

  /**
   * Checks cardinality and decomposition for a multi-surface task. The task
   * naturally spans docs and tests, so multiple specialists should be used.
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should use multiple relevant specialists for multi-surface task',
    params: {
      settings: {
        experimental: {
          enableAgents: true,
          agents: {
            overrides: {
              generalist: { enabled: true },
            },
          },
        },
      },
    },
    prompt:
      'Add a short README description for this library and also add a test file that verifies add(1, 2) returns 3.',
    files: {
      ...TEST_AGENTS.DOCS_AGENT.asFile(),
      ...TEST_AGENTS.TESTING_AGENT.asFile(),
      'index.ts': INDEX_TS,
      'README.md': 'TODO: update the README.\n',
      'package.json': MOCK_PACKAGE_JSON,
    },
    assert: async (rig, _result) => {
      const toolLogs = rig.readToolLogs() as Array<{
        toolRequest: { name: string; args: string };
      }>;
      const readme = readProjectFile(rig, 'README.md');

      await expectSubagentCall(rig, TEST_AGENTS.DOCS_AGENT.name);
      await expectSubagentCall(rig, TEST_AGENTS.TESTING_AGENT.name);

      expect(readme).not.toContain('TODO: update the README.');
      expect(isSubagentCalled(toolLogs, 'generalist')).toBe(false);
    },
  });

  /**
   * Checks that the main agent can correctly select the appropriate subagent
   * from a large pool of available subagents (10 total).
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should select the correct subagent from a pool of 10 different agents',
    prompt: 'Please add a new SQL table migration for a user profile.',
    files: {
      ...TEST_AGENTS.DOCS_AGENT.asFile(),
      ...TEST_AGENTS.TESTING_AGENT.asFile(),
      ...TEST_AGENTS.DATABASE_AGENT.asFile(),
      ...TEST_AGENTS.CSS_AGENT.asFile(),
      ...TEST_AGENTS.I18N_AGENT.asFile(),
      ...TEST_AGENTS.SECURITY_AGENT.asFile(),
      ...TEST_AGENTS.DEVOPS_AGENT.asFile(),
      ...TEST_AGENTS.ANALYTICS_AGENT.asFile(),
      ...TEST_AGENTS.ACCESSIBILITY_AGENT.asFile(),
      ...TEST_AGENTS.MOBILE_AGENT.asFile(),
      'package.json': MOCK_PACKAGE_JSON,
    },
    assert: async (rig, _result) => {
      const toolLogs = rig.readToolLogs();
      await expectSubagentCall(rig, TEST_AGENTS.DATABASE_AGENT.name);

      // Ensure the generalist and other irrelevant specialists were not invoked
      const uncalledAgents = [
        TEST_AGENTS.DOCS_AGENT.name,
        TEST_AGENTS.TESTING_AGENT.name,
        TEST_AGENTS.CSS_AGENT.name,
        TEST_AGENTS.I18N_AGENT.name,
        TEST_AGENTS.SECURITY_AGENT.name,
        TEST_AGENTS.DEVOPS_AGENT.name,
        TEST_AGENTS.ANALYTICS_AGENT.name,
        TEST_AGENTS.ACCESSIBILITY_AGENT.name,
        TEST_AGENTS.MOBILE_AGENT.name,
      ];

      for (const agentName of uncalledAgents) {
        expect(isSubagentCalled(toolLogs, agentName)).toBe(false);
      }
      expect(isSubagentCalled(toolLogs, 'generalist')).toBe(false);
    },
  });

  /**
   * Checks that the main agent can correctly select the appropriate subagent
   * from a large pool of available subagents, even when many irrelevant MCP tools are present.
   *
   * This test includes stress tests the subagent delegation with ~80 tools.
   */
  evalTest('USUALLY_PASSES', {
    suiteName: 'default',
    suiteType: 'behavioral',
    name: 'should select the correct subagent from a pool of 10 different agents with MCP tools present',
    prompt: 'Please add a new SQL table migration for a user profile.',
    setup: async (rig) => {
      rig.addTestMcpServer('workspace-server', 'google-workspace');
    },
    files: {
      ...TEST_AGENTS.DOCS_AGENT.asFile(),
      ...TEST_AGENTS.TESTING_AGENT.asFile(),
      ...TEST_AGENTS.DATABASE_AGENT.asFile(),
      ...TEST_AGENTS.CSS_AGENT.asFile(),
      ...TEST_AGENTS.I18N_AGENT.asFile(),
      ...TEST_AGENTS.SECURITY_AGENT.asFile(),
      ...TEST_AGENTS.DEVOPS_AGENT.asFile(),
      ...TEST_AGENTS.ANALYTICS_AGENT.asFile(),
      ...TEST_AGENTS.ACCESSIBILITY_AGENT.asFile(),
      ...TEST_AGENTS.MOBILE_AGENT.asFile(),
      'package.json': MOCK_PACKAGE_JSON,
    },
    assert: async (rig, _result) => {
      const toolLogs = rig.readToolLogs();
      await expectSubagentCall(rig, TEST_AGENTS.DATABASE_AGENT.name);

      // Ensure the generalist and other irrelevant specialists were not invoked
      const uncalledAgents = [
        TEST_AGENTS.DOCS_AGENT.name,
        TEST_AGENTS.TESTING_AGENT.name,
        TEST_AGENTS.CSS_AGENT.name,
        TEST_AGENTS.I18N_AGENT.name,
        TEST_AGENTS.SECURITY_AGENT.name,
        TEST_AGENTS.DEVOPS_AGENT.name,
        TEST_AGENTS.ANALYTICS_AGENT.name,
        TEST_AGENTS.ACCESSIBILITY_AGENT.name,
        TEST_AGENTS.MOBILE_AGENT.name,
      ];

      for (const agentName of uncalledAgents) {
        expect(isSubagentCalled(toolLogs, agentName)).toBe(false);
      }
      expect(isSubagentCalled(toolLogs, 'generalist')).toBe(false);
    },
  });
});