mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(bedrock-agentcore): delete a memory's events when the memory is deleted (#6554)
This commit is contained in:
1 parent
6acd90640d
commit
a79cc4d8c4
5 files changed
+194
-3
No files matched your search
+30
@@ -1,13 +1,22 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcore;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcore.model.MemoryEvent;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
@@ -25,6 +34,9 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
@QuarkusTest
|
||||
class BedrockAgentCoreEventIntegrationTest {
|
||||
|
||||
@Inject
|
||||
StorageFactory storageFactory;
|
||||
|
||||
private static String createMemory(String name) {
|
||||
return given().contentType(ContentType.JSON)
|
||||
.body("{\"name\":\"" + name + "\",\"eventExpiryDuration\":7}")
|
||||
@@ -246,4 +258,22 @@ class BedrockAgentCoreEventIntegrationTest {
|
||||
.when().post("/memories/" + memoryId + "/actor/actor1/sessions/sess1")
|
||||
.then().statusCode(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingAMemoryDropsOnlyItsStoredEvents() {
|
||||
String gone = createMemory("evtGone");
|
||||
String kept = createMemory("evtKept");
|
||||
createEvent(gone, "actor1", "sess1", 1789300800d, "bye");
|
||||
createEvent(kept, "actor1", "sess1", 1789300800d, "stay");
|
||||
|
||||
given().when().delete("/memories/" + gone + "/delete")
|
||||
.then().statusCode(202);
|
||||
|
||||
// The events are unreachable through the API once the memory is gone, so read the store.
|
||||
List<String> stored = storageFactory.<MemoryEvent>create("bedrockagentcore",
|
||||
"bedrock-agentcore-events.json", new TypeReference<>() {})
|
||||
.scanAllAccounts().stream().map(MemoryEvent::getMemoryId).toList();
|
||||
assertThat(stored, hasItem(kept));
|
||||
assertThat(stored, not(hasItem(gone)));
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package io.github.hectorvent.floci.services.bedrockagentcore;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcore.model.MemoryEvent;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.BedrockAgentCoreMemoryService;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.BedrockAgentCoreMemoryService.MemoryDeleted;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class BedrockAgentCoreEventServiceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
|
||||
@Test
|
||||
void deletingAMemoryDropsOnlyItsEvents() {
|
||||
InMemoryStorage<String, MemoryEvent> events = new InMemoryStorage<>();
|
||||
BedrockAgentCoreMemoryService memories = mock(BedrockAgentCoreMemoryService.class);
|
||||
doAnswer(call -> {
|
||||
call.<Runnable>getArgument(2).run();
|
||||
return null;
|
||||
}).when(memories).writeEvent(anyString(), anyString(), any());
|
||||
BedrockAgentCoreEventService service = new BedrockAgentCoreEventService(events, memories);
|
||||
String gone = "gone-0123456789";
|
||||
String kept = "kept-0123456789";
|
||||
service.createEvent(gone, "actor1", "sess1", 1789300800d, List.of(), true, null, REGION);
|
||||
service.createEvent(kept, "actor1", "sess1", 1789300800d, List.of(), true, null, REGION);
|
||||
|
||||
service.onMemoryDeleted(new MemoryDeleted(gone));
|
||||
|
||||
assertEquals(List.of(kept), events.scan(key -> true).stream().map(MemoryEvent::getMemoryId).toList());
|
||||
}
|
||||
}
|
||||
+72
@@ -3,13 +3,19 @@ package io.github.hectorvent.floci.services.bedrockagentcorecontrol;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.BedrockAgentCoreMemoryService.MemoryDeleted;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
|
||||
import io.github.hectorvent.floci.testing.MutableClock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
@@ -128,6 +134,72 @@ class BedrockAgentCoreMemoryServiceTest {
|
||||
() -> service.delete(id, "other", REGION)).getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteAnnouncesTheMemoryOnceAndNotOnReplay() {
|
||||
List<MemoryDeleted> deleted = new ArrayList<>();
|
||||
service = new BedrockAgentCoreMemoryService(
|
||||
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), clock, deleted::add);
|
||||
String id = create("myMemory").getMemoryId();
|
||||
service.delete(id, "del-1", REGION);
|
||||
service.delete(id, "del-1", REGION);
|
||||
assertEquals(List.of(new MemoryDeleted(id)), deleted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedEventCleanupLeavesTheMemoryForARetry() {
|
||||
AtomicBoolean cleanupFails = new AtomicBoolean(true);
|
||||
List<MemoryDeleted> deleted = new ArrayList<>();
|
||||
service = new BedrockAgentCoreMemoryService(
|
||||
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), clock, event -> {
|
||||
if (cleanupFails.get()) {
|
||||
throw new IllegalStateException("events file write failed");
|
||||
}
|
||||
deleted.add(event);
|
||||
});
|
||||
String id = create("myMemory").getMemoryId();
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> service.delete(id, "del-1", REGION));
|
||||
assertEquals(id, service.get(id, REGION).getMemoryId());
|
||||
|
||||
cleanupFails.set(false);
|
||||
assertEquals("DELETING", service.delete(id, "del-1", REGION).getStatus());
|
||||
assertEquals(List.of(new MemoryDeleted(id)), deleted);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
void deleteWaitsForAnEventWriteInProgress() throws InterruptedException {
|
||||
List<String> order = Collections.synchronizedList(new ArrayList<>());
|
||||
service = new BedrockAgentCoreMemoryService(
|
||||
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), clock,
|
||||
event -> order.add("cleanup"));
|
||||
String id = create("myMemory").getMemoryId();
|
||||
CountDownLatch writing = new CountDownLatch(1);
|
||||
CountDownLatch release = new CountDownLatch(1);
|
||||
Thread writer = new Thread(() -> service.writeEvent(id, REGION, () -> {
|
||||
writing.countDown();
|
||||
try {
|
||||
release.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
order.add("write");
|
||||
}));
|
||||
writer.start();
|
||||
writing.await();
|
||||
|
||||
Thread deleter = new Thread(() -> service.delete(id, null, REGION));
|
||||
deleter.start();
|
||||
while (deleter.isAlive() && deleter.getState() != Thread.State.WAITING) {
|
||||
Thread.onSpinWait();
|
||||
}
|
||||
release.countDown();
|
||||
writer.join();
|
||||
deleter.join();
|
||||
|
||||
assertEquals(List.of("write", "cleanup"), order);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTokenExpiresAfterWindowReturns404() {
|
||||
Memory memory = create("myMemory");
|
||||
|
||||
Reference in new issue
Block a user