Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3ec5c01
feat(selfhost): celld build and deploy core, plaintext mode (L22b, WI…
raphaeltm Nov 19, 2026
f32153e
fix(selfhost): pass the box's env file, state dir and secrets to the …
raphaeltm Nov 19, 2026
199e4cd
fix(selfhost): never mint platform keys over an earlier deploy; write…
raphaeltm Nov 19, 2026
587a283
fix(selfhost): create-account reads the deploy's --secrets file and f…
raphaeltm Nov 19, 2026
d29d55e
test(api): the Cloudflare entry must not reach scripts/ (scripts/self…
raphaeltm Nov 19, 2026
f602acb
refactor(selfhost): box-settings takes reverse-tunnel defaults and bo…
raphaeltm Nov 19, 2026
755042a
ci: select self-host suites by what they read, not by scripts/selfhos…
raphaeltm Nov 19, 2026
f557fc8
fix(selfhost): refuse vm-agent build paths with a comma (docker --mou…
raphaeltm Nov 19, 2026
7f3fcef
chore(selfhost): name mechanisms instead of plan rounds; share REPO_R…
raphaeltm Nov 19, 2026
1a17668
task: record L22a shipped evidence and L22b progress
raphaeltm Nov 19, 2026
db0cfda
test(api): prove the scripts/ prefix in the Cloudflare boundary list …
raphaeltm Nov 19, 2026
ceca7bf
Merge remote-tracking branch 'origin/main' into sam/l22b-celld-deploy…
raphaeltm Nov 19, 2026
0f22ec0
fix(selfhost): linear-time dotenv and bucket-prefix parsing; pnpm exe…
raphaeltm Nov 19, 2026
5063c4b
refactor(selfhost): clear SonarCloud code smells in the self-host scr…
raphaeltm Nov 19, 2026
1126919
fix(selfhost): report a stored mixed-case account; owner guard in the…
raphaeltm Nov 19, 2026
10ce222
fix(selfhost): never regenerate a lost identity key; complete new gro…
raphaeltm Nov 19, 2026
d17eed7
fix(selfhost): replace gateway.env and the collector env files atomic…
raphaeltm Nov 19, 2026
31535f2
fix(selfhost): upgrade retries, box-scoped rollback, selected compose…
raphaeltm Nov 19, 2026
51c0242
fix(selfhost): build the vm-agent with CGO_ENABLED=0
raphaeltm Nov 19, 2026
36adf18
fix(selfhost): spawn operator tools by absolute PATH lookup; time out…
raphaeltm Nov 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,22 @@ jobs:
- 'apps/web/src/pages/project-chat/lineageUtils.ts'
- 'apps/web/src/pages/project-chat/useTaskGroups.ts'
- 'scripts/deploy/deploy-signing-keys.ts'
# Imported by scripts/selfhost/celld-config.ts (below).
- 'scripts/deploy/durable-object-migrations.ts'
- 'scripts/deploy/types.ts'
- 'scripts/deploy/vapid-keys.ts'
# Self-host build tests import the celld generator, presign vars, SMTP settings and
# account SQL: these modules plus the scripts/selfhost modules they import. The
# transitive ones are listed by hand: ci-suite-selection.test.ts checks the direct
# imports but does not follow `./x.js` specifiers to `x.ts`.
- 'scripts/selfhost/account-sql.ts'
- 'scripts/selfhost/box-settings.ts'
- 'scripts/selfhost/celld-config.ts'
- 'scripts/selfhost/celld-project.ts'
- 'scripts/selfhost/mail-settings.ts'
- 'scripts/selfhost/observability-migrations.ts'
- 'scripts/selfhost/reverse-tunnel-migrations.ts'
- 'scripts/selfhost/secret-files.ts'
- 'scripts/selfhost/selfhost-migrations.ts'
# selfhost-inbound-email.test.ts builds this receiver (Self-host Email job only).
- 'deploy/selfhost/sam-mx/**'
Expand Down Expand Up @@ -227,6 +239,8 @@ jobs:
- 'apps/*/package.json'
- 'packages/*/package.json'
- 'packages/vm-agent/Makefile'
# scripts/selfhost/vm-agent-artifacts.test.ts pins the Go build image to its go directive.
- 'packages/vm-agent/go.mod'
- 'packages/cli/Makefile'
- 'apps/installer/scripts/**'
- 'apps/www/scripts/build-tracker.ts'
Expand All @@ -235,6 +249,12 @@ jobs:
- 'apps/api/scripts/**'
- 'apps/api/container-entrypoints/**'
- 'scripts/deploy/**'
# Self-hosted (celld) packaging: config generator, deploy, upgrade, create-account.
- 'scripts/selfhost/**'
# ...and what its tests read outside it: the self-host migration projection and the
# SMTP settings parser.
- 'apps/api/src/selfhost/**'
- 'packages/agent-email/src/smtp-config.ts'
# The generated Worker config is built from wrangler.toml, so its text-binding
# guard and effective-config tests must run when only wrangler.toml changes.
- 'apps/api/wrangler.toml'
Expand Down Expand Up @@ -853,7 +873,9 @@ jobs:
run: pnpm quality:file-sizes

- name: Type check deployment scripts
run: npx tsc --project scripts/deploy/tsconfig.json --noEmit
run: |
pnpm exec tsc --project scripts/deploy/tsconfig.json --noEmit
pnpm exec tsc --project scripts/selfhost/tsconfig.json --noEmit

- name: Validate script syntax
run: |
Expand All @@ -867,6 +889,11 @@ jobs:
- name: Run deploy and quality script tests
run: pnpm quality:scripts:test

# celld wrangler.json generator, plaintext deploy (--init/--dry-run), upgrade, root-only
# secret files, pinned versions. No Docker, celld or frp: those run on a box.
- name: Run self-host packaging tests
run: pnpm selfhost:test

- name: Verify installer engine, runtime integrity and bounded proof contracts
run: |
pnpm exec vitest run --config scripts/deploy/installer-engine.vitest.config.ts
Expand Down
23 changes: 23 additions & 0 deletions apps/api/src/selfhost/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* What the self-host (celld) build adds to the shared Worker entry, applied once by the generated
* celld entry (`scripts/selfhost/celld-project.ts`) before any request. `apps/api/src/index.ts`
* never imports this module, so the Cloudflare bundle carries none of it
* (tests/unit/selfhost/reverse-tunnel-placement.test.ts).
*
* - speech-to-text and read-aloud through admin-configured endpoints (`speech/`);
* - product analytics in OBSERVABILITY_DATABASE when ANALYTICS_SINK="d1" (`analytics/`),
* with its `analytics.d1.*` retention settings;
* - the reverse-tunnel transport, gateway routes and lease sweep (`reverse-tunnel/entry.ts`),
* with the shared cloud-init generator as the frpc boot renderer.
*/
import { generateCloudInit } from '@simple-agent-manager/cloud-init';

import { installAnalyticsD1Sink } from './analytics';
import { type SharedWorkerEntry, withReverseTunnel } from './reverse-tunnel/entry';
import { installSpeechEndpoints } from './speech';

export function withSelfHostBuild<T extends SharedWorkerEntry>(worker: T): T {
installSpeechEndpoints();
installAnalyticsD1Sink();
return withReverseTunnel(worker, generateCloudInit);
}
82 changes: 55 additions & 27 deletions apps/api/src/selfhost/reverse-tunnel/config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
const REVERSE_TUNNEL_HEARTBEAT_INTERVAL_SECONDS = '30';
const REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS = '90';

import { constantTimeEqual } from '../../lib/constant-time';
import type { ReverseTunnelEnv as Env } from './env';
import {
DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
DEFAULT_REVERSE_TUNNEL_PORT_RANGE_END,
DEFAULT_REVERSE_TUNNEL_PORT_RANGE_START,
DEFAULT_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS,
MAX_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
MAX_REVERSE_TUNNEL_PORT_RANGE_SIZE,
MAX_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS,
MIN_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE,
} from './limits';

export {
DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
DEFAULT_REVERSE_TUNNEL_PORT_RANGE_END,
DEFAULT_REVERSE_TUNNEL_PORT_RANGE_START,
DEFAULT_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS,
MAX_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
MAX_REVERSE_TUNNEL_PORT_RANGE_SIZE,
MAX_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS,
MIN_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS,
REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE,
} from './limits';

export const REVERSE_TUNNEL_UPSTREAM_HOST = '127.0.0.1';
export const DEFAULT_REVERSE_TUNNEL_PORT_RANGE_START = 20000;
export const DEFAULT_REVERSE_TUNNEL_PORT_RANGE_END = 29999;
export const MAX_REVERSE_TUNNEL_PORT_RANGE_SIZE = 20000;
export const MIN_REVERSE_TUNNEL_GATEWAY_SECRET_LENGTH = 32;

export const DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS = Number(
REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS
);
export const MIN_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS =
2 * Number(REVERSE_TUNNEL_HEARTBEAT_INTERVAL_SECONDS);
export const MAX_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS = 3600;
export const REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE = 2;
export const DEFAULT_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS =
REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE * DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS;
export const MAX_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS = 86_400;

const CREDENTIAL_LABEL = 'sam-reverse-tunnel-credential:v1';

export type ReverseTunnelEnv = Pick<
Expand Down Expand Up @@ -181,28 +187,50 @@ async function signIdentity(
purpose: string,
identity: ReverseTunnelCredentialIdentity
): Promise<ArrayBuffer> {
if (!env.ENCRYPTION_KEY) throw new Error('ENCRYPTION_KEY is required for reverse-tunnel credentials');
if (!env.ENCRYPTION_KEY)
throw new Error('ENCRYPTION_KEY is required for reverse-tunnel credentials');
for (const value of Object.values(identity)) {
if (!value || value.length > 256) throw new Error('Reverse-tunnel identity is required and bounded');
if (!value || value.length > 256)
throw new Error('Reverse-tunnel identity is required and bounded');
}
const encoder = new TextEncoder();
const root = await crypto.subtle.importKey('raw', encoder.encode(env.ENCRYPTION_KEY), 'HKDF', false, ['deriveKey']);
const root = await crypto.subtle.importKey(
'raw',
encoder.encode(env.ENCRYPTION_KEY),
'HKDF',
false,
['deriveKey']
);
const key = await crypto.subtle.deriveKey(
{ name: 'HKDF', hash: 'SHA-256', salt: encoder.encode('sam:reverse-tunnel:v1'), info: encoder.encode(purpose) },
root, { name: 'HMAC', hash: 'SHA-256', length: 256 }, false, ['sign']
{
name: 'HKDF',
hash: 'SHA-256',
salt: encoder.encode('sam:reverse-tunnel:v1'),
info: encoder.encode(purpose),
},
root,
{ name: 'HMAC', hash: 'SHA-256', length: 256 },
false,
['sign']
);
return crypto.subtle.sign(
'HMAC',
key,
encoder.encode(
JSON.stringify([identity.leaseId, identity.nodeId, identity.runtimeIncarnationId])
)
);
return crypto.subtle.sign('HMAC', key, encoder.encode(JSON.stringify([
identity.leaseId, identity.nodeId, identity.runtimeIncarnationId,
])));
}
export async function deriveReverseTunnelCredential(
env: Pick<Env, 'ENCRYPTION_KEY'>, identity: ReverseTunnelCredentialIdentity
env: Pick<Env, 'ENCRYPTION_KEY'>,
identity: ReverseTunnelCredentialIdentity
): Promise<string> {
return base64Url(await signIdentity(env, CREDENTIAL_LABEL, identity));
}
/** frps replaces the previous control before activating another with this same server-chosen id. */
export async function deriveReverseTunnelRunId(
env: Pick<Env, 'ENCRYPTION_KEY'>, identity: ReverseTunnelCredentialIdentity
env: Pick<Env, 'ENCRYPTION_KEY'>,
identity: ReverseTunnelCredentialIdentity
): Promise<string> {
return hex(await signIdentity(env, 'sam-reverse-tunnel-control:v1', identity)).slice(0, 32);
}
Expand Down
20 changes: 20 additions & 0 deletions apps/api/src/selfhost/reverse-tunnel/limits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Reverse-tunnel defaults and bounds, free of imports so the box deploy script
* (scripts/selfhost/box-settings.ts) resolves frps's settings from the same values the Worker
* enforces (config.ts) instead of a copy of them.
*/
const REVERSE_TUNNEL_HEARTBEAT_INTERVAL_SECONDS = 30;

export const DEFAULT_REVERSE_TUNNEL_PORT_RANGE_START = 20000;
export const DEFAULT_REVERSE_TUNNEL_PORT_RANGE_END = 29999;
export const MAX_REVERSE_TUNNEL_PORT_RANGE_SIZE = 20000;

export const DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS = 90;
export const MIN_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS =
2 * REVERSE_TUNNEL_HEARTBEAT_INTERVAL_SECONDS;
export const MAX_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS = 3600;
/** A released port is not reissued before this many heartbeat timeouts. */
export const REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE = 2;
export const DEFAULT_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS =
REVERSE_TUNNEL_PORT_REUSE_HEARTBEAT_MULTIPLE * DEFAULT_REVERSE_TUNNEL_HEARTBEAT_TIMEOUT_SECONDS;
export const MAX_REVERSE_TUNNEL_PORT_REUSE_MIN_AGE_SECONDS = 86_400;
22 changes: 19 additions & 3 deletions apps/api/tests/helpers/selfhost-only-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export const SELFHOST_ONLY_PATHS: readonly string[] = [
'apps/api/src/vendor/dgit/',
];

/**
* Repository-relative path prefixes the Cloudflare Worker's entry must not reach: the self-host-only
* code, plus `scripts/` (deploy and packaging tooling, scripts/selfhost/ included), which runs on
* an operator's machine with node APIs and never belongs in any Worker bundle.
*/
export const CLOUDFLARE_ENTRY_FORBIDDEN_PATHS: readonly string[] = [
...SELFHOST_ONLY_PATHS,
'scripts/',
];

/** npm packages only the self-host build may import (pako 2 is dgit's compression library). */
export const SELFHOST_ONLY_PACKAGES: readonly string[] = ['pako'];

Expand Down Expand Up @@ -85,9 +95,15 @@ export function collectImportGraph(entry: string): ImportGraph {
return { files, packages };
}

/** Every self-host-only file and package `graph` reaches, as human-readable findings. */
export function selfhostOnlyFindings(graph: ImportGraph): string[] {
const files = graph.files.filter((file) => SELFHOST_ONLY_PATHS.some((p) => file.startsWith(p)));
/**
* Every self-host-only file and package `graph` reaches, as human-readable findings. `paths` are
* the forbidden file prefixes (`CLOUDFLARE_ENTRY_FORBIDDEN_PATHS` for the Cloudflare entry).
*/
export function selfhostOnlyFindings(
graph: ImportGraph,
paths: readonly string[] = SELFHOST_ONLY_PATHS
): string[] {
const files = graph.files.filter((file) => paths.some((p) => file.startsWith(p)));
const packages = SELFHOST_ONLY_PACKAGES.filter((name) => graph.packages.has(name)).map(
(name) => `package ${name} (imported by ${graph.packages.get(name)})`
);
Expand Down
Loading
Loading