codeflash-agent/plugin/languages/java/references/team-orchestration.md
mashraf-222 270cb56cee
Feat/java language support (#12)
* Add Java/Kotlin detection to top-level language router

Adds pom.xml, build.gradle, build.gradle.kts, settings.gradle, and
settings.gradle.kts as markers that route to the codeflash-java router.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add Java/Kotlin agent definitions for all optimization domains

10 agents covering the full optimization pipeline:
- codeflash-java: router/team lead for domain detection
- codeflash-java-setup: environment detection (build tool, JDK, profiling tools)
- codeflash-java-deep: cross-domain optimizer (default)
- codeflash-java-cpu: data structures, algorithms, JIT deopt, JMH benchmarks
- codeflash-java-memory: heap/GC tuning, escape analysis, leak detection
- codeflash-java-async: virtual threads, lock contention, CompletableFuture
- codeflash-java-structure: class loading, JPMS, startup time, circular deps
- codeflash-java-scan: quick cross-domain diagnosis via JFR/jdeps/GC logs
- codeflash-java-ci: GitHub webhook handler for Java PRs
- codeflash-java-pr-prep: JMH benchmarks and PR body templates

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add Java domain reference guides for all optimization domains

6 guides covering deep domain knowledge for agent consumption:
- data-structures: collection selection, autoboxing, JIT patterns, sorting
- memory: JVM heap layout, GC algorithms and tuning, escape analysis, leaks
- async: virtual threads, structured concurrency, lock hierarchy, contention
- structure: class loading, JPMS, CDS/AppCDS, ServiceLoader, Spring startup
- database: JPA N+1, HikariCP, pagination, batch operations, EXPLAIN plans
- native: JNI, Panama FFM API, GraalVM native-image, Vector API

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add Java optimization skills: session launcher and JFR profiling

- codeflash-optimize: session launcher with start/resume/status/scan/review
- jfr-profiling: quick-action JFR profiling in cpu/alloc/wall modes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Slim Java agents to match Go's concise ~175-line pattern

Move inline code examples, antipattern encyclopedias, JMH templates,
and deep-dive sections from agent prompts into reference guides.
Agents now contain only: target tables, one-liner antipatterns,
reasoning checklists, profiling commands, and keep/discard trees.

Line counts (before → after):
  cpu:       636 → 181
  memory:    878 → 193
  async:     578 → 165
  structure: 532 → 167
  deep:      507 → 186
  scan:      440 → 163
  Average:   595 → 176 (vs Go's 175)

Adds to data-structures/guide.md:
  - Collection contract traps table
  - Reflection → MethodHandle migration pattern
  - JMH benchmark template

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Fix Makefile build: use rsync merge and portable sed -i

Two bugs in the build target:
1. cp -R created nested dirs (agents/agents/, references/references/)
   instead of merging language overlay into shared base. Fix: rsync -a.
2. sed -i '' is macOS-only; fails silently on Linux. Fix: sed -i.bak
   (works on both macOS and Linux), then delete .bak files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Add HANDOFF.md session lifecycle to Java agents

Java agents could read HANDOFF.md on resume but never wrote or
updated it. A session that hit plateau would lose all context —
what was tried, what worked, why it stopped, what to do next.

Changes:
- Deep agent: init HANDOFF.md on fresh start, record after each
  experiment, write Stop Reason + learnings.md on session end
- Domain agents (CPU, memory, async, structure): record to
  HANDOFF.md after each keep/discard, write session-end state
- Handoff template: make language-agnostic (was Python-specific),
  add Session status, Strategy & Decisions, and Stop Reason fields

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* Close 11 gaps between Java and Python plugins

Add missing sections to Java deep agent: experiment loop depth (12 steps),
library boundary breaking, Phase 0 environment setup, CI mode, pre-submit
review, adversarial review, team orchestration, cross-domain results schema,
and structured progress reporting.

Add polymorphic dispatch safety to CPU agent and data-structures guide.
Add diff hygiene to CPU agent. Add native reference to router.

Create two new reference files: library-replacement.md (Guava/Commons/
Jackson/Joda replacement tables) and team-orchestration.md (full dispatch
and merge protocol).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-14 18:49:41 -05:00

5.5 KiB

Team Orchestration -- Java Deep Mode

Protocol for creating, dispatching, coordinating, and merging work from domain-specialist agents. The deep agent uses this when the unified target table has a mix of multi-domain and single-domain targets.

Creating the team

After unified profiling, if dispatching:

TeamCreate("deep-session")
TaskCreate("Unified profiling") -- mark completed
TaskCreate("Cross-domain experiments")
TaskCreate("Dispatched: CPU targets")   -- if dispatching CPU agent
TaskCreate("Dispatched: Memory targets") -- if dispatching memory agent
TaskCreate("Dispatched: Async targets")  -- if dispatching async agent

Dispatching domain agents

The key difference from the router dispatching blindly: you provide cross-domain context the domain agent wouldn't have.

CPU specialist example

Agent(subagent_type: "codeflash-java-cpu", name: "cpu-specialist",
      team_name: "deep-session", isolation: "worktree", prompt: "
  You are working under the deep optimizer's direction.

  ## Targeted Assignment
  Optimize these specific functions: <list from unified target table>

  ## Cross-Domain Context (from deep profiling)
  - processRecords: 45% CPU, but 40% of that is GC from 120 MiB allocation.
    I've already fixed the allocation in experiment 1. Re-profile -- the CPU
    picture should be cleaner now. Focus on the remaining algorithmic work.
  - serialize: 18% CPU, pure CPU problem -- no memory interaction.
    Likely autoboxing-in-loop or O(n^2) pattern.

  ## Environment
  <setup.md contents: JDK version, build tool, test command, GC algorithm>

  ## Conventions
  <conventions.md contents if exists>

  Work on these targets only. Send results via SendMessage(to: 'deep-lead').
")

Memory specialist example

Agent(subagent_type: "codeflash-java-memory", name: "mem-specialist",
      team_name: "deep-session", isolation: "worktree", prompt: "
  You are working under the deep optimizer's direction.

  ## Targeted Assignment
  Reduce allocations in loadData -- it allocates 500 MiB and triggers
  300ms of G1 mixed collection pauses.

  ## Cross-Domain Context
  - This method is called in a thread pool. Large allocations here
    trigger GC pauses that block all worker threads.
  - The async team will benefit from your memory reduction.
  - Do NOT change the thread pool configuration -- that's the async domain.
  ...")

Async specialist example

Agent(subagent_type: "codeflash-java-async", name: "async-specialist",
      team_name: "deep-session", isolation: "worktree", prompt: "
  You are working under the deep optimizer's direction.

  ## Targeted Assignment
  Fix lock contention in CacheManager -- JFR shows 340ms avg monitor wait
  on the synchronized block at CacheManager.java:88.

  ## Cross-Domain Context
  - The memory team is reducing allocation pressure in loadData.
    Once they finish, GC pauses will drop and thread throughput will
    improve even without your fix. But the lock contention is independent.
  ...")

Dispatching a researcher

Spawn a researcher to read ahead on targets while you work on the current one:

Agent(subagent_type: "codeflash-researcher", name: "researcher",
      team_name: "deep-session", prompt: "
  Investigate these targets from the deep optimizer's unified target table:
  1. serialize in OutputService.java:88 -- 18% CPU, no memory interaction
  2. validate in Validator.java:12 -- 8% CPU, +15 MiB memory
  For each, identify the specific antipattern and whether there are
  cross-domain interactions I might have missed.
  Send findings to: SendMessage(to: 'deep-lead')
")

Receiving results from dispatched agents

When dispatched agents send results via SendMessage:

  1. Integrate findings into unified view. Update the target table with their results.
  2. Check for cross-domain effects. If the CPU specialist's fix reduced CPU time, re-profile memory -- did GC behavior change?
  3. Revise strategy. Dispatched results may shift priorities. A memory specialist reducing allocations by 80% means your CPU targets' profiles are stale -- re-profile.
  4. Track in results.tsv. Record dispatched results with a note: dispatched:cpu-specialist in the description field.

Parallel dispatch with profiling conflict awareness

Two agents profiling simultaneously experience higher variance from CPU contention. JFR CPU sampling and async-profiler timing modes are affected; JFR allocation event profiling (jdk.ObjectAllocationInNewTLAB) is not.

Include in every dispatched agent's prompt: "You are running in parallel with another optimizer. Expect higher variance -- use 3x re-run confirmation for all results near the keep/discard threshold."

Merging dispatched work

When dispatched agents complete:

  1. Collect branches. git branch --list 'codeflash/*' -- each dispatched agent created its own branch in its worktree.
  2. Check for file overlap. Cross-reference changed files between your branch and dispatched branches. git diff --name-only main..codeflash/cpu-specialist vs your branch.
  3. Merge in impact order. Highest improvement first. If files overlap, check whether changes conflict or complement.
  4. Re-profile after merge. The combined changes may produce compounding effects -- or regressions. Run the unified profiling script on the merged state.
  5. Record the merged state in HANDOFF.md and results.tsv.

Team cleanup

When done (all dispatched agents complete and merged):

TeamDelete("deep-session")

Preserve .codeflash/results.tsv, .codeflash/HANDOFF.md, and .codeflash/learnings.md.