gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Refactor Agent Input Validation to use AJV/JSON Schema

Open abhipatel12 opened this issue 11 months ago • 4 comments

Problem

Currently, DelegateToAgentTool relies on a manual switch statement to map our custom InputConfig types to Zod validators. This approach is brittle, duplicates validation logic, and limits us to a small subset of JSON Schema features (blocking the use of enum, oneOf, nested objects, etc.).

Goal

Refactor the agent input system to use standard JSONSchema7 definitions and leverage our existing AJV-based SchemaValidator. This unifies validation logic across tools and agents.

Implementation Plan

1. Extend SchemaValidator Update packages/core/src/utils/schemaValidator.ts to expose a schema validation method:

static validateSchema(schema: unknown): string | null {
  if (!schema) return null;
  const isValid = ajValidator.validateSchema(schema);
  return isValid ? null : ajValidator.errorsText(ajValidator.errors);
}

2. Update Type Definitions Replace the custom input mapping in packages/core/src/agents/types.ts with standard JSON Schema:

import type { JSONSchema7 } from 'json-schema';

export interface InputConfig {
  /** Standard JSON Schema for validation and tool generation */
  inputSchema: JSONSchema7;
}

3. Refactor DelegateToAgentTool Remove the Zod mapping logic in packages/core/src/agents/delegate-to-agent-tool.ts. Instead, validate schemas at registration and merge them directly:

// Proposed logic
const agentSchemas = definitions.map((def) => {
  const schemaError = SchemaValidator.validateSchema(def.inputConfig.inputSchema);
  if (schemaError) throw new Error(`Invalid schema for ${def.name}: ${schemaError}`);

  return {
    type: 'object',
    properties: {
      agent_name: { const: def.name, description: def.description },
      ...def.inputConfig.inputSchema.properties,
    },
    required: ['agent_name', ...(def.inputConfig.inputSchema.required || [])],
  };
});

Migration Checklist

  • [ ] Add validateSchema to SchemaValidator
  • [ ] Update InputConfig interface (consider temporary deprecation period for old format)
  • [ ] Refactor DelegateToAgentTool to consume JSON Schema
  • [ ] Update CodebaseInvestigatorAgent and other existing agents to the new schema format
  • [ ] Verify validation occurs at agent registration time

abhipatel12 avatar Dec 09 '25 22:12 abhipatel12

Hi , I will work on this.

liqzheng avatar Dec 10 '25 17:12 liqzheng

This seems more complex than I expected. I'll pass on this one for now.

liqzheng avatar Dec 10 '25 18:12 liqzheng

/assign

jonathanh1 avatar Dec 10 '25 22:12 jonathanh1

👋 @jonathanh1, you've been assigned to this issue! Thank you for taking the time to contribute. Make sure to check out our contributing guidelines.

gemini-cli[bot] avatar Dec 10 '25 22:12 gemini-cli[bot]

@jonathanh1 and @psinha40898 , thank you for the PRs! Because this issue is on the critical path for a major feature, it makes sense for a maintainer to tackle it. My apologies for the churn.

joshualitt avatar Jan 15 '26 19:01 joshualitt