Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 88
feat(project): wire dev handler#1966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| # Environment variables for local development. | ||
| # `agentcore dev` loads this file into your agent's process. Values here | ||
| # override anything the CLI injects. This file is gitignored — keep secrets | ||
| # out of version control, but they are safe here. | ||
| # `agentcore project dev` loads this file into your agent's process. Values here | ||
| # override injected values except PORT, FASTMCP_PORT, and LOCAL_DEV, which the | ||
| # CLI owns. This file is gitignored — keep secrets out of version control. | ||
| # | ||
| # Example: | ||
| # MY_API_KEY=... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { createHash } from "node:crypto"; | ||
| import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; | ||
| import { mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join, resolve } from "node:path"; | ||
| import { parseEnv } from "node:util"; | ||
| import { InputValidationError, InvalidEnvironmentError } from "../../errors"; | ||
| import type { DevEvent, DevServerInput } from "../../handlers/project/dev/types"; | ||
| import { | ||
| @@ -17,6 +18,7 @@ import { ContainerDevRunner } from "./container"; | ||
| type ProcessCall = { | ||
| command: string[]; | ||
| options: StreamProcessOptions; | ||
| envFile?: { path: string; contents: string; mode: number }; | ||
| }; | ||
| type StreamBehavior = ( | ||
| @@ -61,11 +63,20 @@ function harness( | ||
| config: { | ||
| available?: (tool: string, probeArgs?: string[]) => Promise<boolean>; | ||
| stream?: StreamBehavior; | ||
| awsDirectory?: string; | ||
| processEnv?: NodeJS.ProcessEnv; | ||
| } = {}, | ||
| ) { | ||
| const calls: ProcessCall[] = []; | ||
| const fakeStreamProcess: ProcessStreamer = async function* (command, options) { | ||
| calls.push({ command, options }); | ||
| const call: ProcessCall = { command, options }; | ||
| calls.push(call); | ||
| const envFileFlag = command.indexOf("--env-file"); | ||
| if (envFileFlag >= 0) { | ||
| const path = command[envFileFlag + 1]!; | ||
| const [contents, metadata] = await Promise.all([readFile(path, "utf8"), stat(path)]); | ||
| call.envFile = { path, contents, mode: metadata.mode & 0o777 }; | ||
| } | ||
| if (config.stream) yield* config.stream(command, options); | ||
| }; | ||
| return { | ||
| @@ -77,6 +88,11 @@ function harness( | ||
| (async (tool) => { | ||
| return tool === "docker"; | ||
| }), | ||
| awsDirectory: config.awsDirectory ?? join(tmpdir(), "agentcore-container-no-aws"), | ||
| processEnv: config.processEnv ?? { | ||
| AWS_ACCESS_KEY_ID: "test-access-key", | ||
| AWS_SECRET_ACCESS_KEY: "test-secret-key", | ||
| }, | ||
| }), | ||
| }; | ||
| } | ||
| @@ -154,7 +170,10 @@ describe("ContainerDevRunner", () => { | ||
| ".", | ||
| ]); | ||
| expect(build.options.cwd).toBe(root); | ||
| expect(build.options.env).toBe(process.env); | ||
| expect(build.options.env).toEqual({ | ||
| AWS_ACCESS_KEY_ID: "test-access-key", | ||
| AWS_SECRET_ACCESS_KEY: "test-secret-key", | ||
| }); | ||
| expect(build.options.redactedCommand).toContain("AGENT_NAME=<redacted>"); | ||
| expect(build.options.redactedCommand).toContain("TARGET=<redacted>"); | ||
| expect(build.options.redactedCommand?.join(" ")).not.toContain("hello-world"); | ||
| @@ -188,18 +207,63 @@ describe("ContainerDevRunner", () => { | ||
| containerName(root), | ||
| "-p", | ||
| `127.0.0.1:3000:${containerPort}`, | ||
| "-e", | ||
| "API_KEY=super-secret", | ||
| "-e", | ||
| `PORT=${containerPort}`, | ||
| "-e", | ||
| "LOCAL_DEV=1", | ||
| ...(protocol === "MCP" ? ["-e", "FASTMCP_PORT=8000"] : []), | ||
| "--env-file", | ||
| run.envFile!.path, | ||
| imageTag(root), | ||
| ]); | ||
| expect(run.options.env).toBe(process.env); | ||
| expect(run.options.redactedCommand).toContain("API_KEY=<redacted>"); | ||
| expect(run.options.redactedCommand?.join(" ")).not.toContain("super-secret"); | ||
| expect(run.options.env).toEqual({ | ||
| AWS_ACCESS_KEY_ID: "test-access-key", | ||
| AWS_SECRET_ACCESS_KEY: "test-secret-key", | ||
| }); | ||
| expect(parseEnv(run.envFile!.contents)).toEqual({ | ||
| AWS_ACCESS_KEY_ID: "test-access-key", | ||
| AWS_SECRET_ACCESS_KEY: "test-secret-key", | ||
| API_KEY: "super-secret", | ||
| PORT: String(containerPort), | ||
| LOCAL_DEV: "1", | ||
| ...(protocol === "MCP" ? { FASTMCP_PORT: "8000" } : {}), | ||
| }); | ||
| if (process.platform !== "win32") expect(run.envFile!.mode).toBe(0o600); | ||
| await expect(readFile(run.envFile!.path, "utf8")).rejects.toThrow(); | ||
| expect(run.command.join(" ")).not.toContain("super-secret"); | ||
| expect(run.command.join(" ")).not.toContain("test-secret-key"); | ||
| }); | ||
| test("uses a shared AWS config and rejects missing credentials", async () => { | ||
| const projectRuntime = runtime(); | ||
| const root = await projectRoot(projectRuntime); | ||
| const awsDirectory = join(root, ".aws"); | ||
| await mkdir(awsDirectory); | ||
| await writeFile(join(awsDirectory, "config"), "[profile sandbox]\nregion=us-east-1\n"); | ||
| const { calls, runner } = harness({ | ||
| awsDirectory, | ||
| processEnv: { AWS_PROFILE: "sandbox", AWS_REGION: "us-east-1" }, | ||
| }); | ||
| await collect(runner.run(input(root, projectRuntime))); | ||
| const run = commandCall(calls, "run"); | ||
| expect(run.command).toContain(`${awsDirectory}:/aws-config:ro`); | ||
| expect(run.command).not.toContain("AWS_PROFILE"); | ||
| expect(run.command).not.toContain("AWS_CONFIG_FILE"); | ||
| expect(parseEnv(run.envFile!.contents)).toMatchObject({ | ||
| AWS_PROFILE: "sandbox", | ||
| AWS_REGION: "us-east-1", | ||
| AWS_CONFIG_FILE: "/aws-config/config", | ||
| AWS_SHARED_CREDENTIALS_FILE: "/aws-config/credentials", | ||
| }); | ||
| expect(run.command.join(" ")).not.toContain("sandbox"); | ||
| const missing = harness({ | ||
| awsDirectory: join(root, "missing-aws"), | ||
| processEnv: {}, | ||
| }); | ||
| const missingCredentials = collect(missing.runner.run(input(root, projectRuntime))); | ||
| await expect(missingCredentials).rejects.toBeInstanceOf(InvalidEnvironmentError); | ||
| await expect(missingCredentials).rejects.toThrow( | ||
| "Unable to resolve AWS credentials for the container", | ||
| ); | ||
| expect(missing.calls).toHaveLength(0); | ||
| }); | ||
| test("preserves an existing build context .dockerignore", async () => { | ||
| @@ -244,7 +308,7 @@ describe("ContainerDevRunner", () => { | ||
| ); | ||
| }); | ||
| test("keeps app variables out of the container CLI environment", async () => { | ||
| test("keeps app variables out of the container CLI control environment", async () => { | ||
| const projectRuntime = runtime(); | ||
| const root = await projectRoot(projectRuntime); | ||
| const { calls, runner } = harness(); | ||
| @@ -254,9 +318,10 @@ describe("ContainerDevRunner", () => { | ||
| await collect(runner.run(runInput)); | ||
| const run = commandCall(calls, "run"); | ||
| expect(run.command).toContain("DOCKER_HOST=tcp://application-value"); | ||
| expect(run.options.env).toBe(process.env); | ||
| expect(run.options.redactedCommand).toContain("DOCKER_HOST=<redacted>"); | ||
| expect(run.command).not.toContain("DOCKER_HOST"); | ||
| expect(run.command.join(" ")).not.toContain("tcp://application-value"); | ||
| expect(run.options.env?.DOCKER_HOST).toBeUndefined(); | ||
| expect(parseEnv(run.envFile!.contents).DOCKER_HOST).toBe("tcp://application-value"); | ||
| }); | ||
| test("selects the first tool that supports container builds", async () => { | ||
| @@ -409,6 +474,33 @@ describe("ContainerDevRunner", () => { | ||
| expect(calls.map(({ command }) => command[1])).toEqual(["rm"]); | ||
| }); | ||
| test("rejects build contexts outside the project root, including symlinks", async () => { | ||
| const root = await mkdtemp(join(tmpdir(), "agentcore-container-")); | ||
| const outside = await mkdtemp(join(tmpdir(), "agentcore-container-outside-")); | ||
| tempDirectories.push(root, outside); | ||
| await symlink(outside, join(root, "linked"), process.platform === "win32" ? "junction" : "dir"); | ||
| const probes: string[] = []; | ||
| for (const buildContextPath of ["..", "linked"]) { | ||
| const { calls, runner } = harness({ | ||
| available: async (tool) => { | ||
| probes.push(tool); | ||
| return true; | ||
| }, | ||
| }); | ||
| const escapedContext = collect(runner.run(input(root, runtime({ buildContextPath })))); | ||
| await expect(escapedContext).rejects.toBeInstanceOf(InputValidationError); | ||
| await expect(escapedContext).rejects.toThrow( | ||
| "container build context must be within the project root", | ||
tejaskash marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ); | ||
| expect(calls).toHaveLength(0); | ||
| } | ||
| expect(probes).toHaveLength(0); | ||
| await expect(readFile(join(outside, ".dockerignore"), "utf8")).rejects.toThrow(); | ||
| }); | ||
| test("rejects a build context that is not a directory", async () => { | ||
| const root = await mkdtemp(join(tmpdir(), "agentcore-container-")); | ||
| tempDirectories.push(root); | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.