generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 7
refactor: Make makeJsonAgent bundleable with @endo/bundle-source #750
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
Open
grypez
wants to merge
2
commits into
main
Choose a base branch
from
grypez/refactor-kernel-errors-libp2p
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
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
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
46 changes: 46 additions & 0 deletions
46
packages/kernel-errors/src/bundleable/SampleGenerationError.ts
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { ErrorCode } from '../error-codes.ts'; | ||
|
|
||
| /** | ||
| * Error options with optional stack trace. | ||
| */ | ||
| type ErrorOptionsWithStack = { | ||
| cause?: Error; | ||
| stack?: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Bundleable version of SampleGenerationError. | ||
| * This version does not include marshaling logic to avoid dependencies | ||
| * on @metamask/utils that prevent vat bundling. | ||
| * | ||
| * An error indicating that the LLM generated invalid response. | ||
| * This error should trigger resampling from the LLM. | ||
| */ | ||
| export class SampleGenerationError extends Error { | ||
| public readonly code: typeof ErrorCode.SampleGenerationError; | ||
|
|
||
| public readonly data: { sample: string }; | ||
|
|
||
| /** | ||
| * Creates a new SampleGenerationError. | ||
| * | ||
| * @param sample - The invalid sample text generated by the LLM. | ||
| * @param cause - The underlying error that caused sample generation to fail. | ||
| * @param options - Additional error options including stack. | ||
| * @param options.stack - The stack trace of the error. | ||
| */ | ||
| constructor(sample: string, cause: Error, options?: ErrorOptionsWithStack) { | ||
| super('LLM generated invalid response.', { cause }); | ||
|
|
||
| this.name = 'SampleGenerationError'; | ||
| this.code = ErrorCode.SampleGenerationError; | ||
| this.data = { sample }; | ||
|
|
||
| if (options?.stack) { | ||
| this.stack = options.stack; | ||
| } | ||
|
|
||
| harden(this); | ||
| } | ||
| } | ||
| harden(SampleGenerationError); |
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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * Bundleable versions of kernel-errors. | ||
| * | ||
| * These versions do not include marshaling logic to avoid dependencies | ||
| * on @metamask/utils that prevent vat bundling with @endo/bundle-source. | ||
| * | ||
| * Use these exports when bundling vat code: | ||
| * import { SampleGenerationError } from '@metamask/kernel-errors/bundleable'; | ||
| */ | ||
| export { SampleGenerationError } from './SampleGenerationError.ts'; | ||
| export { ErrorCode, ErrorSentinel } from '../error-codes.ts'; |
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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Enum defining all error codes for Ocap errors. | ||
| */ | ||
| export const ErrorCode = { | ||
| AbortError: 'ABORT_ERROR', | ||
| DuplicateEndowment: 'DUPLICATE_ENDOWMENT', | ||
| StreamReadError: 'STREAM_READ_ERROR', | ||
| VatAlreadyExists: 'VAT_ALREADY_EXISTS', | ||
| VatDeleted: 'VAT_DELETED', | ||
| VatNotFound: 'VAT_NOT_FOUND', | ||
| SubclusterNotFound: 'SUBCLUSTER_NOT_FOUND', | ||
| SampleGenerationError: 'SAMPLE_GENERATION_ERROR', | ||
| InternalError: 'INTERNAL_ERROR', | ||
| ResourceLimitError: 'RESOURCE_LIMIT_ERROR', | ||
| } as const; | ||
|
|
||
| export type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode]; | ||
|
|
||
| /** | ||
| * A sentinel value used to identify marshaled errors. | ||
| */ | ||
| export const ErrorSentinel = '@@MARSHALED_ERROR'; |
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
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * A simple counter which increments and returns when called. | ||
| * | ||
| * @param start - One less than the first returned number. | ||
| * @returns A counter. | ||
| */ | ||
| export const makeCounter = (start: number = 0) => { | ||
| let counter: number = start; | ||
| return () => { | ||
| counter += 1; | ||
| return counter; | ||
| }; | ||
| }; |
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
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class identity mismatch breaks retry logic across packages
High Severity
The import of
SampleGenerationErrorchanged from@metamask/kernel-errorsto@metamask/kernel-errors/bundleable. Since these export different class objects, theinstanceofcheck inwithRetrieson line 47 will fail for errors thrown by packages that still import from the main export (likekernel-agents-repl). This breaks retry functionality for invalid LLM responses when usingmakeReplAgent, as errors won't be recognized as retryable and will be thrown immediately instead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude recommend a fix that works for this PR
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude finished @grypez's task in 1m 6s —— View job
Recommended fix for class identity mismatch
I've analyzed the issue and here's my recommendation:
Problem Summary
The PR creates two separate
SampleGenerationErrorclasses:@metamask/kernel-errors): Full-featured version with marshaling logic, extendsBaseError, depends on@metamask/utils@metamask/kernel-errors/bundleable): Lightweight version without marshaling, extendsErrordirectly, no@metamask/utilsdependencyThe issue occurs because:
kernel-agents(used in vats) imports from/bundleable(line 1 ofattempt.ts:47)kernel-agents-replimports from the main export (line 1 ofsample-collector.ts:39)instanceofchecks fail because they're different class objectsRecommended Solution: Use Error Code Checking Instead of instanceof
Since you've already extracted
ErrorCodetoerror-codes.ts(which is already bundleable), the cleanest solution is to replaceinstanceofchecks with error code checks:In
packages/kernel-agents/src/attempt.ts:Why this works:
SampleGenerationErrorhave a.codeproperty with the same valueErrorCodecan be imported separately without pulling in unbundleable dependenciesAlternative (if you need stricter typing):
Create a type guard function in
error-codes.ts:Then use it in
attempt.ts:This approach gives you proper type narrowing and works with both versions of the class.