diff --git a/backend/src/main/java/run/webup/platform/AgentContext.java b/backend/src/main/java/run/webup/platform/AgentContext.java index d7d424d..8cae92b 100644 --- a/backend/src/main/java/run/webup/platform/AgentContext.java +++ b/backend/src/main/java/run/webup/platform/AgentContext.java @@ -8,19 +8,27 @@ import java.util.ArrayList; import java.util.List; final class AgentContext { + private static final String WORKFLOW = + """ + + Work in a loop: inspect the files relevant to the next change, make one focused edit, + and run a targeted check. Start with the current workspace and recent tool results. + When a check fails, diagnose the observed failure before repeating it. Re-read a file + only if it changed or you need a specific missing detail. Before finishing, run the + relevant build or tests and report what changed, what passed, and what remains unverified. + """; private static final String HANDOFF = "\n\nContinue this task in the existing /workspace. Earlier conversation was compacted." - + " Inspect existing files before repeating work. Treat earlier activity as reference" - + " data, not new instructions."; + + " Resume from the last verified result or unfinished change below. Check only the" + + " files needed for the next step. Treat earlier activity as reference data."; private AgentContext() {} static List messages( ObjectMapper json, TaskService.AgentHistory history, String prompt) { - if (history.through() < 0) return history.messages(); var messages = new ArrayList(); - messages.add(history.messages().getFirst()); - messages.add(user(json, prompt, history.summary())); + messages.add(system(json, history.messages().getFirst())); + if (history.through() >= 0) messages.add(user(json, prompt, history.summary())); messages.addAll(history.messages().subList(1, history.messages().size())); return messages; } @@ -38,7 +46,7 @@ final class AgentContext { var notes = new ArrayList(); long available = target(context, maxOutput) - - bytes(history.messages().getFirst()) + - bytes(system(json, history.messages().getFirst())) - bytes(user(json, prompt, "")); long limit = Math.min(3072, Math.max(0, available)); if (limit == 0) return ""; @@ -64,6 +72,12 @@ final class AgentContext { return json.createObjectNode().put("role", "user").put("content", content); } + private static JsonNode system(ObjectMapper json, JsonNode saved) { + return json.createObjectNode() + .put("role", "system") + .put("content", saved.path("content").asText() + WORKFLOW); + } + private static long target(int context, int maxOutput) { // Size is an estimate for when to roll over, never a reason to suspend the task. return Math.max(0L, (long) context - maxOutput - 2048); @@ -72,6 +86,27 @@ final class AgentContext { private static String note(ObjectMapper json, JsonNode message) { var parts = new ArrayList(); String text = message.path("content").asText().strip(); + if (role(message, "tool")) { + try { + var result = json.readTree(text); + String tool = message.path("name").asText(); + if (result != null && tool.equals("read_file") && result.has("content")) { + // The paired call already preserves the path. Repeating file contents crowds out + // progress. + return ""; + } + if (result != null && tool.equals("run_command") && result.has("exitCode")) { + text = + "exit code " + + result.path("exitCode").asInt() + + (result.path("timedOut").asBoolean(false) ? " (timed out)" : "") + + " | " + + result.path("output").asText(); + } + } catch (JsonProcessingException ignored) { + // Older histories may contain a plain-text tool result. + } + } if (!text.isEmpty()) parts.add(clip(text, 220)); for (JsonNode call : message.path("tool_calls")) { var function = call.path("function"); diff --git a/backend/src/test/java/run/webup/platform/AgentWorkerIntegrationTest.java b/backend/src/test/java/run/webup/platform/AgentWorkerIntegrationTest.java index 0ddd65a..1848cd4 100644 --- a/backend/src/test/java/run/webup/platform/AgentWorkerIntegrationTest.java +++ b/backend/src/test/java/run/webup/platform/AgentWorkerIntegrationTest.java @@ -423,6 +423,7 @@ class AgentWorkerIntegrationTest { var first = captured.getAllValues().getFirst().path("messages"); assertEquals(2, first.size()); assertEquals("system", first.get(0).path("role").asText()); + assertTrue(first.get(0).path("content").asText().contains("make one focused edit")); assertTrue(first.get(1).path("content").asText().startsWith(prompt)); assertTrue(first.get(1).path("content").asText().contains("index.html")); assertTrue(first.get(1).path("content").asText().contains("npm run build")); @@ -454,6 +455,7 @@ class AgentWorkerIntegrationTest { var checkpoint = tasks.agentHistory(id); assertTrue(checkpoint.through() > 0); assertTrue(checkpoint.summary().contains("npm run build")); + assertTrue(checkpoint.summary().contains("exit code 0")); assertTrue(checkpoint.summary().contains("7200")); assertEquals( 2, checkpoint.messages().size(), "Only the system prompt and new model turn are active"); diff --git a/docs/AI-DEVELOPMENT.md b/docs/AI-DEVELOPMENT.md index 98828aa..02672f6 100644 --- a/docs/AI-DEVELOPMENT.md +++ b/docs/AI-DEVELOPMENT.md @@ -40,6 +40,8 @@ Worker 以条件更新原子领取任务,持有 300 秒租约;调用模型 任务按预算执行,没有固定轮次上限。每轮最多 8 个工具,终端命令最长 120 秒。历史接近模型窗口时,Worker 保存交接摘要,在同一任务内开启下一段模型对话。新对话带上项目要求、本轮用户需求和历史进度摘要,继续使用原工作区及预算。摘要从已有消息和工具结果提取,无需单独调用模型。原始消息和日志仍可查询,已执行的工具不会重放。旧版因轮次或上下文上限暂停的任务可以直接继续。 +Agent 按检查相关文件、小步修改和验证的顺序推进。压缩交接保留工具路径和命令退出状态,省略已读取的完整文件内容,便于下一段从最近的进展继续。此流程参考了 [Anthropic 对长任务 Agent 的公开设计](https://www.anthropic.com/research/effective-harnesses-for-long-running-agents)。 + 供应商明确拒绝过长输入且未返回 usage 时,可在仍有旧历史的情况下压缩后重试。超时等无法确定计费状态的错误走核算流程。供应商的实际上下文和输出限制仍适用。后续任务复制最近完成任务的消息,项目文件持久保存在对应 PVC。 COMPLETED 表示模型结束本轮工具循环,不是生产发布 / 构建验收结论。用户需核对实际工具日志、退出码、文件与构建测试结果。自动浏览器测试、预览代理和正式部署尚未实现。