fix(bedrock-agentcore): save a runtime's create tags with the runtime (#7130)

* fix(bedrock-agentcore): save a runtime's create tags with the runtime

The controller added the tags to the runtime after the service had already
stored it. WAL and persistent storage had written the record by then, so the
tags were lost on a crash unless something later saved the runtime again. The
same code ran on a clientToken replay and merged the retry's tags into the
original runtime.

Pass the tags into createAgentRuntime and set them before storage.put, as
CreateMemory does. The replay path returns before that point, so it no longer
changes them. CloudFormation keeps the existing overload because it applies
tags in its own rollback-guarded step.

Fixes #7126

* test(bedrock-agentcore): tighten the runtime create tag tests

The replay test ignored the second create's ARN, so it would still pass if
the retry created a second runtime. Assert that both creates return the same
ARN.

The WAL test started its try block after setup, so a failure there left both
stores and their compaction threads running. Guard each store from the point
it is constructed.
This commit is contained in:
Ridwan authored and GitHub committed 2026-10-31 17:20:19 -04:00
1 parent 300e62a0ed
commit 2d68edc4b7
4 files changed
+87 -4

No files matched your search

@@ -72,12 +72,9 @@ public class BedrockAgentCoreControlController {
stringMap(req.get("environmentVariables")),
obj(req, "authorizerConfiguration"),
obj(req, "protocolConfiguration"),
stringMap(req.get("tags")),
text(req, "clientToken"),
region);
Map<String, String> tags = stringMap(req.get("tags"));
if (tags != null) {
runtime.getTags().putAll(tags);
}
String version = String.valueOf(runtime.getLatestVersion());
ObjectNode out = objectMapper.createObjectNode();
@@ -85,6 +85,14 @@ public class BedrockAgentCoreControlService {
String roleArn, String description, Map<String, String> environmentVariables,
JsonNode authorizerConfiguration, JsonNode protocolConfiguration,
String clientToken, String region) {
return createAgentRuntime(name, artifact, networkConfiguration, roleArn, description, environmentVariables,
authorizerConfiguration, protocolConfiguration, null, clientToken, region);
}
public AgentRuntime createAgentRuntime(String name, JsonNode artifact, JsonNode networkConfiguration,
String roleArn, String description, Map<String, String> environmentVariables,
JsonNode authorizerConfiguration, JsonNode protocolConfiguration,
Map<String, String> tags, String clientToken, String region) {
if (clientToken != null) {
Optional<AgentRuntime> replay = storage.scan(k -> k.startsWith(keyPrefix(region))).stream()
.filter(r -> clientToken.equals(r.getClientToken()))
@@ -139,6 +147,9 @@ public class BedrockAgentCoreControlService {
}
runtime.getVersions().add(snapshot(runtime, "1", now));
runtime.getEndpoints().add(newEndpoint("DEFAULT", "1", null, now));
if (tags != null) {
runtime.getTags().putAll(tags);
}
storage.put(key(region, id), runtime);
LOG.infov("Created AgentCore runtime {0} (id={1})", name, id);
@@ -6,17 +6,23 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.PersistentStorage;
import io.github.hectorvent.floci.core.storage.WalStorage;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.AgentRuntime;
import io.github.hectorvent.floci.services.bedrockagentcorecontrol.model.Memory;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
/**
* Persistence-across-restart coverage: writes a fully-populated agent runtime through
@@ -83,6 +89,45 @@ class BedrockAgentCorePersistenceTest {
assertTrue(loaded.getEndpoints().stream().anyMatch(e -> "DEFAULT".equals(e.getName())));
}
@Test
void tagsSentToCreateAgentRuntimeAreInTheWalRecord(@TempDir Path dir) throws IOException {
TypeReference<Map<String, AgentRuntime>> ref = new TypeReference<>() {};
Path wal = dir.resolve("runtimes.wal");
Path walAfterCrash = dir.resolve("runtimes-after-crash.wal");
WalStorage<String, AgentRuntime> writer =
new WalStorage<>(dir.resolve("runtimes.json"), wal, ref, 3_600_000L);
try {
writer.load();
RegionResolver regionResolver = new RegionResolver(REGION, "000000000000");
BedrockAgentCoreControlController controller = new BedrockAgentCoreControlController(
new BedrockAgentCoreControlService(writer, regionResolver), regionResolver, mapper);
Response response = controller.createAgentRuntime(mock(HttpHeaders.class), """
{
"agentRuntimeName": "walAgent",
"agentRuntimeArtifact": {"containerConfiguration": {"containerUri": "x:latest"}},
"networkConfiguration": {"networkMode": "PUBLIC"},
"roleArn": "arn:aws:iam::000000000000:role/agent",
"tags": {"team": "ml"}
}""");
assertEquals(202, response.getStatus());
String id = ((ObjectNode) response.getEntity()).path("agentRuntimeId").asText();
// A copy of the WAL is what a crash leaves behind: nothing later rewrites the runtime.
Files.copy(wal, walAfterCrash);
WalStorage<String, AgentRuntime> reader =
new WalStorage<>(dir.resolve("runtimes-after-crash.json"), walAfterCrash, ref, 3_600_000L);
try {
reader.load();
assertEquals(Map.of("team", "ml"), reader.get("runtime:" + REGION + ":" + id).orElseThrow().getTags());
} finally {
reader.shutdown();
}
} finally {
writer.shutdown();
}
}
@Test
void memorySurvivesPersistentStorageRoundTrip(@TempDir Path dir) {
BedrockAgentCoreMemoryService svc = new BedrockAgentCoreMemoryService(
@@ -13,6 +13,7 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@@ -76,6 +77,35 @@ class BedrockAgentCoreTaggingIdentityIntegrationTest {
.then().statusCode(404);
}
@Test
void aClientTokenReplayKeepsTheTagsOfTheOriginalCreate() {
String clientToken = "replay-create-runtime-tags-token-0001";
String firstArn = createRuntimeWithTags(clientToken, "{\"team\":\"ml\"}");
String replayArn = createRuntimeWithTags(clientToken, "{\"env\":\"dev\"}");
assertEquals(firstArn, replayArn);
given().when().get("/tags/" + firstArn)
.then().statusCode(200)
.body("tags.team", equalTo("ml"))
.body("tags.env", nullValue());
}
private static String createRuntimeWithTags(String clientToken, String tags) {
return given().contentType("application/json")
.body("""
{
"agentRuntimeName": "replayTagAgent",
"agentRuntimeArtifact": {"containerConfiguration": {"containerUri": "x:latest"}},
"networkConfiguration": {"networkMode": "PUBLIC"},
"roleArn": "arn:aws:iam::000000000000:role/agent",
"clientToken": "%s",
"tags": %s
}""".formatted(clientToken, tags))
.when().put("/runtimes/")
.then().statusCode(202)
.extract().path("agentRuntimeArn");
}
@Test
void nonDefaultAccountCanTagItsMemoryAndRejectsForeignIdentity() {
String authorization = "AWS4-HMAC-SHA256 Credential=111111111111/20261028/us-east-1/bedrock-agentcore/aws4_request";