@evident-ai/runner-cdk 0.1.0 → 0.1.1-dev.d96ef8a
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -16
- package/dist/controller-lambda/handler.js +50523 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +8 -1
- package/dist/microvm/constants.d.ts +4 -0
- package/dist/microvm/constants.js +24 -0
- package/dist/microvm/construct.d.ts +73 -0
- package/dist/microvm/construct.js +242 -0
- package/dist/microvm/controller/doorbell.d.ts +73 -0
- package/dist/microvm/controller/doorbell.js +107 -0
- package/dist/microvm/controller/handle-doorbell.d.ts +27 -0
- package/dist/microvm/controller/handle-doorbell.js +480 -0
- package/dist/microvm/controller/microvm-client.d.ts +75 -0
- package/dist/microvm/controller/microvm-client.js +7 -0
- package/dist/microvm/controller/shape-catalogue.d.ts +64 -0
- package/dist/microvm/controller/shape-catalogue.js +108 -0
- package/dist/microvm/controller/throttle-retry.d.ts +11 -0
- package/dist/microvm/controller/throttle-retry.js +27 -0
- package/dist/microvm/shapes.d.ts +72 -0
- package/dist/microvm/shapes.js +93 -0
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export { EvidentScaleToZeroConstruct, type EvidentScaleToZeroConstructProps, } from './evident-scale-to-zero-construct';
|
|
2
2
|
export { EvidentWaker, type EvidentWakerProps } from './waker/construct';
|
|
3
|
+
export { EvidentMicrovmConstruct, type EvidentMicrovmConstructProps } from './microvm/construct';
|
|
4
|
+
export { MICROVM_SHAPES, validateShapes, type MicrovmShape } from './microvm/shapes';
|
|
5
|
+
export { MICROVM_MAX_RUN_SECONDS } from './microvm/constants';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EvidentWaker = exports.EvidentScaleToZeroConstruct = void 0;
|
|
3
|
+
exports.MICROVM_MAX_RUN_SECONDS = exports.validateShapes = exports.MICROVM_SHAPES = exports.EvidentMicrovmConstruct = exports.EvidentWaker = exports.EvidentScaleToZeroConstruct = void 0;
|
|
4
4
|
var evident_scale_to_zero_construct_1 = require("./evident-scale-to-zero-construct");
|
|
5
5
|
Object.defineProperty(exports, "EvidentScaleToZeroConstruct", { enumerable: true, get: function () { return evident_scale_to_zero_construct_1.EvidentScaleToZeroConstruct; } });
|
|
6
6
|
var construct_1 = require("./waker/construct");
|
|
7
7
|
Object.defineProperty(exports, "EvidentWaker", { enumerable: true, get: function () { return construct_1.EvidentWaker; } });
|
|
8
|
+
var construct_2 = require("./microvm/construct");
|
|
9
|
+
Object.defineProperty(exports, "EvidentMicrovmConstruct", { enumerable: true, get: function () { return construct_2.EvidentMicrovmConstruct; } });
|
|
10
|
+
var shapes_1 = require("./microvm/shapes");
|
|
11
|
+
Object.defineProperty(exports, "MICROVM_SHAPES", { enumerable: true, get: function () { return shapes_1.MICROVM_SHAPES; } });
|
|
12
|
+
Object.defineProperty(exports, "validateShapes", { enumerable: true, get: function () { return shapes_1.validateShapes; } });
|
|
13
|
+
var constants_1 = require("./microvm/constants");
|
|
14
|
+
Object.defineProperty(exports, "MICROVM_MAX_RUN_SECONDS", { enumerable: true, get: function () { return constants_1.MICROVM_MAX_RUN_SECONDS; } });
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Shared literals for the MicroVM controller. Every controller module imports
|
|
3
|
+
// from here rather than restating a value that must match across them.
|
|
4
|
+
//
|
|
5
|
+
// Split from `infrastructure/evident-microvm/src/constants.ts` (#1476): the
|
|
6
|
+
// hook/image values (`HOOKS_DIR`, `HOOKS_PORT`, `HOOK_TIMEOUT_SECONDS`) stay
|
|
7
|
+
// there because `image/hook-server.ts` — bundled into the Docker image by a
|
|
8
|
+
// different pipeline — imports them too and cannot take an npm dependency on
|
|
9
|
+
// this construct package.
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.MICROVM_MAX_RUN_SECONDS = exports.SUSPENDING_POLL_INTERVAL_MS = exports.SUSPENDING_POLL_ATTEMPTS = exports.RUN_HOOK_PAYLOAD_MAX_BYTES = void 0;
|
|
12
|
+
// AWS caps `runHookPayload` at 16 KiB; anything larger is rejected at its API,
|
|
13
|
+
// so the controller checks it before spending a call.
|
|
14
|
+
exports.RUN_HOOK_PAYLOAD_MAX_BYTES = 16384;
|
|
15
|
+
// Worst-case poll (attempts × interval ≈ 2 s) must fit the controller Lambda's 8 s
|
|
16
|
+
// timeout alongside the retry backoff.
|
|
17
|
+
exports.SUSPENDING_POLL_ATTEMPTS = 4;
|
|
18
|
+
exports.SUSPENDING_POLL_INTERVAL_MS = 500;
|
|
19
|
+
// AWS's non-adjustable ceiling on a single MicroVM run, passed as
|
|
20
|
+
// `RunMicrovmCommand`'s `maximumDurationInSeconds`. The version pruner reuses it
|
|
21
|
+
// as its post-deactivation staging delay: once a version has been INACTIVE for
|
|
22
|
+
// longer than any VM can possibly still be running, no VM started before the
|
|
23
|
+
// deactivation can outlive it. The two MUST move together.
|
|
24
|
+
exports.MICROVM_MAX_RUN_SECONDS = 28800;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Construct } from 'constructs';
|
|
2
|
+
import * as s3 from 'aws-cdk-lib/aws-s3';
|
|
3
|
+
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
|
|
4
|
+
import { type MicrovmShape } from './shapes';
|
|
5
|
+
export interface EvidentMicrovmConstructProps {
|
|
6
|
+
/**
|
|
7
|
+
* Build context for the image, already staged on disk. An argument rather
|
|
8
|
+
* than something this construct stages itself, so synthesising the stack
|
|
9
|
+
* stays a pure function of its inputs instead of depending on how the
|
|
10
|
+
* ambient checkout was cloned.
|
|
11
|
+
*/
|
|
12
|
+
readonly imageSource: string;
|
|
13
|
+
/**
|
|
14
|
+
* ARN of the Lambda-managed base MicroVM image, from
|
|
15
|
+
* `aws lambda list-managed-microvm-images`. A string, not a `CfnParameter`
|
|
16
|
+
* built here: the caller creates the parameter at ITS OWN stack scope
|
|
17
|
+
* (preserving its logical id) and passes its resolved value down — a
|
|
18
|
+
* construct must not mint parameters into a consumer's stack.
|
|
19
|
+
*/
|
|
20
|
+
readonly baseImageArn: string;
|
|
21
|
+
/**
|
|
22
|
+
* Version of the Lambda-managed base MicroVM image, from
|
|
23
|
+
* `aws lambda list-managed-microvm-images`. See `baseImageArn` for why this
|
|
24
|
+
* is a plain string.
|
|
25
|
+
*/
|
|
26
|
+
readonly baseImageVersion: string;
|
|
27
|
+
/**
|
|
28
|
+
* The shape catalogue to build one image per shape from (#723). Defaults to
|
|
29
|
+
* `MICROVM_SHAPES`, the committed catalogue. Overridable so a caller can
|
|
30
|
+
* synth a local multi-shape template without mutating that module
|
|
31
|
+
* constant.
|
|
32
|
+
*/
|
|
33
|
+
readonly shapes?: readonly MicrovmShape[];
|
|
34
|
+
/**
|
|
35
|
+
* The secret holding the shared HMAC key the controller verifies every
|
|
36
|
+
* doorbell against, under the field `DOORBELL_SECRET`. Any Secrets Manager
|
|
37
|
+
* secret, however the caller manages it — this construct has no opinion on
|
|
38
|
+
* SOPS, KMS, or any other production mechanism. Required (not defaulted
|
|
39
|
+
* here): a default would need a scope of its own, and a caller-owned
|
|
40
|
+
* default keeps its own logical id — the caller builds one exactly once,
|
|
41
|
+
* in its own scope, and passes the result in either case.
|
|
42
|
+
*/
|
|
43
|
+
readonly doorbellSecret: secretsmanager.ISecret;
|
|
44
|
+
/**
|
|
45
|
+
* TCP port the image's hook server listens on, baked into both the image
|
|
46
|
+
* (`HOOKS_PORT` env var) and the `MicrovmImage`'s `hooks.port` — a mismatch
|
|
47
|
+
* means AWS probes a port nothing binds. Caller-supplied because the hook
|
|
48
|
+
* server is part of `imageSource`, which this construct does not build.
|
|
49
|
+
*/
|
|
50
|
+
readonly hooksPort: number;
|
|
51
|
+
/**
|
|
52
|
+
* Seconds AWS waits for a Run/Resume/Suspend/Terminate hook to answer,
|
|
53
|
+
* before the image build itself times out (`IMAGE_HOOK_TIMEOUT_SECONDS`,
|
|
54
|
+
* fixed at 600 s — building the snapshot is a different budget from
|
|
55
|
+
* running a hook).
|
|
56
|
+
*/
|
|
57
|
+
readonly hookTimeoutSeconds: number;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The customer-account half of the per-session runner (#558): the MicroVM
|
|
61
|
+
* image and the roles it builds and runs as, plus the stateless controller
|
|
62
|
+
* Lambda that turns a signed doorbell into RunMicrovm / ResumeMicrovm /
|
|
63
|
+
* SuspendMicrovm.
|
|
64
|
+
*/
|
|
65
|
+
export declare class EvidentMicrovmConstruct extends Construct {
|
|
66
|
+
/** POST target for Evident's signed doorbell (HMAC-verified in the handler). */
|
|
67
|
+
readonly functionUrl: string;
|
|
68
|
+
/** The shapes the controller can launch, with the image ARN each RunMicrovm uses. */
|
|
69
|
+
readonly shapeCatalogueJson: string;
|
|
70
|
+
/** Bucket a runner restores model credentials from: seed `<state_prefix>/claude/credentials.json` here. */
|
|
71
|
+
readonly durableStateBucket: s3.Bucket;
|
|
72
|
+
constructor(scope: Construct, id: string, props: EvidentMicrovmConstructProps);
|
|
73
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.EvidentMicrovmConstruct = void 0;
|
|
37
|
+
const path = __importStar(require("path"));
|
|
38
|
+
const cdk = __importStar(require("aws-cdk-lib"));
|
|
39
|
+
const constructs_1 = require("constructs");
|
|
40
|
+
const lambda = __importStar(require("aws-cdk-lib/aws-lambda"));
|
|
41
|
+
const s3 = __importStar(require("aws-cdk-lib/aws-s3"));
|
|
42
|
+
const lambda_microvm_cdk_1 = require("@evident-ai/lambda-microvm-cdk");
|
|
43
|
+
const shapes_1 = require("./shapes");
|
|
44
|
+
// Image hooks build the snapshot, so they get minutes where the runtime hooks
|
|
45
|
+
// get AWS's 60 s.
|
|
46
|
+
const IMAGE_HOOK_TIMEOUT_SECONDS = 600;
|
|
47
|
+
// The field inside the doorbell secret, shared by this construct (which
|
|
48
|
+
// names it for the controller) and whatever produced `props.doorbellSecret`.
|
|
49
|
+
const DOORBELL_SECRET_KEY = 'DOORBELL_SECRET';
|
|
50
|
+
/**
|
|
51
|
+
* The customer-account half of the per-session runner (#558): the MicroVM
|
|
52
|
+
* image and the roles it builds and runs as, plus the stateless controller
|
|
53
|
+
* Lambda that turns a signed doorbell into RunMicrovm / ResumeMicrovm /
|
|
54
|
+
* SuspendMicrovm.
|
|
55
|
+
*/
|
|
56
|
+
class EvidentMicrovmConstruct extends constructs_1.Construct {
|
|
57
|
+
/** POST target for Evident's signed doorbell (HMAC-verified in the handler). */
|
|
58
|
+
functionUrl;
|
|
59
|
+
/** The shapes the controller can launch, with the image ARN each RunMicrovm uses. */
|
|
60
|
+
shapeCatalogueJson;
|
|
61
|
+
/** Bucket a runner restores model credentials from: seed `<state_prefix>/claude/credentials.json` here. */
|
|
62
|
+
durableStateBucket;
|
|
63
|
+
constructor(scope, id, props) {
|
|
64
|
+
super(scope, id);
|
|
65
|
+
// Synth-time failure, before any construct exists.
|
|
66
|
+
const shapes = (0, shapes_1.validateShapes)(props.shapes ?? shapes_1.MICROVM_SHAPES);
|
|
67
|
+
const [defaultShape, ...additionalShapes] = shapes;
|
|
68
|
+
// The model credentials a runner restores at /run, and the litestream
|
|
69
|
+
// session replica (it is `LITESTREAM_BUCKET` below), share one per-runner
|
|
70
|
+
// prefix chosen at /run — synth can't name it, and lifecycle filters take
|
|
71
|
+
// a literal prefix with no wildcards, so any rule here is bucket-wide.
|
|
72
|
+
// RETAIN: deleting the stack must not cost hand-bootstrapped logins.
|
|
73
|
+
// Versioned, plus the 7-day noncurrent-version expiry below: a clobbered
|
|
74
|
+
// credential is fixed by the user re-authenticating their provider, not a
|
|
75
|
+
// version restore, so the versions carry no credential-recovery duty —
|
|
76
|
+
// the window instead covers the operator accident-net for a manually
|
|
77
|
+
// deleted corrupt replica (`--on-unusable-replica=leave` never self-heals
|
|
78
|
+
// one; see README). No current-object expiration, deliberately — that
|
|
79
|
+
// would destroy both outright. Count-based retention
|
|
80
|
+
// (`noncurrentVersionsToRetain`) would reap nothing here instead: it's
|
|
81
|
+
// ANDed with the age condition and counts newer noncurrent versions of
|
|
82
|
+
// the SAME key, but litestream's LTX keys are txid-unique and churn by
|
|
83
|
+
// DELETE, so a churned key has exactly one noncurrent version and zero
|
|
84
|
+
// newer ones.
|
|
85
|
+
const durableState = new s3.Bucket(this, 'DurableState', {
|
|
86
|
+
encryption: s3.BucketEncryption.S3_MANAGED,
|
|
87
|
+
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
|
|
88
|
+
versioned: true,
|
|
89
|
+
enforceSSL: true,
|
|
90
|
+
removalPolicy: cdk.RemovalPolicy.RETAIN,
|
|
91
|
+
lifecycleRules: [
|
|
92
|
+
{
|
|
93
|
+
// Litestream uploads multipart; an abandoned upload is billed and is
|
|
94
|
+
// invisible to ListObjectsV2, so nothing else would ever reap it.
|
|
95
|
+
id: 'AbortIncompleteUploads',
|
|
96
|
+
abortIncompleteMultipartUploadAfter: cdk.Duration.days(7),
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
id: 'ExpireNoncurrentVersions',
|
|
100
|
+
noncurrentVersionExpiration: cdk.Duration.days(7),
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
// The environment shared by every shape's image...
|
|
105
|
+
const imageEnvironment = {
|
|
106
|
+
HOOKS_PORT: String(props.hooksPort),
|
|
107
|
+
// The bucket is identical for every VM from this image version, so it
|
|
108
|
+
// may be baked. The PREFIX inside it may NOT: it selects one runner's
|
|
109
|
+
// credential store, so baking it would point every VM at the same one.
|
|
110
|
+
// It arrives per-VM in the /run payload — `hook-scripts.test.ts` and
|
|
111
|
+
// this construct's test both hold that line.
|
|
112
|
+
LITESTREAM_BUCKET: durableState.bucketName,
|
|
113
|
+
// No AWS_REGION here: the service rejects it as a reserved key
|
|
114
|
+
// ("Environment variable key 'AWS_REGION' is reserved", 400), which is
|
|
115
|
+
// also why the synchroniser's S3 client does not need it set — the
|
|
116
|
+
// MicroVM runtime supplies the region itself.
|
|
117
|
+
// Read by `evident run` itself (not the hooks), the same 24h window ECS
|
|
118
|
+
// already bakes in for its own runners (evident-scale-to-zero-construct.ts:191).
|
|
119
|
+
// Safe to bake: identical for every VM from this image version.
|
|
120
|
+
EVIDENT_SESSION_CLEANUP_MAX_AGE: '24h',
|
|
121
|
+
};
|
|
122
|
+
// ...and the hooks configuration every shape's image is built with.
|
|
123
|
+
const imageHooks = {
|
|
124
|
+
port: props.hooksPort,
|
|
125
|
+
lifecycleTimeout: cdk.Duration.seconds(props.hookTimeoutSeconds),
|
|
126
|
+
buildTimeout: cdk.Duration.seconds(IMAGE_HOOK_TIMEOUT_SECONDS),
|
|
127
|
+
};
|
|
128
|
+
// Built first and with no `executionRole` prop, exactly as before this
|
|
129
|
+
// shape catalogue existed: same construct id (`Image`, frozen on the
|
|
130
|
+
// shape record — see shapes.ts), same `imageName`, same description. The
|
|
131
|
+
// construct therefore creates `Image/ExecutionRole` at its current path
|
|
132
|
+
// and current logical id — zero CloudFormation churn on the live,
|
|
133
|
+
// hand-deployed stack (D3/D9). Every additional shape below borrows this
|
|
134
|
+
// role rather than getting one of its own, which is why `validateShapes`
|
|
135
|
+
// requires the default shape to be `shapes[0]`: it must exist before any
|
|
136
|
+
// other shape can reference its execution role.
|
|
137
|
+
const defaultImage = new lambda_microvm_cdk_1.MicrovmImage(this, defaultShape.constructId, {
|
|
138
|
+
imageName: defaultShape.imageName,
|
|
139
|
+
description: defaultShape.description,
|
|
140
|
+
// A local directory, so the zip is uploaded by `cdk deploy` itself and the
|
|
141
|
+
// image ARN is a stack reference rather than a deploy-time input.
|
|
142
|
+
source: props.imageSource,
|
|
143
|
+
baseImageArn: props.baseImageArn,
|
|
144
|
+
baseImageVersion: props.baseImageVersion,
|
|
145
|
+
memoryMiB: defaultShape.memoryMiB,
|
|
146
|
+
environment: imageEnvironment,
|
|
147
|
+
hooks: imageHooks,
|
|
148
|
+
});
|
|
149
|
+
// WART, accepted deliberately: every additional shape's image is built
|
|
150
|
+
// with `defaultImage.executionRole`, so this role's construct path
|
|
151
|
+
// (`Image/ExecutionRole`) reads as if it belongs to just the default
|
|
152
|
+
// shape's image, when in fact it is shared by every shape. That is
|
|
153
|
+
// intentional — see D3 in the plan. Do NOT "fix" this with
|
|
154
|
+
// `overrideLogicalId`: renaming the logical id IS the CloudFormation
|
|
155
|
+
// churn this design exists to avoid (a delete-and-recreate of the role a
|
|
156
|
+
// running MicroVM has live session credentials from). The wart is one
|
|
157
|
+
// comment; the override would undo the point of D3.
|
|
158
|
+
const images = new Map([[defaultShape.name, defaultImage]]);
|
|
159
|
+
for (const shape of additionalShapes) {
|
|
160
|
+
images.set(shape.name, new lambda_microvm_cdk_1.MicrovmImage(this, shape.constructId, {
|
|
161
|
+
imageName: shape.imageName,
|
|
162
|
+
description: shape.description,
|
|
163
|
+
source: props.imageSource,
|
|
164
|
+
baseImageArn: props.baseImageArn,
|
|
165
|
+
baseImageVersion: props.baseImageVersion,
|
|
166
|
+
memoryMiB: shape.memoryMiB,
|
|
167
|
+
executionRole: defaultImage.executionRole,
|
|
168
|
+
environment: imageEnvironment,
|
|
169
|
+
hooks: imageHooks,
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
// Bucket-wide rather than per-prefix (as the ECS runner's task role is):
|
|
173
|
+
// the prefix arrives at /run, so it cannot be named at synth, and one
|
|
174
|
+
// execution role is shared by every VM of every shape (D3). So a VM can
|
|
175
|
+
// read another session's credential store — acceptable within a single
|
|
176
|
+
// customer account, and what Phase 4 (#263) has to tighten when this goes
|
|
177
|
+
// multi-tenant.
|
|
178
|
+
durableState.grantReadWrite(defaultImage.executionRole);
|
|
179
|
+
// The shape catalogue the controller resolves a doorbell's `shape` field
|
|
180
|
+
// against, and advertises over the describe channel (#723). One JSON
|
|
181
|
+
// array, built with `Stack.toJsonString` — not `JSON.stringify` — because
|
|
182
|
+
// each `image_arn` is an unresolved `Fn::GetAtt` token at synth time, and
|
|
183
|
+
// `toJsonString` is the API that resolves tokens embedded in a JSON
|
|
184
|
+
// string (it emits an `Fn::Join`). `Stack.of(this)`, not `this`, because
|
|
185
|
+
// this class is a `Construct`, not a `Stack` — `toJsonString` is Stack-only.
|
|
186
|
+
const shapeCatalogueJson = cdk.Stack.of(this).toJsonString(shapes.map((shape) => ({
|
|
187
|
+
name: shape.name,
|
|
188
|
+
title: shape.title,
|
|
189
|
+
description: shape.description,
|
|
190
|
+
memory_mib: shape.memoryMiB,
|
|
191
|
+
image_arn: images.get(shape.name).imageArn,
|
|
192
|
+
is_default: shape.isDefault,
|
|
193
|
+
})));
|
|
194
|
+
const controller = new lambda.Function(this, 'Controller', {
|
|
195
|
+
runtime: lambda.Runtime.NODEJS_22_X,
|
|
196
|
+
// Pre-bundled at PACKAGE build time (`pnpm --filter @evident-ai/runner-cdk
|
|
197
|
+
// build`, see scripts/build.ts), not at synth time — see the waker's
|
|
198
|
+
// `construct.ts` for why (`NodejsFunction` can't ship in a published
|
|
199
|
+
// construct: its entry file isn't in the tarball, and bundling would
|
|
200
|
+
// need esbuild on the consumer's synth machine). Resolved from the
|
|
201
|
+
// PACKAGE ROOT, two directories below `__dirname` here either compiled
|
|
202
|
+
// (`dist/microvm`) or via ts-node (`src/microvm`) — same as the waker.
|
|
203
|
+
code: lambda.Code.fromAsset(path.join(__dirname, '..', '..', 'dist', 'controller-lambda')),
|
|
204
|
+
handler: 'handler.handler',
|
|
205
|
+
// 128 MB is a slow cold start for a bundled SDK client on a path with a
|
|
206
|
+
// hard client deadline.
|
|
207
|
+
memorySize: 256,
|
|
208
|
+
// Deliberately BELOW Evident's WEBHOOK_DELIVERY_TIMEOUT_MS (10 s): the
|
|
209
|
+
// SUSPENDING poll can take ~2 s, and the 409 it produces is only useful if
|
|
210
|
+
// it reaches Evident before the client aborts. Do not "align" this to 10 s.
|
|
211
|
+
timeout: cdk.Duration.seconds(8),
|
|
212
|
+
environment: {
|
|
213
|
+
// The ARN and field name, never the value: the controller reads it from
|
|
214
|
+
// Secrets Manager at runtime, so the key is not exposed to anyone with
|
|
215
|
+
// lambda:GetFunctionConfiguration.
|
|
216
|
+
DOORBELL_SECRET_ARN: props.doorbellSecret.secretArn,
|
|
217
|
+
DOORBELL_SECRET_KEY: DOORBELL_SECRET_KEY,
|
|
218
|
+
MICROVM_SHAPES: shapeCatalogueJson,
|
|
219
|
+
// The role every shape's MicroVM shares (D3) — still one role, still
|
|
220
|
+
// this same expression, now documented as shared rather than singular.
|
|
221
|
+
MICROVM_EXECUTION_ROLE_ARN: defaultImage.executionRole.roleArn,
|
|
222
|
+
},
|
|
223
|
+
});
|
|
224
|
+
// The four control actions are granted on '*', the PassNetworkConnector
|
|
225
|
+
// resource is a partition/region wildcard, and PassRole names the
|
|
226
|
+
// execution role every shape now shares (D3) — so nothing in this grant
|
|
227
|
+
// is image-specific (V4), and one call on the default shape's image
|
|
228
|
+
// authorises the controller for every shape's MicroVM. N calls would
|
|
229
|
+
// emit N near-identical policy statements and break the "exactly one
|
|
230
|
+
// statement granting X" guarantees this construct's test encodes.
|
|
231
|
+
defaultImage.grantRun(controller);
|
|
232
|
+
props.doorbellSecret.grantRead(controller);
|
|
233
|
+
const functionUrl = controller.addFunctionUrl({
|
|
234
|
+
// HMAC verification of the doorbell body IS the auth, as for the ECS waker.
|
|
235
|
+
authType: lambda.FunctionUrlAuthType.NONE,
|
|
236
|
+
});
|
|
237
|
+
this.functionUrl = functionUrl.url;
|
|
238
|
+
this.shapeCatalogueJson = shapeCatalogueJson;
|
|
239
|
+
this.durableStateBucket = durableState;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
exports.EvidentMicrovmConstruct = EvidentMicrovmConstruct;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The doorbell Evident POSTs to the controller's Function URL. `run_payload` is
|
|
3
|
+
* OPAQUE: it carries the runner key and whatever else the MicroVM's `/run` hook
|
|
4
|
+
* needs, and the controller only ever re-serialises it into `runHookPayload`. It
|
|
5
|
+
* is never parsed, never logged and never echoed — that is what makes the
|
|
6
|
+
* controller credential-free.
|
|
7
|
+
*
|
|
8
|
+
* `occurred_at` is required because it is the per-event half of the `RunMicrovm`
|
|
9
|
+
* `clientToken` (see `clientTokenFor`): a token derived from `runner_id` alone
|
|
10
|
+
* would be stable for the runner's whole life, so a post-termination wake would
|
|
11
|
+
* replay the dead VM's cached response and start nothing.
|
|
12
|
+
*
|
|
13
|
+
* A third arm, `runner.shapes_requested`, is a describe request rather than an
|
|
14
|
+
* event: it has no runner and no event instant, so it is parsed BEFORE the
|
|
15
|
+
* `runner_id`/`occurred_at` guards below. Its staleness is zero by construction
|
|
16
|
+
* — the response renders from the same deploy-time environment variable
|
|
17
|
+
* CloudFormation writes when it builds the images, so there is no second source
|
|
18
|
+
* to drift from. Replay is deliberately undefended: it is a pure read with no
|
|
19
|
+
* side effect, and its response carries only deploy-time metadata (shape names,
|
|
20
|
+
* titles, descriptions, memory) that is already public to the operator, so a
|
|
21
|
+
* replayed request yields nothing to gain. It is not defenceless — no response
|
|
22
|
+
* is produced without a valid HMAC over the exact body. ⚠️ Any future
|
|
23
|
+
* STATE-CHANGING request type added to this channel needs real replay defence
|
|
24
|
+
* (a timestamp window or a nonce); this reasoning does not extend to one.
|
|
25
|
+
*/
|
|
26
|
+
declare const WAKE_EVENT_TYPES: readonly ["agent.message_queued", "agent.wake_requested"];
|
|
27
|
+
declare const SUSPEND_EVENT_TYPE = "runner.suspend_requested";
|
|
28
|
+
export declare const SHAPES_EVENT_TYPE = "runner.shapes_requested";
|
|
29
|
+
type WakeEventType = (typeof WAKE_EVENT_TYPES)[number];
|
|
30
|
+
type DoorbellFields = {
|
|
31
|
+
runnerId: string;
|
|
32
|
+
occurredAt: string;
|
|
33
|
+
microvmId?: string;
|
|
34
|
+
};
|
|
35
|
+
export type WakeDoorbell = DoorbellFields & {
|
|
36
|
+
type: WakeEventType;
|
|
37
|
+
runHookPayload: string;
|
|
38
|
+
/**
|
|
39
|
+
* The shape a doorbell asks for, by name. `undefined` means "the default
|
|
40
|
+
* shape" — the controller resolves it against the shape catalogue.
|
|
41
|
+
*/
|
|
42
|
+
shape?: string;
|
|
43
|
+
};
|
|
44
|
+
export type Doorbell = WakeDoorbell | (DoorbellFields & {
|
|
45
|
+
type: typeof SUSPEND_EVENT_TYPE;
|
|
46
|
+
}) | {
|
|
47
|
+
type: typeof SHAPES_EVENT_TYPE;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Rejection reasons. Every one is a fixed constant: it goes into both the
|
|
51
|
+
* response body (which Evident persists) and the log line, so it must never
|
|
52
|
+
* contain anything derived from the request.
|
|
53
|
+
*/
|
|
54
|
+
export declare const DOORBELL_REJECTION: {
|
|
55
|
+
readonly invalidJson: "invalid_json";
|
|
56
|
+
readonly missingRunnerId: "missing_runner_id";
|
|
57
|
+
readonly missingOccurredAt: "missing_occurred_at";
|
|
58
|
+
readonly unsupportedType: "unsupported_event_type";
|
|
59
|
+
readonly invalidMicrovmId: "invalid_microvm_id";
|
|
60
|
+
readonly invalidShape: "invalid_shape";
|
|
61
|
+
readonly missingRunPayload: "missing_run_payload";
|
|
62
|
+
readonly runPayloadTooLarge: "run_payload_too_large";
|
|
63
|
+
};
|
|
64
|
+
export type DoorbellRejection = (typeof DOORBELL_REJECTION)[keyof typeof DOORBELL_REJECTION];
|
|
65
|
+
export type ParsedDoorbell = {
|
|
66
|
+
ok: true;
|
|
67
|
+
doorbell: Doorbell;
|
|
68
|
+
} | {
|
|
69
|
+
ok: false;
|
|
70
|
+
reason: DoorbellRejection;
|
|
71
|
+
};
|
|
72
|
+
export declare function parseDoorbell(rawBody: string): ParsedDoorbell;
|
|
73
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DOORBELL_REJECTION = exports.SHAPES_EVENT_TYPE = void 0;
|
|
4
|
+
exports.parseDoorbell = parseDoorbell;
|
|
5
|
+
const constants_1 = require("../constants");
|
|
6
|
+
/**
|
|
7
|
+
* The doorbell Evident POSTs to the controller's Function URL. `run_payload` is
|
|
8
|
+
* OPAQUE: it carries the runner key and whatever else the MicroVM's `/run` hook
|
|
9
|
+
* needs, and the controller only ever re-serialises it into `runHookPayload`. It
|
|
10
|
+
* is never parsed, never logged and never echoed — that is what makes the
|
|
11
|
+
* controller credential-free.
|
|
12
|
+
*
|
|
13
|
+
* `occurred_at` is required because it is the per-event half of the `RunMicrovm`
|
|
14
|
+
* `clientToken` (see `clientTokenFor`): a token derived from `runner_id` alone
|
|
15
|
+
* would be stable for the runner's whole life, so a post-termination wake would
|
|
16
|
+
* replay the dead VM's cached response and start nothing.
|
|
17
|
+
*
|
|
18
|
+
* A third arm, `runner.shapes_requested`, is a describe request rather than an
|
|
19
|
+
* event: it has no runner and no event instant, so it is parsed BEFORE the
|
|
20
|
+
* `runner_id`/`occurred_at` guards below. Its staleness is zero by construction
|
|
21
|
+
* — the response renders from the same deploy-time environment variable
|
|
22
|
+
* CloudFormation writes when it builds the images, so there is no second source
|
|
23
|
+
* to drift from. Replay is deliberately undefended: it is a pure read with no
|
|
24
|
+
* side effect, and its response carries only deploy-time metadata (shape names,
|
|
25
|
+
* titles, descriptions, memory) that is already public to the operator, so a
|
|
26
|
+
* replayed request yields nothing to gain. It is not defenceless — no response
|
|
27
|
+
* is produced without a valid HMAC over the exact body. ⚠️ Any future
|
|
28
|
+
* STATE-CHANGING request type added to this channel needs real replay defence
|
|
29
|
+
* (a timestamp window or a nonce); this reasoning does not extend to one.
|
|
30
|
+
*/
|
|
31
|
+
const WAKE_EVENT_TYPES = ['agent.message_queued', 'agent.wake_requested'];
|
|
32
|
+
const SUSPEND_EVENT_TYPE = 'runner.suspend_requested';
|
|
33
|
+
exports.SHAPES_EVENT_TYPE = 'runner.shapes_requested';
|
|
34
|
+
/**
|
|
35
|
+
* Rejection reasons. Every one is a fixed constant: it goes into both the
|
|
36
|
+
* response body (which Evident persists) and the log line, so it must never
|
|
37
|
+
* contain anything derived from the request.
|
|
38
|
+
*/
|
|
39
|
+
exports.DOORBELL_REJECTION = {
|
|
40
|
+
invalidJson: 'invalid_json',
|
|
41
|
+
missingRunnerId: 'missing_runner_id',
|
|
42
|
+
missingOccurredAt: 'missing_occurred_at',
|
|
43
|
+
unsupportedType: 'unsupported_event_type',
|
|
44
|
+
invalidMicrovmId: 'invalid_microvm_id',
|
|
45
|
+
invalidShape: 'invalid_shape',
|
|
46
|
+
missingRunPayload: 'missing_run_payload',
|
|
47
|
+
runPayloadTooLarge: 'run_payload_too_large',
|
|
48
|
+
};
|
|
49
|
+
function nonBlankString(value) {
|
|
50
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
51
|
+
}
|
|
52
|
+
function isWakeType(value) {
|
|
53
|
+
return WAKE_EVENT_TYPES.includes(value);
|
|
54
|
+
}
|
|
55
|
+
function parseDoorbell(rawBody) {
|
|
56
|
+
let body;
|
|
57
|
+
try {
|
|
58
|
+
body = JSON.parse(rawBody);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.invalidJson };
|
|
62
|
+
}
|
|
63
|
+
if (typeof body !== 'object' || body === null || Array.isArray(body)) {
|
|
64
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.invalidJson };
|
|
65
|
+
}
|
|
66
|
+
const { type, runner_id: runnerId, occurred_at: occurredAt, microvm_id: microvmId, run_payload: runPayload, shape, } = body;
|
|
67
|
+
if (type !== SUSPEND_EVENT_TYPE && type !== exports.SHAPES_EVENT_TYPE && !isWakeType(type)) {
|
|
68
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.unsupportedType };
|
|
69
|
+
}
|
|
70
|
+
// A describe request has no runner and no event instant — resolve it here,
|
|
71
|
+
// before the runner_id/occurred_at guards below, or it could never satisfy them.
|
|
72
|
+
if (type === exports.SHAPES_EVENT_TYPE) {
|
|
73
|
+
return { ok: true, doorbell: { type } };
|
|
74
|
+
}
|
|
75
|
+
if (!nonBlankString(runnerId)) {
|
|
76
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.missingRunnerId };
|
|
77
|
+
}
|
|
78
|
+
if (!nonBlankString(occurredAt)) {
|
|
79
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.missingOccurredAt };
|
|
80
|
+
}
|
|
81
|
+
if (microvmId !== undefined && !nonBlankString(microvmId)) {
|
|
82
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.invalidMicrovmId };
|
|
83
|
+
}
|
|
84
|
+
const fields = { runnerId, occurredAt, microvmId };
|
|
85
|
+
if (type === SUSPEND_EVENT_TYPE) {
|
|
86
|
+
return { ok: true, doorbell: { ...fields, type } };
|
|
87
|
+
}
|
|
88
|
+
if (shape !== undefined && !nonBlankString(shape)) {
|
|
89
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.invalidShape };
|
|
90
|
+
}
|
|
91
|
+
if (runPayload === undefined || runPayload === null) {
|
|
92
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.missingRunPayload };
|
|
93
|
+
}
|
|
94
|
+
const runHookPayload = JSON.stringify(runPayload);
|
|
95
|
+
if (Buffer.byteLength(runHookPayload, 'utf8') > constants_1.RUN_HOOK_PAYLOAD_MAX_BYTES) {
|
|
96
|
+
return { ok: false, reason: exports.DOORBELL_REJECTION.runPayloadTooLarge };
|
|
97
|
+
}
|
|
98
|
+
// Omit `shape` entirely when absent, rather than setting it to `undefined`,
|
|
99
|
+
// so a body with no shape parses to exactly the same object as before this
|
|
100
|
+
// field existed.
|
|
101
|
+
return {
|
|
102
|
+
ok: true,
|
|
103
|
+
doorbell: shape === undefined
|
|
104
|
+
? { ...fields, type, runHookPayload }
|
|
105
|
+
: { ...fields, type, shape, runHookPayload },
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MicrovmClient } from './microvm-client';
|
|
2
|
+
import type { ShapeCatalogue } from './shape-catalogue';
|
|
3
|
+
import { type Timing } from './throttle-retry';
|
|
4
|
+
/**
|
|
5
|
+
* The controller's pure decision core: verify the HMAC over the raw body, parse
|
|
6
|
+
* the doorbell, then map the MicroVM's current state to exactly one lifecycle
|
|
7
|
+
* call. Everything with I/O is injected, so the whole tree is unit-testable.
|
|
8
|
+
*
|
|
9
|
+
* The response body NEVER contains anything derived from `run_payload` — Evident
|
|
10
|
+
* persists it as `response_body`.
|
|
11
|
+
*/
|
|
12
|
+
type ActionKind = 'none' | 'run' | 'resume' | 'suspend' | 'rejected' | 'describe';
|
|
13
|
+
export type DoorbellResult = {
|
|
14
|
+
statusCode: number;
|
|
15
|
+
body: string;
|
|
16
|
+
action: {
|
|
17
|
+
kind: ActionKind;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export declare function handleDoorbell({ rawBody, signatureHeader, doorbellSecret, shapes, microvm, sleep, random, }: {
|
|
21
|
+
rawBody: string;
|
|
22
|
+
signatureHeader: string | undefined;
|
|
23
|
+
doorbellSecret: string;
|
|
24
|
+
shapes: ShapeCatalogue;
|
|
25
|
+
microvm: MicrovmClient;
|
|
26
|
+
} & Timing): Promise<DoorbellResult>;
|
|
27
|
+
export {};
|