vlambda 0.3.0__tar.gz → 0.4.0__tar.gz

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.
Files changed (45) hide show
  1. {vlambda-0.3.0 → vlambda-0.4.0}/PKG-INFO +196 -3
  2. {vlambda-0.3.0 → vlambda-0.4.0}/README.md +195 -2
  3. {vlambda-0.3.0 → vlambda-0.4.0}/pyproject.toml +1 -1
  4. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/__init__.py +5 -0
  5. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/__main__.py +166 -17
  6. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/assets.py +5 -1
  7. vlambda-0.4.0/python/vlambda/async_support.py +46 -0
  8. vlambda-0.4.0/python/vlambda/broker.py +429 -0
  9. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/cache.py +28 -5
  10. vlambda-0.4.0/python/vlambda/capabilities.py +204 -0
  11. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/cli.py +125 -3
  12. vlambda-0.4.0/python/vlambda/cron.py +144 -0
  13. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/dbpool.py +5 -1
  14. vlambda-0.4.0/python/vlambda/egress_store.py +112 -0
  15. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/forkd.py +11 -0
  16. vlambda-0.4.0/python/vlambda/kv_store.py +313 -0
  17. vlambda-0.4.0/python/vlambda/lifecycle.py +70 -0
  18. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/metrics.py +5 -0
  19. vlambda-0.4.0/python/vlambda/openapi.py +213 -0
  20. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/pool.py +57 -11
  21. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/project.py +3 -1
  22. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/request.py +8 -2
  23. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/reset.py +7 -1
  24. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/runner.py +21 -6
  25. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/sandbox.py +3 -2
  26. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/sandbox_os.py +23 -4
  27. vlambda-0.4.0/python/vlambda/schedules.py +577 -0
  28. vlambda-0.4.0/python/vlambda/sqlite_compat.py +38 -0
  29. vlambda-0.4.0/python/vlambda/tasks.py +401 -0
  30. {vlambda-0.3.0 → vlambda-0.4.0}/src/vlambda_native.v +369 -38
  31. {vlambda-0.3.0 → vlambda-0.4.0}/tests/test_unit.py +423 -0
  32. {vlambda-0.3.0 → vlambda-0.4.0}/tests/test_vlambda.py +429 -17
  33. {vlambda-0.3.0 → vlambda-0.4.0}/vcraft.toml +1 -1
  34. vlambda-0.3.0/python/vlambda/tasks.py +0 -206
  35. {vlambda-0.3.0 → vlambda-0.4.0}/LICENSE +0 -0
  36. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/auth.py +0 -0
  37. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/env.py +0 -0
  38. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/packages.py +0 -0
  39. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/py.typed +0 -0
  40. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/response.py +0 -0
  41. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/shared_cache.py +0 -0
  42. {vlambda-0.3.0 → vlambda-0.4.0}/python/vlambda/transfer.py +0 -0
  43. {vlambda-0.3.0 → vlambda-0.4.0}/tests/conftest.py +0 -0
  44. {vlambda-0.3.0 → vlambda-0.4.0}/v.mod +0 -0
  45. {vlambda-0.3.0 → vlambda-0.4.0}/vcraft_build.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: vlambda
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: An ASGI-like server for Python lambdas, written in V.
5
5
  License: MIT
6
6
  Requires-Python: >=3.14
@@ -33,6 +33,10 @@ POST that code to `/vlambda` and it becomes `GET /f/<slug>` instantly.
33
33
  - **Auth** — JWT-based authentication with per-user ownership
34
34
  - **Web UI** — Dashboard for managing lambdas, code editor, metrics
35
35
  - **Async invocation** — Fire-and-forget with task tracking
36
+ - **KV store** — per-lambda typed key/value state with TTL and atomic increments
37
+ - **Controlled egress** — allow-listed HTTPS calls from the sandbox via `request.fetch`
38
+ - **Schedules** — persistent five-field cron jobs with timezones, run history and a single leader
39
+ - **OpenAPI** — owner-scoped spec, metadata editor, `/docs` viewer and CLI export
36
40
  - **Sandbox** — Landlock + seccomp in Docker, plus restricted Python builtins and blocked modules
37
41
  - **Metrics** — Prometheus-compatible + Chart.js dashboard
38
42
  - **Per-lambda method** — GET, POST, PUT, PATCH, DELETE
@@ -48,6 +52,13 @@ POST that code to `/vlambda` and it becomes `GET /f/<slug>` instantly.
48
52
  - **POC templates** — HTML + `fetch`, webhook, JSON API, static site
49
53
  - **Free-threaded builds** — `cp314t`/`cp315t` wheels
50
54
 
55
+ ## Roadmap
56
+
57
+ 0.4.0 is in planning: per-lambda KV, allow-listed egress, cron schedules,
58
+ OpenAPI, single-host HA and an optional gVisor runtime. See
59
+ [docs/ROADMAP-0.4.0.md](docs/ROADMAP-0.4.0.md) for architecture, API contracts,
60
+ phases and release criteria.
61
+
51
62
  ## Installation
52
63
 
53
64
  ```bash
@@ -137,9 +148,16 @@ See `docker/README.md` for details, limits and the reset command.
137
148
  | `--cert` | `VLAMBDA_CERT` | | TLS certificate path |
138
149
  | `--key` | `VLAMBDA_KEY` | | TLS key path |
139
150
  | | `VLAMBDA_REGISTRATION` | 1 | Allow public registration (`0` disables it) |
151
+ | | `VLAMBDA_SQLITE_JOURNAL` | auto | `wal` or `delete`; auto falls back to `delete` when CPython and system SQLite versions differ |
140
152
  | | `VLAMBDA_RESP_CACHE_TTL` | 0 | GET response cache TTL in seconds (0 disables it) |
141
153
  | | `VLAMBDA_CACHE_BACKEND` | memory | `memory`, `file` or `redis` for the optional response cache |
142
154
 
155
+ vlambda normally uses SQLite WAL. When CPython bundles a SQLite version
156
+ different from the system `libsqlite3` used by the V extension (for example on
157
+ Python 3.15), it automatically falls back to the rollback journal to avoid
158
+ cross-library WAL corruption. Set `VLAMBDA_SQLITE_JOURNAL=wal` or `delete` to
159
+ override it.
160
+
143
161
  The GET response cache is disabled by default because a handler can depend on
144
162
  headers, cookies, `request.env`, time or other request state. Enable it only for
145
163
  stateless, idempotent GET lambdas; handlers that read request context, log,
@@ -207,6 +225,19 @@ Project exports include text files. Binary files are listed in
207
225
  - `GET /vlambda/<slug>/packages` — List requirements
208
226
  - `POST /vlambda/<slug>/packages` — Install PyPI packages into the vendor dir
209
227
 
228
+ ### KV, egress and schedules
229
+ - `GET/PUT/DELETE /vlambda/<slug>/kv[/<key>]` — Read, write or delete KV entries
230
+ - `GET/PUT /vlambda/<slug>/egress` — Read or replace the egress allow-list
231
+ - `GET/POST /vlambda/<slug>/schedules` — List or create schedules
232
+ - `PUT/DELETE /vlambda/<slug>/schedules/<id>` — Update or delete a schedule
233
+ - `POST /vlambda/<slug>/schedules/<id>/run` — Run a schedule now
234
+ - `GET /vlambda/<slug>/schedules/<id>/runs` — Run history
235
+
236
+ ### Metadata and OpenAPI
237
+ - `GET/PUT /vlambda/<slug>/meta` — Summary, tags and description
238
+ - `GET /openapi.json` — Owner-scoped OpenAPI 3.1 document
239
+ - `GET /docs` — Bundled OpenAPI viewer
240
+
210
241
  ### Monitoring (requires auth)
211
242
  - `GET /health` — Health check
212
243
  - `GET /ready` — Readiness
@@ -268,6 +299,20 @@ and helpers `header()`, `json()`, `form()` and `log()`.
268
299
  `Response` supports `json()`, `text()`, `html()`, `redirect()`, `no_content()`,
269
300
  status codes, headers, cookies and binary bodies.
270
301
 
302
+ Handlers may also be coroutine functions. vlambda awaits the coroutine and
303
+ serializes its value exactly like a synchronous return:
304
+
305
+ ```python
306
+ async def double(value):
307
+ return value * 2
308
+
309
+ async def handler(request):
310
+ return {"values": [await double(n) for n in (1, 2, 3)]}
311
+ ```
312
+
313
+ Await local coroutines or helpers you define with `async def`. The `asyncio`
314
+ module stays blocked because it exposes sockets indirectly.
315
+
271
316
  Safe builtins: `abs`, `all`, `any`, `bool`, `dict`, `enumerate`, `filter`, `float`, `int`, `len`, `list`, `map`, `max`, `min`, `range`, `reversed`, `round`, `set`, `sorted`, `str`, `sum`, `tuple`, `zip`, `print`, etc.
272
317
 
273
318
  Blocked modules: `os`, `posix`, `subprocess`, `sys`, `importlib`, `ctypes`, `socket`, `_socket`, `urllib`, `http`, `shutil`, `pathlib`, `io`, `pickle`, `marshal`, `resource`, `fcntl`, `mmap`, `sqlite3`, etc.
@@ -280,8 +325,12 @@ With the Docker default (`VLAMBDA_SANDBOX=fork`), user code runs in a fresh
280
325
  runner process confined with Landlock and seccomp.
281
326
  Landlock limits the filesystem to `/opt`, `/usr`, `/lib`, `/etc`, the current
282
327
  project/vendor and a private temporary directory; `/data`, `/proc`, `/sys` and
283
- the rest of the server cannot be read or listed. seccomp blocks network, cross-process
284
- signals, `ptrace`, `mount`, namespace changes and similar syscalls. Resource
328
+ the rest of the server cannot be read or listed. seccomp blocks TCP/UDP and every
329
+ non-Unix socket, cross-process signals, `ptrace`, `mount`, namespace changes and
330
+ similar syscalls. Only `AF_UNIX` client sockets to the local capability broker
331
+ are allowed. Do not mount the Docker socket, cloud metadata proxies or other
332
+ privileged Unix sockets into the container: the sandbox needs Unix sockets for
333
+ the broker and cannot distinguish them from other local sockets. Resource
285
334
  limits are applied too, and each invocation gets a fresh process.
286
335
 
287
336
  The plain `python -m vlambda` command without `VLAMBDA_SANDBOX` uses the
@@ -312,6 +361,145 @@ lambda's runner. Code lambdas read them from `request.env`; projects also see
312
361
  them in `os.environ`. Use `VLAMBDA_ENCRYPTION_KEY` if you want to rotate
313
362
  `VLAMBDA_SECRET` without losing existing secrets.
314
363
 
364
+ ## KV store
365
+
366
+ Every lambda has a small persistent key/value store, isolated by lambda and
367
+ accessible through an allow-listed capability broker rather than a database
368
+ connection in the sandbox.
369
+
370
+ ```python
371
+ def handler(request):
372
+ hits = request.kv.incr("hits")
373
+ request.kv.set("last", {"path": request.path}, ttl=3600)
374
+ return {"hits": hits, "last": request.kv.get("last")}
375
+ ```
376
+
377
+ Projects use the same API with `from vlambda import kv`. Values can be strings,
378
+ numbers, booleans, null, JSON documents or bytes; `incr()` is atomic and `ttl`
379
+ is optional. Limits are 256 keys, 64 KiB per value and 1 MiB per lambda.
380
+
381
+ Management API (owner only, `X-Admin-Token` not needed):
382
+
383
+ - `GET /vlambda/<slug>/kv?prefix=` — list
384
+ - `GET /vlambda/<slug>/kv/<key>` — read
385
+ - `PUT /vlambda/<slug>/kv/<key>` — write `{"value": ..., "ttl": 3600}`
386
+ - `DELETE /vlambda/<slug>/kv/<key>` — delete one key
387
+ - `DELETE /vlambda/<slug>/kv` — clear the store
388
+
389
+ The UI exposes a KV card in the code and project editors.
390
+
391
+ ## Egress
392
+
393
+ Lambdas cannot open TCP sockets. They can call allow-listed hostnames through
394
+ the capability broker:
395
+
396
+ ```python
397
+ def handler(request):
398
+ response = request.fetch("https://api.example.com/items", timeout=5)
399
+ return {"status": response.status, "data": response.json()}
400
+ ```
401
+
402
+ Projects use `from vlambda import fetch`. Supported methods are `GET`, `HEAD`,
403
+ `POST`, `PUT`, `PATCH` and `DELETE`; pass `data=` for raw bodies or
404
+ `json_payload=` for JSON. The response exposes `status`, `headers`, `body`,
405
+ `text` and `json()`.
406
+
407
+ Configure the allow-list per lambda (`GET/PUT /vlambda/<slug>/egress`):
408
+
409
+ ```bash
410
+ python -m vlambda egress <slug> --set api.example.com --set "*.example.org"
411
+ ```
412
+
413
+ Matching is exact, or `*.example.org` for the apex and subdomains. HTTPS is
414
+ required by default; set `VLAMBDA_EGRESS_ALLOW_HTTP=1` for local development.
415
+ Private, loopback, link-local and reserved addresses are rejected; set
416
+ `VLAMBDA_EGRESS_ALLOW_PRIVATE=1` only for tests. Redirects are not followed.
417
+ Each invocation is limited to 20 calls, 1 MiB request bodies and 2 MiB
418
+ responses, with a 10 s maximum timeout. Denied hosts are logged without query
419
+ strings.
420
+
421
+ ## Schedules
422
+
423
+ Create persistent cron jobs per lambda:
424
+
425
+ ```bash
426
+ python -m vlambda schedule create <slug> --name nightly --cron "0 3 * * *" \
427
+ --method POST --path /schedule --payload '{"job": "cleanup"}'
428
+ python -m vlambda schedule list <slug>
429
+ python -m vlambda schedule run <slug> <id>
430
+ python -m vlambda schedule runs <slug> <id>
431
+ python -m vlambda schedule delete <slug> <id>
432
+ ```
433
+
434
+ Supported expressions use five fields (`minute hour day-of-month month weekday`),
435
+ with lists, ranges and steps, plus month/day names and IANA timezones. The
436
+ scheduler runs one leader per database lease; overlapping runs are skipped and
437
+ windows missed by more than `VLAMBDA_SCHEDULE_GRACE` seconds are recorded as
438
+ `missed`. Run history is retained for `VLAMBDA_SCHEDULE_RETENTION` days (7 by
439
+ default, max 1000 runs per schedule).
440
+
441
+ API:
442
+
443
+ - `GET/POST /vlambda/<slug>/schedules`
444
+ - `PUT/DELETE /vlambda/<slug>/schedules/<id>`
445
+ - `POST /vlambda/<slug>/schedules/<id>/run`
446
+ - `GET /vlambda/<slug>/schedules/<id>/runs`
447
+
448
+ The UI exposes a schedules card in the code and project editors.
449
+
450
+ ## gVisor
451
+
452
+ For a second isolation boundary, run the container with the gVisor runtime:
453
+
454
+ ```bash
455
+ docker run --runtime=runsc ... ghcr.io/rroblf01/vlambda:latest
456
+ # or merge the compose override:
457
+ docker compose -f docker/compose.yml -f docker/compose.gvisor.yml up -d
458
+ ```
459
+
460
+ Kubernetes uses `runtimeClassName: gvisor` on the pod. The internal fork +
461
+ Landlock + seccomp sandbox remains active. `scripts/gvisor-smoke.sh` builds the
462
+ image, runs the health check under `runsc` and skips cleanly when gVisor is not
463
+ installed. Firecracker/microVM-per-invocation is deferred to 0.5.0.
464
+
465
+ ## High availability (single host)
466
+
467
+ 0.4.0 supports multiple replicas on one host sharing a local `/data` volume.
468
+ SQLite WAL, a scheduler lease, task claiming and graceful drain keep scheduled
469
+ runs and tasks single-execution across replicas.
470
+
471
+ ```bash
472
+ docker compose -f docker/compose.ha.yml up -d --build --scale vlambda=2
473
+ ```
474
+
475
+ The sample puts nginx in front of two replicas. Do not place SQLite on NFS; use
476
+ a local volume on the same host. Multi-host HA needs an external database and is
477
+ deferred to 0.5.0.
478
+
479
+ Relevant settings:
480
+
481
+ - `VLAMBDA_INSTANCE_ID` — unique replica name (defaults to hostname:pid)
482
+ - `VLAMBDA_DRAIN_TIMEOUT` — graceful shutdown wait (seconds, default 20)
483
+ - `VLAMBDA_TASK_LEASE` — task claim lease (seconds, default 600)
484
+ - `VLAMBDA_SCHEDULE_GRACE` / `VLAMBDA_SCHEDULE_RETENTION` — cron behavior
485
+ - `/ready` returns `503` while a replica is draining
486
+
487
+ ## OpenAPI
488
+
489
+ Every lambda can carry `summary`, `tags` and `description` metadata, editable
490
+ from the code/project editor or `PUT /vlambda/<slug>/meta`. The generated spec is
491
+ available at `/openapi.json` (authenticated and owner-scoped by default; set
492
+ `VLAMBDA_OPENAPI_PUBLIC=1` for a public spec) and a lightweight viewer is served
493
+ at `/docs` without external assets.
494
+
495
+ ```bash
496
+ python -m vlambda openapi --out openapi.json
497
+ ```
498
+
499
+ The spec describes `/f/<slug>` for every owned lambda, its method, request body
500
+ for write methods, generic JSON/text/binary responses and the `X-Request-Id`
501
+ header. Project static files are described under `/f/<slug>/{path}`.
502
+
315
503
  ## CLI
316
504
 
317
505
  The CLI talks to a running server and uses `VLAMBDA_URL`/`VLAMBDA_TOKEN` or
@@ -322,6 +510,11 @@ python -m vlambda deploy main.py --name my-lambda --method GET
322
510
  python -m vlambda invoke <slug>
323
511
  python -m vlambda logs --slug <slug> --days 1
324
512
  python -m vlambda env <slug> --set PLAIN=hello --secret TOKEN=s3cret
513
+ python -m vlambda kv <slug> --set hits=0 --ttl 3600
514
+ python -m vlambda kv <slug> --get hits
515
+ python -m vlambda egress <slug> --set api.example.com
516
+ python -m vlambda schedule list <slug>
517
+ python -m vlambda openapi --out openapi.json
325
518
  python -m vlambda task <task-id>
326
519
  ```
327
520
 
@@ -22,6 +22,10 @@ POST that code to `/vlambda` and it becomes `GET /f/<slug>` instantly.
22
22
  - **Auth** — JWT-based authentication with per-user ownership
23
23
  - **Web UI** — Dashboard for managing lambdas, code editor, metrics
24
24
  - **Async invocation** — Fire-and-forget with task tracking
25
+ - **KV store** — per-lambda typed key/value state with TTL and atomic increments
26
+ - **Controlled egress** — allow-listed HTTPS calls from the sandbox via `request.fetch`
27
+ - **Schedules** — persistent five-field cron jobs with timezones, run history and a single leader
28
+ - **OpenAPI** — owner-scoped spec, metadata editor, `/docs` viewer and CLI export
25
29
  - **Sandbox** — Landlock + seccomp in Docker, plus restricted Python builtins and blocked modules
26
30
  - **Metrics** — Prometheus-compatible + Chart.js dashboard
27
31
  - **Per-lambda method** — GET, POST, PUT, PATCH, DELETE
@@ -37,6 +41,13 @@ POST that code to `/vlambda` and it becomes `GET /f/<slug>` instantly.
37
41
  - **POC templates** — HTML + `fetch`, webhook, JSON API, static site
38
42
  - **Free-threaded builds** — `cp314t`/`cp315t` wheels
39
43
 
44
+ ## Roadmap
45
+
46
+ 0.4.0 is in planning: per-lambda KV, allow-listed egress, cron schedules,
47
+ OpenAPI, single-host HA and an optional gVisor runtime. See
48
+ [docs/ROADMAP-0.4.0.md](docs/ROADMAP-0.4.0.md) for architecture, API contracts,
49
+ phases and release criteria.
50
+
40
51
  ## Installation
41
52
 
42
53
  ```bash
@@ -126,9 +137,16 @@ See `docker/README.md` for details, limits and the reset command.
126
137
  | `--cert` | `VLAMBDA_CERT` | | TLS certificate path |
127
138
  | `--key` | `VLAMBDA_KEY` | | TLS key path |
128
139
  | | `VLAMBDA_REGISTRATION` | 1 | Allow public registration (`0` disables it) |
140
+ | | `VLAMBDA_SQLITE_JOURNAL` | auto | `wal` or `delete`; auto falls back to `delete` when CPython and system SQLite versions differ |
129
141
  | | `VLAMBDA_RESP_CACHE_TTL` | 0 | GET response cache TTL in seconds (0 disables it) |
130
142
  | | `VLAMBDA_CACHE_BACKEND` | memory | `memory`, `file` or `redis` for the optional response cache |
131
143
 
144
+ vlambda normally uses SQLite WAL. When CPython bundles a SQLite version
145
+ different from the system `libsqlite3` used by the V extension (for example on
146
+ Python 3.15), it automatically falls back to the rollback journal to avoid
147
+ cross-library WAL corruption. Set `VLAMBDA_SQLITE_JOURNAL=wal` or `delete` to
148
+ override it.
149
+
132
150
  The GET response cache is disabled by default because a handler can depend on
133
151
  headers, cookies, `request.env`, time or other request state. Enable it only for
134
152
  stateless, idempotent GET lambdas; handlers that read request context, log,
@@ -196,6 +214,19 @@ Project exports include text files. Binary files are listed in
196
214
  - `GET /vlambda/<slug>/packages` — List requirements
197
215
  - `POST /vlambda/<slug>/packages` — Install PyPI packages into the vendor dir
198
216
 
217
+ ### KV, egress and schedules
218
+ - `GET/PUT/DELETE /vlambda/<slug>/kv[/<key>]` — Read, write or delete KV entries
219
+ - `GET/PUT /vlambda/<slug>/egress` — Read or replace the egress allow-list
220
+ - `GET/POST /vlambda/<slug>/schedules` — List or create schedules
221
+ - `PUT/DELETE /vlambda/<slug>/schedules/<id>` — Update or delete a schedule
222
+ - `POST /vlambda/<slug>/schedules/<id>/run` — Run a schedule now
223
+ - `GET /vlambda/<slug>/schedules/<id>/runs` — Run history
224
+
225
+ ### Metadata and OpenAPI
226
+ - `GET/PUT /vlambda/<slug>/meta` — Summary, tags and description
227
+ - `GET /openapi.json` — Owner-scoped OpenAPI 3.1 document
228
+ - `GET /docs` — Bundled OpenAPI viewer
229
+
199
230
  ### Monitoring (requires auth)
200
231
  - `GET /health` — Health check
201
232
  - `GET /ready` — Readiness
@@ -257,6 +288,20 @@ and helpers `header()`, `json()`, `form()` and `log()`.
257
288
  `Response` supports `json()`, `text()`, `html()`, `redirect()`, `no_content()`,
258
289
  status codes, headers, cookies and binary bodies.
259
290
 
291
+ Handlers may also be coroutine functions. vlambda awaits the coroutine and
292
+ serializes its value exactly like a synchronous return:
293
+
294
+ ```python
295
+ async def double(value):
296
+ return value * 2
297
+
298
+ async def handler(request):
299
+ return {"values": [await double(n) for n in (1, 2, 3)]}
300
+ ```
301
+
302
+ Await local coroutines or helpers you define with `async def`. The `asyncio`
303
+ module stays blocked because it exposes sockets indirectly.
304
+
260
305
  Safe builtins: `abs`, `all`, `any`, `bool`, `dict`, `enumerate`, `filter`, `float`, `int`, `len`, `list`, `map`, `max`, `min`, `range`, `reversed`, `round`, `set`, `sorted`, `str`, `sum`, `tuple`, `zip`, `print`, etc.
261
306
 
262
307
  Blocked modules: `os`, `posix`, `subprocess`, `sys`, `importlib`, `ctypes`, `socket`, `_socket`, `urllib`, `http`, `shutil`, `pathlib`, `io`, `pickle`, `marshal`, `resource`, `fcntl`, `mmap`, `sqlite3`, etc.
@@ -269,8 +314,12 @@ With the Docker default (`VLAMBDA_SANDBOX=fork`), user code runs in a fresh
269
314
  runner process confined with Landlock and seccomp.
270
315
  Landlock limits the filesystem to `/opt`, `/usr`, `/lib`, `/etc`, the current
271
316
  project/vendor and a private temporary directory; `/data`, `/proc`, `/sys` and
272
- the rest of the server cannot be read or listed. seccomp blocks network, cross-process
273
- signals, `ptrace`, `mount`, namespace changes and similar syscalls. Resource
317
+ the rest of the server cannot be read or listed. seccomp blocks TCP/UDP and every
318
+ non-Unix socket, cross-process signals, `ptrace`, `mount`, namespace changes and
319
+ similar syscalls. Only `AF_UNIX` client sockets to the local capability broker
320
+ are allowed. Do not mount the Docker socket, cloud metadata proxies or other
321
+ privileged Unix sockets into the container: the sandbox needs Unix sockets for
322
+ the broker and cannot distinguish them from other local sockets. Resource
274
323
  limits are applied too, and each invocation gets a fresh process.
275
324
 
276
325
  The plain `python -m vlambda` command without `VLAMBDA_SANDBOX` uses the
@@ -301,6 +350,145 @@ lambda's runner. Code lambdas read them from `request.env`; projects also see
301
350
  them in `os.environ`. Use `VLAMBDA_ENCRYPTION_KEY` if you want to rotate
302
351
  `VLAMBDA_SECRET` without losing existing secrets.
303
352
 
353
+ ## KV store
354
+
355
+ Every lambda has a small persistent key/value store, isolated by lambda and
356
+ accessible through an allow-listed capability broker rather than a database
357
+ connection in the sandbox.
358
+
359
+ ```python
360
+ def handler(request):
361
+ hits = request.kv.incr("hits")
362
+ request.kv.set("last", {"path": request.path}, ttl=3600)
363
+ return {"hits": hits, "last": request.kv.get("last")}
364
+ ```
365
+
366
+ Projects use the same API with `from vlambda import kv`. Values can be strings,
367
+ numbers, booleans, null, JSON documents or bytes; `incr()` is atomic and `ttl`
368
+ is optional. Limits are 256 keys, 64 KiB per value and 1 MiB per lambda.
369
+
370
+ Management API (owner only, `X-Admin-Token` not needed):
371
+
372
+ - `GET /vlambda/<slug>/kv?prefix=` — list
373
+ - `GET /vlambda/<slug>/kv/<key>` — read
374
+ - `PUT /vlambda/<slug>/kv/<key>` — write `{"value": ..., "ttl": 3600}`
375
+ - `DELETE /vlambda/<slug>/kv/<key>` — delete one key
376
+ - `DELETE /vlambda/<slug>/kv` — clear the store
377
+
378
+ The UI exposes a KV card in the code and project editors.
379
+
380
+ ## Egress
381
+
382
+ Lambdas cannot open TCP sockets. They can call allow-listed hostnames through
383
+ the capability broker:
384
+
385
+ ```python
386
+ def handler(request):
387
+ response = request.fetch("https://api.example.com/items", timeout=5)
388
+ return {"status": response.status, "data": response.json()}
389
+ ```
390
+
391
+ Projects use `from vlambda import fetch`. Supported methods are `GET`, `HEAD`,
392
+ `POST`, `PUT`, `PATCH` and `DELETE`; pass `data=` for raw bodies or
393
+ `json_payload=` for JSON. The response exposes `status`, `headers`, `body`,
394
+ `text` and `json()`.
395
+
396
+ Configure the allow-list per lambda (`GET/PUT /vlambda/<slug>/egress`):
397
+
398
+ ```bash
399
+ python -m vlambda egress <slug> --set api.example.com --set "*.example.org"
400
+ ```
401
+
402
+ Matching is exact, or `*.example.org` for the apex and subdomains. HTTPS is
403
+ required by default; set `VLAMBDA_EGRESS_ALLOW_HTTP=1` for local development.
404
+ Private, loopback, link-local and reserved addresses are rejected; set
405
+ `VLAMBDA_EGRESS_ALLOW_PRIVATE=1` only for tests. Redirects are not followed.
406
+ Each invocation is limited to 20 calls, 1 MiB request bodies and 2 MiB
407
+ responses, with a 10 s maximum timeout. Denied hosts are logged without query
408
+ strings.
409
+
410
+ ## Schedules
411
+
412
+ Create persistent cron jobs per lambda:
413
+
414
+ ```bash
415
+ python -m vlambda schedule create <slug> --name nightly --cron "0 3 * * *" \
416
+ --method POST --path /schedule --payload '{"job": "cleanup"}'
417
+ python -m vlambda schedule list <slug>
418
+ python -m vlambda schedule run <slug> <id>
419
+ python -m vlambda schedule runs <slug> <id>
420
+ python -m vlambda schedule delete <slug> <id>
421
+ ```
422
+
423
+ Supported expressions use five fields (`minute hour day-of-month month weekday`),
424
+ with lists, ranges and steps, plus month/day names and IANA timezones. The
425
+ scheduler runs one leader per database lease; overlapping runs are skipped and
426
+ windows missed by more than `VLAMBDA_SCHEDULE_GRACE` seconds are recorded as
427
+ `missed`. Run history is retained for `VLAMBDA_SCHEDULE_RETENTION` days (7 by
428
+ default, max 1000 runs per schedule).
429
+
430
+ API:
431
+
432
+ - `GET/POST /vlambda/<slug>/schedules`
433
+ - `PUT/DELETE /vlambda/<slug>/schedules/<id>`
434
+ - `POST /vlambda/<slug>/schedules/<id>/run`
435
+ - `GET /vlambda/<slug>/schedules/<id>/runs`
436
+
437
+ The UI exposes a schedules card in the code and project editors.
438
+
439
+ ## gVisor
440
+
441
+ For a second isolation boundary, run the container with the gVisor runtime:
442
+
443
+ ```bash
444
+ docker run --runtime=runsc ... ghcr.io/rroblf01/vlambda:latest
445
+ # or merge the compose override:
446
+ docker compose -f docker/compose.yml -f docker/compose.gvisor.yml up -d
447
+ ```
448
+
449
+ Kubernetes uses `runtimeClassName: gvisor` on the pod. The internal fork +
450
+ Landlock + seccomp sandbox remains active. `scripts/gvisor-smoke.sh` builds the
451
+ image, runs the health check under `runsc` and skips cleanly when gVisor is not
452
+ installed. Firecracker/microVM-per-invocation is deferred to 0.5.0.
453
+
454
+ ## High availability (single host)
455
+
456
+ 0.4.0 supports multiple replicas on one host sharing a local `/data` volume.
457
+ SQLite WAL, a scheduler lease, task claiming and graceful drain keep scheduled
458
+ runs and tasks single-execution across replicas.
459
+
460
+ ```bash
461
+ docker compose -f docker/compose.ha.yml up -d --build --scale vlambda=2
462
+ ```
463
+
464
+ The sample puts nginx in front of two replicas. Do not place SQLite on NFS; use
465
+ a local volume on the same host. Multi-host HA needs an external database and is
466
+ deferred to 0.5.0.
467
+
468
+ Relevant settings:
469
+
470
+ - `VLAMBDA_INSTANCE_ID` — unique replica name (defaults to hostname:pid)
471
+ - `VLAMBDA_DRAIN_TIMEOUT` — graceful shutdown wait (seconds, default 20)
472
+ - `VLAMBDA_TASK_LEASE` — task claim lease (seconds, default 600)
473
+ - `VLAMBDA_SCHEDULE_GRACE` / `VLAMBDA_SCHEDULE_RETENTION` — cron behavior
474
+ - `/ready` returns `503` while a replica is draining
475
+
476
+ ## OpenAPI
477
+
478
+ Every lambda can carry `summary`, `tags` and `description` metadata, editable
479
+ from the code/project editor or `PUT /vlambda/<slug>/meta`. The generated spec is
480
+ available at `/openapi.json` (authenticated and owner-scoped by default; set
481
+ `VLAMBDA_OPENAPI_PUBLIC=1` for a public spec) and a lightweight viewer is served
482
+ at `/docs` without external assets.
483
+
484
+ ```bash
485
+ python -m vlambda openapi --out openapi.json
486
+ ```
487
+
488
+ The spec describes `/f/<slug>` for every owned lambda, its method, request body
489
+ for write methods, generic JSON/text/binary responses and the `X-Request-Id`
490
+ header. Project static files are described under `/f/<slug>/{path}`.
491
+
304
492
  ## CLI
305
493
 
306
494
  The CLI talks to a running server and uses `VLAMBDA_URL`/`VLAMBDA_TOKEN` or
@@ -311,6 +499,11 @@ python -m vlambda deploy main.py --name my-lambda --method GET
311
499
  python -m vlambda invoke <slug>
312
500
  python -m vlambda logs --slug <slug> --days 1
313
501
  python -m vlambda env <slug> --set PLAIN=hello --secret TOKEN=s3cret
502
+ python -m vlambda kv <slug> --set hits=0 --ttl 3600
503
+ python -m vlambda kv <slug> --get hits
504
+ python -m vlambda egress <slug> --set api.example.com
505
+ python -m vlambda schedule list <slug>
506
+ python -m vlambda openapi --out openapi.json
314
507
  python -m vlambda task <task-id>
315
508
  ```
316
509
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "vlambda"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  authors = [{name = "Ricardo Robles Fernández"}]
5
5
  license = {text = "MIT"}
6
6
 
@@ -2,6 +2,11 @@
2
2
 
3
3
  import os
4
4
 
5
+ from vlambda.sqlite_compat import configure as _configure_sqlite
6
+
7
+ _configure_sqlite()
8
+
9
+ from vlambda.capabilities import EgressResponse, fetch, kv # noqa: F401
5
10
  from vlambda.response import Response # noqa: F401
6
11
 
7
12
  # Sandboxed runners only need the pure-Python modules; importing the native