Skip to content

feat: gateway policy generate - #2171

Merged
tejaskash merged 5 commits into
refactorfrom
feat/gateway-policy-generate
Sep 2, 2026
Merged

feat: gateway policy generate #2171
tejaskash merged 5 commits into
refactorfrom
feat/gateway-policy-generate

Conversation

@tejaskash

@tejaskash tejaskash commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

What

  • Adds agentcore gateway policy generate --gateway-id <id|arn> --prompt <text> which generates a Cedar policy for a deployed Gateway from a natural-language prompt and prints only the Cedar to stdout, ready to pipe into project add policy --statement -.
  • --policy-engine-id defaults to the Gateway's attached engine, --name names the generation request, --json prints the structured result including the service findings.
  • New PolicyClient Core sub-client (core.policy) behind a consumer-owned CorePolicyClient interface.
  • Replaces the project add policy --generate approach from feat: generate Cedar policies from natural language in project add policy #2127 per review.

Testing

  • Golden tests in src/handlers/gateway/gateway.policy.test.tsx recorded against a deployed project (human output, --json by ARN with an explicit engine, no engine attached, nonexistent engine, untranslatable prompt). Waiter timeout and GENERATE_FAILED are faked at .send() in gateway.test.tsx alongside the flag validation rows.
  • Live in us-west-2: deployed a project with a Gateway and attached Policy Engine, generated by id and by ARN, added the Cedar with project add policy, redeployed, confirmed the policy ACTIVE, tore down.
Screen.Recording.2026-09-02.at.4.12.52.PM.mov

@github-actions github-actions Bot added the size/l PR size: L label Sep 2, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added agentcore-harness-reviewing AgentCore Harness review in progress claude-security-reviewing Claude Code /security-review in progress labels Sep 2, 2026
@agentcore-devx-automation

Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label Sep 2, 2026
@codecov-commenter

codecov-commenter commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.18%. Comparing base (89d302b) to head (ce49534).

Additional details and impacted files
@@             Coverage Diff              @@
##           refactor    #2171      +/-   ##
============================================
+ Coverage     97.16%   97.18%   +0.01%     
============================================
  Files           519      522       +3     
  Lines         35506    35679     +173     
============================================
+ Hits          34500    34673     +173     
  Misses         1006     1006              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@agentcore-devx-automation agentcore-devx-automation Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AgentCore Harness Review

Verdict: Looks good

I looked over the diff and pulled the branch locally. The change is well-scoped and reads cleanly:

  • PolicyClient.generatePolicy mocks only at the AWS SDK send() boundary, so the SDK-provided waiter (waitForPolicyGenerationCompleted) runs its real polling loop against fake responses. Tests exercise the happy path (both engine-inference paths, paginated assets, both cedar and policy definition members, findings mapping) plus the notable failure modes (no engine attached, no gateway ARN, no generation id, GENERATE_FAILED reasons, non-translatable prompt, empty assets, waiter timeout) with test.each. No excessive mocking.
  • Handler tests use the real createRootHandler + TestCoreClient/TestPolicyClient, and assert exact stdout/stderr for the human path, --json output, --json error rendering, and missing-flag rejection before the client is called.
  • Error taxonomy is sensible (InputValidationError for missing engine, MalformedServiceResponseError for missing service fields, NetworkingError for the waiter timeout with statusReasons surfaced on GENERATE_FAILED).
  • The sub-router intentionally omits renderTui/supportedTuiCommands so a bare agentcore gateway policy fails on the missing flag rather than dropping into the TUI, which matches the PR description and gives scripted callers a predictable failure.
  • Coupling of src/core/policy.tsx on src/handlers/gateway/policy/types is consistent with every other Core sub-client in this repo.
  • No missing telemetry: this repo emits cli.command_run at the root in src/index.ts and no handler subtree adds per-command instrumentation, so nothing new is expected here.

Minor observations, not blocking:

  • formatFindings in src/handlers/gateway/policy/format.ts numbers findings by the raw asset index (including untranslated fragments that are omitted from stdout), so a user cross-referencing policy 2 [INVALID] on stderr with the two Cedar statements on stdout can't always line them up. --json gives the exact per-policy findings, so this is only a plain-text ergonomics thing.
  • resourceIdFromArn in src/core/policy.tsx accepts anything starting with arn: and returns the trailing segment; a malformed ARN like arn:foo would round-trip to arn:foo. The service will still reject it, so no correctness issue.

Nothing here requires changes before merging.

@agentcore-devx-automation agentcore-devx-automation Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label Sep 2, 2026
@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026

@jariy17 jariy17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good just fix the tests.

Comment thread src/core/policy.test.ts Outdated
@@ -0,0 +1,184 @@
import { describe, expect, test } from "bun:test";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Golden Tests only please. If there is a test case that can't use Golden Tests like Throttling Error Handling, throw it into gateway.test.tsx.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The Core tests are now golden fixtures recorded against a deployed project in gateway.policy.test.tsx. The two waiter outcomes that cannot be recorded (timeout, GENERATE_FAILED) live in gateway.test.tsx with the control client faked at .send().

Comment thread src/core/policy.tsx

const DEFAULT_WAIT: PolicyGenerationWait = { maxWaitTime: 60, minDelay: 2, maxDelay: 5 };

function resourceIdFromArn(value: string): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: OOS but there must be a shared util function for this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Comment thread src/core/policy.tsx
private readonly wait: PolicyGenerationWait = DEFAULT_WAIT,
) {}

async *generatePolicy(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOS but a lot of our coreClient function's don't yield helpful messages like in here. We should work on this later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Comment thread src/core/policy.tsx Outdated

yield { type: "step", message: `Resolving gateway ${gatewayId}` };
const gateway = await control.send(new GetGatewayCommand({ gatewayIdentifier: gatewayId }));
if (!gateway.gatewayArn) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not possible for GetGateway to return no gatewayArn. Do you know why ur agent is being this overprotective. It's probably because aws-sdk-js make every response parameter optional.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed and removed, along with the same guard on policyGenerationId. Both now use a non-null assertion like the rest of Core. Added a CLAUDE.md guideline so this pattern does not come back.

@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label Sep 2, 2026
@agentcore-devx-automation

Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label Sep 2, 2026
@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026
@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label Sep 2, 2026
@agentcore-devx-automation

Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label Sep 2, 2026
@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026
@tejaskash tejaskash changed the title feat: gateway policy generate turns a prompt into Cedar feat: gateway policy generate Sep 2, 2026
@github-actions github-actions Bot added size/l PR size: L and removed size/l PR size: L labels Sep 2, 2026
control plane is faked at .send() while the real PolicyClient and waiter run.
**/
describe("gateway policy generate against a faked control plane", () => {
function coreWith(status: string, statusReasons?: string[]): Core {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow Up: Can't you use the settle() function in fixtures.tsx? Settle() will perform a wait while recording the Golden tests.

@jariy17 jariy17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a follow up on the tests.

@tejaskash
tejaskash merged commit 8af13ef into refactor Sep 2, 2026
34 of 35 checks passed
@tejaskash
tejaskash deleted the feat/gateway-policy-generate branch September 2, 2026 20:48
This was referenced Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/l PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants