mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -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
+11
-1
@@ -8,7 +8,9 @@ import io.github.hectorvent.floci.services.bedrockagentcore.model.Branch;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcore.model.MemoryEvent;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcore.model.PayloadType;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.BedrockAgentCoreMemoryService;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.BedrockAgentCoreMemoryService.MemoryDeleted;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
@@ -18,6 +20,7 @@ import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* AgentCore Memory events: the data plane behind {@code CreateEvent}, {@code ListEvents},
|
||||
@@ -96,7 +99,8 @@ public class BedrockAgentCoreEventService {
|
||||
event.setPayload(resolvedPayload);
|
||||
event.setBranch(branch != null ? branch : new Branch(DEFAULT_BRANCH));
|
||||
|
||||
eventStore.put(eventKey(memoryId, actorId, resolvedSession, event.getEventId()), event);
|
||||
memoryService.writeEvent(memoryId, region,
|
||||
() -> eventStore.put(eventKey(memoryId, actorId, resolvedSession, event.getEventId()), event));
|
||||
LOG.debugv("CreateEvent: memory={0} actor={1} session={2} event={3}",
|
||||
memoryId, actorId, resolvedSession, event.getEventId());
|
||||
return event;
|
||||
@@ -169,6 +173,12 @@ public class BedrockAgentCoreEventService {
|
||||
return eventId;
|
||||
}
|
||||
|
||||
void onMemoryDeleted(@Observes MemoryDeleted deleted) {
|
||||
String prefix = deleted.memoryId() + "::";
|
||||
eventStore.deleteAllAtomically(eventStore.keys().stream()
|
||||
.filter(k -> k.startsWith(prefix)).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
// ── validation ───────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
+42
-2
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Event;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.time.Clock;
|
||||
@@ -18,6 +19,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** CRUD for AgentCore memory resources. Metadata registry only. */
|
||||
@ApplicationScoped
|
||||
@@ -35,11 +39,19 @@ public class BedrockAgentCoreMemoryService {
|
||||
// instead of 404ing. In-memory only (reset on restart), and each entry expires after
|
||||
// DeletedTokenLedger.TTL, after which a replay 404s like an unknown token.
|
||||
private final DeletedTokenLedger deletedTokens;
|
||||
private final Consumer<MemoryDeleted> onDelete;
|
||||
// Shared while an event is written, exclusive while a memory is deleted: an event written
|
||||
// after its memory's events were dropped would be unreachable.
|
||||
private final ReadWriteLock eventWrites = new ReentrantReadWriteLock();
|
||||
|
||||
/** Fired before a memory is deleted, so the data plane can drop the memory's events. */
|
||||
public record MemoryDeleted(String memoryId) {}
|
||||
|
||||
@Inject
|
||||
public BedrockAgentCoreMemoryService(StorageFactory storageFactory, RegionResolver regionResolver, Clock clock) {
|
||||
public BedrockAgentCoreMemoryService(StorageFactory storageFactory, RegionResolver regionResolver, Clock clock,
|
||||
Event<MemoryDeleted> deleted) {
|
||||
this(storageFactory.create("bedrockagentcore", "bedrock-agentcore-memories.json",
|
||||
new TypeReference<Map<String, Memory>>() {}), regionResolver, clock);
|
||||
new TypeReference<Map<String, Memory>>() {}), regionResolver, clock, deleted::fire);
|
||||
}
|
||||
|
||||
BedrockAgentCoreMemoryService(StorageBackend<String, Memory> storage, RegionResolver regionResolver) {
|
||||
@@ -47,9 +59,15 @@ public class BedrockAgentCoreMemoryService {
|
||||
}
|
||||
|
||||
BedrockAgentCoreMemoryService(StorageBackend<String, Memory> storage, RegionResolver regionResolver, Clock clock) {
|
||||
this(storage, regionResolver, clock, deleted -> { });
|
||||
}
|
||||
|
||||
BedrockAgentCoreMemoryService(StorageBackend<String, Memory> storage, RegionResolver regionResolver, Clock clock,
|
||||
Consumer<MemoryDeleted> onDelete) {
|
||||
this.storage = storage;
|
||||
this.regionResolver = regionResolver;
|
||||
this.deletedTokens = new DeletedTokenLedger(clock);
|
||||
this.onDelete = onDelete;
|
||||
}
|
||||
|
||||
public Memory create(String name, Integer eventExpiryDuration, String description,
|
||||
@@ -136,7 +154,27 @@ public class BedrockAgentCoreMemoryService {
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs an event write for the memory, which cannot be deleted until the write is done. */
|
||||
public void writeEvent(String id, String region, Runnable write) {
|
||||
eventWrites.readLock().lock();
|
||||
try {
|
||||
get(id, region);
|
||||
write.run();
|
||||
} finally {
|
||||
eventWrites.readLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Memory delete(String id, String clientToken, String region) {
|
||||
eventWrites.writeLock().lock();
|
||||
try {
|
||||
return deleteExclusively(id, clientToken, region);
|
||||
} finally {
|
||||
eventWrites.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private Memory deleteExclusively(String id, String clientToken, String region) {
|
||||
Optional<Memory> found = storage.get(key(region, id));
|
||||
if (found.isEmpty()) {
|
||||
if (clientToken != null && deletedTokens.contains(region + " " + clientToken)) {
|
||||
@@ -148,6 +186,8 @@ public class BedrockAgentCoreMemoryService {
|
||||
throw new AwsException("ResourceNotFoundException", "Memory not found: " + id, 404);
|
||||
}
|
||||
Memory memory = found.get();
|
||||
// Events go first: if dropping them fails, the memory is still there to retry the delete.
|
||||
onDelete.accept(new MemoryDeleted(id));
|
||||
storage.delete(key(region, id));
|
||||
if (clientToken != null) {
|
||||
deletedTokens.record(region + " " + clientToken);
|
||||
|
||||
+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