Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 168 additions & 0 deletions src/core/project/backends/cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,174 @@ describe("CdkBackend.resolveProjectResources", () => {
expect(subject.accountCredentials).toEqual([]);
});

/** Each gateway's targets as `[gateway, [[target, id]...]]`; a target with no id has none. */
const gatewayTargetIds = (resources: ResolvedProjectResource[]) =>
resources
.filter(({ resourceType }) => resourceType === "gateway")
.map(({ name, children }) => [
name,
(children ?? []).map((child) => [
child.name,
child.deploymentState === "deployed" && "id" in child ? child.id : undefined,
]),
]);

const ONE = boxGatewaySpec("agent").name;
const TWO = boxGatewaySpec("second_agent").name;
Comment thread
SaiAbhishek98 marked this conversation as resolved.
Resolved
/** A gateway and its target names; a deployed gateway, the target id exports, and the ids expected. */
test.each([
{
title: "a target name that two gateways share gets the id of its own gateway",
gateways: [
[ONE, ["bedrock", "egress-github"]],
[TWO, ["bedrock"]],
],
deployed: [ONE, TWO],
exports: {
[`${ONE}:bedrock`]: "bedrock-of-one",
[`${TWO}:bedrock`]: "bedrock-of-two",
"egress-github": "github-of-one",
},
expected: [
[
ONE,
[
["bedrock", "bedrock-of-one"],
["egress-github", "github-of-one"],
],
],
[TWO, [["bedrock", "bedrock-of-two"]]],
],
},
{
title: "the target of one gateway gets the export name without the gateway",
gateways: [[ONE, ["bedrock"]]],
deployed: [ONE],
exports: { bedrock: "bedrock-1" },
expected: [[ONE, [["bedrock", "bedrock-1"]]]],
},
{
title:
"a target name with a hyphen does not take the id of a gateway and target joined by a hyphen",
gateways: [
["a", ["b"]],
["tools", ["a-b"]],
],
deployed: ["a", "tools"],
exports: { b: "b-of-a", "a-b": "a-b-of-tools" },
expected: [
["a", [["b", "b-of-a"]]],
["tools", [["a-b", "a-b-of-tools"]]],
],
},
{
title: "a target of one gateway does not take the id of a target named after the gateway",
gateways: [["tools", ["search", "tools-search"]]],
deployed: ["tools"],
exports: { search: "search-1", "tools-search": "tools-search-1" },
expected: [
[
"tools",
[
["search", "search-1"],
["tools-search", "tools-search-1"],
],
],
],
},
{
title:
"a shared target name with a hyphen does not give its id to the target of another gateway",
gateways: [
["a", ["b-c"]],
["x", ["b-c"]],
["a-b", ["c"]],
],
deployed: ["a", "x", "a-b"],
exports: { "a:b-c": "b-c-of-a", "x:b-c": "b-c-of-x", c: "c-of-a-b" },
expected: [
["a", [["b-c", "b-c-of-a"]]],
["x", [["b-c", "b-c-of-x"]]],
["a-b", [["c", "c-of-a-b"]]],
],
},
{
title:
"a shared target name of a gateway with an underscore gets the export name with a hyphen",
gateways: [
["my_tools", ["search"]],
["other", ["search"]],
],
deployed: ["my_tools", "other"],
exports: { "my-tools:search": "search-of-my-tools", "other:search": "search-of-other" },
expected: [
["my_tools", [["search", "search-of-my-tools"]]],
["other", [["search", "search-of-other"]]],
],
},
{
title:
"a target name that two gateways share gives no id while the stack has only the export without the gateway",
gateways: [
[ONE, ["bedrock"]],
[TWO, ["bedrock"]],
],
deployed: [ONE],
exports: { bedrock: "bedrock-of-one" },
expected: [
[ONE, [["bedrock", undefined]]],
[TWO, [["bedrock", undefined]]],
],
},
{
title:
"a target of a gateway that is not deployed gives no id, also when an export has its name",
gateways: [
[ONE, ["bedrock"]],
[TWO, ["egress-github"]],
],
deployed: [ONE],
exports: { bedrock: "bedrock-of-one", "egress-github": "github-of-a-previous-deploy" },
expected: [
[ONE, [["bedrock", "bedrock-of-one"]]],
[TWO, [["egress-github", undefined]]],
],
},
] as {
title: string;
gateways: [string, string[]][];
deployed: string[];
exports: Record<string, string>;
expected: [string, [string, string | undefined][]][];
}[])("$title", async ({ gateways, deployed, exports, expected }) => {
const input = await project();
input.spec = {
...input.spec,
agentCoreGateways: gateways.map(([name, targets]) => ({
name,
targets: targets.map((target) => ({ name: target })),
})),
} as unknown as typeof input.spec;
await updateTargetState(json, input.rootPath, TARGET.name, { stackArn: STACK_ARN });
const subject = harness({
describedStack: {
StackName: S,
CreationTime: new Date(0),
StackStatus: "CREATE_COMPLETE",
Outputs: [
...deployed.map((gateway) =>
out(`${S}-Gateway-${gateway.replace(/_/g, "-")}-Arn`, `arn:gateway/${gateway}`),
),
...Object.entries(exports).map(([name, id]) => out(`${S}-GatewayTarget-${name}-Id`, id)),
],
},
});

const resources = await subject.backend.resolveProjectResources(input, { target: TARGET });

expect(gatewayTargetIds(resources)).toEqual(expected);
});

test("nests a gateway's targets under the gateway", async () => {
const input = await project();
input.spec = {
Expand Down
34 changes: 27 additions & 7 deletions src/core/project/backends/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ export class CdkBackend implements ProjectBackend {
const asArn = (value: string | undefined) => (value ? { arn: value } : undefined);
const asId = (value: string | undefined) => (value ? { id: value } : undefined);

// The L3 joins a target name that two or more gateways have to its gateway with a colon.
const gatewaysByTargetName = new Map<string, number>();
for (const gateway of spec.agentCoreGateways) {
for (const name of new Set((gateway.targets ?? []).map((target) => target.name))) {
gatewaysByTargetName.set(name, (gatewaysByTargetName.get(name) ?? 0) + 1);
}
}

const identifierOf = (
resourceType: DeployableResource,
name: string,
Expand All @@ -675,8 +683,12 @@ export class CdkBackend implements ProjectBackend {
case "gateway":
return asArn(byExportName("Gateway", name, "Arn"));
case "gateway-target":
// The L3 exports an id for targets and never an ARN
return asId(byExportName("GatewayTarget", name, "Id"));
// The L3 exports an id for targets and never an ARN.
return asId(
Comment thread
SaiAbhishek98 marked this conversation as resolved.
Resolved
(gatewaysByTargetName.get(name) ?? 0) > 1
? byExportName("GatewayTarget", `${owner ?? ""}:${name}`, "Id")
: byExportName("GatewayTarget", name, "Id"),
);
case "policy":
// ExportName: <StackName>-Policy-<engineName>-<policyName>-Arn
return asArn(byExportName("Policy", owner ?? "", name, "Arn"));
Expand Down Expand Up @@ -742,13 +754,21 @@ export class CdkBackend implements ProjectBackend {
...spec.credentials.map(({ name }) => resolve("credential", name)),
...spec.evaluators.map(({ name }) => resolve("evaluator", name)),
...spec.onlineEvalConfigs.map(({ name }) => resolve("online-eval", name)),
...spec.agentCoreGateways.map((gateway) =>
resolve("gateway", gateway.name, {
...spec.agentCoreGateways.map((gateway) => {
// A target of a gateway that is not deployed has no id, even when an export has its name.
const deployed = identifierOf("gateway", gateway.name) !== undefined;
return resolve("gateway", gateway.name, {
children: (gateway.targets ?? []).map(({ name }) =>
resolve("gateway-target", name, { owner: gateway.name }),
deployed
? resolve("gateway-target", name, { owner: gateway.name })
: {
resourceType: "gateway-target" as const,
name,
deploymentState: "local-only" as const,
},
),
}),
),
});
}),
...spec.policyEngines.map((engine) =>
resolve("policy-engine", engine.name, {
children: (engine.policies ?? []).map(({ name }) =>
Expand Down
89 changes: 70 additions & 19 deletions src/core/project/manager.box.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AddResourceInput, Project } from "../../handlers/project/types";
import { resolveRuntimeTemplateShortcut } from "../../handlers/project/shortcuts";
import { createSilentLogger, TestIdentityClient } from "../../testing";
import { BOX_RELEASE } from "./backends/boxRelease";
import { FsProjectManager } from "./manager";
import { boxGatewayLogsNotice, FsProjectManager } from "./manager";
import { newProjectSpec } from "./templates/project";
import { defaultLogTargetNotice } from "./runtimeProfile/rules/additions";
import { boxStartFolder } from "./templates/boxProfile";
Expand Down Expand Up @@ -44,6 +44,18 @@ function subject(json?: ReadWriteJson): FsProjectManager {
});
}

/** A JSON reader and writer that refuses each write of `specFile`, for each user, root too. */
function refusingSpecWrites(specFile: string): ReadWriteJson {
const files = new FsReadWriteJson({ logger: createSilentLogger() });
return {
read: (path, schema) => files.read(path, schema),
write: async (path, data) => {
if (path === specFile) throw new Error(`the test refuses the write of ${path}`);
return files.write(path, data);
},
};
}

async function drain<T>(generator: AsyncGenerator<unknown, T>): Promise<T> {
while (true) {
const next = await generator.next();
Expand Down Expand Up @@ -202,21 +214,26 @@ describe("agentcore create", () => {
tempDirectories.push(directory);
process.chdir(directory);

const project = await drain(
manager.create({
name: "example",
skipInstall: true,
skipGit: true,
scaffoldRuntimeInput: resolveRuntimeTemplateShortcut(template, { runtimeName: "agent" }),
}),
);
const warnings: string[] = [];
const generator = manager.create({
name: "example",
skipInstall: true,
skipGit: true,
scaffoldRuntimeInput: resolveRuntimeTemplateShortcut(template, { runtimeName: "agent" }),
});
let next = await generator.next();
for (; !next.done; next = await generator.next()) {
if (next.value.type === "warning") warnings.push(next.value.message);
}
const project = next.value;

// Only a box template runs its agent in a box, so only it makes a Box project.
const spec = await specOnDisk(project);
expect(spec.projectType).toBe(projectType);
expect((spec.agentCoreGateways ?? []).map(({ name }) => name)).toEqual(
projectType ? ["agent-box-egress"] : [],
);
expect(warnings.includes(boxGatewayLogsNotice("agent-box-egress"))).toBe(projectType === "box");
});

test('a Box template writes "projectType": "box" and a Box gateway for each runtime', () => {
Expand All @@ -236,6 +253,7 @@ describe("agentcore create", () => {
protocolType: "None",
boundRuntime: "my_agent",
discoveryPrefix: "AGENTCORE_GATEWAY_BOX",
enableApplicationLogs: true,
targets: [],
});
});
Expand Down Expand Up @@ -399,6 +417,24 @@ describe("gateway targets in a Box project", () => {
]);
});

test("add runtime says that the new Box gateway sends its logs, and how to stop them", async () => {
const manager = subject();
const project = await createdProject(manager);

const warnings: string[] = [];
const generator = manager.addResource(project, {
resourceType: "runtime",
resourceConfig: { name: "search", language: "Python", protocol: "HTTP" },
} as never);
for (let next = await generator.next(); !next.done; next = await generator.next()) {
const event = next.value as { type: string; message?: string };
if (event.type === "warning") warnings.push(event.message!);
}

const notices = warnings.filter((warning) => warning.includes("APPLICATION_LOGS"));
expect(notices).toEqual([boxGatewayLogsNotice(boxGatewaySpec("search").name)]);
});

test("add runtime refuses a Bedrock Agent import in a Box project, and changes nothing", async () => {
const manager = subject();
const project = await createdProject(manager);
Expand Down Expand Up @@ -592,16 +628,7 @@ describe("gateway targets in a Box project", () => {
test("a failed write of agentcore.json gives no notice of the default log target", async () => {
const project = await createdProject(subject());
const specFile = join(project.rootPath, "agentcore", "agentcore.json");
// The write of agentcore.json fails for each user, root too; each other file is written.
const files = new FsReadWriteJson({ logger: createSilentLogger() });
const json: ReadWriteJson = {
read: (path, schema) => files.read(path, schema),
write: async (path, data) => {
if (path === specFile) throw new Error(`the test refuses the write of ${path}`);
return files.write(path, data);
},
};
const generator = subject(json).addResource(project, {
const generator = subject(refusingSpecWrites(specFile)).addResource(project, {
resourceType: "runtime",
resourceConfig: { name: "search", language: "Python", protocol: "HTTP" },
} as never);
Expand All @@ -617,6 +644,30 @@ describe("gateway targets in a Box project", () => {
expect(events.some((event) => (event as { type?: string }).type === "warning")).toBe(false);
});

test("a failed write of agentcore.json gives no notice that the new Box gateway sends its logs", async () => {
const project = await createdProject(subject());
const specFile = join(project.rootPath, "agentcore", "agentcore.json");
const before = await specText(project);
const generator = subject(refusingSpecWrites(specFile)).addResource(project, {
resourceType: "runtime",
resourceConfig: { name: "search", language: "Python", protocol: "HTTP" },
} as never);
const messages: (string | undefined)[] = [];

await expect(
(async () => {
for (let next = await generator.next(); !next.done; next = await generator.next()) {
messages.push((next.value as { message?: string }).message);
}
})(),
).rejects.toThrow(`the test refuses the write of ${specFile}`);
// The add wrote the box profile of the new runtime, and then the write of agentcore.json failed.
const profile = join(project.rootPath, ".box", "search", "local", "profile.toml");
expect(messages).toContain(`writing box profile ${profile}`);
expect(messages).not.toContain(boxGatewayLogsNotice(boxGatewaySpec("search").name));
Comment thread
SaiAbhishek98 marked this conversation as resolved.
Resolved
expect(await specText(project)).toBe(before);
});

test("add runtime warns when the kept box profile is for another protocol", async () => {
const manager = subject();
let project = await createdProject(manager, ["agent", "search"]);
Expand Down
Loading
Loading