SupportedUnderstudyAction Enum validation for 'method' on act/observe#1613
Conversation
🦋 Changeset detectedLatest commit: 4aa2573 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
1 issue found across 2 files
Confidence score: 2/5
SupportedUnderstudyActionis passed toz.enuminpackages/core/lib/inference.ts, which will throw at runtime becausez.enumexpects a string literal tuple, making this a likely break in schema construction- Given the 7/10 severity and direct runtime failure risk, this change carries high merge risk until the enum handling is corrected
- Pay close attention to
packages/core/lib/inference.ts- fix the enum usage (e.g.,z.nativeEnumorObject.values(...)) to avoid runtime schema errors.
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/core/lib/inference.ts">
<violation number="1" location="packages/core/lib/inference.ts:258">
P1: `SupportedUnderstudyAction` is a TS enum, but `z.enum` expects a string literal tuple. This will fail schema construction at runtime; use `z.nativeEnum` (or pass `Object.values(...)`) for enums.</violation>
</file>
Architecture diagram
sequenceDiagram
participant User as User Code
participant Core as Stagehand Core
participant LLM as LLM Client
participant Zod as Zod Validator
User->>Core: act() / observe()
Note over Core: CHANGED: Schema definition now uses<br/>z.enum(SupportedUnderstudyAction)<br/>instead of z.string()
Core->>LLM: Generate completion (prompt + schema)
LLM-->>Core: JSON Response (proposed action)
Core->>Zod: Validate response against schema
alt Valid Method (e.g. "click", "type")
Zod-->>Core: Parsed Object
Core->>Core: Proceed to execution
Core-->>User: Success
else NEW: Invalid Method (e.g. "hover", "unknown")
Note over Zod: Validation fails immediately<br/>(Value not in Enum)
Zod--xCore: ZodError
Core--xUser: Throw Validation Error
end
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Greptile OverviewGreptile SummaryThis PR adds enum validation to the Key Changes:
How it Works: Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant LLM as LLM Client
participant Inference as act/observe inference
participant Zod as Zod Schema Validator
participant Handler as ActHandler/ObserveHandler
participant Understudy as performUnderstudyMethod
LLM->>Inference: Generate action with method field
Inference->>Zod: Validate against schema with enum
alt Invalid method (e.g., "invalidMethod")
Zod-->>Inference: ❌ Validation fails
Inference-->>LLM: Reject early - unsupported method
else Valid method (e.g., "click")
Zod-->>Inference: ✅ Validation passes
Inference->>Handler: Return validated action
Handler->>Understudy: performUnderstudyMethod(method, xpath, args)
Understudy->>Understudy: Check METHOD_HANDLER_MAP or switch
Understudy-->>Handler: Execute method handler
Handler-->>LLM: Action completed
end
|
packages/core/lib/inference.ts
Outdated
| ), | ||
| method: z | ||
| .string() | ||
| .enum(SupportedUnderstudyAction) |
There was a problem hiding this comment.
Should use z.nativeEnum(SupportedUnderstudyAction) instead of z.enum(SupportedUnderstudyAction). The z.enum() function expects a tuple of string literals like ["value1", "value2"], while z.nativeEnum() is designed for TypeScript enums.
| .enum(SupportedUnderstudyAction) | |
| .nativeEnum(SupportedUnderstudyAction) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/lib/inference.ts
Line: 258:258
Comment:
Should use `z.nativeEnum(SupportedUnderstudyAction)` instead of `z.enum(SupportedUnderstudyAction)`. The `z.enum()` function expects a tuple of string literals like `["value1", "value2"]`, while `z.nativeEnum()` is designed for TypeScript enums.
```suggestion
.nativeEnum(SupportedUnderstudyAction)
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Outdated for Zod 4. According to the documentation:
"This replaces the z.nativeEnum() API in Zod 3"
Docs:
In Zod 4, z.enum() now accepts TypeScript enums directly:
enum Fish {
Salmon = 0,
Tuna = 1
}
const FishEnum = z.enum(Fish); // ✅ Works in Zod 4
packages/core/lib/inference.ts
Outdated
| .describe("a description of the accessible element and its purpose"), | ||
| method: z | ||
| .string() | ||
| .enum(SupportedUnderstudyAction) |
There was a problem hiding this comment.
Same issue - should use z.nativeEnum(SupportedUnderstudyAction) instead of z.enum(SupportedUnderstudyAction).
| .enum(SupportedUnderstudyAction) | |
| .nativeEnum(SupportedUnderstudyAction) |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/core/lib/inference.ts
Line: 393:393
Comment:
Same issue - should use `z.nativeEnum(SupportedUnderstudyAction)` instead of `z.enum(SupportedUnderstudyAction)`.
```suggestion
.nativeEnum(SupportedUnderstudyAction)
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
Outdated for Zod 4. According to the documentation:
"This replaces the z.nativeEnum() API in Zod 3"
Docs:
In Zod 4, z.enum() now accepts TypeScript enums directly:
enum Fish {
Salmon = 0,
Tuna = 1
}
const FishEnum = z.enum(Fish); // ✅ Works in Zod 4
|
Addresses follow up on #1608 |
Add comprehensive tests for SupportedUnderstudyAction enum validation with both Zod v3 and v4. These tests verify that: 1. Object.values(SupportedUnderstudyAction) produces correct array for z.enum() 2. z.enum() with Object.values() works correctly in both Zod v3 and v4 3. z.enum() with raw TypeScript enum throws error on parse in Zod v3 4. z.enum() with raw TypeScript enum works in Zod v4 (but not v3 compatible) 5. All SupportedUnderstudyAction values are valid enum options This test demonstrates that the current implementation in PR browserbase#1613 would break for users running Zod v3, since z.enum() in v3 does NOT accept TypeScript enums directly. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
# why # what changed # test plan <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes Zod v3 enum handling for method selection in act() and observe() to prevent v3 runtime errors and keep validation consistent across Zod v3 and v4. - **Bug Fixes** - Use Object.values(SupportedUnderstudyAction) with z.enum() in inference.ts for act() and observe(). - Add tests verifying enum validation works in both Zod v3 and v4, including invalid method rejection. - Supports Linear STG-1212 by enforcing enum-based method selection in act/observe. <sup>Written for commit f827712. Summary will update on new commits. <a href="https://cubic.dev/pr/browserbase/stagehand/pull/1618">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. --> --------- Co-authored-by: Chromie Bot <chromie@browserbase.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
|
@miguelg719 I have started the AI code review. It will take a few minutes to complete. |
|
I can see the AI code review has been initiated for this PR. The changes look focused on adding proper enum validation for the The implementation appears to address an important validation gap where unsupported methods could slip through without proper validation. Using Feel free to ask any questions about the changes or share the AI review results when they're ready! |
There was a problem hiding this comment.
No issues found across 3 files
Confidence score: 5/5
- Automated review surfaced no issues in the provided summaries.
- No files require special attention.
Architecture diagram
sequenceDiagram
participant Client
participant Inference as Inference (act/observe)
participant Zod as Zod Validator
participant LLM as LLM Provider
Note over Client, LLM: Flow applies to both act() and observe()
Client->>Inference: Call act() or observe()
Note over Inference: CHANGED: Schema Definition
Inference->>Inference: Define 'method' using z.enum(Object.values(Enum))<br/>(Ensures Zod v3/v4 compatibility)
Inference->>LLM: Prompt with updated schema description
LLM-->>Inference: Return JSON response
Inference->>Zod: Validate response against schema
alt NEW: Invalid "method" (not in SupportedUnderstudyAction)
Note right of LLM: e.g. { method: "non_existent_action" }
Zod-->>Inference: Validation Error (Invalid Enum Value)
Inference-->>Client: Throw Error (Fail Fast)
else Valid "method"
Note right of LLM: e.g. { method: "click" }
Zod-->>Inference: Success (Parsed Action)
Inference->>Inference: Execute Action / Return Observation
Inference-->>Client: Result
end
#1623) … to Vitest - Migrate test file from packages/core/lib/v3/tests/ to packages/core/tests/ - Replace @playwright/test imports with vitest (describe, expect, it) - Update test.describe() -> describe() and test() -> it() - Adjust import path for SupportedUnderstudyAction enum # why # what changed # test plan <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Migrated the Zod enum compatibility tests from Playwright to Vitest and moved them to packages/core/tests. This consolidates on Vitest and keeps Zod v3/v4 enum compatibility coverage for SupportedUnderstudyAction. - **Refactors** - Replaced @playwright/test with vitest (describe, expect, it). - Updated test.describe/test() to describe/it. - Adjusted SupportedUnderstudyAction import to ../lib/v3/types/private/handlers. <sup>Written for commit 9f58557. Summary will update on new commits. <a href="https://cubic.dev/pr/browserbase/stagehand/pull/1623">Review in cubic</a></sup> <!-- End of auto-generated description by cubic. --> Co-authored-by: Chromie Bot <chromie@browserbase.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
why
actandobservedon't fail on validation if the LLM generates an action with a non-supported methodwhat changed
test plan
Summary by cubic
Enforces SupportedUnderstudyAction enum for the method field in act and observe so invalid actions are rejected during schema validation. Prevents unsupported methods from slipping through when the LLM generates actions and supports Linear STG-1212.
Bug Fixes
Refactors
Written for commit 4aa2573. Summary will update on new commits. Review in cubic