Repository navigation
minor refactor sandbox #1008
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
thesujai
wants to merge
3
commits into
main
Choose a base branch
from
fix/daytona-on-error-callback-rename
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
minor refactor sandbox #1008
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@truefoundry/trueforge-core': patch | ||
| '@truefoundry/trueforge': patch | ||
| --- | ||
|
|
||
| Surface Daytona sandbox create failures through a host `onError` hook (authz still ignored for P1) instead of a provider-owned critical alert callback. |
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 |
|---|---|---|
|
|
@@ -81,8 +81,11 @@ export interface DaytonaSandboxProviderOptions { | |
| /** Defaults to 1 hour (same as the gateway's max agent execution time). */ | ||
| previewUrlExpirySeconds?: number; | ||
| logger: Logger; | ||
| /** Host alert hook (e.g. Sentry P1). */ | ||
| onCriticalAlert?: (error: Error) => void; | ||
| /** | ||
| * Host hook for fresh-create failures only (wired through getOrCreateSandbox). | ||
| * Other provider catch paths do not call this; the host decides what to alert on. | ||
| */ | ||
| onError?: (error: Error) => void; | ||
| } | ||
|
|
||
| export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnvironment> { | ||
|
|
@@ -99,7 +102,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| private readonly apiKey: string; | ||
| private readonly apiUrl: string; | ||
| private readonly logger: Logger; | ||
| private readonly onCriticalAlert: ((error: Error) => void) | undefined; | ||
| private readonly onError: ((error: Error) => void) | undefined; | ||
| private readonly daytona: Daytona; | ||
| private static readonly cachedSandboxes = new Map<string, { sandbox: Sandbox; defaultTimeoutMs: number }>(); | ||
| // De-dupes concurrent recovery attempts on the same sandbox to a single refreshData+start round-trip. | ||
|
|
@@ -117,7 +120,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| this.fileMaxBytesForDownload = options.fileMaxBytesForDownload; | ||
| this.natsBridgePort = options.natsBridgePort ?? DEFAULT_SANDBOX_NATS_WS_PORT; | ||
| this.previewUrlExpirySeconds = options.previewUrlExpirySeconds ?? DEFAULT_PREVIEW_URL_EXPIRY_SECONDS; | ||
| this.onCriticalAlert = options.onCriticalAlert; | ||
| this.onError = options.onError; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are many places in this file has If they should not be piped then onError should be optional arg of getOrCreateSandbox? |
||
| this.logger = options.logger.child({ module: 'DaytonaProvider' }); | ||
| } | ||
|
|
||
|
|
@@ -340,30 +343,39 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| throw new Error(SNAPSHOT_INACTIVE_USER_MESSAGE); | ||
| } | ||
|
|
||
| /** Fresh create: reject an inactive tip before calling Daytona create. */ | ||
| private async createFreshSandbox(environment?: DaytonaSandboxEnvironment): Promise<Sandbox> { | ||
| /** New create: reject an inactive tip before calling Daytona create. */ | ||
| private async createNewSandbox({ | ||
| environment, | ||
| onError, | ||
| }: { | ||
| environment?: DaytonaSandboxEnvironment; | ||
| onError?: (error: Error) => void; | ||
| }): Promise<Sandbox> { | ||
| const env = this.requireEnvironment(environment); | ||
| try { | ||
| await this.rejectIfSnapshotInactive(env.snapshot_ref); | ||
| return await this.daytona.create(this.buildCreateParams(env)); | ||
| } catch (error) { | ||
| if (error instanceof DaytonaError && (error.statusCode === 401 || error.statusCode === 403)) { | ||
| throw error; | ||
| } | ||
| const alertError = error instanceof Error ? error : new Error('Daytona sandbox create failed', { cause: error }); | ||
| const createError = error instanceof Error ? error : new Error('Daytona sandbox create failed', { cause: error }); | ||
| this.logger.error('Daytona sandbox create failed', { | ||
| snapshot_ref: env.snapshot_ref, | ||
| ...extractErrorLogFields(alertError), | ||
| ...extractErrorLogFields(createError), | ||
| }); | ||
| this.onCriticalAlert?.(alertError); | ||
| throw error; | ||
| onError?.(createError); | ||
| throw createError; | ||
| } | ||
| } | ||
|
|
||
| private async getOrCreateSandbox( | ||
| sandboxId?: string, | ||
| environment?: DaytonaSandboxEnvironment, | ||
| ): Promise<{ sandbox: Sandbox; defaultTimeoutMs: number }> { | ||
| private async getOrCreateSandbox({ | ||
| sandboxId, | ||
| environment, | ||
| onError, | ||
| }: { | ||
| sandboxId?: string; | ||
| environment?: DaytonaSandboxEnvironment; | ||
| /** Only used on the fresh-create path; restore/exec paths omit this. */ | ||
| onError?: (error: Error) => void; | ||
| }): Promise<{ sandbox: Sandbox; defaultTimeoutMs: number }> { | ||
| if (sandboxId) { | ||
| validateSandboxOwnedByTenant({ sandboxId, tenantName: this.tenantName }); | ||
| const cached = DaytonaSandboxProvider.cachedSandboxes.get(sandboxId); | ||
|
|
@@ -374,7 +386,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
|
|
||
| const sandbox = sandboxId | ||
| ? await this.restoreExistingSandbox(sandboxId) | ||
| : await this.createFreshSandbox(environment); | ||
| : await this.createNewSandbox({ environment, onError }); | ||
|
|
||
| const entry = { sandbox, defaultTimeoutMs: this.timeoutMs }; | ||
| DaytonaSandboxProvider.cachedSandboxes.set(sandbox.name, entry); | ||
|
|
@@ -476,7 +488,10 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
|
|
||
| async createSandbox(environment?: DaytonaSandboxEnvironment): Promise<{ sandboxId: string }> { | ||
| return context.with(suppressTracing(context.active()), async () => { | ||
| const { sandbox } = await this.getOrCreateSandbox(undefined, environment); | ||
| const { sandbox } = await this.getOrCreateSandbox({ | ||
| environment, | ||
| onError: this.onError, | ||
| }); | ||
| this.logger.debug(`Sandbox created: name=${sandbox.name}`); | ||
| return { sandboxId: sandbox.name }; | ||
| }); | ||
|
|
@@ -486,7 +501,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| return context.with(suppressTracing(context.active()), async (): Promise<ExecResult> => { | ||
| try { | ||
| return await this.executeWithSandboxRecovery(params.sandboxId, async () => { | ||
| const { sandbox, defaultTimeoutMs } = await this.getOrCreateSandbox(params.sandboxId); | ||
| const { sandbox, defaultTimeoutMs } = await this.getOrCreateSandbox({ sandboxId: params.sandboxId }); | ||
| const response = await sandbox.process.executeCommand( | ||
| params.command, | ||
| params.cwd, | ||
|
|
@@ -519,7 +534,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| return context.with(suppressTracing(context.active()), async () => { | ||
| try { | ||
| return await this.executeWithSandboxRecovery(params.sandboxId, async () => { | ||
| const { sandbox } = await this.getOrCreateSandbox(params.sandboxId); | ||
| const { sandbox } = await this.getOrCreateSandbox({ sandboxId: params.sandboxId }); | ||
|
|
||
| const info = await this.getFileInfo(sandbox, params.path); | ||
| if (info.isDir) { | ||
|
|
@@ -548,7 +563,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| return context.with(suppressTracing(context.active()), async () => { | ||
| try { | ||
| await this.executeWithSandboxRecovery(params.sandboxId, async () => { | ||
| const { sandbox } = await this.getOrCreateSandbox(params.sandboxId); | ||
| const { sandbox } = await this.getOrCreateSandbox({ sandboxId: params.sandboxId }); | ||
| await sandbox.fs.uploadFile(params.content, params.remotePath); | ||
| }); | ||
| } catch (e: unknown) { | ||
|
|
@@ -563,7 +578,7 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv | |
| return context.with(suppressTracing(context.active()), async () => { | ||
| try { | ||
| return await this.executeWithSandboxRecovery(params.sandboxId, async () => { | ||
| const { sandbox } = await this.getOrCreateSandbox(params.sandboxId); | ||
| const { sandbox } = await this.getOrCreateSandbox({ sandboxId: params.sandboxId }); | ||
| const signed = await sandbox.getSignedPreviewUrl(params.port, params.expiresInSeconds); | ||
| return signed.url; | ||
| }); | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.