From 2578624f484cd9838a471230c8a8bfa9b8a3c772 Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Tue, 24 Nov 2026 13:35:04 -0800 Subject: [PATCH 1/5] feat(box): Send the spans of a boxed agent to the log group of its runtime The default X-Ray target of a Runtime profile sent the spans to X-Ray with no log group, so X-Ray put them in aws/spans, and the spans stream of the runtime's own log group stayed empty. With a box release that takes `log_group` and `log_stream` on a `cloudwatch_traces` target (strands-agents/box#629), the default target now names the log group and the log stream that the Runtime sets in OTEL_EXPORTER_OTLP_TRACES_HEADERS. The @box/agentcore pack reads the two values through the inputs `runtime.traces_log_group` and `runtime.traces_log_stream`. BOX_TRACES_LOG_GROUP_VERSION is the first box release that takes the two keys. A box.version before it gets the target without them, because an older box refuses them. Set the constant to the release that ships strands-agents/box#629. Tests: additions.test.ts and boxProfile.test.ts pin both sides of the version gate, the header line, and the grant. bun test: 6516 pass, 9 skip, 0 fail. Typecheck, lint and format pass. --- src/core/project/boxNetworkSource.test.ts | 2 + .../runtimeProfile/rules/additions.test.ts | 57 +++++++++++++++++-- .../project/runtimeProfile/rules/additions.ts | 43 ++++++++++++-- src/core/project/runtimeProfile/types.ts | 4 ++ src/core/project/templates/boxProfile.test.ts | 35 ++++++++++++ src/core/project/templates/boxProfile.ts | 47 ++++++++++++++- 6 files changed, 174 insertions(+), 14 deletions(-) diff --git a/src/core/project/boxNetworkSource.test.ts b/src/core/project/boxNetworkSource.test.ts index e141e15e5..8ed27c208 100644 --- a/src/core/project/boxNetworkSource.test.ts +++ b/src/core/project/boxNetworkSource.test.ts @@ -476,6 +476,8 @@ describe("the default X-Ray target of a runtime with OTEL on", () => { region: "${{ inputs.aws.region }}", include: ["deny", "permit", "trace"], secret: { ref: "aws://" }, + log_group: "${{ inputs.runtime.traces_log_group }}", + log_stream: "${{ inputs.runtime.traces_log_stream }}", }); const [file] = policyOf(on!); expect(JSON.parse((file as { text: string }).text)).toEqual({ diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 5d6d91d5a..8c8dc36d9 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -33,6 +33,7 @@ import { defaultTracesTelemetry, MEMORY_ROUTE, runtimeProfileHeader, + runtimeTracesTarget, } from "./additions"; const PROFILE_FILE = ".box/agent/local/profile.toml"; @@ -173,12 +174,17 @@ const DEFAULT_TARGET = { secret: { ref: "aws://" }, }; -/** The default X-Ray target of a Runtime profile with OTEL on. */ +/** + * The default X-Ray target of a Runtime profile with OTEL on, for a box that takes the log group + * and stream of a `cloudwatch_traces` target, as the box of {@link runtimeOf} does. + */ const DEFAULT_TRACES = { kind: "cloudwatch_traces", region: "${{ inputs.aws.region }}", include: ["deny", "permit", "trace"], secret: { ref: "aws://" }, + log_group: "${{ inputs.runtime.traces_log_group }}", + log_stream: "${{ inputs.runtime.traces_log_stream }}", }; /** The words that start the agent under opentelemetry-instrument on the Runtime. */ @@ -1426,7 +1432,40 @@ describe("the additions rule", () => { expect(tableOf(apply({}, contextOf(runtimeOf(build))).table)).toEqual({ agentcore: DEFAULT_TARGET, }); - expect({ ...DEFAULT_TRACES_TARGET } as TomlTable).toEqual(DEFAULT_TRACES); + expect(runtimeTracesTarget(otelRuntime(build))).toEqual(DEFAULT_TRACES); + }, + ); + + test.each([ + ["0.3.0", false], + ["0.3.1", false], + ["0.3.0-preview.1", false], + ["0.1.11-preview.10", false], + ["0.3.2-preview.1", false], + ["0.3.2", true], + ["0.4.0", true], + ["1.0.0", true], + ])( + "sends the spans of the default X-Ray target to the runtime's own log group and stream only for a box that takes them (box %s)", + (version, routed) => { + const runtime = { ...OTEL_CONTAINER, box: { version } } as ProjectRuntime; + const traces = tableOf(apply({}, contextOf(runtime)).table).agentcore_traces; + const { log_group, log_stream, ...rest } = DEFAULT_TRACES; + + expect(traces).toEqual(routed ? DEFAULT_TRACES : rest); + expect(defaultTracesTelemetry(apply({}, contextOf(runtime)).table, BUILD_REGION)).toEqual([ + { + name: "agentcore_traces", + kind: "cloudwatch_traces", + region: BUILD_REGION, + include: rest.include, + }, + ]); + expect({ ...DEFAULT_TRACES_TARGET } as TomlTable).toEqual(rest); + expect([log_group, log_stream]).toEqual([ + "${{ inputs.runtime.traces_log_group }}", + "${{ inputs.runtime.traces_log_stream }}", + ]); }, ); @@ -1517,10 +1556,18 @@ describe("the additions rule", () => { expect(inputs.aws.region).toMatchObject({ from: "env:AWS_REGION" }); }); - test("names the default X-Ray target in the header", () => { - const context = contextOf(OTEL_CODEZIP); + test("names the default X-Ray target in the header, with the runtime's own log group when the box takes it", () => { + const routed = contextOf(OTEL_CODEZIP); + const plain = contextOf({ ...OTEL_CODEZIP, box: { version: "0.3.0" } } as ProjectRuntime); - expect(runtimeProfileHeader(context, apply({}, context).table).at(-1)).toBe( + expect(runtimeProfileHeader(routed, apply({}, routed).table).at(-1)).toBe( + "[telemetry.agentcore_traces] is the default X-Ray target: the agent's spans and box's " + + "own, in the Runtime's region, to the runtime's own log group and log stream, which " + + "box reads from OTEL_EXPORTER_OTLP_TRACES_HEADERS when the run starts. Add a " + + `cloudwatch_traces target to ${PROFILE_FILE} to use another, or set ` + + "instrumentation.enableOtel to false in agentcore.json to send none.", + ); + expect(runtimeProfileHeader(plain, apply({}, plain).table).at(-1)).toBe( "[telemetry.agentcore_traces] is the default X-Ray target: the agent's spans and box's " + `own, in the Runtime's region. Add a cloudwatch_traces target to ${PROFILE_FILE} to ` + "use another, or set instrumentation.enableOtel to false in agentcore.json to send none.", diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 21dce6603..4ae4fa762 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -15,7 +15,7 @@ * | `[tool.]` | a table that the template writes, as it writes it, goes; a table of the user stays | * | `[telemetry.]` | each CloudWatch target that box takes is signed with a bare `aws://`, in the deploy region when it names none; one that box refuses goes; a `file` target goes, with a warning | * | `[telemetry.agentcore]` | added when no target is `cloudwatch_logs`: the runtime's own log group and log stream | - * | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region | + * | `[telemetry.agentcore_traces]` | added with OTEL on when no target is `cloudwatch_traces`: X-Ray in the Runtime's region, to the runtime's own log group and spans stream when the box takes them | * | `[egress.agentcore]` | added when the runtime has its default Memory and no route of the profile has the AgentCore host: that host, which box signs with a bare `aws://` and sends to directly, not through the Box gateway | * | a route to the AgentCore host | with a default Memory, it goes to the host directly, signed with a bare `aws://`, with its name and other keys | * @@ -34,9 +34,13 @@ import { boxWorkloadPort, OBSERVABILITY_OFF_ENV, observabilityOff, + BOX_TRACES_LOG_GROUP_VERSION, otelOn, RUNTIME_LOGS_HEADERS_ENV, + RUNTIME_TRACES_HEADERS_ENV, + tracesToRuntimeLogGroup, } from "../../templates/boxProfile"; +import type { ProjectRuntime } from "../../../../projectSchemas/runtime"; import type { CloudWatchTelemetry } from "../../boxNetwork"; import { isTable, isVenvPython } from "../projectPaths"; import { FILE_TELEMETRY_KIND } from "./refusals"; @@ -102,7 +106,7 @@ export const DEFAULT_TELEMETRY_TARGET = { /** * The X-Ray target of a Runtime profile with OTEL on ({@link otelOn}) that has no * `cloudwatch_traces` target: the agent's spans and box's own, in the Runtime's region, signed with - * the execution role. + * the execution role. {@link runtimeTracesTarget} adds the runtime's own log group and stream. */ export const DEFAULT_TRACES_TARGET = { kind: "cloudwatch_traces", @@ -111,6 +115,21 @@ export const DEFAULT_TRACES_TARGET = { secret: { ref: RUNTIME_AWS_SIGNER }, } as const; +/** + * {@link DEFAULT_TRACES_TARGET} for `runtime`: when its box takes them ({@link tracesToRuntimeLogGroup}), + * with the log group and log stream that the Runtime names for its spans, so the spans reach the + * runtime's own log group as the spans of a runtime without a box do. + */ +export function runtimeTracesTarget(runtime: Pick): TomlTable { + return { + ...structuredClone(DEFAULT_TRACES_TARGET), + ...(tracesToRuntimeLogGroup(runtime) && { + log_group: RUNTIME_INPUTS.tracesLogGroup, + log_stream: RUNTIME_INPUTS.tracesLogStream, + }), + }; +} + /** The program that starts a Python command with the OpenTelemetry instrumentation. */ const OTEL_INSTRUMENT = "opentelemetry-instrument"; @@ -180,16 +199,28 @@ export function runtimeProfileHeader( ...defaultTracesTargets(profile).map( (name) => `[telemetry.${name}] is the default X-Ray target: the agent's spans and box's own, in the ` + - `Runtime's region. Add a cloudwatch_traces target to ${context.profileFile} to use ` + + "Runtime's region" + + (Object.hasOwn(tableOf(tableOf(profile.telemetry)[name]), "log_group") + ? `, to the runtime's own log group and log stream, which box reads from ` + + `${RUNTIME_TRACES_HEADERS_ENV} when the run starts` + : "") + + `. Add a cloudwatch_traces target to ${context.profileFile} to use ` + "another, or set instrumentation.enableOtel to false in agentcore.json to send none.", ), ]; } -/** The name of each {@link DEFAULT_TRACES_TARGET} in the `[telemetry]` of `profile`. */ +/** + * The name of each default X-Ray target in the `[telemetry]` of `profile`, in either form of + * {@link runtimeTracesTarget}. + */ function defaultTracesTargets(profile: Readonly): string[] { + const forms = [ + runtimeTracesTarget({}), + runtimeTracesTarget({ box: { version: BOX_TRACES_LOG_GROUP_VERSION } }), + ]; return Object.entries(tableOf(profile.telemetry)) - .filter(([, target]) => sameValue(target, DEFAULT_TRACES_TARGET)) + .filter(([, target]) => forms.some((form) => sameValue(target, form))) .map(([name]) => name); } @@ -755,7 +786,7 @@ function runtimeTelemetry( const name = Object.hasOwn(result, "agentcore_traces") ? "agentcore_traces_default" : "agentcore_traces"; - result[name] = structuredClone(DEFAULT_TRACES_TARGET); + result[name] = runtimeTracesTarget(context.runtime); } return result; } diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index dce187321..9e82c3fe6 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -24,6 +24,10 @@ export const RUNTIME_INPUTS = { gatewayUrl: "${{ inputs.gateway.url }}", /** The Runtime's region (@box/agentcore). */ awsRegion: "${{ inputs.aws.region }}", + /** The runtime's own log group, for its spans (@box/agentcore). */ + tracesLogGroup: "${{ inputs.runtime.traces_log_group }}", + /** The log stream of the runtime's spans (@box/agentcore). */ + tracesLogStream: "${{ inputs.runtime.traces_log_stream }}", /** The unpacked zip of a CodeZip runtime (@box/agentcore-codezip). */ codeDir: "${{ inputs.code.dir }}", /** The Python of a CodeZip Runtime (@box/agentcore-codezip). */ diff --git a/src/core/project/templates/boxProfile.test.ts b/src/core/project/templates/boxProfile.test.ts index f8e98543b..b8eb775c3 100644 --- a/src/core/project/templates/boxProfile.test.ts +++ b/src/core/project/templates/boxProfile.test.ts @@ -660,6 +660,41 @@ describe("renderAgentCorePack", () => { ); }); + test.each([ + ["0.3.2", true, true], + ["1.0.0", true, true], + ["0.3.1", true, false], + ["0.3.0", true, false], + ["0.3.2", false, false], + ])( + "reads the log group and stream of the runtime's spans from OTEL_EXPORTER_OTLP_TRACES_HEADERS only with OTEL on and a box that takes them (box %s, OTEL %p)", + (version, otel, declared) => { + const { runtime } = parse( + renderAgentCorePack({ name: "V3Demo" } as unknown as ProjectSpec, { + name: "agent", + box: { version }, + ...(!otel && { instrumentation: { enableOtel: false } }), + }), + ).inputs as Record; + + if (!declared) { + expect(runtime).not.toHaveProperty("traces_log_group"); + expect(runtime).not.toHaveProperty("traces_log_stream"); + return; + } + expect(runtime.traces_log_group).toEqual({ + description: "The runtime's own CloudWatch Logs log group, for its spans.", + from: "env:OTEL_EXPORTER_OTLP_TRACES_HEADERS", + key: "x-aws-log-group", + }); + expect(runtime.traces_log_stream).toEqual({ + description: "The log stream of the runtime's spans.", + from: "env:OTEL_EXPORTER_OTLP_TRACES_HEADERS", + key: "x-aws-log-stream", + }); + }, + ); + test("turns OTEL on when the runtime has no instrumentation, as create writes it", () => { const text = renderAgentCorePack({ name: "V3Demo" } as unknown as ProjectSpec, { name: "agent", diff --git a/src/core/project/templates/boxProfile.ts b/src/core/project/templates/boxProfile.ts index bb4c02fe1..2c5bd712d 100644 --- a/src/core/project/templates/boxProfile.ts +++ b/src/core/project/templates/boxProfile.ts @@ -1,4 +1,5 @@ import { join, posix } from "node:path"; +import semver from "semver"; import { callsBedrockModel } from "../../../handlers/project/shortcuts"; import type { ScaffoldRuntimeInput } from "../../../handlers/project/types"; import { credentialEnvVarName } from "../../../projectSchemas/credential"; @@ -49,6 +50,27 @@ const BOX_CODE_ENV = "BOX_CODE"; */ export const RUNTIME_LOGS_HEADERS_ENV = "OTEL_EXPORTER_OTLP_LOGS_HEADERS"; +/** + * The variable in which AgentCore Runtime names the runtime's own log group and the log stream of + * its spans, as `x-aws-log-group=,x-aws-log-stream=`, with other keys. + */ +export const RUNTIME_TRACES_HEADERS_ENV = "OTEL_EXPORTER_OTLP_TRACES_HEADERS"; + +/** + * The first box release whose `cloudwatch_traces` target takes `log_group` and `log_stream` + * (strands-agents/box#629). Set it to the release that ships that change. + */ +export const BOX_TRACES_LOG_GROUP_VERSION = "0.3.2"; + +/** + * Whether the box of `runtime` sends its spans to the log group that the Runtime names: its + * `box.version` is {@link BOX_TRACES_LOG_GROUP_VERSION} or later. + */ +export function tracesToRuntimeLogGroup(runtime: Pick): boolean { + const version = semver.valid(runtime.box?.version ?? ""); + return version !== null && semver.gte(version, BOX_TRACES_LOG_GROUP_VERSION); +} + /** The pack of box that each box profile imports. */ const BOX_BASE_PACK = "@box/base"; @@ -717,7 +739,10 @@ const RUNTIME_RESOURCE_ATTRIBUTES_ENV = "OTEL_RESOURCE_ATTRIBUTES"; * input carries the Runtime's region into the agent's environment. The `gateway.url` input fills each * egress backend `url`, bounded by a pattern so a value from the environment names only the host the * profile expects. The `runtime.log_group` and `runtime.log_stream` inputs each read one key of - * {@link RUNTIME_LOGS_HEADERS_ENV}, for the default telemetry target of the Runtime profile. One + * {@link RUNTIME_LOGS_HEADERS_ENV}, for the default telemetry target of the Runtime profile. With + * OTEL on and {@link tracesToRuntimeLogGroup}, the `runtime.traces_log_group` and + * `runtime.traces_log_stream` inputs each read one key of {@link RUNTIME_TRACES_HEADERS_ENV}, for + * the default X-Ray target. One * input per project memory carries its ID into the agent's environment. With OTEL on for `runtime`, * the agent gets {@link AGENT_OTEL_ENV} and the Runtime's resource attributes, so the console links * its spans to the runtime; the profile merges after the pack, so a value of the user wins. Each @@ -728,7 +753,7 @@ const RUNTIME_RESOURCE_ATTRIBUTES_ENV = "OTEL_RESOURCE_ATTRIBUTES"; */ export function renderAgentCorePack( spec: ProjectSpec, - runtime: Pick, + runtime: Pick, gateways: readonly BoxProjectGateway[] = [], ): string { const otel = otelOn(runtime); @@ -760,7 +785,23 @@ from = ${str(`env:${memoryEnvVarName(memory.name)}`)}`, [inputs.runtime.resource_attributes] description = "The OpenTelemetry resource attributes of the runtime." from = ${str(`env:${RUNTIME_RESOURCE_ATTRIBUTES_ENV}`)} -default = ${str(`service.name=${runtime.name}`)}` +default = ${str(`service.name=${runtime.name}`)}${ + tracesToRuntimeLogGroup(runtime) + ? ` + +# The runtime's own log group and the log stream of its spans, for the default X-Ray target. The +# Runtime names both in ${RUNTIME_TRACES_HEADERS_ENV}. +[inputs.runtime.traces_log_group] +description = "The runtime's own CloudWatch Logs log group, for its spans." +from = ${str(`env:${RUNTIME_TRACES_HEADERS_ENV}`)} +key = "x-aws-log-group" + +[inputs.runtime.traces_log_stream] +description = "The log stream of the runtime's spans." +from = ${str(`env:${RUNTIME_TRACES_HEADERS_ENV}`)} +key = "x-aws-log-stream"` + : "" + }` : ""; const gatewayInputs = gateways .map((gateway) => { From c118a51ad244fd53ab0793e101025decc456d2f3 Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Tue, 24 Nov 2026 13:44:32 -0800 Subject: [PATCH 2/5] docs(box): Say where the spans of a boxed agent go on AgentCore Runtime --- docs/box-local-testing.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index abefb6e66..061da269f 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -313,7 +313,10 @@ profile that names a pack `box/` fails: box refuses it and names the `@box **The default X-Ray target.** With `enableOtel` on and no `cloudwatch_traces` target, the Runtime profile gets `[telemetry.agentcore_traces]`: box sends the spans of the agent, and its own deny, - permit, and trace spans, to X-Ray in the region of the Runtime. Build gives the execution role + permit, and trace spans, to X-Ray in the region of the Runtime. With box 0.3.2 or later + (`BOX_TRACES_LOG_GROUP_VERSION`), the target also names the runtime's own log group and its + `spans` stream, which box reads from `OTEL_EXPORTER_OTLP_TRACES_HEADERS`, so the spans go there + and not to `aws/spans`. Build gives the execution role `xray:PutSpans` and `xray:PutTraceSegments` in `agentcore/box-telemetry-policy-.json`. To send the spans to a different target, add a `cloudwatch_traces` target to the local profile. To send no spans, set `instrumentation.enableOtel` to `false` in `agentcore.json`. When `enableOtel` @@ -535,8 +538,9 @@ the deny, permit, and trace records, and box signs it with the execution role. With `instrumentation.enableOtel` on, box also sends the spans of the agent and its own spans to X-Ray, through the target `[telemetry.agentcore_traces]`. Build gives the execution role the X-Ray -grant for it. "The default X-Ray target" in step 4 gives the rules. CloudWatch Transaction Search -keeps the spans in the log group `aws/spans`. +grant for it. "The default X-Ray target" in step 4 gives the rules. With box 0.3.2 or later, the +spans go to the stream `spans` of the runtime's own log group. With an older box, CloudWatch +Transaction Search keeps them in the log group `aws/spans`. The CLI and the L3 constructs make no log group for box. The role that the CDK makes for the runtime can write to the log group of the Runtime, thus build adds no grant for it. A runtime that names an From f14485359549e2be5a7a4e43de07725bac9c511f Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Wed, 25 Nov 2026 10:25:08 -0800 Subject: [PATCH 3/5] docs(box): Name the silent span loss and how to find it (review) --- docs/box-local-testing.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/box-local-testing.md b/docs/box-local-testing.md index 061da269f..470c26a4d 100644 --- a/docs/box-local-testing.md +++ b/docs/box-local-testing.md @@ -316,7 +316,14 @@ profile that names a pack `box/` fails: box refuses it and names the `@box permit, and trace spans, to X-Ray in the region of the Runtime. With box 0.3.2 or later (`BOX_TRACES_LOG_GROUP_VERSION`), the target also names the runtime's own log group and its `spans` stream, which box reads from `OTEL_EXPORTER_OTLP_TRACES_HEADERS`, so the spans go there - and not to `aws/spans`. Build gives the execution role + and not to `aws/spans`. The Runtime must set `OTEL_EXPORTER_OTLP_TRACES_HEADERS` with + `x-aws-log-group` and `x-aws-log-stream`, or box does not start. If the log group is incorrect, + or box cannot write to it, no span gets to a log group and box shows no error. The exporter + of box examines only the HTTP status. If no spans get to the `spans` stream, set + `STRANDS_BOX_TELEMETRY_DEBUG` to `"1"` in the `envVars` of the runtime. Then look for + `telemetry export failed` in the log group of the runtime. The spans get to the log group only + with the `logs:PutResourcePolicy` grant that the L3 runtime role has on + `/aws/bedrock-agentcore/runtimes/*`. The CLI adds no grant for it. Build gives the execution role `xray:PutSpans` and `xray:PutTraceSegments` in `agentcore/box-telemetry-policy-.json`. To send the spans to a different target, add a `cloudwatch_traces` target to the local profile. To send no spans, set `instrumentation.enableOtel` to `false` in `agentcore.json`. When `enableOtel` From fcb727768c051e90bf6e6d16415c900598afd08b Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Wed, 25 Nov 2026 10:33:04 -0800 Subject: [PATCH 4/5] fix(box): Gate the traces log group on the staged box, not box.version (review) --- src/core/project/boxNetworkSource.test.ts | 8 +++- .../runtimeProfile/rules/additions.test.ts | 48 +++++++++++++++---- .../project/runtimeProfile/rules/additions.ts | 18 +++---- src/core/project/runtimeProfile/types.ts | 2 + src/core/project/templates/boxProfile.test.ts | 11 +++-- src/core/project/templates/boxProfile.ts | 17 ++++--- 6 files changed, 67 insertions(+), 37 deletions(-) diff --git a/src/core/project/boxNetworkSource.test.ts b/src/core/project/boxNetworkSource.test.ts index 8ed27c208..85cc0e0f7 100644 --- a/src/core/project/boxNetworkSource.test.ts +++ b/src/core/project/boxNetworkSource.test.ts @@ -9,6 +9,8 @@ import type { BoxProfileResolver } from "./boxProfileResolver"; import { boxShowOf, FakeBoxProfileResolver } from "./boxProfileResolver-test-support"; import { telemetryPolicyWriter } from "./boxNetworkWriters"; import { boxTelemetryPolicyFile } from "./boxTelemetry"; +import { BOX_RELEASE } from "./backends/boxRelease"; +import { tracesToRuntimeLogGroup } from "./templates/boxProfile"; const directories: string[] = []; afterEach(async () => { @@ -476,8 +478,10 @@ describe("the default X-Ray target of a runtime with OTEL on", () => { region: "${{ inputs.aws.region }}", include: ["deny", "permit", "trace"], secret: { ref: "aws://" }, - log_group: "${{ inputs.runtime.traces_log_group }}", - log_stream: "${{ inputs.runtime.traces_log_stream }}", + ...(tracesToRuntimeLogGroup(BOX_RELEASE.version) && { + log_group: "${{ inputs.runtime.traces_log_group }}", + log_stream: "${{ inputs.runtime.traces_log_stream }}", + }), }); const [file] = policyOf(on!); expect(JSON.parse((file as { text: string }).text)).toEqual({ diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index 8c8dc36d9..b05af0af3 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -19,7 +19,9 @@ import { BOX_BEDROCK_ROUTE, renderAgentCorePack, renderBoxProfile, + tracesToRuntimeLogGroup, } from "../../templates/boxProfile"; +import { BOX_RELEASE } from "../../backends/boxRelease"; import { runtimeProfileContext } from "../context"; import { deriveRuntimeProfile } from "../derive"; import { serializeRuntimeProfile } from "../serialize"; @@ -110,11 +112,15 @@ function specOf(runtime: ProjectRuntime, memories: readonly string[] = []) { } as unknown as ProjectSpec; } +/** The box of {@link contextOf}: one that takes the log group of its spans. */ +const BOX_VERSION = "1.0.0"; + function contextOf( runtime: ProjectRuntime, options: { memories?: readonly string[]; telemetry?: readonly CloudWatchTelemetry[]; + boxVersion?: string; } = {}, ) { const spec = specOf(runtime, options.memories); @@ -123,6 +129,7 @@ function contextOf( runtime, profileFile: PROFILE_FILE, region: BUILD_REGION, + boxVersion: options.boxVersion ?? BOX_VERSION, gateway: { name: spec.agentCoreGateways[0]!.name, targets: spec.agentCoreGateways[0]!.targets, @@ -174,15 +181,20 @@ const DEFAULT_TARGET = { secret: { ref: "aws://" }, }; -/** - * The default X-Ray target of a Runtime profile with OTEL on, for a box that takes the log group - * and stream of a `cloudwatch_traces` target, as the box of {@link runtimeOf} does. - */ -const DEFAULT_TRACES = { +/** The default X-Ray target of a Runtime profile with OTEL on, for a box before 0.3.2. */ +const PLAIN_TRACES = { kind: "cloudwatch_traces", region: "${{ inputs.aws.region }}", include: ["deny", "permit", "trace"], secret: { ref: "aws://" }, +}; + +/** + * The default X-Ray target of a Runtime profile with OTEL on, for a box that takes the log group + * and stream of a `cloudwatch_traces` target, as {@link BOX_VERSION} does. + */ +const DEFAULT_TRACES = { + ...PLAIN_TRACES, log_group: "${{ inputs.runtime.traces_log_group }}", log_stream: "${{ inputs.runtime.traces_log_stream }}", }; @@ -1432,7 +1444,7 @@ describe("the additions rule", () => { expect(tableOf(apply({}, contextOf(runtimeOf(build))).table)).toEqual({ agentcore: DEFAULT_TARGET, }); - expect(runtimeTracesTarget(otelRuntime(build))).toEqual(DEFAULT_TRACES); + expect(runtimeTracesTarget(BOX_VERSION)).toEqual(DEFAULT_TRACES); }, ); @@ -1448,12 +1460,12 @@ describe("the additions rule", () => { ])( "sends the spans of the default X-Ray target to the runtime's own log group and stream only for a box that takes them (box %s)", (version, routed) => { - const runtime = { ...OTEL_CONTAINER, box: { version } } as ProjectRuntime; - const traces = tableOf(apply({}, contextOf(runtime)).table).agentcore_traces; + const context = contextOf(OTEL_CONTAINER, { boxVersion: version }); + const traces = tableOf(apply({}, context).table).agentcore_traces; const { log_group, log_stream, ...rest } = DEFAULT_TRACES; expect(traces).toEqual(routed ? DEFAULT_TRACES : rest); - expect(defaultTracesTelemetry(apply({}, contextOf(runtime)).table, BUILD_REGION)).toEqual([ + expect(defaultTracesTelemetry(apply({}, context).table, BUILD_REGION)).toEqual([ { name: "agentcore_traces", kind: "cloudwatch_traces", @@ -1469,6 +1481,22 @@ describe("the additions rule", () => { }, ); + test("takes the box that build stages, not box.version, for the default X-Ray target and its inputs", () => { + const [older, newer] = ["0.2.4", "1.0.0"].map((version) => { + const runtime = { ...OTEL_CONTAINER, box: { version } } as ProjectRuntime; + const context = { ...contextOf(runtime), boxVersion: undefined }; + return { + traces: tableOf(apply({}, context).table).agentcore_traces, + pack: renderAgentCorePack(context.spec, runtime), + }; + }); + + expect(newer).toEqual(older!); + expect(older!.traces).toEqual( + tracesToRuntimeLogGroup(BOX_RELEASE.version) ? DEFAULT_TRACES : PLAIN_TRACES, + ); + }); + test("adds the default X-Ray target beside a cloudwatch_logs target of the user", () => { const local = { telemetry: { logs: CLOUDWATCH.logs } }; @@ -1558,7 +1586,7 @@ describe("the additions rule", () => { test("names the default X-Ray target in the header, with the runtime's own log group when the box takes it", () => { const routed = contextOf(OTEL_CODEZIP); - const plain = contextOf({ ...OTEL_CODEZIP, box: { version: "0.3.0" } } as ProjectRuntime); + const plain = contextOf(OTEL_CODEZIP, { boxVersion: "0.3.0" }); expect(runtimeProfileHeader(routed, apply({}, routed).table).at(-1)).toBe( "[telemetry.agentcore_traces] is the default X-Ray target: the agent's spans and box's " + diff --git a/src/core/project/runtimeProfile/rules/additions.ts b/src/core/project/runtimeProfile/rules/additions.ts index 4ae4fa762..99532b06d 100644 --- a/src/core/project/runtimeProfile/rules/additions.ts +++ b/src/core/project/runtimeProfile/rules/additions.ts @@ -40,7 +40,6 @@ import { RUNTIME_TRACES_HEADERS_ENV, tracesToRuntimeLogGroup, } from "../../templates/boxProfile"; -import type { ProjectRuntime } from "../../../../projectSchemas/runtime"; import type { CloudWatchTelemetry } from "../../boxNetwork"; import { isTable, isVenvPython } from "../projectPaths"; import { FILE_TELEMETRY_KIND } from "./refusals"; @@ -116,14 +115,14 @@ export const DEFAULT_TRACES_TARGET = { } as const; /** - * {@link DEFAULT_TRACES_TARGET} for `runtime`: when its box takes them ({@link tracesToRuntimeLogGroup}), - * with the log group and log stream that the Runtime names for its spans, so the spans reach the - * runtime's own log group as the spans of a runtime without a box do. + * {@link DEFAULT_TRACES_TARGET}, with the log group and log stream that the Runtime names for its + * spans when box `boxVersion`, by default the staged box, takes them + * ({@link tracesToRuntimeLogGroup}). */ -export function runtimeTracesTarget(runtime: Pick): TomlTable { +export function runtimeTracesTarget(boxVersion?: string): TomlTable { return { ...structuredClone(DEFAULT_TRACES_TARGET), - ...(tracesToRuntimeLogGroup(runtime) && { + ...(tracesToRuntimeLogGroup(boxVersion) && { log_group: RUNTIME_INPUTS.tracesLogGroup, log_stream: RUNTIME_INPUTS.tracesLogStream, }), @@ -215,10 +214,7 @@ export function runtimeProfileHeader( * {@link runtimeTracesTarget}. */ function defaultTracesTargets(profile: Readonly): string[] { - const forms = [ - runtimeTracesTarget({}), - runtimeTracesTarget({ box: { version: BOX_TRACES_LOG_GROUP_VERSION } }), - ]; + const forms = [DEFAULT_TRACES_TARGET, runtimeTracesTarget(BOX_TRACES_LOG_GROUP_VERSION)]; return Object.entries(tableOf(profile.telemetry)) .filter(([, target]) => forms.some((form) => sameValue(target, form))) .map(([name]) => name); @@ -786,7 +782,7 @@ function runtimeTelemetry( const name = Object.hasOwn(result, "agentcore_traces") ? "agentcore_traces_default" : "agentcore_traces"; - result[name] = runtimeTracesTarget(context.runtime); + result[name] = runtimeTracesTarget(context.boxVersion); } return result; } diff --git a/src/core/project/runtimeProfile/types.ts b/src/core/project/runtimeProfile/types.ts index 9e82c3fe6..4ba15750a 100644 --- a/src/core/project/runtimeProfile/types.ts +++ b/src/core/project/runtimeProfile/types.ts @@ -76,6 +76,8 @@ export interface RuntimeProfileContext { readonly profileFile: string; /** The region build builds for: of the Box gateway, its `bedrock` target, and the Memory route. */ readonly region: string; + /** The version of the box that runs the profile; absent: the box that build stages. */ + readonly boxVersion?: string; /** The runtime's Box gateway: its name, and its targets with the ones build adds. */ readonly gateway: { readonly name: string; diff --git a/src/core/project/templates/boxProfile.test.ts b/src/core/project/templates/boxProfile.test.ts index b8eb775c3..6d07fe367 100644 --- a/src/core/project/templates/boxProfile.test.ts +++ b/src/core/project/templates/boxProfile.test.ts @@ -670,11 +670,12 @@ describe("renderAgentCorePack", () => { "reads the log group and stream of the runtime's spans from OTEL_EXPORTER_OTLP_TRACES_HEADERS only with OTEL on and a box that takes them (box %s, OTEL %p)", (version, otel, declared) => { const { runtime } = parse( - renderAgentCorePack({ name: "V3Demo" } as unknown as ProjectSpec, { - name: "agent", - box: { version }, - ...(!otel && { instrumentation: { enableOtel: false } }), - }), + renderAgentCorePack( + { name: "V3Demo" } as unknown as ProjectSpec, + { name: "agent", ...(!otel && { instrumentation: { enableOtel: false } }) }, + [], + version, + ), ).inputs as Record; if (!declared) { diff --git a/src/core/project/templates/boxProfile.ts b/src/core/project/templates/boxProfile.ts index 2c5bd712d..e3ec18c1e 100644 --- a/src/core/project/templates/boxProfile.ts +++ b/src/core/project/templates/boxProfile.ts @@ -8,6 +8,7 @@ import { isBoxProject, type ProjectSpec } from "../../../projectSchemas/project" import { DEFAULT_MODEL_IDS, type ProjectRuntime } from "../../../projectSchemas/runtime"; import { DEV_PORTS } from "../../dev/port"; import { PYTHON_ENV } from "../../dev/codezip"; +import { BOX_RELEASE } from "../backends/boxRelease"; import { modelProviderCredentialName } from "./runtime"; import { projectGatewayInputs, type BoxProjectGateway } from "../boxNetwork/projectGateways"; @@ -62,13 +63,10 @@ export const RUNTIME_TRACES_HEADERS_ENV = "OTEL_EXPORTER_OTLP_TRACES_HEADERS"; */ export const BOX_TRACES_LOG_GROUP_VERSION = "0.3.2"; -/** - * Whether the box of `runtime` sends its spans to the log group that the Runtime names: its - * `box.version` is {@link BOX_TRACES_LOG_GROUP_VERSION} or later. - */ -export function tracesToRuntimeLogGroup(runtime: Pick): boolean { - const version = semver.valid(runtime.box?.version ?? ""); - return version !== null && semver.gte(version, BOX_TRACES_LOG_GROUP_VERSION); +/** Whether box `version`, by default the staged box, takes the log group of its spans. */ +export function tracesToRuntimeLogGroup(version: string = BOX_RELEASE.version): boolean { + const valid = semver.valid(version); + return valid !== null && semver.gte(valid, BOX_TRACES_LOG_GROUP_VERSION); } /** The pack of box that each box profile imports. */ @@ -753,8 +751,9 @@ const RUNTIME_RESOURCE_ATTRIBUTES_ENV = "OTEL_RESOURCE_ATTRIBUTES"; */ export function renderAgentCorePack( spec: ProjectSpec, - runtime: Pick, + runtime: Pick, gateways: readonly BoxProjectGateway[] = [], + boxVersion?: string, ): string { const otel = otelOn(runtime); const memories = spec.memories ?? []; @@ -786,7 +785,7 @@ from = ${str(`env:${memoryEnvVarName(memory.name)}`)}`, description = "The OpenTelemetry resource attributes of the runtime." from = ${str(`env:${RUNTIME_RESOURCE_ATTRIBUTES_ENV}`)} default = ${str(`service.name=${runtime.name}`)}${ - tracesToRuntimeLogGroup(runtime) + tracesToRuntimeLogGroup(boxVersion) ? ` # The runtime's own log group and the log stream of its spans, for the default X-Ray target. The From 09242066fcac1c96a6ea72a4da7fc54a65708fb2 Mon Sep 17 00:00:00 2001 From: Sai Abhishek Date: Wed, 25 Nov 2026 10:37:04 -0800 Subject: [PATCH 5/5] test(box): Check that the pack declares each traces input the Runtime profile names (review) --- .../runtimeProfile/rules/additions.test.ts | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/core/project/runtimeProfile/rules/additions.test.ts b/src/core/project/runtimeProfile/rules/additions.test.ts index b05af0af3..5f7393403 100644 --- a/src/core/project/runtimeProfile/rules/additions.test.ts +++ b/src/core/project/runtimeProfile/rules/additions.test.ts @@ -17,6 +17,7 @@ import { BOX_BEDROCK_API_KEY_ROUTE, BOX_BEDROCK_PACK, BOX_BEDROCK_ROUTE, + BOX_TRACES_LOG_GROUP_VERSION, renderAgentCorePack, renderBoxProfile, tracesToRuntimeLogGroup, @@ -31,7 +32,6 @@ import { agentStartsUnderOtel, apply, DEFAULT_TELEMETRY_TARGET, - DEFAULT_TRACES_TARGET, defaultTracesTelemetry, MEMORY_ROUTE, runtimeProfileHeader, @@ -1462,22 +1462,16 @@ describe("the additions rule", () => { (version, routed) => { const context = contextOf(OTEL_CONTAINER, { boxVersion: version }); const traces = tableOf(apply({}, context).table).agentcore_traces; - const { log_group, log_stream, ...rest } = DEFAULT_TRACES; - expect(traces).toEqual(routed ? DEFAULT_TRACES : rest); + expect(traces).toEqual(routed ? DEFAULT_TRACES : PLAIN_TRACES); expect(defaultTracesTelemetry(apply({}, context).table, BUILD_REGION)).toEqual([ { name: "agentcore_traces", kind: "cloudwatch_traces", region: BUILD_REGION, - include: rest.include, + include: PLAIN_TRACES.include, }, ]); - expect({ ...DEFAULT_TRACES_TARGET } as TomlTable).toEqual(rest); - expect([log_group, log_stream]).toEqual([ - "${{ inputs.runtime.traces_log_group }}", - "${{ inputs.runtime.traces_log_stream }}", - ]); }, ); @@ -1497,6 +1491,26 @@ describe("the additions rule", () => { ); }); + test.each(BUILDS)( + "declares in @box/agentcore each traces input that the Runtime profile names, for the same box (%s)", + (build) => { + const runtime = otelRuntime(build); + const context = contextOf(runtime, { boxVersion: BOX_TRACES_LOG_GROUP_VERSION }); + const { profile } = deriveRuntimeProfile(createdProfile(runtime), context); + const named = [ + ...serializeRuntimeProfile(profile).matchAll( + /\$\{\{ inputs\.runtime\.(traces_\w+) \}\}/g, + ), + ].map(([, name]) => name); + const { inputs } = parse( + renderAgentCorePack(context.spec, runtime, [], BOX_TRACES_LOG_GROUP_VERSION), + ) as { inputs: { runtime: TomlTable } }; + + expect(named).not.toEqual([]); + expect(Object.keys(inputs.runtime)).toEqual(expect.arrayContaining(named)); + }, + ); + test("adds the default X-Ray target beside a cloudwatch_logs target of the user", () => { const local = { telemetry: { logs: CLOUDWATCH.logs } };