Skip to content

Java: Implement @CopilotTool ergonomics#1792

Open
edburns wants to merge 31 commits into
mainfrom
edburns/1682-java-tool-ergonomics-review-draft-01
Open

Java: Implement @CopilotTool ergonomics#1792
edburns wants to merge 31 commits into
mainfrom
edburns/1682-java-tool-ergonomics-review-draft-01

Conversation

@edburns

@edburns edburns commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

PR 1792 Reviewer’s Guide — Java @CopilotTool Ergonomics

PR: #1792
Epic: #1682
ADR: java/docs/adr/adr-005-tool-definition.md

This PR introduces the Java high-level tool API (@CopilotTool + @Param) and compile-time metadata generation, while keeping low-level ToolDefinition.create(...) as the foundation.

What to optimize for in review

Please focus on API semantics, generated-invocation correctness, and schema fidelity rather than style. The main question is whether this design is robust enough for long-term Java SDK ergonomics and interoperability.

Scope mapped to epic child issues

Epic item Delivered in PR 1792
#1758 @CopilotTool and @Param annotations
#1759 Compile-time SchemaGenerator (TypeMirror -> JSON Schema source)
#1760 JSR-269 CopilotToolProcessor generating $$CopilotToolMeta
#1761 ToolDefinition.fromObject(...) / fromClass(...) registration bridge
#1762 E2E integration test and snapshot for ergonomic path
#1789 Follow-up item remains open; related hardening landed here (default/optional handling)

Commit stream reading order (recommended)

Ignore WIP/prompt commits and review in this logical order:

  1. feat(java): create @CopilotTool and @Param annotations (#1763)
  2. feat(java): add SchemaGenerator compile-time type-to-JSON-Schema utility (#1766)
  3. feat(java): Add CopilotToolProcessor annotation processor (task 4.3) (#1777)
  4. feat(java): Add ToolDefinition.fromObject() and fromClass() registration API (#1779)
  5. Add E2E integration test for ergonomic @CopilotTool + ToolDefinition.fromObject() API (#1787)
  6. Hardening commits: Optional extraction fix, static-tool qualified-name fix, java.time schema format hints, CodeQL cleanup, snapshot stabilization, test generation fixes.

File-level review map

Area Primary files
Public API surface java/src/main/java/com/github/copilot/tool/CopilotTool.java, java/src/main/java/com/github/copilot/tool/Param.java, java/src/main/java/com/github/copilot/rpc/ToolDefinition.java
Metadata provider contract java/src/main/java/com/github/copilot/tool/CopilotToolMetadataProvider.java
Processor/codegen java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java, java/src/main/resources/META-INF/services/javax.annotation.processing.Processor, java/src/main/java/module-info.java
Schema mapping java/src/main/java/com/github/copilot/tool/SchemaGenerator.java
Unit tests (processor) java/src/test/java/com/github/copilot/tool/CopilotToolProcessorTest.java, java/src/test/java/com/github/copilot/tool/SchemaGeneratorTest.java
Unit tests (runtime bridge) java/src/test/java/com/github/copilot/rpc/ToolDefinitionFromObjectTest.java + fixtures under java/src/test/java/com/github/copilot/rpc/fixtures/
E2E parity proof java/src/test/java/com/github/copilot/e2e/ErgonomicToolDefinitionIT.java, test/snapshots/tools/ergonomic_tool_definition.yaml

Architectural intent (as implemented)

  1. User-facing model: langchain4j-style method annotations.
  2. Implementation strategy: Micronaut-style compile-time processor/codegen, not reflection-first.
  3. Bridge: ToolDefinition.fromObject/fromClass loads generated $$CopilotToolMeta.
  4. No silent fallback: missing generated metadata throws explicit IllegalStateException.
  5. Low-level path preserved: ToolDefinition.create* remains available.

High-value review questions (actionable feedback requested)

  1. Is @Retention(RUNTIME) on @CopilotTool/@Param still the right long-term choice now that fromObject() is processor-only by default (no runtime fallback path)?
  2. Is requiring non-private tool methods the right constraint vs. adding MethodHandles-based access?
  3. Is the current default-value policy complete and safe?
    • required=true + defaultValue is compile-time error
    • defaults applied only when key is absent
    • generated path semantics match intended runtime behavior
  4. Is return-type handling policy correct for real agents?
    • String passthrough
    • void -> "Success"
    • CompletableFuture<String> passthrough (object-cast)
    • non-string results JSON-serialized
  5. Is exception policy acceptable in generated lambdas (RuntimeException wrapping during serialization)?
  6. Is SchemaGenerator coverage sufficient for v1 (primitives, boxed, collections, maps, records/classes, enums, sealed interfaces, java.time formats, Optional variants)?
  7. Is Param.name() interaction with generated extraction/schema intuitive and predictable for advanced use?
  8. Is the static-tool path (fromClass) now robust enough after qualified-class-name and instance-method guard fixes?
  9. Is ToolDefer.NONE -> null wire behavior the right compatibility choice?
  10. Any JPMS edge cases missed around generated metadata loading, module boundaries, or processor registration?

Deliberate non-goals / constraints in this PR

  • No broad reflection fallback in fromObject() when metadata is missing (fail fast instead).
  • No change to low-level tool-definition API contract.
  • Ergonomic API remains @CopilotExperimental.

Extra context from iteration artifacts

The prompt/design trail in copilot-sdk-01/1682-java-tool-ergonomics-prompts-remove-before-merge tracks the ignorance-reduction phase and key decisions that shaped this PR (especially annotation shape, default semantics, processor-only direction, and schema coverage expansions).

If you see a mismatch between ADR intent and implementation behavior, that is exactly the class of feedback I want.

Note on CopilotToolMetadataProvider

CopilotToolMetadataProvider<T> is the runtime seam between generated metadata and ToolDefinition.fromObject(...) loading.
The processor emits $$CopilotToolMeta classes that implement this interface and expose:

List<ToolDefinition> definitions(T instance, ObjectMapper mapper)

This keeps fromObject/fromClass decoupled from generated-class internals, and also allows advanced/manual implementations of the provider contract when needed.

Usage sample snippets (from copilot-sdk-01/.../simple-weather-demo)

1. pom.xml — experimental + annotation processor

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.15.0</version>
    <configuration>
        <compilerArgs>
            <arg>-Acopilot.experimental.allowed=true</arg>
        </compilerArgs>
        <annotationProcessorPaths>
            <path>
                <groupId>com.github</groupId>
                <artifactId>copilot-sdk-java</artifactId>
                <version>${copilot.sdk.version}</version>
            </path>
        </annotationProcessorPaths>
    </configuration>
</plugin>

2. Tool declaration with @CopilotTool

@CopilotTool("Get the current weather for a given city")
public String getWeather(
        @Param(value = "The city to get weather for", required = true) String city,
        @Param(value = "Temperature unit: celsius or fahrenheit", required = false, defaultValue = "celsius") String unit) {
    // ...
}

3. Registration via ToolDefinition.fromObject(...)

WeatherTools weatherTools = new WeatherTools();
List<ToolDefinition> toolDefs = ToolDefinition.fromObject(weatherTools);

SessionConfig sessionConfig = new SessionConfig()
        .setOnPermissionRequest(PermissionHandler.APPROVE_ALL)
        .setAvailableTools(new ToolSet().addCustom("*"))
        .setTools(toolDefs);

Copilot AI review requested due to automatic review settings June 24, 2026 20:59
@edburns edburns requested a review from a team as a code owner June 24, 2026 20:59
Comment thread java/src/main/java/com/github/copilot/tool/SchemaGenerator.java Fixed
Comment thread java/src/main/java/com/github/copilot/tool/SchemaGenerator.java Fixed
Comment thread java/src/main/java/com/github/copilot/tool/SchemaGenerator.java Fixed
Comment thread java/src/test/java/com/github/copilot/e2e/ErgonomicTestTools.java Fixed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an ergonomic, annotation-based Java tool definition API (@CopilotTool / @Param) backed by a JSR-269 annotation processor that generates $$CopilotToolMeta companion classes, enabling ToolDefinition.fromObject(...) (and fromClass(...)) to produce runnable ToolDefinition instances with generated JSON Schemas and handlers.

Changes:

  • Add @CopilotTool / @Param annotations and CopilotToolProcessor to generate tool metadata classes at compile time.
  • Add ToolDefinition.fromObject(...) / fromClass(...) runtime entry points to load generated tool definitions.
  • Add unit/processor tests plus a Java E2E snapshot + integration test to validate wire-level behavior.
Show a summary per file
File Description
test/snapshots/tools/ergonomic_tool_definition.yaml New replay snapshot for ergonomic tool definition scenario
java/src/test/java/com/github/copilot/tool/SchemaGeneratorTest.java Compilation-testing coverage for schema generator outputs
java/src/test/java/com/github/copilot/tool/CopilotToolProcessorTest.java Processor compilation tests for generated meta + validation errors
java/src/test/java/com/github/copilot/tool/CopilotToolAnnotationTest.java Annotation retention/targets/defaults verification
java/src/test/java/com/github/copilot/rpc/ToolDefinitionFromObjectTest.java E2E-style unit tests for ToolDefinition.fromObject(...) behavior
java/src/test/java/com/github/copilot/rpc/fixtures/SimpleTools$$CopilotToolMeta.java Hand-written fixture mimicking generated meta output (simple tools)
java/src/test/java/com/github/copilot/rpc/fixtures/SimpleTools.java Fixture tool class annotated with @CopilotTool / @Param
java/src/test/java/com/github/copilot/rpc/fixtures/OverrideTools$$CopilotToolMeta.java Fixture meta for override flag behavior
java/src/test/java/com/github/copilot/rpc/fixtures/OverrideTools.java Fixture tool class for override flag
java/src/test/java/com/github/copilot/rpc/fixtures/MultiReturnTools$$CopilotToolMeta.java Fixture meta for return-type handling patterns
java/src/test/java/com/github/copilot/rpc/fixtures/MultiReturnTools.java Fixture tool class for return-type handling patterns
java/src/test/java/com/github/copilot/rpc/fixtures/DefaultValueTools$$CopilotToolMeta.java Fixture meta for default parameter values
java/src/test/java/com/github/copilot/rpc/fixtures/DefaultValueTools.java Fixture tool class for default parameter values
java/src/test/java/com/github/copilot/rpc/fixtures/DateTimeTools$$CopilotToolMeta.java Fixture meta for java.time argument conversion
java/src/test/java/com/github/copilot/rpc/fixtures/DateTimeTools.java Fixture tool class using LocalDateTime parameter
java/src/test/java/com/github/copilot/rpc/fixtures/ArgCoercionTools$$CopilotToolMeta.java Fixture meta for mixed-argument coercion
java/src/test/java/com/github/copilot/rpc/fixtures/ArgCoercionTools.java Fixture tool class for mixed-argument coercion
java/src/test/java/com/github/copilot/e2e/ErgonomicToolDefinitionIT.java Failsafe IT validating end-to-end ergonomic tool usage with snapshot
java/src/test/java/com/github/copilot/e2e/ErgonomicTestTools$$CopilotToolMeta.java Hand-written E2E meta fixture mimicking processor output
java/src/test/java/com/github/copilot/e2e/ErgonomicTestTools.java Tool fixture class for E2E ergonomic test
java/src/main/resources/META-INF/services/javax.annotation.processing.Processor Registers CopilotToolProcessor as an annotation processor
java/src/main/java/module-info.java Exports com.github.copilot.tool and provides processor in module-info
java/src/main/java/com/github/copilot/tool/SchemaGenerator.java New compile-time JSON-schema source generator
java/src/main/java/com/github/copilot/tool/Param.java New @Param annotation
java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java New processor generating $$CopilotToolMeta classes
java/src/main/java/com/github/copilot/tool/CopilotToolMetadataProvider.java Contract interface for generated/manual metadata providers
java/src/main/java/com/github/copilot/tool/CopilotTool.java New @CopilotTool annotation
java/src/main/java/com/github/copilot/rpc/ToolDefinition.java Adds fromObject(...) / fromClass(...) + mapper holder
java/src/main/java/com/github/copilot/rpc/ToolDefer.java Adds NONE sentinel + serialization safety adjustments
java/mvnw Adds Maven wrapper script under java/

Copilot's findings

  • Files reviewed: 29/30 changed files
  • Comments generated: 3

Comment thread java/src/main/java/com/github/copilot/tool/SchemaGenerator.java Outdated
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@rotationtv1-crypto

Copy link
Copy Markdown

edburns/1682-java-tool-ergonomics-review-draft-01

@edburns edburns changed the title Implement @CopilotTool ergonomics Java: Implement @CopilotTool ergonomics Jun 25, 2026
@github-actions

This comment has been minimized.

edburns and others added 19 commits June 25, 2026 14:24
Your branch is up to date with 'upstream/edburns/1682-java-tool-ergonomics'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   1682-java-tool-ergonomics-prompts-remove-before-merge/20260618-prompts.md

Signed-off-by: Ed Burns <edburns@microsoft.com>
- Add NONE constant to ToolDefer enum for annotation default value
- Create com.github.copilot.tool.CopilotTool annotation
- Create com.github.copilot.tool.Param annotation
- Export com.github.copilot.tool package in module-info.java
- Add CopilotToolAnnotationTest verifying retention, targets, defaults

Closes #1758
NONE is an annotation-only sentinel for @copilotTool(defer=...) defaults.
Its @jsonvalue now returns null so @JsonInclude(NON_NULL) omits it from
the JSON-RPC payload, matching the nullable/optional semantics used by
all other SDKs (.NET CopilotToolDefer?, Node defer?, Go omitempty,
Python | None, Rust Option<DeferMode>).
* WIP Phase 4.1

* Remove prompts, pre-merge

* fix(java): correct ToolDefer.NONE Javadoc on @jsonvalue null semantics

Clarify that @jsonvalue returning null does not cause field omission
by @JsonInclude(NON_NULL) — it only changes the leak from "" to null.
The primary protection is mapping NONE to a null field reference before
constructing ToolDefinition (responsibility of the annotation processor
and ToolDefinition.fromObject()).

* fix(java): address three review comments

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* Revert "Remove prompts, pre-merge"

This reverts commit a4fe9b2.

---------

Co-authored-by: Ed Burns <edburns@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
…ity (#1766)

* Initial plan

* feat(java): add SchemaGenerator compile-time type-to-JSON-Schema utility

Creates SchemaGenerator.java that maps javax.lang.model TypeMirror
instances to JSON Schema source code literals (Map.of(...) expressions).

Implements all 24 type mappings from the specification including:
- Primitives and boxed types (int/Integer, long/Long, etc.)
- String, UUID, OffsetDateTime
- Collections (List<T>, Collection<T>, Set<T>)
- Maps (Map<String, V> with typed values)
- Arrays (String[])
- Enums (with constant enumeration)
- Records and POJOs (with properties/required)
- Optional<T>, OptionalInt, OptionalDouble
- Sealed interfaces (oneOf)
- JsonNode and Object (any)

Also adds SchemaGeneratorTest using compilation-testing approach
with javax.tools.JavaCompiler to exercise the generator at compile time.

Closes #1759

* fix: address code review - remove unused param, handle all primitive types

* fix(java): correct SimpleJavaFileObject override - getCharContent not getContent

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* spotless

* Remove .class files generated by test

* spotless

* fix: use Map.ofEntries for properties to avoid Map.of 10-entry limit

Address review comment r3461777483: Map.of() only supports up to 10
key-value pairs. Switch properties maps in SchemaGenerator to use
Map.ofEntries(Map.entry(...), ...) so records/POJOs/methods with >10
fields won't cause generated source compilation failures.

Update SchemaGeneratorTest expectations to match the new format.

* fix: add missing Byte/Short/Character boxed type mappings

Address review comment r3461777428: Byte and Short now map to
"integer", Character maps to "string", matching their primitive
equivalents. Add tests for all three.

* fix: add missing OptionalLong mapping in generateDeclaredTypeSchema

Address review comment r3461777459: OptionalLong was handled in
isOptionalType/unwrapOptional but missing from generateDeclaredTypeSchema,
causing it to fall through to POJO introspection when used as a direct
return type. Add the mapping and tests for OptionalInt, OptionalLong,
and OptionalDouble.

* fix: correct misleading @JsonSubTypes comment on sealed interface handling

Address review comment r3461777579: the implementation uses
getPermittedSubclasses() (Java sealed types), not Jackson annotations.

* test: add sealed interface test for oneOf schema generation

Address review comment r3461777685: the processor had special handling
for TestSealed* types but no test exercised generateSealedSchema().
Add a test with a sealed interface (TestSealedShape) and two record
permits (Circle, Rect) verifying the oneOf schema output.

* test: add >10-field record test proving Map.ofEntries compiles

Address review comment r3461777706: add a test with an 11-component
record that verifies the generated Map.ofEntries(...) expression
actually compiles, proving the Map.of 10-entry limit fix works
end-to-end.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>
…1777)

* Resume 1682 iterating

* Phase 03 answer questions

* On branch edburns/1682-java-tool-ergonomics
Your branch is up to date with 'upstream/edburns/1682-java-tool-ergonomics'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   1682-java-tool-ergonomics-prompts-remove-before-merge/20260618-prompts.md

Signed-off-by: Ed Burns <edburns@microsoft.com>

* WIP: Phase 3. Question 3.4

* WIP: Phase 3. Question 3.6

* WIP: Phase 3. Question 3.6: Answer

* Answer 3.7

* Resolve 3.8

* Initial plan

* feat(java): create @copilotTool and @Param annotations with tests

- Add NONE constant to ToolDefer enum for annotation default value
- Create com.github.copilot.tool.CopilotTool annotation
- Create com.github.copilot.tool.Param annotation
- Export com.github.copilot.tool package in module-info.java
- Add CopilotToolAnnotationTest verifying retention, targets, defaults

Closes #1758

* spotless

* fix(java): make ToolDefer.NONE serialize as null to prevent wire leak

NONE is an annotation-only sentinel for @copilotTool(defer=...) defaults.
Its @jsonvalue now returns null so @JsonInclude(NON_NULL) omits it from
the JSON-RPC payload, matching the nullable/optional semantics used by
all other SDKs (.NET CopilotToolDefer?, Node defer?, Go omitempty,
Python | None, Rust Option<DeferMode>).

* WIP Phase 4.1

* feat(java): create @copilotTool and @Param annotations (#1763)

* WIP Phase 4.1

* Remove prompts, pre-merge

* fix(java): correct ToolDefer.NONE Javadoc on @jsonvalue null semantics

Clarify that @jsonvalue returning null does not cause field omission
by @JsonInclude(NON_NULL) — it only changes the leak from "" to null.
The primary protection is mapping NONE to a null field reference before
constructing ToolDefinition (responsibility of the annotation processor
and ToolDefinition.fromObject()).

* fix(java): address three review comments

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* Revert "Remove prompts, pre-merge"

This reverts commit a4fe9b2.

---------

Co-authored-by: Ed Burns <edburns@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* Initial plan

* feat(java): add SchemaGenerator compile-time type-to-JSON-Schema utility (#1766)

* Initial plan

* feat(java): add SchemaGenerator compile-time type-to-JSON-Schema utility

Creates SchemaGenerator.java that maps javax.lang.model TypeMirror
instances to JSON Schema source code literals (Map.of(...) expressions).

Implements all 24 type mappings from the specification including:
- Primitives and boxed types (int/Integer, long/Long, etc.)
- String, UUID, OffsetDateTime
- Collections (List<T>, Collection<T>, Set<T>)
- Maps (Map<String, V> with typed values)
- Arrays (String[])
- Enums (with constant enumeration)
- Records and POJOs (with properties/required)
- Optional<T>, OptionalInt, OptionalDouble
- Sealed interfaces (oneOf)
- JsonNode and Object (any)

Also adds SchemaGeneratorTest using compilation-testing approach
with javax.tools.JavaCompiler to exercise the generator at compile time.

Closes #1759

* fix: address code review - remove unused param, handle all primitive types

* fix(java): correct SimpleJavaFileObject override - getCharContent not getContent

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* spotless

* Remove .class files generated by test

* spotless

* fix: use Map.ofEntries for properties to avoid Map.of 10-entry limit

Address review comment r3461777483: Map.of() only supports up to 10
key-value pairs. Switch properties maps in SchemaGenerator to use
Map.ofEntries(Map.entry(...), ...) so records/POJOs/methods with >10
fields won't cause generated source compilation failures.

Update SchemaGeneratorTest expectations to match the new format.

* fix: add missing Byte/Short/Character boxed type mappings

Address review comment r3461777428: Byte and Short now map to
"integer", Character maps to "string", matching their primitive
equivalents. Add tests for all three.

* fix: add missing OptionalLong mapping in generateDeclaredTypeSchema

Address review comment r3461777459: OptionalLong was handled in
isOptionalType/unwrapOptional but missing from generateDeclaredTypeSchema,
causing it to fall through to POJO introspection when used as a direct
return type. Add the mapping and tests for OptionalInt, OptionalLong,
and OptionalDouble.

* fix: correct misleading @JsonSubTypes comment on sealed interface handling

Address review comment r3461777579: the implementation uses
getPermittedSubclasses() (Java sealed types), not Jackson annotations.

* test: add sealed interface test for oneOf schema generation

Address review comment r3461777685: the processor had special handling
for TestSealed* types but no test exercised generateSealedSchema().
Add a test with a sealed interface (TestSealedShape) and two record
permits (Circle, Rect) verifying the oneOf schema output.

* test: add >10-field record test proving Map.ofEntries compiles

Address review comment r3461777706: add a test with an 11-component
record that verifies the generated Map.ofEntries(...) expression
actually compiles, proving the Map.of 10-entry limit fix works
end-to-end.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>

* WIP 4.3

* Initial plan

* feat(java): Add CopilotToolProcessor annotation processor (task 4.3)

Implements JSR 269 annotation processor that finds @CopilotTool-annotated
methods and generates $$CopilotToolMeta companion classes containing tool
definitions, JSON Schema, and invocation lambdas.

Key features:
- snake_case tool name conversion from camelCase method names
- Access level enforcement (compile error for private methods)
- Return type handling (String, void, CompletableFuture<String>, etc.)
- Argument deserialization (direct cast for primitives/String, convertValue for complex)
- @Param description and defaultValue support in schema
- ToolDefer support (NONE maps to null/regular create)
- overridesBuiltInTool and skipPermission support

Also includes comprehensive test suite using javax.tools.JavaCompiler
programmatic compilation.

Closes #1760

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* fix: Address code review feedback

- Use fully qualified type names in generated code for type safety
- Fix Files.walk() resource leak in test with try-with-resources
- Rename exception variables for clarity

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* fix: Fix Spotless formatting and test classpath for JDK 17

- Remove unused Collections import
- Reformat boolean expressions: && at start of continuation lines
- Reformat ternary: ? at start of continuation line
- Reformat .replace() chain with one call per line
- Fix hasErrorContaining stream method chain formatting
- Fix resolveClasspath() to use System.getProperty("java.class.path")
  first, ensuring Jackson and all test deps are available when compiling
  generated $$CopilotToolMeta code

* fix: Fix remaining Spotless violations and test classpath resolution

- Merge propertyEntries.add() onto one line per formatter requirement
- Fix sb.append() chain formatting to match Eclipse formatter output
- Revert escapeJava to original line-breaking style (formatter preference)
- Fix resolveClasspath() to combine system classpath with CodeSource
  paths from key classes (SDK, Jackson, RPC types) ensuring all
  dependencies are available for javac in the annotation processor test

* fix: Add jackson-core and jackson-annotations to test classpath

The generated 6342CopilotToolMeta code uses ObjectMapper which requires
jackson-core (Versioned, JsonFactory) and jackson-annotations at
compile time. Add these transitive dependencies to the key classes
list so their CodeSource paths are included in the javac classpath.

* fix: Fix Spotless formatting for keyClasses array initializer

* fix(java): Pass ObjectMapper as parameter in generated $$CopilotToolMeta contract

Address PR #1777 review comment (r3463252393): the generated
$$CopilotToolMeta class was using `new ObjectMapper()`, which lacks
the SDK Jackson configuration (JavaTimeModule, NON_NULL inclusion,
lenient unknown-properties). This would break tool argument coercion
and return serialization at runtime for java.time.* and other types.

Instead of embedding a bare or configured ObjectMapper in the
generated code, change the generated `definitions()` method signature
from:
    definitions(MyTools instance)
to:
    definitions(MyTools instance, ObjectMapper mapper)

This establishes an internal contract: the caller (the future
ToolDefinition.fromObject() in issue #1761) is responsible for
supplying a properly configured mapper via reflective invocation.
The generated code uses `mapper` for all convertValue() and
writeValueAsString() calls.

Benefits:
- No DRY violation (mapper config stays canonical in JsonRpcClient)
- No new public API exposing ObjectMapper
- No package-visibility workarounds
- Clean separation: generated code declares its needs, caller supplies

Issue #1761 description has been updated to document this contract
so the implementing agent knows to pass ObjectMapper as the second
argument when reflectively invoking definitions().

* fix(java): restrict single-param shortcut to records only

Address review comment on PR #1777: the isRecordOrPojo heuristic
incorrectly triggered for JDK container types (List, Map, etc.)
when used as a single tool parameter. For example, a tool with
parameter List<String> would attempt to deserialize the entire
arguments object as a List, failing at runtime.

Replace the heuristic with a deterministic check: only Java records
qualify for the getArgumentsAs() shortcut. Records are immutable
data carriers with compiler-guaranteed component lists, making them
safe for whole-object deserialization. POJOs and all other class
types now fall through to the per-field extraction path, which
always works correctly.

Removed isSimpleType() helper which was only used by the old
heuristic.

* fix(java): emit typed default values in JSON Schema

Address review comment on PR #1777: @Param(defaultValue=...) was
always emitted as a JSON string in the generated schema's 'default'
field, making numeric and boolean defaults the wrong type (e.g.,
"10" instead of 10, "true" instead of true).

Changes:
- withMeta helper: String defaultValue -> Object defaultValue
- buildPropertySchema: reuse generateDefaultLiteral() to emit typed
  Java literals (int, boolean, etc.) instead of always quoting
- Add test emitsTypedDefaultValuesInSchema verifying int -> 10,
  boolean -> true, String -> "hello" in generated code

* fix(java): fix double 61059CopilotToolMeta suffix in test helper

Address review comment on PR #1777: getGeneratedSource() fallback
search appended 61059CopilotToolMeta to a simpleName that already
contained it, producing MyTools$$CopilotToolMeta$$CopilotToolMeta.
Simplify to just match on 'class <simpleName>'.

* fix(java): use record constructor for independent flag combination

Address SDK Consistency Review on PR #1777: the if/else if chain
in writeToolDefinition silently dropped combined annotation flags
(e.g., overridesBuiltInTool + skipPermission + defer). All other
SDKs support combining these flags simultaneously.

Replace the factory method dispatch with a direct call to the
ToolDefinition record constructor, which accepts all seven fields
independently. Each flag is now emitted as its own argument:
Boolean.TRUE or null for overridesBuiltInTool/skipPermission,
ToolDefer.X or null for defer.

Add test generatesCombinedFlags verifying all three flags appear
in generated code when set together.

---------

Signed-off-by: Ed Burns <edburns@microsoft.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Copilot AI and others added 11 commits June 25, 2026 14:25
…ion API (#1779)

* Initial plan

* feat(java): Add ToolDefinition.fromObject() and fromClass() static methods

Adds static methods that load processor-generated $$CopilotToolMeta
classes and return List<ToolDefinition> with fully working tool
definitions (schema + invocation handlers).

- fromObject(Object): discovers tools from an instance with @copilotTool methods
- fromClass(Class<?>): discovers tools from a class with static @copilotTool methods
- Private getConfiguredMapper(): provides ObjectMapper matching JsonRpcClient config
- Throws IllegalStateException with helpful message if generated class not found
- Both methods annotated with @CopilotExperimental

Includes comprehensive test suite (ToolDefinitionFromObjectTest) covering:
- Basic discovery and schema verification
- Handler invocation for String, void, and CompletableFuture returns
- Argument coercion with primitives, String, boolean, and enums
- Default value handling when arguments are omitted
- Error case for missing generated class
- java.time argument deserialization (validates JavaTimeModule contract)
- Override tool flag propagation
- ToolDefer.NONE → null mapping (defer absent from JSON output)

Closes #1761

Co-authored-by: edburns <75821+edburns@users.noreply.github.com>

* fix: replace misleading generated-file comment in test fixtures

The $$CopilotToolMeta test fixtures are hand-written, not processor-
generated. Update the header comment to say so accurately.
Also fix Spotless formatting in CopilotToolProcessor.java.

Addresses PR review comment about test Javadoc inaccuracy.

* fix: introduce CopilotToolMetadataProvider interface to eliminate setAccessible

Replace reflective Method.invoke + setAccessible(true) in
ToolDefinition.loadDefinitions() with a typed interface cast.
Generated $$CopilotToolMeta classes now implement
CopilotToolMetadataProvider<T>, making them JPMS-safe and
removing the InaccessibleObjectException risk.

Addresses review comment r3468393716.

* fix: validate fromClass() rejects instance @copilotTool methods

fromClass() now scans for non-static @copilotTool methods and throws
IllegalArgumentException with an actionable message listing the
offending methods and directing users to fromObject() instead.
Prevents hard-to-diagnose NullPointerException at invocation time.

Addresses review comment r3468393764.

* fix: use parsed JSON tree for defer-absence assertion

Replace raw json.contains("defer") substring search with
ObjectNode.has("defer") to avoid false positives if another
field ever contains the substring.

Addresses review comment r3468393829.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edburns <75821+edburns@users.noreply.github.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>
…fromObject() API (#1787)

* Initial plan

* Initial plan

* Initial plan

* Add E2E integration test for ergonomic @copilotTool + ToolDefinition.fromObject() API

Create ErgonomicToolDefinitionIT that proves the ergonomic annotation-based
API produces identical wire behavior to the low-level ToolDefinition.create()
API, tested against the replay proxy.

Files added:
- test/snapshots/tools/ergonomic_tool_definition.yaml (identical to
  low_level_tool_definition.yaml since wire format is the same)
- java/src/test/java/com/github/copilot/e2e/ErgonomicToolDefinitionIT.java
- java/src/test/java/com/github/copilot/e2e/ErgonomicTestTools.java
- java/src/test/java/com/github/copilot/e2e/ErgonomicTestTools$$CopilotToolMeta.java

Closes #1762

* spotless

* fix: use passed ObjectMapper for record-parameter conversion

The single-record-parameter shortcut in CopilotToolProcessor generated
invocation.getArgumentsAs() which uses an unconfigured ObjectMapper
internally (no JavaTimeModule, no SDK settings). Switch to
mapper.convertValue(args, RecordType.class) which uses the
SDK-configured mapper passed to the definitions() method.

Addresses review comment r3469523760.

* fix: exclude Optional types from required list in generated schema

CopilotToolProcessor.generateSchemaWithParamMetadata() now checks if
a parameter type is Optional/OptionalInt/OptionalLong/OptionalDouble
before adding it to the JSON Schema required list. This aligns with
SchemaGenerator which already treats these types as optional.

Addresses review comment r3469523801.

* fix: correct misleading Javadoc in ToolDefinitionFromObjectTest

The class-level Javadoc incorrectly stated that the annotation processor
generates $$CopilotToolMeta fixtures during test compilation. In reality,
the module has <proc>none</proc> and these fixtures are hand-written
classes under com.github.copilot.rpc.fixtures.

Addresses review comment r3469523833.

* fix: remove unused grep override tool from E2E test

The ErgonomicToolDefinitionIT snapshot only exercises set_current_phase
and search_items. The grep tool (with overridesBuiltInTool=true) was
never invoked, making it dead code that contradicted the PR description.

Addresses review comment r3469523851.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Ed Burns <edburns@microsoft.com>
- CopilotToolProcessor.writeMetaClass: remove unused 'classElement' param
- SchemaGenerator.isOptionalType: remove unused 'typeUtils' and 'elementUtils'
- SchemaGenerator.unwrapOptional: remove unused 'elementUtils'
- ErgonomicTestTools.searchItems: use 'keyword' param in return value
The searchItems tool now includes the keyword in its response, so update
the replay proxy snapshot to expect the new format.
For static methods, the processor now generates
ClassName.method(...) instead of instance.method(...), making the
generated code clearer and avoiding compiler warnings about accessing
static members via instance references.

Adds StaticTools fixture and fromClass_staticToolInvocation test.
- LocalDateTime, Instant, ZonedDateTime → format: date-time
- LocalDate → format: date
- LocalTime → format: time

These hints tell the LLM what string format to produce for date/time
parameters. Previously only OffsetDateTime was mapped.

Adds SchemaGeneratorTest cases for each new type mapping.
The processor now generates null-check + wrapping code for Optional,
OptionalInt, OptionalLong, and OptionalDouble parameters instead of
incorrectly calling mapper.convertValue(..., Optional.class).

For Optional<T>, extracts the inner value using type-appropriate
coercion then wraps with Optional.of()/Optional.empty().
For OptionalInt/Long/Double, uses the primitive Number extraction
then wraps with the corresponding OptionalX.of()/empty().

Adds CopilotToolProcessorTest for generated code verification and
ToolDefinitionFromObjectTest for end-to-end handler invocation with
both present and absent optional values.
…#1799)

* Fix Java tool-processor test generation and stabilize session-id test

Address the Java test failures observed in the Java 17 surefire/failsafe run by fixing how annotation-processing output is discovered in CopilotToolProcessor tests and by hardening one timing-sensitive session test.

Changes included:

- CopilotToolProcessor: resolve @copilotTool elements via TypeElement lookup and reuse that element list through validation and generation passes, making annotation discovery robust across compiler/module contexts.

- CopilotToolProcessorTest: force annotation processing in the in-memory compile harness (-proc:full, explicit processor), close the file manager with try-with-resources, and add a collecting forwarding file manager that captures generated source content from getJavaFileForOutput to avoid missing generated CopilotToolMeta classes in tests.

- CopilotSessionTest#testShouldGetLastSessionId: add bounded retry for session creation (including timeout and execution-timeout-cause handling) to absorb transient startup delays while preserving failure behavior on persistent errors.

Result:

- CopilotToolProcessorTest now consistently observes generated CopilotToolMeta output and passes.

- The full requested Maven workflow (jacoco prepare/report + surefire + failsafe under Java 17, with prior Java 25 compile) completes successfully.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Spotless

* Avoid leaking session.

The retry on session creation uses `future.get(timeout)` but does not cancel the in-flight `createSession` future when a timeout occurs. If attempt 1 eventually completes after attempt 2 starts, it can leave an orphaned session registered in the client (and potentially race `getLastSessionId` persistence), reintroducing flakiness and leaking resources. Capture the future and cancel it on timeout before retrying.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Add abort-session snapshot variant for interrupted tool calls

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@edburns edburns force-pushed the edburns/1682-java-tool-ergonomics-review-draft-01 branch from ebff5aa to 60eb094 Compare June 25, 2026 18:26
@github-actions

This comment has been minimized.

modified:   java/src/main/java/com/github/copilot/tool/CopilotToolProcessor.java
modified:   java/src/main/java/com/github/copilot/tool/Param.java
modified:   java/src/main/java/com/github/copilot/tool/SchemaGenerator.java

- Add `CopilotExperimental` more liberally
@github-actions

Copy link
Copy Markdown
Contributor

Cross-SDK Consistency Review ✅

This PR implements Java-idiomatic ergonomic tool definition (@CopilotTool, @Param, annotation processor) and is consistent with the equivalent features already present in all other SDKs.

Feature Parity Matrix

Feature Java (this PR) Python Node.js .NET Go Rust
Ergonomic tool definition @CopilotTool annotation @define_tool decorator defineTool() CopilotTool.DefineTool() DefineTool[T]() define_tool()
overridesBuiltInTool ✅ annotation attr ✅ kwarg ✅ config field CopilotToolOptions ✅ struct field ✅ builder method
skipPermission ✅ annotation attr ✅ kwarg ✅ config field CopilotToolOptions ✅ struct field ✅ builder method
defer ✅ annotation attr ✅ kwarg ✅ config field CopilotToolOptions ✅ struct field ✅ builder method
Schema auto-generation ✅ via SchemaGenerator ✅ via Pydantic ✅ via Zod ✅ via AIFunctionFactory ✅ via jsonschema-go ✅ via schemars

Observations

  1. All three tool options (overridesBuiltInTool, skipPermission, defer) are properly surfaced in the @CopilotTool annotation, matching the options available in Python, Node.js, and .NET helper APIs.

  2. ToolDefer.NONE sentinel is a Java-specific necessity (annotation defaults cannot be null). The Javadoc clearly documents that this must be mapped to null before being forwarded to ToolDefinition, which the annotation processor correctly handles. Other SDKs use the language's natural null/None/Optional semantics.

  3. @Param annotation (description, name override, required, defaultValue) is Java-specific but consistent in spirit with Pydantic Field(...) in Python, JSON struct tags in Go, and [Description] / [JsonPropertyName] attributes in .NET.

  4. All changes are scoped to java/ — no other SDK implementations are affected.

No cross-SDK consistency issues found. The Java annotation-based ergonomics are a well-designed language-idiomatic equivalent of the tool definition helpers in all other SDKs.

Generated by SDK Consistency Review Agent for issue #1792 · sonnet46 1.9M ·

generateOptionalExtraction(sb, paramName, varName, paramType);
} else {
sb.append(" ").append(getTypeString(paramType)).append(" ").append(varName)
.append(" = ").append(generateArgExtractionFromMap(paramName, paramType)).append(";\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional primitives NPE at runtime. A @Param(required=false) int limit is correctly dropped from the schema's required, but this branch still emits int limit = ((Number) args.get("limit")).intValue();. When the model omits the arg, args.get("limit") is null and the unboxing throws NullPointerException: Cannot invoke "java.lang.Number.intValue()" because ... Map.get(Object) is null (confirmed by running the real processor).

A primitive simply can't represent "absent." I'd reject this at compile time — emit an error telling the user to use a boxed type (Integer), an Optional, or supply a defaultValue — which matches the existing compile-time validations for private methods and required+default conflicts.

String typeName = getTypeString(params.get(0).asType());
String paramName = params.get(0).getSimpleName().toString();
sb.append(" ").append(typeName).append(" ").append(paramName)
.append(" = mapper.convertValue(args, ").append(typeName).append(".class);\n");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single-record tools are broken — the schema and the handler disagree. generateSchemaWithParamMetadata nests the record under the parameter name (properties: { req: { ...fields... } }, required: [req]), but here the handler does mapper.convertValue(args, SearchArgs.class) against the whole args map. So when the model sends the schema-conformant {"req":{"query":"hello","limit":7}}, the record comes back with null/0 fields — silent data loss with the SDK's lenient mapper (FAIL_ON_UNKNOWN_PROPERTIES=false), or UnrecognizedPropertyException with a strict one. This is exactly the record pattern that ToolDefinition's own Javadoc recommends, and no fixture exercises it so it's green in CI.

I verified this by running the real processor: schema {properties={req={...}}, required=[req]}, payload {req:{query=hello,limit=7}} → handler returned query=null, limit=0.

Fix: flatten the schema for the single-record case so the record's components are the top-level properties (matching convertValue(args, Record.class)). Also, declaring the record straight from invocation.getArguments() avoids the secondary bug where a record param literally named args collides with the Map args local declared just above.

return "(Boolean) args.get(\"" + paramName + "\")";
}
// Complex types: enums, records, POJOs
return "mapper.convertValue(args.get(\"" + paramName + "\"), " + qualifiedName + ".class)";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generics are erased here. qualifiedName + ".class" produces e.g. mapper.convertValue(args.get("ids"), java.util.List.class), so a List<UUID> parameter deserializes to a raw List whose elements stay String (or LinkedHashMap) — never UUID. The schema is correct (items: {type: string, format: uuid}), but real user code doing for (UUID id : ids) hits a ClassCastException. Same for List<MyRecord>, Set<Foo>, Map<String,Long>, etc.

Confirmed with the real processor: payload {ids:["...uuid..."]} → element class came back as java.lang.String.

Use Jackson's TypeReference built from the full generic type (type.toString(), which yields java.util.List<java.util.UUID>) instead of .class — e.g. mapper.convertValue(args.get("ids"), new com.fasterxml.jackson.core.type.TypeReference<java.util.List<java.util.UUID>>(){}).

// Complex types: enums, records, POJOs
return "mapper.convertValue(args.get(\"" + paramName + "\"), " + qualifiedName + ".class)";
}
return "(Object) args.get(\"" + paramName + "\")";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array parameters generate code that doesn't compile. Arrays aren't primitive or DECLARED, so they fall through to (Object) args.get(...), while the variable is declared with the real array type. For @Param String[] names the processor emits:

java.lang.String[] names = (Object) args.get("names");

incompatible types: java.lang.Object cannot be converted to java.lang.String[] (confirmed by compiling the real generated output). SchemaGenerator already supports arrays, so any tool with an array param breaks the consumer's build entirely. Route arrays (and any non-scalar) through mapper.convertValue(args.get("x"), new TypeReference<T>(){}) — the same fix as the generics issue covers this.

case LONG :
case SHORT :
case BYTE :
return defaultValue;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultValue is injected as a raw, unvalidated source token, in both the schema and the lambda. For @Param(required=false, defaultValue="abc") int count the processor emits ... withMeta(Map.of("type","integer"), "count", abc) and Object countRaw = ... : abc;cannot find symbol: variable abc, i.e. the consumer's build fails (confirmed against the real processor). Even when it compiles, types can disagree: defaultValue="1.5" on an int advertises 1.5 in the schema's default but truncates to 1 at runtime.

Two things here: (1) validate the default against the parameter type at compile time and emit a clear error on unparseable values (consistent with the other @Param validations in process()); (2) emit a correctly-typed literal — quote strings/chars, append L/F/D for long/float/double — so the schema default and the runtime value match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants