Continue AI tasks in a fresh context after compaction
This commit is contained in:
1 parent
db82325ed7
commit
e48ae583f9
7 files changed
+358
-16
No files matched your search
@@ -357,6 +357,109 @@ class AgentWorkerIntegrationTest {
|
||||
verify(sandbox).execute(eq(project), argThat(call -> call.name().equals("write_file")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedHistoryResumesAndCompactsAgainWithoutReplayingToolsOrLosingLogs() throws Exception {
|
||||
String prompt = "Continue building the existing site";
|
||||
UUID id =
|
||||
(UUID)
|
||||
tasks
|
||||
.create(user, project, model, prompt, new BigDecimal("20"), "long-history")
|
||||
.get("id");
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
String oldSource = "older-source-marker".repeat(2400);
|
||||
var old = gateway.parse("OPENAI", response(oldSource, "write_file"));
|
||||
var recent = gateway.parse("OPENAI", response("Build check", "run_command"));
|
||||
tasks.response(id, lease, old);
|
||||
tasks.tool(id, lease, old.calls().getFirst(), "{\"written\":6400}");
|
||||
tasks.response(id, lease, recent);
|
||||
tasks.tool(id, lease, recent.calls().getFirst(), "{\"exitCode\":0}");
|
||||
tasks.finish(id, lease, "PAUSED", "上下文达到保守容量限制,请整理需求后新建任务", false);
|
||||
tasks.action(id, user, "resume");
|
||||
|
||||
String newSource = "newer-source-marker".repeat(2200);
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenReturn(response(newSource, "write_file"), response("Done", null));
|
||||
when(sandbox.execute(any(), any())).thenReturn("{\"written\":7200}");
|
||||
when(sandbox.inspect(project))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 1).put("complete", true));
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(tasks.claim());
|
||||
|
||||
assertEquals("COMPLETED", tasks.config(id).get("status"));
|
||||
var captured = org.mockito.ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(transport, times(2))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), captured.capture());
|
||||
var first = captured.getAllValues().getFirst().path("messages");
|
||||
assertEquals(2, first.size());
|
||||
assertEquals("system", first.get(0).path("role").asText());
|
||||
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"));
|
||||
assertFalse(first.toString().contains(oldSource));
|
||||
var second = captured.getAllValues().getLast().path("messages");
|
||||
assertEquals(2, second.size());
|
||||
assertTrue(second.get(1).path("content").asText().startsWith(prompt));
|
||||
assertTrue(second.get(1).path("content").asText().contains("7200"));
|
||||
assertFalse(second.toString().contains(newSource));
|
||||
assertTrue(
|
||||
tasks.history(id).stream().anyMatch(m -> oldSource.equals(m.path("content").asText())));
|
||||
assertTrue(
|
||||
tasks.history(id).stream().anyMatch(m -> newSource.equals(m.path("content").asText())));
|
||||
verify(sandbox).execute(eq(project), argThat(call -> call.name().equals("write_file")));
|
||||
assertEquals(
|
||||
4,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM ai_usage_records WHERE task_id=?", Integer.class, id));
|
||||
assertEquals(
|
||||
2,
|
||||
users
|
||||
.db()
|
||||
.queryForObject(
|
||||
"SELECT COUNT(*) FROM ai_task_steps WHERE task_id=? AND event='CONTEXT_COMPACTED'",
|
||||
Integer.class,
|
||||
id));
|
||||
var checkpoint = tasks.agentHistory(id);
|
||||
assertTrue(checkpoint.through() > 0);
|
||||
assertTrue(checkpoint.summary().contains("npm run build"));
|
||||
assertTrue(checkpoint.summary().contains("7200"));
|
||||
assertEquals(
|
||||
2, checkpoint.messages().size(), "Only the system prompt and new model turn are active");
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitContextRejectionRetriesWithFreshConversationAndChargesOnlyAcceptedCall()
|
||||
throws Exception {
|
||||
String prompt = "Continue the project";
|
||||
UUID id =
|
||||
(UUID)
|
||||
tasks
|
||||
.create(user, project, model, prompt, new BigDecimal("20"), "rejected-context")
|
||||
.get("id");
|
||||
var claimed = tasks.claim();
|
||||
String lease = claimed.get("lease_token").toString();
|
||||
var prior = gateway.parse("OPENAI", response("Earlier test output", "run_command"));
|
||||
tasks.response(id, lease, prior);
|
||||
tasks.tool(id, lease, prior.calls().getFirst(), "{\"exitCode\":0}");
|
||||
when(transport.request(anyString(), anyString(), anyString(), anyString(), anyInt(), any()))
|
||||
.thenThrow(new ProviderTransport.ContextRejected())
|
||||
.thenReturn(response("Done", null));
|
||||
when(sandbox.inspect(project))
|
||||
.thenReturn(json.createObjectNode().put("sourceFiles", 1).put("complete", true));
|
||||
|
||||
new AgentWorker(tasks, gateway, sandbox, vault, json).run(claimed);
|
||||
|
||||
assertEquals("COMPLETED", tasks.config(id).get("status"));
|
||||
var captured = org.mockito.ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(transport, times(2))
|
||||
.request(anyString(), anyString(), anyString(), anyString(), anyInt(), captured.capture());
|
||||
assertEquals(4, captured.getAllValues().getFirst().path("messages").size());
|
||||
assertEquals(2, captured.getAllValues().getLast().path("messages").size());
|
||||
assertEquals(2, ((Number) tasks.config(id).get("step_count")).intValue());
|
||||
verify(sandbox, never()).execute(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void incompleteInspectionCannotClaimWorkspaceIsEmptyOrCompleted() throws Exception {
|
||||
when(sandbox.inspect(any()))
|
||||
|
||||
Reference in new issue
Block a user