fix(bedrock-agentcore): expire remembered delete client tokens (#4477)

* fix(bedrock-agentcore): expire remembered delete client tokens

Runtime, endpoint, and memory deletes remembered every clientToken of
a completed delete forever, so a long-running emulator would grow an
unbounded set of tokens. Replace the per-service token sets with a
shared DeletedTokenLedger that stamps each token with the time it was
recorded and treats it as absent once it is older than an 8 hour
window, matching the clientToken idempotency window AWS documents for
other control planes (for example DynamoDB's ImportTable and
SageMaker APIs) and already used in this repo's
VerifiedPermissionsService. A replay past that window now 404s like
an unknown token instead of returning the DELETING marker forever,
and expired entries are purged on the next write so the ledger cannot
grow without bound.

* perf(bedrock-agentcore): purge expired delete tokens from the front of a queue

Recording a token scanned every live token to find the expired ones, so
a run of deletes inside the 8 hour window did quadratic work. Tokens
are now also queued in the order they were recorded, and purge pops
only the expired entries at the front. A token reused while its first
record is live keeps the newer record when the older one is purged.

* fix(bedrock-agentcore): keep one ledger entry per delete token

The queue added one entry per record, so a client reusing a token for
many deletes inside the window grew the ledger with the number of
deletes rather than the number of tokens. An insertion-ordered map now
holds one entry per token; re-recording moves the token to the back, and
purge still stops at the first live entry.

* fix(bedrock-agentcore): time delete tokens from their first use

Re-recording a token restamped it, so a token reused every few hours
never expired. The window AWS documents for client tokens runs from the
first request that used the token, so record now keeps the first
timestamp with putIfAbsent. The ledger stays ordered by first use, so
purge can still stop at the first live entry.

* fix(bedrock-agentcore): restart an expired delete token the purge missed

If the wall clock steps back, an expired token can sit behind a live
one where the front purge stops, and putIfAbsent then kept its stale
timestamp on reuse, so an immediate replay 404ed. record now goes
through contains, which drops an expired entry, so reusing it starts a
fresh window.
This commit is contained in:
Niels Kootstra authored and GitHub committed 2026-09-26 16:33:02 -05:00
1 parent 60b8fd2037
commit fe55cf0d06
5 files changed
+227 -17

No files matched your search

@@ -9,9 +9,11 @@ import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntime;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeEndpoint;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntimeVersion;
import io.github.hectorvent.floci.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
@@ -28,12 +30,13 @@ class BedrockAgentCoreControlServiceTest {
private static final String REGION = "us-east-1";
private final ObjectMapper mapper = new ObjectMapper();
private final MutableClock clock = new MutableClock();
private BedrockAgentCoreControlService service;
@BeforeEach
void setUp() {
service = new BedrockAgentCoreControlService(
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"));
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), null, clock);
}
private ObjectNode artifact() {
@@ -217,6 +220,76 @@ class BedrockAgentCoreControlServiceTest {
() -> service.deleteAgentRuntime(id, "other", REGION)).getHttpStatus());
}
@Test
void deleteRuntimeTokenExpiresAfterWindowReturns404() {
AgentRuntime rt = create("myAgent");
String id = rt.getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(id, "del-1", REGION).getStatus());
clock.advance(DeletedTokenLedger.TTL.plusSeconds(1));
assertEquals(404, assertThrows(AwsException.class,
() -> service.deleteAgentRuntime(id, "del-1", REGION)).getHttpStatus());
}
@Test
void deleteRuntimeTokenCanBeReusedAfterItExpires() {
// Once a token's record expires it must behave as if it had never been recorded,
// not merely as "expired but still occupying the map": a later delete that reuses
// the same token value records it again, and that fresh record is honored.
AgentRuntime first = create("firstAgent");
String firstId = first.getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(firstId, "reused-token", REGION).getStatus());
clock.advance(DeletedTokenLedger.TTL.plusSeconds(1));
AgentRuntime second = create("secondAgent");
String secondId = second.getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(secondId, "reused-token", REGION).getStatus());
// The replay right after is still within the fresh token's own window.
assertEquals("DELETING", service.deleteAgentRuntime(secondId, "reused-token", REGION).getStatus());
// The old runtime's id is unrelated to the reused token and stays gone.
assertEquals(404, assertThrows(AwsException.class,
() -> service.deleteAgentRuntime(firstId, "some-other-token", REGION)).getHttpStatus());
}
@Test
void deleteRuntimeTokenReusedWithinItsWindowStillExpiresFromItsFirstUse() {
// The window runs from the first request that used the token, so reusing it does
// not extend the window.
String firstId = create("firstAgent").getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(firstId, "reused-token", REGION).getStatus());
clock.advance(Duration.ofHours(7));
String secondId = create("secondAgent").getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(secondId, "reused-token", REGION).getStatus());
clock.advance(Duration.ofHours(1).plusSeconds(1));
assertEquals(404, assertThrows(AwsException.class,
() -> service.deleteAgentRuntime(secondId, "reused-token", REGION)).getHttpStatus());
}
@Test
void deleteRuntimeTokenReusedAfterExpiryIsHonouredEvenWhenTheClockMovedBack() {
// A wall clock stepping back can leave an expired token behind a live one, where the
// front purge cannot reach it. Reusing that token must still start a fresh window.
clock.advance(Duration.ofHours(2));
String liveId = create("liveAgent").getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(liveId, "live-token", REGION).getStatus());
clock.advance(Duration.ofHours(-3));
String staleId = create("staleAgent").getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(staleId, "stale-token", REGION).getStatus());
clock.advance(DeletedTokenLedger.TTL.plusSeconds(1));
String reusedId = create("reusedAgent").getAgentRuntimeId();
assertEquals("DELETING", service.deleteAgentRuntime(reusedId, "stale-token", REGION).getStatus());
assertEquals("DELETING", service.deleteAgentRuntime(reusedId, "stale-token", REGION).getStatus());
}
@Test
void deleteRemovesAndReportsDeleting() {
AgentRuntime rt = create("myAgent");
@@ -286,4 +359,31 @@ class BedrockAgentCoreControlServiceTest {
assertEquals(409, assertThrows(AwsException.class,
() -> service.createEndpoint(id, "DEFAULT", null, null, null, REGION)).getHttpStatus());
}
@Test
void deleteEndpointIsIdempotentByClientToken() {
AgentRuntime rt = create("myAgent");
String id = rt.getAgentRuntimeId();
service.createEndpoint(id, "prod", null, null, null, REGION);
assertEquals("DELETING", service.deleteEndpoint(id, "prod", "ep-del-1", REGION).getStatus());
// Replayed delete with the same token succeeds instead of 404ing.
assertEquals("DELETING", service.deleteEndpoint(id, "prod", "ep-del-1", REGION).getStatus());
// A different token against the now-missing endpoint still 404s.
assertEquals(404, assertThrows(AwsException.class,
() -> service.deleteEndpoint(id, "prod", "other", REGION)).getHttpStatus());
}
@Test
void deleteEndpointTokenExpiresAfterWindowReturns404() {
AgentRuntime rt = create("myAgent");
String id = rt.getAgentRuntimeId();
service.createEndpoint(id, "prod", null, null, null, REGION);
assertEquals("DELETING", service.deleteEndpoint(id, "prod", "ep-del-1", REGION).getStatus());
clock.advance(DeletedTokenLedger.TTL.plusSeconds(1));
assertEquals(404, assertThrows(AwsException.class,
() -> service.deleteEndpoint(id, "prod", "ep-del-1", REGION)).getHttpStatus());
}
}
@@ -4,6 +4,7 @@ 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.model.Memory;
import io.github.hectorvent.floci.testing.MutableClock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -21,12 +22,13 @@ class BedrockAgentCoreMemoryServiceTest {
private static final String KEY_ARN = "arn:aws:kms:us-east-1:000000000000:key/mem-key";
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/mem-role";
private final MutableClock clock = new MutableClock();
private BedrockAgentCoreMemoryService service;
@BeforeEach
void setUp() {
service = new BedrockAgentCoreMemoryService(
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"));
new InMemoryStorage<>(), new RegionResolver(REGION, "000000000000"), clock);
}
private Memory create(String name) {
@@ -102,6 +104,30 @@ class BedrockAgentCoreMemoryServiceTest {
assertEquals(ROLE_ARN, after.getMemoryExecutionRoleArn());
}
@Test
void deleteIsIdempotentByClientToken() {
Memory memory = create("myMemory");
String id = memory.getMemoryId();
assertEquals("DELETING", service.delete(id, "del-1", REGION).getStatus());
// Replayed delete with the same token succeeds instead of 404ing.
assertEquals("DELETING", service.delete(id, "del-1", REGION).getStatus());
// A different token against the now-missing id still 404s.
assertEquals(404, assertThrows(AwsException.class,
() -> service.delete(id, "other", REGION)).getHttpStatus());
}
@Test
void deleteTokenExpiresAfterWindowReturns404() {
Memory memory = create("myMemory");
String id = memory.getMemoryId();
assertEquals("DELETING", service.delete(id, "del-1", REGION).getStatus());
clock.advance(DeletedTokenLedger.TTL.plusSeconds(1));
assertEquals(404, assertThrows(AwsException.class,
() -> service.delete(id, "del-1", REGION)).getHttpStatus());
}
@Test
void tagOperationsRoundTripThroughTheArn() {
Memory memory = create("tagMemory");