Skip to content

Commit 69a45c1

Browse files
committed
feat: Agentic Pentest Engine and Context Window Optimization
1 parent 7e8ff83 commit 69a45c1

5 files changed

Lines changed: 235 additions & 178 deletions

File tree

src/main/java/com/secai/ai/ToolExecutor.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
public class ToolExecutor {
2121

22+
private static final java.util.Map<String, String> cache = new java.util.concurrent.ConcurrentHashMap<>();
23+
2224
private static String truncate(String text, int maxLength) {
2325
if (text == null || text.length() <= maxLength) {
2426
return text;
@@ -147,6 +149,12 @@ public static String readFile(String targetPath) {
147149
}
148150

149151
public static String runCommand(String command, String projectPath, Scanner scanner) {
152+
String cacheKey = "cmd:" + command;
153+
if (cache.containsKey(cacheKey)) {
154+
System.out.println("\033[36m[AI returning CACHED command: " + command + " ...]\033[0m");
155+
return cache.get(cacheKey);
156+
}
157+
150158
boolean confirmed = askConfirmation("\033[36mAllow AI to run: '" + command + "'? [y/N]: \033[0m", scanner);
151159
if (!confirmed) {
152160
System.out.println("\033[31mCommand rejected.\033[0m");
@@ -179,7 +187,9 @@ public static String runCommand(String command, String projectPath, Scanner scan
179187
System.out.println(output.trim().isEmpty() ? "(No output)" : output.trim());
180188
System.out.println("\033[33m----------------------\033[0m\n");
181189

182-
return "Command exited with code " + exitCode + ". Output:\n" + output;
190+
String result = "Command exited with code " + exitCode + ". Output:\n" + output;
191+
cache.put(cacheKey, result);
192+
return result;
183193
} else {
184194
long pid = process.pid();
185195
return "Command started in background with PID " + pid + " and is still running. Logs are written to secai-run.log.";
@@ -201,6 +211,12 @@ public static String killCommand(String pidStr) {
201211
}
202212

203213
public static String httpRequest(String url, String method, String headers, String body) {
214+
String cacheKey = "http:" + method + ":" + url + ":" + body;
215+
if (cache.containsKey(cacheKey)) {
216+
System.out.println("\033[36m[AI returning CACHED HTTP request to " + url + " ...]\033[0m");
217+
return cache.get(cacheKey);
218+
}
219+
204220
System.out.println("\033[36m[AI sending HTTP " + method + " to " + url + " ...]\033[0m");
205221
try {
206222
HttpRequest.Builder builder = HttpRequest.newBuilder()
@@ -236,13 +252,21 @@ public static String httpRequest(String url, String method, String headers, Stri
236252
response.headers().map().forEach((k, v) -> result.append(k).append(": ").append(String.join(", ", v)).append("\n"));
237253
result.append("Body:\n").append(truncate(response.body(), 20000));
238254

239-
return result.toString();
255+
String resultStr = result.toString();
256+
cache.put(cacheKey, resultStr);
257+
return resultStr;
240258
} catch (Exception e) {
241259
return "HTTP request failed: " + e.getMessage();
242260
}
243261
}
244262

245263
public static String runSandboxed(String command, String projectPath, Scanner scanner) {
264+
String cacheKey = "sandbox:" + command;
265+
if (cache.containsKey(cacheKey)) {
266+
System.out.println("\033[36m[AI returning CACHED sandboxed command: " + command + " ...]\033[0m");
267+
return cache.get(cacheKey);
268+
}
269+
246270
boolean confirmed = askConfirmation("\033[36mAllow AI to run SANDBOXED command (Docker): '" + command + "'? [y/N]: \033[0m", scanner);
247271
if (!confirmed) {
248272
System.out.println("\033[31mCommand rejected.\033[0m");
@@ -285,7 +309,9 @@ public static String runSandboxed(String command, String projectPath, Scanner sc
285309
System.out.println(output.trim().isEmpty() ? "(No output)" : output.trim());
286310
System.out.println("\033[33m----------------------\033[0m\n");
287311

288-
return "Sandboxed command exited with code " + exitCode + ". Output:\n" + output;
312+
String result = "Sandboxed command exited with code " + exitCode + ". Output:\n" + output;
313+
cache.put(cacheKey, result);
314+
return result;
289315
} else {
290316
process.destroyForcibly();
291317
return "Sandboxed command timed out after 30 seconds and was killed.";

0 commit comments

Comments
 (0)