From 0a5777030c8d707781072ba90c4e6135c987739a Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 30 Nov 2026 12:29:43 -0800 Subject: [PATCH 1/4] test(e2e): Check that a boxed agent's OpenTelemetry logs, box's policy records and its traces reach CloudWatch --- e2eTest/project/templates.test.ts | 118 +++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) diff --git a/e2eTest/project/templates.test.ts b/e2eTest/project/templates.test.ts index bd25a39b2..6c0ced56a 100644 --- a/e2eTest/project/templates.test.ts +++ b/e2eTest/project/templates.test.ts @@ -5,7 +5,7 @@ import { existsSync } from "node:fs"; import { cp, mkdtemp, readdir, readFile, writeFile } from "node:fs/promises"; import { randomBytes } from "node:crypto"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { basename, join } from "node:path"; import z from "zod"; import { E2E_PREFIX, TAGS } from "../constants"; import { CliRunner, parseResult, type RunResult } from "../helpers/run"; @@ -229,6 +229,11 @@ const LocalRuntimeInvokeResponseSchema = RuntimeInvokeResponseSchema.extend({ }); const DeployResponseSchema = z.object({ message: z.string() }); const JsonObjectSchema = z.record(z.string(), z.unknown()); +const TracesSchema = z.object({ + traces: z.array( + z.object({ traceId: z.string(), spanCount: z.union([z.string(), z.number()]).optional() }), + ), +}); const McpResponseSchema = z.object({ jsonrpc: z.literal("2.0"), id: z.unknown(), @@ -874,6 +879,117 @@ describe.skipIf(!BOX_DEPLOYS)( }, ); + // With OTEL on, as the template has it, box sends the agent's OpenTelemetry logs and its own + // policy records to the runtime's log group (`[telemetry.agentcore]`), and the spans of the + // agent and of box to X-Ray and the runtime's spans stream (`[telemetry.agentcore_traces]`). + // The test reads them back through `agentcore logs` and `agentcore traces`, as a user does. + test.concurrent( + "a boxed agent's OpenTelemetry logs, box's records and the traces reach CloudWatch", + { timeout: DEPLOY_TEST_TIMEOUT + 10 * 60 * 1000 }, + async () => { + const projectDir = await createTemplateProject(cli, strands, { deploys: true }); + // The resource of the agent's spans and logs, which the Runtime names after the runtime. + const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; + await deployed(cli, projectDir, async () => { + await ask( + cli, + projectDir, + CREATED_RUNTIME_NAME, + getSessionId("box-otel"), + "Reply with a short greeting.", + ); + await assertRunsInBox(cli, projectDir); + const logs = (query: string) => + cli.run( + [ + "logs", + "runtime", + "--name", + CREATED_RUNTIME_NAME, + "--since", + "30m", + "--query", + query, + ], + projectDir, + ); + // The agent's OpenTelemetry log records, under the runtime's resource. + await retry( + async () => { + const found = await logs(`"${service}"`); + expect(found.exitCode).toBe(0); + expect(found.stdout).toMatch(/"resource"/); + expect(found.stdout).toContain(service); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + // Box's own policy records for the agent's calls, with their verdict. + await retry( + async () => { + const found = await logs('"strands.box.policy.verdict"'); + expect(found.exitCode).toBe(0); + expect(found.stdout).toContain("strands.box.policy.verdict"); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + // A trace of the invoke, with the agent's spans and box's. + const traceId = await retry( + async () => { + const listed = parseResult( + TracesSchema, + await cli.run( + [ + "traces", + "runtime", + "list", + "--name", + CREATED_RUNTIME_NAME, + "--since", + "30m", + "--json", + ], + projectDir, + ), + ); + const trace = listed.traces.find((t) => Number(t.spanCount ?? 0) > 1); + if (!trace) throw new Error(`no trace with spans yet: ${JSON.stringify(listed)}`); + return trace.traceId; + }, + 10 * 60 * 1000, + 30 * 1000, + ); + const output = join(projectDir, "trace.json"); + parseResult( + JsonObjectSchema, + await cli.run( + [ + "traces", + "runtime", + "get", + traceId, + "--name", + CREATED_RUNTIME_NAME, + "--since", + "30m", + "--output", + output, + "--json", + ], + projectDir, + ), + ); + const spans = ( + JSON.parse(await readFile(output, "utf8")) as { "@message"?: unknown }[] + ).map((record) => JSON.stringify(record["@message"] ?? record)); + expect(spans.length).toBeGreaterThan(1); + expect(spans.some((span) => span.includes(service))).toBe(true); + expect(spans.some((span) => span.includes("strands.box"))).toBe(true); + }); + }, + ); + // The template's agent keeps the conversation in Memory: short-term events within a session, // and facts that long-term extraction makes available to a new session of the same actor. test.concurrent( From b1d4be161fe0e3ef606aa3553c61794168876083 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 30 Nov 2026 12:46:33 -0800 Subject: [PATCH 2/4] test(e2e): Check the invoke's own trace, by its session, not box's start trace --- e2eTest/project/templates.test.ts | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/e2eTest/project/templates.test.ts b/e2eTest/project/templates.test.ts index 6c0ced56a..5e72ed0e5 100644 --- a/e2eTest/project/templates.test.ts +++ b/e2eTest/project/templates.test.ts @@ -231,7 +231,11 @@ const DeployResponseSchema = z.object({ message: z.string() }); const JsonObjectSchema = z.record(z.string(), z.unknown()); const TracesSchema = z.object({ traces: z.array( - z.object({ traceId: z.string(), spanCount: z.union([z.string(), z.number()]).optional() }), + z.object({ + traceId: z.string(), + sessionId: z.string().optional(), + spanCount: z.union([z.string(), z.number()]).optional(), + }), ), }); const McpResponseSchema = z.object({ @@ -890,14 +894,9 @@ describe.skipIf(!BOX_DEPLOYS)( const projectDir = await createTemplateProject(cli, strands, { deploys: true }); // The resource of the agent's spans and logs, which the Runtime names after the runtime. const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; + const session = getSessionId("box-otel"); await deployed(cli, projectDir, async () => { - await ask( - cli, - projectDir, - CREATED_RUNTIME_NAME, - getSessionId("box-otel"), - "Reply with a short greeting.", - ); + await ask(cli, projectDir, CREATED_RUNTIME_NAME, session, "Reply with a short greeting."); await assertRunsInBox(cli, projectDir); const logs = (query: string) => cli.run( @@ -934,7 +933,8 @@ describe.skipIf(!BOX_DEPLOYS)( 5 * 60 * 1000, 20 * 1000, ); - // A trace of the invoke, with the agent's spans and box's. + // The trace of the invoke, by its session: box's own start makes a trace of its own, with + // box's spans only. The invoke's has the agent's spans and box's for the agent's calls. const traceId = await retry( async () => { const listed = parseResult( @@ -953,8 +953,9 @@ describe.skipIf(!BOX_DEPLOYS)( projectDir, ), ); - const trace = listed.traces.find((t) => Number(t.spanCount ?? 0) > 1); - if (!trace) throw new Error(`no trace with spans yet: ${JSON.stringify(listed)}`); + const trace = listed.traces.find((t) => t.sessionId === session); + if (!trace) + throw new Error(`no trace of session ${session} yet: ${JSON.stringify(listed)}`); return trace.traceId; }, 10 * 60 * 1000, From e9ee916bd3cc92d08a7138ba31ed3b45b731d815 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Mon, 30 Nov 2026 13:21:51 -0800 Subject: [PATCH 3/4] test(e2e): Check the agent's OpenTelemetry logs and the invoke's trace in every template deploy test, and box's records and spans in the Box ones --- e2eTest/project/templates.test.ts | 210 ++++++++++++++---------------- 1 file changed, 98 insertions(+), 112 deletions(-) diff --git a/e2eTest/project/templates.test.ts b/e2eTest/project/templates.test.ts index 5e72ed0e5..c9a0fb47e 100644 --- a/e2eTest/project/templates.test.ts +++ b/e2eTest/project/templates.test.ts @@ -303,10 +303,12 @@ describe( TIMEOUT_MS.PROJECT_DEV + 2 * TIMEOUT_MS.PROJECT_INVOKE + 2 * TIMEOUT_MS.PROJECT_DEPLOY + - TIMEOUT_MS.PROJECT_REMOVE, + TIMEOUT_MS.PROJECT_REMOVE + + TELEMETRY_TIMEOUT, }, async (runtime) => { const projectDir = await createTemplateProject(cli, runtime, { deploys: true }); + const session = getSessionId(runtime.name); let scenarioCompleted = false; try { if (!runtime.skipDev) { @@ -332,7 +334,7 @@ describe( "--runtime", CREATED_RUNTIME_NAME, "--session-id", - getSessionId(runtime.name), + session, "--payload", JSON.stringify(runtime.payload), "--json", @@ -350,6 +352,7 @@ describe( assertProtocolResponse(runtime, response.body); } if (runtime.box) await assertRunsInBox(cli, projectDir); + await assertTelemetry(cli, projectDir, session, { box: Boolean(runtime.box) }); const removed = parseResult( OperationSchema, @@ -416,10 +419,12 @@ describe.skipIf(!BOX_DEPLOYS)( 2 * BOX_TEMPLATES.length * (TIMEOUT_MS.PROJECT_DEV + TIMEOUT_MS.PROJECT_INVOKE) + 2 * TIMEOUT_MS.PROJECT_INVOKE + 3 * TIMEOUT_MS.PROJECT_DEPLOY + - TIMEOUT_MS.PROJECT_REMOVE, + TIMEOUT_MS.PROJECT_REMOVE + + TELEMETRY_TIMEOUT, }, async (runtime) => { const projectDir = await createTemplateProject(cli, runtime, { deploys: true }); + const session = getSessionId(runtime.name); const withDev = fullFlow(runtime); let scenarioCompleted = false; try { @@ -443,7 +448,7 @@ describe.skipIf(!BOX_DEPLOYS)( "--runtime", CREATED_RUNTIME_NAME, "--session-id", - getSessionId(runtime.name), + session, "--payload", JSON.stringify(runtime.payload), "--json", @@ -462,6 +467,7 @@ describe.skipIf(!BOX_DEPLOYS)( } await assertRunsInBox(cli, projectDir); + await assertTelemetry(cli, projectDir, session, { box: true }); // Dev again after build and deploy: they leave state (box's profile bindings, the // Runtime profile, .box-dist) that dev must still run beside. @@ -883,114 +889,6 @@ describe.skipIf(!BOX_DEPLOYS)( }, ); - // With OTEL on, as the template has it, box sends the agent's OpenTelemetry logs and its own - // policy records to the runtime's log group (`[telemetry.agentcore]`), and the spans of the - // agent and of box to X-Ray and the runtime's spans stream (`[telemetry.agentcore_traces]`). - // The test reads them back through `agentcore logs` and `agentcore traces`, as a user does. - test.concurrent( - "a boxed agent's OpenTelemetry logs, box's records and the traces reach CloudWatch", - { timeout: DEPLOY_TEST_TIMEOUT + 10 * 60 * 1000 }, - async () => { - const projectDir = await createTemplateProject(cli, strands, { deploys: true }); - // The resource of the agent's spans and logs, which the Runtime names after the runtime. - const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; - const session = getSessionId("box-otel"); - await deployed(cli, projectDir, async () => { - await ask(cli, projectDir, CREATED_RUNTIME_NAME, session, "Reply with a short greeting."); - await assertRunsInBox(cli, projectDir); - const logs = (query: string) => - cli.run( - [ - "logs", - "runtime", - "--name", - CREATED_RUNTIME_NAME, - "--since", - "30m", - "--query", - query, - ], - projectDir, - ); - // The agent's OpenTelemetry log records, under the runtime's resource. - await retry( - async () => { - const found = await logs(`"${service}"`); - expect(found.exitCode).toBe(0); - expect(found.stdout).toMatch(/"resource"/); - expect(found.stdout).toContain(service); - }, - 5 * 60 * 1000, - 20 * 1000, - ); - // Box's own policy records for the agent's calls, with their verdict. - await retry( - async () => { - const found = await logs('"strands.box.policy.verdict"'); - expect(found.exitCode).toBe(0); - expect(found.stdout).toContain("strands.box.policy.verdict"); - }, - 5 * 60 * 1000, - 20 * 1000, - ); - // The trace of the invoke, by its session: box's own start makes a trace of its own, with - // box's spans only. The invoke's has the agent's spans and box's for the agent's calls. - const traceId = await retry( - async () => { - const listed = parseResult( - TracesSchema, - await cli.run( - [ - "traces", - "runtime", - "list", - "--name", - CREATED_RUNTIME_NAME, - "--since", - "30m", - "--json", - ], - projectDir, - ), - ); - const trace = listed.traces.find((t) => t.sessionId === session); - if (!trace) - throw new Error(`no trace of session ${session} yet: ${JSON.stringify(listed)}`); - return trace.traceId; - }, - 10 * 60 * 1000, - 30 * 1000, - ); - const output = join(projectDir, "trace.json"); - parseResult( - JsonObjectSchema, - await cli.run( - [ - "traces", - "runtime", - "get", - traceId, - "--name", - CREATED_RUNTIME_NAME, - "--since", - "30m", - "--output", - output, - "--json", - ], - projectDir, - ), - ); - const spans = ( - JSON.parse(await readFile(output, "utf8")) as { "@message"?: unknown }[] - ).map((record) => JSON.stringify(record["@message"] ?? record)); - expect(spans.length).toBeGreaterThan(1); - expect(spans.some((span) => span.includes(service))).toBe(true); - expect(spans.some((span) => span.includes("strands.box"))).toBe(true); - }); - }, - ); - // The template's agent keeps the conversation in Memory: short-term events within a session, // and facts that long-term extraction makes available to a new session of the same actor. test.concurrent( @@ -1480,6 +1378,94 @@ async function assertRunsInBox( expect(errors.stdout).not.toMatch(/ERROR|blocked by egress control|AccessDenied/); } +/** + * Checks that the deployed agent's telemetry reached CloudWatch after an invoke in `session`: its + * OpenTelemetry log records under the runtime's resource in the runtime's log group, and the + * invoke's trace, found by its session (`agentcore traces runtime list`), with the agent's spans. + * With `box`, also box's policy records for the agent's calls, and box's spans in that trace: box + * sends both to CloudWatch (`[telemetry.agentcore]`, `[telemetry.agentcore_traces]`). Box's own + * start makes a trace of its own, with box's spans only, so the trace is picked by session. + */ +async function assertTelemetry( + cli: CliRunner, + projectDir: string, + session: string, + { box }: { box: boolean }, +): Promise { + // The resource of the agent's spans and logs, which the Runtime names after the runtime. + const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; + const logs = (query: string) => + cli.run( + ["logs", "runtime", "--name", CREATED_RUNTIME_NAME, "--since", "30m", "--query", query], + projectDir, + ); + await retry( + async () => { + const found = await logs(`"${service}"`); + expect(found.exitCode).toBe(0); + expect(found.stdout).toMatch(/"resource"/); + expect(found.stdout).toContain(service); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + if (box) { + await retry( + async () => { + const found = await logs('"strands.box.policy.verdict"'); + expect(found.exitCode).toBe(0); + expect(found.stdout).toContain("strands.box.policy.verdict"); + }, + 5 * 60 * 1000, + 20 * 1000, + ); + } + const traceId = await retry( + async () => { + const listed = parseResult( + TracesSchema, + await cli.run( + ["traces", "runtime", "list", "--name", CREATED_RUNTIME_NAME, "--since", "30m", "--json"], + projectDir, + ), + ); + const trace = listed.traces.find((t) => t.sessionId === session); + if (!trace) throw new Error(`no trace of session ${session} yet: ${JSON.stringify(listed)}`); + return trace.traceId; + }, + 10 * 60 * 1000, + 30 * 1000, + ); + const output = join(projectDir, "trace.json"); + parseResult( + JsonObjectSchema, + await cli.run( + [ + "traces", + "runtime", + "get", + traceId, + "--name", + CREATED_RUNTIME_NAME, + "--since", + "30m", + "--output", + output, + "--json", + ], + projectDir, + ), + ); + const spans = (JSON.parse(await readFile(output, "utf8")) as { "@message"?: unknown }[]).map( + (record) => JSON.stringify(record["@message"] ?? record), + ); + expect(spans.some((span) => span.includes(service))).toBe(true); + if (box) expect(spans.some((span) => span.includes("strands.box"))).toBe(true); +} + +/** How long assertTelemetry may wait for CloudWatch, for the timeouts of the tests that call it. */ +const TELEMETRY_TIMEOUT = 20 * 60 * 1000; + /** Deploys `projectDir`, runs `scenario`, and tears the stack down even when a step fails. */ async function deployed( cli: CliRunner, From 2c275ac0fdcf0fd62615eecc72a9aadc24047323 Mon Sep 17 00:00:00 2001 From: Quang Nguyen Date: Tue, 1 Dec 2026 05:28:28 -0800 Subject: [PATCH 4/4] test(e2e): Check only the telemetry each template sends The TypeScript templates have no OpenTelemetry SDK and the FastMCP server sends no log records, so their deploy tests skip assertTelemetry. The minimal Box agent makes no outbound call, so box records no policy decision for it, and its test skips that one check. The full e2e run had failed these four on every runner. --- e2eTest/project/templates.test.ts | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/e2eTest/project/templates.test.ts b/e2eTest/project/templates.test.ts index c9a0fb47e..a03eafc66 100644 --- a/e2eTest/project/templates.test.ts +++ b/e2eTest/project/templates.test.ts @@ -30,6 +30,14 @@ type RuntimeTemplateTestCase = { * concurrent Runtime tests skip it; the sequential dev tests below cover it. */ box?: boolean; + /** + * What of the agent's telemetry the test checks in CloudWatch after the deployed invoke + * (assertTelemetry): `"none"` for a template that sends none (the TypeScript templates have no + * OpenTelemetry SDK, and the FastMCP server sends no log records), and for a Box template whose + * agent makes no outbound call `"no-policy-records"`, since box then records no policy decision. + * Every check otherwise. + */ + telemetry?: "none" | "no-policy-records"; }; const MCP_INITIALIZE_PAYLOAD = { @@ -137,18 +145,21 @@ const RUNTIME_TEMPLATES: RuntimeTemplateTestCase[] = [ }, { name: "agent_ts_strands", + telemetry: "none" as const, template: "agent-typescript-strands", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, }, { name: "agent_ts_vercel", + telemetry: "none" as const, template: "agent-typescript-vercel", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, }, { name: "mcp_python_fastmcp", + telemetry: "none" as const, template: "mcp-python-fastmcp", protocol: "MCP" as const, payload: MCP_INITIALIZE_PAYLOAD, @@ -175,6 +186,7 @@ const RUNTIME_TEMPLATES: RuntimeTemplateTestCase[] = [ const BOX_TEMPLATES: RuntimeTemplateTestCase[] = [ { name: "agent_python_minimal_box", + telemetry: "no-policy-records" as const, template: "agent-python-minimal-box", protocol: "HTTP" as const, payload: { prompt: "Reply with a short greeting." }, @@ -352,7 +364,12 @@ describe( assertProtocolResponse(runtime, response.body); } if (runtime.box) await assertRunsInBox(cli, projectDir); - await assertTelemetry(cli, projectDir, session, { box: Boolean(runtime.box) }); + if (runtime.telemetry !== "none") { + await assertTelemetry(cli, projectDir, session, { + box: Boolean(runtime.box), + policyRecords: runtime.telemetry !== "no-policy-records", + }); + } const removed = parseResult( OperationSchema, @@ -467,7 +484,10 @@ describe.skipIf(!BOX_DEPLOYS)( } await assertRunsInBox(cli, projectDir); - await assertTelemetry(cli, projectDir, session, { box: true }); + await assertTelemetry(cli, projectDir, session, { + box: true, + policyRecords: runtime.telemetry !== "no-policy-records", + }); // Dev again after build and deploy: they leave state (box's profile bindings, the // Runtime profile, .box-dist) that dev must still run beside. @@ -1382,7 +1402,8 @@ async function assertRunsInBox( * Checks that the deployed agent's telemetry reached CloudWatch after an invoke in `session`: its * OpenTelemetry log records under the runtime's resource in the runtime's log group, and the * invoke's trace, found by its session (`agentcore traces runtime list`), with the agent's spans. - * With `box`, also box's policy records for the agent's calls, and box's spans in that trace: box + * With `box`, also box's policy records for the agent's calls (unless `policyRecords` is false, for an + * agent that makes none), and box's spans in that trace: box * sends both to CloudWatch (`[telemetry.agentcore]`, `[telemetry.agentcore_traces]`). Box's own * start makes a trace of its own, with box's spans only, so the trace is picked by session. */ @@ -1390,7 +1411,7 @@ async function assertTelemetry( cli: CliRunner, projectDir: string, session: string, - { box }: { box: boolean }, + { box, policyRecords = box }: { box: boolean; policyRecords?: boolean }, ): Promise { // The resource of the agent's spans and logs, which the Runtime names after the runtime. const service = `${basename(projectDir)}_default_${CREATED_RUNTIME_NAME}.DEFAULT`; @@ -1409,7 +1430,7 @@ async function assertTelemetry( 5 * 60 * 1000, 20 * 1000, ); - if (box) { + if (box && policyRecords) { await retry( async () => { const found = await logs('"strands.box.policy.verdict"');