Skip to content

Commit 120d489

Browse files
authored
Extraction of Patch Text + Updates to PatchCat module (#114)
* Adding PatchCat submodule * Adding support for using PatchCat in Gin Local Search * Fixing compatibility issues with updates from LLM branch * Adressing PR code review comments * Nullifying all res values when tests are skipped + bug fix in updating bestPatch * Adding cluster and action to patch print to output + adding write of thrown out patches * Extraction of LLMRecplacement text in patch into PatchCat and output csv * Updating to latest version of PatchCat
1 parent bd1ba10 commit 120d489

5 files changed

Lines changed: 41 additions & 19 deletions

File tree

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
[submodule "PatchCat"]
22
path = PatchCat
33
url = https://github.com/karineek/PatchCat
4-
[submodule "Docker"]
5-
path = Docker
6-
url = https://github.com/domsob/gin-docker

PatchCat

src/main/java/gin/edit/llm/LLMReplaceStatement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,8 @@ public String toString() {
238238
return this.getClass().getCanonicalName() + " \"" + destinationFilename + "\":" + destinationStatement + "\nPrompt: !!!\n" + lastPrompt + "\n!!! --> !!!\n" + lastReplacement + "\n!!!";
239239
}
240240

241+
public String getLastReplacement() {
242+
return this.lastReplacement;
243+
}
244+
241245
}

src/main/java/gin/util/GP.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ protected void writePatch(int iteration, int evaluationNumber, UnitTestResultSet
193193
outputFileWriter.writeNext(entry);
194194
}
195195

196-
protected void writePatchWithPatchCatInfo(int iteration, int evaluationNumber, UnitTestResultSet results, String methodName, Double fitness, double improvement, int cluster, String action) {
196+
protected void writePatchWithPatchCatInfo(int iteration, int evaluationNumber, UnitTestResultSet results, String methodName, Double fitness, double improvement, int cluster, String action, String patch) {
197197
String[] entry = { methodName
198198
, Integer.toString(iteration)
199199
, Integer.toString(evaluationNumber)
200-
, results.getPatch().toString()
200+
, patch
201201
, Integer.toString(cluster)
202202
, action
203203
, results.getCleanCompile() == null ? "null" : Boolean.toString(results.getCleanCompile())

src/main/java/gin/util/LocalSearchSimple.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.sampullara.cli.Argument;
1717

1818
import java.io.File;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.Collections;
@@ -125,20 +127,20 @@ public String clusterAction(int cluster) throws IOException{
125127
throw new IOException("Clustering Failed");
126128
}
127129

128-
public void implementClusterAction(String action, String className, String methodName, List<UnitTest> tests, Patch patch, int iteration, int cluster) {
130+
public void implementClusterAction(String action, String className, String methodName, List<UnitTest> tests, Patch patch, int iteration, int cluster, String diff) {
129131
// ACTION C - Throw away patch
130132
if (action == "C") {
131133
UnitTestResultSet results = new UnitTestResultSet(patch, "", null, new ArrayList<>(), null, "", null, new ArrayList<>());
132-
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, null, 0, cluster, "C");
134+
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, null, 0, cluster, "C", diff);
133135
}
134136

135137
// ACTION B - Proceed as normal
136-
if (action == "B") {
138+
else if (action == "B") {
137139
// Calculate fitness
138140
UnitTestResultSet results = testPatch(className, tests, patch, null);
139141
double newFitness = fitness(results);
140-
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, newFitness, compareFitness(newFitness, best), cluster, "B");
141-
142+
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, newFitness, compareFitness(newFitness, best), cluster, "B", diff);
143+
142144
// Check if better
143145
if (compareFitness(newFitness, best) > 0) {
144146
best = newFitness;
@@ -151,7 +153,8 @@ public void implementClusterAction(String action, String className, String metho
151153
else {
152154
//Add dummy fitness entry as we don't want to test the patch
153155
UnitTestResultSet results = new UnitTestResultSet(patch, "", null, new ArrayList<>(), null, "", null, new ArrayList<>());
154-
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, null, 0, cluster, "A");
156+
super.writePatchWithPatchCatInfo(iteration, iteration, results, methodName, null, 0, cluster, "A", diff);
157+
155158
bestPatch = patch;
156159
}
157160
}
@@ -175,7 +178,7 @@ protected void search(TargetMethod method, Patch origPatch) {
175178
// Calculate fitness and record result, including fitness improvement (currently 0)
176179
double orig = fitness(results);
177180
if (Boolean.TRUE.equals(patchCat)){
178-
super.writePatchWithPatchCatInfo(-1, 0, results, methodName, orig, 0, -1, "Original");
181+
super.writePatchWithPatchCatInfo(-1, 0, results, methodName, orig, 0, -1, "Original", "");
179182
} else { super.writePatch(-1, 0, results, methodName, orig, 0); }
180183

181184
// Set original as best for now
@@ -187,26 +190,44 @@ protected void search(TargetMethod method, Patch origPatch) {
187190
// Add a mutation
188191
Patch patch = neighbour(bestPatch);
189192
boolean toTest = false;
193+
String lastReplacement = "";
190194
Logger.info("Patch is: " + patch.toString());
191195
Logger.info("Original Patch is: " + origPatch.toString());
192196

193197
try {
194198
// Support for PatchCat Integration
195199
if (Boolean.TRUE.equals(patchCat)) {
196200
Logger.info("Running PatchCat");
197-
201+
202+
patch.apply();
203+
Edit lastEdit = patch.getEdits().get(patch.getEdits().size() - 1);
204+
if (lastEdit instanceof LLMReplaceStatement llmEdit) {
205+
lastReplacement = llmEdit.getLastReplacement();
206+
} else {
207+
continue;
208+
}
209+
210+
// Logger.info("Current patch diff is: " + diff);
211+
Logger.info("Last replacement is: " + lastReplacement);
212+
198213
ProcessBuilder builder = new ProcessBuilder(
199214
"python3",
200-
"../gin/PatchCat/src/PatchCat.py",
201-
patch.toString(), origPatch.toString()
202-
);
215+
"../gin/PatchCat/src/PatchCatGin.py",
216+
"--diff-text", lastReplacement.toString(),
217+
"--vectorizer-path", "../gin/PatchCat/src/running-model/vectorizer.pkl",
218+
"--model-path", "../gin/PatchCat/src/running-model/model.pkl"
219+
);
203220

204221
builder.environment().put("PYTHONUNBUFFERED", "1");
205-
222+
// builder.redirectErrorStream(true);
223+
206224
Process process = builder.start();
225+
207226
BufferedReader reader = new BufferedReader(
208227
new InputStreamReader(process.getInputStream())
209228
);
229+
230+
//Read the output from PatchCatGin to get the cluster number
210231
String line = reader.readLine();
211232
int cluster = -1;
212233

@@ -227,7 +248,7 @@ protected void search(TargetMethod method, Patch origPatch) {
227248

228249
int exit = process.waitFor();
229250

230-
implementClusterAction(action, className, methodName, tests, patch, i, cluster);
251+
implementClusterAction(action, className, methodName, tests, patch, i, cluster, lastReplacement);
231252

232253
} else { // Regular Local Search without PatchCat
233254
// Calculate fitness

0 commit comments

Comments
 (0)