-
Notifications
You must be signed in to change notification settings - Fork 18
feat: optional AWS Lambda MicroVM stateful sandbox backend #16
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
Draft
danny-avila
wants to merge
24
commits into
main
Choose a base branch
from
feat/sandbox-backend-seam
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.
Draft
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7619a39
refactor: extract SandboxBackend seam from worker sandbox dispatch
danny-avila 63aacf6
feat: derive runtime session ids and add Redis session registry (dark…
danny-avila 9f7ee1a
feat: Lambda MicroVM client wrapper, test fake, and op throttle
danny-avila 4d8d7e0
feat: Lambda MicroVM image target, lifecycle hooks, and stateless bac…
danny-avila ac4fdef
feat: persistent session workspace for stateful MicroVM sessions
danny-avila f80700e
feat: backend session orchestration (find-or-launch + warm-VM reuse)
danny-avila 60fdf3b
feat: session workspace checkpoint/restore for perceived statefulness…
danny-avila a38933a
feat: auto-checkpoint session workspaces to S3 with restore on relaunch
danny-avila a71fcfb
feat: hookless per-request session binding via X-Runtime-Session-Id h…
danny-avila b4d7856
docs: Lambda MicroVM stateful sessions runbook + Terraform + image he…
danny-avila 3c7e9d3
fix: address review findings (lock TTL, symlink chown, fence ordering…
danny-avila 02cbdf1
fix(docs): address review findings in Lambda MicroVM IaC + guide
danny-avila 8e2da55
fix(docs): region-unique bucket names + correct checkpoint-cred guidance
danny-avila daa43b0
fix: address Codex review of stateful session mechanics
danny-avila 59f6fae
fix: address Macroscope review of the Codex-fix commit
danny-avila e0dd912
fix: address Codex review round 2 of stateful session mechanics
danny-avila 06bbb17
fix: address Codex review round 3 of stateful session mechanics
danny-avila 78673a2
fix: address Codex review round 4 + Macroscope of stateful sessions
danny-avila 2f75eff
fix: address Codex review round 5 of stateful session mechanics
danny-avila 299a77b
fix: address Codex review round 6 of stateful session mechanics
danny-avila 11bc26c
fix: address Codex review round 7 of stateful session mechanics
danny-avila 61ba32e
docs(lambda-microvm): guard the build-log path against the wrong "fix"
danny-avila a53ff5f
fix: address Codex review round 8 of stateful session mechanics
danny-avila 7871287
fix: address Codex review round 9 of stateful session mechanics
danny-avila 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { afterEach, describe, expect, test } from 'bun:test'; | ||
| import { | ||
| LIFECYCLE_HOOK_BASE_PATH, | ||
| applyRunHook, | ||
| getMicrovmRunContext, | ||
| resetMicrovmRunContextForTests, | ||
| } from './lifecycle'; | ||
|
|
||
| afterEach(resetMicrovmRunContextForTests); | ||
|
|
||
| describe('MicroVM /run hook context', () => { | ||
| test('captures microvmId and runHookPayload from the platform body', () => { | ||
| const context = applyRunHook({ | ||
| microvmId: 'mvm-0123', | ||
| runHookPayload: '{"runtime_session_id":"rt_x"}', | ||
| }); | ||
| expect(context.microvmId).toBe('mvm-0123'); | ||
| expect(context.runHookPayload).toBe('{"runtime_session_id":"rt_x"}'); | ||
| expect(getMicrovmRunContext()).toBe(context); | ||
| }); | ||
|
|
||
| test('first run wins: a different microvmId does not overwrite the context', () => { | ||
| applyRunHook({ microvmId: 'mvm-first', runHookPayload: 'a' }); | ||
| applyRunHook({ microvmId: 'mvm-second', runHookPayload: 'b' }); | ||
| expect(getMicrovmRunContext()?.microvmId).toBe('mvm-first'); | ||
| expect(getMicrovmRunContext()?.runHookPayload).toBe('a'); | ||
| }); | ||
|
|
||
| test('retries with the same microvmId are idempotent', () => { | ||
| const first = applyRunHook({ microvmId: 'mvm-0123' }); | ||
| const second = applyRunHook({ microvmId: 'mvm-0123' }); | ||
| expect(second).toBe(first); | ||
| }); | ||
|
|
||
| test('tolerates malformed and empty bodies', () => { | ||
| expect(applyRunHook(undefined).microvmId).toBeUndefined(); | ||
| resetMicrovmRunContextForTests(); | ||
| expect(applyRunHook('not-an-object').microvmId).toBeUndefined(); | ||
| resetMicrovmRunContextForTests(); | ||
| expect(applyRunHook({ microvmId: 42, runHookPayload: {} }).microvmId).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('hook base path matches the AWS well-known prefix', () => { | ||
| expect(LIFECYCLE_HOOK_BASE_PATH).toBe('/aws/lambda-microvms/runtime/v1'); | ||
| }); | ||
| }); |
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,94 @@ | ||
| import express, { Router, type Request, type Response } from 'express'; | ||
| import { logger } from '../logger'; | ||
| import { bindSessionWorkspace, parseSessionBinding, unbindSessionWorkspace } from '../session-workspace'; | ||
|
|
||
| /** | ||
| * AWS Lambda MicroVM hook endpoints. The platform POSTs to | ||
| * `/aws/lambda-microvms/runtime/v1/<hook>` on the configured hook port: | ||
| * | ||
| * /ready, /validate image build hooks (200 = proceed, 503 = retry) | ||
| * /run after start; body {microvmId, runHookPayload}; | ||
| * external traffic only flows after this returns 200 | ||
| * /resume, /suspend, /terminate lifecycle transitions | ||
| * | ||
| * Phase 1-2: structured no-ops that log and 200. /suspend and /terminate | ||
| * are the Phase 3 checkpoint-flush attachment points; /run captures the | ||
| * per-VM payload for later runtime-session wiring. | ||
| */ | ||
|
|
||
| export const LIFECYCLE_HOOK_BASE_PATH = '/aws/lambda-microvms/runtime/v1'; | ||
|
|
||
| export interface MicrovmRunContext { | ||
| microvmId?: string; | ||
| runHookPayload?: string; | ||
| receivedAt: number; | ||
| } | ||
|
|
||
| let runContext: MicrovmRunContext | undefined; | ||
|
|
||
| export function getMicrovmRunContext(): MicrovmRunContext | undefined { | ||
| return runContext; | ||
| } | ||
|
|
||
| export function resetMicrovmRunContextForTests(): void { | ||
| runContext = undefined; | ||
| } | ||
|
|
||
| /** First /run wins; retries with the same microvmId are idempotent, a | ||
| * different id is logged and ignored. Always 200 — a non-200 would fail | ||
| * the platform's lifecycle operation. */ | ||
| export function applyRunHook(body: unknown): MicrovmRunContext { | ||
| const parsed = (typeof body === 'object' && body !== null ? body : {}) as { | ||
| microvmId?: unknown; | ||
| runHookPayload?: unknown; | ||
| }; | ||
| const microvmId = typeof parsed.microvmId === 'string' ? parsed.microvmId : undefined; | ||
| const runHookPayload = typeof parsed.runHookPayload === 'string' ? parsed.runHookPayload : undefined; | ||
|
|
||
| if (runContext == null) { | ||
| runContext = { microvmId, runHookPayload, receivedAt: Date.now() }; | ||
| const binding = parseSessionBinding(runHookPayload); | ||
| if (binding) { | ||
| bindSessionWorkspace(binding); | ||
| logger.info({ runtimeSessionId: binding.runtimeSessionId }, 'Bound persistent session workspace'); | ||
| } | ||
| } else if (runContext.microvmId != null && microvmId != null && runContext.microvmId !== microvmId) { | ||
| logger.warn( | ||
| { existing: runContext.microvmId, incoming: microvmId }, | ||
| 'Ignoring /run hook for a different microvmId', | ||
| ); | ||
| } | ||
| return runContext; | ||
| } | ||
|
|
||
| const lifecycleRouter = Router(); | ||
|
|
||
| function ackHook(hook: string) { | ||
| return (_req: Request, res: Response): Response => { | ||
| logger.info({ hook }, 'MicroVM lifecycle hook invoked'); | ||
| return res.status(200).json({ hook, status: 'ok' }); | ||
| }; | ||
| } | ||
|
|
||
| lifecycleRouter.post('/ready', ackHook('ready')); | ||
| lifecycleRouter.post('/validate', ackHook('validate')); | ||
|
|
||
| lifecycleRouter.post('/run', express.json({ limit: '32kb' }), (req: Request, res: Response) => { | ||
| const context = applyRunHook(req.body); | ||
| logger.info( | ||
| { hook: 'run', microvmId: context.microvmId, hasPayload: context.runHookPayload != null }, | ||
| 'MicroVM lifecycle hook invoked', | ||
| ); | ||
| return res.status(200).json({ hook: 'run', status: 'ok' }); | ||
| }); | ||
|
|
||
| lifecycleRouter.post('/resume', ackHook('resume')); | ||
| lifecycleRouter.post('/suspend', ackHook('suspend')); | ||
|
|
||
| lifecycleRouter.post('/terminate', (_req: Request, res: Response) => { | ||
| logger.info({ hook: 'terminate' }, 'MicroVM lifecycle hook invoked'); | ||
| void unbindSessionWorkspace().catch((err) => logger.error({ err }, 'Failed to unbind session workspace on terminate')); | ||
| return res.status(200).json({ hook: 'terminate', status: 'ok' }); | ||
| }); | ||
|
|
||
| export default lifecycleRouter; | ||
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
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.
🟡 Medium
api/lifecycle.ts:48applyRunHookpermanently locks in the very first/runbody, even when it is malformed or missing bothmicrovmIdandrunHookPayload. Line 48 stores{ microvmId: undefined, runHookPayload: undefined }inrunContext, so any later retry with the real values falls through theelse ifbranch (both ids areundefined, so they don't mismatch) and never re-initializes or binds the session workspace. A transient bad first payload thus leaves the VM with an emptyrunContextfor its whole lifetime. Consider gating theelse ifso retries with real values overwrite an uninitializedrunContext— e.g. only treat the existing context as locked when it has a non-nullmicrovmId.🚀 Reply "fix it for me" or copy this AI Prompt for your agent: