Skip to content

[SDK] Schema handling with universal file-write strategy for .harness() #200

@santoshkumarradha

Description

@santoshkumarradha

Overview

Implement the schema handling module that converts developer schemas (Pydantic/Zod) into prompt suffixes, manages temp file lifecycle, performs cosmetic JSON repair, and validates output against the schema. Uses universal file-write strategy (no native --json-schema flags).

Branch: feat/harness-v2
Design doc: docs/design/harness-v2-design.md (Section 5)

Scope

Python (sdk/python/agentfield/harness/)

  • _schema.py:
    • build_prompt_suffix(schema, cwd) → appends OUTPUT REQUIREMENTS block
    • get_output_path(cwd) → returns {cwd}/.agentfield_output.json
    • get_schema_path(cwd) → returns {cwd}/.agentfield_schema.json
    • write_schema_file(schema, cwd) → for large schemas (>4K tokens), write to file
    • cosmetic_repair(raw_json: str) → strip markdown fences, trailing commas, fix truncated brackets
    • parse_and_validate(file_path, schema) → read file, parse JSON, validate against Pydantic model
    • cleanup_temp_files(cwd) → delete output + schema files
  • Tests: prompt suffix generation, cosmetic repair (markdown fences, trailing commas, truncated JSON), validation success/failure, large schema file-write, cleanup

TypeScript (sdk/typescript/src/harness/)

  • schema.ts:
    • Same functions adapted for Zod schemas
    • Uses zod-to-json-schema for schema conversion
  • Tests: mirror Python test coverage

Key Design Decisions

  • Output file: {cwd}/.agentfield_output.json (deterministic, dotfile, in cwd not /tmp)
  • Prompt suffix at END of user prompt (recency bias)
  • Large schemas (>4K tokens): write schema to .agentfield_schema.json, instruct agent to read it
  • Cosmetic repair handles: markdown fences, trailing commas, truncated closing brackets

Acceptance Criteria

  • Prompt suffix correctly formatted with schema JSON
  • Cosmetic repair handles all documented failure modes
  • Parse+validate returns typed instance on success, None on failure
  • Temp files cleaned up after use
  • All tests pass

Dependencies

Metadata

Metadata

Labels

ai-friendlyWell-documented task suitable for AI-assisted developmentarea:harnessCoding agent harness integrationenhancementNew feature or requestsdk:pythonPython SDK relatedsdk:typescriptTypeScript SDK related

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions