@fabrika/provider-zerops 0.9.0-dev.7 → 0.9.0-dev.9

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 (68) hide show
  1. package/package.json +11 -10
  2. package/sandbox/Dockerfile +16 -0
  3. package/sandbox/bin/postgres-auth.sh +38 -0
  4. package/sandbox/bin/reset.sh +12 -0
  5. package/sandbox/bin/run-build.sh +35 -0
  6. package/sandbox/bin/run-launcher.sh +18 -0
  7. package/sandbox/bin/start-launcher.sh +25 -0
  8. package/sandbox/bin/stop.sh +47 -0
  9. package/sandbox/init.sh +61 -0
  10. package/src/api/client.ts +3 -1
  11. package/src/api/errors.ts +7 -3
  12. package/src/control.ts +150 -35
  13. package/src/execution.ts +12 -77
  14. package/src/installation/topology/setups.ts +3 -1
  15. package/src/installation/topology/topology.ts +1 -1
  16. package/src/manifest.ts +2 -7
  17. package/src/namespace/claims.ts +4 -0
  18. package/src/namespace/codec.ts +17 -0
  19. package/src/namespace/errors.ts +24 -0
  20. package/src/namespace/manifest.ts +31 -5
  21. package/src/namespace/provisioning.ts +68 -112
  22. package/src/namespace/proxy-reads.ts +16 -1
  23. package/src/namespace/proxy-secrets.ts +18 -5
  24. package/src/namespace/sandbox-host.ts +236 -0
  25. package/src/namespace/subdomain.ts +113 -0
  26. package/src/namespace/topology.ts +1 -1
  27. package/src/namespace/types.ts +11 -0
  28. package/src/namespace/update.ts +1 -0
  29. package/src/provider.ts +0 -40
  30. package/src/proxy-manifest-carrier.ts +21 -7
  31. package/src/sandbox/base-image.ts +100 -0
  32. package/src/sandbox/constants.ts +43 -0
  33. package/src/sandbox/deploy.ts +471 -0
  34. package/src/sandbox/dev-artifact.ts +206 -0
  35. package/src/sandbox/lifecycle.ts +200 -0
  36. package/src/sandbox/scripts.ts +26 -0
  37. package/src/sandbox-host.ts +31 -0
  38. package/src/source/port.ts +5 -0
  39. package/src/zbox/client.ts +267 -0
  40. package/src/zbox/errors.ts +45 -0
  41. package/src/zbox/index.ts +23 -0
  42. package/src/zbox/types.ts +168 -0
  43. package/types/src/api/errors.d.ts +3 -1
  44. package/types/src/control.d.ts +12 -1
  45. package/types/src/execution.d.ts +2 -13
  46. package/types/src/installation/topology/topology.d.ts +1 -1
  47. package/types/src/manifest.d.ts +3 -8
  48. package/types/src/namespace/codec.d.ts +18 -0
  49. package/types/src/namespace/errors.d.ts +12 -0
  50. package/types/src/namespace/proxy-reads.d.ts +6 -1
  51. package/types/src/namespace/proxy-secrets.d.ts +1 -1
  52. package/types/src/namespace/sandbox-host.d.ts +23 -0
  53. package/types/src/namespace/subdomain.d.ts +43 -0
  54. package/types/src/namespace/types.d.ts +10 -0
  55. package/types/src/provider.d.ts +0 -3
  56. package/types/src/proxy-manifest-carrier.d.ts +8 -1
  57. package/types/src/sandbox/base-image.d.ts +28 -0
  58. package/types/src/sandbox/constants.d.ts +29 -0
  59. package/types/src/sandbox/deploy.d.ts +75 -0
  60. package/types/src/sandbox/dev-artifact.d.ts +51 -0
  61. package/types/src/sandbox/lifecycle.d.ts +27 -0
  62. package/types/src/sandbox/scripts.d.ts +11 -0
  63. package/types/src/sandbox-host.d.ts +18 -0
  64. package/types/src/source/port.d.ts +5 -0
  65. package/types/src/zbox/client.d.ts +2 -0
  66. package/types/src/zbox/errors.d.ts +25 -0
  67. package/types/src/zbox/index.d.ts +3 -0
  68. package/types/src/zbox/types.d.ts +462 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/provider-zerops",
3
- "version": "0.9.0-dev.7",
3
+ "version": "0.9.0-dev.9",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,6 +12,7 @@
12
12
  },
13
13
  "files": [
14
14
  "src",
15
+ "sandbox",
15
16
  "!src/**/__tests__/**",
16
17
  "types"
17
18
  ],
@@ -44,15 +45,15 @@
44
45
  },
45
46
  "dependencies": {
46
47
  "zod": "^3.25.76",
47
- "@fabrika/app": "0.9.0-dev.7",
48
- "@fabrika/auth": "0.9.0-dev.7",
49
- "@fabrika/auth-core": "0.9.0-dev.7",
50
- "@fabrika/email": "0.9.0-dev.7",
51
- "@fabrika/iam-contract": "0.9.0-dev.7",
52
- "@fabrika/installation-init": "0.9.0-dev.7",
53
- "@fabrika/operations-contract": "0.9.0-dev.7",
54
- "@fabrika/provider-contract": "0.9.0-dev.7",
55
- "@fabrika/proxy-core": "0.9.0-dev.7",
48
+ "@fabrika/app": "0.9.0-dev.9",
49
+ "@fabrika/auth": "0.9.0-dev.9",
50
+ "@fabrika/auth-core": "0.9.0-dev.9",
51
+ "@fabrika/email": "0.9.0-dev.9",
52
+ "@fabrika/iam-contract": "0.9.0-dev.9",
53
+ "@fabrika/installation-init": "0.9.0-dev.9",
54
+ "@fabrika/operations-contract": "0.9.0-dev.9",
55
+ "@fabrika/provider-contract": "0.9.0-dev.9",
56
+ "@fabrika/proxy-core": "0.9.0-dev.9",
56
57
  "yaml": "^2.9.1"
57
58
  }
58
59
  }
@@ -0,0 +1,16 @@
1
+ # The base of every dev workspace sandbox: Bun, a PostgreSQL that listens on 127.0.0.1 only, the unprivileged user the
2
+ # build runs as, and the init script. Built on the sandbox host by `POST /base-build`; its name carries the hash of this
3
+ # directory's base files. Every other script reaches a sandbox with each deploy (`bin/`), so a fix needs no new base.
4
+ FROM oven/bun:1.3.14-alpine@sha256:efc5e42c7bedc1661ab0b7272c74c3ebf794f054297f530a62055f2d1a0eb662
5
+
6
+ # The PostgreSQL major is pinned by the package name; the patch floats, because Alpine drops a revision from its index
7
+ # once a newer one ships, and a pinned revision would then fail every later base build.
8
+ RUN apk add --no-cache postgresql17 postgresql17-contrib su-exec \
9
+ && adduser -D -h /home/fabrika-build -s /sbin/nologin -u 10001 fabrika-build
10
+
11
+ COPY init.sh /usr/local/lib/fabrika/
12
+
13
+ WORKDIR /app
14
+
15
+ ENTRYPOINT ["/bin/sh"]
16
+ CMD ["/usr/local/lib/fabrika/init.sh"]
@@ -0,0 +1,38 @@
1
+ #!/bin/sh
2
+ # Converges PostgreSQL's authentication before every build: the socket only root and PostgreSQL reach trusts every
3
+ # connection, and TCP on 127.0.0.1 takes the superuser's password, which only root can read. The build user can thus
4
+ # neither read a credential nor connect as the superuser.
5
+ set -eu
6
+
7
+ STATE=/var/lib/fabrika
8
+ PGDATA=$STATE/postgres
9
+ PASSWORD_FILE=$STATE/postgres-password
10
+
11
+ chown postgres:postgres /run/postgresql
12
+ chmod 700 /run/postgresql
13
+
14
+ waited=0
15
+ until su-exec postgres pg_isready -q -h /run/postgresql; do
16
+ if [ "$waited" -ge 60 ]; then
17
+ echo 'fabrika sandbox: PostgreSQL is not running'
18
+ exit 1
19
+ fi
20
+ sleep 1
21
+ waited=$((waited + 1))
22
+ done
23
+
24
+ if [ ! -s "$PASSWORD_FILE" ]; then
25
+ (
26
+ umask 077
27
+ head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n' >"$PASSWORD_FILE.tmp"
28
+ )
29
+ mv "$PASSWORD_FILE.tmp" "$PASSWORD_FILE"
30
+ fi
31
+ chmod 600 "$PASSWORD_FILE"
32
+
33
+ # Through stdin, so the password is in no process's arguments.
34
+ printf "ALTER ROLE postgres PASSWORD '%s';\n" "$(cat "$PASSWORD_FILE")" \
35
+ | su-exec postgres psql -X -q -v ON_ERROR_STOP=1 -h /run/postgresql -U postgres -d postgres -f - >/dev/null
36
+ printf '%s\n' 'local all all trust' 'host all all 127.0.0.1/32 scram-sha-256' >"$PGDATA/pg_hba.conf"
37
+ chown postgres:postgres "$PGDATA/pg_hba.conf"
38
+ su-exec postgres pg_ctl -D "$PGDATA" reload >/dev/null
@@ -0,0 +1,12 @@
1
+ #!/bin/sh
2
+ # What every deploy runs before it touches the application root: stop the previous launcher and any build still running,
3
+ # delete the launcher's environment so the untrusted build cannot read it, and converge PostgreSQL's authentication.
4
+ set -eu
5
+
6
+ BIN=$(dirname "$0")
7
+ STATE=/var/lib/fabrika
8
+
9
+ /bin/sh "$BIN/stop.sh" launcher
10
+ /bin/sh "$BIN/stop.sh" build
11
+ rm -f "$STATE/env" /app/fabrika/dev-booted
12
+ /bin/sh "$BIN/postgres-auth.sh"
@@ -0,0 +1,35 @@
1
+ #!/bin/sh
2
+ # Runs the build steps the deploy wrote to /var/lib/fabrika/build/<n>.sh, in order, each as the unprivileged build user
3
+ # with a clean environment from the application root, and stops at the first failure with its exit code. Whatever the
4
+ # build started is killed when it ends, and root owns the application root again.
5
+ set -eu
6
+
7
+ STATE=/var/lib/fabrika
8
+ STEPS=$STATE/build
9
+ BUILD_USER=fabrika-build
10
+ COUNT=$1
11
+
12
+ finish() {
13
+ code=$?
14
+ su-exec "$BUILD_USER" kill -KILL -1 2>/dev/null || true
15
+ chown -R root:root /app 2>/dev/null || true
16
+ rm -f "$STATE/build.pid"
17
+ exit "$code"
18
+ }
19
+ trap finish EXIT
20
+ echo $$ >"$STATE/build.pid"
21
+ chown -R "$BUILD_USER:$BUILD_USER" /app
22
+
23
+ step=1
24
+ while [ "$step" -le "$COUNT" ]; do
25
+ echo "fabrika-build: step $step/$COUNT"
26
+ code=0
27
+ (cd /app && exec env -i PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/home/$BUILD_USER \
28
+ su-exec "$BUILD_USER" /bin/sh -e "$STEPS/$step.sh") || code=$?
29
+ if [ "$code" -ne 0 ]; then
30
+ echo "fabrika-build: step $step/$COUNT failed with exit code $code"
31
+ exit "$code"
32
+ fi
33
+ step=$((step + 1))
34
+ done
35
+ echo 'fabrika-build: done'
@@ -0,0 +1,18 @@
1
+ #!/bin/sh
2
+ # The launcher process itself: the environment reaches it only through the env file the deploy wrote 0600, never through
3
+ # an argument or the sandbox's launch environment, which the sandbox host echoes. The database URL gets the local
4
+ # superuser's password here, which never leaves the sandbox.
5
+ set -eu
6
+
7
+ STATE=/var/lib/fabrika
8
+
9
+ set -a
10
+ . "$STATE/env"
11
+ set +a
12
+ if [ -n "${FABRIKA_APP_DATABASE_URL:-}" ]; then
13
+ FABRIKA_APP_DATABASE_URL="postgres://postgres:$(cat "$STATE/postgres-password")@127.0.0.1:5432/postgres"
14
+ export FABRIKA_APP_DATABASE_URL
15
+ fi
16
+ cd /app
17
+ echo $$ >"$STATE/launcher.pid"
18
+ exec bun -e 'await import("@fabrika/app/dev").then((dev) => dev.runDevLauncher(process.env))'
@@ -0,0 +1,25 @@
1
+ #!/bin/sh
2
+ # Starts the dev launcher detached in its own session and returns. Its output goes to a file only root reads: the
3
+ # launcher reports its log to Control itself.
4
+ set -eu
5
+
6
+ BIN=$(dirname "$0")
7
+ STATE=/var/lib/fabrika
8
+
9
+ if [ ! -f "$STATE/env" ] || [ ! -f /app/fabrika/dev.generated.json ]; then
10
+ echo 'fabrika sandbox: no deploy left a launcher configuration and environment'
11
+ exit 1
12
+ fi
13
+ pid=$(cat "$STATE/launcher.pid" 2>/dev/null || true)
14
+ case "$pid" in
15
+ '' | *[!0-9]*) ;;
16
+ *)
17
+ if kill -0 "$pid" 2>/dev/null; then
18
+ echo "fabrika sandbox: a launcher is already running (pid $pid)"
19
+ exit 1
20
+ fi
21
+ ;;
22
+ esac
23
+ rm -f /app/fabrika/dev-booted
24
+ umask 077
25
+ setsid /bin/sh "$BIN/run-launcher.sh" </dev/null >"$STATE/launcher.log" 2>&1 &
@@ -0,0 +1,47 @@
1
+ #!/bin/sh
2
+ # Stops the process `/var/lib/fabrika/<name>.pid` names and every process below it: TERM to it, then KILL to what is
3
+ # left after a grace period. A missing pidfile or a process already gone is stopped. `build` also kills every process of
4
+ # the build user, so no daemon a build started outlives it.
5
+ set -u
6
+
7
+ case "${1:-}" in
8
+ launcher | build) ;;
9
+ *)
10
+ echo 'usage: stop.sh launcher|build'
11
+ exit 2
12
+ ;;
13
+ esac
14
+ PIDFILE=/var/lib/fabrika/$1.pid
15
+ GRACE_SECONDS=10
16
+
17
+ tree() {
18
+ for child in $(pgrep -P "$1"); do
19
+ tree "$child"
20
+ done
21
+ echo "$1"
22
+ }
23
+
24
+ pid=$(cat "$PIDFILE" 2>/dev/null || true)
25
+ case "$pid" in
26
+ '' | *[!0-9]*) ;;
27
+ *)
28
+ if kill -0 "$pid" 2>/dev/null; then
29
+ before=$(tree "$pid")
30
+ kill -TERM "$pid" 2>/dev/null
31
+ waited=0
32
+ while kill -0 "$pid" 2>/dev/null && [ "$waited" -lt "$GRACE_SECONDS" ]; do
33
+ sleep 1
34
+ waited=$((waited + 1))
35
+ done
36
+ after=''
37
+ if kill -0 "$pid" 2>/dev/null; then after=$(tree "$pid"); fi
38
+ kill -KILL $before $after 2>/dev/null
39
+ fi
40
+ ;;
41
+ esac
42
+ rm -f "$PIDFILE"
43
+ if [ "$1" = build ]; then
44
+ # The kernel signals every process of the build user at once; a fork cannot slip past it.
45
+ su-exec fabrika-build kill -KILL -1 2>/dev/null
46
+ fi
47
+ exit 0
@@ -0,0 +1,61 @@
1
+ #!/bin/sh
2
+ # PID 1 of a dev workspace sandbox. Starts PostgreSQL on 127.0.0.1, then the dev launcher when a deploy left its
3
+ # configuration and environment, so a cold boot serves again without Control. Without them the sandbox idles. The
4
+ # scripts it starts are the ones the last deploy uploaded (`bin/current` names their version).
5
+ set -u
6
+
7
+ STATE=/var/lib/fabrika
8
+ PGDATA=$STATE/postgres
9
+
10
+ uploaded() {
11
+ version=$(cat "$STATE/bin/current" 2>/dev/null || true)
12
+ case "$version" in
13
+ '' | *[!0-9a-f]*) return 1 ;;
14
+ esac
15
+ [ -f "$STATE/bin/$version/$1" ] && echo "$STATE/bin/$version/$1"
16
+ }
17
+
18
+ shutdown() {
19
+ if stop=$(uploaded stop.sh); then /bin/sh "$stop" launcher; fi
20
+ su-exec postgres pg_ctl -D "$PGDATA" -m fast stop >/dev/null 2>&1
21
+ exit 0
22
+ }
23
+ trap shutdown TERM INT
24
+
25
+ mkdir -p "$STATE" /run/postgresql
26
+ # Traversable only: PostgreSQL reaches its own directory below it; every file holding a credential is 0600.
27
+ chmod 711 "$STATE"
28
+ chown postgres:postgres /run/postgresql
29
+ # Only root and PostgreSQL reach the socket, which trusts every local connection.
30
+ chmod 700 /run/postgresql
31
+ # Pidfiles name processes of an earlier boot; their pids may belong to anything now.
32
+ rm -f "$STATE"/*.pid
33
+
34
+ if [ ! -s "$PGDATA/PG_VERSION" ]; then
35
+ rm -rf "$PGDATA"
36
+ mkdir -p "$PGDATA"
37
+ chown postgres:postgres "$PGDATA"
38
+ chmod 700 "$PGDATA"
39
+ # TCP needs the superuser's password, which the first deploy sets (`bin/postgres-auth.sh`).
40
+ if su-exec postgres initdb -D "$PGDATA" -U postgres --auth-local=trust --auth-host=scram-sha-256 --encoding=UTF8 >"$STATE/initdb.log" 2>&1; then
41
+ printf '%s\n' 'local all all trust' 'host all all 127.0.0.1/32 scram-sha-256' >"$PGDATA/pg_hba.conf"
42
+ printf '%s\n' "listen_addresses = '127.0.0.1'" >>"$PGDATA/postgresql.conf"
43
+ else
44
+ echo "fabrika sandbox: initdb failed; see $STATE/initdb.log"
45
+ fi
46
+ fi
47
+
48
+ # PID 1 runs no PostgreSQL yet, so a lock file left by a stopped sandbox is stale.
49
+ rm -f "$PGDATA/postmaster.pid"
50
+ if ! su-exec postgres pg_ctl -D "$PGDATA" -w -t 60 -l "$PGDATA/server.log" start >/dev/null; then
51
+ echo "fabrika sandbox: PostgreSQL did not start; see $PGDATA/server.log"
52
+ fi
53
+
54
+ if [ -f "$STATE/env" ] && [ -f /app/fabrika/dev.generated.json ] && start=$(uploaded start-launcher.sh); then
55
+ /bin/sh "$start"
56
+ fi
57
+
58
+ while :; do
59
+ sleep 3600 &
60
+ wait $!
61
+ done
package/src/api/client.ts CHANGED
@@ -453,7 +453,9 @@ export const createZeropsApi = (options: ZeropsApiOptions): ZeropsApi => {
453
453
  await awaitUserDataSync(created, label, signal)
454
454
  return
455
455
  } catch (error) {
456
- if (!(error instanceof ZeropsApiError) || error.code !== USER_DATA_DUPLICATE_KEY) {
456
+ // A key the platform generated itself (a managed service's own variable) answers an `errorList` that holds the
457
+ // duplicate beside `userDataUseOfSystemKey`; updating its record is allowed (measured on `zbox@1`).
458
+ if (!(error instanceof ZeropsApiError) || (error.code !== USER_DATA_DUPLICATE_KEY && !error.listedCodes.includes(USER_DATA_DUPLICATE_KEY))) {
457
459
  throw error
458
460
  }
459
461
  }
package/src/api/errors.ts CHANGED
@@ -21,8 +21,11 @@ export const isAppVersionNotFoundResponse = (value: unknown): boolean => {
21
21
  * branch on `userDataDuplicateKey` rather than matching a prose message.
22
22
  */
23
23
  export class ZeropsApiError extends Error {
24
- constructor(message: string, readonly status: number, readonly code: string) {
24
+ /** The codes of an `errorList` answer's entries: the platform reports several refusals of one request there. */
25
+ readonly listedCodes: readonly string[]
26
+ constructor(message: string, readonly status: number, readonly code: string, listedCodes: readonly string[] = []) {
25
27
  super(message)
28
+ this.listedCodes = listedCodes
26
29
  this.name = 'ZeropsApiError'
27
30
  }
28
31
  }
@@ -45,6 +48,7 @@ export const apiError = (label: string, status: number, body: unknown, redactDet
45
48
  const { error: inner, ...outer } = errorEnvelopeSchema.parse(body)
46
49
  const code = inner.code ?? outer.code ?? ''
47
50
  const message = redactDetail ? '' : (inner.message ?? outer.message ?? '')
48
- const detail = [code, message].filter((part) => part !== '').join(': ')
49
- return new ZeropsApiError(`zerops: ${label} failed (${status})${detail === '' ? '' : ` — ${detail.slice(0, 300)}`}`, status, code)
51
+ const listed = [...inner.meta, ...outer.meta].flatMap((entry) => entry.code === undefined ? [] : [entry.code])
52
+ const detail = [code, ...listed, message].filter((part) => part !== '').join(': ')
53
+ return new ZeropsApiError(`zerops: ${label} failed (${status})${detail === '' ? '' : ` — ${detail.slice(0, 300)}`}`, status, code, listed)
50
54
  }
package/src/control.ts CHANGED
@@ -7,7 +7,6 @@ import type {
7
7
  ProviderDeployInput,
8
8
  ProviderDeploymentNamespace,
9
9
  ProviderDeployOutcome,
10
- ProviderDevEnvironmentInput,
11
10
  ProviderEnvelope,
12
11
  ProviderEnvironment,
13
12
  ProviderReconcileOutcome,
@@ -46,16 +45,25 @@ import {
46
45
  type ZeropsNamespaceTarget,
47
46
  zeropsNamespaceTargetCodec,
48
47
  } from './namespace'
49
- import { assertNamespaceProxyReads, type OlderProxyReaders } from './namespace/proxy-reads'
48
+ import { namespaceError, ZEROPS_DEV_TARGET_RETIRED } from './namespace/errors'
49
+ import { assertNamespaceProxyAcceptsSandboxes, assertNamespaceProxyReads, type OlderProxyReaders } from './namespace/proxy-reads'
50
+ import type { ZeropsNamespaceSandboxHost } from './namespace/types'
50
51
  import { createZeropsDeployer, ZEROPS_VERSION_DEADLINE_MS } from './provider'
52
+ import { deploySandbox, parseSandboxCheckpoint } from './sandbox/deploy'
53
+ import { assertSandboxArtifact } from './sandbox/dev-artifact'
54
+ import { createZeropsDevSandboxes } from './sandbox/lifecycle'
51
55
  import { allowedReferenceHosts, assertNoPlatformOnlyReference, foreignReferenceHost, serviceReferenceHosts } from './service-references'
52
56
  import { normalizeZeropsSourceRepository } from './source'
53
57
  import type { ZeropsSourcePort, ZeropsSourcePrivateBinding } from './source/port'
54
58
  import type { ZeropsRunState, ZeropsRuntimeTarget } from './types'
55
59
  import { deleteVersion, isVersionNotFound } from './version-attempt'
60
+ import { createZboxClient, type ZboxClient, type ZboxFetch } from './zbox'
56
61
 
57
62
  /** Provider coordinates that are safe to persist. Credentials are composed only for a live run. */
58
- export interface ZeropsStoredTarget {
63
+ export type ZeropsStoredTarget = ZeropsServiceTarget | ZeropsDevSandboxesTarget
64
+
65
+ /** An environment served by its own Zerops service. */
66
+ export interface ZeropsServiceTarget {
59
67
  serviceId: string
60
68
  /**
61
69
  * The object-storage service ids the first registration's collision-refusing import created: the creation evidence
@@ -64,14 +72,26 @@ export interface ZeropsStoredTarget {
64
72
  createdBuckets?: string[]
65
73
  }
66
74
 
67
- const storedTargetSchema = z.object({ serviceId: z.string().min(1), createdBuckets: z.array(z.string().min(1)).min(1).optional() })
75
+ /** A `dev` environment: it has no service, and each MCP workspace runs in a sandbox of the namespace's sandbox host. */
76
+ export interface ZeropsDevSandboxesTarget {
77
+ kind: 'devSandboxes'
78
+ }
79
+
80
+ const storedTargetSchema = z.union([
81
+ z.object({ kind: z.literal('devSandboxes') }).strict(),
82
+ z.object({ serviceId: z.string().min(1), createdBuckets: z.array(z.string().min(1)).min(1).optional() }),
83
+ ])
68
84
 
85
+ // The dev variant is additive: every stored service target still reads as it did, so the version stays.
69
86
  export const zeropsStoredTargetCodec: ProviderCodec<ZeropsStoredTarget> = {
70
87
  version: 2,
71
- encode: (target) => ({
72
- serviceId: target.serviceId,
73
- ...(target.createdBuckets === undefined ? {} : { createdBuckets: [...target.createdBuckets] }),
74
- }),
88
+ encode: (target): JsonValue => {
89
+ if ('kind' in target) return { kind: target.kind }
90
+ return {
91
+ serviceId: target.serviceId,
92
+ ...(target.createdBuckets === undefined ? {} : { createdBuckets: [...target.createdBuckets] }),
93
+ }
94
+ },
75
95
  decode: (payload) => {
76
96
  const result = storedTargetSchema.safeParse(payload)
77
97
  if (!result.success) throw new Error('Zerops stored target serviceId must be a non-empty string')
@@ -79,6 +99,12 @@ export const zeropsStoredTargetCodec: ProviderCodec<ZeropsStoredTarget> = {
79
99
  },
80
100
  }
81
101
 
102
+ /** The service of a target that has one; a dev environment's sandboxes are not one. */
103
+ const serviceTargetOf = (target: ZeropsStoredTarget): ZeropsServiceTarget => {
104
+ if ('kind' in target) throw new Error('zerops: a dev environment runs in workspace sandboxes and has no Zerops service')
105
+ return target
106
+ }
107
+
82
108
  const envelope = <T>(codec: ProviderCodec<T>, value: T): ProviderEnvelope => ({
83
109
  provider: 'zerops',
84
110
  version: codec.version,
@@ -102,6 +128,10 @@ export interface ZeropsControlProviderOptions {
102
128
  /** The manifest parsers of the older releases' proxies, which a namespace pinned to an older source may run. */
103
129
  readonly olderProxies: OlderProxyReaders
104
130
  }
131
+ /** The transport of the sandbox host API; tests pass a fake. */
132
+ readonly zboxFetch?: ZboxFetch
133
+ /** Where a provider event outside a run is reported (a dev sandbox replaced); the process's stderr by default. */
134
+ readonly log?: (line: string) => void
105
135
  }
106
136
 
107
137
  interface DecodedZeropsEnvironment {
@@ -257,7 +287,7 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
257
287
  }
258
288
 
259
289
  const assertVersionCoordinates = (environment: ProviderEnvironment, appVersionId: string, version: ZeropsAppVersion): void => {
260
- const serviceId = decodeEnvironment(environment).target.serviceId
290
+ const serviceId = serviceTargetOf(decodeEnvironment(environment).target).serviceId
261
291
  if (version.id !== appVersionId || (version.serviceStackId !== undefined && version.serviceStackId !== serviceId)) {
262
292
  throw new Error('Zerops app-version response has different coordinates')
263
293
  }
@@ -290,23 +320,58 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
290
320
  return { commitSha: result.commitSha }
291
321
  }
292
322
 
293
- /** Stop or start the service of a `dev` environment outside a run, and wait for the platform's process. */
294
- const runningState = async (input: ProviderDevEnvironmentInput, action: 'start' | 'stop'): Promise<void> => {
295
- if (input.environment.kind !== 'dev') throw new Error('zerops: only a dev environment is stopped and started outside a run')
296
- const placement = resolvedEnvironment(input.environment, { requireReady: false })
297
- const serviceId = placement.target.serviceId
298
- // Idempotent: Control's record of a stopped service can lag behind a start from elsewhere.
299
- const status = (await api.getService({ serviceId, signal: input.signal })).status
300
- if (status === (action === 'stop' ? 'STOPPED' : 'ACTIVE')) return
301
- const process = action === 'stop'
302
- ? await api.stopService({ serviceId, signal: input.signal })
303
- : await api.startService({ serviceId, signal: input.signal })
304
- await waitForProcess({
305
- api,
306
- processId: process.id,
307
- sleep: options.sleep ?? defaultSleep,
308
- signal: input.signal,
309
- label: `the ${action} of ${input.environment.appId}/${input.environment.env}`,
323
+ /** The namespace's sandbox host, which runs the workspace sandboxes of its dev environment; null until it is created. */
324
+ const sandboxHostOf = (environment: ProviderEnvironment): ZeropsNamespaceSandboxHost | null => {
325
+ const { target, namespaceTarget } = decodeEnvironment(environment)
326
+ // An older release registered a dev environment as its own Zerops service; it never runs in a sandbox.
327
+ if (environment.kind === 'dev' && !('kind' in target)) {
328
+ throw namespaceError(
329
+ ZEROPS_DEV_TARGET_RETIRED,
330
+ `zerops: dev environment \`${environment.appId}/${environment.env}\` was registered with its own Zerops service, which this release `
331
+ + 'no longer runs; delete the environment and register it again with --kind=dev and a wildcard domain',
332
+ false,
333
+ )
334
+ }
335
+ return namespaceTarget.sandboxHost ?? null
336
+ }
337
+
338
+ const sandboxClient = (apiOrigin: string, sandboxKey: string): ZboxClient =>
339
+ createZboxClient({ baseUrl: apiOrigin, apiKey: sandboxKey, ...(options.zboxFetch === undefined ? {} : { fetch: options.zboxFetch }) })
340
+
341
+ const devSandboxes = createZeropsDevSandboxes({
342
+ client: (environment, sandboxKey) => {
343
+ const host = sandboxHostOf(environment)
344
+ return host === null ? null : sandboxClient(host.apiOrigin, sandboxKey)
345
+ },
346
+ admit: (environment) => {
347
+ sandboxHostOf(environment)
348
+ const { namespace, namespaceTarget } = decodeNamespace(environment)
349
+ assertNamespaceProxyAcceptsSandboxes(namespace.id, namespaceTarget.proxySource)
350
+ },
351
+ sleep: options.sleep ?? defaultSleep,
352
+ log: options.log ?? ((line) => console.warn(line)),
353
+ })
354
+
355
+ /**
356
+ * A dev environment imports no service: each workspace gets a sandbox when it binds. What a sandbox cannot give the
357
+ * application is refused here, before anything is created.
358
+ */
359
+ const prepareDevRegistration = (
360
+ registration: ProviderRegistration,
361
+ namespaceId: string,
362
+ namespaceTarget: ZeropsNamespaceTarget,
363
+ ): ProviderRegistration => {
364
+ if (namespaceTarget.publicAccess === 'zerops-subdomain') {
365
+ throw new Error(
366
+ `zerops: a dev environment serves each workspace on its own host, so namespace \`${namespaceId}\` needs custom-domain public access`,
367
+ )
368
+ }
369
+ const manifest = decodeEnvelope('artifact', 'zerops', registration.environment.artifact, zeropsArtifactCodec)
370
+ assertFabrikaManifestCoordinates(manifest.app, { appId: registration.app.id, env: registration.environment.env })
371
+ assertSandboxArtifact(manifest)
372
+ return normalizeRegistration({
373
+ app: registration.app,
374
+ environment: { ...registration.environment, target: envelope(zeropsStoredTargetCodec, { kind: 'devSandboxes' }) },
310
375
  })
311
376
  }
312
377
 
@@ -355,18 +420,54 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
355
420
  ...(binding === undefined ? {} : { privateBinding: binding }),
356
421
  }
357
422
  }
423
+ if (input.devSandbox !== undefined && input.environment.kind !== 'dev') {
424
+ throw new Error('zerops: only a dev environment deploys into a workspace sandbox')
425
+ }
426
+ if (input.environment.kind === 'dev') {
427
+ if (input.devSandbox === undefined) throw new Error('zerops: a dev environment deploys only into a workspace sandbox, through bind_dev')
428
+ const host = sandboxHostOf(input.environment)
429
+ if (host === null) throw new Error("zerops: the namespace's sandbox host is not created yet; bind the workspace again")
430
+ const result = await deploySandbox({
431
+ appId: input.app.id,
432
+ env: input.environment.env,
433
+ runId: input.runId,
434
+ artifact,
435
+ access: input.environment.access,
436
+ vars: input.vars,
437
+ secrets: input.secrets,
438
+ managedEnvironment: input.managedEnvironment,
439
+ ...(source === undefined
440
+ ? {}
441
+ : {
442
+ source: {
443
+ repository: source.repository,
444
+ commitSha: source.commitSha,
445
+ ...(source.privateBinding === undefined ? {} : { privateBinding: source.privateBinding }),
446
+ },
447
+ }),
448
+ sandbox: { sandboxId: input.devSandbox.sandboxId, apiOrigin: host.apiOrigin, sandboxKey: input.devSandbox.sandboxKey },
449
+ client: sandboxClient(host.apiOrigin, input.devSandbox.sandboxKey),
450
+ sourcePort: options.source,
451
+ sleep: options.sleep ?? defaultSleep,
452
+ dryRun: input.dryRun,
453
+ signal: input.signal,
454
+ events: input.events,
455
+ })
456
+ input.signal.throwIfAborted()
457
+ return { state: result.status }
458
+ }
459
+ const serviceTarget = serviceTargetOf(placement.target)
358
460
  const runtimeTarget: ZeropsRuntimeTarget = {
359
461
  projectId: placement.projectId,
360
- serviceId: placement.target.serviceId,
462
+ serviceId: serviceTarget.serviceId,
361
463
  accessToken: options.accessToken,
362
- ...(placement.target.createdBuckets === undefined ? {} : { createdBuckets: placement.target.createdBuckets }),
464
+ ...(serviceTarget.createdBuckets === undefined ? {} : { createdBuckets: serviceTarget.createdBuckets }),
363
465
  ...(source === undefined ? {} : { source }),
364
466
  ...(options.apiBaseUrl !== undefined ? { apiBaseUrl: options.apiBaseUrl } : {}),
365
467
  }
366
468
  const result = await deployRun({
367
469
  appId: input.app.id,
368
470
  env: input.environment.env,
369
- ...(input.environment.kind === 'dev' && source !== undefined ? { dev: { head: source.commitSha } } : {}),
370
471
  secrets: input.secrets,
371
472
  vars: input.vars,
372
473
  managedEnvironment: input.managedEnvironment,
@@ -381,13 +482,12 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
381
482
  input.signal.throwIfAborted()
382
483
  return { state: result.status, ...(result.artifactState === undefined ? {} : { artifactState: result.artifactState }) }
383
484
  },
384
- devEnvironments: {
385
- stop: (input) => runningState(input, 'stop'),
386
- start: (input) => runningState(input, 'start'),
387
- },
485
+ devSandboxes,
388
486
  cancel: async (input): Promise<ProviderCancelOutcome> => {
389
487
  const signal = input.signal
390
488
  signal.throwIfAborted()
489
+ // The deploy's own invocation stops a sandbox build on a cancel; nothing here holds the sandbox host's key.
490
+ if (parseSandboxCheckpoint(input) !== null) return { state: 'settled', outcome: 'failed' }
391
491
  const checkpoint = runCheckpoint(input)
392
492
  if (checkpoint === null) return { state: 'settled', outcome: 'failed' }
393
493
  if (!checkpoint.triggered) {
@@ -415,6 +515,10 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
415
515
  const log = async (line: string): Promise<void> => {
416
516
  await input.log?.(line).catch(() => {})
417
517
  }
518
+ if (parseSandboxCheckpoint(input) !== null) {
519
+ await log('deploy interrupted: Control stopped while it deployed into the workspace sandbox; bind the workspace again')
520
+ return { state: 'failed' }
521
+ }
418
522
  const checkpoint = runCheckpoint(input)
419
523
  if (checkpoint === null) {
420
524
  await log('unrecognised checkpoint; failing without platform calls')
@@ -457,7 +561,14 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
457
561
  assertNoPlatformOnlyReference(input.name)
458
562
  assertNoPlatformOnlyReference(input.value)
459
563
  assertOwnReferencesOnly(input.environment, input.name, input.value)
460
- const target = decodeEnvironment(input.environment).target
564
+ const stored = decodeEnvironment(input.environment).target
565
+ if ('kind' in stored) {
566
+ throw new ProviderSecretError(
567
+ 'zerops: a dev environment runs in workspace sandboxes and keeps no secret on a Zerops service',
568
+ 'devEnvironment',
569
+ )
570
+ }
571
+ const target = stored
461
572
  try {
462
573
  await api.putServiceEnv({
463
574
  serviceId: target.serviceId,
@@ -474,7 +585,10 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
474
585
  },
475
586
  delete: async (input) => {
476
587
  assertAuthoredManagedName(input.name)
477
- const target = decodeEnvironment(input.environment).target
588
+ const stored = decodeEnvironment(input.environment).target
589
+ // A dev environment's put refuses, so there is nothing to delete.
590
+ if ('kind' in stored) return
591
+ const target = stored
478
592
  const variables = await api.listServiceEnv({ serviceId: target.serviceId, signal: abortSignal() })
479
593
  const found = variables.find((variable) => variable.key === input.name)
480
594
  if (found !== undefined) {
@@ -492,6 +606,7 @@ export const createZeropsControlProvider = (options: ZeropsControlProviderOption
492
606
  if (namespaceTarget.ready !== true || namespaceTarget.projectId === undefined) {
493
607
  throw new Error(`Zerops deployment namespace \`${namespace.id}\` is not ready`)
494
608
  }
609
+ if (input.registration.environment.kind === 'dev') return prepareDevRegistration(input.registration, namespace.id, namespaceTarget)
495
610
  const projectId = namespaceTarget.projectId
496
611
  const manifest = decodeEnvelope('artifact', 'zerops', input.registration.environment.artifact, zeropsArtifactCodec)
497
612
  assertFabrikaManifestCoordinates(manifest.app, {