diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8c52ff9 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..9050450 --- /dev/null +++ b/.prettierignore @@ -0,0 +1,23 @@ +# Dependencies and build output +node_modules/ +.next/ +dist/ +coverage/ +apps/ide/lib/ +apps/ide/plugins/ +apps/ide/src-gen/ +apps/ide/gen-esbuild.*.mjs +apps/ide/esbuild.mjs + +# Generated or pinned files +package-lock.json +**/package-lock.json +*.lock.json +apps/web/next-env.d.ts + +# Local state and hand-written prose +.wishforge/ +*.md + +# Sandbox-protected client configuration +**/.mcp.json diff --git a/.prettierrc.json b/.prettierrc.json new file mode 100644 index 0000000..aeed638 --- /dev/null +++ b/.prettierrc.json @@ -0,0 +1,6 @@ +{ + "singleQuote": true, + "printWidth": 120, + "trailingComma": "all", + "arrowParens": "avoid" +} diff --git a/apps/api/package.json b/apps/api/package.json index 1dfaf2a..ddb3c0b 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -1 +1,32 @@ -{"name":"@wishforge/api","version":"0.1.0","private":true,"type":"module","scripts":{"dev":"tsx watch --env-file=../../.env.local src/main.ts","start":"tsx src/main.ts","typecheck":"tsc --noEmit","build":"tsc --noEmit"},"dependencies":{"@modelcontextprotocol/sdk":"^1.26.0","@nestjs/common":"^12.1.3","@nestjs/core":"^12.1.3","@nestjs/platform-express":"^12.1.3","@opentelemetry/api":"1.9.1","@opentelemetry/exporter-trace-otlp-http":"0.223.0","@opentelemetry/sdk-node":"0.223.0","@prometheus-io/client":"0.16.1","@temporalio/client":"^1.13.0","@wishforge/contracts":"*","jose":"^6.0.0","pg":"^8.16.0","reflect-metadata":"^0.2.2","rxjs":"^7.8.2","zod":"^3.25.0"},"devDependencies":{"@types/express":"^5.0.0"}} \ No newline at end of file +{ + "name": "@wishforge/api", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "dev": "tsx watch --env-file=../../.env.local src/main.ts", + "start": "tsx src/main.ts", + "typecheck": "tsc --noEmit", + "build": "tsc --noEmit" + }, + "dependencies": { + "@modelcontextprotocol/sdk": "^1.26.0", + "@nestjs/common": "^12.1.3", + "@nestjs/core": "^12.1.3", + "@nestjs/platform-express": "^12.1.3", + "@opentelemetry/api": "1.9.1", + "@opentelemetry/exporter-trace-otlp-http": "0.223.0", + "@opentelemetry/sdk-node": "0.223.0", + "@prometheus-io/client": "0.16.1", + "@temporalio/client": "^1.13.0", + "@wishforge/contracts": "*", + "jose": "^6.0.0", + "pg": "^8.16.0", + "reflect-metadata": "^0.2.2", + "rxjs": "^7.8.2", + "zod": "^3.25.0" + }, + "devDependencies": { + "@types/express": "^5.0.0" + } +} diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts index 75d8281..223ec9f 100644 --- a/apps/api/src/auth.ts +++ b/apps/api/src/auth.ts @@ -1,4 +1,4 @@ -import {resolveGatewayTarget} from './domain/gateway-target.js'; +import { resolveGatewayTarget } from './domain/gateway-target.js'; import { createRemoteJWKSet, jwtVerify } from 'jose'; import { DomainError, type Actor } from '@wishforge/contracts'; import { sameSecret } from './domain/policy.js'; @@ -7,27 +7,67 @@ import { ServiceTokens } from './service-tokens.js'; export class Authenticator { private jwks; - constructor(private store: Store, private env = process.env) { + constructor( + private store: Store, + private env = process.env, + ) { if (env.OIDC_ISSUER) { let issuer: URL; - try { issuer = new URL(env.OIDC_ISSUER); } catch { throw new Error('OIDC issuer URL is invalid'); } - if (issuer.username || issuer.password || issuer.search || issuer.hash || !['https:', 'http:'].includes(issuer.protocol) || (env.NODE_ENV === 'production' && issuer.protocol !== 'https:')) throw new Error('OIDC issuer must be HTTPS in production and contain no credentials, query or fragment'); + try { + issuer = new URL(env.OIDC_ISSUER); + } catch { + throw new Error('OIDC issuer URL is invalid'); + } + if ( + issuer.username || + issuer.password || + issuer.search || + issuer.hash || + !['https:', 'http:'].includes(issuer.protocol) || + (env.NODE_ENV === 'production' && issuer.protocol !== 'https:') + ) + throw new Error('OIDC issuer must be HTTPS in production and contain no credentials, query or fragment'); this.jwks = createRemoteJWKSet(new URL(`${env.OIDC_ISSUER.replace(/\/$/, '')}/protocol/openid-connect/certs`)); } - if (env.NODE_ENV === 'production' && env.WF_DEV_AUTH_TOKEN) throw new Error('Development authentication cannot be configured in production'); + if (env.NODE_ENV === 'production' && env.WF_DEV_AUTH_TOKEN) + throw new Error('Development authentication cannot be configured in production'); } async authenticate(authorization?: string): Promise { if (!authorization?.startsWith('Bearer ')) throw new DomainError('UNAUTHENTICATED', 'Sign in to continue', 401); const token = authorization.slice(7); - if (this.env.NODE_ENV !== 'production' && this.env.WF_DEV_AUTH_TOKEN && sameSecret(token, this.env.WF_DEV_AUTH_TOKEN)) { + if ( + this.env.NODE_ENV !== 'production' && + this.env.WF_DEV_AUTH_TOKEN && + sameSecret(token, this.env.WF_DEV_AUTH_TOKEN) + ) { return this.member('demo', '11111111-1111-4111-8111-111111111111', 'human', 'wishforge-web'); } - if (!this.jwks || !this.env.OIDC_ISSUER || !this.env.OIDC_AUDIENCE) throw new DomainError('UNAUTHENTICATED', 'OIDC is not configured', 401); + if (!this.jwks || !this.env.OIDC_ISSUER || !this.env.OIDC_AUDIENCE) + throw new DomainError('UNAUTHENTICATED', 'OIDC is not configured', 401); try { - const { payload } = await jwtVerify(token, this.jwks, { issuer: this.env.OIDC_ISSUER, audience: this.env.OIDC_AUDIENCE, algorithms: ['RS256', 'ES256'] }); - if (typeof payload.sub !== 'string' || typeof payload.tenant_id !== 'string' || typeof payload.exp !== 'number' || !Number.isFinite(payload.exp) || payload.exp * 1000 <= Date.now()) throw new Error('Missing identity claims'); + const { payload } = await jwtVerify(token, this.jwks, { + issuer: this.env.OIDC_ISSUER, + audience: this.env.OIDC_AUDIENCE, + algorithms: ['RS256', 'ES256'], + }); + if ( + typeof payload.sub !== 'string' || + typeof payload.tenant_id !== 'string' || + typeof payload.exp !== 'number' || + !Number.isFinite(payload.exp) || + payload.exp * 1000 <= Date.now() + ) + throw new Error('Missing identity claims'); const clientId = typeof payload.azp === 'string' ? payload.azp : undefined; - return { ...await this.member(payload.tenant_id, payload.sub, clientId === this.env.WF_WEB_CLIENT_ID ? 'human' : 'connector', clientId), authExpiresAt: payload.exp * 1000 }; + return { + ...(await this.member( + payload.tenant_id, + payload.sub, + clientId === this.env.WF_WEB_CLIENT_ID ? 'human' : 'connector', + clientId, + )), + authExpiresAt: payload.exp * 1000, + }; } catch (error) { if (error instanceof DomainError) throw error; throw new DomainError('UNAUTHENTICATED', 'Access token is invalid or expired', 401); @@ -39,23 +79,46 @@ export class Authenticator { return { tenantId, userId, role, kind, clientId }; } async internal(authorization: string | undefined, tenant: string | undefined): Promise { - if (!tenant || !(this.env.WF_DISPATCH_TENANTS ?? '').split(',').includes(tenant)) throw new DomainError('FORBIDDEN', 'Service identity is not assigned to this tenant', 403); + if (!tenant || !(this.env.WF_DISPATCH_TENANTS ?? '').split(',').includes(tenant)) + throw new DomainError('FORBIDDEN', 'Service identity is not assigned to this tenant', 403); const token = authorization?.startsWith('Bearer ') ? authorization.slice(7) : ''; const scoped = await new ServiceTokens(this.env.WF_INTERNAL_TOKEN_KEY).verify(token); if (scoped && scoped.tenantId === tenant) { const run = await this.store.transaction(tenant, tx => tx.get('run', scoped.runId!)); - if (run && run.executionGeneration === scoped.credentialId && !['paused', 'canceled', 'succeeded', 'failed'].includes(String(run.status))) return scoped; + if ( + run && + run.executionGeneration === scoped.credentialId && + !['paused', 'canceled', 'succeeded', 'failed'].includes(String(run.status)) + ) + return scoped; throw new DomainError('UNAUTHENTICATED', 'Execution credential was revoked or superseded', 401); } - const gateway=await resolveGatewayTarget(this.env,tenant);if(gateway&&sameSecret(token,gateway.token))return{tenantId:tenant,userId:'workspace-gateway',role:'admin',kind:'worker'}; - for (const [name, kind] of [['WF_WORKER_TOKEN', 'worker'], ['WF_VERIFIER_TOKEN', 'verifier'], ['WF_DEPLOYER_TOKEN', 'deployer'], ['WF_EVALUATOR_TOKEN', 'evaluator']] as const) { - if (this.env[name] && sameSecret(token, this.env[name]!)) return { tenantId: tenant, userId: kind, role: 'admin', kind }; + const gateway = await resolveGatewayTarget(this.env, tenant); + if (gateway && sameSecret(token, gateway.token)) + return { tenantId: tenant, userId: 'workspace-gateway', role: 'admin', kind: 'worker' }; + for (const [name, kind] of [ + ['WF_WORKER_TOKEN', 'worker'], + ['WF_VERIFIER_TOKEN', 'verifier'], + ['WF_DEPLOYER_TOKEN', 'deployer'], + ['WF_EVALUATOR_TOKEN', 'evaluator'], + ] as const) { + if (this.env[name] && sameSecret(token, this.env[name]!)) + return { tenantId: tenant, userId: kind, role: 'admin', kind }; } const { createHash } = await import('node:crypto'); if (token.length >= 32) { const hash = createHash('sha256').update(token).digest('hex'); - const registration = await this.store.transaction(tenant, async tx => (await tx.list('runner')).find(r => r.tokenHash === hash && r.status === 'active')); - if (registration) return { tenantId: tenant, userId: `runner:${registration.environmentId}`, role: 'deployer', kind: 'deployer', clientId: registration.id }; + const registration = await this.store.transaction(tenant, async tx => + (await tx.list('runner')).find(r => r.tokenHash === hash && r.status === 'active'), + ); + if (registration) + return { + tenantId: tenant, + userId: `runner:${registration.environmentId}`, + role: 'deployer', + kind: 'deployer', + clientId: registration.id, + }; } throw new DomainError('UNAUTHENTICATED', 'Invalid service identity', 401); } diff --git a/apps/api/src/db/migrate.ts b/apps/api/src/db/migrate.ts index 40dd749..41b1f52 100644 --- a/apps/api/src/db/migrate.ts +++ b/apps/api/src/db/migrate.ts @@ -3,26 +3,41 @@ import { readFile } from 'node:fs/promises'; import { pathToFileURL } from 'node:url'; export async function migrate(url: string, appPassword: string) { - if (!appPassword || appPassword.length < 16) throw new Error('WF_DATABASE_APP_PASSWORD must be at least 16 characters'); - const c = new pg.Client({ connectionString: url }); await c.connect(); + if (!appPassword || appPassword.length < 16) + throw new Error('WF_DATABASE_APP_PASSWORD must be at least 16 characters'); + const c = new pg.Client({ connectionString: url }); + await c.connect(); try { await c.query('BEGIN'); await c.query('SELECT pg_advisory_xact_lock(9184721)'); await c.query(await readFile(new URL('./001_initial.sql', import.meta.url), 'utf8')); - await c.query("DO $$ BEGIN IF NOT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='wishforge_app') THEN CREATE ROLE wishforge_app LOGIN NOSUPERUSER NOBYPASSRLS; END IF; END $$"); + await c.query( + "DO $$ BEGIN IF NOT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='wishforge_app') THEN CREATE ROLE wishforge_app LOGIN NOSUPERUSER NOBYPASSRLS; END IF; END $$", + ); // Password is supplied out of band and escaped as a SQL literal, never logged. await c.query(`ALTER ROLE wishforge_app PASSWORD '${appPassword.replaceAll("'", "''")}'`); await c.query('GRANT USAGE ON SCHEMA public TO wishforge_app'); await c.query('REVOKE CREATE ON SCHEMA public FROM PUBLIC'); - await c.query('GRANT SELECT,INSERT,UPDATE,DELETE ON tenants,memberships,entities,run_events,idempotency,outbox,usage_ledger TO wishforge_app'); + await c.query( + 'GRANT SELECT,INSERT,UPDATE,DELETE ON tenants,memberships,entities,run_events,idempotency,outbox,usage_ledger TO wishforge_app', + ); await c.query('REVOKE INSERT,UPDATE,DELETE ON tenants FROM wishforge_app'); await c.query('GRANT SELECT ON audit_events TO wishforge_app'); await c.query('REVOKE INSERT,UPDATE,DELETE,TRUNCATE ON audit_events FROM wishforge_app'); await c.query('COMMIT'); - } catch (error) { await c.query('ROLLBACK'); throw error; } finally { await c.end(); } + } catch (error) { + await c.query('ROLLBACK'); + throw error; + } finally { + await c.end(); + } } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { - if (process.env.NODE_ENV !== 'production') { try { process.loadEnvFile('.env.local'); } catch {} } + if (process.env.NODE_ENV !== 'production') { + try { + process.loadEnvFile('.env.local'); + } catch {} + } await migrate(process.env.DATABASE_ADMIN_URL ?? '', process.env.WF_DATABASE_APP_PASSWORD ?? ''); console.log('Database migration applied; runtime role enforces RLS.'); } diff --git a/apps/api/src/db/seed.ts b/apps/api/src/db/seed.ts index 5b2f704..98cf932 100644 --- a/apps/api/src/db/seed.ts +++ b/apps/api/src/db/seed.ts @@ -1,10 +1,18 @@ import pg from 'pg'; if (process.env.NODE_ENV === 'production') throw new Error('Development seed is disabled in production'); -try { process.loadEnvFile('.env.local'); } catch {} +try { + process.loadEnvFile('.env.local'); +} catch {} const c = new pg.Client({ connectionString: process.env.DATABASE_ADMIN_URL }); await c.connect(); try { - await c.query("INSERT INTO tenants(id,name,region) VALUES('demo','Local workspace',$1) ON CONFLICT(id) DO NOTHING", [process.env.WF_REGION ?? 'local']); - await c.query("INSERT INTO memberships(tenant_id,user_id,role) VALUES('demo','11111111-1111-4111-8111-111111111111','owner') ON CONFLICT DO NOTHING"); - console.log('Local tenant and owner seeded. No projects, runs, evidence or deployments are fabricated.'); -} finally { await c.end(); } + await c.query("INSERT INTO tenants(id,name,region) VALUES('demo','Local workspace',$1) ON CONFLICT(id) DO NOTHING", [ + process.env.WF_REGION ?? 'local', + ]); + await c.query( + "INSERT INTO memberships(tenant_id,user_id,role) VALUES('demo','11111111-1111-4111-8111-111111111111','owner') ON CONFLICT DO NOTHING", + ); + console.log('Local tenant and owner seeded. No projects, runs, evidence or deployments are fabricated.'); +} finally { + await c.end(); +} diff --git a/apps/api/src/db/store.ts b/apps/api/src/db/store.ts index 4e0fd12..1432e2d 100644 --- a/apps/api/src/db/store.ts +++ b/apps/api/src/db/store.ts @@ -3,11 +3,53 @@ import pg from 'pg'; import type { Entity, EntityKind, Role, RunEvent, TenantEntitlementSnapshot } from '@wishforge/contracts'; import { auditContext } from '../audit-context.js'; -export interface Tenant { id: string; name: string; maxConcurrentRuns: number; tokenLimit: number; region: string; planId?: string; planVersion?: string; entitlements?: TenantEntitlementSnapshot | null } -export interface Meter { id: string; runId?: string; tokens: number; computeSeconds: number; storageBytes: number; at: string; source?: 'provider_proxy' | 'artifact_service' | 'executor_reported' | 'legacy'; measuredDimensions?: string[] } -export interface Membership { userId: string; role: Role; active: boolean } -export function normalizeMeter(entry: Meter): Meter { const source = entry.source ?? (entry.id.startsWith('provider-proxy:') ? 'provider_proxy' : entry.id.startsWith('storage:artifact-') ? 'artifact_service' : 'executor_reported'); return { ...entry, source, measuredDimensions: entry.measuredDimensions ?? (source === 'provider_proxy' ? ['model_tokens'] : source === 'artifact_service' ? ['artifact_bytes_added'] : []) }; } -export interface OutboxItem { id: string; type: string; payload: Record; attempts: number } +export interface Tenant { + id: string; + name: string; + maxConcurrentRuns: number; + tokenLimit: number; + region: string; + planId?: string; + planVersion?: string; + entitlements?: TenantEntitlementSnapshot | null; +} +export interface Meter { + id: string; + runId?: string; + tokens: number; + computeSeconds: number; + storageBytes: number; + at: string; + source?: 'provider_proxy' | 'artifact_service' | 'executor_reported' | 'legacy'; + measuredDimensions?: string[]; +} +export interface Membership { + userId: string; + role: Role; + active: boolean; +} +export function normalizeMeter(entry: Meter): Meter { + const source = + entry.source ?? + (entry.id.startsWith('provider-proxy:') + ? 'provider_proxy' + : entry.id.startsWith('storage:artifact-') + ? 'artifact_service' + : 'executor_reported'); + return { + ...entry, + source, + measuredDimensions: + entry.measuredDimensions ?? + (source === 'provider_proxy' ? ['model_tokens'] : source === 'artifact_service' ? ['artifact_bytes_added'] : []), + }; +} +export interface OutboxItem { + id: string; + type: string; + payload: Record; + attempts: number; +} export interface Transaction { tenant(): Promise; membership(userId: string): Promise; @@ -27,60 +69,186 @@ export interface Transaction { meter(entry: Meter): Promise; usage(since: string): Promise; } -export interface Store { transaction(tenantId: string, work: (tx: Transaction) => Promise): Promise; close(): Promise } +export interface Store { + transaction(tenantId: string, work: (tx: Transaction) => Promise): Promise; + close(): Promise; +} export class PgStore implements Store { readonly pool: pg.Pool; - constructor(url: string) { this.pool = new pg.Pool({ connectionString: url, max: 15, connectionTimeoutMillis: 5000, idleTimeoutMillis: 30000 }); } + constructor(url: string) { + this.pool = new pg.Pool({ + connectionString: url, + max: 15, + connectionTimeoutMillis: 5000, + idleTimeoutMillis: 30000, + }); + } async assertSafeRole() { const result = await this.pool.query('SELECT rolsuper, rolbypassrls FROM pg_roles WHERE rolname=current_user'); - if (result.rows[0]?.rolsuper || result.rows[0]?.rolbypassrls) throw new Error('DATABASE_URL must use a non-superuser, NOBYPASSRLS application role. Run db:migrate first.'); + if (result.rows[0]?.rolsuper || result.rows[0]?.rolbypassrls) + throw new Error('DATABASE_URL must use a non-superuser, NOBYPASSRLS application role. Run db:migrate first.'); } async transaction(tenantId: string, work: (tx: Transaction) => Promise): Promise { const c = await this.pool.connect(); try { await c.query('BEGIN'); - await c.query("SELECT set_config('app.tenant_id',$1,true), set_config('statement_timeout','10000',true)", [tenantId]); + await c.query("SELECT set_config('app.tenant_id',$1,true), set_config('statement_timeout','10000',true)", [ + tenantId, + ]); await c.query("SELECT set_config('app.actor_id',$1,true)", [auditContext.getStore()?.actorId ?? 'platform']); // Serializes tenant quota/state changes, never external network or model work. await c.query('SELECT pg_advisory_xact_lock(hashtextextended($1,0))', [tenantId]); const value = await work(new PgTransaction(c, tenantId)); await c.query('COMMIT'); return value; - } catch (error) { await c.query('ROLLBACK'); throw error; } - finally { c.release(); } + } catch (error) { + await c.query('ROLLBACK'); + throw error; + } finally { + c.release(); + } + } + async close() { + await this.pool.end(); } - async close() { await this.pool.end(); } } class PgTransaction implements Transaction { - constructor(private c: pg.PoolClient, private tenantId: string) {} + constructor( + private c: pg.PoolClient, + private tenantId: string, + ) {} async tenant(): Promise { - const { rows } = await this.c.query('SELECT id,name,max_concurrent_runs AS "maxConcurrentRuns",token_limit AS "tokenLimit",region,plan_id AS "planId",plan_version AS "planVersion",entitlements FROM tenants WHERE id=$1', [this.tenantId]); + const { rows } = await this.c.query( + 'SELECT id,name,max_concurrent_runs AS "maxConcurrentRuns",token_limit AS "tokenLimit",region,plan_id AS "planId",plan_version AS "planVersion",entitlements FROM tenants WHERE id=$1', + [this.tenantId], + ); if (!rows[0]) throw new Error('Tenant does not exist'); return { ...rows[0], tokenLimit: Number(rows[0].tokenLimit) }; } - async membership(userId: string) { const r = await this.c.query('SELECT role FROM memberships WHERE tenant_id=$1 AND user_id=$2 AND active=true', [this.tenantId, userId]); return r.rows[0]?.role as Role | undefined; } - async members(): Promise { const result = await this.c.query('SELECT user_id AS "userId",role,active FROM memberships WHERE tenant_id=$1 ORDER BY user_id', [this.tenantId]); return result.rows; } - async putMembership(member: Membership) { await this.c.query('INSERT INTO memberships(tenant_id,user_id,role,active) VALUES($1,$2,$3,$4) ON CONFLICT(tenant_id,user_id) DO UPDATE SET role=EXCLUDED.role,active=EXCLUDED.active', [this.tenantId, member.userId, member.role, member.active]); } - async get(kind: EntityKind, id: string): Promise { const r = await this.c.query('SELECT data FROM entities WHERE tenant_id=$1 AND kind=$2 AND id=$3', [this.tenantId, kind, id]); return r.rows[0]?.data; } - async list(kind: EntityKind): Promise { const r = await this.c.query('SELECT data FROM entities WHERE tenant_id=$1 AND kind=$2 ORDER BY created_at DESC', [this.tenantId, kind]); return r.rows.map(x => x.data); } + async membership(userId: string) { + const r = await this.c.query('SELECT role FROM memberships WHERE tenant_id=$1 AND user_id=$2 AND active=true', [ + this.tenantId, + userId, + ]); + return r.rows[0]?.role as Role | undefined; + } + async members(): Promise { + const result = await this.c.query( + 'SELECT user_id AS "userId",role,active FROM memberships WHERE tenant_id=$1 ORDER BY user_id', + [this.tenantId], + ); + return result.rows; + } + async putMembership(member: Membership) { + await this.c.query( + 'INSERT INTO memberships(tenant_id,user_id,role,active) VALUES($1,$2,$3,$4) ON CONFLICT(tenant_id,user_id) DO UPDATE SET role=EXCLUDED.role,active=EXCLUDED.active', + [this.tenantId, member.userId, member.role, member.active], + ); + } + async get(kind: EntityKind, id: string): Promise { + const r = await this.c.query('SELECT data FROM entities WHERE tenant_id=$1 AND kind=$2 AND id=$3', [ + this.tenantId, + kind, + id, + ]); + return r.rows[0]?.data; + } + async list(kind: EntityKind): Promise { + const r = await this.c.query('SELECT data FROM entities WHERE tenant_id=$1 AND kind=$2 ORDER BY created_at DESC', [ + this.tenantId, + kind, + ]); + return r.rows.map(x => x.data); + } async put(kind: EntityKind, value: Entity) { if (value.tenantId !== this.tenantId) throw new Error('Tenant mismatch'); - await this.c.query('INSERT INTO entities(tenant_id,kind,id,version,data,created_at) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(tenant_id,kind,id) DO UPDATE SET version=EXCLUDED.version,data=EXCLUDED.data', [this.tenantId, kind, value.id, value.version, value, value.createdAt]); + await this.c.query( + 'INSERT INTO entities(tenant_id,kind,id,version,data,created_at) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(tenant_id,kind,id) DO UPDATE SET version=EXCLUDED.version,data=EXCLUDED.data', + [this.tenantId, kind, value.id, value.version, value, value.createdAt], + ); } async event(runId: string, type: string, message: string, data?: Record): Promise { const event = { id: randomUUID(), runId, type, message, time: new Date().toISOString(), data }; - const { rows } = await this.c.query('INSERT INTO run_events(tenant_id,id,run_id,seq,event) SELECT $1,$2,$3,COALESCE(MAX(seq),0)+1,$4 FROM run_events WHERE tenant_id=$1 AND run_id=$3 RETURNING seq', [this.tenantId, event.id, runId, event]); + const { rows } = await this.c.query( + 'INSERT INTO run_events(tenant_id,id,run_id,seq,event) SELECT $1,$2,$3,COALESCE(MAX(seq),0)+1,$4 FROM run_events WHERE tenant_id=$1 AND run_id=$3 RETURNING seq', + [this.tenantId, event.id, runId, event], + ); return { ...event, seq: Number(rows[0].seq) }; } - async events(runId: string, after = 0): Promise { const r = await this.c.query('SELECT seq,event FROM run_events WHERE tenant_id=$1 AND run_id=$2 AND seq>$3 ORDER BY seq LIMIT 1000', [this.tenantId, runId, after]); return r.rows.map(x => ({ ...x.event, seq: Number(x.seq) })); } - async idempotency(key: string) { const r = await this.c.query('SELECT digest,response FROM idempotency WHERE tenant_id=$1 AND key=$2', [this.tenantId, key]); return r.rows[0]; } - async remember(key: string, digest: string, response: unknown) { await this.c.query('INSERT INTO idempotency(tenant_id,key,digest,response) VALUES($1,$2,$3,$4)', [this.tenantId, key, digest, JSON.stringify(response)]); } - async enqueue(type: string, payload: Record, id = randomUUID()) { await this.c.query('INSERT INTO outbox(tenant_id,id,type,payload) VALUES($1,$2,$3,$4) ON CONFLICT(tenant_id,id) DO NOTHING', [this.tenantId, id, type, payload]); } - async outbox(): Promise { const r = await this.c.query("SELECT id,type,payload,attempts FROM outbox WHERE tenant_id=$1 AND delivered_at IS NULL AND next_attempt_at<=now() AND type IN ('run.start','run.signal','workspace.provision','workspace.reconcile') ORDER BY CASE WHEN type='run.signal' THEN 0 WHEN type='workspace.reconcile' THEN 1 ELSE 2 END,created_at LIMIT 50", [this.tenantId]); return r.rows; } - async acknowledge(id: string) { await this.c.query('UPDATE outbox SET delivered_at=now() WHERE tenant_id=$1 AND id=$2', [this.tenantId, id]); } - async defer(id: string, seconds: number) { await this.c.query("UPDATE outbox SET attempts=attempts+1,next_attempt_at=now()+$3*interval '1 second' WHERE tenant_id=$1 AND id=$2", [this.tenantId,id,seconds]); } - async meter(entry: Meter) { const measured = normalizeMeter(entry); await this.c.query('INSERT INTO usage_ledger(tenant_id,id,run_id,tokens,compute_seconds,storage_bytes,created_at,measurement_source,measured_dimensions) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT(tenant_id,id) DO NOTHING', [this.tenantId, entry.id, entry.runId, entry.tokens, entry.computeSeconds, entry.storageBytes, entry.at, measured.source, JSON.stringify(measured.measuredDimensions)]); } - async usage(since: string): Promise { const r = await this.c.query('SELECT id,run_id AS "runId",tokens,compute_seconds AS "computeSeconds",storage_bytes AS "storageBytes",created_at AS at,measurement_source AS source,measured_dimensions AS "measuredDimensions" FROM usage_ledger WHERE tenant_id=$1 AND created_at >= $2 ORDER BY created_at,id', [this.tenantId, since]); return r.rows.map(x => ({ ...x, at: new Date(x.at).toISOString(), tokens: Number(x.tokens), storageBytes: Number(x.storageBytes), computeSeconds: Number(x.computeSeconds) })); } + async events(runId: string, after = 0): Promise { + const r = await this.c.query( + 'SELECT seq,event FROM run_events WHERE tenant_id=$1 AND run_id=$2 AND seq>$3 ORDER BY seq LIMIT 1000', + [this.tenantId, runId, after], + ); + return r.rows.map(x => ({ ...x.event, seq: Number(x.seq) })); + } + async idempotency(key: string) { + const r = await this.c.query('SELECT digest,response FROM idempotency WHERE tenant_id=$1 AND key=$2', [ + this.tenantId, + key, + ]); + return r.rows[0]; + } + async remember(key: string, digest: string, response: unknown) { + await this.c.query('INSERT INTO idempotency(tenant_id,key,digest,response) VALUES($1,$2,$3,$4)', [ + this.tenantId, + key, + digest, + JSON.stringify(response), + ]); + } + async enqueue(type: string, payload: Record, id = randomUUID()) { + await this.c.query( + 'INSERT INTO outbox(tenant_id,id,type,payload) VALUES($1,$2,$3,$4) ON CONFLICT(tenant_id,id) DO NOTHING', + [this.tenantId, id, type, payload], + ); + } + async outbox(): Promise { + const r = await this.c.query( + "SELECT id,type,payload,attempts FROM outbox WHERE tenant_id=$1 AND delivered_at IS NULL AND next_attempt_at<=now() AND type IN ('run.start','run.signal','workspace.provision','workspace.reconcile') ORDER BY CASE WHEN type='run.signal' THEN 0 WHEN type='workspace.reconcile' THEN 1 ELSE 2 END,created_at LIMIT 50", + [this.tenantId], + ); + return r.rows; + } + async acknowledge(id: string) { + await this.c.query('UPDATE outbox SET delivered_at=now() WHERE tenant_id=$1 AND id=$2', [this.tenantId, id]); + } + async defer(id: string, seconds: number) { + await this.c.query( + "UPDATE outbox SET attempts=attempts+1,next_attempt_at=now()+$3*interval '1 second' WHERE tenant_id=$1 AND id=$2", + [this.tenantId, id, seconds], + ); + } + async meter(entry: Meter) { + const measured = normalizeMeter(entry); + await this.c.query( + 'INSERT INTO usage_ledger(tenant_id,id,run_id,tokens,compute_seconds,storage_bytes,created_at,measurement_source,measured_dimensions) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9) ON CONFLICT(tenant_id,id) DO NOTHING', + [ + this.tenantId, + entry.id, + entry.runId, + entry.tokens, + entry.computeSeconds, + entry.storageBytes, + entry.at, + measured.source, + JSON.stringify(measured.measuredDimensions), + ], + ); + } + async usage(since: string): Promise { + const r = await this.c.query( + 'SELECT id,run_id AS "runId",tokens,compute_seconds AS "computeSeconds",storage_bytes AS "storageBytes",created_at AS at,measurement_source AS source,measured_dimensions AS "measuredDimensions" FROM usage_ledger WHERE tenant_id=$1 AND created_at >= $2 ORDER BY created_at,id', + [this.tenantId, since], + ); + return r.rows.map(x => ({ + ...x, + at: new Date(x.at).toISOString(), + tokens: Number(x.tokens), + storageBytes: Number(x.storageBytes), + computeSeconds: Number(x.computeSeconds), + })); + } } diff --git a/apps/api/src/dispatcher.ts b/apps/api/src/dispatcher.ts index 7eb8495..cdf7f58 100644 --- a/apps/api/src/dispatcher.ts +++ b/apps/api/src/dispatcher.ts @@ -1,4 +1,4 @@ -import {resolveGatewayTarget} from './domain/gateway-target.js'; +import { resolveGatewayTarget } from './domain/gateway-target.js'; import { Client, Connection, WorkflowIdReusePolicy } from '@temporalio/client'; import type { ControlPlane } from './domain/control-plane.js'; import { ServiceTokens } from './service-tokens.js'; @@ -15,31 +15,105 @@ export class Dispatcher { async start() { while (!this.stopped) { try { - this.connection ??= await Connection.connect({ address: this.control.env.TEMPORAL_ADDRESS ?? '127.0.0.1:7233' }); - const client = new Client({ connection: this.connection, namespace: this.control.env.TEMPORAL_NAMESPACE ?? 'default' }); - for (const tenant of (this.control.env.WF_DISPATCH_TENANTS ?? '').split(',').map(value => value.trim()).filter(Boolean)) { + this.connection ??= await Connection.connect({ + address: this.control.env.TEMPORAL_ADDRESS ?? '127.0.0.1:7233', + }); + const client = new Client({ + connection: this.connection, + namespace: this.control.env.TEMPORAL_NAMESPACE ?? 'default', + }); + for (const tenant of (this.control.env.WF_DISPATCH_TENANTS ?? '') + .split(',') + .map(value => value.trim()) + .filter(Boolean)) { // Tenant configuration failures must not tear down Temporal or prevent // cancellation signals for already-running work. let provisioner: ProvisionerTarget | undefined; let provisionerError: unknown; - try { provisioner = await resolveProvisionerTarget(this.control.env, tenant); } - catch (error) { provisionerError = error; } + try { + provisioner = await resolveProvisionerTarget(this.control.env, tenant); + } catch (error) { + provisionerError = error; + } const pending = await this.control.store.transaction(tenant, tx => tx.outbox()); for (const item of pending) { if (item.type === 'workspace.provision') { try { - if (provisionerError) throw provisionerError; - const gateway=await resolveGatewayTarget(this.control.env,tenant);if(!gateway){await this.control.store.transaction(tenant,tx=>tx.defer(item.id,60));continue;} - const currentWorkspace=await this.control.store.transaction(tenant,tx=>must(tx,'workspace',String(item.payload.workspaceId))); - if(currentWorkspace.lifecycle){await this.control.store.transaction(tenant,tx=>tx.acknowledge(item.id));continue;} - if (!provisioner) { await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); continue; } - const response = await fetch(`${provisioner.url}/workspaces`, { method: 'POST', headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ tenantId: tenant, projectId: item.payload.projectId, workspaceId: item.payload.workspaceId }), signal: AbortSignal.timeout(30000) }); + if (provisionerError) throw provisionerError; + const gateway = await resolveGatewayTarget(this.control.env, tenant); + if (!gateway) { + await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); + continue; + } + const currentWorkspace = await this.control.store.transaction(tenant, tx => + must(tx, 'workspace', String(item.payload.workspaceId)), + ); + if (currentWorkspace.lifecycle) { + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } + if (!provisioner) { + await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); + continue; + } + const response = await fetch(`${provisioner.url}/workspaces`, { + method: 'POST', + headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, + body: JSON.stringify({ + tenantId: tenant, + projectId: item.payload.projectId, + workspaceId: item.payload.workspaceId, + }), + signal: AbortSignal.timeout(30000), + }); if (!response.ok) throw new Error('WorkspaceProvisioningFailed'); - const state = await response.json() as { status: 'ready' | 'pending'; upstreamUrl: string; collaborationUpstreamUrl?: string }; + const state = (await response.json()) as { + status: 'ready' | 'pending'; + upstreamUrl: string; + collaborationUpstreamUrl?: string; + }; if (!['ready', 'pending'].includes(state.status)) throw new Error('InvalidWorkspaceReceipt'); - await this.control.store.transaction(tenant, async tx => { const w = await must(tx, 'workspace', String(item.payload.workspaceId)); if(w.lifecycle){await tx.acknowledge(item.id);return;} w.managedBy='wishforge-provisioner'; if(state.status==='ready')w.lastActivityAt=new Date().toISOString(); w.status = state.status === 'ready' ? 'ready' : 'requested'; w.reason = state.status === 'ready' ? undefined : 'Waiting for the isolated Theia instance to become ready.'; if (state.status === 'ready') { const target = new URL(state.upstreamUrl); if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) throw new Error('InvalidWorkspaceTarget'); w.upstreamUrl = target.toString(); if (state.collaborationUpstreamUrl) { const collaboration = new URL(state.collaborationUpstreamUrl); if (collaboration.hostname !== target.hostname || collaboration.port !== '8100' || collaboration.username || collaboration.password || collaboration.pathname !== '/' || collaboration.search || collaboration.hash) throw new Error('InvalidCollaborationTarget'); w.collaborationUpstreamUrl = collaboration.toString(); } w.url = `${gateway.publicUrl}/workspaces/${w.id}/`; await tx.acknowledge(item.id); } await tx.put('workspace', touch(w)); }); - if (state.status === 'pending') await this.control.store.transaction(tenant, tx => tx.defer(item.id, 15)); - } catch (error) { await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); console.error(`Workspace provisioning pending: ${error instanceof Error ? error.name : 'failure'}`); } + await this.control.store.transaction(tenant, async tx => { + const w = await must(tx, 'workspace', String(item.payload.workspaceId)); + if (w.lifecycle) { + await tx.acknowledge(item.id); + return; + } + w.managedBy = 'wishforge-provisioner'; + if (state.status === 'ready') w.lastActivityAt = new Date().toISOString(); + w.status = state.status === 'ready' ? 'ready' : 'requested'; + w.reason = + state.status === 'ready' ? undefined : 'Waiting for the isolated Theia instance to become ready.'; + if (state.status === 'ready') { + const target = new URL(state.upstreamUrl); + if (!['http:', 'https:'].includes(target.protocol) || target.username || target.password) + throw new Error('InvalidWorkspaceTarget'); + w.upstreamUrl = target.toString(); + if (state.collaborationUpstreamUrl) { + const collaboration = new URL(state.collaborationUpstreamUrl); + if ( + collaboration.hostname !== target.hostname || + collaboration.port !== '8100' || + collaboration.username || + collaboration.password || + collaboration.pathname !== '/' || + collaboration.search || + collaboration.hash + ) + throw new Error('InvalidCollaborationTarget'); + w.collaborationUpstreamUrl = collaboration.toString(); + } + w.url = `${gateway.publicUrl}/workspaces/${w.id}/`; + await tx.acknowledge(item.id); + } + await tx.put('workspace', touch(w)); + }); + if (state.status === 'pending') + await this.control.store.transaction(tenant, tx => tx.defer(item.id, 15)); + } catch (error) { + await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); + console.error(`Workspace provisioning pending: ${error instanceof Error ? error.name : 'failure'}`); + } continue; } if (item.type !== 'run.start' && item.type !== 'run.signal') continue; @@ -48,44 +122,170 @@ export class Dispatcher { if (item.type === 'run.start') { const runId = String(item.payload.runId); const run = await this.control.store.transaction(tenant, tx => must(tx, 'run', runId)); - if (run.pendingStartId && run.pendingStartId !== item.id) { await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); continue; } - if (['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)) { await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); continue; } - if (Number(item.payload.revision) !== run.revision) { await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); continue; } - const provisioningReasons = ['sandbox_provisioner','resource_signer_unconfigured','runtime_image_unconfigured','runtime_image_migration_required','resource_configuration_invalid','resource_plan_changed','resource_previous_generation']; - if (run.status === 'waiting' && run.executionGeneration === item.id && !provisioningReasons.includes(run.waitingReason ?? '')) { await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); continue; } + if (run.pendingStartId && run.pendingStartId !== item.id) { + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } + if (['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)) { + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } + if (Number(item.payload.revision) !== run.revision) { + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } + const provisioningReasons = [ + 'sandbox_provisioner', + 'resource_signer_unconfigured', + 'runtime_image_unconfigured', + 'runtime_image_migration_required', + 'resource_configuration_invalid', + 'resource_plan_changed', + 'resource_previous_generation', + ]; + if ( + run.status === 'waiting' && + run.executionGeneration === item.id && + !provisioningReasons.includes(run.waitingReason ?? '') + ) { + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } if (provisionerError) throw provisionerError; if (!provisioner) { - await this.control.store.transaction(tenant, async tx => { const current = await must(tx, 'run', runId); if (current.waitingReason !== 'sandbox_provisioner') { current.status = 'waiting'; current.waitingReason = 'sandbox_provisioner'; await tx.put('run', touch(current)); await tx.event(runId, 'run.waiting', 'An isolated per-run sandbox provisioner must be connected before execution.'); } }); + await this.control.store.transaction(tenant, async tx => { + const current = await must(tx, 'run', runId); + if (current.waitingReason !== 'sandbox_provisioner') { + current.status = 'waiting'; + current.waitingReason = 'sandbox_provisioner'; + await tx.put('run', touch(current)); + await tx.event( + runId, + 'run.waiting', + 'An isolated per-run sandbox provisioner must be connected before execution.', + ); + } + }); await this.control.store.transaction(tenant, tx => tx.defer(item.id, 60)); continue; } const tokens = new ServiceTokens(this.control.env.WF_INTERNAL_TOKEN_KEY); - const resourceGrant = await this.control.store.transaction(tenant, tx => reserveExecutionGrant(tx, runId, item.id, this.control.env)); + const resourceGrant = await this.control.store.transaction(tenant, tx => + reserveExecutionGrant(tx, runId, item.id, this.control.env), + ); let provision: Response; - try { provision = await fetch(`${provisioner.url}/runs`, { method: 'POST', headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ tenantId: tenant, runId, generation: item.id, resourceGrant, executorToken: await tokens.issue(tenant, runId, 'worker', item.id), verifierToken: await tokens.issue(tenant, runId, 'verifier', item.id) }), signal: AbortSignal.timeout(15000) }); } - catch { await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); throw new Error('SandboxProvisioningUnknown'); } - if (!provision.ok) { const body = await provision.json().catch(()=>({})) as {error?:string}; await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); if (body.error === 'RUNTIME_IMAGE_MIGRATION_REQUIRED') throw new DomainError('RUNTIME_IMAGE_MIGRATION_REQUIRED','Pinned runtime image is outside the provisioner current/previous allowlist; authorize checkpoint migration'); throw new Error('SandboxProvisioningFailed'); } - const receipt = await provision.json() as { status: string; tenantId: string; runId: string; generation: string }; - if (receipt.tenantId !== tenant || receipt.runId !== runId || receipt.generation !== item.id) { await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); throw new Error('SandboxReceiptScopeMismatch'); } - if (receipt.status !== 'provisioned') { await this.control.waitRun({ tenantId: tenant, userId: 'worker', role: 'admin', kind: 'worker' }, runId, 'sandbox_failed: the prior sandbox attempt ended; resume explicitly to create a new generation.', item.id); await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); continue; } + try { + provision = await fetch(`${provisioner.url}/runs`, { + method: 'POST', + headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, + body: JSON.stringify({ + tenantId: tenant, + runId, + generation: item.id, + resourceGrant, + executorToken: await tokens.issue(tenant, runId, 'worker', item.id), + verifierToken: await tokens.issue(tenant, runId, 'verifier', item.id), + }), + signal: AbortSignal.timeout(15000), + }); + } catch { + await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); + throw new Error('SandboxProvisioningUnknown'); + } + if (!provision.ok) { + const body = (await provision.json().catch(() => ({}))) as { error?: string }; + await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); + if (body.error === 'RUNTIME_IMAGE_MIGRATION_REQUIRED') + throw new DomainError( + 'RUNTIME_IMAGE_MIGRATION_REQUIRED', + 'Pinned runtime image is outside the provisioner current/previous allowlist; authorize checkpoint migration', + ); + throw new Error('SandboxProvisioningFailed'); + } + const receipt = (await provision.json()) as { + status: string; + tenantId: string; + runId: string; + generation: string; + }; + if (receipt.tenantId !== tenant || receipt.runId !== runId || receipt.generation !== item.id) { + await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'unknown')); + throw new Error('SandboxReceiptScopeMismatch'); + } + if (receipt.status !== 'provisioned') { + await this.control.waitRun( + { tenantId: tenant, userId: 'worker', role: 'admin', kind: 'worker' }, + runId, + 'sandbox_failed: the prior sandbox attempt ended; resume explicitly to create a new generation.', + item.id, + ); + await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); + continue; + } await this.control.store.transaction(tenant, tx => recordAllocation(tx, runId, item.id, 'provisioned')); - if (run.status === 'waiting' && provisioningReasons.includes(run.waitingReason ?? '')) await this.control.store.transaction(tenant, async tx => { const current = await must(tx, 'run', runId); if (current.status === 'waiting' && provisioningReasons.includes(current.waitingReason ?? '')) { current.status = 'queued'; current.waitingReason = undefined; await tx.put('run', touch(current)); } }); - await client.workflow.signalWithStart('runWorkflow', { workflowId, taskQueue: 'wishforge-runs', args: [{ tenantId: tenant, runId: item.payload.runId, generation: item.id }], signal: 'resume', signalArgs: [{ generation: item.id }], workflowIdReusePolicy: WorkflowIdReusePolicy.ALLOW_DUPLICATE }); + if (run.status === 'waiting' && provisioningReasons.includes(run.waitingReason ?? '')) + await this.control.store.transaction(tenant, async tx => { + const current = await must(tx, 'run', runId); + if (current.status === 'waiting' && provisioningReasons.includes(current.waitingReason ?? '')) { + current.status = 'queued'; + current.waitingReason = undefined; + await tx.put('run', touch(current)); + } + }); + await client.workflow.signalWithStart('runWorkflow', { + workflowId, + taskQueue: 'wishforge-runs', + args: [{ tenantId: tenant, runId: item.payload.runId, generation: item.id }], + signal: 'resume', + signalArgs: [{ generation: item.id }], + workflowIdReusePolicy: WorkflowIdReusePolicy.ALLOW_DUPLICATE, + }); } else if (item.payload.action === 'cancel' || item.payload.action === 'pause') { - try { await client.workflow.getHandle(workflowId).cancel(); } catch (error) { if (!(error instanceof Error && error.name === 'WorkflowNotFoundError')) throw error; } + try { + await client.workflow.getHandle(workflowId).cancel(); + } catch (error) { + if (!(error instanceof Error && error.name === 'WorkflowNotFoundError')) throw error; + } } else if (item.payload.action === 'completed') { - try { await client.workflow.getHandle(workflowId).signal('resume'); } catch (error) { if (!(error instanceof Error && error.name === 'WorkflowNotFoundError')) throw error; } + try { + await client.workflow.getHandle(workflowId).signal('resume'); + } catch (error) { + if (!(error instanceof Error && error.name === 'WorkflowNotFoundError')) throw error; + } } await this.control.store.transaction(tenant, tx => tx.acknowledge(item.id)); } catch (error) { - if (item.type === 'run.start' && error instanceof DomainError && error.code !== 'EXECUTION_CAPACITY') await this.control.store.transaction(tenant, async tx => { const current = await must(tx, 'run', String(item.payload.runId)); if (!['canceled','failed','succeeded','paused'].includes(current.status)) { current.status='waiting'; current.waitingReason=error.code.toLowerCase(); await tx.put('run',touch(current)); await tx.event(current.id,'run.waiting',error.message); } }); - await this.control.store.transaction(tenant, tx => tx.defer(item.id, Math.min(120, 5 * 2 ** Math.min(item.attempts, 5)))); console.error(`Outbox ${item.type} pending: ${error instanceof DomainError ? error.code : error instanceof Error ? error.name : 'delivery failure'}`); + if (item.type === 'run.start' && error instanceof DomainError && error.code !== 'EXECUTION_CAPACITY') + await this.control.store.transaction(tenant, async tx => { + const current = await must(tx, 'run', String(item.payload.runId)); + if (!['canceled', 'failed', 'succeeded', 'paused'].includes(current.status)) { + current.status = 'waiting'; + current.waitingReason = error.code.toLowerCase(); + await tx.put('run', touch(current)); + await tx.event(current.id, 'run.waiting', error.message); + } + }); + await this.control.store.transaction(tenant, tx => + tx.defer(item.id, Math.min(120, 5 * 2 ** Math.min(item.attempts, 5))), + ); + console.error( + `Outbox ${item.type} pending: ${error instanceof DomainError ? error.code : error instanceof Error ? error.name : 'delivery failure'}`, + ); } } } - } catch (error) { console.error(`Temporal dispatcher reconnecting: ${error instanceof Error ? error.name : 'connection failure'}`); await this.connection?.close(); this.connection = undefined; } + } catch (error) { + console.error( + `Temporal dispatcher reconnecting: ${error instanceof Error ? error.name : 'connection failure'}`, + ); + await this.connection?.close(); + this.connection = undefined; + } await new Promise(resolve => setTimeout(resolve, 2000)); } } - async close() { this.stopped = true; await this.connection?.close(); } + async close() { + this.stopped = true; + await this.connection?.close(); + } } diff --git a/apps/api/src/domain/application-access.ts b/apps/api/src/domain/application-access.ts index 2116edb..953c01b 100644 --- a/apps/api/src/domain/application-access.ts +++ b/apps/api/src/domain/application-access.ts @@ -1,9 +1,99 @@ -import {DomainError,normalizeApplicationUrl,matchesApplicationAccess,type Environment,type Release} from '@wishforge/contracts'; -export function accessConfiguration(environment:Pick,env:NodeJS.ProcessEnv,additionalOrigins:string[]=[]){ - let applicationUrl:string|undefined; - if(environment.applicationUrl){try{applicationUrl=normalizeApplicationUrl(environment.applicationUrl,env.NODE_ENV!=='production'&&!environment.production);}catch{throw new DomainError('APPLICATION_URL_INVALID','Use an external HTTPS application URL without credentials, query or fragment; only development permits loopback HTTP',400);}} - if(applicationUrl){const origin=new URL(applicationUrl).origin;for(const configured of [env.WF_PUBLIC_URL,env.WF_API_PUBLIC_URL,env.WF_IDE_PUBLIC_URL,env.WF_WORKSPACE_GATEWAY_BASE,...additionalOrigins]){if(!configured)continue;let protectedOrigin:string;try{protectedOrigin=new URL(configured).origin;}catch{throw new DomainError('APPLICATION_ORIGIN_CONFIGURATION_INVALID','Platform identity origins must be configured correctly before assigning an application URL',503);}if(origin===protectedOrigin)throw new DomainError('APPLICATION_ORIGIN_CONFLICT','Applications must use an origin separate from the console, API and IDE authentication origins',400);}} - const visibility=environment.visibility??'private';return{applicationUrl,visibility,accessStatus:applicationUrl&&visibility==='public'?'configured' as const:'unconfigured' as const,accessReason:!applicationUrl?'Application URL has not been configured.':visibility==='private'?'Private application identity gateway is not integrated; deployment is blocked until independently validated.':undefined}; +import { + DomainError, + normalizeApplicationUrl, + matchesApplicationAccess, + type Environment, + type Release, +} from '@wishforge/contracts'; +export function accessConfiguration( + environment: Pick, + env: NodeJS.ProcessEnv, + additionalOrigins: string[] = [], +) { + let applicationUrl: string | undefined; + if (environment.applicationUrl) { + try { + applicationUrl = normalizeApplicationUrl( + environment.applicationUrl, + env.NODE_ENV !== 'production' && !environment.production, + ); + } catch { + throw new DomainError( + 'APPLICATION_URL_INVALID', + 'Use an external HTTPS application URL without credentials, query or fragment; only development permits loopback HTTP', + 400, + ); + } + } + if (applicationUrl) { + const origin = new URL(applicationUrl).origin; + for (const configured of [ + env.WF_PUBLIC_URL, + env.WF_API_PUBLIC_URL, + env.WF_IDE_PUBLIC_URL, + env.WF_WORKSPACE_GATEWAY_BASE, + ...additionalOrigins, + ]) { + if (!configured) continue; + let protectedOrigin: string; + try { + protectedOrigin = new URL(configured).origin; + } catch { + throw new DomainError( + 'APPLICATION_ORIGIN_CONFIGURATION_INVALID', + 'Platform identity origins must be configured correctly before assigning an application URL', + 503, + ); + } + if (origin === protectedOrigin) + throw new DomainError( + 'APPLICATION_ORIGIN_CONFLICT', + 'Applications must use an origin separate from the console, API and IDE authentication origins', + 400, + ); + } + } + const visibility = environment.visibility ?? 'private'; + return { + applicationUrl, + visibility, + accessStatus: applicationUrl && visibility === 'public' ? ('configured' as const) : ('unconfigured' as const), + accessReason: !applicationUrl + ? 'Application URL has not been configured.' + : visibility === 'private' + ? 'Private application identity gateway is not integrated; deployment is blocked until independently validated.' + : undefined, + }; +} +export function assertApplicationConfigured( + release: Pick, + env: NodeJS.ProcessEnv, +) { + if (!release.applicationUrl) { + if (env.NODE_ENV === 'production' || release.deliveryRequired) + throw new DomainError( + 'APPLICATION_URL_REQUIRED', + 'Production delivery requires an explicit application URL and access acceptance', + 503, + ); + return; + } + if (release.visibility !== 'public' && !release.privateAccessPolicyDigest) + throw new DomainError( + 'PRIVATE_ACCESS_UNCONFIGURED', + 'A real private application identity gateway is not integrated; private delivery is not enabled', + 503, + ); +} +export function assertApplicationAccepted( + release: Pick, + evidence: unknown, + env: NodeJS.ProcessEnv, +) { + assertApplicationConfigured(release, env); + if (!matchesApplicationAccess(release, evidence)) + throw new DomainError( + 'APPLICATION_ACCESS_REQUIRED', + 'Acceptance must bind the exact public application URL, stable origin and access probe', + ); } -export function assertApplicationConfigured(release:Pick,env:NodeJS.ProcessEnv){if(!release.applicationUrl){if(env.NODE_ENV==='production'||release.deliveryRequired)throw new DomainError('APPLICATION_URL_REQUIRED','Production delivery requires an explicit application URL and access acceptance',503);return;}if(release.visibility!=='public'&&!release.privateAccessPolicyDigest)throw new DomainError('PRIVATE_ACCESS_UNCONFIGURED','A real private application identity gateway is not integrated; private delivery is not enabled',503);} -export function assertApplicationAccepted(release:Pick,evidence:unknown,env:NodeJS.ProcessEnv){assertApplicationConfigured(release,env);if(!matchesApplicationAccess(release,evidence))throw new DomainError('APPLICATION_ACCESS_REQUIRED','Acceptance must bind the exact public application URL, stable origin and access probe');} diff --git a/apps/api/src/domain/artifacts.ts b/apps/api/src/domain/artifacts.ts index dc45eb5..d1ee1f7 100644 --- a/apps/api/src/domain/artifacts.ts +++ b/apps/api/src/domain/artifacts.ts @@ -1,74 +1,249 @@ import { createHash } from 'node:crypto'; import { DomainError, type Actor, type Entity, type Run } from '@wishforge/contracts'; -import { ArtifactError, MAX_ARTIFACT_BYTES, S3Artifacts, sha256, type StoredArtifact } from '../../../../packages/artifacts/src/index.js'; +import { + ArtifactError, + MAX_ARTIFACT_BYTES, + S3Artifacts, + sha256, + type StoredArtifact, +} from '../../../../packages/artifacts/src/index.js'; import type { Store, Transaction } from '../db/store.js'; import { must, record, projectAccess, executionLease, executionScope } from './control-plane.js'; -export interface ArtifactRecord extends Entity { projectId: string; runId: string; kind: 'git-bundle'; sha256: string; size: number; contentType: string; object: StoredArtifact } +export interface ArtifactRecord extends Entity { + projectId: string; + runId: string; + kind: 'git-bundle'; + sha256: string; + size: number; + contentType: string; + object: StoredArtifact; +} export class Artifacts { readonly objects: S3Artifacts; - constructor(private store: Store, private env = process.env, objects?: S3Artifacts) { this.objects = objects ?? new S3Artifacts(env); } + constructor( + private store: Store, + private env = process.env, + objects?: S3Artifacts, + ) { + this.objects = objects ?? new S3Artifacts(env); + } private async authorize(tx: Transaction, actor: Actor, artifact: ArtifactRecord) { if (artifact.tenantId !== actor.tenantId) throw new DomainError('NOT_FOUND', 'Artifact not found', 404); const run = await must(tx, 'run', artifact.runId); - if (run.projectId !== artifact.projectId || artifact.object.runId !== run.id || artifact.object.tenantId !== run.tenantId || artifact.object.projectId !== run.projectId || artifact.object.sha256 !== artifact.sha256 || artifact.object.size !== artifact.size) throw new DomainError('ARTIFACT_SCOPE_MISMATCH', 'Artifact ownership record is inconsistent'); + if ( + run.projectId !== artifact.projectId || + artifact.object.runId !== run.id || + artifact.object.tenantId !== run.tenantId || + artifact.object.projectId !== run.projectId || + artifact.object.sha256 !== artifact.sha256 || + artifact.object.size !== artifact.size + ) + throw new DomainError('ARTIFACT_SCOPE_MISMATCH', 'Artifact ownership record is inconsistent'); if (['human', 'connector'].includes(actor.kind)) await projectAccess(tx, actor, run.projectId); - else { if (!['worker', 'verifier'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'Source artifacts require a project member or assigned execution identity', 403); executionScope(actor, run); } + else { + if (!['worker', 'verifier'].includes(actor.kind)) + throw new DomainError( + 'FORBIDDEN', + 'Source artifacts require a project member or assigned execution identity', + 403, + ); + executionScope(actor, run); + } + } + private visible(record: ArtifactRecord) { + return { + id: record.id, + tenantId: record.tenantId, + projectId: record.projectId, + runId: record.runId, + kind: record.kind, + sha256: record.sha256, + size: record.size, + contentType: record.contentType, + createdAt: record.createdAt, + downloadUrl: `/v1/artifacts/${record.id}/download`, + }; + } + async metadata(actor: Actor, id: string) { + return this.store.transaction(actor.tenantId, async tx => { + const artifact = await must(tx, 'artifact', id); + await this.authorize(tx, actor, artifact); + return this.visible(artifact); + }); } - private visible(record: ArtifactRecord) { return { id: record.id, tenantId: record.tenantId, projectId: record.projectId, runId: record.runId, kind: record.kind, sha256: record.sha256, size: record.size, contentType: record.contentType, createdAt: record.createdAt, downloadUrl: `/v1/artifacts/${record.id}/download` }; } - async metadata(actor: Actor, id: string) { return this.store.transaction(actor.tenantId, async tx => { const artifact = await must(tx, 'artifact', id); await this.authorize(tx, actor, artifact); return this.visible(artifact); }); } async download(actor: Actor, id: string) { - const artifact = await this.store.transaction(actor.tenantId, async tx => { const found = await must(tx, 'artifact', id); await this.authorize(tx, actor, found); return found; }); + const artifact = await this.store.transaction(actor.tenantId, async tx => { + const found = await must(tx, 'artifact', id); + await this.authorize(tx, actor, found); + return found; + }); let body: Buffer; - try { body = await this.objects.get(artifact.object); } catch (error) { throw this.storageError(error); } + try { + body = await this.objects.get(artifact.object); + } catch (error) { + throw this.storageError(error); + } // Recheck membership/revocation and object identity after external I/O. - await this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'artifact', id); await this.authorize(tx, actor, current); if (current.version !== artifact.version || current.sha256 !== artifact.sha256) throw new DomainError('ARTIFACT_CHANGED', 'Artifact record changed while reading'); if (['human', 'connector'].includes(actor.kind) && !await tx.membership(actor.userId)) throw new DomainError('FORBIDDEN', 'Tenant membership was revoked', 403); }); + await this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'artifact', id); + await this.authorize(tx, actor, current); + if (current.version !== artifact.version || current.sha256 !== artifact.sha256) + throw new DomainError('ARTIFACT_CHANGED', 'Artifact record changed while reading'); + if (['human', 'connector'].includes(actor.kind) && !(await tx.membership(actor.userId))) + throw new DomainError('FORBIDDEN', 'Tenant membership was revoked', 403); + }); return { metadata: this.visible(artifact), body }; } - async uploadCheckpointBundle(actor: Actor, runId: string, leaseToken: number, input: Record): Promise> { - if (!['worker', 'verifier'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'Only an assigned worker/verifier can upload checkpoints', 403); - const run = await this.store.transaction(actor.tenantId, async tx => { const found = await must(tx, 'run', runId); executionLease(actor, found, leaseToken); return found; }); + async uploadCheckpointBundle( + actor: Actor, + runId: string, + leaseToken: number, + input: Record, + ): Promise> { + if (!['worker', 'verifier'].includes(actor.kind)) + throw new DomainError('FORBIDDEN', 'Only an assigned worker/verifier can upload checkpoints', 403); + const run = await this.store.transaction(actor.tenantId, async tx => { + const found = await must(tx, 'run', runId); + executionLease(actor, found, leaseToken); + return found; + }); if (input.gitBundleBase64 === undefined) { if (typeof input.gitBundleArtifactId === 'string') { - const artifact = await this.store.transaction(actor.tenantId, async tx => { const value = await must(tx, 'artifact', String(input.gitBundleArtifactId)); await this.authorize(tx, actor, value); return value; }); - if (artifact.runId !== runId || artifact.sha256 !== input.gitBundleSha256) throw new DomainError('ARTIFACT_SCOPE_MISMATCH', 'Checkpoint artifact must belong to this exact run and digest'); + const artifact = await this.store.transaction(actor.tenantId, async tx => { + const value = await must(tx, 'artifact', String(input.gitBundleArtifactId)); + await this.authorize(tx, actor, value); + return value; + }); + if (artifact.runId !== runId || artifact.sha256 !== input.gitBundleSha256) + throw new DomainError( + 'ARTIFACT_SCOPE_MISMATCH', + 'Checkpoint artifact must belong to this exact run and digest', + ); return { ...input }; } - if (this.env.NODE_ENV === 'production') throw new DomainError('CHECKPOINT_ARTIFACT_REQUIRED', 'Production checkpoints require a durable source bundle'); + if (this.env.NODE_ENV === 'production') + throw new DomainError('CHECKPOINT_ARTIFACT_REQUIRED', 'Production checkpoints require a durable source bundle'); return { ...input }; } - if (typeof input.gitBundleBase64 !== 'string' || input.gitBundleBase64.length > Math.ceil(MAX_ARTIFACT_BYTES / 3) * 4 || !/^[A-Za-z0-9+/]*={0,2}$/.test(input.gitBundleBase64) || typeof input.gitBundleSha256 !== 'string' || !/^[a-f0-9]{64}$/.test(input.gitBundleSha256)) throw new DomainError('CHECKPOINT_ARTIFACT_INVALID', 'Checkpoint bundle must be base64 with a SHA-256 hex digest and at most 4 MiB', 400); + if ( + typeof input.gitBundleBase64 !== 'string' || + input.gitBundleBase64.length > Math.ceil(MAX_ARTIFACT_BYTES / 3) * 4 || + !/^[A-Za-z0-9+/]*={0,2}$/.test(input.gitBundleBase64) || + typeof input.gitBundleSha256 !== 'string' || + !/^[a-f0-9]{64}$/.test(input.gitBundleSha256) + ) + throw new DomainError( + 'CHECKPOINT_ARTIFACT_INVALID', + 'Checkpoint bundle must be base64 with a SHA-256 hex digest and at most 4 MiB', + 400, + ); const body = Buffer.from(input.gitBundleBase64, 'base64'); - if (!body.length || body.length > MAX_ARTIFACT_BYTES || body.toString('base64') !== input.gitBundleBase64 || sha256(body) !== input.gitBundleSha256) throw new DomainError('ARTIFACT_DIGEST_MISMATCH', 'Checkpoint bundle bytes do not match their declared digest or size', 400); + if ( + !body.length || + body.length > MAX_ARTIFACT_BYTES || + body.toString('base64') !== input.gitBundleBase64 || + sha256(body) !== input.gitBundleSha256 + ) + throw new DomainError( + 'ARTIFACT_DIGEST_MISMATCH', + 'Checkpoint bundle bytes do not match their declared digest or size', + 400, + ); if (!this.objects.configured()) { - if (this.env.NODE_ENV === 'production') throw new DomainError('ARTIFACT_STORAGE_UNCONFIGURED', 'Production checkpoint storage requires configured S3 and SSE-KMS', 503); + if (this.env.NODE_ENV === 'production') + throw new DomainError( + 'ARTIFACT_STORAGE_UNCONFIGURED', + 'Production checkpoint storage requires configured S3 and SSE-KMS', + 503, + ); return { ...input, artifactStorage: 'inline-development' }; } let object: StoredArtifact; - try { object = await this.objects.put({ tenantId: actor.tenantId, projectId: run.projectId, runId }, body, input.gitBundleSha256, 'application/x-git-bundle'); } catch (error) { throw this.storageError(error); } - const id = `artifact-${createHash('sha256').update(JSON.stringify([actor.tenantId, run.projectId, runId, object.sha256])).digest('hex')}`; + try { + object = await this.objects.put( + { tenantId: actor.tenantId, projectId: run.projectId, runId }, + body, + input.gitBundleSha256, + 'application/x-git-bundle', + ); + } catch (error) { + throw this.storageError(error); + } + const id = `artifact-${createHash('sha256') + .update(JSON.stringify([actor.tenantId, run.projectId, runId, object.sha256])) + .digest('hex')}`; await this.store.transaction(actor.tenantId, async tx => { - const current = await must(tx, 'run', runId); executionLease(actor, current, leaseToken); - if (current.revision !== run.revision) throw new DomainError('STALE_CHECKPOINT', 'Requirement revision changed while uploading'); + const current = await must(tx, 'run', runId); + executionLease(actor, current, leaseToken); + if (current.revision !== run.revision) + throw new DomainError('STALE_CHECKPOINT', 'Requirement revision changed while uploading'); const prior = await tx.get('artifact', id); - if (prior) { await this.authorize(tx, actor, prior); if (prior.sha256 !== object.sha256 || prior.size !== object.size) throw new DomainError('ARTIFACT_IMMUTABILITY_CONFLICT', 'Artifact record does not match its content address'); return; } - const artifact: ArtifactRecord = { ...record(actor.tenantId), id, projectId: run.projectId, runId, kind: 'git-bundle', sha256: object.sha256, size: object.size, contentType: object.contentType, object }; + if (prior) { + await this.authorize(tx, actor, prior); + if (prior.sha256 !== object.sha256 || prior.size !== object.size) + throw new DomainError('ARTIFACT_IMMUTABILITY_CONFLICT', 'Artifact record does not match its content address'); + return; + } + const artifact: ArtifactRecord = { + ...record(actor.tenantId), + id, + projectId: run.projectId, + runId, + kind: 'git-bundle', + sha256: object.sha256, + size: object.size, + contentType: object.contentType, + object, + }; await tx.put('artifact', artifact); - await tx.meter({ id: `storage:${id}`, runId, tokens: 0, computeSeconds: 0, storageBytes: object.size, at: new Date().toISOString() }); - await tx.event(runId, 'artifact.stored', 'Source bundle persisted in private content-addressed object storage.', { artifactId: id, sha256: object.sha256, size: object.size }); + await tx.meter({ + id: `storage:${id}`, + runId, + tokens: 0, + computeSeconds: 0, + storageBytes: object.size, + at: new Date().toISOString(), + }); + await tx.event(runId, 'artifact.stored', 'Source bundle persisted in private content-addressed object storage.', { + artifactId: id, + sha256: object.sha256, + size: object.size, + }); }); const { gitBundleBase64: _, ...checkpoint } = input; return { ...checkpoint, gitBundleArtifactId: id, gitBundleSha256: object.sha256, artifactStorage: 's3' }; } - async hydrateCheckpoint(actor: Actor, runId: string, checkpoint: Record): Promise> { + async hydrateCheckpoint( + actor: Actor, + runId: string, + checkpoint: Record, + ): Promise> { if (!checkpoint.gitBundleArtifactId) { - if (this.env.NODE_ENV === 'production' && checkpoint.gitBundleBase64) throw new DomainError('CHECKPOINT_ARTIFACT_REQUIRED', 'Inline development bundles cannot be used in production'); + if (this.env.NODE_ENV === 'production' && checkpoint.gitBundleBase64) + throw new DomainError( + 'CHECKPOINT_ARTIFACT_REQUIRED', + 'Inline development bundles cannot be used in production', + ); return checkpoint; } - if (typeof checkpoint.gitBundleArtifactId !== 'string') throw new DomainError('CHECKPOINT_ARTIFACT_INVALID', 'Invalid checkpoint artifact reference'); + if (typeof checkpoint.gitBundleArtifactId !== 'string') + throw new DomainError('CHECKPOINT_ARTIFACT_INVALID', 'Invalid checkpoint artifact reference'); const { metadata, body } = await this.download(actor, checkpoint.gitBundleArtifactId); - if (metadata.runId !== runId || metadata.sha256 !== checkpoint.gitBundleSha256) throw new DomainError('ARTIFACT_SCOPE_MISMATCH', 'Checkpoint reference does not match the requested run or digest'); + if (metadata.runId !== runId || metadata.sha256 !== checkpoint.gitBundleSha256) + throw new DomainError( + 'ARTIFACT_SCOPE_MISMATCH', + 'Checkpoint reference does not match the requested run or digest', + ); return { ...checkpoint, gitBundleBase64: body.toString('base64') }; } - private storageError(error: unknown) { return error instanceof ArtifactError ? new DomainError(error.code, error.message, 503) : new DomainError('ARTIFACT_STORAGE_UNAVAILABLE', 'Private artifact storage is unavailable; the checkpoint was not marked durable', 503); } + private storageError(error: unknown) { + return error instanceof ArtifactError + ? new DomainError(error.code, error.message, 503) + : new DomainError( + 'ARTIFACT_STORAGE_UNAVAILABLE', + 'Private artifact storage is unavailable; the checkpoint was not marked durable', + 503, + ); + } } diff --git a/apps/api/src/domain/billing.ts b/apps/api/src/domain/billing.ts index b0e9c32..2f70772 100644 --- a/apps/api/src/domain/billing.ts +++ b/apps/api/src/domain/billing.ts @@ -1,5 +1,11 @@ import { z } from 'zod'; -import { DomainError, type Actor, type BillingInvoice, type BillingUsageLine, type TenantEntitlementSnapshot } from '@wishforge/contracts'; +import { + DomainError, + type Actor, + type BillingInvoice, + type BillingUsageLine, + type TenantEntitlementSnapshot, +} from '@wishforge/contracts'; import type { Meter, Store, Transaction } from '../db/store.js'; import { idempotent, record, touch } from './control-plane.js'; import { digest, requireRole } from './policy.js'; @@ -7,58 +13,275 @@ import { effectiveEntitlements, requireBillingOperator } from './entitlements.js export function billingPeriod(period?: string) { const selected = period ?? new Date().toISOString().slice(0, 7); - if (!/^\d{4}-(0[1-9]|1[0-2])$/.test(selected)) throw new DomainError('INVALID_BILLING_PERIOD', 'Use a UTC calendar month in YYYY-MM format', 400); - const [year, month] = selected.split('-').map(Number); if (year! < 2000 || year! > 9998) throw new DomainError('INVALID_BILLING_PERIOD', 'Billing year is out of range', 400); - return { period: selected, periodStart: new Date(Date.UTC(year!, month! - 1, 1)).toISOString(), periodEnd: new Date(Date.UTC(year!, month!, 1)).toISOString() }; + if (!/^\d{4}-(0[1-9]|1[0-2])$/.test(selected)) + throw new DomainError('INVALID_BILLING_PERIOD', 'Use a UTC calendar month in YYYY-MM format', 400); + const [year, month] = selected.split('-').map(Number); + if (year! < 2000 || year! > 9998) + throw new DomainError('INVALID_BILLING_PERIOD', 'Billing year is out of range', 400); + return { + period: selected, + periodStart: new Date(Date.UTC(year!, month! - 1, 1)).toISOString(), + periodEnd: new Date(Date.UTC(year!, month!, 1)).toISOString(), + }; } function line(entry: Meter): BillingUsageLine { const source = entry.source ?? 'legacy'; - const provider = source === 'provider_proxy', artifact = source === 'artifact_service' && entry.measuredDimensions?.includes('artifact_bytes_added'); - return { id: entry.id, runId: entry.runId, at: entry.at, source, modelTokens: provider || entry.tokens > 0 ? entry.tokens : null, modelMeasurement: provider ? 'provider_reported' : source === 'artifact_service' ? 'not_applicable' : source === 'executor_reported' && entry.tokens > 0 ? 'executor_reported' : 'unknown', reportedComputeSeconds: entry.computeSeconds > 0 ? entry.computeSeconds : null, artifactBytesAdded: artifact ? entry.storageBytes : null, platformComputeMeasurement: 'unmeasured', platformStorageMeasurement: artifact ? 'partial' : 'unmeasured' }; + const provider = source === 'provider_proxy', + artifact = source === 'artifact_service' && entry.measuredDimensions?.includes('artifact_bytes_added'); + return { + id: entry.id, + runId: entry.runId, + at: entry.at, + source, + modelTokens: provider || entry.tokens > 0 ? entry.tokens : null, + modelMeasurement: provider + ? 'provider_reported' + : source === 'artifact_service' + ? 'not_applicable' + : source === 'executor_reported' && entry.tokens > 0 + ? 'executor_reported' + : 'unknown', + reportedComputeSeconds: entry.computeSeconds > 0 ? entry.computeSeconds : null, + artifactBytesAdded: artifact ? entry.storageBytes : null, + platformComputeMeasurement: 'unmeasured', + platformStorageMeasurement: artifact ? 'partial' : 'unmeasured', + }; } export class Billing { - constructor(private store: Store, private env = process.env) {} - private readable(actor: Actor) { requireRole(actor, ['owner', 'admin']); } - private async report(tx: Transaction, period?: string): Promise<{ invoice: BillingInvoice; lines: BillingUsageLine[] }> { - const window = billingPeriod(period), tenant = await tx.tenant(); - const lines = (await tx.usage(window.periodStart)).filter(entry => entry.at < window.periodEnd).map(line).sort((a, b) => a.at.localeCompare(b.at) || a.id.localeCompare(b.id)); - const unresolved = (await tx.list('provider_proxy_request')).filter(request => request.createdAt < window.periodEnd && ['unknown', 'dispatched'].includes(String(request.status)) && request.path === '/responses'); - const tokenLines = lines.filter(entry => entry.modelTokens !== null), artifactLines = lines.filter(entry => entry.artifactBytesAdded !== null); - const hasUnknown = unresolved.length > 0 || lines.some(entry => ['unknown', 'executor_reported'].includes(entry.modelMeasurement)); + constructor( + private store: Store, + private env = process.env, + ) {} + private readable(actor: Actor) { + requireRole(actor, ['owner', 'admin']); + } + private async report( + tx: Transaction, + period?: string, + ): Promise<{ invoice: BillingInvoice; lines: BillingUsageLine[] }> { + const window = billingPeriod(period), + tenant = await tx.tenant(); + const lines = (await tx.usage(window.periodStart)) + .filter(entry => entry.at < window.periodEnd) + .map(line) + .sort((a, b) => a.at.localeCompare(b.at) || a.id.localeCompare(b.id)); + const unresolved = (await tx.list('provider_proxy_request')).filter( + request => + request.createdAt < window.periodEnd && + ['unknown', 'dispatched'].includes(String(request.status)) && + request.path === '/responses', + ); + const tokenLines = lines.filter(entry => entry.modelTokens !== null), + artifactLines = lines.filter(entry => entry.artifactBytesAdded !== null); + const hasUnknown = + unresolved.length > 0 || lines.some(entry => ['unknown', 'executor_reported'].includes(entry.modelMeasurement)); const active = effectiveEntitlements(tenant); - const assignments = (await tx.list('entitlement_event')).filter(event => ['plan.assigned', 'tenant.provisioned'].includes(String(event.action)) && event.createdAt < window.periodEnd && event.entitlements).sort((a, b) => b.createdAt.localeCompare(a.createdAt)); + const assignments = (await tx.list('entitlement_event')) + .filter( + event => + ['plan.assigned', 'tenant.provisioned'].includes(String(event.action)) && + event.createdAt < window.periodEnd && + event.entitlements, + ) + .sort((a, b) => b.createdAt.localeCompare(a.createdAt)); const historical = assignments[0]?.entitlements as TenantEntitlementSnapshot | undefined; - const planId = historical?.planId ?? ((!active.assignedAt || active.assignedAt < window.periodEnd) ? active.planId : 'unknown'); - const planVersion = historical?.planVersion ?? ((!active.assignedAt || active.assignedAt < window.periodEnd) ? active.planVersion : 'unrecorded'); - const summary = { tenantId: tenant.id, ...window, timezone: 'UTC' as const, planId, planVersion, model: { charge: 'external' as const, reportedTokens: tokenLines.length ? tokenLines.reduce((sum, entry) => sum + entry.modelTokens!, 0) : null, measurement: tokenLines.length ? hasUnknown ? 'partial' as const : 'reported' as const : 'unknown' as const, unresolvedRequests: unresolved.length, amount: null }, platform: { computeSeconds: null, computeMeasurement: 'unmeasured' as const, artifactBytesAdded: artifactLines.length ? artifactLines.reduce((sum, entry) => sum + entry.artifactBytesAdded!, 0) : null, storageByteHours: null, storageMeasurement: artifactLines.length ? 'partial' as const : 'unmeasured' as const, amount: null }, currency: null, amountDue: null, lineCount: lines.length }; + const planId = + historical?.planId ?? (!active.assignedAt || active.assignedAt < window.periodEnd ? active.planId : 'unknown'); + const planVersion = + historical?.planVersion ?? + (!active.assignedAt || active.assignedAt < window.periodEnd ? active.planVersion : 'unrecorded'); + const summary = { + tenantId: tenant.id, + ...window, + timezone: 'UTC' as const, + planId, + planVersion, + model: { + charge: 'external' as const, + reportedTokens: tokenLines.length ? tokenLines.reduce((sum, entry) => sum + entry.modelTokens!, 0) : null, + measurement: tokenLines.length + ? hasUnknown + ? ('partial' as const) + : ('reported' as const) + : ('unknown' as const), + unresolvedRequests: unresolved.length, + amount: null, + }, + platform: { + computeSeconds: null, + computeMeasurement: 'unmeasured' as const, + artifactBytesAdded: artifactLines.length + ? artifactLines.reduce((sum, entry) => sum + entry.artifactBytesAdded!, 0) + : null, + storageByteHours: null, + storageMeasurement: artifactLines.length ? ('partial' as const) : ('unmeasured' as const), + amount: null, + }, + currency: null, + amountDue: null, + lineCount: lines.length, + }; const checksum = digest({ summary, lines, unresolvedRequestIds: unresolved.map(request => request.id).sort() }); - const record = await tx.get('billing_period', `billing-${window.period}`); const settlement = record?.settlement as BillingInvoice['settlement']; - const invoice: BillingInvoice = { ...summary, digest: checksum, status: settlement ? settlement.invoiceDigest === checksum ? settlement.status : 'needs_reconciliation' : 'unreviewed', ...(settlement ? { settlement } : {}) }; + const record = await tx.get('billing_period', `billing-${window.period}`); + const settlement = record?.settlement as BillingInvoice['settlement']; + const invoice: BillingInvoice = { + ...summary, + digest: checksum, + status: settlement + ? settlement.invoiceDigest === checksum + ? settlement.status + : 'needs_reconciliation' + : 'unreviewed', + ...(settlement ? { settlement } : {}), + }; return { invoice, lines }; } - async usage(actor: Actor, period?: string) { this.readable(actor); return this.store.transaction(actor.tenantId, async tx => { const result = await this.report(tx, period); return { period: result.invoice.period, periodStart: result.invoice.periodStart, periodEnd: result.invoice.periodEnd, timezone: 'UTC', lines: result.lines, coverage: { model: result.invoice.model.measurement, compute: 'unmeasured', storage: result.invoice.platform.storageMeasurement }, supplierCharges: 'external', priced: false }; }); } - async invoice(actor: Actor, period?: string) { this.readable(actor); return this.store.transaction(actor.tenantId, async tx => (await this.report(tx, period)).invoice); } + async usage(actor: Actor, period?: string) { + this.readable(actor); + return this.store.transaction(actor.tenantId, async tx => { + const result = await this.report(tx, period); + return { + period: result.invoice.period, + periodStart: result.invoice.periodStart, + periodEnd: result.invoice.periodEnd, + timezone: 'UTC', + lines: result.lines, + coverage: { + model: result.invoice.model.measurement, + compute: 'unmeasured', + storage: result.invoice.platform.storageMeasurement, + }, + supplierCharges: 'external', + priced: false, + }; + }); + } + async invoice(actor: Actor, period?: string) { + this.readable(actor); + return this.store.transaction(actor.tenantId, async tx => (await this.report(tx, period)).invoice); + } async exportCsv(actor: Actor, period?: string) { - this.readable(actor); const report = await this.store.transaction(actor.tenantId, tx => this.report(tx, period)); - const cell = (value: unknown) => { let text = value === null || value === undefined ? '' : String(value); if (/^[\s\u0000-\u001f]*[=+\-@]/.test(text) || /^[\t\r\n]/.test(text)) text = `'${text}`; return `"${text.replaceAll('"', '""')}"`; }; - const rows: unknown[][] = [['period', 'entry_id', 'run_id', 'recorded_at', 'source', 'model_tokens_reported', 'model_measurement', 'supplier_charge', 'platform_compute_seconds', 'compute_measurement', 'artifact_bytes_added', 'storage_measurement', 'amount_due', 'currency', 'invoice_status']]; - for (const value of report.lines) rows.push([report.invoice.period, value.id, value.runId, value.at, value.source, value.modelTokens, value.modelMeasurement, 'external', null, 'unmeasured', value.artifactBytesAdded, value.platformStorageMeasurement, null, null, report.invoice.status]); - if (!report.lines.length) rows.push([report.invoice.period, '', '', '', 'no_measurements', null, 'unknown', 'external', null, 'unmeasured', null, 'unmeasured', null, null, report.invoice.status]); - return { filename: `wishforge-invoice-${report.invoice.period}.csv`, content: `\ufeff${rows.map(row => row.map(cell).join(',')).join('\r\n')}\r\n`, digest: report.invoice.digest }; + this.readable(actor); + const report = await this.store.transaction(actor.tenantId, tx => this.report(tx, period)); + const cell = (value: unknown) => { + let text = value === null || value === undefined ? '' : String(value); + if (/^[\s\u0000-\u001f]*[=+\-@]/.test(text) || /^[\t\r\n]/.test(text)) text = `'${text}`; + return `"${text.replaceAll('"', '""')}"`; + }; + const rows: unknown[][] = [ + [ + 'period', + 'entry_id', + 'run_id', + 'recorded_at', + 'source', + 'model_tokens_reported', + 'model_measurement', + 'supplier_charge', + 'platform_compute_seconds', + 'compute_measurement', + 'artifact_bytes_added', + 'storage_measurement', + 'amount_due', + 'currency', + 'invoice_status', + ], + ]; + for (const value of report.lines) + rows.push([ + report.invoice.period, + value.id, + value.runId, + value.at, + value.source, + value.modelTokens, + value.modelMeasurement, + 'external', + null, + 'unmeasured', + value.artifactBytesAdded, + value.platformStorageMeasurement, + null, + null, + report.invoice.status, + ]); + if (!report.lines.length) + rows.push([ + report.invoice.period, + '', + '', + '', + 'no_measurements', + null, + 'unknown', + 'external', + null, + 'unmeasured', + null, + 'unmeasured', + null, + null, + report.invoice.status, + ]); + return { + filename: `wishforge-invoice-${report.invoice.period}.csv`, + content: `\ufeff${rows.map(row => row.map(cell).join(',')).join('\r\n')}\r\n`, + digest: report.invoice.digest, + }; } async settle(actor: Actor, period: string, input: unknown, key?: string): Promise { requireBillingOperator(actor, this.env); - const data = z.object({ action: z.enum(['review', 'record_settled', 'void']), invoiceDigest: z.string().regex(/^sha256:[a-f0-9]{64}$/), reference: z.string().trim().min(3).max(200), note: z.string().trim().min(10).max(2000), acknowledgeUnmeasured: z.literal(true) }).strict().parse(input); - return this.store.transaction(actor.tenantId, tx => idempotent(tx, key, { operation: 'billing.settlement', period, ...data }, async () => { - const { invoice } = await this.report(tx, period); - if (invoice.digest !== data.invoiceDigest) throw new DomainError('INVOICE_CHANGED', 'Usage or entitlement evidence changed; inspect the current invoice before recording settlement'); - if (data.action === 'record_settled' && Date.parse(invoice.periodEnd) > Date.now()) throw new DomainError('BILLING_PERIOD_OPEN', 'Record usage-period settlement only after the UTC month closes'); - const status = data.action === 'review' ? 'reviewed' : data.action === 'record_settled' ? 'settled_recorded' : 'voided'; - const settlement: NonNullable = { status, reference: data.reference, note: data.note, actorId: actor.userId, at: new Date().toISOString(), invoiceDigest: invoice.digest, acknowledgedUnmeasured: true }; - const current = await tx.get('billing_period', `billing-${period}`); const value = current ? touch(current) : { ...record(actor.tenantId), id: `billing-${period}` }; - await tx.put('billing_period', { ...value, period, settlement }); - await tx.put('billing_event', { ...record(actor.tenantId), period, action: data.action, actorId: actor.userId, reference: data.reference, note: data.note, invoiceDigest: invoice.digest, acknowledgedUnmeasured: true, amount: null, currency: null }); - return { ...invoice, status, settlement }; - })); + const data = z + .object({ + action: z.enum(['review', 'record_settled', 'void']), + invoiceDigest: z.string().regex(/^sha256:[a-f0-9]{64}$/), + reference: z.string().trim().min(3).max(200), + note: z.string().trim().min(10).max(2000), + acknowledgeUnmeasured: z.literal(true), + }) + .strict() + .parse(input); + return this.store.transaction(actor.tenantId, tx => + idempotent(tx, key, { operation: 'billing.settlement', period, ...data }, async () => { + const { invoice } = await this.report(tx, period); + if (invoice.digest !== data.invoiceDigest) + throw new DomainError( + 'INVOICE_CHANGED', + 'Usage or entitlement evidence changed; inspect the current invoice before recording settlement', + ); + if (data.action === 'record_settled' && Date.parse(invoice.periodEnd) > Date.now()) + throw new DomainError( + 'BILLING_PERIOD_OPEN', + 'Record usage-period settlement only after the UTC month closes', + ); + const status = + data.action === 'review' ? 'reviewed' : data.action === 'record_settled' ? 'settled_recorded' : 'voided'; + const settlement: NonNullable = { + status, + reference: data.reference, + note: data.note, + actorId: actor.userId, + at: new Date().toISOString(), + invoiceDigest: invoice.digest, + acknowledgedUnmeasured: true, + }; + const current = await tx.get('billing_period', `billing-${period}`); + const value = current ? touch(current) : { ...record(actor.tenantId), id: `billing-${period}` }; + await tx.put('billing_period', { ...value, period, settlement }); + await tx.put('billing_event', { + ...record(actor.tenantId), + period, + action: data.action, + actorId: actor.userId, + reference: data.reference, + note: data.note, + invoiceDigest: invoice.digest, + acknowledgedUnmeasured: true, + amount: null, + currency: null, + }); + return { ...invoice, status, settlement }; + }), + ); } } diff --git a/apps/api/src/domain/connections.ts b/apps/api/src/domain/connections.ts index 3e2717e..2300078 100644 --- a/apps/api/src/domain/connections.ts +++ b/apps/api/src/domain/connections.ts @@ -11,55 +11,137 @@ export class CredentialVault { constructor(private env = process.env) {} async seal(tenantId: string, value: string): Promise { if (this.env.VAULT_ADDR) return this.transit('encrypt', tenantId, value); - if (this.env.NODE_ENV === 'production') throw new DomainError('VAULT_UNCONFIGURED', 'Production requires an external Vault transit service', 503); - const key = this.key(), iv = randomBytes(12), cipher = createCipheriv('aes-256-gcm', key, iv); cipher.setAAD(Buffer.from(tenantId)); - const ciphertext = Buffer.concat([cipher.update(value, 'utf8'), cipher.final()]); return `local:${iv.toString('base64url')}:${cipher.getAuthTag().toString('base64url')}:${ciphertext.toString('base64url')}`; + if (this.env.NODE_ENV === 'production') + throw new DomainError('VAULT_UNCONFIGURED', 'Production requires an external Vault transit service', 503); + const key = this.key(), + iv = randomBytes(12), + cipher = createCipheriv('aes-256-gcm', key, iv); + cipher.setAAD(Buffer.from(tenantId)); + const ciphertext = Buffer.concat([cipher.update(value, 'utf8'), cipher.final()]); + return `local:${iv.toString('base64url')}:${cipher.getAuthTag().toString('base64url')}:${ciphertext.toString('base64url')}`; } async open(tenantId: string, encrypted: string): Promise { if (encrypted.startsWith('vault:')) return this.transit('decrypt', tenantId, encrypted); - if (this.env.NODE_ENV === 'production') throw new DomainError('VAULT_UNCONFIGURED', 'Local credential envelopes are not accepted in production', 503); - const [, iv, tag, payload] = encrypted.split(':'); const decipher = createDecipheriv('aes-256-gcm', this.key(), Buffer.from(iv, 'base64url')); decipher.setAAD(Buffer.from(tenantId)); decipher.setAuthTag(Buffer.from(tag, 'base64url')); + if (this.env.NODE_ENV === 'production') + throw new DomainError('VAULT_UNCONFIGURED', 'Local credential envelopes are not accepted in production', 503); + const [, iv, tag, payload] = encrypted.split(':'); + const decipher = createDecipheriv('aes-256-gcm', this.key(), Buffer.from(iv, 'base64url')); + decipher.setAAD(Buffer.from(tenantId)); + decipher.setAuthTag(Buffer.from(tag, 'base64url')); return Buffer.concat([decipher.update(Buffer.from(payload, 'base64url')), decipher.final()]).toString('utf8'); } - private key() { const hex = this.env.WF_VAULT_KEY ?? ''; if (!/^[0-9a-f]{64}$/i.test(hex)) throw new DomainError('VAULT_UNCONFIGURED', 'Credential encryption has not been configured', 503); return Buffer.from(hex, 'hex'); } + private key() { + const hex = this.env.WF_VAULT_KEY ?? ''; + if (!/^[0-9a-f]{64}$/i.test(hex)) + throw new DomainError('VAULT_UNCONFIGURED', 'Credential encryption has not been configured', 503); + return Buffer.from(hex, 'hex'); + } private async transit(operation: 'encrypt' | 'decrypt', tenant: string, value: string) { - if (!this.env.VAULT_ADDR?.startsWith('https://') || !this.env.VAULT_TOKEN || !this.env.VAULT_TRANSIT_KEY) throw new DomainError('VAULT_UNCONFIGURED', 'Vault HTTPS URL, token and transit key are required', 503); - const response = await fetch(`${this.env.VAULT_ADDR}/v1/transit/${operation}/${encodeURIComponent(this.env.VAULT_TRANSIT_KEY)}`, { method: 'POST', headers: { 'X-Vault-Token': this.env.VAULT_TOKEN, 'Content-Type': 'application/json' }, body: JSON.stringify({ context: Buffer.from(tenant).toString('base64'), ...(operation === 'encrypt' ? { plaintext: Buffer.from(value).toString('base64') } : { ciphertext: value }) }), signal: AbortSignal.timeout(10000) }); + if (!this.env.VAULT_ADDR?.startsWith('https://') || !this.env.VAULT_TOKEN || !this.env.VAULT_TRANSIT_KEY) + throw new DomainError('VAULT_UNCONFIGURED', 'Vault HTTPS URL, token and transit key are required', 503); + const response = await fetch( + `${this.env.VAULT_ADDR}/v1/transit/${operation}/${encodeURIComponent(this.env.VAULT_TRANSIT_KEY)}`, + { + method: 'POST', + headers: { 'X-Vault-Token': this.env.VAULT_TOKEN, 'Content-Type': 'application/json' }, + body: JSON.stringify({ + context: Buffer.from(tenant).toString('base64'), + ...(operation === 'encrypt' ? { plaintext: Buffer.from(value).toString('base64') } : { ciphertext: value }), + }), + signal: AbortSignal.timeout(10000), + }, + ); if (!response.ok) throw new DomainError('VAULT_UNAVAILABLE', 'Credential service is unavailable', 503); - const body = await response.json() as { data: { ciphertext: string; plaintext: string } }; return operation === 'encrypt' ? body.data.ciphertext : Buffer.from(body.data.plaintext, 'base64').toString('utf8'); + const body = (await response.json()) as { data: { ciphertext: string; plaintext: string } }; + return operation === 'encrypt' ? body.data.ciphertext : Buffer.from(body.data.plaintext, 'base64').toString('utf8'); } } -const inputSchema = z.object({ provider: z.enum(providers), label: z.string().trim().min(1).max(120), apiKey: z.string().min(8).max(16000) }).strict(); +const inputSchema = z + .object({ + provider: z.enum(providers), + label: z.string().trim().min(1).max(120), + apiKey: z.string().min(8).max(16000), + }) + .strict(); export class Connections { readonly vault: CredentialVault; - constructor(private store: Store, private env = process.env) { this.vault = new CredentialVault(env); } - list(actor: Actor) { requireRole(actor, ['owner', 'admin']); return this.store.transaction(actor.tenantId, async tx => (await tx.list('provider_connection')).map(({ ciphertext: _, ...publicFields }) => publicFields)); } + constructor( + private store: Store, + private env = process.env, + ) { + this.vault = new CredentialVault(env); + } + list(actor: Actor) { + requireRole(actor, ['owner', 'admin']); + return this.store.transaction(actor.tenantId, async tx => + (await tx.list('provider_connection')).map(({ ciphertext: _, ...publicFields }) => publicFields), + ); + } async create(actor: Actor, input: unknown) { - requireRole(actor, ['owner', 'admin']); const data = inputSchema.parse(input); + requireRole(actor, ['owner', 'admin']); + const data = inputSchema.parse(input); const ciphertext = await this.vault.seal(actor.tenantId, data.apiKey); // Network outside DB transaction. return this.store.transaction(actor.tenantId, async tx => { - for (const old of await tx.list('provider_connection')) if (old.provider === data.provider && old.status === 'active') await tx.put('provider_connection', { ...touch(old), status: 'revoked', ciphertext: null }); - const connection = { ...record(actor.tenantId), provider: data.provider, label: data.label, status: 'active', ciphertext }; - await tx.put('provider_connection', connection); const { ciphertext: _, ...publicFields } = connection; return publicFields; + for (const old of await tx.list('provider_connection')) + if (old.provider === data.provider && old.status === 'active') + await tx.put('provider_connection', { ...touch(old), status: 'revoked', ciphertext: null }); + const connection = { + ...record(actor.tenantId), + provider: data.provider, + label: data.label, + status: 'active', + ciphertext, + }; + await tx.put('provider_connection', connection); + const { ciphertext: _, ...publicFields } = connection; + return publicFields; + }); + } + revoke(actor: Actor, id: string) { + requireRole(actor, ['owner', 'admin']); + return this.store.transaction(actor.tenantId, async tx => { + const c = await must(tx, 'provider_connection', id); + await tx.put('provider_connection', { ...touch(c), status: 'revoked', ciphertext: null }); + return { revoked: true }; }); } - revoke(actor: Actor, id: string) { requireRole(actor, ['owner', 'admin']); return this.store.transaction(actor.tenantId, async tx => { const c = await must(tx, 'provider_connection', id); await tx.put('provider_connection', { ...touch(c), status: 'revoked', ciphertext: null }); return { revoked: true }; }); } async forRun(actor: Actor, run: Run) { if (actor.kind !== 'worker' || actor.tenantId !== run.tenantId || !run.lease) return undefined; const connection = await this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'run', run.id); - if (current.status !== 'running' || !current.lease || Date.parse(current.lease.expiresAt) <= Date.now() || current.lease.token !== run.lease!.token || current.leaseOwnerId !== actor.userId || current.leaseOwnerKind !== actor.kind) return undefined; + if ( + current.status !== 'running' || + !current.lease || + Date.parse(current.lease.expiresAt) <= Date.now() || + current.lease.token !== run.lease!.token || + current.leaseOwnerId !== actor.userId || + current.leaseOwnerKind !== actor.kind + ) + return undefined; executionLease(actor, current, run.lease!.token); return (await tx.list('provider_connection')).find(c => c.provider === current.provider && c.status === 'active'); }); if (!connection?.ciphertext) return undefined; - if (this.env.NODE_ENV === 'production' || this.env.WF_PROVIDER_PROXY_ENABLED === 'true') { if (run.provider !== 'codex') throw new DomainError('PROVIDER_PROXY_UNSUPPORTED', 'This provider has no verified credential-isolation proxy in production', 503); return { ...(await new ProviderProxy(this.store, this.env, { vault: this.vault }).credential(actor, run)), model: typeof run.providerModel === 'string' ? run.providerModel : undefined }; } + if (this.env.NODE_ENV === 'production' || this.env.WF_PROVIDER_PROXY_ENABLED === 'true') { + if (run.provider !== 'codex') + throw new DomainError( + 'PROVIDER_PROXY_UNSUPPORTED', + 'This provider has no verified credential-isolation proxy in production', + 503, + ); + return { + ...(await new ProviderProxy(this.store, this.env, { vault: this.vault }).credential(actor, run)), + model: typeof run.providerModel === 'string' ? run.providerModel : undefined, + }; + } const apiKey = await this.vault.open(actor.tenantId, String(connection.ciphertext)); // Vault I/O runs outside the transaction. Re-check revocation and lease after decrypting. return this.store.transaction(actor.tenantId, async tx => { - const current = await must(tx, 'run', run.id); executionLease(actor, current, run.lease!.token); + const current = await must(tx, 'run', run.id); + executionLease(actor, current, run.lease!.token); const active = await tx.get('provider_connection', connection.id); - if (current.status !== 'running' || active?.status !== 'active' || active.version !== connection.version) return undefined; + if (current.status !== 'running' || active?.status !== 'active' || active.version !== connection.version) + return undefined; return { apiKey }; }); } diff --git a/apps/api/src/domain/control-plane.ts b/apps/api/src/domain/control-plane.ts index fb8c913..ffcb262 100644 --- a/apps/api/src/domain/control-plane.ts +++ b/apps/api/src/domain/control-plane.ts @@ -1,5 +1,24 @@ import { randomUUID } from 'node:crypto'; -import { DomainError, stages, projectInput, wishInput, commandInput, transitionInput, evidenceInput, type Actor, type Entity, type EntityKind, type Project, type Wish, type Run, type Evidence, type Release, type Environment, type ProviderInfo, type UsageSummary } from '@wishforge/contracts'; +import { + DomainError, + stages, + projectInput, + wishInput, + commandInput, + transitionInput, + evidenceInput, + type Actor, + type Entity, + type EntityKind, + type Project, + type Wish, + type Run, + type Evidence, + type Release, + type Environment, + type ProviderInfo, + type UsageSummary, +} from '@wishforge/contracts'; import type { Store, Transaction } from '../db/store.js'; import { checkLease, digest, redact, requireRole } from './policy.js'; import { GitStorage } from './git-storage.js'; @@ -13,46 +32,117 @@ import { assertVerificationProfile, verificationPolicyDigest } from './verificat import { WorkspaceIntegration } from './workspace-integration.js'; export const now = () => new Date().toISOString(); -export function record(tenantId: string): Entity { const time = now(); return { id: randomUUID(), tenantId, version: 1, createdAt: time, updatedAt: time }; } -export function touch(value: T): T { return { ...value, version: value.version + 1, updatedAt: now() }; } -export async function must(tx: Transaction, kind: EntityKind, id: string): Promise { const v = await tx.get(kind, id); if (!v) throw new DomainError('NOT_FOUND', 'Resource not found', 404); return v; } +export function record(tenantId: string): Entity { + const time = now(); + return { id: randomUUID(), tenantId, version: 1, createdAt: time, updatedAt: time }; +} +export function touch(value: T): T { + return { ...value, version: value.version + 1, updatedAt: now() }; +} +export async function must(tx: Transaction, kind: EntityKind, id: string): Promise { + const v = await tx.get(kind, id); + if (!v) throw new DomainError('NOT_FOUND', 'Resource not found', 404); + return v; +} export async function projectAccess(tx: Transaction, actor: Actor, projectId: string): Promise { const p = await must(tx, 'project', projectId); - if (['human', 'connector'].includes(actor.kind) && !['owner', 'admin'].includes(actor.role) && !p.memberIds.includes(actor.userId)) throw new DomainError('NOT_FOUND', 'Project not found', 404); + if ( + ['human', 'connector'].includes(actor.kind) && + !['owner', 'admin'].includes(actor.role) && + !p.memberIds.includes(actor.userId) + ) + throw new DomainError('NOT_FOUND', 'Project not found', 404); return p; } -export async function idempotent(tx: Transaction, key: string | undefined, input: unknown, fn: () => Promise): Promise { - if (!key || key.length < 8 || key.length > 200) throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400); - const hash = digest(input), previous = await tx.idempotency(key); - if (previous) { if (previous.digest !== hash) throw new DomainError('IDEMPOTENCY_CONFLICT', 'This key was already used with a different request'); return previous.response as T; } - const result = await fn(); await tx.remember(key, hash, result); return result; +export async function idempotent( + tx: Transaction, + key: string | undefined, + input: unknown, + fn: () => Promise, +): Promise { + if (!key || key.length < 8 || key.length > 200) + throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400); + const hash = digest(input), + previous = await tx.idempotency(key); + if (previous) { + if (previous.digest !== hash) + throw new DomainError('IDEMPOTENCY_CONFLICT', 'This key was already used with a different request'); + return previous.response as T; + } + const result = await fn(); + await tx.remember(key, hash, result); + return result; } export const terminal = (run: Run) => ['succeeded', 'failed', 'canceled'].includes(run.status); export function executionScope(actor: Actor, run: Run) { - if (!['worker', 'verifier', 'deployer'].includes(actor.kind) || actor.tenantId !== run.tenantId || (actor.runId && actor.runId !== run.id)) throw new DomainError('FORBIDDEN', 'Execution identity is not assigned to this run', 403); - if (actor.credentialId && run.executionGeneration !== actor.credentialId) throw new DomainError('EXECUTION_REVOKED', 'Execution credential was replaced', 403); + if ( + !['worker', 'verifier', 'deployer'].includes(actor.kind) || + actor.tenantId !== run.tenantId || + (actor.runId && actor.runId !== run.id) + ) + throw new DomainError('FORBIDDEN', 'Execution identity is not assigned to this run', 403); + if (actor.credentialId && run.executionGeneration !== actor.credentialId) + throw new DomainError('EXECUTION_REVOKED', 'Execution credential was replaced', 403); } export function executionLease(actor: Actor, run: Run, token: number) { - executionScope(actor, run); checkLease(run, token, Date.now()); - if (run.leaseOwnerId !== actor.userId || run.leaseOwnerKind !== actor.kind) throw new DomainError('LEASE_IDENTITY_MISMATCH', 'The authenticated principal does not own this execution lease', 403); + executionScope(actor, run); + checkLease(run, token, Date.now()); + if (run.leaseOwnerId !== actor.userId || run.leaseOwnerKind !== actor.kind) + throw new DomainError( + 'LEASE_IDENTITY_MISMATCH', + 'The authenticated principal does not own this execution lease', + 403, + ); } export class ControlPlane { readonly artifacts: Artifacts; readonly workspaceIntegration: WorkspaceIntegration; - constructor(readonly store: Store, readonly env = process.env) { this.artifacts = new Artifacts(store, env); this.workspaceIntegration=new WorkspaceIntegration(store,env); } - async projects(actor: Actor) { return this.store.transaction(actor.tenantId, async tx => (await tx.list('project')).filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId))); } + constructor( + readonly store: Store, + readonly env = process.env, + ) { + this.artifacts = new Artifacts(store, env); + this.workspaceIntegration = new WorkspaceIntegration(store, env); + } + async projects(actor: Actor) { + return this.store.transaction(actor.tenantId, async tx => + (await tx.list('project')).filter( + p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId), + ), + ); + } async createProject(actor: Actor, input: unknown) { - requireRole(actor, ['owner', 'admin', 'developer']); const data = projectInput.parse(input); - if (!/^[\w][\w./-]*$/.test(data.targetBranch) || /\.\.|\/\.|\.lock$|\/$|\/\//.test(data.targetBranch)) throw new DomainError('INVALID_BRANCH', 'Invalid target branch', 400); + requireRole(actor, ['owner', 'admin', 'developer']); + const data = projectInput.parse(input); + if (!/^[\w][\w./-]*$/.test(data.targetBranch) || /\.\.|\/\.|\.lock$|\/$|\/\//.test(data.targetBranch)) + throw new DomainError('INVALID_BRANCH', 'Invalid target branch', 400); if (data.repositoryUrl) { - try { validateRepositoryUrl(data.repositoryUrl, { allowLocalHttp: this.env.WF_GIT_ALLOW_LOCAL_HTTP === 'true', production: this.env.NODE_ENV === 'production' }); } - catch (error) { throw new DomainError('INVALID_REPOSITORY', error instanceof Error ? error.message : 'Use an HTTPS repository URL; SSH is not supported', 400); } + try { + validateRepositoryUrl(data.repositoryUrl, { + allowLocalHttp: this.env.WF_GIT_ALLOW_LOCAL_HTTP === 'true', + production: this.env.NODE_ENV === 'production', + }); + } catch (error) { + throw new DomainError( + 'INVALID_REPOSITORY', + error instanceof Error ? error.message : 'Use an HTTPS repository URL; SSH is not supported', + 400, + ); + } } const project = await this.store.transaction(actor.tenantId, async tx => { await requireCapacity(tx, 'project'); - const project = { ...record(actor.tenantId), ...data, memberIds: [actor.userId], autoDeploy: false, repositoryStorage: data.repositoryUrl ? 'external' : 'platform', repositoryStatus: data.repositoryUrl ? 'ready' : 'unconfigured' } as Project; - await tx.put('project', project); return project; + const project = { + ...record(actor.tenantId), + ...data, + memberIds: [actor.userId], + autoDeploy: false, + repositoryStorage: data.repositoryUrl ? 'external' : 'platform', + repositoryStatus: data.repositoryUrl ? 'ready' : 'unconfigured', + } as Project; + await tx.put('project', project); + return project; }); if (!project.repositoryUrl) return this.provisionRepository(actor, project.id); return project; @@ -64,221 +154,701 @@ export class ControlPlane { const git = new GitStorage(this.env); if (!git.configured()) return project; let repository: Awaited>; - try { repository = await git.provision(project); } catch (error) { - return this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'project', projectId); current.repositoryStatus = 'unavailable'; current.repositoryError = error instanceof DomainError ? error.message : 'Repository provisioning failed'; await tx.put('project', touch(current)); return current; }); + try { + repository = await git.provision(project); + } catch (error) { + return this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'project', projectId); + current.repositoryStatus = 'unavailable'; + current.repositoryError = error instanceof DomainError ? error.message : 'Repository provisioning failed'; + await tx.put('project', touch(current)); + return current; + }); } - return this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'project', projectId); if (current.repositoryUrl) return current; Object.assign(current, { repositoryUrl: repository.repositoryUrl, sourceCommit: repository.initialCommit, targetBranch: repository.defaultBranch, repositoryStatus: 'ready', repositoryError: undefined }); await tx.put('project', touch(current)); return current; }); + return this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'project', projectId); + if (current.repositoryUrl) return current; + Object.assign(current, { + repositoryUrl: repository.repositoryUrl, + sourceCommit: repository.initialCommit, + targetBranch: repository.defaultBranch, + repositoryStatus: 'ready', + repositoryError: undefined, + }); + await tx.put('project', touch(current)); + return current; + }); + } + async providers(actor: Actor): Promise { + return this.store.transaction(actor.tenantId, tx => this.providerInfo(tx)); } - async providers(actor: Actor): Promise { return this.store.transaction(actor.tenantId, tx => this.providerInfo(tx)); } async providerInfo(tx: Transaction): Promise { const connections = await tx.list('provider_connection'); const connected = (id: string) => connections.some(c => c.provider === id && c.status === 'active'); - return ([['codex', 'Codex'], ['codebuddy', 'CodeBuddy'], ['workbuddy_cloud', 'WorkBuddy Cloud']] as const).map(([id, name]) => { + return ( + [ + ['codex', 'Codex'], + ['codebuddy', 'CodeBuddy'], + ['workbuddy_cloud', 'WorkBuddy Cloud'], + ] as const + ).map(([id, name]) => { const configured = connected(id); - const productionAllowed = this.env.NODE_ENV !== 'production' || (id === 'codex' && providerProxyConfigured(this.env)); - const enabled = configured && productionAllowed && id !== 'workbuddy_cloud' && (id !== 'codebuddy' || !!this.env.WF_CODEBUDDY_BIN); - const reason = !productionAllowed ? 'Production execution requires a verified credential-isolation proxy and approved model configuration; raw BYOK is never sent to a production job.' : !configured ? 'Connect a tenant-owned provider credential to execute tasks.' : id === 'workbuddy_cloud' ? 'Cloud coding is gated until export, recovery and cancellation contracts are verified.' : id === 'codebuddy' && !this.env.WF_CODEBUDDY_BIN ? 'CodeBuddy executable is not configured on the worker.' : 'Available for worker preflight; account/model access is checked when a task starts.'; - return { id, name, configured, enabled, reason, capabilities: { codeExport: enabled, resume: enabled, cancel: enabled, eventReplay: false, usage: id === 'codex' && enabled, cloud: id === 'workbuddy_cloud' } }; + const productionAllowed = + this.env.NODE_ENV !== 'production' || (id === 'codex' && providerProxyConfigured(this.env)); + const enabled = + configured && + productionAllowed && + id !== 'workbuddy_cloud' && + (id !== 'codebuddy' || !!this.env.WF_CODEBUDDY_BIN); + const reason = !productionAllowed + ? 'Production execution requires a verified credential-isolation proxy and approved model configuration; raw BYOK is never sent to a production job.' + : !configured + ? 'Connect a tenant-owned provider credential to execute tasks.' + : id === 'workbuddy_cloud' + ? 'Cloud coding is gated until export, recovery and cancellation contracts are verified.' + : id === 'codebuddy' && !this.env.WF_CODEBUDDY_BIN + ? 'CodeBuddy executable is not configured on the worker.' + : 'Available for worker preflight; account/model access is checked when a task starts.'; + return { + id, + name, + configured, + enabled, + reason, + capabilities: { + codeExport: enabled, + resume: enabled, + cancel: enabled, + eventReplay: false, + usage: id === 'codex' && enabled, + cloud: id === 'workbuddy_cloud', + }, + }; }); } - async submit(actor: Actor, projectId: string, input: unknown, key?: string, metadata?: { purpose: 'evolution'; noDeployment: true; sourceRunIds: string[]; baselineBundle: string; evolutionScope: 'prompt' | 'orchestration' | 'tool' | 'platform'; metaTaskId: string }) { - requireRole(actor, ['owner', 'admin', 'developer']); const data = wishInput.parse(input); if (metadata && (actor.kind !== 'evaluator' || actor.tenantId !== this.env.WF_SYSTEM_TENANT)) throw new DomainError('FORBIDDEN', 'Evolution metadata is restricted to the configured system evaluator', 403); + async submit( + actor: Actor, + projectId: string, + input: unknown, + key?: string, + metadata?: { + purpose: 'evolution'; + noDeployment: true; + sourceRunIds: string[]; + baselineBundle: string; + evolutionScope: 'prompt' | 'orchestration' | 'tool' | 'platform'; + metaTaskId: string; + }, + ) { + requireRole(actor, ['owner', 'admin', 'developer']); + const data = wishInput.parse(input); + if (metadata && (actor.kind !== 'evaluator' || actor.tenantId !== this.env.WF_SYSTEM_TENANT)) + throw new DomainError('FORBIDDEN', 'Evolution metadata is restricted to the configured system evaluator', 403); this.validateKey(key); return this.store.transaction(actor.tenantId, async tx => { const project = await projectAccess(tx, actor, projectId); - return idempotent(tx, `wish:${actor.userId}:${projectId}:${key ?? ''}`, key ? { projectId, ...data, ...(metadata ? { metadata } : {}) } : await this.missingKey(), async () => { - const tenant = await tx.tenant(); const runs = await tx.list('run'); const usage = await tx.usage(this.period()); - const reserved = runs.reduce((sum, run) => sum + run.reservedTokens, 0); - if (reserved + usage.reduce((n, x) => n + x.tokens, 0) + data.budget.maxTokens > effectiveEntitlements(tenant).limits.monthlyModelTokens) throw new DomainError('QUOTA_EXCEEDED', 'Tenant token reservation limit is exhausted', 429); - const provider = (await this.providerInfo(tx)).find(p => p.id === data.provider)!; - const runnable = provider.enabled && !!project.repositoryUrl; - const wish = { ...record(actor.tenantId), projectId, revision: 1, goal: data.goal, acceptanceCriteria: data.acceptanceCriteria } as Wish; - const identity = record(actor.tenantId); - if (metadata && metadata.baselineBundle !== String((await tx.get('policy', 'evolution-routing-v1'))?.stableVersion ?? 'baseline-v1')) throw new DomainError('BASELINE_CHANGED', 'Meta generation must use the current stable bundle'); - const bundle = metadata ? { version: metadata.baselineBundle, config: await resolveBundle(tx, metadata.baselineBundle), candidateId: undefined } : await selectBundle(tx, { tenantId: actor.tenantId, projectId, runId: identity.id, canaryOptIn: project.canaryOptIn === true, internalProject: actor.tenantId === this.env.WF_SYSTEM_TENANT }); - const run = { ...identity, ...(metadata ?? {}), projectId, wishId: wish.id, revision: 1, ...data, stage: 'clarify', status: runnable ? 'queued' : 'waiting', waitingReason: !provider.enabled ? 'credentials' : !project.repositoryUrl ? 'git_storage' : undefined, pendingStartId: runnable ? randomUUID() : undefined, repairCount: 0, bundleVersion: bundle.version, providerModel: bundle.config.model ?? (data.provider === 'codex' ? this.env.WF_CODEX_DEFAULT_MODEL ?? this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.split(',')[0]?.trim() : undefined), evolutionCandidateId: bundle.candidateId, policyVersion: 'policy-v1', reservedTokens: data.budget.maxTokens, usedTokens: 0, activeMilliseconds: 0 } as Run; - await tx.put('wish', wish); await tx.put('run', run); - await tx.event(run.id, 'wish.submitted', 'Wish accepted and acceptance criteria recorded.', { revision: 1 }); - if (runnable) await tx.enqueue('run.start', { runId: run.id, revision: 1 }, String(run.pendingStartId)); - else await tx.event(run.id, 'run.waiting', !provider.enabled ? provider.reason : 'Provision durable Git storage or connect a repository before execution.', { reason: run.waitingReason }); - return { wish_id: wish.id, run_id: run.id, status_url: `/runs/${run.id}` }; - }); + return idempotent( + tx, + `wish:${actor.userId}:${projectId}:${key ?? ''}`, + key ? { projectId, ...data, ...(metadata ? { metadata } : {}) } : await this.missingKey(), + async () => { + const tenant = await tx.tenant(); + const runs = await tx.list('run'); + const usage = await tx.usage(this.period()); + const reserved = runs.reduce((sum, run) => sum + run.reservedTokens, 0); + if ( + reserved + usage.reduce((n, x) => n + x.tokens, 0) + data.budget.maxTokens > + effectiveEntitlements(tenant).limits.monthlyModelTokens + ) + throw new DomainError('QUOTA_EXCEEDED', 'Tenant token reservation limit is exhausted', 429); + const provider = (await this.providerInfo(tx)).find(p => p.id === data.provider)!; + const runnable = provider.enabled && !!project.repositoryUrl; + const wish = { + ...record(actor.tenantId), + projectId, + revision: 1, + goal: data.goal, + acceptanceCriteria: data.acceptanceCriteria, + } as Wish; + const identity = record(actor.tenantId); + if ( + metadata && + metadata.baselineBundle !== + String((await tx.get('policy', 'evolution-routing-v1'))?.stableVersion ?? 'baseline-v1') + ) + throw new DomainError('BASELINE_CHANGED', 'Meta generation must use the current stable bundle'); + const bundle = metadata + ? { + version: metadata.baselineBundle, + config: await resolveBundle(tx, metadata.baselineBundle), + candidateId: undefined, + } + : await selectBundle(tx, { + tenantId: actor.tenantId, + projectId, + runId: identity.id, + canaryOptIn: project.canaryOptIn === true, + internalProject: actor.tenantId === this.env.WF_SYSTEM_TENANT, + }); + const run = { + ...identity, + ...(metadata ?? {}), + projectId, + wishId: wish.id, + revision: 1, + ...data, + stage: 'clarify', + status: runnable ? 'queued' : 'waiting', + waitingReason: !provider.enabled ? 'credentials' : !project.repositoryUrl ? 'git_storage' : undefined, + pendingStartId: runnable ? randomUUID() : undefined, + repairCount: 0, + bundleVersion: bundle.version, + providerModel: + bundle.config.model ?? + (data.provider === 'codex' + ? (this.env.WF_CODEX_DEFAULT_MODEL ?? this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.split(',')[0]?.trim()) + : undefined), + evolutionCandidateId: bundle.candidateId, + policyVersion: 'policy-v1', + reservedTokens: data.budget.maxTokens, + usedTokens: 0, + activeMilliseconds: 0, + } as Run; + await tx.put('wish', wish); + await tx.put('run', run); + await tx.event(run.id, 'wish.submitted', 'Wish accepted and acceptance criteria recorded.', { revision: 1 }); + if (runnable) await tx.enqueue('run.start', { runId: run.id, revision: 1 }, String(run.pendingStartId)); + else + await tx.event( + run.id, + 'run.waiting', + !provider.enabled + ? provider.reason + : 'Provision durable Git storage or connect a repository before execution.', + { reason: run.waitingReason }, + ); + return { wish_id: wish.id, run_id: run.id, status_url: `/runs/${run.id}` }; + }, + ); + }); + } + private async missingKey(): Promise { + throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide Idempotency-Key', 400); + } + private validateKey(key?: string) { + if (!key || key.length < 8 || key.length > 200) + throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400); + } + async runs(actor: Actor) { + return this.store.transaction(actor.tenantId, async tx => { + const accessible = new Set( + (await tx.list('project')) + .filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId)) + .map(p => p.id), + ); + return (await tx.list('run')).filter(r => accessible.has(r.projectId)); + }); + } + async run(actor: Actor, id: string) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + await projectAccess(tx, actor, run.projectId); + return run; + }); + } + async events(actor: Actor, id: string, after = 0) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + await projectAccess(tx, actor, run.projectId); + return tx.events(id, after); }); } - private async missingKey(): Promise { throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide Idempotency-Key', 400); } - private validateKey(key?: string) { if (!key || key.length < 8 || key.length > 200) throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400); } - async runs(actor: Actor) { return this.store.transaction(actor.tenantId, async tx => { const accessible = new Set((await tx.list('project')).filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId)).map(p => p.id)); return (await tx.list('run')).filter(r => accessible.has(r.projectId)); }); } - async run(actor: Actor, id: string) { return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); await projectAccess(tx, actor, run.projectId); return run; }); } - async events(actor: Actor, id: string, after = 0) { return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); await projectAccess(tx, actor, run.projectId); return tx.events(id, after); }); } async command(actor: Actor, id: string, input: unknown, key?: string) { - requireRole(actor, ['owner', 'admin', 'developer']); const data = commandInput.parse(input); + requireRole(actor, ['owner', 'admin', 'developer']); + const data = commandInput.parse(input); this.validateKey(key); return this.store.transaction(actor.tenantId, async tx => { - let run = await must(tx, 'run', id); const project = await projectAccess(tx, actor, run.projectId); + let run = await must(tx, 'run', id); + const project = await projectAccess(tx, actor, run.projectId); return idempotent(tx, `command:${actor.userId}:${id}:${key}`, data, async () => { if (terminal(run)) throw new DomainError('TERMINAL_RUN', 'Submit a new wish to change a completed task'); - if (run.purpose === 'evolution' && run.generatedEvolutionCandidateId && ['resume', 'answer'].includes(data.action)) throw new DomainError('EVOLUTION_GENERATION_COMPLETE', 'The candidate is already recorded; use its evaluation and promotion actions'); - if (data.action === 'resume' && (run.status === 'queued' || (run.status === 'running' && run.lease && Date.parse(run.lease.expiresAt) > Date.now()))) return run; + if ( + run.purpose === 'evolution' && + run.generatedEvolutionCandidateId && + ['resume', 'answer'].includes(data.action) + ) + throw new DomainError( + 'EVOLUTION_GENERATION_COMPLETE', + 'The candidate is already recorded; use its evaluation and promotion actions', + ); + if ( + data.action === 'resume' && + (run.status === 'queued' || + (run.status === 'running' && run.lease && Date.parse(run.lease.expiresAt) > Date.now())) + ) + return run; this.accountTime(run); - run = touch(run); run.lease = run.lease ? { ...run.lease, token: run.lease.token + 1, expiresAt: now() } : undefined; - if (data.action === 'cancel') { run.status = 'canceled'; if (!run.providerProxyActiveRequest) run.reservedTokens = 0; run.waitingReason = undefined; } - else if (data.action === 'pause') { run.status = 'paused'; run.waitingReason = 'user'; } - else { + run = touch(run); + run.lease = run.lease ? { ...run.lease, token: run.lease.token + 1, expiresAt: now() } : undefined; + if (data.action === 'cancel') { + run.status = 'canceled'; + if (!run.providerProxyActiveRequest) run.reservedTokens = 0; + run.waitingReason = undefined; + } else if (data.action === 'pause') { + run.status = 'paused'; + run.waitingReason = 'user'; + } else { if (data.action === 'answer') { if (!data.message) throw new DomainError('ANSWER_REQUIRED', 'Provide the additional requirement', 400); - const wish = touch(await must(tx, 'wish', run.wishId)); wish.revision += 1; wish.goal = `${wish.goal}\n\n${data.message}`; + const wish = touch(await must(tx, 'wish', run.wishId)); + wish.revision += 1; + wish.goal = `${wish.goal}\n\n${data.message}`; if (data.acceptanceCriteria) wish.acceptanceCriteria = data.acceptanceCriteria; - await tx.put('wish', wish); run.revision = wish.revision; run.goal = wish.goal; run.acceptanceCriteria = wish.acceptanceCriteria; run.stage = 'plan'; run.verifiedCommit = undefined; + await tx.put('wish', wish); + run.revision = wish.revision; + run.goal = wish.goal; + run.acceptanceCriteria = wish.acceptanceCriteria; + run.stage = 'plan'; + run.verifiedCommit = undefined; for (const approval of await tx.list('approval')) { const release = await tx.get('release', String(approval.releaseId)); - if (release?.runId === run.id && ['pending', 'approved'].includes(String(approval.status))) await tx.put('approval', { ...touch(approval), status: 'invalidated' }); + if (release?.runId === run.id && ['pending', 'approved'].includes(String(approval.status))) + await tx.put('approval', { ...touch(approval), status: 'invalidated' }); } } const provider = (await this.providerInfo(tx)).find(p => p.id === run.provider)!; - if (run.activeMilliseconds >= run.budget.maxMinutes * 60_000) throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Active execution time budget is exhausted'); - if (run.usedTokens >= run.budget.maxTokens) throw new DomainError('TOKEN_BUDGET_EXHAUSTED', 'Token budget is exhausted'); + if (run.activeMilliseconds >= run.budget.maxMinutes * 60_000) + throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Active execution time budget is exhausted'); + if (run.usedTokens >= run.budget.maxTokens) + throw new DomainError('TOKEN_BUDGET_EXHAUSTED', 'Token budget is exhausted'); const runnable = provider.enabled && !!project.repositoryUrl; - run.status = runnable ? 'queued' : 'waiting'; run.waitingReason = !provider.enabled ? 'credentials' : !project.repositoryUrl ? 'git_storage' : undefined; - if (runnable) { run.pendingStartId = randomUUID(); await tx.enqueue('run.start', { runId: id, revision: run.revision }, String(run.pendingStartId)); } + run.status = runnable ? 'queued' : 'waiting'; + run.waitingReason = !provider.enabled ? 'credentials' : !project.repositoryUrl ? 'git_storage' : undefined; + if (runnable) { + run.pendingStartId = randomUUID(); + await tx.enqueue('run.start', { runId: id, revision: run.revision }, String(run.pendingStartId)); + } } - await tx.put('run', run); await tx.event(id, `run.${data.action}`, data.message ? redact(data.message) : `Run ${data.action} requested.`, { revision: run.revision }); - await tx.enqueue('run.signal', { runId: id, action: data.action }); return run; + await tx.put('run', run); + await tx.event( + id, + `run.${data.action}`, + data.message ? redact(data.message) : `Run ${data.action} requested.`, + { revision: run.revision }, + ); + await tx.enqueue('run.signal', { runId: id, action: data.action }); + return run; }); }); } async lease(actor: Actor, id: string, workerId: string, generation?: string): Promise { - if (!['worker', 'verifier', 'deployer'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'Execution identity required', 403); + if (!['worker', 'verifier', 'deployer'].includes(actor.kind)) + throw new DomainError('FORBIDDEN', 'Execution identity required', 403); return this.store.transaction(actor.tenantId, async tx => { - let run = await must(tx, 'run', id); const tenant = await tx.tenant(); const time = Date.now(); + let run = await must(tx, 'run', id); + const tenant = await tx.tenant(); + const time = Date.now(); executionScope(actor, run); - if (run.executionGeneration && generation !== run.executionGeneration) throw new DomainError('STALE_GENERATION', 'A newer execution generation superseded this worker'); - if (!['queued', 'running'].includes(run.status)) throw new DomainError('RUN_NOT_RUNNABLE', `Run is ${run.status}`); - if (run.lease && Date.parse(run.lease.expiresAt) > time) { if (run.lease.workerId === workerId && run.leaseOwnerId === actor.userId && run.leaseOwnerKind === actor.kind) return run; throw new DomainError('LEASE_HELD', 'A live worker already owns this run'); } - const occupied = (await tx.list('run')).filter(r => r.id !== id && r.status === 'running' && r.lease && Date.parse(r.lease.expiresAt) > time).length; - if (occupied >= effectiveEntitlements(tenant).limits.maxConcurrentRuns) throw new DomainError('CONCURRENCY_LIMIT', 'Tenant execution slots are occupied', 429); - this.accountTime(run); if (run.activeMilliseconds >= run.budget.maxMinutes * 60000) throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Run time limit reached'); - if (run.usedTokens >= run.budget.maxTokens) throw new DomainError('TOKEN_BUDGET_EXHAUSTED', 'Token budget is exhausted'); - run = touch(run); run.status = 'running'; run.lastActiveAt = now(); run.lease = { token: (run.lease?.token ?? 0) + 1, workerId, expiresAt: new Date(time + 60000).toISOString() }; - run.leaseOwnerId = actor.userId; run.leaseOwnerKind = actor.kind; - await tx.put('run', run); await tx.event(id, 'run.leased', 'Worker acquired an execution lease.', { fencingToken: run.lease.token }); return run; + if (run.executionGeneration && generation !== run.executionGeneration) + throw new DomainError('STALE_GENERATION', 'A newer execution generation superseded this worker'); + if (!['queued', 'running'].includes(run.status)) + throw new DomainError('RUN_NOT_RUNNABLE', `Run is ${run.status}`); + if (run.lease && Date.parse(run.lease.expiresAt) > time) { + if (run.lease.workerId === workerId && run.leaseOwnerId === actor.userId && run.leaseOwnerKind === actor.kind) + return run; + throw new DomainError('LEASE_HELD', 'A live worker already owns this run'); + } + const occupied = (await tx.list('run')).filter( + r => r.id !== id && r.status === 'running' && r.lease && Date.parse(r.lease.expiresAt) > time, + ).length; + if (occupied >= effectiveEntitlements(tenant).limits.maxConcurrentRuns) + throw new DomainError('CONCURRENCY_LIMIT', 'Tenant execution slots are occupied', 429); + this.accountTime(run); + if (run.activeMilliseconds >= run.budget.maxMinutes * 60000) + throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Run time limit reached'); + if (run.usedTokens >= run.budget.maxTokens) + throw new DomainError('TOKEN_BUDGET_EXHAUSTED', 'Token budget is exhausted'); + run = touch(run); + run.status = 'running'; + run.lastActiveAt = now(); + run.lease = { token: (run.lease?.token ?? 0) + 1, workerId, expiresAt: new Date(time + 60000).toISOString() }; + run.leaseOwnerId = actor.userId; + run.leaseOwnerKind = actor.kind; + await tx.put('run', run); + await tx.event(id, 'run.leased', 'Worker acquired an execution lease.', { fencingToken: run.lease.token }); + return run; }); } async heartbeat(actor: Actor, id: string, token: number) { return this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', id); executionLease(actor, run, token); if (run.status !== 'running') throw new DomainError('RUN_NOT_RUNNING', 'Run is no longer running'); - this.accountTime(run); if (run.activeMilliseconds >= run.budget.maxMinutes * 60000) { run.status = 'waiting'; run.waitingReason = 'budget'; run.lease!.expiresAt = now(); } - else { run.lastActiveAt = now(); run.lease!.expiresAt = new Date(Date.now() + 60000).toISOString(); } - await tx.put('run', run.status === 'waiting' ? touch(run) : run); return run; + const run = await must(tx, 'run', id); + executionLease(actor, run, token); + if (run.status !== 'running') throw new DomainError('RUN_NOT_RUNNING', 'Run is no longer running'); + this.accountTime(run); + if (run.activeMilliseconds >= run.budget.maxMinutes * 60000) { + run.status = 'waiting'; + run.waitingReason = 'budget'; + run.lease!.expiresAt = now(); + } else { + run.lastActiveAt = now(); + run.lease!.expiresAt = new Date(Date.now() + 60000).toISOString(); + } + await tx.put('run', run.status === 'waiting' ? touch(run) : run); + return run; }); } async releaseLease(actor: Actor, id: string, token: number) { - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); executionLease(actor, run, token); this.accountTime(run); run.lease!.expiresAt = now(); await tx.put('run', touch(run)); return run; }); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, token); + this.accountTime(run); + run.lease!.expiresAt = now(); + await tx.put('run', touch(run)); + return run; + }); } async waitRun(actor: Actor, id: string, reason: string, expectedGeneration?: string) { - if (actor.kind !== 'worker' || actor.runId) throw new DomainError('FORBIDDEN', 'Trusted workflow coordinator required', 403); - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); if (terminal(run) || run.status === 'paused' || (expectedGeneration && run.executionGeneration !== expectedGeneration)) return run; if (run.lease && Date.parse(run.lease.expiresAt) > Date.now()) throw new DomainError('LEASE_HELD', 'Execution is still active'); const message = redact(reason).slice(0, 2000); if (run.status === 'waiting' && run.waitingReason === message) return run; this.accountTime(run); run.status = 'waiting'; run.waitingReason = message; await tx.put('run', touch(run)); await tx.event(id, 'run.waiting', run.waitingReason); return run; }); + if (actor.kind !== 'worker' || actor.runId) + throw new DomainError('FORBIDDEN', 'Trusted workflow coordinator required', 403); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + if ( + terminal(run) || + run.status === 'paused' || + (expectedGeneration && run.executionGeneration !== expectedGeneration) + ) + return run; + if (run.lease && Date.parse(run.lease.expiresAt) > Date.now()) + throw new DomainError('LEASE_HELD', 'Execution is still active'); + const message = redact(reason).slice(0, 2000); + if (run.status === 'waiting' && run.waitingReason === message) return run; + this.accountTime(run); + run.status = 'waiting'; + run.waitingReason = message; + await tx.put('run', touch(run)); + await tx.event(id, 'run.waiting', run.waitingReason); + return run; + }); } async transition(actor: Actor, id: string, input: unknown) { const data = transitionInput.parse(input); - if (!['worker', 'verifier', 'deployer'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'Execution identity required', 403); - if(['integrate','build'].includes(data.stage)&&data.status==='running')await this.workspaceIntegration.enforce(actor,id,data.leaseToken); + if (!['worker', 'verifier', 'deployer'].includes(actor.kind)) + throw new DomainError('FORBIDDEN', 'Execution identity required', 403); + if (['integrate', 'build'].includes(data.stage) && data.status === 'running') + await this.workspaceIntegration.enforce(actor, id, data.leaseToken); return this.store.transaction(actor.tenantId, async tx => { - let run = await must(tx, 'run', id); executionLease(actor, run, data.leaseToken); + let run = await must(tx, 'run', id); + executionLease(actor, run, data.leaseToken); if (terminal(run) || run.status === 'paused') throw new DomainError('RUN_NOT_RUNNABLE', 'Run is stopped'); - if (data.expectedVersion && data.expectedVersion !== run.version) throw new DomainError('VERSION_CONFLICT', 'Run version changed'); - if (data.status === 'succeeded' || data.stage === 'accept') throw new DomainError('DELIVERY_EVIDENCE_REQUIRED', 'Only verified release acceptance can complete a run'); - if (data.sourceCommit && run.verifiedCommit && data.sourceCommit !== run.verifiedCommit && !data.repair) throw new DomainError('COMMIT_MISMATCH', 'A different commit must be independently verified before progressing'); - const previous = stages.indexOf(run.stage), next = stages.indexOf(data.stage); - if (next < previous && !(data.repair && data.stage === 'implement')) throw new DomainError('INVALID_TRANSITION', 'Cannot move backward without a repair attempt'); + if (data.expectedVersion && data.expectedVersion !== run.version) + throw new DomainError('VERSION_CONFLICT', 'Run version changed'); + if (data.status === 'succeeded' || data.stage === 'accept') + throw new DomainError('DELIVERY_EVIDENCE_REQUIRED', 'Only verified release acceptance can complete a run'); + if (data.sourceCommit && run.verifiedCommit && data.sourceCommit !== run.verifiedCommit && !data.repair) + throw new DomainError( + 'COMMIT_MISMATCH', + 'A different commit must be independently verified before progressing', + ); + const previous = stages.indexOf(run.stage), + next = stages.indexOf(data.stage); + if (next < previous && !(data.repair && data.stage === 'implement')) + throw new DomainError('INVALID_TRANSITION', 'Cannot move backward without a repair attempt'); if (next > previous + 1) throw new DomainError('INVALID_TRANSITION', 'Execution stages cannot be skipped'); - if (data.repair) { if (run.repairCount >= run.budget.maxRepairRounds) throw new DomainError('REPAIR_LIMIT', 'Repair budget exhausted'); run.repairCount++; run.verifiedCommit = undefined; } - if (['integrate', 'build', 'preview', 'release'].includes(data.stage) && !run.verifiedCommit) throw new DomainError('VERIFICATION_REQUIRED', 'Trusted verification of this revision is required'); - this.accountTime(run); run = touch(run); - if (run.activeMilliseconds >= run.budget.maxMinutes * 60000 || run.usedTokens >= run.budget.maxTokens) { run.status = 'waiting'; run.waitingReason = 'budget'; run.lease!.expiresAt = now(); await tx.put('run', run); await tx.event(id, 'run.waiting', 'Execution budget exhausted.'); return run; } - run.stage = data.stage; run.status = data.status; run.waitingReason = data.waitingReason; + if (data.repair) { + if (run.repairCount >= run.budget.maxRepairRounds) + throw new DomainError('REPAIR_LIMIT', 'Repair budget exhausted'); + run.repairCount++; + run.verifiedCommit = undefined; + } + if (['integrate', 'build', 'preview', 'release'].includes(data.stage) && !run.verifiedCommit) + throw new DomainError('VERIFICATION_REQUIRED', 'Trusted verification of this revision is required'); + this.accountTime(run); + run = touch(run); + if (run.activeMilliseconds >= run.budget.maxMinutes * 60000 || run.usedTokens >= run.budget.maxTokens) { + run.status = 'waiting'; + run.waitingReason = 'budget'; + run.lease!.expiresAt = now(); + await tx.put('run', run); + await tx.event(id, 'run.waiting', 'Execution budget exhausted.'); + return run; + } + run.stage = data.stage; + run.status = data.status; + run.waitingReason = data.waitingReason; if (data.sourceCommit) run.sourceCommit = data.sourceCommit; if (data.status === 'failed') run.error = redact(data.message).slice(0, 5000); - if (data.checkpointId) { const checkpoint = await must(tx, 'checkpoint', data.checkpointId); if (checkpoint.runId !== id) throw new DomainError('CHECKPOINT_MISMATCH', 'Checkpoint belongs to another run'); run.checkpointId = data.checkpointId; } - if (run.status === 'running') run.lastActiveAt = now(); else run.lastActiveAt = undefined; + if (data.checkpointId) { + const checkpoint = await must(tx, 'checkpoint', data.checkpointId); + if (checkpoint.runId !== id) throw new DomainError('CHECKPOINT_MISMATCH', 'Checkpoint belongs to another run'); + run.checkpointId = data.checkpointId; + } + if (run.status === 'running') run.lastActiveAt = now(); + else run.lastActiveAt = undefined; if (terminal(run) && !run.providerProxyActiveRequest) run.reservedTokens = 0; - await tx.put('run', run); await tx.event(id, 'run.transition', redact(data.message), { stage: run.stage, status: run.status, revision: run.revision }); return run; + await tx.put('run', run); + await tx.event(id, 'run.transition', redact(data.message), { + stage: run.stage, + status: run.status, + revision: run.revision, + }); + return run; }); } async evidence(actor: Actor, input: unknown) { const data = evidenceInput.parse(input); - if ((data.type === 'deployment' && actor.kind !== 'deployer') || (data.type !== 'deployment' && actor.kind !== 'verifier')) throw new DomainError('FORBIDDEN', 'An independent verifier/deployer identity must submit evidence', 403); - if(data.type==='build'&&data.passed)await this.workspaceIntegration.enforce(actor,data.runId,data.leaseToken??0); + if ( + (data.type === 'deployment' && actor.kind !== 'deployer') || + (data.type !== 'deployment' && actor.kind !== 'verifier') + ) + throw new DomainError('FORBIDDEN', 'An independent verifier/deployer identity must submit evidence', 403); + if (data.type === 'build' && data.passed) + await this.workspaceIntegration.enforce(actor, data.runId, data.leaseToken ?? 0); return this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', data.runId); if (run.revision !== data.revision || terminal(run)) throw new DomainError('STALE_EVIDENCE', 'Evidence does not match the active requirement revision'); + const run = await must(tx, 'run', data.runId); + if (run.revision !== data.revision || terminal(run)) + throw new DomainError('STALE_EVIDENCE', 'Evidence does not match the active requirement revision'); executionScope(actor, run); if (data.type !== 'deployment') { executionLease(actor, run, data.leaseToken ?? 0); - if (run.status !== 'running') throw new DomainError('STALE_EVIDENCE', 'Verification requires an active execution'); - if (run.activeMilliseconds + (run.lastActiveAt ? Math.max(0, Date.now() - Date.parse(run.lastActiveAt)) : 0) >= run.budget.maxMinutes * 60000) throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Run time limit reached'); + if (run.status !== 'running') + throw new DomainError('STALE_EVIDENCE', 'Verification requires an active execution'); + if ( + run.activeMilliseconds + (run.lastActiveAt ? Math.max(0, Date.now() - Date.parse(run.lastActiveAt)) : 0) >= + run.budget.maxMinutes * 60000 + ) + throw new DomainError('EXECUTION_BUDGET_EXHAUSTED', 'Run time limit reached'); } else { - if (!data.releaseId || !data.environmentId || !data.bindingDigest) throw new DomainError('DEPLOYMENT_BINDING_REQUIRED', 'Deployment evidence must bind a release, environment and binding digest', 400); - const release = await must(tx, 'release', data.releaseId), environment = await must(tx, 'environment', data.environmentId); - if (actor.userId.startsWith('runner:') && actor.userId !== `runner:${environment.id}`) throw new DomainError('FORBIDDEN', 'Runner cannot attest another environment', 403); - if (release.runId !== run.id || release.revision !== run.revision || release.environmentId !== environment.id || environment.activeReleaseId !== release.id || !['deploying', 'unknown'].includes(release.status) || release.bindingDigest !== data.bindingDigest || release.sourceCommit !== data.sourceCommit || release.imageDigest !== data.imageDigest) throw new DomainError('STALE_EVIDENCE', 'Deployment evidence does not match the active release'); + if (!data.releaseId || !data.environmentId || !data.bindingDigest) + throw new DomainError( + 'DEPLOYMENT_BINDING_REQUIRED', + 'Deployment evidence must bind a release, environment and binding digest', + 400, + ); + const release = await must(tx, 'release', data.releaseId), + environment = await must(tx, 'environment', data.environmentId); + if (actor.userId.startsWith('runner:') && actor.userId !== `runner:${environment.id}`) + throw new DomainError('FORBIDDEN', 'Runner cannot attest another environment', 403); + if ( + release.runId !== run.id || + release.revision !== run.revision || + release.environmentId !== environment.id || + environment.activeReleaseId !== release.id || + !['deploying', 'unknown'].includes(release.status) || + release.bindingDigest !== data.bindingDigest || + release.sourceCommit !== data.sourceCommit || + release.imageDigest !== data.imageDigest + ) + throw new DomainError('STALE_EVIDENCE', 'Deployment evidence does not match the active release'); } const project = await must(tx, 'project', run.projectId); if (data.passed && data.type !== 'deployment') { assertVerificationProfile(project, this.env); - if (project.verificationProfile && data.verificationProfileDigest !== digest(project.verificationProfile)) throw new DomainError('STALE_EVIDENCE', 'Evidence must bind the installed independent acceptance profile'); + if (project.verificationProfile && data.verificationProfileDigest !== digest(project.verificationProfile)) + throw new DomainError('STALE_EVIDENCE', 'Evidence must bind the installed independent acceptance profile'); } if (data.type === 'verification' && data.passed) { const requiredCommands = [...project.verificationCommands, ...(project.verificationProfile?.commands ?? [])]; - if (!requiredCommands.length || !data.commands?.length || data.commands.some(c => c.exitCode !== 0) || requiredCommands.some(argv => !data.commands!.some(c => digest(c.argv) === digest(argv)))) throw new DomainError('INCOMPLETE_VERIFICATION', 'All protected verification commands must pass'); - run.sourceCommit = data.sourceCommit; run.verifiedCommit = data.sourceCommit; run.verifiedPolicyDigest = verificationPolicyDigest(project); await tx.put('run', touch(run)); + if ( + !requiredCommands.length || + !data.commands?.length || + data.commands.some(c => c.exitCode !== 0) || + requiredCommands.some(argv => !data.commands!.some(c => digest(c.argv) === digest(argv))) + ) + throw new DomainError('INCOMPLETE_VERIFICATION', 'All protected verification commands must pass'); + run.sourceCommit = data.sourceCommit; + run.verifiedCommit = data.sourceCommit; + run.verifiedPolicyDigest = verificationPolicyDigest(project); + await tx.put('run', touch(run)); } - if (data.type === 'build' && data.passed && (!data.imageDigest || run.verifiedCommit !== data.sourceCommit || (project.verificationProfile && run.verifiedPolicyDigest !== verificationPolicyDigest(project)))) throw new DomainError('UNVERIFIED_BUILD', 'Build must bind the verified commit and an immutable image digest'); - const evidence = { ...record(actor.tenantId), ...data, issuer: actor.userId, verificationPolicyDigest: verificationPolicyDigest(project) } as Evidence; - if(data.type==='deployment'&&data.passed){const release=await must(tx,'release',data.releaseId!);if(release.visibility==='private'&&release.applicationUrl)throw new DomainError('PRIVATE_RUNNER_RECEIPT_REQUIRED','Private deployment evidence must be submitted through the bound signed Runner task receipt');} - await tx.put('evidence', evidence); await tx.event(run.id, 'evidence.recorded', `${data.type} evidence ${data.passed ? 'passed' : 'failed'}.`, { evidenceId: evidence.id, sourceCommit: data.sourceCommit }); return evidence; + if ( + data.type === 'build' && + data.passed && + (!data.imageDigest || + run.verifiedCommit !== data.sourceCommit || + (project.verificationProfile && run.verifiedPolicyDigest !== verificationPolicyDigest(project))) + ) + throw new DomainError('UNVERIFIED_BUILD', 'Build must bind the verified commit and an immutable image digest'); + const evidence = { + ...record(actor.tenantId), + ...data, + issuer: actor.userId, + verificationPolicyDigest: verificationPolicyDigest(project), + } as Evidence; + if (data.type === 'deployment' && data.passed) { + const release = await must(tx, 'release', data.releaseId!); + if (release.visibility === 'private' && release.applicationUrl) + throw new DomainError( + 'PRIVATE_RUNNER_RECEIPT_REQUIRED', + 'Private deployment evidence must be submitted through the bound signed Runner task receipt', + ); + } + await tx.put('evidence', evidence); + await tx.event(run.id, 'evidence.recorded', `${data.type} evidence ${data.passed ? 'passed' : 'failed'}.`, { + evidenceId: evidence.id, + sourceCommit: data.sourceCommit, + }); + return evidence; }); } async context(actor: Actor, id: string) { const result = await this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', id); const project = await projectAccess(tx, actor, run.projectId); - const connection = (await tx.list('provider_connection')).find(c => c.provider === run.provider && c.status === 'active'); + const run = await must(tx, 'run', id); + const project = await projectAccess(tx, actor, run.projectId); + const connection = (await tx.list('provider_connection')).find( + c => c.provider === run.provider && c.status === 'active', + ); const cp = run.checkpointId ? await tx.get('checkpoint', run.checkpointId) : undefined; - const evidence = (await tx.list('evidence')).filter(e => e.runId === id && e.revision === run.revision && e.sourceCommit === run.verifiedCommit && e.passed); - const release = (await tx.list('release')).find(r => r.runId === id && r.revision === run.revision && r.sourceCommit === run.verifiedCommit); + const evidence = (await tx.list('evidence')).filter( + e => e.runId === id && e.revision === run.revision && e.sourceCommit === run.verifiedCommit && e.passed, + ); + const release = (await tx.list('release')).find( + r => r.runId === id && r.revision === run.revision && r.sourceCommit === run.verifiedCommit, + ); const deliveryConfig = project.deliveryConfig as { environmentId?: string } | undefined; - return { run, project, checkpoint: cp?.checkpoint ?? null, workspace: cp?.workspace, verificationCommands: project.verificationCommands, verificationProfile: project.verificationProfile, verificationProfileDigest: project.verificationProfile ? digest(project.verificationProfile) : undefined, verificationProfileRequired: this.env.NODE_ENV === 'production', connectionId: connection?.id ?? null, bundleConfig: await resolveBundle(tx, run.bundleVersion), delivery: { release, verificationEvidenceId: evidence.find(e => e.type === 'verification')?.id, buildEvidenceId: evidence.find(e => e.type === 'build')?.id, environmentId: release?.environmentId ?? deliveryConfig?.environmentId } }; + return { + run, + project, + checkpoint: cp?.checkpoint ?? null, + workspace: cp?.workspace, + verificationCommands: project.verificationCommands, + verificationProfile: project.verificationProfile, + verificationProfileDigest: project.verificationProfile ? digest(project.verificationProfile) : undefined, + verificationProfileRequired: this.env.NODE_ENV === 'production', + connectionId: connection?.id ?? null, + bundleConfig: await resolveBundle(tx, run.bundleVersion), + delivery: { + release, + verificationEvidenceId: evidence.find(e => e.type === 'verification')?.id, + buildEvidenceId: evidence.find(e => e.type === 'build')?.id, + environmentId: release?.environmentId ?? deliveryConfig?.environmentId, + }, + }; }); - if (result.checkpoint && ['worker', 'verifier'].includes(actor.kind)) result.checkpoint = await this.artifacts.hydrateCheckpoint(actor, id, result.checkpoint as Record); + if (result.checkpoint && ['worker', 'verifier'].includes(actor.kind)) + result.checkpoint = await this.artifacts.hydrateCheckpoint( + actor, + id, + result.checkpoint as Record, + ); return result; } async checkpoint(actor: Actor, id: string, input: Record, token: number, workspace?: string) { const checkpoint = await this.artifacts.uploadCheckpointBundle(actor, id, token, input); - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); executionLease(actor, run, token); const cp = { ...record(actor.tenantId), runId: id, revision: run.revision, checkpoint, workspace }; await tx.put('checkpoint', cp); run.checkpointId = cp.id; await tx.put('run', touch(run)); return cp; }); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, token); + const cp = { ...record(actor.tenantId), runId: id, revision: run.revision, checkpoint, workspace }; + await tx.put('checkpoint', cp); + run.checkpointId = cp.id; + await tx.put('run', touch(run)); + return cp; + }); } - async classify(actor: Actor, id: string, input: { leaseToken: number; sourceCommit: string; changedPaths: string[]; migration: boolean; permissionsExpanded: boolean }) { + async classify( + actor: Actor, + id: string, + input: { + leaseToken: number; + sourceCommit: string; + changedPaths: string[]; + migration: boolean; + permissionsExpanded: boolean; + }, + ) { if (actor.kind !== 'verifier') throw new DomainError('FORBIDDEN', 'Independent verifier required', 403); - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); executionLease(actor, run, input.leaseToken); if (!run.checkpointId || run.verifiedCommit !== input.sourceCommit) throw new DomainError('VERIFICATION_REQUIRED', 'Classification must bind a verified checkpoint'); const cp = await must(tx, 'checkpoint', run.checkpointId); if (cp.revision !== run.revision || cp.runId !== run.id) throw new DomainError('STALE_CHECKPOINT', 'Classification requires a current revision checkpoint'); const { leaseToken: _, ...classification } = input; cp.trustedClassification = classification; await tx.put('checkpoint', touch(cp)); return { accepted: true }; }); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, input.leaseToken); + if (!run.checkpointId || run.verifiedCommit !== input.sourceCommit) + throw new DomainError('VERIFICATION_REQUIRED', 'Classification must bind a verified checkpoint'); + const cp = await must(tx, 'checkpoint', run.checkpointId); + if (cp.revision !== run.revision || cp.runId !== run.id) + throw new DomainError('STALE_CHECKPOINT', 'Classification requires a current revision checkpoint'); + const { leaseToken: _, ...classification } = input; + cp.trustedClassification = classification; + await tx.put('checkpoint', touch(cp)); + return { accepted: true }; + }); } async providerEvent(actor: Actor, id: string, token: number, event: Record) { - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', id); executionLease(actor, run, token); return tx.event(id, `provider.${String(event.type ?? 'progress')}`, redact(String(event.message ?? event.text ?? event.type ?? 'Provider progress')).slice(0, 5000), { sequence: event.sequence, provider: run.provider }); }); - } - period() { const date = new Date(); return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1)).toISOString(); } - async usage(actor: Actor): Promise { - return this.store.transaction(actor.tenantId, async tx => { const tenant = await tx.tenant(); const runs = await tx.list('run'); const usage = await tx.usage(this.period()); return { activeRuns: runs.filter(r => r.status === 'running').length, maxConcurrentRuns: tenant.maxConcurrentRuns, tokens: usage.reduce((n, u) => n + u.tokens, 0), reservedTokens: runs.reduce((n, r) => n + r.reservedTokens, 0), tokenLimit: tenant.tokenLimit, computeSeconds: usage.reduce((n, u) => n + u.computeSeconds, 0), storageBytes: usage.reduce((n, u) => n + u.storageBytes, 0), modelCost: 'external', periodStart: this.period() }; }); - } - async meter(actor: Actor, id: string, input: { operationId: string; tokens: number; computeSeconds: number; leaseToken: number }) { - if (!Number.isSafeInteger(input.tokens) || input.tokens < 0 || !Number.isFinite(input.computeSeconds) || input.computeSeconds < 0 || !input.operationId) throw new DomainError('INVALID_USAGE', 'Invalid usage measurement', 400); return this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', id); executionScope(actor, run); + const run = await must(tx, 'run', id); + executionLease(actor, run, token); + return tx.event( + id, + `provider.${String(event.type ?? 'progress')}`, + redact(String(event.message ?? event.text ?? event.type ?? 'Provider progress')).slice(0, 5000), + { sequence: event.sequence, provider: run.provider }, + ); + }); + } + period() { + const date = new Date(); + return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1)).toISOString(); + } + async usage(actor: Actor): Promise { + return this.store.transaction(actor.tenantId, async tx => { + const tenant = await tx.tenant(); + const runs = await tx.list('run'); + const usage = await tx.usage(this.period()); + return { + activeRuns: runs.filter(r => r.status === 'running').length, + maxConcurrentRuns: tenant.maxConcurrentRuns, + tokens: usage.reduce((n, u) => n + u.tokens, 0), + reservedTokens: runs.reduce((n, r) => n + r.reservedTokens, 0), + tokenLimit: tenant.tokenLimit, + computeSeconds: usage.reduce((n, u) => n + u.computeSeconds, 0), + storageBytes: usage.reduce((n, u) => n + u.storageBytes, 0), + modelCost: 'external', + periodStart: this.period(), + }; + }); + } + async meter( + actor: Actor, + id: string, + input: { operationId: string; tokens: number; computeSeconds: number; leaseToken: number }, + ) { + if ( + !Number.isSafeInteger(input.tokens) || + input.tokens < 0 || + !Number.isFinite(input.computeSeconds) || + input.computeSeconds < 0 || + !input.operationId + ) + throw new DomainError('INVALID_USAGE', 'Invalid usage measurement', 400); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionScope(actor, run); const { leaseToken, ...measurement } = input; return idempotent(tx, `meter:${id}:${input.operationId}`, measurement, async () => { - executionLease(actor, run, leaseToken); this.accountTime(run); - run.usedTokens += input.tokens; run.reservedTokens = Math.max(0, run.reservedTokens - input.tokens); - await tx.meter({ id: `${id}:${input.operationId}`, runId: id, tokens: input.tokens, computeSeconds: input.computeSeconds, storageBytes: 0, at: now() }); - if (run.usedTokens >= run.budget.maxTokens || run.activeMilliseconds >= run.budget.maxMinutes * 60000) { run.status = 'waiting'; run.waitingReason = 'budget'; run.lease!.expiresAt = now(); } - else if (run.status === 'running') run.lastActiveAt = now(); - await tx.put('run', touch(run)); return { accepted: true, remaining: Math.max(0, run.budget.maxTokens - run.usedTokens) }; + executionLease(actor, run, leaseToken); + this.accountTime(run); + run.usedTokens += input.tokens; + run.reservedTokens = Math.max(0, run.reservedTokens - input.tokens); + await tx.meter({ + id: `${id}:${input.operationId}`, + runId: id, + tokens: input.tokens, + computeSeconds: input.computeSeconds, + storageBytes: 0, + at: now(), + }); + if (run.usedTokens >= run.budget.maxTokens || run.activeMilliseconds >= run.budget.maxMinutes * 60000) { + run.status = 'waiting'; + run.waitingReason = 'budget'; + run.lease!.expiresAt = now(); + } else if (run.status === 'running') run.lastActiveAt = now(); + await tx.put('run', touch(run)); + return { accepted: true, remaining: Math.max(0, run.budget.maxTokens - run.usedTokens) }; }); }); } - private accountTime(run: Run) { if (run.lastActiveAt) { const end = Math.min(Date.now(), run.lease ? Date.parse(run.lease.expiresAt) : Date.now()); run.activeMilliseconds += Math.max(0, end - Date.parse(run.lastActiveAt)); run.lastActiveAt = undefined; } } + private accountTime(run: Run) { + if (run.lastActiveAt) { + const end = Math.min(Date.now(), run.lease ? Date.parse(run.lease.expiresAt) : Date.now()); + run.activeMilliseconds += Math.max(0, end - Date.parse(run.lastActiveAt)); + run.lastActiveAt = undefined; + } + } } diff --git a/apps/api/src/domain/deployment-secret-policy.ts b/apps/api/src/domain/deployment-secret-policy.ts index 51bfe16..d918c7e 100644 --- a/apps/api/src/domain/deployment-secret-policy.ts +++ b/apps/api/src/domain/deployment-secret-policy.ts @@ -1,2 +1,12 @@ -import {DomainError} from '@wishforge/contracts'; -export function requireDeploymentSecretPolicy(input:{secretVersionRefs?:string[];deploymentSecretPolicyDigest?:string}){if(input.secretVersionRefs?.length&&!/^sha256:[a-f0-9]{64}$/.test(input.deploymentSecretPolicyDigest??''))throw new DomainError('DEPLOYMENT_SECRET_POLICY_REQUIRED','Versioned application secrets require the operator-reviewed deployment secret policy digest',503);} +import { DomainError } from '@wishforge/contracts'; +export function requireDeploymentSecretPolicy(input: { + secretVersionRefs?: string[]; + deploymentSecretPolicyDigest?: string; +}) { + if (input.secretVersionRefs?.length && !/^sha256:[a-f0-9]{64}$/.test(input.deploymentSecretPolicyDigest ?? '')) + throw new DomainError( + 'DEPLOYMENT_SECRET_POLICY_REQUIRED', + 'Versioned application secrets require the operator-reviewed deployment secret policy digest', + 503, + ); +} diff --git a/apps/api/src/domain/entitlements.ts b/apps/api/src/domain/entitlements.ts index 42823c6..d49ac9e 100644 --- a/apps/api/src/domain/entitlements.ts +++ b/apps/api/src/domain/entitlements.ts @@ -1,91 +1,375 @@ import { createHmac, randomUUID } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { z } from 'zod'; -import { DomainError, roles, type Actor, type CommercialPlan, type EntitlementView, type ExecutionResourceGrant, type PlanLimits, type Run, type TenantEntitlementSnapshot } from '@wishforge/contracts'; +import { + DomainError, + roles, + type Actor, + type CommercialPlan, + type EntitlementView, + type ExecutionResourceGrant, + type PlanLimits, + type Run, + type TenantEntitlementSnapshot, +} from '@wishforge/contracts'; import type { Store, Tenant, Transaction } from '../db/store.js'; import { idempotent, must, record, touch } from './control-plane.js'; import { requireHuman } from './policy.js'; const positive = z.number().int().positive(); -export const planLimitsSchema = z.object({ maxMembers: positive.max(10000), maxProjects: positive.max(10000), maxWorkspaces: positive.max(10000), maxConcurrentRuns: positive.max(1000), monthlyModelTokens: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), execution: z.object({ cpuMilli: positive.min(100).max(64000), memoryMiB: positive.min(512).max(262144), storageMiB: positive.min(128).max(1048576) }).strict() }).strict(); -export const planSchema = z.object({ id: z.string().regex(/^[a-z0-9][a-z0-9-]{1,39}$/), version: z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_.-]{0,39}$/), name: z.string().trim().min(1).max(120), limits: planLimitsSchema }).strict(); -const entitlementSchema = z.object({ schemaVersion: z.literal(1), planId: planSchema.shape.id, planVersion: planSchema.shape.version, limits: planLimitsSchema, assignedAt: z.string().datetime(), assignedBy: z.string().min(1) }).strict(); -export const PILOT_PLAN: CommercialPlan = { id: 'pilot', version: 'pilot-v1', name: 'Invitation pilot', limits: { maxMembers: 10, maxProjects: 10, maxWorkspaces: 5, maxConcurrentRuns: 2, monthlyModelTokens: 2_000_000, execution: { cpuMilli: 2000, memoryMiB: 4096, storageMiB: 10240 } } }; +export const planLimitsSchema = z + .object({ + maxMembers: positive.max(10000), + maxProjects: positive.max(10000), + maxWorkspaces: positive.max(10000), + maxConcurrentRuns: positive.max(1000), + monthlyModelTokens: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER), + execution: z + .object({ + cpuMilli: positive.min(100).max(64000), + memoryMiB: positive.min(512).max(262144), + storageMiB: positive.min(128).max(1048576), + }) + .strict(), + }) + .strict(); +export const planSchema = z + .object({ + id: z.string().regex(/^[a-z0-9][a-z0-9-]{1,39}$/), + version: z.string().regex(/^[A-Za-z0-9][A-Za-z0-9_.-]{0,39}$/), + name: z.string().trim().min(1).max(120), + limits: planLimitsSchema, + }) + .strict(); +const entitlementSchema = z + .object({ + schemaVersion: z.literal(1), + planId: planSchema.shape.id, + planVersion: planSchema.shape.version, + limits: planLimitsSchema, + assignedAt: z.string().datetime(), + assignedBy: z.string().min(1), + }) + .strict(); +export const PILOT_PLAN: CommercialPlan = { + id: 'pilot', + version: 'pilot-v1', + name: 'Invitation pilot', + limits: { + maxMembers: 10, + maxProjects: 10, + maxWorkspaces: 5, + maxConcurrentRuns: 2, + monthlyModelTokens: 2_000_000, + execution: { cpuMilli: 2000, memoryMiB: 4096, storageMiB: 10240 }, + }, +}; export async function configuredPlans(env = process.env): Promise { if (!env.WF_PLAN_CATALOG_FILE) return [structuredClone(PILOT_PLAN)]; - const bytes = await readFile(env.WF_PLAN_CATALOG_FILE); if (bytes.length > 65536) throw new DomainError('PLAN_CONFIGURATION_INVALID', 'Plan catalog exceeds 64 KiB', 503); - const catalog = z.object({ schemaVersion: z.literal(1), plans: z.array(planSchema).min(1).max(100) }).strict().parse(JSON.parse(bytes.toString('utf8'))); - if (new Set(catalog.plans.map(plan => plan.id)).size !== catalog.plans.length) throw new DomainError('PLAN_CONFIGURATION_INVALID', 'Plan IDs must be unique', 503); + const bytes = await readFile(env.WF_PLAN_CATALOG_FILE); + if (bytes.length > 65536) throw new DomainError('PLAN_CONFIGURATION_INVALID', 'Plan catalog exceeds 64 KiB', 503); + const catalog = z + .object({ schemaVersion: z.literal(1), plans: z.array(planSchema).min(1).max(100) }) + .strict() + .parse(JSON.parse(bytes.toString('utf8'))); + if (new Set(catalog.plans.map(plan => plan.id)).size !== catalog.plans.length) + throw new DomainError('PLAN_CONFIGURATION_INVALID', 'Plan IDs must be unique', 503); return catalog.plans; } -export function effectiveEntitlements(tenant: Tenant): { planId: string; planVersion: string; limits: PlanLimits; legacy: boolean; assignedAt?: string; assignedBy?: string } { +export function effectiveEntitlements(tenant: Tenant): { + planId: string; + planVersion: string; + limits: PlanLimits; + legacy: boolean; + assignedAt?: string; + assignedBy?: string; +} { if (tenant.entitlements) { - const parsed = entitlementSchema.safeParse(tenant.entitlements); if (!parsed.success) throw new DomainError('ENTITLEMENTS_INVALID', 'Operator entitlement snapshot requires reconciliation', 503); - return { ...parsed.data, legacy: false, limits: { ...parsed.data.limits, maxConcurrentRuns: Math.min(parsed.data.limits.maxConcurrentRuns, tenant.maxConcurrentRuns), monthlyModelTokens: Math.min(parsed.data.limits.monthlyModelTokens, tenant.tokenLimit) } }; + const parsed = entitlementSchema.safeParse(tenant.entitlements); + if (!parsed.success) + throw new DomainError('ENTITLEMENTS_INVALID', 'Operator entitlement snapshot requires reconciliation', 503); + return { + ...parsed.data, + legacy: false, + limits: { + ...parsed.data.limits, + maxConcurrentRuns: Math.min(parsed.data.limits.maxConcurrentRuns, tenant.maxConcurrentRuns), + monthlyModelTokens: Math.min(parsed.data.limits.monthlyModelTokens, tenant.tokenLimit), + }, + }; } - return { planId: 'legacy', planVersion: 'legacy-v1', legacy: true, limits: { ...structuredClone(PILOT_PLAN.limits), maxProjects: 20, maxWorkspaces: 10, maxConcurrentRuns: tenant.maxConcurrentRuns, monthlyModelTokens: tenant.tokenLimit } }; + return { + planId: 'legacy', + planVersion: 'legacy-v1', + legacy: true, + limits: { + ...structuredClone(PILOT_PLAN.limits), + maxProjects: 20, + maxWorkspaces: 10, + maxConcurrentRuns: tenant.maxConcurrentRuns, + monthlyModelTokens: tenant.tokenLimit, + }, + }; } export function requireBillingOperator(actor: Actor, env = process.env) { requireHuman(actor); - if (actor.role !== 'admin' || !(env.WF_BILLING_OPERATOR_SUBJECTS ?? '').split(',').map(value => value.trim()).filter(Boolean).includes(actor.userId)) throw new DomainError('BILLING_OPERATOR_REQUIRED', 'A human admin explicitly assigned to billing operations is required', 403); + if ( + actor.role !== 'admin' || + !(env.WF_BILLING_OPERATOR_SUBJECTS ?? '') + .split(',') + .map(value => value.trim()) + .filter(Boolean) + .includes(actor.userId) + ) + throw new DomainError( + 'BILLING_OPERATOR_REQUIRED', + 'A human admin explicitly assigned to billing operations is required', + 403, + ); } export async function requireCapacity(tx: Transaction, kind: 'project' | 'workspace') { const limits = effectiveEntitlements(await tx.tenant()).limits; const count = (await tx.list(kind)).filter(value => value.status !== 'deleted').length; - if (count >= (kind === 'project' ? limits.maxProjects : limits.maxWorkspaces)) throw new DomainError('ENTITLEMENT_LIMIT', `Tenant ${kind} allowance is exhausted`, 429); + if (count >= (kind === 'project' ? limits.maxProjects : limits.maxWorkspaces)) + throw new DomainError('ENTITLEMENT_LIMIT', `Tenant ${kind} allowance is exhausted`, 429); +} +export function heldAllocation(run: Run): boolean { + const allocation = run.resourceAllocation as { status?: string } | undefined; + return !!allocation && ['reserved', 'provisioned', 'unknown', 'releasing'].includes(String(allocation.status)); +} +export function occupiesExecutionSlot(run: Run): boolean { + return ( + heldAllocation(run) || !!(run.status === 'running' && run.lease && Date.parse(run.lease.expiresAt) > Date.now()) + ); +} +export function resourceGrantPayload(grant: Omit) { + return JSON.stringify({ + schemaVersion: grant.schemaVersion, + tenantId: grant.tenantId, + runId: grant.runId, + generation: grant.generation, + planId: grant.planId, + planVersion: grant.planVersion, + image: grant.image, + cpuMilli: grant.cpuMilli, + memoryMiB: grant.memoryMiB, + storageMiB: grant.storageMiB, + issuedAt: grant.issuedAt, + expiresAt: grant.expiresAt, + }); +} +function configuredInteger(value: string | undefined, fallback: number, minimum: number) { + const number = value === undefined ? fallback : Number(value); + if (!Number.isSafeInteger(number) || number < minimum) + throw new DomainError( + 'RESOURCE_CONFIGURATION_INVALID', + 'Execution resource default is below the supported provisioner minimum', + 503, + ); + return number; } -export function heldAllocation(run: Run): boolean { const allocation = run.resourceAllocation as { status?: string } | undefined; return !!allocation && ['reserved', 'provisioned', 'unknown', 'releasing'].includes(String(allocation.status)); } -export function occupiesExecutionSlot(run: Run): boolean { return heldAllocation(run) || !!(run.status === 'running' && run.lease && Date.parse(run.lease.expiresAt) > Date.now()); } -export function resourceGrantPayload(grant: Omit) { return JSON.stringify({ schemaVersion: grant.schemaVersion, tenantId: grant.tenantId, runId: grant.runId, generation: grant.generation, planId: grant.planId, planVersion: grant.planVersion, image: grant.image, cpuMilli: grant.cpuMilli, memoryMiB: grant.memoryMiB, storageMiB: grant.storageMiB, issuedAt: grant.issuedAt, expiresAt: grant.expiresAt }); } -function configuredInteger(value: string | undefined, fallback: number, minimum: number) { const number = value === undefined ? fallback : Number(value); if (!Number.isSafeInteger(number) || number < minimum) throw new DomainError('RESOURCE_CONFIGURATION_INVALID', 'Execution resource default is below the supported provisioner minimum', 503); return number; } /** Caller holds the tenant transaction lock; the claim survives uncertain provisioning. */ -export async function reserveExecutionGrant(tx: Transaction, runId: string, generation: string, env = process.env): Promise { - if (!env.WF_RESOURCE_GRANT_SECRET || env.WF_RESOURCE_GRANT_SECRET.length < 32) throw new DomainError('RESOURCE_SIGNER_UNCONFIGURED', 'Configure the independent resource-grant signing secret', 503); - const run = await must(tx, 'run', runId); const plan = effectiveEntitlements(await tx.tenant()); - if (['succeeded', 'failed', 'canceled', 'paused'].includes(run.status)) throw new DomainError('RUN_STOPPED', 'Stopped run cannot reserve a sandbox'); +export async function reserveExecutionGrant( + tx: Transaction, + runId: string, + generation: string, + env = process.env, +): Promise { + if (!env.WF_RESOURCE_GRANT_SECRET || env.WF_RESOURCE_GRANT_SECRET.length < 32) + throw new DomainError( + 'RESOURCE_SIGNER_UNCONFIGURED', + 'Configure the independent resource-grant signing secret', + 503, + ); + const run = await must(tx, 'run', runId); + const plan = effectiveEntitlements(await tx.tenant()); + if (['succeeded', 'failed', 'canceled', 'paused'].includes(run.status)) + throw new DomainError('RUN_STOPPED', 'Stopped run cannot reserve a sandbox'); const others = (await tx.list('run')).filter(other => other.id !== run.id && occupiesExecutionSlot(other)); - if (others.length >= plan.limits.maxConcurrentRuns) throw new DomainError('EXECUTION_CAPACITY', 'Execution slots are occupied; this request remains queued', 429); - if (!run.runtimeImage && run.checkpointId) throw new DomainError('RUNTIME_IMAGE_MIGRATION_REQUIRED', 'Legacy checkpoint has no pinned runtime image; authorize a checkpoint migration before resuming'); + if (others.length >= plan.limits.maxConcurrentRuns) + throw new DomainError('EXECUTION_CAPACITY', 'Execution slots are occupied; this request remains queued', 429); + if (!run.runtimeImage && run.checkpointId) + throw new DomainError( + 'RUNTIME_IMAGE_MIGRATION_REQUIRED', + 'Legacy checkpoint has no pinned runtime image; authorize a checkpoint migration before resuming', + ); const image = String(run.runtimeImage ?? env.WF_RUN_IMAGE ?? env.WF_RUN_WORKER_IMAGE ?? ''); - if (!/^[A-Za-z0-9][A-Za-z0-9._:/-]*@sha256:[a-f0-9]{64}$/.test(image)) throw new DomainError('RUNTIME_IMAGE_UNCONFIGURED', 'An immutable approved runtime image digest is required', 503); - const previous = run.resourceAllocation as { generation?: string; grant?: ExecutionResourceGrant; status?: string } | undefined; - if (previous?.generation !== generation && heldAllocation(run)) throw new DomainError('RESOURCE_PREVIOUS_GENERATION', 'The previous sandbox allocation must be stopped and reconciled before a new generation can reserve resources'); - const requested = previous?.generation === generation && previous.grant ? previous.grant : { - cpuMilli: Math.min(plan.limits.execution.cpuMilli, configuredInteger(env.WF_DEFAULT_EXECUTION_CPU_MILLI, 2000, 100)), - memoryMiB: Math.min(plan.limits.execution.memoryMiB, configuredInteger(env.WF_DEFAULT_EXECUTION_MEMORY_MIB, 4096, 512)), - storageMiB: Math.min(plan.limits.execution.storageMiB, configuredInteger(env.WF_DEFAULT_EXECUTION_STORAGE_MIB, 10240, 128)), - }; - if (requested.cpuMilli > plan.limits.execution.cpuMilli || requested.memoryMiB > plan.limits.execution.memoryMiB || requested.storageMiB > plan.limits.execution.storageMiB) throw new DomainError('RESOURCE_PLAN_CHANGED', 'Existing sandbox grant exceeds current entitlements; reconcile before reprovisioning'); + if (!/^[A-Za-z0-9][A-Za-z0-9._:/-]*@sha256:[a-f0-9]{64}$/.test(image)) + throw new DomainError('RUNTIME_IMAGE_UNCONFIGURED', 'An immutable approved runtime image digest is required', 503); + const previous = run.resourceAllocation as + { generation?: string; grant?: ExecutionResourceGrant; status?: string } | undefined; + if (previous?.generation !== generation && heldAllocation(run)) + throw new DomainError( + 'RESOURCE_PREVIOUS_GENERATION', + 'The previous sandbox allocation must be stopped and reconciled before a new generation can reserve resources', + ); + const requested = + previous?.generation === generation && previous.grant + ? previous.grant + : { + cpuMilli: Math.min( + plan.limits.execution.cpuMilli, + configuredInteger(env.WF_DEFAULT_EXECUTION_CPU_MILLI, 2000, 100), + ), + memoryMiB: Math.min( + plan.limits.execution.memoryMiB, + configuredInteger(env.WF_DEFAULT_EXECUTION_MEMORY_MIB, 4096, 512), + ), + storageMiB: Math.min( + plan.limits.execution.storageMiB, + configuredInteger(env.WF_DEFAULT_EXECUTION_STORAGE_MIB, 10240, 128), + ), + }; + if ( + requested.cpuMilli > plan.limits.execution.cpuMilli || + requested.memoryMiB > plan.limits.execution.memoryMiB || + requested.storageMiB > plan.limits.execution.storageMiB + ) + throw new DomainError( + 'RESOURCE_PLAN_CHANGED', + 'Existing sandbox grant exceeds current entitlements; reconcile before reprovisioning', + ); const issuedAt = Math.floor(Date.now() / 1000); - const payload: Omit = { schemaVersion: 1, tenantId: run.tenantId, runId, generation, planId: plan.planId, planVersion: plan.planVersion, image, cpuMilli: requested.cpuMilli, memoryMiB: requested.memoryMiB, storageMiB: requested.storageMiB, issuedAt, expiresAt: issuedAt + 300 }; - const grant = { ...payload, signature: createHmac('sha256', env.WF_RESOURCE_GRANT_SECRET).update(resourceGrantPayload(payload)).digest('base64url') }; - run.runtimeImage = image; run.executionGeneration = generation; run.resourceAllocation = { generation, grant, status: previous?.generation === generation && previous.status && previous.status !== 'released' ? previous.status : 'reserved', updatedAt: new Date().toISOString() }; - await tx.put('run', touch(run)); return grant; + const payload: Omit = { + schemaVersion: 1, + tenantId: run.tenantId, + runId, + generation, + planId: plan.planId, + planVersion: plan.planVersion, + image, + cpuMilli: requested.cpuMilli, + memoryMiB: requested.memoryMiB, + storageMiB: requested.storageMiB, + issuedAt, + expiresAt: issuedAt + 300, + }; + const grant = { + ...payload, + signature: createHmac('sha256', env.WF_RESOURCE_GRANT_SECRET) + .update(resourceGrantPayload(payload)) + .digest('base64url'), + }; + run.runtimeImage = image; + run.executionGeneration = generation; + run.resourceAllocation = { + generation, + grant, + status: + previous?.generation === generation && previous.status && previous.status !== 'released' + ? previous.status + : 'reserved', + updatedAt: new Date().toISOString(), + }; + await tx.put('run', touch(run)); + return grant; } -export async function recordAllocation(tx: Transaction, runId: string, generation: string, status: 'provisioned' | 'unknown' | 'releasing' | 'released') { - const run = await must(tx, 'run', runId); const current = run.resourceAllocation as { generation?: string; status?: string } | undefined; +export async function recordAllocation( + tx: Transaction, + runId: string, + generation: string, + status: 'provisioned' | 'unknown' | 'releasing' | 'released', +) { + const run = await must(tx, 'run', runId); + const current = run.resourceAllocation as { generation?: string; status?: string } | undefined; if (current?.generation !== generation || current.status === status) return; - run.resourceAllocation = { ...current, status, updatedAt: new Date().toISOString() }; await tx.put('run', touch(run)); - await tx.event(run.id, `resources.${status}`, status === 'released' ? 'Provisioner confirmed compute Jobs stopped; retained persistent storage remains unmeasured.' : `Sandbox resource allocation ${status}.`, { generation }); + run.resourceAllocation = { ...current, status, updatedAt: new Date().toISOString() }; + await tx.put('run', touch(run)); + await tx.event( + run.id, + `resources.${status}`, + status === 'released' + ? 'Provisioner confirmed compute Jobs stopped; retained persistent storage remains unmeasured.' + : `Sandbox resource allocation ${status}.`, + { generation }, + ); } export class Entitlements { - constructor(private store: Store, private env = process.env) {} - async view(actor: Actor): Promise { return this.store.transaction(actor.tenantId, async tx => { const plan = effectiveEntitlements(await tx.tenant()); return { ...plan, counts: { members: (await tx.members()).filter(member => member.active).length, projects: (await tx.list('project')).filter(p => p.status !== 'deleted').length, workspaces: (await tx.list('workspace')).filter(w => w.status !== 'deleted').length, reservedExecutionSlots: (await tx.list('run')).filter(occupiesExecutionSlot).length } }; }); } - async plans(actor: Actor) { if (!['owner', 'admin'].includes(actor.role)) throw new DomainError('FORBIDDEN', 'Tenant owner/admin required', 403); return configuredPlans(this.env); } + constructor( + private store: Store, + private env = process.env, + ) {} + async view(actor: Actor): Promise { + return this.store.transaction(actor.tenantId, async tx => { + const plan = effectiveEntitlements(await tx.tenant()); + return { + ...plan, + counts: { + members: (await tx.members()).filter(member => member.active).length, + projects: (await tx.list('project')).filter(p => p.status !== 'deleted').length, + workspaces: (await tx.list('workspace')).filter(w => w.status !== 'deleted').length, + reservedExecutionSlots: (await tx.list('run')).filter(occupiesExecutionSlot).length, + }, + }; + }); + } + async plans(actor: Actor) { + if (!['owner', 'admin'].includes(actor.role)) + throw new DomainError('FORBIDDEN', 'Tenant owner/admin required', 403); + return configuredPlans(this.env); + } async inviteMember(actor: Actor, input: unknown, key?: string) { - requireBillingOperator(actor, this.env); const data = z.object({ userId: z.string().trim().min(1).max(200), role: z.enum(roles) }).strict().parse(input); - return this.store.transaction(actor.tenantId, async tx => idempotent(tx, key, { operation: 'member.invite', ...data }, async () => { - const members = await tx.members(), current = members.find(member => member.userId === data.userId && member.active); const limit = effectiveEntitlements(await tx.tenant()).limits.maxMembers; - if (!current && members.filter(member => member.active).length >= limit) throw new DomainError('MEMBER_LIMIT', 'Tenant active-member allowance is exhausted', 429); - await tx.putMembership({ ...data, active: true }); await tx.put('entitlement_event', { ...record(actor.tenantId), action: 'member.invited', actorId: actor.userId, targetSubject: data.userId, role: data.role }); return { userId: data.userId, role: data.role, active: true }; - })); + requireBillingOperator(actor, this.env); + const data = z + .object({ userId: z.string().trim().min(1).max(200), role: z.enum(roles) }) + .strict() + .parse(input); + return this.store.transaction(actor.tenantId, async tx => + idempotent(tx, key, { operation: 'member.invite', ...data }, async () => { + const members = await tx.members(), + current = members.find(member => member.userId === data.userId && member.active); + const limit = effectiveEntitlements(await tx.tenant()).limits.maxMembers; + if (!current && members.filter(member => member.active).length >= limit) + throw new DomainError('MEMBER_LIMIT', 'Tenant active-member allowance is exhausted', 429); + await tx.putMembership({ ...data, active: true }); + await tx.put('entitlement_event', { + ...record(actor.tenantId), + action: 'member.invited', + actorId: actor.userId, + targetSubject: data.userId, + role: data.role, + }); + return { userId: data.userId, role: data.role, active: true }; + }), + ); } async reconcileAllocation(actor: Actor, runId: string, input: unknown, key?: string) { - requireBillingOperator(actor, this.env); const data = z.object({ confirmedStopped: z.literal(true), reference: z.string().trim().min(3).max(200), note: z.string().trim().min(10).max(2000) }).strict().parse(input); - return this.store.transaction(actor.tenantId, tx => idempotent(tx, key, { operation: 'resources.reconcile', runId, ...data }, async () => { - const run = await must(tx, 'run', runId); const allocation = run.resourceAllocation as { generation?: string; status?: string; cleanupConfirmedAt?: string } | undefined; - if (!allocation?.generation || !['unknown', 'releasing'].includes(String(allocation.status)) || !allocation.cleanupConfirmedAt || ['running','queued'].includes(run.status) || (run.lease && Date.parse(run.lease.expiresAt) > Date.now())) throw new DomainError('RESOURCE_RECONCILIATION_REQUIRED', 'Only stopped runs with a provisioner cleanup receipt can be manually reconciled'); - await recordAllocation(tx, runId, allocation.generation, 'released'); await tx.put('entitlement_event', { ...record(actor.tenantId), action: 'resources.manually_reconciled', runId, actorId: actor.userId, reference: data.reference, note: data.note }); return { runId, status: 'released', basis: 'operator_record_after_cleanup', measuredBilling: false }; - })); + requireBillingOperator(actor, this.env); + const data = z + .object({ + confirmedStopped: z.literal(true), + reference: z.string().trim().min(3).max(200), + note: z.string().trim().min(10).max(2000), + }) + .strict() + .parse(input); + return this.store.transaction(actor.tenantId, tx => + idempotent(tx, key, { operation: 'resources.reconcile', runId, ...data }, async () => { + const run = await must(tx, 'run', runId); + const allocation = run.resourceAllocation as + { generation?: string; status?: string; cleanupConfirmedAt?: string } | undefined; + if ( + !allocation?.generation || + !['unknown', 'releasing'].includes(String(allocation.status)) || + !allocation.cleanupConfirmedAt || + ['running', 'queued'].includes(run.status) || + (run.lease && Date.parse(run.lease.expiresAt) > Date.now()) + ) + throw new DomainError( + 'RESOURCE_RECONCILIATION_REQUIRED', + 'Only stopped runs with a provisioner cleanup receipt can be manually reconciled', + ); + await recordAllocation(tx, runId, allocation.generation, 'released'); + await tx.put('entitlement_event', { + ...record(actor.tenantId), + action: 'resources.manually_reconciled', + runId, + actorId: actor.userId, + reference: data.reference, + note: data.note, + }); + return { runId, status: 'released', basis: 'operator_record_after_cleanup', measuredBilling: false }; + }), + ); } } diff --git a/apps/api/src/domain/evolution-source.ts b/apps/api/src/domain/evolution-source.ts index 17837a8..f77b8d2 100644 --- a/apps/api/src/domain/evolution-source.ts +++ b/apps/api/src/domain/evolution-source.ts @@ -5,25 +5,85 @@ import { verificationPolicyDigest } from './verification-policy.js'; import { contentDigest } from '../../../../packages/evolution/src/index.js'; export interface CodeCandidateSource { - schemaVersion: 1; scope: 'tool' | 'platform' | 'trust_root'; sourceCommit: string; - baseCommit: string; gitBundleSha256: string; checkpointId: string; - artifactId?: string; verificationEvidenceId: string; changedPaths: string[]; - parentBundle: string; candidateDigest: string; + schemaVersion: 1; + scope: 'tool' | 'platform' | 'trust_root'; + sourceCommit: string; + baseCommit: string; + gitBundleSha256: string; + checkpointId: string; + artifactId?: string; + verificationEvidenceId: string; + changedPaths: string[]; + parentBundle: string; + candidateDigest: string; } // These paths are a protected platform policy, never accepted from a candidate. -const trustRootPath = /(^|\/)(auth\.ts|service-tokens\.ts|policy\.ts|provider-proxy\.ts|connections\.ts|entitlements\.ts|billing\.ts|runner-broker\.ts|releases\.ts|evolution\.ts|evolution-source\.ts|verification-policy\.ts|workspace-integration\.ts|store\.ts|001_initial\.sql|signing\.[^/]+)$|^(packages\/(evolution|contracts)\/|apps\/api\/src\/(auth|db)\/|infra\/k8s\/base\/)/; +const trustRootPath = + /(^|\/)(auth\.ts|service-tokens\.ts|policy\.ts|provider-proxy\.ts|connections\.ts|entitlements\.ts|billing\.ts|runner-broker\.ts|releases\.ts|evolution\.ts|evolution-source\.ts|verification-policy\.ts|workspace-integration\.ts|store\.ts|001_initial\.sql|signing\.[^/]+)$|^(packages\/(evolution|contracts)\/|apps\/api\/src\/(auth|db)\/|infra\/k8s\/base\/)/; export async function codeCandidateSource(tx: Transaction, run: Run): Promise { - if (run.purpose !== 'evolution' || run.noDeployment !== true || !['tool', 'platform'].includes(String(run.evolutionScope)) || !run.verifiedCommit || !run.checkpointId) throw new DomainError('UNVERIFIED_CODE_CANDIDATE', 'A verified code-generation task and checkpoint are required'); + if ( + run.purpose !== 'evolution' || + run.noDeployment !== true || + !['tool', 'platform'].includes(String(run.evolutionScope)) || + !run.verifiedCommit || + !run.checkpointId + ) + throw new DomainError('UNVERIFIED_CODE_CANDIDATE', 'A verified code-generation task and checkpoint are required'); const saved = await must(tx, 'checkpoint', run.checkpointId); const cp = saved.checkpoint as Record | undefined; const classification = saved.trustedClassification as { sourceCommit?: string; changedPaths?: string[] } | undefined; const project = await must(tx, 'project', run.projectId); - if (!cp || saved.revision !== run.revision || saved.runId !== run.id || cp.sourceCommit !== run.verifiedCommit || !/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(String(cp.baseCommit)) || !/^[a-f0-9]{64}$/.test(String(cp.gitBundleSha256)) || classification?.sourceCommit !== run.verifiedCommit || !Array.isArray(classification.changedPaths) || classification.changedPaths.length === 0 || classification.changedPaths.some(p => typeof p !== 'string' || !p || p.startsWith('/') || p.split('/').includes('..'))) throw new DomainError('CODE_SOURCE_REQUIRED', 'Code proposals require an exact durable checkpoint and independently classified nonempty source patch'); - const evidence = (await tx.list('evidence')).find(e => e.runId === run.id && e.revision === run.revision && e.sourceCommit === run.verifiedCommit && e.type === 'verification' && e.passed && e.verificationPolicyDigest === verificationPolicyDigest(project)); - if (!evidence || run.verifiedPolicyDigest !== verificationPolicyDigest(project)) throw new DomainError('STALE_EVIDENCE', 'Code proposal acceptance must match the current protected verification policy'); + if ( + !cp || + saved.revision !== run.revision || + saved.runId !== run.id || + cp.sourceCommit !== run.verifiedCommit || + !/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(String(cp.baseCommit)) || + !/^[a-f0-9]{64}$/.test(String(cp.gitBundleSha256)) || + classification?.sourceCommit !== run.verifiedCommit || + !Array.isArray(classification.changedPaths) || + classification.changedPaths.length === 0 || + classification.changedPaths.some( + p => typeof p !== 'string' || !p || p.startsWith('/') || p.split('/').includes('..'), + ) + ) + throw new DomainError( + 'CODE_SOURCE_REQUIRED', + 'Code proposals require an exact durable checkpoint and independently classified nonempty source patch', + ); + const evidence = (await tx.list('evidence')).find( + e => + e.runId === run.id && + e.revision === run.revision && + e.sourceCommit === run.verifiedCommit && + e.type === 'verification' && + e.passed && + e.verificationPolicyDigest === verificationPolicyDigest(project), + ); + if (!evidence || run.verifiedPolicyDigest !== verificationPolicyDigest(project)) + throw new DomainError( + 'STALE_EVIDENCE', + 'Code proposal acceptance must match the current protected verification policy', + ); const changedPaths = [...new Set(classification.changedPaths)].sort(); - const scope: CodeCandidateSource['scope'] = changedPaths.some(p => trustRootPath.test(p)) ? 'trust_root' : run.evolutionScope as 'tool' | 'platform'; - const binding = { schemaVersion: 1 as const, scope, sourceCommit: run.verifiedCommit, baseCommit: String(cp.baseCommit), gitBundleSha256: String(cp.gitBundleSha256), parentBundle: String(run.baselineBundle) }; - return { ...binding, checkpointId: saved.id, ...(typeof cp.gitBundleArtifactId === 'string' ? { artifactId: cp.gitBundleArtifactId } : {}), verificationEvidenceId: evidence.id, changedPaths, candidateDigest: contentDigest(binding) }; + const scope: CodeCandidateSource['scope'] = changedPaths.some(p => trustRootPath.test(p)) + ? 'trust_root' + : (run.evolutionScope as 'tool' | 'platform'); + const binding = { + schemaVersion: 1 as const, + scope, + sourceCommit: run.verifiedCommit, + baseCommit: String(cp.baseCommit), + gitBundleSha256: String(cp.gitBundleSha256), + parentBundle: String(run.baselineBundle), + }; + return { + ...binding, + checkpointId: saved.id, + ...(typeof cp.gitBundleArtifactId === 'string' ? { artifactId: cp.gitBundleArtifactId } : {}), + verificationEvidenceId: evidence.id, + changedPaths, + candidateDigest: contentDigest(binding), + }; } diff --git a/apps/api/src/domain/evolution.ts b/apps/api/src/domain/evolution.ts index 4a6baf9..fed00f0 100644 --- a/apps/api/src/domain/evolution.ts +++ b/apps/api/src/domain/evolution.ts @@ -1,207 +1,618 @@ -import { DomainError, evolutionInput, type Actor, type Entity, type EvolutionCandidate, type Run } from '@wishforge/contracts'; -import { bundleConfigSchema, bundleDigest, BASELINE_BUNDLE, canaryBucket, contentDigest, observationSchema, signedEvaluationSchema, verifyRecord, type AgentBundleConfig, type SignedEvaluationReport } from '../../../../packages/evolution/src/index.js'; +import { + DomainError, + evolutionInput, + type Actor, + type Entity, + type EvolutionCandidate, + type Run, +} from '@wishforge/contracts'; +import { + bundleConfigSchema, + bundleDigest, + BASELINE_BUNDLE, + canaryBucket, + contentDigest, + observationSchema, + signedEvaluationSchema, + verifyRecord, + type AgentBundleConfig, + type SignedEvaluationReport, +} from '../../../../packages/evolution/src/index.js'; import type { Store, Transaction } from '../db/store.js'; import { record, touch, must, executionLease } from './control-plane.js'; import { codeCandidateSource, type CodeCandidateSource } from './evolution-source.js'; import { evaluationGate, requireHuman, requireRole } from './policy.js'; -export interface Candidate extends EvolutionCandidate { sourceArtifact?: CodeCandidateSource; bundleConfig?: AgentBundleConfig; evaluation?: SignedEvaluationReport; shadowVerified?: boolean; canaryAudience?: 'internal' | 'opt_in'; observedRunIds?: string[]; approvalDigest?: string; externalRelease?: unknown; externalAction?: { id: string; action: 'rollout' | 'rollback'; status: 'requested' | 'observed'; deploymentId: string; imageDigest: string; allocationPercent: number; audience?: string; stageStartedAt?: string }; } -interface Bundle extends Entity { config: AgentBundleConfig; digest: string; } -interface Routing extends Entity { stableVersion: string; previousStableVersion?: string; } +export interface Candidate extends EvolutionCandidate { + sourceArtifact?: CodeCandidateSource; + bundleConfig?: AgentBundleConfig; + evaluation?: SignedEvaluationReport; + shadowVerified?: boolean; + canaryAudience?: 'internal' | 'opt_in'; + observedRunIds?: string[]; + approvalDigest?: string; + externalRelease?: unknown; + externalAction?: { + id: string; + action: 'rollout' | 'rollback'; + status: 'requested' | 'observed'; + deploymentId: string; + imageDigest: string; + allocationPercent: number; + audience?: string; + stageStartedAt?: string; + }; +} +interface Bundle extends Entity { + config: AgentBundleConfig; + digest: string; +} +interface Routing extends Entity { + stableVersion: string; + previousStableVersion?: string; +} const ROUTING_ID = 'evolution-routing-v1'; const candidateSchema = evolutionInput.extend({ bundleConfig: bundleConfigSchema.optional() }); export async function resolveBundle(tx: Transaction, version: string): Promise { if (version === 'baseline-v1') return structuredClone(BASELINE_BUNDLE); const bundle = await tx.get('bundle', version); - if (!bundle || bundleDigest(bundle.config) !== version || bundle.digest !== version) throw new DomainError('BUNDLE_UNAVAILABLE', 'Pinned AgentBundle is absent or its immutable content changed'); + if (!bundle || bundleDigest(bundle.config) !== version || bundle.digest !== version) + throw new DomainError('BUNDLE_UNAVAILABLE', 'Pinned AgentBundle is absent or its immutable content changed'); return bundleConfigSchema.parse(bundle.config); } /** Called once when creating a run. Recovery resolves the saved version and never reselects. */ -export async function selectBundle(tx: Transaction, input: { tenantId: string; projectId: string; runId: string; canaryOptIn?: boolean; internalProject?: boolean }): Promise<{ version: string; config: AgentBundleConfig; candidateId?: string }> { +export async function selectBundle( + tx: Transaction, + input: { tenantId: string; projectId: string; runId: string; canaryOptIn?: boolean; internalProject?: boolean }, +): Promise<{ version: string; config: AgentBundleConfig; candidateId?: string }> { const stable = (await tx.get('policy', ROUTING_ID))?.stableVersion ?? 'baseline-v1'; - const candidates = (await tx.list('evolution')).filter(c => c.status === 'canary' && c.parentBundle === stable && c.bundleConfig && c.scope !== 'platform' && c.scope !== 'trust_root'); - const candidate = candidates.find(c => (c.canaryAudience === 'internal' ? input.internalProject : input.canaryOptIn || input.internalProject) && canaryBucket(input.tenantId,input.projectId,input.runId,c.candidateDigest) < c.canaryPercent); + const candidates = (await tx.list('evolution')).filter( + c => + c.status === 'canary' && + c.parentBundle === stable && + c.bundleConfig && + c.scope !== 'platform' && + c.scope !== 'trust_root', + ); + const candidate = candidates.find( + c => + (c.canaryAudience === 'internal' ? input.internalProject : input.canaryOptIn || input.internalProject) && + canaryBucket(input.tenantId, input.projectId, input.runId, c.candidateDigest) < c.canaryPercent, + ); const version = candidate?.candidateDigest ?? stable; return { version, config: await resolveBundle(tx, version), ...(candidate ? { candidateId: candidate.id } : {}) }; } export class Evolution { - constructor(private store: Store, private env = process.env, private clock: () => number = Date.now) {} - list(actor: Actor) { return this.store.transaction(actor.tenantId, tx => tx.list('evolution')); } + constructor( + private store: Store, + private env = process.env, + private clock: () => number = Date.now, + ) {} + list(actor: Actor) { + return this.store.transaction(actor.tenantId, tx => tx.list('evolution')); + } async createCodeCandidate(actor: Actor, runId: string, leaseToken: number) { - if (actor.kind !== 'verifier' || actor.tenantId !== this.env.WF_SYSTEM_TENANT) throw new DomainError('FORBIDDEN', 'System independent verifier required', 403); + if (actor.kind !== 'verifier' || actor.tenantId !== this.env.WF_SYSTEM_TENANT) + throw new DomainError('FORBIDDEN', 'System independent verifier required', 403); const prepared = await this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', runId); executionLease(actor, run, leaseToken); - if (run.generatedEvolutionCandidateId) return { previous: await must(tx, 'evolution', String(run.generatedEvolutionCandidateId)) }; - if (run.status !== 'running') throw new DomainError('RUN_STOPPED', 'Candidate extraction requires an active verifier'); + const run = await must(tx, 'run', runId); + executionLease(actor, run, leaseToken); + if (run.generatedEvolutionCandidateId) + return { previous: await must(tx, 'evolution', String(run.generatedEvolutionCandidateId)) }; + if (run.status !== 'running') + throw new DomainError('RUN_STOPPED', 'Candidate extraction requires an active verifier'); return { run, artifact: await codeCandidateSource(tx, run) }; }); if (prepared.previous) return prepared.previous; const { run, artifact } = prepared; - return this.create(actor, { name: `Verified ${artifact!.scope} patch ${run!.id.slice(0, 8)}`, hypothesis: run!.goal.slice(0, 4000), scope: artifact!.scope, parentBundle: artifact!.parentBundle, candidateDigest: artifact!.candidateDigest, targetMetric: 'success', dataScope: 'platform' }, { runId, leaseToken, codeArtifact: artifact! }); + return this.create( + actor, + { + name: `Verified ${artifact!.scope} patch ${run!.id.slice(0, 8)}`, + hypothesis: run!.goal.slice(0, 4000), + scope: artifact!.scope, + parentBundle: artifact!.parentBundle, + candidateDigest: artifact!.candidateDigest, + targetMetric: 'success', + dataScope: 'platform', + }, + { runId, leaseToken, codeArtifact: artifact! }, + ); } - create(actor: Actor, input: unknown, source?: { runId: string; leaseToken: number; codeArtifact?: CodeCandidateSource }) { - requireRole(actor, ['owner','admin']); const data = candidateSchema.parse(input); - if (data.dataScope === 'platform' && actor.tenantId !== this.env.WF_SYSTEM_TENANT) throw new DomainError('CROSS_TENANT_LEARNING_DISABLED', 'Global evolution requires the separately funded platform tenant',403); + create( + actor: Actor, + input: unknown, + source?: { runId: string; leaseToken: number; codeArtifact?: CodeCandidateSource }, + ) { + requireRole(actor, ['owner', 'admin']); + const data = candidateSchema.parse(input); + if (data.dataScope === 'platform' && actor.tenantId !== this.env.WF_SYSTEM_TENANT) + throw new DomainError( + 'CROSS_TENANT_LEARNING_DISABLED', + 'Global evolution requires the separately funded platform tenant', + 403, + ); if (data.bundleConfig) { - if (!['prompt','orchestration'].includes(data.scope)) throw new DomainError('BUNDLE_SCOPE', 'Executable code and trust roots cannot be installed through prompt bundle configuration'); - if (bundleDigest(data.bundleConfig) !== data.candidateDigest) throw new DomainError('BUNDLE_DIGEST_MISMATCH', 'Candidate digest must address the complete bundle configuration'); - if (data.bundleConfig.model && !(this.env.WF_EVOLUTION_ALLOWED_MODELS ?? '').split(',').map(v => v.trim()).includes(data.bundleConfig.model)) throw new DomainError('MODEL_NOT_PREAUTHORIZED', 'Candidate model requires operator preauthorization',403); + if (!['prompt', 'orchestration'].includes(data.scope)) + throw new DomainError( + 'BUNDLE_SCOPE', + 'Executable code and trust roots cannot be installed through prompt bundle configuration', + ); + if (bundleDigest(data.bundleConfig) !== data.candidateDigest) + throw new DomainError( + 'BUNDLE_DIGEST_MISMATCH', + 'Candidate digest must address the complete bundle configuration', + ); + if ( + data.bundleConfig.model && + !(this.env.WF_EVOLUTION_ALLOWED_MODELS ?? '') + .split(',') + .map(v => v.trim()) + .includes(data.bundleConfig.model) + ) + throw new DomainError('MODEL_NOT_PREAUTHORIZED', 'Candidate model requires operator preauthorization', 403); } return this.store.transaction(actor.tenantId, async tx => { let generationRun: Run | undefined; if (source) { - if (actor.kind !== 'verifier' || actor.tenantId !== this.env.WF_SYSTEM_TENANT) throw new DomainError('FORBIDDEN', 'Candidate extraction requires the system verifier', 403); - generationRun = await must(tx, 'run', source.runId); executionLease(actor, generationRun, source.leaseToken); - if (generationRun.purpose !== 'evolution' || generationRun.noDeployment !== true || !generationRun.verifiedCommit || data.parentBundle !== generationRun.baselineBundle || data.scope !== (source.codeArtifact?.scope ?? generationRun.evolutionScope)) throw new DomainError('UNVERIFIED_CANDIDATE', 'Candidate must match the independently verified evolution task and fixed baseline'); - if (source.codeArtifact && contentDigest(await codeCandidateSource(tx, generationRun)) !== contentDigest(source.codeArtifact)) throw new DomainError('STALE_EVIDENCE', 'Code candidate source changed while recording'); - if (generationRun.generatedEvolutionCandidateId) { const previous = await must(tx, 'evolution', String(generationRun.generatedEvolutionCandidateId)); if (previous.candidateDigest !== data.candidateDigest) throw new DomainError('IDEMPOTENCY_CONFLICT', 'This generation task already produced a different candidate'); return previous; } + if (actor.kind !== 'verifier' || actor.tenantId !== this.env.WF_SYSTEM_TENANT) + throw new DomainError('FORBIDDEN', 'Candidate extraction requires the system verifier', 403); + generationRun = await must(tx, 'run', source.runId); + executionLease(actor, generationRun, source.leaseToken); + if ( + generationRun.purpose !== 'evolution' || + generationRun.noDeployment !== true || + !generationRun.verifiedCommit || + data.parentBundle !== generationRun.baselineBundle || + data.scope !== (source.codeArtifact?.scope ?? generationRun.evolutionScope) + ) + throw new DomainError( + 'UNVERIFIED_CANDIDATE', + 'Candidate must match the independently verified evolution task and fixed baseline', + ); + if ( + source.codeArtifact && + contentDigest(await codeCandidateSource(tx, generationRun)) !== contentDigest(source.codeArtifact) + ) + throw new DomainError('STALE_EVIDENCE', 'Code candidate source changed while recording'); + if (generationRun.generatedEvolutionCandidateId) { + const previous = await must(tx, 'evolution', String(generationRun.generatedEvolutionCandidateId)); + if (previous.candidateDigest !== data.candidateDigest) + throw new DomainError( + 'IDEMPOTENCY_CONFLICT', + 'This generation task already produced a different candidate', + ); + return previous; + } } - const stable = (await tx.get('policy',ROUTING_ID))?.stableVersion ?? 'baseline-v1'; - if (data.bundleConfig && data.parentBundle !== stable) throw new DomainError('BASELINE_CHANGED', 'Evaluate against the current stable bundle'); + const stable = (await tx.get('policy', ROUTING_ID))?.stableVersion ?? 'baseline-v1'; + if (data.bundleConfig && data.parentBundle !== stable) + throw new DomainError('BASELINE_CHANGED', 'Evaluate against the current stable bundle'); if (data.bundleConfig) { - const existing = await tx.get('bundle',data.candidateDigest); - if (existing && contentDigest(existing.config) !== contentDigest(data.bundleConfig)) throw new DomainError('IMMUTABLE_BUNDLE', 'Bundle content is immutable'); - if (!existing) await tx.put('bundle',{ ...record(actor.tenantId),id: data.candidateDigest,digest:data.candidateDigest,config:data.bundleConfig }); + const existing = await tx.get('bundle', data.candidateDigest); + if (existing && contentDigest(existing.config) !== contentDigest(data.bundleConfig)) + throw new DomainError('IMMUTABLE_BUNDLE', 'Bundle content is immutable'); + if (!existing) + await tx.put('bundle', { + ...record(actor.tenantId), + id: data.candidateDigest, + digest: data.candidateDigest, + config: data.bundleConfig, + }); } - const c: Candidate = { ...record(actor.tenantId),...data,...(source?.codeArtifact ? { sourceArtifact: source.codeArtifact } : {}),status:'proposed',canaryPercent:0,canaryCompletedRuns:0,canaryFailures:0,observedRunIds:[] }; - await tx.put('evolution',c); + const c: Candidate = { + ...record(actor.tenantId), + ...data, + ...(source?.codeArtifact ? { sourceArtifact: source.codeArtifact } : {}), + status: 'proposed', + canaryPercent: 0, + canaryCompletedRuns: 0, + canaryFailures: 0, + observedRunIds: [], + }; + await tx.put('evolution', c); if (generationRun) { - if (generationRun.providerProxyActiveRequest) throw new DomainError('MODEL_USAGE_UNCONFIRMED', 'Model usage must settle before recording a generated candidate'); generationRun.generatedEvolutionCandidateId = c.id; generationRun.reservedTokens = 0; generationRun.status = 'waiting'; generationRun.waitingReason = 'evolution_evaluation'; - if (generationRun.lastActiveAt) generationRun.activeMilliseconds += Math.max(0, Math.min(this.clock(), Date.parse(generationRun.lease!.expiresAt)) - Date.parse(generationRun.lastActiveAt)); + if (generationRun.providerProxyActiveRequest) + throw new DomainError( + 'MODEL_USAGE_UNCONFIRMED', + 'Model usage must settle before recording a generated candidate', + ); + generationRun.generatedEvolutionCandidateId = c.id; + generationRun.reservedTokens = 0; + generationRun.status = 'waiting'; + generationRun.waitingReason = 'evolution_evaluation'; + if (generationRun.lastActiveAt) + generationRun.activeMilliseconds += Math.max( + 0, + Math.min(this.clock(), Date.parse(generationRun.lease!.expiresAt)) - Date.parse(generationRun.lastActiveAt), + ); generationRun.lastActiveAt = undefined; - await tx.put('run', touch(generationRun)); await tx.event(generationRun.id, 'evolution.candidate.generated', 'Verified candidate recorded; independent evaluation and promotion are still required.', { candidateId: c.id, candidateDigest: c.candidateDigest }); + await tx.put('run', touch(generationRun)); + await tx.event( + generationRun.id, + 'evolution.candidate.generated', + 'Verified candidate recorded; independent evaluation and promotion are still required.', + { candidateId: c.id, candidateDigest: c.candidateDigest }, + ); } return c; }); } - private verify(input: { signature: string; keyId: string; issuedAt: string; tenantId: string; candidateId: string; candidateDigest: string; [key: string]: unknown }, actor: Actor, candidate: Candidate, platform = false) { - let keys: Record = {}; - try { keys = JSON.parse((platform ? this.env.WF_PLATFORM_RELEASE_PUBLIC_KEYS : this.env.WF_EVALUATOR_PUBLIC_KEYS) ?? '{}'); } catch { throw new DomainError('EVALUATOR_UNCONFIGURED','Operator verification key configuration is invalid',503); } + private verify( + input: { + signature: string; + keyId: string; + issuedAt: string; + tenantId: string; + candidateId: string; + candidateDigest: string; + [key: string]: unknown; + }, + actor: Actor, + candidate: Candidate, + platform = false, + ) { + let keys: Record = {}; + try { + keys = JSON.parse( + (platform ? this.env.WF_PLATFORM_RELEASE_PUBLIC_KEYS : this.env.WF_EVALUATOR_PUBLIC_KEYS) ?? '{}', + ); + } catch { + throw new DomainError('EVALUATOR_UNCONFIGURED', 'Operator verification key configuration is invalid', 503); + } const key = keys[input.keyId]; - if (!key) throw new DomainError('EVALUATOR_UNCONFIGURED','No trusted Ed25519 verification key is configured for this issuer',503); - if (!verifyRecord(input,key)) throw new DomainError('INVALID_EVALUATION_SIGNATURE','Independent evidence signature is invalid',403); - if (input.tenantId !== actor.tenantId || input.candidateId !== candidate.id || input.candidateDigest !== candidate.candidateDigest) throw new DomainError('EVIDENCE_BINDING_MISMATCH','Evidence is bound to a different tenant, candidate or version'); - const age = this.clock()-Date.parse(input.issuedAt); - if (age < -60_000 || age > 7*86400000) throw new DomainError('EVIDENCE_EXPIRED','Evidence must have been issued within the last seven days'); + if (!key) + throw new DomainError( + 'EVALUATOR_UNCONFIGURED', + 'No trusted Ed25519 verification key is configured for this issuer', + 503, + ); + if (!verifyRecord(input, key)) + throw new DomainError('INVALID_EVALUATION_SIGNATURE', 'Independent evidence signature is invalid', 403); + if ( + input.tenantId !== actor.tenantId || + input.candidateId !== candidate.id || + input.candidateDigest !== candidate.candidateDigest + ) + throw new DomainError( + 'EVIDENCE_BINDING_MISMATCH', + 'Evidence is bound to a different tenant, candidate or version', + ); + const age = this.clock() - Date.parse(input.issuedAt); + if (age < -60_000 || age > 7 * 86400000) + throw new DomainError('EVIDENCE_EXPIRED', 'Evidence must have been issued within the last seven days'); } evaluate(actor: Actor, id: string, input: unknown) { - if (actor.kind !== 'evaluator') throw new DomainError('FORBIDDEN','Independent evaluator identity required',403); + if (actor.kind !== 'evaluator') throw new DomainError('FORBIDDEN', 'Independent evaluator identity required', 403); const report = signedEvaluationSchema.parse(input); - return this.store.transaction(actor.tenantId,async tx => { - const c = await must(tx,'evolution',id); this.verify(report,actor,c); - if(report.targetMetric!==c.targetMetric)throw new DomainError('EVIDENCE_BINDING_MISMATCH','Evaluation target metric differs from the declared hypothesis'); - if (!['proposed','evaluating','paused'].includes(c.status)) throw new DomainError('INVALID_CANDIDATE_STATE','Candidate is not awaiting evaluation'); - const failures = evaluationGate(report,c); c.evaluation=report; c.evaluationFailures=failures; c.status=failures.length?'rejected':'shadowing'; - c.shadowVerified=false; c.approvedBy=undefined; c.approvalDigest=undefined; - await tx.put('evolution',touch(c)); return c; + return this.store.transaction(actor.tenantId, async tx => { + const c = await must(tx, 'evolution', id); + this.verify(report, actor, c); + if (report.targetMetric !== c.targetMetric) + throw new DomainError( + 'EVIDENCE_BINDING_MISMATCH', + 'Evaluation target metric differs from the declared hypothesis', + ); + if (!['proposed', 'evaluating', 'paused'].includes(c.status)) + throw new DomainError('INVALID_CANDIDATE_STATE', 'Candidate is not awaiting evaluation'); + const failures = evaluationGate(report, c); + c.evaluation = report; + c.evaluationFailures = failures; + c.status = failures.length ? 'rejected' : 'shadowing'; + c.shadowVerified = false; + c.approvedBy = undefined; + c.approvalDigest = undefined; + await tx.put('evolution', touch(c)); + return c; }); } private requestExternal(c: Candidate, action: 'rollout' | 'rollback') { - const release = c.externalRelease as { deploymentId: string; imageDigest: string; previousImageDigest?: string } | undefined; - if (!release || (action === 'rollback' && !release.previousImageDigest)) throw new DomainError('EXTERNAL_RELEASE_REQUIRED', 'Independent release identity and a compatible previous image are required'); - const request = { action, status: 'requested' as const, deploymentId: release.deploymentId, imageDigest: action === 'rollback' ? release.previousImageDigest! : release.imageDigest, allocationPercent: action === 'rollback' ? 0 : c.canaryPercent, audience: c.canaryAudience, stageStartedAt: c.canaryStartedAt }; + const release = c.externalRelease as + { deploymentId: string; imageDigest: string; previousImageDigest?: string } | undefined; + if (!release || (action === 'rollback' && !release.previousImageDigest)) + throw new DomainError( + 'EXTERNAL_RELEASE_REQUIRED', + 'Independent release identity and a compatible previous image are required', + ); + const request = { + action, + status: 'requested' as const, + deploymentId: release.deploymentId, + imageDigest: action === 'rollback' ? release.previousImageDigest! : release.imageDigest, + allocationPercent: action === 'rollback' ? 0 : c.canaryPercent, + audience: c.canaryAudience, + stageStartedAt: c.canaryStartedAt, + }; c.externalAction = { ...request, id: contentDigest([c.id, request]) }; } async rolloutTick(actor: Actor) { - if (!this.env.WF_SYSTEM_TENANT || actor.tenantId !== this.env.WF_SYSTEM_TENANT || actor.runId || !['human', 'evaluator'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'System operator or independent evaluator required', 403); + if ( + !this.env.WF_SYSTEM_TENANT || + actor.tenantId !== this.env.WF_SYSTEM_TENANT || + actor.runId || + !['human', 'evaluator'].includes(actor.kind) + ) + throw new DomainError('FORBIDDEN', 'System operator or independent evaluator required', 403); requireRole(actor, ['owner', 'admin']); - if (this.env.WF_EVOLUTION_AUTO_PROMOTION !== 'true') return { status: 'unconfigured', reason: 'Automatic promotion requires operator preauthorization', outcomes: [] }; - const candidates = (await this.list(actor)).filter(c => c.scope !== 'trust_root' && ['shadowing', 'canary'].includes(c.status)).slice(0, 50); + if (this.env.WF_EVOLUTION_AUTO_PROMOTION !== 'true') + return { status: 'unconfigured', reason: 'Automatic promotion requires operator preauthorization', outcomes: [] }; + const candidates = (await this.list(actor)) + .filter(c => c.scope !== 'trust_root' && ['shadowing', 'canary'].includes(c.status)) + .slice(0, 50); const outcomes = []; for (const candidate of candidates) { - const action = candidate.status === 'shadowing' && candidate.shadowVerified ? 'start_canary' : candidate.status === 'canary' && candidate.canaryCompletedRuns >= 30 && candidate.canaryFailures === 0 && this.clock() - Date.parse(candidate.canaryStartedAt ?? '') >= 86400000 ? 'advance' : undefined; + const action = + candidate.status === 'shadowing' && candidate.shadowVerified + ? 'start_canary' + : candidate.status === 'canary' && + candidate.canaryCompletedRuns >= 30 && + candidate.canaryFailures === 0 && + this.clock() - Date.parse(candidate.canaryStartedAt ?? '') >= 86400000 + ? 'advance' + : undefined; if (!action) continue; - try { const updated = await this.command(actor, candidate.id, action); outcomes.push({ id: candidate.id, action, status: updated.status, percent: updated.canaryPercent }); } - catch (error) { if (!(error instanceof DomainError)) throw error; outcomes.push({ id: candidate.id, action, blocked: error.code }); } + try { + const updated = await this.command(actor, candidate.id, action); + outcomes.push({ id: candidate.id, action, status: updated.status, percent: updated.canaryPercent }); + } catch (error) { + if (!(error instanceof DomainError)) throw error; + outcomes.push({ id: candidate.id, action, blocked: error.code }); + } } return { status: 'processed', outcomes }; } - private binding(c: Candidate) { return contentDigest({ candidateDigest:c.candidateDigest,parentBundle:c.parentBundle,evaluation:c.evaluation,externalRelease:c.externalRelease }); } - async command(actor: Actor,id: string,action: string) { - requireRole(actor,['owner','admin']); - return this.store.transaction(actor.tenantId,async tx => { - const c=await must(tx,'evolution',id); - if (action === 'approve') { requireHuman(actor); if(c.scope==='trust_root')throw new DomainError('EXTERNAL_TRUST_ROOT_RELEASE_REQUIRED','Trust root changes use the separate operator release channel'); c.approvedBy=actor.userId;c.approvalDigest=this.binding(c); } - else if(action === 'start_shadow') { if(!['proposed','evaluating','paused'].includes(c.status)||!c.evaluation||evaluationGate(c.evaluation,c).length)throw new DomainError('EVALUATION_REQUIRED','Independent evaluation must pass before shadowing');c.status='shadowing';c.shadowVerified=false; } - else if(action === 'start_canary') { - if(c.status!=='shadowing'||!c.shadowVerified||!c.evaluation||evaluationGate(c.evaluation,c).length)throw new DomainError('SHADOW_EVIDENCE_REQUIRED','Signed no-production-write shadow evidence and passing evaluation are required'); - if(c.scope==='trust_root')throw new DomainError('EXTERNAL_TRUST_ROOT_RELEASE_REQUIRED','Trust roots are outside the automatic rollout channel'); - if(!c.bundleConfig&&(!c.externalRelease||!c.approvedBy||c.approvalDigest!==this.binding(c)))throw new DomainError('EXTERNAL_RELEASE_REQUIRED','Code changes need independent release evidence and artifact-bound human approval'); - if(c.bundleConfig) { - const stable=(await tx.get('policy',ROUTING_ID))?.stableVersion??'baseline-v1'; - if(c.parentBundle!==stable)throw new DomainError('BASELINE_CHANGED','Reevaluate against the new stable version'); - if((await tx.list('evolution')).some(other=>other.id!==id&&other.status==='canary'))throw new DomainError('CANARY_BUSY','Another candidate already occupies the canary channel'); - } - if (!c.bundleConfig && !(c.externalRelease as {previousImageDigest?:string})?.previousImageDigest) throw new DomainError('EXTERNAL_RELEASE_REQUIRED', 'Code canary requires the preceding compatible stable image'); - c.status='canary';c.canaryPercent=5;c.canaryAudience='internal';c.canaryCompletedRuns=0;c.canaryFailures=0;c.observedRunIds=[];c.canaryStartedAt=new Date(this.clock()).toISOString(); - if (!c.bundleConfig) this.requestExternal(c, 'rollout'); - } else if(action==='advance') { - if(c.status!=='canary'||!c.canaryStartedAt||this.clock()-Date.parse(c.canaryStartedAt)<86400000||c.canaryCompletedRuns<30||c.canaryFailures>0)throw new DomainError('CANARY_EVIDENCE_REQUIRED','Each audience and percentage stage needs 24 hours, 30 accepted tasks and no gate failures'); - if(c.canaryAudience==='internal'){c.canaryAudience='opt_in';c.canaryPercent=5;} - else if(c.canaryPercent<100)c.canaryPercent=c.canaryPercent===5?25:100; - else { - c.status='promoted'; - if(c.bundleConfig){ const routing=await tx.get('policy',ROUTING_ID);await tx.put('policy',{...(routing?touch(routing):record(actor.tenantId)),id:ROUTING_ID,stableVersion:c.candidateDigest,previousStableVersion:c.parentBundle}); } - } - c.canaryCompletedRuns=0;c.canaryStartedAt=new Date(this.clock()).toISOString(); - if (!c.bundleConfig && c.status === 'canary') this.requestExternal(c, 'rollout'); - } else if(action==='rollback') { - if (!c.bundleConfig) { this.requestExternal(c, 'rollback'); c.status='paused'; c.canaryPercent=0; await tx.put('evolution',touch(c)); return c; } - c.status='rolled_back';c.canaryPercent=0; - const routing=await tx.get('policy',ROUTING_ID); - if(routing?.stableVersion===c.candidateDigest){routing.stableVersion=c.parentBundle;await resolveBundle(tx,routing.stableVersion);await tx.put('policy',touch(routing));} - } else throw new DomainError('INVALID_COMMAND','Unknown evolution action',400); - await tx.put('evolution',touch(c));return c; + private binding(c: Candidate) { + return contentDigest({ + candidateDigest: c.candidateDigest, + parentBundle: c.parentBundle, + evaluation: c.evaluation, + externalRelease: c.externalRelease, }); } - async observe(actor: Actor,id: string,input: unknown) { - if(actor.kind!=='evaluator')throw new DomainError('FORBIDDEN','Independent evaluator required',403); - const observation=observationSchema.parse(input); - return this.store.transaction(actor.tenantId,async tx=>{ - const c=await must(tx,'evolution',id);this.verify(observation,actor,c,observation.kind.startsWith('platform_')); - const key=`evolution:${id}:${observation.operationId}`,hash=contentDigest(observation),existing=await tx.idempotency(key); - if(existing){if(existing.digest!==hash)throw new DomainError('IDEMPOTENCY_CONFLICT','Observation ID reused with different evidence');return c;} - if(observation.kind==='shadow') { - if(c.status!=='shadowing')throw new DomainError('INVALID_CANDIDATE_STATE','Candidate is not shadowing'); - c.shadowVerified=observation.hardGateFailures===0;c.shadowEvidence=observation; - if(observation.hardGateFailures)c.status='rejected'; - } else if(observation.kind==='platform_release') { - if(c.bundleConfig||c.scope==='trust_root')throw new DomainError('INVALID_CANDIDATE_STATE','External code evidence cannot change bundle or trust-root permissions'); - if(c.sourceArtifact && observation.sourceCommit !== c.sourceArtifact.sourceCommit) throw new DomainError('EVIDENCE_BINDING_MISMATCH','Release must build the independently verified source proposal'); - if(observation.releaseDigest!==c.candidateDigest)throw new DomainError('EVIDENCE_BINDING_MISMATCH','External release digest differs from the candidate'); - c.externalRelease=observation;c.approvedBy=undefined;c.approvalDigest=undefined; + async command(actor: Actor, id: string, action: string) { + requireRole(actor, ['owner', 'admin']); + return this.store.transaction(actor.tenantId, async tx => { + const c = await must(tx, 'evolution', id); + if (action === 'approve') { + requireHuman(actor); + if (c.scope === 'trust_root') + throw new DomainError( + 'EXTERNAL_TRUST_ROOT_RELEASE_REQUIRED', + 'Trust root changes use the separate operator release channel', + ); + c.approvedBy = actor.userId; + c.approvalDigest = this.binding(c); + } else if (action === 'start_shadow') { + if ( + !['proposed', 'evaluating', 'paused'].includes(c.status) || + !c.evaluation || + evaluationGate(c.evaluation, c).length + ) + throw new DomainError('EVALUATION_REQUIRED', 'Independent evaluation must pass before shadowing'); + c.status = 'shadowing'; + c.shadowVerified = false; + } else if (action === 'start_canary') { + if (c.status !== 'shadowing' || !c.shadowVerified || !c.evaluation || evaluationGate(c.evaluation, c).length) + throw new DomainError( + 'SHADOW_EVIDENCE_REQUIRED', + 'Signed no-production-write shadow evidence and passing evaluation are required', + ); + if (c.scope === 'trust_root') + throw new DomainError( + 'EXTERNAL_TRUST_ROOT_RELEASE_REQUIRED', + 'Trust roots are outside the automatic rollout channel', + ); + if (!c.bundleConfig && (!c.externalRelease || !c.approvedBy || c.approvalDigest !== this.binding(c))) + throw new DomainError( + 'EXTERNAL_RELEASE_REQUIRED', + 'Code changes need independent release evidence and artifact-bound human approval', + ); + if (c.bundleConfig) { + const stable = (await tx.get('policy', ROUTING_ID))?.stableVersion ?? 'baseline-v1'; + if (c.parentBundle !== stable) + throw new DomainError('BASELINE_CHANGED', 'Reevaluate against the new stable version'); + if ((await tx.list('evolution')).some(other => other.id !== id && other.status === 'canary')) + throw new DomainError('CANARY_BUSY', 'Another candidate already occupies the canary channel'); + } + if (!c.bundleConfig && !(c.externalRelease as { previousImageDigest?: string })?.previousImageDigest) + throw new DomainError( + 'EXTERNAL_RELEASE_REQUIRED', + 'Code canary requires the preceding compatible stable image', + ); + c.status = 'canary'; + c.canaryPercent = 5; + c.canaryAudience = 'internal'; + c.canaryCompletedRuns = 0; + c.canaryFailures = 0; + c.observedRunIds = []; + c.canaryStartedAt = new Date(this.clock()).toISOString(); + if (!c.bundleConfig) this.requestExternal(c, 'rollout'); + } else if (action === 'advance') { + if ( + c.status !== 'canary' || + !c.canaryStartedAt || + this.clock() - Date.parse(c.canaryStartedAt) < 86400000 || + c.canaryCompletedRuns < 30 || + c.canaryFailures > 0 + ) + throw new DomainError( + 'CANARY_EVIDENCE_REQUIRED', + 'Each audience and percentage stage needs 24 hours, 30 accepted tasks and no gate failures', + ); + if (c.canaryAudience === 'internal') { + c.canaryAudience = 'opt_in'; + c.canaryPercent = 5; + } else if (c.canaryPercent < 100) c.canaryPercent = c.canaryPercent === 5 ? 25 : 100; + else { + c.status = 'promoted'; + if (c.bundleConfig) { + const routing = await tx.get('policy', ROUTING_ID); + await tx.put('policy', { + ...(routing ? touch(routing) : record(actor.tenantId)), + id: ROUTING_ID, + stableVersion: c.candidateDigest, + previousStableVersion: c.parentBundle, + }); + } + } + c.canaryCompletedRuns = 0; + c.canaryStartedAt = new Date(this.clock()).toISOString(); + if (!c.bundleConfig && c.status === 'canary') this.requestExternal(c, 'rollout'); + } else if (action === 'rollback') { + if (!c.bundleConfig) { + this.requestExternal(c, 'rollback'); + c.status = 'paused'; + c.canaryPercent = 0; + await tx.put('evolution', touch(c)); + return c; + } + c.status = 'rolled_back'; + c.canaryPercent = 0; + const routing = await tx.get('policy', ROUTING_ID); + if (routing?.stableVersion === c.candidateDigest) { + routing.stableVersion = c.parentBundle; + await resolveBundle(tx, routing.stableVersion); + await tx.put('policy', touch(routing)); + } + } else throw new DomainError('INVALID_COMMAND', 'Unknown evolution action', 400); + await tx.put('evolution', touch(c)); + return c; + }); + } + async observe(actor: Actor, id: string, input: unknown) { + if (actor.kind !== 'evaluator') throw new DomainError('FORBIDDEN', 'Independent evaluator required', 403); + const observation = observationSchema.parse(input); + return this.store.transaction(actor.tenantId, async tx => { + const c = await must(tx, 'evolution', id); + this.verify(observation, actor, c, observation.kind.startsWith('platform_')); + const key = `evolution:${id}:${observation.operationId}`, + hash = contentDigest(observation), + existing = await tx.idempotency(key); + if (existing) { + if (existing.digest !== hash) + throw new DomainError('IDEMPOTENCY_CONFLICT', 'Observation ID reused with different evidence'); + return c; + } + if (observation.kind === 'shadow') { + if (c.status !== 'shadowing') throw new DomainError('INVALID_CANDIDATE_STATE', 'Candidate is not shadowing'); + c.shadowVerified = observation.hardGateFailures === 0; + c.shadowEvidence = observation; + if (observation.hardGateFailures) c.status = 'rejected'; + } else if (observation.kind === 'platform_release') { + if (c.bundleConfig || c.scope === 'trust_root') + throw new DomainError( + 'INVALID_CANDIDATE_STATE', + 'External code evidence cannot change bundle or trust-root permissions', + ); + if (c.sourceArtifact && observation.sourceCommit !== c.sourceArtifact.sourceCommit) + throw new DomainError( + 'EVIDENCE_BINDING_MISMATCH', + 'Release must build the independently verified source proposal', + ); + if (observation.releaseDigest !== c.candidateDigest) + throw new DomainError('EVIDENCE_BINDING_MISMATCH', 'External release digest differs from the candidate'); + c.externalRelease = observation; + c.approvedBy = undefined; + c.approvalDigest = undefined; } else if (observation.kind === 'platform_canary') { - const release = c.externalRelease as { deploymentId: string; imageDigest: string; sourceCommit: string } | undefined; - if (c.bundleConfig || c.status !== 'canary' || !release || observation.deploymentId !== release.deploymentId || observation.imageDigest !== release.imageDigest || observation.sourceCommit !== release.sourceCommit || observation.stageStartedAt !== c.canaryStartedAt || observation.allocationPercent !== c.canaryPercent || observation.audience !== c.canaryAudience) throw new DomainError('STALE_CANARY_OBSERVATION', 'Independent code rollout evidence must bind this exact artifact, deployment and stage'); + const release = c.externalRelease as + { deploymentId: string; imageDigest: string; sourceCommit: string } | undefined; + if ( + c.bundleConfig || + c.status !== 'canary' || + !release || + observation.deploymentId !== release.deploymentId || + observation.imageDigest !== release.imageDigest || + observation.sourceCommit !== release.sourceCommit || + observation.stageStartedAt !== c.canaryStartedAt || + observation.allocationPercent !== c.canaryPercent || + observation.audience !== c.canaryAudience + ) + throw new DomainError( + 'STALE_CANARY_OBSERVATION', + 'Independent code rollout evidence must bind this exact artifact, deployment and stage', + ); const seen = new Set(c.observedRunIds ?? []); for (const sample of observation.samples) { if (seen.has(sample.id)) continue; - if (Date.parse(sample.startedAt) < Date.parse(c.canaryStartedAt!) || Date.parse(sample.completedAt) < Date.parse(sample.startedAt) || Date.parse(sample.completedAt) > Date.parse(observation.issuedAt)) throw new DomainError('INVALID_CANARY_RUN', 'External sample timestamps must be within this stage and evidence window'); - seen.add(sample.id); c.canaryCompletedRuns += sample.passed ? 1 : 0; c.canaryFailures += sample.passed ? 0 : 1; + if ( + Date.parse(sample.startedAt) < Date.parse(c.canaryStartedAt!) || + Date.parse(sample.completedAt) < Date.parse(sample.startedAt) || + Date.parse(sample.completedAt) > Date.parse(observation.issuedAt) + ) + throw new DomainError( + 'INVALID_CANARY_RUN', + 'External sample timestamps must be within this stage and evidence window', + ); + seen.add(sample.id); + c.canaryCompletedRuns += sample.passed ? 1 : 0; + c.canaryFailures += sample.passed ? 0 : 1; } - c.observedRunIds = [...seen]; c.canaryFailures += observation.hardGateFailures; + c.observedRunIds = [...seen]; + c.canaryFailures += observation.hardGateFailures; if (c.externalAction?.action === 'rollout') c.externalAction.status = 'observed'; - if (c.canaryFailures > 0) { this.requestExternal(c, 'rollback'); c.status = 'paused'; c.canaryPercent = 0; } + if (c.canaryFailures > 0) { + this.requestExternal(c, 'rollback'); + c.status = 'paused'; + c.canaryPercent = 0; + } } else if (observation.kind === 'platform_rollback') { const action = c.externalAction; - if (c.bundleConfig || c.status !== 'paused' || !action || action.action !== 'rollback' || action.id !== observation.actionId || action.deploymentId !== observation.deploymentId || action.imageDigest !== observation.restoredImageDigest) throw new DomainError('EVIDENCE_BINDING_MISMATCH', 'Rollback receipt must match the pending independent rollback action'); - action.status = 'observed'; c.status = 'rolled_back'; c.canaryPercent = 0; c.externalRollbackEvidence = observation; + if ( + c.bundleConfig || + c.status !== 'paused' || + !action || + action.action !== 'rollback' || + action.id !== observation.actionId || + action.deploymentId !== observation.deploymentId || + action.imageDigest !== observation.restoredImageDigest + ) + throw new DomainError( + 'EVIDENCE_BINDING_MISMATCH', + 'Rollback receipt must match the pending independent rollback action', + ); + action.status = 'observed'; + c.status = 'rolled_back'; + c.canaryPercent = 0; + c.externalRollbackEvidence = observation; } else { - if(c.status!=='canary'||observation.stageStartedAt!==c.canaryStartedAt)throw new DomainError('STALE_CANARY_OBSERVATION','Observation is not for the active canary stage'); - if(!c.bundleConfig)throw new DomainError('EXTERNAL_CANARY_REQUIRED','Platform code rollout remains managed by the independent release system'); - const observed=new Set(c.observedRunIds??[]); - for(const runId of new Set(observation.runIds)) { - if(observed.has(runId))continue; - const run=await must(tx,'run',runId); - if(run.bundleVersion!==c.candidateDigest||run.evolutionCandidateId!==c.id||Date.parse(run.createdAt)(tx, 'run', runId); + if ( + run.bundleVersion !== c.candidateDigest || + run.evolutionCandidateId !== c.id || + Date.parse(run.createdAt) < Date.parse(c.canaryStartedAt!) || + !['succeeded', 'failed'].includes(run.status) + ) + throw new DomainError( + 'INVALID_CANARY_RUN', + 'Only terminal runs assigned this candidate during this stage can count', + ); + c.canaryCompletedRuns += run.status === 'succeeded' ? 1 : 0; + c.canaryFailures += run.status === 'failed' ? 1 : 0; + observed.add(runId); + } + c.observedRunIds = [...observed]; + c.canaryFailures += observation.hardGateFailures; + if (c.canaryFailures > 0) { + c.status = 'rolled_back'; + c.canaryPercent = 0; } - c.observedRunIds=[...observed];c.canaryFailures+=observation.hardGateFailures; - if(c.canaryFailures>0){c.status='rolled_back';c.canaryPercent=0;} } - await tx.put('evolution',touch(c));await tx.remember(key,hash,{accepted:true});return c; + await tx.put('evolution', touch(c)); + await tx.remember(key, hash, { accepted: true }); + return c; }); } } diff --git a/apps/api/src/domain/gateway-target.ts b/apps/api/src/domain/gateway-target.ts index 15d4629..957fc76 100644 --- a/apps/api/src/domain/gateway-target.ts +++ b/apps/api/src/domain/gateway-target.ts @@ -1,19 +1,148 @@ -import {readFile} from 'node:fs/promises'; -import {z} from 'zod'; -import {DomainError} from '@wishforge/contracts'; -const entry=z.object({internalUrl:z.string().max(2048),publicUrl:z.string().max(2048),tokenEnv:z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/)}).strict(); -const schema=z.object({schemaVersion:z.literal(1),tenants:z.record(z.string().regex(/^[A-Za-z0-9_-]{1,128}$/),entry)}).strict(); -export interface GatewayTarget{internalUrl:string;publicUrl:string;token:string} -function origin(raw:string,env:NodeJS.ProcessEnv){let value:URL;try{value=new URL(raw);}catch{throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Gateway URL is invalid',503);}if(value.username||value.password||value.pathname!=='/'||value.search||value.hash||!(value.protocol==='https:'||(env.NODE_ENV!=='production'&&value.protocol==='http:'&&['127.0.0.1','localhost','[::1]'].includes(value.hostname))))throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Gateway requires an HTTPS origin (development loopback excepted)',503);return value.origin;} -async function mapping(file:string){try{const bytes=await readFile(file);if(bytes.length>65536)throw Error();return schema.parse(JSON.parse(bytes.toString('utf8')));}catch{throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Operator tenant gateway map is unreadable or invalid',503);}} -export async function resolveGatewayTarget(env:NodeJS.ProcessEnv,tenantId:string):Promise{ - if(!/^[A-Za-z0-9_-]{1,128}$/.test(tenantId))throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Invalid gateway tenant scope',503); - let config:{internalUrl:string;publicUrl:string;tokenEnv:string}|undefined; - if(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE){const map=await mapping(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE);config=Object.prototype.hasOwnProperty.call(map.tenants,tenantId)?map.tenants[tenantId]:undefined;if(!config)return;for(const [otherId,other] of Object.entries(map.tenants)){if(otherId===tenantId)continue;if(origin(other.publicUrl,env)===origin(config.publicUrl,env)||(env[config.tokenEnv]&&env[config.tokenEnv]===env[other.tokenEnv]))throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Tenant gateways require distinct public origins and service credentials',503);}} - else{if(!env.WF_WORKSPACE_GATEWAY_BASE&&!env.WF_WORKSPACE_GATEWAY_TOKEN)return;const tenants=[...new Set((env.WF_DISPATCH_TENANTS??'').split(',').map(x=>x.trim()).filter(Boolean))];const bound=env.WF_WORKSPACE_GATEWAY_TENANT_ID??(tenants.length===1?tenants[0]:undefined);if(!bound)throw new DomainError('GATEWAY_TENANT_BINDING_REQUIRED','A legacy gateway must be explicitly bound to one tenant; configure a tenant gateway map for multiple tenants',503);if(bound!==tenantId)return;config={internalUrl:env.WF_WORKSPACE_GATEWAY_BASE??'',publicUrl:env.WF_IDE_PUBLIC_URL??env.WF_WORKSPACE_GATEWAY_BASE??'',tokenEnv:'WF_WORKSPACE_GATEWAY_TOKEN'};} - const token=env[config.tokenEnv];if(!token||token.length<32)throw new DomainError('GATEWAY_CONFIGURATION_INVALID','Gateway requires a tenant-scoped service token',503); - return{internalUrl:origin(config.internalUrl,env),publicUrl:origin(config.publicUrl,env),token}; +import { readFile } from 'node:fs/promises'; +import { z } from 'zod'; +import { DomainError } from '@wishforge/contracts'; +const entry = z + .object({ + internalUrl: z.string().max(2048), + publicUrl: z.string().max(2048), + tokenEnv: z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/), + }) + .strict(); +const schema = z + .object({ schemaVersion: z.literal(1), tenants: z.record(z.string().regex(/^[A-Za-z0-9_-]{1,128}$/), entry) }) + .strict(); +export interface GatewayTarget { + internalUrl: string; + publicUrl: string; + token: string; +} +function origin(raw: string, env: NodeJS.ProcessEnv) { + let value: URL; + try { + value = new URL(raw); + } catch { + throw new DomainError('GATEWAY_CONFIGURATION_INVALID', 'Gateway URL is invalid', 503); + } + if ( + value.username || + value.password || + value.pathname !== '/' || + value.search || + value.hash || + !( + value.protocol === 'https:' || + (env.NODE_ENV !== 'production' && + value.protocol === 'http:' && + ['127.0.0.1', 'localhost', '[::1]'].includes(value.hostname)) + ) + ) + throw new DomainError( + 'GATEWAY_CONFIGURATION_INVALID', + 'Gateway requires an HTTPS origin (development loopback excepted)', + 503, + ); + return value.origin; +} +async function mapping(file: string) { + try { + const bytes = await readFile(file); + if (bytes.length > 65536) throw Error(); + return schema.parse(JSON.parse(bytes.toString('utf8'))); + } catch { + throw new DomainError('GATEWAY_CONFIGURATION_INVALID', 'Operator tenant gateway map is unreadable or invalid', 503); + } +} +export async function resolveGatewayTarget( + env: NodeJS.ProcessEnv, + tenantId: string, +): Promise { + if (!/^[A-Za-z0-9_-]{1,128}$/.test(tenantId)) + throw new DomainError('GATEWAY_CONFIGURATION_INVALID', 'Invalid gateway tenant scope', 503); + let config: { internalUrl: string; publicUrl: string; tokenEnv: string } | undefined; + if (env.WF_WORKSPACE_GATEWAY_TARGETS_FILE) { + const map = await mapping(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE); + config = Object.prototype.hasOwnProperty.call(map.tenants, tenantId) ? map.tenants[tenantId] : undefined; + if (!config) return; + for (const [otherId, other] of Object.entries(map.tenants)) { + if (otherId === tenantId) continue; + if ( + origin(other.publicUrl, env) === origin(config.publicUrl, env) || + (env[config.tokenEnv] && env[config.tokenEnv] === env[other.tokenEnv]) + ) + throw new DomainError( + 'GATEWAY_CONFIGURATION_INVALID', + 'Tenant gateways require distinct public origins and service credentials', + 503, + ); + } + } else { + if (!env.WF_WORKSPACE_GATEWAY_BASE && !env.WF_WORKSPACE_GATEWAY_TOKEN) return; + const tenants = [ + ...new Set( + (env.WF_DISPATCH_TENANTS ?? '') + .split(',') + .map(x => x.trim()) + .filter(Boolean), + ), + ]; + const bound = env.WF_WORKSPACE_GATEWAY_TENANT_ID ?? (tenants.length === 1 ? tenants[0] : undefined); + if (!bound) + throw new DomainError( + 'GATEWAY_TENANT_BINDING_REQUIRED', + 'A legacy gateway must be explicitly bound to one tenant; configure a tenant gateway map for multiple tenants', + 503, + ); + if (bound !== tenantId) return; + config = { + internalUrl: env.WF_WORKSPACE_GATEWAY_BASE ?? '', + publicUrl: env.WF_IDE_PUBLIC_URL ?? env.WF_WORKSPACE_GATEWAY_BASE ?? '', + tokenEnv: 'WF_WORKSPACE_GATEWAY_TOKEN', + }; + } + const token = env[config.tokenEnv]; + if (!token || token.length < 32) + throw new DomainError('GATEWAY_CONFIGURATION_INVALID', 'Gateway requires a tenant-scoped service token', 503); + return { internalUrl: origin(config.internalUrl, env), publicUrl: origin(config.publicUrl, env), token }; } /** All known platform gateway origins are protected from untrusted application hosting. */ -export async function gatewayIdentityOrigins(env:NodeJS.ProcessEnv):Promise{if(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE){const map=await mapping(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE);return[...new Set(Object.values(map.tenants).flatMap(item=>[origin(item.internalUrl,env),origin(item.publicUrl,env)]))];}return[env.WF_WORKSPACE_GATEWAY_BASE,env.WF_IDE_PUBLIC_URL].filter((value):value is string=>!!value).map(value=>origin(value,env));} -export function validateGatewayLaunch(target:GatewayTarget,payload:unknown){const value=payload as {launchUrl?:unknown;expiresIn?:unknown};let url:URL;try{if(typeof value?.launchUrl!=='string')throw Error();url=new URL(value.launchUrl);}catch{throw new DomainError('INVALID_GATEWAY_LAUNCH','Gateway did not return a valid one-time launch URL',502);}const keys=[...url.searchParams.keys()];if(url.origin!==target.publicUrl||url.pathname!=='/session'||url.username||url.password||url.hash||keys.length!==1||keys[0]!=='code'||!/^[A-Za-z0-9_-]{40,128}$/.test(url.searchParams.get('code')??'')||value.expiresIn!==30)throw new DomainError('INVALID_GATEWAY_LAUNCH','One-time launch URL does not match the assigned tenant gateway',502);return{launchUrl:url.toString(),expiresIn:30};} +export async function gatewayIdentityOrigins(env: NodeJS.ProcessEnv): Promise { + if (env.WF_WORKSPACE_GATEWAY_TARGETS_FILE) { + const map = await mapping(env.WF_WORKSPACE_GATEWAY_TARGETS_FILE); + return [ + ...new Set( + Object.values(map.tenants).flatMap(item => [origin(item.internalUrl, env), origin(item.publicUrl, env)]), + ), + ]; + } + return [env.WF_WORKSPACE_GATEWAY_BASE, env.WF_IDE_PUBLIC_URL] + .filter((value): value is string => !!value) + .map(value => origin(value, env)); +} +export function validateGatewayLaunch(target: GatewayTarget, payload: unknown) { + const value = payload as { launchUrl?: unknown; expiresIn?: unknown }; + let url: URL; + try { + if (typeof value?.launchUrl !== 'string') throw Error(); + url = new URL(value.launchUrl); + } catch { + throw new DomainError('INVALID_GATEWAY_LAUNCH', 'Gateway did not return a valid one-time launch URL', 502); + } + const keys = [...url.searchParams.keys()]; + if ( + url.origin !== target.publicUrl || + url.pathname !== '/session' || + url.username || + url.password || + url.hash || + keys.length !== 1 || + keys[0] !== 'code' || + !/^[A-Za-z0-9_-]{40,128}$/.test(url.searchParams.get('code') ?? '') || + value.expiresIn !== 30 + ) + throw new DomainError( + 'INVALID_GATEWAY_LAUNCH', + 'One-time launch URL does not match the assigned tenant gateway', + 502, + ); + return { launchUrl: url.toString(), expiresIn: 30 }; +} diff --git a/apps/api/src/domain/git-storage.ts b/apps/api/src/domain/git-storage.ts index 9c32e78..21746d9 100644 --- a/apps/api/src/domain/git-storage.ts +++ b/apps/api/src/domain/git-storage.ts @@ -2,20 +2,65 @@ import { DomainError, type Actor, type Project, type Run } from '@wishforge/cont /** Only the trusted control plane can provision repositories or mint repository-scoped access. */ export class GitStorage { - constructor(private env = process.env, private fetcher: typeof fetch = fetch) {} - configured() { return Boolean(this.env.WF_GIT_SERVICE_URL && this.env.WF_GIT_ADMIN_TOKEN); } + constructor( + private env = process.env, + private fetcher: typeof fetch = fetch, + ) {} + configured() { + return Boolean(this.env.WF_GIT_SERVICE_URL && this.env.WF_GIT_ADMIN_TOKEN); + } private async request(route: string, body: unknown): Promise { - if (!this.configured()) throw new DomainError('GIT_STORAGE_UNCONFIGURED', 'Platform Git storage must be configured for new projects', 503); + if (!this.configured()) + throw new DomainError( + 'GIT_STORAGE_UNCONFIGURED', + 'Platform Git storage must be configured for new projects', + 503, + ); const base = new URL(this.env.WF_GIT_SERVICE_URL!); - if (base.protocol !== 'https:' && !(this.env.NODE_ENV !== 'production' && ['localhost', '127.0.0.1', '[::1]'].includes(base.hostname))) throw new DomainError('INSECURE_GIT_STORAGE', 'Git control transport requires HTTPS', 503); - const response = await this.fetcher(new URL(route, base), { method: 'POST', headers: { Authorization: `Bearer ${this.env.WF_GIT_ADMIN_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body), redirect: 'error', signal: AbortSignal.timeout(15000) }); - if (!response.ok) throw new DomainError('GIT_STORAGE_UNAVAILABLE', 'Repository storage operation failed; retry provisioning after checking the service', 503); + if ( + base.protocol !== 'https:' && + !(this.env.NODE_ENV !== 'production' && ['localhost', '127.0.0.1', '[::1]'].includes(base.hostname)) + ) + throw new DomainError('INSECURE_GIT_STORAGE', 'Git control transport requires HTTPS', 503); + const response = await this.fetcher(new URL(route, base), { + method: 'POST', + headers: { Authorization: `Bearer ${this.env.WF_GIT_ADMIN_TOKEN}`, 'Content-Type': 'application/json' }, + body: JSON.stringify(body), + redirect: 'error', + signal: AbortSignal.timeout(15000), + }); + if (!response.ok) + throw new DomainError( + 'GIT_STORAGE_UNAVAILABLE', + 'Repository storage operation failed; retry provisioning after checking the service', + 503, + ); return response.json() as Promise; } - provision(project: Project) { return this.request<{ repositoryUrl: string; initialCommit: string; defaultBranch: string }>('/repos', { tenantId: project.tenantId, projectId: project.id, defaultBranch: project.targetBranch }); } + provision(project: Project) { + return this.request<{ repositoryUrl: string; initialCommit: string; defaultBranch: string }>('/repos', { + tenantId: project.tenantId, + projectId: project.id, + defaultBranch: project.targetBranch, + }); + } async credential(actor: Actor, project: Project, run: Run) { - if (project.repositoryStorage !== 'platform' || !['worker', 'verifier'].includes(actor.kind) || !run.lease || Date.parse(run.lease.expiresAt) <= Date.now() || run.status !== 'running') return undefined; - if (actor.runId && actor.runId !== run.id) throw new DomainError('FORBIDDEN', 'Git access is scoped to the assigned run', 403); - return this.request<{ token: string; expiresAt: string }>('/tokens', { tenantId: project.tenantId, projectId: project.id, runId: run.id, permission: actor.kind === 'verifier' ? 'write' : 'read', ttlSeconds: 900 }); + if ( + project.repositoryStorage !== 'platform' || + !['worker', 'verifier'].includes(actor.kind) || + !run.lease || + Date.parse(run.lease.expiresAt) <= Date.now() || + run.status !== 'running' + ) + return undefined; + if (actor.runId && actor.runId !== run.id) + throw new DomainError('FORBIDDEN', 'Git access is scoped to the assigned run', 403); + return this.request<{ token: string; expiresAt: string }>('/tokens', { + tenantId: project.tenantId, + projectId: project.id, + runId: run.id, + permission: actor.kind === 'verifier' ? 'write' : 'read', + ttlSeconds: 900, + }); } } diff --git a/apps/api/src/domain/github.ts b/apps/api/src/domain/github.ts index 2ef7c51..5ad850d 100644 --- a/apps/api/src/domain/github.ts +++ b/apps/api/src/domain/github.ts @@ -1,171 +1,544 @@ import { createHash } from 'node:crypto'; import { z } from 'zod'; -import { DomainError, type Actor, type Entity, type Project, type Run, type GitHubDeliveryState } from '@wishforge/contracts'; -import { GitHubApp, GitHubError, operationMarker, type PullRequestIntent, type PullRequestSnapshot, type RepositoryScope } from '../../../../packages/github/src/index.js'; +import { + DomainError, + type Actor, + type Entity, + type Project, + type Run, + type GitHubDeliveryState, +} from '@wishforge/contracts'; +import { + GitHubApp, + GitHubError, + operationMarker, + type PullRequestIntent, + type PullRequestSnapshot, + type RepositoryScope, +} from '../../../../packages/github/src/index.js'; import type { Store, Transaction } from '../db/store.js'; import { executionLease, executionScope, must, projectAccess, record, touch } from './control-plane.js'; import { digest, requireHuman, requireRole } from './policy.js'; -const createInput = z.object({ operationId: z.string().regex(/^[a-zA-Z0-9_-]{8,100}$/), title: z.string().trim().min(1).max(200), body: z.string().max(20000), headBranch: z.string().min(1).max(200), baseBranch: z.string().min(1).max(200), expectedHeadSha: z.string().regex(/^[a-f0-9]{40}$/), expectedBaseSha: z.string().regex(/^[a-f0-9]{40}$/) }).strict(); -export interface GitHubOperation extends Entity { projectId: string; runId: string; revision: number; repositoryUrl: string; scope: RepositoryScope; intent: PullRequestIntent; inputDigest: string; status: 'creating' | 'unknown' | 'open' | 'closed' | 'failed' | 'merging' | 'merge_unknown' | 'merge_pending' | 'merged' | 'reverification_required' | 'stale'; snapshot?: PullRequestSnapshot; mergeAttempted?: boolean; mergeApproval?: { actorId: string; headSha: string; baseSha: string; revision: number; grantedAt: string }; errorCode?: string } +const createInput = z + .object({ + operationId: z.string().regex(/^[a-zA-Z0-9_-]{8,100}$/), + title: z.string().trim().min(1).max(200), + body: z.string().max(20000), + headBranch: z.string().min(1).max(200), + baseBranch: z.string().min(1).max(200), + expectedHeadSha: z.string().regex(/^[a-f0-9]{40}$/), + expectedBaseSha: z.string().regex(/^[a-f0-9]{40}$/), + }) + .strict(); +export interface GitHubOperation extends Entity { + projectId: string; + runId: string; + revision: number; + repositoryUrl: string; + scope: RepositoryScope; + intent: PullRequestIntent; + inputDigest: string; + status: + | 'creating' + | 'unknown' + | 'open' + | 'closed' + | 'failed' + | 'merging' + | 'merge_unknown' + | 'merge_pending' + | 'merged' + | 'reverification_required' + | 'stale'; + snapshot?: PullRequestSnapshot; + mergeAttempted?: boolean; + mergeApproval?: { actorId: string; headSha: string; baseSha: string; revision: number; grantedAt: string }; + errorCode?: string; +} export class GitHubIntegration { readonly app: GitHubApp; - constructor(private store: Store, env = process.env, app?: GitHubApp) { this.app = app ?? new GitHubApp(env); } + constructor( + private store: Store, + env = process.env, + app?: GitHubApp, + ) { + this.app = app ?? new GitHubApp(env); + } async status(actor: Actor, projectId: string) { const project = await this.store.transaction(actor.tenantId, tx => projectAccess(tx, actor, projectId)); if (!this.app.configured()) return { configured: false, enabled: false, reason: 'GITHUB_UNCONFIGURED' }; - try { const scope = await this.app.scope(actor.tenantId, project.repositoryUrl ?? ''); return { configured: true, enabled: true, repository: `${scope.owner}/${scope.name}`, allowedBaseBranches: scope.allowedBaseBranches, allowAutomaticMerge: scope.allowAutomaticMerge === true }; } - catch (error) { if (error instanceof GitHubError) return { configured: true, enabled: false, reason: error.code }; throw error; } + try { + const scope = await this.app.scope(actor.tenantId, project.repositoryUrl ?? ''); + return { + configured: true, + enabled: true, + repository: `${scope.owner}/${scope.name}`, + allowedBaseBranches: scope.allowedBaseBranches, + allowAutomaticMerge: scope.allowAutomaticMerge === true, + }; + } catch (error) { + if (error instanceof GitHubError) return { configured: true, enabled: false, reason: error.code }; + throw error; + } } async requiresPullRequest(project: Project): Promise { if (project.repositoryStorage === 'platform' || !project.repositoryUrl) return false; - let url: URL; try { url = new URL(project.repositoryUrl); } catch { return false; } + let url: URL; + try { + url = new URL(project.repositoryUrl); + } catch { + return false; + } if (url.hostname.toLowerCase() === 'github.com') return true; if (!this.app.configured()) return false; const configuration = await this.app.configuration(); return url.origin === new URL(configuration.webOrigin ?? 'https://github.com').origin; } async credential(actor: Actor, project: Project, run: Run) { - if (!['worker', 'verifier'].includes(actor.kind) || !run.lease || run.status !== 'running' || Date.parse(run.lease.expiresAt) <= Date.now()) return undefined; - if (!await this.requiresPullRequest(project)) return undefined; - await this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'run', run.id); executionLease(actor, current, run.lease!.token); const saved = await must(tx, 'project', current.projectId); if (saved.id !== project.id || saved.repositoryUrl !== project.repositoryUrl) throw new DomainError('GITHUB_SCOPE_CHANGED', 'Repository changed'); }); + if ( + !['worker', 'verifier'].includes(actor.kind) || + !run.lease || + run.status !== 'running' || + Date.parse(run.lease.expiresAt) <= Date.now() + ) + return undefined; + if (!(await this.requiresPullRequest(project))) return undefined; + await this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'run', run.id); + executionLease(actor, current, run.lease!.token); + const saved = await must(tx, 'project', current.projectId); + if (saved.id !== project.id || saved.repositoryUrl !== project.repositoryUrl) + throw new DomainError('GITHUB_SCOPE_CHANGED', 'Repository changed'); + }); try { - const scope = await this.app.scope(actor.tenantId, project.repositoryUrl ?? ''); const credential = await this.app.token(scope, actor.kind === 'verifier' ? 'write' : 'read'); - await this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'run', run.id); executionLease(actor, current, run.lease!.token); if (current.status !== 'running') throw new DomainError('RUN_STOPPED', 'Run stopped while issuing repository credentials'); }); + const scope = await this.app.scope(actor.tenantId, project.repositoryUrl ?? ''); + const credential = await this.app.token(scope, actor.kind === 'verifier' ? 'write' : 'read'); + await this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'run', run.id); + executionLease(actor, current, run.lease!.token); + if (current.status !== 'running') + throw new DomainError('RUN_STOPPED', 'Run stopped while issuing repository credentials'); + }); return { ...credential, repositoryUrl: project.repositoryUrl!, username: 'x-access-token' }; - } catch (error) { throw this.error(error); } + } catch (error) { + throw this.error(error); + } } private async access(tx: Transaction, actor: Actor, operation: GitHubOperation) { await projectAccess(tx, actor, operation.projectId); - if (!['human', 'connector'].includes(actor.kind)) executionScope(actor, await must(tx, 'run', operation.runId)); + if (!['human', 'connector'].includes(actor.kind)) + executionScope(actor, await must(tx, 'run', operation.runId)); + } + private writer(actor: Actor) { + if (actor.kind === 'verifier') return; + requireRole(actor, ['owner', 'admin', 'developer']); + if (!['human', 'connector'].includes(actor.kind)) + throw new DomainError( + 'FORBIDDEN', + 'Only a project developer or independent verifier may create a pull request', + 403, + ); } - private writer(actor: Actor) { if (actor.kind === 'verifier') return; requireRole(actor, ['owner', 'admin', 'developer']); if (!['human', 'connector'].includes(actor.kind)) throw new DomainError('FORBIDDEN', 'Only a project developer or independent verifier may create a pull request', 403); } private async currentScope(operation: GitHubOperation) { const scope = await this.app.scope(operation.tenantId, operation.repositoryUrl); - if (scope.repositoryId !== operation.scope.repositoryId || scope.installationId !== operation.scope.installationId || scope.apiBaseUrl !== operation.scope.apiBaseUrl) throw new GitHubError('GITHUB_SCOPE_CHANGED', 'Operator repository mapping changed'); + if ( + scope.repositoryId !== operation.scope.repositoryId || + scope.installationId !== operation.scope.installationId || + scope.apiBaseUrl !== operation.scope.apiBaseUrl + ) + throw new GitHubError('GITHUB_SCOPE_CHANGED', 'Operator repository mapping changed'); return scope; } private async verifierLease(actor: Actor, runId: string, leaseToken: number) { - if (actor.kind !== 'verifier') throw new DomainError('FORBIDDEN', 'Independent verifier required for GitHub delivery', 403); - return this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', runId); executionLease(actor, run, leaseToken); if (run.status !== 'running') throw new DomainError('RUN_STOPPED', 'GitHub delivery requires an active verifier'); return { run, project: await must(tx, 'project', run.projectId) }; }); + if (actor.kind !== 'verifier') + throw new DomainError('FORBIDDEN', 'Independent verifier required for GitHub delivery', 403); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', runId); + executionLease(actor, run, leaseToken); + if (run.status !== 'running') throw new DomainError('RUN_STOPPED', 'GitHub delivery requires an active verifier'); + return { run, project: await must(tx, 'project', run.projectId) }; + }); } private deliveryOperation(operation: GitHubOperation): NonNullable { - return { id: operation.id, revision: operation.revision, status: operation.status, headBranch: operation.intent.headBranch, baseBranch: operation.intent.baseBranch, expectedHeadSha: operation.intent.expectedHeadSha, expectedBaseSha: operation.intent.expectedBaseSha, mergeApproved: !!operation.mergeApproval, mergeAttempted: operation.mergeAttempted === true, pullRequestUrl: operation.snapshot?.url, mergedCommitSha: operation.snapshot?.merged ? operation.snapshot.mergeCommitSha ?? undefined : undefined }; + return { + id: operation.id, + revision: operation.revision, + status: operation.status, + headBranch: operation.intent.headBranch, + baseBranch: operation.intent.baseBranch, + expectedHeadSha: operation.intent.expectedHeadSha, + expectedBaseSha: operation.intent.expectedBaseSha, + mergeApproved: !!operation.mergeApproval, + mergeAttempted: operation.mergeAttempted === true, + pullRequestUrl: operation.snapshot?.url, + mergedCommitSha: operation.snapshot?.merged ? (operation.snapshot.mergeCommitSha ?? undefined) : undefined, + }; } async workerState(actor: Actor, runId: string, leaseToken: number): Promise { const { run, project } = await this.verifierLease(actor, runId, leaseToken); - if (!await this.requiresPullRequest(project)) return { required: false, enabled: false }; + if (!(await this.requiresPullRequest(project))) return { required: false, enabled: false }; const status = await this.status(actor, project.id); if (!status.enabled) return { required: true, enabled: false, reason: status.reason }; const operation = await this.store.transaction(actor.tenantId, async tx => { - const pinned = run.githubOperationId ? await tx.get('github_operation', String(run.githubOperationId)) : undefined; + const pinned = run.githubOperationId + ? await tx.get('github_operation', String(run.githubOperationId)) + : undefined; if (pinned?.runId === runId && pinned.revision === run.revision) return pinned; - return (await tx.list('github_operation')).filter(op => op.runId === runId && op.revision === run.revision).sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0]; + return (await tx.list('github_operation')) + .filter(op => op.runId === runId && op.revision === run.revision) + .sort((a, b) => b.createdAt.localeCompare(a.createdAt))[0]; }); - return { required: true, enabled: true, allowAutomaticMerge: status.allowAutomaticMerge === true, ...(operation ? { operation: this.deliveryOperation(operation) } : {}) }; + return { + required: true, + enabled: true, + allowAutomaticMerge: status.allowAutomaticMerge === true, + ...(operation ? { operation: this.deliveryOperation(operation) } : {}), + }; } async workerCreate(actor: Actor, runId: string, input: unknown): Promise { - const data = z.object({ leaseToken: z.number().int().positive(), candidateCommit: z.string().regex(/^[a-f0-9]{40}$/), expectedBaseCommit: z.string().regex(/^[a-f0-9]{40}$/), headBranch: z.string() }).strict().parse(input); + const data = z + .object({ + leaseToken: z.number().int().positive(), + candidateCommit: z.string().regex(/^[a-f0-9]{40}$/), + expectedBaseCommit: z.string().regex(/^[a-f0-9]{40}$/), + headBranch: z.string(), + }) + .strict() + .parse(input); const { run, project } = await this.verifierLease(actor, runId, data.leaseToken); - if (data.headBranch !== `wishforge/${runId}/${data.candidateCommit}` || run.verifiedCommit !== data.candidateCommit) throw new DomainError('GITHUB_UNVERIFIED_HEAD', 'Only the verified immutable run review branch can be submitted'); + if (data.headBranch !== `wishforge/${runId}/${data.candidateCommit}` || run.verifiedCommit !== data.candidateCommit) + throw new DomainError('GITHUB_UNVERIFIED_HEAD', 'Only the verified immutable run review branch can be submitted'); await this.store.transaction(actor.tenantId, async tx => { const checkpoint = run.checkpointId ? await tx.get('checkpoint', run.checkpointId) : undefined; const portable = checkpoint?.checkpoint as { sourceCommit?: string; baseCommit?: string } | undefined; - if (checkpoint?.runId !== runId || checkpoint.revision !== run.revision || portable?.sourceCommit !== data.candidateCommit || portable.baseCommit !== data.expectedBaseCommit) throw new DomainError('GITHUB_CHECKPOINT_MISMATCH', 'Review head and target must match the verified candidate checkpoint'); + if ( + checkpoint?.runId !== runId || + checkpoint.revision !== run.revision || + portable?.sourceCommit !== data.candidateCommit || + portable.baseCommit !== data.expectedBaseCommit + ) + throw new DomainError( + 'GITHUB_CHECKPOINT_MISMATCH', + 'Review head and target must match the verified candidate checkpoint', + ); + }); + const current = await this.workerState(actor, runId, data.leaseToken); + if (current.operation || !current.enabled) return current; + await this.createPullRequest(actor, runId, { + operationId: `candidate-${run.revision}-${data.candidateCommit}`, + title: `WishForge: ${run.goal.slice(0, 140)}`, + body: `Verified candidate for requirement revision ${run.revision}. Independent verification must run again on the actual merge commit before build or release.`, + headBranch: data.headBranch, + baseBranch: project.targetBranch, + expectedHeadSha: data.candidateCommit, + expectedBaseSha: data.expectedBaseCommit, }); - const current = await this.workerState(actor, runId, data.leaseToken); if (current.operation || !current.enabled) return current; - await this.createPullRequest(actor, runId, { operationId: `candidate-${run.revision}-${data.candidateCommit}`, title: `WishForge: ${run.goal.slice(0, 140)}`, body: `Verified candidate for requirement revision ${run.revision}. Independent verification must run again on the actual merge commit before build or release.`, headBranch: data.headBranch, baseBranch: project.targetBranch, expectedHeadSha: data.candidateCommit, expectedBaseSha: data.expectedBaseCommit }); return this.workerState(actor, runId, data.leaseToken); } async workerAdvance(actor: Actor, runId: string, input: unknown): Promise { - const data = z.object({ leaseToken: z.number().int().positive(), operationId: z.string().min(1) }).strict().parse(input); + const data = z + .object({ leaseToken: z.number().int().positive(), operationId: z.string().min(1) }) + .strict() + .parse(input); const { run } = await this.verifierLease(actor, runId, data.leaseToken); - const owned = await this.store.transaction(actor.tenantId, tx => must(tx, 'github_operation', data.operationId)); - if (owned.runId !== runId || owned.revision !== run.revision) throw new DomainError('GITHUB_OPERATION_SCOPE', 'Review belongs to another run or requirement revision', 403); + const owned = await this.store.transaction(actor.tenantId, tx => + must(tx, 'github_operation', data.operationId), + ); + if (owned.runId !== runId || owned.revision !== run.revision) + throw new DomainError('GITHUB_OPERATION_SCOPE', 'Review belongs to another run or requirement revision', 403); let operation = await this.reconcilePullRequest(actor, data.operationId); await this.verifierLease(actor, runId, data.leaseToken); if (!operation.snapshot?.merged && operation.status === 'open') { - try { operation = await this.merge(actor, operation.id); } - catch (error) { if (!(error instanceof DomainError && error.code === 'GITHUB_MERGE_NOT_AUTHORIZED')) throw error; return { ...(await this.workerState(actor, runId, data.leaseToken)), reason: 'github_merge_approval' }; } + try { + operation = await this.merge(actor, operation.id); + } catch (error) { + if (!(error instanceof DomainError && error.code === 'GITHUB_MERGE_NOT_AUTHORIZED')) throw error; + return { ...(await this.workerState(actor, runId, data.leaseToken)), reason: 'github_merge_approval' }; + } } - return { ...(await this.workerState(actor, runId, data.leaseToken)), operation: this.deliveryOperation(operation), reason: operation.snapshot?.merged ? undefined : ['unknown', 'merge_unknown', 'creating', 'merging'].includes(operation.status) ? 'github_outcome_unknown' : operation.status === 'stale' ? 'github_candidate_stale' : 'github_merge_pending' }; + return { + ...(await this.workerState(actor, runId, data.leaseToken)), + operation: this.deliveryOperation(operation), + reason: operation.snapshot?.merged + ? undefined + : ['unknown', 'merge_unknown', 'creating', 'merging'].includes(operation.status) + ? 'github_outcome_unknown' + : operation.status === 'stale' + ? 'github_candidate_stale' + : 'github_merge_pending', + }; } async createPullRequest(actor: Actor, runId: string, input: unknown) { - this.writer(actor); const data = createInput.parse(input); - const source = await this.store.transaction(actor.tenantId, async tx => { const run = await must(tx, 'run', runId); const project = await projectAccess(tx, actor, run.projectId); if (actor.kind === 'verifier') executionScope(actor, run); return { run, project }; }); - let scope: RepositoryScope; try { scope = await this.app.scope(actor.tenantId, source.project.repositoryUrl ?? ''); } catch (error) { throw this.error(error); } - if (data.baseBranch !== source.project.targetBranch || !scope.allowedBaseBranches.includes(data.baseBranch) || !(data.headBranch === `wishforge/${runId}` || data.headBranch.startsWith(`wishforge/${runId}/`))) throw new DomainError('GITHUB_BRANCH_DENIED', 'PR must use this run branch and the protected project target'); - const id = `gh-${createHash('sha256').update(JSON.stringify([actor.tenantId, runId, data.operationId])).digest('hex')}`; + this.writer(actor); + const data = createInput.parse(input); + const source = await this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', runId); + const project = await projectAccess(tx, actor, run.projectId); + if (actor.kind === 'verifier') executionScope(actor, run); + return { run, project }; + }); + let scope: RepositoryScope; + try { + scope = await this.app.scope(actor.tenantId, source.project.repositoryUrl ?? ''); + } catch (error) { + throw this.error(error); + } + if ( + data.baseBranch !== source.project.targetBranch || + !scope.allowedBaseBranches.includes(data.baseBranch) || + !(data.headBranch === `wishforge/${runId}` || data.headBranch.startsWith(`wishforge/${runId}/`)) + ) + throw new DomainError('GITHUB_BRANCH_DENIED', 'PR must use this run branch and the protected project target'); + const id = `gh-${createHash('sha256') + .update(JSON.stringify([actor.tenantId, runId, data.operationId])) + .digest('hex')}`; const prepared = await this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', runId); const project = await projectAccess(tx, actor, run.projectId); + const run = await must(tx, 'run', runId); + const project = await projectAccess(tx, actor, run.projectId); const previous = await tx.get('github_operation', id); - if (previous) { if (previous.inputDigest !== digest(data)) throw new DomainError('IDEMPOTENCY_CONFLICT', 'GitHub operation ID was already used for a different candidate'); return { operation: previous, dispatch: false }; } - if (run.verifiedCommit !== data.expectedHeadSha || run.revision !== source.run.revision || ['paused', 'canceled', 'failed'].includes(run.status) || project.repositoryUrl !== source.project.repositoryUrl) throw new DomainError('GITHUB_UNVERIFIED_HEAD', 'PR creation requires this exact independently verified candidate'); + if (previous) { + if (previous.inputDigest !== digest(data)) + throw new DomainError( + 'IDEMPOTENCY_CONFLICT', + 'GitHub operation ID was already used for a different candidate', + ); + return { operation: previous, dispatch: false }; + } + if ( + run.verifiedCommit !== data.expectedHeadSha || + run.revision !== source.run.revision || + ['paused', 'canceled', 'failed'].includes(run.status) || + project.repositoryUrl !== source.project.repositoryUrl + ) + throw new DomainError( + 'GITHUB_UNVERIFIED_HEAD', + 'PR creation requires this exact independently verified candidate', + ); const { operationId: _, ...rest } = data; - const operation: GitHubOperation = { ...record(actor.tenantId), id, projectId: project.id, runId, revision: run.revision, repositoryUrl: project.repositoryUrl!, scope, inputDigest: digest(data), intent: { ...rest, operationMarker: operationMarker(actor.tenantId, runId, data.operationId) }, status: 'creating' }; - await tx.put('github_operation', operation); run.githubOperationId = id; await tx.put('run', touch(run)); await tx.event(runId, 'github.pr.requested', 'Pull request creation recorded before contacting GitHub.', { operationId: id }); return { operation, dispatch: true }; + const operation: GitHubOperation = { + ...record(actor.tenantId), + id, + projectId: project.id, + runId, + revision: run.revision, + repositoryUrl: project.repositoryUrl!, + scope, + inputDigest: digest(data), + intent: { ...rest, operationMarker: operationMarker(actor.tenantId, runId, data.operationId) }, + status: 'creating', + }; + await tx.put('github_operation', operation); + run.githubOperationId = id; + await tx.put('run', touch(run)); + await tx.event(runId, 'github.pr.requested', 'Pull request creation recorded before contacting GitHub.', { + operationId: id, + }); + return { operation, dispatch: true }; }); if (!prepared.dispatch) return this.reconcilePullRequest(actor, id); - try { return await this.recordSnapshot(actor, id, await this.app.createPullRequest(scope, prepared.operation.intent)); } - catch (error) { await this.failure(actor, id, error, false); throw this.error(error); } + try { + return await this.recordSnapshot(actor, id, await this.app.createPullRequest(scope, prepared.operation.intent)); + } catch (error) { + await this.failure(actor, id, error, false); + throw this.error(error); + } + } + async readPullRequest(actor: Actor, id: string) { + return this.reconcilePullRequest(actor, id); } - async readPullRequest(actor: Actor, id: string) { return this.reconcilePullRequest(actor, id); } async reconcilePullRequest(actor: Actor, id: string) { - const operation = await this.store.transaction(actor.tenantId, async tx => { const found = await must(tx, 'github_operation', id); await this.access(tx, actor, found); return found; }); + const operation = await this.store.transaction(actor.tenantId, async tx => { + const found = await must(tx, 'github_operation', id); + await this.access(tx, actor, found); + return found; + }); try { const scope = await this.currentScope(operation); - const snapshot = operation.snapshot ? await this.app.readPullRequest(scope, operation.snapshot.number) : await this.app.reconcilePullRequest(scope, operation.intent); + const snapshot = operation.snapshot + ? await this.app.readPullRequest(scope, operation.snapshot.number) + : await this.app.reconcilePullRequest(scope, operation.intent); if (snapshot) return this.recordSnapshot(actor, id, snapshot); // An absence observed after timeout is not proof that POST never happened. - return this.store.transaction(actor.tenantId, async tx => { const current = await must(tx, 'github_operation', id); current.status = 'unknown'; await tx.put('github_operation', touch(current)); return current; }); - } catch (error) { throw this.error(error); } + return this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'github_operation', id); + current.status = 'unknown'; + await tx.put('github_operation', touch(current)); + return current; + }); + } catch (error) { + throw this.error(error); + } } async authorizeMerge(actor: Actor, id: string, input: { expectedHeadSha: string; expectedBaseSha: string }) { - requireRole(actor, ['owner', 'admin', 'deployer']); requireHuman(actor); + requireRole(actor, ['owner', 'admin', 'deployer']); + requireHuman(actor); const observed = await this.reconcilePullRequest(actor, id); return this.store.transaction(actor.tenantId, async tx => { - const operation = await must(tx, 'github_operation', id); await this.access(tx, actor, operation); const run = await must(tx, 'run', operation.runId); - if (operation.version !== observed.version || operation.mergeAttempted || operation.status !== 'open' || run.revision !== operation.revision || run.verifiedCommit !== input.expectedHeadSha || operation.snapshot?.headSha !== input.expectedHeadSha || operation.snapshot?.baseSha !== input.expectedBaseSha || operation.intent.expectedHeadSha !== input.expectedHeadSha || operation.intent.expectedBaseSha !== input.expectedBaseSha) throw new DomainError('GITHUB_APPROVAL_STALE', 'Approval must bind the current verified head and target SHA'); - operation.mergeApproval = { actorId: actor.userId, headSha: input.expectedHeadSha, baseSha: input.expectedBaseSha, revision: run.revision, grantedAt: new Date().toISOString() }; await tx.put('github_operation', touch(operation)); return operation; + const operation = await must(tx, 'github_operation', id); + await this.access(tx, actor, operation); + const run = await must(tx, 'run', operation.runId); + if ( + operation.version !== observed.version || + operation.mergeAttempted || + operation.status !== 'open' || + run.revision !== operation.revision || + run.verifiedCommit !== input.expectedHeadSha || + operation.snapshot?.headSha !== input.expectedHeadSha || + operation.snapshot?.baseSha !== input.expectedBaseSha || + operation.intent.expectedHeadSha !== input.expectedHeadSha || + operation.intent.expectedBaseSha !== input.expectedBaseSha + ) + throw new DomainError('GITHUB_APPROVAL_STALE', 'Approval must bind the current verified head and target SHA'); + operation.mergeApproval = { + actorId: actor.userId, + headSha: input.expectedHeadSha, + baseSha: input.expectedBaseSha, + revision: run.revision, + grantedAt: new Date().toISOString(), + }; + await tx.put('github_operation', touch(operation)); + return operation; }); } async merge(actor: Actor, id: string) { - if (actor.kind !== 'verifier') { requireRole(actor, ['owner', 'admin', 'deployer']); requireHuman(actor); } - const observed = await this.reconcilePullRequest(actor, id); const scope = await this.currentScope(observed); + if (actor.kind !== 'verifier') { + requireRole(actor, ['owner', 'admin', 'deployer']); + requireHuman(actor); + } + const observed = await this.reconcilePullRequest(actor, id); + const scope = await this.currentScope(observed); const claim = await this.store.transaction(actor.tenantId, async tx => { - const operation = await must(tx, 'github_operation', id); await this.access(tx, actor, operation); const run = await must(tx, 'run', operation.runId); - if (operation.mergeAttempted || operation.snapshot?.merged) return { dispatch: false, operation, mode: 'human' as const }; + const operation = await must(tx, 'github_operation', id); + await this.access(tx, actor, operation); + const run = await must(tx, 'run', operation.runId); + if (operation.mergeAttempted || operation.snapshot?.merged) + return { dispatch: false, operation, mode: 'human' as const }; const approval = operation.mergeApproval; - const human = !!approval && approval.revision === run.revision && approval.headSha === operation.intent.expectedHeadSha && approval.baseSha === operation.intent.expectedBaseSha; - if (!human && !scope.allowAutomaticMerge) throw new DomainError('GITHUB_MERGE_NOT_AUTHORIZED', 'Exact-version human approval or operator preauthorization is required'); - if (operation.status !== 'open' || run.revision !== operation.revision || run.verifiedCommit !== operation.intent.expectedHeadSha || ['paused', 'failed', 'canceled'].includes(run.status)) throw new DomainError('GITHUB_APPROVAL_STALE', 'Verified candidate changed before merge'); - operation.mergeAttempted = true; operation.status = 'merging'; await tx.put('github_operation', touch(operation)); return { dispatch: true, operation, mode: human ? 'human' as const : 'preauthorized' as const }; + const human = + !!approval && + approval.revision === run.revision && + approval.headSha === operation.intent.expectedHeadSha && + approval.baseSha === operation.intent.expectedBaseSha; + if (!human && !scope.allowAutomaticMerge) + throw new DomainError( + 'GITHUB_MERGE_NOT_AUTHORIZED', + 'Exact-version human approval or operator preauthorization is required', + ); + if ( + operation.status !== 'open' || + run.revision !== operation.revision || + run.verifiedCommit !== operation.intent.expectedHeadSha || + ['paused', 'failed', 'canceled'].includes(run.status) + ) + throw new DomainError('GITHUB_APPROVAL_STALE', 'Verified candidate changed before merge'); + operation.mergeAttempted = true; + operation.status = 'merging'; + await tx.put('github_operation', touch(operation)); + return { dispatch: true, operation, mode: human ? ('human' as const) : ('preauthorized' as const) }; }); if (!claim.dispatch) return claim.operation; - try { const snapshot = await this.app.mergePullRequest(scope, claim.operation.snapshot!.number, claim.operation.intent, { mode: claim.mode, binding: `${claim.operation.intent.expectedHeadSha}:${claim.operation.intent.expectedBaseSha}` }); return this.recordSnapshot(actor, id, snapshot); } - catch (error) { await this.failure(actor, id, error, true); throw this.error(error); } + try { + const snapshot = await this.app.mergePullRequest( + scope, + claim.operation.snapshot!.number, + claim.operation.intent, + { + mode: claim.mode, + binding: `${claim.operation.intent.expectedHeadSha}:${claim.operation.intent.expectedBaseSha}`, + }, + ); + return this.recordSnapshot(actor, id, snapshot); + } catch (error) { + await this.failure(actor, id, error, true); + throw this.error(error); + } } private async recordSnapshot(actor: Actor, id: string, snapshot: PullRequestSnapshot) { return this.store.transaction(actor.tenantId, async tx => { - const operation = await must(tx, 'github_operation', id); await this.access(tx, actor, operation); const run = await must(tx, 'run', operation.runId); - if (!snapshot.body.includes(operation.intent.operationMarker) || snapshot.headRef !== operation.intent.headBranch || snapshot.baseRef !== operation.intent.baseBranch) throw new DomainError('GITHUB_PULL_SCOPE_INVALID', 'PR marker or branch binding changed'); - operation.snapshot = snapshot; operation.errorCode = undefined; - if (run.revision !== operation.revision) { operation.status = 'stale'; operation.mergeApproval = undefined; } - else if (snapshot.merged && snapshot.mergeCommitSha) { + const operation = await must(tx, 'github_operation', id); + await this.access(tx, actor, operation); + const run = await must(tx, 'run', operation.runId); + if ( + !snapshot.body.includes(operation.intent.operationMarker) || + snapshot.headRef !== operation.intent.headBranch || + snapshot.baseRef !== operation.intent.baseBranch + ) + throw new DomainError('GITHUB_PULL_SCOPE_INVALID', 'PR marker or branch binding changed'); + operation.snapshot = snapshot; + operation.errorCode = undefined; + if (run.revision !== operation.revision) { + operation.status = 'stale'; + operation.mergeApproval = undefined; + } else if (snapshot.merged && snapshot.mergeCommitSha) { operation.status = run.verifiedCommit === snapshot.mergeCommitSha ? 'merged' : 'reverification_required'; - if (operation.status === 'reverification_required' && !['canceled', 'failed', 'succeeded'].includes(run.status)) { - const activeVerifier = actor.kind === 'verifier' && run.status === 'running' && run.leaseOwnerId === actor.userId && run.leaseOwnerKind === 'verifier' && run.lease && Date.parse(run.lease.expiresAt) > Date.now(); - run.sourceCommit = snapshot.mergeCommitSha; run.verifiedCommit = undefined; run.stage = 'verify'; - if (!activeVerifier) { run.status = 'waiting'; run.waitingReason = 'merged_sha_reverification'; run.lastActiveAt = undefined; if (run.lease) run.lease.expiresAt = new Date().toISOString(); } + if ( + operation.status === 'reverification_required' && + !['canceled', 'failed', 'succeeded'].includes(run.status) + ) { + const activeVerifier = + actor.kind === 'verifier' && + run.status === 'running' && + run.leaseOwnerId === actor.userId && + run.leaseOwnerKind === 'verifier' && + run.lease && + Date.parse(run.lease.expiresAt) > Date.now(); + run.sourceCommit = snapshot.mergeCommitSha; + run.verifiedCommit = undefined; + run.stage = 'verify'; + if (!activeVerifier) { + run.status = 'waiting'; + run.waitingReason = 'merged_sha_reverification'; + run.lastActiveAt = undefined; + if (run.lease) run.lease.expiresAt = new Date().toISOString(); + } await tx.put('run', touch(run)); - for (const approval of await tx.list('approval')) { const release = await tx.get('release', String(approval.releaseId)); if (release?.runId === run.id && ['pending', 'approved'].includes(String(approval.status))) await tx.put('approval', { ...touch(approval), status: 'invalidated' }); } + for (const approval of await tx.list('approval')) { + const release = await tx.get('release', String(approval.releaseId)); + if (release?.runId === run.id && ['pending', 'approved'].includes(String(approval.status))) + await tx.put('approval', { ...touch(approval), status: 'invalidated' }); + } } - } else if (snapshot.headSha !== operation.intent.expectedHeadSha || snapshot.baseSha !== operation.intent.expectedBaseSha) { operation.status = 'stale'; operation.mergeApproval = undefined; } - else if (operation.mergeAttempted && snapshot.state === 'open') operation.status = 'merge_pending'; + } else if ( + snapshot.headSha !== operation.intent.expectedHeadSha || + snapshot.baseSha !== operation.intent.expectedBaseSha + ) { + operation.status = 'stale'; + operation.mergeApproval = undefined; + } else if (operation.mergeAttempted && snapshot.state === 'open') operation.status = 'merge_pending'; else operation.status = snapshot.state; - const next = touch(operation); await tx.put('github_operation', next); await tx.event(run.id, 'github.pr.observed', 'GitHub pull request state reconciled; merge acceptance is distinct from source verification.', { operationId: id, number: snapshot.number, state: next.status, headSha: snapshot.headSha, baseSha: snapshot.baseSha, mergeCommitSha: snapshot.mergeCommitSha }); return next; + const next = touch(operation); + await tx.put('github_operation', next); + await tx.event( + run.id, + 'github.pr.observed', + 'GitHub pull request state reconciled; merge acceptance is distinct from source verification.', + { + operationId: id, + number: snapshot.number, + state: next.status, + headSha: snapshot.headSha, + baseSha: snapshot.baseSha, + mergeCommitSha: snapshot.mergeCommitSha, + }, + ); + return next; }); } - private async failure(actor: Actor, id: string, error: unknown, merging: boolean) { await this.store.transaction(actor.tenantId, async tx => { const op = await must(tx, 'github_operation', id); op.status = error instanceof GitHubError && error.unknownOutcome ? merging ? 'merge_unknown' : 'unknown' : 'failed'; op.errorCode = error instanceof GitHubError ? error.code : 'GITHUB_UNAVAILABLE'; await tx.put('github_operation', touch(op)); }); } - private error(error: unknown) { return error instanceof DomainError ? error : error instanceof GitHubError ? new DomainError(error.code, error.message, error.unknownOutcome ? 503 : 409) : new DomainError('GITHUB_UNAVAILABLE', 'GitHub integration is unavailable; no success is recorded', 503); } + private async failure(actor: Actor, id: string, error: unknown, merging: boolean) { + await this.store.transaction(actor.tenantId, async tx => { + const op = await must(tx, 'github_operation', id); + op.status = + error instanceof GitHubError && error.unknownOutcome ? (merging ? 'merge_unknown' : 'unknown') : 'failed'; + op.errorCode = error instanceof GitHubError ? error.code : 'GITHUB_UNAVAILABLE'; + await tx.put('github_operation', touch(op)); + }); + } + private error(error: unknown) { + return error instanceof DomainError + ? error + : error instanceof GitHubError + ? new DomainError(error.code, error.message, error.unknownOutcome ? 503 : 409) + : new DomainError('GITHUB_UNAVAILABLE', 'GitHub integration is unavailable; no success is recorded', 503); + } } diff --git a/apps/api/src/domain/meta-evolution.ts b/apps/api/src/domain/meta-evolution.ts index ab25b17..28b2498 100644 --- a/apps/api/src/domain/meta-evolution.ts +++ b/apps/api/src/domain/meta-evolution.ts @@ -1,97 +1,350 @@ import { DomainError, type Actor, type Entity, type Project, type Run, type Budget } from '@wishforge/contracts'; import { z } from 'zod'; -import { clusterFailures, improvementTask, normalizeFailure, type AutoEvolutionScope, type FailureObservation } from '../../../../packages/evolution/src/meta-loop.js'; +import { + clusterFailures, + improvementTask, + normalizeFailure, + type AutoEvolutionScope, + type FailureObservation, +} from '../../../../packages/evolution/src/meta-loop.js'; import { ControlPlane, must, record, touch } from './control-plane.js'; import { requireRole } from './policy.js'; import { resolveBundle } from './evolution.js'; import { resolveProvisionerTarget } from './provisioner-target.js'; -export interface MetaTask extends Entity {kind:'meta_task';status:'prepared'|'submitted'|'deferred';baselineBundle:string;scope:AutoEvolutionScope;failureSignature:string;projectId:string;provider:'codex'|'codebuddy';budget:Budget;sourceRunIds:string[];experienceIds:string[];requesterId:string;submission:ReturnType;runId?:string;wishId?:string;lastError?:string;} -interface AuthorizedExperience extends Entity {kind:'failure_observation';sourceTenantId:string;sourceRunIds:string[];summary:string;authorization:{purpose:'platform_evolution';approvedBy:string;approvedAt:string;revokedAt?:string};} -export type MetaTickResult={status:'unconfigured'|'idle'|'deferred';reason:string}|{status:'submitted'|'existing';metaTaskId:string;runId:string;wishId:string;sourceRunIds:string[];baselineBundle:string;scope:AutoEvolutionScope}; -const settingsSchema=z.object({projectId:z.string().min(1),provider:z.enum(['codex','codebuddy']),scope:z.enum(['prompt','orchestration','tool','platform']),minimum:z.coerce.number().int().min(1).max(50),maxTokens:z.coerce.number().int().min(1000).max(2000000),maxMinutes:z.coerce.number().int().min(1).max(180),lookbackDays:z.coerce.number().int().min(1).max(30)}); +export interface MetaTask extends Entity { + kind: 'meta_task'; + status: 'prepared' | 'submitted' | 'deferred'; + baselineBundle: string; + scope: AutoEvolutionScope; + failureSignature: string; + projectId: string; + provider: 'codex' | 'codebuddy'; + budget: Budget; + sourceRunIds: string[]; + experienceIds: string[]; + requesterId: string; + submission: ReturnType; + runId?: string; + wishId?: string; + lastError?: string; +} +interface AuthorizedExperience extends Entity { + kind: 'failure_observation'; + sourceTenantId: string; + sourceRunIds: string[]; + summary: string; + authorization: { purpose: 'platform_evolution'; approvedBy: string; approvedAt: string; revokedAt?: string }; +} +export type MetaTickResult = + | { status: 'unconfigured' | 'idle' | 'deferred'; reason: string } + | { + status: 'submitted' | 'existing'; + metaTaskId: string; + runId: string; + wishId: string; + sourceRunIds: string[]; + baselineBundle: string; + scope: AutoEvolutionScope; + }; +const settingsSchema = z.object({ + projectId: z.string().min(1), + provider: z.enum(['codex', 'codebuddy']), + scope: z.enum(['prompt', 'orchestration', 'tool', 'platform']), + minimum: z.coerce.number().int().min(1).max(50), + maxTokens: z.coerce.number().int().min(1000).max(2000000), + maxMinutes: z.coerce.number().int().min(1).max(180), + lookbackDays: z.coerce.number().int().min(1).max(30), +}); /** One explicit tick; no timers, no cross-tenant query, no model calls in this service. */ export class MetaEvolution { - constructor(private control:ControlPlane,private env=control.env,private clock:()=>number=Date.now){} - async tick(actor:Actor):Promise{ - requireRole(actor,['owner','admin']); - const tenantId=this.env.WF_SYSTEM_TENANT; - if(!tenantId)return{status:'unconfigured',reason:'WF_SYSTEM_TENANT is required; customer usage cannot fund platform evolution'}; - if(actor.tenantId!==tenantId||actor.runId||!['human','worker','evaluator'].includes(actor.kind))throw new DomainError('FORBIDDEN','Only an authorized platform operator may trigger the system meta loop',403); - const parsed=settingsSchema.safeParse({projectId:this.env.WF_META_PROJECT_ID,provider:this.env.WF_META_PROVIDER??'codex',scope:this.env.WF_META_SCOPE??'prompt',minimum:this.env.WF_META_MIN_FAILURES??3,maxTokens:this.env.WF_META_MAX_TOKENS??30000,maxMinutes:this.env.WF_META_MAX_MINUTES??15,lookbackDays:this.env.WF_META_LOOKBACK_DAYS??7}); - if(!parsed.success)return{status:'unconfigured',reason:'Configure a system project and valid bounded candidate-generation settings'}; - try { if (!await resolveProvisionerTarget(this.env, tenantId)) return { status: 'unconfigured', reason: 'A system-tenant-scoped isolated provisioner is required before creating improvement work' }; } catch (error) { if (!(error instanceof DomainError)) throw error; return { status: 'unconfigured', reason: error.code }; } - const settings=parsed.data; - const prepared=await this.control.store.transaction(tenantId,async tx=>{ - const project=await tx.get('project',settings.projectId); - if(!project?.repositoryUrl||!project.verificationCommands.length)return{status:'unconfigured' as const,reason:'The system project requires a durable repository and protected independent verification commands'}; - const provider=(await this.control.providerInfo(tx)).find(p=>p.id===settings.provider); - if(!provider?.enabled)return{status:'unconfigured' as const,reason:'Connect a separately funded system-tenant provider credential and configure its executor'}; - const baselineBundle=String((await tx.get('policy','evolution-routing-v1'))?.stableVersion??'baseline-v1'); - const baseline=await resolveBundle(tx,baselineBundle); - const experiences=await tx.list('experience'); - const tasks=experiences.filter(e=>e.kind==='meta_task') as MetaTask[]; + constructor( + private control: ControlPlane, + private env = control.env, + private clock: () => number = Date.now, + ) {} + async tick(actor: Actor): Promise { + requireRole(actor, ['owner', 'admin']); + const tenantId = this.env.WF_SYSTEM_TENANT; + if (!tenantId) + return { + status: 'unconfigured', + reason: 'WF_SYSTEM_TENANT is required; customer usage cannot fund platform evolution', + }; + if (actor.tenantId !== tenantId || actor.runId || !['human', 'worker', 'evaluator'].includes(actor.kind)) + throw new DomainError('FORBIDDEN', 'Only an authorized platform operator may trigger the system meta loop', 403); + const parsed = settingsSchema.safeParse({ + projectId: this.env.WF_META_PROJECT_ID, + provider: this.env.WF_META_PROVIDER ?? 'codex', + scope: this.env.WF_META_SCOPE ?? 'prompt', + minimum: this.env.WF_META_MIN_FAILURES ?? 3, + maxTokens: this.env.WF_META_MAX_TOKENS ?? 30000, + maxMinutes: this.env.WF_META_MAX_MINUTES ?? 15, + lookbackDays: this.env.WF_META_LOOKBACK_DAYS ?? 7, + }); + if (!parsed.success) + return { + status: 'unconfigured', + reason: 'Configure a system project and valid bounded candidate-generation settings', + }; + try { + if (!(await resolveProvisionerTarget(this.env, tenantId))) + return { + status: 'unconfigured', + reason: 'A system-tenant-scoped isolated provisioner is required before creating improvement work', + }; + } catch (error) { + if (!(error instanceof DomainError)) throw error; + return { status: 'unconfigured', reason: error.code }; + } + const settings = parsed.data; + const prepared = await this.control.store.transaction(tenantId, async tx => { + const project = await tx.get('project', settings.projectId); + if (!project?.repositoryUrl || !project.verificationCommands.length) + return { + status: 'unconfigured' as const, + reason: 'The system project requires a durable repository and protected independent verification commands', + }; + const provider = (await this.control.providerInfo(tx)).find(p => p.id === settings.provider); + if (!provider?.enabled) + return { + status: 'unconfigured' as const, + reason: 'Connect a separately funded system-tenant provider credential and configure its executor', + }; + const baselineBundle = String((await tx.get('policy', 'evolution-routing-v1'))?.stableVersion ?? 'baseline-v1'); + const baseline = await resolveBundle(tx, baselineBundle); + const experiences = await tx.list('experience'); + const tasks = experiences.filter(e => e.kind === 'meta_task') as MetaTask[]; // Replaying the same prepared operation repairs a crash after submit without creating a second run. - const pending=tasks.find(t=>!t.runId&&t.baselineBundle===baselineBundle&&t.scope===settings.scope&&['prepared','deferred'].includes(t.status)); - if(pending){ - for(const sourceId of pending.sourceRunIds){const source=await tx.get('run',sourceId);if(!source||source.tenantId!==tenantId||source.status!=='failed'||source.purpose==='evolution'||source.bundleVersion!==baselineBundle)return{status:'deferred' as const,reason:'Prepared source evidence changed; operator review is required'};} - for(const experienceId of pending.experienceIds){const e=await tx.get('experience',experienceId);if(!e||e.sourceTenantId!==tenantId||e.authorization?.purpose!=='platform_evolution'||e.authorization.revokedAt||!['owner','admin'].includes((await tx.membership(e.authorization.approvedBy))??''))return{status:'deferred' as const,reason:'Prepared experience authorization was revoked; operator review is required'};} - return{task:pending}; + const pending = tasks.find( + t => + !t.runId && + t.baselineBundle === baselineBundle && + t.scope === settings.scope && + ['prepared', 'deferred'].includes(t.status), + ); + if (pending) { + for (const sourceId of pending.sourceRunIds) { + const source = await tx.get('run', sourceId); + if ( + !source || + source.tenantId !== tenantId || + source.status !== 'failed' || + source.purpose === 'evolution' || + source.bundleVersion !== baselineBundle + ) + return { + status: 'deferred' as const, + reason: 'Prepared source evidence changed; operator review is required', + }; + } + for (const experienceId of pending.experienceIds) { + const e = await tx.get('experience', experienceId); + if ( + !e || + e.sourceTenantId !== tenantId || + e.authorization?.purpose !== 'platform_evolution' || + e.authorization.revokedAt || + !['owner', 'admin'].includes((await tx.membership(e.authorization.approvedBy)) ?? '') + ) + return { + status: 'deferred' as const, + reason: 'Prepared experience authorization was revoked; operator review is required', + }; + } + return { task: pending }; } - const runs=(await tx.list('run')).filter(r=>r.status==='failed'&&r.purpose!=='evolution'&&r.bundleVersion===baselineBundle&&Date.parse(r.updatedAt)>=this.clock()-settings.lookbackDays*86400000).slice(0,200); - const byId=new Map(runs.map(r=>[r.id,r])); - const authorizedByRun=new Map(); - for(const raw of experiences){ - if(raw.kind!=='failure_observation')continue; - const e=raw as AuthorizedExperience,auth=e.authorization; - if(e.sourceTenantId!==tenantId||auth?.purpose!=='platform_evolution'||auth.revokedAt||!auth.approvedBy||!Array.isArray(e.sourceRunIds)||!e.sourceRunIds.length||!e.sourceRunIds.every(id=>byId.has(id))||typeof e.summary!=='string')continue; - const role=await tx.membership(auth.approvedBy);if(!['owner','admin'].includes(role??''))continue; - const approvedAt=Date.parse(auth.approvedAt);if(!Number.isFinite(approvedAt)||approvedAt>this.clock())continue; - for(const runId of e.sourceRunIds){const list=authorizedByRun.get(runId)??[];list.push({summary:e.summary,id:e.id});authorizedByRun.set(runId,list);} + const runs = (await tx.list('run')) + .filter( + r => + r.status === 'failed' && + r.purpose !== 'evolution' && + r.bundleVersion === baselineBundle && + Date.parse(r.updatedAt) >= this.clock() - settings.lookbackDays * 86400000, + ) + .slice(0, 200); + const byId = new Map(runs.map(r => [r.id, r])); + const authorizedByRun = new Map(); + for (const raw of experiences) { + if (raw.kind !== 'failure_observation') continue; + const e = raw as AuthorizedExperience, + auth = e.authorization; + if ( + e.sourceTenantId !== tenantId || + auth?.purpose !== 'platform_evolution' || + auth.revokedAt || + !auth.approvedBy || + !Array.isArray(e.sourceRunIds) || + !e.sourceRunIds.length || + !e.sourceRunIds.every(id => byId.has(id)) || + typeof e.summary !== 'string' + ) + continue; + const role = await tx.membership(auth.approvedBy); + if (!['owner', 'admin'].includes(role ?? '')) continue; + const approvedAt = Date.parse(auth.approvedAt); + if (!Number.isFinite(approvedAt) || approvedAt > this.clock()) continue; + for (const runId of e.sourceRunIds) { + const list = authorizedByRun.get(runId) ?? []; + list.push({ summary: e.summary, id: e.id }); + authorizedByRun.set(runId, list); + } } - const observations:FailureObservation[]=[];let remainingEventBudget=5000; - for(const run of runs){ - let summary=typeof run.error==='string'?run.error:''; - if(!summary){ - let after=0; - for(let page=0;page<10&&remainingEventBudget>0;page++){ - const events=await tx.events(run.id,after);remainingEventBudget-=events.length; - const failure=[...events].reverse().find(e=>e.type==='provider.failed'||(e.type==='run.transition'&&e.data?.status==='failed'));if(failure)summary=failure.message; - const next=events.at(-1)?.seq??after;if(events.length<1000||next<=after)break;after=next; + const observations: FailureObservation[] = []; + let remainingEventBudget = 5000; + for (const run of runs) { + let summary = typeof run.error === 'string' ? run.error : ''; + if (!summary) { + let after = 0; + for (let page = 0; page < 10 && remainingEventBudget > 0; page++) { + const events = await tx.events(run.id, after); + remainingEventBudget -= events.length; + const failure = [...events] + .reverse() + .find(e => e.type === 'provider.failed' || (e.type === 'run.transition' && e.data?.status === 'failed')); + if (failure) summary = failure.message; + const next = events.at(-1)?.seq ?? after; + if (events.length < 1000 || next <= after) break; + after = next; } } - const authorized=authorizedByRun.get(run.id)??[]; + const authorized = authorizedByRun.get(run.id) ?? []; // Authorization can enrich missing failure evidence, but never copy a foreign tenant's text. - summary=normalizeFailure(summary||authorized[0]?.summary||''); - if(summary)observations.push({runId:run.id,tenantId,baselineBundle,provider:run.provider,stage:run.stage,summary,experienceIds:authorized.map(e=>e.id)}); + summary = normalizeFailure(summary || authorized[0]?.summary || ''); + if (summary) + observations.push({ + runId: run.id, + tenantId, + baselineBundle, + provider: run.provider, + stage: run.stage, + summary, + experienceIds: authorized.map(e => e.id), + }); } - const consumed=new Set(tasks.filter(t=>t.baselineBundle===baselineBundle&&t.scope===settings.scope&&t.runId).flatMap(t=>t.sourceRunIds)); - const cluster=clusterFailures(observations,tenantId,baselineBundle,settings.minimum,consumed)[0]; - if(!cluster)return{status:'idle' as const,reason:'No unconsumed platform-owned failure cluster meets the configured threshold'}; - let submission:ReturnType; - try{submission=improvementTask(cluster,baselineBundle,baseline,settings.scope);}catch{return{status:'unconfigured' as const,reason:'Baseline/evidence context exceeds the trusted task size limit'};} - const existing=await tx.get('experience',submission.metadata.metaTaskId);if(existing)return{task:existing}; - const task:MetaTask={...record(tenantId),id:submission.metadata.metaTaskId,kind:'meta_task',status:'prepared',baselineBundle,scope:settings.scope,failureSignature:cluster.signature,projectId:settings.projectId,provider:settings.provider,budget:{maxTokens:settings.maxTokens,maxMinutes:settings.maxMinutes,maxRepairRounds:1},sourceRunIds:cluster.sourceRunIds,experienceIds:cluster.experienceIds,requesterId:actor.userId,submission}; - await tx.put('experience',task);return{task}; + const consumed = new Set( + tasks + .filter(t => t.baselineBundle === baselineBundle && t.scope === settings.scope && t.runId) + .flatMap(t => t.sourceRunIds), + ); + const cluster = clusterFailures(observations, tenantId, baselineBundle, settings.minimum, consumed)[0]; + if (!cluster) + return { + status: 'idle' as const, + reason: 'No unconsumed platform-owned failure cluster meets the configured threshold', + }; + let submission: ReturnType; + try { + submission = improvementTask(cluster, baselineBundle, baseline, settings.scope); + } catch { + return { + status: 'unconfigured' as const, + reason: 'Baseline/evidence context exceeds the trusted task size limit', + }; + } + const existing = await tx.get('experience', submission.metadata.metaTaskId); + if (existing) return { task: existing }; + const task: MetaTask = { + ...record(tenantId), + id: submission.metadata.metaTaskId, + kind: 'meta_task', + status: 'prepared', + baselineBundle, + scope: settings.scope, + failureSignature: cluster.signature, + projectId: settings.projectId, + provider: settings.provider, + budget: { maxTokens: settings.maxTokens, maxMinutes: settings.maxMinutes, maxRepairRounds: 1 }, + sourceRunIds: cluster.sourceRunIds, + experienceIds: cluster.experienceIds, + requesterId: actor.userId, + submission, + }; + await tx.put('experience', task); + return { task }; }); - if(!('task'in prepared))return prepared; - const task=prepared.task!; - if(task.runId&&task.wishId)return{status:'existing',metaTaskId:task.id,runId:task.runId,wishId:task.wishId,sourceRunIds:task.sourceRunIds,baselineBundle:task.baselineBundle,scope:task.scope}; - const serviceActor:Actor={tenantId,userId:'platform-meta-loop',role:'admin',kind:'evaluator'}; - try{ - const result=await this.control.submit(serviceActor,task.projectId,{goal:task.submission.goal,acceptanceCriteria:task.submission.acceptanceCriteria,provider:task.provider,budget:task.budget},task.submission.idempotencyKey,task.submission.metadata); - return this.control.store.transaction(tenantId,async tx=>{ - const current=await must(tx,'experience',task.id);if(current.runId&¤t.wishId)return{status:'existing',metaTaskId:current.id,runId:current.runId,wishId:current.wishId,sourceRunIds:current.sourceRunIds,baselineBundle:current.baselineBundle,scope:current.scope};current.status='submitted';current.runId=result.run_id;current.wishId=result.wish_id;current.lastError=undefined;await tx.put('experience',touch(current)); - await tx.event(result.run_id,'evolution.task_submitted','Platform-owned failure cluster submitted for candidate generation; evaluation and publication remain separately gated',{metaTaskId:task.id,sourceRunIds:task.sourceRunIds,baselineBundle:task.baselineBundle,scope:task.scope}); - return{status:'submitted',metaTaskId:task.id,runId:result.run_id,wishId:result.wish_id,sourceRunIds:task.sourceRunIds,baselineBundle:task.baselineBundle,scope:task.scope}; + if (!('task' in prepared)) return prepared; + const task = prepared.task!; + if (task.runId && task.wishId) + return { + status: 'existing', + metaTaskId: task.id, + runId: task.runId, + wishId: task.wishId, + sourceRunIds: task.sourceRunIds, + baselineBundle: task.baselineBundle, + scope: task.scope, + }; + const serviceActor: Actor = { tenantId, userId: 'platform-meta-loop', role: 'admin', kind: 'evaluator' }; + try { + const result = await this.control.submit( + serviceActor, + task.projectId, + { + goal: task.submission.goal, + acceptanceCriteria: task.submission.acceptanceCriteria, + provider: task.provider, + budget: task.budget, + }, + task.submission.idempotencyKey, + task.submission.metadata, + ); + return this.control.store.transaction(tenantId, async tx => { + const current = await must(tx, 'experience', task.id); + if (current.runId && current.wishId) + return { + status: 'existing', + metaTaskId: current.id, + runId: current.runId, + wishId: current.wishId, + sourceRunIds: current.sourceRunIds, + baselineBundle: current.baselineBundle, + scope: current.scope, + }; + current.status = 'submitted'; + current.runId = result.run_id; + current.wishId = result.wish_id; + current.lastError = undefined; + await tx.put('experience', touch(current)); + await tx.event( + result.run_id, + 'evolution.task_submitted', + 'Platform-owned failure cluster submitted for candidate generation; evaluation and publication remain separately gated', + { + metaTaskId: task.id, + sourceRunIds: task.sourceRunIds, + baselineBundle: task.baselineBundle, + scope: task.scope, + }, + ); + return { + status: 'submitted', + metaTaskId: task.id, + runId: result.run_id, + wishId: result.wish_id, + sourceRunIds: task.sourceRunIds, + baselineBundle: task.baselineBundle, + scope: task.scope, + }; }); - }catch(error){ - return this.control.store.transaction(tenantId,async tx=>{ - const current=await must(tx,'experience',task.id); - if(current.runId&¤t.wishId)return{status:'existing',metaTaskId:current.id,runId:current.runId,wishId:current.wishId,sourceRunIds:current.sourceRunIds,baselineBundle:current.baselineBundle,scope:current.scope}; - current.status='deferred';current.lastError=error instanceof DomainError?error.code:'SUBMISSION_UNAVAILABLE';await tx.put('experience',touch(current)); - return{status:'deferred',reason:current.lastError}; + } catch (error) { + return this.control.store.transaction(tenantId, async tx => { + const current = await must(tx, 'experience', task.id); + if (current.runId && current.wishId) + return { + status: 'existing', + metaTaskId: current.id, + runId: current.runId, + wishId: current.wishId, + sourceRunIds: current.sourceRunIds, + baselineBundle: current.baselineBundle, + scope: current.scope, + }; + current.status = 'deferred'; + current.lastError = error instanceof DomainError ? error.code : 'SUBMISSION_UNAVAILABLE'; + await tx.put('experience', touch(current)); + return { status: 'deferred', reason: current.lastError }; }); } } diff --git a/apps/api/src/domain/policy.ts b/apps/api/src/domain/policy.ts index a60af94..975e56e 100644 --- a/apps/api/src/domain/policy.ts +++ b/apps/api/src/domain/policy.ts @@ -1,31 +1,71 @@ import { createHash, timingSafeEqual } from 'node:crypto'; -import { DomainError, type Actor, type EvaluationReport, type EvolutionCandidate, type Run } from '@wishforge/contracts'; +import { + DomainError, + type Actor, + type EvaluationReport, + type EvolutionCandidate, + type Run, +} from '@wishforge/contracts'; export function canonical(value: unknown): string { if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`; - if (value && typeof value === 'object') return `{${Object.entries(value).filter(([, v]) => v !== undefined).sort(([a], [b]) => a.localeCompare(b)).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(',')}}`; + if (value && typeof value === 'object') + return `{${Object.entries(value) + .filter(([, v]) => v !== undefined) + .sort(([a], [b]) => a.localeCompare(b)) + .map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`) + .join(',')}}`; return JSON.stringify(value); } export const digest = (value: unknown) => `sha256:${createHash('sha256').update(canonical(value)).digest('hex')}`; -export function sameSecret(a: string, b: string) { const x = Buffer.from(a), y = Buffer.from(b); return x.length === y.length && x.length >= 16 && timingSafeEqual(x, y); } -export function requireRole(actor: Actor, permitted: Actor['role'][]) { if (!permitted.includes(actor.role)) throw new DomainError('FORBIDDEN', 'Your role cannot perform this action', 403); } -export function requireHuman(actor: Actor) { if (actor.kind !== 'human') throw new DomainError('HUMAN_APPROVAL_REQUIRED', 'Confirm this action in an authenticated interactive session', 403); } -export function checkLease(run: Run, token: number, now: number) { if (!run.lease || run.lease.token !== token || Date.parse(run.lease.expiresAt) <= now) throw new DomainError('STALE_LEASE', 'Worker lease expired or was replaced'); } -export function releaseRisk(input: { changedPaths?: string[]; migration?: boolean; permissionsExpanded?: boolean; evidenceComplete: boolean; preauthorized: boolean }) { +export function sameSecret(a: string, b: string) { + const x = Buffer.from(a), + y = Buffer.from(b); + return x.length === y.length && x.length >= 16 && timingSafeEqual(x, y); +} +export function requireRole(actor: Actor, permitted: Actor['role'][]) { + if (!permitted.includes(actor.role)) throw new DomainError('FORBIDDEN', 'Your role cannot perform this action', 403); +} +export function requireHuman(actor: Actor) { + if (actor.kind !== 'human') + throw new DomainError( + 'HUMAN_APPROVAL_REQUIRED', + 'Confirm this action in an authenticated interactive session', + 403, + ); +} +export function checkLease(run: Run, token: number, now: number) { + if (!run.lease || run.lease.token !== token || Date.parse(run.lease.expiresAt) <= now) + throw new DomainError('STALE_LEASE', 'Worker lease expired or was replaced'); +} +export function releaseRisk(input: { + changedPaths?: string[]; + migration?: boolean; + permissionsExpanded?: boolean; + evidenceComplete: boolean; + preauthorized: boolean; +}) { const reasons: string[] = []; if (!input.evidenceComplete) reasons.push('Missing trusted change classification'); if (!input.preauthorized) reasons.push('Environment is not preauthorized for automatic release'); if (input.migration) reasons.push('Database migration'); if (input.permissionsExpanded) reasons.push('Permission expansion'); for (const path of input.changedPaths ?? []) { - if (/(^|\/)(auth|security|billing|migrations?|infra|\.github|\.codex)(\/|$)|(^|\/)(Dockerfile|compose[^/]*|wishforge\.ya?ml|package(-lock)?\.json|pnpm-lock\.yaml|requirements[^/]*|pyproject\.toml)$/.test(path)) reasons.push(`Protected change: ${path}`); + if ( + /(^|\/)(auth|security|billing|migrations?|infra|\.github|\.codex)(\/|$)|(^|\/)(Dockerfile|compose[^/]*|wishforge\.ya?ml|package(-lock)?\.json|pnpm-lock\.yaml|requirements[^/]*|pyproject\.toml)$/.test( + path, + ) + ) + reasons.push(`Protected change: ${path}`); } return [...new Set(reasons)]; } export function evaluationGate(report: EvaluationReport, candidate: EvolutionCandidate): string[] { const failures: string[] = []; - if (report.candidateDigest !== candidate.candidateDigest || report.baselineDigest !== candidate.parentBundle) failures.push('Evaluation does not bind the candidate and baseline'); - if (report.distinctTasks < 50 || report.repeats < 3) failures.push('At least 50 distinct tasks with 3 repeats are required'); + if (report.candidateDigest !== candidate.candidateDigest || report.baselineDigest !== candidate.parentBundle) + failures.push('Evaluation does not bind the candidate and baseline'); + if (report.distinctTasks < 50 || report.repeats < 3) + failures.push('At least 50 distinct tasks with 3 repeats are required'); if (report.hardGateFailures !== 0) failures.push('Hard safety gate failed'); if (report.successDeltaLower95 < 0) failures.push('Quality non-regression is not established'); if (report.costRatio > 1.1 || report.latencyP95Ratio > 1.1) failures.push('Cost or latency regression exceeds 10%'); @@ -33,5 +73,8 @@ export function evaluationGate(report: EvaluationReport, candidate: EvolutionCan return failures; } export function redact(message: string): string { - return message.replace(/(bearer\s+)[\w.\-]+/gi, '$1[redacted]').replace(/\b(sk-[\w-]{8,})\b/g, '[redacted]').replace(/((?:token|password|secret|api[_-]?key)\s*[=:]\s*)[^\s,;]+/gi, '$1[redacted]'); + return message + .replace(/(bearer\s+)[\w.\-]+/gi, '$1[redacted]') + .replace(/\b(sk-[\w-]{8,})\b/g, '[redacted]') + .replace(/((?:token|password|secret|api[_-]?key)\s*[=:]\s*)[^\s,;]+/gi, '$1[redacted]'); } diff --git a/apps/api/src/domain/previews.ts b/apps/api/src/domain/previews.ts index 6548f3c..bbc0f39 100644 --- a/apps/api/src/domain/previews.ts +++ b/apps/api/src/domain/previews.ts @@ -8,64 +8,431 @@ import { deploymentIdentity } from './releases.js'; import { gatewayIdentityOrigins } from './gateway-target.js'; const id = z.string().regex(/^[A-Za-z0-9_-]{1,128}$/); -const target = z.object({ tenantId:id,environmentId:id,projectId:id,publicOrigin:z.string().max(2048),upstreamOrigin:z.string().max(2048),gatewayTokenEnv:z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/),proofPublicKeyFile:z.string().min(1).max(4096) }).strict(); -export interface PreviewTarget {tenantId:string;environmentId:string;projectId:string;publicOrigin:string;upstreamOrigin:string;gatewayToken:string;proofPublicKeyPem:string;policyDigest:string} -function origin(value:string, production:boolean, publicAddress:boolean) { - let parsed:URL;try{parsed=new URL(value);}catch{throw new DomainError('PREVIEW_CONFIGURATION_INVALID','Invalid preview origin',503);} - const loopback=['127.0.0.1','localhost','[::1]'].includes(parsed.hostname); - if(parsed.username||parsed.password||parsed.pathname!=='/'||parsed.search||parsed.hash||!['http:','https:'].includes(parsed.protocol)||(publicAddress&&parsed.protocol!=='https:'&&(production||!loopback)))throw new DomainError('PREVIEW_CONFIGURATION_INVALID','Preview origins must be dedicated origins; public production access requires HTTPS',503); +const target = z + .object({ + tenantId: id, + environmentId: id, + projectId: id, + publicOrigin: z.string().max(2048), + upstreamOrigin: z.string().max(2048), + gatewayTokenEnv: z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/), + proofPublicKeyFile: z.string().min(1).max(4096), + }) + .strict(); +export interface PreviewTarget { + tenantId: string; + environmentId: string; + projectId: string; + publicOrigin: string; + upstreamOrigin: string; + gatewayToken: string; + proofPublicKeyPem: string; + policyDigest: string; +} +function origin(value: string, production: boolean, publicAddress: boolean) { + let parsed: URL; + try { + parsed = new URL(value); + } catch { + throw new DomainError('PREVIEW_CONFIGURATION_INVALID', 'Invalid preview origin', 503); + } + const loopback = ['127.0.0.1', 'localhost', '[::1]'].includes(parsed.hostname); + if ( + parsed.username || + parsed.password || + parsed.pathname !== '/' || + parsed.search || + parsed.hash || + !['http:', 'https:'].includes(parsed.protocol) || + (publicAddress && parsed.protocol !== 'https:' && (production || !loopback)) + ) + throw new DomainError( + 'PREVIEW_CONFIGURATION_INVALID', + 'Preview origins must be dedicated origins; public production access requires HTTPS', + 503, + ); return parsed.origin; } -export async function resolvePreviewTarget(env:NodeJS.ProcessEnv,tenantId:string,environmentId:string):Promise{ - if(!env.WF_PREVIEW_TARGETS_FILE)return undefined; - let entries:z.infer[]; - try{const bytes=await readFile(env.WF_PREVIEW_TARGETS_FILE);if(bytes.length>65536)throw new Error();entries=z.object({schemaVersion:z.literal(1),targets:z.array(target).max(1000)}).strict().parse(JSON.parse(bytes.toString('utf8'))).targets;} - catch{throw new DomainError('PREVIEW_CONFIGURATION_INVALID','Operator preview map is unreadable or invalid',503);} - const identities=new Set(),hosts=new Set(); - const identityOrigins=[env.WF_PUBLIC_URL,env.WF_API_PUBLIC_URL,env.WF_IDE_PUBLIC_URL,env.WF_WORKSPACE_GATEWAY_BASE,...await gatewayIdentityOrigins(env)].filter((value):value is string=>!!value).map(value=>new URL(value).hostname); - for(const entry of entries){entry.publicOrigin=origin(entry.publicOrigin,env.NODE_ENV==='production',true);entry.upstreamOrigin=origin(entry.upstreamOrigin,env.NODE_ENV==='production',false);const key=`${entry.tenantId}\0${entry.environmentId}`,host=new URL(entry.publicOrigin).hostname;if(identities.has(key)||hosts.has(host)||identityOrigins.includes(host)||entry.publicOrigin===entry.upstreamOrigin)throw new DomainError('PREVIEW_ORIGIN_CONFLICT','Each preview requires a dedicated hostname separate from all preview and identity sites',503);identities.add(key);hosts.add(host);} - const entry=entries.find(value=>value.tenantId===tenantId&&value.environmentId===environmentId);if(!entry)return undefined; - const gatewayToken=env[entry.gatewayTokenEnv];if(!gatewayToken||gatewayToken.length<32)throw new DomainError('PREVIEW_CONFIGURATION_INVALID','Scoped preview gateway credential is missing',503); - let proofPublicKeyPem:string;try{const bytes=await readFile(entry.proofPublicKeyFile);if(bytes.length>16384)throw new Error();proofPublicKeyPem=bytes.toString();if(createPublicKey(proofPublicKeyPem).asymmetricKeyType!=='ed25519')throw new Error();}catch{throw new DomainError('PREVIEW_CONFIGURATION_INVALID','Preview gateway Ed25519 public key is invalid',503);} - const policyDigest=`sha256:${createHash('sha256').update(JSON.stringify({tenantId:entry.tenantId,projectId:entry.projectId,environmentId:entry.environmentId,publicOrigin:entry.publicOrigin,upstreamOrigin:entry.upstreamOrigin,proofPublicKeyPem})).digest('hex')}`; - return {...entry,gatewayToken,proofPublicKeyPem,policyDigest}; +export async function resolvePreviewTarget( + env: NodeJS.ProcessEnv, + tenantId: string, + environmentId: string, +): Promise { + if (!env.WF_PREVIEW_TARGETS_FILE) return undefined; + let entries: z.infer[]; + try { + const bytes = await readFile(env.WF_PREVIEW_TARGETS_FILE); + if (bytes.length > 65536) throw new Error(); + entries = z + .object({ schemaVersion: z.literal(1), targets: z.array(target).max(1000) }) + .strict() + .parse(JSON.parse(bytes.toString('utf8'))).targets; + } catch { + throw new DomainError('PREVIEW_CONFIGURATION_INVALID', 'Operator preview map is unreadable or invalid', 503); + } + const identities = new Set(), + hosts = new Set(); + const identityOrigins = [ + env.WF_PUBLIC_URL, + env.WF_API_PUBLIC_URL, + env.WF_IDE_PUBLIC_URL, + env.WF_WORKSPACE_GATEWAY_BASE, + ...(await gatewayIdentityOrigins(env)), + ] + .filter((value): value is string => !!value) + .map(value => new URL(value).hostname); + for (const entry of entries) { + entry.publicOrigin = origin(entry.publicOrigin, env.NODE_ENV === 'production', true); + entry.upstreamOrigin = origin(entry.upstreamOrigin, env.NODE_ENV === 'production', false); + const key = `${entry.tenantId}\0${entry.environmentId}`, + host = new URL(entry.publicOrigin).hostname; + if ( + identities.has(key) || + hosts.has(host) || + identityOrigins.includes(host) || + entry.publicOrigin === entry.upstreamOrigin + ) + throw new DomainError( + 'PREVIEW_ORIGIN_CONFLICT', + 'Each preview requires a dedicated hostname separate from all preview and identity sites', + 503, + ); + identities.add(key); + hosts.add(host); + } + const entry = entries.find(value => value.tenantId === tenantId && value.environmentId === environmentId); + if (!entry) return undefined; + const gatewayToken = env[entry.gatewayTokenEnv]; + if (!gatewayToken || gatewayToken.length < 32) + throw new DomainError('PREVIEW_CONFIGURATION_INVALID', 'Scoped preview gateway credential is missing', 503); + let proofPublicKeyPem: string; + try { + const bytes = await readFile(entry.proofPublicKeyFile); + if (bytes.length > 16384) throw new Error(); + proofPublicKeyPem = bytes.toString(); + if (createPublicKey(proofPublicKeyPem).asymmetricKeyType !== 'ed25519') throw new Error(); + } catch { + throw new DomainError('PREVIEW_CONFIGURATION_INVALID', 'Preview gateway Ed25519 public key is invalid', 503); + } + const policyDigest = `sha256:${createHash('sha256') + .update( + JSON.stringify({ + tenantId: entry.tenantId, + projectId: entry.projectId, + environmentId: entry.environmentId, + publicOrigin: entry.publicOrigin, + upstreamOrigin: entry.upstreamOrigin, + proofPublicKeyPem, + }), + ) + .digest('hex')}`; + return { ...entry, gatewayToken, proofPublicKeyPem, policyDigest }; } -const opaque=()=>randomBytes(32).toString('base64url'); -const hash=(value:string)=>createHash('sha256').update(value).digest('hex'); -const readOnly=(method:string)=>['GET','HEAD'].includes(method); -export const previewAuthorizeInput=z.object({tenantId:id,environmentId:id,kind:z.enum(['launch','session','probe']),credential:z.string().regex(/^[A-Za-z0-9_-]{43}$/),method:z.enum(['GET','HEAD','POST','PUT','PATCH','DELETE','OPTIONS','WS']).default('GET'),path:z.string().startsWith('/').max(4096).default('/'),challenge:z.string().regex(/^[A-Za-z0-9_-]{43}$/).optional()}).strict(); -type AuthorizationInput=z.infer; +const opaque = () => randomBytes(32).toString('base64url'); +const hash = (value: string) => createHash('sha256').update(value).digest('hex'); +const readOnly = (method: string) => ['GET', 'HEAD'].includes(method); +export const previewAuthorizeInput = z + .object({ + tenantId: id, + environmentId: id, + kind: z.enum(['launch', 'session', 'probe']), + credential: z.string().regex(/^[A-Za-z0-9_-]{43}$/), + method: z.enum(['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'WS']).default('GET'), + path: z.string().startsWith('/').max(4096).default('/'), + challenge: z + .string() + .regex(/^[A-Za-z0-9_-]{43}$/) + .optional(), + }) + .strict(); +type AuthorizationInput = z.infer; -export class Previews{ - constructor(private store:Store,private env=process.env){} - private async target(tenantId:string,environmentId:string){const config=await resolvePreviewTarget(this.env,tenantId,environmentId);if(!config)throw new DomainError('PRIVATE_ACCESS_UNCONFIGURED','Operator private preview routing is not configured for this environment',503);return config;} - private async environment(tx:Transaction,config:PreviewTarget){const environment=await must(tx,'environment',config.environmentId);if(environment.projectId!==config.projectId||environment.visibility!=='private'||environment.applicationUrl&&new URL(environment.applicationUrl).origin!==config.publicOrigin)throw new DomainError('PREVIEW_SCOPE_MISMATCH','Preview environment no longer matches its operator routing',403);return environment;} - private async member(tx:Transaction,tenantId:string,userId:string,projectId:string){const role=await tx.membership(userId);if(!role)throw new DomainError('FORBIDDEN','Preview membership was revoked',403);const actor:Actor={tenantId,userId,kind:'human',role};await projectAccess(tx,actor,projectId);return actor;} - private async currentRelease(tx:Transaction,environment:Environment,releaseId:string,policyDigest:string,bindingDigest?:string){const release=await must(tx,'release',releaseId);if(release.environmentId!==environment.id||release.projectId!==environment.projectId||release.visibility!=='private'||release.privateAccessPolicyDigest!==policyDigest||release.applicationUrl!==environment.applicationUrl||(bindingDigest&&release.bindingDigest!==bindingDigest)||!(['deploying','unknown'].includes(release.status)&&environment.activeReleaseId===release.id||release.status==='healthy'&&environment.currentReleaseId===release.id))throw new DomainError('PREVIEW_RELEASE_STALE','Preview release was replaced or is not active',403);return release;} - async launch(actor:Actor,environmentId:string){ - if(!['human','connector'].includes(actor.kind))throw new DomainError('FORBIDDEN','Interactive preview identity required',403); - const subjectExpiresAt=actor.authExpiresAt??(this.env.NODE_ENV!=='production'?Date.now()+900000:undefined);if(!Number.isSafeInteger(subjectExpiresAt)||subjectExpiresAt!<=Date.now())throw new DomainError('UNAUTHENTICATED','A live expiring user credential is required for preview launch',401); - const config=await this.target(actor.tenantId,environmentId);return this.store.transaction(actor.tenantId,async tx=>{const environment=await this.environment(tx,config);await this.member(tx,actor.tenantId,actor.userId,environment.projectId);if(!environment.currentReleaseId)throw new DomainError('PREVIEW_NOT_READY','No accepted private release is available',409);const release=await this.currentRelease(tx,environment,environment.currentReleaseId,config.policyDigest);const code=opaque(),expiresAt=new Date(Math.min(Date.now()+30000,subjectExpiresAt!)).toISOString();await tx.put('preview_authorization',{...record(actor.tenantId),id:`launch-${hash(code)}`,kind:'launch',status:'active',environmentId,projectId:environment.projectId,userId:actor.userId,subjectExpiresAt,releaseId:release.id,bindingDigest:release.bindingDigest,expiresAt,publicOrigin:config.publicOrigin,policyDigest:config.policyDigest});return{launchUrl:`${config.publicOrigin}/_wishforge/session?code=${code}`,expiresAt,visibility:'private'};}); +export class Previews { + constructor( + private store: Store, + private env = process.env, + ) {} + private async target(tenantId: string, environmentId: string) { + const config = await resolvePreviewTarget(this.env, tenantId, environmentId); + if (!config) + throw new DomainError( + 'PRIVATE_ACCESS_UNCONFIGURED', + 'Operator private preview routing is not configured for this environment', + 503, + ); + return config; } - async issueProbe(actor:Actor,releaseId:string){return this.store.transaction(actor.tenantId,tx=>this.issueProbeInTransaction(tx,actor,releaseId));} - async issueProbeForRunner(actor:Actor,releaseId:string,taskId:string){ - return this.store.transaction(actor.tenantId,async tx=>{ - const release=await must(tx,'release',releaseId),environment=await must(tx,'environment',release.environmentId),task=await must(tx,'runner_task',taskId),runner=environment.runnerId?await tx.get('runner',environment.runnerId):undefined; - const envelope=task.envelope as {signature?:string;payload?:{id?:string;tenantId?:string;environmentId?:string;expiresAt?:string;release?:{id?:string;bindingDigest?:string}}}|undefined,payload=envelope?.payload; - if(actor.kind!=='deployer'||actor.userId!==`runner:${environment.id}`||!runner||runner.status!=='active'||actor.clientId!==runner.id||task.runnerId!==runner.id||task.status!=='pending'||task.environmentId!==environment.id||task.releaseId!==releaseId||!envelope?.signature||payload?.id!==taskId||payload.tenantId!==actor.tenantId||payload.environmentId!==environment.id||!Number.isFinite(Date.parse(payload.expiresAt??''))||Date.parse(payload.expiresAt!)<=Date.now()||payload.release?.id!==releaseId||payload.release.bindingDigest!==release.bindingDigest||environment.activeReleaseId!==release.id)throw new DomainError('STALE_RUNNER_TASK','A live signed task from the current environment runner is required for a preview probe',403); - const issued=await this.issueProbeInTransaction(tx,actor,releaseId),authorization=await must(tx,'preview_authorization',`probe-${issued.capabilitySha256}`); - const expiresAt=new Date(Math.min(Date.parse(issued.expiresAt),Date.parse(payload.expiresAt!))).toISOString();authorization.runnerTaskId=taskId;authorization.expiresAt=expiresAt;await tx.put('preview_authorization',touch(authorization));return{...issued,expiresAt}; + private async environment(tx: Transaction, config: PreviewTarget) { + const environment = await must(tx, 'environment', config.environmentId); + if ( + environment.projectId !== config.projectId || + environment.visibility !== 'private' || + (environment.applicationUrl && new URL(environment.applicationUrl).origin !== config.publicOrigin) + ) + throw new DomainError( + 'PREVIEW_SCOPE_MISMATCH', + 'Preview environment no longer matches its operator routing', + 403, + ); + return environment; + } + private async member(tx: Transaction, tenantId: string, userId: string, projectId: string) { + const role = await tx.membership(userId); + if (!role) throw new DomainError('FORBIDDEN', 'Preview membership was revoked', 403); + const actor: Actor = { tenantId, userId, kind: 'human', role }; + await projectAccess(tx, actor, projectId); + return actor; + } + private async currentRelease( + tx: Transaction, + environment: Environment, + releaseId: string, + policyDigest: string, + bindingDigest?: string, + ) { + const release = await must(tx, 'release', releaseId); + if ( + release.environmentId !== environment.id || + release.projectId !== environment.projectId || + release.visibility !== 'private' || + release.privateAccessPolicyDigest !== policyDigest || + release.applicationUrl !== environment.applicationUrl || + (bindingDigest && release.bindingDigest !== bindingDigest) || + !( + (['deploying', 'unknown'].includes(release.status) && environment.activeReleaseId === release.id) || + (release.status === 'healthy' && environment.currentReleaseId === release.id) + ) + ) + throw new DomainError('PREVIEW_RELEASE_STALE', 'Preview release was replaced or is not active', 403); + return release; + } + async launch(actor: Actor, environmentId: string) { + if (!['human', 'connector'].includes(actor.kind)) + throw new DomainError('FORBIDDEN', 'Interactive preview identity required', 403); + const subjectExpiresAt = + actor.authExpiresAt ?? (this.env.NODE_ENV !== 'production' ? Date.now() + 900000 : undefined); + if (!Number.isSafeInteger(subjectExpiresAt) || subjectExpiresAt! <= Date.now()) + throw new DomainError('UNAUTHENTICATED', 'A live expiring user credential is required for preview launch', 401); + const config = await this.target(actor.tenantId, environmentId); + return this.store.transaction(actor.tenantId, async tx => { + const environment = await this.environment(tx, config); + await this.member(tx, actor.tenantId, actor.userId, environment.projectId); + if (!environment.currentReleaseId) + throw new DomainError('PREVIEW_NOT_READY', 'No accepted private release is available', 409); + const release = await this.currentRelease(tx, environment, environment.currentReleaseId, config.policyDigest); + const code = opaque(), + expiresAt = new Date(Math.min(Date.now() + 30000, subjectExpiresAt!)).toISOString(); + await tx.put('preview_authorization', { + ...record(actor.tenantId), + id: `launch-${hash(code)}`, + kind: 'launch', + status: 'active', + environmentId, + projectId: environment.projectId, + userId: actor.userId, + subjectExpiresAt, + releaseId: release.id, + bindingDigest: release.bindingDigest, + expiresAt, + publicOrigin: config.publicOrigin, + policyDigest: config.policyDigest, + }); + return { launchUrl: `${config.publicOrigin}/_wishforge/session?code=${code}`, expiresAt, visibility: 'private' }; }); } - async issueProbeInTransaction(tx:Transaction,actor:Actor,releaseId:string){const release=await must(tx,'release',releaseId);deploymentIdentity(actor,release.environmentId,release.runId);const config=await this.target(actor.tenantId,release.environmentId),environment=await this.environment(tx,config);await this.currentRelease(tx,environment,releaseId,config.policyDigest,release.bindingDigest);const capability=opaque(),challenge=opaque(),capabilitySha256=hash(capability),expiresAt=new Date(Date.now()+120000).toISOString();await tx.put('preview_authorization',{...record(actor.tenantId),id:`probe-${hash(capability)}`,kind:'probe',status:'active',challenge,capabilitySha256,environmentId:environment.id,projectId:environment.projectId,releaseId,bindingDigest:release.bindingDigest,expiresAt,publicOrigin:config.publicOrigin,policyDigest:config.policyDigest});return{tenantId:actor.tenantId,environmentId:environment.id,publicOrigin:config.publicOrigin,releaseId,bindingDigest:release.bindingDigest,capability,challenge,capabilitySha256,expiresAt,proofPublicKeyPem:config.proofPublicKeyPem,policyDigest:config.policyDigest};} - async authorize(serviceAuthorization:string|undefined,untrusted:unknown){ - const input=previewAuthorizeInput.parse(untrusted),config=await this.target(input.tenantId,input.environmentId);const expected=Buffer.from(config.gatewayToken),supplied=Buffer.from(serviceAuthorization?.replace(/^Bearer /,'')??'');if(expected.length!==supplied.length||!timingSafeEqual(expected,supplied))throw new DomainError('UNAUTHENTICATED','Preview gateway identity required',401); - if(input.path.startsWith('//')||input.path.includes('\\')||input.path.startsWith('/_wishforge/'))throw new DomainError('PREVIEW_PATH_INVALID','Reserved preview path',400); - return this.store.transaction(input.tenantId,async tx=>{const authorization=await must(tx,'preview_authorization',`${input.kind}-${hash(input.credential)}`);if(authorization.kind!==input.kind||authorization.environmentId!==input.environmentId||authorization.status!=='active'||!Number.isFinite(Date.parse(String(authorization.expiresAt)))||Date.parse(String(authorization.expiresAt))<=Date.now()||authorization.publicOrigin!==config.publicOrigin||authorization.policyDigest!==config.policyDigest||input.kind!=='probe'&&(!Number.isSafeInteger(authorization.subjectExpiresAt)||Number(authorization.subjectExpiresAt)<=Date.now()))throw new DomainError('PREVIEW_AUTH_EXPIRED','Preview authorization is expired or mismatched',401);const environment=await this.environment(tx,config);const release=await this.currentRelease(tx,environment,String(authorization.releaseId),config.policyDigest,String(authorization.bindingDigest)); - let role='probe';if(input.kind!=='probe'){const actor=await this.member(tx,input.tenantId,String(authorization.userId),environment.projectId);role=actor.role;if(role==='viewer'&&!readOnly(input.method))throw new DomainError('FORBIDDEN','Viewer preview access is read-only',403);}else {if(input.challenge!==authorization.challenge)throw new DomainError('FORBIDDEN','Release probe challenge must match its issued capability',403);if(authorization.runnerTaskId){const task=await must(tx,'runner_task',String(authorization.runnerTaskId)),payload=(task.envelope as {payload?:{expiresAt?:string}}|undefined)?.payload;if(task.status!=='pending'||task.runnerId!==environment.runnerId||task.releaseId!==release.id||!Number.isFinite(Date.parse(payload?.expiresAt??''))||Date.parse(payload?.expiresAt??'')<=Date.now())throw new DomainError('STALE_RUNNER_TASK','Preview probe task is no longer active',403);}if(!readOnly(input.method))throw new DomainError('FORBIDDEN','Release probe permits GET/HEAD only',403);const protectedConfig=release.deliveryConfigSnapshot as {healthPath?:string;smokeChecks?:Array<{path:string}>}|undefined;const application=release.applicationUrl?new URL(release.applicationUrl):undefined;const paths=[protectedConfig?.healthPath,...(protectedConfig?.smokeChecks??[]).map(check=>check.path),application?application.pathname+application.search:undefined].filter((value):value is string=>typeof value==='string');if(!paths.includes(input.path))throw new DomainError('FORBIDDEN','Release probe is limited to protected acceptance paths',403);} - const base={tenantId:input.tenantId,environmentId:environment.id,projectId:environment.projectId,publicOrigin:config.publicOrigin,applicationUrl:release.applicationUrl,role,releaseId:release.id,bindingDigest:release.bindingDigest,policyDigest:config.policyDigest,expiresAt:String(authorization.expiresAt)}; - if(input.kind!=='launch')return base; - authorization.status='consumed';await tx.put('preview_authorization',touch(authorization));const sessionToken=opaque(),expiresAt=new Date(Math.min(Date.now()+900000,Number(authorization.subjectExpiresAt))).toISOString();await tx.put('preview_authorization',{...record(input.tenantId),id:`session-${hash(sessionToken)}`,kind:'session',status:'active',environmentId:environment.id,projectId:environment.projectId,userId:authorization.userId,subjectExpiresAt:authorization.subjectExpiresAt,releaseId:release.id,bindingDigest:release.bindingDigest,publicOrigin:config.publicOrigin,policyDigest:config.policyDigest,expiresAt});return{...base,sessionToken,expiresAt}; + async issueProbe(actor: Actor, releaseId: string) { + return this.store.transaction(actor.tenantId, tx => this.issueProbeInTransaction(tx, actor, releaseId)); + } + async issueProbeForRunner(actor: Actor, releaseId: string, taskId: string) { + return this.store.transaction(actor.tenantId, async tx => { + const release = await must(tx, 'release', releaseId), + environment = await must(tx, 'environment', release.environmentId), + task = await must(tx, 'runner_task', taskId), + runner = environment.runnerId ? await tx.get('runner', environment.runnerId) : undefined; + const envelope = task.envelope as + | { + signature?: string; + payload?: { + id?: string; + tenantId?: string; + environmentId?: string; + expiresAt?: string; + release?: { id?: string; bindingDigest?: string }; + }; + } + | undefined, + payload = envelope?.payload; + if ( + actor.kind !== 'deployer' || + actor.userId !== `runner:${environment.id}` || + !runner || + runner.status !== 'active' || + actor.clientId !== runner.id || + task.runnerId !== runner.id || + task.status !== 'pending' || + task.environmentId !== environment.id || + task.releaseId !== releaseId || + !envelope?.signature || + payload?.id !== taskId || + payload.tenantId !== actor.tenantId || + payload.environmentId !== environment.id || + !Number.isFinite(Date.parse(payload.expiresAt ?? '')) || + Date.parse(payload.expiresAt!) <= Date.now() || + payload.release?.id !== releaseId || + payload.release.bindingDigest !== release.bindingDigest || + environment.activeReleaseId !== release.id + ) + throw new DomainError( + 'STALE_RUNNER_TASK', + 'A live signed task from the current environment runner is required for a preview probe', + 403, + ); + const issued = await this.issueProbeInTransaction(tx, actor, releaseId), + authorization = await must(tx, 'preview_authorization', `probe-${issued.capabilitySha256}`); + const expiresAt = new Date(Math.min(Date.parse(issued.expiresAt), Date.parse(payload.expiresAt!))).toISOString(); + authorization.runnerTaskId = taskId; + authorization.expiresAt = expiresAt; + await tx.put('preview_authorization', touch(authorization)); + return { ...issued, expiresAt }; + }); + } + async issueProbeInTransaction(tx: Transaction, actor: Actor, releaseId: string) { + const release = await must(tx, 'release', releaseId); + deploymentIdentity(actor, release.environmentId, release.runId); + const config = await this.target(actor.tenantId, release.environmentId), + environment = await this.environment(tx, config); + await this.currentRelease(tx, environment, releaseId, config.policyDigest, release.bindingDigest); + const capability = opaque(), + challenge = opaque(), + capabilitySha256 = hash(capability), + expiresAt = new Date(Date.now() + 120000).toISOString(); + await tx.put('preview_authorization', { + ...record(actor.tenantId), + id: `probe-${hash(capability)}`, + kind: 'probe', + status: 'active', + challenge, + capabilitySha256, + environmentId: environment.id, + projectId: environment.projectId, + releaseId, + bindingDigest: release.bindingDigest, + expiresAt, + publicOrigin: config.publicOrigin, + policyDigest: config.policyDigest, + }); + return { + tenantId: actor.tenantId, + environmentId: environment.id, + publicOrigin: config.publicOrigin, + releaseId, + bindingDigest: release.bindingDigest, + capability, + challenge, + capabilitySha256, + expiresAt, + proofPublicKeyPem: config.proofPublicKeyPem, + policyDigest: config.policyDigest, + }; + } + async authorize(serviceAuthorization: string | undefined, untrusted: unknown) { + const input = previewAuthorizeInput.parse(untrusted), + config = await this.target(input.tenantId, input.environmentId); + const expected = Buffer.from(config.gatewayToken), + supplied = Buffer.from(serviceAuthorization?.replace(/^Bearer /, '') ?? ''); + if (expected.length !== supplied.length || !timingSafeEqual(expected, supplied)) + throw new DomainError('UNAUTHENTICATED', 'Preview gateway identity required', 401); + if (input.path.startsWith('//') || input.path.includes('\\') || input.path.startsWith('/_wishforge/')) + throw new DomainError('PREVIEW_PATH_INVALID', 'Reserved preview path', 400); + return this.store.transaction(input.tenantId, async tx => { + const authorization = await must(tx, 'preview_authorization', `${input.kind}-${hash(input.credential)}`); + if ( + authorization.kind !== input.kind || + authorization.environmentId !== input.environmentId || + authorization.status !== 'active' || + !Number.isFinite(Date.parse(String(authorization.expiresAt))) || + Date.parse(String(authorization.expiresAt)) <= Date.now() || + authorization.publicOrigin !== config.publicOrigin || + authorization.policyDigest !== config.policyDigest || + (input.kind !== 'probe' && + (!Number.isSafeInteger(authorization.subjectExpiresAt) || + Number(authorization.subjectExpiresAt) <= Date.now())) + ) + throw new DomainError('PREVIEW_AUTH_EXPIRED', 'Preview authorization is expired or mismatched', 401); + const environment = await this.environment(tx, config); + const release = await this.currentRelease( + tx, + environment, + String(authorization.releaseId), + config.policyDigest, + String(authorization.bindingDigest), + ); + let role = 'probe'; + if (input.kind !== 'probe') { + const actor = await this.member(tx, input.tenantId, String(authorization.userId), environment.projectId); + role = actor.role; + if (role === 'viewer' && !readOnly(input.method)) + throw new DomainError('FORBIDDEN', 'Viewer preview access is read-only', 403); + } else { + if (input.challenge !== authorization.challenge) + throw new DomainError('FORBIDDEN', 'Release probe challenge must match its issued capability', 403); + if (authorization.runnerTaskId) { + const task = await must(tx, 'runner_task', String(authorization.runnerTaskId)), + payload = (task.envelope as { payload?: { expiresAt?: string } } | undefined)?.payload; + if ( + task.status !== 'pending' || + task.runnerId !== environment.runnerId || + task.releaseId !== release.id || + !Number.isFinite(Date.parse(payload?.expiresAt ?? '')) || + Date.parse(payload?.expiresAt ?? '') <= Date.now() + ) + throw new DomainError('STALE_RUNNER_TASK', 'Preview probe task is no longer active', 403); + } + if (!readOnly(input.method)) throw new DomainError('FORBIDDEN', 'Release probe permits GET/HEAD only', 403); + const protectedConfig = release.deliveryConfigSnapshot as + { healthPath?: string; smokeChecks?: Array<{ path: string }> } | undefined; + const application = release.applicationUrl ? new URL(release.applicationUrl) : undefined; + const paths = [ + protectedConfig?.healthPath, + ...(protectedConfig?.smokeChecks ?? []).map(check => check.path), + application ? application.pathname + application.search : undefined, + ].filter((value): value is string => typeof value === 'string'); + if (!paths.includes(input.path)) + throw new DomainError('FORBIDDEN', 'Release probe is limited to protected acceptance paths', 403); + } + const base = { + tenantId: input.tenantId, + environmentId: environment.id, + projectId: environment.projectId, + publicOrigin: config.publicOrigin, + applicationUrl: release.applicationUrl, + role, + releaseId: release.id, + bindingDigest: release.bindingDigest, + policyDigest: config.policyDigest, + expiresAt: String(authorization.expiresAt), + }; + if (input.kind !== 'launch') return base; + authorization.status = 'consumed'; + await tx.put('preview_authorization', touch(authorization)); + const sessionToken = opaque(), + expiresAt = new Date(Math.min(Date.now() + 900000, Number(authorization.subjectExpiresAt))).toISOString(); + await tx.put('preview_authorization', { + ...record(input.tenantId), + id: `session-${hash(sessionToken)}`, + kind: 'session', + status: 'active', + environmentId: environment.id, + projectId: environment.projectId, + userId: authorization.userId, + subjectExpiresAt: authorization.subjectExpiresAt, + releaseId: release.id, + bindingDigest: release.bindingDigest, + publicOrigin: config.publicOrigin, + policyDigest: config.policyDigest, + expiresAt, + }); + return { ...base, sessionToken, expiresAt }; }); } } diff --git a/apps/api/src/domain/private-access-verifier.ts b/apps/api/src/domain/private-access-verifier.ts index f385a51..9687eff 100644 --- a/apps/api/src/domain/private-access-verifier.ts +++ b/apps/api/src/domain/private-access-verifier.ts @@ -1,17 +1,119 @@ -import {DomainError,type Environment,type Release,type PrivateAccessEvidence,type Entity} from '@wishforge/contracts'; -import type {Transaction} from '../db/store.js'; -import {must,touch} from './control-plane.js'; -import {resolvePreviewTarget} from './previews.js'; -import {verifyPrivateAccessSamples,type ReleaseManifest} from '../../../../packages/runtime/src/index.js'; -export async function privatePolicyForEnvironment(environment:Environment,env:NodeJS.ProcessEnv){if(environment.visibility!=='private'||!environment.applicationUrl)return undefined;const config=await resolvePreviewTarget(env,environment.tenantId,environment.id);if(!config||config.projectId!==environment.projectId||config.publicOrigin!==new URL(environment.applicationUrl).origin)throw new DomainError('PRIVATE_ACCESS_UNCONFIGURED','An operator-managed private identity gateway must match this tenant, environment and application origin',503);return config.policyDigest;} -export async function verifyPrivateEvidence(tx:Transaction,env:NodeJS.ProcessEnv,release:Release,untrusted:unknown,receiptId:string){ - const evidence=untrusted as PrivateAccessEvidence;if(!evidence||!/^[a-f0-9]{64}$/.test(evidence.capabilitySha256??'')||!/^[A-Za-z0-9_-]{43}$/.test(evidence.challenge??''))throw new DomainError('PRIVATE_ACCESS_PROOF_REQUIRED','Independent signed private access evidence is required'); - const environment=await must(tx,'environment',release.environmentId);const config=await resolvePreviewTarget(env,release.tenantId,release.environmentId);if(!config||config.policyDigest!==release.privateAccessPolicyDigest||config.projectId!==release.projectId||environment.visibility!=='private'||environment.applicationUrl!==release.applicationUrl||new URL(release.applicationUrl!).origin!==config.publicOrigin||!(environment.activeReleaseId===release.id||environment.currentReleaseId===release.id))throw new DomainError('PRIVATE_ACCESS_POLICY_STALE','Private gateway policy or active release changed'); - const authorization=await tx.get('preview_authorization',`probe-${evidence.capabilitySha256}`); - if(!authorization||authorization.kind!=='probe'||authorization.runnerTaskId!==receiptId||!['active','consumed'].includes(String(authorization.status))||authorization.status==='consumed'&&authorization.consumedBy!==receiptId||authorization.capabilitySha256!==evidence.capabilitySha256||authorization.challenge!==evidence.challenge||authorization.environmentId!==release.environmentId||authorization.projectId!==release.projectId||authorization.releaseId!==release.id||authorization.bindingDigest!==release.bindingDigest||authorization.policyDigest!==config.policyDigest||authorization.publicOrigin!==config.publicOrigin||Date.parse(String(authorization.expiresAt))<=Date.now())throw new DomainError('PRIVATE_ACCESS_PROBE_STALE','Private access nonce, capability digest or authorization expiry does not match this receipt'); - const contract=release.deliveryConfigSnapshot as Pick|undefined;if(!contract?.healthPath||!contract.smokeChecks?.length)throw new DomainError('PRIVATE_ACCESS_UNCONFIGURED','Protected private health and business checks are required'); - const manifest={schemaVersion:1,id:release.id,tenantId:release.tenantId,environmentId:release.environmentId,sourceCommit:release.sourceCommit,image:release.imageDigest,applicationUrl:release.applicationUrl,visibility:'private',bindingDigest:release.bindingDigest,privateAccessPolicyDigest:release.privateAccessPolicyDigest,...contract} as ReleaseManifest; - try{verifyPrivateAccessSamples(manifest,evidence,config.proofPublicKeyPem,String(authorization.challenge),String(authorization.capabilitySha256));}catch{throw new DomainError('PRIVATE_ACCESS_PROOF_INVALID','Gateway signature, request body/status or required private acceptance checks failed');} - authorization.status='consumed';authorization.consumedBy=receiptId;authorization.consumedAt=new Date().toISOString();await tx.put('preview_authorization',touch(authorization));return{visibility:'private',policyDigest:config.policyDigest,checkedAt:evidence.checkedAt,checks:['health','business-smoke','application-url','anonymous-denied','private-access']}; +import { + DomainError, + type Environment, + type Release, + type PrivateAccessEvidence, + type Entity, +} from '@wishforge/contracts'; +import type { Transaction } from '../db/store.js'; +import { must, touch } from './control-plane.js'; +import { resolvePreviewTarget } from './previews.js'; +import { verifyPrivateAccessSamples, type ReleaseManifest } from '../../../../packages/runtime/src/index.js'; +export async function privatePolicyForEnvironment(environment: Environment, env: NodeJS.ProcessEnv) { + if (environment.visibility !== 'private' || !environment.applicationUrl) return undefined; + const config = await resolvePreviewTarget(env, environment.tenantId, environment.id); + if ( + !config || + config.projectId !== environment.projectId || + config.publicOrigin !== new URL(environment.applicationUrl).origin + ) + throw new DomainError( + 'PRIVATE_ACCESS_UNCONFIGURED', + 'An operator-managed private identity gateway must match this tenant, environment and application origin', + 503, + ); + return config.policyDigest; +} +export async function verifyPrivateEvidence( + tx: Transaction, + env: NodeJS.ProcessEnv, + release: Release, + untrusted: unknown, + receiptId: string, +) { + const evidence = untrusted as PrivateAccessEvidence; + if ( + !evidence || + !/^[a-f0-9]{64}$/.test(evidence.capabilitySha256 ?? '') || + !/^[A-Za-z0-9_-]{43}$/.test(evidence.challenge ?? '') + ) + throw new DomainError('PRIVATE_ACCESS_PROOF_REQUIRED', 'Independent signed private access evidence is required'); + const environment = await must(tx, 'environment', release.environmentId); + const config = await resolvePreviewTarget(env, release.tenantId, release.environmentId); + if ( + !config || + config.policyDigest !== release.privateAccessPolicyDigest || + config.projectId !== release.projectId || + environment.visibility !== 'private' || + environment.applicationUrl !== release.applicationUrl || + new URL(release.applicationUrl!).origin !== config.publicOrigin || + !(environment.activeReleaseId === release.id || environment.currentReleaseId === release.id) + ) + throw new DomainError('PRIVATE_ACCESS_POLICY_STALE', 'Private gateway policy or active release changed'); + const authorization = await tx.get('preview_authorization', `probe-${evidence.capabilitySha256}`); + if ( + !authorization || + authorization.kind !== 'probe' || + authorization.runnerTaskId !== receiptId || + !['active', 'consumed'].includes(String(authorization.status)) || + (authorization.status === 'consumed' && authorization.consumedBy !== receiptId) || + authorization.capabilitySha256 !== evidence.capabilitySha256 || + authorization.challenge !== evidence.challenge || + authorization.environmentId !== release.environmentId || + authorization.projectId !== release.projectId || + authorization.releaseId !== release.id || + authorization.bindingDigest !== release.bindingDigest || + authorization.policyDigest !== config.policyDigest || + authorization.publicOrigin !== config.publicOrigin || + Date.parse(String(authorization.expiresAt)) <= Date.now() + ) + throw new DomainError( + 'PRIVATE_ACCESS_PROBE_STALE', + 'Private access nonce, capability digest or authorization expiry does not match this receipt', + ); + const contract = release.deliveryConfigSnapshot as + Pick | undefined; + if (!contract?.healthPath || !contract.smokeChecks?.length) + throw new DomainError('PRIVATE_ACCESS_UNCONFIGURED', 'Protected private health and business checks are required'); + const manifest = { + schemaVersion: 1, + id: release.id, + tenantId: release.tenantId, + environmentId: release.environmentId, + sourceCommit: release.sourceCommit, + image: release.imageDigest, + applicationUrl: release.applicationUrl, + visibility: 'private', + bindingDigest: release.bindingDigest, + privateAccessPolicyDigest: release.privateAccessPolicyDigest, + ...contract, + } as ReleaseManifest; + try { + verifyPrivateAccessSamples( + manifest, + evidence, + config.proofPublicKeyPem, + String(authorization.challenge), + String(authorization.capabilitySha256), + ); + } catch { + throw new DomainError( + 'PRIVATE_ACCESS_PROOF_INVALID', + 'Gateway signature, request body/status or required private acceptance checks failed', + ); + } + authorization.status = 'consumed'; + authorization.consumedBy = receiptId; + authorization.consumedAt = new Date().toISOString(); + await tx.put('preview_authorization', touch(authorization)); + return { + visibility: 'private', + policyDigest: config.policyDigest, + checkedAt: evidence.checkedAt, + checks: ['health', 'business-smoke', 'application-url', 'anonymous-denied', 'private-access'], + }; +} +export function publicEvidence(evidence: Entity) { + const { privateAccessEvidence: _, ...visible } = evidence; + return visible; } -export function publicEvidence(evidence:Entity){const {privateAccessEvidence:_,...visible}=evidence;return visible;} diff --git a/apps/api/src/domain/provider-proxy.ts b/apps/api/src/domain/provider-proxy.ts index be741df..a24714a 100644 --- a/apps/api/src/domain/provider-proxy.ts +++ b/apps/api/src/domain/provider-proxy.ts @@ -5,111 +5,405 @@ import type { Store, Transaction } from '../db/store.js'; import { CredentialVault } from './connections.js'; import { executionLease, must, record, touch, now } from './control-plane.js'; -const ISSUER = 'wishforge-provider-proxy', AUDIENCE = 'wishforge-openai-proxy'; +const ISSUER = 'wishforge-provider-proxy', + AUDIENCE = 'wishforge-openai-proxy'; const UPSTREAM = 'https://api.openai.com/v1'; -const MAX_BODY = 2 * 1024 * 1024, MAX_FRAME = 1024 * 1024, MAX_RESPONSE = 16 * 1024 * 1024, MAX_REPLAY = 2 * 1024 * 1024; +const MAX_BODY = 2 * 1024 * 1024, + MAX_FRAME = 1024 * 1024, + MAX_RESPONSE = 16 * 1024 * 1024, + MAX_REPLAY = 2 * 1024 * 1024; const hash = (value: string | Uint8Array) => createHash('sha256').update(value).digest('hex'); -interface ProxyClaims { tenantId: string; runId: string; generation: string; leaseToken: number; ownerId: string; connectionId: string; connectionVersion: number } -export interface ProxyRequestRecord extends Entity { runId: string; projectId: string; connectionId: string; connectionVersion: number; generation: string; leaseToken: number; bodyDigest: string; path: string; status: 'dispatched' | 'completed' | 'rejected' | 'unknown'; reservedTokens: number; usageTokens?: number; responseId?: string; responseStatus?: number; responseType?: string; responseBody?: string; unknownReason?: string } -export interface ProviderProxyRequest { authorization?: string; method: string; path: string; headers?: Headers | Record; body?: Uint8Array; signal?: AbortSignal } -interface Options { vault?: Pick; fetcher?: typeof fetch; pollMs?: number } +interface ProxyClaims { + tenantId: string; + runId: string; + generation: string; + leaseToken: number; + ownerId: string; + connectionId: string; + connectionVersion: number; +} +export interface ProxyRequestRecord extends Entity { + runId: string; + projectId: string; + connectionId: string; + connectionVersion: number; + generation: string; + leaseToken: number; + bodyDigest: string; + path: string; + status: 'dispatched' | 'completed' | 'rejected' | 'unknown'; + reservedTokens: number; + usageTokens?: number; + responseId?: string; + responseStatus?: number; + responseType?: string; + responseBody?: string; + unknownReason?: string; +} +export interface ProviderProxyRequest { + authorization?: string; + method: string; + path: string; + headers?: Headers | Record; + body?: Uint8Array; + signal?: AbortSignal; +} +interface Options { + vault?: Pick; + fetcher?: typeof fetch; + pollMs?: number; +} export function providerProxyConfigured(env = process.env) { if (env.NODE_ENV !== 'production' && env.WF_PROVIDER_PROXY_ENABLED !== 'true') return false; - if (!env.WF_PROVIDER_PROXY_PUBLIC_URL || !env.WF_PROVIDER_PROXY_SIGNING_KEY || env.WF_PROVIDER_PROXY_SIGNING_KEY.length < 32 || !env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.trim()) return false; - try { const url = new URL(env.WF_PROVIDER_PROXY_PUBLIC_URL); return !url.username && !url.password && !url.search && !url.hash && url.pathname.replace(/\/$/, '').endsWith('/provider-proxy/v1') && (url.protocol === 'https:' || (env.NODE_ENV !== 'production' && url.protocol === 'http:' && ['127.0.0.1', 'localhost'].includes(url.hostname))); } catch { return false; } + if ( + !env.WF_PROVIDER_PROXY_PUBLIC_URL || + !env.WF_PROVIDER_PROXY_SIGNING_KEY || + env.WF_PROVIDER_PROXY_SIGNING_KEY.length < 32 || + !env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.trim() + ) + return false; + try { + const url = new URL(env.WF_PROVIDER_PROXY_PUBLIC_URL); + return ( + !url.username && + !url.password && + !url.search && + !url.hash && + url.pathname.replace(/\/$/, '').endsWith('/provider-proxy/v1') && + (url.protocol === 'https:' || + (env.NODE_ENV !== 'production' && + url.protocol === 'http:' && + ['127.0.0.1', 'localhost'].includes(url.hostname))) + ); + } catch { + return false; + } } export class ProviderProxy { private readonly vault: Pick; private readonly fetcher: typeof fetch; private readonly pollMs: number; - constructor(private store: Store, private env = process.env, options: Options = {}) { this.vault = options.vault ?? new CredentialVault(env); this.fetcher = options.fetcher ?? fetch; this.pollMs = Math.max(25, Math.min(options.pollMs ?? 500, 1000)); } - private requireConfigured() { if (!providerProxyConfigured(this.env)) throw new DomainError('PROVIDER_PROXY_UNCONFIGURED', 'Configure the isolated model proxy, signing key, public HTTPS base URL and allowed model list', 503); } - private key() { this.requireConfigured(); return new TextEncoder().encode(this.env.WF_PROVIDER_PROXY_SIGNING_KEY!); } - private models() { return new Set(this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS!.split(',').map(v => v.trim()).filter(Boolean)); } - private actor(claim: ProxyClaims): Actor { return { tenantId: claim.tenantId, userId: claim.ownerId, kind: 'worker', role: 'admin', runId: claim.runId, ...(claim.generation === 'development-local' ? {} : { credentialId: claim.generation }) }; } + constructor( + private store: Store, + private env = process.env, + options: Options = {}, + ) { + this.vault = options.vault ?? new CredentialVault(env); + this.fetcher = options.fetcher ?? fetch; + this.pollMs = Math.max(25, Math.min(options.pollMs ?? 500, 1000)); + } + private requireConfigured() { + if (!providerProxyConfigured(this.env)) + throw new DomainError( + 'PROVIDER_PROXY_UNCONFIGURED', + 'Configure the isolated model proxy, signing key, public HTTPS base URL and allowed model list', + 503, + ); + } + private key() { + this.requireConfigured(); + return new TextEncoder().encode(this.env.WF_PROVIDER_PROXY_SIGNING_KEY!); + } + private models() { + return new Set( + this.env + .WF_PROVIDER_PROXY_ALLOWED_MODELS!.split(',') + .map(v => v.trim()) + .filter(Boolean), + ); + } + private actor(claim: ProxyClaims): Actor { + return { + tenantId: claim.tenantId, + userId: claim.ownerId, + kind: 'worker', + role: 'admin', + runId: claim.runId, + ...(claim.generation === 'development-local' ? {} : { credentialId: claim.generation }), + }; + } private async valid(tx: Transaction, claim: ProxyClaims) { const run = await must(tx, 'run', claim.runId); executionLease(this.actor(claim), run, claim.leaseToken); - if (run.provider !== 'codex' || run.status !== 'running' || (run.executionGeneration ?? 'development-local') !== claim.generation) throw new DomainError('PROVIDER_PROXY_REVOKED', 'Proxy credential no longer belongs to an active Codex execution', 403); - const elapsed = run.activeMilliseconds + (run.lastActiveAt ? Math.max(0, Date.now() - Date.parse(run.lastActiveAt)) : 0); - if (elapsed >= run.budget.maxMinutes * 60_000) throw new DomainError('PROVIDER_TIME_EXHAUSTED', 'Run execution-time limit is exhausted', 429); + if ( + run.provider !== 'codex' || + run.status !== 'running' || + (run.executionGeneration ?? 'development-local') !== claim.generation + ) + throw new DomainError( + 'PROVIDER_PROXY_REVOKED', + 'Proxy credential no longer belongs to an active Codex execution', + 403, + ); + const elapsed = + run.activeMilliseconds + (run.lastActiveAt ? Math.max(0, Date.now() - Date.parse(run.lastActiveAt)) : 0); + if (elapsed >= run.budget.maxMinutes * 60_000) + throw new DomainError('PROVIDER_TIME_EXHAUSTED', 'Run execution-time limit is exhausted', 429); const connection = await must(tx, 'provider_connection', claim.connectionId); - if (connection.provider !== 'codex' || connection.status !== 'active' || connection.version !== claim.connectionVersion || typeof connection.ciphertext !== 'string') throw new DomainError('PROVIDER_CONNECTION_REVOKED', 'Provider connection was revoked or replaced', 403); + if ( + connection.provider !== 'codex' || + connection.status !== 'active' || + connection.version !== claim.connectionVersion || + typeof connection.ciphertext !== 'string' + ) + throw new DomainError('PROVIDER_CONNECTION_REVOKED', 'Provider connection was revoked or replaced', 403); return { run, connection }; } async credential(actor: Actor, run: Run) { this.requireConfigured(); - if (actor.kind !== 'worker' || run.provider !== 'codex' || !run.lease) throw new DomainError('PROVIDER_PROXY_FORBIDDEN', 'Codex executor lease required', 403); + if (actor.kind !== 'worker' || run.provider !== 'codex' || !run.lease) + throw new DomainError('PROVIDER_PROXY_FORBIDDEN', 'Codex executor lease required', 403); const claims = await this.store.transaction(actor.tenantId, async tx => { - const current = await must(tx, 'run', run.id); executionLease(actor, current, run.lease!.token); + const current = await must(tx, 'run', run.id); + executionLease(actor, current, run.lease!.token); if (current.status !== 'running') throw new DomainError('RUN_STOPPED', 'Run is not executing'); - if (this.env.NODE_ENV === 'production' && (!actor.credentialId || actor.credentialId !== current.executionGeneration)) throw new DomainError('PROVIDER_GENERATION_REQUIRED', 'Production requires a run-scoped execution generation', 403); - const connection = (await tx.list('provider_connection')).find(c => c.provider === 'codex' && c.status === 'active' && typeof c.ciphertext === 'string'); - if (!connection) throw new DomainError('PROVIDER_CONNECTION_MISSING', 'Connect an active tenant-owned Codex credential', 503); - return { tenantId: actor.tenantId, runId: current.id, generation: String(current.executionGeneration ?? 'development-local'), leaseToken: current.lease!.token, ownerId: actor.userId, connectionId: connection.id, connectionVersion: connection.version, remainingSeconds: Math.max(0, Math.floor((current.budget.maxMinutes * 60000 - current.activeMilliseconds) / 1000)) }; + if ( + this.env.NODE_ENV === 'production' && + (!actor.credentialId || actor.credentialId !== current.executionGeneration) + ) + throw new DomainError( + 'PROVIDER_GENERATION_REQUIRED', + 'Production requires a run-scoped execution generation', + 403, + ); + const connection = (await tx.list('provider_connection')).find( + c => c.provider === 'codex' && c.status === 'active' && typeof c.ciphertext === 'string', + ); + if (!connection) + throw new DomainError('PROVIDER_CONNECTION_MISSING', 'Connect an active tenant-owned Codex credential', 503); + return { + tenantId: actor.tenantId, + runId: current.id, + generation: String(current.executionGeneration ?? 'development-local'), + leaseToken: current.lease!.token, + ownerId: actor.userId, + connectionId: connection.id, + connectionVersion: connection.version, + remainingSeconds: Math.max( + 0, + Math.floor((current.budget.maxMinutes * 60000 - current.activeMilliseconds) / 1000), + ), + }; }); - const { remainingSeconds, ...binding } = claims; if (!remainingSeconds) throw new DomainError('PROVIDER_TIME_EXHAUSTED', 'Run execution-time limit is exhausted', 429); + const { remainingSeconds, ...binding } = claims; + if (!remainingSeconds) + throw new DomainError('PROVIDER_TIME_EXHAUSTED', 'Run execution-time limit is exhausted', 429); const ttl = Math.min(3600, remainingSeconds); - const apiKey = await new SignJWT(binding).setProtectedHeader({ alg: 'HS256' }).setIssuer(ISSUER).setAudience(AUDIENCE).setSubject(actor.userId).setIssuedAt().setExpirationTime(`${ttl}s`).sign(this.key()); - return { apiKey, baseUrl: this.env.WF_PROVIDER_PROXY_PUBLIC_URL!.replace(/\/$/, ''), expiresAt: new Date(Date.now() + ttl * 1000).toISOString(), metering: 'proxy' as const }; + const apiKey = await new SignJWT(binding) + .setProtectedHeader({ alg: 'HS256' }) + .setIssuer(ISSUER) + .setAudience(AUDIENCE) + .setSubject(actor.userId) + .setIssuedAt() + .setExpirationTime(`${ttl}s`) + .sign(this.key()); + return { + apiKey, + baseUrl: this.env.WF_PROVIDER_PROXY_PUBLIC_URL!.replace(/\/$/, ''), + expiresAt: new Date(Date.now() + ttl * 1000).toISOString(), + metering: 'proxy' as const, + }; } private async claims(authorization?: string): Promise { - if (!authorization?.startsWith('Bearer ')) throw new DomainError('PROVIDER_PROXY_UNAUTHENTICATED', 'Run-scoped model credential required', 401); + if (!authorization?.startsWith('Bearer ')) + throw new DomainError('PROVIDER_PROXY_UNAUTHENTICATED', 'Run-scoped model credential required', 401); try { - const { payload } = await jwtVerify(authorization.slice(7), this.key(), { algorithms: ['HS256'], issuer: ISSUER, audience: AUDIENCE }); - if (![payload.tenantId, payload.runId, payload.generation, payload.ownerId, payload.connectionId].every(v => typeof v === 'string' && v.length > 0) || !Number.isSafeInteger(payload.leaseToken) || Number(payload.leaseToken) < 1 || !Number.isSafeInteger(payload.connectionVersion) || payload.sub !== payload.ownerId) throw new Error('Invalid claims'); + const { payload } = await jwtVerify(authorization.slice(7), this.key(), { + algorithms: ['HS256'], + issuer: ISSUER, + audience: AUDIENCE, + }); + if ( + ![payload.tenantId, payload.runId, payload.generation, payload.ownerId, payload.connectionId].every( + v => typeof v === 'string' && v.length > 0, + ) || + !Number.isSafeInteger(payload.leaseToken) || + Number(payload.leaseToken) < 1 || + !Number.isSafeInteger(payload.connectionVersion) || + payload.sub !== payload.ownerId + ) + throw new Error('Invalid claims'); return payload as unknown as ProxyClaims; - } catch (error) { if (error instanceof DomainError) throw error; throw new DomainError('PROVIDER_PROXY_UNAUTHENTICATED', 'Proxy credential is invalid or expired', 401); } + } catch (error) { + if (error instanceof DomainError) throw error; + throw new DomainError('PROVIDER_PROXY_UNAUTHENTICATED', 'Proxy credential is invalid or expired', 401); + } } private body(request: ProviderProxyRequest): { data?: Record; raw: string } { const raw = new TextDecoder('utf-8', { fatal: true }).decode(request.body ?? new Uint8Array()); - if (Buffer.byteLength(raw) > MAX_BODY) throw new DomainError('PROVIDER_REQUEST_TOO_LARGE', 'Proxy request exceeds 2 MiB', 413); - if (request.method === 'GET') { if (raw) throw new DomainError('PROVIDER_INVALID_REQUEST', 'Model listing cannot contain a body', 400); return { raw }; } - let data: Record; try { data = JSON.parse(raw); } catch { throw new DomainError('PROVIDER_INVALID_REQUEST', 'Responses requires a JSON object', 400); } - if (!data || Array.isArray(data) || typeof data !== 'object' || typeof data.model !== 'string' || !this.models().has(data.model)) throw new DomainError('PROVIDER_MODEL_DENIED', 'Choose an operator-approved model', 403); - if (data.background === true || data.previous_response_id || data.conversation || data.prompt || data.multi_agent) throw new DomainError('PROVIDER_STATEFUL_REQUEST_DENIED', 'Unbounded remote state and server-hosted execution are not supported by this proxy', 400); + if (Buffer.byteLength(raw) > MAX_BODY) + throw new DomainError('PROVIDER_REQUEST_TOO_LARGE', 'Proxy request exceeds 2 MiB', 413); + if (request.method === 'GET') { + if (raw) throw new DomainError('PROVIDER_INVALID_REQUEST', 'Model listing cannot contain a body', 400); + return { raw }; + } + let data: Record; + try { + data = JSON.parse(raw); + } catch { + throw new DomainError('PROVIDER_INVALID_REQUEST', 'Responses requires a JSON object', 400); + } + if ( + !data || + Array.isArray(data) || + typeof data !== 'object' || + typeof data.model !== 'string' || + !this.models().has(data.model) + ) + throw new DomainError('PROVIDER_MODEL_DENIED', 'Choose an operator-approved model', 403); + if (data.background === true || data.previous_response_id || data.conversation || data.prompt || data.multi_agent) + throw new DomainError( + 'PROVIDER_STATEFUL_REQUEST_DENIED', + 'Unbounded remote state and server-hosted execution are not supported by this proxy', + 400, + ); // Pinned Codex CLI sends transport-only client metadata; never forward it as an API parameter. if (data.client_metadata !== undefined) { - if (!data.client_metadata || typeof data.client_metadata !== 'object' || Array.isArray(data.client_metadata) || Object.keys(data.client_metadata).length > 20 || Object.entries(data.client_metadata).some(([key, value]) => key.length > 100 || typeof value !== 'string' || value.length > 4096)) throw new DomainError('PROVIDER_INVALID_REQUEST', 'Client metadata must be a bounded string map', 400); + if ( + !data.client_metadata || + typeof data.client_metadata !== 'object' || + Array.isArray(data.client_metadata) || + Object.keys(data.client_metadata).length > 20 || + Object.entries(data.client_metadata).some( + ([key, value]) => key.length > 100 || typeof value !== 'string' || value.length > 4096, + ) + ) + throw new DomainError('PROVIDER_INVALID_REQUEST', 'Client metadata must be a bounded string map', 400); delete data.client_metadata; } - const allowed = new Set(['model', 'input', 'instructions', 'tools', 'tool_choice', 'parallel_tool_calls', 'stream', 'stream_options', 'reasoning', 'store', 'background', 'include', 'service_tier', 'text', 'prompt_cache_key', 'prompt_cache_retention', 'prompt_cache_options', 'metadata', 'truncation', 'max_output_tokens', 'temperature', 'top_p', 'top_logprobs', 'user', 'safety_identifier']); - if (Object.keys(data).some(key => !allowed.has(key))) throw new DomainError('PROVIDER_PARAMETER_DENIED', 'Request uses an unverified provider capability', 400); - if (data.max_output_tokens !== undefined && (!Number.isSafeInteger(data.max_output_tokens) || data.max_output_tokens < 1)) throw new DomainError('PROVIDER_INVALID_REQUEST', 'max_output_tokens must be a positive integer', 400); + const allowed = new Set([ + 'model', + 'input', + 'instructions', + 'tools', + 'tool_choice', + 'parallel_tool_calls', + 'stream', + 'stream_options', + 'reasoning', + 'store', + 'background', + 'include', + 'service_tier', + 'text', + 'prompt_cache_key', + 'prompt_cache_retention', + 'prompt_cache_options', + 'metadata', + 'truncation', + 'max_output_tokens', + 'temperature', + 'top_p', + 'top_logprobs', + 'user', + 'safety_identifier', + ]); + if (Object.keys(data).some(key => !allowed.has(key))) + throw new DomainError('PROVIDER_PARAMETER_DENIED', 'Request uses an unverified provider capability', 400); + if ( + data.max_output_tokens !== undefined && + (!Number.isSafeInteger(data.max_output_tokens) || data.max_output_tokens < 1) + ) + throw new DomainError('PROVIDER_INVALID_REQUEST', 'max_output_tokens must be a positive integer', 400); const localTool = (tool: any, depth = 0): boolean => { if (!tool || typeof tool !== 'object' || depth > 3) return false; if (['function', 'custom'].includes(tool.type)) return true; - return tool.type === 'namespace' && typeof tool.name === 'string' && /^[a-zA-Z0-9_-]{1,80}$/.test(tool.name) && Array.isArray(tool.tools) && tool.tools.length > 0 && tool.tools.length <= 128 && tool.tools.every((child: any) => localTool(child, depth + 1)); + return ( + tool.type === 'namespace' && + typeof tool.name === 'string' && + /^[a-zA-Z0-9_-]{1,80}$/.test(tool.name) && + Array.isArray(tool.tools) && + tool.tools.length > 0 && + tool.tools.length <= 128 && + tool.tools.every((child: any) => localTool(child, depth + 1)) + ); }; - if (data.tools !== undefined && (!Array.isArray(data.tools) || data.tools.length > 128 || !data.tools.every((tool: any) => localTool(tool)))) throw new DomainError('PROVIDER_REMOTE_TOOL_DENIED', 'Only local function/custom tools and bounded namespaces of those tools are permitted', 403); + if ( + data.tools !== undefined && + (!Array.isArray(data.tools) || data.tools.length > 128 || !data.tools.every((tool: any) => localTool(tool))) + ) + throw new DomainError( + 'PROVIDER_REMOTE_TOOL_DENIED', + 'Only local function/custom tools and bounded namespaces of those tools are permitted', + 403, + ); const inspect = (value: any, depth = 0) => { if (depth > 60) throw new DomainError('PROVIDER_INVALID_REQUEST', 'Request nesting exceeds limit', 400); if (!value || typeof value !== 'object') return; - if (['input_image', 'input_audio', 'input_file', 'image_url', 'file', 'audio', 'video', 'item_reference'].includes(value.type) || value.file_id || value.image_url || value.audio_url) throw new DomainError('PROVIDER_MULTIMODAL_DENIED', 'Only self-contained text inputs are supported for bounded proxy metering', 403); + if ( + ['input_image', 'input_audio', 'input_file', 'image_url', 'file', 'audio', 'video', 'item_reference'].includes( + value.type, + ) || + value.file_id || + value.image_url || + value.audio_url + ) + throw new DomainError( + 'PROVIDER_MULTIMODAL_DENIED', + 'Only self-contained text inputs are supported for bounded proxy metering', + 403, + ); for (const child of Object.values(value)) inspect(child, depth + 1); - }; inspect(data.input); + }; + inspect(data.input); return { raw, data }; } async forward(request: ProviderProxyRequest): Promise { this.requireConfigured(); - if (!((request.method === 'POST' && request.path === '/responses') || (request.method === 'GET' && request.path === '/models'))) throw new DomainError('PROVIDER_ROUTE_DENIED', 'Only POST /responses and GET /models are permitted', 404); - const claim = await this.claims(request.authorization), parsed = this.body(request); - const headers = request.headers instanceof Headers ? request.headers : new Headers(Object.entries(request.headers ?? {}).filter((pair): pair is [string, string] => typeof pair[1] === 'string')); + if (!( + (request.method === 'POST' && request.path === '/responses') || + (request.method === 'GET' && request.path === '/models') + )) + throw new DomainError('PROVIDER_ROUTE_DENIED', 'Only POST /responses and GET /models are permitted', 404); + const claim = await this.claims(request.authorization), + parsed = this.body(request); + const headers = + request.headers instanceof Headers + ? request.headers + : new Headers( + Object.entries(request.headers ?? {}).filter( + (pair): pair is [string, string] => typeof pair[1] === 'string', + ), + ); const explicitKey = headers.get('idempotency-key') ?? headers.get('x-client-request-id'); - if (explicitKey && !/^[a-zA-Z0-9_.:-]{8,200}$/.test(explicitKey)) throw new DomainError('PROVIDER_INVALID_REQUEST_ID', 'Invalid request idempotency key', 400); - const bodyDigest = hash(parsed.raw); const requestId = `proxy-${hash(JSON.stringify([claim.runId, claim.generation, claim.leaseToken, claim.connectionId, claim.connectionVersion, request.path, explicitKey ?? bodyDigest]))}`; + if (explicitKey && !/^[a-zA-Z0-9_.:-]{8,200}$/.test(explicitKey)) + throw new DomainError('PROVIDER_INVALID_REQUEST_ID', 'Invalid request idempotency key', 400); + const bodyDigest = hash(parsed.raw); + const requestId = `proxy-${hash(JSON.stringify([claim.runId, claim.generation, claim.leaseToken, claim.connectionId, claim.connectionVersion, request.path, explicitKey ?? bodyDigest]))}`; const prepared = await this.store.transaction(claim.tenantId, async tx => { const { run, connection } = await this.valid(tx, claim); const prior = await tx.get('provider_proxy_request', requestId); if (prior) { - if (prior.bodyDigest !== bodyDigest) throw new DomainError('PROVIDER_IDEMPOTENCY_CONFLICT', 'Request key was reused for different input'); - if (prior.status === 'unknown') throw new DomainError('PROVIDER_OUTCOME_UNKNOWN', 'Prior upstream outcome requires reconciliation; no retry was sent', 409); - if (prior.status === 'dispatched') throw new DomainError('PROVIDER_REQUEST_IN_FLIGHT', 'The original request is still in flight; it was not repeated', 409); - if (prior.responseBody === undefined) throw new DomainError('PROVIDER_RESPONSE_NOT_REPLAYABLE', 'Request was already processed; inspect recorded response identity before continuing', 409); + if (prior.bodyDigest !== bodyDigest) + throw new DomainError('PROVIDER_IDEMPOTENCY_CONFLICT', 'Request key was reused for different input'); + if (prior.status === 'unknown') + throw new DomainError( + 'PROVIDER_OUTCOME_UNKNOWN', + 'Prior upstream outcome requires reconciliation; no retry was sent', + 409, + ); + if (prior.status === 'dispatched') + throw new DomainError( + 'PROVIDER_REQUEST_IN_FLIGHT', + 'The original request is still in flight; it was not repeated', + 409, + ); + if (prior.responseBody === undefined) + throw new DomainError( + 'PROVIDER_RESPONSE_NOT_REPLAYABLE', + 'Request was already processed; inspect recorded response identity before continuing', + 409, + ); return { replay: prior, connection }; } - if (run.providerProxyActiveRequest) throw new DomainError('PROVIDER_REQUEST_IN_FLIGHT', 'Only one model request per run is allowed; an unresolved request retains its reservation', 409); - if (run.usedTokens >= run.budget.maxTokens || run.reservedTokens < 1) throw new DomainError('PROVIDER_BUDGET_EXHAUSTED', 'Run token budget is exhausted', 429); + if (run.providerProxyActiveRequest) + throw new DomainError( + 'PROVIDER_REQUEST_IN_FLIGHT', + 'Only one model request per run is allowed; an unresolved request retains its reservation', + 409, + ); + if (run.usedTokens >= run.budget.maxTokens || run.reservedTokens < 1) + throw new DomainError('PROVIDER_BUDGET_EXHAUSTED', 'Run token budget is exhausted', 429); let reserved = 0; if (parsed.data) { // Text-only requests use a deliberately conservative byte-count reservation, @@ -117,89 +411,359 @@ export class ProviderProxy { const inputReserve = Buffer.byteLength(parsed.raw) + 4096; const remaining = Math.min(run.budget.maxTokens - run.usedTokens, run.reservedTokens); const outputLimit = Math.min(parsed.data.max_output_tokens ?? 8192, remaining - inputReserve); - if (outputLimit < 16) throw new DomainError('PROVIDER_BUDGET_EXHAUSTED', 'Remaining budget cannot cover bounded input and output', 429); - parsed.data.max_output_tokens = outputLimit; parsed.data.store = false; parsed.data.background = false; parsed.data.service_tier = 'default'; parsed.data.safety_identifier = hash(`${claim.tenantId}/${claim.runId}`); parsed.data.prompt_cache_key = hash(`${claim.tenantId}/${claim.runId}`); delete parsed.data.user; reserved = inputReserve + outputLimit; + if (outputLimit < 16) + throw new DomainError( + 'PROVIDER_BUDGET_EXHAUSTED', + 'Remaining budget cannot cover bounded input and output', + 429, + ); + parsed.data.max_output_tokens = outputLimit; + parsed.data.store = false; + parsed.data.background = false; + parsed.data.service_tier = 'default'; + parsed.data.safety_identifier = hash(`${claim.tenantId}/${claim.runId}`); + parsed.data.prompt_cache_key = hash(`${claim.tenantId}/${claim.runId}`); + delete parsed.data.user; + reserved = inputReserve + outputLimit; } - const operation: ProxyRequestRecord = { ...record(claim.tenantId), id: requestId, runId: run.id, projectId: run.projectId, connectionId: connection.id, connectionVersion: connection.version, generation: claim.generation, leaseToken: claim.leaseToken, bodyDigest, path: request.path, status: 'dispatched', reservedTokens: reserved }; - run.providerProxyActiveRequest = requestId; await tx.put('run', touch(run)); await tx.put('provider_proxy_request', operation); - await tx.event(run.id, 'provider.proxy.dispatched', 'Scoped model request recorded before upstream I/O.', { requestId, reservedTokens: reserved }); return { operation, connection }; + const operation: ProxyRequestRecord = { + ...record(claim.tenantId), + id: requestId, + runId: run.id, + projectId: run.projectId, + connectionId: connection.id, + connectionVersion: connection.version, + generation: claim.generation, + leaseToken: claim.leaseToken, + bodyDigest, + path: request.path, + status: 'dispatched', + reservedTokens: reserved, + }; + run.providerProxyActiveRequest = requestId; + await tx.put('run', touch(run)); + await tx.put('provider_proxy_request', operation); + await tx.event(run.id, 'provider.proxy.dispatched', 'Scoped model request recorded before upstream I/O.', { + requestId, + reservedTokens: reserved, + }); + return { operation, connection }; }); - if ('replay' in prepared && prepared.replay) return new Response(prepared.replay.responseBody, { status: prepared.replay.responseStatus, headers: { 'content-type': prepared.replay.responseType!, 'cache-control': 'no-store', 'x-wishforge-request-id': requestId } }); + if ('replay' in prepared && prepared.replay) + return new Response(prepared.replay.responseBody, { + status: prepared.replay.responseStatus, + headers: { + 'content-type': prepared.replay.responseType!, + 'cache-control': 'no-store', + 'x-wishforge-request-id': requestId, + }, + }); const operation = prepared.operation!; - const abort = new AbortController(); let reader: ReadableStreamDefaultReader | undefined; let settled = false, settlePromise: Promise | undefined, pollBusy = false, upstreamStarted = false; - const finish = (status: ProxyRequestRecord['status'], usageTokens?: number, response?: { status: number; type: string; body?: string; id?: string }, reason?: string): Promise => { + const abort = new AbortController(); + let reader: ReadableStreamDefaultReader | undefined; + let settled = false, + settlePromise: Promise | undefined, + pollBusy = false, + upstreamStarted = false; + const finish = ( + status: ProxyRequestRecord['status'], + usageTokens?: number, + response?: { status: number; type: string; body?: string; id?: string }, + reason?: string, + ): Promise => { if (settlePromise) return settlePromise; - settled = true; clearInterval(timer); request.signal?.removeEventListener('abort', canceled); + settled = true; + clearInterval(timer); + request.signal?.removeEventListener('abort', canceled); settlePromise = this.store.transaction(claim.tenantId, async tx => { - const op = await must(tx, 'provider_proxy_request', requestId); if (op.status !== 'dispatched') return; - const run = await must(tx, 'run', claim.runId); op.status = status; op.usageTokens = usageTokens; op.unknownReason = reason; op.responseId = response?.id; op.responseStatus = response?.status; op.responseType = response?.type; op.responseBody = response?.body; - if (usageTokens !== undefined) { await tx.meter({ id: `provider-proxy:${requestId}`, runId: run.id, tokens: usageTokens, computeSeconds: 0, storageBytes: 0, at: now() }); run.usedTokens += usageTokens; run.reservedTokens = Math.max(0, run.reservedTokens - usageTokens); } - if (status === 'unknown') { run.reservedTokens = Math.max(run.reservedTokens, op.reservedTokens); if (!['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)) { run.status = 'waiting'; run.waitingReason = 'provider_usage_unknown'; if (run.lease) run.lease.expiresAt = now(); } } - else if (run.providerProxyActiveRequest === requestId) { run.providerProxyActiveRequest = undefined; if (['canceled', 'failed', 'succeeded'].includes(run.status)) run.reservedTokens = 0; } - if (run.usedTokens >= run.budget.maxTokens && !['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)) { run.status = 'waiting'; run.waitingReason = 'budget'; if (run.lease) run.lease.expiresAt = now(); } - await tx.put('provider_proxy_request', touch(op)); await tx.put('run', touch(run)); await tx.event(run.id, `provider.proxy.${status}`, status === 'unknown' ? 'Upstream outcome or usage is unknown; new model requests are blocked pending reconciliation.' : 'Trusted upstream request outcome recorded.', { requestId, usageTokens, responseId: response?.id }); - }); return settlePromise; + const op = await must(tx, 'provider_proxy_request', requestId); + if (op.status !== 'dispatched') return; + const run = await must(tx, 'run', claim.runId); + op.status = status; + op.usageTokens = usageTokens; + op.unknownReason = reason; + op.responseId = response?.id; + op.responseStatus = response?.status; + op.responseType = response?.type; + op.responseBody = response?.body; + if (usageTokens !== undefined) { + await tx.meter({ + id: `provider-proxy:${requestId}`, + runId: run.id, + tokens: usageTokens, + computeSeconds: 0, + storageBytes: 0, + at: now(), + }); + run.usedTokens += usageTokens; + run.reservedTokens = Math.max(0, run.reservedTokens - usageTokens); + } + if (status === 'unknown') { + run.reservedTokens = Math.max(run.reservedTokens, op.reservedTokens); + if (!['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)) { + run.status = 'waiting'; + run.waitingReason = 'provider_usage_unknown'; + if (run.lease) run.lease.expiresAt = now(); + } + } else if (run.providerProxyActiveRequest === requestId) { + run.providerProxyActiveRequest = undefined; + if (['canceled', 'failed', 'succeeded'].includes(run.status)) run.reservedTokens = 0; + } + if ( + run.usedTokens >= run.budget.maxTokens && + !['canceled', 'failed', 'succeeded', 'paused'].includes(run.status) + ) { + run.status = 'waiting'; + run.waitingReason = 'budget'; + if (run.lease) run.lease.expiresAt = now(); + } + await tx.put('provider_proxy_request', touch(op)); + await tx.put('run', touch(run)); + await tx.event( + run.id, + `provider.proxy.${status}`, + status === 'unknown' + ? 'Upstream outcome or usage is unknown; new model requests are blocked pending reconciliation.' + : 'Trusted upstream request outcome recorded.', + { requestId, usageTokens, responseId: response?.id }, + ); + }); + return settlePromise; }; - const canceled = () => { abort.abort(); void reader?.cancel().catch(() => undefined); if (!settled) { const unknown = upstreamStarted && request.method === 'POST'; void finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, undefined, 'client_disconnected'); } }; - const timer = setInterval(() => { if (settled || pollBusy) return; pollBusy = true; void this.store.transaction(claim.tenantId, tx => this.valid(tx, claim)).catch(() => { abort.abort(); void reader?.cancel().catch(() => undefined); if (!settled) { const unknown = upstreamStarted && request.method === 'POST'; void finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, undefined, 'lease_or_connection_revoked'); } }).finally(() => { pollBusy = false; }); }, this.pollMs); timer.unref(); + const canceled = () => { + abort.abort(); + void reader?.cancel().catch(() => undefined); + if (!settled) { + const unknown = upstreamStarted && request.method === 'POST'; + void finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, undefined, 'client_disconnected'); + } + }; + const timer = setInterval(() => { + if (settled || pollBusy) return; + pollBusy = true; + void this.store + .transaction(claim.tenantId, tx => this.valid(tx, claim)) + .catch(() => { + abort.abort(); + void reader?.cancel().catch(() => undefined); + if (!settled) { + const unknown = upstreamStarted && request.method === 'POST'; + void finish( + unknown ? 'unknown' : 'rejected', + unknown ? undefined : 0, + undefined, + 'lease_or_connection_revoked', + ); + } + }) + .finally(() => { + pollBusy = false; + }); + }, this.pollMs); + timer.unref(); request.signal?.addEventListener('abort', canceled, { once: true }); try { - if (request.signal?.aborted) { canceled(); await settlePromise; throw new DomainError('PROVIDER_REQUEST_CANCELED', 'Request canceled', 409); } + if (request.signal?.aborted) { + canceled(); + await settlePromise; + throw new DomainError('PROVIDER_REQUEST_CANCELED', 'Request canceled', 409); + } const apiKey = await this.vault.open(claim.tenantId, String(prepared.connection.ciphertext)); - if (!/^[a-zA-Z0-9_-]{8,16000}$/.test(apiKey)) throw new DomainError('PROVIDER_KEY_INVALID', 'Provider credential format is invalid', 503); + if (!/^[a-zA-Z0-9_-]{8,16000}$/.test(apiKey)) + throw new DomainError('PROVIDER_KEY_INVALID', 'Provider credential format is invalid', 503); await this.store.transaction(claim.tenantId, tx => this.valid(tx, claim)); upstreamStarted = true; - const upstream = await this.fetcher(`${UPSTREAM}${request.path}`, { method: request.method, headers: { Authorization: `Bearer ${apiKey}`, 'content-type': 'application/json', Accept: parsed.data?.stream === true ? 'text/event-stream' : 'application/json', 'X-Client-Request-Id': requestId }, ...(parsed.data ? { body: JSON.stringify(parsed.data) } : {}), redirect: 'error', signal: AbortSignal.any([abort.signal, AbortSignal.timeout(120000)]) }); - const redact = (text: string) => text.replaceAll(apiKey, '[redacted]').replaceAll(request.authorization!.slice(7), '[redacted]'); + const upstream = await this.fetcher(`${UPSTREAM}${request.path}`, { + method: request.method, + headers: { + Authorization: `Bearer ${apiKey}`, + 'content-type': 'application/json', + Accept: parsed.data?.stream === true ? 'text/event-stream' : 'application/json', + 'X-Client-Request-Id': requestId, + }, + ...(parsed.data ? { body: JSON.stringify(parsed.data) } : {}), + redirect: 'error', + signal: AbortSignal.any([abort.signal, AbortSignal.timeout(120000)]), + }); + const redact = (text: string) => + text.replaceAll(apiKey, '[redacted]').replaceAll(request.authorization!.slice(7), '[redacted]'); if (!upstream.ok) { - await upstream.body?.cancel(); const unknown = request.method === 'POST' && (upstream.status >= 500 || upstream.status === 408); - const text = JSON.stringify({ error: { code: unknown ? 'PROVIDER_OUTCOME_UNKNOWN' : 'PROVIDER_REQUEST_REJECTED', type: 'provider_proxy_error', message: unknown ? 'Provider outcome is unknown; reconcile before retrying.' : 'Provider rejected the request.' } }); - await finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, { status: upstream.status, type: 'application/json', body: text }); return new Response(text, { status: upstream.status, headers: { 'content-type': 'application/json', 'cache-control': 'no-store' } }); + await upstream.body?.cancel(); + const unknown = request.method === 'POST' && (upstream.status >= 500 || upstream.status === 408); + const text = JSON.stringify({ + error: { + code: unknown ? 'PROVIDER_OUTCOME_UNKNOWN' : 'PROVIDER_REQUEST_REJECTED', + type: 'provider_proxy_error', + message: unknown + ? 'Provider outcome is unknown; reconcile before retrying.' + : 'Provider rejected the request.', + }, + }); + await finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, { + status: upstream.status, + type: 'application/json', + body: text, + }); + return new Response(text, { + status: upstream.status, + headers: { 'content-type': 'application/json', 'cache-control': 'no-store' }, + }); } if (!upstream.body) throw new DomainError('PROVIDER_RESPONSE_INVALID', 'Provider response body is missing', 502); reader = upstream.body.getReader(); if (parsed.data?.stream === true) { - if (!upstream.headers.get('content-type')?.includes('text/event-stream')) throw new DomainError('PROVIDER_RESPONSE_INVALID', 'Expected a Responses event stream', 502); + if (!upstream.headers.get('content-type')?.includes('text/event-stream')) + throw new DomainError('PROVIDER_RESPONSE_INVALID', 'Expected a Responses event stream', 502); const stream = new ReadableStream({ - start: controller => { void (async () => { - const decoder = new TextDecoder(); const encoder = new TextEncoder(); let buffer = '', total = 0, replay = '', responseId: string | undefined; - try { - while (!settled) { - const item = await reader!.read(); if (item.done) break; - total += item.value.byteLength; if (total > MAX_RESPONSE) throw new Error('Response size limit'); buffer = (buffer + decoder.decode(item.value, { stream: true })).replace(/\r\n/g, '\n'); - let index: number; - while ((index = buffer.indexOf('\n\n')) >= 0) { - const frame = buffer.slice(0, index); buffer = buffer.slice(index + 2); - if (Buffer.byteLength(frame) > MAX_FRAME) throw new Error('SSE frame size limit'); - const data = frame.split('\n').filter(line => line.startsWith('data:')).map(line => line.slice(5).trimStart()).join('\n'); - let event: any; if (data && data !== '[DONE]') { try { event = JSON.parse(data); } catch { throw new Error('Invalid SSE JSON'); } } - if (event?.response?.id && typeof event.response.id === 'string') { if (responseId && responseId !== event.response.id) throw new Error('Response identity mismatch'); responseId = event.response.id; } - const safe = redact(`${frame}\n\n`); if (replay.length <= MAX_REPLAY) replay += safe; - if (['response.completed', 'response.incomplete', 'response.failed'].includes(event?.type)) { - const usage = trustedUsage(event.response?.usage); if (usage === undefined || !responseId) throw new Error('Terminal response lacks trusted usage'); - await finish('completed', usage, { status: 200, type: 'text/event-stream', body: replay.length <= MAX_REPLAY ? replay : undefined, id: responseId }); controller.enqueue(encoder.encode(safe)); controller.close(); await reader!.cancel(); return; + start: controller => { + void (async () => { + const decoder = new TextDecoder(); + const encoder = new TextEncoder(); + let buffer = '', + total = 0, + replay = '', + responseId: string | undefined; + try { + while (!settled) { + const item = await reader!.read(); + if (item.done) break; + total += item.value.byteLength; + if (total > MAX_RESPONSE) throw new Error('Response size limit'); + buffer = (buffer + decoder.decode(item.value, { stream: true })).replace(/\r\n/g, '\n'); + let index: number; + while ((index = buffer.indexOf('\n\n')) >= 0) { + const frame = buffer.slice(0, index); + buffer = buffer.slice(index + 2); + if (Buffer.byteLength(frame) > MAX_FRAME) throw new Error('SSE frame size limit'); + const data = frame + .split('\n') + .filter(line => line.startsWith('data:')) + .map(line => line.slice(5).trimStart()) + .join('\n'); + let event: any; + if (data && data !== '[DONE]') { + try { + event = JSON.parse(data); + } catch { + throw new Error('Invalid SSE JSON'); + } + } + if (event?.response?.id && typeof event.response.id === 'string') { + if (responseId && responseId !== event.response.id) throw new Error('Response identity mismatch'); + responseId = event.response.id; + } + const safe = redact(`${frame}\n\n`); + if (replay.length <= MAX_REPLAY) replay += safe; + if (['response.completed', 'response.incomplete', 'response.failed'].includes(event?.type)) { + const usage = trustedUsage(event.response?.usage); + if (usage === undefined || !responseId) throw new Error('Terminal response lacks trusted usage'); + await finish('completed', usage, { + status: 200, + type: 'text/event-stream', + body: replay.length <= MAX_REPLAY ? replay : undefined, + id: responseId, + }); + controller.enqueue(encoder.encode(safe)); + controller.close(); + await reader!.cancel(); + return; + } + controller.enqueue(encoder.encode(safe)); } - controller.enqueue(encoder.encode(safe)); + if (Buffer.byteLength(buffer) > MAX_FRAME) throw new Error('SSE frame size limit'); } - if (Buffer.byteLength(buffer) > MAX_FRAME) throw new Error('SSE frame size limit'); + if (!settled) await finish('unknown', undefined, undefined, 'stream_ended_without_usage'); + controller.error(new Error('Provider stream ended without a trusted terminal usage receipt')); + } catch { + if (!settled) await finish('unknown', undefined, undefined, 'stream_interrupted'); + abort.abort(); + await reader?.cancel().catch(() => undefined); + controller.error(new Error('Provider stream interrupted; request outcome requires reconciliation')); } - if (!settled) await finish('unknown', undefined, undefined, 'stream_ended_without_usage'); - controller.error(new Error('Provider stream ended without a trusted terminal usage receipt')); - } catch { if (!settled) await finish('unknown', undefined, undefined, 'stream_interrupted'); abort.abort(); await reader?.cancel().catch(() => undefined); controller.error(new Error('Provider stream interrupted; request outcome requires reconciliation')); } - })(); }, - cancel: async () => { canceled(); await settlePromise; }, + })(); + }, + cancel: async () => { + canceled(); + await settlePromise; + }, + }); + return new Response(stream, { + status: 200, + headers: { + 'content-type': 'text/event-stream', + 'cache-control': 'no-store', + 'x-accel-buffering': 'no', + 'x-wishforge-request-id': requestId, + }, }); - return new Response(stream, { status: 200, headers: { 'content-type': 'text/event-stream', 'cache-control': 'no-store', 'x-accel-buffering': 'no', 'x-wishforge-request-id': requestId } }); } - const chunks: Uint8Array[] = []; let total = 0; - while (true) { const item = await reader.read(); if (item.done) break; total += item.value.byteLength; if (total > MAX_REPLAY) throw new Error('Response size limit'); chunks.push(item.value); } + const chunks: Uint8Array[] = []; + let total = 0; + while (true) { + const item = await reader.read(); + if (item.done) break; + total += item.value.byteLength; + if (total > MAX_REPLAY) throw new Error('Response size limit'); + chunks.push(item.value); + } if (settled) throw new DomainError('PROVIDER_REQUEST_CANCELED', 'Request was canceled or revoked', 409); - const raw = new TextDecoder().decode(Buffer.concat(chunks)); let data: any; try { data = JSON.parse(raw); } catch { throw new Error('Provider returned invalid JSON'); } + const raw = new TextDecoder().decode(Buffer.concat(chunks)); + let data: any; + try { + data = JSON.parse(raw); + } catch { + throw new Error('Provider returned invalid JSON'); + } const usage = request.path === '/models' ? 0 : trustedUsage(data.usage); - if (usage === undefined || (request.path === '/responses' && typeof data.id !== 'string')) throw new Error('Provider response lacks trusted usage'); - if (request.path === '/models') { if (!Array.isArray(data.data)) throw new Error('Invalid model listing'); data = { object: 'list', data: data.data.filter((model: any) => this.models().has(model.id)) }; } - const safe = redact(JSON.stringify(data)); await finish('completed', usage, { status: 200, type: 'application/json', body: safe, id: request.path === '/responses' ? data.id : undefined }); return new Response(safe, { status: 200, headers: { 'content-type': 'application/json', 'cache-control': 'no-store', 'x-wishforge-request-id': requestId } }); - } catch (error) { abort.abort(); await reader?.cancel().catch(() => undefined); const unknown = upstreamStarted && request.method === 'POST'; if (!settled) await finish(unknown ? 'unknown' : 'rejected', unknown ? undefined : 0, undefined, unknown ? 'upstream_result_unknown' : 'credential_or_lease_unavailable'); if (error instanceof DomainError) throw error; throw new DomainError(unknown ? 'PROVIDER_OUTCOME_UNKNOWN' : 'PROVIDER_UNAVAILABLE', 'Upstream response unavailable; no automatic retry was sent', 503); } + if (usage === undefined || (request.path === '/responses' && typeof data.id !== 'string')) + throw new Error('Provider response lacks trusted usage'); + if (request.path === '/models') { + if (!Array.isArray(data.data)) throw new Error('Invalid model listing'); + data = { object: 'list', data: data.data.filter((model: any) => this.models().has(model.id)) }; + } + const safe = redact(JSON.stringify(data)); + await finish('completed', usage, { + status: 200, + type: 'application/json', + body: safe, + id: request.path === '/responses' ? data.id : undefined, + }); + return new Response(safe, { + status: 200, + headers: { + 'content-type': 'application/json', + 'cache-control': 'no-store', + 'x-wishforge-request-id': requestId, + }, + }); + } catch (error) { + abort.abort(); + await reader?.cancel().catch(() => undefined); + const unknown = upstreamStarted && request.method === 'POST'; + if (!settled) + await finish( + unknown ? 'unknown' : 'rejected', + unknown ? undefined : 0, + undefined, + unknown ? 'upstream_result_unknown' : 'credential_or_lease_unavailable', + ); + if (error instanceof DomainError) throw error; + throw new DomainError( + unknown ? 'PROVIDER_OUTCOME_UNKNOWN' : 'PROVIDER_UNAVAILABLE', + 'Upstream response unavailable; no automatic retry was sent', + 503, + ); + } } } -function trustedUsage(value: any): number | undefined { return value && Number.isSafeInteger(value.input_tokens) && value.input_tokens >= 0 && Number.isSafeInteger(value.output_tokens) && value.output_tokens >= 0 && Number.isSafeInteger(value.total_tokens) && value.total_tokens === value.input_tokens + value.output_tokens ? value.total_tokens : undefined; } +function trustedUsage(value: any): number | undefined { + return value && + Number.isSafeInteger(value.input_tokens) && + value.input_tokens >= 0 && + Number.isSafeInteger(value.output_tokens) && + value.output_tokens >= 0 && + Number.isSafeInteger(value.total_tokens) && + value.total_tokens === value.input_tokens + value.output_tokens + ? value.total_tokens + : undefined; +} diff --git a/apps/api/src/domain/provisioner-target.ts b/apps/api/src/domain/provisioner-target.ts index 2e91f9b..e1cafc0 100644 --- a/apps/api/src/domain/provisioner-target.ts +++ b/apps/api/src/domain/provisioner-target.ts @@ -3,27 +3,59 @@ import { z } from 'zod'; import { DomainError } from '@wishforge/contracts'; const tenantIdSchema = z.string().regex(/^[A-Za-z0-9_-]{1,128}$/); -const targetSchema = z.object({ url: z.string().max(2048), tokenEnv: z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/) }).strict(); +const targetSchema = z + .object({ url: z.string().max(2048), tokenEnv: z.string().regex(/^WF_[A-Z0-9_]{1,100}_TOKEN$/) }) + .strict(); const fileSchema = z.object({ schemaVersion: z.literal(1), tenants: z.record(tenantIdSchema, targetSchema) }).strict(); -export interface ProvisionerTarget { url: string; token: string } +export interface ProvisionerTarget { + url: string; + token: string; +} function checkedTarget(url: string, token: string | undefined, env: NodeJS.ProcessEnv): ProvisionerTarget { let parsed: URL; - try { parsed = new URL(url); } catch { throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Operator provisioner URL is invalid', 503); } + try { + parsed = new URL(url); + } catch { + throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Operator provisioner URL is invalid', 503); + } const loopback = ['127.0.0.1', 'localhost', '[::1]'].includes(parsed.hostname); - if (!(parsed.protocol === 'https:' || (env.NODE_ENV !== 'production' && parsed.protocol === 'http:' && loopback)) || parsed.username || parsed.password || parsed.search || parsed.hash || !token || token.length < 32) throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Provisioner requires a scoped service token and HTTPS (local development loopback excepted)', 503); + if ( + !(parsed.protocol === 'https:' || (env.NODE_ENV !== 'production' && parsed.protocol === 'http:' && loopback)) || + parsed.username || + parsed.password || + parsed.search || + parsed.hash || + !token || + token.length < 32 + ) + throw new DomainError( + 'PROVISIONER_CONFIGURATION_INVALID', + 'Provisioner requires a scoped service token and HTTPS (local development loopback excepted)', + 503, + ); return { url: parsed.toString().replace(/\/$/, ''), token }; } /** Operator-owned routing only: no request/model value can supply an endpoint or credential. */ -export async function resolveProvisionerTarget(env: NodeJS.ProcessEnv, tenantId: string): Promise { - if (!tenantIdSchema.safeParse(tenantId).success) throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Invalid provisioner tenant scope', 503); +export async function resolveProvisionerTarget( + env: NodeJS.ProcessEnv, + tenantId: string, +): Promise { + if (!tenantIdSchema.safeParse(tenantId).success) + throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Invalid provisioner tenant scope', 503); if (env.WF_PROVISIONER_TARGETS_FILE) { let parsed: z.infer; try { const bytes = await readFile(env.WF_PROVISIONER_TARGETS_FILE); if (bytes.length > 65536) throw new Error('too large'); parsed = fileSchema.parse(JSON.parse(bytes.toString('utf8'))); - } catch { throw new DomainError('PROVISIONER_CONFIGURATION_INVALID', 'Operator tenant provisioner map is unreadable or invalid', 503); } + } catch { + throw new DomainError( + 'PROVISIONER_CONFIGURATION_INVALID', + 'Operator tenant provisioner map is unreadable or invalid', + 503, + ); + } const entry = Object.prototype.hasOwnProperty.call(parsed.tenants, tenantId) ? parsed.tenants[tenantId] : undefined; // Presence of a map explicitly disables legacy fallback, including for omitted tenants. return entry ? checkedTarget(entry.url, env[entry.tokenEnv], env) : undefined; @@ -31,9 +63,21 @@ export async function resolveProvisionerTarget(env: NodeJS.ProcessEnv, tenantId: // Local setup pre-generates a credential before a provisioner is installed. // A credential alone is not an endpoint and must leave runs safely waiting. if (!env.WF_PROVISIONER_URL) return undefined; - const configured = [...new Set((env.WF_DISPATCH_TENANTS ?? '').split(',').map(value => value.trim()).filter(Boolean))]; + const configured = [ + ...new Set( + (env.WF_DISPATCH_TENANTS ?? '') + .split(',') + .map(value => value.trim()) + .filter(Boolean), + ), + ]; const boundTenant = env.WF_PROVISIONER_TENANT_ID ?? (configured.length === 1 ? configured[0] : undefined); - if (!boundTenant) throw new DomainError('PROVISIONER_TENANT_BINDING_REQUIRED', 'A legacy global provisioner requires one explicitly bound tenant; configure the tenant provisioner map for multiple tenants', 503); + if (!boundTenant) + throw new DomainError( + 'PROVISIONER_TENANT_BINDING_REQUIRED', + 'A legacy global provisioner requires one explicitly bound tenant; configure the tenant provisioner map for multiple tenants', + 503, + ); if (boundTenant !== tenantId) return undefined; return checkedTarget(env.WF_PROVISIONER_URL, env.WF_PROVISIONER_TOKEN, env); } diff --git a/apps/api/src/domain/releases.ts b/apps/api/src/domain/releases.ts index 319caa0..08571a1 100644 --- a/apps/api/src/domain/releases.ts +++ b/apps/api/src/domain/releases.ts @@ -1,59 +1,315 @@ -import {requireDeploymentSecretPolicy} from './deployment-secret-policy.js'; -import {privatePolicyForEnvironment,verifyPrivateEvidence,publicEvidence} from './private-access-verifier.js'; -import {gatewayIdentityOrigins} from './gateway-target.js'; -import { DomainError, environmentInput, releaseInput, type Actor, type Environment, type Release, type Approval, type Run, type Evidence, type Project } from '@wishforge/contracts'; +import { requireDeploymentSecretPolicy } from './deployment-secret-policy.js'; +import { privatePolicyForEnvironment, verifyPrivateEvidence, publicEvidence } from './private-access-verifier.js'; +import { gatewayIdentityOrigins } from './gateway-target.js'; +import { + DomainError, + environmentInput, + releaseInput, + type Actor, + type Environment, + type Release, + type Approval, + type Run, + type Evidence, + type Project, +} from '@wishforge/contracts'; import type { Store, Transaction } from '../db/store.js'; import { record, touch, must, projectAccess, idempotent, now, executionScope } from './control-plane.js'; import { digest, releaseRisk, requireRole, requireHuman, redact } from './policy.js'; -import {accessConfiguration,assertApplicationConfigured,assertApplicationAccepted} from './application-access.js'; +import { accessConfiguration, assertApplicationConfigured, assertApplicationAccepted } from './application-access.js'; import { assertVerificationProfile, verificationPolicyDigest } from './verification-policy.js'; export function releaseBinding(release: Release) { - return { deploymentSecretPolicyDigest:release.deploymentSecretPolicyDigest,privateAccessPolicyDigest:release.privateAccessPolicyDigest,applicationUrl:release.applicationUrl,visibility:release.visibility,deliveryRequired:release.deliveryRequired,developmentLoopback:release.developmentLoopback,runId: release.runId, revision: release.revision, sourceCommit: release.sourceCommit, imageDigest: release.imageDigest, architecture: release.architecture, configVersion: release.configVersion, secretVersionRefs: release.secretVersionRefs, environmentId: release.environmentId, evidenceIds: release.evidenceIds.slice(0, 2), policyVersion: release.policyVersion, previousReleaseId: release.previousReleaseId, deliveryConfigSnapshot: release.deliveryConfigSnapshot, verificationPolicyDigest: release.verificationPolicyDigest }; + return { + deploymentSecretPolicyDigest: release.deploymentSecretPolicyDigest, + privateAccessPolicyDigest: release.privateAccessPolicyDigest, + applicationUrl: release.applicationUrl, + visibility: release.visibility, + deliveryRequired: release.deliveryRequired, + developmentLoopback: release.developmentLoopback, + runId: release.runId, + revision: release.revision, + sourceCommit: release.sourceCommit, + imageDigest: release.imageDigest, + architecture: release.architecture, + configVersion: release.configVersion, + secretVersionRefs: release.secretVersionRefs, + environmentId: release.environmentId, + evidenceIds: release.evidenceIds.slice(0, 2), + policyVersion: release.policyVersion, + previousReleaseId: release.previousReleaseId, + deliveryConfigSnapshot: release.deliveryConfigSnapshot, + verificationPolicyDigest: release.verificationPolicyDigest, + }; } export function deploymentIdentity(actor: Actor, environmentId: string, runId: string) { - if (actor.kind !== 'deployer' || (actor.runId && actor.runId !== runId) || (actor.userId.startsWith('runner:') && actor.userId !== `runner:${environmentId}`)) throw new DomainError('FORBIDDEN', 'Deployment identity is not assigned to this environment/run', 403); + if ( + actor.kind !== 'deployer' || + (actor.runId && actor.runId !== runId) || + (actor.userId.startsWith('runner:') && actor.userId !== `runner:${environmentId}`) + ) + throw new DomainError('FORBIDDEN', 'Deployment identity is not assigned to this environment/run', 403); } export class Releases { - constructor(private store: Store, private env = process.env) {} - private async projectIds(tx: Transaction, actor: Actor) { return new Set((await tx.list('project')).filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId)).map(p => p.id)); } - async environments(actor:Actor){return this.store.transaction(actor.tenantId,async tx=>{const ids=await this.projectIds(tx,actor);return Promise.all((await tx.list('environment')).filter(e=>ids.has(e.projectId)).map(async environment=>{if(environment.visibility!=='private'||!environment.applicationUrl)return environment;try{await privatePolicyForEnvironment(environment,this.env);return{...environment,accessStatus:'configured',accessReason:undefined};}catch{return{...environment,accessStatus:'unconfigured',accessReason:'Private identity gateway routing is missing or no longer matches this environment.'};}}));});} - async createEnvironment(actor: Actor, input: unknown) { - requireHuman(actor); requireRole(actor, ['owner', 'admin']); const data = environmentInput.parse(input);requireDeploymentSecretPolicy(data); - return this.store.transaction(actor.tenantId, async tx => { await projectAccess(tx, actor, data.projectId); if (data.runnerId) throw new DomainError('RUNNER_REGISTRATION_REQUIRED', 'Register a runner after creating the environment', 400); const env = { ...record(actor.tenantId), ...data, ...accessConfiguration(data,this.env,await gatewayIdentityOrigins(this.env)) } as Environment; await tx.put('environment', env); return env; }); + constructor( + private store: Store, + private env = process.env, + ) {} + private async projectIds(tx: Transaction, actor: Actor) { + return new Set( + (await tx.list('project')) + .filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId)) + .map(p => p.id), + ); + } + async environments(actor: Actor) { + return this.store.transaction(actor.tenantId, async tx => { + const ids = await this.projectIds(tx, actor); + return Promise.all( + (await tx.list('environment')) + .filter(e => ids.has(e.projectId)) + .map(async environment => { + if (environment.visibility !== 'private' || !environment.applicationUrl) return environment; + try { + await privatePolicyForEnvironment(environment, this.env); + return { ...environment, accessStatus: 'configured', accessReason: undefined }; + } catch { + return { + ...environment, + accessStatus: 'unconfigured', + accessReason: 'Private identity gateway routing is missing or no longer matches this environment.', + }; + } + }), + ); + }); + } + async createEnvironment(actor: Actor, input: unknown) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin']); + const data = environmentInput.parse(input); + requireDeploymentSecretPolicy(data); + return this.store.transaction(actor.tenantId, async tx => { + await projectAccess(tx, actor, data.projectId); + if (data.runnerId) + throw new DomainError('RUNNER_REGISTRATION_REQUIRED', 'Register a runner after creating the environment', 400); + const env = { + ...record(actor.tenantId), + ...data, + ...accessConfiguration(data, this.env, await gatewayIdentityOrigins(this.env)), + } as Environment; + await tx.put('environment', env); + return env; + }); + } + async updateAccess( + actor: Actor, + id: string, + input: { applicationUrl: string | null; visibility: 'private' | 'public' }, + ) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin']); + return this.store.transaction(actor.tenantId, async tx => { + const environment = await must(tx, 'environment', id); + await projectAccess(tx, actor, environment.projectId); + if (environment.activeReleaseId) + throw new DomainError( + 'ENVIRONMENT_CONFLICT', + 'Reconcile the active deployment before changing application access', + ); + const config = accessConfiguration( + { ...environment, applicationUrl: input.applicationUrl ?? undefined, visibility: input.visibility }, + this.env, + await gatewayIdentityOrigins(this.env), + ); + Object.assign(environment, config); + await tx.put('environment', touch(environment)); + return environment; + }); + } + async updateDeploymentSecrets( + actor: Actor, + id: string, + input: { secretVersionRefs: string[]; deploymentSecretPolicyDigest: string | null }, + ) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin']); + const policy = { + secretVersionRefs: input.secretVersionRefs, + deploymentSecretPolicyDigest: input.deploymentSecretPolicyDigest ?? undefined, + }; + requireDeploymentSecretPolicy(policy); + return this.store.transaction(actor.tenantId, async tx => { + const environment = await must(tx, 'environment', id); + await projectAccess(tx, actor, environment.projectId); + if (environment.activeReleaseId) + throw new DomainError( + 'ENVIRONMENT_CONFLICT', + 'Reconcile the active deployment before changing application secret references', + ); + Object.assign(environment, policy); + await tx.put('environment', touch(environment)); + return environment; + }); + } + private async visibleRelease(release: Release, environment: Environment | undefined) { + const { deliveryUrl: _, deliveryLaunchPath: __, ...hidden } = release; + const base = hidden as Release; + if ( + release.status !== 'healthy' || + environment?.currentReleaseId !== release.id || + release.applicationUrl !== environment.applicationUrl || + release.visibility !== environment.visibility + ) + return base; + if (release.visibility === 'public' && release.deliveryUrl === release.applicationUrl) return release; + if (release.visibility === 'private' && release.deliveryLaunchPath) { + try { + if ((await privatePolicyForEnvironment(environment, this.env)) === release.privateAccessPolicyDigest) + return { ...base, deliveryLaunchPath: `/delivery/${release.environmentId}/launch` }; + } catch {} + } + return base; + } + async list(actor: Actor) { + return this.store.transaction(actor.tenantId, async tx => { + const ids = await this.projectIds(tx, actor), + environments = new Map((await tx.list('environment')).map(e => [e.id, e])); + return Promise.all( + (await tx.list('release')) + .filter(r => ids.has(r.projectId)) + .map(r => this.visibleRelease(r, environments.get(r.environmentId))), + ); + }); + } + async delivery(actor: Actor, id: string) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + await projectAccess(tx, actor, run.projectId); + const environments = new Map((await tx.list('environment')).map(e => [e.id, e])); + const releases = await Promise.all( + (await tx.list('release')) + .filter(r => r.runId === id) + .map(r => this.visibleRelease(r, environments.get(r.environmentId))), + ); + const release = releases.find(r => !!r.deliveryUrl || !!r.deliveryLaunchPath) ?? releases[0] ?? null; + return { + run, + release, + releases, + evidence: (await tx.list('evidence')).filter(e => e.runId === id).map(publicEvidence), + checkpoint: run.checkpointId ? await tx.get('checkpoint', run.checkpointId) : null, + accepted: run.status === 'succeeded', + accessStatus: release?.deliveryUrl || release?.deliveryLaunchPath ? 'verified' : 'unconfigured', + ...(release?.deliveryUrl ? { url: release.deliveryUrl, visibility: release.visibility } : {}), + ...(release?.deliveryLaunchPath + ? { visibility: 'private', environmentId: release.environmentId, launchPath: release.deliveryLaunchPath } + : {}), + }; + }); } - async updateAccess(actor:Actor,id:string,input:{applicationUrl:string|null;visibility:'private'|'public'}){requireHuman(actor);requireRole(actor,['owner','admin']);return this.store.transaction(actor.tenantId,async tx=>{const environment=await must(tx,'environment',id);await projectAccess(tx,actor,environment.projectId);if(environment.activeReleaseId)throw new DomainError('ENVIRONMENT_CONFLICT','Reconcile the active deployment before changing application access');const config=accessConfiguration({...environment,applicationUrl:input.applicationUrl??undefined,visibility:input.visibility},this.env,await gatewayIdentityOrigins(this.env));Object.assign(environment,config);await tx.put('environment',touch(environment));return environment;});} - async updateDeploymentSecrets(actor:Actor,id:string,input:{secretVersionRefs:string[];deploymentSecretPolicyDigest:string|null}){requireHuman(actor);requireRole(actor,['owner','admin']);const policy={secretVersionRefs:input.secretVersionRefs,deploymentSecretPolicyDigest:input.deploymentSecretPolicyDigest??undefined};requireDeploymentSecretPolicy(policy);return this.store.transaction(actor.tenantId,async tx=>{const environment=await must(tx,'environment',id);await projectAccess(tx,actor,environment.projectId);if(environment.activeReleaseId)throw new DomainError('ENVIRONMENT_CONFLICT','Reconcile the active deployment before changing application secret references');Object.assign(environment,policy);await tx.put('environment',touch(environment));return environment;});} - private async visibleRelease(release:Release,environment:Environment|undefined){const {deliveryUrl:_,deliveryLaunchPath:__,...hidden}=release;const base=hidden as Release;if(release.status!=='healthy'||environment?.currentReleaseId!==release.id||release.applicationUrl!==environment.applicationUrl||release.visibility!==environment.visibility)return base;if(release.visibility==='public'&&release.deliveryUrl===release.applicationUrl)return release;if(release.visibility==='private'&&release.deliveryLaunchPath){try{if(await privatePolicyForEnvironment(environment,this.env)===release.privateAccessPolicyDigest)return{...base,deliveryLaunchPath:`/delivery/${release.environmentId}/launch`};}catch{}}return base;} - async list(actor:Actor){return this.store.transaction(actor.tenantId,async tx=>{const ids=await this.projectIds(tx,actor),environments=new Map((await tx.list('environment')).map(e=>[e.id,e]));return Promise.all((await tx.list('release')).filter(r=>ids.has(r.projectId)).map(r=>this.visibleRelease(r,environments.get(r.environmentId))));});} - async delivery(actor:Actor,id:string){return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);await projectAccess(tx,actor,run.projectId);const environments=new Map((await tx.list('environment')).map(e=>[e.id,e]));const releases=await Promise.all((await tx.list('release')).filter(r=>r.runId===id).map(r=>this.visibleRelease(r,environments.get(r.environmentId))));const release=releases.find(r=>!!r.deliveryUrl||!!r.deliveryLaunchPath)??releases[0]??null;return{run,release,releases,evidence:(await tx.list('evidence')).filter(e=>e.runId===id).map(publicEvidence),checkpoint:run.checkpointId?await tx.get('checkpoint',run.checkpointId):null,accepted:run.status==='succeeded',accessStatus:release?.deliveryUrl||release?.deliveryLaunchPath?'verified':'unconfigured',...(release?.deliveryUrl?{url:release.deliveryUrl,visibility:release.visibility}:{}),...(release?.deliveryLaunchPath?{visibility:'private',environmentId:release.environmentId,launchPath:release.deliveryLaunchPath}:{})};});} async request(actor: Actor, input: unknown, key?: string) { - requireRole(actor, ['owner', 'admin', 'deployer']); const data = releaseInput.parse(input); + requireRole(actor, ['owner', 'admin', 'deployer']); + const data = releaseInput.parse(input); return this.store.transaction(actor.tenantId, async tx => { - const run = await must(tx, 'run', data.runId); if (run.providerProxyActiveRequest) throw new DomainError('MODEL_USAGE_UNCONFIRMED', 'Outstanding model usage must be reconciled before publication'); if (run.purpose === 'evolution' || run.noDeployment) throw new DomainError('CANDIDATE_ONLY', 'Evolution generation tasks cannot publish an application release'); if (actor.kind === 'deployer' && run.executionGeneration && data.generation !== run.executionGeneration) throw new DomainError('STALE_GENERATION', 'A newer execution generation superseded this delivery'); const project = await projectAccess(tx, actor, run.projectId); + const run = await must(tx, 'run', data.runId); + if (run.providerProxyActiveRequest) + throw new DomainError( + 'MODEL_USAGE_UNCONFIRMED', + 'Outstanding model usage must be reconciled before publication', + ); + if (run.purpose === 'evolution' || run.noDeployment) + throw new DomainError('CANDIDATE_ONLY', 'Evolution generation tasks cannot publish an application release'); + if (actor.kind === 'deployer' && run.executionGeneration && data.generation !== run.executionGeneration) + throw new DomainError('STALE_GENERATION', 'A newer execution generation superseded this delivery'); + const project = await projectAccess(tx, actor, run.projectId); if (!['human', 'connector'].includes(actor.kind)) executionScope(actor, run); return idempotent(tx, key, { operation: 'release', actorId: actor.userId, ...data }, async () => { - if (['canceled', 'failed', 'paused'].includes(run.status)) throw new DomainError('RUN_STOPPED', 'Stopped runs cannot be published'); + if (['canceled', 'failed', 'paused'].includes(run.status)) + throw new DomainError('RUN_STOPPED', 'Stopped runs cannot be published'); const env = await must(tx, 'environment', data.environmentId); - if (env.projectId !== run.projectId) throw new DomainError('ENVIRONMENT_MISMATCH', 'Environment belongs to another project'); - requireDeploymentSecretPolicy(env);accessConfiguration(env,this.env,await gatewayIdentityOrigins(this.env));const privateAccessPolicyDigest=await privatePolicyForEnvironment(env,this.env);assertApplicationConfigured({...env,privateAccessPolicyDigest},this.env); + if (env.projectId !== run.projectId) + throw new DomainError('ENVIRONMENT_MISMATCH', 'Environment belongs to another project'); + requireDeploymentSecretPolicy(env); + accessConfiguration(env, this.env, await gatewayIdentityOrigins(this.env)); + const privateAccessPolicyDigest = await privatePolicyForEnvironment(env, this.env); + assertApplicationConfigured({ ...env, privateAccessPolicyDigest }, this.env); assertVerificationProfile(project, this.env); - const verification = await must(tx, 'evidence', data.verificationEvidenceId), build = await must(tx, 'evidence', data.buildEvidenceId); - for (const e of [verification, build]) if (e.runId !== run.id || e.revision !== run.revision || !e.passed || e.sourceCommit !== run.verifiedCommit) throw new DomainError('STALE_EVIDENCE', 'Evidence must pass for this exact run, revision and commit'); - if (verification.type !== 'verification' || build.type !== 'build' || !build.imageDigest) throw new DomainError('EVIDENCE_REQUIRED', 'Trusted test and immutable build evidence are required'); - if (verification.verificationPolicyDigest !== verificationPolicyDigest(project) || build.verificationPolicyDigest !== verificationPolicyDigest(project)) throw new DomainError('STALE_EVIDENCE', 'Protected verification policy changed after evidence was recorded'); + const verification = await must(tx, 'evidence', data.verificationEvidenceId), + build = await must(tx, 'evidence', data.buildEvidenceId); + for (const e of [verification, build]) + if (e.runId !== run.id || e.revision !== run.revision || !e.passed || e.sourceCommit !== run.verifiedCommit) + throw new DomainError('STALE_EVIDENCE', 'Evidence must pass for this exact run, revision and commit'); + if (verification.type !== 'verification' || build.type !== 'build' || !build.imageDigest) + throw new DomainError('EVIDENCE_REQUIRED', 'Trusted test and immutable build evidence are required'); + if ( + verification.verificationPolicyDigest !== verificationPolicyDigest(project) || + build.verificationPolicyDigest !== verificationPolicyDigest(project) + ) + throw new DomainError('STALE_EVIDENCE', 'Protected verification policy changed after evidence was recorded'); const checkpoint = run.checkpointId ? await tx.get('checkpoint', run.checkpointId) : undefined; - const classification = checkpoint?.trustedClassification as { changedPaths: string[]; migration: boolean; permissionsExpanded: boolean; sourceCommit: string } | undefined; - const riskReasons = releaseRisk({ ...classification, evidenceComplete: !!classification && checkpoint?.revision === run.revision && classification.sourceCommit === run.verifiedCommit, preauthorized: env.autoDeploy }); - const binding = { deploymentSecretPolicyDigest:env.deploymentSecretPolicyDigest,privateAccessPolicyDigest,applicationUrl:env.applicationUrl,visibility:env.visibility??'private',deliveryRequired:this.env.NODE_ENV==='production'||!!env.applicationUrl,developmentLoopback:this.env.NODE_ENV!=='production'&&!env.production,runId: run.id, revision: run.revision, sourceCommit: run.verifiedCommit!, imageDigest: build.imageDigest, architecture: env.architecture, configVersion: env.configVersion, secretVersionRefs: env.secretVersionRefs, environmentId: env.id, evidenceIds: [verification.id, build.id], policyVersion: run.policyVersion, previousReleaseId: env.currentReleaseId, deliveryConfigSnapshot: project.deliveryConfig ?? null, verificationPolicyDigest: verificationPolicyDigest(project) }; - const release = { ...record(actor.tenantId), ...binding, projectId: run.projectId, status: riskReasons.length ? 'awaiting_approval' : 'ready', bindingDigest: digest(binding), riskReasons } as Release; + const classification = checkpoint?.trustedClassification as + | { changedPaths: string[]; migration: boolean; permissionsExpanded: boolean; sourceCommit: string } + | undefined; + const riskReasons = releaseRisk({ + ...classification, + evidenceComplete: + !!classification && + checkpoint?.revision === run.revision && + classification.sourceCommit === run.verifiedCommit, + preauthorized: env.autoDeploy, + }); + const binding = { + deploymentSecretPolicyDigest: env.deploymentSecretPolicyDigest, + privateAccessPolicyDigest, + applicationUrl: env.applicationUrl, + visibility: env.visibility ?? 'private', + deliveryRequired: this.env.NODE_ENV === 'production' || !!env.applicationUrl, + developmentLoopback: this.env.NODE_ENV !== 'production' && !env.production, + runId: run.id, + revision: run.revision, + sourceCommit: run.verifiedCommit!, + imageDigest: build.imageDigest, + architecture: env.architecture, + configVersion: env.configVersion, + secretVersionRefs: env.secretVersionRefs, + environmentId: env.id, + evidenceIds: [verification.id, build.id], + policyVersion: run.policyVersion, + previousReleaseId: env.currentReleaseId, + deliveryConfigSnapshot: project.deliveryConfig ?? null, + verificationPolicyDigest: verificationPolicyDigest(project), + }; + const release = { + ...record(actor.tenantId), + ...binding, + projectId: run.projectId, + status: riskReasons.length ? 'awaiting_approval' : 'ready', + bindingDigest: digest(binding), + riskReasons, + } as Release; if (riskReasons.length) { - const approval = { ...record(actor.tenantId), releaseId: release.id, bindingDigest: release.bindingDigest, status: 'pending', expiresAt: new Date(Date.now() + 86400000).toISOString() } as Approval; - await tx.put('approval', approval); release.approvalId = approval.id; + const approval = { + ...record(actor.tenantId), + releaseId: release.id, + bindingDigest: release.bindingDigest, + status: 'pending', + expiresAt: new Date(Date.now() + 86400000).toISOString(), + } as Approval; + await tx.put('approval', approval); + release.approvalId = approval.id; } - await tx.put('release', release); await tx.event(run.id, 'release.created', riskReasons.length ? 'Release requires approval of the exact artifact and environment.' : 'Release passed the configured automatic policy.', { releaseId: release.id, bindingDigest: release.bindingDigest }); + await tx.put('release', release); + await tx.event( + run.id, + 'release.created', + riskReasons.length + ? 'Release requires approval of the exact artifact and environment.' + : 'Release passed the configured automatic policy.', + { releaseId: release.id, bindingDigest: release.bindingDigest }, + ); if (!riskReasons.length) await tx.enqueue('release.ready', { releaseId: release.id }); return release; }); @@ -61,76 +317,250 @@ export class Releases { } /** Revalidate immutable authorization at every boundary, including after deployment I/O. */ async assertCurrent(tx: Transaction, release: Release, requireApproval = true) { - const run = await must(tx, 'run', release.runId), environment = await must(tx, 'environment', release.environmentId), project = await must(tx, 'project', release.projectId); + const run = await must(tx, 'run', release.runId), + environment = await must(tx, 'environment', release.environmentId), + project = await must(tx, 'project', release.projectId); assertVerificationProfile(project, this.env); - if (environment.applicationUrl!==release.applicationUrl||(environment.visibility??'private')!==(release.visibility??'private'))throw new DomainError('RELEASE_STALE','Application URL or visibility changed; request a new release'); - accessConfiguration(environment,this.env,await gatewayIdentityOrigins(this.env));const privatePolicy=await privatePolicyForEnvironment(environment,this.env);if(privatePolicy!==release.privateAccessPolicyDigest)throw new DomainError('RELEASE_STALE','Private identity gateway policy changed; request a new release');assertApplicationConfigured(release,this.env); - if (digest(releaseBinding(release)) !== release.bindingDigest || run.revision !== release.revision || run.verifiedCommit !== release.sourceCommit || ['canceled', 'failed', 'paused'].includes(run.status) || run.policyVersion !== release.policyVersion || environment.projectId !== release.projectId || environment.deploymentSecretPolicyDigest!==release.deploymentSecretPolicyDigest||environment.configVersion !== release.configVersion || environment.architecture !== release.architecture || digest(environment.secretVersionRefs) !== digest(release.secretVersionRefs) || digest(project.deliveryConfig ?? null) !== digest(release.deliveryConfigSnapshot) || verificationPolicyDigest(project) !== release.verificationPolicyDigest || environment.currentReleaseId !== release.previousReleaseId) throw new DomainError('RELEASE_STALE', 'Requirements, code, policy, configuration, secrets or environment changed; request a new release'); + if ( + environment.applicationUrl !== release.applicationUrl || + (environment.visibility ?? 'private') !== (release.visibility ?? 'private') + ) + throw new DomainError('RELEASE_STALE', 'Application URL or visibility changed; request a new release'); + accessConfiguration(environment, this.env, await gatewayIdentityOrigins(this.env)); + const privatePolicy = await privatePolicyForEnvironment(environment, this.env); + if (privatePolicy !== release.privateAccessPolicyDigest) + throw new DomainError('RELEASE_STALE', 'Private identity gateway policy changed; request a new release'); + assertApplicationConfigured(release, this.env); + if ( + digest(releaseBinding(release)) !== release.bindingDigest || + run.revision !== release.revision || + run.verifiedCommit !== release.sourceCommit || + ['canceled', 'failed', 'paused'].includes(run.status) || + run.policyVersion !== release.policyVersion || + environment.projectId !== release.projectId || + environment.deploymentSecretPolicyDigest !== release.deploymentSecretPolicyDigest || + environment.configVersion !== release.configVersion || + environment.architecture !== release.architecture || + digest(environment.secretVersionRefs) !== digest(release.secretVersionRefs) || + digest(project.deliveryConfig ?? null) !== digest(release.deliveryConfigSnapshot) || + verificationPolicyDigest(project) !== release.verificationPolicyDigest || + environment.currentReleaseId !== release.previousReleaseId + ) + throw new DomainError( + 'RELEASE_STALE', + 'Requirements, code, policy, configuration, secrets or environment changed; request a new release', + ); requireDeploymentSecretPolicy(release); if (requireApproval && release.approvalId) { const approval = await must(tx, 'approval', release.approvalId); - if (approval.status !== 'approved' || approval.releaseId !== release.id || approval.bindingDigest !== release.bindingDigest) throw new DomainError('APPROVAL_STALE', 'Release approval is absent or invalidated'); + if ( + approval.status !== 'approved' || + approval.releaseId !== release.id || + approval.bindingDigest !== release.bindingDigest + ) + throw new DomainError('APPROVAL_STALE', 'Release approval is absent or invalidated'); } return { run, environment, project }; } async decide(actor: Actor, id: string, input: { decision: string; bindingDigest: string }) { - requireRole(actor, ['owner', 'admin', 'deployer']); requireHuman(actor); - if (!['approve', 'reject'].includes(input.decision)) throw new DomainError('INVALID_DECISION', 'Choose approve or reject', 400); + requireRole(actor, ['owner', 'admin', 'deployer']); + requireHuman(actor); + if (!['approve', 'reject'].includes(input.decision)) + throw new DomainError('INVALID_DECISION', 'Choose approve or reject', 400); return this.store.transaction(actor.tenantId, async tx => { - const approval = await must(tx, 'approval', id); const release = await must(tx, 'release', approval.releaseId); await projectAccess(tx, actor, release.projectId); - if (approval.status !== 'pending' || Date.parse(approval.expiresAt) <= Date.now()) throw new DomainError('APPROVAL_EXPIRED', 'Approval is expired or already resolved'); - if (input.bindingDigest !== approval.bindingDigest || release.bindingDigest !== approval.bindingDigest || release.status !== 'awaiting_approval') throw new DomainError('APPROVAL_STALE', 'Release no longer matches this approval'); + const approval = await must(tx, 'approval', id); + const release = await must(tx, 'release', approval.releaseId); + await projectAccess(tx, actor, release.projectId); + if (approval.status !== 'pending' || Date.parse(approval.expiresAt) <= Date.now()) + throw new DomainError('APPROVAL_EXPIRED', 'Approval is expired or already resolved'); + if ( + input.bindingDigest !== approval.bindingDigest || + release.bindingDigest !== approval.bindingDigest || + release.status !== 'awaiting_approval' + ) + throw new DomainError('APPROVAL_STALE', 'Release no longer matches this approval'); await this.assertCurrent(tx, release, false); - approval.status = input.decision === 'approve' ? 'approved' : 'rejected'; approval.decidedBy = actor.userId; + approval.status = input.decision === 'approve' ? 'approved' : 'rejected'; + approval.decidedBy = actor.userId; release.status = input.decision === 'approve' ? 'ready' : 'failed'; - await tx.put('approval', touch(approval)); await tx.put('release', touch(release)); - await tx.event(release.runId, 'release.approval', `Release ${input.decision} decision recorded.`, { releaseId: release.id, actorId: actor.userId }); - if (release.status === 'ready') await tx.enqueue('release.ready', { releaseId: release.id }); return release; + await tx.put('approval', touch(approval)); + await tx.put('release', touch(release)); + await tx.event(release.runId, 'release.approval', `Release ${input.decision} decision recorded.`, { + releaseId: release.id, + actorId: actor.userId, + }); + if (release.status === 'ready') await tx.enqueue('release.ready', { releaseId: release.id }); + return release; }); } async begin(actor: Actor, id: string) { return this.store.transaction(actor.tenantId, async tx => { - const release = await must(tx, 'release', id); deploymentIdentity(actor, release.environmentId, release.runId); + const release = await must(tx, 'release', id); + deploymentIdentity(actor, release.environmentId, release.runId); const { environment: env } = await this.assertCurrent(tx, release); if (release.status === 'deploying' && env.activeReleaseId === id) return release; if (release.status !== 'ready') throw new DomainError('RELEASE_NOT_READY', 'Release must pass policy/approval'); if (env.activeReleaseId) throw new DomainError('ENVIRONMENT_CONFLICT', 'Another release owns the environment'); - env.activeReleaseId = release.id; release.status = 'deploying'; await tx.put('environment', touch(env)); await tx.put('release', touch(release)); return release; + env.activeReleaseId = release.id; + release.status = 'deploying'; + await tx.put('environment', touch(env)); + await tx.put('release', touch(release)); + return release; }); } async accept(actor: Actor, id: string, input: { evidenceId: string; bindingDigest: string }) { return this.store.transaction(actor.tenantId, tx => this.acceptInTransaction(tx, actor, id, input)); } - async acceptInTransaction(tx: Transaction, actor: Actor, id: string, input: { evidenceId: string; bindingDigest: string }) { - const release = await must(tx, 'release', id); deploymentIdentity(actor, release.environmentId, release.runId); - if (release.status === 'healthy' && input.bindingDigest === release.bindingDigest && release.evidenceIds.includes(input.evidenceId)) return release; - const { run, environment: env } = await this.assertCurrent(tx, release), evidence = await must(tx, 'evidence', input.evidenceId); - if (!['deploying', 'unknown'].includes(release.status) || env.activeReleaseId !== release.id || release.bindingDigest !== input.bindingDigest) throw new DomainError('RELEASE_CONFLICT', 'Deployment does not own this release'); - if (evidence.type !== 'deployment' || !evidence.passed || evidence.runId !== run.id || evidence.revision !== release.revision || evidence.sourceCommit !== release.sourceCommit || evidence.imageDigest !== release.imageDigest || evidence.releaseId !== release.id || evidence.environmentId !== env.id || evidence.bindingDigest !== release.bindingDigest || !evidence.checks?.includes('health') || !evidence.checks.includes('business-smoke')) throw new DomainError('ACCEPTANCE_REQUIRED', 'Health and business smoke evidence must bind this exact release and environment'); - if(release.visibility==='private'&&release.applicationUrl){await verifyPrivateEvidence(tx,this.env,release,evidence.privateAccessEvidence,String(evidence.runnerTaskId??evidence.id));release.deliveryLaunchPath=`/delivery/${release.environmentId}/launch`;delete release.deliveryUrl;}else{assertApplicationAccepted(release,evidence.accessEvidence,this.env);if(release.applicationUrl)release.deliveryUrl=release.applicationUrl;} - release.status = 'healthy'; release.deployedAt = now(); release.evidenceIds.push(evidence.id); env.currentReleaseId = id; env.activeReleaseId = undefined; - if (run.providerProxyActiveRequest) throw new DomainError('MODEL_USAGE_UNCONFIRMED', 'Outstanding model usage must be reconciled before acceptance'); run.status = 'succeeded'; run.stage = 'accept'; run.reservedTokens = 0; run.lastActiveAt = undefined; if (run.lease) run.lease.expiresAt = now(); - await tx.put('release', touch(release)); await tx.put('environment', touch(env)); await tx.put('run', touch(run)); await tx.event(run.id, 'delivery.accepted', 'Deployed artifact passed health and business acceptance.', { releaseId: id, evidenceId: evidence.id }); await tx.enqueue('run.signal', { runId: run.id, action: 'completed' }); return release; + async acceptInTransaction( + tx: Transaction, + actor: Actor, + id: string, + input: { evidenceId: string; bindingDigest: string }, + ) { + const release = await must(tx, 'release', id); + deploymentIdentity(actor, release.environmentId, release.runId); + if ( + release.status === 'healthy' && + input.bindingDigest === release.bindingDigest && + release.evidenceIds.includes(input.evidenceId) + ) + return release; + const { run, environment: env } = await this.assertCurrent(tx, release), + evidence = await must(tx, 'evidence', input.evidenceId); + if ( + !['deploying', 'unknown'].includes(release.status) || + env.activeReleaseId !== release.id || + release.bindingDigest !== input.bindingDigest + ) + throw new DomainError('RELEASE_CONFLICT', 'Deployment does not own this release'); + if ( + evidence.type !== 'deployment' || + !evidence.passed || + evidence.runId !== run.id || + evidence.revision !== release.revision || + evidence.sourceCommit !== release.sourceCommit || + evidence.imageDigest !== release.imageDigest || + evidence.releaseId !== release.id || + evidence.environmentId !== env.id || + evidence.bindingDigest !== release.bindingDigest || + !evidence.checks?.includes('health') || + !evidence.checks.includes('business-smoke') + ) + throw new DomainError( + 'ACCEPTANCE_REQUIRED', + 'Health and business smoke evidence must bind this exact release and environment', + ); + if (release.visibility === 'private' && release.applicationUrl) { + await verifyPrivateEvidence( + tx, + this.env, + release, + evidence.privateAccessEvidence, + String(evidence.runnerTaskId ?? evidence.id), + ); + release.deliveryLaunchPath = `/delivery/${release.environmentId}/launch`; + delete release.deliveryUrl; + } else { + assertApplicationAccepted(release, evidence.accessEvidence, this.env); + if (release.applicationUrl) release.deliveryUrl = release.applicationUrl; + } + release.status = 'healthy'; + release.deployedAt = now(); + release.evidenceIds.push(evidence.id); + env.currentReleaseId = id; + env.activeReleaseId = undefined; + if (run.providerProxyActiveRequest) + throw new DomainError('MODEL_USAGE_UNCONFIRMED', 'Outstanding model usage must be reconciled before acceptance'); + run.status = 'succeeded'; + run.stage = 'accept'; + run.reservedTokens = 0; + run.lastActiveAt = undefined; + if (run.lease) run.lease.expiresAt = now(); + await tx.put('release', touch(release)); + await tx.put('environment', touch(env)); + await tx.put('run', touch(run)); + await tx.event(run.id, 'delivery.accepted', 'Deployed artifact passed health and business acceptance.', { + releaseId: id, + evidenceId: evidence.id, + }); + await tx.enqueue('run.signal', { runId: run.id, action: 'completed' }); + return release; } async reportFailure(actor: Actor, id: string, status: 'failed' | 'unknown', message: string) { - return this.store.transaction(actor.tenantId, async tx => { const release = await must(tx, 'release', id); deploymentIdentity(actor, release.environmentId, release.runId); const env = await must(tx, 'environment', release.environmentId); if (!['deploying', 'unknown'].includes(release.status) || env.activeReleaseId !== id) throw new DomainError('RELEASE_CONFLICT', 'Failure receipt does not own the active deployment'); release.status = status; /* Preserve the lock until live-state reconciliation, even for a reported failure. */ await tx.put('release', touch(release)); await tx.event(release.runId, 'release.failure', redact(message), { releaseId: id, status }); return release; }); + return this.store.transaction(actor.tenantId, async tx => { + const release = await must(tx, 'release', id); + deploymentIdentity(actor, release.environmentId, release.runId); + const env = await must(tx, 'environment', release.environmentId); + if (!['deploying', 'unknown'].includes(release.status) || env.activeReleaseId !== id) + throw new DomainError('RELEASE_CONFLICT', 'Failure receipt does not own the active deployment'); + release.status = status; + /* Preserve the lock until live-state reconciliation, even for a reported failure. */ await tx.put( + 'release', + touch(release), + ); + await tx.event(release.runId, 'release.failure', redact(message), { releaseId: id, status }); + return release; + }); } async rollback(actor: Actor, id: string, key?: string) { - requireRole(actor, ['owner', 'admin', 'deployer']); requireHuman(actor); + requireRole(actor, ['owner', 'admin', 'deployer']); + requireHuman(actor); return this.store.transaction(actor.tenantId, async tx => { - const release = await must(tx, 'release', id); await projectAccess(tx, actor, release.projectId); + const release = await must(tx, 'release', id); + await projectAccess(tx, actor, release.projectId); return idempotent(tx, key, { operation: 'rollback', actorId: actor.userId, id }, async () => { const env = await must(tx, 'environment', release.environmentId); - if (env.activeReleaseId) throw new DomainError('ENVIRONMENT_CONFLICT', 'Reconcile the active deployment before rollback'); - if (env.currentReleaseId !== id || release.status !== 'healthy' || !release.previousReleaseId) throw new DomainError('ROLLBACK_UNAVAILABLE', 'No compatible previous live release is known'); + if (env.activeReleaseId) + throw new DomainError('ENVIRONMENT_CONFLICT', 'Reconcile the active deployment before rollback'); + if (env.currentReleaseId !== id || release.status !== 'healthy' || !release.previousReleaseId) + throw new DomainError('ROLLBACK_UNAVAILABLE', 'No compatible previous live release is known'); const previous = await must(tx, 'release', release.previousReleaseId); - if (previous.environmentId !== env.id || previous.projectId !== release.projectId || !['healthy', 'rolled_back'].includes(previous.status) || previous.privateAccessPolicyDigest!==await privatePolicyForEnvironment(env,this.env)||previous.applicationUrl!==env.applicationUrl||(previous.visibility??'private')!==(env.visibility??'private')||previous.deploymentSecretPolicyDigest!==env.deploymentSecretPolicyDigest||previous.configVersion !== env.configVersion || previous.architecture !== env.architecture || digest(previous.secretVersionRefs) !== digest(env.secretVersionRefs) || digest(previous.deliveryConfigSnapshot) !== digest(release.deliveryConfigSnapshot)) throw new DomainError('ROLLBACK_REQUIRES_OPERATOR', 'Previous artifact, configuration and secrets require compatibility review'); - const existing = (await tx.list('runner_task')).find(t => t.environmentId === env.id && t.action === 'rollback' && ['pending', 'dispatching', 'unknown'].includes(String(t.status))); + if ( + previous.environmentId !== env.id || + previous.projectId !== release.projectId || + !['healthy', 'rolled_back'].includes(previous.status) || + previous.privateAccessPolicyDigest !== (await privatePolicyForEnvironment(env, this.env)) || + previous.applicationUrl !== env.applicationUrl || + (previous.visibility ?? 'private') !== (env.visibility ?? 'private') || + previous.deploymentSecretPolicyDigest !== env.deploymentSecretPolicyDigest || + previous.configVersion !== env.configVersion || + previous.architecture !== env.architecture || + digest(previous.secretVersionRefs) !== digest(env.secretVersionRefs) || + digest(previous.deliveryConfigSnapshot) !== digest(release.deliveryConfigSnapshot) + ) + throw new DomainError( + 'ROLLBACK_REQUIRES_OPERATOR', + 'Previous artifact, configuration and secrets require compatibility review', + ); + const existing = (await tx.list('runner_task')).find( + t => + t.environmentId === env.id && + t.action === 'rollback' && + ['pending', 'dispatching', 'unknown'].includes(String(t.status)), + ); if (existing) throw new DomainError('ENVIRONMENT_CONFLICT', 'A rollback is already pending'); - const operation = { ...record(actor.tenantId), action: 'rollback', releaseId: id, targetReleaseId: previous.id, environmentId: env.id, expectedReleaseId: id, targetBindingDigest: previous.bindingDigest, status: 'pending' }; + const operation = { + ...record(actor.tenantId), + action: 'rollback', + releaseId: id, + targetReleaseId: previous.id, + environmentId: env.id, + expectedReleaseId: id, + targetBindingDigest: previous.bindingDigest, + status: 'pending', + }; // Acquiring the environment lock here prevents a ready forward release racing the rollback. - env.activeReleaseId = previous.id; await tx.put('environment', touch(env)); - await tx.put('runner_task', operation); await tx.enqueue('release.rollback', { operationId: operation.id }); await tx.event(release.runId, 'rollback.requested', 'Rollback queued for the authenticated environment runner.', { operationId: operation.id, targetReleaseId: previous.id }); return operation; + env.activeReleaseId = previous.id; + await tx.put('environment', touch(env)); + await tx.put('runner_task', operation); + await tx.enqueue('release.rollback', { operationId: operation.id }); + await tx.event( + release.runId, + 'rollback.requested', + 'Rollback queued for the authenticated environment runner.', + { operationId: operation.id, targetReleaseId: previous.id }, + ); + return operation; }); }); } diff --git a/apps/api/src/domain/resource-dispatch.ts b/apps/api/src/domain/resource-dispatch.ts index e6bd97f..52686ee 100644 --- a/apps/api/src/domain/resource-dispatch.ts +++ b/apps/api/src/domain/resource-dispatch.ts @@ -5,25 +5,82 @@ import { heldAllocation, recordAllocation } from './entitlements.js'; import { resolveProvisionerTarget } from './provisioner-target.js'; /** Normal stopped generations release compute only after a scoped provisioner receipt. */ -export async function releaseStoppedAllocations(control: ControlPlane, tenantId: string, fetcher: typeof fetch = fetch) { - const provisioner = await resolveProvisionerTarget(control.env, tenantId); if (!provisioner) return; - const candidates = await control.store.transaction(tenantId, async tx => (await tx.list('run')).filter(run => heldAllocation(run) && ['succeeded','failed','canceled','paused','waiting'].includes(run.status) && (!run.lease || Date.parse(run.lease.expiresAt) <= Date.now()))); +export async function releaseStoppedAllocations( + control: ControlPlane, + tenantId: string, + fetcher: typeof fetch = fetch, +) { + const provisioner = await resolveProvisionerTarget(control.env, tenantId); + if (!provisioner) return; + const candidates = await control.store.transaction(tenantId, async tx => + (await tx.list('run')).filter( + run => + heldAllocation(run) && + ['succeeded', 'failed', 'canceled', 'paused', 'waiting'].includes(run.status) && + (!run.lease || Date.parse(run.lease.expiresAt) <= Date.now()), + ), + ); for (const observed of candidates) { - const allocation = observed.resourceAllocation as { generation: string; grant: ExecutionResourceGrant; status: string; cleanupConfirmedAt?: string }; + const allocation = observed.resourceAllocation as { + generation: string; + grant: ExecutionResourceGrant; + status: string; + cleanupConfirmedAt?: string; + }; if (allocation.status === 'unknown' && allocation.cleanupConfirmedAt) continue; try { const current = await control.store.transaction(tenantId, tx => must(tx, 'run', observed.id)); - if ((current.resourceAllocation as { generation?: string } | undefined)?.generation !== allocation.generation || !['succeeded','failed','canceled','paused','waiting'].includes(current.status) || (current.lease && Date.parse(current.lease.expiresAt) > Date.now())) continue; - const response = await fetcher(`${provisioner.url}/runs/release`, { method: 'POST', headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ tenantId, runId: observed.id, generation: allocation.generation, resourceGrant: allocation.grant }), signal: AbortSignal.timeout(30000) }); - if (!response.ok) continue; - const receipt = await response.json() as { tenantId: string; runId: string; generation: string; status: string; computeReleased: boolean; persistentStorageRetained: boolean }; - if (receipt.tenantId !== tenantId || receipt.runId !== observed.id || receipt.generation !== allocation.generation || receipt.status !== 'released' || receipt.computeReleased !== true || receipt.persistentStorageRetained !== true) continue; - await control.store.transaction(tenantId, async tx => { - const latest = await must(tx, 'run', observed.id); const value = latest.resourceAllocation as { generation?: string; status?: string } | undefined; - if (value?.generation !== allocation.generation || ['running','queued'].includes(latest.status)) return; - if (allocation.status === 'unknown' || value.status === 'unknown') { latest.resourceAllocation = { ...value, cleanupConfirmedAt: new Date().toISOString(), status: 'unknown' }; await tx.put('run', touch(latest)); await tx.event(latest.id, 'resources.reconciliation_required', 'Compute cleanup was observed after an unknown provisioning outcome; operator reconciliation is required before releasing the reserved slot.'); } - else await recordAllocation(tx, latest.id, allocation.generation, 'released'); + if ( + (current.resourceAllocation as { generation?: string } | undefined)?.generation !== allocation.generation || + !['succeeded', 'failed', 'canceled', 'paused', 'waiting'].includes(current.status) || + (current.lease && Date.parse(current.lease.expiresAt) > Date.now()) + ) + continue; + const response = await fetcher(`${provisioner.url}/runs/release`, { + method: 'POST', + headers: { Authorization: `Bearer ${provisioner.token}`, 'Content-Type': 'application/json' }, + body: JSON.stringify({ + tenantId, + runId: observed.id, + generation: allocation.generation, + resourceGrant: allocation.grant, + }), + signal: AbortSignal.timeout(30000), }); - } catch { /* Keep the durable slot; a missing receipt is never proof resources stopped. */ } + if (!response.ok) continue; + const receipt = (await response.json()) as { + tenantId: string; + runId: string; + generation: string; + status: string; + computeReleased: boolean; + persistentStorageRetained: boolean; + }; + if ( + receipt.tenantId !== tenantId || + receipt.runId !== observed.id || + receipt.generation !== allocation.generation || + receipt.status !== 'released' || + receipt.computeReleased !== true || + receipt.persistentStorageRetained !== true + ) + continue; + await control.store.transaction(tenantId, async tx => { + const latest = await must(tx, 'run', observed.id); + const value = latest.resourceAllocation as { generation?: string; status?: string } | undefined; + if (value?.generation !== allocation.generation || ['running', 'queued'].includes(latest.status)) return; + if (allocation.status === 'unknown' || value.status === 'unknown') { + latest.resourceAllocation = { ...value, cleanupConfirmedAt: new Date().toISOString(), status: 'unknown' }; + await tx.put('run', touch(latest)); + await tx.event( + latest.id, + 'resources.reconciliation_required', + 'Compute cleanup was observed after an unknown provisioning outcome; operator reconciliation is required before releasing the reserved slot.', + ); + } else await recordAllocation(tx, latest.id, allocation.generation, 'released'); + }); + } catch { + /* Keep the durable slot; a missing receipt is never proof resources stopped. */ + } } } diff --git a/apps/api/src/domain/runner-broker.ts b/apps/api/src/domain/runner-broker.ts index 4daaa06..9132400 100644 --- a/apps/api/src/domain/runner-broker.ts +++ b/apps/api/src/domain/runner-broker.ts @@ -1,142 +1,455 @@ -import {resolvePreviewTarget} from './previews.js'; -import {verifyPrivateEvidence,privatePolicyForEnvironment} from './private-access-verifier.js'; -import {matchesApplicationAccess} from '@wishforge/contracts'; +import { resolvePreviewTarget } from './previews.js'; +import { verifyPrivateEvidence, privatePolicyForEnvironment } from './private-access-verifier.js'; +import { matchesApplicationAccess } from '@wishforge/contracts'; import { createHash, createPrivateKey, createPublicKey, randomBytes, randomUUID, sign } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { z } from 'zod'; -import { DomainError, type Actor, type Entity, type Environment, type Release, type Run, type Evidence } from '@wishforge/contracts'; +import { + DomainError, + type Actor, + type Entity, + type Environment, + type Release, + type Run, + type Evidence, +} from '@wishforge/contracts'; import type { Store, Transaction } from '../db/store.js'; import { ControlPlane, must, projectAccess, record, touch, now } from './control-plane.js'; import { canonical, digest, requireHuman, requireRole } from './policy.js'; import { Releases, releaseBinding } from './releases.js'; -interface Envelope { keyId: string; signature: string; payload: { id: string; action: string; target: string; expiresAt: string; release: { id: string; sourceCommit: string; image: string; [key: string]: unknown }; [key: string]: unknown } } -const receipt = z.object({ result: z.object({ releaseId: z.string().min(1), status: z.enum(['dry-run', 'applied', 'verified', 'failed', 'unconfigured', 'not-found', 'conflict']), target: z.enum(['compose', 'kubernetes']), evidence: z.record(z.string(), z.unknown()), rendered: z.string().optional() }).strict() }).strict(); +interface Envelope { + keyId: string; + signature: string; + payload: { + id: string; + action: string; + target: string; + expiresAt: string; + release: { id: string; sourceCommit: string; image: string; [key: string]: unknown }; + [key: string]: unknown; + }; +} +const receipt = z + .object({ + result: z + .object({ + releaseId: z.string().min(1), + status: z.enum(['dry-run', 'applied', 'verified', 'failed', 'unconfigured', 'not-found', 'conflict']), + target: z.enum(['compose', 'kubernetes']), + evidence: z.record(z.string(), z.unknown()), + rendered: z.string().optional(), + }) + .strict(), + }) + .strict(); export class RunnerBroker { - constructor(private store: Store, private control: ControlPlane, private releases: Releases, private env = process.env) {} + constructor( + private store: Store, + private control: ControlPlane, + private releases: Releases, + private env = process.env, + ) {} async register(actor: Actor, environmentId: string) { - requireRole(actor, ['owner', 'admin']); requireHuman(actor); - const token = randomBytes(32).toString('base64url'); const tokenHash = createHash('sha256').update(token).digest('hex'); + requireRole(actor, ['owner', 'admin']); + requireHuman(actor); + const token = randomBytes(32).toString('base64url'); + const tokenHash = createHash('sha256').update(token).digest('hex'); const publicKeys = await this.publicKeys(); const runner = await this.store.transaction(actor.tenantId, async tx => { - const environment = await must(tx, 'environment', environmentId); await projectAccess(tx, actor, environment.projectId); - for (const old of await tx.list('runner')) if (old.environmentId === environmentId) await tx.put('runner', { ...touch(old), status: 'revoked' }); - const registration = { ...record(actor.tenantId), environmentId, tokenHash, status: 'active' }; await tx.put('runner', registration); environment.runnerId = registration.id; await tx.put('environment', touch(environment)); return registration; + const environment = await must(tx, 'environment', environmentId); + await projectAccess(tx, actor, environment.projectId); + for (const old of await tx.list('runner')) + if (old.environmentId === environmentId) await tx.put('runner', { ...touch(old), status: 'revoked' }); + const registration = { ...record(actor.tenantId), environmentId, tokenHash, status: 'active' }; + await tx.put('runner', registration); + environment.runnerId = registration.id; + await tx.put('environment', touch(environment)); + return registration; }); - return { id: runner.id, environmentId, tenantId: actor.tenantId, token, publicKeys, note: 'Save this token once. Runner also requires an operator-issued client certificate; no client certificate is minted here.' }; + return { + id: runner.id, + environmentId, + tenantId: actor.tenantId, + token, + publicKeys, + note: 'Save this token once. Runner also requires an operator-issued client certificate; no client certificate is minted here.', + }; + } + async publicKeys() { + const key = await this.signingKey(); + return { + [this.env.WF_RUNNER_SIGNING_KEY_ID ?? 'v1']: createPublicKey(key) + .export({ type: 'spki', format: 'pem' }) + .toString(), + }; } - async publicKeys() { const key = await this.signingKey(); return { [this.env.WF_RUNNER_SIGNING_KEY_ID ?? 'v1']: createPublicKey(key).export({ type: 'spki', format: 'pem' }).toString() }; } private async signingKey() { - if (!this.env.WF_RUNNER_SIGNING_KEY_FILE) throw new DomainError('SIGNER_UNCONFIGURED', 'Configure the independent Runner signing key before enabling deployment', 503); + if (!this.env.WF_RUNNER_SIGNING_KEY_FILE) + throw new DomainError( + 'SIGNER_UNCONFIGURED', + 'Configure the independent Runner signing key before enabling deployment', + 503, + ); const key = createPrivateKey(await readFile(this.env.WF_RUNNER_SIGNING_KEY_FILE)); - if (key.asymmetricKeyType !== 'ed25519') throw new DomainError('INVALID_SIGNER', 'Runner task key must be Ed25519', 503); + if (key.asymmetricKeyType !== 'ed25519') + throw new DomainError('INVALID_SIGNER', 'Runner task key must be Ed25519', 503); return key; } private async registration(tx: Transaction, actor: Actor, environment: Environment) { - if (actor.kind !== 'deployer' || actor.userId !== `runner:${environment.id}`) throw new DomainError('FORBIDDEN', 'Runner is not registered for this environment', 403); + if (actor.kind !== 'deployer' || actor.userId !== `runner:${environment.id}`) + throw new DomainError('FORBIDDEN', 'Runner is not registered for this environment', 403); const registration = environment.runnerId ? await tx.get('runner', environment.runnerId) : undefined; - if (!registration || registration.status !== 'active' || (actor.clientId && actor.clientId !== registration.id)) throw new DomainError('RUNNER_REVOKED', 'Runner registration is inactive or replaced', 403); + if (!registration || registration.status !== 'active' || (actor.clientId && actor.clientId !== registration.id)) + throw new DomainError('RUNNER_REVOKED', 'Runner registration is inactive or replaced', 403); return registration; } async poll(actor: Actor, environmentId: string) { const key = await this.signingKey(); return this.store.transaction(actor.tenantId, async tx => { - const environment = await must(tx, 'environment', environmentId), registration = await this.registration(tx, actor, environment); + const environment = await must(tx, 'environment', environmentId), + registration = await this.registration(tx, actor, environment); const tasks = await tx.list('runner_task'); const pending = tasks.find(t => t.environmentId === environmentId && t.status === 'pending' && t.envelope); if (pending) { const envelope = pending.envelope as Envelope; if (pending.runnerId === registration.id && Date.parse(envelope.payload.expiresAt) > Date.now()) { - if (environment.activeReleaseId !== pending.releaseId) throw new DomainError('STALE_RUNNER_TASK', 'Task no longer owns the environment'); + if (environment.activeReleaseId !== pending.releaseId) + throw new DomainError('STALE_RUNNER_TASK', 'Task no longer owns the environment'); const target = await must(tx, 'release', String(pending.releaseId)); if (!pending.rollbackOperationId) await this.releases.assertCurrent(tx, target); else { const operation = await must(tx, 'runner_task', String(pending.rollbackOperationId)); - if (operation.lastTaskId !== pending.id || operation.expectedReleaseId !== environment.currentReleaseId || target.applicationUrl!==environment.applicationUrl||(target.visibility??'private')!==(environment.visibility??'private')||target.deploymentSecretPolicyDigest!==environment.deploymentSecretPolicyDigest||target.configVersion !== environment.configVersion || target.architecture !== environment.architecture || digest(target.secretVersionRefs) !== digest(environment.secretVersionRefs)) throw new DomainError('ROLLBACK_STALE', 'Rollback state changed'); + if ( + operation.lastTaskId !== pending.id || + operation.expectedReleaseId !== environment.currentReleaseId || + target.applicationUrl !== environment.applicationUrl || + (target.visibility ?? 'private') !== (environment.visibility ?? 'private') || + target.deploymentSecretPolicyDigest !== environment.deploymentSecretPolicyDigest || + target.configVersion !== environment.configVersion || + target.architecture !== environment.architecture || + digest(target.secretVersionRefs) !== digest(environment.secretVersionRefs) + ) + throw new DomainError('ROLLBACK_STALE', 'Rollback state changed'); } return envelope; } // An old lease may already have changed the target: the next task may only reconcile it. - pending.status = 'expired'; await tx.put('runner_task', touch(pending)); + pending.status = 'expired'; + await tx.put('runner_task', touch(pending)); } - const rollback = tasks.find(t => t.environmentId === environmentId && t.action === 'rollback' && ['pending', 'dispatching', 'unknown'].includes(String(t.status))); + const rollback = tasks.find( + t => + t.environmentId === environmentId && + t.action === 'rollback' && + ['pending', 'dispatching', 'unknown'].includes(String(t.status)), + ); let release: Release | undefined; let action: 'deploy' | 'reconcile'; let previous: string | undefined; if (rollback) { release = await must(tx, 'release', String(rollback.targetReleaseId)); - if (environment.currentReleaseId !== rollback.expectedReleaseId || environment.activeReleaseId !== release.id || rollback.targetBindingDigest !== release.bindingDigest || digest(releaseBinding(release)) !== release.bindingDigest || release.environmentId !== environment.id || release.applicationUrl!==environment.applicationUrl||(release.visibility??'private')!==(environment.visibility??'private')||release.deploymentSecretPolicyDigest!==environment.deploymentSecretPolicyDigest||release.configVersion !== environment.configVersion || release.architecture !== environment.architecture || digest(release.secretVersionRefs) !== digest(environment.secretVersionRefs)) throw new DomainError('ROLLBACK_STALE', 'Rollback no longer matches the environment and approved previous artifact'); - action = rollback.status === 'pending' ? 'deploy' : 'reconcile'; previous = String(rollback.expectedReleaseId); + if ( + environment.currentReleaseId !== rollback.expectedReleaseId || + environment.activeReleaseId !== release.id || + rollback.targetBindingDigest !== release.bindingDigest || + digest(releaseBinding(release)) !== release.bindingDigest || + release.environmentId !== environment.id || + release.applicationUrl !== environment.applicationUrl || + (release.visibility ?? 'private') !== (environment.visibility ?? 'private') || + release.deploymentSecretPolicyDigest !== environment.deploymentSecretPolicyDigest || + release.configVersion !== environment.configVersion || + release.architecture !== environment.architecture || + digest(release.secretVersionRefs) !== digest(environment.secretVersionRefs) + ) + throw new DomainError( + 'ROLLBACK_STALE', + 'Rollback no longer matches the environment and approved previous artifact', + ); + action = rollback.status === 'pending' ? 'deploy' : 'reconcile'; + previous = String(rollback.expectedReleaseId); } else { const releases = await tx.list('release'); - release = environment.activeReleaseId ? releases.find(r => r.id === environment.activeReleaseId && ['deploying', 'unknown', 'failed'].includes(r.status)) : releases.find(r => r.environmentId === environmentId && r.status === 'ready'); + release = environment.activeReleaseId + ? releases.find( + r => r.id === environment.activeReleaseId && ['deploying', 'unknown', 'failed'].includes(r.status), + ) + : releases.find(r => r.environmentId === environmentId && r.status === 'ready'); if (!release) return null; await this.releases.assertCurrent(tx, release); - action = release.status === 'ready' ? 'deploy' : 'reconcile'; previous = release.previousReleaseId; + action = release.status === 'ready' ? 'deploy' : 'reconcile'; + previous = release.previousReleaseId; } - const config = release.deliveryConfigSnapshot as { containerPort?: number; healthPath?: string; smokeChecks?: unknown[]; config?: Record } | null; - if (!config?.containerPort || !config.healthPath || !config.smokeChecks?.length) throw new DomainError('ACCEPTANCE_UNCONFIGURED', 'Protected port, health path and business smoke checks are required before deployment', 503); + const config = release.deliveryConfigSnapshot as { + containerPort?: number; + healthPath?: string; + smokeChecks?: unknown[]; + config?: Record; + } | null; + if (!config?.containerPort || !config.healthPath || !config.smokeChecks?.length) + throw new DomainError( + 'ACCEPTANCE_UNCONFIGURED', + 'Protected port, health path and business smoke checks are required before deployment', + 503, + ); let privateAccess; - if(release.visibility==='private'&&release.applicationUrl){const target=await resolvePreviewTarget(this.env,actor.tenantId,environmentId);if(!target||release.privateAccessPolicyDigest!==target.policyDigest)throw new DomainError('PRIVATE_ACCESS_POLICY_STALE','Private preview policy changed');privateAccess={publicOrigin:target.publicOrigin,proofPublicKeyPem:target.proofPublicKeyPem,policyDigest:target.policyDigest};} - const time = Date.now(), id = randomUUID(); - const payload = JSON.parse(JSON.stringify({ schemaVersion: 1, id, tenantId: actor.tenantId, environmentId, nonce: randomUUID(), issuedAt: new Date(time).toISOString(), expiresAt: new Date(time + 300000).toISOString(), action, target: environment.type === 'compose' ? 'compose' : 'kubernetes', release: { schemaVersion: 1, id: release.id, tenantId: actor.tenantId, environmentId, sourceCommit: release.sourceCommit, image: release.imageDigest, architecture: release.architecture, configVersion: release.configVersion,deploymentSecretPolicyDigest:release.deploymentSecretPolicyDigest, secretVersionRefs: release.secretVersionRefs, bindingDigest: release.bindingDigest, applicationUrl:release.applicationUrl,visibility:release.visibility,privateAccessPolicyDigest:release.privateAccessPolicyDigest,privateAccess,deliveryRequired:release.deliveryRequired,developmentLoopback:release.developmentLoopback, config: config.config, containerPort: config.containerPort, healthPath: config.healthPath, smokeChecks: config.smokeChecks, expectedPreviousReleaseId: previous ?? null } })); + if (release.visibility === 'private' && release.applicationUrl) { + const target = await resolvePreviewTarget(this.env, actor.tenantId, environmentId); + if (!target || release.privateAccessPolicyDigest !== target.policyDigest) + throw new DomainError('PRIVATE_ACCESS_POLICY_STALE', 'Private preview policy changed'); + privateAccess = { + publicOrigin: target.publicOrigin, + proofPublicKeyPem: target.proofPublicKeyPem, + policyDigest: target.policyDigest, + }; + } + const time = Date.now(), + id = randomUUID(); + const payload = JSON.parse( + JSON.stringify({ + schemaVersion: 1, + id, + tenantId: actor.tenantId, + environmentId, + nonce: randomUUID(), + issuedAt: new Date(time).toISOString(), + expiresAt: new Date(time + 300000).toISOString(), + action, + target: environment.type === 'compose' ? 'compose' : 'kubernetes', + release: { + schemaVersion: 1, + id: release.id, + tenantId: actor.tenantId, + environmentId, + sourceCommit: release.sourceCommit, + image: release.imageDigest, + architecture: release.architecture, + configVersion: release.configVersion, + deploymentSecretPolicyDigest: release.deploymentSecretPolicyDigest, + secretVersionRefs: release.secretVersionRefs, + bindingDigest: release.bindingDigest, + applicationUrl: release.applicationUrl, + visibility: release.visibility, + privateAccessPolicyDigest: release.privateAccessPolicyDigest, + privateAccess, + deliveryRequired: release.deliveryRequired, + developmentLoopback: release.developmentLoopback, + config: config.config, + containerPort: config.containerPort, + healthPath: config.healthPath, + smokeChecks: config.smokeChecks, + expectedPreviousReleaseId: previous ?? null, + }, + }), + ); const keyId = this.env.WF_RUNNER_SIGNING_KEY_ID ?? 'v1'; - const envelope = { keyId, payload, signature: sign(null, Buffer.from(canonical({ keyId, payload })), key).toString('base64url') }; - const task = { ...record(actor.tenantId), id, environmentId, releaseId: release.id, runnerId: registration.id, status: 'pending', envelope, ...(rollback ? { rollbackOperationId: rollback.id } : {}) }; + const envelope = { + keyId, + payload, + signature: sign(null, Buffer.from(canonical({ keyId, payload })), key).toString('base64url'), + }; + const task = { + ...record(actor.tenantId), + id, + environmentId, + releaseId: release.id, + runnerId: registration.id, + status: 'pending', + envelope, + ...(rollback ? { rollbackOperationId: rollback.id } : {}), + }; await tx.put('runner_task', task); - if (rollback) { if(release.visibility==='private'&&release.applicationUrl){release.status='deploying';await tx.put('release',touch(release));} rollback.status = 'dispatching'; rollback.lastTaskId = task.id; await tx.put('runner_task', touch(rollback)); } - else { environment.activeReleaseId = release.id; release.status = 'deploying'; await tx.put('environment', touch(environment)); await tx.put('release', touch(release)); } + if (rollback) { + if (release.visibility === 'private' && release.applicationUrl) { + release.status = 'deploying'; + await tx.put('release', touch(release)); + } + rollback.status = 'dispatching'; + rollback.lastTaskId = task.id; + await tx.put('runner_task', touch(rollback)); + } else { + environment.activeReleaseId = release.id; + release.status = 'deploying'; + await tx.put('environment', touch(environment)); + await tx.put('release', touch(release)); + } return envelope; }); } async result(actor: Actor, taskId: string, input: unknown) { const { result } = receipt.parse(input); return this.store.transaction(actor.tenantId, async tx => { - const task = await must(tx, 'runner_task', taskId), environment = await must(tx, 'environment', String(task.environmentId)); + const task = await must(tx, 'runner_task', taskId), + environment = await must(tx, 'environment', String(task.environmentId)); const registration = await this.registration(tx, actor, environment); - if (task.runnerId !== registration.id || !task.envelope) throw new DomainError('STALE_RUNNER_TASK', 'Receipt belongs to a replaced runner or an unsigned operation'); - const envelope = task.envelope as Envelope, payload = envelope.payload; - if (result.releaseId !== payload.release.id || result.target !== payload.target) throw new DomainError('RESULT_MISMATCH', 'Receipt does not match the signed release and deployment target'); + if (task.runnerId !== registration.id || !task.envelope) + throw new DomainError('STALE_RUNNER_TASK', 'Receipt belongs to a replaced runner or an unsigned operation'); + const envelope = task.envelope as Envelope, + payload = envelope.payload; + if (result.releaseId !== payload.release.id || result.target !== payload.target) + throw new DomainError('RESULT_MISMATCH', 'Receipt does not match the signed release and deployment target'); if (task.status === 'completed') { - if (task.resultDigest !== digest(result)) throw new DomainError('RECEIPT_CONFLICT', 'A different receipt has already been committed for this task'); + if (task.resultDigest !== digest(result)) + throw new DomainError('RECEIPT_CONFLICT', 'A different receipt has already been committed for this task'); return { acknowledged: true, delivered: task.delivered === true, rolledBack: task.rolledBack === true }; } - if (task.status !== 'pending' || Date.parse(payload.expiresAt) <= Date.now()) throw new DomainError('STALE_RUNNER_TASK', 'Task lease expired or was replaced; reconcile the target using a new signed task'); + if (task.status !== 'pending' || Date.parse(payload.expiresAt) <= Date.now()) + throw new DomainError( + 'STALE_RUNNER_TASK', + 'Task lease expired or was replaced; reconcile the target using a new signed task', + ); const release = await must(tx, 'release', String(task.releaseId)); - if (environment.activeReleaseId !== release.id || release.sourceCommit !== payload.release.sourceCommit || release.imageDigest !== payload.release.image || release.bindingDigest !== payload.release.bindingDigest) throw new DomainError('RESULT_MISMATCH', 'Receipt no longer owns the deployed artifact or environment'); - const basicVerified=result.status==='verified'&&result.evidence.identityMatched===true&&result.evidence.trafficSwitched===true&&Array.isArray(result.evidence.checks)&&result.evidence.checks.includes('health')&&result.evidence.checks.includes('business-smoke'); - const privateMode=release.visibility==='private'&&!!release.applicationUrl; - let verified=basicVerified&&!privateMode&&matchesApplicationAccess(release,result.evidence.accessEvidence)&&(this.env.NODE_ENV!=='production'||!!release.applicationUrl); - if(privateMode&&basicVerified){await verifyPrivateEvidence(tx,this.env,release,result.evidence.privateAccessEvidence,task.id);verified=true;} + if ( + environment.activeReleaseId !== release.id || + release.sourceCommit !== payload.release.sourceCommit || + release.imageDigest !== payload.release.image || + release.bindingDigest !== payload.release.bindingDigest + ) + throw new DomainError('RESULT_MISMATCH', 'Receipt no longer owns the deployed artifact or environment'); + const basicVerified = + result.status === 'verified' && + result.evidence.identityMatched === true && + result.evidence.trafficSwitched === true && + Array.isArray(result.evidence.checks) && + result.evidence.checks.includes('health') && + result.evidence.checks.includes('business-smoke'); + const privateMode = release.visibility === 'private' && !!release.applicationUrl; + let verified = + basicVerified && + !privateMode && + matchesApplicationAccess(release, result.evidence.accessEvidence) && + (this.env.NODE_ENV !== 'production' || !!release.applicationUrl); + if (privateMode && basicVerified) { + await verifyPrivateEvidence(tx, this.env, release, result.evidence.privateAccessEvidence, task.id); + verified = true; + } let rolledBack = false; if (task.rollbackOperationId) { const operation = await must(tx, 'runner_task', String(task.rollbackOperationId)); - if (operation.status !== 'dispatching' || operation.lastTaskId !== task.id || environment.currentReleaseId !== operation.expectedReleaseId || release.id !== operation.targetReleaseId || release.bindingDigest !== operation.targetBindingDigest || environment.applicationUrl!==release.applicationUrl||(environment.visibility??'private')!==(release.visibility??'private')||environment.deploymentSecretPolicyDigest!==release.deploymentSecretPolicyDigest||environment.configVersion !== release.configVersion || digest(environment.secretVersionRefs) !== digest(release.secretVersionRefs)) throw new DomainError('ROLLBACK_STALE', 'Rollback receipt no longer matches the active operation'); + if ( + operation.status !== 'dispatching' || + operation.lastTaskId !== task.id || + environment.currentReleaseId !== operation.expectedReleaseId || + release.id !== operation.targetReleaseId || + release.bindingDigest !== operation.targetBindingDigest || + environment.applicationUrl !== release.applicationUrl || + (environment.visibility ?? 'private') !== (release.visibility ?? 'private') || + environment.deploymentSecretPolicyDigest !== release.deploymentSecretPolicyDigest || + environment.configVersion !== release.configVersion || + digest(environment.secretVersionRefs) !== digest(release.secretVersionRefs) + ) + throw new DomainError('ROLLBACK_STALE', 'Rollback receipt no longer matches the active operation'); if (verified) { const original = await must(tx, 'release', String(operation.releaseId)); - original.status = 'rolled_back'; release.status = 'healthy';if(release.applicationUrl){if(privateMode){release.deliveryLaunchPath=`/delivery/${release.environmentId}/launch`;delete release.deliveryUrl;}else release.deliveryUrl=release.applicationUrl;} environment.currentReleaseId = release.id; environment.activeReleaseId = undefined; operation.status = 'completed'; operation.completedAt = now(); - const evidence = this.deploymentEvidence(actor, release, environment, task.id,result.evidence); await tx.put('evidence', evidence); operation.evidenceId = evidence.id; - await tx.put('release', touch(original)); await tx.put('release', touch(release)); await tx.put('environment', touch(environment)); - await tx.event(original.runId, 'release.rolled_back', 'Previous immutable artifact restored and passed health and business acceptance.', { operationId: operation.id, targetReleaseId: release.id, evidenceId: evidence.id }); rolledBack = true; - } else { operation.status = 'unknown'; const original = await must(tx, 'release', String(operation.releaseId)); await tx.event(original.runId, 'rollback.unknown', 'Rollback requires live-state reconciliation.', { operationId: operation.id }); } + original.status = 'rolled_back'; + release.status = 'healthy'; + if (release.applicationUrl) { + if (privateMode) { + release.deliveryLaunchPath = `/delivery/${release.environmentId}/launch`; + delete release.deliveryUrl; + } else release.deliveryUrl = release.applicationUrl; + } + environment.currentReleaseId = release.id; + environment.activeReleaseId = undefined; + operation.status = 'completed'; + operation.completedAt = now(); + const evidence = this.deploymentEvidence(actor, release, environment, task.id, result.evidence); + await tx.put('evidence', evidence); + operation.evidenceId = evidence.id; + await tx.put('release', touch(original)); + await tx.put('release', touch(release)); + await tx.put('environment', touch(environment)); + await tx.event( + original.runId, + 'release.rolled_back', + 'Previous immutable artifact restored and passed health and business acceptance.', + { operationId: operation.id, targetReleaseId: release.id, evidenceId: evidence.id }, + ); + rolledBack = true; + } else { + operation.status = 'unknown'; + const original = await must(tx, 'release', String(operation.releaseId)); + await tx.event(original.runId, 'rollback.unknown', 'Rollback requires live-state reconciliation.', { + operationId: operation.id, + }); + } await tx.put('runner_task', touch(operation)); } else if (verified) { await this.releases.assertCurrent(tx, release); - const evidence = this.deploymentEvidence(actor, release, environment, task.id,result.evidence); + const evidence = this.deploymentEvidence(actor, release, environment, task.id, result.evidence); await tx.put('evidence', evidence); - await this.releases.acceptInTransaction(tx, actor, release.id, { evidenceId: evidence.id, bindingDigest: release.bindingDigest }); + await this.releases.acceptInTransaction(tx, actor, release.id, { + evidenceId: evidence.id, + bindingDigest: release.bindingDigest, + }); } else { - if (!['deploying', 'unknown', 'failed'].includes(release.status)) throw new DomainError('RELEASE_CONFLICT', 'Deployment receipt is obsolete'); - release.status = 'unknown'; await tx.put('release', touch(release)); - await tx.event(release.runId, 'release.failure', `Runner reported ${result.status}; live state must be reconciled before another deployment.`, { releaseId: release.id, taskId }); + if (!['deploying', 'unknown', 'failed'].includes(release.status)) + throw new DomainError('RELEASE_CONFLICT', 'Deployment receipt is obsolete'); + release.status = 'unknown'; + await tx.put('release', touch(release)); + await tx.event( + release.runId, + 'release.failure', + `Runner reported ${result.status}; live state must be reconciled before another deployment.`, + { releaseId: release.id, taskId }, + ); } - task.status = 'completed'; task.result = result; task.resultDigest = digest(result); task.delivered = verified && !task.rollbackOperationId; task.rolledBack = rolledBack; + task.status = 'completed'; + task.result = result; + task.resultDigest = digest(result); + task.delivered = verified && !task.rollbackOperationId; + task.rolledBack = rolledBack; await tx.put('runner_task', touch(task)); - return { acknowledged: true, delivered: task.delivered, rolledBack, ...(verified&&release.applicationUrl?(privateMode?{launchPath:`/delivery/${release.environmentId}/launch`,environmentId:release.environmentId,visibility:'private'}:{url:release.applicationUrl}):{}) }; + return { + acknowledged: true, + delivered: task.delivered, + rolledBack, + ...(verified && release.applicationUrl + ? privateMode + ? { + launchPath: `/delivery/${release.environmentId}/launch`, + environmentId: release.environmentId, + visibility: 'private', + } + : { url: release.applicationUrl } + : {}), + }; }); } - private deploymentEvidence(actor: Actor, release: Release, environment: Environment, taskId: string,resultEvidence:Record): Evidence { - return { ...record(actor.tenantId), runId: release.runId, revision: Number(release.revision), sourceCommit: release.sourceCommit, imageDigest: release.imageDigest, type: 'deployment', passed: true, ...(release.applicationUrl?(release.visibility==='private'?{privateAccessEvidence:resultEvidence.privateAccessEvidence,privateAccessSummary:{policyDigest:release.privateAccessPolicyDigest}}:{accessEvidence:resultEvidence.accessEvidence}):{}), checks: ['health', 'business-smoke',...(release.visibility==='private'&&release.applicationUrl?['private-access','anonymous-denied']:[])], issuer: actor.userId, releaseId: release.id, environmentId: environment.id, bindingDigest: release.bindingDigest, runnerTaskId: taskId }; + private deploymentEvidence( + actor: Actor, + release: Release, + environment: Environment, + taskId: string, + resultEvidence: Record, + ): Evidence { + return { + ...record(actor.tenantId), + runId: release.runId, + revision: Number(release.revision), + sourceCommit: release.sourceCommit, + imageDigest: release.imageDigest, + type: 'deployment', + passed: true, + ...(release.applicationUrl + ? release.visibility === 'private' + ? { + privateAccessEvidence: resultEvidence.privateAccessEvidence, + privateAccessSummary: { policyDigest: release.privateAccessPolicyDigest }, + } + : { accessEvidence: resultEvidence.accessEvidence } + : {}), + checks: [ + 'health', + 'business-smoke', + ...(release.visibility === 'private' && release.applicationUrl ? ['private-access', 'anonymous-denied'] : []), + ], + issuer: actor.userId, + releaseId: release.id, + environmentId: environment.id, + bindingDigest: release.bindingDigest, + runnerTaskId: taskId, + }; } } diff --git a/apps/api/src/domain/verification-policy.ts b/apps/api/src/domain/verification-policy.ts index 1949bb7..e4107bb 100644 --- a/apps/api/src/domain/verification-policy.ts +++ b/apps/api/src/domain/verification-policy.ts @@ -2,18 +2,39 @@ import { z } from 'zod'; import { DomainError, type Project, type VerificationProfile } from '@wishforge/contracts'; import { digest } from './policy.js'; -const profilePath = z.string().regex(/^\/usr\/local\/lib\/wishforge-verification\/[a-zA-Z0-9_./-]+$/).refine(p => !p.includes('..') && !p.includes('//')); -export const verificationProfileSchema = z.object({ - id: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,79}$/), - version: z.string().min(1).max(100), - commands: z.array(z.array(z.string().min(1).max(4096)).min(1).max(50)).min(1).max(20), - files: z.array(z.object({ path: profilePath, sha256: z.string().regex(/^[a-f0-9]{64}$/) }).strict()).min(1).max(100) -}).strict().superRefine((p, ctx) => { - if (new Set(p.files.map(f => f.path)).size !== p.files.length) ctx.addIssue({ code: 'custom', message: 'Profile file paths must be unique' }); - for (const argv of p.commands) { - if (!argv[0]?.startsWith('/') || !argv.some(a => p.files.some(f => f.path === a)) || argv.some(a => a.includes('\0'))) ctx.addIssue({ code: 'custom', message: 'Each command must use an absolute executable and a pinned acceptance file' }); - } -}); +const profilePath = z + .string() + .regex(/^\/usr\/local\/lib\/wishforge-verification\/[a-zA-Z0-9_./-]+$/) + .refine(p => !p.includes('..') && !p.includes('//')); +export const verificationProfileSchema = z + .object({ + id: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9_-]{0,79}$/), + version: z.string().min(1).max(100), + commands: z + .array(z.array(z.string().min(1).max(4096)).min(1).max(50)) + .min(1) + .max(20), + files: z + .array(z.object({ path: profilePath, sha256: z.string().regex(/^[a-f0-9]{64}$/) }).strict()) + .min(1) + .max(100), + }) + .strict() + .superRefine((p, ctx) => { + if (new Set(p.files.map(f => f.path)).size !== p.files.length) + ctx.addIssue({ code: 'custom', message: 'Profile file paths must be unique' }); + for (const argv of p.commands) { + if ( + !argv[0]?.startsWith('/') || + !argv.some(a => p.files.some(f => f.path === a)) || + argv.some(a => a.includes('\0')) + ) + ctx.addIssue({ + code: 'custom', + message: 'Each command must use an absolute executable and a pinned acceptance file', + }); + } + }); export function configuredVerificationProfiles(env: NodeJS.ProcessEnv): VerificationProfile[] { if (!env.WF_VERIFICATION_PROFILES_JSON) return []; @@ -21,22 +42,41 @@ export function configuredVerificationProfiles(env: NodeJS.ProcessEnv): Verifica const profiles = z.array(verificationProfileSchema).max(100).parse(JSON.parse(env.WF_VERIFICATION_PROFILES_JSON)); if (new Set(profiles.map(p => p.id)).size !== profiles.length) throw new Error('Duplicate profile id'); return profiles; - } catch { throw new DomainError('VERIFICATION_CONFIGURATION_INVALID', 'Operator verification profile configuration is invalid', 503); } + } catch { + throw new DomainError( + 'VERIFICATION_CONFIGURATION_INVALID', + 'Operator verification profile configuration is invalid', + 503, + ); + } } export function selectVerificationProfile(id: string, env: NodeJS.ProcessEnv): VerificationProfile { const profile = configuredVerificationProfiles(env).find(p => p.id === id); - if (!profile) throw new DomainError('VERIFICATION_PROFILE_UNAVAILABLE', 'Choose an operator-installed verification profile', 409); + if (!profile) + throw new DomainError('VERIFICATION_PROFILE_UNAVAILABLE', 'Choose an operator-installed verification profile', 409); return profile; } export function verificationPolicyDigest(project: Project): string { // Preserve the previous development contract when no independent profile exists. - return digest(project.verificationProfile ? { commands: project.verificationCommands, profile: project.verificationProfile } : project.verificationCommands); + return digest( + project.verificationProfile + ? { commands: project.verificationCommands, profile: project.verificationProfile } + : project.verificationCommands, + ); } export function assertVerificationProfile(project: Project, env: NodeJS.ProcessEnv) { if (!project.verificationProfile) { - if (env.NODE_ENV === 'production') throw new DomainError('VERIFICATION_PROFILE_REQUIRED', 'Production verification requires an operator-installed independent acceptance profile'); + if (env.NODE_ENV === 'production') + throw new DomainError( + 'VERIFICATION_PROFILE_REQUIRED', + 'Production verification requires an operator-installed independent acceptance profile', + ); return; } const active = selectVerificationProfile(project.verificationProfile.id, env); - if (digest(active) !== digest(project.verificationProfile)) throw new DomainError('VERIFICATION_PROFILE_REPLACED', 'The installed acceptance profile changed; select the new version and reverify'); + if (digest(active) !== digest(project.verificationProfile)) + throw new DomainError( + 'VERIFICATION_PROFILE_REPLACED', + 'The installed acceptance profile changed; select the new version and reverify', + ); } diff --git a/apps/api/src/domain/workspace-integration.ts b/apps/api/src/domain/workspace-integration.ts index e27680d..38787b2 100644 --- a/apps/api/src/domain/workspace-integration.ts +++ b/apps/api/src/domain/workspace-integration.ts @@ -1,63 +1,443 @@ -import {resolveGatewayTarget} from './gateway-target.js'; +import { resolveGatewayTarget } from './gateway-target.js'; import { DomainError, type Actor, type Entity, type Run } from '@wishforge/contracts'; import type { Store } from '../db/store.js'; import { executionLease, executionScope, must, projectAccess, touch } from './control-plane.js'; import { digest, requireHuman, requireRole } from './policy.js'; import { validateSnapshot, type WorkspaceSnapshot } from '../../../../packages/runtime/src/workspace-snapshot.js'; -export interface WorkspaceInput extends WorkspaceSnapshot { workspaceId:string;workspaceVersion:number;sourceCommit:string;activityDigest:string;requirementRevision:number;importGeneration:unknown;importedBy:string;importedAt:string } -interface InputState { workspaceId:string;latestSnapshot:WorkspaceSnapshot|null;currentDiskDigest:string;activityDigest:string;dirty:boolean;activeClients:number } -interface MatchRecord { generation:unknown;leaseToken:number;inputDigest:string;targetCommit:string;matchedAt:string } -export interface IntegrationLease {runId:string;generation:unknown;verifierId:string;leaseToken:number;fence:number;expiresAt:string} +export interface WorkspaceInput extends WorkspaceSnapshot { + workspaceId: string; + workspaceVersion: number; + sourceCommit: string; + activityDigest: string; + requirementRevision: number; + importGeneration: unknown; + importedBy: string; + importedAt: string; +} +interface InputState { + workspaceId: string; + latestSnapshot: WorkspaceSnapshot | null; + currentDiskDigest: string; + activityDigest: string; + dirty: boolean; + activeClients: number; +} +interface MatchRecord { + generation: unknown; + leaseToken: number; + inputDigest: string; + targetCommit: string; + matchedAt: string; +} +export interface IntegrationLease { + runId: string; + generation: unknown; + verifierId: string; + leaseToken: number; + fence: number; + expiresAt: string; +} export class WorkspaceIntegration { - constructor(private store:Store,private env=process.env,private fetcher:typeof fetch=fetch){} - private async load(actor:Actor,id:string){return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);await projectAccess(tx,actor,run.projectId);if(!['human','connector'].includes(actor.kind))executionScope(actor,run);const workspaces=(await tx.list('workspace')).filter(w=>w.projectId===run.projectId&&w.status!=='archived'&&(w.status==='ready'||typeof w.upstreamUrl==='string'));return{run,workspaces,inputs:(run.workspaceInputs??[]) as WorkspaceInput[]};});} - private async gateway(tenantId:string){const target=await resolveGatewayTarget(this.env,tenantId);if(!target)throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','No authenticated workspace gateway is configured for this tenant',503);return target;} - private async state(run:Run,workspace:Entity):Promise{ - if(workspace.status!=='ready')throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','Active workspace state is unavailable; integration is blocked',503); - const gateway=await this.gateway(run.tenantId); - const response=await this.fetcher(new URL('/internal/integration-state',gateway.internalUrl),{method:'POST',headers:{authorization:`Bearer ${gateway.token}`,'content-type':'application/json'},body:JSON.stringify({tenantId:run.tenantId,workspaceId:workspace.id,runId:run.id,generation:run.executionGeneration,revision:run.revision}),redirect:'error',signal:AbortSignal.timeout(15000)}).catch(()=>{throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','Workspace input service is unreachable; integration is blocked',503);}); - if(!response.ok)throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','Could not establish current workspace input state; integration is blocked',503); - const value=await response.json().catch(()=>{throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','Workspace input service returned an unreadable receipt',503);}) as InputState; - if(!value||value.workspaceId!==workspace.id||typeof value.dirty!=='boolean'||!/^[a-f0-9]{64}$/.test(value.currentDiskDigest)||!/^[a-f0-9]{64}$/.test(value.activityDigest))throw new DomainError('WORKSPACE_GATE_UNAVAILABLE','Workspace returned an invalid input-state receipt',503); + constructor( + private store: Store, + private env = process.env, + private fetcher: typeof fetch = fetch, + ) {} + private async load(actor: Actor, id: string) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + await projectAccess(tx, actor, run.projectId); + if (!['human', 'connector'].includes(actor.kind)) executionScope(actor, run); + const workspaces = (await tx.list('workspace')).filter( + w => + w.projectId === run.projectId && + w.status !== 'archived' && + (w.status === 'ready' || typeof w.upstreamUrl === 'string'), + ); + return { run, workspaces, inputs: (run.workspaceInputs ?? []) as WorkspaceInput[] }; + }); + } + private async gateway(tenantId: string) { + const target = await resolveGatewayTarget(this.env, tenantId); + if (!target) + throw new DomainError( + 'WORKSPACE_GATE_UNAVAILABLE', + 'No authenticated workspace gateway is configured for this tenant', + 503, + ); + return target; + } + private async state(run: Run, workspace: Entity): Promise { + if (workspace.status !== 'ready') + throw new DomainError( + 'WORKSPACE_GATE_UNAVAILABLE', + 'Active workspace state is unavailable; integration is blocked', + 503, + ); + const gateway = await this.gateway(run.tenantId); + const response = await this.fetcher(new URL('/internal/integration-state', gateway.internalUrl), { + method: 'POST', + headers: { authorization: `Bearer ${gateway.token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ + tenantId: run.tenantId, + workspaceId: workspace.id, + runId: run.id, + generation: run.executionGeneration, + revision: run.revision, + }), + redirect: 'error', + signal: AbortSignal.timeout(15000), + }).catch(() => { + throw new DomainError( + 'WORKSPACE_GATE_UNAVAILABLE', + 'Workspace input service is unreachable; integration is blocked', + 503, + ); + }); + if (!response.ok) + throw new DomainError( + 'WORKSPACE_GATE_UNAVAILABLE', + 'Could not establish current workspace input state; integration is blocked', + 503, + ); + const value = (await response.json().catch(() => { + throw new DomainError( + 'WORKSPACE_GATE_UNAVAILABLE', + 'Workspace input service returned an unreadable receipt', + 503, + ); + })) as InputState; + if ( + !value || + value.workspaceId !== workspace.id || + typeof value.dirty !== 'boolean' || + !/^[a-f0-9]{64}$/.test(value.currentDiskDigest) || + !/^[a-f0-9]{64}$/.test(value.activityDigest) + ) + throw new DomainError('WORKSPACE_GATE_UNAVAILABLE', 'Workspace returned an invalid input-state receipt', 503); return value; } - async inspect(actor:Actor,id:string){const loaded=await this.load(actor,id);return{required:loaded.workspaces.length>0,requirementRevision:loaded.run.revision,generation:loaded.run.executionGeneration,workspaces:loaded.workspaces.map(w=>({id:w.id,status:w.status})),snapshots:loaded.inputs.filter(input=>input.requirementRevision===loaded.run.revision&&loaded.workspaces.some(w=>w.id===input.workspaceId))};} - async importSnapshot(actor:Actor,id:string,workspaceId:string,sourceCommit:string,userAuthorization:string){ - requireHuman(actor);requireRole(actor,['owner','admin','developer']);if(!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(sourceCommit))throw new DomainError('INVALID_COMMIT','A full committed source SHA is required',400); - const before=await this.load(actor,id);const workspace=before.workspaces.find(w=>w.id===workspaceId);if(!workspace||workspace.status!=='ready')throw new DomainError('NOT_FOUND','Ready workspace not found',404); - const gateway=await this.gateway(actor.tenantId); - const response=await this.fetcher(new URL(`/workspaces/${workspaceId}/_wishforge/integration/snapshot`,gateway.internalUrl),{method:'POST',headers:{authorization:userAuthorization,'content-type':'application/json'},body:'{}',redirect:'error',signal:AbortSignal.timeout(45000)}); - if(!response.ok)throw new DomainError('WORKSPACE_SAVE_BARRIER_REQUIRED','Every active editor must save and acknowledge before importing the workspace',409); - const state=await response.json() as InputState;if(state.workspaceId!==workspaceId||state.dirty!==false||!/^[a-f0-9]{64}$/.test(state.activityDigest)||!state.latestSnapshot||state.currentDiskDigest!==state.latestSnapshot.digest)throw new DomainError('WORKSPACE_INPUT_CHANGED','Workspace changed before its snapshot could be imported');const snapshot=state.latestSnapshot;validateSnapshot(snapshot); - return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);await projectAccess(tx,actor,run.projectId);const current=await must(tx,'workspace',workspaceId);if(run.revision!==before.run.revision||run.projectId!==before.run.projectId||current.version!==workspace.version)throw new DomainError('WORKSPACE_INPUT_CHANGED','Requirement or workspace changed while saving'); - const input:WorkspaceInput={...snapshot,workspaceId,workspaceVersion:current.version,sourceCommit,activityDigest:state.activityDigest,requirementRevision:run.revision,importGeneration:run.executionGeneration,importedBy:actor.userId,importedAt:new Date().toISOString()}; - run.workspaceInputs=[...((run.workspaceInputs??[]) as WorkspaceInput[]).filter(item=>item.workspaceId!==workspaceId),input];delete run.workspaceInputMatch;await tx.put('run',touch(run));await tx.event(id,'workspace.snapshot-imported','Saved workspace input imported; independent Git tree matching is still required',{workspaceId,snapshotRevision:input.revision,snapshotDigest:input.digest,sourceCommit});return{...input,trust:'input-only',nextAction:'Commit/push the saved tree to the integration target, then resume for independent matching'};}); + async inspect(actor: Actor, id: string) { + const loaded = await this.load(actor, id); + return { + required: loaded.workspaces.length > 0, + requirementRevision: loaded.run.revision, + generation: loaded.run.executionGeneration, + workspaces: loaded.workspaces.map(w => ({ id: w.id, status: w.status })), + snapshots: loaded.inputs.filter( + input => + input.requirementRevision === loaded.run.revision && loaded.workspaces.some(w => w.id === input.workspaceId), + ), + }; } - private async fresh(actor:Actor,id:string){const loaded=await this.load(actor,id);if(!loaded.workspaces.length)return loaded;for(const w of loaded.workspaces){const input=loaded.inputs.find(i=>i.workspaceId===w.id&&i.requirementRevision===loaded.run.revision);if(!input)throw new DomainError('WORKSPACE_SNAPSHOT_REQUIRED','Import a fresh coordinated snapshot of each ready workspace before integration');if(input.workspaceVersion!==w.version)throw new DomainError('WORKSPACE_INPUT_CHANGED','Workspace provisioning changed; import a new snapshot');const current=await this.state(loaded.run,w);if(current.dirty||current.latestSnapshot?.id!==input.id||current.latestSnapshot.revision!==input.revision||current.currentDiskDigest!==input.digest||current.activityDigest!==input.activityDigest)throw new DomainError('WORKSPACE_INPUT_CHANGED','Editor versions, membership, saved revision or source content changed; save, import and reverify');}return loaded;} - async recordMatch(actor:Actor,id:string,body:{leaseToken:number;targetCommit:string;snapshots:Array<{workspaceId:string;sourceCommit:string;snapshotRevision:number;snapshotDigest:string}>}){ - if(actor.kind!=='verifier')throw new DomainError('FORBIDDEN','Independent verifier identity required',403);const scoped=await this.load(actor,id);executionLease(actor,scoped.run,body.leaseToken);const loaded=await this.fresh(actor,id);executionLease(actor,loaded.run,body.leaseToken);if(!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(body.targetCommit))throw new DomainError('INVALID_COMMIT','Full integration target SHA required',400); - const expected=loaded.inputs.filter(i=>loaded.workspaces.some(w=>w.id===i.workspaceId)).map(i=>({workspaceId:i.workspaceId,sourceCommit:i.sourceCommit,snapshotRevision:i.revision,snapshotDigest:i.digest})).sort((a,b)=>a.workspaceId.localeCompare(b.workspaceId)); - if(digest(expected)!==digest([...body.snapshots].sort((a,b)=>a.workspaceId.localeCompare(b.workspaceId))))throw new DomainError('WORKSPACE_INPUT_CHANGED','Verifier result does not bind the exact imported workspace inputs'); - return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionLease(actor,run,body.leaseToken);this.assertProjectLease(actor,run,await projectAccess(tx,actor,run.projectId));if(run.revision!==loaded.run.revision||digest(run.workspaceInputs??[])!==digest(loaded.inputs))throw new DomainError('WORKSPACE_INPUT_CHANGED','Inputs changed while the verifier matched Git');run.workspaceInputMatch={generation:run.executionGeneration,leaseToken:body.leaseToken,inputDigest:digest(loaded.inputs),targetCommit:body.targetCommit,matchedAt:new Date().toISOString()} satisfies MatchRecord;await tx.put('run',touch(run));return{matched:true,inputDigest:digest(loaded.inputs)};}); + async importSnapshot(actor: Actor, id: string, workspaceId: string, sourceCommit: string, userAuthorization: string) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin', 'developer']); + if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(sourceCommit)) + throw new DomainError('INVALID_COMMIT', 'A full committed source SHA is required', 400); + const before = await this.load(actor, id); + const workspace = before.workspaces.find(w => w.id === workspaceId); + if (!workspace || workspace.status !== 'ready') + throw new DomainError('NOT_FOUND', 'Ready workspace not found', 404); + const gateway = await this.gateway(actor.tenantId); + const response = await this.fetcher( + new URL(`/workspaces/${workspaceId}/_wishforge/integration/snapshot`, gateway.internalUrl), + { + method: 'POST', + headers: { authorization: userAuthorization, 'content-type': 'application/json' }, + body: '{}', + redirect: 'error', + signal: AbortSignal.timeout(45000), + }, + ); + if (!response.ok) + throw new DomainError( + 'WORKSPACE_SAVE_BARRIER_REQUIRED', + 'Every active editor must save and acknowledge before importing the workspace', + 409, + ); + const state = (await response.json()) as InputState; + if ( + state.workspaceId !== workspaceId || + state.dirty !== false || + !/^[a-f0-9]{64}$/.test(state.activityDigest) || + !state.latestSnapshot || + state.currentDiskDigest !== state.latestSnapshot.digest + ) + throw new DomainError('WORKSPACE_INPUT_CHANGED', 'Workspace changed before its snapshot could be imported'); + const snapshot = state.latestSnapshot; + validateSnapshot(snapshot); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + await projectAccess(tx, actor, run.projectId); + const current = await must(tx, 'workspace', workspaceId); + if ( + run.revision !== before.run.revision || + run.projectId !== before.run.projectId || + current.version !== workspace.version + ) + throw new DomainError('WORKSPACE_INPUT_CHANGED', 'Requirement or workspace changed while saving'); + const input: WorkspaceInput = { + ...snapshot, + workspaceId, + workspaceVersion: current.version, + sourceCommit, + activityDigest: state.activityDigest, + requirementRevision: run.revision, + importGeneration: run.executionGeneration, + importedBy: actor.userId, + importedAt: new Date().toISOString(), + }; + run.workspaceInputs = [ + ...((run.workspaceInputs ?? []) as WorkspaceInput[]).filter(item => item.workspaceId !== workspaceId), + input, + ]; + delete run.workspaceInputMatch; + await tx.put('run', touch(run)); + await tx.event( + id, + 'workspace.snapshot-imported', + 'Saved workspace input imported; independent Git tree matching is still required', + { workspaceId, snapshotRevision: input.revision, snapshotDigest: input.digest, sourceCommit }, + ); + return { + ...input, + trust: 'input-only', + nextAction: 'Commit/push the saved tree to the integration target, then resume for independent matching', + }; + }); } - async acquire(actor:Actor,id:string,leaseToken:number){ - if(actor.kind!=='verifier')throw new DomainError('FORBIDDEN','Independent verifier required',403); - return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionLease(actor,run,leaseToken);const project=await projectAccess(tx,actor,run.projectId);const previous=project.integrationLease as IntegrationLease|undefined; - if(previous&&Date.parse(previous.expiresAt)>Date.now()){const owner=await tx.get('run',previous.runId);const live=owner&&owner.executionGeneration===previous.generation&&owner.lease?.token===previous.leaseToken&&Date.parse(owner.lease.expiresAt)>Date.now();if(live&&(previous.runId!==id||previous.generation!==run.executionGeneration||previous.leaseToken!==leaseToken||previous.verifierId!==actor.userId))throw new DomainError('INTEGRATION_BUSY','Another run owns the project integration queue');if(live){previous.expiresAt=new Date(Math.min(Date.now()+60000,Date.parse(run.lease!.expiresAt))).toISOString();await tx.put('project',touch(project));return previous;}} - const fence=Number(project.integrationFence??0)+1;const lease:IntegrationLease={runId:id,generation:run.executionGeneration,verifierId:actor.userId,leaseToken,fence,expiresAt:new Date(Math.min(Date.now()+60000,Date.parse(run.lease!.expiresAt))).toISOString()};project.integrationFence=fence;project.integrationLease=lease;run.projectIntegrationFence=fence;await tx.put('project',touch(project));await tx.put('run',touch(run));return lease;}); + private async fresh(actor: Actor, id: string) { + const loaded = await this.load(actor, id); + if (!loaded.workspaces.length) return loaded; + for (const w of loaded.workspaces) { + const input = loaded.inputs.find(i => i.workspaceId === w.id && i.requirementRevision === loaded.run.revision); + if (!input) + throw new DomainError( + 'WORKSPACE_SNAPSHOT_REQUIRED', + 'Import a fresh coordinated snapshot of each ready workspace before integration', + ); + if (input.workspaceVersion !== w.version) + throw new DomainError('WORKSPACE_INPUT_CHANGED', 'Workspace provisioning changed; import a new snapshot'); + const current = await this.state(loaded.run, w); + if ( + current.dirty || + current.latestSnapshot?.id !== input.id || + current.latestSnapshot.revision !== input.revision || + current.currentDiskDigest !== input.digest || + current.activityDigest !== input.activityDigest + ) + throw new DomainError( + 'WORKSPACE_INPUT_CHANGED', + 'Editor versions, membership, saved revision or source content changed; save, import and reverify', + ); + } + return loaded; } - private assertProjectLease(actor:Actor,run:Run,project:Entity){ - if(this.env.NODE_ENV!=='production'&&!run.executionGeneration)return; - const lock=project.integrationLease as IntegrationLease|undefined; - if(!lock||lock.runId!==run.id||lock.generation!==run.executionGeneration||lock.verifierId!==actor.userId||lock.leaseToken!==run.lease?.token||lock.fence!==run.projectIntegrationFence||Date.parse(lock.expiresAt)<=Date.now())throw new DomainError('INTEGRATION_LEASE_REQUIRED','A live database-backed project integration lease is required'); + async recordMatch( + actor: Actor, + id: string, + body: { + leaseToken: number; + targetCommit: string; + snapshots: Array<{ workspaceId: string; sourceCommit: string; snapshotRevision: number; snapshotDigest: string }>; + }, + ) { + if (actor.kind !== 'verifier') throw new DomainError('FORBIDDEN', 'Independent verifier identity required', 403); + const scoped = await this.load(actor, id); + executionLease(actor, scoped.run, body.leaseToken); + const loaded = await this.fresh(actor, id); + executionLease(actor, loaded.run, body.leaseToken); + if (!/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/.test(body.targetCommit)) + throw new DomainError('INVALID_COMMIT', 'Full integration target SHA required', 400); + const expected = loaded.inputs + .filter(i => loaded.workspaces.some(w => w.id === i.workspaceId)) + .map(i => ({ + workspaceId: i.workspaceId, + sourceCommit: i.sourceCommit, + snapshotRevision: i.revision, + snapshotDigest: i.digest, + })) + .sort((a, b) => a.workspaceId.localeCompare(b.workspaceId)); + if (digest(expected) !== digest([...body.snapshots].sort((a, b) => a.workspaceId.localeCompare(b.workspaceId)))) + throw new DomainError( + 'WORKSPACE_INPUT_CHANGED', + 'Verifier result does not bind the exact imported workspace inputs', + ); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, body.leaseToken); + this.assertProjectLease(actor, run, await projectAccess(tx, actor, run.projectId)); + if (run.revision !== loaded.run.revision || digest(run.workspaceInputs ?? []) !== digest(loaded.inputs)) + throw new DomainError('WORKSPACE_INPUT_CHANGED', 'Inputs changed while the verifier matched Git'); + run.workspaceInputMatch = { + generation: run.executionGeneration, + leaseToken: body.leaseToken, + inputDigest: digest(loaded.inputs), + targetCommit: body.targetCommit, + matchedAt: new Date().toISOString(), + } satisfies MatchRecord; + await tx.put('run', touch(run)); + return { matched: true, inputDigest: digest(loaded.inputs) }; + }); + } + async acquire(actor: Actor, id: string, leaseToken: number) { + if (actor.kind !== 'verifier') throw new DomainError('FORBIDDEN', 'Independent verifier required', 403); + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, leaseToken); + const project = await projectAccess(tx, actor, run.projectId); + const previous = project.integrationLease as IntegrationLease | undefined; + if (previous && Date.parse(previous.expiresAt) > Date.now()) { + const owner = await tx.get('run', previous.runId); + const live = + owner && + owner.executionGeneration === previous.generation && + owner.lease?.token === previous.leaseToken && + Date.parse(owner.lease.expiresAt) > Date.now(); + if ( + live && + (previous.runId !== id || + previous.generation !== run.executionGeneration || + previous.leaseToken !== leaseToken || + previous.verifierId !== actor.userId) + ) + throw new DomainError('INTEGRATION_BUSY', 'Another run owns the project integration queue'); + if (live) { + previous.expiresAt = new Date(Math.min(Date.now() + 60000, Date.parse(run.lease!.expiresAt))).toISOString(); + await tx.put('project', touch(project)); + return previous; + } + } + const fence = Number(project.integrationFence ?? 0) + 1; + const lease: IntegrationLease = { + runId: id, + generation: run.executionGeneration, + verifierId: actor.userId, + leaseToken, + fence, + expiresAt: new Date(Math.min(Date.now() + 60000, Date.parse(run.lease!.expiresAt))).toISOString(), + }; + project.integrationFence = fence; + project.integrationLease = lease; + run.projectIntegrationFence = fence; + await tx.put('project', touch(project)); + await tx.put('run', touch(run)); + return lease; + }); + } + private assertProjectLease(actor: Actor, run: Run, project: Entity) { + if (this.env.NODE_ENV !== 'production' && !run.executionGeneration) return; + const lock = project.integrationLease as IntegrationLease | undefined; + if ( + !lock || + lock.runId !== run.id || + lock.generation !== run.executionGeneration || + lock.verifierId !== actor.userId || + lock.leaseToken !== run.lease?.token || + lock.fence !== run.projectIntegrationFence || + Date.parse(lock.expiresAt) <= Date.now() + ) + throw new DomainError( + 'INTEGRATION_LEASE_REQUIRED', + 'A live database-backed project integration lease is required', + ); + } + async renew(actor: Actor, id: string, leaseToken: number, fence: number) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, leaseToken); + const project = await projectAccess(tx, actor, run.projectId); + this.assertProjectLease(actor, run, project); + const lock = project.integrationLease as IntegrationLease; + if (!lock || lock.fence !== fence) + throw new DomainError('STALE_INTEGRATION_FENCE', 'Project integration ownership changed'); + lock.expiresAt = new Date(Math.min(Date.now() + 60000, Date.parse(run.lease!.expiresAt))).toISOString(); + await tx.put('project', touch(project)); + return lock; + }); + } + async release(actor: Actor, id: string, leaseToken: number, fence: number) { + return this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionScope(actor, run); + const project = await projectAccess(tx, actor, run.projectId); + const lock = project.integrationLease as IntegrationLease | undefined; + if ( + lock?.runId === id && + lock.generation === run.executionGeneration && + lock.verifierId === actor.userId && + lock.leaseToken === leaseToken && + lock.fence === fence + ) { + delete project.integrationLease; + await tx.put('project', touch(project)); + return { released: true }; + } + return { released: false }; + }); + } + async enforce(actor: Actor, id: string, leaseToken: number) { + const before = await this.load(actor, id); + executionLease(actor, before.run, leaseToken); + try { + await this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, leaseToken); + this.assertProjectLease(actor, run, await projectAccess(tx, actor, run.projectId)); + }); + const loaded = await this.fresh(actor, id); + await this.store.transaction(actor.tenantId, async tx => { + const current = await must(tx, 'run', id); + executionLease(actor, current, leaseToken); + this.assertProjectLease(actor, current, await projectAccess(tx, actor, current.projectId)); + if (current.revision !== loaded.run.revision || digest(current.workspaceInputs ?? []) !== digest(loaded.inputs)) + throw new DomainError('WORKSPACE_INPUT_CHANGED', 'Workspace inputs changed during current-state checks'); + const active = (await tx.list('workspace')).filter( + w => + w.projectId === current.projectId && + w.status !== 'archived' && + (w.status === 'ready' || typeof w.upstreamUrl === 'string'), + ); + const versions = (list: Entity[]) => + list.map(w => ({ id: w.id, version: w.version })).sort((a, b) => a.id.localeCompare(b.id)); + if (digest(versions(active)) !== digest(versions(loaded.workspaces))) + throw new DomainError( + 'WORKSPACE_INPUT_CHANGED', + 'Workspace membership or provisioning changed during current-state checks', + ); + if (!active.length) return; + const match = current.workspaceInputMatch as MatchRecord | undefined; + if ( + !match || + match.generation !== current.executionGeneration || + match.leaseToken !== current.lease?.token || + match.inputDigest !== digest(loaded.inputs) + ) + throw new DomainError( + 'WORKSPACE_GIT_MATCH_REQUIRED', + 'Independent verifier must match the imported snapshot to the committed integration input in this execution generation', + ); + }); + } catch (error) { + if ( + error instanceof DomainError && + (error.code.startsWith('WORKSPACE_') || error.code.startsWith('INTEGRATION_')) + ) + await this.store.transaction(actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + executionLease(actor, run, leaseToken); + if (['succeeded', 'failed', 'canceled', 'paused'].includes(run.status)) return; + run.status = 'waiting'; + run.waitingReason = error.code.toLowerCase(); + if (run.lastActiveAt) run.activeMilliseconds += Math.max(0, Date.now() - Date.parse(run.lastActiveAt)); + run.lastActiveAt = undefined; + if (run.lease) run.lease.expiresAt = new Date().toISOString(); + await tx.put('run', touch(run)); + await tx.event(id, 'run.waiting', error.message); + }); + throw error; + } } - async renew(actor:Actor,id:string,leaseToken:number,fence:number){return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionLease(actor,run,leaseToken);const project=await projectAccess(tx,actor,run.projectId);this.assertProjectLease(actor,run,project);const lock=project.integrationLease as IntegrationLease;if(!lock||lock.fence!==fence)throw new DomainError('STALE_INTEGRATION_FENCE','Project integration ownership changed');lock.expiresAt=new Date(Math.min(Date.now()+60000,Date.parse(run.lease!.expiresAt))).toISOString();await tx.put('project',touch(project));return lock;});} - async release(actor:Actor,id:string,leaseToken:number,fence:number){return this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionScope(actor,run);const project=await projectAccess(tx,actor,run.projectId);const lock=project.integrationLease as IntegrationLease|undefined;if(lock?.runId===id&&lock.generation===run.executionGeneration&&lock.verifierId===actor.userId&&lock.leaseToken===leaseToken&&lock.fence===fence){delete project.integrationLease;await tx.put('project',touch(project));return{released:true};}return{released:false};});} - async enforce(actor:Actor,id:string,leaseToken:number){const before=await this.load(actor,id);executionLease(actor,before.run,leaseToken);try{await this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionLease(actor,run,leaseToken);this.assertProjectLease(actor,run,await projectAccess(tx,actor,run.projectId));});const loaded=await this.fresh(actor,id);await this.store.transaction(actor.tenantId,async tx=>{const current=await must(tx,'run',id);executionLease(actor,current,leaseToken);this.assertProjectLease(actor,current,await projectAccess(tx,actor,current.projectId)); - if(current.revision!==loaded.run.revision||digest(current.workspaceInputs??[])!==digest(loaded.inputs))throw new DomainError('WORKSPACE_INPUT_CHANGED','Workspace inputs changed during current-state checks'); - const active=(await tx.list('workspace')).filter(w=>w.projectId===current.projectId&&w.status!=='archived'&&(w.status==='ready'||typeof w.upstreamUrl==='string')); - const versions=(list:Entity[])=>list.map(w=>({id:w.id,version:w.version})).sort((a,b)=>a.id.localeCompare(b.id)); - if(digest(versions(active))!==digest(versions(loaded.workspaces)))throw new DomainError('WORKSPACE_INPUT_CHANGED','Workspace membership or provisioning changed during current-state checks'); - if(!active.length)return;const match=current.workspaceInputMatch as MatchRecord|undefined;if(!match||match.generation!==current.executionGeneration||match.leaseToken!==current.lease?.token||match.inputDigest!==digest(loaded.inputs))throw new DomainError('WORKSPACE_GIT_MATCH_REQUIRED','Independent verifier must match the imported snapshot to the committed integration input in this execution generation');});}catch(error){if(error instanceof DomainError&&(error.code.startsWith('WORKSPACE_')||error.code.startsWith('INTEGRATION_')))await this.store.transaction(actor.tenantId,async tx=>{const run=await must(tx,'run',id);executionLease(actor,run,leaseToken);if(['succeeded','failed','canceled','paused'].includes(run.status))return;run.status='waiting';run.waitingReason=error.code.toLowerCase();if(run.lastActiveAt)run.activeMilliseconds+=Math.max(0,Date.now()-Date.parse(run.lastActiveAt));run.lastActiveAt=undefined;if(run.lease)run.lease.expiresAt=new Date().toISOString();await tx.put('run',touch(run));await tx.event(id,'run.waiting',error.message);});throw error;}} } diff --git a/apps/api/src/domain/workspace-launch.ts b/apps/api/src/domain/workspace-launch.ts index 140295c..c5a6069 100644 --- a/apps/api/src/domain/workspace-launch.ts +++ b/apps/api/src/domain/workspace-launch.ts @@ -1,9 +1,40 @@ -import {DomainError,type Actor} from '@wishforge/contracts'; -import type {Store} from '../db/store.js'; -import {must,projectAccess} from './control-plane.js'; -import {requireHuman,requireRole} from './policy.js'; -import {resolveGatewayTarget,validateGatewayLaunch} from './gateway-target.js'; -export class WorkspaceLaunch{ - constructor(private store:Store,private env=process.env,private fetcher:typeof fetch=fetch){} - async launch(actor:Actor,id:string,authorization:string){requireHuman(actor);requireRole(actor,['owner','admin','developer']);await this.store.transaction(actor.tenantId,async tx=>{const workspace=await must(tx,'workspace',id);await projectAccess(tx,actor,String(workspace.projectId));if(workspace.status!=='ready'||!workspace.upstreamUrl)throw new DomainError('WORKSPACE_NOT_READY','Workspace is not ready',503);});const target=await resolveGatewayTarget(this.env,actor.tenantId);if(!target)throw new DomainError('GATEWAY_UNCONFIGURED','No IDE gateway is configured for this tenant',503);const response=await this.fetcher(new URL('/launch',target.internalUrl),{method:'POST',headers:{authorization,'content-type':'application/json'},body:JSON.stringify({workspaceId:id}),redirect:'error',signal:AbortSignal.timeout(15000)}).catch(()=>{throw new DomainError('GATEWAY_UNAVAILABLE','Tenant IDE gateway is unavailable',503);});if(!response.ok)throw new DomainError('IDE_LAUNCH_DENIED','Tenant gateway denied this interactive workspace session',[401,403,404].includes(response.status)?response.status:503);return validateGatewayLaunch(target,await response.json());} +import { DomainError, type Actor } from '@wishforge/contracts'; +import type { Store } from '../db/store.js'; +import { must, projectAccess } from './control-plane.js'; +import { requireHuman, requireRole } from './policy.js'; +import { resolveGatewayTarget, validateGatewayLaunch } from './gateway-target.js'; +export class WorkspaceLaunch { + constructor( + private store: Store, + private env = process.env, + private fetcher: typeof fetch = fetch, + ) {} + async launch(actor: Actor, id: string, authorization: string) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin', 'developer']); + await this.store.transaction(actor.tenantId, async tx => { + const workspace = await must(tx, 'workspace', id); + await projectAccess(tx, actor, String(workspace.projectId)); + if (workspace.status !== 'ready' || !workspace.upstreamUrl) + throw new DomainError('WORKSPACE_NOT_READY', 'Workspace is not ready', 503); + }); + const target = await resolveGatewayTarget(this.env, actor.tenantId); + if (!target) throw new DomainError('GATEWAY_UNCONFIGURED', 'No IDE gateway is configured for this tenant', 503); + const response = await this.fetcher(new URL('/launch', target.internalUrl), { + method: 'POST', + headers: { authorization, 'content-type': 'application/json' }, + body: JSON.stringify({ workspaceId: id }), + redirect: 'error', + signal: AbortSignal.timeout(15000), + }).catch(() => { + throw new DomainError('GATEWAY_UNAVAILABLE', 'Tenant IDE gateway is unavailable', 503); + }); + if (!response.ok) + throw new DomainError( + 'IDE_LAUNCH_DENIED', + 'Tenant gateway denied this interactive workspace session', + [401, 403, 404].includes(response.status) ? response.status : 503, + ); + return validateGatewayLaunch(target, await response.json()); + } } diff --git a/apps/api/src/domain/workspace-lifecycle.ts b/apps/api/src/domain/workspace-lifecycle.ts index 305f146..d96a115 100644 --- a/apps/api/src/domain/workspace-lifecycle.ts +++ b/apps/api/src/domain/workspace-lifecycle.ts @@ -1,49 +1,348 @@ -import {resolveGatewayTarget} from './gateway-target.js'; -import {resolveProvisionerTarget} from './provisioner-target.js'; +import { resolveGatewayTarget } from './gateway-target.js'; +import { resolveProvisionerTarget } from './provisioner-target.js'; import { randomUUID } from 'node:crypto'; import { DomainError, type Actor, type Entity } from '@wishforge/contracts'; import type { OutboxItem, Store, Transaction } from '../db/store.js'; import { idempotent, must, projectAccess, touch } from './control-plane.js'; import { digest, requireHuman, requireRole } from './policy.js'; import { validateSnapshot, type WorkspaceSnapshot } from '../../../../packages/runtime/src/workspace-snapshot.js'; -export const WORKSPACE_IDLE_MS=30*60_000; -interface Lifecycle {revision:number;desired:'paused'|'running';automatic:boolean;requestedAt:string;operationId:string;state:'queued'|'unknown'|'observing'|'complete';snapshotDigest?:string} -interface SleepState {workspaceId:string;gatewayConnections:number;known:boolean;activeClients:number;terminals:number;debugSessions:number;dirty:boolean;snapshotComplete:boolean;snapshotRevision:number;diskDigest:string} -export class WorkspaceLifecycle { - constructor(private store:Store,private env=process.env,private fetcher:typeof fetch=fetch){} - - private async access(tx:Transaction,actor:Actor,id:string){requireHuman(actor);requireRole(actor,['owner','admin','developer']);const currentRole=await tx.membership(actor.userId);if(!currentRole||!['owner','admin','developer'].includes(currentRole))throw new DomainError('FORBIDDEN','Workspace permission was revoked',403);const workspace=await must(tx,'workspace',id);await projectAccess(tx,{...actor,role:currentRole},String(workspace.projectId));return workspace;} - private safe(w:Entity){return{id:w.id,status:w.status,lifecycle:w.lifecycle,reason:w.reason,lastActivityAt:w.lastActivityAt};} - private async configured(tenantId:string){const [provisioner,gateway]=await Promise.all([resolveProvisionerTarget(this.env,tenantId),resolveGatewayTarget(this.env,tenantId)]);if(!provisioner||!gateway)throw new DomainError('WORKSPACE_LIFECYCLE_UNCONFIGURED','Scoped workspace provisioner and gateway are required for this tenant',503);return{provisioner,gateway};} - async manual(actor:Actor,id:string,action:'pause'|'resume',key:string,authorization:string){ - if(!key||key.length<8||key.length>200)throw new DomainError('IDEMPOTENCY_REQUIRED','Provide an Idempotency-Key of 8–200 characters',400); - const request={workspaceId:id,action};const before=await this.store.transaction(actor.tenantId,async tx=>{const workspace=await this.access(tx,actor,id);const previous=await tx.idempotency(key);if(previous){if(previous.digest!==digest(request))throw new DomainError('IDEMPOTENCY_CONFLICT','Key already identifies another workspace action');return{previous:previous.response,workspace};}return{workspace};});if('previous' in before)return before.previous; - const {gateway}=await this.configured(actor.tenantId);if(before.workspace.managedBy!=='wishforge-provisioner')throw new DomainError('WORKSPACE_LIFECYCLE_UNCONFIGURED','This workspace does not have a managed lifecycle',503); - const desired=action==='pause'?'paused':'running';if(before.workspace.status===(action==='pause'?'paused':'ready'))return this.store.transaction(actor.tenantId,async tx=>{const current=await this.access(tx,actor,id);return idempotent(tx,key,request,async()=>{if(current.status!==before.workspace.status)throw new DomainError('WORKSPACE_OPERATION_CHANGED','Workspace changed before no-op confirmation');return this.safe(current);});}); - if(!['ready','paused','pausing','resuming','unknown'].includes(String(before.workspace.status)))throw new DomainError('WORKSPACE_NOT_READY','Workspace provisioning must be reconciled first'); - let snapshot:WorkspaceSnapshot|undefined; - if(action==='pause'){ - if(before.workspace.status!=='ready')throw new DomainError('WORKSPACE_OPERATION_PENDING','Resolve the existing lifecycle operation before pausing'); - const response=await this.fetcher(new URL(`/workspaces/${id}/_wishforge/integration/snapshot`,gateway.internalUrl),{method:'POST',headers:{authorization,'content-type':'application/json'},body:'{}',redirect:'error',signal:AbortSignal.timeout(45000)}).catch(()=>{throw new DomainError('WORKSPACE_SAVE_REQUIRED','Could not confirm all editor saves; workspace remains running');}); - if(!response.ok)throw new DomainError('WORKSPACE_SAVE_REQUIRED','Every active editor must acknowledge a saved snapshot before pausing'); - const receipt=await response.json() as {workspaceId:string;dirty:boolean;latestSnapshot?:WorkspaceSnapshot;currentDiskDigest:string};if(receipt.workspaceId!==id||receipt.dirty!==false||!receipt.latestSnapshot||receipt.currentDiskDigest!==receipt.latestSnapshot.digest)throw new DomainError('WORKSPACE_SAVE_REQUIRED','Workspace changed while saving');validateSnapshot(receipt.latestSnapshot);snapshot=receipt.latestSnapshot; - } - return this.store.transaction(actor.tenantId,async tx=>{const workspace=await this.access(tx,actor,id);return idempotent(tx,key,request,async()=>{if(workspace.upstreamUrl!==before.workspace.upstreamUrl||Number((workspace.lifecycle as Lifecycle|undefined)?.revision??0)!==Number((before.workspace.lifecycle as Lifecycle|undefined)?.revision??0))throw new DomainError('WORKSPACE_OPERATION_CHANGED','Another lifecycle action took ownership');return this.queue(tx,workspace,desired,false,snapshot?.digest);});}); - } - private async queue(tx:Transaction,workspace:Entity,desired:Lifecycle['desired'],automatic:boolean,snapshotDigest?:string){const revision=Number((workspace.lifecycle as Lifecycle|undefined)?.revision??0)+1;const operationId=randomUUID();workspace.lifecycle={revision,desired,automatic,requestedAt:new Date().toISOString(),operationId,state:'queued',snapshotDigest} satisfies Lifecycle;workspace.status=desired==='paused'?'pausing':'resuming';workspace.reason='Waiting for the isolated workspace controller to confirm the requested state.';await tx.put('workspace',touch(workspace));await tx.enqueue('workspace.reconcile',{workspaceId:workspace.id,projectId:workspace.projectId,revision,desired},operationId);return this.safe(workspace);} - private async safety(workspace:Entity):Promise{const gateway=await resolveGatewayTarget(this.env,workspace.tenantId);if(!gateway)throw new DomainError('WORKSPACE_ACTIVITY_UNKNOWN','Tenant gateway is unavailable');const response=await this.fetcher(new URL('/internal/lifecycle-state',gateway.internalUrl),{method:'POST',headers:{authorization:`Bearer ${gateway.token}`,'content-type':'application/json'},body:JSON.stringify({tenantId:workspace.tenantId,workspaceId:workspace.id}),redirect:'error',signal:AbortSignal.timeout(15000)});if(!response.ok)throw new DomainError('WORKSPACE_ACTIVITY_UNKNOWN','Workspace activity cannot be established');const value=await response.json() as SleepState;if(value.workspaceId!==workspace.id||value.known!==true||value.gatewayConnections!==0||value.activeClients!==0||value.terminals!==0||value.debugSessions!==0||value.dirty!==false||value.snapshotComplete!==true||!Number.isSafeInteger(value.snapshotRevision)||value.snapshotRevision<1||!/^[a-f0-9]{64}$/.test(value.diskDigest))throw new DomainError('WORKSPACE_NOT_IDLE','Workspace has active or unknown work, or lacks a complete saved snapshot');return value;} - async scheduleIdle(tenantId:string){if(this.env.WF_WORKSPACE_AUTO_SLEEP==='false')return;try{await this.configured(tenantId);}catch{return;}const cutoff=Date.now()-WORKSPACE_IDLE_MS;const candidates=await this.store.transaction(tenantId,async tx=>(await tx.list('workspace')).filter(w=>w.managedBy==='wishforge-provisioner'&&w.status==='ready'&&typeof w.lastActivityAt==='string'&&Date.parse(w.lastActivityAt)<=cutoff));for(const before of candidates){try{const receipt=await this.safety(before);await this.store.transaction(tenantId,async tx=>{const w=await must(tx,'workspace',before.id);if(w.status!=='ready'||w.lastActivityAt!==before.lastActivityAt||Date.parse(String(w.lastActivityAt))>cutoff)return;await this.queue(tx,w,'paused',true,receipt.diskDigest);});}catch{/* Unknown or active work is never automatically stopped. */}}} - async reconcile(tenantId:string,item:OutboxItem){const workspace=await this.store.transaction(tenantId,tx=>must(tx,'workspace',String(item.payload.workspaceId)));const operation=workspace.lifecycle as Lifecycle|undefined;if(!operation||operation.operationId!==item.id||operation.revision!==item.payload.revision){await this.store.transaction(tenantId,tx=>tx.acknowledge(item.id));return;} - try{const {provisioner}=await this.configured(tenantId); - if(operation.automatic&&Date.now()-Date.parse(operation.requestedAt)<5000){await this.store.transaction(tenantId,tx=>tx.defer(item.id,5));return;} - const target=new URL(`${provisioner.url}/workspaces/${workspace.id}/lifecycle`);target.searchParams.set('tenantId',tenantId);target.searchParams.set('projectId',String(workspace.projectId));const headers={authorization:`Bearer ${provisioner.token}`,'content-type':'application/json'}; - // Always observe first. A lost response never causes an unconditional mutation replay. - const observed=await this.fetcher(target,{headers,redirect:'error',signal:AbortSignal.timeout(15000)});if(!observed.ok)throw new Error('WORKSPACE_OBSERVE_UNKNOWN');let state=await observed.json() as {workspaceId:string;revision:number;desired:string;status:string}; - const valid=()=>state.workspaceId===workspace.id&&Number.isSafeInteger(state.revision)&&state.revision>=0&&['running','paused'].includes(state.desired)&&['ready','paused','pending'].includes(state.status);if(!valid()||state.revision>operation.revision)throw new Error('WORKSPACE_OBSERVE_UNKNOWN'); - if(state.revision{const current=await must(tx,'workspace',workspace.id);if((current.lifecycle as Lifecycle|undefined)?.operationId!==item.id)return;await this.queue(tx,current,'running',false);await tx.acknowledge(item.id);});return;}} - if(state.revision{const current=await must(tx,'workspace',workspace.id);if((current.lifecycle as Lifecycle|undefined)?.operationId!==item.id)throw new Error('WORKSPACE_SUPERSEDED');if(operation.automatic&¤t.lastActivityAt!==workspace.lastActivityAt)throw new Error('WORKSPACE_ACTIVITY_CHANGED');current.lifecycle={...operation,state:'unknown'};await tx.put('workspace',touch(current));});target.search='';const changed=await this.fetcher(target,{method:'PUT',headers,body:JSON.stringify({tenantId,projectId:workspace.projectId,workspaceId:workspace.id,revision:operation.revision,desired:operation.desired}),redirect:'error',signal:AbortSignal.timeout(15000)});if(!changed.ok)throw new Error('WORKSPACE_RECONCILE_UNKNOWN');state=await changed.json() as typeof state;} - if(!valid()||state.revision!==operation.revision||state.desired!==operation.desired)throw new Error('WORKSPACE_OBSERVE_UNKNOWN'); - await this.store.transaction(tenantId,async tx=>{const current=await must(tx,'workspace',workspace.id);if((current.lifecycle as Lifecycle|undefined)?.operationId!==item.id)return;const complete=(operation.desired==='paused'&&state.status==='paused')||(operation.desired==='running'&&state.status==='ready');current.lifecycle={...operation,state:complete?'complete':'observing'};current.status=complete?state.status:operation.desired==='paused'?'pausing':'resuming';current.reason=complete?undefined:'Waiting for observed workspace replicas and readiness.';if(complete&&state.status==='ready')current.lastActivityAt=new Date().toISOString();await tx.put('workspace',touch(current));if(complete)await tx.acknowledge(item.id);else await tx.defer(item.id,10);}); - }catch{await this.store.transaction(tenantId,async tx=>{const current=await must(tx,'workspace',workspace.id);if((current.lifecycle as Lifecycle|undefined)?.operationId!==item.id)return;current.status='unknown';current.reason='Workspace controller outcome is unknown; observation will be retried before any new action.';current.lifecycle={...operation,state:'unknown'};await tx.put('workspace',touch(current));await tx.defer(item.id,30);});} - } +export const WORKSPACE_IDLE_MS = 30 * 60_000; +interface Lifecycle { + revision: number; + desired: 'paused' | 'running'; + automatic: boolean; + requestedAt: string; + operationId: string; + state: 'queued' | 'unknown' | 'observing' | 'complete'; + snapshotDigest?: string; +} +interface SleepState { + workspaceId: string; + gatewayConnections: number; + known: boolean; + activeClients: number; + terminals: number; + debugSessions: number; + dirty: boolean; + snapshotComplete: boolean; + snapshotRevision: number; + diskDigest: string; +} +export class WorkspaceLifecycle { + constructor( + private store: Store, + private env = process.env, + private fetcher: typeof fetch = fetch, + ) {} + + private async access(tx: Transaction, actor: Actor, id: string) { + requireHuman(actor); + requireRole(actor, ['owner', 'admin', 'developer']); + const currentRole = await tx.membership(actor.userId); + if (!currentRole || !['owner', 'admin', 'developer'].includes(currentRole)) + throw new DomainError('FORBIDDEN', 'Workspace permission was revoked', 403); + const workspace = await must(tx, 'workspace', id); + await projectAccess(tx, { ...actor, role: currentRole }, String(workspace.projectId)); + return workspace; + } + private safe(w: Entity) { + return { id: w.id, status: w.status, lifecycle: w.lifecycle, reason: w.reason, lastActivityAt: w.lastActivityAt }; + } + private async configured(tenantId: string) { + const [provisioner, gateway] = await Promise.all([ + resolveProvisionerTarget(this.env, tenantId), + resolveGatewayTarget(this.env, tenantId), + ]); + if (!provisioner || !gateway) + throw new DomainError( + 'WORKSPACE_LIFECYCLE_UNCONFIGURED', + 'Scoped workspace provisioner and gateway are required for this tenant', + 503, + ); + return { provisioner, gateway }; + } + async manual(actor: Actor, id: string, action: 'pause' | 'resume', key: string, authorization: string) { + if (!key || key.length < 8 || key.length > 200) + throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400); + const request = { workspaceId: id, action }; + const before = await this.store.transaction(actor.tenantId, async tx => { + const workspace = await this.access(tx, actor, id); + const previous = await tx.idempotency(key); + if (previous) { + if (previous.digest !== digest(request)) + throw new DomainError('IDEMPOTENCY_CONFLICT', 'Key already identifies another workspace action'); + return { previous: previous.response, workspace }; + } + return { workspace }; + }); + if ('previous' in before) return before.previous; + const { gateway } = await this.configured(actor.tenantId); + if (before.workspace.managedBy !== 'wishforge-provisioner') + throw new DomainError( + 'WORKSPACE_LIFECYCLE_UNCONFIGURED', + 'This workspace does not have a managed lifecycle', + 503, + ); + const desired = action === 'pause' ? 'paused' : 'running'; + if (before.workspace.status === (action === 'pause' ? 'paused' : 'ready')) + return this.store.transaction(actor.tenantId, async tx => { + const current = await this.access(tx, actor, id); + return idempotent(tx, key, request, async () => { + if (current.status !== before.workspace.status) + throw new DomainError('WORKSPACE_OPERATION_CHANGED', 'Workspace changed before no-op confirmation'); + return this.safe(current); + }); + }); + if (!['ready', 'paused', 'pausing', 'resuming', 'unknown'].includes(String(before.workspace.status))) + throw new DomainError('WORKSPACE_NOT_READY', 'Workspace provisioning must be reconciled first'); + let snapshot: WorkspaceSnapshot | undefined; + if (action === 'pause') { + if (before.workspace.status !== 'ready') + throw new DomainError('WORKSPACE_OPERATION_PENDING', 'Resolve the existing lifecycle operation before pausing'); + const response = await this.fetcher( + new URL(`/workspaces/${id}/_wishforge/integration/snapshot`, gateway.internalUrl), + { + method: 'POST', + headers: { authorization, 'content-type': 'application/json' }, + body: '{}', + redirect: 'error', + signal: AbortSignal.timeout(45000), + }, + ).catch(() => { + throw new DomainError( + 'WORKSPACE_SAVE_REQUIRED', + 'Could not confirm all editor saves; workspace remains running', + ); + }); + if (!response.ok) + throw new DomainError( + 'WORKSPACE_SAVE_REQUIRED', + 'Every active editor must acknowledge a saved snapshot before pausing', + ); + const receipt = (await response.json()) as { + workspaceId: string; + dirty: boolean; + latestSnapshot?: WorkspaceSnapshot; + currentDiskDigest: string; + }; + if ( + receipt.workspaceId !== id || + receipt.dirty !== false || + !receipt.latestSnapshot || + receipt.currentDiskDigest !== receipt.latestSnapshot.digest + ) + throw new DomainError('WORKSPACE_SAVE_REQUIRED', 'Workspace changed while saving'); + validateSnapshot(receipt.latestSnapshot); + snapshot = receipt.latestSnapshot; + } + return this.store.transaction(actor.tenantId, async tx => { + const workspace = await this.access(tx, actor, id); + return idempotent(tx, key, request, async () => { + if ( + workspace.upstreamUrl !== before.workspace.upstreamUrl || + Number((workspace.lifecycle as Lifecycle | undefined)?.revision ?? 0) !== + Number((before.workspace.lifecycle as Lifecycle | undefined)?.revision ?? 0) + ) + throw new DomainError('WORKSPACE_OPERATION_CHANGED', 'Another lifecycle action took ownership'); + return this.queue(tx, workspace, desired, false, snapshot?.digest); + }); + }); + } + private async queue( + tx: Transaction, + workspace: Entity, + desired: Lifecycle['desired'], + automatic: boolean, + snapshotDigest?: string, + ) { + const revision = Number((workspace.lifecycle as Lifecycle | undefined)?.revision ?? 0) + 1; + const operationId = randomUUID(); + workspace.lifecycle = { + revision, + desired, + automatic, + requestedAt: new Date().toISOString(), + operationId, + state: 'queued', + snapshotDigest, + } satisfies Lifecycle; + workspace.status = desired === 'paused' ? 'pausing' : 'resuming'; + workspace.reason = 'Waiting for the isolated workspace controller to confirm the requested state.'; + await tx.put('workspace', touch(workspace)); + await tx.enqueue( + 'workspace.reconcile', + { workspaceId: workspace.id, projectId: workspace.projectId, revision, desired }, + operationId, + ); + return this.safe(workspace); + } + private async safety(workspace: Entity): Promise { + const gateway = await resolveGatewayTarget(this.env, workspace.tenantId); + if (!gateway) throw new DomainError('WORKSPACE_ACTIVITY_UNKNOWN', 'Tenant gateway is unavailable'); + const response = await this.fetcher(new URL('/internal/lifecycle-state', gateway.internalUrl), { + method: 'POST', + headers: { authorization: `Bearer ${gateway.token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ tenantId: workspace.tenantId, workspaceId: workspace.id }), + redirect: 'error', + signal: AbortSignal.timeout(15000), + }); + if (!response.ok) throw new DomainError('WORKSPACE_ACTIVITY_UNKNOWN', 'Workspace activity cannot be established'); + const value = (await response.json()) as SleepState; + if ( + value.workspaceId !== workspace.id || + value.known !== true || + value.gatewayConnections !== 0 || + value.activeClients !== 0 || + value.terminals !== 0 || + value.debugSessions !== 0 || + value.dirty !== false || + value.snapshotComplete !== true || + !Number.isSafeInteger(value.snapshotRevision) || + value.snapshotRevision < 1 || + !/^[a-f0-9]{64}$/.test(value.diskDigest) + ) + throw new DomainError( + 'WORKSPACE_NOT_IDLE', + 'Workspace has active or unknown work, or lacks a complete saved snapshot', + ); + return value; + } + async scheduleIdle(tenantId: string) { + if (this.env.WF_WORKSPACE_AUTO_SLEEP === 'false') return; + try { + await this.configured(tenantId); + } catch { + return; + } + const cutoff = Date.now() - WORKSPACE_IDLE_MS; + const candidates = await this.store.transaction(tenantId, async tx => + (await tx.list('workspace')).filter( + w => + w.managedBy === 'wishforge-provisioner' && + w.status === 'ready' && + typeof w.lastActivityAt === 'string' && + Date.parse(w.lastActivityAt) <= cutoff, + ), + ); + for (const before of candidates) { + try { + const receipt = await this.safety(before); + await this.store.transaction(tenantId, async tx => { + const w = await must(tx, 'workspace', before.id); + if ( + w.status !== 'ready' || + w.lastActivityAt !== before.lastActivityAt || + Date.parse(String(w.lastActivityAt)) > cutoff + ) + return; + await this.queue(tx, w, 'paused', true, receipt.diskDigest); + }); + } catch { + /* Unknown or active work is never automatically stopped. */ + } + } + } + async reconcile(tenantId: string, item: OutboxItem) { + const workspace = await this.store.transaction(tenantId, tx => + must(tx, 'workspace', String(item.payload.workspaceId)), + ); + const operation = workspace.lifecycle as Lifecycle | undefined; + if (!operation || operation.operationId !== item.id || operation.revision !== item.payload.revision) { + await this.store.transaction(tenantId, tx => tx.acknowledge(item.id)); + return; + } + try { + const { provisioner } = await this.configured(tenantId); + if (operation.automatic && Date.now() - Date.parse(operation.requestedAt) < 5000) { + await this.store.transaction(tenantId, tx => tx.defer(item.id, 5)); + return; + } + const target = new URL(`${provisioner.url}/workspaces/${workspace.id}/lifecycle`); + target.searchParams.set('tenantId', tenantId); + target.searchParams.set('projectId', String(workspace.projectId)); + const headers = { authorization: `Bearer ${provisioner.token}`, 'content-type': 'application/json' }; + // Always observe first. A lost response never causes an unconditional mutation replay. + const observed = await this.fetcher(target, { headers, redirect: 'error', signal: AbortSignal.timeout(15000) }); + if (!observed.ok) throw new Error('WORKSPACE_OBSERVE_UNKNOWN'); + let state = (await observed.json()) as { workspaceId: string; revision: number; desired: string; status: string }; + const valid = () => + state.workspaceId === workspace.id && + Number.isSafeInteger(state.revision) && + state.revision >= 0 && + ['running', 'paused'].includes(state.desired) && + ['ready', 'paused', 'pending'].includes(state.status); + if (!valid() || state.revision > operation.revision) throw new Error('WORKSPACE_OBSERVE_UNKNOWN'); + if (state.revision < operation.revision && operation.automatic) { + try { + const receipt = await this.safety(workspace); + if ( + receipt.diskDigest !== operation.snapshotDigest || + Date.now() - Date.parse(String(workspace.lastActivityAt)) < WORKSPACE_IDLE_MS + ) + throw new Error('ACTIVITY_CHANGED'); + } catch { + await this.store.transaction(tenantId, async tx => { + const current = await must(tx, 'workspace', workspace.id); + if ((current.lifecycle as Lifecycle | undefined)?.operationId !== item.id) return; + await this.queue(tx, current, 'running', false); + await tx.acknowledge(item.id); + }); + return; + } + } + if (state.revision < operation.revision) { + await this.store.transaction(tenantId, async tx => { + const current = await must(tx, 'workspace', workspace.id); + if ((current.lifecycle as Lifecycle | undefined)?.operationId !== item.id) + throw new Error('WORKSPACE_SUPERSEDED'); + if (operation.automatic && current.lastActivityAt !== workspace.lastActivityAt) + throw new Error('WORKSPACE_ACTIVITY_CHANGED'); + current.lifecycle = { ...operation, state: 'unknown' }; + await tx.put('workspace', touch(current)); + }); + target.search = ''; + const changed = await this.fetcher(target, { + method: 'PUT', + headers, + body: JSON.stringify({ + tenantId, + projectId: workspace.projectId, + workspaceId: workspace.id, + revision: operation.revision, + desired: operation.desired, + }), + redirect: 'error', + signal: AbortSignal.timeout(15000), + }); + if (!changed.ok) throw new Error('WORKSPACE_RECONCILE_UNKNOWN'); + state = (await changed.json()) as typeof state; + } + if (!valid() || state.revision !== operation.revision || state.desired !== operation.desired) + throw new Error('WORKSPACE_OBSERVE_UNKNOWN'); + await this.store.transaction(tenantId, async tx => { + const current = await must(tx, 'workspace', workspace.id); + if ((current.lifecycle as Lifecycle | undefined)?.operationId !== item.id) return; + const complete = + (operation.desired === 'paused' && state.status === 'paused') || + (operation.desired === 'running' && state.status === 'ready'); + current.lifecycle = { ...operation, state: complete ? 'complete' : 'observing' }; + current.status = complete ? state.status : operation.desired === 'paused' ? 'pausing' : 'resuming'; + current.reason = complete ? undefined : 'Waiting for observed workspace replicas and readiness.'; + if (complete && state.status === 'ready') current.lastActivityAt = new Date().toISOString(); + await tx.put('workspace', touch(current)); + if (complete) await tx.acknowledge(item.id); + else await tx.defer(item.id, 10); + }); + } catch { + await this.store.transaction(tenantId, async tx => { + const current = await must(tx, 'workspace', workspace.id); + if ((current.lifecycle as Lifecycle | undefined)?.operationId !== item.id) return; + current.status = 'unknown'; + current.reason = 'Workspace controller outcome is unknown; observation will be retried before any new action.'; + current.lifecycle = { ...operation, state: 'unknown' }; + await tx.put('workspace', touch(current)); + await tx.defer(item.id, 30); + }); + } + } } diff --git a/apps/api/src/http.ts b/apps/api/src/http.ts index 36b51eb..f881f37 100644 --- a/apps/api/src/http.ts +++ b/apps/api/src/http.ts @@ -1,5 +1,21 @@ -import {resolveGatewayTarget} from './domain/gateway-target.js'; -import { Body, Controller, Delete, Get, HttpCode, Inject, Param, Patch, Post, Query, Req, Res, type ExceptionFilter, Catch, type ArgumentsHost } from '@nestjs/common'; +import { resolveGatewayTarget } from './domain/gateway-target.js'; +import { + Body, + Controller, + Delete, + Get, + HttpCode, + Inject, + Param, + Patch, + Post, + Query, + Req, + Res, + type ExceptionFilter, + Catch, + type ArgumentsHost, +} from '@nestjs/common'; import type { Request, Response } from 'express'; import { z, ZodError } from 'zod'; import { DomainError, type Actor, type Entity, type Project, type Run, type Release } from '@wishforge/contracts'; @@ -19,8 +35,20 @@ import { requireRole, requireHuman } from './domain/policy.js'; import { configuredVerificationProfiles, selectVerificationProfile } from './domain/verification-policy.js'; export class Services { - auth: Authenticator; releases: Releases; evolution: Evolution; connections: Connections; runners: RunnerBroker; github: GitHubIntegration; - constructor(readonly control: ControlPlane) { this.auth = new Authenticator(control.store, control.env); this.releases = new Releases(control.store, control.env); this.evolution = new Evolution(control.store, control.env); this.connections = new Connections(control.store, control.env); this.runners = new RunnerBroker(control.store, control, this.releases, control.env); this.github = new GitHubIntegration(control.store, control.env); } + auth: Authenticator; + releases: Releases; + evolution: Evolution; + connections: Connections; + runners: RunnerBroker; + github: GitHubIntegration; + constructor(readonly control: ControlPlane) { + this.auth = new Authenticator(control.store, control.env); + this.releases = new Releases(control.store, control.env); + this.evolution = new Evolution(control.store, control.env); + this.connections = new Connections(control.store, control.env); + this.runners = new RunnerBroker(control.store, control, this.releases, control.env); + this.github = new GitHubIntegration(control.store, control.env); + } } type AuthRequest = Request & { actor: Actor }; const ok = (data: T) => ({ data }); @@ -31,10 +59,18 @@ export class Errors implements ExceptionFilter { catch(error: unknown, host: ArgumentsHost) { const res = host.switchToHttp().getResponse(); if (res.headersSent) return res.end(); - if (error instanceof ZodError) return res.status(400).json({ error: { code: 'INVALID_INPUT', message: 'Request validation failed', details: error.flatten() } }); - if (error instanceof DomainError) return res.status(error.status).json({ error: { code: error.code, message: error.message, details: error.details } }); + if (error instanceof ZodError) + return res + .status(400) + .json({ error: { code: 'INVALID_INPUT', message: 'Request validation failed', details: error.flatten() } }); + if (error instanceof DomainError) + return res + .status(error.status) + .json({ error: { code: error.code, message: error.message, details: error.details } }); console.error(error instanceof Error ? `${error.name}: ${error.message}` : 'Internal request failure'); - return res.status(500).json({ error: { code: 'INTERNAL_ERROR', message: 'Request failed. Consult the server audit log.' } }); + return res + .status(500) + .json({ error: { code: 'INTERNAL_ERROR', message: 'Request failed. Consult the server audit log.' } }); } } @@ -42,134 +78,685 @@ export class Errors implements ExceptionFilter { export class ApiController { constructor(@Inject(Services) private s: Services) {} @Get('dashboard') async dashboard(@Req() req: AuthRequest) { - const a = req.actor; const [projects, runs, releases, providers, evolution, usage] = await Promise.all([this.s.control.projects(a), this.s.control.runs(a), this.s.releases.list(a), this.s.control.providers(a), this.s.evolution.list(a), this.s.control.usage(a)]); - return ok({ projects, runs, releases, providers, evolution, usage, system: { mode: this.s.control.env.NODE_ENV === 'production' ? 'production' : 'development', region: this.s.control.env.WF_REGION ?? 'unconfigured', authMode: this.s.control.env.WF_DEV_AUTH_TOKEN ? 'local-development' : 'oidc' } }); + const a = req.actor; + const [projects, runs, releases, providers, evolution, usage] = await Promise.all([ + this.s.control.projects(a), + this.s.control.runs(a), + this.s.releases.list(a), + this.s.control.providers(a), + this.s.evolution.list(a), + this.s.control.usage(a), + ]); + return ok({ + projects, + runs, + releases, + providers, + evolution, + usage, + system: { + mode: this.s.control.env.NODE_ENV === 'production' ? 'production' : 'development', + region: this.s.control.env.WF_REGION ?? 'unconfigured', + authMode: this.s.control.env.WF_DEV_AUTH_TOKEN ? 'local-development' : 'oidc', + }, + }); } - @Get('projects') async projects(@Req() r: AuthRequest) { return ok(await this.s.control.projects(r.actor)); } - @Post('projects') async createProject(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.control.createProject(r.actor, b)); } - @Get('verification-profiles') async verificationProfiles() { return ok(configuredVerificationProfiles(this.s.control.env).map(({ id, version }) => ({ id, version }))); } - @Post('projects/:id/verification-profile') async verificationProfile(@Req() r: AuthRequest, @Param('id') id: string, @Body() body: unknown) { - requireRole(r.actor, ['owner', 'admin']); requireHuman(r.actor); - const { profileId } = z.object({ profileId: z.string().min(1).max(80) }).strict().parse(body); + @Get('projects') async projects(@Req() r: AuthRequest) { + return ok(await this.s.control.projects(r.actor)); + } + @Post('projects') async createProject(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.control.createProject(r.actor, b)); + } + @Get('verification-profiles') async verificationProfiles() { + return ok(configuredVerificationProfiles(this.s.control.env).map(({ id, version }) => ({ id, version }))); + } + @Post('projects/:id/verification-profile') async verificationProfile( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + requireRole(r.actor, ['owner', 'admin']); + requireHuman(r.actor); + const { profileId } = z + .object({ profileId: z.string().min(1).max(80) }) + .strict() + .parse(body); const profile = selectVerificationProfile(profileId, this.s.control.env); - return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { const project = await projectAccess(tx, r.actor, id); const next = { ...touch(project), verificationProfile: profile }; await tx.put('project', next); return next; })); + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + const project = await projectAccess(tx, r.actor, id); + const next = { ...touch(project), verificationProfile: profile }; + await tx.put('project', next); + return next; + }), + ); + } + @Post('projects/:id/repository') async provisionRepository(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.control.provisionRepository(r.actor, id)); + } + @Get('projects/:id/github') async githubStatus(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.github.status(r.actor, id)); + } + @Post('runs/:id/github/pull-requests') async githubPr( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.github.createPullRequest(r.actor, id, b)); + } + @Get('github/operations/:id') async githubRead(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.github.readPullRequest(r.actor, id)); + } + @Post('github/operations/:id/authorize-merge') async githubAuthorize( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { expectedHeadSha: string; expectedBaseSha: string }, + ) { + return ok(await this.s.github.authorizeMerge(r.actor, id, b)); + } + @Post('github/operations/:id/merge') async githubMerge(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.github.merge(r.actor, id)); + } + @Get('projects/:id') async project(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.control.store.transaction(r.actor.tenantId, tx => projectAccess(tx, r.actor, id))); } - @Post('projects/:id/repository') async provisionRepository(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.control.provisionRepository(r.actor, id)); } - @Get('projects/:id/github') async githubStatus(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.github.status(r.actor, id)); } - @Post('runs/:id/github/pull-requests') async githubPr(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.github.createPullRequest(r.actor, id, b)); } - @Get('github/operations/:id') async githubRead(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.github.readPullRequest(r.actor, id)); } - @Post('github/operations/:id/authorize-merge') async githubAuthorize(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { expectedHeadSha: string; expectedBaseSha: string }) { return ok(await this.s.github.authorizeMerge(r.actor, id, b)); } - @Post('github/operations/:id/merge') async githubMerge(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.github.merge(r.actor, id)); } - @Get('projects/:id') async project(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.control.store.transaction(r.actor.tenantId, tx => projectAccess(tx, r.actor, id))); } @Patch('projects/:id') async settings(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { - requireRole(r.actor, ['owner', 'admin']); requireHuman(r.actor); - const data = z.object({ canaryOptIn: z.boolean().optional(), verificationCommands: z.array(z.array(z.string().min(1)).min(1)).max(20).optional(), memberIds: z.array(z.string().min(1)).optional(), deliveryConfig: z.object({ imageRepository: z.string().min(1), architecture: z.enum(['linux/amd64', 'linux/arm64']), environmentId: z.string().min(1), dockerfile: z.string().default('Dockerfile'), sourceContext: z.string().default('.'), targetBranch: z.string().optional(), dockerContext: z.string().optional(), containerPort: z.number().int().min(1).max(65535), healthPath: z.string().startsWith('/'), smokeChecks: z.array(z.object({ path: z.string().startsWith('/'), expectedStatus: z.number().int().min(100).max(599), bodyIncludes: z.string().optional() })).min(1) }).strict().optional() }).strict().parse(b); - return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { const p = await projectAccess(tx, r.actor, id); const next = { ...touch(p), ...data }; await tx.put('project', next); return next; })); + requireRole(r.actor, ['owner', 'admin']); + requireHuman(r.actor); + const data = z + .object({ + canaryOptIn: z.boolean().optional(), + verificationCommands: z + .array(z.array(z.string().min(1)).min(1)) + .max(20) + .optional(), + memberIds: z.array(z.string().min(1)).optional(), + deliveryConfig: z + .object({ + imageRepository: z.string().min(1), + architecture: z.enum(['linux/amd64', 'linux/arm64']), + environmentId: z.string().min(1), + dockerfile: z.string().default('Dockerfile'), + sourceContext: z.string().default('.'), + targetBranch: z.string().optional(), + dockerContext: z.string().optional(), + containerPort: z.number().int().min(1).max(65535), + healthPath: z.string().startsWith('/'), + smokeChecks: z + .array( + z.object({ + path: z.string().startsWith('/'), + expectedStatus: z.number().int().min(100).max(599), + bodyIncludes: z.string().optional(), + }), + ) + .min(1), + }) + .strict() + .optional(), + }) + .strict() + .parse(b); + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + const p = await projectAccess(tx, r.actor, id); + const next = { ...touch(p), ...data }; + await tx.put('project', next); + return next; + }), + ); } - @Post('projects/:id/wishes') @HttpCode(202) async submit(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.control.submit(r.actor, id, b, key(r))); } - @Get('runs') async runs(@Req() r: AuthRequest) { return ok(await this.s.control.runs(r.actor)); } - @Get('runs/:id') async run(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.control.run(r.actor, id)); } - @Get('runs/:id/events') async events(@Req() r: AuthRequest, @Param('id') id: string, @Query('after') after?: string) { return ok(await this.s.control.events(r.actor, id, Math.max(0, Number(after) || 0))); } - @Get('runs/:id/events/stream') async stream(@Req() r: AuthRequest, @Res() res: Response, @Param('id') id: string, @Query('after') after?: string) { + @Post('projects/:id/wishes') @HttpCode(202) async submit( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.control.submit(r.actor, id, b, key(r))); + } + @Get('runs') async runs(@Req() r: AuthRequest) { + return ok(await this.s.control.runs(r.actor)); + } + @Get('runs/:id') async run(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.control.run(r.actor, id)); + } + @Get('runs/:id/events') async events(@Req() r: AuthRequest, @Param('id') id: string, @Query('after') after?: string) { + return ok(await this.s.control.events(r.actor, id, Math.max(0, Number(after) || 0))); + } + @Get('runs/:id/events/stream') async stream( + @Req() r: AuthRequest, + @Res() res: Response, + @Param('id') id: string, + @Query('after') after?: string, + ) { await this.s.control.run(r.actor, id); - res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-store', 'Connection': 'keep-alive', 'X-Accel-Buffering': 'no' }); res.flushHeaders(); - let cursor = Math.max(0, Number(r.header('Last-Event-ID') ?? after) || 0), busy = false; - const pump = async () => { if (busy || res.writableEnded) return; busy = true; try { const actor = await this.s.auth.authenticate(r.header('Authorization')); for (const e of await this.s.control.events(actor, id, cursor)) { res.write(`id: ${e.seq}\nevent: run\ndata: ${JSON.stringify(e)}\n\n`); cursor = e.seq; } res.write(': heartbeat\n\n'); } catch { res.end(); } finally { busy = false; } }; - const timer = setInterval(() => void pump(), 2000); r.on('close', () => clearInterval(timer)); await pump(); + res.set({ + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'no-store', + Connection: 'keep-alive', + 'X-Accel-Buffering': 'no', + }); + res.flushHeaders(); + let cursor = Math.max(0, Number(r.header('Last-Event-ID') ?? after) || 0), + busy = false; + const pump = async () => { + if (busy || res.writableEnded) return; + busy = true; + try { + const actor = await this.s.auth.authenticate(r.header('Authorization')); + for (const e of await this.s.control.events(actor, id, cursor)) { + res.write(`id: ${e.seq}\nevent: run\ndata: ${JSON.stringify(e)}\n\n`); + cursor = e.seq; + } + res.write(': heartbeat\n\n'); + } catch { + res.end(); + } finally { + busy = false; + } + }; + const timer = setInterval(() => void pump(), 2000); + r.on('close', () => clearInterval(timer)); + await pump(); + } + @Post('runs/:id/commands') async command(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { + return ok(await this.s.control.command(r.actor, id, b, key(r))); + } + @Get('runs/:id/artifacts') async artifacts(@Req() r: AuthRequest, @Param('id') id: string) { + const { evidence, checkpoint, release } = await this.s.releases.delivery(r.actor, id); + return ok({ evidence, checkpoint, release }); + } + @Get('runs/:id/delivery') async delivery(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.releases.delivery(r.actor, id)); + } + @Get('providers') async providers(@Req() r: AuthRequest) { + return ok(await this.s.control.providers(r.actor)); + } + @Get('artifacts/:id') async artifactMetadata(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.control.artifacts.metadata(r.actor, id)); + } + @Get('artifacts/:id/download') async artifactDownload( + @Req() r: AuthRequest, + @Param('id') id: string, + @Res() res: Response, + ) { + const { metadata, body } = await this.s.control.artifacts.download(r.actor, id); + res.set({ + 'Content-Type': metadata.contentType, + 'Content-Length': String(body.length), + 'Content-Disposition': `attachment; filename="${metadata.sha256}.bundle"`, + 'X-Content-Type-Options': 'nosniff', + 'Cache-Control': 'no-store', + }); + res.send(body); + } + @Get('provider-connections') async connections(@Req() r: AuthRequest) { + return ok(await this.s.connections.list(r.actor)); + } + @Post('provider-connections') async connect(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.connections.create(r.actor, b)); + } + @Delete('provider-connections/:id') async revoke(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.connections.revoke(r.actor, id)); + } + @Get('environments') async envs(@Req() r: AuthRequest) { + return ok(await this.s.releases.environments(r.actor)); + } + @Patch('environments/:id/access') async environmentAccess( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + const input = z + .object({ applicationUrl: z.string().max(2048).nullable(), visibility: z.enum(['private', 'public']) }) + .strict() + .parse(body); + return ok(await this.s.releases.updateAccess(r.actor, id, input)); + } + @Patch('environments/:id/deployment-secrets') async environmentSecrets( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + const input = z + .object({ + secretVersionRefs: z.array(z.string().min(1).max(256)).max(32), + deploymentSecretPolicyDigest: z + .string() + .regex(/^sha256:[a-f0-9]{64}$/) + .nullable(), + }) + .strict() + .parse(body); + return ok(await this.s.releases.updateDeploymentSecrets(r.actor, id, input)); + } + @Post('environments') async env(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.releases.createEnvironment(r.actor, b)); + } + @Post('environments/:id/runners') async runner(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.runners.register(r.actor, id)); + } + @Get('releases') async releases(@Req() r: AuthRequest) { + return ok(await this.s.releases.list(r.actor)); + } + @Post('releases') async release(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.releases.request(r.actor, b, key(r))); + } + @Post('approvals/:id/decision') async decision( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { decision: string; bindingDigest: string }, + ) { + return ok(await this.s.releases.decide(r.actor, id, b)); + } + @Post('releases/:id/rollback') async rollback(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.releases.rollback(r.actor, id, key(r))); + } + @Get('evolution') async evolution(@Req() r: AuthRequest) { + return ok(await this.s.evolution.list(r.actor)); + } + @Get('evolution/bundle') async activeBundle(@Req() r: AuthRequest) { + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + const routing = await tx.get('policy', 'evolution-routing-v1'); + const stableVersion = String(routing?.stableVersion ?? 'baseline-v1'); + return { stableVersion, config: await resolveBundle(tx, stableVersion) }; + }), + ); + } + @Post('evolution') async candidate(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.evolution.create(r.actor, b)); + } + @Post('evolution/tick') async metaTick(@Req() r: AuthRequest) { + return ok(await new MetaEvolution(this.s.control).tick(r.actor)); + } + @Post('evolution/:id/commands') async evolve( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { action: string }, + ) { + return ok(await this.s.evolution.command(r.actor, id, b.action)); + } + @Get('entitlements') async entitlements(@Req() r: AuthRequest) { + return ok(await new Entitlements(this.s.control.store, this.s.control.env).view(r.actor)); + } + @Get('billing/plans') async billingPlans(@Req() r: AuthRequest) { + return ok(await new Entitlements(this.s.control.store, this.s.control.env).plans(r.actor)); + } + @Post('billing/members') async inviteMember(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await new Entitlements(this.s.control.store, this.s.control.env).inviteMember(r.actor, b, key(r))); + } + @Post('billing/executions/:id/reconcile') async reconcileResources( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok( + await new Entitlements(this.s.control.store, this.s.control.env).reconcileAllocation(r.actor, id, b, key(r)), + ); + } + @Get('billing/usage') async billingUsage(@Req() r: AuthRequest, @Query('period') period?: string) { + return ok(await new Billing(this.s.control.store, this.s.control.env).usage(r.actor, period)); + } + @Get('billing/invoices/:period') async billingInvoice(@Req() r: AuthRequest, @Param('period') period: string) { + return ok(await new Billing(this.s.control.store, this.s.control.env).invoice(r.actor, period)); + } + @Get('billing/invoices/:period/export') async billingExport( + @Req() r: AuthRequest, + @Param('period') period: string, + @Res() res: Response, + ) { + const exported = await new Billing(this.s.control.store, this.s.control.env).exportCsv(r.actor, period); + res.type('text/csv').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`); + res.send(exported.content); + } + @Post('billing/invoices/:period/settlement') async billingSettlement( + @Req() r: AuthRequest, + @Param('period') period: string, + @Body() b: unknown, + ) { + return ok(await new Billing(this.s.control.store, this.s.control.env).settle(r.actor, period, b, key(r))); + } + @Get('usage') async usage(@Req() r: AuthRequest) { + return ok(await this.s.control.usage(r.actor)); + } + @Get('usage/export') async exportUsage(@Req() r: AuthRequest, @Res() res: Response) { + const exported = await new Billing(this.s.control.store, this.s.control.env).exportCsv(r.actor); + res.type('text/csv').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`); + res.send(exported.content); + } + @Get('projects/:id/workspaces') async workspaces(@Req() r: AuthRequest, @Param('id') id: string) { + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + await projectAccess(tx, r.actor, id); + return (await tx.list('workspace')) + .filter(w => w.projectId === id) + .map(({ upstreamUrl: _, collaborationUpstreamUrl: _collaboration, ...publicValue }) => publicValue); + }), + ); } - @Post('runs/:id/commands') async command(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.control.command(r.actor, id, b, key(r))); } - @Get('runs/:id/artifacts') async artifacts(@Req() r: AuthRequest, @Param('id') id: string) { const {evidence,checkpoint,release}=await this.s.releases.delivery(r.actor,id);return ok({evidence,checkpoint,release}); } - @Get('runs/:id/delivery') async delivery(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.releases.delivery(r.actor,id)); } - @Get('providers') async providers(@Req() r: AuthRequest) { return ok(await this.s.control.providers(r.actor)); } - @Get('artifacts/:id') async artifactMetadata(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.control.artifacts.metadata(r.actor, id)); } - @Get('artifacts/:id/download') async artifactDownload(@Req() r: AuthRequest, @Param('id') id: string, @Res() res: Response) { const { metadata, body } = await this.s.control.artifacts.download(r.actor, id); res.set({ 'Content-Type': metadata.contentType, 'Content-Length': String(body.length), 'Content-Disposition': `attachment; filename="${metadata.sha256}.bundle"`, 'X-Content-Type-Options': 'nosniff', 'Cache-Control': 'no-store' }); res.send(body); } - @Get('provider-connections') async connections(@Req() r: AuthRequest) { return ok(await this.s.connections.list(r.actor)); } - @Post('provider-connections') async connect(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.connections.create(r.actor, b)); } - @Delete('provider-connections/:id') async revoke(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.connections.revoke(r.actor, id)); } - @Get('environments') async envs(@Req() r: AuthRequest) { return ok(await this.s.releases.environments(r.actor)); } - @Patch('environments/:id/access') async environmentAccess(@Req() r:AuthRequest,@Param('id') id:string,@Body() body:unknown){const input=z.object({applicationUrl:z.string().max(2048).nullable(),visibility:z.enum(['private','public'])}).strict().parse(body);return ok(await this.s.releases.updateAccess(r.actor,id,input));} - @Patch('environments/:id/deployment-secrets') async environmentSecrets(@Req()r:AuthRequest,@Param('id')id:string,@Body()body:unknown){const input=z.object({secretVersionRefs:z.array(z.string().min(1).max(256)).max(32),deploymentSecretPolicyDigest:z.string().regex(/^sha256:[a-f0-9]{64}$/).nullable()}).strict().parse(body);return ok(await this.s.releases.updateDeploymentSecrets(r.actor,id,input));} - @Post('environments') async env(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.releases.createEnvironment(r.actor, b)); } - @Post('environments/:id/runners') async runner(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.runners.register(r.actor, id)); } - @Get('releases') async releases(@Req() r: AuthRequest) { return ok(await this.s.releases.list(r.actor)); } - @Post('releases') async release(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.releases.request(r.actor, b, key(r))); } - @Post('approvals/:id/decision') async decision(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { decision: string; bindingDigest: string }) { return ok(await this.s.releases.decide(r.actor, id, b)); } - @Post('releases/:id/rollback') async rollback(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.releases.rollback(r.actor, id, key(r))); } - @Get('evolution') async evolution(@Req() r: AuthRequest) { return ok(await this.s.evolution.list(r.actor)); } - @Get('evolution/bundle') async activeBundle(@Req() r: AuthRequest) { return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { const routing = await tx.get('policy', 'evolution-routing-v1'); const stableVersion = String(routing?.stableVersion ?? 'baseline-v1'); return { stableVersion, config: await resolveBundle(tx, stableVersion) }; })); } - @Post('evolution') async candidate(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.evolution.create(r.actor, b)); } - @Post('evolution/tick') async metaTick(@Req() r: AuthRequest) { return ok(await new MetaEvolution(this.s.control).tick(r.actor)); } - @Post('evolution/:id/commands') async evolve(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { action: string }) { return ok(await this.s.evolution.command(r.actor, id, b.action)); } - @Get('entitlements') async entitlements(@Req() r: AuthRequest) { return ok(await new Entitlements(this.s.control.store, this.s.control.env).view(r.actor)); } - @Get('billing/plans') async billingPlans(@Req() r: AuthRequest) { return ok(await new Entitlements(this.s.control.store, this.s.control.env).plans(r.actor)); } - @Post('billing/members') async inviteMember(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await new Entitlements(this.s.control.store, this.s.control.env).inviteMember(r.actor, b, key(r))); } - @Post('billing/executions/:id/reconcile') async reconcileResources(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await new Entitlements(this.s.control.store, this.s.control.env).reconcileAllocation(r.actor, id, b, key(r))); } - @Get('billing/usage') async billingUsage(@Req() r: AuthRequest, @Query('period') period?: string) { return ok(await new Billing(this.s.control.store, this.s.control.env).usage(r.actor, period)); } - @Get('billing/invoices/:period') async billingInvoice(@Req() r: AuthRequest, @Param('period') period: string) { return ok(await new Billing(this.s.control.store, this.s.control.env).invoice(r.actor, period)); } - @Get('billing/invoices/:period/export') async billingExport(@Req() r: AuthRequest, @Param('period') period: string, @Res() res: Response) { const exported = await new Billing(this.s.control.store, this.s.control.env).exportCsv(r.actor, period); res.type('text/csv').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`); res.send(exported.content); } - @Post('billing/invoices/:period/settlement') async billingSettlement(@Req() r: AuthRequest, @Param('period') period: string, @Body() b: unknown) { return ok(await new Billing(this.s.control.store, this.s.control.env).settle(r.actor, period, b, key(r))); } - @Get('usage') async usage(@Req() r: AuthRequest) { return ok(await this.s.control.usage(r.actor)); } - @Get('usage/export') async exportUsage(@Req() r: AuthRequest, @Res() res: Response) { const exported = await new Billing(this.s.control.store, this.s.control.env).exportCsv(r.actor); res.type('text/csv').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`); res.send(exported.content); } - @Get('projects/:id/workspaces') async workspaces(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { await projectAccess(tx, r.actor, id); return (await tx.list('workspace')).filter(w => w.projectId === id).map(({ upstreamUrl: _, collaborationUpstreamUrl: _collaboration, ...publicValue }) => publicValue); })); } @Post('projects/:id/workspaces') async workspace(@Req() r: AuthRequest, @Param('id') id: string) { requireRole(r.actor, ['owner', 'admin', 'developer']); - return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { await projectAccess(tx, r.actor, id); const existing = (await tx.list('workspace')).find(w => w.projectId === id); if (existing) return existing; await requireCapacity(tx, 'workspace'); const workspace = { ...record(r.actor.tenantId), projectId: id, status: 'unconfigured', reason: 'An isolated Theia workspace provisioner and authenticated gateway must be connected before this workspace can be opened.' }; await tx.put('workspace', workspace); await tx.enqueue('workspace.provision', { workspaceId: workspace.id, projectId: id }); return workspace; })); + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + await projectAccess(tx, r.actor, id); + const existing = (await tx.list('workspace')).find(w => w.projectId === id); + if (existing) return existing; + await requireCapacity(tx, 'workspace'); + const workspace = { + ...record(r.actor.tenantId), + projectId: id, + status: 'unconfigured', + reason: + 'An isolated Theia workspace provisioner and authenticated gateway must be connected before this workspace can be opened.', + }; + await tx.put('workspace', workspace); + await tx.enqueue('workspace.provision', { workspaceId: workspace.id, projectId: id }); + return workspace; + }), + ); } } @Controller('internal') export class InternalController { constructor(@Inject(Services) private s: Services) {} - @Post('runs/:id/github/state') async githubState(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number }) { return ok(await this.s.github.workerState(r.actor, id, b.leaseToken)); } - @Post('runs/:id/github/create') async githubCreate(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.github.workerCreate(r.actor, id, b)); } - @Post('runs/:id/github/advance') async githubAdvance(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.github.workerAdvance(r.actor, id, b)); } - @Get('runner/tasks') async runnerTask(@Req() r: AuthRequest, @Query('environmentId') environmentId: string) { return this.s.runners.poll(r.actor, environmentId); } - @Post('runner/tasks/:id/result') async runnerResult(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { result: { releaseId: string; status: string; target: string; evidence: Record } }) { return this.s.runners.result(r.actor, id, b); } - @Post('runs/:id/lease') async lease(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { workerId: string; generation?: string }) { if (!b.workerId) throw new DomainError('INVALID_WORKER', 'workerId required', 400); return ok(await this.s.control.lease(r.actor, id, b.workerId, b.generation)); } - @Post('runs/:id/lease/release') async releaseLease(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number }) { return ok(await this.s.control.releaseLease(r.actor, id, b.leaseToken)); } - @Post('runs/:id/heartbeat') async heartbeat(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number }) { return ok(await this.s.control.heartbeat(r.actor, id, b.leaseToken)); } - @Post('runs/:id/wait') async wait(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { reason: string; generation?: string }) { return ok(await this.s.control.waitRun(r.actor, id, b.reason, b.generation)); } - @Get('runs/:id/state') async workflowState(@Req() r: AuthRequest, @Param('id') id: string) { if (r.actor.kind !== 'worker' || r.actor.runId) throw new DomainError('FORBIDDEN', 'Trusted coordinator identity required', 403); return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { const run = await must(tx, 'run', id); return { status: run.status, generation: run.executionGeneration, generatedEvolutionCandidateId: run.generatedEvolutionCandidateId }; })); } - @Post('runs/:id/transition') async transition(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.control.transition(r.actor, id, b)); } - @Get('runs/:id/context') async context(@Req() r: AuthRequest, @Param('id') id: string) { const context = await this.s.control.context(r.actor, id); const [providerCredential, gitCredential] = await Promise.all([this.s.connections.forRun(r.actor, context.run), context.project.repositoryStorage === 'platform' ? new GitStorage(this.s.control.env).credential(r.actor, context.project, context.run) : this.s.github.app.configured() ? this.s.github.credential(r.actor, context.project, context.run) : undefined]); return ok({ ...context, providerCredential, gitCredential }); } - @Post('runs/:id/checkpoint') async checkpoint(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number; checkpoint: Record; workspace?: string }) { return ok(await this.s.control.checkpoint(r.actor, id, b.checkpoint, b.leaseToken, b.workspace)); } - @Post('runs/:id/classification') async classification(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number; sourceCommit: string; changedPaths: string[]; migration: boolean; permissionsExpanded: boolean }) { return ok(await this.s.control.classify(r.actor, id, b)); } - @Post('runs/:id/events') async events(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number; event: Record }) { return ok(await this.s.control.providerEvent(r.actor, id, b.leaseToken, b.event)); } - @Post('runs/:id/meter') async meter(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { operationId: string; tokens: number; computeSeconds: number; leaseToken: number }) { return ok(await this.s.control.meter(r.actor, id, b)); } - @Post('evidence') async evidence(@Req() r: AuthRequest, @Body() b: unknown) { return ok(await this.s.control.evidence(r.actor, b)); } - @Post('runs/:id/evolution-code-candidate') async extractedCodeCandidate(@Req() r: AuthRequest, @Param('id') id: string, @Body() body: unknown) { + @Post('runs/:id/github/state') async githubState( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number }, + ) { + return ok(await this.s.github.workerState(r.actor, id, b.leaseToken)); + } + @Post('runs/:id/github/create') async githubCreate( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.github.workerCreate(r.actor, id, b)); + } + @Post('runs/:id/github/advance') async githubAdvance( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.github.workerAdvance(r.actor, id, b)); + } + @Get('runner/tasks') async runnerTask(@Req() r: AuthRequest, @Query('environmentId') environmentId: string) { + return this.s.runners.poll(r.actor, environmentId); + } + @Post('runner/tasks/:id/result') async runnerResult( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { result: { releaseId: string; status: string; target: string; evidence: Record } }, + ) { + return this.s.runners.result(r.actor, id, b); + } + @Post('runs/:id/lease') async lease( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { workerId: string; generation?: string }, + ) { + if (!b.workerId) throw new DomainError('INVALID_WORKER', 'workerId required', 400); + return ok(await this.s.control.lease(r.actor, id, b.workerId, b.generation)); + } + @Post('runs/:id/lease/release') async releaseLease( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number }, + ) { + return ok(await this.s.control.releaseLease(r.actor, id, b.leaseToken)); + } + @Post('runs/:id/heartbeat') async heartbeat( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number }, + ) { + return ok(await this.s.control.heartbeat(r.actor, id, b.leaseToken)); + } + @Post('runs/:id/wait') async wait( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { reason: string; generation?: string }, + ) { + return ok(await this.s.control.waitRun(r.actor, id, b.reason, b.generation)); + } + @Get('runs/:id/state') async workflowState(@Req() r: AuthRequest, @Param('id') id: string) { + if (r.actor.kind !== 'worker' || r.actor.runId) + throw new DomainError('FORBIDDEN', 'Trusted coordinator identity required', 403); + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + const run = await must(tx, 'run', id); + return { + status: run.status, + generation: run.executionGeneration, + generatedEvolutionCandidateId: run.generatedEvolutionCandidateId, + }; + }), + ); + } + @Post('runs/:id/transition') async transition(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { + return ok(await this.s.control.transition(r.actor, id, b)); + } + @Get('runs/:id/context') async context(@Req() r: AuthRequest, @Param('id') id: string) { + const context = await this.s.control.context(r.actor, id); + const [providerCredential, gitCredential] = await Promise.all([ + this.s.connections.forRun(r.actor, context.run), + context.project.repositoryStorage === 'platform' + ? new GitStorage(this.s.control.env).credential(r.actor, context.project, context.run) + : this.s.github.app.configured() + ? this.s.github.credential(r.actor, context.project, context.run) + : undefined, + ]); + return ok({ ...context, providerCredential, gitCredential }); + } + @Post('runs/:id/checkpoint') async checkpoint( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number; checkpoint: Record; workspace?: string }, + ) { + return ok(await this.s.control.checkpoint(r.actor, id, b.checkpoint, b.leaseToken, b.workspace)); + } + @Post('runs/:id/classification') async classification( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() + b: { + leaseToken: number; + sourceCommit: string; + changedPaths: string[]; + migration: boolean; + permissionsExpanded: boolean; + }, + ) { + return ok(await this.s.control.classify(r.actor, id, b)); + } + @Post('runs/:id/events') async events( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number; event: Record }, + ) { + return ok(await this.s.control.providerEvent(r.actor, id, b.leaseToken, b.event)); + } + @Post('runs/:id/meter') async meter( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { operationId: string; tokens: number; computeSeconds: number; leaseToken: number }, + ) { + return ok(await this.s.control.meter(r.actor, id, b)); + } + @Post('evidence') async evidence(@Req() r: AuthRequest, @Body() b: unknown) { + return ok(await this.s.control.evidence(r.actor, b)); + } + @Post('runs/:id/evolution-code-candidate') async extractedCodeCandidate( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { const { leaseToken } = z.object({ leaseToken: z.number().int().positive() }).strict().parse(body); const candidate = await this.s.evolution.createCodeCandidate(r.actor, id, leaseToken); - return ok({ candidateId: candidate.id, candidateDigest: candidate.candidateDigest, scope: candidate.scope, status: candidate.status }); + return ok({ + candidateId: candidate.id, + candidateDigest: candidate.candidateDigest, + scope: candidate.scope, + status: candidate.status, + }); } - @Post('runs/:id/evolution-candidate') async extractedCandidate(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { leaseToken: number; bundleConfig: unknown }) { + @Post('runs/:id/evolution-candidate') async extractedCandidate( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { leaseToken: number; bundleConfig: unknown }, + ) { const config = bundleConfigSchema.parse(b.bundleConfig); const run = await this.s.control.store.transaction(r.actor.tenantId, tx => must(tx, 'run', id)); - const candidate = await this.s.evolution.create(r.actor, { name: `Verified improvement ${id.slice(0, 8)}`, hypothesis: run.goal.slice(0, 4000), scope: run.evolutionScope, parentBundle: run.baselineBundle, candidateDigest: bundleDigest(config), targetMetric: 'success', dataScope: 'platform', bundleConfig: config }, { runId: id, leaseToken: b.leaseToken }); + const candidate = await this.s.evolution.create( + r.actor, + { + name: `Verified improvement ${id.slice(0, 8)}`, + hypothesis: run.goal.slice(0, 4000), + scope: run.evolutionScope, + parentBundle: run.baselineBundle, + candidateDigest: bundleDigest(config), + targetMetric: 'success', + dataScope: 'platform', + bundleConfig: config, + }, + { runId: id, leaseToken: b.leaseToken }, + ); return ok({ candidateId: candidate.id, candidateDigest: candidate.candidateDigest, status: candidate.status }); } - @Post('releases') async release(@Req() r: AuthRequest, @Body() b: unknown) { if (r.actor.kind !== 'deployer') throw new DomainError('FORBIDDEN', 'Deployer required', 403); return ok(await this.s.releases.request(r.actor, b, key(r))); } - @Post('releases/:id/begin') async begin(@Req() r: AuthRequest, @Param('id') id: string) { return ok(await this.s.releases.begin(r.actor, id)); } - @Post('releases/:id/accept') async accept(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { evidenceId: string; bindingDigest: string }) { return ok(await this.s.releases.accept(r.actor, id, b)); } - @Post('releases/:id/failure') async failure(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { status: 'failed' | 'unknown'; message: string }) { if (!['failed', 'unknown'].includes(b.status)) throw new DomainError('INVALID_STATUS', 'Failure status required', 400); return ok(await this.s.releases.reportFailure(r.actor, id, b.status, b.message)); } - @Post('evolution/:id/evaluation') async evaluation(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.evolution.evaluate(r.actor, id, b)); } - @Post('evolution/rollout/tick') async rolloutTick(@Req() r: AuthRequest) { return ok(await this.s.evolution.rolloutTick(r.actor)); } - @Get('evolution/external-actions') async externalEvolutionActions(@Req() r: AuthRequest) { - if (r.actor.kind !== 'evaluator' || r.actor.tenantId !== this.s.control.env.WF_SYSTEM_TENANT) throw new DomainError('FORBIDDEN', 'Independent platform release operator required', 403); - return ok((await this.s.evolution.list(r.actor)).filter(c => c.externalAction?.status === 'requested').map(c => ({ candidateId: c.id, candidateDigest: c.candidateDigest, action: c.externalAction, sourceArtifact: c.sourceArtifact }))); + @Post('releases') async release(@Req() r: AuthRequest, @Body() b: unknown) { + if (r.actor.kind !== 'deployer') throw new DomainError('FORBIDDEN', 'Deployer required', 403); + return ok(await this.s.releases.request(r.actor, b, key(r))); } - @Post('evolution/tick') async systemMetaTick(@Req() r: AuthRequest) { if (r.actor.kind !== 'evaluator') throw new DomainError('FORBIDDEN', 'System evaluator identity required', 403); return ok(await new MetaEvolution(this.s.control).tick(r.actor)); } - @Post('evolution/:id/observations') async observe(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) { return ok(await this.s.evolution.observe(r.actor, id, b)); } - @Post('workspaces/:id/authorize') async authorizeWorkspace(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { userToken: string; operation: string }) { - if (r.actor.userId !== 'workspace-gateway') throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403); + @Post('releases/:id/begin') async begin(@Req() r: AuthRequest, @Param('id') id: string) { + return ok(await this.s.releases.begin(r.actor, id)); + } + @Post('releases/:id/accept') async accept( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { evidenceId: string; bindingDigest: string }, + ) { + return ok(await this.s.releases.accept(r.actor, id, b)); + } + @Post('releases/:id/failure') async failure( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { status: 'failed' | 'unknown'; message: string }, + ) { + if (!['failed', 'unknown'].includes(b.status)) + throw new DomainError('INVALID_STATUS', 'Failure status required', 400); + return ok(await this.s.releases.reportFailure(r.actor, id, b.status, b.message)); + } + @Post('evolution/:id/evaluation') async evaluation( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.evolution.evaluate(r.actor, id, b)); + } + @Post('evolution/rollout/tick') async rolloutTick(@Req() r: AuthRequest) { + return ok(await this.s.evolution.rolloutTick(r.actor)); + } + @Get('evolution/external-actions') async externalEvolutionActions(@Req() r: AuthRequest) { + if (r.actor.kind !== 'evaluator' || r.actor.tenantId !== this.s.control.env.WF_SYSTEM_TENANT) + throw new DomainError('FORBIDDEN', 'Independent platform release operator required', 403); + return ok( + (await this.s.evolution.list(r.actor)) + .filter(c => c.externalAction?.status === 'requested') + .map(c => ({ + candidateId: c.id, + candidateDigest: c.candidateDigest, + action: c.externalAction, + sourceArtifact: c.sourceArtifact, + })), + ); + } + @Post('evolution/tick') async systemMetaTick(@Req() r: AuthRequest) { + if (r.actor.kind !== 'evaluator') throw new DomainError('FORBIDDEN', 'System evaluator identity required', 403); + return ok(await new MetaEvolution(this.s.control).tick(r.actor)); + } + @Post('evolution/:id/observations') async observe( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: unknown, + ) { + return ok(await this.s.evolution.observe(r.actor, id, b)); + } + @Post('workspaces/:id/authorize') async authorizeWorkspace( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { userToken: string; operation: string }, + ) { + if (r.actor.userId !== 'workspace-gateway') + throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403); const user = await this.s.auth.authenticate(`Bearer ${b.userToken}`); if (user.tenantId !== r.actor.tenantId) throw new DomainError('NOT_FOUND', 'Workspace not found', 404); - return ok(await this.s.control.store.transaction(user.tenantId, async tx => { const workspace = await must(tx, 'workspace', id); await projectAccess(tx, user, String(workspace.projectId)); if (['edit', 'terminal', 'debug'].includes(b.operation)){requireHuman(user);requireRole(user, ['owner', 'admin', 'developer']);} if (workspace.status !== 'ready' || !workspace.upstreamUrl) throw new DomainError('WORKSPACE_NOT_READY', 'Workspace has not been provisioned', 503); if(r.actor.userId==='workspace-gateway'&&(!workspace.lastActivityAt||Date.now()-Date.parse(String(workspace.lastActivityAt))>=15000)){workspace.lastActivityAt=new Date().toISOString();await tx.put('workspace',workspace);} return { tenantId: user.tenantId, projectId: workspace.projectId, workspaceId: id, upstreamUrl: workspace.upstreamUrl, collaborationUpstreamUrl: workspace.collaborationUpstreamUrl, userId: user.userId, role: user.role }; })); + return ok( + await this.s.control.store.transaction(user.tenantId, async tx => { + const workspace = await must(tx, 'workspace', id); + await projectAccess(tx, user, String(workspace.projectId)); + if (['edit', 'terminal', 'debug'].includes(b.operation)) { + requireHuman(user); + requireRole(user, ['owner', 'admin', 'developer']); + } + if (workspace.status !== 'ready' || !workspace.upstreamUrl) + throw new DomainError('WORKSPACE_NOT_READY', 'Workspace has not been provisioned', 503); + if ( + r.actor.userId === 'workspace-gateway' && + (!workspace.lastActivityAt || Date.now() - Date.parse(String(workspace.lastActivityAt)) >= 15000) + ) { + workspace.lastActivityAt = new Date().toISOString(); + await tx.put('workspace', workspace); + } + return { + tenantId: user.tenantId, + projectId: workspace.projectId, + workspaceId: id, + upstreamUrl: workspace.upstreamUrl, + collaborationUpstreamUrl: workspace.collaborationUpstreamUrl, + userId: user.userId, + role: user.role, + }; + }), + ); } - @Post('workspaces/:id/provisioned') async provisioned(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: { upstreamUrl: string; collaborationUpstreamUrl?: string }) { + @Post('workspaces/:id/provisioned') async provisioned( + @Req() r: AuthRequest, + @Param('id') id: string, + @Body() b: { upstreamUrl: string; collaborationUpstreamUrl?: string }, + ) { if (r.actor.kind !== 'worker') throw new DomainError('FORBIDDEN', 'Workspace controller required', 403); - let collaboration: URL | undefined; if (b.collaborationUpstreamUrl) { collaboration = new URL(b.collaborationUpstreamUrl); if (!['http:', 'https:'].includes(collaboration.protocol) || collaboration.username || collaboration.password || collaboration.pathname !== '/' || collaboration.search || collaboration.hash) throw new DomainError('INVALID_COLLABORATION_UPSTREAM', 'Invalid workspace collaboration backend', 400); } const upstream = new URL(b.upstreamUrl); if (!['http:', 'https:'].includes(upstream.protocol) || upstream.username || upstream.password) throw new DomainError('INVALID_UPSTREAM', 'Invalid workspace backend', 400); - const base = (await resolveGatewayTarget(this.s.control.env,r.actor.tenantId))?.publicUrl; if (!base) throw new DomainError('GATEWAY_UNCONFIGURED', 'Authenticated workspace gateway is not configured', 503); - return ok(await this.s.control.store.transaction(r.actor.tenantId, async tx => { const w = await must(tx, 'workspace', id); if(w.lifecycle)throw new DomainError('WORKSPACE_LIFECYCLE_OWNED','Lifecycle controller must reconcile this workspace'); w.status = 'ready'; w.upstreamUrl = upstream.toString(); if (collaboration) w.collaborationUpstreamUrl = collaboration.toString(); w.url = `${base.replace(/\/$/, '')}/workspaces/${id}/`; w.reason = undefined; await tx.put('workspace', touch(w)); return { id, status: w.status, url: w.url }; })); + let collaboration: URL | undefined; + if (b.collaborationUpstreamUrl) { + collaboration = new URL(b.collaborationUpstreamUrl); + if ( + !['http:', 'https:'].includes(collaboration.protocol) || + collaboration.username || + collaboration.password || + collaboration.pathname !== '/' || + collaboration.search || + collaboration.hash + ) + throw new DomainError('INVALID_COLLABORATION_UPSTREAM', 'Invalid workspace collaboration backend', 400); + } + const upstream = new URL(b.upstreamUrl); + if (!['http:', 'https:'].includes(upstream.protocol) || upstream.username || upstream.password) + throw new DomainError('INVALID_UPSTREAM', 'Invalid workspace backend', 400); + const base = (await resolveGatewayTarget(this.s.control.env, r.actor.tenantId))?.publicUrl; + if (!base) throw new DomainError('GATEWAY_UNCONFIGURED', 'Authenticated workspace gateway is not configured', 503); + return ok( + await this.s.control.store.transaction(r.actor.tenantId, async tx => { + const w = await must(tx, 'workspace', id); + if (w.lifecycle) + throw new DomainError('WORKSPACE_LIFECYCLE_OWNED', 'Lifecycle controller must reconcile this workspace'); + w.status = 'ready'; + w.upstreamUrl = upstream.toString(); + if (collaboration) w.collaborationUpstreamUrl = collaboration.toString(); + w.url = `${base.replace(/\/$/, '')}/workspaces/${id}/`; + w.reason = undefined; + await tx.put('workspace', touch(w)); + return { id, status: w.status, url: w.url }; + }), + ); } } diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 565854a..2a219b4 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -1,4 +1,4 @@ -import {WorkspaceLaunchController} from './workspace-launch-http.js'; +import { WorkspaceLaunchController } from './workspace-launch-http.js'; import 'reflect-metadata'; import { Module } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; @@ -13,59 +13,173 @@ import { auditContext } from './audit-context.js'; import { observability } from './telemetry.js'; import { ProviderProxy } from './domain/provider-proxy.js'; import { once } from 'node:events'; -import {WorkspaceLifecycleController,WorkspaceLifecycleInternalController} from './workspace-lifecycle-http.js'; -import {WorkspaceLifecycleWorker} from './workspace-lifecycle-worker.js'; +import { WorkspaceLifecycleController, WorkspaceLifecycleInternalController } from './workspace-lifecycle-http.js'; +import { WorkspaceLifecycleWorker } from './workspace-lifecycle-worker.js'; import { WorkspaceInputController, WorkspaceIntegrationController } from './workspace-integration-http.js'; import { PreviewController, PreviewControlController, PreviewProbeController } from './preview-http.js'; import { ResourceReconciliationWorker } from './resource-reconciliation-worker.js'; export async function bootstrap() { // Migration credentials belong to operator tools, never the running application. - delete process.env.DATABASE_ADMIN_URL; delete process.env.WF_DATABASE_APP_PASSWORD; + delete process.env.DATABASE_ADMIN_URL; + delete process.env.WF_DATABASE_APP_PASSWORD; if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is required. Run setup:local, db:migrate and db:seed.'); - const store = new PgStore(process.env.DATABASE_URL); await store.assertSafeRole(); + const store = new PgStore(process.env.DATABASE_URL); + await store.assertSafeRole(); const service = new Services(new ControlPlane(store)); const telemetry = await observability(); - @Module({ controllers: [ApiController, InternalController, WorkspaceInputController, WorkspaceIntegrationController, WorkspaceLifecycleController, WorkspaceLifecycleInternalController, WorkspaceLaunchController, PreviewController, PreviewControlController, PreviewProbeController], providers: [{ provide: Services, useValue: service }] }) + @Module({ + controllers: [ + ApiController, + InternalController, + WorkspaceInputController, + WorkspaceIntegrationController, + WorkspaceLifecycleController, + WorkspaceLifecycleInternalController, + WorkspaceLaunchController, + PreviewController, + PreviewControlController, + PreviewProbeController, + ], + providers: [{ provide: Services, useValue: service }], + }) class AppModule {} const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log'], bodyParser: false }); - app.use(express.json({ limit: '8mb' })); app.useGlobalFilters(new Errors()); - const raw = app.getHttpAdapter().getInstance(); raw.disable('x-powered-by'); + app.use(express.json({ limit: '8mb' })); + app.useGlobalFilters(new Errors()); + const raw = app.getHttpAdapter().getInstance(); + raw.disable('x-powered-by'); raw.use(telemetry.middleware); raw.get('/metrics', telemetry.metrics); - raw.get('/health', (_req: express.Request, res: express.Response) => res.json({ status: 'ok', service: 'wishforge-control-plane', version: '0.1.0' })); - raw.get('/readyz', async (_req: express.Request, res: express.Response) => { try { await store.pool.query('SELECT 1'); res.json({ status: 'ready' }); } catch { res.status(503).json({ status: 'unavailable' }); } }); - raw.get('/.well-known/oauth-protected-resource', (_req: express.Request, res: express.Response) => res.json({ resource: `${process.env.WF_API_PUBLIC_URL ?? 'http://127.0.0.1:4000'}/mcp`, authorization_servers: process.env.OIDC_ISSUER ? [process.env.OIDC_ISSUER] : [], scopes_supported: ['openid', 'profile', 'wishforge'] })); - const authError = (error: unknown, res: express.Response) => { const known = error instanceof DomainError; res.status(known ? error.status : 500).json({ error: { code: known ? error.code : 'AUTH_UNAVAILABLE', message: known ? error.message : 'Authentication is unavailable' } }); }; + raw.get('/health', (_req: express.Request, res: express.Response) => + res.json({ status: 'ok', service: 'wishforge-control-plane', version: '0.1.0' }), + ); + raw.get('/readyz', async (_req: express.Request, res: express.Response) => { + try { + await store.pool.query('SELECT 1'); + res.json({ status: 'ready' }); + } catch { + res.status(503).json({ status: 'unavailable' }); + } + }); + raw.get('/.well-known/oauth-protected-resource', (_req: express.Request, res: express.Response) => + res.json({ + resource: `${process.env.WF_API_PUBLIC_URL ?? 'http://127.0.0.1:4000'}/mcp`, + authorization_servers: process.env.OIDC_ISSUER ? [process.env.OIDC_ISSUER] : [], + scopes_supported: ['openid', 'profile', 'wishforge'], + }), + ); + const authError = (error: unknown, res: express.Response) => { + const known = error instanceof DomainError; + res.status(known ? error.status : 500).json({ + error: { + code: known ? error.code : 'AUTH_UNAVAILABLE', + message: known ? error.message : 'Authentication is unavailable', + }, + }); + }; // This route uses a separate token audience and never accepts a normal OIDC or internal-worker credential. raw.all('/provider-proxy/v1/:resource', async (req: express.Request, res: express.Response) => { - const abort = new AbortController(); res.once('close', () => { if (!res.writableEnded) abort.abort(); }); + const abort = new AbortController(); + res.once('close', () => { + if (!res.writableEnded) abort.abort(); + }); try { - const upstream = await new ProviderProxy(store).forward({ authorization: req.header('Authorization'), method: req.method, path: `/${req.params.resource}`, headers: Object.fromEntries(Object.entries(req.headers).filter((entry): entry is [string, string] => typeof entry[1] === 'string')), body: req.method === 'GET' ? undefined : Buffer.from(JSON.stringify(req.body ?? null)), signal: abort.signal }); - res.status(upstream.status); upstream.headers.forEach((value, name) => res.setHeader(name, value)); res.flushHeaders(); - if (upstream.body) { const reader = upstream.body.getReader(); try { while (!abort.signal.aborted) { const part = await reader.read(); if (part.done) break; if (!res.write(Buffer.from(part.value))) await once(res, 'drain', { signal: abort.signal }); } } finally { await reader.cancel().catch(() => undefined); } } + const upstream = await new ProviderProxy(store).forward({ + authorization: req.header('Authorization'), + method: req.method, + path: `/${req.params.resource}`, + headers: Object.fromEntries( + Object.entries(req.headers).filter((entry): entry is [string, string] => typeof entry[1] === 'string'), + ), + body: req.method === 'GET' ? undefined : Buffer.from(JSON.stringify(req.body ?? null)), + signal: abort.signal, + }); + res.status(upstream.status); + upstream.headers.forEach((value, name) => res.setHeader(name, value)); + res.flushHeaders(); + if (upstream.body) { + const reader = upstream.body.getReader(); + try { + while (!abort.signal.aborted) { + const part = await reader.read(); + if (part.done) break; + if (!res.write(Buffer.from(part.value))) await once(res, 'drain', { signal: abort.signal }); + } + } finally { + await reader.cancel().catch(() => undefined); + } + } res.end(); - } catch (error) { if (!res.headersSent) authError(error, res); else res.destroy(); } - }); - raw.use('/v1', async (req: express.Request, res: express.Response, next: express.NextFunction) => { try { const actor = await service.auth.authenticate(req.header('Authorization')); (req as express.Request & { actor: unknown }).actor = actor; res.setHeader('Cache-Control', 'no-store'); auditContext.run({ actorId: actor.userId }, next); } catch (error) { authError(error, res); } }); - raw.use('/internal', async (req: express.Request, res: express.Response, next: express.NextFunction) => { try { - const actor = await service.auth.internal(req.header('Authorization'), req.header('X-WishForge-Tenant')); - if (actor.runId) { - const addressed = /^\/runs\/([^/]+)\//.exec(req.path)?.[1] ?? (req.path === '/evidence' ? req.body?.runId : undefined); - if (addressed !== actor.runId) throw new DomainError('FORBIDDEN', 'Execution token is scoped to a single run', 403); + } catch (error) { + if (!res.headersSent) authError(error, res); + else res.destroy(); } - if (actor.userId === 'workspace-gateway' && !/^\/workspaces\/[^/]+\/(authorize|integration-authorize|lifecycle-authorize)$/.test(req.path)) throw new DomainError('FORBIDDEN', 'Gateway identity is limited to workspace authorization', 403); - if (actor.userId.startsWith('runner:') && !req.path.startsWith('/runner/')) throw new DomainError('FORBIDDEN', 'Runner identity is limited to assigned deployment tasks', 403); - (req as express.Request & { actor: unknown }).actor = actor; res.setHeader('Cache-Control', 'no-store'); auditContext.run({ actorId: actor.userId }, next); - } catch (error) { authError(error, res); } }); - raw.post('/mcp', async (req: express.Request, res: express.Response) => { try { await handleMcp(service, req, res); } catch (error) { if (!res.headersSent) { res.setHeader('WWW-Authenticate', `Bearer resource_metadata="${process.env.WF_API_PUBLIC_URL ?? 'http://127.0.0.1:4000'}/.well-known/oauth-protected-resource"`); authError(error, res); } } }); + }); + raw.use('/v1', async (req: express.Request, res: express.Response, next: express.NextFunction) => { + try { + const actor = await service.auth.authenticate(req.header('Authorization')); + (req as express.Request & { actor: unknown }).actor = actor; + res.setHeader('Cache-Control', 'no-store'); + auditContext.run({ actorId: actor.userId }, next); + } catch (error) { + authError(error, res); + } + }); + raw.use('/internal', async (req: express.Request, res: express.Response, next: express.NextFunction) => { + try { + const actor = await service.auth.internal(req.header('Authorization'), req.header('X-WishForge-Tenant')); + if (actor.runId) { + const addressed = + /^\/runs\/([^/]+)\//.exec(req.path)?.[1] ?? (req.path === '/evidence' ? req.body?.runId : undefined); + if (addressed !== actor.runId) + throw new DomainError('FORBIDDEN', 'Execution token is scoped to a single run', 403); + } + if ( + actor.userId === 'workspace-gateway' && + !/^\/workspaces\/[^/]+\/(authorize|integration-authorize|lifecycle-authorize)$/.test(req.path) + ) + throw new DomainError('FORBIDDEN', 'Gateway identity is limited to workspace authorization', 403); + if (actor.userId.startsWith('runner:') && !req.path.startsWith('/runner/')) + throw new DomainError('FORBIDDEN', 'Runner identity is limited to assigned deployment tasks', 403); + (req as express.Request & { actor: unknown }).actor = actor; + res.setHeader('Cache-Control', 'no-store'); + auditContext.run({ actorId: actor.userId }, next); + } catch (error) { + authError(error, res); + } + }); + raw.post('/mcp', async (req: express.Request, res: express.Response) => { + try { + await handleMcp(service, req, res); + } catch (error) { + if (!res.headersSent) { + res.setHeader( + 'WWW-Authenticate', + `Bearer resource_metadata="${process.env.WF_API_PUBLIC_URL ?? 'http://127.0.0.1:4000'}/.well-known/oauth-protected-resource"`, + ); + authError(error, res); + } + } + }); raw.all('/mcp', (_req: express.Request, res: express.Response) => res.status(405).setHeader('Allow', 'POST').end()); await app.listen(Number(process.env.PORT ?? 4000), process.env.HOST ?? '127.0.0.1'); - const workspaceLifecycle=new WorkspaceLifecycleWorker(service.control);if(process.env.WF_DISPATCH_ENABLED!=='false')workspaceLifecycle.start(); - const dispatcher = new Dispatcher(service.control); if (process.env.WF_DISPATCH_ENABLED !== 'false') void dispatcher.start(); - const resourceReconciliation = new ResourceReconciliationWorker(service.control); if (process.env.WF_DISPATCH_ENABLED !== 'false') resourceReconciliation.start(); - const shutdown = async () => { await resourceReconciliation.close(); await workspaceLifecycle.close(); await dispatcher.close(); await app.close(); await store.close(); await telemetry.close(); }; - process.once('SIGTERM', () => void shutdown()); process.once('SIGINT', () => void shutdown()); + const workspaceLifecycle = new WorkspaceLifecycleWorker(service.control); + if (process.env.WF_DISPATCH_ENABLED !== 'false') workspaceLifecycle.start(); + const dispatcher = new Dispatcher(service.control); + if (process.env.WF_DISPATCH_ENABLED !== 'false') void dispatcher.start(); + const resourceReconciliation = new ResourceReconciliationWorker(service.control); + if (process.env.WF_DISPATCH_ENABLED !== 'false') resourceReconciliation.start(); + const shutdown = async () => { + await resourceReconciliation.close(); + await workspaceLifecycle.close(); + await dispatcher.close(); + await app.close(); + await store.close(); + await telemetry.close(); + }; + process.once('SIGTERM', () => void shutdown()); + process.once('SIGINT', () => void shutdown()); return { app, service, shutdown }; } await bootstrap(); diff --git a/apps/api/src/mcp.ts b/apps/api/src/mcp.ts index 2c2f119..5fce7cb 100644 --- a/apps/api/src/mcp.ts +++ b/apps/api/src/mcp.ts @@ -8,17 +8,104 @@ import type { Services } from './http.js'; export async function handleMcp(s: Services, req: Request, res: Response) { const authenticated = await s.auth.authenticate(req.header('Authorization')); const actor = { ...authenticated, kind: 'connector' as const }; - const server = new McpServer({ name: 'wishforge', version: '0.1.0' }, { instructions: 'Submit a wish with explicit acceptance criteria and reuse its idempotency_key when retrying. Tasks continue after this call returns. Use run_id to inspect status and answer questions. Agent completion is not verified delivery. Human release approvals must be completed at the returned web URL.' }); - const result = async (fn: () => Promise) => { try { const data = await fn(); return { content: [{ type: 'text' as const, text: JSON.stringify({ data }) }] }; } catch (error) { return { isError: true, content: [{ type: 'text' as const, text: JSON.stringify({ error: { code: error instanceof DomainError ? error.code : 'TOOL_ERROR', message: error instanceof Error ? error.message : 'Tool failed' } }) }] }; } }; - server.registerTool('submit_wish', { description: 'Submit a versioned wish; returns quickly with wish_id and run_id.', inputSchema: { project_id: z.string(), idempotency_key: z.string().min(8), ...wishInput.shape } }, ({ project_id, idempotency_key, ...input }) => result(() => s.control.submit(actor, project_id, input, idempotency_key))); - server.registerTool('get_run', { description: 'Read task stage, status and waiting reason.', inputSchema: { run_id: z.string() } }, ({ run_id }) => result(() => s.control.run(actor, run_id))); - server.registerTool('get_run_events', { description: 'Read persisted progress events after a cursor.', inputSchema: { run_id: z.string(), cursor: z.number().int().nonnegative().default(0) } }, ({ run_id, cursor }) => result(() => s.control.events(actor, run_id, cursor))); - server.registerTool('answer_question', { description: 'Add a requirement revision; invalidates obsolete verification and approval.', inputSchema: { run_id: z.string(), answer: z.string().min(1), idempotency_key: z.string().min(8) } }, ({ run_id, answer, idempotency_key }) => result(() => s.control.command(actor, run_id, { action: 'answer', message: answer }, idempotency_key))); - server.registerTool('control_run', { description: 'Pause, resume or cancel future execution. Cancel does not roll back a healthy live release.', inputSchema: { run_id: z.string(), action: z.enum(['pause', 'resume', 'cancel']), idempotency_key: z.string().min(8) } }, ({ run_id, action, idempotency_key }) => result(() => s.control.command(actor, run_id, { action }, idempotency_key))); - server.registerTool('get_delivery', { description: 'Read verified release and evidence, if available.', inputSchema: { run_id: z.string() } }, ({ run_id }) => result(async () => { return {run_id,...await s.releases.delivery(actor,run_id)}; })); - server.registerTool('request_release', { description: 'Request publication of trusted build evidence. High-risk publication requires an interactive human approval.', inputSchema: { ...releaseInput.shape, idempotency_key: z.string().min(8) } }, ({ idempotency_key, ...input }) => result(async () => { const release = await s.releases.request(actor, input, idempotency_key); return { release, approval_url: release.approvalId ? `${s.control.env.WF_PUBLIC_URL}/releases` : null }; })); + const server = new McpServer( + { name: 'wishforge', version: '0.1.0' }, + { + instructions: + 'Submit a wish with explicit acceptance criteria and reuse its idempotency_key when retrying. Tasks continue after this call returns. Use run_id to inspect status and answer questions. Agent completion is not verified delivery. Human release approvals must be completed at the returned web URL.', + }, + ); + const result = async (fn: () => Promise) => { + try { + const data = await fn(); + return { content: [{ type: 'text' as const, text: JSON.stringify({ data }) }] }; + } catch (error) { + return { + isError: true, + content: [ + { + type: 'text' as const, + text: JSON.stringify({ + error: { + code: error instanceof DomainError ? error.code : 'TOOL_ERROR', + message: error instanceof Error ? error.message : 'Tool failed', + }, + }), + }, + ], + }; + } + }; + server.registerTool( + 'submit_wish', + { + description: 'Submit a versioned wish; returns quickly with wish_id and run_id.', + inputSchema: { project_id: z.string(), idempotency_key: z.string().min(8), ...wishInput.shape }, + }, + ({ project_id, idempotency_key, ...input }) => + result(() => s.control.submit(actor, project_id, input, idempotency_key)), + ); + server.registerTool( + 'get_run', + { description: 'Read task stage, status and waiting reason.', inputSchema: { run_id: z.string() } }, + ({ run_id }) => result(() => s.control.run(actor, run_id)), + ); + server.registerTool( + 'get_run_events', + { + description: 'Read persisted progress events after a cursor.', + inputSchema: { run_id: z.string(), cursor: z.number().int().nonnegative().default(0) }, + }, + ({ run_id, cursor }) => result(() => s.control.events(actor, run_id, cursor)), + ); + server.registerTool( + 'answer_question', + { + description: 'Add a requirement revision; invalidates obsolete verification and approval.', + inputSchema: { run_id: z.string(), answer: z.string().min(1), idempotency_key: z.string().min(8) }, + }, + ({ run_id, answer, idempotency_key }) => + result(() => s.control.command(actor, run_id, { action: 'answer', message: answer }, idempotency_key)), + ); + server.registerTool( + 'control_run', + { + description: 'Pause, resume or cancel future execution. Cancel does not roll back a healthy live release.', + inputSchema: { + run_id: z.string(), + action: z.enum(['pause', 'resume', 'cancel']), + idempotency_key: z.string().min(8), + }, + }, + ({ run_id, action, idempotency_key }) => + result(() => s.control.command(actor, run_id, { action }, idempotency_key)), + ); + server.registerTool( + 'get_delivery', + { description: 'Read verified release and evidence, if available.', inputSchema: { run_id: z.string() } }, + ({ run_id }) => + result(async () => { + return { run_id, ...(await s.releases.delivery(actor, run_id)) }; + }), + ); + server.registerTool( + 'request_release', + { + description: + 'Request publication of trusted build evidence. High-risk publication requires an interactive human approval.', + inputSchema: { ...releaseInput.shape, idempotency_key: z.string().min(8) }, + }, + ({ idempotency_key, ...input }) => + result(async () => { + const release = await s.releases.request(actor, input, idempotency_key); + return { release, approval_url: release.approvalId ? `${s.control.env.WF_PUBLIC_URL}/releases` : null }; + }), + ); const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined, enableJsonResponse: true }); await server.connect(transport); - res.on('close', () => { void transport.close(); void server.close(); }); + res.on('close', () => { + void transport.close(); + void server.close(); + }); await transport.handleRequest(req, res, req.body); } diff --git a/apps/api/src/preview-http.ts b/apps/api/src/preview-http.ts index b428191..4d6d590 100644 --- a/apps/api/src/preview-http.ts +++ b/apps/api/src/preview-http.ts @@ -1,23 +1,41 @@ -import {Body,Controller,Inject,Param,Post,Req} from '@nestjs/common'; -import type {Request} from 'express'; -import type {Actor} from '@wishforge/contracts'; -import {z} from 'zod'; -import {Services} from './http.js'; -import {Previews} from './domain/previews.js'; -type AuthRequest=Request&{actor:Actor}; +import { Body, Controller, Inject, Param, Post, Req } from '@nestjs/common'; +import type { Request } from 'express'; +import type { Actor } from '@wishforge/contracts'; +import { z } from 'zod'; +import { Services } from './http.js'; +import { Previews } from './domain/previews.js'; +type AuthRequest = Request & { actor: Actor }; @Controller('v1/environments') -export class PreviewController{ - constructor(@Inject(Services)private s:Services){} - @Post(':id/preview-launch')async launch(@Req()req:AuthRequest,@Param('id')id:string){return{data:await new Previews(this.s.control.store,this.s.control.env).launch(req.actor,id)};} +export class PreviewController { + constructor(@Inject(Services) private s: Services) {} + @Post(':id/preview-launch') async launch(@Req() req: AuthRequest, @Param('id') id: string) { + return { data: await new Previews(this.s.control.store, this.s.control.env).launch(req.actor, id) }; + } } /** This path has its own environment-scoped service authentication, not OIDC or worker auth. */ @Controller('preview-control') -export class PreviewControlController{ - constructor(@Inject(Services)private s:Services){} - @Post('authorize')async authorize(@Req()req:Request,@Body()body:unknown){return{data:await new Previews(this.s.control.store,this.s.control.env).authorize(req.header('Authorization'),body)};} +export class PreviewControlController { + constructor(@Inject(Services) private s: Services) {} + @Post('authorize') async authorize(@Req() req: Request, @Body() body: unknown) { + return { + data: await new Previews(this.s.control.store, this.s.control.env).authorize(req.header('Authorization'), body), + }; + } } @Controller('internal/runner/releases') -export class PreviewProbeController{ - constructor(@Inject(Services)private s:Services){} - @Post(':id/preview-probe')async probe(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){const input=z.object({taskId:z.string().regex(/^[A-Za-z0-9_-]{1,128}$/)}).strict().parse(body);return{data:await new Previews(this.s.control.store,this.s.control.env).issueProbeForRunner(req.actor,id,input.taskId)};} +export class PreviewProbeController { + constructor(@Inject(Services) private s: Services) {} + @Post(':id/preview-probe') async probe(@Req() req: AuthRequest, @Param('id') id: string, @Body() body: unknown) { + const input = z + .object({ taskId: z.string().regex(/^[A-Za-z0-9_-]{1,128}$/) }) + .strict() + .parse(body); + return { + data: await new Previews(this.s.control.store, this.s.control.env).issueProbeForRunner( + req.actor, + id, + input.taskId, + ), + }; + } } diff --git a/apps/api/src/resource-reconciliation-worker.ts b/apps/api/src/resource-reconciliation-worker.ts index 6b30e43..b2b3cc6 100644 --- a/apps/api/src/resource-reconciliation-worker.ts +++ b/apps/api/src/resource-reconciliation-worker.ts @@ -10,16 +10,27 @@ export class ResourceReconciliationWorker { start() { const tick = async () => { if (this.stopped) return; - this.busy = this.tick(); await this.busy; + this.busy = this.tick(); + await this.busy; if (!this.stopped) this.timer = setTimeout(() => void tick(), 10000); }; void tick(); } async tick() { - for (const tenant of (this.control.env.WF_DISPATCH_TENANTS ?? '').split(',').map(value => value.trim()).filter(Boolean)) { - try { await releaseStoppedAllocations(this.control, tenant); } - catch { /* Keep the durable reservation on any missing or unknown result. */ } + for (const tenant of (this.control.env.WF_DISPATCH_TENANTS ?? '') + .split(',') + .map(value => value.trim()) + .filter(Boolean)) { + try { + await releaseStoppedAllocations(this.control, tenant); + } catch { + /* Keep the durable reservation on any missing or unknown result. */ + } } } - async close() { this.stopped = true; if (this.timer) clearTimeout(this.timer); await this.busy; } + async close() { + this.stopped = true; + if (this.timer) clearTimeout(this.timer); + await this.busy; + } } diff --git a/apps/api/src/service-tokens.ts b/apps/api/src/service-tokens.ts index 42aeb9e..20f6c82 100644 --- a/apps/api/src/service-tokens.ts +++ b/apps/api/src/service-tokens.ts @@ -2,10 +2,47 @@ import { SignJWT, jwtVerify } from 'jose'; import { DomainError, type Actor } from '@wishforge/contracts'; export class ServiceTokens { constructor(private secret: string | undefined) {} - private key() { if (!this.secret || this.secret.length < 32) throw new DomainError('INTERNAL_SIGNER_UNCONFIGURED', 'Configure the internal token signing secret', 503); return new TextEncoder().encode(this.secret); } - async issue(tenantId: string, runId: string, kind: 'worker' | 'verifier', generation: string) { return new SignJWT({ tenant_id: tenantId, run_id: runId, kind, generation }).setProtectedHeader({ alg: 'HS256' }).setIssuer('wishforge-control-plane').setAudience('wishforge-internal').setSubject(`${kind}:${runId}:${generation}`).setIssuedAt().setExpirationTime('4h').sign(this.key()); } + private key() { + if (!this.secret || this.secret.length < 32) + throw new DomainError('INTERNAL_SIGNER_UNCONFIGURED', 'Configure the internal token signing secret', 503); + return new TextEncoder().encode(this.secret); + } + async issue(tenantId: string, runId: string, kind: 'worker' | 'verifier', generation: string) { + return new SignJWT({ tenant_id: tenantId, run_id: runId, kind, generation }) + .setProtectedHeader({ alg: 'HS256' }) + .setIssuer('wishforge-control-plane') + .setAudience('wishforge-internal') + .setSubject(`${kind}:${runId}:${generation}`) + .setIssuedAt() + .setExpirationTime('4h') + .sign(this.key()); + } async verify(token: string): Promise { if (!token.startsWith('eyJ') || !this.secret) return undefined; - try { const { payload } = await jwtVerify(token, this.key(), { issuer: 'wishforge-control-plane', audience: 'wishforge-internal', algorithms: ['HS256'] }); if (!payload.sub || typeof payload.tenant_id !== 'string' || typeof payload.run_id !== 'string' || typeof payload.generation !== 'string' || !['worker', 'verifier'].includes(String(payload.kind))) return undefined; return { userId: payload.sub, tenantId: payload.tenant_id, runId: payload.run_id, credentialId: payload.generation, kind: payload.kind as 'worker' | 'verifier', role: 'admin' }; } catch { return undefined; } + try { + const { payload } = await jwtVerify(token, this.key(), { + issuer: 'wishforge-control-plane', + audience: 'wishforge-internal', + algorithms: ['HS256'], + }); + if ( + !payload.sub || + typeof payload.tenant_id !== 'string' || + typeof payload.run_id !== 'string' || + typeof payload.generation !== 'string' || + !['worker', 'verifier'].includes(String(payload.kind)) + ) + return undefined; + return { + userId: payload.sub, + tenantId: payload.tenant_id, + runId: payload.run_id, + credentialId: payload.generation, + kind: payload.kind as 'worker' | 'verifier', + role: 'admin', + }; + } catch { + return undefined; + } } } diff --git a/apps/api/src/telemetry.ts b/apps/api/src/telemetry.ts index 64981a7..7b7f79f 100644 --- a/apps/api/src/telemetry.ts +++ b/apps/api/src/telemetry.ts @@ -8,40 +8,83 @@ export async function observability(env = process.env) { let stopTracing: (() => Promise) | undefined; if (env.OTEL_EXPORTER_OTLP_ENDPOINT) { const endpoint = new URL(env.OTEL_EXPORTER_OTLP_ENDPOINT); - if (!['http:', 'https:'].includes(endpoint.protocol) || endpoint.username || endpoint.password) throw new Error('Invalid OTLP collector URL'); - const [{ NodeSDK }, { OTLPTraceExporter }] = await Promise.all([import('@opentelemetry/sdk-node'), import('@opentelemetry/exporter-trace-otlp-http')]); - const sdk = new NodeSDK({ serviceName: 'wishforge-control-plane', traceExporter: new OTLPTraceExporter({ url: new URL('/v1/traces', endpoint).toString() }) }); - sdk.start(); stopTracing = () => sdk.shutdown(); + if (!['http:', 'https:'].includes(endpoint.protocol) || endpoint.username || endpoint.password) + throw new Error('Invalid OTLP collector URL'); + const [{ NodeSDK }, { OTLPTraceExporter }] = await Promise.all([ + import('@opentelemetry/sdk-node'), + import('@opentelemetry/exporter-trace-otlp-http'), + ]); + const sdk = new NodeSDK({ + serviceName: 'wishforge-control-plane', + traceExporter: new OTLPTraceExporter({ url: new URL('/v1/traces', endpoint).toString() }), + }); + sdk.start(); + stopTracing = () => sdk.shutdown(); } const registry = new Registry(); collectDefaultMetrics({ register: registry, prefix: 'wishforge_' }); - const requests = new Counter({ name: 'wishforge_http_requests_total', help: 'Completed HTTP requests', labelNames: ['method', 'route', 'status'] as const, registers: [registry] }); - const duration = new Histogram({ name: 'wishforge_http_request_duration_seconds', help: 'HTTP handler duration', labelNames: ['method', 'route'] as const, buckets: [.01, .05, .1, .25, .5, 1, 2, 5, 15], registers: [registry] }); + const requests = new Counter({ + name: 'wishforge_http_requests_total', + help: 'Completed HTTP requests', + labelNames: ['method', 'route', 'status'] as const, + registers: [registry], + }); + const duration = new Histogram({ + name: 'wishforge_http_request_duration_seconds', + help: 'HTTP handler duration', + labelNames: ['method', 'route'] as const, + buckets: [0.01, 0.05, 0.1, 0.25, 0.5, 1, 2, 5, 15], + registers: [registry], + }); return { middleware(req: Request, res: Response, next: NextFunction) { if (req.path === '/metrics') return next(); const started = process.hrtime.bigint(); const parent = propagation.extract(context.active(), req.headers); - const span = trace.getTracer('wishforge-http').startSpan('HTTP request', { kind: SpanKind.SERVER, attributes: { 'http.request.method': req.method } }, parent); + const span = trace + .getTracer('wishforge-http') + .startSpan( + 'HTTP request', + { kind: SpanKind.SERVER, attributes: { 'http.request.method': req.method } }, + parent, + ); let finished = false; const finish = () => { - if (finished) return; finished = true; - const method = ['GET','POST','PUT','PATCH','DELETE','HEAD','OPTIONS'].includes(req.method) ? req.method : 'OTHER'; + if (finished) return; + finished = true; + const method = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'].includes(req.method) + ? req.method + : 'OTHER'; const route = typeof req.route?.path === 'string' ? req.route.path : 'unmatched'; const status = res.statusCode; - requests.inc({ method, route, status: String(status) }); duration.observe({ method, route }, Number(process.hrtime.bigint() - started) / 1e9); - span.updateName(`${method} ${route}`); span.setAttribute('http.route', route); span.setAttribute('http.response.status_code', status); - if (status >= 500) span.setStatus({ code: SpanStatusCode.ERROR }); span.end(); + requests.inc({ method, route, status: String(status) }); + duration.observe({ method, route }, Number(process.hrtime.bigint() - started) / 1e9); + span.updateName(`${method} ${route}`); + span.setAttribute('http.route', route); + span.setAttribute('http.response.status_code', status); + if (status >= 500) span.setStatus({ code: SpanStatusCode.ERROR }); + span.end(); }; - res.once('finish', finish); res.once('close', finish); + res.once('finish', finish); + res.once('close', finish); context.with(trace.setSpan(parent, span), next); }, async metrics(req: Request, res: Response) { - if (!env.WF_METRICS_TOKEN) { res.status(404).end(); return; } + if (!env.WF_METRICS_TOKEN) { + res.status(404).end(); + return; + } const value = req.header('Authorization')?.replace(/^Bearer /, '') ?? ''; - if (!sameSecret(value, env.WF_METRICS_TOKEN)) { res.status(401).end(); return; } - res.type(registry.contentType).setHeader('Cache-Control', 'no-store'); res.send(await registry.metrics()); + if (!sameSecret(value, env.WF_METRICS_TOKEN)) { + res.status(401).end(); + return; + } + res.type(registry.contentType).setHeader('Cache-Control', 'no-store'); + res.send(await registry.metrics()); + }, + async close() { + registry.clear(); + await stopTracing?.(); }, - async close() { registry.clear(); await stopTracing?.(); }, }; } diff --git a/apps/api/src/workspace-integration-http.ts b/apps/api/src/workspace-integration-http.ts index 0e34545..938a317 100644 --- a/apps/api/src/workspace-integration-http.ts +++ b/apps/api/src/workspace-integration-http.ts @@ -4,27 +4,138 @@ import { z } from 'zod'; import { DomainError, type Actor, type Run } from '@wishforge/contracts'; import { Services } from './http.js'; import { must } from './domain/control-plane.js'; -type AuthRequest=Request&{actor:Actor}; -const lease=z.object({leaseToken:z.number().int().positive()}).strict(); -const fenced=z.object({leaseToken:z.number().int().positive(),fence:z.number().int().positive()}).strict(); +type AuthRequest = Request & { actor: Actor }; +const lease = z.object({ leaseToken: z.number().int().positive() }).strict(); +const fenced = z.object({ leaseToken: z.number().int().positive(), fence: z.number().int().positive() }).strict(); @Controller('v1') export class WorkspaceInputController { - constructor(@Inject(Services)private s:Services){} - @Get('runs/:id/workspace-input')async inspect(@Req()req:AuthRequest,@Param('id')id:string){return{data:await this.s.control.workspaceIntegration.inspect(req.actor,id)};} - @Post('runs/:id/workspaces/:workspaceId/import-snapshot')async import(@Req()req:AuthRequest,@Param('id')id:string,@Param('workspaceId')workspaceId:string,@Body()body:unknown){const input=z.object({sourceCommit:z.string().regex(/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/),confirmation:z.literal('saved-workspace-committed')}).strict().parse(body);return{data:await this.s.control.workspaceIntegration.importSnapshot(req.actor,id,workspaceId,input.sourceCommit,req.header('Authorization')??'')};} + constructor(@Inject(Services) private s: Services) {} + @Get('runs/:id/workspace-input') async inspect(@Req() req: AuthRequest, @Param('id') id: string) { + return { data: await this.s.control.workspaceIntegration.inspect(req.actor, id) }; + } + @Post('runs/:id/workspaces/:workspaceId/import-snapshot') async import( + @Req() req: AuthRequest, + @Param('id') id: string, + @Param('workspaceId') workspaceId: string, + @Body() body: unknown, + ) { + const input = z + .object({ + sourceCommit: z.string().regex(/^[a-f0-9]{40}(?:[a-f0-9]{24})?$/), + confirmation: z.literal('saved-workspace-committed'), + }) + .strict() + .parse(body); + return { + data: await this.s.control.workspaceIntegration.importSnapshot( + req.actor, + id, + workspaceId, + input.sourceCommit, + req.header('Authorization') ?? '', + ), + }; + } } @Controller('internal') export class WorkspaceIntegrationController { - constructor(@Inject(Services)private s:Services){} - @Get('runs/:id/workspace-input')async inspect(@Req()req:AuthRequest,@Param('id')id:string){return{data:await this.s.control.workspaceIntegration.inspect(req.actor,id)};} - @Post('runs/:id/workspace-input/match')async match(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){const input=z.object({leaseToken:z.number().int().positive(),targetCommit:z.string(),snapshots:z.array(z.object({workspaceId:z.string(),sourceCommit:z.string(),snapshotRevision:z.number().int().positive(),snapshotDigest:z.string().regex(/^[a-f0-9]{64}$/)}).strict()).max(20)}).strict().parse(body);return{data:await this.s.control.workspaceIntegration.recordMatch(req.actor,id,input)};} - @Post('runs/:id/workspace-input/check')async check(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){await this.s.control.workspaceIntegration.enforce(req.actor,id,lease.parse(body).leaseToken);return{data:{allowed:true}};} - @Post('runs/:id/integration-lease/acquire')async acquire(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){return{data:await this.s.control.workspaceIntegration.acquire(req.actor,id,lease.parse(body).leaseToken)};} - @Post('runs/:id/integration-lease/renew')async renew(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){const data=fenced.parse(body);return{data:await this.s.control.workspaceIntegration.renew(req.actor,id,data.leaseToken,data.fence)};} - @Post('runs/:id/integration-lease/release')async release(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){const data=fenced.parse(body);return{data:await this.s.control.workspaceIntegration.release(req.actor,id,data.leaseToken,data.fence)};} - @Post('workspaces/:id/integration-authorize')async authorize(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){ - if(req.actor.userId!=='workspace-gateway')throw new DomainError('FORBIDDEN','Workspace gateway identity required',403); - const input=z.object({runId:z.string(),revision:z.number().int().positive(),generation:z.string().optional()}).strict().parse(body); - return{data:await this.s.control.store.transaction(req.actor.tenantId,async tx=>{const workspace=await must(tx,'workspace',id),run=await must(tx,'run',input.runId);if(workspace.projectId!==run.projectId||workspace.status!=='ready'||!workspace.upstreamUrl||run.revision!==input.revision||run.executionGeneration!==input.generation||run.status!=='running'||!run.lease||Date.parse(run.lease.expiresAt)<=Date.now())throw new DomainError('WORKSPACE_INPUT_UNAVAILABLE','Workspace is not a live integration input',409);return{tenantId:req.actor.tenantId,workspaceId:id,projectId:run.projectId,upstreamUrl:workspace.upstreamUrl,userId:`integration:${run.id}`,role:'admin',purpose:'integration-state'};})}; + constructor(@Inject(Services) private s: Services) {} + @Get('runs/:id/workspace-input') async inspect(@Req() req: AuthRequest, @Param('id') id: string) { + return { data: await this.s.control.workspaceIntegration.inspect(req.actor, id) }; + } + @Post('runs/:id/workspace-input/match') async match( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + const input = z + .object({ + leaseToken: z.number().int().positive(), + targetCommit: z.string(), + snapshots: z + .array( + z + .object({ + workspaceId: z.string(), + sourceCommit: z.string(), + snapshotRevision: z.number().int().positive(), + snapshotDigest: z.string().regex(/^[a-f0-9]{64}$/), + }) + .strict(), + ) + .max(20), + }) + .strict() + .parse(body); + return { data: await this.s.control.workspaceIntegration.recordMatch(req.actor, id, input) }; + } + @Post('runs/:id/workspace-input/check') async check( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + await this.s.control.workspaceIntegration.enforce(req.actor, id, lease.parse(body).leaseToken); + return { data: { allowed: true } }; + } + @Post('runs/:id/integration-lease/acquire') async acquire( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + return { data: await this.s.control.workspaceIntegration.acquire(req.actor, id, lease.parse(body).leaseToken) }; + } + @Post('runs/:id/integration-lease/renew') async renew( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + const data = fenced.parse(body); + return { data: await this.s.control.workspaceIntegration.renew(req.actor, id, data.leaseToken, data.fence) }; + } + @Post('runs/:id/integration-lease/release') async release( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + const data = fenced.parse(body); + return { data: await this.s.control.workspaceIntegration.release(req.actor, id, data.leaseToken, data.fence) }; + } + @Post('workspaces/:id/integration-authorize') async authorize( + @Req() req: AuthRequest, + @Param('id') id: string, + @Body() body: unknown, + ) { + if (req.actor.userId !== 'workspace-gateway') + throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403); + const input = z + .object({ runId: z.string(), revision: z.number().int().positive(), generation: z.string().optional() }) + .strict() + .parse(body); + return { + data: await this.s.control.store.transaction(req.actor.tenantId, async tx => { + const workspace = await must(tx, 'workspace', id), + run = await must(tx, 'run', input.runId); + if ( + workspace.projectId !== run.projectId || + workspace.status !== 'ready' || + !workspace.upstreamUrl || + run.revision !== input.revision || + run.executionGeneration !== input.generation || + run.status !== 'running' || + !run.lease || + Date.parse(run.lease.expiresAt) <= Date.now() + ) + throw new DomainError('WORKSPACE_INPUT_UNAVAILABLE', 'Workspace is not a live integration input', 409); + return { + tenantId: req.actor.tenantId, + workspaceId: id, + projectId: run.projectId, + upstreamUrl: workspace.upstreamUrl, + userId: `integration:${run.id}`, + role: 'admin', + purpose: 'integration-state', + }; + }), + }; } } diff --git a/apps/api/src/workspace-launch-http.ts b/apps/api/src/workspace-launch-http.ts index 866c4ba..bd5cb2e 100644 --- a/apps/api/src/workspace-launch-http.ts +++ b/apps/api/src/workspace-launch-http.ts @@ -1,3 +1,24 @@ -import {Body,Controller,Inject,Param,Post,Req} from '@nestjs/common';import type {Request} from 'express';import {z} from 'zod';import type {Actor} from '@wishforge/contracts';import {Services} from './http.js';import {WorkspaceLaunch} from './domain/workspace-launch.js'; +import { Body, Controller, Inject, Param, Post, Req } from '@nestjs/common'; +import type { Request } from 'express'; +import { z } from 'zod'; +import type { Actor } from '@wishforge/contracts'; +import { Services } from './http.js'; +import { WorkspaceLaunch } from './domain/workspace-launch.js'; @Controller('v1/workspaces') -export class WorkspaceLaunchController{constructor(@Inject(Services)private s:Services){}@Post(':id/launch')async launch(@Req()req:Request&{actor:Actor},@Param('id')id:string,@Body()body:unknown){z.object({}).strict().parse(body);return{data:await new WorkspaceLaunch(this.s.control.store,this.s.control.env).launch(req.actor,id,req.header('Authorization')??'')};}} +export class WorkspaceLaunchController { + constructor(@Inject(Services) private s: Services) {} + @Post(':id/launch') async launch( + @Req() req: Request & { actor: Actor }, + @Param('id') id: string, + @Body() body: unknown, + ) { + z.object({}).strict().parse(body); + return { + data: await new WorkspaceLaunch(this.s.control.store, this.s.control.env).launch( + req.actor, + id, + req.header('Authorization') ?? '', + ), + }; + } +} diff --git a/apps/api/src/workspace-lifecycle-http.ts b/apps/api/src/workspace-lifecycle-http.ts index 766a4b0..8e6659f 100644 --- a/apps/api/src/workspace-lifecycle-http.ts +++ b/apps/api/src/workspace-lifecycle-http.ts @@ -1,18 +1,55 @@ -import {Body,Controller,Inject,Param,Post,Req} from '@nestjs/common'; -import {z} from 'zod'; -import type {Request} from 'express'; -import {DomainError,type Actor} from '@wishforge/contracts'; -import {Services} from './http.js'; -import {must} from './domain/control-plane.js'; -import {WorkspaceLifecycle} from './domain/workspace-lifecycle.js'; -type AuthRequest=Request&{actor:Actor}; +import { Body, Controller, Inject, Param, Post, Req } from '@nestjs/common'; +import { z } from 'zod'; +import type { Request } from 'express'; +import { DomainError, type Actor } from '@wishforge/contracts'; +import { Services } from './http.js'; +import { must } from './domain/control-plane.js'; +import { WorkspaceLifecycle } from './domain/workspace-lifecycle.js'; +type AuthRequest = Request & { actor: Actor }; @Controller('v1/workspaces') -export class WorkspaceLifecycleController{ - constructor(@Inject(Services)private s:Services){} - @Post(':id/lifecycle')async control(@Req()req:AuthRequest,@Param('id')id:string,@Body()body:unknown){const input=z.object({action:z.enum(['pause','resume']),confirmation:z.literal('change-workspace-lifecycle')}).strict().parse(body);return{data:await new WorkspaceLifecycle(this.s.control.store,this.s.control.env).manual(req.actor,id,input.action,req.header('Idempotency-Key')??'',req.header('Authorization')??'')};} +export class WorkspaceLifecycleController { + constructor(@Inject(Services) private s: Services) {} + @Post(':id/lifecycle') async control(@Req() req: AuthRequest, @Param('id') id: string, @Body() body: unknown) { + const input = z + .object({ action: z.enum(['pause', 'resume']), confirmation: z.literal('change-workspace-lifecycle') }) + .strict() + .parse(body); + return { + data: await new WorkspaceLifecycle(this.s.control.store, this.s.control.env).manual( + req.actor, + id, + input.action, + req.header('Idempotency-Key') ?? '', + req.header('Authorization') ?? '', + ), + }; + } } @Controller('internal/workspaces') -export class WorkspaceLifecycleInternalController{ - constructor(@Inject(Services)private s:Services){} - @Post(':id/lifecycle-authorize')async authorize(@Req()req:AuthRequest,@Param('id')id:string){if(req.actor.userId!=='workspace-gateway')throw new DomainError('FORBIDDEN','Workspace gateway identity required',403);return{data:await this.s.control.store.transaction(req.actor.tenantId,async tx=>{const w=await must(tx,'workspace',id);if(!['ready','pausing','unknown'].includes(String(w.status))||!w.upstreamUrl||w.managedBy!=='wishforge-provisioner')throw new DomainError('WORKSPACE_NOT_READY','Workspace activity cannot be established',503);return{tenantId:req.actor.tenantId,workspaceId:id,projectId:w.projectId,upstreamUrl:w.upstreamUrl,userId:'lifecycle-inspector',role:'admin',purpose:'workspace-lifecycle'};})};} +export class WorkspaceLifecycleInternalController { + constructor(@Inject(Services) private s: Services) {} + @Post(':id/lifecycle-authorize') async authorize(@Req() req: AuthRequest, @Param('id') id: string) { + if (req.actor.userId !== 'workspace-gateway') + throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403); + return { + data: await this.s.control.store.transaction(req.actor.tenantId, async tx => { + const w = await must(tx, 'workspace', id); + if ( + !['ready', 'pausing', 'unknown'].includes(String(w.status)) || + !w.upstreamUrl || + w.managedBy !== 'wishforge-provisioner' + ) + throw new DomainError('WORKSPACE_NOT_READY', 'Workspace activity cannot be established', 503); + return { + tenantId: req.actor.tenantId, + workspaceId: id, + projectId: w.projectId, + upstreamUrl: w.upstreamUrl, + userId: 'lifecycle-inspector', + role: 'admin', + purpose: 'workspace-lifecycle', + }; + }), + }; + } } diff --git a/apps/api/src/workspace-lifecycle-worker.ts b/apps/api/src/workspace-lifecycle-worker.ts index 63bdfb3..942171a 100644 --- a/apps/api/src/workspace-lifecycle-worker.ts +++ b/apps/api/src/workspace-lifecycle-worker.ts @@ -1,10 +1,36 @@ -import type {ControlPlane} from './domain/control-plane.js'; -import {WorkspaceLifecycle} from './domain/workspace-lifecycle.js'; +import type { ControlPlane } from './domain/control-plane.js'; +import { WorkspaceLifecycle } from './domain/workspace-lifecycle.js'; /** Independent of Temporal availability; durable outbox reconciliation owns no application deployment. */ -export class WorkspaceLifecycleWorker{ - private timer?:ReturnType;private stopped=false;private busy?:Promise; - constructor(private control:ControlPlane){} - start(){const tick=async()=>{if(this.stopped)return;this.busy=this.tick();await this.busy;if(!this.stopped)this.timer=setTimeout(()=>void tick(),10000);};void tick();} - private async tick(){const lifecycle=new WorkspaceLifecycle(this.control.store,this.control.env);for(const tenant of(this.control.env.WF_DISPATCH_TENANTS??'').split(',').filter(Boolean)){try{const pending=await this.control.store.transaction(tenant,tx=>tx.outbox());for(const item of pending.filter(x=>x.type==='workspace.reconcile'))await lifecycle.reconcile(tenant,item);await lifecycle.scheduleIdle(tenant);}catch{/* Durable items remain pending; never infer ready/suspended from a transport failure. */}}} - async close(){this.stopped=true;if(this.timer)clearTimeout(this.timer);await this.busy;} +export class WorkspaceLifecycleWorker { + private timer?: ReturnType; + private stopped = false; + private busy?: Promise; + constructor(private control: ControlPlane) {} + start() { + const tick = async () => { + if (this.stopped) return; + this.busy = this.tick(); + await this.busy; + if (!this.stopped) this.timer = setTimeout(() => void tick(), 10000); + }; + void tick(); + } + private async tick() { + const lifecycle = new WorkspaceLifecycle(this.control.store, this.control.env); + for (const tenant of (this.control.env.WF_DISPATCH_TENANTS ?? '').split(',').filter(Boolean)) { + try { + const pending = await this.control.store.transaction(tenant, tx => tx.outbox()); + for (const item of pending.filter(x => x.type === 'workspace.reconcile')) + await lifecycle.reconcile(tenant, item); + await lifecycle.scheduleIdle(tenant); + } catch { + /* Durable items remain pending; never infer ready/suspended from a transport failure. */ + } + } + } + async close() { + this.stopped = true; + if (this.timer) clearTimeout(this.timer); + await this.busy; + } } diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 3be161e..832af42 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -1 +1 @@ -{"extends":"../../tsconfig.base.json","include":["src/**/*.ts"]} +{ "extends": "../../tsconfig.base.json", "include": ["src/**/*.ts"] } diff --git a/apps/git-service/package.json b/apps/git-service/package.json index efe65f8..394228c 100644 --- a/apps/git-service/package.json +++ b/apps/git-service/package.json @@ -1 +1,13 @@ -{ "name": "@wishforge/git-service", "version": "0.1.0", "private": true, "type": "module", "scripts": { "dev": "tsx src/main.ts", "typecheck": "tsc --noEmit" }, "dependencies": { "@wishforge/runtime": "*" } } +{ + "name": "@wishforge/git-service", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "dev": "tsx src/main.ts", + "typecheck": "tsc --noEmit" + }, + "dependencies": { + "@wishforge/runtime": "*" + } +} diff --git a/apps/git-service/src/main.ts b/apps/git-service/src/main.ts index 026b72f..3fb206f 100644 --- a/apps/git-service/src/main.ts +++ b/apps/git-service/src/main.ts @@ -1,10 +1,22 @@ import { createGitService } from './service.js'; -async function main(){ - for(const key of ['WF_GIT_ROOT','WF_GIT_PUBLIC_BASE_URL','WF_GIT_ADMIN_TOKEN','WF_GIT_TOKEN_SECRET'])if(!process.env[key])throw new Error(`unconfigured: ${key} is required`); - const server=await createGitService({root:process.env.WF_GIT_ROOT!,publicBaseUrl:process.env.WF_GIT_PUBLIC_BASE_URL!,adminToken:process.env.WF_GIT_ADMIN_TOKEN!,tokenSecret:process.env.WF_GIT_TOKEN_SECRET!,allowLocalHttp:process.env.WF_GIT_ALLOW_LOCAL_HTTP==='true'}); +async function main() { + for (const key of ['WF_GIT_ROOT', 'WF_GIT_PUBLIC_BASE_URL', 'WF_GIT_ADMIN_TOKEN', 'WF_GIT_TOKEN_SECRET']) + if (!process.env[key]) throw new Error(`unconfigured: ${key} is required`); + const server = await createGitService({ + root: process.env.WF_GIT_ROOT!, + publicBaseUrl: process.env.WF_GIT_PUBLIC_BASE_URL!, + adminToken: process.env.WF_GIT_ADMIN_TOKEN!, + tokenSecret: process.env.WF_GIT_TOKEN_SECRET!, + allowLocalHttp: process.env.WF_GIT_ALLOW_LOCAL_HTTP === 'true', + }); const port = Number(process.env.WF_GIT_PORT ?? 4504); if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('WF_GIT_PORT must be a valid port'); - server.listen(port,process.env.WF_GIT_HOST ?? '127.0.0.1',()=>console.log(`Git storage listening on port ${port}`)); - process.once('SIGTERM',()=>server.close()); + server.listen(port, process.env.WF_GIT_HOST ?? '127.0.0.1', () => + console.log(`Git storage listening on port ${port}`), + ); + process.once('SIGTERM', () => server.close()); } -void main().catch(error=>{console.error(error.message);process.exitCode=1;}); +void main().catch(error => { + console.error(error.message); + process.exitCode = 1; +}); diff --git a/apps/git-service/src/service.ts b/apps/git-service/src/service.ts index eb8d964..84bd1b4 100644 --- a/apps/git-service/src/service.ts +++ b/apps/git-service/src/service.ts @@ -5,110 +5,331 @@ import { createServer, type IncomingMessage, type ServerResponse } from 'node:ht import path from 'node:path'; import { checkedCommand, RuntimeError, safeIdentifier, requireBranchName } from '@wishforge/runtime'; -export interface GitServiceConfig { root: string; publicBaseUrl: string; adminToken: string; tokenSecret: string; allowLocalHttp?: boolean; maxPackBytes?: number; } -export interface GitClaims { aud: 'wishforge-git'; tenantId: string; projectId: string; permission: 'read' | 'write'; iat: number; exp: number; runId?: string; } -function sameSecret(a: string, b: string) { const x=Buffer.from(a),y=Buffer.from(b); return x.length===y.length && timingSafeEqual(x,y); } -export function issueGitToken(claims: GitClaims, secret: string): string { - const payload=Buffer.from(JSON.stringify(claims)).toString('base64url'); - return `${payload}.${createHmac('sha256',secret).update(payload).digest('base64url')}`; +export interface GitServiceConfig { + root: string; + publicBaseUrl: string; + adminToken: string; + tokenSecret: string; + allowLocalHttp?: boolean; + maxPackBytes?: number; } -export function verifyGitToken(token: string, secret: string, scope: { tenantId: string; projectId: string; write: boolean }, now = Math.floor(Date.now()/1000)): GitClaims { - const [payload,signature,...extra]=token.split('.'); - if(!payload || !signature || extra.length || !sameSecret(signature,createHmac('sha256',secret).update(payload).digest('base64url'))) throw new RuntimeError('UNAUTHORIZED','Invalid Git token'); +export interface GitClaims { + aud: 'wishforge-git'; + tenantId: string; + projectId: string; + permission: 'read' | 'write'; + iat: number; + exp: number; + runId?: string; +} +function sameSecret(a: string, b: string) { + const x = Buffer.from(a), + y = Buffer.from(b); + return x.length === y.length && timingSafeEqual(x, y); +} +export function issueGitToken(claims: GitClaims, secret: string): string { + const payload = Buffer.from(JSON.stringify(claims)).toString('base64url'); + return `${payload}.${createHmac('sha256', secret).update(payload).digest('base64url')}`; +} +export function verifyGitToken( + token: string, + secret: string, + scope: { tenantId: string; projectId: string; write: boolean }, + now = Math.floor(Date.now() / 1000), +): GitClaims { + const [payload, signature, ...extra] = token.split('.'); + if ( + !payload || + !signature || + extra.length || + !sameSecret(signature, createHmac('sha256', secret).update(payload).digest('base64url')) + ) + throw new RuntimeError('UNAUTHORIZED', 'Invalid Git token'); let claims: GitClaims; - try { claims=JSON.parse(Buffer.from(payload,'base64url').toString()) as GitClaims; } - catch { throw new RuntimeError('UNAUTHORIZED','Invalid Git token payload'); } - if(claims.aud!=='wishforge-git' || claims.tenantId!==scope.tenantId || claims.projectId!==scope.projectId || (scope.write && claims.permission!=='write') || !['read','write'].includes(claims.permission) || !Number.isInteger(claims.exp) || !Number.isInteger(claims.iat) || claims.exp<=now || claims.iat>now+30 || claims.exp-claims.iat>900) throw new RuntimeError('UNAUTHORIZED','Git token expired or does not authorize this repository/operation'); + try { + claims = JSON.parse(Buffer.from(payload, 'base64url').toString()) as GitClaims; + } catch { + throw new RuntimeError('UNAUTHORIZED', 'Invalid Git token payload'); + } + if ( + claims.aud !== 'wishforge-git' || + claims.tenantId !== scope.tenantId || + claims.projectId !== scope.projectId || + (scope.write && claims.permission !== 'write') || + !['read', 'write'].includes(claims.permission) || + !Number.isInteger(claims.exp) || + !Number.isInteger(claims.iat) || + claims.exp <= now || + claims.iat > now + 30 || + claims.exp - claims.iat > 900 + ) + throw new RuntimeError('UNAUTHORIZED', 'Git token expired or does not authorize this repository/operation'); return claims; } -async function jsonBody(request: IncomingMessage):Promise> { - const chunks:Buffer[]=[];let bytes=0; - for await(const chunk of request) { bytes+=Buffer.byteLength(chunk); if(bytes>64*1024) throw new RuntimeError('PAYLOAD_TOO_LARGE','Admin request exceeds limit');chunks.push(Buffer.from(chunk)); } - return JSON.parse(Buffer.concat(chunks).toString() || '{}') as Record; +async function jsonBody(request: IncomingMessage): Promise> { + const chunks: Buffer[] = []; + let bytes = 0; + for await (const chunk of request) { + bytes += Buffer.byteLength(chunk); + if (bytes > 64 * 1024) throw new RuntimeError('PAYLOAD_TOO_LARGE', 'Admin request exceeds limit'); + chunks.push(Buffer.from(chunk)); + } + return JSON.parse(Buffer.concat(chunks).toString() || '{}') as Record; +} +function json(response: ServerResponse, status: number, body: unknown) { + response.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' }); + response.end(JSON.stringify(body)); } -function json(response:ServerResponse,status:number,body:unknown) { response.writeHead(status,{'content-type':'application/json','cache-control':'no-store'});response.end(JSON.stringify(body)); } -export async function createGitService(config:GitServiceConfig) { - if(config.adminToken.length<24 || config.tokenSecret.length<32) throw new RuntimeError('UNCONFIGURED','Strong Git admin and token-signing secrets are required'); - const publicBase=new URL(config.publicBaseUrl); - if (publicBase.username || publicBase.password || publicBase.search || publicBase.hash || publicBase.pathname !== '/') throw new RuntimeError('INVALID_ENDPOINT','Public Git URL must be an origin without credentials'); - if(publicBase.protocol!=='https:' && !(process.env.NODE_ENV!=='production' && config.allowLocalHttp && publicBase.protocol==='http:' && ['127.0.0.1','localhost','[::1]'].includes(publicBase.hostname))) throw new RuntimeError('INSECURE_ENDPOINT','Public Git URL must be HTTPS (explicit loopback HTTP is development-only)'); - await mkdir(config.root,{recursive:true,mode:0o700});const root=await realpath(config.root); - const gitEnv={PATH:process.env.PATH,LANG:'C.UTF-8',GIT_CONFIG_GLOBAL:'/dev/null',GIT_CONFIG_NOSYSTEM:'1',GIT_AUTHOR_NAME:'WishForge',GIT_AUTHOR_EMAIL:'platform@wishforge.invalid',GIT_COMMITTER_NAME:'WishForge',GIT_COMMITTER_EMAIL:'platform@wishforge.invalid'}; - const git=(cwd:string,args:string[],input?:string)=>checkedCommand({executable:'git',args:['-c','core.hooksPath=/dev/null',...args],cwd,input,env:gitEnv}); - const location=async(tenantId:string,projectId:string)=> { - safeIdentifier(tenantId);safeIdentifier(projectId); - const parent=path.join(root,tenantId);await mkdir(parent,{recursive:true,mode:0o700}); - if(!(await realpath(parent)).startsWith(`${root}${path.sep}`)) throw new RuntimeError('REPOSITORY_SCOPE','Repository parent escaped the storage root'); - const repository = path.join(parent,`${projectId}.git`); - const resolved = await realpath(repository).catch(error => { if (error.code === 'ENOENT') return repository; throw error; }); - if (resolved !== repository) throw new RuntimeError('REPOSITORY_SCOPE','Repository links are not permitted'); +export async function createGitService(config: GitServiceConfig) { + if (config.adminToken.length < 24 || config.tokenSecret.length < 32) + throw new RuntimeError('UNCONFIGURED', 'Strong Git admin and token-signing secrets are required'); + const publicBase = new URL(config.publicBaseUrl); + if (publicBase.username || publicBase.password || publicBase.search || publicBase.hash || publicBase.pathname !== '/') + throw new RuntimeError('INVALID_ENDPOINT', 'Public Git URL must be an origin without credentials'); + if ( + publicBase.protocol !== 'https:' && + !( + process.env.NODE_ENV !== 'production' && + config.allowLocalHttp && + publicBase.protocol === 'http:' && + ['127.0.0.1', 'localhost', '[::1]'].includes(publicBase.hostname) + ) + ) + throw new RuntimeError( + 'INSECURE_ENDPOINT', + 'Public Git URL must be HTTPS (explicit loopback HTTP is development-only)', + ); + await mkdir(config.root, { recursive: true, mode: 0o700 }); + const root = await realpath(config.root); + const gitEnv = { + PATH: process.env.PATH, + LANG: 'C.UTF-8', + GIT_CONFIG_GLOBAL: '/dev/null', + GIT_CONFIG_NOSYSTEM: '1', + GIT_AUTHOR_NAME: 'WishForge', + GIT_AUTHOR_EMAIL: 'platform@wishforge.invalid', + GIT_COMMITTER_NAME: 'WishForge', + GIT_COMMITTER_EMAIL: 'platform@wishforge.invalid', + }; + const git = (cwd: string, args: string[], input?: string) => + checkedCommand({ executable: 'git', args: ['-c', 'core.hooksPath=/dev/null', ...args], cwd, input, env: gitEnv }); + const location = async (tenantId: string, projectId: string) => { + safeIdentifier(tenantId); + safeIdentifier(projectId); + const parent = path.join(root, tenantId); + await mkdir(parent, { recursive: true, mode: 0o700 }); + if (!(await realpath(parent)).startsWith(`${root}${path.sep}`)) + throw new RuntimeError('REPOSITORY_SCOPE', 'Repository parent escaped the storage root'); + const repository = path.join(parent, `${projectId}.git`); + const resolved = await realpath(repository).catch(error => { + if (error.code === 'ENOENT') return repository; + throw error; + }); + if (resolved !== repository) throw new RuntimeError('REPOSITORY_SCOPE', 'Repository links are not permitted'); return repository; }; - const server=createServer((request,response)=>{void(async()=>{ - const url=new URL(request.url ?? '/',publicBase); - if(url.pathname==='/health' && request.method==='GET') { json(response,200,{status:'ok',service:'git-storage'});return; } - if(['/repos','/tokens'].includes(url.pathname) && request.method==='POST') { - const auth=request.headers.authorization ?? ''; - if(!auth.startsWith('Bearer ') || !sameSecret(auth.slice(7),config.adminToken)) {json(response,401,{error:'UNAUTHORIZED'});return;} - const body=await jsonBody(request);const tenantId=String(body.tenantId ?? ''),projectId=String(body.projectId ?? ''); - const repository=await location(tenantId,projectId); - if(url.pathname==='/repos') { - if(body.defaultBranch!==undefined && typeof body.defaultBranch!=='string') throw new RuntimeError('INVALID_REF','defaultBranch must be a safe Git branch name'); - const requestedBranch=requireBranchName(body.defaultBranch as string | undefined ?? 'main'); - const lockPath=`${repository}.provision.lock`; - const lock=await open(lockPath,'wx',0o600).catch(()=>{throw new RuntimeError('REPOSITORY_BUSY','Repository provisioning is already in progress');}); - try { - if(!await stat(repository).then(()=>true,error=>{if(error.code==='ENOENT')return false;throw error;})) { - await git(path.dirname(repository),['init','--bare',`--initial-branch=${requestedBranch}`,repository]); - const tree=await git(repository,['hash-object','-t','tree','--stdin'],''); - const commit=await git(repository,['commit-tree',tree,'-m','Initialize WishForge project']); - await git(repository,['update-ref',`refs/heads/${requestedBranch}`,commit]); - await git(repository,['config','http.receivepack','true']); - await git(repository,['config','receive.denyNonFastForwards','true']); - await git(repository,['config','receive.denyDeletes','true']); - } - const headRef=await git(repository,['symbolic-ref','--quiet','HEAD']); - if(!headRef.startsWith('refs/heads/'))throw new RuntimeError('REPOSITORY_DEFAULT_BRANCH_UNAVAILABLE','Repository HEAD is not a branch; existing state was not changed'); - const defaultBranch=requireBranchName(headRef.slice('refs/heads/'.length)); - const initialCommit=await git(repository,['rev-parse','--verify',`${headRef}^{commit}`]); - json(response,200,{repositoryUrl:new URL(`/git/${tenantId}/${projectId}.git`,publicBase).href,initialCommit,defaultBranch}); - } finally {await lock.close();await unlink(lockPath);} - } else { - await stat(repository); - if(body.permission!=='read' && body.permission!=='write') throw new RuntimeError('INVALID_PERMISSION','Git permission must be read or write'); - const ttl=Number(body.ttlSeconds ?? 300);if(!Number.isInteger(ttl)||ttl<30||ttl>900)throw new RuntimeError('INVALID_TTL','Token TTL must be 30..900 seconds'); - const issued=Math.floor(Date.now()/1000);const claims:GitClaims={aud:'wishforge-git',tenantId,projectId,permission:body.permission,iat:issued,exp:issued+ttl,...(typeof body.runId==='string'?{runId:safeIdentifier(body.runId)}:{})}; - json(response,200,{token:issueGitToken(claims,config.tokenSecret),expiresAt:new Date(claims.exp*1000).toISOString()}); + const server = createServer((request, response) => { + void (async () => { + const url = new URL(request.url ?? '/', publicBase); + if (url.pathname === '/health' && request.method === 'GET') { + json(response, 200, { status: 'ok', service: 'git-storage' }); + return; } - return; - } - const match=/^\/git\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\.git\/(info\/refs|git-upload-pack|git-receive-pack)$/.exec(url.pathname); - if(!match) {json(response,404,{error:'NOT_FOUND'});return;} - const tenantId=match[1]!,projectId=match[2]!,operation=match[3]!; - const service=operation==='info/refs'?url.searchParams.get('service'):operation; - if(!['git-upload-pack','git-receive-pack'].includes(service ?? '') || (operation==='info/refs'?request.method!=='GET':request.method!=='POST')) {json(response,400,{error:'INVALID_GIT_OPERATION'});return;} - const authorization=request.headers.authorization ?? ''; - if(!authorization.startsWith('Bearer ')) {response.setHeader('www-authenticate','Bearer realm="wishforge-git"');json(response,401,{error:'UNAUTHORIZED'});return;} - const claims=verifyGitToken(authorization.slice(7),config.tokenSecret,{tenantId,projectId,write:service==='git-receive-pack'}); - const repository=await location(tenantId,projectId);await stat(repository); - const child=spawn('git',['http-backend'],{env:{...gitEnv,GIT_PROJECT_ROOT:root,GIT_HTTP_EXPORT_ALL:'1',PATH_INFO:`/${tenantId}/${projectId}.git/${operation}`,REQUEST_METHOD:request.method!,QUERY_STRING:url.searchParams.toString(),CONTENT_TYPE:String(request.headers['content-type'] ?? ''),CONTENT_LENGTH:String(request.headers['content-length'] ?? ''),REMOTE_USER:claims.runId ?? 'wishforge',REMOTE_ADDR:request.socket.remoteAddress ?? '',HTTP_GIT_PROTOCOL:String(request.headers['git-protocol'] ?? '')},stdio:['pipe','pipe','pipe'],shell:false}); - let headers=Buffer.alloc(0),headersSent=false,bytes=0; - const timer=setTimeout(()=>child.kill('SIGKILL'),300_000); - request.on('data',chunk=>{bytes+=chunk.length;if(bytes>(config.maxPackBytes ?? 512*1024*1024)){child.kill('SIGKILL');request.destroy();}}); - request.on('aborted',()=>child.kill('SIGKILL'));response.on('close',()=>{if(!response.writableEnded)child.kill('SIGKILL');}); - child.stdout.on('data',(chunk:Buffer)=>{ - if(headersSent){response.write(chunk);return;} - headers=Buffer.concat([headers,chunk]);const end=headers.indexOf('\r\n\r\n'); - if(end===-1){if(headers.length>64*1024)child.kill('SIGKILL');return;} - let status=200;const parsed:Record={}; - for(const line of headers.subarray(0,end).toString().split('\r\n')){const separator=line.indexOf(':');if(separator<1)continue;const name=line.slice(0,separator).toLowerCase(),value=line.slice(separator+1).trim();if(name==='status')status=parseInt(value,10);else if(['content-type','cache-control','expires','pragma','content-encoding'].includes(name))parsed[name]=value;} - response.writeHead(status,parsed);headersSent=true;response.write(headers.subarray(end+4));headers=Buffer.alloc(0); + if (['/repos', '/tokens'].includes(url.pathname) && request.method === 'POST') { + const auth = request.headers.authorization ?? ''; + if (!auth.startsWith('Bearer ') || !sameSecret(auth.slice(7), config.adminToken)) { + json(response, 401, { error: 'UNAUTHORIZED' }); + return; + } + const body = await jsonBody(request); + const tenantId = String(body.tenantId ?? ''), + projectId = String(body.projectId ?? ''); + const repository = await location(tenantId, projectId); + if (url.pathname === '/repos') { + if (body.defaultBranch !== undefined && typeof body.defaultBranch !== 'string') + throw new RuntimeError('INVALID_REF', 'defaultBranch must be a safe Git branch name'); + const requestedBranch = requireBranchName((body.defaultBranch as string | undefined) ?? 'main'); + const lockPath = `${repository}.provision.lock`; + const lock = await open(lockPath, 'wx', 0o600).catch(() => { + throw new RuntimeError('REPOSITORY_BUSY', 'Repository provisioning is already in progress'); + }); + try { + if ( + !(await stat(repository).then( + () => true, + error => { + if (error.code === 'ENOENT') return false; + throw error; + }, + )) + ) { + await git(path.dirname(repository), [ + 'init', + '--bare', + `--initial-branch=${requestedBranch}`, + repository, + ]); + const tree = await git(repository, ['hash-object', '-t', 'tree', '--stdin'], ''); + const commit = await git(repository, ['commit-tree', tree, '-m', 'Initialize WishForge project']); + await git(repository, ['update-ref', `refs/heads/${requestedBranch}`, commit]); + await git(repository, ['config', 'http.receivepack', 'true']); + await git(repository, ['config', 'receive.denyNonFastForwards', 'true']); + await git(repository, ['config', 'receive.denyDeletes', 'true']); + } + const headRef = await git(repository, ['symbolic-ref', '--quiet', 'HEAD']); + if (!headRef.startsWith('refs/heads/')) + throw new RuntimeError( + 'REPOSITORY_DEFAULT_BRANCH_UNAVAILABLE', + 'Repository HEAD is not a branch; existing state was not changed', + ); + const defaultBranch = requireBranchName(headRef.slice('refs/heads/'.length)); + const initialCommit = await git(repository, ['rev-parse', '--verify', `${headRef}^{commit}`]); + json(response, 200, { + repositoryUrl: new URL(`/git/${tenantId}/${projectId}.git`, publicBase).href, + initialCommit, + defaultBranch, + }); + } finally { + await lock.close(); + await unlink(lockPath); + } + } else { + await stat(repository); + if (body.permission !== 'read' && body.permission !== 'write') + throw new RuntimeError('INVALID_PERMISSION', 'Git permission must be read or write'); + const ttl = Number(body.ttlSeconds ?? 300); + if (!Number.isInteger(ttl) || ttl < 30 || ttl > 900) + throw new RuntimeError('INVALID_TTL', 'Token TTL must be 30..900 seconds'); + const issued = Math.floor(Date.now() / 1000); + const claims: GitClaims = { + aud: 'wishforge-git', + tenantId, + projectId, + permission: body.permission, + iat: issued, + exp: issued + ttl, + ...(typeof body.runId === 'string' ? { runId: safeIdentifier(body.runId) } : {}), + }; + json(response, 200, { + token: issueGitToken(claims, config.tokenSecret), + expiresAt: new Date(claims.exp * 1000).toISOString(), + }); + } + return; + } + const match = + /^\/git\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\.git\/(info\/refs|git-upload-pack|git-receive-pack)$/.exec( + url.pathname, + ); + if (!match) { + json(response, 404, { error: 'NOT_FOUND' }); + return; + } + const tenantId = match[1]!, + projectId = match[2]!, + operation = match[3]!; + const service = operation === 'info/refs' ? url.searchParams.get('service') : operation; + if ( + !['git-upload-pack', 'git-receive-pack'].includes(service ?? '') || + (operation === 'info/refs' ? request.method !== 'GET' : request.method !== 'POST') + ) { + json(response, 400, { error: 'INVALID_GIT_OPERATION' }); + return; + } + const authorization = request.headers.authorization ?? ''; + if (!authorization.startsWith('Bearer ')) { + response.setHeader('www-authenticate', 'Bearer realm="wishforge-git"'); + json(response, 401, { error: 'UNAUTHORIZED' }); + return; + } + const claims = verifyGitToken(authorization.slice(7), config.tokenSecret, { + tenantId, + projectId, + write: service === 'git-receive-pack', + }); + const repository = await location(tenantId, projectId); + await stat(repository); + const child = spawn('git', ['http-backend'], { + env: { + ...gitEnv, + GIT_PROJECT_ROOT: root, + GIT_HTTP_EXPORT_ALL: '1', + PATH_INFO: `/${tenantId}/${projectId}.git/${operation}`, + REQUEST_METHOD: request.method!, + QUERY_STRING: url.searchParams.toString(), + CONTENT_TYPE: String(request.headers['content-type'] ?? ''), + CONTENT_LENGTH: String(request.headers['content-length'] ?? ''), + REMOTE_USER: claims.runId ?? 'wishforge', + REMOTE_ADDR: request.socket.remoteAddress ?? '', + HTTP_GIT_PROTOCOL: String(request.headers['git-protocol'] ?? ''), + }, + stdio: ['pipe', 'pipe', 'pipe'], + shell: false, + }); + let headers = Buffer.alloc(0), + headersSent = false, + bytes = 0; + const timer = setTimeout(() => child.kill('SIGKILL'), 300_000); + request.on('data', chunk => { + bytes += chunk.length; + if (bytes > (config.maxPackBytes ?? 512 * 1024 * 1024)) { + child.kill('SIGKILL'); + request.destroy(); + } + }); + request.on('aborted', () => child.kill('SIGKILL')); + response.on('close', () => { + if (!response.writableEnded) child.kill('SIGKILL'); + }); + child.stdout.on('data', (chunk: Buffer) => { + if (headersSent) { + response.write(chunk); + return; + } + headers = Buffer.concat([headers, chunk]); + const end = headers.indexOf('\r\n\r\n'); + if (end === -1) { + if (headers.length > 64 * 1024) child.kill('SIGKILL'); + return; + } + let status = 200; + const parsed: Record = {}; + for (const line of headers.subarray(0, end).toString().split('\r\n')) { + const separator = line.indexOf(':'); + if (separator < 1) continue; + const name = line.slice(0, separator).toLowerCase(), + value = line.slice(separator + 1).trim(); + if (name === 'status') status = parseInt(value, 10); + else if (['content-type', 'cache-control', 'expires', 'pragma', 'content-encoding'].includes(name)) + parsed[name] = value; + } + response.writeHead(status, parsed); + headersSent = true; + response.write(headers.subarray(end + 4)); + headers = Buffer.alloc(0); + }); + child.stderr.on('data', () => undefined); + child.stdin.on('error', () => undefined); + child.on('error', () => { + clearTimeout(timer); + if (!headersSent) json(response, 503, { error: 'GIT_BACKEND_UNAVAILABLE' }); + else response.destroy(); + }); + child.on('close', code => { + clearTimeout(timer); + if (!headersSent) json(response, code === 0 ? 502 : 503, { error: 'GIT_BACKEND_FAILED' }); + else response.end(); + }); + request.pipe(child.stdin); + })().catch(error => { + if (response.headersSent) { + response.destroy(); + return; + } + const code = error instanceof RuntimeError ? error.code : 'GIT_SERVICE_ERROR'; + json(response, code === 'UNAUTHORIZED' ? 401 : code === 'REPOSITORY_BUSY' ? 409 : 400, { error: code }); }); - child.stderr.on('data',()=>undefined);child.stdin.on('error',()=>undefined); - child.on('error',()=>{clearTimeout(timer);if(!headersSent)json(response,503,{error:'GIT_BACKEND_UNAVAILABLE'});else response.destroy();}); - child.on('close',code=>{clearTimeout(timer);if(!headersSent)json(response,code===0?502:503,{error:'GIT_BACKEND_FAILED'});else response.end();}); - request.pipe(child.stdin); - })().catch(error=>{if(response.headersSent){response.destroy();return;}const code=error instanceof RuntimeError?error.code:'GIT_SERVICE_ERROR';json(response,code==='UNAUTHORIZED'?401:code==='REPOSITORY_BUSY'?409:400,{error:code});});}); + }); return server; } diff --git a/apps/git-service/test/service.test.ts b/apps/git-service/test/service.test.ts index 517a8ae..161ba34 100644 --- a/apps/git-service/test/service.test.ts +++ b/apps/git-service/test/service.test.ts @@ -5,46 +5,130 @@ import path from 'node:path'; import { GitWorkspace } from '@wishforge/runtime'; import { createGitService, issueGitToken, verifyGitToken } from '../src/service.js'; -it('refuses the development HTTP override in production before creating storage',async()=>{vi.stubEnv('NODE_ENV','production');try{await expect(createGitService({root:'/unused-production-fixture',publicBaseUrl:'http://127.0.0.1:4504',adminToken:'admin-'.repeat(8),tokenSecret:'signing-'.repeat(8),allowLocalHttp:true})).rejects.toMatchObject({code:'INSECURE_ENDPOINT'});}finally{vi.unstubAllEnvs();}}); - -it('enforces repository, permission and expiry scope on short-lived Git tokens',()=>{ - const secret='s'.repeat(32);const token=issueGitToken({aud:'wishforge-git',tenantId:'tenant',projectId:'project',permission:'read',iat:100,exp:400},secret); - expect(verifyGitToken(token,secret,{tenantId:'tenant',projectId:'project',write:false},200).permission).toBe('read'); - expect(()=>verifyGitToken(token,secret,{tenantId:'other',projectId:'project',write:false},200)).toThrow(); - expect(()=>verifyGitToken(token,secret,{tenantId:'tenant',projectId:'project',write:true},200)).toThrow(); - expect(()=>verifyGitToken(token,secret,{tenantId:'tenant',projectId:'project',write:false},500)).toThrow(); +it('refuses the development HTTP override in production before creating storage', async () => { + vi.stubEnv('NODE_ENV', 'production'); + try { + await expect( + createGitService({ + root: '/unused-production-fixture', + publicBaseUrl: 'http://127.0.0.1:4504', + adminToken: 'admin-'.repeat(8), + tokenSecret: 'signing-'.repeat(8), + allowLocalHttp: true, + }), + ).rejects.toMatchObject({ code: 'INSECURE_ENDPOINT' }); + } finally { + vi.unstubAllEnvs(); + } }); -it('provisions a real bare repository and serves authenticated clone/CAS push over smart HTTP',async()=>{ - const root=await mkdtemp(path.join(os.tmpdir(),'wishforge-git-service-')); - const admin='admin-'.repeat(8);const server=await createGitService({root:path.join(root,'repos'),publicBaseUrl:'http://127.0.0.1:4504',adminToken:admin,tokenSecret:'signing-'.repeat(8),allowLocalHttp:true}); - await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(0,'127.0.0.1',resolve);}); - const address=server.address();if(!address || typeof address==='string')throw new Error('no port'); - const base=`http://127.0.0.1:${address.port}`; - const post=async(route:string,body:unknown)=>{const response=await fetch(`${base}${route}`,{method:'POST',headers:{authorization:`Bearer ${admin}`,'content-type':'application/json'},body:JSON.stringify(body)});expect(response.status).toBe(200);return response.json() as Promise>;}; - try{ - const repo=await post('/repos',{tenantId:'tenant',projectId:'project',defaultBranch:'release/v1'});expect(repo.initialCommit).toMatch(/^[a-f0-9]{40}$/);expect(repo.defaultBranch).toBe('release/v1'); - const repeated=await post('/repos',{tenantId:'tenant',projectId:'project',defaultBranch:'main'});expect(repeated.defaultBranch).toBe('release/v1');expect(repeated.initialCommit).toBe(repo.initialCommit); - expect((await post('/repos',{tenantId:'tenant',projectId:'default-main'})).defaultBranch).toBe('main'); - const repositoryUrl=`${base}/git/tenant/project.git`; - const reader=await post('/tokens',{tenantId:'tenant',projectId:'project',permission:'read'}); - const writer=await post('/tokens',{tenantId:'tenant',projectId:'project',permission:'write'}); - const git=new GitWorkspace(undefined,{allowLocalHttp:true,credential:{token:reader.token!,repositoryUrl}}); - const checkout=await git.create({root:path.join(root,'clones'),runId:'run',repository:repositoryUrl,targetBranch:'release/v1'}); - await writeFile(path.join(checkout.workspace,'README.md'),'A persistent project\n'); - const checkpoint=await git.checkpoint(checkout.workspace,checkout.baseCommit,'run','test'); - const push={repository:checkout.workspace,targetRef:'refs/heads/release/v1',expectedTargetCommit:checkout.baseCommit,candidateCommit:checkpoint.sourceCommit,verifiedCommit:checkpoint.sourceCommit}; +it('enforces repository, permission and expiry scope on short-lived Git tokens', () => { + const secret = 's'.repeat(32); + const token = issueGitToken( + { aud: 'wishforge-git', tenantId: 'tenant', projectId: 'project', permission: 'read', iat: 100, exp: 400 }, + secret, + ); + expect( + verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: false }, 200).permission, + ).toBe('read'); + expect(() => verifyGitToken(token, secret, { tenantId: 'other', projectId: 'project', write: false }, 200)).toThrow(); + expect(() => verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: true }, 200)).toThrow(); + expect(() => + verifyGitToken(token, secret, { tenantId: 'tenant', projectId: 'project', write: false }, 500), + ).toThrow(); +}); + +it('provisions a real bare repository and serves authenticated clone/CAS push over smart HTTP', async () => { + const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-git-service-')); + const admin = 'admin-'.repeat(8); + const server = await createGitService({ + root: path.join(root, 'repos'), + publicBaseUrl: 'http://127.0.0.1:4504', + adminToken: admin, + tokenSecret: 'signing-'.repeat(8), + allowLocalHttp: true, + }); + await new Promise((resolve, reject) => { + server.once('error', reject); + server.listen(0, '127.0.0.1', resolve); + }); + const address = server.address(); + if (!address || typeof address === 'string') throw new Error('no port'); + const base = `http://127.0.0.1:${address.port}`; + const post = async (route: string, body: unknown) => { + const response = await fetch(`${base}${route}`, { + method: 'POST', + headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' }, + body: JSON.stringify(body), + }); + expect(response.status).toBe(200); + return response.json() as Promise>; + }; + try { + const repo = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'release/v1' }); + expect(repo.initialCommit).toMatch(/^[a-f0-9]{40}$/); + expect(repo.defaultBranch).toBe('release/v1'); + const repeated = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'main' }); + expect(repeated.defaultBranch).toBe('release/v1'); + expect(repeated.initialCommit).toBe(repo.initialCommit); + expect((await post('/repos', { tenantId: 'tenant', projectId: 'default-main' })).defaultBranch).toBe('main'); + const repositoryUrl = `${base}/git/tenant/project.git`; + const reader = await post('/tokens', { tenantId: 'tenant', projectId: 'project', permission: 'read' }); + const writer = await post('/tokens', { tenantId: 'tenant', projectId: 'project', permission: 'write' }); + const git = new GitWorkspace(undefined, { + allowLocalHttp: true, + credential: { token: reader.token!, repositoryUrl }, + }); + const checkout = await git.create({ + root: path.join(root, 'clones'), + runId: 'run', + repository: repositoryUrl, + targetBranch: 'release/v1', + }); + await writeFile(path.join(checkout.workspace, 'README.md'), 'A persistent project\n'); + const checkpoint = await git.checkpoint(checkout.workspace, checkout.baseCommit, 'run', 'test'); + const push = { + repository: checkout.workspace, + targetRef: 'refs/heads/release/v1', + expectedTargetCommit: checkout.baseCommit, + candidateCommit: checkpoint.sourceCommit, + verifiedCommit: checkpoint.sourceCommit, + }; await expect(git.pushVerifiedCandidate(push)).rejects.toThrow(); - git.setCredential({token:writer.token!,repositoryUrl});await git.pushVerifiedCandidate(push); - const second=await git.create({root:path.join(root,'clones'),runId:'second',repository:repositoryUrl,targetBranch:'release/v1'}); + git.setCredential({ token: writer.token!, repositoryUrl }); + await git.pushVerifiedCandidate(push); + const second = await git.create({ + root: path.join(root, 'clones'), + runId: 'second', + repository: repositoryUrl, + targetBranch: 'release/v1', + }); expect(second.baseCommit).toBe(checkpoint.sourceCommit); - const afterPush=await post('/repos',{tenantId:'tenant',projectId:'project',defaultBranch:'other'});expect(afterPush.defaultBranch).toBe('release/v1');expect(afterPush.initialCommit).toBe(checkpoint.sourceCommit); - const unsafe=await fetch(`${base}/repos`,{method:'POST',headers:{authorization:`Bearer ${admin}`,'content-type':'application/json'},body:JSON.stringify({tenantId:'tenant',projectId:'invalid-branch',defaultBranch:'main:other'})});expect(unsafe.status).toBe(400); - const cross=await fetch(`${base}/git/other/project.git/info/refs?service=git-upload-pack`,{headers:{authorization:`Bearer ${reader.token!}`}}); + const afterPush = await post('/repos', { tenantId: 'tenant', projectId: 'project', defaultBranch: 'other' }); + expect(afterPush.defaultBranch).toBe('release/v1'); + expect(afterPush.initialCommit).toBe(checkpoint.sourceCommit); + const unsafe = await fetch(`${base}/repos`, { + method: 'POST', + headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' }, + body: JSON.stringify({ tenantId: 'tenant', projectId: 'invalid-branch', defaultBranch: 'main:other' }), + }); + expect(unsafe.status).toBe(400); + const cross = await fetch(`${base}/git/other/project.git/info/refs?service=git-upload-pack`, { + headers: { authorization: `Bearer ${reader.token!}` }, + }); expect(cross.status).toBe(401); await mkdir(path.join(root, 'outside.git')); await symlink(path.join(root, 'outside.git'), path.join(root, 'repos', 'tenant', 'escape.git')); - const escaped=await fetch(`${base}/repos`,{method:'POST',headers:{authorization:`Bearer ${admin}`,'content-type':'application/json'},body:JSON.stringify({tenantId:'tenant',projectId:'escape'})}); - expect(escaped.status).toBe(400);expect((await escaped.json()).error).toBe('REPOSITORY_SCOPE'); - }finally{server.closeAllConnections();await new Promise(resolve=>server.close(()=>resolve()));await rm(root,{recursive:true,force:true});} -},15000); + const escaped = await fetch(`${base}/repos`, { + method: 'POST', + headers: { authorization: `Bearer ${admin}`, 'content-type': 'application/json' }, + body: JSON.stringify({ tenantId: 'tenant', projectId: 'escape' }), + }); + expect(escaped.status).toBe(400); + expect((await escaped.json()).error).toBe('REPOSITORY_SCOPE'); + } finally { + server.closeAllConnections(); + await new Promise(resolve => server.close(() => resolve())); + await rm(root, { recursive: true, force: true }); + } +}, 15000); diff --git a/apps/git-service/tsconfig.json b/apps/git-service/tsconfig.json index 7f4b8b9..58d1749 100644 --- a/apps/git-service/tsconfig.json +++ b/apps/git-service/tsconfig.json @@ -1 +1,5 @@ -{ "extends": "../../tsconfig.base.json", "compilerOptions": { "noEmit": true, "types": ["node"] }, "include": ["src/**/*.ts", "test/**/*.ts"] } +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { "noEmit": true, "types": ["node"] }, + "include": ["src/**/*.ts", "test/**/*.ts"] +} diff --git a/apps/ide/collaboration/client-factory.cjs b/apps/ide/collaboration/client-factory.cjs index bd662b4..a4a7c2e 100644 --- a/apps/ide/collaboration/client-factory.cjs +++ b/apps/ide/collaboration/client-factory.cjs @@ -6,22 +6,44 @@ function collaborationBase(raw, browserLocation) { const location = new URL(browserLocation); const base = new URL(raw, location.origin); const match = /^\/workspaces\/([a-zA-Z0-9][a-zA-Z0-9_-]*)\//.exec(location.pathname); - if (!match || base.origin !== location.origin || base.username || base.password || base.search || base.hash || base.pathname !== `/workspaces/${match[1]}/collaboration/`) throw new Error('Collaboration must use this workspace authenticated gateway path'); + if ( + !match || + base.origin !== location.origin || + base.username || + base.password || + base.search || + base.hash || + base.pathname !== `/workspaces/${match[1]}/collaboration/` + ) + throw new Error('Collaboration must use this workspace authenticated gateway path'); return base; } function normalizeAuthMetadata(meta) { - const providers = (meta.providers ?? []).filter(provider => provider.type === 'form' && provider.name === 'unverified' && provider.endpoint === '/api/login/simple').map(provider => ({ ...provider, endpoint: 'api/login/simple' })); + const providers = (meta.providers ?? []) + .filter( + provider => + provider.type === 'form' && provider.name === 'unverified' && provider.endpoint === '/api/login/simple', + ) + .map(provider => ({ ...provider, endpoint: 'api/login/simple' })); if (providers.length !== 1) throw new Error('Workspace collaboration requires its isolated display-name provider'); return { ...meta, providers, loginPageUrl: undefined }; } function createPrefixTransport({ io, SocketIoTransport }, baseUrl) { const base = new URL(baseUrl); - return { id: 'socket.io', createTransport(_url, headers) { - // io(baseUrl) treats the pathname as a namespace and silently uses /socket.io. - // Explicit path and origin keep both Engine.IO polling and WS upgrade scoped. - const socket = io(base.origin, { path: `${base.pathname}socket.io`, extraHeaders: headers, withCredentials: true, transports: ['polling', 'websocket'] }); - return new SocketIoTransport(socket); - } }; + return { + id: 'socket.io', + createTransport(_url, headers) { + // io(baseUrl) treats the pathname as a namespace and silently uses /socket.io. + // Explicit path and origin keep both Engine.IO polling and WS upgrade scoped. + const socket = io(base.origin, { + path: `${base.pathname}socket.io`, + extraHeaders: headers, + withCredentials: true, + transports: ['polling', 'websocket'], + }); + return new SocketIoTransport(socket); + }, + }; } function createPrefixConnectionProvider(deps, options) { const base = collaborationBase(options.url, options.browserLocation); diff --git a/apps/ide/collaboration/start-isolated.mjs b/apps/ide/collaboration/start-isolated.mjs index 0e12d1d..f5efb68 100644 --- a/apps/ide/collaboration/start-isolated.mjs +++ b/apps/ide/collaboration/start-isolated.mjs @@ -5,10 +5,12 @@ import { initializeProtocol } from 'open-collaboration-protocol'; import { CollaborationServer, CredentialsManager, AuthEndpoint, SimpleLoginEndpoint } from 'open-collaboration-server'; const id = value => typeof value === 'string' && /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,199}$/.test(value); -if (!id(process.env.WF_WORKSPACE_ID) || !id(process.env.WF_TENANT_ID)) throw new Error('Each OCT instance requires its own WF_WORKSPACE_ID and WF_TENANT_ID'); +if (!id(process.env.WF_WORKSPACE_ID) || !id(process.env.WF_TENANT_ID)) + throw new Error('Each OCT instance requires its own WF_WORKSPACE_ID and WF_TENANT_ID'); const hostname = process.env.WF_OCT_HOST ?? '127.0.0.1'; if (!['127.0.0.1', '0.0.0.0'].includes(hostname)) throw new Error('Invalid isolated OCT listen address'); -if (hostname !== '127.0.0.1' && process.env.WISHFORGE_GATEWAY_ENFORCED !== 'true') throw new Error('Network binding requires an enforced workspace gateway and default-deny network policy'); +if (hostname !== '127.0.0.1' && process.env.WISHFORGE_GATEWAY_ENFORCED !== 'true') + throw new Error('Network binding requires an enforced workspace gateway and default-deny network policy'); const port = Number(process.env.WF_OCT_PORT ?? 8100); if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error('Invalid isolated OCT port'); // Never use OCT's shared development signing key. A restart invalidates all room @@ -17,7 +19,9 @@ process.env.OCT_JWT_PRIVATE_KEY = randomBytes(48).toString('base64url'); process.env.OCT_ACTIVATE_SIMPLE_LOGIN = 'true'; process.env.OCT_SERVER_OWNER = `WishForge workspace ${process.env.WF_WORKSPACE_ID}`; initializeProtocol({ cryptoModule: webcrypto }); -const { default: serverModule } = await import(new URL('./inversify-module.js', import.meta.resolve('open-collaboration-server')).href); +const { default: serverModule } = await import( + new URL('./inversify-module.js', import.meta.resolve('open-collaboration-server')).href +); const container = new Container(); container.load(serverModule); // This deployment deliberately has one display-name form behind the gateway; diff --git a/apps/ide/collaboration/test/client-factory.test.cjs b/apps/ide/collaboration/test/client-factory.test.cjs index d92c62f..d26c994 100644 --- a/apps/ide/collaboration/test/client-factory.test.cjs +++ b/apps/ide/collaboration/test/client-factory.test.cjs @@ -3,20 +3,52 @@ const assert = require('node:assert/strict'); const { collaborationBase, normalizeAuthMetadata, createPrefixTransport } = require('../client-factory.cjs'); test('collaboration URLs remain within the authenticated workspace and same origin', () => { const location = 'https://ide.example/workspaces/alpha/index.html'; - assert.equal(collaborationBase('https://ide.example/workspaces/alpha/collaboration/', location).pathname, '/workspaces/alpha/collaboration/'); - for (const url of ['https://external.example/workspaces/alpha/collaboration/', 'https://ide.example/workspaces/beta/collaboration/', 'https://ide.example/socket.io/', 'https://ide.example/workspaces/alpha/collaboration/?token=secret']) assert.throws(() => collaborationBase(url, location)); + assert.equal( + collaborationBase('https://ide.example/workspaces/alpha/collaboration/', location).pathname, + '/workspaces/alpha/collaboration/', + ); + for (const url of [ + 'https://external.example/workspaces/alpha/collaboration/', + 'https://ide.example/workspaces/beta/collaboration/', + 'https://ide.example/socket.io/', + 'https://ide.example/workspaces/alpha/collaboration/?token=secret', + ]) + assert.throws(() => collaborationBase(url, location)); }); test('simple display-name form URL cannot escape the workspace basepath', () => { - const meta = normalizeAuthMetadata({ providers: [{ type: 'form', name: 'unverified', endpoint: '/api/login/simple' }, { type: 'web', name: 'github', endpoint: 'https://external.example' }] }); + const meta = normalizeAuthMetadata({ + providers: [ + { type: 'form', name: 'unverified', endpoint: '/api/login/simple' }, + { type: 'web', name: 'github', endpoint: 'https://external.example' }, + ], + }); assert.equal(meta.providers.length, 1); - assert.equal(new URL(meta.providers[0].endpoint, 'https://ide.example/workspaces/alpha/collaboration/').pathname, '/workspaces/alpha/collaboration/api/login/simple'); + assert.equal( + new URL(meta.providers[0].endpoint, 'https://ide.example/workspaces/alpha/collaboration/').pathname, + '/workspaces/alpha/collaboration/api/login/simple', + ); assert.throws(() => normalizeAuthMetadata({ providers: [], loginPageUrl: 'https://external.example/login' })); }); test('Socket.IO uses origin namespace plus a workspace-specific Engine.IO path', () => { - let observed; const socket = {}; - const adapter = createPrefixTransport({ io: (...args) => { observed = args; return socket; }, SocketIoTransport: class { constructor(value) { this.socket = value; } } }, 'https://ide.example/workspaces/alpha/collaboration/'); + let observed; + const socket = {}; + const adapter = createPrefixTransport( + { + io: (...args) => { + observed = args; + return socket; + }, + SocketIoTransport: class { + constructor(value) { + this.socket = value; + } + }, + }, + 'https://ide.example/workspaces/alpha/collaboration/', + ); const transport = adapter.createTransport('ignored', { 'x-oct-jwt': 'room-token' }); - assert.equal(transport.socket, socket); assert.equal(observed[0], 'https://ide.example'); + assert.equal(transport.socket, socket); + assert.equal(observed[0], 'https://ide.example'); assert.equal(observed[1].path, '/workspaces/alpha/collaboration/socket.io'); assert.deepEqual(observed[1].transports, ['polling', 'websocket']); assert.equal(observed[1].extraHeaders['x-oct-jwt'], 'room-token'); diff --git a/apps/ide/collaboration/test/isolated-oct.test.mjs b/apps/ide/collaboration/test/isolated-oct.test.mjs index cb3e0e6..53424fe 100644 --- a/apps/ide/collaboration/test/isolated-oct.test.mjs +++ b/apps/ide/collaboration/test/isolated-oct.test.mjs @@ -16,83 +16,266 @@ const Y = await import('../../node_modules/yjs/dist/yjs.mjs'); const { Awareness } = await import('../../node_modules/y-protocols/awareness.js'); protocol.initializeProtocol({ cryptoModule: webcrypto }); const execute = promisify(execFile); -const docker = (...args) => execute('docker', ['--context', process.env.WF_TEST_DOCKER_CONTEXT ?? 'colima', ...args], { timeout: 30_000 }); +const docker = (...args) => + execute('docker', ['--context', process.env.WF_TEST_DOCKER_CONTEXT ?? 'colima', ...args], { timeout: 30_000 }); const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); -async function until(check, message) { const end = Date.now() + 10_000; do { if (await check()) return; await sleep(25); } while (Date.now() < end); throw new Error(message); } -async function listen(server) { await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); return `http://127.0.0.1:${server.address().port}`; } +async function until(check, message) { + const end = Date.now() + 10_000; + do { + if (await check()) return; + await sleep(25); + } while (Date.now() < end); + throw new Error(message); +} +async function listen(server) { + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + return `http://127.0.0.1:${server.address().port}`; +} -test('two isolated OCT instances support scoped Socket.IO/Yjs collaboration and reject invite/token reuse across workspaces', { skip: process.env.WF_OCT_INTEGRATION !== '1', timeout: 60_000 }, async () => { - const names = [], connections = [], docs = [], awareness = [], sockets = []; - let api, gateway; const revoked = new Set(); - const members = { alice: { workspace: 'workspace-a', role: 'developer' }, bob: { workspace: 'workspace-a', role: 'developer' }, charlie: { workspace: 'workspace-b', role: 'developer' }, viewer: { workspace: 'workspace-a', role: 'viewer' } }; - try { - const origins = {}; - for (const workspace of ['workspace-a', 'workspace-b']) { - const name = `wishforge-oct-test-${randomUUID().slice(0, 8)}`; names.push(name); - await docker('run', '-d', '--rm', '--name', name, '--read-only', '--cap-drop', 'ALL', '--security-opt', 'no-new-privileges', '--tmpfs', '/tmp:rw,noexec,nosuid,size=16m', '-p', '127.0.0.1::8100', '-e', `WF_WORKSPACE_ID=${workspace}`, '-e', 'WF_TENANT_ID=fixture-tenant', '-e', 'WISHFORGE_GATEWAY_ENFORCED=true', 'wishforge/oct-workspace:0.3.1-wf1'); - const port = (await docker('port', name, '8100/tcp')).stdout.trim().split(':').at(-1); origins[workspace] = `http://127.0.0.1:${port}`; - await until(async () => { try { return (await fetch(`${origins[workspace]}/api/meta`)).ok; } catch { return false; } }, 'OCT did not start'); +test( + 'two isolated OCT instances support scoped Socket.IO/Yjs collaboration and reject invite/token reuse across workspaces', + { skip: process.env.WF_OCT_INTEGRATION !== '1', timeout: 60_000 }, + async () => { + const names = [], + connections = [], + docs = [], + awareness = [], + sockets = []; + let api, gateway; + const revoked = new Set(); + const members = { + alice: { workspace: 'workspace-a', role: 'developer' }, + bob: { workspace: 'workspace-a', role: 'developer' }, + charlie: { workspace: 'workspace-b', role: 'developer' }, + viewer: { workspace: 'workspace-a', role: 'viewer' }, + }; + try { + const origins = {}; + for (const workspace of ['workspace-a', 'workspace-b']) { + const name = `wishforge-oct-test-${randomUUID().slice(0, 8)}`; + names.push(name); + await docker( + 'run', + '-d', + '--rm', + '--name', + name, + '--read-only', + '--cap-drop', + 'ALL', + '--security-opt', + 'no-new-privileges', + '--tmpfs', + '/tmp:rw,noexec,nosuid,size=16m', + '-p', + '127.0.0.1::8100', + '-e', + `WF_WORKSPACE_ID=${workspace}`, + '-e', + 'WF_TENANT_ID=fixture-tenant', + '-e', + 'WISHFORGE_GATEWAY_ENFORCED=true', + 'wishforge/oct-workspace:0.3.1-wf1', + ); + const port = (await docker('port', name, '8100/tcp')).stdout.trim().split(':').at(-1); + origins[workspace] = `http://127.0.0.1:${port}`; + await until(async () => { + try { + return (await fetch(`${origins[workspace]}/api/meta`)).ok; + } catch { + return false; + } + }, 'OCT did not start'); + } + api = http.createServer(async (req, res) => { + let text = ''; + for await (const bytes of req) text += bytes; + const request = JSON.parse(text), + who = members[request.userToken], + workspace = req.url.split('/')[3]; + if (!who || revoked.has(request.userToken) || who.workspace !== workspace) { + res.writeHead(403); + res.end(); + return; + } + assert.equal(request.operation, 'edit'); + res.setHeader('content-type', 'application/json'); + res.end( + JSON.stringify({ + data: { + tenantId: 'fixture-tenant', + projectId: `project-${workspace}`, + workspaceId: workspace, + role: who.role, + upstreamUrl: origins[workspace], + collaborationUpstreamUrl: origins[workspace], + }, + }), + ); + }); + const apiOrigin = await listen(api); + // Reserve a local port before gateway construction, which requires its public origin. + const reserve = http.createServer(); + const publicUrl = await listen(reserve); + await new Promise(resolve => reserve.close(resolve)); + gateway = createGateway({ + publicUrl, + tenantId: 'fixture-tenant', + serviceToken: 'gateway-fixture-token', + apiOrigin, + allowedUpstreamOrigins: Object.values(origins), + allowedCollaborationOrigins: Object.values(origins), + reauthorizeMs: 50, + }); + await new Promise(resolve => gateway.server.listen(Number(new URL(publicUrl).port), '127.0.0.1', resolve)); + async function session(user) { + const workspace = members[user].workspace; + const launch = await fetch(`${publicUrl}/launch`, { + method: 'POST', + headers: { authorization: `Bearer ${user}`, 'content-type': 'application/json' }, + body: JSON.stringify({ workspaceId: workspace }), + }); + if (!launch.ok) return { status: launch.status }; + const { launchUrl } = await launch.json(); + const response = await fetch(launchUrl, { redirect: 'manual' }); + return { status: response.status, cookie: response.headers.get('set-cookie').split(';')[0], workspace }; + } + const alice = await session('alice'), + bob = await session('bob'), + charlie = await session('charlie'); + assert.equal((await session('viewer')).status, 403); + function client(user, displayName) { + const url = `${publicUrl}/workspaces/${user.workspace}/collaboration/`; + const localFetch = (target, init = {}) => + fetch(target, { ...init, headers: { ...init.headers, cookie: user.cookie, origin: publicUrl } }); + return factory.createPrefixConnectionProvider( + { + ...protocol, + io: (origin, options) => { + const socket = io(origin, { + ...options, + withCredentials: false, + extraHeaders: { ...options.extraHeaders, Cookie: user.cookie, Origin: publicUrl }, + }); + sockets.push(socket); + socket.on('connect_error', error => + console.error( + 'OCT socket connection error:', + error.message, + error.description, + error.context?.status, + error.context?.responseText?.slice(0, 300), + ), + ); + return socket; + }, + }, + { + url, + browserLocation: `${publicUrl}/workspaces/${user.workspace}/`, + client: 'WishForge integration', + fetch: localFetch, + authenticationHandler: async (token, metadata) => { + assert.equal(metadata.providers[0].endpoint, 'api/login/simple'); + return ( + await localFetch(new URL(metadata.providers[0].endpoint, url), { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ token, user: displayName }), + }) + ).ok; + }, + }, + ); + } + const hostProvider = client(alice, 'Host display name'), + guestProvider = client(bob, 'Guest display name'); + const claim = await hostProvider.createRoom({ abortSignal: AbortSignal.timeout(10_000) }); + const host = await hostProvider.connect(claim.roomToken); + connections.push(host); + let hostIdentity; + host.peer.onInfo((_origin, peer) => { + hostIdentity = peer; + }); + host.peer.onJoinRequest(async () => ({ workspace: { name: 'fixture', folders: ['source'] } })); + host.room.onJoin((_origin, peer) => + host.peer.init(peer.id, { + protocol: protocol.VERSION, + host: hostIdentity, + guests: [peer], + permissions: { readonly: false }, + capabilities: {}, + workspace: { name: 'fixture', folders: ['source'] }, + }), + ); + await until(() => !!hostIdentity, 'Host peer did not connect'); + const guestClaim = await guestProvider.joinRoom({ + roomId: claim.roomId, + abortSignal: AbortSignal.timeout(10_000), + }); + const guest = await guestProvider.connect(guestClaim.roomToken, guestClaim.host); + connections.push(guest); + guest.peer.onInfo(() => {}); + let initialized = false; + guest.peer.onInit(() => { + initialized = true; + }); + await until(() => initialized, 'Guest did not receive encrypted initialization'); + for (const connection of [host, guest]) { + const doc = new Y.Doc(); + const state = new Awareness(doc); + docs.push(doc); + awareness.push(state); + const provider = new OpenCollaborationYjsProvider(connection, doc, state); + provider.connect(); + } + docs[0].getText('source/example.ts').insert(0, 'const greeting = "hello";'); + await until( + () => docs[1].getText('source/example.ts').toString().includes('hello'), + 'Host edit did not reach guest', + ); + docs[1].getText('source/example.ts').insert(docs[1].getText('source/example.ts').length, '\n// guest edit'); + await until( + () => docs[0].getText('source/example.ts').toString().includes('guest edit'), + 'Guest edit did not reach host', + ); + assert.equal(docs[0].getText('source/example.ts').toString(), docs[1].getText('source/example.ts').toString()); + assert.equal( + ( + await fetch(`${publicUrl}/workspaces/workspace-a/collaboration/api/meta`, { + headers: { cookie: charlie.cookie }, + }) + ).status, + 401, + ); + const outsider = client(charlie, 'Allowed in other workspace'); + await assert.rejects( + () => outsider.joinRoom({ roomId: claim.roomId, abortSignal: AbortSignal.timeout(5_000) }), + /Room not found/, + ); + const tokenCheck = await fetch(`${origins['workspace-b']}/api/login/validate`, { + method: 'POST', + headers: { 'x-oct-jwt': hostProvider.authToken }, + }); + assert.equal((await tokenCheck.json()).valid, false, 'Per-instance keys must reject another workspace OCT JWT'); + revoked.add('bob'); + await until(() => sockets[1].disconnected, 'Revoked member websocket was not closed'); + assert.equal( + (await fetch(`${publicUrl}/workspaces/workspace-a/collaboration/api/meta`, { headers: { cookie: bob.cookie } })) + .status, + 403, + ); + } catch (error) { + for (const name of names) console.error((await docker('logs', '--tail', '15', name)).stdout); + throw error; + } finally { + for (const state of awareness) state.destroy(); + for (const doc of docs) doc.destroy(); + for (const connection of connections) connection.dispose(); + for (const socket of sockets) socket.disconnect(); + if (gateway) await gateway.close(); + if (api) await new Promise(resolve => api.close(resolve)); + await Promise.all(names.map(name => docker('stop', '-t', '1', name).catch(() => undefined))); } - api = http.createServer(async (req, res) => { - let text = ''; for await (const bytes of req) text += bytes; - const request = JSON.parse(text), who = members[request.userToken], workspace = req.url.split('/')[3]; - if (!who || revoked.has(request.userToken) || who.workspace !== workspace) { res.writeHead(403); res.end(); return; } - assert.equal(request.operation, 'edit'); - res.setHeader('content-type', 'application/json'); res.end(JSON.stringify({ data: { tenantId: 'fixture-tenant', projectId: `project-${workspace}`, workspaceId: workspace, role: who.role, upstreamUrl: origins[workspace], collaborationUpstreamUrl: origins[workspace] } })); - }); - const apiOrigin = await listen(api); - // Reserve a local port before gateway construction, which requires its public origin. - const reserve = http.createServer(); const publicUrl = await listen(reserve); await new Promise(resolve => reserve.close(resolve)); - gateway = createGateway({ publicUrl, tenantId: 'fixture-tenant', serviceToken: 'gateway-fixture-token', apiOrigin, allowedUpstreamOrigins: Object.values(origins), allowedCollaborationOrigins: Object.values(origins), reauthorizeMs: 50 }); - await new Promise(resolve => gateway.server.listen(Number(new URL(publicUrl).port), '127.0.0.1', resolve)); - async function session(user) { - const workspace = members[user].workspace; - const launch = await fetch(`${publicUrl}/launch`, { method: 'POST', headers: { authorization: `Bearer ${user}`, 'content-type': 'application/json' }, body: JSON.stringify({ workspaceId: workspace }) }); - if (!launch.ok) return { status: launch.status }; - const { launchUrl } = await launch.json(); const response = await fetch(launchUrl, { redirect: 'manual' }); - return { status: response.status, cookie: response.headers.get('set-cookie').split(';')[0], workspace }; - } - const alice = await session('alice'), bob = await session('bob'), charlie = await session('charlie'); - assert.equal((await session('viewer')).status, 403); - function client(user, displayName) { - const url = `${publicUrl}/workspaces/${user.workspace}/collaboration/`; - const localFetch = (target, init = {}) => fetch(target, { ...init, headers: { ...init.headers, cookie: user.cookie, origin: publicUrl } }); - return factory.createPrefixConnectionProvider({ ...protocol, io: (origin, options) => { const socket = io(origin, { ...options, withCredentials: false, extraHeaders: { ...options.extraHeaders, Cookie: user.cookie, Origin: publicUrl } }); sockets.push(socket); socket.on('connect_error', error => console.error('OCT socket connection error:', error.message, error.description, error.context?.status, error.context?.responseText?.slice(0,300))); return socket; } }, { url, browserLocation: `${publicUrl}/workspaces/${user.workspace}/`, client: 'WishForge integration', fetch: localFetch, authenticationHandler: async (token, metadata) => { - assert.equal(metadata.providers[0].endpoint, 'api/login/simple'); - return (await localFetch(new URL(metadata.providers[0].endpoint, url), { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ token, user: displayName }) })).ok; - } }); - } - const hostProvider = client(alice, 'Host display name'), guestProvider = client(bob, 'Guest display name'); - const claim = await hostProvider.createRoom({ abortSignal: AbortSignal.timeout(10_000) }); - const host = await hostProvider.connect(claim.roomToken); connections.push(host); - let hostIdentity; host.peer.onInfo((_origin, peer) => { hostIdentity = peer; }); - host.peer.onJoinRequest(async () => ({ workspace: { name: 'fixture', folders: ['source'] } })); - host.room.onJoin((_origin, peer) => host.peer.init(peer.id, { protocol: protocol.VERSION, host: hostIdentity, guests: [peer], permissions: { readonly: false }, capabilities: {}, workspace: { name: 'fixture', folders: ['source'] } })); - await until(() => !!hostIdentity, 'Host peer did not connect'); - const guestClaim = await guestProvider.joinRoom({ roomId: claim.roomId, abortSignal: AbortSignal.timeout(10_000) }); - const guest = await guestProvider.connect(guestClaim.roomToken, guestClaim.host); connections.push(guest); guest.peer.onInfo(() => {}); - let initialized = false; guest.peer.onInit(() => { initialized = true; }); - await until(() => initialized, 'Guest did not receive encrypted initialization'); - for (const connection of [host, guest]) { const doc = new Y.Doc(); const state = new Awareness(doc); docs.push(doc); awareness.push(state); const provider = new OpenCollaborationYjsProvider(connection, doc, state); provider.connect(); } - docs[0].getText('source/example.ts').insert(0, 'const greeting = "hello";'); - await until(() => docs[1].getText('source/example.ts').toString().includes('hello'), 'Host edit did not reach guest'); - docs[1].getText('source/example.ts').insert(docs[1].getText('source/example.ts').length, '\n// guest edit'); - await until(() => docs[0].getText('source/example.ts').toString().includes('guest edit'), 'Guest edit did not reach host'); - assert.equal(docs[0].getText('source/example.ts').toString(), docs[1].getText('source/example.ts').toString()); - assert.equal((await fetch(`${publicUrl}/workspaces/workspace-a/collaboration/api/meta`, { headers: { cookie: charlie.cookie } })).status, 401); - const outsider = client(charlie, 'Allowed in other workspace'); - await assert.rejects(() => outsider.joinRoom({ roomId: claim.roomId, abortSignal: AbortSignal.timeout(5_000) }), /Room not found/); - const tokenCheck = await fetch(`${origins['workspace-b']}/api/login/validate`, { method: 'POST', headers: { 'x-oct-jwt': hostProvider.authToken } }); - assert.equal((await tokenCheck.json()).valid, false, 'Per-instance keys must reject another workspace OCT JWT'); - revoked.add('bob'); - await until(() => sockets[1].disconnected, 'Revoked member websocket was not closed'); - assert.equal((await fetch(`${publicUrl}/workspaces/workspace-a/collaboration/api/meta`, { headers: { cookie: bob.cookie } })).status, 403); - } catch (error) { - for (const name of names) console.error((await docker('logs', '--tail', '15', name)).stdout); - throw error; - } finally { - for (const state of awareness) state.destroy(); for (const doc of docs) doc.destroy(); for (const connection of connections) connection.dispose(); for (const socket of sockets) socket.disconnect(); - if (gateway) await gateway.close(); if (api) await new Promise(resolve => api.close(resolve)); - await Promise.all(names.map(name => docker('stop', '-t', '1', name).catch(() => undefined))); - } -}); + }, +); diff --git a/apps/ide/coordination/index.cjs b/apps/ide/coordination/index.cjs index d17e555..4059288 100644 --- a/apps/ide/coordination/index.cjs +++ b/apps/ide/coordination/index.cjs @@ -5,31 +5,70 @@ const path = require('node:path'); const { createHash, randomBytes } = require('node:crypto'); const { fileURLToPath } = require('node:url'); -const EXCLUDED = new Set(['.git', 'node_modules', 'dist', 'build', 'coverage', '.next', '.cache', 'src-gen', '.wishforge', '.wishforge-runtime', '.runtime', '.coordination']); +const EXCLUDED = new Set([ + '.git', + 'node_modules', + 'dist', + 'build', + 'coverage', + '.next', + '.cache', + 'src-gen', + '.wishforge', + '.wishforge-runtime', + '.runtime', + '.coordination', +]); const CLIENT_TTL_MS = 30_000; const BARRIER_TTL_MS = 30_000; const DEBUG_LEASE_MS = 30_000; -const DEFAULT_SNAPSHOT_LIMITS = Object.freeze({ maxFileBytes: 32 * 1024 * 1024, maxTotalBytes: 256 * 1024 * 1024, maxFiles: 20_000 }); +const DEFAULT_SNAPSHOT_LIMITS = Object.freeze({ + maxFileBytes: 32 * 1024 * 1024, + maxTotalBytes: 256 * 1024 * 1024, + maxFiles: 20_000, +}); class CoordinationError extends Error { - constructor(code, message) { super(message); this.name = 'CoordinationError'; this.code = code; } + constructor(code, message) { + super(message); + this.name = 'CoordinationError'; + this.code = code; + } +} +function fail(code, message) { + throw new CoordinationError(code, message); +} +function sha(value) { + return createHash('sha256').update(value).digest('hex'); +} +function clone(value) { + return JSON.parse(JSON.stringify(value)); +} +function inside(root, target) { + return target === root || target.startsWith(root + path.sep); } -function fail(code, message) { throw new CoordinationError(code, message); } -function sha(value) { return createHash('sha256').update(value).digest('hex'); } -function clone(value) { return JSON.parse(JSON.stringify(value)); } -function inside(root, target) { return target === root || target.startsWith(root + path.sep); } function identifier(value, name) { - if (typeof value !== 'string' || value.length < 1 || value.length > 256 || /[\u0000-\u001f]/.test(value)) fail('INVALID_INPUT', `Invalid ${name}`); + if (typeof value !== 'string' || value.length < 1 || value.length > 256 || /[\u0000-\u001f]/.test(value)) + fail('INVALID_INPUT', `Invalid ${name}`); return value; } function directorySync(directory) { const fd = fs.openSync(directory, fs.constants.O_RDONLY); - try { fs.fsyncSync(fd); } finally { fs.closeSync(fd); } + try { + fs.fsyncSync(fd); + } finally { + fs.closeSync(fd); + } } function atomicFile(destination, value, immutable = false) { const temporary = destination + '.' + randomBytes(16).toString('hex') + '.tmp'; const fd = fs.openSync(temporary, 'wx', 0o600); - try { fs.writeFileSync(fd, value); fs.fsyncSync(fd); } finally { fs.closeSync(fd); } + try { + fs.writeFileSync(fd, value); + fs.fsyncSync(fd); + } finally { + fs.closeSync(fd); + } try { if (immutable) { fs.chmodSync(temporary, 0o400); @@ -37,11 +76,14 @@ function atomicFile(destination, value, immutable = false) { fs.unlinkSync(temporary); } else fs.renameSync(temporary, destination); directorySync(path.dirname(destination)); - } finally { if (fs.existsSync(temporary)) fs.unlinkSync(temporary); } + } finally { + if (fs.existsSync(temporary)) fs.unlinkSync(temporary); + } } function ensurePrivateDirectory(directory) { fs.mkdirSync(directory, { recursive: true, mode: 0o700 }); - if (fs.lstatSync(directory).isSymbolicLink() || !fs.statSync(directory).isDirectory()) fail('UNSAFE_STATE_PATH', 'State path must be a real directory'); + if (fs.lstatSync(directory).isSymbolicLink() || !fs.statSync(directory).isDirectory()) + fail('UNSAFE_STATE_PATH', 'State path must be a real directory'); fs.chmodSync(directory, 0o700); } function prospectiveRealPath(directory) { @@ -50,25 +92,37 @@ function prospectiveRealPath(directory) { while (!fs.existsSync(existing)) { const parent = path.dirname(existing); if (parent === existing) fail('UNSAFE_STATE_PATH', 'State directory has no existing ancestor'); - missing.unshift(path.basename(existing)); existing = parent; + missing.unshift(path.basename(existing)); + existing = parent; } return path.join(fs.realpathSync(existing), ...missing); } class Coordinator { constructor({ workspaceRoot, stateDirectory, now = Date.now, limits = {} }) { - if (typeof workspaceRoot !== 'string' || typeof stateDirectory !== 'string' || typeof now !== 'function') fail('INVALID_INPUT', 'Workspace, state directory and clock are required'); - if (!limits || typeof limits !== 'object' || Array.isArray(limits) || Object.keys(limits).some(key => !Object.hasOwn(DEFAULT_SNAPSHOT_LIMITS, key))) fail('INVALID_INPUT', 'Invalid snapshot limits'); + if (typeof workspaceRoot !== 'string' || typeof stateDirectory !== 'string' || typeof now !== 'function') + fail('INVALID_INPUT', 'Workspace, state directory and clock are required'); + if ( + !limits || + typeof limits !== 'object' || + Array.isArray(limits) || + Object.keys(limits).some(key => !Object.hasOwn(DEFAULT_SNAPSHOT_LIMITS, key)) + ) + fail('INVALID_INPUT', 'Invalid snapshot limits'); this.limits = Object.freeze({ ...DEFAULT_SNAPSHOT_LIMITS, ...limits }); - for (const [key, value] of Object.entries(this.limits)) if (!Number.isSafeInteger(value) || value <= 0) fail('INVALID_INPUT', `${key} must be a positive safe integer`); + for (const [key, value] of Object.entries(this.limits)) + if (!Number.isSafeInteger(value) || value <= 0) fail('INVALID_INPUT', `${key} must be a positive safe integer`); this.requestedWorkspaceRoot = path.resolve(workspaceRoot); this.workspaceRoot = fs.realpathSync(workspaceRoot); if (!fs.statSync(this.workspaceRoot).isDirectory()) fail('INVALID_INPUT', 'Workspace root must be a directory'); const requested = path.resolve(stateDirectory); - if (inside(this.workspaceRoot, requested) || inside(requested, this.workspaceRoot)) fail('UNSAFE_STATE_PATH', 'State directory must be separate from the source workspace'); - if (fs.existsSync(requested) && fs.lstatSync(requested).isSymbolicLink()) fail('UNSAFE_STATE_PATH', 'State directory must not be a symlink'); + if (inside(this.workspaceRoot, requested) || inside(requested, this.workspaceRoot)) + fail('UNSAFE_STATE_PATH', 'State directory must be separate from the source workspace'); + if (fs.existsSync(requested) && fs.lstatSync(requested).isSymbolicLink()) + fail('UNSAFE_STATE_PATH', 'State directory must not be a symlink'); this.stateDirectory = prospectiveRealPath(requested); - if (inside(this.workspaceRoot, this.stateDirectory) || inside(this.stateDirectory, this.workspaceRoot)) fail('UNSAFE_STATE_PATH', 'State directory must be separate from the source workspace'); + if (inside(this.workspaceRoot, this.stateDirectory) || inside(this.stateDirectory, this.workspaceRoot)) + fail('UNSAFE_STATE_PATH', 'State directory must be separate from the source workspace'); ensurePrivateDirectory(this.stateDirectory); this.objectsDirectory = path.join(this.stateDirectory, 'objects'); this.snapshotsDirectory = path.join(this.stateDirectory, 'snapshots'); @@ -84,24 +138,46 @@ class Coordinator { _withLock(action) { const lock = path.join(this.stateDirectory, 'writer.lock'); let fd; - try { fd = fs.openSync(lock, 'wx', 0o600); } - catch (error) { + try { + fd = fs.openSync(lock, 'wx', 0o600); + } catch (error) { if (error.code !== 'EEXIST') throw error; // A crashed writer leaves a lock. Never steal a live or undecodable lock. let previous; - try { previous = JSON.parse(fs.readFileSync(lock, 'utf8')); } catch { fail('STATE_BUSY', 'State writer lock requires operator inspection'); } + try { + previous = JSON.parse(fs.readFileSync(lock, 'utf8')); + } catch { + fail('STATE_BUSY', 'State writer lock requires operator inspection'); + } if (!Number.isSafeInteger(previous.pid) || previous.pid <= 0) fail('STATE_BUSY', 'State writer lock is invalid'); - try { process.kill(previous.pid, 0); fail('STATE_BUSY', 'Another coordinator is writing state'); } - catch (probe) { if (probe.code !== 'ESRCH') throw probe; } + try { + process.kill(previous.pid, 0); + fail('STATE_BUSY', 'Another coordinator is writing state'); + } catch (probe) { + if (probe.code !== 'ESRCH') throw probe; + } + fs.unlinkSync(lock); + try { + fd = fs.openSync(lock, 'wx', 0o600); + } catch { + fail('STATE_BUSY', 'Another coordinator is writing state'); + } + } + try { + fs.writeFileSync(fd, JSON.stringify({ pid: process.pid })); + fs.fsyncSync(fd); + return action(); + } finally { + fs.closeSync(fd); fs.unlinkSync(lock); - try { fd = fs.openSync(lock, 'wx', 0o600); } catch { fail('STATE_BUSY', 'Another coordinator is writing state'); } } - try { fs.writeFileSync(fd, JSON.stringify({ pid: process.pid })); fs.fsyncSync(fd); return action(); } - finally { fs.closeSync(fd); fs.unlinkSync(lock); } } _recover() { - const snapshots = fs.readdirSync(this.snapshotsDirectory).filter(name => /^\d{12}\.json$/.test(name)).sort(); + const snapshots = fs + .readdirSync(this.snapshotsDirectory) + .filter(name => /^\d{12}\.json$/.test(name)) + .sort(); let revision = 0; let latest = null; const commits = new Map(); @@ -110,14 +186,30 @@ class Coordinator { if (!fs.lstatSync(file).isFile()) fail('STATE_CORRUPT', 'Snapshot is not a regular file'); const snapshot = JSON.parse(fs.readFileSync(file, 'utf8')); const { id, ...body } = snapshot; - if (snapshot.schemaVersion !== 1 || snapshot.workspace !== this.workspaceRoot || snapshot.revision !== revision + 1 || snapshot.previousId !== (latest?.id ?? null) || id !== sha(JSON.stringify(body)) || name !== `${String(snapshot.revision).padStart(12, '0')}.json`) fail('STATE_CORRUPT', 'Snapshot chain failed integrity verification'); - if (!Array.isArray(snapshot.files) || snapshot.digest !== sha(JSON.stringify(snapshot.files))) fail('STATE_CORRUPT', 'Snapshot manifest failed integrity verification'); + if ( + snapshot.schemaVersion !== 1 || + snapshot.workspace !== this.workspaceRoot || + snapshot.revision !== revision + 1 || + snapshot.previousId !== (latest?.id ?? null) || + id !== sha(JSON.stringify(body)) || + name !== `${String(snapshot.revision).padStart(12, '0')}.json` + ) + fail('STATE_CORRUPT', 'Snapshot chain failed integrity verification'); + if (!Array.isArray(snapshot.files) || snapshot.digest !== sha(JSON.stringify(snapshot.files))) + fail('STATE_CORRUPT', 'Snapshot manifest failed integrity verification'); for (const item of snapshot.files) { - if (typeof item.path !== 'string' || path.posix.isAbsolute(item.path) || item.path.split('/').some(part => part === '..' || part === '' || part === '.') || !/^[a-f0-9]{64}$/.test(item.sha256)) fail('STATE_CORRUPT', 'Snapshot contains an unsafe file entry'); + if ( + typeof item.path !== 'string' || + path.posix.isAbsolute(item.path) || + item.path.split('/').some(part => part === '..' || part === '' || part === '.') || + !/^[a-f0-9]{64}$/.test(item.sha256) + ) + fail('STATE_CORRUPT', 'Snapshot contains an unsafe file entry'); const object = path.join(this.objectsDirectory, item.sha256); if (!fs.lstatSync(object).isFile()) fail('STATE_CORRUPT', 'Snapshot object is not a regular file'); const bytes = fs.readFileSync(object); - if (bytes.length !== item.size || sha(bytes) !== item.sha256) fail('STATE_CORRUPT', 'Snapshot content failed integrity verification'); + if (bytes.length !== item.size || sha(bytes) !== item.sha256) + fail('STATE_CORRUPT', 'Snapshot content failed integrity verification'); } revision = snapshot.revision; latest = snapshot; @@ -128,57 +220,106 @@ class Coordinator { if (fs.existsSync(journalPath)) { if (!fs.lstatSync(journalPath).isFile()) fail('STATE_CORRUPT', 'Journal is not a regular file'); const journal = JSON.parse(fs.readFileSync(journalPath, 'utf8')); - if (journal.schemaVersion !== 1 || journal.workspace !== this.workspaceRoot || !Number.isSafeInteger(journal.revision) || journal.revision > revision || journal.revision < 0 || (journal.revision === revision && journal.latestId !== (latest?.id ?? null))) fail('STATE_CORRUPT', 'Journal does not match committed snapshots'); + if ( + journal.schemaVersion !== 1 || + journal.workspace !== this.workspaceRoot || + !Number.isSafeInteger(journal.revision) || + journal.revision > revision || + journal.revision < 0 || + (journal.revision === revision && journal.latestId !== (latest?.id ?? null)) + ) + fail('STATE_CORRUPT', 'Journal does not match committed snapshots'); } this.revision = revision; this.latestSnapshot = latest; this.commits = commits; // A committed immutable manifest is the transaction record. Repair a stale/missing head after crash. - atomicFile(journalPath, JSON.stringify({ schemaVersion: 1, workspace: this.workspaceRoot, revision, latestId: latest?.id ?? null })); + atomicFile( + journalPath, + JSON.stringify({ schemaVersion: 1, workspace: this.workspaceRoot, revision, latestId: latest?.id ?? null }), + ); } _documents(payload) { - if (!payload || !Array.isArray(payload.documents) || payload.documents.length > 10_000 || (payload.dirty !== undefined && typeof payload.dirty !== 'boolean')) fail('INVALID_INPUT', 'A document version vector is required'); + if ( + !payload || + !Array.isArray(payload.documents) || + payload.documents.length > 10_000 || + (payload.dirty !== undefined && typeof payload.dirty !== 'boolean') + ) + fail('INVALID_INPUT', 'A document version vector is required'); const seen = new Set(); - const documents = payload.documents.map(document => { - if (!document || typeof document.uri !== 'string' || document.uri.length < 1 || document.uri.length > 8192 || /[\u0000-\u001f]/.test(document.uri) || !Number.isSafeInteger(document.version) || document.version < 0 || typeof document.dirty !== 'boolean') fail('INVALID_INPUT', 'Invalid document version'); - let uri = document.uri; - if (uri.startsWith('file:')) { - let target; - try { target = fileURLToPath(uri); } catch { fail('INVALID_INPUT', 'Invalid file URI'); } - // macOS /var and /tmp aliases, or a provisioned root alias, refer to the same configured workspace. - if (inside(this.requestedWorkspaceRoot, path.resolve(target))) target = path.resolve(this.workspaceRoot, path.relative(this.requestedWorkspaceRoot, target)); - if (!inside(this.workspaceRoot, path.resolve(target))) fail('OUTSIDE_WORKSPACE', 'Document is outside this workspace'); - uri = path.relative(this.workspaceRoot, target).split(path.sep).join('/'); - } else if (!/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(uri)) { - const target = path.resolve(this.workspaceRoot, uri); - if (!inside(this.workspaceRoot, target)) fail('OUTSIDE_WORKSPACE', 'Document is outside this workspace'); - uri = path.relative(this.workspaceRoot, target).split(path.sep).join('/'); - } - // Non-file editor URIs (including collaboration and untitled documents) are opaque IDs, never paths to read. - if (seen.has(uri)) fail('INVALID_INPUT', 'Duplicate document URI'); - seen.add(uri); - return { uri, version: document.version, dirty: document.dirty }; - }).sort((left, right) => left.uri.localeCompare(right.uri)); + const documents = payload.documents + .map(document => { + if ( + !document || + typeof document.uri !== 'string' || + document.uri.length < 1 || + document.uri.length > 8192 || + /[\u0000-\u001f]/.test(document.uri) || + !Number.isSafeInteger(document.version) || + document.version < 0 || + typeof document.dirty !== 'boolean' + ) + fail('INVALID_INPUT', 'Invalid document version'); + let uri = document.uri; + if (uri.startsWith('file:')) { + let target; + try { + target = fileURLToPath(uri); + } catch { + fail('INVALID_INPUT', 'Invalid file URI'); + } + // macOS /var and /tmp aliases, or a provisioned root alias, refer to the same configured workspace. + if (inside(this.requestedWorkspaceRoot, path.resolve(target))) + target = path.resolve(this.workspaceRoot, path.relative(this.requestedWorkspaceRoot, target)); + if (!inside(this.workspaceRoot, path.resolve(target))) + fail('OUTSIDE_WORKSPACE', 'Document is outside this workspace'); + uri = path.relative(this.workspaceRoot, target).split(path.sep).join('/'); + } else if (!/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(uri)) { + const target = path.resolve(this.workspaceRoot, uri); + if (!inside(this.workspaceRoot, target)) fail('OUTSIDE_WORKSPACE', 'Document is outside this workspace'); + uri = path.relative(this.workspaceRoot, target).split(path.sep).join('/'); + } + // Non-file editor URIs (including collaboration and untitled documents) are opaque IDs, never paths to read. + if (seen.has(uri)) fail('INVALID_INPUT', 'Duplicate document URI'); + seen.add(uri); + return { uri, version: document.version, dirty: document.dirty }; + }) + .sort((left, right) => left.uri.localeCompare(right.uri)); return { documents, dirty: payload.dirty === true || documents.some(document => document.dirty) }; } - _versions(documents) { return JSON.stringify(documents.map(({ uri, version }) => ({ uri, version }))); } - _invalidate(reason) { - for (const barrier of this.barriers.values()) if (barrier.status === 'pending') { barrier.status = 'invalid'; barrier.reason = reason; } + _versions(documents) { + return JSON.stringify(documents.map(({ uri, version }) => ({ uri, version }))); + } + _invalidate(reason) { + for (const barrier of this.barriers.values()) + if (barrier.status === 'pending') { + barrier.status = 'invalid'; + barrier.reason = reason; + } + } + _activeClients() { + return [...this.clients.values()].filter(client => this.now() - client.lastSeen < CLIENT_TTL_MS); } - _activeClients() { return [...this.clients.values()].filter(client => this.now() - client.lastSeen < CLIENT_TTL_MS); } register(actorId, clientId, payload) { - identifier(actorId, 'actor ID'); identifier(clientId, 'client ID'); + identifier(actorId, 'actor ID'); + identifier(clientId, 'client ID'); const existing = this.clients.get(clientId); if (existing && existing.actorId !== actorId) fail('CLIENT_OWNERSHIP', 'Client belongs to another actor'); const normalized = this._documents(payload); for (const barrier of this.barriers.values()) { if (barrier.status !== 'pending') continue; const participant = barrier.participants.find(item => item.clientId === clientId); - if (!participant || this._versions(normalized.documents) !== this._versions(participant.documents) || normalized.dirty) { - barrier.status = 'invalid'; barrier.reason = 'Client membership or document version changed'; + if ( + !participant || + this._versions(normalized.documents) !== this._versions(participant.documents) || + normalized.dirty + ) { + barrier.status = 'invalid'; + barrier.reason = 'Client membership or document version changed'; } } const client = { actorId, clientId, ...normalized, lastSeen: this.now() }; @@ -187,12 +328,17 @@ class Coordinator { } unregister(actorId, clientId) { - identifier(actorId, 'actor ID'); identifier(clientId, 'client ID'); + identifier(actorId, 'actor ID'); + identifier(clientId, 'client ID'); const client = this.clients.get(clientId); if (!client) return { unregistered: true }; if (client.actorId !== actorId) fail('CLIENT_OWNERSHIP', 'Client belongs to another actor'); this.clients.delete(clientId); - for (const barrier of this.barriers.values()) if (barrier.status === 'pending' && barrier.participants.some(item => item.clientId === clientId)) { barrier.status = 'invalid'; barrier.reason = 'Participant disconnected'; } + for (const barrier of this.barriers.values()) + if (barrier.status === 'pending' && barrier.participants.some(item => item.clientId === clientId)) { + barrier.status = 'invalid'; + barrier.reason = 'Participant disconnected'; + } return { unregistered: true }; } @@ -202,7 +348,8 @@ class Coordinator { let totalBytes = 0; const checkSize = size => { if (size > BigInt(this.limits.maxFileBytes)) fail('SNAPSHOT_LIMIT', 'Snapshot file exceeds maxFileBytes'); - if (size + BigInt(totalBytes) > BigInt(this.limits.maxTotalBytes)) fail('SNAPSHOT_LIMIT', 'Snapshot exceeds maxTotalBytes'); + if (size + BigInt(totalBytes) > BigInt(this.limits.maxTotalBytes)) + fail('SNAPSHOT_LIMIT', 'Snapshot exceeds maxTotalBytes'); }; const walk = (directory, relative = '') => { for (const name of fs.readdirSync(directory).sort()) { @@ -212,19 +359,23 @@ class Coordinator { if (entry.isSymbolicLink()) fail('UNSAFE_WORKSPACE_PATH', 'Snapshots reject symbolic links'); const logical = relative ? relative + '/' + name : name; if (entry.isDirectory()) { - if (!inside(this.workspaceRoot, fs.realpathSync(absolute))) fail('UNSAFE_WORKSPACE_PATH', 'Directory escaped the workspace'); + if (!inside(this.workspaceRoot, fs.realpathSync(absolute))) + fail('UNSAFE_WORKSPACE_PATH', 'Directory escaped the workspace'); walk(absolute, logical); } else if (entry.isFile()) { if (files.length >= this.limits.maxFiles) fail('SNAPSHOT_LIMIT', 'Snapshot exceeds maxFiles'); - if (!Number.isSafeInteger(entry.size) || entry.size < 0) fail('SNAPSHOT_LIMIT', 'Snapshot file has unsupported size'); + if (!Number.isSafeInteger(entry.size) || entry.size < 0) + fail('SNAPSHOT_LIMIT', 'Snapshot file has unsupported size'); checkSize(BigInt(entry.size)); // Check both the path and open descriptor; O_NOFOLLOW rejects last-component symlink races. - if (!inside(this.workspaceRoot, fs.realpathSync(absolute))) fail('UNSAFE_WORKSPACE_PATH', 'File escaped the workspace'); + if (!inside(this.workspaceRoot, fs.realpathSync(absolute))) + fail('UNSAFE_WORKSPACE_PATH', 'File escaped the workspace'); const fd = fs.openSync(absolute, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW); let bytes; try { const before = fs.fstatSync(fd, { bigint: true }); - if (!before.isFile() || before.ino !== BigInt(entry.ino) || before.dev !== BigInt(entry.dev)) fail('DISK_CHANGED', 'File changed while reading'); + if (!before.isFile() || before.ino !== BigInt(entry.ino) || before.dev !== BigInt(entry.dev)) + fail('DISK_CHANGED', 'File changed while reading'); checkSize(before.size); // A file may grow after fstat. Never let readFileSync allocate from // that mutable size; read only the already-validated bounded length. @@ -238,8 +389,19 @@ class Coordinator { const extra = fs.readSync(fd, Buffer.alloc(1), 0, 1, null); const after = fs.fstatSync(fd, { bigint: true }); checkSize(after.size); - if (read !== bytes.length || extra !== 0 || before.ino !== after.ino || before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs || after.size !== BigInt(bytes.length)) fail('DISK_CHANGED', 'File changed while reading'); - } finally { fs.closeSync(fd); } + if ( + read !== bytes.length || + extra !== 0 || + before.ino !== after.ino || + before.size !== after.size || + before.mtimeNs !== after.mtimeNs || + before.ctimeNs !== after.ctimeNs || + after.size !== BigInt(bytes.length) + ) + fail('DISK_CHANGED', 'File changed while reading'); + } finally { + fs.closeSync(fd); + } totalBytes += bytes.length; const digest = sha(bytes); if (retainBlobs) blobs.set(digest, bytes); @@ -253,17 +415,35 @@ class Coordinator { beginBarrier(actorId) { identifier(actorId, 'actor ID'); - for (const barrier of this.barriers.values()) if (barrier.status === 'pending' && this.now() < barrier.expiresAt) fail('BARRIER_BUSY', 'A save barrier is already pending'); + for (const barrier of this.barriers.values()) + if (barrier.status === 'pending' && this.now() < barrier.expiresAt) + fail('BARRIER_BUSY', 'A save barrier is already pending'); const active = this._activeClients(); - if (!active.some(client => client.actorId === actorId)) fail('CLIENT_REQUIRED', 'Register an active client before starting a barrier'); + if (!active.some(client => client.actorId === actorId)) + fail('CLIENT_REQUIRED', 'Register an active client before starting a barrier'); const disk = this._capture(false); const barrier = { - id: randomBytes(24).toString('hex'), actorId, status: 'pending', createdAt: this.now(), expiresAt: this.now() + BARRIER_TTL_MS, - diskDigest: disk.digest, participants: active.map(client => ({ actorId: client.actorId, clientId: client.clientId, documents: clone(client.documents), acknowledged: false })), + id: randomBytes(24).toString('hex'), + actorId, + status: 'pending', + createdAt: this.now(), + expiresAt: this.now() + BARRIER_TTL_MS, + diskDigest: disk.digest, + participants: active.map(client => ({ + actorId: client.actorId, + clientId: client.clientId, + documents: clone(client.documents), + acknowledged: false, + })), }; this.barriers.set(barrier.id, barrier); // Bound ephemeral metadata without discarding an in-flight barrier. - if (this.barriers.size > 100) for (const [id, previous] of this.barriers) if (previous.status !== 'pending' || previous.expiresAt <= this.now()) { this.barriers.delete(id); break; } + if (this.barriers.size > 100) + for (const [id, previous] of this.barriers) + if (previous.status !== 'pending' || previous.expiresAt <= this.now()) { + this.barriers.delete(id); + break; + } return clone(barrier); } @@ -271,20 +451,31 @@ class Coordinator { const barrier = this.barriers.get(id); if (!barrier) fail('BARRIER_NOT_FOUND', 'Save barrier does not exist'); if (barrier.status !== 'pending') fail('BARRIER_INVALID', barrier.reason || 'Save barrier is no longer pending'); - if (this.now() >= barrier.expiresAt) { barrier.status = 'expired'; fail('BARRIER_EXPIRED', 'Save barrier expired'); } + if (this.now() >= barrier.expiresAt) { + barrier.status = 'expired'; + fail('BARRIER_EXPIRED', 'Save barrier expired'); + } return barrier; } acknowledge(actorId, barrierId, clientId, payload) { - identifier(actorId, 'actor ID'); identifier(clientId, 'client ID'); + identifier(actorId, 'actor ID'); + identifier(clientId, 'client ID'); const barrier = this._barrier(barrierId); const participant = barrier.participants.find(item => item.clientId === clientId); const client = this.clients.get(clientId); - if (!participant || !client || participant.actorId !== actorId || client.actorId !== actorId) fail('CLIENT_OWNERSHIP', 'Only the participant may acknowledge its own saved documents'); + if (!participant || !client || participant.actorId !== actorId || client.actorId !== actorId) + fail('CLIENT_OWNERSHIP', 'Only the participant may acknowledge its own saved documents'); const normalized = this._documents(payload); - if (normalized.dirty || client.dirty) fail('DIRTY_DOCUMENTS', 'All documents must be saved and registered clean before acknowledgement'); - if (this._versions(normalized.documents) !== this._versions(participant.documents) || this._versions(client.documents) !== this._versions(participant.documents)) { - barrier.status = 'invalid'; barrier.reason = 'Document version changed'; fail('VERSION_CHANGED', barrier.reason); + if (normalized.dirty || client.dirty) + fail('DIRTY_DOCUMENTS', 'All documents must be saved and registered clean before acknowledgement'); + if ( + this._versions(normalized.documents) !== this._versions(participant.documents) || + this._versions(client.documents) !== this._versions(participant.documents) + ) { + barrier.status = 'invalid'; + barrier.reason = 'Document version changed'; + fail('VERSION_CHANGED', barrier.reason); } if (this.now() - client.lastSeen >= CLIENT_TTL_MS) fail('CLIENT_EXPIRED', 'Client heartbeat expired'); participant.acknowledged = true; @@ -295,9 +486,11 @@ class Coordinator { for (const participant of barrier.participants) { const client = this.clients.get(participant.clientId); if (!participant.acknowledged) fail('ACK_REQUIRED', 'Every frozen participant must acknowledge saving'); - if (!client || client.actorId !== participant.actorId || this.now() - client.lastSeen >= CLIENT_TTL_MS) fail('CLIENT_EXPIRED', 'A participant disconnected or expired'); + if (!client || client.actorId !== participant.actorId || this.now() - client.lastSeen >= CLIENT_TTL_MS) + fail('CLIENT_EXPIRED', 'A participant disconnected or expired'); if (client.dirty) fail('DIRTY_DOCUMENTS', 'A participant has unsaved documents'); - if (this._versions(client.documents) !== this._versions(participant.documents)) fail('VERSION_CHANGED', 'A participant changed document versions'); + if (this._versions(client.documents) !== this._versions(participant.documents)) + fail('VERSION_CHANGED', 'A participant changed document versions'); } } @@ -305,7 +498,8 @@ class Coordinator { identifier(actorId, 'actor ID'); const committed = this.commits.get(barrierId); if (committed) { - if (committed.actorId !== actorId) fail('BARRIER_OWNERSHIP', 'Only the barrier creator may retrieve its committed result'); + if (committed.actorId !== actorId) + fail('BARRIER_OWNERSHIP', 'Only the barrier creator may retrieve its committed result'); return clone(committed); } const barrier = this._barrier(barrierId); @@ -315,33 +509,66 @@ class Coordinator { this._recover(); const recovered = this.commits.get(barrierId); if (recovered) { - if (recovered.actorId !== actorId) fail('BARRIER_OWNERSHIP', 'Only the barrier creator may retrieve its committed result'); - barrier.status = 'committed'; barrier.snapshotId = recovered.id; barrier.revision = recovered.revision; + if (recovered.actorId !== actorId) + fail('BARRIER_OWNERSHIP', 'Only the barrier creator may retrieve its committed result'); + barrier.status = 'committed'; + barrier.snapshotId = recovered.id; + barrier.revision = recovered.revision; return clone(recovered); } const disk = this._capture(); - if (disk.digest !== barrier.diskDigest) { barrier.status = 'invalid'; barrier.reason = 'Disk changed during save barrier'; fail('DISK_CHANGED', barrier.reason); } + if (disk.digest !== barrier.diskDigest) { + barrier.status = 'invalid'; + barrier.reason = 'Disk changed during save barrier'; + fail('DISK_CHANGED', barrier.reason); + } for (const [digest, bytes] of disk.blobs) { const object = path.join(this.objectsDirectory, digest); if (fs.existsSync(object)) { - if (!fs.lstatSync(object).isFile() || sha(fs.readFileSync(object)) !== digest) fail('STATE_CORRUPT', 'Existing content-addressed object is invalid'); + if (!fs.lstatSync(object).isFile() || sha(fs.readFileSync(object)) !== digest) + fail('STATE_CORRUPT', 'Existing content-addressed object is invalid'); } else atomicFile(object, bytes, true); } // Re-read immediately before committing; saved snapshot content is never read from mutable source later. - if (this._capture(false).digest !== disk.digest) { barrier.status = 'invalid'; barrier.reason = 'Disk changed while materializing snapshot'; fail('DISK_CHANGED', barrier.reason); } + if (this._capture(false).digest !== disk.digest) { + barrier.status = 'invalid'; + barrier.reason = 'Disk changed while materializing snapshot'; + fail('DISK_CHANGED', barrier.reason); + } this._barrier(barrierId); this._assertParticipants(barrier); const body = { - schemaVersion: 1, workspace: this.workspaceRoot, revision: this.revision + 1, previousId: this.latestSnapshot?.id ?? null, - digest: disk.digest, files: disk.files, createdAt: this.now(), actorId, barrierId, + schemaVersion: 1, + workspace: this.workspaceRoot, + revision: this.revision + 1, + previousId: this.latestSnapshot?.id ?? null, + digest: disk.digest, + files: disk.files, + createdAt: this.now(), + actorId, + barrierId, }; const snapshot = { id: sha(JSON.stringify(body)), ...body }; - atomicFile(path.join(this.snapshotsDirectory, `${String(snapshot.revision).padStart(12, '0')}.json`), JSON.stringify(snapshot), true); + atomicFile( + path.join(this.snapshotsDirectory, `${String(snapshot.revision).padStart(12, '0')}.json`), + JSON.stringify(snapshot), + true, + ); this.revision = snapshot.revision; this.latestSnapshot = snapshot; - atomicFile(path.join(this.stateDirectory, 'journal.json'), JSON.stringify({ schemaVersion: 1, workspace: this.workspaceRoot, revision: this.revision, latestId: snapshot.id })); + atomicFile( + path.join(this.stateDirectory, 'journal.json'), + JSON.stringify({ + schemaVersion: 1, + workspace: this.workspaceRoot, + revision: this.revision, + latestId: snapshot.id, + }), + ); this.commits.set(barrierId, snapshot); - barrier.status = 'committed'; barrier.snapshotId = snapshot.id; barrier.revision = snapshot.revision; + barrier.status = 'committed'; + barrier.snapshotId = snapshot.id; + barrier.revision = snapshot.revision; return clone(snapshot); }); } @@ -354,7 +581,8 @@ class Coordinator { identifier(actorId, 'actor ID'); const current = this._liveDebugLease(); if (current) { - if (current.actorId !== actorId || current.token !== token) fail('DEBUG_BUSY', 'Debug control is held by a live lease'); + if (current.actorId !== actorId || current.token !== token) + fail('DEBUG_BUSY', 'Debug control is held by a live lease'); current.expiresAt = this.now() + DEBUG_LEASE_MS; return clone(current); } @@ -363,34 +591,79 @@ class Coordinator { } assertDebugController(actorId, token) { const lease = this._liveDebugLease(); - if (!lease || lease.actorId !== actorId || typeof token !== 'string' || token !== lease.token) fail('DEBUG_FORBIDDEN', 'An active debug controller lease is required'); + if (!lease || lease.actorId !== actorId || typeof token !== 'string' || token !== lease.token) + fail('DEBUG_FORBIDDEN', 'An active debug controller lease is required'); return { actorId, expiresAt: lease.expiresAt }; } debugTransfer(actorId, token, targetActorId) { identifier(targetActorId, 'target actor ID'); this.assertDebugController(actorId, token); - if (!this._activeClients().some(client => client.actorId === targetActorId)) fail('CLIENT_REQUIRED', 'Target controller must have an active registered client'); - this.debugLease = { actorId: targetActorId, token: randomBytes(32).toString('hex'), expiresAt: this.now() + DEBUG_LEASE_MS }; + if (!this._activeClients().some(client => client.actorId === targetActorId)) + fail('CLIENT_REQUIRED', 'Target controller must have an active registered client'); + this.debugLease = { + actorId: targetActorId, + token: randomBytes(32).toString('hex'), + expiresAt: this.now() + DEBUG_LEASE_MS, + }; return clone(this.debugLease); } - debugRelease(actorId, token) { this.assertDebugController(actorId, token); this.debugLease = null; return { released: true }; } + debugRelease(actorId, token) { + this.assertDebugController(actorId, token); + this.debugLease = null; + return { released: true }; + } revoke(actorId) { identifier(actorId, 'actor ID'); for (const [id, client] of this.clients) if (client.actorId === actorId) this.clients.delete(id); - for (const barrier of this.barriers.values()) if (barrier.status === 'pending' && (barrier.actorId === actorId || barrier.participants.some(item => item.actorId === actorId))) { barrier.status = 'invalid'; barrier.reason = 'Participant access revoked'; } + for (const barrier of this.barriers.values()) + if ( + barrier.status === 'pending' && + (barrier.actorId === actorId || barrier.participants.some(item => item.actorId === actorId)) + ) { + barrier.status = 'invalid'; + barrier.reason = 'Participant access revoked'; + } if (this.debugLease?.actorId === actorId) this.debugLease = null; return { revoked: true }; } state() { const lease = this._liveDebugLease(); - return clone({ schemaVersion: 1, revision: this.revision, latestSnapshot: this.latestSnapshot, clients: this._activeClients(), barriers: [...this.barriers.values()].map(barrier => ({ ...barrier, status: barrier.status === 'pending' && this.now() >= barrier.expiresAt ? 'expired' : barrier.status })), debugController: lease ? { actorId: lease.actorId, expiresAt: lease.expiresAt } : null }); + return clone({ + schemaVersion: 1, + revision: this.revision, + latestSnapshot: this.latestSnapshot, + clients: this._activeClients(), + barriers: [...this.barriers.values()].map(barrier => ({ + ...barrier, + status: barrier.status === 'pending' && this.now() >= barrier.expiresAt ? 'expired' : barrier.status, + })), + debugController: lease ? { actorId: lease.actorId, expiresAt: lease.expiresAt } : null, + }); } inputState() { - const state=this.state(); - const clients=state.clients.map(({actorId,clientId,documents,dirty})=>({actorId,clientId,documents,dirty})).sort((a,b)=>a.clientId.localeCompare(b.clientId)); - const snapshot=state.latestSnapshot; - return { latestSnapshot:snapshot?{id:snapshot.id,revision:snapshot.revision,digest:snapshot.digest,files:snapshot.files}:null,currentDiskDigest:this._capture(false).digest,activityDigest:sha(JSON.stringify(clients)),dirty:clients.some(client=>client.dirty),activeClients:clients.length }; + const state = this.state(); + const clients = state.clients + .map(({ actorId, clientId, documents, dirty }) => ({ actorId, clientId, documents, dirty })) + .sort((a, b) => a.clientId.localeCompare(b.clientId)); + const snapshot = state.latestSnapshot; + return { + latestSnapshot: snapshot + ? { id: snapshot.id, revision: snapshot.revision, digest: snapshot.digest, files: snapshot.files } + : null, + currentDiskDigest: this._capture(false).digest, + activityDigest: sha(JSON.stringify(clients)), + dirty: clients.some(client => client.dirty), + activeClients: clients.length, + }; } } -module.exports = { Coordinator, CoordinationError, EXCLUDED: Object.freeze([...EXCLUDED]), CLIENT_TTL_MS, BARRIER_TTL_MS, DEBUG_LEASE_MS, DEFAULT_SNAPSHOT_LIMITS }; +module.exports = { + Coordinator, + CoordinationError, + EXCLUDED: Object.freeze([...EXCLUDED]), + CLIENT_TTL_MS, + BARRIER_TTL_MS, + DEBUG_LEASE_MS, + DEFAULT_SNAPSHOT_LIMITS, +}; diff --git a/apps/ide/coordination/package.json b/apps/ide/coordination/package.json index de61273..14e2599 100644 --- a/apps/ide/coordination/package.json +++ b/apps/ide/coordination/package.json @@ -3,6 +3,10 @@ "version": "0.1.0", "private": true, "main": "index.cjs", - "scripts": { "test": "node --test test/*.test.cjs" }, - "engines": { "node": ">=22" } + "scripts": { + "test": "node --test test/*.test.cjs" + }, + "engines": { + "node": ">=22" + } } diff --git a/apps/ide/coordination/test/coordinator.test.cjs b/apps/ide/coordination/test/coordinator.test.cjs index c83716c..48a43bb 100644 --- a/apps/ide/coordination/test/coordinator.test.cjs +++ b/apps/ide/coordination/test/coordinator.test.cjs @@ -18,11 +18,22 @@ function fixture(t, extraOptions = {}) { const options = { workspaceRoot, stateDirectory, now: () => time, ...extraOptions }; const coordinator = new Coordinator(options); t.after(() => fs.rmSync(directory, { recursive: true, force: true })); - return { directory, workspaceRoot, stateDirectory, coordinator, options, advance: ms => { time += ms; } }; + return { + directory, + workspaceRoot, + stateDirectory, + coordinator, + options, + advance: ms => { + time += ms; + }, + }; } const clean = (version = 1) => ({ documents: [{ uri: 'app.ts', version, dirty: false }] }); const dirty = (version = 1) => ({ documents: [{ uri: 'app.ts', version, dirty: true }] }); -function code(expected) { return error => error.code === expected; } +function code(expected) { + return error => error.code === expected; +} function acknowledged(coordinator, actor = 'alice', client = 'alice-tab') { coordinator.register(actor, client, clean()); const barrier = coordinator.beginBarrier(actor); @@ -41,9 +52,16 @@ test('saved immutable manifest records exact content and revision outside source fs.writeFileSync(path.join(workspaceRoot, 'src', 'main.py'), 'print(42)\n'); const snapshot = await engine.commitBarrier('alice', acknowledged(engine).id); assert.equal(snapshot.revision, 1); - assert.deepEqual(snapshot.files.map(file => file.path), ['app.ts', 'src/main.py']); + assert.deepEqual( + snapshot.files.map(file => file.path), + ['app.ts', 'src/main.py'], + ); assert.deepEqual(fs.readFileSync(path.join(workspaceRoot, 'app.ts')), original); - const manifestPath = path.join(stateDirectory, 'snapshots', fs.readdirSync(path.join(stateDirectory, 'snapshots'))[0]); + const manifestPath = path.join( + stateDirectory, + 'snapshots', + fs.readdirSync(path.join(stateDirectory, 'snapshots'))[0], + ); const savedManifest = fs.readFileSync(manifestPath); fs.writeFileSync(path.join(workspaceRoot, 'app.ts'), 'new content after snapshot'); assert.deepEqual(fs.readFileSync(path.join(stateDirectory, 'objects', snapshot.files[0].sha256)), original); @@ -57,12 +75,18 @@ test('saved immutable manifest records exact content and revision outside source test('constructor limits have bounded defaults and reject nonpositive or unsafe configuration', t => { const { coordinator: engine, options } = fixture(t); - assert.deepEqual(engine.limits, { maxFileBytes: 32 * 1024 * 1024, maxTotalBytes: 256 * 1024 * 1024, maxFiles: 20_000 }); + assert.deepEqual(engine.limits, { + maxFileBytes: 32 * 1024 * 1024, + maxTotalBytes: 256 * 1024 * 1024, + maxFiles: 20_000, + }); assert.deepEqual(engine.limits, DEFAULT_SNAPSHOT_LIMITS); - for (const key of Object.keys(DEFAULT_SNAPSHOT_LIMITS)) for (const value of [0, -1, 1.5, NaN, Infinity, '32', null, undefined, Number.MAX_SAFE_INTEGER + 1]) { - assert.throws(() => new Coordinator({ ...options, limits: { [key]: value } }), code('INVALID_INPUT')); - } - for (const limits of [null, [], 'limits', { unknownLimit: 1 }, { toString: 1 }]) assert.throws(() => new Coordinator({ ...options, limits }), code('INVALID_INPUT')); + for (const key of Object.keys(DEFAULT_SNAPSHOT_LIMITS)) + for (const value of [0, -1, 1.5, NaN, Infinity, '32', null, undefined, Number.MAX_SAFE_INTEGER + 1]) { + assert.throws(() => new Coordinator({ ...options, limits: { [key]: value } }), code('INVALID_INPUT')); + } + for (const limits of [null, [], 'limits', { unknownLimit: 1 }, { toString: 1 }]) + assert.throws(() => new Coordinator({ ...options, limits }), code('INVALID_INPUT')); }); test('single-file resource limit rejects before opening source for reading', t => { @@ -71,9 +95,15 @@ test('single-file resource limit rejects before opening source for reading', t = engine.register('alice', 'a', clean()); const open = fs.openSync; let sourceOpened = false; - fs.openSync = (file, ...args) => { if (typeof file === 'string' && file.endsWith('/app.ts')) sourceOpened = true; return open(file, ...args); }; - try { assert.throws(() => engine.beginBarrier('alice'), code('SNAPSHOT_LIMIT')); } - finally { fs.openSync = open; } + fs.openSync = (file, ...args) => { + if (typeof file === 'string' && file.endsWith('/app.ts')) sourceOpened = true; + return open(file, ...args); + }; + try { + assert.throws(() => engine.beginBarrier('alice'), code('SNAPSHOT_LIMIT')); + } finally { + fs.openSync = open; + } assert.equal(sourceOpened, false); assert.equal(engine.state().revision, 0); }); @@ -88,11 +118,17 @@ test('source growth after fd stat is read only into the bounded allocation then const allocations = []; fs.readSync = (fd, buffer, ...args) => { allocations.push(buffer.length); - if (!grew) { grew = true; fs.appendFileSync(source, 'defghijklmnop'); } + if (!grew) { + grew = true; + fs.appendFileSync(source, 'defghijklmnop'); + } return read(fd, buffer, ...args); }; - try { assert.throws(() => engine.beginBarrier('alice'), code('SNAPSHOT_LIMIT')); } - finally { fs.readSync = read; } + try { + assert.throws(() => engine.beginBarrier('alice'), code('SNAPSHOT_LIMIT')); + } finally { + fs.readSync = read; + } assert.equal(grew, true); assert.deepEqual(allocations, [3, 1]); assert.equal(engine.state().revision, 0); @@ -112,14 +148,19 @@ test('aggregate bytes and file count caps reject duplicate content and empty fil }); test('exact configured limits permit a snapshot while excluded runtime files do not consume caps', async t => { - const { coordinator: engine, workspaceRoot } = fixture(t, { limits: { maxFileBytes: 5, maxTotalBytes: 10, maxFiles: 2 } }); + const { coordinator: engine, workspaceRoot } = fixture(t, { + limits: { maxFileBytes: 5, maxTotalBytes: 10, maxFiles: 2 }, + }); fs.writeFileSync(path.join(workspaceRoot, 'app.ts'), '12345'); fs.writeFileSync(path.join(workspaceRoot, 'second.ts'), '67890'); fs.mkdirSync(path.join(workspaceRoot, 'node_modules')); fs.writeFileSync(path.join(workspaceRoot, 'node_modules', 'large'), 'excluded-runtime-file'); const snapshot = await engine.commitBarrier('alice', acknowledged(engine).id); assert.equal(snapshot.files.length, 2); - assert.equal(snapshot.files.reduce((total, file) => total + file.size, 0), 10); + assert.equal( + snapshot.files.reduce((total, file) => total + file.size, 0), + 10, + ); }); test('commit checks barrier expiry again after the final filesystem scan', async t => { @@ -127,7 +168,11 @@ test('commit checks barrier expiry again after the final filesystem scan', async const barrier = acknowledged(engine); const capture = engine._capture.bind(engine); let captures = 0; - engine._capture = (...args) => { const result = capture(...args); if (++captures === 2) advance(BARRIER_TTL_MS); return result; }; + engine._capture = (...args) => { + const result = capture(...args); + if (++captures === 2) advance(BARRIER_TTL_MS); + return result; + }; await assert.rejects(engine.commitBarrier('alice', barrier.id), code('BARRIER_EXPIRED')); assert.equal(engine.state().revision, 0); assert.deepEqual(fs.readdirSync(path.join(stateDirectory, 'snapshots')), []); @@ -141,7 +186,11 @@ test('commit checks stale participant heartbeats after scanning even when barrie engine.acknowledge('alice', barrier.id, 'a', clean()); const capture = engine._capture.bind(engine); let captures = 0; - engine._capture = (...args) => { const result = capture(...args); if (++captures === 2) advance(CLIENT_TTL_MS - 10_000); return result; }; + engine._capture = (...args) => { + const result = capture(...args); + if (++captures === 2) advance(CLIENT_TTL_MS - 10_000); + return result; + }; await assert.rejects(engine.commitBarrier('alice', barrier.id), code('CLIENT_EXPIRED')); assert.equal(engine.state().revision, 0); assert.deepEqual(fs.readdirSync(path.join(stateDirectory, 'snapshots')), []); @@ -201,7 +250,10 @@ test('unregister invalidates that client barrier without revoking other actor ta const barrier = engine.beginBarrier('alice'); assert.throws(() => engine.unregister('bob', 'a'), code('CLIENT_OWNERSHIP')); engine.unregister('alice', 'a'); - assert.deepEqual(engine.state().clients.map(client => client.clientId), ['another-tab', 'b']); + assert.deepEqual( + engine.state().clients.map(client => client.clientId), + ['another-tab', 'b'], + ); await assert.rejects(engine.commitBarrier('alice', barrier.id), code('BARRIER_INVALID')); assert.deepEqual(engine.unregister('alice', 'a'), { unregistered: true }); }); @@ -276,11 +328,15 @@ test('crash after manifest persistence but before journal update recovers a sing const rename = fs.renameSync; let journalWrites = 0; fs.renameSync = (from, to) => { - if (to === path.join(engine.stateDirectory, 'journal.json') && ++journalWrites === 2) throw Object.assign(new Error('injected journal disk failure'), { code: 'EIO' }); + if (to === path.join(engine.stateDirectory, 'journal.json') && ++journalWrites === 2) + throw Object.assign(new Error('injected journal disk failure'), { code: 'EIO' }); return rename(from, to); }; - try { await assert.rejects(engine.commitBarrier('alice', barrier.id), /injected journal disk failure/); } - finally { fs.renameSync = rename; } + try { + await assert.rejects(engine.commitBarrier('alice', barrier.id), /injected journal disk failure/); + } finally { + fs.renameSync = rename; + } assert.equal(JSON.parse(fs.readFileSync(path.join(stateDirectory, 'journal.json'))).revision, 0); const recovered = new Coordinator(options); const snapshot = await recovered.commitBarrier('alice', barrier.id); @@ -300,13 +356,28 @@ test('recovery refuses content-addressed object tampering and never reconstructs test('rejects state inside source, symlink escapes and file URIs outside workspace', t => { const { coordinator: engine, workspaceRoot, directory } = fixture(t); - assert.throws(() => new Coordinator({ workspaceRoot, stateDirectory: path.join(workspaceRoot, '.state') }), code('UNSAFE_STATE_PATH')); + assert.throws( + () => new Coordinator({ workspaceRoot, stateDirectory: path.join(workspaceRoot, '.state') }), + code('UNSAFE_STATE_PATH'), + ); assert.throws(() => new Coordinator({ workspaceRoot, stateDirectory: directory }), code('UNSAFE_STATE_PATH')); fs.symlinkSync(workspaceRoot, path.join(directory, 'source-alias')); - assert.throws(() => new Coordinator({ workspaceRoot, stateDirectory: path.join(directory, 'source-alias', 'should-not-create') }), code('UNSAFE_STATE_PATH')); + assert.throws( + () => new Coordinator({ workspaceRoot, stateDirectory: path.join(directory, 'source-alias', 'should-not-create') }), + code('UNSAFE_STATE_PATH'), + ); assert.equal(fs.existsSync(path.join(workspaceRoot, 'should-not-create')), false); - assert.throws(() => engine.register('alice', 'a', { documents: [{ uri: pathToFileURL(path.join(directory, 'outside')).href, version: 1, dirty: false }] }), code('OUTSIDE_WORKSPACE')); - assert.throws(() => engine.register('alice', 'a', { documents: [{ uri: '../outside', version: 1, dirty: false }] }), code('OUTSIDE_WORKSPACE')); + assert.throws( + () => + engine.register('alice', 'a', { + documents: [{ uri: pathToFileURL(path.join(directory, 'outside')).href, version: 1, dirty: false }], + }), + code('OUTSIDE_WORKSPACE'), + ); + assert.throws( + () => engine.register('alice', 'a', { documents: [{ uri: '../outside', version: 1, dirty: false }] }), + code('OUTSIDE_WORKSPACE'), + ); engine.register('alice', 'a', clean()); fs.writeFileSync(path.join(directory, 'secret'), 'not in source'); fs.symlinkSync(path.join(directory, 'secret'), path.join(workspaceRoot, 'escape')); @@ -318,9 +389,26 @@ test('rejects state inside source, symlink escapes and file URIs outside workspa test('document input validation normalizes file URIs and refuses duplicate vectors', t => { const { coordinator: engine, workspaceRoot } = fixture(t); - assert.throws(() => engine.register('alice', 'a', { documents: [{ uri: 'app.ts', version: NaN, dirty: false }] }), code('INVALID_INPUT')); - assert.throws(() => engine.register('alice', 'a', { documents: [{ uri: 'app.ts', version: 1, dirty: false }, { uri: pathToFileURL(path.join(workspaceRoot, 'app.ts')).href, version: 1, dirty: false }] }), code('INVALID_INPUT')); - const client = engine.register('alice', 'a', { documents: [{ uri: pathToFileURL(path.join(workspaceRoot, 'app.ts')).href, version: 1, dirty: false }, { uri: 'untitled:unsaved', version: 0, dirty: true }] }); + assert.throws( + () => engine.register('alice', 'a', { documents: [{ uri: 'app.ts', version: NaN, dirty: false }] }), + code('INVALID_INPUT'), + ); + assert.throws( + () => + engine.register('alice', 'a', { + documents: [ + { uri: 'app.ts', version: 1, dirty: false }, + { uri: pathToFileURL(path.join(workspaceRoot, 'app.ts')).href, version: 1, dirty: false }, + ], + }), + code('INVALID_INPUT'), + ); + const client = engine.register('alice', 'a', { + documents: [ + { uri: pathToFileURL(path.join(workspaceRoot, 'app.ts')).href, version: 1, dirty: false }, + { uri: 'untitled:unsaved', version: 0, dirty: true }, + ], + }); assert.equal(client.documents[0].uri, 'app.ts'); assert.equal(client.dirty, true); }); @@ -373,7 +461,10 @@ test('debug transfer rotates token and requires live target; revoke invalidates engine.acknowledge('bob', barrier.id, 'b', clean()); engine.revoke('bob'); assert.throws(() => engine.assertDebugController('bob', transferred.token), code('DEBUG_FORBIDDEN')); - assert.equal(engine.state().clients.some(client => client.actorId === 'bob'), false); + assert.equal( + engine.state().clients.some(client => client.actorId === 'bob'), + false, + ); await assert.rejects(engine.commitBarrier('alice', barrier.id), code('BARRIER_INVALID')); }); @@ -392,7 +483,8 @@ test('integration state tracks dirty documents, membership and disk without trea await engine.commitBarrier('alice', acknowledged(engine).id); const before = engine.inputState(); assert.equal(before.currentDiskDigest, before.latestSnapshot.digest); - advance(1000); engine.register('alice', 'alice-tab', clean()); + advance(1000); + engine.register('alice', 'alice-tab', clean()); assert.equal(engine.inputState().activityDigest, before.activityDigest); engine.register('alice', 'alice-tab', dirty(2)); assert.equal(engine.inputState().dirty, true); diff --git a/apps/ide/gateway/main.mjs b/apps/ide/gateway/main.mjs index d4e3862..b80bdf5 100644 --- a/apps/ide/gateway/main.mjs +++ b/apps/ide/gateway/main.mjs @@ -9,10 +9,22 @@ const gateway = createGateway({ apiOrigin: process.env.WF_API_URL ?? 'http://127.0.0.1:4000', tenantId: process.env.WF_TENANT_ID ?? 'demo', serviceToken: process.env.WF_WORKSPACE_GATEWAY_TOKEN, - allowedUpstreamOrigins: (process.env.WF_IDE_ALLOWED_UPSTREAM_ORIGINS ?? (production ? '' : 'http://127.0.0.1:4300')).split(',').filter(Boolean), + allowedUpstreamOrigins: (process.env.WF_IDE_ALLOWED_UPSTREAM_ORIGINS ?? (production ? '' : 'http://127.0.0.1:4300')) + .split(',') + .filter(Boolean), allowedServiceNamespace: process.env.WF_IDE_ALLOWED_SERVICE_NAMESPACE, allowedCollaborationOrigins: (process.env.WF_COLLABORATION_ALLOWED_UPSTREAM_ORIGINS ?? '').split(',').filter(Boolean), - assertionPrivateKey: process.env.WF_IDE_GATEWAY_PRIVATE_KEY_FILE ? readFileSync(process.env.WF_IDE_GATEWAY_PRIVATE_KEY_FILE, 'utf8') : undefined, + assertionPrivateKey: process.env.WF_IDE_GATEWAY_PRIVATE_KEY_FILE + ? readFileSync(process.env.WF_IDE_GATEWAY_PRIVATE_KEY_FILE, 'utf8') + : undefined, }); -gateway.server.listen(Number(process.env.WF_IDE_GATEWAY_PORT ?? 4400), process.env.WF_IDE_GATEWAY_HOST ?? '127.0.0.1', () => console.log(`Workspace gateway listening at ${publicUrl}`)); -for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, async () => { await gateway.close(); process.exit(0); }); +gateway.server.listen( + Number(process.env.WF_IDE_GATEWAY_PORT ?? 4400), + process.env.WF_IDE_GATEWAY_HOST ?? '127.0.0.1', + () => console.log(`Workspace gateway listening at ${publicUrl}`), +); +for (const signal of ['SIGTERM', 'SIGINT']) + process.on(signal, async () => { + await gateway.close(); + process.exit(0); + }); diff --git a/apps/ide/gateway/package.json b/apps/ide/gateway/package.json index b3781c9..8ab3f04 100644 --- a/apps/ide/gateway/package.json +++ b/apps/ide/gateway/package.json @@ -3,6 +3,11 @@ "version": "0.1.0", "private": true, "type": "module", - "scripts": { "start": "node main.mjs", "test": "node --test test/*.test.mjs" }, - "dependencies": { "ws": "8.21.0" } + "scripts": { + "start": "node main.mjs", + "test": "node --test test/*.test.mjs" + }, + "dependencies": { + "ws": "8.21.0" + } } diff --git a/apps/ide/gateway/server.mjs b/apps/ide/gateway/server.mjs index c38d9d0..ce47848 100644 --- a/apps/ide/gateway/server.mjs +++ b/apps/ide/gateway/server.mjs @@ -7,17 +7,31 @@ import assertions from '../security/assertions.cjs'; const COOKIE = 'WF_IDE_SESSION'; const ID = /^[a-zA-Z0-9_-]{1,128}$/; const EDIT_ROLES = new Set(['owner', 'admin', 'maintainer', 'developer', 'editor']); -class HttpError extends Error { constructor(status, message) { super(message); this.status = status; } } +class HttpError extends Error { + constructor(status, message) { + super(message); + this.status = status; + } +} const random = () => randomBytes(32).toString('base64url'); export function createGateway(config) { const publicUrl = new URL(config.publicUrl); - if (!['http:', 'https:'].includes(publicUrl.protocol) || publicUrl.pathname !== '/' || publicUrl.search || publicUrl.username || publicUrl.password) throw new Error('publicUrl must be an HTTP(S) origin'); - if (!ID.test(config.tenantId) || !config.serviceToken) throw new Error('A fixed tenantId and gateway serviceToken are required'); + if ( + !['http:', 'https:'].includes(publicUrl.protocol) || + publicUrl.pathname !== '/' || + publicUrl.search || + publicUrl.username || + publicUrl.password + ) + throw new Error('publicUrl must be an HTTP(S) origin'); + if (!ID.test(config.tenantId) || !config.serviceToken) + throw new Error('A fixed tenantId and gateway serviceToken are required'); const upstreamOrigins = new Set(config.allowedUpstreamOrigins); const collaborationOrigins = new Set(config.allowedCollaborationOrigins ?? []); const serviceNamespace = config.allowedServiceNamespace; - if (serviceNamespace && (!/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(serviceNamespace) || serviceNamespace.length > 63)) throw new Error('Invalid service namespace'); + if (serviceNamespace && (!/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(serviceNamespace) || serviceNamespace.length > 63)) + throw new Error('Invalid service namespace'); if (!upstreamOrigins.size && !serviceNamespace) throw new Error('Explicit upstream allowlist required'); const fetchImpl = config.fetchImpl ?? fetch; const sessions = new Map(); @@ -26,48 +40,92 @@ export function createGateway(config) { // Keep it server-side, scoped to an allowed upstream, never in browser cookies. const upstreamTokens = new Map(); const sockets = new Set(); - const workspaceSockets=new Map(); + const workspaceSockets = new Map(); const rawSockets = new Set(); const sessionTtl = Math.min(config.sessionTtlMs ?? 900000, 900000); const pollMs = Math.min(config.reauthorizeMs ?? 1000, 1000); const now = () => Date.now(); const purge = () => { - for (const map of [sessions, launches]) for (const [key, value] of map) if (value.expiresAt <= now()) map.delete(key); + for (const map of [sessions, launches]) + for (const [key, value] of map) if (value.expiresAt <= now()) map.delete(key); }; - const timer = setInterval(purge, 5000); timer.unref(); + const timer = setInterval(purge, 5000); + timer.unref(); const wss = new WebSocketServer({ noServer: true, maxPayload: 16 * 1024 * 1024, perMessageDeflate: false }); async function authorize(workspaceId, userToken, operation, collaboration = false) { - if (!ID.test(workspaceId) || !userToken || userToken.length > 16384) throw new HttpError(401, 'Authentication required'); + if (!ID.test(workspaceId) || !userToken || userToken.length > 16384) + throw new HttpError(401, 'Authentication required'); let response; try { - response = await fetchImpl(`${config.apiOrigin}/internal/workspaces/${encodeURIComponent(workspaceId)}/authorize`, { - method: 'POST', - headers: { authorization: `Bearer ${config.serviceToken}`, 'content-type': 'application/json', 'X-WishForge-Tenant': config.tenantId }, - body: JSON.stringify({ userToken, operation }), signal: AbortSignal.timeout(3000), redirect: 'error', - }); - } catch { throw new HttpError(503, 'Workspace authorization unavailable'); } - if (!response.ok) throw new HttpError([401, 403, 404].includes(response.status) ? response.status : 503, 'Workspace access denied'); + response = await fetchImpl( + `${config.apiOrigin}/internal/workspaces/${encodeURIComponent(workspaceId)}/authorize`, + { + method: 'POST', + headers: { + authorization: `Bearer ${config.serviceToken}`, + 'content-type': 'application/json', + 'X-WishForge-Tenant': config.tenantId, + }, + body: JSON.stringify({ userToken, operation }), + signal: AbortSignal.timeout(3000), + redirect: 'error', + }, + ); + } catch { + throw new HttpError(503, 'Workspace authorization unavailable'); + } + if (!response.ok) + throw new HttpError([401, 403, 404].includes(response.status) ? response.status : 503, 'Workspace access denied'); const payload = await response.json(); const grant = payload.data ?? payload; - if (grant.tenantId !== config.tenantId || grant.workspaceId !== workspaceId || !grant.projectId || !EDIT_ROLES.has(grant.role)) throw new HttpError(403, 'Workspace grant mismatch'); + if ( + grant.tenantId !== config.tenantId || + grant.workspaceId !== workspaceId || + !grant.projectId || + !EDIT_ROLES.has(grant.role) + ) + throw new HttpError(403, 'Workspace grant mismatch'); let upstream; - try { upstream = new URL(collaboration ? grant.collaborationUpstreamUrl : grant.upstreamUrl); } catch { throw new HttpError(502, 'Invalid workspace upstream'); } + try { + upstream = new URL(collaboration ? grant.collaborationUpstreamUrl : grant.upstreamUrl); + } catch { + throw new HttpError(502, 'Invalid workspace upstream'); + } const origins = collaboration ? collaborationOrigins : upstreamOrigins; - const serviceAllowed = serviceNamespace && upstream.protocol === 'http:' && upstream.port === (collaboration ? '8100' : '3000') && new RegExp(`^wf-ide-[a-f0-9]{24}\\.${serviceNamespace}\\.svc(?:\\.cluster\\.local)?$`).test(upstream.hostname); - if ((!origins.has(upstream.origin) && !serviceAllowed) || !['http:', 'https:'].includes(upstream.protocol) || upstream.username || upstream.password || upstream.search || upstream.hash) throw new HttpError(502, 'Workspace upstream is not permitted'); + const serviceAllowed = + serviceNamespace && + upstream.protocol === 'http:' && + upstream.port === (collaboration ? '8100' : '3000') && + new RegExp(`^wf-ide-[a-f0-9]{24}\\.${serviceNamespace}\\.svc(?:\\.cluster\\.local)?$`).test(upstream.hostname); + if ( + (!origins.has(upstream.origin) && !serviceAllowed) || + !['http:', 'https:'].includes(upstream.protocol) || + upstream.username || + upstream.password || + upstream.search || + upstream.hash + ) + throw new HttpError(502, 'Workspace upstream is not permitted'); return { ...grant, upstream }; } function tokenFor(req, workspaceId) { const authorization = req.headers.authorization; - if (authorization?.startsWith('Bearer ')) return { userToken: authorization.slice(7), expiresAt: Number.MAX_SAFE_INTEGER }; - const opaque = req.headers.cookie?.split(';').map(v => v.trim()).find(v => v.startsWith(`${COOKIE}=`))?.slice(COOKIE.length + 1); + if (authorization?.startsWith('Bearer ')) + return { userToken: authorization.slice(7), expiresAt: Number.MAX_SAFE_INTEGER }; + const opaque = req.headers.cookie + ?.split(';') + .map(v => v.trim()) + .find(v => v.startsWith(`${COOKIE}=`)) + ?.slice(COOKIE.length + 1); const session = opaque && sessions.get(opaque); - if (!session || session.expiresAt <= now() || session.workspaceId !== workspaceId) throw new HttpError(401, 'Launch an authenticated IDE session'); + if (!session || session.expiresAt <= now() || session.workspaceId !== workspaceId) + throw new HttpError(401, 'Launch an authenticated IDE session'); return session; } function originCheck(req, required = false) { - if ((required && !req.headers.origin) || (req.headers.origin && req.headers.origin !== publicUrl.origin)) throw new HttpError(403, 'Origin not allowed'); + if ((required && !req.headers.origin) || (req.headers.origin && req.headers.origin !== publicUrl.origin)) + throw new HttpError(403, 'Origin not allowed'); } function route(req) { const parsed = new URL(req.url, publicUrl); @@ -78,7 +136,13 @@ export function createGateway(config) { if (collaboration) suffix = suffix.slice('/collaboration'.length) || '/'; // Multiplexed Theia services need edit permission. Callers cannot downgrade // this to preview by supplying a query parameter or header. - const operation = collaboration ? 'edit' : /\/terminals?(?:\/|$)/.test(suffix) ? 'terminal' : /\/debug(?:\/|$)/.test(suffix) ? 'debug' : 'edit'; + const operation = collaboration + ? 'edit' + : /\/terminals?(?:\/|$)/.test(suffix) + ? 'terminal' + : /\/debug(?:\/|$)/.test(suffix) + ? 'debug' + : 'edit'; return { workspaceId: match[1], suffix, query: parsed.search, operation, collaboration }; } function targetFor(grant, routing) { @@ -89,127 +153,308 @@ export function createGateway(config) { } function proxyHeaders(req, target, grant) { const headers = { ...req.headers }; - for (const key of Object.keys(headers)) if (['authorization', 'cookie', 'connection', 'upgrade', 'proxy-authorization', 'proxy-authenticate', 'te', 'trailer', 'transfer-encoding'].includes(key) || key.startsWith('x-forwarded-') || key.startsWith('x-wishforge-') || key.startsWith('sec-websocket-')) delete headers[key]; + for (const key of Object.keys(headers)) + if ( + [ + 'authorization', + 'cookie', + 'connection', + 'upgrade', + 'proxy-authorization', + 'proxy-authenticate', + 'te', + 'trailer', + 'transfer-encoding', + ].includes(key) || + key.startsWith('x-forwarded-') || + key.startsWith('x-wishforge-') || + key.startsWith('sec-websocket-') + ) + delete headers[key]; headers.host = target.host; headers.origin = target.origin; headers['x-forwarded-host'] = publicUrl.host; headers['x-forwarded-proto'] = publicUrl.protocol.slice(0, -1); headers['x-wishforge-tenant'] = grant.tenantId; headers['x-wishforge-workspace'] = grant.workspaceId; - if (config.assertionPrivateKey) headers['x-wishforge-actor-assertion'] = assertions.signAssertion(config.assertionPrivateKey, grant); + if (config.assertionPrivateKey) + headers['x-wishforge-actor-assertion'] = assertions.signAssertion(config.assertionPrivateKey, grant); const token = upstreamTokens.get(grant.upstream.href); if (token) headers.cookie = `theia-connection-token=${token}`; return headers; } function fail(res, error) { - if (res.headersSent) { res.destroy(); return; } - res.writeHead(error.status ?? 500, { 'content-type': 'application/json', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' }); + if (res.headersSent) { + res.destroy(); + return; + } + res.writeHead(error.status ?? 500, { + 'content-type': 'application/json', + 'cache-control': 'no-store', + 'referrer-policy': 'no-referrer', + }); res.end(JSON.stringify({ error: error.status ? error.message : 'Gateway error' })); } async function readJson(req) { let body = ''; - for await (const chunk of req) { body += chunk; if (body.length > 2048) throw new HttpError(413, 'Request too large'); } - try { return JSON.parse(body); } catch { throw new HttpError(400, 'Invalid JSON'); } + for await (const chunk of req) { + body += chunk; + if (body.length > 2048) throw new HttpError(413, 'Request too large'); + } + try { + return JSON.parse(body); + } catch { + throw new HttpError(400, 'Invalid JSON'); + } } const server = http.createServer(async (req, res) => { try { const url = new URL(req.url, publicUrl); - if (url.pathname === '/healthz' && req.method === 'GET') { res.writeHead(200); res.end('ok'); return; } - if (['/internal/integration-state','/internal/lifecycle-state'].includes(url.pathname) && req.method === 'POST') { - const lifecycle=url.pathname==='/internal/lifecycle-state'; - const supplied=req.headers.authorization?.startsWith('Bearer ')?req.headers.authorization.slice(7):''; - const expected=Buffer.from(config.serviceToken),actual=Buffer.from(supplied); - if(actual.length!==expected.length||!timingSafeEqual(actual,expected)||!config.assertionPrivateKey)throw new HttpError(401,'Integration service authentication required'); - const body=await readJson(req); - if(body.tenantId!==config.tenantId||!ID.test(body.workspaceId??'')||(!lifecycle&&!ID.test(body.runId??'')))throw new HttpError(400,'Invalid integration scope'); - const authorized=await fetchImpl(`${config.apiOrigin}/internal/workspaces/${body.workspaceId}/${lifecycle?'lifecycle-authorize':'integration-authorize'}`,{method:'POST',headers:{authorization:`Bearer ${config.serviceToken}`,'content-type':'application/json','X-WishForge-Tenant':config.tenantId},body:JSON.stringify(lifecycle?{}:{runId:body.runId,generation:body.generation,revision:body.revision}),redirect:'error',signal:AbortSignal.timeout(3000)}); - if(!authorized.ok)throw new HttpError(403,'Integration scope denied'); - const payload=await authorized.json();const grant=payload.data??payload; - if(grant.tenantId!==config.tenantId||grant.workspaceId!==body.workspaceId||grant.purpose!==(lifecycle?'workspace-lifecycle':'integration-state'))throw new HttpError(403,'Integration grant mismatch'); - const target=new URL(grant.upstreamUrl); - const managed=serviceNamespace&&target.protocol==='http:'&&target.port==='3000'&&new RegExp(`^wf-ide-[a-f0-9]{24}\\.${serviceNamespace}\\.svc(?:\\.cluster\\.local)?$`).test(target.hostname); - if((!upstreamOrigins.has(target.origin)&&!managed)||target.username||target.password||target.search||target.hash)throw new HttpError(502,'Integration upstream denied'); - const response=await fetchImpl(new URL(lifecycle?'/_wishforge/lifecycle/state':'/_wishforge/integration/state',target),{headers:{'x-wishforge-actor-assertion':assertions.signAssertion(config.assertionPrivateKey,grant)},redirect:'error',signal:AbortSignal.timeout(10000)}); - if(!response.ok)throw new HttpError(503,'Workspace input state unavailable'); - res.writeHead(200,{'content-type':'application/json','cache-control':'no-store'});const receipt=await response.json();res.end(JSON.stringify(lifecycle?{...receipt,gatewayConnections:workspaceSockets.get(body.workspaceId)?.size??0}:receipt));return; + if (url.pathname === '/healthz' && req.method === 'GET') { + res.writeHead(200); + res.end('ok'); + return; + } + if ( + ['/internal/integration-state', '/internal/lifecycle-state'].includes(url.pathname) && + req.method === 'POST' + ) { + const lifecycle = url.pathname === '/internal/lifecycle-state'; + const supplied = req.headers.authorization?.startsWith('Bearer ') ? req.headers.authorization.slice(7) : ''; + const expected = Buffer.from(config.serviceToken), + actual = Buffer.from(supplied); + if (actual.length !== expected.length || !timingSafeEqual(actual, expected) || !config.assertionPrivateKey) + throw new HttpError(401, 'Integration service authentication required'); + const body = await readJson(req); + if ( + body.tenantId !== config.tenantId || + !ID.test(body.workspaceId ?? '') || + (!lifecycle && !ID.test(body.runId ?? '')) + ) + throw new HttpError(400, 'Invalid integration scope'); + const authorized = await fetchImpl( + `${config.apiOrigin}/internal/workspaces/${body.workspaceId}/${lifecycle ? 'lifecycle-authorize' : 'integration-authorize'}`, + { + method: 'POST', + headers: { + authorization: `Bearer ${config.serviceToken}`, + 'content-type': 'application/json', + 'X-WishForge-Tenant': config.tenantId, + }, + body: JSON.stringify( + lifecycle ? {} : { runId: body.runId, generation: body.generation, revision: body.revision }, + ), + redirect: 'error', + signal: AbortSignal.timeout(3000), + }, + ); + if (!authorized.ok) throw new HttpError(403, 'Integration scope denied'); + const payload = await authorized.json(); + const grant = payload.data ?? payload; + if ( + grant.tenantId !== config.tenantId || + grant.workspaceId !== body.workspaceId || + grant.purpose !== (lifecycle ? 'workspace-lifecycle' : 'integration-state') + ) + throw new HttpError(403, 'Integration grant mismatch'); + const target = new URL(grant.upstreamUrl); + const managed = + serviceNamespace && + target.protocol === 'http:' && + target.port === '3000' && + new RegExp(`^wf-ide-[a-f0-9]{24}\\.${serviceNamespace}\\.svc(?:\\.cluster\\.local)?$`).test(target.hostname); + if ( + (!upstreamOrigins.has(target.origin) && !managed) || + target.username || + target.password || + target.search || + target.hash + ) + throw new HttpError(502, 'Integration upstream denied'); + const response = await fetchImpl( + new URL(lifecycle ? '/_wishforge/lifecycle/state' : '/_wishforge/integration/state', target), + { + headers: { 'x-wishforge-actor-assertion': assertions.signAssertion(config.assertionPrivateKey, grant) }, + redirect: 'error', + signal: AbortSignal.timeout(10000), + }, + ); + if (!response.ok) throw new HttpError(503, 'Workspace input state unavailable'); + res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' }); + const receipt = await response.json(); + res.end( + JSON.stringify( + lifecycle ? { ...receipt, gatewayConnections: workspaceSockets.get(body.workspaceId)?.size ?? 0 } : receipt, + ), + ); + return; } if (url.pathname === '/launch' && req.method === 'POST') { originCheck(req); - const userToken = req.headers.authorization?.startsWith('Bearer ') ? req.headers.authorization.slice(7) : undefined; + const userToken = req.headers.authorization?.startsWith('Bearer ') + ? req.headers.authorization.slice(7) + : undefined; const body = await readJson(req); - if (!body || Object.keys(body).some(key => key !== 'workspaceId') || !ID.test(body.workspaceId ?? '')) throw new HttpError(400, 'workspaceId is required'); + if (!body || Object.keys(body).some(key => key !== 'workspaceId') || !ID.test(body.workspaceId ?? '')) + throw new HttpError(400, 'workspaceId is required'); await authorize(body.workspaceId, userToken, 'edit'); - purge(); if (launches.size >= 1000 || sessions.size >= 1000) throw new HttpError(429, 'Session capacity reached'); - const code = random(); launches.set(code, { workspaceId: body.workspaceId, userToken, expiresAt: now() + 30000 }); - res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store', 'referrer-policy': 'no-referrer' }); - res.end(JSON.stringify({ launchUrl: `${publicUrl.origin}/session?code=${encodeURIComponent(code)}`, expiresIn: 30 })); return; + purge(); + if (launches.size >= 1000 || sessions.size >= 1000) throw new HttpError(429, 'Session capacity reached'); + const code = random(); + launches.set(code, { workspaceId: body.workspaceId, userToken, expiresAt: now() + 30000 }); + res.writeHead(200, { + 'content-type': 'application/json', + 'cache-control': 'no-store', + 'referrer-policy': 'no-referrer', + }); + res.end( + JSON.stringify({ launchUrl: `${publicUrl.origin}/session?code=${encodeURIComponent(code)}`, expiresIn: 30 }), + ); + return; } if (url.pathname === '/session' && req.method === 'GET') { - const code = url.searchParams.get('code'); const launch = launches.get(code); launches.delete(code); + const code = url.searchParams.get('code'); + const launch = launches.get(code); + launches.delete(code); if (!launch || launch.expiresAt <= now()) throw new HttpError(401, 'Launch code is invalid or expired'); await authorize(launch.workspaceId, launch.userToken, 'edit'); - const opaque = random(); sessions.set(opaque, { ...launch, expiresAt: now() + sessionTtl }); + const opaque = random(); + sessions.set(opaque, { ...launch, expiresAt: now() + sessionTtl }); res.writeHead(303, { location: `/workspaces/${launch.workspaceId}/`, 'set-cookie': `${COOKIE}=${opaque}; HttpOnly; SameSite=Strict; Path=/workspaces/${launch.workspaceId}/; Max-Age=${Math.floor(sessionTtl / 1000)}${publicUrl.protocol === 'https:' ? '; Secure' : ''}`, - 'cache-control': 'no-store', 'referrer-policy': 'no-referrer', - }); res.end(); return; + 'cache-control': 'no-store', + 'referrer-policy': 'no-referrer', + }); + res.end(); + return; } originCheck(req); const routing = route(req); const session = tokenFor(req, routing.workspaceId); const grant = await authorize(routing.workspaceId, session.userToken, routing.operation, routing.collaboration); const target = targetFor(grant, routing); - const proxy = (target.protocol === 'https:' ? https : http).request(target, { method: req.method, headers: proxyHeaders(req, target, grant) }, upstream => { - const headers = { ...upstream.headers, 'referrer-policy': 'no-referrer', 'cache-control': 'no-store' }; - for (const cookie of upstream.headers['set-cookie'] ?? []) { - const token = /^theia-connection-token=([^;\s]{1,512})(?:;|$)/.exec(cookie)?.[1]; - if (token) upstreamTokens.set(grant.upstream.href, token); - } - delete headers['set-cookie']; - if (headers.location) { - const location = new URL(headers.location, target); - if (location.origin === target.origin) headers.location = `/workspaces/${routing.workspaceId}${routing.collaboration ? '/collaboration' : ''}${location.pathname}${location.search}${location.hash}`; - } - res.writeHead(upstream.statusCode ?? 502, headers); upstream.pipe(res); - }); + const proxy = (target.protocol === 'https:' ? https : http).request( + target, + { method: req.method, headers: proxyHeaders(req, target, grant) }, + upstream => { + const headers = { ...upstream.headers, 'referrer-policy': 'no-referrer', 'cache-control': 'no-store' }; + for (const cookie of upstream.headers['set-cookie'] ?? []) { + const token = /^theia-connection-token=([^;\s]{1,512})(?:;|$)/.exec(cookie)?.[1]; + if (token) upstreamTokens.set(grant.upstream.href, token); + } + delete headers['set-cookie']; + if (headers.location) { + const location = new URL(headers.location, target); + if (location.origin === target.origin) + headers.location = `/workspaces/${routing.workspaceId}${routing.collaboration ? '/collaboration' : ''}${location.pathname}${location.search}${location.hash}`; + } + res.writeHead(upstream.statusCode ?? 502, headers); + upstream.pipe(res); + }, + ); proxy.on('error', () => fail(res, new HttpError(502, 'Workspace upstream unavailable'))); proxy.setTimeout(30000, () => proxy.destroy()); - req.on('aborted', () => proxy.destroy()); res.on('close', () => proxy.destroy()); req.pipe(proxy); - } catch (error) { fail(res, error); } + req.on('aborted', () => proxy.destroy()); + res.on('close', () => proxy.destroy()); + req.pipe(proxy); + } catch (error) { + fail(res, error); + } + }); + server.on('connection', socket => { + rawSockets.add(socket); + socket.once('close', () => rawSockets.delete(socket)); }); - server.on('connection', socket => { rawSockets.add(socket); socket.once('close', () => rawSockets.delete(socket)); }); server.on('upgrade', async (req, socket, head) => { socket.on('error', () => {}); try { originCheck(req, !req.headers.authorization); - const routing = route(req); const session = tokenFor(req, routing.workspaceId); + const routing = route(req); + const session = tokenFor(req, routing.workspaceId); const grant = await authorize(routing.workspaceId, session.userToken, routing.operation, routing.collaboration); - const target = targetFor(grant, routing); target.protocol = target.protocol === 'https:' ? 'wss:' : 'ws:'; - const protocols = req.headers['sec-websocket-protocol']?.split(',').map(p => p.trim()).filter(Boolean); - const upstream = new WebSocket(target, protocols, { headers: proxyHeaders(req, grant.upstream, grant), maxPayload: 16 * 1024 * 1024, handshakeTimeout: 5000, perMessageDeflate: false }); + const target = targetFor(grant, routing); + target.protocol = target.protocol === 'https:' ? 'wss:' : 'ws:'; + const protocols = req.headers['sec-websocket-protocol'] + ?.split(',') + .map(p => p.trim()) + .filter(Boolean); + const upstream = new WebSocket(target, protocols, { + headers: proxyHeaders(req, grant.upstream, grant), + maxPayload: 16 * 1024 * 1024, + handshakeTimeout: 5000, + perMessageDeflate: false, + }); let accepted = false; - upstream.once('error', () => { if (!accepted) socket.destroy(); }); - socket.once('close', () => { if (!accepted) upstream.terminate(); }); - upstream.once('open', () => wss.handleUpgrade(req, socket, head, client => { - accepted = true; sockets.add(client); - const workspaceSet=workspaceSockets.get(routing.workspaceId)??new Set();workspaceSockets.set(routing.workspaceId,workspaceSet);workspaceSet.add(client); - const close = () => { clearInterval(poll); sockets.delete(client);workspaceSet.delete(client);if(!workspaceSet.size)workspaceSockets.delete(routing.workspaceId); client.terminate(); upstream.terminate(); }; - client.on('message', (data, binary) => { if (upstream.readyState === WebSocket.OPEN) upstream.send(data, { binary }); }); - upstream.on('message', (data, binary) => { if (client.readyState === WebSocket.OPEN) client.send(data, { binary }); }); - client.on('close', close); upstream.on('close', close); client.on('error', close); upstream.on('error', close); - let checking = false; - const poll = setInterval(async () => { - if (checking) return; checking = true; - try { - if (session.expiresAt <= now()) throw new Error('Session expired'); - const fresh = await authorize(routing.workspaceId, session.userToken, routing.operation, routing.collaboration); - if (fresh.upstream.href !== grant.upstream.href) throw new Error('Workspace moved'); - } catch { close(); } finally { checking = false; } - }, pollMs); poll.unref(); - })); - } catch (error) { socket.end(`HTTP/1.1 ${error.status ?? 500} Access Denied\r\nConnection: close\r\n\r\n`); } + upstream.once('error', () => { + if (!accepted) socket.destroy(); + }); + socket.once('close', () => { + if (!accepted) upstream.terminate(); + }); + upstream.once('open', () => + wss.handleUpgrade(req, socket, head, client => { + accepted = true; + sockets.add(client); + const workspaceSet = workspaceSockets.get(routing.workspaceId) ?? new Set(); + workspaceSockets.set(routing.workspaceId, workspaceSet); + workspaceSet.add(client); + const close = () => { + clearInterval(poll); + sockets.delete(client); + workspaceSet.delete(client); + if (!workspaceSet.size) workspaceSockets.delete(routing.workspaceId); + client.terminate(); + upstream.terminate(); + }; + client.on('message', (data, binary) => { + if (upstream.readyState === WebSocket.OPEN) upstream.send(data, { binary }); + }); + upstream.on('message', (data, binary) => { + if (client.readyState === WebSocket.OPEN) client.send(data, { binary }); + }); + client.on('close', close); + upstream.on('close', close); + client.on('error', close); + upstream.on('error', close); + let checking = false; + const poll = setInterval(async () => { + if (checking) return; + checking = true; + try { + if (session.expiresAt <= now()) throw new Error('Session expired'); + const fresh = await authorize( + routing.workspaceId, + session.userToken, + routing.operation, + routing.collaboration, + ); + if (fresh.upstream.href !== grant.upstream.href) throw new Error('Workspace moved'); + } catch { + close(); + } finally { + checking = false; + } + }, pollMs); + poll.unref(); + }), + ); + } catch (error) { + socket.end(`HTTP/1.1 ${error.status ?? 500} Access Denied\r\nConnection: close\r\n\r\n`); + } }); - server.on('close', () => { clearInterval(timer); for (const client of sockets) client.terminate(); wss.close(); }); - return { server, close: async () => { for (const client of sockets) client.terminate(); for (const socket of rawSockets) socket.destroy(); await new Promise(resolve => server.close(resolve)); }, sessionCount: () => sessions.size }; + server.on('close', () => { + clearInterval(timer); + for (const client of sockets) client.terminate(); + wss.close(); + }); + return { + server, + close: async () => { + for (const client of sockets) client.terminate(); + for (const socket of rawSockets) socket.destroy(); + await new Promise(resolve => server.close(resolve)); + }, + sessionCount: () => sessions.size, + }; } diff --git a/apps/ide/gateway/test/gateway.test.mjs b/apps/ide/gateway/test/gateway.test.mjs index 6060b7b..8bf18be 100644 --- a/apps/ide/gateway/test/gateway.test.mjs +++ b/apps/ide/gateway/test/gateway.test.mjs @@ -8,46 +8,103 @@ import { generateKeyPairSync } from 'node:crypto'; import assertions from '../../security/assertions.cjs'; async function listen(server) { - server.listen(0, '127.0.0.1'); await once(server, 'listening'); + server.listen(0, '127.0.0.1'); + await once(server, 'listening'); return `http://127.0.0.1:${server.address().port}`; } async function fixture(t, options = {}) { const state = { revoked: false, role: 'developer', calls: [], receivedHeaders: undefined }; - const upstream = http.createServer((req, res) => { state.receivedHeaders = req.headers; + const upstream = http.createServer((req, res) => { + state.receivedHeaders = req.headers; if (state.connectionToken) { res.setHeader('set-cookie', `theia-connection-token=${state.connectionToken}; HttpOnly; SameSite=Strict; Path=/`); - if (req.url === '/protected' && req.headers.cookie !== `theia-connection-token=${state.connectionToken}`) { res.writeHead(403); res.end(); return; } + if (req.url === '/protected' && req.headers.cookie !== `theia-connection-token=${state.connectionToken}`) { + res.writeHead(403); + res.end(); + return; + } } - res.writeHead(200, { 'content-type': 'application/json' }); res.end(JSON.stringify({ path: req.url })); }); + res.writeHead(200, { 'content-type': 'application/json' }); + res.end(JSON.stringify({ path: req.url })); + }); const websocket = new WebSocketServer({ server: upstream }); websocket.on('connection', socket => socket.on('message', (data, binary) => socket.send(data, { binary }))); const upstreamUrl = await listen(upstream); state.upstreamUrl = upstreamUrl; state.collaborationUpstreamUrl = upstreamUrl; const auth = http.createServer(async (req, res) => { - let raw = ''; for await (const part of req) raw += part; - const body = JSON.parse(raw); state.calls.push({ path: req.url, headers: req.headers, body }); - if (state.revoked || (req.url.endsWith('/lifecycle-authorize') ? false : req.url.endsWith('/integration-authorize') ? body.runId !== 'run-1' : body.userToken !== 'user-token')) { res.writeHead(403); res.end(); return; } + let raw = ''; + for await (const part of req) raw += part; + const body = JSON.parse(raw); + state.calls.push({ path: req.url, headers: req.headers, body }); + if ( + state.revoked || + (req.url.endsWith('/lifecycle-authorize') + ? false + : req.url.endsWith('/integration-authorize') + ? body.runId !== 'run-1' + : body.userToken !== 'user-token') + ) { + res.writeHead(403); + res.end(); + return; + } res.writeHead(200, { 'content-type': 'application/json' }); - res.end(JSON.stringify({ data: { tenantId: 'tenant-a', projectId: 'p1', workspaceId: 'w1', userId:'authenticated-user', role: state.role, purpose:req.url.endsWith('/lifecycle-authorize')?'workspace-lifecycle':req.url.endsWith('/integration-authorize')?'integration-state':undefined, upstreamUrl: state.upstreamUrl, collaborationUpstreamUrl: state.collaborationUpstreamUrl } })); + res.end( + JSON.stringify({ + data: { + tenantId: 'tenant-a', + projectId: 'p1', + workspaceId: 'w1', + userId: 'authenticated-user', + role: state.role, + purpose: req.url.endsWith('/lifecycle-authorize') + ? 'workspace-lifecycle' + : req.url.endsWith('/integration-authorize') + ? 'integration-state' + : undefined, + upstreamUrl: state.upstreamUrl, + collaborationUpstreamUrl: state.collaborationUpstreamUrl, + }, + }), + ); }); const apiOrigin = await listen(auth); // Reserve a local port, then bind the gateway with its actual public origin. - const reservation = http.createServer(); const publicUrl = await listen(reservation); await new Promise(r => reservation.close(r)); - const gateway = createGateway({ publicUrl, apiOrigin, tenantId: 'tenant-a', serviceToken: 'gateway-secret', allowedUpstreamOrigins: [upstreamUrl], allowedCollaborationOrigins:[upstreamUrl], assertionPrivateKey:options.privateKey, reauthorizeMs: 50 }); - gateway.server.listen(Number(new URL(publicUrl).port), '127.0.0.1'); await once(gateway.server, 'listening'); + const reservation = http.createServer(); + const publicUrl = await listen(reservation); + await new Promise(r => reservation.close(r)); + const gateway = createGateway({ + publicUrl, + apiOrigin, + tenantId: 'tenant-a', + serviceToken: 'gateway-secret', + allowedUpstreamOrigins: [upstreamUrl], + allowedCollaborationOrigins: [upstreamUrl], + assertionPrivateKey: options.privateKey, + reauthorizeMs: 50, + }); + gateway.server.listen(Number(new URL(publicUrl).port), '127.0.0.1'); + await once(gateway.server, 'listening'); t.after(async () => { await gateway.close(); for (const socket of websocket.clients) socket.terminate(); websocket.close(); await Promise.all([new Promise(r => upstream.close(r)), new Promise(r => auth.close(r))]); }); - const requestLaunch = () => fetch(`${publicUrl}/launch`, { method: 'POST', headers: { authorization: 'Bearer user-token', 'content-type': 'application/json' }, body: JSON.stringify({ workspaceId: 'w1' }) }); + const requestLaunch = () => + fetch(`${publicUrl}/launch`, { + method: 'POST', + headers: { authorization: 'Bearer user-token', 'content-type': 'application/json' }, + body: JSON.stringify({ workspaceId: 'w1' }), + }); async function session() { - const response = await requestLaunch(); assert.equal(response.status, 200); + const response = await requestLaunch(); + assert.equal(response.status, 200); const { launchUrl } = await response.json(); assert.ok(!launchUrl.includes('user-token')); - const exchanged = await fetch(launchUrl, { redirect: 'manual' }); assert.equal(exchanged.status, 303); + const exchanged = await fetch(launchUrl, { redirect: 'manual' }); + assert.equal(exchanged.status, 303); return { launchUrl, exchanged, cookie: exchanged.headers.get('set-cookie').split(';')[0] }; } return { state, publicUrl, requestLaunch, session }; @@ -62,95 +119,198 @@ test('requires authentication, exchanges a one-time code, and never forwards bea assert.equal(s.exchanged.headers.get('referrer-policy'), 'no-referrer'); assert.equal((await fetch(s.launchUrl, { redirect: 'manual' })).status, 401); const response = await fetch(`${f.publicUrl}/workspaces/w1/file?name=test`, { headers: { cookie: s.cookie } }); - assert.equal(response.status, 200); assert.deepEqual(await response.json(), { path: '/file?name=test' }); - assert.equal(f.state.receivedHeaders.authorization, undefined); assert.equal(f.state.receivedHeaders.cookie, undefined); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { path: '/file?name=test' }); + assert.equal(f.state.receivedHeaders.authorization, undefined); + assert.equal(f.state.receivedHeaders.cookie, undefined); assert.equal(f.state.receivedHeaders['x-wishforge-tenant'], 'tenant-a'); - assert.ok(f.state.calls.every(c => c.headers.authorization === 'Bearer gateway-secret' && c.headers['x-wishforge-tenant'] === 'tenant-a')); + assert.ok( + f.state.calls.every( + c => c.headers.authorization === 'Bearer gateway-secret' && c.headers['x-wishforge-tenant'] === 'tenant-a', + ), + ); assert.equal((await fetch(`${f.publicUrl}/workspaces/w2/`, { headers: { cookie: s.cookie } })).status, 401); }); test('viewer cannot obtain edit access and caller cannot choose an upstream or downgrade operation', async t => { - const f = await fixture(t); f.state.role = 'viewer'; + const f = await fixture(t); + f.state.role = 'viewer'; assert.equal((await f.requestLaunch()).status, 403); - f.state.role = 'developer'; f.state.upstreamUrl = 'http://169.254.169.254/'; + f.state.role = 'developer'; + f.state.upstreamUrl = 'http://169.254.169.254/'; assert.equal((await f.requestLaunch()).status, 502); - const malformed = await fetch(`${f.publicUrl}/launch`, { method: 'POST', headers: { authorization: 'Bearer user-token', 'content-type': 'application/json' }, body: JSON.stringify({ workspaceId: 'w1', upstreamUrl: 'http://evil.invalid' }) }); + const malformed = await fetch(`${f.publicUrl}/launch`, { + method: 'POST', + headers: { authorization: 'Bearer user-token', 'content-type': 'application/json' }, + body: JSON.stringify({ workspaceId: 'w1', upstreamUrl: 'http://evil.invalid' }), + }); assert.equal(malformed.status, 400); await fetch(`${f.publicUrl}/workspaces/w1/?operation=preview`, { headers: { authorization: 'Bearer user-token' } }); assert.equal(f.state.calls.at(-1).body.operation, 'edit'); }); test('HTTP rechecks revocation and rejects cross-origin cookie requests', async t => { - const f = await fixture(t); const s = await f.session(); - assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/`, { headers: { cookie: s.cookie, origin: 'https://attacker.invalid' } })).status, 403); + const f = await fixture(t); + const s = await f.session(); + assert.equal( + ( + await fetch(`${f.publicUrl}/workspaces/w1/`, { + headers: { cookie: s.cookie, origin: 'https://attacker.invalid' }, + }) + ).status, + 403, + ); f.state.revoked = true; assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/`, { headers: { cookie: s.cookie } })).status, 403); }); test('preserves the upstream Theia process token server-side without forwarding client cookies', async t => { - const f = await fixture(t); f.state.connectionToken = 'server-only-process-token'; const s = await f.session(); + const f = await fixture(t); + f.state.connectionToken = 'server-only-process-token'; + const s = await f.session(); const first = await fetch(`${f.publicUrl}/workspaces/w1/`, { headers: { cookie: `${s.cookie}; unrelated=secret` } }); - assert.equal(first.status, 200); assert.equal(first.headers.get('set-cookie'), null); - const protectedResponse = await fetch(`${f.publicUrl}/workspaces/w1/protected`, { headers: { cookie: `${s.cookie}; theia-connection-token=attacker-value` } }); + assert.equal(first.status, 200); + assert.equal(first.headers.get('set-cookie'), null); + const protectedResponse = await fetch(`${f.publicUrl}/workspaces/w1/protected`, { + headers: { cookie: `${s.cookie}; theia-connection-token=attacker-value` }, + }); assert.equal(protectedResponse.status, 200); assert.equal(f.state.receivedHeaders.cookie, 'theia-connection-token=server-only-process-token'); }); -test('gateway signs the API-authorized actor and strips forged workspace identity headers',async t=>{ - const pair=generateKeyPairSync('ed25519');const f=await fixture(t,{privateKey:pair.privateKey.export({type:'pkcs8',format:'pem'})});const s=await f.session(); - const response=await fetch(`${f.publicUrl}/workspaces/w1/`,{headers:{cookie:s.cookie,'x-wishforge-actor-assertion':'forged','x-wishforge-user':'attacker'}}); - assert.equal(response.status,200); - const who=assertions.verifyAssertion(pair.publicKey.export({type:'spki',format:'pem'}),f.state.receivedHeaders['x-wishforge-actor-assertion'],{tenantId:'tenant-a',workspaceId:'w1'}); - assert.equal(who.actorId,'authenticated-user');assert.equal(f.state.receivedHeaders['x-wishforge-user'],undefined); +test('gateway signs the API-authorized actor and strips forged workspace identity headers', async t => { + const pair = generateKeyPairSync('ed25519'); + const f = await fixture(t, { privateKey: pair.privateKey.export({ type: 'pkcs8', format: 'pem' }) }); + const s = await f.session(); + const response = await fetch(`${f.publicUrl}/workspaces/w1/`, { + headers: { cookie: s.cookie, 'x-wishforge-actor-assertion': 'forged', 'x-wishforge-user': 'attacker' }, + }); + assert.equal(response.status, 200); + const who = assertions.verifyAssertion( + pair.publicKey.export({ type: 'spki', format: 'pem' }), + f.state.receivedHeaders['x-wishforge-actor-assertion'], + { tenantId: 'tenant-a', workspaceId: 'w1' }, + ); + assert.equal(who.actorId, 'authenticated-user'); + assert.equal(f.state.receivedHeaders['x-wishforge-user'], undefined); }); test('WebSocket traffic is authenticated and a revoked member is disconnected', { timeout: 5000 }, async t => { - const f = await fixture(t); const s = await f.session(); - const ws = new WebSocket(`${f.publicUrl.replace('http:', 'ws:')}/workspaces/w1/services`, { origin: f.publicUrl, headers: { cookie: s.cookie } }); - await once(ws, 'open'); ws.send('editor-frame'); - const [frame] = await once(ws, 'message'); assert.equal(frame.toString(), 'editor-frame'); - const closed = once(ws, 'close'); f.state.revoked = true; await closed; + const f = await fixture(t); + const s = await f.session(); + const ws = new WebSocket(`${f.publicUrl.replace('http:', 'ws:')}/workspaces/w1/services`, { + origin: f.publicUrl, + headers: { cookie: s.cookie }, + }); + await once(ws, 'open'); + ws.send('editor-frame'); + const [frame] = await once(ws, 'message'); + assert.equal(frame.toString(), 'editor-frame'); + const closed = once(ws, 'close'); + f.state.revoked = true; + await closed; assert.ok(f.state.calls.length >= 4); }); -test('collaboration HTTP/WS stays behind workspace edit authorization and cannot select another upstream', {timeout:5000}, async t => { - const f=await fixture(t);const s=await f.session(); - const reply=await fetch(`${f.publicUrl}/workspaces/w1/collaboration/api/meta`,{headers:{cookie:s.cookie,'x-oct-client':'test'}}); - assert.equal(reply.status,200);assert.deepEqual(await reply.json(),{path:'/api/meta'}); - assert.equal(f.state.calls.at(-1).body.operation,'edit'); - assert.equal(f.state.receivedHeaders['x-oct-client'],'test'); - const ws=new WebSocket(`${f.publicUrl.replace('http:','ws:')}/workspaces/w1/collaboration/socket.io/`,{origin:f.publicUrl,headers:{cookie:s.cookie}}); - await once(ws,'open');ws.send('room-frame');const [frame]=await once(ws,'message');assert.equal(frame.toString(),'room-frame'); - const closed=once(ws,'close');f.state.revoked=true;await closed; - f.state.revoked=false;f.state.collaborationUpstreamUrl='http://169.254.169.254/'; - assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/collaboration/api/meta`,{headers:{cookie:s.cookie}})).status,502); -}); - +test( + 'collaboration HTTP/WS stays behind workspace edit authorization and cannot select another upstream', + { timeout: 5000 }, + async t => { + const f = await fixture(t); + const s = await f.session(); + const reply = await fetch(`${f.publicUrl}/workspaces/w1/collaboration/api/meta`, { + headers: { cookie: s.cookie, 'x-oct-client': 'test' }, + }); + assert.equal(reply.status, 200); + assert.deepEqual(await reply.json(), { path: '/api/meta' }); + assert.equal(f.state.calls.at(-1).body.operation, 'edit'); + assert.equal(f.state.receivedHeaders['x-oct-client'], 'test'); + const ws = new WebSocket(`${f.publicUrl.replace('http:', 'ws:')}/workspaces/w1/collaboration/socket.io/`, { + origin: f.publicUrl, + headers: { cookie: s.cookie }, + }); + await once(ws, 'open'); + ws.send('room-frame'); + const [frame] = await once(ws, 'message'); + assert.equal(frame.toString(), 'room-frame'); + const closed = once(ws, 'close'); + f.state.revoked = true; + await closed; + f.state.revoked = false; + f.state.collaborationUpstreamUrl = 'http://169.254.169.254/'; + assert.equal( + (await fetch(`${f.publicUrl}/workspaces/w1/collaboration/api/meta`, { headers: { cookie: s.cookie } })).status, + 502, + ); + }, +); test('integration state relay is service authenticated, scope checked and purpose restricted', async t => { - const pair=generateKeyPairSync('ed25519'); - const f=await fixture(t,{privateKey:pair.privateKey.export({type:'pkcs8',format:'pem'})}); - const request=(token,extra={})=>fetch(`${f.publicUrl}/internal/integration-state`,{method:'POST',headers:{authorization:`Bearer ${token}`,'content-type':'application/json'},body:JSON.stringify({tenantId:'tenant-a',workspaceId:'w1',runId:'run-1',revision:1,generation:'generation-one',...extra})}); - assert.equal((await request('user-token')).status,401); - assert.equal((await request('gateway-secret',{tenantId:'tenant-b'})).status,400); - assert.equal((await request('gateway-secret',{runId:'other-run'})).status,403); - const response=await request('gateway-secret');assert.equal(response.status,200); - assert.deepEqual(await response.json(),{path:'/_wishforge/integration/state'}); - const token=f.state.receivedHeaders['x-wishforge-actor-assertion']; - const key=pair.publicKey.export({type:'spki',format:'pem'}); - assert.equal(assertions.verifyAssertion(key,token,{tenantId:'tenant-a',workspaceId:'w1',purpose:'integration-state'}).purpose,'integration-state'); - assert.throws(()=>assertions.verifyAssertion(key,token,{tenantId:'tenant-a',workspaceId:'w1'}),/PURPOSE/); - f.state.upstreamUrl='http://169.254.169.254/';assert.equal((await request('gateway-secret')).status,502); - f.state.revoked=true;assert.equal((await request('gateway-secret')).status,403); + const pair = generateKeyPairSync('ed25519'); + const f = await fixture(t, { privateKey: pair.privateKey.export({ type: 'pkcs8', format: 'pem' }) }); + const request = (token, extra = {}) => + fetch(`${f.publicUrl}/internal/integration-state`, { + method: 'POST', + headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, + body: JSON.stringify({ + tenantId: 'tenant-a', + workspaceId: 'w1', + runId: 'run-1', + revision: 1, + generation: 'generation-one', + ...extra, + }), + }); + assert.equal((await request('user-token')).status, 401); + assert.equal((await request('gateway-secret', { tenantId: 'tenant-b' })).status, 400); + assert.equal((await request('gateway-secret', { runId: 'other-run' })).status, 403); + const response = await request('gateway-secret'); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { path: '/_wishforge/integration/state' }); + const token = f.state.receivedHeaders['x-wishforge-actor-assertion']; + const key = pair.publicKey.export({ type: 'spki', format: 'pem' }); + assert.equal( + assertions.verifyAssertion(key, token, { tenantId: 'tenant-a', workspaceId: 'w1', purpose: 'integration-state' }) + .purpose, + 'integration-state', + ); + assert.throws(() => assertions.verifyAssertion(key, token, { tenantId: 'tenant-a', workspaceId: 'w1' }), /PURPOSE/); + f.state.upstreamUrl = 'http://169.254.169.254/'; + assert.equal((await request('gateway-secret')).status, 502); + f.state.revoked = true; + assert.equal((await request('gateway-secret')).status, 403); }); - -test('lifecycle inspection is read-only scoped and accounts for every live workspace socket',async t=>{ - const pair=generateKeyPairSync('ed25519');const f=await fixture(t,{privateKey:pair.privateKey.export({type:'pkcs8',format:'pem'})}); - const inspect=()=>fetch(`${f.publicUrl}/internal/lifecycle-state`,{method:'POST',headers:{authorization:'Bearer gateway-secret','content-type':'application/json'},body:JSON.stringify({tenantId:'tenant-a',workspaceId:'w1'})}); - let response=await inspect();assert.equal(response.status,200);assert.deepEqual(await response.json(),{path:'/_wishforge/lifecycle/state',gatewayConnections:0}); - const key=pair.publicKey.export({type:'spki',format:'pem'});const token=f.state.receivedHeaders['x-wishforge-actor-assertion']; - assert.equal(assertions.verifyAssertion(key,token,{tenantId:'tenant-a',workspaceId:'w1',purpose:'workspace-lifecycle'}).purpose,'workspace-lifecycle');assert.throws(()=>assertions.verifyAssertion(key,token,{tenantId:'tenant-a',workspaceId:'w1'}),/PURPOSE/); - const session=await f.session();const ws=new WebSocket(`${f.publicUrl.replace('http:','ws:')}/workspaces/w1/services`,{origin:f.publicUrl,headers:{cookie:session.cookie}});await once(ws,'open'); - response=await inspect();assert.equal((await response.json()).gatewayConnections,1);ws.close();await once(ws,'close');response=await inspect();assert.equal((await response.json()).gatewayConnections,0); +test('lifecycle inspection is read-only scoped and accounts for every live workspace socket', async t => { + const pair = generateKeyPairSync('ed25519'); + const f = await fixture(t, { privateKey: pair.privateKey.export({ type: 'pkcs8', format: 'pem' }) }); + const inspect = () => + fetch(`${f.publicUrl}/internal/lifecycle-state`, { + method: 'POST', + headers: { authorization: 'Bearer gateway-secret', 'content-type': 'application/json' }, + body: JSON.stringify({ tenantId: 'tenant-a', workspaceId: 'w1' }), + }); + let response = await inspect(); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { path: '/_wishforge/lifecycle/state', gatewayConnections: 0 }); + const key = pair.publicKey.export({ type: 'spki', format: 'pem' }); + const token = f.state.receivedHeaders['x-wishforge-actor-assertion']; + assert.equal( + assertions.verifyAssertion(key, token, { tenantId: 'tenant-a', workspaceId: 'w1', purpose: 'workspace-lifecycle' }) + .purpose, + 'workspace-lifecycle', + ); + assert.throws(() => assertions.verifyAssertion(key, token, { tenantId: 'tenant-a', workspaceId: 'w1' }), /PURPOSE/); + const session = await f.session(); + const ws = new WebSocket(`${f.publicUrl.replace('http:', 'ws:')}/workspaces/w1/services`, { + origin: f.publicUrl, + headers: { cookie: session.cookie }, + }); + await once(ws, 'open'); + response = await inspect(); + assert.equal((await response.json()).gatewayConnections, 1); + ws.close(); + await once(ws, 'close'); + response = await inspect(); + assert.equal((await response.json()).gatewayConnections, 0); }); diff --git a/apps/ide/policy/backend.cjs b/apps/ide/policy/backend.cjs index e2e6e2a..6f9cc9e 100644 --- a/apps/ide/policy/backend.cjs +++ b/apps/ide/policy/backend.cjs @@ -6,82 +6,294 @@ const { DebugServiceImpl } = require('@theia/debug/lib/node/debug-service-impl') const { DebugAdapterSessionManager } = require('@theia/debug/lib/node/debug-adapter-session-manager'); const { ForwardingDebugChannel } = require('@theia/debug/lib/common/debug-service'); const { HostedPluginProcess } = require('@theia/plugin-ext/lib/hosted/node/hosted-plugin-process'); -const {ProcessManager}=require('@theia/process/lib/node/process-manager'); -const {sleepReceipt}=require('./lifecycle-safety.cjs'); +const { ProcessManager } = require('@theia/process/lib/node/process-manager'); +const { sleepReceipt } = require('./lifecycle-safety.cjs'); const { BackendApplicationContribution } = require('@theia/core/lib/node/backend-application'); -const {filterPluginRpc,guardDebugChannel}=require('./plugin-dap-guard.cjs'); -const fs = require('node:fs');const path = require('node:path');const {randomUUID,createHash}=require('node:crypto');const {pathToFileURL}=require('node:url'); -const {verifyAssertion}=require('../security/assertions.cjs'); -const {Coordinator}=require('../coordination/index.cjs'); -const Context=Symbol('WishForgeConnectionContext'),Service=Symbol('WishForgeCoordinatorService'); -const connections=new Map();let engine;let snapshotPending=false;let hostedDebugTouched=false; -function coordinator(){if(!engine){for(const name of ['WISHFORGE_WORKSPACE_PATH','WF_COORDINATION_STATE_DIR','WF_WORKSPACE_ID','WF_TENANT_ID','WF_IDE_GATEWAY_PUBLIC_KEY_FILE'])if(!process.env[name])throw new Error(`COORDINATION_UNCONFIGURED: ${name}`);engine=new Coordinator({workspaceRoot:process.env.WISHFORGE_WORKSPACE_PATH,stateDirectory:process.env.WF_COORDINATION_STATE_DIR});}return engine;} -function actor(channel){coordinator();return verifyAssertion(fs.readFileSync(process.env.WF_IDE_GATEWAY_PUBLIC_KEY_FILE,'utf8'),channel.socket?.request?.headers?.['x-wishforge-actor-assertion'],{tenantId:process.env.WF_TENANT_ID,workspaceId:process.env.WF_WORKSPACE_ID});} -function checkControl(context){const who=actor(context.channel);coordinator().assertDebugController(who.actorId,context.debugToken);return who;} -const timeout=(promise)=>Promise.race([promise,new Promise((_,reject)=>setTimeout(()=>reject(new Error('EDITOR_ACK_TIMEOUT')),10000))]); -class CoordinationService { - constructor(context){this.context=context;} - register(documents){const who=actor(this.context.channel);this.context.actorId=who.actorId;coordinator().register(who.actorId,this.context.id,{documents});return{actorId:who.actorId,clientId:this.context.id,workspaceId:who.workspaceId,workspaceRoot:process.env.WISHFORGE_WORKSPACE_PATH,state:coordinator().state()};} - status(){actor(this.context.channel);return coordinator().state();} - async snapshot(){ - const who=actor(this.context.channel);const participants=[...connections.values()]; - if(snapshotPending)throw new Error('BARRIER_BUSY');snapshotPending=true; - const requestClient=`snapshot-request-${randomUUID()}`; - const prepared=[]; - try{ - for(const entry of participants){const identity=actor(entry.channel);const documents=await timeout(entry.frontend.prepareSnapshot());coordinator().register(identity.actorId,entry.id,{documents});prepared.push({entry,identity,documents});} - coordinator().register(who.actorId,requestClient,{documents:[]}); - const barrier=coordinator().beginBarrier(who.actorId); - coordinator().acknowledge(who.actorId,barrier.id,requestClient,{documents:[]}); - for(const {entry,identity,documents} of prepared){const confirmed=await timeout(entry.frontend.confirmSnapshot(documents));coordinator().acknowledge(identity.actorId,barrier.id,entry.id,{documents:confirmed});} - return await coordinator().commitBarrier(who.actorId,barrier.id); - } finally {coordinator().unregister(who.actorId,requestClient);await Promise.allSettled(participants.map(entry=>timeout(entry.frontend.releaseSnapshot())));snapshotPending=false;} +const { filterPluginRpc, guardDebugChannel } = require('./plugin-dap-guard.cjs'); +const fs = require('node:fs'); +const path = require('node:path'); +const { randomUUID, createHash } = require('node:crypto'); +const { pathToFileURL } = require('node:url'); +const { verifyAssertion } = require('../security/assertions.cjs'); +const { Coordinator } = require('../coordination/index.cjs'); +const Context = Symbol('WishForgeConnectionContext'), + Service = Symbol('WishForgeCoordinatorService'); +const connections = new Map(); +let engine; +let snapshotPending = false; +let hostedDebugTouched = false; +function coordinator() { + if (!engine) { + for (const name of [ + 'WISHFORGE_WORKSPACE_PATH', + 'WF_COORDINATION_STATE_DIR', + 'WF_WORKSPACE_ID', + 'WF_TENANT_ID', + 'WF_IDE_GATEWAY_PUBLIC_KEY_FILE', + ]) + if (!process.env[name]) throw new Error(`COORDINATION_UNCONFIGURED: ${name}`); + engine = new Coordinator({ + workspaceRoot: process.env.WISHFORGE_WORKSPACE_PATH, + stateDirectory: process.env.WF_COORDINATION_STATE_DIR, + }); + } + return engine; +} +function actor(channel) { + coordinator(); + return verifyAssertion( + fs.readFileSync(process.env.WF_IDE_GATEWAY_PUBLIC_KEY_FILE, 'utf8'), + channel.socket?.request?.headers?.['x-wishforge-actor-assertion'], + { tenantId: process.env.WF_TENANT_ID, workspaceId: process.env.WF_WORKSPACE_ID }, + ); +} +function checkControl(context) { + const who = actor(context.channel); + coordinator().assertDebugController(who.actorId, context.debugToken); + return who; +} +const timeout = promise => + Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(new Error('EDITOR_ACK_TIMEOUT')), 10000))]); +class CoordinationService { + constructor(context) { + this.context = context; + } + register(documents) { + const who = actor(this.context.channel); + this.context.actorId = who.actorId; + coordinator().register(who.actorId, this.context.id, { documents }); + return { + actorId: who.actorId, + clientId: this.context.id, + workspaceId: who.workspaceId, + workspaceRoot: process.env.WISHFORGE_WORKSPACE_PATH, + state: coordinator().state(), + }; + } + status() { + actor(this.context.channel); + return coordinator().state(); + } + async snapshot() { + const who = actor(this.context.channel); + const participants = [...connections.values()]; + if (snapshotPending) throw new Error('BARRIER_BUSY'); + snapshotPending = true; + const requestClient = `snapshot-request-${randomUUID()}`; + const prepared = []; + try { + for (const entry of participants) { + const identity = actor(entry.channel); + const documents = await timeout(entry.frontend.prepareSnapshot()); + coordinator().register(identity.actorId, entry.id, { documents }); + prepared.push({ entry, identity, documents }); + } + coordinator().register(who.actorId, requestClient, { documents: [] }); + const barrier = coordinator().beginBarrier(who.actorId); + coordinator().acknowledge(who.actorId, barrier.id, requestClient, { documents: [] }); + for (const { entry, identity, documents } of prepared) { + const confirmed = await timeout(entry.frontend.confirmSnapshot(documents)); + coordinator().acknowledge(identity.actorId, barrier.id, entry.id, { documents: confirmed }); + } + return await coordinator().commitBarrier(who.actorId, barrier.id); + } finally { + coordinator().unregister(who.actorId, requestClient); + await Promise.allSettled(participants.map(entry => timeout(entry.frontend.releaseSnapshot()))); + snapshotPending = false; + } + } + readSaved(relative) { + actor(this.context.channel); + const root = fs.realpathSync(process.env.WISHFORGE_WORKSPACE_PATH); + if (typeof relative !== 'string' || !relative || path.isAbsolute(relative)) + throw new Error('INVALID_DOCUMENT_PATH'); + const file = fs.realpathSync(path.resolve(root, relative)); + if (!file.startsWith(root + path.sep) || fs.statSync(file).size > 4 * 1024 * 1024) + throw new Error('DOCUMENT_SCOPE'); + const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW); + let content; + try { + const info = fs.fstatSync(fd); + if (!info.isFile() || info.size > 4 * 1024 * 1024) throw new Error('DOCUMENT_SCOPE'); + content = fs.readFileSync(fd, 'utf8'); + } finally { + fs.closeSync(fd); + } + return { content, digest: createHash('sha256').update(content).digest('hex'), uri: pathToFileURL(file).href }; + } + acquireDebug() { + const who = actor(this.context.channel); + this.context.actorId = who.actorId; + const lease = coordinator().debugAcquire(who.actorId, this.context.debugToken); + this.context.debugToken = lease.token; + return { actorId: lease.actorId, expiresAt: lease.expiresAt }; + } + releaseDebug() { + const who = actor(this.context.channel); + if (this.context.debugToken) coordinator().debugRelease(who.actorId, this.context.debugToken); + this.context.debugToken = undefined; + return true; + } + async transferDebug(targetClientId) { + const who = actor(this.context.channel); + const target = connections.get(targetClientId); + if (!target) throw new Error('TARGET_NOT_CONNECTED'); + const identity = actor(target.channel); + const lease = coordinator().debugTransfer(who.actorId, this.context.debugToken, identity.actorId); + target.debugToken = lease.token; + this.context.debugToken = undefined; + target.frontend.debugGranted().catch(() => {}); + return { actorId: identity.actorId, clientId: target.id }; + } + peers() { + actor(this.context.channel); + return [...connections.values()].map(entry => ({ clientId: entry.id, actorId: actor(entry.channel).actorId })); } - readSaved(relative){actor(this.context.channel);const root=fs.realpathSync(process.env.WISHFORGE_WORKSPACE_PATH);if(typeof relative!=='string'||!relative||path.isAbsolute(relative))throw new Error('INVALID_DOCUMENT_PATH');const file=fs.realpathSync(path.resolve(root,relative));if(!file.startsWith(root+path.sep)||fs.statSync(file).size>4*1024*1024)throw new Error('DOCUMENT_SCOPE');const fd=fs.openSync(file,fs.constants.O_RDONLY|fs.constants.O_NOFOLLOW);let content;try{const info=fs.fstatSync(fd);if(!info.isFile()||info.size>4*1024*1024)throw new Error('DOCUMENT_SCOPE');content=fs.readFileSync(fd,'utf8');}finally{fs.closeSync(fd);}return{content,digest:createHash('sha256').update(content).digest('hex'),uri:pathToFileURL(file).href};} - acquireDebug(){const who=actor(this.context.channel);this.context.actorId=who.actorId;const lease=coordinator().debugAcquire(who.actorId,this.context.debugToken);this.context.debugToken=lease.token;return{actorId:lease.actorId,expiresAt:lease.expiresAt};} - releaseDebug(){const who=actor(this.context.channel);if(this.context.debugToken)coordinator().debugRelease(who.actorId,this.context.debugToken);this.context.debugToken=undefined;return true;} - async transferDebug(targetClientId){const who=actor(this.context.channel);const target=connections.get(targetClientId);if(!target)throw new Error('TARGET_NOT_CONNECTED');const identity=actor(target.channel);const lease=coordinator().debugTransfer(who.actorId,this.context.debugToken,identity.actorId);target.debugToken=lease.token;this.context.debugToken=undefined;target.frontend.debugGranted().catch(()=>{});return{actorId:identity.actorId,clientId:target.id};} - peers(){actor(this.context.channel);return[...connections.values()].map(entry=>({clientId:entry.id,actorId:actor(entry.channel).actorId}));} } class IntegrationHttp { - configure(app){ - const identity=(req,purpose)=>verifyAssertion(fs.readFileSync(process.env.WF_IDE_GATEWAY_PUBLIC_KEY_FILE,'utf8'),req.headers['x-wishforge-actor-assertion'],{tenantId:process.env.WF_TENANT_ID,workspaceId:process.env.WF_WORKSPACE_ID,purpose}); - app.get('/_wishforge/lifecycle/state',(req,res)=>{try{identity(req,'workspace-lifecycle');res.set('Cache-Control','no-store').json({workspaceId:process.env.WF_WORKSPACE_ID,...sleepReceipt({coordinator:coordinator(),processManager:this.processManager,debugManager:this.debugManager,snapshotPending,hostedDebugTouched})});}catch{res.status(409).json({error:'WORKSPACE_ACTIVITY_UNKNOWN'});}}); - app.get('/_wishforge/integration/state',(req,res)=>{try{identity(req,'integration-state');res.set('Cache-Control','no-store').json({workspaceId:process.env.WF_WORKSPACE_ID,...coordinator().inputState()});}catch{res.status(409).json({error:'WORKSPACE_INPUT_UNAVAILABLE'});}}); - app.post('/_wishforge/integration/snapshot',async(req,res)=>{try{identity(req,'interactive');const service=new CoordinationService({channel:{socket:{request:req}}});await service.snapshot();res.set('Cache-Control','no-store').json({workspaceId:process.env.WF_WORKSPACE_ID,...coordinator().inputState()});}catch(error){res.status(409).json({error:'WORKSPACE_SAVE_BARRIER_REQUIRED',message:error.message});}}); + configure(app) { + const identity = (req, purpose) => + verifyAssertion( + fs.readFileSync(process.env.WF_IDE_GATEWAY_PUBLIC_KEY_FILE, 'utf8'), + req.headers['x-wishforge-actor-assertion'], + { tenantId: process.env.WF_TENANT_ID, workspaceId: process.env.WF_WORKSPACE_ID, purpose }, + ); + app.get('/_wishforge/lifecycle/state', (req, res) => { + try { + identity(req, 'workspace-lifecycle'); + res.set('Cache-Control', 'no-store').json({ + workspaceId: process.env.WF_WORKSPACE_ID, + ...sleepReceipt({ + coordinator: coordinator(), + processManager: this.processManager, + debugManager: this.debugManager, + snapshotPending, + hostedDebugTouched, + }), + }); + } catch { + res.status(409).json({ error: 'WORKSPACE_ACTIVITY_UNKNOWN' }); + } + }); + app.get('/_wishforge/integration/state', (req, res) => { + try { + identity(req, 'integration-state'); + res + .set('Cache-Control', 'no-store') + .json({ workspaceId: process.env.WF_WORKSPACE_ID, ...coordinator().inputState() }); + } catch { + res.status(409).json({ error: 'WORKSPACE_INPUT_UNAVAILABLE' }); + } + }); + app.post('/_wishforge/integration/snapshot', async (req, res) => { + try { + identity(req, 'interactive'); + const service = new CoordinationService({ channel: { socket: { request: req } } }); + await service.snapshot(); + res + .set('Cache-Control', 'no-store') + .json({ workspaceId: process.env.WF_WORKSPACE_ID, ...coordinator().inputState() }); + } catch (error) { + res.status(409).json({ error: 'WORKSPACE_SAVE_BARRIER_REQUIRED', message: error.message }); + } + }); } } -injectable()(IntegrationHttp);inject(ProcessManager)(IntegrationHttp.prototype,'processManager');inject(DebugAdapterSessionManager)(IntegrationHttp.prototype,'debugManager'); +injectable()(IntegrationHttp); +inject(ProcessManager)(IntegrationHttp.prototype, 'processManager'); +inject(DebugAdapterSessionManager)(IntegrationHttp.prototype, 'debugManager'); // Native debug adapters: no command reaches the adapter without the server lease. class ScopedMessagingService extends DefaultMessagingService { - getConnectionChannelHandlers(channel){const handlers=super.getConnectionChannelHandlers(channel); - handlers.push('/services/debug-adapter/:id',({id},wsChannel)=>{try{const context=channel.wishforgeContext;const session=this.container.get(DebugAdapterSessionManager).find(id);if(!session||!context){wsChannel.close();return;}session.start(guardDebugChannel(new ForwardingDebugChannel(wsChannel),()=>checkControl(context)));}catch{wsChannel.close();}});return handlers;} + getConnectionChannelHandlers(channel) { + const handlers = super.getConnectionChannelHandlers(channel); + handlers.push('/services/debug-adapter/:id', ({ id }, wsChannel) => { + try { + const context = channel.wishforgeContext; + const session = this.container.get(DebugAdapterSessionManager).find(id); + if (!session || !context) { + wsChannel.close(); + return; + } + session.start(guardDebugChannel(new ForwardingDebugChannel(wsChannel), () => checkControl(context))); + } catch { + wsChannel.close(); + } + }); + return handlers; + } } injectable()(ScopedMessagingService); class ScopedDebugService extends DebugServiceImpl { - async createDebugSession(...args){checkControl(this.context);return super.createDebugSession(...args);} - async terminateDebugSession(...args){checkControl(this.context);return super.terminateDebugSession(...args);} - dispose(){super.terminateDebugSession().catch(()=>{});} + async createDebugSession(...args) { + checkControl(this.context); + return super.createDebugSession(...args); + } + async terminateDebugSession(...args) { + checkControl(this.context); + return super.terminateDebugSession(...args); + } + dispose() { + super.terminateDebugSession().catch(() => {}); + } } -injectable()(ScopedDebugService);inject(Context)(ScopedDebugService.prototype,'context'); +injectable()(ScopedDebugService); +inject(Context)(ScopedDebugService.prototype, 'context'); // VS Code/js-debug adapters use a different channel inside hosted-plugin RPC. // Gate those requests on the backend before any payload reaches the plugin host. class ScopedHostedPluginProcess extends HostedPluginProcess { - onMessage(hostId,payload){try{const result=filterPluginRpc(payload,()=>{checkControl(this.context);hostedDebugTouched=true;});if(result.denied)this.logger.warn('WishForge debug control denied',result.denied);if(result.replies)this.client?.postMessage(hostId,result.replies);if(result.forwarded)super.onMessage(hostId,result.forwarded);}catch(error){this.logger.error('WishForge rejected malformed plugin RPC',error.message);}} + onMessage(hostId, payload) { + try { + const result = filterPluginRpc(payload, () => { + checkControl(this.context); + hostedDebugTouched = true; + }); + if (result.denied) this.logger.warn('WishForge debug control denied', result.denied); + if (result.replies) this.client?.postMessage(hostId, result.replies); + if (result.forwarded) super.onMessage(hostId, result.forwarded); + } catch (error) { + this.logger.error('WishForge rejected malformed plugin RPC', error.message); + } + } } -injectable()(ScopedHostedPluginProcess);inject(Context)(ScopedHostedPluginProcess.prototype,'context'); -Object.defineProperty(exports,'__esModule',{value:true}); -exports.default=new ContainerModule((bind,_unbind,_isBound,rebind)=>{ +injectable()(ScopedHostedPluginProcess); +inject(Context)(ScopedHostedPluginProcess.prototype, 'context'); +Object.defineProperty(exports, '__esModule', { value: true }); +exports.default = new ContainerModule((bind, _unbind, _isBound, rebind) => { require('./plugin-install-policy.cjs').bindManagedPlugins(rebind); - bind(IntegrationHttp).toSelf().inSingletonScope();bind(BackendApplicationContribution).toService(IntegrationHttp); + bind(IntegrationHttp).toSelf().inSingletonScope(); + bind(BackendApplicationContribution).toService(IntegrationHttp); rebind(DefaultMessagingService).to(ScopedMessagingService).inSingletonScope(); - bind(ConnectionContainerModule).toConstantValue(ConnectionContainerModule.create(({bind,rebind,bindBackendService})=>{ - bind(Context).toDynamicValue(ctx=>{const channel=ctx.container.get(MainChannel);const context={id:randomUUID(),channel};channel.wishforgeContext=context;channel.onClose(()=>{connections.delete(context.id);try{if(context.actorId){coordinator().unregister(context.actorId,context.id);if(context.debugToken)coordinator().debugRelease(context.actorId,context.debugToken);}}catch{}});return context;}).inSingletonScope(); - bind(Service).toDynamicValue(ctx=>new CoordinationService(ctx.container.get(Context))).inSingletonScope(); - bindBackendService('/services/wishforge-coordination',Service,(service,frontend)=>{service.context.frontend=frontend;connections.set(service.context.id,service.context);return service;}); - rebind(DebugService).to(ScopedDebugService).inSingletonScope(); - rebind(HostedPluginProcess).to(ScopedHostedPluginProcess).inSingletonScope(); - })); + bind(ConnectionContainerModule).toConstantValue( + ConnectionContainerModule.create(({ bind, rebind, bindBackendService }) => { + bind(Context) + .toDynamicValue(ctx => { + const channel = ctx.container.get(MainChannel); + const context = { id: randomUUID(), channel }; + channel.wishforgeContext = context; + channel.onClose(() => { + connections.delete(context.id); + try { + if (context.actorId) { + coordinator().unregister(context.actorId, context.id); + if (context.debugToken) coordinator().debugRelease(context.actorId, context.debugToken); + } + } catch {} + }); + return context; + }) + .inSingletonScope(); + bind(Service) + .toDynamicValue(ctx => new CoordinationService(ctx.container.get(Context))) + .inSingletonScope(); + bindBackendService('/services/wishforge-coordination', Service, (service, frontend) => { + service.context.frontend = frontend; + connections.set(service.context.id, service.context); + return service; + }); + rebind(DebugService).to(ScopedDebugService).inSingletonScope(); + rebind(HostedPluginProcess).to(ScopedHostedPluginProcess).inSingletonScope(); + }), + ); }); diff --git a/apps/ide/policy/coordination-frontend.cjs b/apps/ide/policy/coordination-frontend.cjs index e975c91..6a5811e 100644 --- a/apps/ide/policy/coordination-frontend.cjs +++ b/apps/ide/policy/coordination-frontend.cjs @@ -1,42 +1,306 @@ -const { FrontendApplicationContribution, ApplicationShell, OpenerService, open }=require('@theia/core/lib/browser'); -const { CommandContribution, MessageService, QuickInputService, UntitledResourceResolver, URI }=require('@theia/core'); -const { EditorManager }=require('@theia/editor/lib/browser'); -const { WorkspaceService }=require('@theia/workspace/lib/browser'); -const { WebSocketConnectionProvider }=require('@theia/core/lib/browser/messaging/ws-connection-provider'); -const { StatusBar, StatusBarAlignment }=require('@theia/core/lib/browser/status-bar/status-bar'); -const { DiffUris }=require('@theia/core/lib/browser/diff-uris'); -const { DraftStore }=require('./drafts.cjs'); -const Service=Symbol('WishForgeFrontendCoordination'); -const vector=documents=>JSON.stringify(documents.map(d=>({uri:d.uri,version:d.version,dirty:d.dirty})).sort((a,b)=>a.uri.localeCompare(b.uri))); +const { FrontendApplicationContribution, ApplicationShell, OpenerService, open } = require('@theia/core/lib/browser'); +const { + CommandContribution, + MessageService, + QuickInputService, + UntitledResourceResolver, + URI, +} = require('@theia/core'); +const { EditorManager } = require('@theia/editor/lib/browser'); +const { WorkspaceService } = require('@theia/workspace/lib/browser'); +const { WebSocketConnectionProvider } = require('@theia/core/lib/browser/messaging/ws-connection-provider'); +const { StatusBar, StatusBarAlignment } = require('@theia/core/lib/browser/status-bar/status-bar'); +const { DiffUris } = require('@theia/core/lib/browser/diff-uris'); +const { DraftStore } = require('./drafts.cjs'); +const Service = Symbol('WishForgeFrontendCoordination'); +const vector = documents => + JSON.stringify( + documents.map(d => ({ uri: d.uri, version: d.version, dirty: d.dirty })).sort((a, b) => a.uri.localeCompare(b.uri)), + ); class FrontendCoordination { - constructor(services){Object.assign(this,services);this.drafts=new DraftStore(window.indexedDB);this.tracked=new Map();this.frozen=new Map();this.controller=false;this.busy=false;this.draftWarning=false;} - documents(){const documents=new Map();for(const widget of this.editors.all){const doc={uri:widget.editor.uri.toString(),version:widget.editor.document.version,dirty:widget.editor.document.dirty};const previous=documents.get(doc.uri);if(previous&&previous.version!==doc.version)throw new Error('Conflicting editor versions for one document');documents.set(doc.uri,{...doc,dirty:doc.dirty||previous?.dirty===true});}return [...documents.values()];} - async onStart(){ - this.rpc=this.connection.createProxy('/services/wishforge-coordination',{prepareSnapshot:()=>this.prepareSnapshot(),confirmSnapshot:docs=>this.confirmSnapshot(docs),releaseSnapshot:()=>this.releaseSnapshot(),debugGranted:()=>{this.controller=true;return this.heartbeat();}}); - this.rpc.onDidCloseConnection?.(()=>{this.releaseSnapshot();this.controller=false;}); - this.editors.onCreated(widget=>this.track(widget));for(const widget of this.editors.all)this.track(widget); - await this.heartbeat();this.timer=setInterval(()=>this.heartbeat(),5000); - window.addEventListener('pagehide',()=>{for(const widget of this.editors.all)this.persistDraft(widget);}); + constructor(services) { + Object.assign(this, services); + this.drafts = new DraftStore(window.indexedDB); + this.tracked = new Map(); + this.frozen = new Map(); + this.controller = false; + this.busy = false; + this.draftWarning = false; } - async heartbeat(){try{this.identity=await this.rpc.register(this.documents());this.scope=`${this.identity.workspaceId}/${this.identity.actorId}`;if(this.controller)await this.rpc.acquireDebug();const state=await this.rpc.status();this.status.setElement('wishforge-coordination',{text:`$(cloud-upload) Saved r${state.revision}${this.controller?' · Debug control':''}`,alignment:StatusBarAlignment.RIGHT,priority:5,tooltip:'WishForge coordinated snapshot and draft recovery',command:'wishforge.snapshot'});if(!this.recoveryNotified){this.recoveryNotified=true;const saved=await this.drafts.list(this.scope);if(saved.length)this.messages.info(`${saved.length} unconfirmed draft(s) are available. Use WishForge: Recover Unsaved Draft to compare them with saved files.`);}}catch{this.controller=false;this.status.setElement('wishforge-coordination',{text:'$(warning) Coordination unavailable',alignment:StatusBarAlignment.RIGHT,priority:5,tooltip:'Snapshots and debug control require authenticated workspace coordination.'});}} - pathFor(widget){const current=widget.editor.uri;for(const root of this.workspace.tryGetRoots()){const prefix=root.resource.path.toString().replace(/\/$/,'')+'/';if(current.scheme===root.resource.scheme&¤t.path.toString().startsWith(prefix))return current.path.toString().slice(prefix.length);}return undefined;} - track(widget){if(this.tracked.has(widget))return;const state={id:window.crypto.randomUUID(),baseText:widget.editor.document.getText(),wasDirty:widget.editor.document.dirty};this.tracked.set(widget,state);const change=()=>{if(!state.timer)state.timer=setTimeout(()=>{state.timer=undefined;this.persistDraft(widget);},120);this.rpc?.register(this.documents()).catch(()=>{});};widget.editor.document.onContentChanged(change);widget.editor.document.onDirtyChanged(change);if(this.busy)this.freeze(widget);} - async persistDraft(widget){const state=this.tracked.get(widget);if(!state||!this.scope)return;const doc=widget.editor.document;const relative=this.pathFor(widget);if(!relative)return; - try{if(doc.dirty){state.wasDirty=true;await this.drafts.put({id:state.id,scope:this.scope,path:relative,uri:widget.editor.uri.toString(),text:doc.getText(),baseText:state.baseText,version:doc.version,updatedAt:Date.now()});}else if(state.wasDirty){await this.drafts.remove(state.id);state.baseText=doc.getText();state.wasDirty=false;}}catch(error){if(!this.draftWarning){this.draftWarning=true;this.messages.warn(`Draft recovery storage unavailable: ${error.message}`);}} + documents() { + const documents = new Map(); + for (const widget of this.editors.all) { + const doc = { + uri: widget.editor.uri.toString(), + version: widget.editor.document.version, + dirty: widget.editor.document.dirty, + }; + const previous = documents.get(doc.uri); + if (previous && previous.version !== doc.version) throw new Error('Conflicting editor versions for one document'); + documents.set(doc.uri, { ...doc, dirty: doc.dirty || previous?.dirty === true }); + } + return [...documents.values()]; } - freeze(widget){if(this.frozen.has(widget))return;const control=widget.editor.getControl?.();if(!control?.updateOptions)throw new Error('This editor cannot be frozen for a coordinated snapshot');const previous=control.getRawOptions?.().readOnly??widget.editor.readOnly??false;this.frozen.set(widget,{control,previous});control.updateOptions({readOnly:true});} - async prepareSnapshot(){if(this.busy)throw new Error('Editor is already participating in a save barrier');this.busy=true;for(const w of this.editors.all)this.freeze(w);try{await this.shell.saveAll();const docs=this.documents();if(docs.some(doc=>doc.dirty))throw new Error('Unconfirmed or unsaved documents prevent the snapshot');return docs;}catch(error){this.releaseSnapshot();throw error;}} - confirmSnapshot(expected){const docs=this.documents();if(!this.busy||vector(docs)!==vector(expected)||docs.some(doc=>doc.dirty))throw new Error('Document versions changed during the save barrier');return docs;} - releaseSnapshot(){for(const {control,previous}of this.frozen.values())control.updateOptions({readOnly:previous});this.frozen.clear();this.busy=false;return true;} - registerCommands(commands){ - commands.registerCommand({id:'wishforge.snapshot',label:'Create Verified Workspace Snapshot',category:'WishForge'},{execute:async()=>{try{const snapshot=await this.rpc.snapshot();await this.heartbeat();this.messages.info(`Saved workspace revision ${snapshot.revision} (${snapshot.digest.slice(0,12)}).`);}catch(error){this.messages.error(`Snapshot refused: ${error.message}`);}}}); - commands.registerCommand({id:'wishforge.recover-draft',label:'Recover Unsaved Draft',category:'WishForge'},{execute:()=>this.recover()}); - commands.registerCommand({id:'wishforge.debug-acquire',label:'Acquire Debug Control',category:'WishForge'},{execute:async()=>{try{await this.rpc.acquireDebug();this.controller=true;await this.heartbeat();this.messages.info('Debug control acquired for this client.');}catch(error){this.messages.error(error.message);}}}); - commands.registerCommand({id:'wishforge.debug-release',label:'Release Debug Control',category:'WishForge'},{execute:async()=>{await this.rpc.releaseDebug();this.controller=false;await this.heartbeat();}}); - commands.registerCommand({id:'wishforge.debug-transfer',label:'Transfer Debug Control',category:'WishForge'},{execute:async()=>{try{const peers=(await this.rpc.peers()).filter(p=>p.clientId!==this.identity?.clientId);const picked=await this.quick.pick(peers.map(p=>({label:p.actorId,description:p.clientId,value:p.clientId})),{title:'Transfer control to a connected workspace client'});if(picked){await this.rpc.transferDebug(picked.value);this.controller=false;await this.heartbeat();}}catch(error){this.messages.error(error.message);}}}); + async onStart() { + this.rpc = this.connection.createProxy('/services/wishforge-coordination', { + prepareSnapshot: () => this.prepareSnapshot(), + confirmSnapshot: docs => this.confirmSnapshot(docs), + releaseSnapshot: () => this.releaseSnapshot(), + debugGranted: () => { + this.controller = true; + return this.heartbeat(); + }, + }); + this.rpc.onDidCloseConnection?.(() => { + this.releaseSnapshot(); + this.controller = false; + }); + this.editors.onCreated(widget => this.track(widget)); + for (const widget of this.editors.all) this.track(widget); + await this.heartbeat(); + this.timer = setInterval(() => this.heartbeat(), 5000); + window.addEventListener('pagehide', () => { + for (const widget of this.editors.all) this.persistDraft(widget); + }); + } + async heartbeat() { + try { + this.identity = await this.rpc.register(this.documents()); + this.scope = `${this.identity.workspaceId}/${this.identity.actorId}`; + if (this.controller) await this.rpc.acquireDebug(); + const state = await this.rpc.status(); + this.status.setElement('wishforge-coordination', { + text: `$(cloud-upload) Saved r${state.revision}${this.controller ? ' · Debug control' : ''}`, + alignment: StatusBarAlignment.RIGHT, + priority: 5, + tooltip: 'WishForge coordinated snapshot and draft recovery', + command: 'wishforge.snapshot', + }); + if (!this.recoveryNotified) { + this.recoveryNotified = true; + const saved = await this.drafts.list(this.scope); + if (saved.length) + this.messages.info( + `${saved.length} unconfirmed draft(s) are available. Use WishForge: Recover Unsaved Draft to compare them with saved files.`, + ); + } + } catch { + this.controller = false; + this.status.setElement('wishforge-coordination', { + text: '$(warning) Coordination unavailable', + alignment: StatusBarAlignment.RIGHT, + priority: 5, + tooltip: 'Snapshots and debug control require authenticated workspace coordination.', + }); + } + } + pathFor(widget) { + const current = widget.editor.uri; + for (const root of this.workspace.tryGetRoots()) { + const prefix = root.resource.path.toString().replace(/\/$/, '') + '/'; + if (current.scheme === root.resource.scheme && current.path.toString().startsWith(prefix)) + return current.path.toString().slice(prefix.length); + } + return undefined; + } + track(widget) { + if (this.tracked.has(widget)) return; + const state = { + id: window.crypto.randomUUID(), + baseText: widget.editor.document.getText(), + wasDirty: widget.editor.document.dirty, + }; + this.tracked.set(widget, state); + const change = () => { + if (!state.timer) + state.timer = setTimeout(() => { + state.timer = undefined; + this.persistDraft(widget); + }, 120); + this.rpc?.register(this.documents()).catch(() => {}); + }; + widget.editor.document.onContentChanged(change); + widget.editor.document.onDirtyChanged(change); + if (this.busy) this.freeze(widget); + } + async persistDraft(widget) { + const state = this.tracked.get(widget); + if (!state || !this.scope) return; + const doc = widget.editor.document; + const relative = this.pathFor(widget); + if (!relative) return; + try { + if (doc.dirty) { + state.wasDirty = true; + await this.drafts.put({ + id: state.id, + scope: this.scope, + path: relative, + uri: widget.editor.uri.toString(), + text: doc.getText(), + baseText: state.baseText, + version: doc.version, + updatedAt: Date.now(), + }); + } else if (state.wasDirty) { + await this.drafts.remove(state.id); + state.baseText = doc.getText(); + state.wasDirty = false; + } + } catch (error) { + if (!this.draftWarning) { + this.draftWarning = true; + this.messages.warn(`Draft recovery storage unavailable: ${error.message}`); + } + } + } + freeze(widget) { + if (this.frozen.has(widget)) return; + const control = widget.editor.getControl?.(); + if (!control?.updateOptions) throw new Error('This editor cannot be frozen for a coordinated snapshot'); + const previous = control.getRawOptions?.().readOnly ?? widget.editor.readOnly ?? false; + this.frozen.set(widget, { control, previous }); + control.updateOptions({ readOnly: true }); + } + async prepareSnapshot() { + if (this.busy) throw new Error('Editor is already participating in a save barrier'); + this.busy = true; + for (const w of this.editors.all) this.freeze(w); + try { + await this.shell.saveAll(); + const docs = this.documents(); + if (docs.some(doc => doc.dirty)) throw new Error('Unconfirmed or unsaved documents prevent the snapshot'); + return docs; + } catch (error) { + this.releaseSnapshot(); + throw error; + } + } + confirmSnapshot(expected) { + const docs = this.documents(); + if (!this.busy || vector(docs) !== vector(expected) || docs.some(doc => doc.dirty)) + throw new Error('Document versions changed during the save barrier'); + return docs; + } + releaseSnapshot() { + for (const { control, previous } of this.frozen.values()) control.updateOptions({ readOnly: previous }); + this.frozen.clear(); + this.busy = false; + return true; + } + registerCommands(commands) { + commands.registerCommand( + { id: 'wishforge.snapshot', label: 'Create Verified Workspace Snapshot', category: 'WishForge' }, + { + execute: async () => { + try { + const snapshot = await this.rpc.snapshot(); + await this.heartbeat(); + this.messages.info(`Saved workspace revision ${snapshot.revision} (${snapshot.digest.slice(0, 12)}).`); + } catch (error) { + this.messages.error(`Snapshot refused: ${error.message}`); + } + }, + }, + ); + commands.registerCommand( + { id: 'wishforge.recover-draft', label: 'Recover Unsaved Draft', category: 'WishForge' }, + { execute: () => this.recover() }, + ); + commands.registerCommand( + { id: 'wishforge.debug-acquire', label: 'Acquire Debug Control', category: 'WishForge' }, + { + execute: async () => { + try { + await this.rpc.acquireDebug(); + this.controller = true; + await this.heartbeat(); + this.messages.info('Debug control acquired for this client.'); + } catch (error) { + this.messages.error(error.message); + } + }, + }, + ); + commands.registerCommand( + { id: 'wishforge.debug-release', label: 'Release Debug Control', category: 'WishForge' }, + { + execute: async () => { + await this.rpc.releaseDebug(); + this.controller = false; + await this.heartbeat(); + }, + }, + ); + commands.registerCommand( + { id: 'wishforge.debug-transfer', label: 'Transfer Debug Control', category: 'WishForge' }, + { + execute: async () => { + try { + const peers = (await this.rpc.peers()).filter(p => p.clientId !== this.identity?.clientId); + const picked = await this.quick.pick( + peers.map(p => ({ label: p.actorId, description: p.clientId, value: p.clientId })), + { title: 'Transfer control to a connected workspace client' }, + ); + if (picked) { + await this.rpc.transferDebug(picked.value); + this.controller = false; + await this.heartbeat(); + } + } catch (error) { + this.messages.error(error.message); + } + }, + }, + ); + } + async recover() { + try { + if (!this.scope) throw new Error('Authenticated workspace identity unavailable'); + const records = await this.drafts.list(this.scope); + const picked = await this.quick.pick( + records.map(record => ({ + label: record.path, + description: new Date(record.updatedAt).toLocaleString(), + record, + })), + { title: 'Compare an unconfirmed draft with the saved file' }, + ); + if (!picked) return; + const draft = picked.record; + const saved = await this.rpc.readSaved(draft.path); + const resource = await this.untitled.createUntitledResource(draft.text, draft.path.split('.').at(-1)); + const left = new URI('mem-txt-readonly:/Saved-file').withQuery(saved.content); + await open(this.opener, DiffUris.encode(left, resource.uri, `Saved ↔ recovered ${draft.path}`)); + this.messages.info( + saved.content === draft.baseText + ? 'Draft opened as an unsaved recovery copy. Review and Save As to apply it.' + : 'The saved file changed after this draft. Review the comparison before applying the recovery copy.', + ); + } catch (error) { + this.messages.error(`Draft recovery: ${error.message}`); + } } - async recover(){try{if(!this.scope)throw new Error('Authenticated workspace identity unavailable');const records=await this.drafts.list(this.scope);const picked=await this.quick.pick(records.map(record=>({label:record.path,description:new Date(record.updatedAt).toLocaleString(),record})),{title:'Compare an unconfirmed draft with the saved file'});if(!picked)return;const draft=picked.record;const saved=await this.rpc.readSaved(draft.path);const resource=await this.untitled.createUntitledResource(draft.text,draft.path.split('.').at(-1));const left=new URI('mem-txt-readonly:/Saved-file').withQuery(saved.content);await open(this.opener,DiffUris.encode(left,resource.uri,`Saved ↔ recovered ${draft.path}`));this.messages.info(saved.content===draft.baseText?'Draft opened as an unsaved recovery copy. Review and Save As to apply it.':'The saved file changed after this draft. Review the comparison before applying the recovery copy.');}catch(error){this.messages.error(`Draft recovery: ${error.message}`);}} } -function bindCoordination(bind){bind(Service).toDynamicValue(ctx=>new FrontendCoordination({editors:ctx.container.get(EditorManager),workspace:ctx.container.get(WorkspaceService),shell:ctx.container.get(ApplicationShell),messages:ctx.container.get(MessageService),quick:ctx.container.get(QuickInputService),untitled:ctx.container.get(UntitledResourceResolver),opener:ctx.container.get(OpenerService),connection:ctx.container.get(WebSocketConnectionProvider),status:ctx.container.get(StatusBar)})).inSingletonScope();bind(FrontendApplicationContribution).toService(Service);bind(CommandContribution).toService(Service);} -module.exports={bindCoordination,FrontendCoordination}; +function bindCoordination(bind) { + bind(Service) + .toDynamicValue( + ctx => + new FrontendCoordination({ + editors: ctx.container.get(EditorManager), + workspace: ctx.container.get(WorkspaceService), + shell: ctx.container.get(ApplicationShell), + messages: ctx.container.get(MessageService), + quick: ctx.container.get(QuickInputService), + untitled: ctx.container.get(UntitledResourceResolver), + opener: ctx.container.get(OpenerService), + connection: ctx.container.get(WebSocketConnectionProvider), + status: ctx.container.get(StatusBar), + }), + ) + .inSingletonScope(); + bind(FrontendApplicationContribution).toService(Service); + bind(CommandContribution).toService(Service); +} +module.exports = { bindCoordination, FrontendCoordination }; diff --git a/apps/ide/policy/drafts.cjs b/apps/ide/policy/drafts.cjs index 4be0101..cf50f92 100644 --- a/apps/ide/policy/drafts.cjs +++ b/apps/ide/policy/drafts.cjs @@ -1,9 +1,46 @@ class DraftStore { - constructor(indexedDB){this.indexedDB=indexedDB;} - async database(){if(!this.pending)this.pending=new Promise((resolve,reject)=>{const request=this.indexedDB.open('wishforge-unconfirmed-drafts',1);request.onupgradeneeded=()=>request.result.createObjectStore('drafts',{keyPath:'id'});request.onsuccess=()=>resolve(request.result);request.onerror=()=>reject(request.error);});return this.pending;} - async operation(mode, action){const db=await this.database();return new Promise((resolve,reject)=>{const tx=db.transaction('drafts',mode);const request=action(tx.objectStore('drafts'));let value;request.onsuccess=()=>{value=request.result;};tx.oncomplete=()=>resolve(value);tx.onerror=()=>reject(tx.error);tx.onabort=()=>reject(tx.error);});} - put(record){if(record.text.length+record.baseText.length>2*1024*1024||new TextEncoder().encode(record.text+record.baseText).byteLength>2*1024*1024)throw new Error('Draft exceeds the 2 MiB local recovery limit');return this.operation('readwrite',store=>store.put(record));} - remove(id){return this.operation('readwrite',store=>store.delete(id));} - async list(scope){return(await this.operation('readonly',store=>store.getAll())).filter(record=>record.scope===scope).sort((a,b)=>b.updatedAt-a.updatedAt);} + constructor(indexedDB) { + this.indexedDB = indexedDB; + } + async database() { + if (!this.pending) + this.pending = new Promise((resolve, reject) => { + const request = this.indexedDB.open('wishforge-unconfirmed-drafts', 1); + request.onupgradeneeded = () => request.result.createObjectStore('drafts', { keyPath: 'id' }); + request.onsuccess = () => resolve(request.result); + request.onerror = () => reject(request.error); + }); + return this.pending; + } + async operation(mode, action) { + const db = await this.database(); + return new Promise((resolve, reject) => { + const tx = db.transaction('drafts', mode); + const request = action(tx.objectStore('drafts')); + let value; + request.onsuccess = () => { + value = request.result; + }; + tx.oncomplete = () => resolve(value); + tx.onerror = () => reject(tx.error); + tx.onabort = () => reject(tx.error); + }); + } + put(record) { + if ( + record.text.length + record.baseText.length > 2 * 1024 * 1024 || + new TextEncoder().encode(record.text + record.baseText).byteLength > 2 * 1024 * 1024 + ) + throw new Error('Draft exceeds the 2 MiB local recovery limit'); + return this.operation('readwrite', store => store.put(record)); + } + remove(id) { + return this.operation('readwrite', store => store.delete(id)); + } + async list(scope) { + return (await this.operation('readonly', store => store.getAll())) + .filter(record => record.scope === scope) + .sort((a, b) => b.updatedAt - a.updatedAt); + } } -module.exports={DraftStore}; +module.exports = { DraftStore }; diff --git a/apps/ide/policy/frontend.cjs b/apps/ide/policy/frontend.cjs index 6759afe..af8a327 100644 --- a/apps/ide/policy/frontend.cjs +++ b/apps/ide/policy/frontend.cjs @@ -1,5 +1,7 @@ const { ContainerModule, injectable } = require('@theia/core/shared/inversify'); -const { CollaborationFrontendContribution } = require('@theia/collaboration/lib/browser/collaboration-frontend-contribution'); +const { + CollaborationFrontendContribution, +} = require('@theia/collaboration/lib/browser/collaboration-frontend-contribution'); const { CollaborationWorkspaceService } = require('@theia/collaboration/lib/browser/collaboration-workspace-service'); const { FrontendApplicationConfigProvider } = require('@theia/core/lib/browser/frontend-application-config-provider'); const { ConnectionProvider, SocketIoTransport } = require('open-collaboration-protocol'); @@ -14,28 +16,40 @@ class ScopedCollaborationContribution extends CollaborationFrontendContribution const url = await this.getCollaborationServerUrl(); return collaborationBase(url, window.location.href).href; } - init() { this.scopedUrl().then(() => this.setStatusBarEntryDefault()).catch(() => {}); } + init() { + this.scopedUrl() + .then(() => this.setStatusBarEntryDefault()) + .catch(() => {}); + } registerCommands(commands) { - this.scopedUrl().then(() => super.registerCommands(commands)).catch(() => {}); + this.scopedUrl() + .then(() => super.registerCommands(commands)) + .catch(() => {}); } async createConnectionProvider() { const url = await this.scopedUrl(); - return createPrefixConnectionProvider({ ConnectionProvider, SocketIoTransport, io }, { - url, browserLocation: window.location.href, - client: FrontendApplicationConfigProvider.get().applicationName, - fetch: window.fetch.bind(window), - authenticationHandler: (token, metadata) => this.handleAuth(url, token, metadata), - // OCT tokens are instance-local; never reuse a different workspace token - // from the upstream global localStorage key. - userToken: undefined, - }); + return createPrefixConnectionProvider( + { ConnectionProvider, SocketIoTransport, io }, + { + url, + browserLocation: window.location.href, + client: FrontendApplicationConfigProvider.get().applicationName, + fetch: window.fetch.bind(window), + authenticationHandler: (token, metadata) => this.handleAuth(url, token, metadata), + // OCT tokens are instance-local; never reuse a different workspace token + // from the upstream global localStorage key. + userToken: undefined, + }, + ); } } injectable()(ScopedCollaborationContribution); // Every backend already belongs to one provisioned project. A guest's virtual // collaboration URI must not overwrite the backend-wide default for other tabs. class DedicatedWorkspaceService extends CollaborationWorkspaceService { - isRemoteSession() { return true; } + isRemoteSession() { + return true; + } } injectable()(DedicatedWorkspaceService); module.exports.default = new ContainerModule((_bind, _unbind, _isBound, rebind) => { diff --git a/apps/ide/policy/lifecycle-safety.cjs b/apps/ide/policy/lifecycle-safety.cjs index 7304809..18fef20 100644 --- a/apps/ide/policy/lifecycle-safety.cjs +++ b/apps/ide/policy/lifecycle-safety.cjs @@ -1,13 +1,26 @@ 'use strict'; /** Pinned Theia 1.76 seam. Unknown process/debug inventory is never idle evidence. */ -function sleepReceipt({coordinator,processManager,debugManager,snapshotPending,hostedDebugTouched}){ - try{ - if(!(processManager?.processes instanceof Map)||typeof debugManager?.getAll!=='function')return{known:false}; - const input=coordinator.inputState();const state=coordinator.state(); - // Hosted plugin debug sessions are not enumerated by the native DAP manager. - // Once used, require explicit pause until backend restart instead of guessing termination. - const known=!snapshotPending&&!hostedDebugTouched; - return{known,activeClients:input.activeClients,terminals:processManager.processes.size,debugSessions:[...debugManager.getAll()].length+(state.debugController?1:0),dirty:input.dirty,snapshotComplete:!!input.latestSnapshot&&input.latestSnapshot.digest===input.currentDiskDigest,snapshotRevision:input.latestSnapshot?.revision??0,diskDigest:input.currentDiskDigest}; - }catch{return{known:false};} +function sleepReceipt({ coordinator, processManager, debugManager, snapshotPending, hostedDebugTouched }) { + try { + if (!(processManager?.processes instanceof Map) || typeof debugManager?.getAll !== 'function') + return { known: false }; + const input = coordinator.inputState(); + const state = coordinator.state(); + // Hosted plugin debug sessions are not enumerated by the native DAP manager. + // Once used, require explicit pause until backend restart instead of guessing termination. + const known = !snapshotPending && !hostedDebugTouched; + return { + known, + activeClients: input.activeClients, + terminals: processManager.processes.size, + debugSessions: [...debugManager.getAll()].length + (state.debugController ? 1 : 0), + dirty: input.dirty, + snapshotComplete: !!input.latestSnapshot && input.latestSnapshot.digest === input.currentDiskDigest, + snapshotRevision: input.latestSnapshot?.revision ?? 0, + diskDigest: input.currentDiskDigest, + }; + } catch { + return { known: false }; + } } -module.exports={sleepReceipt}; +module.exports = { sleepReceipt }; diff --git a/apps/ide/policy/managed-plugins.cjs b/apps/ide/policy/managed-plugins.cjs index 394dc08..8deb749 100644 --- a/apps/ide/policy/managed-plugins.cjs +++ b/apps/ide/policy/managed-plugins.cjs @@ -4,7 +4,8 @@ const path = require('node:path'); const { createHash } = require('node:crypto'); const { execFileSync } = require('node:child_process'); const sha256 = value => createHash('sha256').update(value).digest('hex'); -const managedError = () => new Error('EXTENSIONS_MANAGED_BY_ADMIN: Extensions are installed and updated only through the operator image build.'); +const managedError = () => + new Error('EXTENSIONS_MANAGED_BY_ADMIN: Extensions are installed and updated only through the operator image build.'); function treeDigest(root) { const files = []; function visit(directory, prefix = '') { @@ -18,33 +19,48 @@ function treeDigest(root) { } } visit(root); - files.sort((left, right) => left[0] < right[0] ? -1 : left[0] > right[0] ? 1 : 0); + files.sort((left, right) => (left[0] < right[0] ? -1 : left[0] > right[0] ? 1 : 0)); return sha256(files.map(([name, digest]) => `${name}\0${digest}\n`).join('')); } function assertIdentity(directory, expected) { if (fs.lstatSync(directory).isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED'); const manifest = JSON.parse(fs.readFileSync(path.join(directory, 'extension/package.json'), 'utf8')); - if (`${manifest.publisher}.${manifest.name}` !== expected.id || manifest.version !== expected.version) throw new Error('EXTENSION_ID_VERSION_REJECTED'); + if (`${manifest.publisher}.${manifest.name}` !== expected.id || manifest.version !== expected.version) + throw new Error('EXTENSION_ID_VERSION_REJECTED'); } -function validateManagedPlugins(root, { - catalog = require('./managed-plugins.lock.json'), - pythonLock = require('../scripts/python-debugger.lock.json'), - platform = `${process.platform}-${process.arch}`, -} = {}) { +function validateManagedPlugins( + root, + { + catalog = require('./managed-plugins.lock.json'), + pythonLock = require('../scripts/python-debugger.lock.json'), + platform = `${process.platform}-${process.arch}`, + } = {}, +) { if (fs.lstatSync(root).isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED'); const extensions = [...catalog.plugins]; - if (pythonLock) extensions.push( - { directory: pythonLock.id, id: pythonLock.id, version: pythonLock.version }, - { directory: pythonLock.pythonDependency.id, id: pythonLock.pythonDependency.id, version: pythonLock.pythonDependency.version }, - { directory: pythonLock.languageServer.id, id: pythonLock.languageServer.id, version: pythonLock.languageServer.version }, - ); + if (pythonLock) + extensions.push( + { directory: pythonLock.id, id: pythonLock.id, version: pythonLock.version }, + { + directory: pythonLock.pythonDependency.id, + id: pythonLock.pythonDependency.id, + version: pythonLock.pythonDependency.version, + }, + { + directory: pythonLock.languageServer.id, + id: pythonLock.languageServer.id, + version: pythonLock.languageServer.version, + }, + ); const allowed = new Set(extensions.map(extension => extension.directory)); const found = fs.readdirSync(root).filter(name => !name.startsWith('.')); - if (found.length !== allowed.size || found.some(name => !allowed.has(name))) throw new Error('EXTENSION_ALLOWLIST_MISMATCH'); + if (found.length !== allowed.size || found.some(name => !allowed.has(name))) + throw new Error('EXTENSION_ALLOWLIST_MISMATCH'); for (const extension of extensions) { const directory = path.join(root, extension.directory); assertIdentity(directory, extension); - if (extension.sha256 && treeDigest(directory) !== extension.sha256) throw new Error(`EXTENSION_DIGEST_REJECTED: ${extension.id}`); + if (extension.sha256 && treeDigest(directory) !== extension.sha256) + throw new Error(`EXTENSION_DIGEST_REJECTED: ${extension.id}`); } if (pythonLock) { const expected = pythonLock.platforms[platform]; @@ -53,11 +69,19 @@ function validateManagedPlugins(root, { const packages = [ { id: pythonLock.id, file: `${pythonLock.id}-${pythonLock.version}@${platform}.vsix`, sha256: expected }, { id: dependency.id, file: `${dependency.id}-${dependency.version}.vsix`, sha256: dependency.sha256 }, - { id: pythonLock.languageServer.id, file: `${pythonLock.languageServer.id}-${pythonLock.languageServer.version}.vsix`, sha256: pythonLock.languageServer.sha256 }, + { + id: pythonLock.languageServer.id, + file: `${pythonLock.languageServer.id}-${pythonLock.languageServer.version}.vsix`, + sha256: pythonLock.languageServer.sha256, + }, ]; // Compare the unpacked bytes against the checksum-pinned official VSIX files, // never against a manifest self-generated from the current runtime directory. - execFileSync('python3', ['-c', ` + execFileSync( + 'python3', + [ + '-c', + ` import hashlib,json,pathlib,stat,sys,zipfile root=pathlib.Path(sys.argv[1]) for package in json.loads(sys.argv[2]): @@ -72,7 +96,12 @@ for package in json.loads(sys.argv[2]): if p.is_symlink(): raise ValueError('EXTENSION_SYMLINK_REJECTED') if p.is_file(): actual[p.relative_to(target).as_posix()]=hashlib.sha256(p.read_bytes()).hexdigest() if actual!=expected: raise ValueError('EXTENSION_CONTENT_DIGEST_REJECTED: '+package['id']) -`, root, JSON.stringify(packages)], { timeout: 30_000, stdio: ['ignore', 'pipe', 'pipe'] }); +`, + root, + JSON.stringify(packages), + ], + { timeout: 30_000, stdio: ['ignore', 'pipe', 'pipe'] }, + ); } return extensions; } diff --git a/apps/ide/policy/package.json b/apps/ide/policy/package.json index 2232e7a..76c74bf 100644 --- a/apps/ide/policy/package.json +++ b/apps/ide/policy/package.json @@ -2,6 +2,14 @@ "name": "@wishforge/ide-policy", "version": "0.1.0", "private": true, - "theiaExtensions": [{ "frontend": "frontend.cjs", "backend": "backend.cjs" }], - "peerDependencies": { "@theia/core": "1.76.0", "@theia/collaboration": "1.76.0" } + "theiaExtensions": [ + { + "frontend": "frontend.cjs", + "backend": "backend.cjs" + } + ], + "peerDependencies": { + "@theia/core": "1.76.0", + "@theia/collaboration": "1.76.0" + } } diff --git a/apps/ide/policy/plugin-dap-guard.cjs b/apps/ide/policy/plugin-dap-guard.cjs index 87e139f..4bd1faa 100644 --- a/apps/ide/policy/plugin-dap-guard.cjs +++ b/apps/ide/policy/plugin-dap-guard.cjs @@ -1,7 +1,13 @@ 'use strict'; -const { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } = require('@theia/core/lib/common/message-rpc/uint8-array-message-buffer'); -const { MsgPackMessageDecoder, MsgPackMessageEncoder } = require('@theia/core/lib/common/message-rpc/rpc-message-encoder'); +const { + Uint8ArrayReadBuffer, + Uint8ArrayWriteBuffer, +} = require('@theia/core/lib/common/message-rpc/uint8-array-message-buffer'); +const { + MsgPackMessageDecoder, + MsgPackMessageEncoder, +} = require('@theia/core/lib/common/message-rpc/rpc-message-encoder'); const MAX_BATCH_MESSAGES = 10_000; const MAX_BATCH_BYTES = 64 * 1024 * 1024; @@ -10,7 +16,9 @@ const MAX_MESSAGE_BYTES = 16 * 1024 * 1024; // numbers increasing across batches for the lifetime of this backend process. let pluginDapResponseSequence = 0; -function rejected(code) { return Object.assign(new Error(code), { code }); } +function rejected(code) { + return Object.assign(new Error(code), { code }); +} // Theia's ReadBuffer intentionally trusts frame lengths. Validate every boundary // first: a Uint8Array may be a view into a larger websocket backing buffer. @@ -42,11 +50,19 @@ function decodeBatch(payload) { const encodedLength = readLength(bytes, inner, MAX_MESSAGE_BYTES); if (encodedLength !== bytes.byteLength - inner.offset) throw rejected('MALFORMED_PLUGIN_RPC'); let message; - try { message = decoder.parse(new Uint8ArrayReadBuffer(bytes)); } - catch { throw rejected('UNDECODABLE_PLUGIN_RPC'); } + try { + message = decoder.parse(new Uint8ArrayReadBuffer(bytes)); + } catch { + throw rejected('UNDECODABLE_PLUGIN_RPC'); + } if (!message || ![1, 2, 3, 4, 5].includes(message.type)) throw rejected('MALFORMED_PLUGIN_RPC'); - if ((message.type !== 2 || message.id !== undefined) && (!Number.isSafeInteger(message.id) || message.id < 0)) throw rejected('MALFORMED_PLUGIN_RPC'); - if ((message.type === 1 || message.type === 2) && (typeof message.method !== 'string' || !Array.isArray(message.args))) throw rejected('MALFORMED_PLUGIN_RPC'); + if ((message.type !== 2 || message.id !== undefined) && (!Number.isSafeInteger(message.id) || message.id < 0)) + throw rejected('MALFORMED_PLUGIN_RPC'); + if ( + (message.type === 1 || message.type === 2) && + (typeof message.method !== 'string' || !Array.isArray(message.args)) + ) + throw rejected('MALFORMED_PLUGIN_RPC'); messages.push({ bytes, message }); } if (cursor.offset !== payload.byteLength) throw rejected('MALFORMED_PLUGIN_RPC'); @@ -65,8 +81,10 @@ function encodeBatch(messages) { function gated(message) { if (message.type !== 1 && message.type !== 2) return false; - return (message.args[0] === 'DebugExt' && ['$createDebugSession', '$terminateDebugSession'].includes(message.method)) || - (message.args[0] === 'ConnectionExt' && ['$sendMessage', '$deleteConnection'].includes(message.method)); + return ( + (message.args[0] === 'DebugExt' && ['$createDebugSession', '$terminateDebugSession'].includes(message.method)) || + (message.args[0] === 'ConnectionExt' && ['$sendMessage', '$deleteConnection'].includes(message.method)) + ); } function assertSynchronously(assertControl) { @@ -78,8 +96,21 @@ function assertSynchronously(assertControl) { function dapRequest(value) { if (typeof value !== 'string') return null; let request; - try { request = JSON.parse(value); } catch { return null; } - if (!request || Array.isArray(request) || request.type !== 'request' || !Number.isSafeInteger(request.seq) || request.seq < 0 || typeof request.command !== 'string' || !request.command) return null; + try { + request = JSON.parse(value); + } catch { + return null; + } + if ( + !request || + Array.isArray(request) || + request.type !== 'request' || + !Number.isSafeInteger(request.seq) || + request.seq < 0 || + typeof request.command !== 'string' || + !request.command + ) + return null; return request; } @@ -90,8 +121,12 @@ function pluginDapDenial(message, encoder) { const request = dapRequest(message.args[2]); if (!request) return null; const response = { - seq: ++pluginDapResponseSequence, type: 'response', request_seq: request.seq, - command: request.command, success: false, message: 'WishForge debug controller lease required', + seq: ++pluginDapResponseSequence, + type: 'response', + request_seq: request.seq, + command: request.command, + success: false, + message: 'WishForge debug controller lease required', }; const writer = new Uint8ArrayWriteBuffer(); // PluginChannel.send discards its RPC promise. ReplyErr alone leaves the DAP @@ -112,8 +147,9 @@ function filterPluginRpc(payload, assertControl) { let denied = 0; for (const { bytes, message } of messages) { if (gated(message)) { - try { assertSynchronously(assertControl); } - catch { + try { + assertSynchronously(assertControl); + } catch { denied++; if (message.type === 1) { const writer = new Uint8ArrayWriteBuffer(); @@ -140,18 +176,44 @@ function guardDebugChannel(raw, assertControl) { onClose: callback => raw.onClose(callback), onError: callback => raw.onError(callback), close: () => raw.close(), - onMessage: callback => raw.onMessage(message => { - try { assertSynchronously(assertControl); } - catch { - let request; - try { request = JSON.parse(message); } catch { raw.close(); return; } - if (!request || request.type !== 'request' || !Number.isSafeInteger(request.seq) || request.seq < 0 || typeof request.command !== 'string' || !request.command) { raw.close(); return; } - raw.send(JSON.stringify({ seq: ++responseSequence, type: 'response', request_seq: request.seq, command: request.command, success: false, message: 'WishForge debug controller lease required' })); - return; - } - // Adapter callback errors are not authorization failures and must not be swallowed. - callback(message); - }), + onMessage: callback => + raw.onMessage(message => { + try { + assertSynchronously(assertControl); + } catch { + let request; + try { + request = JSON.parse(message); + } catch { + raw.close(); + return; + } + if ( + !request || + request.type !== 'request' || + !Number.isSafeInteger(request.seq) || + request.seq < 0 || + typeof request.command !== 'string' || + !request.command + ) { + raw.close(); + return; + } + raw.send( + JSON.stringify({ + seq: ++responseSequence, + type: 'response', + request_seq: request.seq, + command: request.command, + success: false, + message: 'WishForge debug controller lease required', + }), + ); + return; + } + // Adapter callback errors are not authorization failures and must not be swallowed. + callback(message); + }), }; } diff --git a/apps/ide/policy/plugin-install-frontend.cjs b/apps/ide/policy/plugin-install-frontend.cjs index d3c52e5..0327d6a 100644 --- a/apps/ide/policy/plugin-install-frontend.cjs +++ b/apps/ide/policy/plugin-install-frontend.cjs @@ -15,9 +15,17 @@ class ManagedExtensionsNotice { const search = widget.searchBar; search.wishforgeManagedNotice = true; const render = search.render.bind(search); - search.render = () => React.createElement('div', { style: { width: '100%' } }, - React.createElement('p', { style: { margin: '0 0 8px', fontSize: '12px', lineHeight: '1.6' } }, - '扩展由管理员管理。安装与更新须通过平台镜像发布。'), render()); + search.render = () => + React.createElement( + 'div', + { style: { width: '100%' } }, + React.createElement( + 'p', + { style: { margin: '0 0 8px', fontSize: '12px', lineHeight: '1.6' } }, + '扩展由管理员管理。安装与更新须通过平台镜像发布。', + ), + render(), + ); search.update(); }; this.widgets.getWidgets('vsx-extensions-view-container').forEach(label); diff --git a/apps/ide/policy/plugin-install-policy.cjs b/apps/ide/policy/plugin-install-policy.cjs index 5aaeaee..7273078 100644 --- a/apps/ide/policy/plugin-install-policy.cjs +++ b/apps/ide/policy/plugin-install-policy.cjs @@ -7,11 +7,21 @@ const { PluginDeployerImpl } = require('@theia/plugin-ext/lib/main/node/plugin-d const { validateManagedPlugins, managedError } = require('./managed-plugins.cjs'); class ManagedPluginServer extends PluginServerImpl { - async install() { throw managedError(); } - async doInstall() { throw managedError(); } - async uninstall() { throw managedError(); } - async enablePlugin() { throw managedError(); } - async disablePlugin() { throw managedError(); } + async install() { + throw managedError(); + } + async doInstall() { + throw managedError(); + } + async uninstall() { + throw managedError(); + } + async enablePlugin() { + throw managedError(); + } + async disablePlugin() { + throw managedError(); + } } injectable()(ManagedPluginServer); class ManagedPluginDeployer extends PluginDeployerImpl { @@ -24,18 +34,35 @@ class ManagedPluginDeployer extends PluginDeployerImpl { // or resolver participants. All system content was verified before resolution. const entries = await this.resolveAndHandle(`local-dir:${root}`, PluginType.System); const paths = new Set(allowed.map(extension => path.join(root, extension.directory))); - if (entries.length !== paths.size || entries.some(entry => !paths.has(entry.originalPath()) || entry.type !== PluginType.System)) { + if ( + entries.length !== paths.size || + entries.some(entry => !paths.has(entry.originalPath()) || entry.type !== PluginType.System) + ) { throw new Error('EXTENSION_DEPLOYMENT_SCOPE_REJECTED'); } return super.deployPlugins(entries); } - async deploy() { throw managedError(); } - async deployMultipleEntries() { throw managedError(); } - async deployPlugins() { throw managedError(); } - async uninstall() { throw managedError(); } - async undeploy() { throw managedError(); } - async enablePlugin() { throw managedError(); } - async disablePlugin() { throw managedError(); } + async deploy() { + throw managedError(); + } + async deployMultipleEntries() { + throw managedError(); + } + async deployPlugins() { + throw managedError(); + } + async uninstall() { + throw managedError(); + } + async undeploy() { + throw managedError(); + } + async enablePlugin() { + throw managedError(); + } + async disablePlugin() { + throw managedError(); + } } injectable()(ManagedPluginDeployer); function bindManagedPlugins(rebind) { diff --git a/apps/ide/policy/test/backend-guard.test.cjs b/apps/ide/policy/test/backend-guard.test.cjs index ba6181d..0685aee 100644 --- a/apps/ide/policy/test/backend-guard.test.cjs +++ b/apps/ide/policy/test/backend-guard.test.cjs @@ -5,9 +5,19 @@ const assert = require('node:assert/strict'); const fs = require('node:fs'); const os = require('node:os'); const path = require('node:path'); -const { Uint8ArrayReadBuffer, Uint8ArrayWriteBuffer } = require('@theia/core/lib/common/message-rpc/uint8-array-message-buffer'); -const { MsgPackMessageDecoder, MsgPackMessageEncoder } = require('@theia/core/lib/common/message-rpc/rpc-message-encoder'); -const { BatchingChannel, RPCProtocolImpl, createProxyIdentifier } = require('@theia/plugin-ext/lib/common/rpc-protocol'); +const { + Uint8ArrayReadBuffer, + Uint8ArrayWriteBuffer, +} = require('@theia/core/lib/common/message-rpc/uint8-array-message-buffer'); +const { + MsgPackMessageDecoder, + MsgPackMessageEncoder, +} = require('@theia/core/lib/common/message-rpc/rpc-message-encoder'); +const { + BatchingChannel, + RPCProtocolImpl, + createProxyIdentifier, +} = require('@theia/plugin-ext/lib/common/rpc-protocol'); const { ConnectionImpl } = require('@theia/plugin-ext/lib/common/connection'); const { ForwardingDebugChannel } = require('@theia/debug/lib/common/debug-service'); const { Emitter } = require('@theia/core/lib/common/event'); @@ -16,7 +26,9 @@ const { Coordinator, DEBUG_LEASE_MS } = require('../../coordination/index.cjs'); const encoder = new MsgPackMessageEncoder(); const decoder = new MsgPackMessageDecoder(); -const deny = () => { throw new Error('DEBUG_FORBIDDEN'); }; +const deny = () => { + throw new Error('DEBUG_FORBIDDEN'); +}; function rpc(method, service, id = 1, type = 1, rest = []) { const writer = new Uint8ArrayWriteBuffer(); if (type === 2) encoder.notification(writer, method, [service, ...rest], id); @@ -42,18 +54,34 @@ function memoryChannel() { const sent = []; let closed = false; return { - sent, get closed() { return closed; }, - onMessage: messages.event, onError: errors.event, onClose: closes.event, - getWriteBuffer() { const writer = new Uint8ArrayWriteBuffer(); writer.onCommit(value => sent.push(value)); return writer; }, - close() { closed = true; closes.fire({ code: 1000, reason: 'closed' }); }, - receive(bytes) { messages.fire(() => new Uint8ArrayReadBuffer(bytes)); }, + sent, + get closed() { + return closed; + }, + onMessage: messages.event, + onError: errors.event, + onClose: closes.event, + getWriteBuffer() { + const writer = new Uint8ArrayWriteBuffer(); + writer.onCommit(value => sent.push(value)); + return writer; + }, + close() { + closed = true; + closes.fire({ code: 1000, reason: 'closed' }); + }, + receive(bytes) { + messages.fire(() => new Uint8ArrayReadBuffer(bytes)); + }, }; } test('real Theia RPCProtocol emits the DebugExt wire shape gated by the filter', async () => { const wire = memoryChannel(); const protocol = new RPCProtocolImpl(wire); - const pending = protocol.getProxy(createProxyIdentifier('DebugExt')).$createDebugSession({ type: 'pwa-node', name: 'test' }, 'file:///workspace'); + const pending = protocol + .getProxy(createProxyIdentifier('DebugExt')) + .$createDebugSession({ type: 'pwa-node', name: 'test' }, 'file:///workspace'); const rejected = assert.rejects(pending, /DEBUG_CONTROL_REQUIRED/); await new Promise(resolve => setImmediate(resolve)); assert.equal(wire.sent.length, 1); @@ -70,13 +98,18 @@ test('real Theia RPCProtocol emits the DebugExt wire shape gated by the filter', test('unauthorized plugin create/terminate/send/delete requests never enter the forwarded host batch', () => { const calls = [ - rpc('$createDebugSession', 'DebugExt', 10), rpc('$terminateDebugSession', 'DebugExt', 11), - rpc('$sendMessage', 'ConnectionExt', 12, 1, ['session', '{"command":"continue"}']), rpc('$deleteConnection', 'ConnectionExt', 13), + rpc('$createDebugSession', 'DebugExt', 10), + rpc('$terminateDebugSession', 'DebugExt', 11), + rpc('$sendMessage', 'ConnectionExt', 12, 1, ['session', '{"command":"continue"}']), + rpc('$deleteConnection', 'ConnectionExt', 13), ]; const result = filterPluginRpc(batch(...calls), deny); assert.equal(result.forwarded, null); assert.equal(result.denied, 4); - assert.deepEqual(decode(result.replies).map(message => ({ type: message.type, id: message.id, error: message.err.message })), [10, 11, 12, 13].map(id => ({ type: 4, id, error: 'DEBUG_CONTROL_REQUIRED' }))); + assert.deepEqual( + decode(result.replies).map(message => ({ type: message.type, id: message.id, error: message.err.message })), + [10, 11, 12, 13].map(id => ({ type: 4, id, error: 'DEBUG_CONTROL_REQUIRED' })), + ); }); test('denied plugin DAP reaches real frontend RPCProtocol and ConnectionMain as a failed response', async () => { @@ -89,8 +122,14 @@ test('denied plugin DAP reaches real frontend RPCProtocol and ConnectionMain as frontendChannel.onMessage(message => received.push(JSON.parse(message))); let previousSequence = 0; try { - for (const [seq, command] of [[31, 'next'], [32, 'continue']]) { - const pending = proxy.$sendMessage('session-real', JSON.stringify({ type: 'request', seq, command, arguments: { threadId: 1 } })); + for (const [seq, command] of [ + [31, 'next'], + [32, 'continue'], + ]) { + const pending = proxy.$sendMessage( + 'session-real', + JSON.stringify({ type: 'request', seq, command, arguments: { threadId: 1 } }), + ); const rejected = assert.rejects(pending, /DEBUG_CONTROL_REQUIRED/); await new Promise(resolve => setImmediate(resolve)); const result = filterPluginRpc(wire.sent.at(-1), deny); @@ -106,13 +145,22 @@ test('denied plugin DAP reaches real frontend RPCProtocol and ConnectionMain as const response = received.at(-1); assert.ok(Number.isSafeInteger(response.seq) && response.seq > previousSequence); previousSequence = response.seq; - assert.deepEqual({ ...response, seq: undefined }, { - seq: undefined, type: 'response', request_seq: seq, command, success: false, - message: 'WishForge debug controller lease required', - }); + assert.deepEqual( + { ...response, seq: undefined }, + { + seq: undefined, + type: 'response', + request_seq: seq, + command, + success: false, + message: 'WishForge debug controller lease required', + }, + ); } assert.equal(received.length, 2); - } finally { protocol.dispose(); } + } finally { + protocol.dispose(); + } }); test('denied DAP carried as an RPC notification receives only a DAP failure notification', () => { @@ -129,7 +177,9 @@ test('denied DAP carried as an RPC notification receives only a DAP failure noti test('malformed or nonrequest denied plugin DAP never fabricates a DAP response', () => { const cases = [ - ['session', 'bad-json'], ['session', 'null'], ['session', '[]'], + ['session', 'bad-json'], + ['session', 'null'], + ['session', '[]'], ['session', '{"type":"event","seq":1,"command":"next"}'], ['session', '{"type":"request","seq":"1","command":"next"}'], ['session', '{"type":"request","seq":-1,"command":"next"}'], @@ -154,7 +204,10 @@ test('ordinary RPC forwards byte-for-byte while denied notifications do not fabr const ordinary = rpc('$acceptModelChanged', 'EditorsAndDocumentsExt', 1, 2); const reply = new Uint8ArrayWriteBuffer(); encoder.replyOK(reply, 8, { success: true }); - const result = filterPluginRpc(batch(ordinary, rpc('$sendMessage', 'ConnectionExt', 2, 2), reply.getCurrentContents()), deny); + const result = filterPluginRpc( + batch(ordinary, rpc('$sendMessage', 'ConnectionExt', 2, 2), reply.getCurrentContents()), + deny, + ); assert.deepEqual(result.forwarded, batch(ordinary, reply.getCurrentContents())); assert.equal(result.replies, null); assert.equal(result.denied, 1); @@ -163,17 +216,25 @@ test('ordinary RPC forwards byte-for-byte while denied notifications do not fabr test('controller forwarding preserves actual BatchingChannel bytes and rechecks authorization for every command', () => { const wire = memoryChannel(); const batching = new BatchingChannel(wire); - const first = batching.getWriteBuffer(); encoder.request(first, 1, '$createDebugSession', ['DebugExt']); first.commit(); - const second = batching.getWriteBuffer(); encoder.request(second, 2, '$sendMessage', ['ConnectionExt', 'session', '{}']); second.commit(); + const first = batching.getWriteBuffer(); + encoder.request(first, 1, '$createDebugSession', ['DebugExt']); + first.commit(); + const second = batching.getWriteBuffer(); + encoder.request(second, 2, '$sendMessage', ['ConnectionExt', 'session', '{}']); + second.commit(); batching.sendAccumulated(); const payload = wire.sent[0]; let checks = 0; - const allowed = filterPluginRpc(payload, () => { checks++; }); + const allowed = filterPluginRpc(payload, () => { + checks++; + }); assert.deepEqual(allowed.forwarded, payload); assert.equal(allowed.replies, null); assert.equal(checks, 2); checks = 0; - const revokedMidBatch = filterPluginRpc(payload, () => { if (++checks === 2) deny(); }); + const revokedMidBatch = filterPluginRpc(payload, () => { + if (++checks === 2) deny(); + }); assert.equal(decode(revokedMidBatch.forwarded).length, 1); assert.equal(decode(revokedMidBatch.replies)[0].id, 2); batching.close(); @@ -181,12 +242,30 @@ test('controller forwarding preserves actual BatchingChannel bytes and rechecks test('malformed, truncated, oversized or trailing frames fail atomically before any forwarding', () => { const good = batch(rpc('$createDebugSession', 'DebugExt')); - const excessive = new Uint8ArrayWriteBuffer(); excessive.writeLength(MAX_BATCH_MESSAGES + 1); - const invalidInner = new Uint8ArrayWriteBuffer(); invalidInner.writeLength(100); invalidInner.writeRaw(new Uint8Array([1])); - const wrongShape = new Uint8ArrayWriteBuffer(); encoder.encode(wrongShape, { type: 1, id: 3, method: '$sendMessage', args: { 0: 'ConnectionExt' } }); - for (const bytes of [new Uint8Array(), good.slice(0, -1), new Uint8Array([...good, 0]), excessive.getCurrentContents(), batch(invalidInner.getCurrentContents()), batch(wrongShape.getCurrentContents()), new Uint8Array([129, 129, 129, 129, 129, 0])]) { + const excessive = new Uint8ArrayWriteBuffer(); + excessive.writeLength(MAX_BATCH_MESSAGES + 1); + const invalidInner = new Uint8ArrayWriteBuffer(); + invalidInner.writeLength(100); + invalidInner.writeRaw(new Uint8Array([1])); + const wrongShape = new Uint8ArrayWriteBuffer(); + encoder.encode(wrongShape, { type: 1, id: 3, method: '$sendMessage', args: { 0: 'ConnectionExt' } }); + for (const bytes of [ + new Uint8Array(), + good.slice(0, -1), + new Uint8Array([...good, 0]), + excessive.getCurrentContents(), + batch(invalidInner.getCurrentContents()), + batch(wrongShape.getCurrentContents()), + new Uint8Array([129, 129, 129, 129, 129, 0]), + ]) { let invoked = false; - assert.throws(() => filterPluginRpc(bytes, () => { invoked = true; }), /PLUGIN_RPC|RPC_BATCH_LIMIT/); + assert.throws( + () => + filterPluginRpc(bytes, () => { + invoked = true; + }), + /PLUGIN_RPC|RPC_BATCH_LIMIT/, + ); assert.equal(invoked, false); } // Prevent reading into extra bytes from a websocket buffer outside the actual view. @@ -197,20 +276,25 @@ test('malformed, truncated, oversized or trailing frames fail atomically before test('guard cannot accidentally authorize on a false or asynchronous decision', () => { const payload = batch(rpc('$sendMessage', 'ConnectionExt')); - for (const callback of [() => false, () => Promise.resolve(true)]) assert.equal(filterPluginRpc(payload, callback).forwarded, null); + for (const callback of [() => false, () => Promise.resolve(true)]) + assert.equal(filterPluginRpc(payload, callback).forwarded, null); assert.throws(() => filterPluginRpc(payload), /DEBUG_GUARD_REQUIRED/); }); test('real coordinator lease owner, renewal, expiry and revocation govern plugin forwarding', t => { const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'wishforge-debug-guard-')); - const workspaceRoot = path.join(directory, 'source'); fs.mkdirSync(workspaceRoot); + const workspaceRoot = path.join(directory, 'source'); + fs.mkdirSync(workspaceRoot); let now = 1000; const coordinator = new Coordinator({ workspaceRoot, stateDirectory: path.join(directory, 'state'), now: () => now }); t.after(() => fs.rmSync(directory, { recursive: true, force: true })); const alice = coordinator.debugAcquire('alice'); const payload = batch(rpc('$sendMessage', 'ConnectionExt')); assert.equal(filterPluginRpc(payload, () => coordinator.assertDebugController('bob', alice.token)).forwarded, null); - assert.equal(filterPluginRpc(payload, () => coordinator.assertDebugController('alice', 'another-tab-token')).forwarded, null); + assert.equal( + filterPluginRpc(payload, () => coordinator.assertDebugController('alice', 'another-tab-token')).forwarded, + null, + ); const permitted = () => coordinator.assertDebugController('alice', alice.token); assert.deepEqual(filterPluginRpc(payload, permitted).forwarded, payload); now += 10_000; @@ -220,7 +304,10 @@ test('real coordinator lease owner, renewal, expiry and revocation govern plugin now += 10_000; assert.equal(filterPluginRpc(payload, permitted).forwarded, null); const next = coordinator.debugAcquire('alice'); - assert.deepEqual(filterPluginRpc(payload, () => coordinator.assertDebugController('alice', next.token)).forwarded, payload); + assert.deepEqual( + filterPluginRpc(payload, () => coordinator.assertDebugController('alice', next.token)).forwarded, + payload, + ); coordinator.revoke('alice'); assert.equal(filterPluginRpc(payload, () => coordinator.assertDebugController('alice', next.token)).forwarded, null); }); @@ -229,19 +316,32 @@ test('native DAP wrapper checks the live lease before adapter callback and sends const wire = memoryChannel(); const raw = new ForwardingDebugChannel(wire); let allowed = false; - const guarded = guardDebugChannel(raw, () => { if (!allowed) deny(); }); + const guarded = guardDebugChannel(raw, () => { + if (!allowed) deny(); + }); const received = []; guarded.onMessage(message => received.push(JSON.parse(message))); const incoming = (seq, command) => { - const writer = new Uint8ArrayWriteBuffer(); writer.writeString(JSON.stringify({ type: 'request', seq, command })); wire.receive(writer.getCurrentContents()); + const writer = new Uint8ArrayWriteBuffer(); + writer.writeString(JSON.stringify({ type: 'request', seq, command })); + wire.receive(writer.getCurrentContents()); }; - incoming(1, 'launch'); incoming(2, 'continue'); + incoming(1, 'launch'); + incoming(2, 'continue'); assert.deepEqual(received, []); const errors = wire.sent.map(bytes => JSON.parse(new Uint8ArrayReadBuffer(bytes).readString())); - assert.deepEqual(errors.map(error => [error.seq, error.request_seq, error.command, error.success]), [[1, 1, 'launch', false], [2, 2, 'continue', false]]); - allowed = true; incoming(3, 'next'); + assert.deepEqual( + errors.map(error => [error.seq, error.request_seq, error.command, error.success]), + [ + [1, 1, 'launch', false], + [2, 2, 'continue', false], + ], + ); + allowed = true; + incoming(3, 'next'); assert.equal(received[0].command, 'next'); - allowed = false; incoming(4, 'disconnect'); + allowed = false; + incoming(4, 'disconnect'); assert.equal(received.length, 1); guarded.send(JSON.stringify({ type: 'event', event: 'stopped' })); assert.equal(JSON.parse(new Uint8ArrayReadBuffer(wire.sent.at(-1)).readString()).event, 'stopped'); @@ -254,8 +354,12 @@ test('unauthorized malformed native DAP closes channel without reaching adapter' const wire = memoryChannel(); const guarded = guardDebugChannel(new ForwardingDebugChannel(wire), deny); let reached = false; - guarded.onMessage(() => { reached = true; }); - const writer = new Uint8ArrayWriteBuffer(); writer.writeString(message); wire.receive(writer.getCurrentContents()); + guarded.onMessage(() => { + reached = true; + }); + const writer = new Uint8ArrayWriteBuffer(); + writer.writeString(message); + wire.receive(writer.getCurrentContents()); assert.equal(reached, false); assert.equal(wire.closed, true); assert.equal(wire.sent.length, 0); diff --git a/apps/ide/policy/test/drafts.test.cjs b/apps/ide/policy/test/drafts.test.cjs index 81d4aed..531dbd7 100644 --- a/apps/ide/policy/test/drafts.test.cjs +++ b/apps/ide/policy/test/drafts.test.cjs @@ -1,6 +1,12 @@ -const test=require('node:test');const assert=require('node:assert/strict');const {DraftStore}=require('../drafts.cjs'); -test('draft quota rejects oversized ASCII and multibyte content before touching IndexedDB',()=>{ - const store=new DraftStore({open(){throw new Error('storage should not be opened');}}); - assert.throws(()=>store.put({text:'a'.repeat(2*1024*1024+1),baseText:''}),/2 MiB/); - assert.throws(()=>store.put({text:'中'.repeat(800000),baseText:''}),/2 MiB/); +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { DraftStore } = require('../drafts.cjs'); +test('draft quota rejects oversized ASCII and multibyte content before touching IndexedDB', () => { + const store = new DraftStore({ + open() { + throw new Error('storage should not be opened'); + }, + }); + assert.throws(() => store.put({ text: 'a'.repeat(2 * 1024 * 1024 + 1), baseText: '' }), /2 MiB/); + assert.throws(() => store.put({ text: '中'.repeat(800000), baseText: '' }), /2 MiB/); }); diff --git a/apps/ide/policy/test/lifecycle-safety.test.cjs b/apps/ide/policy/test/lifecycle-safety.test.cjs index 1b8082c..024d89f 100644 --- a/apps/ide/policy/test/lifecycle-safety.test.cjs +++ b/apps/ide/policy/test/lifecycle-safety.test.cjs @@ -1,4 +1,54 @@ -'use strict';const test=require('node:test');const assert=require('node:assert/strict');const {sleepReceipt}=require('../lifecycle-safety.cjs'); -const hash='a'.repeat(64);function fixture(){return{coordinator:{inputState:()=>({activeClients:0,dirty:false,currentDiskDigest:hash,latestSnapshot:{digest:hash,revision:1}}),state:()=>({debugController:null})},processManager:{processes:new Map()},debugManager:{getAll:()=>[].values()},snapshotPending:false,hostedDebugTouched:false};} -test('quiescent saved workspace reports complete snapshots and no active processes',()=>{assert.deepEqual(sleepReceipt(fixture()),{known:true,activeClients:0,terminals:0,debugSessions:0,dirty:false,snapshotComplete:true,snapshotRevision:1,diskDigest:hash});}); -test('unknown/debug/plugin activity, active terminals and incomplete snapshots never look quiescent',()=>{const f=fixture();f.processManager.processes.set(1,{});assert.equal(sleepReceipt(f).terminals,1);f.hostedDebugTouched=true;assert.equal(sleepReceipt(f).known,false);f.hostedDebugTouched=false;f.debugManager.getAll=()=>[{}].values();assert.equal(sleepReceipt(f).debugSessions,1);f.coordinator.inputState=()=>({activeClients:1,dirty:true,currentDiskDigest:hash,latestSnapshot:{digest:'b'.repeat(64),revision:1}});assert.equal(sleepReceipt(f).snapshotComplete,false);assert.equal(sleepReceipt(f).activeClients,1);assert.equal(sleepReceipt({...f,processManager:{}}).known,false);assert.equal(sleepReceipt({...f,snapshotPending:true}).known,false);}); +'use strict'; +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { sleepReceipt } = require('../lifecycle-safety.cjs'); +const hash = 'a'.repeat(64); +function fixture() { + return { + coordinator: { + inputState: () => ({ + activeClients: 0, + dirty: false, + currentDiskDigest: hash, + latestSnapshot: { digest: hash, revision: 1 }, + }), + state: () => ({ debugController: null }), + }, + processManager: { processes: new Map() }, + debugManager: { getAll: () => [].values() }, + snapshotPending: false, + hostedDebugTouched: false, + }; +} +test('quiescent saved workspace reports complete snapshots and no active processes', () => { + assert.deepEqual(sleepReceipt(fixture()), { + known: true, + activeClients: 0, + terminals: 0, + debugSessions: 0, + dirty: false, + snapshotComplete: true, + snapshotRevision: 1, + diskDigest: hash, + }); +}); +test('unknown/debug/plugin activity, active terminals and incomplete snapshots never look quiescent', () => { + const f = fixture(); + f.processManager.processes.set(1, {}); + assert.equal(sleepReceipt(f).terminals, 1); + f.hostedDebugTouched = true; + assert.equal(sleepReceipt(f).known, false); + f.hostedDebugTouched = false; + f.debugManager.getAll = () => [{}].values(); + assert.equal(sleepReceipt(f).debugSessions, 1); + f.coordinator.inputState = () => ({ + activeClients: 1, + dirty: true, + currentDiskDigest: hash, + latestSnapshot: { digest: 'b'.repeat(64), revision: 1 }, + }); + assert.equal(sleepReceipt(f).snapshotComplete, false); + assert.equal(sleepReceipt(f).activeClients, 1); + assert.equal(sleepReceipt({ ...f, processManager: {} }).known, false); + assert.equal(sleepReceipt({ ...f, snapshotPending: true }).known, false); +}); diff --git a/apps/ide/policy/test/plugin-install-policy.test.cjs b/apps/ide/policy/test/plugin-install-policy.test.cjs index df1bd25..a1b6ca8 100644 --- a/apps/ide/policy/test/plugin-install-policy.test.cjs +++ b/apps/ide/policy/test/plugin-install-policy.test.cjs @@ -13,42 +13,74 @@ function fixture(t) { t.after(() => fs.rmSync(root, { recursive: true, force: true })); const directory = path.join(root, 'test.approved'); fs.mkdirSync(path.join(directory, 'extension'), { recursive: true }); - fs.writeFileSync(path.join(directory, 'extension/package.json'), JSON.stringify({ publisher: 'test', name: 'approved', version: '1.0.0' })); + fs.writeFileSync( + path.join(directory, 'extension/package.json'), + JSON.stringify({ publisher: 'test', name: 'approved', version: '1.0.0' }), + ); fs.writeFileSync(path.join(directory, 'extension/main.js'), 'exports.activate = () => {};\n'); - const catalog = { plugins: [{ directory: 'test.approved', id: 'test.approved', version: '1.0.0', sha256: treeDigest(directory) }] }; + const catalog = { + plugins: [{ directory: 'test.approved', id: 'test.approved', version: '1.0.0', sha256: treeDigest(directory) }], + }; return { root, directory, options: { catalog, pythonLock: null } }; } test('approved ID/version/content loads while unrelated hidden cache is ignored', t => { - const f = fixture(t); fs.mkdirSync(path.join(f.root, '.downloads')); + const f = fixture(t); + fs.mkdirSync(path.join(f.root, '.downloads')); assert.equal(validateManagedPlugins(f.root, f.options).length, 1); }); test('same ID and version with changed implementation fails digest validation', t => { - const f = fixture(t); fs.appendFileSync(path.join(f.directory, 'extension/main.js'), 'malicious();'); + const f = fixture(t); + fs.appendFileSync(path.join(f.directory, 'extension/main.js'), 'malicious();'); assert.throws(() => validateManagedPlugins(f.root, f.options), /DIGEST_REJECTED/); }); test('changed version and additional plugin are refused', t => { const f = fixture(t); - fs.writeFileSync(path.join(f.directory, 'extension/package.json'), JSON.stringify({ publisher: 'test', name: 'approved', version: '2.0.0' })); + fs.writeFileSync( + path.join(f.directory, 'extension/package.json'), + JSON.stringify({ publisher: 'test', name: 'approved', version: '2.0.0' }), + ); assert.throws(() => validateManagedPlugins(f.root, f.options), /ID_VERSION_REJECTED/); fs.mkdirSync(path.join(f.root, 'unapproved.plugin')); assert.throws(() => validateManagedPlugins(f.root, f.options), /ALLOWLIST_MISMATCH/); }); test('symlinks cannot substitute plugin code or plugin directories', t => { - const f = fixture(t); fs.symlinkSync('/tmp', path.join(f.directory, 'escape')); + const f = fixture(t); + fs.symlinkSync('/tmp', path.join(f.directory, 'escape')); assert.throws(() => validateManagedPlugins(f.root, f.options), /SYMLINK_REJECTED/); }); test('actual Theia PluginServer install routes reject registry, VSIX, update and forged system type before calling deployer', async () => { const server = new ManagedPluginServer(); - server.pluginDeployer = new Proxy({}, { get() { throw new Error('Unsafe deployer reached'); } }); - for (const id of ['vscode:untrusted.extension', 'file:///tmp/plugin.vsix', 'https://example.invalid/plugin.vsix', 'vscode:ms-python.debugpy@2026.6.0']) { + server.pluginDeployer = new Proxy( + {}, + { + get() { + throw new Error('Unsafe deployer reached'); + }, + }, + ); + for (const id of [ + 'vscode:untrusted.extension', + 'file:///tmp/plugin.vsix', + 'https://example.invalid/plugin.vsix', + 'vscode:ms-python.debugpy@2026.6.0', + ]) { await assert.rejects(server.install(id, 0, { ignoreDependencies: true }), /EXTENSIONS_MANAGED_BY_ADMIN/); } - for (const method of ['doInstall', 'uninstall', 'enablePlugin', 'disablePlugin']) await assert.rejects(server[method]('test.approved'), /EXTENSIONS_MANAGED_BY_ADMIN/); + for (const method of ['doInstall', 'uninstall', 'enablePlugin', 'disablePlugin']) + await assert.rejects(server[method]('test.approved'), /EXTENSIONS_MANAGED_BY_ADMIN/); }); test('direct deployer mutation paths and bulk/update paths are closed', async () => { const deployer = new ManagedPluginDeployer(); - for (const method of ['deploy', 'deployMultipleEntries', 'deployPlugins', 'uninstall', 'undeploy', 'enablePlugin', 'disablePlugin']) { + for (const method of [ + 'deploy', + 'deployMultipleEntries', + 'deployPlugins', + 'uninstall', + 'undeploy', + 'enablePlugin', + 'disablePlugin', + ]) { await assert.rejects(deployer[method]({ id: 'local-dir:/tmp/evil', type: 0 }), /EXTENSIONS_MANAGED_BY_ADMIN/); } }); diff --git a/apps/ide/provisioner/main.mjs b/apps/ide/provisioner/main.mjs index 3b49ddb..e864a0b 100644 --- a/apps/ide/provisioner/main.mjs +++ b/apps/ide/provisioner/main.mjs @@ -3,32 +3,90 @@ import { timingSafeEqual } from 'node:crypto'; import { readConfig, provisionRun } from './provisioner.mjs'; import { provisionWorkspace } from './workspaces.mjs'; import { releaseRun } from './run-release.mjs'; -import {observeWorkspaceLifecycle,reconcileWorkspaceLifecycle} from './workspace-lifecycle.mjs'; +import { observeWorkspaceLifecycle, reconcileWorkspaceLifecycle } from './workspace-lifecycle.mjs'; import { createRunOperationQueue } from './run-operations.mjs'; const config = readConfig(); const expected = process.env.WF_PROVISIONER_TOKEN; const runOperation = createRunOperationQueue(); -if (!expected || expected.length < 32) throw new Error('unconfigured: scoped WF_PROVISIONER_TOKEN must be at least 32 characters'); +if (!expected || expected.length < 32) + throw new Error('unconfigured: scoped WF_PROVISIONER_TOKEN must be at least 32 characters'); const server = http.createServer(async (req, res) => { - res.setHeader('content-type', 'application/json'); res.setHeader('cache-control', 'no-store'); + res.setHeader('content-type', 'application/json'); + res.setHeader('cache-control', 'no-store'); try { const token = req.headers.authorization?.replace(/^Bearer /, '') ?? ''; - if (token.length !== expected.length || !timingSafeEqual(Buffer.from(token), Buffer.from(expected))) { res.writeHead(401); res.end(JSON.stringify({ error: 'UNAUTHORIZED' })); return; } - if (req.url === '/healthz' && req.method === 'GET') { res.end(JSON.stringify({ status: 'configured', tenantId: config.tenantId })); return; } - const target=new URL(req.url,'http://provisioner.local'); - const lifecycle=/^\/workspaces\/([a-zA-Z0-9_-]{1,128})\/lifecycle$/.exec(target.pathname); - if(lifecycle&&req.method==='GET'){const input={tenantId:target.searchParams.get('tenantId'),projectId:target.searchParams.get('projectId'),workspaceId:lifecycle[1]};res.end(JSON.stringify(await observeWorkspaceLifecycle(config,input)));return;} - if(lifecycle&&req.method==='PUT'){let body='';for await(const part of req){body+=part;if(body.length>4000)throw new Error('INVALID_REQUEST');}const input=JSON.parse(body);if(input.workspaceId!==lifecycle[1])throw new Error('INVALID_WORKSPACE_SCOPE');const state=await reconcileWorkspaceLifecycle(config,input);res.writeHead(202);res.end(JSON.stringify(state));return;} - if (!['/runs', '/runs/release', '/workspaces'].includes(req.url) || req.method !== 'POST') { res.writeHead(404); res.end(JSON.stringify({ error: 'NOT_FOUND' })); return; } - let body = ''; for await (const part of req) { body += part; if (body.length > 40000) throw new Error('INVALID_REQUEST'); } + if (token.length !== expected.length || !timingSafeEqual(Buffer.from(token), Buffer.from(expected))) { + res.writeHead(401); + res.end(JSON.stringify({ error: 'UNAUTHORIZED' })); + return; + } + if (req.url === '/healthz' && req.method === 'GET') { + res.end(JSON.stringify({ status: 'configured', tenantId: config.tenantId })); + return; + } + const target = new URL(req.url, 'http://provisioner.local'); + const lifecycle = /^\/workspaces\/([a-zA-Z0-9_-]{1,128})\/lifecycle$/.exec(target.pathname); + if (lifecycle && req.method === 'GET') { + const input = { + tenantId: target.searchParams.get('tenantId'), + projectId: target.searchParams.get('projectId'), + workspaceId: lifecycle[1], + }; + res.end(JSON.stringify(await observeWorkspaceLifecycle(config, input))); + return; + } + if (lifecycle && req.method === 'PUT') { + let body = ''; + for await (const part of req) { + body += part; + if (body.length > 4000) throw new Error('INVALID_REQUEST'); + } + const input = JSON.parse(body); + if (input.workspaceId !== lifecycle[1]) throw new Error('INVALID_WORKSPACE_SCOPE'); + const state = await reconcileWorkspaceLifecycle(config, input); + res.writeHead(202); + res.end(JSON.stringify(state)); + return; + } + if (!['/runs', '/runs/release', '/workspaces'].includes(req.url) || req.method !== 'POST') { + res.writeHead(404); + res.end(JSON.stringify({ error: 'NOT_FOUND' })); + return; + } + let body = ''; + for await (const part of req) { + body += part; + if (body.length > 40000) throw new Error('INVALID_REQUEST'); + } const input = JSON.parse(body); - const result = req.url === '/workspaces' ? await provisionWorkspace(config, input) : await runOperation(input.tenantId, input.runId, () => req.url === '/runs/release' ? releaseRun(config, input) : provisionRun(config, input)); - res.writeHead(202); res.end(JSON.stringify(result)); + const result = + req.url === '/workspaces' + ? await provisionWorkspace(config, input) + : await runOperation(input.tenantId, input.runId, () => + req.url === '/runs/release' ? releaseRun(config, input) : provisionRun(config, input), + ); + res.writeHead(202); + res.end(JSON.stringify(result)); } catch (error) { const infrastructure = error.message.startsWith('KUBERNETES_'); res.writeHead(infrastructure ? 502 : 400); - res.end(JSON.stringify({ error: infrastructure || ['RESOURCE_GRANT_INVALID','RESOURCE_GRANT_EXPIRED','RESOURCE_LIMIT','RUNTIME_IMAGE_MIGRATION_REQUIRED'].includes(error.message) ? error.message : 'INVALID_RUN_REQUEST' })); + res.end( + JSON.stringify({ + error: + infrastructure || + [ + 'RESOURCE_GRANT_INVALID', + 'RESOURCE_GRANT_EXPIRED', + 'RESOURCE_LIMIT', + 'RUNTIME_IMAGE_MIGRATION_REQUIRED', + ].includes(error.message) + ? error.message + : 'INVALID_RUN_REQUEST', + }), + ); } }); -server.listen(Number(process.env.WF_PROVISIONER_PORT ?? 4450), process.env.WF_PROVISIONER_HOST ?? '127.0.0.1', () => console.log('Run provisioner configured; no workloads are launched until an authenticated request arrives.')); +server.listen(Number(process.env.WF_PROVISIONER_PORT ?? 4450), process.env.WF_PROVISIONER_HOST ?? '127.0.0.1', () => + console.log('Run provisioner configured; no workloads are launched until an authenticated request arrives.'), +); diff --git a/apps/ide/provisioner/package.json b/apps/ide/provisioner/package.json index 5290573..53eefe3 100644 --- a/apps/ide/provisioner/package.json +++ b/apps/ide/provisioner/package.json @@ -3,5 +3,8 @@ "version": "0.1.0", "private": true, "type": "module", - "scripts": { "start": "node main.mjs", "test": "node --test test/*.test.mjs" } + "scripts": { + "start": "node main.mjs", + "test": "node --test test/*.test.mjs" + } } diff --git a/apps/ide/provisioner/provisioner.mjs b/apps/ide/provisioner/provisioner.mjs index 205d6e3..f28918f 100644 --- a/apps/ide/provisioner/provisioner.mjs +++ b/apps/ide/provisioner/provisioner.mjs @@ -4,7 +4,10 @@ import { verifyResourceGrant } from './resource-grants.mjs'; const safe = /^[a-zA-Z0-9_-]{1,128}$/; const dns = /^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/; -function requireValue(value, name) { if (!value) throw new Error(`unconfigured: ${name} is required`); return value; } +function requireValue(value, name) { + if (!value) throw new Error(`unconfigured: ${name} is required`); + return value; +} export function readConfig(env = process.env) { const config = { tenantId: requireValue(env.WF_TENANT_ID, 'WF_TENANT_ID'), @@ -21,18 +24,50 @@ export function readConfig(env = process.env) { activeDeadlineSeconds: Number(env.WF_RUN_MAX_SECONDS ?? 3600), verifierDockerSecret: env.WF_VERIFIER_DOCKER_CONFIG_SECRET, resourceGrantSecret: requireValue(env.WF_RESOURCE_GRANT_SECRET, 'WF_RESOURCE_GRANT_SECRET'), - cpuMilliCap: Number(env.WF_RUN_CPU_MILLI_CAP ?? 2000), memoryMiBCap: Number(env.WF_RUN_MEMORY_MIB_CAP ?? 4096), - previousImages: (env.WF_RUN_PREVIOUS_IMAGES ?? '').split(',').map(value=>value.trim()).filter(Boolean), + cpuMilliCap: Number(env.WF_RUN_CPU_MILLI_CAP ?? 2000), + memoryMiBCap: Number(env.WF_RUN_MEMORY_MIB_CAP ?? 4096), + previousImages: (env.WF_RUN_PREVIOUS_IMAGES ?? '') + .split(',') + .map(value => value.trim()) + .filter(Boolean), }; - if (!safe.test(config.tenantId) || [config.namespace, config.controlNamespace, config.runtimeClass, config.storageClass, ...(config.verifierDockerSecret ? [config.verifierDockerSecret] : [])].some(value => !dns.test(value) || value.length > 63)) throw new Error('Invalid configured tenant/namespace/runtime class/storage/secret'); - if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(config.image)) throw new Error('WF_RUN_WORKER_IMAGE must be an immutable sha256 image digest'); - if (config.resourceGrantSecret.length < 32 || !Number.isSafeInteger(config.cpuMilliCap) || config.cpuMilliCap < 100 || !Number.isSafeInteger(config.memoryMiBCap) || config.memoryMiBCap < 512 || config.previousImages.length > 10 || config.previousImages.some(image=>!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(image))) throw new Error('Invalid resource grant signing key, static caps or previous runtime image allowlist'); + if ( + !safe.test(config.tenantId) || + [ + config.namespace, + config.controlNamespace, + config.runtimeClass, + config.storageClass, + ...(config.verifierDockerSecret ? [config.verifierDockerSecret] : []), + ].some(value => !dns.test(value) || value.length > 63) + ) + throw new Error('Invalid configured tenant/namespace/runtime class/storage/secret'); + if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(config.image)) + throw new Error('WF_RUN_WORKER_IMAGE must be an immutable sha256 image digest'); + if ( + config.resourceGrantSecret.length < 32 || + !Number.isSafeInteger(config.cpuMilliCap) || + config.cpuMilliCap < 100 || + !Number.isSafeInteger(config.memoryMiBCap) || + config.memoryMiBCap < 512 || + config.previousImages.length > 10 || + config.previousImages.some(image => !/^[^\s]+@sha256:[a-f0-9]{64}$/.test(image)) + ) + throw new Error('Invalid resource grant signing key, static caps or previous runtime image allowlist'); if (!/^\d+Gi$/.test(config.storageSize)) throw new Error('Invalid run storage size'); - if (!Number.isInteger(config.activeDeadlineSeconds) || config.activeDeadlineSeconds < 60 || config.activeDeadlineSeconds > 10800) throw new Error('Run deadline must be 60..10800 seconds and shorter than the scoped token lifetime'); + if ( + !Number.isInteger(config.activeDeadlineSeconds) || + config.activeDeadlineSeconds < 60 || + config.activeDeadlineSeconds > 10800 + ) + throw new Error('Run deadline must be 60..10800 seconds and shorter than the scoped token lifetime'); const api = new URL(config.controlPlaneUrl); - if (api.protocol !== 'https:' || api.username || api.password || api.search || api.hash) throw new Error('Production control plane must use an HTTPS URL without embedded credentials'); - if (!/^[a-zA-Z0-9.-]+:\d+$/.test(config.temporalAddress)) throw new Error('TEMPORAL_ADDRESS must explicitly include host:port'); - if (Number(config.temporalAddress.split(':').at(-1)) < 1 || Number(config.temporalAddress.split(':').at(-1)) > 65535) throw new Error('Invalid Temporal port'); + if (api.protocol !== 'https:' || api.username || api.password || api.search || api.hash) + throw new Error('Production control plane must use an HTTPS URL without embedded credentials'); + if (!/^[a-zA-Z0-9.-]+:\d+$/.test(config.temporalAddress)) + throw new Error('TEMPORAL_ADDRESS must explicitly include host:port'); + if (Number(config.temporalAddress.split(':').at(-1)) < 1 || Number(config.temporalAddress.split(':').at(-1)) > 65535) + throw new Error('Invalid Temporal port'); return config; } @@ -41,40 +76,96 @@ function assertToken(token, request, kind, deadline) { const parts = token.split('.'); if (parts.length !== 3 || token.length > 16384) throw new Error(); const claims = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8')); - if (claims.tenant_id !== request.tenantId || claims.run_id !== request.runId || claims.generation !== request.generation || claims.kind !== kind || claims.iss !== 'wishforge-control-plane' || claims.aud !== 'wishforge-internal' || !Number.isFinite(claims.exp) || claims.exp * 1000 <= Date.now() + deadline * 1000) throw new Error(); - } catch { throw new Error(`Invalid or insufficient-lifetime ${kind} run token`); } + if ( + claims.tenant_id !== request.tenantId || + claims.run_id !== request.runId || + claims.generation !== request.generation || + claims.kind !== kind || + claims.iss !== 'wishforge-control-plane' || + claims.aud !== 'wishforge-internal' || + !Number.isFinite(claims.exp) || + claims.exp * 1000 <= Date.now() + deadline * 1000 + ) + throw new Error(); + } catch { + throw new Error(`Invalid or insufficient-lifetime ${kind} run token`); + } // The authenticated control plane issues these tokens. This shape/scope check // is defense-in-depth, not signature verification; the API verifies signatures. } export function renderRunResources(config, request) { - if (!request || Object.keys(request).some(key => !['tenantId', 'runId', 'generation', 'executorToken', 'verifierToken', 'resourceGrant'].includes(key)) || request.tenantId !== config.tenantId || !safe.test(request.runId ?? '') || !safe.test(request.generation ?? '')) throw new Error('Run request is outside the configured tenant or has invalid fields'); + if ( + !request || + Object.keys(request).some( + key => !['tenantId', 'runId', 'generation', 'executorToken', 'verifierToken', 'resourceGrant'].includes(key), + ) || + request.tenantId !== config.tenantId || + !safe.test(request.runId ?? '') || + !safe.test(request.generation ?? '') + ) + throw new Error('Run request is outside the configured tenant or has invalid fields'); const resourceGrant = verifyResourceGrant(config, request); // One reservation covers both isolated roles; aggregate requested limits must // stay within the signed run grant, including their separate persistent PVCs. - const roleResources = { cpuMilli: Math.floor(resourceGrant.cpuMilli / 2), memoryMiB: Math.floor(resourceGrant.memoryMiB / 2), storageMiB: Math.floor(resourceGrant.storageMiB / 2) }; + const roleResources = { + cpuMilli: Math.floor(resourceGrant.cpuMilli / 2), + memoryMiB: Math.floor(resourceGrant.memoryMiB / 2), + storageMiB: Math.floor(resourceGrant.storageMiB / 2), + }; assertToken(request.executorToken, request, 'worker', config.activeDeadlineSeconds); assertToken(request.verifierToken, request, 'verifier', config.activeDeadlineSeconds); if (request.executorToken === request.verifierToken) throw new Error('Executor and verifier credentials must differ'); const runHash = createHash('sha256').update(`${request.tenantId}\0${request.runId}`).digest('hex').slice(0, 24); const generationHash = createHash('sha256').update(request.generation).digest('hex').slice(0, 24); - const suffix = createHash('sha256').update(`${request.tenantId}\0${request.runId}\0${request.generation}`).digest('hex').slice(0, 24); + const suffix = createHash('sha256') + .update(`${request.tenantId}\0${request.runId}\0${request.generation}`) + .digest('hex') + .slice(0, 24); const prefix = `wf-${suffix}`; - const labels = { 'app.kubernetes.io/managed-by': 'wishforge-provisioner', 'wishforge.io/run': runHash, 'wishforge.io/generation': generationHash, 'wishforge.io/component': 'runner' }; + const labels = { + 'app.kubernetes.io/managed-by': 'wishforge-provisioner', + 'wishforge.io/run': runHash, + 'wishforge.io/generation': generationHash, + 'wishforge.io/component': 'runner', + }; const metadata = name => ({ name, namespace: config.namespace, labels }); const claims = Object.fromEntries(['executor', 'verifier'].map(role => [role, `${prefix}-${role}-workspace`])); - const resources = Object.values(claims).map(claim => ({ apiVersion: 'v1', kind: 'PersistentVolumeClaim', metadata: metadata(claim), spec: { storageClassName: config.storageClass, accessModes: ['ReadWriteOnce'], resources: { requests: { storage: `${roleResources.storageMiB}Mi` } } } })); + const resources = Object.values(claims).map(claim => ({ + apiVersion: 'v1', + kind: 'PersistentVolumeClaim', + metadata: metadata(claim), + spec: { + storageClassName: config.storageClass, + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: `${roleResources.storageMiB}Mi` } }, + }, + })); for (const role of ['executor', 'verifier']) { const name = `${prefix}-${role}`; const secretName = `${name}-capability`; - resources.push({ apiVersion: 'v1', kind: 'Secret', metadata: metadata(secretName), type: 'Opaque', stringData: { token: request[role === 'executor' ? 'executorToken' : 'verifierToken'] } }); - resources.push({ apiVersion: 'v1', kind: 'ServiceAccount', metadata: metadata(name), automountServiceAccountToken: false }); + resources.push({ + apiVersion: 'v1', + kind: 'Secret', + metadata: metadata(secretName), + type: 'Opaque', + stringData: { token: request[role === 'executor' ? 'executorToken' : 'verifierToken'] }, + }); + resources.push({ + apiVersion: 'v1', + kind: 'ServiceAccount', + metadata: metadata(name), + automountServiceAccountToken: false, + }); const env = [ { name: 'WF_WORKER_ROLE', value: role }, { name: 'WF_SANDBOX_RUN_ID', value: request.runId }, { name: 'WF_SANDBOX_TENANT_ID', value: request.tenantId }, { name: 'WF_SANDBOX_GENERATION', value: request.generation }, - { name: role === 'executor' ? 'WF_WORKER_TOKEN' : 'WF_VERIFIER_TOKEN', valueFrom: { secretKeyRef: { name: secretName, key: 'token' } } }, + { + name: role === 'executor' ? 'WF_WORKER_TOKEN' : 'WF_VERIFIER_TOKEN', + valueFrom: { secretKeyRef: { name: secretName, key: 'token' } }, + }, { name: 'WF_WORKER_ID', value: name }, { name: 'WF_WORKSPACE_ROOT', value: '/workspaces' }, { name: 'WF_PROVIDER_HOME_ROOT', value: '/provider-home' }, @@ -87,8 +178,16 @@ export function renderRunResources(config, request) { { name: 'WF_EXECUTION_MEMORY_MIB_LIMIT', value: String(roleResources.memoryMiB) }, { name: 'WF_EXECUTION_STORAGE_MIB_LIMIT', value: String(roleResources.storageMiB) }, ]; - const mounts = [{ name: 'workspace', mountPath: '/workspaces' }, { name: 'provider-home', mountPath: '/provider-home' }, { name: 'temporary', mountPath: '/tmp' }]; - const volumes = [{ name: 'workspace', persistentVolumeClaim: { claimName: claims[role] } }, { name: 'provider-home', emptyDir: { medium: 'Memory', sizeLimit: '256Mi' } }, { name: 'temporary', emptyDir: { sizeLimit: '4Gi' } }]; + const mounts = [ + { name: 'workspace', mountPath: '/workspaces' }, + { name: 'provider-home', mountPath: '/provider-home' }, + { name: 'temporary', mountPath: '/tmp' }, + ]; + const volumes = [ + { name: 'workspace', persistentVolumeClaim: { claimName: claims[role] } }, + { name: 'provider-home', emptyDir: { medium: 'Memory', sizeLimit: '256Mi' } }, + { name: 'temporary', emptyDir: { sizeLimit: '4Gi' } }, + ]; if (role === 'verifier') { env.push({ name: 'WF_VERIFIER_BWRAP', value: '/usr/bin/bwrap' }); if (config.verifierDockerSecret) { @@ -97,78 +196,234 @@ export function renderRunResources(config, request) { volumes.push({ name: 'docker-config', secret: { secretName: config.verifierDockerSecret, defaultMode: 256 } }); } } - resources.push({ apiVersion: 'batch/v1', kind: 'Job', metadata: metadata(name), spec: { - backoffLimit: 0, activeDeadlineSeconds: config.activeDeadlineSeconds, ttlSecondsAfterFinished: 86400, - template: { metadata: { labels: { ...labels, 'wishforge.io/worker-role': role } }, spec: { - runtimeClassName: config.runtimeClass, serviceAccountName: name, automountServiceAccountToken: false, restartPolicy: 'Never', - securityContext: { runAsNonRoot: true, runAsUser: 1000, runAsGroup: 1000, fsGroup: 1000, seccompProfile: { type: 'RuntimeDefault' } }, - containers: [{ name: role, image: resourceGrant.image, imagePullPolicy: 'Always', command: ['npm', 'run', 'dev', '-w', '@wishforge/worker'], env, - securityContext: { allowPrivilegeEscalation: false, readOnlyRootFilesystem: true, capabilities: { drop: ['ALL'] } }, - resources: { requests: { cpu: `${Math.min(1000,roleResources.cpuMilli)}m`, memory: `${Math.min(2048,roleResources.memoryMiB)}Mi`, 'ephemeral-storage': '1Gi' }, limits: { cpu: `${roleResources.cpuMilli}m`, memory: `${roleResources.memoryMiB}Mi`, 'ephemeral-storage': '8Gi' } }, volumeMounts: mounts }], volumes, - } }, - } }); + resources.push({ + apiVersion: 'batch/v1', + kind: 'Job', + metadata: metadata(name), + spec: { + backoffLimit: 0, + activeDeadlineSeconds: config.activeDeadlineSeconds, + ttlSecondsAfterFinished: 86400, + template: { + metadata: { labels: { ...labels, 'wishforge.io/worker-role': role } }, + spec: { + runtimeClassName: config.runtimeClass, + serviceAccountName: name, + automountServiceAccountToken: false, + restartPolicy: 'Never', + securityContext: { + runAsNonRoot: true, + runAsUser: 1000, + runAsGroup: 1000, + fsGroup: 1000, + seccompProfile: { type: 'RuntimeDefault' }, + }, + containers: [ + { + name: role, + image: resourceGrant.image, + imagePullPolicy: 'Always', + command: ['npm', 'run', 'dev', '-w', '@wishforge/worker'], + env, + securityContext: { + allowPrivilegeEscalation: false, + readOnlyRootFilesystem: true, + capabilities: { drop: ['ALL'] }, + }, + resources: { + requests: { + cpu: `${Math.min(1000, roleResources.cpuMilli)}m`, + memory: `${Math.min(2048, roleResources.memoryMiB)}Mi`, + 'ephemeral-storage': '1Gi', + }, + limits: { + cpu: `${roleResources.cpuMilli}m`, + memory: `${roleResources.memoryMiB}Mi`, + 'ephemeral-storage': '8Gi', + }, + }, + volumeMounts: mounts, + }, + ], + volumes, + }, + }, + }, + }); } const nsSelector = { namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } } }; const apiPort = Number(new URL(config.controlPlaneUrl).port || 443); const temporalPort = Number(config.temporalAddress.split(':').at(-1)); - resources.push({ apiVersion: 'networking.k8s.io/v1', kind: 'NetworkPolicy', metadata: metadata(`${prefix}-isolation`), spec: { - podSelector: { matchLabels: { 'wishforge.io/run': runHash, 'wishforge.io/generation': generationHash } }, policyTypes: ['Ingress', 'Egress'], ingress: [], - egress: [ - { to: [{ namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' } }, podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } } }], ports: [{ protocol: 'UDP', port: 53 }, { protocol: 'TCP', port: 53 }] }, - { to: [{ ...nsSelector, podSelector: { matchLabels: { 'wishforge.io/component': 'api' } } }], ports: [{ protocol: 'TCP', port: apiPort }] }, - { to: [{ ...nsSelector, podSelector: { matchLabels: { 'wishforge.io/component': 'temporal' } } }], ports: [{ protocol: 'TCP', port: temporalPort }] }, - ], - } }); - return { resources, receipt: { tenantId: request.tenantId, runId: request.runId, generation: request.generation, namespace: config.namespace, executorJobName: `${prefix}-executor`, verifierJobName: `${prefix}-verifier`, workspaceClaim: claims.executor, verifierWorkspaceClaim: claims.verifier, status: 'provisioned' } }; + resources.push({ + apiVersion: 'networking.k8s.io/v1', + kind: 'NetworkPolicy', + metadata: metadata(`${prefix}-isolation`), + spec: { + podSelector: { matchLabels: { 'wishforge.io/run': runHash, 'wishforge.io/generation': generationHash } }, + policyTypes: ['Ingress', 'Egress'], + ingress: [], + egress: [ + { + to: [ + { + namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' } }, + podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } }, + }, + ], + ports: [ + { protocol: 'UDP', port: 53 }, + { protocol: 'TCP', port: 53 }, + ], + }, + { + to: [{ ...nsSelector, podSelector: { matchLabels: { 'wishforge.io/component': 'api' } } }], + ports: [{ protocol: 'TCP', port: apiPort }], + }, + { + to: [{ ...nsSelector, podSelector: { matchLabels: { 'wishforge.io/component': 'temporal' } } }], + ports: [{ protocol: 'TCP', port: temporalPort }], + }, + ], + }, + }); + return { + resources, + receipt: { + tenantId: request.tenantId, + runId: request.runId, + generation: request.generation, + namespace: config.namespace, + executorJobName: `${prefix}-executor`, + verifierJobName: `${prefix}-verifier`, + workspaceClaim: claims.executor, + verifierWorkspaceClaim: claims.verifier, + status: 'provisioned', + }, + }; } export function runKubectl(config, document) { return new Promise((resolve, reject) => { - const child = spawn('kubectl', ['--context', config.context, '--namespace', config.namespace, 'apply', '--server-side', '--field-manager=wishforge-provisioner', '-o', 'name', '-f', '-'], { stdio: ['pipe', 'pipe', 'pipe'] }); + const child = spawn( + 'kubectl', + [ + '--context', + config.context, + '--namespace', + config.namespace, + 'apply', + '--server-side', + '--field-manager=wishforge-provisioner', + '-o', + 'name', + '-f', + '-', + ], + { stdio: ['pipe', 'pipe', 'pipe'] }, + ); let output = ''; - const timeout = setTimeout(() => { child.kill('SIGTERM'); reject(new Error('KUBERNETES_APPLY_TIMEOUT')); }, 30000); - child.stdout.on('data', chunk => { output += chunk; if (output.length > 65536) child.kill('SIGTERM'); }); + const timeout = setTimeout(() => { + child.kill('SIGTERM'); + reject(new Error('KUBERNETES_APPLY_TIMEOUT')); + }, 30000); + child.stdout.on('data', chunk => { + output += chunk; + if (output.length > 65536) child.kill('SIGTERM'); + }); // Never expose kubectl diagnostics: API validation errors can contain Secret data. child.stderr.resume(); - child.on('error', () => { clearTimeout(timeout); reject(new Error('KUBERNETES_COMMAND_UNAVAILABLE')); }); - child.on('close', code => { clearTimeout(timeout); code === 0 ? resolve(output) : reject(new Error('KUBERNETES_APPLY_FAILED')); }); - child.stdin.on('error', () => {}); child.stdin.end(JSON.stringify({ apiVersion: 'v1', kind: 'List', items: document })); + child.on('error', () => { + clearTimeout(timeout); + reject(new Error('KUBERNETES_COMMAND_UNAVAILABLE')); + }); + child.on('close', code => { + clearTimeout(timeout); + code === 0 ? resolve(output) : reject(new Error('KUBERNETES_APPLY_FAILED')); + }); + child.stdin.on('error', () => {}); + child.stdin.end(JSON.stringify({ apiVersion: 'v1', kind: 'List', items: document })); }); } export async function lifecycleCommand(config, args) { return new Promise((resolve, reject) => { - const child = spawn('kubectl', ['--context', config.context, '--namespace', config.namespace, ...args], { stdio: ['ignore', 'pipe', 'pipe'] }); - let stdout=''; const timer=setTimeout(()=>{child.kill('SIGKILL');reject(new Error('KUBERNETES_RECONCILE_TIMEOUT'));},45000); - child.stdout.on('data', chunk=>{stdout+=chunk; if(stdout.length>1024*1024)child.kill('SIGKILL');});child.stderr.resume(); - child.on('error',()=>{clearTimeout(timer);reject(new Error('KUBERNETES_RECONCILE_FAILED'));}); - child.on('close',code=>{clearTimeout(timer);code===0?resolve(stdout):reject(new Error('KUBERNETES_RECONCILE_FAILED'));}); + const child = spawn('kubectl', ['--context', config.context, '--namespace', config.namespace, ...args], { + stdio: ['ignore', 'pipe', 'pipe'], + }); + let stdout = ''; + const timer = setTimeout(() => { + child.kill('SIGKILL'); + reject(new Error('KUBERNETES_RECONCILE_TIMEOUT')); + }, 45000); + child.stdout.on('data', chunk => { + stdout += chunk; + if (stdout.length > 1024 * 1024) child.kill('SIGKILL'); + }); + child.stderr.resume(); + child.on('error', () => { + clearTimeout(timer); + reject(new Error('KUBERNETES_RECONCILE_FAILED')); + }); + child.on('close', code => { + clearTimeout(timer); + code === 0 ? resolve(stdout) : reject(new Error('KUBERNETES_RECONCILE_FAILED')); + }); }); } export async function reconcileRunJobs(config, request, execute = lifecycleCommand) { - const { resources }=renderRunResources(config,request); - const labels=resources[0].metadata.labels; - const selector=`app.kubernetes.io/managed-by=wishforge-provisioner,wishforge.io/run=${labels['wishforge.io/run']},wishforge.io/component=runner`; - const listed=JSON.parse(await execute(config,['get','jobs','-l',selector,'-o','json'])); - if(!Array.isArray(listed.items))throw new Error('KUBERNETES_RECONCILE_FAILED'); - let terminal=false; - for(const job of listed.items){ - if(job.metadata?.namespace!==config.namespace || job.metadata?.labels?.['app.kubernetes.io/managed-by']!=='wishforge-provisioner' || job.metadata?.labels?.['wishforge.io/run']!==labels['wishforge.io/run'] || !/^wf-[a-f0-9]{24}-(executor|verifier)$/.test(job.metadata?.name??''))throw new Error('KUBERNETES_RECONCILE_SCOPE_MISMATCH'); - if(job.metadata.labels['wishforge.io/generation']===labels['wishforge.io/generation']){ - if(job.status?.failed>0 || job.status?.succeeded>0 || job.status?.conditions?.some(c=>['Failed','Complete'].includes(c.type)&&c.status==='True'))terminal=true; - } else await execute(config,['delete','job',job.metadata.name,'--cascade=foreground','--wait=true','--timeout=30s']); + const { resources } = renderRunResources(config, request); + const labels = resources[0].metadata.labels; + const selector = `app.kubernetes.io/managed-by=wishforge-provisioner,wishforge.io/run=${labels['wishforge.io/run']},wishforge.io/component=runner`; + const listed = JSON.parse(await execute(config, ['get', 'jobs', '-l', selector, '-o', 'json'])); + if (!Array.isArray(listed.items)) throw new Error('KUBERNETES_RECONCILE_FAILED'); + let terminal = false; + for (const job of listed.items) { + if ( + job.metadata?.namespace !== config.namespace || + job.metadata?.labels?.['app.kubernetes.io/managed-by'] !== 'wishforge-provisioner' || + job.metadata?.labels?.['wishforge.io/run'] !== labels['wishforge.io/run'] || + !/^wf-[a-f0-9]{24}-(executor|verifier)$/.test(job.metadata?.name ?? '') + ) + throw new Error('KUBERNETES_RECONCILE_SCOPE_MISMATCH'); + if (job.metadata.labels['wishforge.io/generation'] === labels['wishforge.io/generation']) { + if ( + job.status?.failed > 0 || + job.status?.succeeded > 0 || + job.status?.conditions?.some(c => ['Failed', 'Complete'].includes(c.type) && c.status === 'True') + ) + terminal = true; + } else + await execute(config, [ + 'delete', + 'job', + job.metadata.name, + '--cascade=foreground', + '--wait=true', + '--timeout=30s', + ]); } - return {terminal}; + return { terminal }; } export async function provisionRun(config, request, apply = runKubectl, reconcile = reconcileRunJobs) { const rendered = renderRunResources(config, request); const state = await reconcile(config, request); - if(state.terminal) return { ...rendered.receipt, status:'failed', reason:'Run generation has terminal jobs; resume with a new generation' }; + if (state.terminal) + return { + ...rendered.receipt, + status: 'failed', + reason: 'Run generation has terminal jobs; resume with a new generation', + }; // Provision policy and identities before Jobs. An interrupted call is retried // with the same deterministic resource names, never with a second workload. - await apply(config, rendered.resources.filter(resource => resource.kind !== 'Job')); - await apply(config, rendered.resources.filter(resource => resource.kind === 'Job')); + await apply( + config, + rendered.resources.filter(resource => resource.kind !== 'Job'), + ); + await apply( + config, + rendered.resources.filter(resource => resource.kind === 'Job'), + ); return rendered.receipt; } diff --git a/apps/ide/provisioner/resource-grants.mjs b/apps/ide/provisioner/resource-grants.mjs index 1143e15..e8b86ca 100644 --- a/apps/ide/provisioner/resource-grants.mjs +++ b/apps/ide/provisioner/resource-grants.mjs @@ -1,15 +1,70 @@ import { createHmac, timingSafeEqual } from 'node:crypto'; -const fields = ['schemaVersion','tenantId','runId','generation','planId','planVersion','image','cpuMilli','memoryMiB','storageMiB','issuedAt','expiresAt']; -export function resourceGrantPayload(grant) { return JSON.stringify(Object.fromEntries(fields.map(name => [name, grant[name]]))); } -export function verifyResourceGrant(config, request, { allowExpired = false, allowRetiredImage = false, cleanup = false } = {}) { +const fields = [ + 'schemaVersion', + 'tenantId', + 'runId', + 'generation', + 'planId', + 'planVersion', + 'image', + 'cpuMilli', + 'memoryMiB', + 'storageMiB', + 'issuedAt', + 'expiresAt', +]; +export function resourceGrantPayload(grant) { + return JSON.stringify(Object.fromEntries(fields.map(name => [name, grant[name]]))); +} +export function verifyResourceGrant( + config, + request, + { allowExpired = false, allowRetiredImage = false, cleanup = false } = {}, +) { const grant = request.resourceGrant; - if (!config.resourceGrantSecret || config.resourceGrantSecret.length < 32 || !grant || Object.keys(grant).some(key => ![...fields,'signature'].includes(key))) throw new Error('RESOURCE_GRANT_INVALID'); - const expected = createHmac('sha256', config.resourceGrantSecret).update(resourceGrantPayload(grant)).digest('base64url'); - const supplied = typeof grant.signature === 'string' ? Buffer.from(grant.signature) : Buffer.alloc(0), signature = Buffer.from(expected); - if (supplied.length !== signature.length || !timingSafeEqual(supplied,signature) || grant.schemaVersion !== 1 || grant.tenantId !== request.tenantId || request.tenantId !== config.tenantId || grant.runId !== request.runId || grant.generation !== request.generation) throw new Error('RESOURCE_GRANT_INVALID'); - const now = Math.floor(Date.now()/1000); - if (!Number.isSafeInteger(grant.issuedAt) || !Number.isSafeInteger(grant.expiresAt) || grant.issuedAt > now + 30 || grant.expiresAt <= grant.issuedAt || grant.expiresAt - grant.issuedAt > 300 || (!allowExpired && grant.expiresAt <= now)) throw new Error('RESOURCE_GRANT_EXPIRED'); - if (!/^[A-Za-z0-9][A-Za-z0-9._:/-]*@sha256:[a-f0-9]{64}$/.test(grant.image) || (!allowRetiredImage && ![config.image, ...(config.previousImages ?? [])].includes(grant.image))) throw new Error('RUNTIME_IMAGE_MIGRATION_REQUIRED'); - for (const [field, maximum, minimum] of [['cpuMilli',config.cpuMilliCap??2000,100],['memoryMiB',config.memoryMiBCap??4096,512],['storageMiB',Number.parseInt(config.storageSize,10)*1024,128]]) if (!Number.isSafeInteger(grant[field]) || grant[field] < minimum || (!cleanup && grant[field] > maximum)) throw new Error('RESOURCE_LIMIT'); + if ( + !config.resourceGrantSecret || + config.resourceGrantSecret.length < 32 || + !grant || + Object.keys(grant).some(key => ![...fields, 'signature'].includes(key)) + ) + throw new Error('RESOURCE_GRANT_INVALID'); + const expected = createHmac('sha256', config.resourceGrantSecret) + .update(resourceGrantPayload(grant)) + .digest('base64url'); + const supplied = typeof grant.signature === 'string' ? Buffer.from(grant.signature) : Buffer.alloc(0), + signature = Buffer.from(expected); + if ( + supplied.length !== signature.length || + !timingSafeEqual(supplied, signature) || + grant.schemaVersion !== 1 || + grant.tenantId !== request.tenantId || + request.tenantId !== config.tenantId || + grant.runId !== request.runId || + grant.generation !== request.generation + ) + throw new Error('RESOURCE_GRANT_INVALID'); + const now = Math.floor(Date.now() / 1000); + if ( + !Number.isSafeInteger(grant.issuedAt) || + !Number.isSafeInteger(grant.expiresAt) || + grant.issuedAt > now + 30 || + grant.expiresAt <= grant.issuedAt || + grant.expiresAt - grant.issuedAt > 300 || + (!allowExpired && grant.expiresAt <= now) + ) + throw new Error('RESOURCE_GRANT_EXPIRED'); + if ( + !/^[A-Za-z0-9][A-Za-z0-9._:/-]*@sha256:[a-f0-9]{64}$/.test(grant.image) || + (!allowRetiredImage && ![config.image, ...(config.previousImages ?? [])].includes(grant.image)) + ) + throw new Error('RUNTIME_IMAGE_MIGRATION_REQUIRED'); + for (const [field, maximum, minimum] of [ + ['cpuMilli', config.cpuMilliCap ?? 2000, 100], + ['memoryMiB', config.memoryMiBCap ?? 4096, 512], + ['storageMiB', Number.parseInt(config.storageSize, 10) * 1024, 128], + ]) + if (!Number.isSafeInteger(grant[field]) || grant[field] < minimum || (!cleanup && grant[field] > maximum)) + throw new Error('RESOURCE_LIMIT'); return grant; } diff --git a/apps/ide/provisioner/run-operations.mjs b/apps/ide/provisioner/run-operations.mjs index 3946a5d..86f66ef 100644 --- a/apps/ide/provisioner/run-operations.mjs +++ b/apps/ide/provisioner/run-operations.mjs @@ -6,7 +6,10 @@ export function createRunOperationQueue() { const previous = pending.get(key) ?? Promise.resolve(); const current = previous.catch(() => {}).then(operation); pending.set(key, current); - try { return await current; } - finally { if (pending.get(key) === current) pending.delete(key); } + try { + return await current; + } finally { + if (pending.get(key) === current) pending.delete(key); + } }; } diff --git a/apps/ide/provisioner/run-release.mjs b/apps/ide/provisioner/run-release.mjs index 909b896..68d2d23 100644 --- a/apps/ide/provisioner/run-release.mjs +++ b/apps/ide/provisioner/run-release.mjs @@ -3,15 +3,54 @@ import { verifyResourceGrant } from './resource-grants.mjs'; import { lifecycleCommand } from './provisioner.mjs'; /** Stops only matching compute Jobs; PVCs are retained and never called free storage. */ export async function releaseRun(config, request, execute = lifecycleCommand) { - if (!request || Object.keys(request).some(key=>!['tenantId','runId','generation','resourceGrant'].includes(key)) || !/^[A-Za-z0-9_-]{1,128}$/.test(request.runId??'') || !/^[A-Za-z0-9_-]{1,128}$/.test(request.generation??'')) throw new Error('RESOURCE_GRANT_INVALID'); - verifyResourceGrant(config,request,{allowExpired:true,allowRetiredImage:true,cleanup:true}); - const run = createHash('sha256').update(`${request.tenantId}\0${request.runId}`).digest('hex').slice(0,24), generation = createHash('sha256').update(request.generation).digest('hex').slice(0,24); - const prefix = `wf-${createHash('sha256').update(`${request.tenantId}\0${request.runId}\0${request.generation}`).digest('hex').slice(0,24)}`; - const selector=`app.kubernetes.io/managed-by=wishforge-provisioner,wishforge.io/run=${run},wishforge.io/generation=${generation},wishforge.io/component=runner`; - const assertScope = value => { if(value.metadata?.namespace!==config.namespace || value.metadata?.labels?.['app.kubernetes.io/managed-by']!=='wishforge-provisioner' || value.metadata?.labels?.['wishforge.io/component']!=='runner' || value.metadata?.labels?.['wishforge.io/run']!==run || value.metadata?.labels?.['wishforge.io/generation']!==generation)throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH'); }; - const jobs=JSON.parse(await execute(config,['get','jobs','-l',selector,'-o','json']));if(!Array.isArray(jobs.items))throw new Error('KUBERNETES_RELEASE_INVALID'); - for(const job of jobs.items){assertScope(job);if(![`${prefix}-executor`,`${prefix}-verifier`].includes(job.metadata.name))throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH');await execute(config,['delete','job',job.metadata.name,'--ignore-not-found=true','--cascade=foreground','--wait=true','--timeout=30s']);} - const pods=JSON.parse(await execute(config,['get','pods','-l',selector,'-o','json']));if(!Array.isArray(pods.items))throw new Error('KUBERNETES_RELEASE_INVALID');for(const pod of pods.items)assertScope(pod); - const active=pods.items.some(pod=>!['Succeeded','Failed'].includes(pod.status?.phase)); - return {tenantId:request.tenantId,runId:request.runId,generation:request.generation,status:active?'releasing':'released',computeReleased:!active,persistentStorageRetained:true}; + if ( + !request || + Object.keys(request).some(key => !['tenantId', 'runId', 'generation', 'resourceGrant'].includes(key)) || + !/^[A-Za-z0-9_-]{1,128}$/.test(request.runId ?? '') || + !/^[A-Za-z0-9_-]{1,128}$/.test(request.generation ?? '') + ) + throw new Error('RESOURCE_GRANT_INVALID'); + verifyResourceGrant(config, request, { allowExpired: true, allowRetiredImage: true, cleanup: true }); + const run = createHash('sha256').update(`${request.tenantId}\0${request.runId}`).digest('hex').slice(0, 24), + generation = createHash('sha256').update(request.generation).digest('hex').slice(0, 24); + const prefix = `wf-${createHash('sha256').update(`${request.tenantId}\0${request.runId}\0${request.generation}`).digest('hex').slice(0, 24)}`; + const selector = `app.kubernetes.io/managed-by=wishforge-provisioner,wishforge.io/run=${run},wishforge.io/generation=${generation},wishforge.io/component=runner`; + const assertScope = value => { + if ( + value.metadata?.namespace !== config.namespace || + value.metadata?.labels?.['app.kubernetes.io/managed-by'] !== 'wishforge-provisioner' || + value.metadata?.labels?.['wishforge.io/component'] !== 'runner' || + value.metadata?.labels?.['wishforge.io/run'] !== run || + value.metadata?.labels?.['wishforge.io/generation'] !== generation + ) + throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH'); + }; + const jobs = JSON.parse(await execute(config, ['get', 'jobs', '-l', selector, '-o', 'json'])); + if (!Array.isArray(jobs.items)) throw new Error('KUBERNETES_RELEASE_INVALID'); + for (const job of jobs.items) { + assertScope(job); + if (![`${prefix}-executor`, `${prefix}-verifier`].includes(job.metadata.name)) + throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH'); + await execute(config, [ + 'delete', + 'job', + job.metadata.name, + '--ignore-not-found=true', + '--cascade=foreground', + '--wait=true', + '--timeout=30s', + ]); + } + const pods = JSON.parse(await execute(config, ['get', 'pods', '-l', selector, '-o', 'json'])); + if (!Array.isArray(pods.items)) throw new Error('KUBERNETES_RELEASE_INVALID'); + for (const pod of pods.items) assertScope(pod); + const active = pods.items.some(pod => !['Succeeded', 'Failed'].includes(pod.status?.phase)); + return { + tenantId: request.tenantId, + runId: request.runId, + generation: request.generation, + status: active ? 'releasing' : 'released', + computeReleased: !active, + persistentStorageRetained: true, + }; } diff --git a/apps/ide/provisioner/test/provisioner.test.mjs b/apps/ide/provisioner/test/provisioner.test.mjs index 32522e1..811c6aa 100644 --- a/apps/ide/provisioner/test/provisioner.test.mjs +++ b/apps/ide/provisioner/test/provisioner.test.mjs @@ -6,16 +6,54 @@ import { resourceGrantPayload } from '../resource-grants.mjs'; import { releaseRun } from '../run-release.mjs'; import { createRunOperationQueue } from '../run-operations.mjs'; -const env = { WF_TENANT_ID: 'tenant-a', WF_KUBE_CONTEXT: 'explicit-test-context', WF_KUBE_NAMESPACE: 'wishforge-tenant-a', WF_RUN_WORKER_IMAGE: `registry.example.invalid/worker@sha256:${'a'.repeat(64)}`, WF_RUN_STORAGE_CLASS: 'reviewed-rwx', WF_CONTROL_PLANE_URL: 'https://api.wishforge-control.svc:8443', WF_CONTROL_NAMESPACE: 'wishforge-control', TEMPORAL_ADDRESS: 'temporal.wishforge-control.svc:7233', TEMPORAL_NAMESPACE: 'wishforge' }; +const env = { + WF_TENANT_ID: 'tenant-a', + WF_KUBE_CONTEXT: 'explicit-test-context', + WF_KUBE_NAMESPACE: 'wishforge-tenant-a', + WF_RUN_WORKER_IMAGE: `registry.example.invalid/worker@sha256:${'a'.repeat(64)}`, + WF_RUN_STORAGE_CLASS: 'reviewed-rwx', + WF_CONTROL_PLANE_URL: 'https://api.wishforge-control.svc:8443', + WF_CONTROL_NAMESPACE: 'wishforge-control', + TEMPORAL_ADDRESS: 'temporal.wishforge-control.svc:7233', + TEMPORAL_NAMESPACE: 'wishforge', +}; env.WF_RESOURCE_GRANT_SECRET = 'test-only-resource-signing-secret-longer-than-32'; const config = readConfig(env); -function grant(overrides={}) { - const issuedAt=Math.floor(Date.now()/1000); - const payload={schemaVersion:1,tenantId:'tenant-a',runId:'run-123',generation:'generation-1',planId:'pilot',planVersion:'pilot-v1',image:config.image,cpuMilli:1500,memoryMiB:3072,storageMiB:8192,issuedAt,expiresAt:issuedAt+300,...overrides}; - return {...payload,signature:createHmac('sha256',env.WF_RESOURCE_GRANT_SECRET).update(resourceGrantPayload(payload)).digest('base64url')}; +function grant(overrides = {}) { + const issuedAt = Math.floor(Date.now() / 1000); + const payload = { + schemaVersion: 1, + tenantId: 'tenant-a', + runId: 'run-123', + generation: 'generation-1', + planId: 'pilot', + planVersion: 'pilot-v1', + image: config.image, + cpuMilli: 1500, + memoryMiB: 3072, + storageMiB: 8192, + issuedAt, + expiresAt: issuedAt + 300, + ...overrides, + }; + return { + ...payload, + signature: createHmac('sha256', env.WF_RESOURCE_GRANT_SECRET) + .update(resourceGrantPayload(payload)) + .digest('base64url'), + }; } -function token(kind, run = 'run-123') { return `header.${Buffer.from(JSON.stringify({ tenant_id: 'tenant-a', run_id: run, generation: 'generation-1', kind, iss: 'wishforge-control-plane', aud: 'wishforge-internal', exp: Math.floor(Date.now() / 1000) + 14400 })).toString('base64url')}.signature`; } -const request = { tenantId: 'tenant-a', runId: 'run-123', generation: 'generation-1', executorToken: token('worker'), verifierToken: token('verifier'), resourceGrant:grant() }; +function token(kind, run = 'run-123') { + return `header.${Buffer.from(JSON.stringify({ tenant_id: 'tenant-a', run_id: run, generation: 'generation-1', kind, iss: 'wishforge-control-plane', aud: 'wishforge-internal', exp: Math.floor(Date.now() / 1000) + 14400 })).toString('base64url')}.signature`; +} +const request = { + tenantId: 'tenant-a', + runId: 'run-123', + generation: 'generation-1', + executorToken: token('worker'), + verifierToken: token('verifier'), + resourceGrant: grant(), +}; test('fails closed without explicit cluster, namespace, image digest and storage', () => { assert.throws(() => readConfig({}), /unconfigured/); @@ -26,92 +64,227 @@ test('fails closed without explicit cluster, namespace, image digest and storage test('renders independent Kata jobs with role-scoped credentials and separate role PVCs', () => { const { resources } = renderRunResources(config, request); - const jobs = resources.filter(r => r.kind === 'Job'); assert.equal(jobs.length, 2); + const jobs = resources.filter(r => r.kind === 'Job'); + assert.equal(jobs.length, 2); assert.notEqual(jobs[0].spec.template.spec.serviceAccountName, jobs[1].spec.template.spec.serviceAccountName); for (const job of jobs) { - const pod = job.spec.template.spec, container = pod.containers[0]; - assert.equal(pod.runtimeClassName, 'wishforge-kata'); assert.equal(pod.automountServiceAccountToken, false); - assert.equal(container.securityContext.allowPrivilegeEscalation, false); assert.equal(container.securityContext.readOnlyRootFilesystem, true); + const pod = job.spec.template.spec, + container = pod.containers[0]; + assert.equal(pod.runtimeClassName, 'wishforge-kata'); + assert.equal(pod.automountServiceAccountToken, false); + assert.equal(container.securityContext.allowPrivilegeEscalation, false); + assert.equal(container.securityContext.readOnlyRootFilesystem, true); assert.deepEqual(container.securityContext.capabilities.drop, ['ALL']); assert.ok(!pod.hostNetwork && !pod.hostPID && !pod.hostIPC); assert.ok(pod.volumes.every(v => !v.hostPath)); assert.ok(!JSON.stringify(pod).includes('docker.sock')); assert.equal(container.env.filter(e => /TOKEN$/.test(e.name)).length, 1); assert.ok(container.env.find(e => /TOKEN$/.test(e.name)).valueFrom.secretKeyRef); - assert.ok(!JSON.stringify(pod).includes(request.executorToken)); assert.ok(!JSON.stringify(pod).includes(request.verifierToken)); - assert.equal(container.image,request.resourceGrant.image); - assert.equal(container.resources.limits.cpu,'750m');assert.equal(container.resources.limits.memory,'1536Mi'); + assert.ok(!JSON.stringify(pod).includes(request.executorToken)); + assert.ok(!JSON.stringify(pod).includes(request.verifierToken)); + assert.equal(container.image, request.resourceGrant.image); + assert.equal(container.resources.limits.cpu, '750m'); + assert.equal(container.resources.limits.memory, '1536Mi'); } - const pvc = resources.filter(r => r.kind === 'PersistentVolumeClaim'); assert.equal(pvc.length, 2); assert.ok(pvc.every(claim => claim.spec.accessModes[0] === 'ReadWriteOnce')); - assert.notEqual(jobs[0].spec.template.spec.volumes[0].persistentVolumeClaim.claimName, jobs[1].spec.template.spec.volumes[0].persistentVolumeClaim.claimName); - const policy = resources.find(r => r.kind === 'NetworkPolicy'); assert.deepEqual(policy.spec.ingress, []); - assert.ok(policy.spec.egress.every(rule => rule.to.every(to => to.namespaceSelector && to.podSelector && !to.ipBlock))); - assert.ok(pvc.every(claim=>claim.spec.resources.requests.storage==='4096Mi')); - assert.ok(jobs.reduce((total,job)=>total+parseInt(job.spec.template.spec.containers[0].resources.limits.cpu),0)<=request.resourceGrant.cpuMilli); - assert.ok(jobs.reduce((total,job)=>total+parseInt(job.spec.template.spec.containers[0].resources.limits.memory),0)<=request.resourceGrant.memoryMiB); - assert.ok(pvc.reduce((total,claim)=>total+parseInt(claim.spec.resources.requests.storage),0)<=request.resourceGrant.storageMiB); - const other = renderRunResources(config, { ...request, runId: 'run-456', executorToken: token('worker', 'run-456'), verifierToken: token('verifier', 'run-456'),resourceGrant:grant({runId:'run-456'}) }); + const pvc = resources.filter(r => r.kind === 'PersistentVolumeClaim'); + assert.equal(pvc.length, 2); + assert.ok(pvc.every(claim => claim.spec.accessModes[0] === 'ReadWriteOnce')); + assert.notEqual( + jobs[0].spec.template.spec.volumes[0].persistentVolumeClaim.claimName, + jobs[1].spec.template.spec.volumes[0].persistentVolumeClaim.claimName, + ); + const policy = resources.find(r => r.kind === 'NetworkPolicy'); + assert.deepEqual(policy.spec.ingress, []); + assert.ok( + policy.spec.egress.every(rule => rule.to.every(to => to.namespaceSelector && to.podSelector && !to.ipBlock)), + ); + assert.ok(pvc.every(claim => claim.spec.resources.requests.storage === '4096Mi')); + assert.ok( + jobs.reduce((total, job) => total + parseInt(job.spec.template.spec.containers[0].resources.limits.cpu), 0) <= + request.resourceGrant.cpuMilli, + ); + assert.ok( + jobs.reduce((total, job) => total + parseInt(job.spec.template.spec.containers[0].resources.limits.memory), 0) <= + request.resourceGrant.memoryMiB, + ); + assert.ok( + pvc.reduce((total, claim) => total + parseInt(claim.spec.resources.requests.storage), 0) <= + request.resourceGrant.storageMiB, + ); + const other = renderRunResources(config, { + ...request, + runId: 'run-456', + executorToken: token('worker', 'run-456'), + verifierToken: token('verifier', 'run-456'), + resourceGrant: grant({ runId: 'run-456' }), + }); assert.notEqual(other.receipt.workspaceClaim, pvc[0].metadata.name); }); -test('rejects tampered, expired, wrong-scope and above-cap resource grants',()=>{ - for(const change of [{cpuMilli:1600},{generation:'other'},{tenantId:'other'}])assert.throws(()=>renderRunResources(config,{...request,resourceGrant:{...request.resourceGrant,...change}}),/RESOURCE_GRANT_INVALID/); - assert.throws(()=>renderRunResources(config,{...request,resourceGrant:grant({runId:'other'})}),/RESOURCE_GRANT_INVALID/); - assert.throws(()=>renderRunResources(config,{...request,resourceGrant:grant({issuedAt:1,expiresAt:301})}),/RESOURCE_GRANT_EXPIRED/); - for(const change of [{cpuMilli:2001},{memoryMiB:4097},{storageMiB:10241}])assert.throws(()=>renderRunResources(config,{...request,resourceGrant:grant(change)}),/RESOURCE_LIMIT/); +test('rejects tampered, expired, wrong-scope and above-cap resource grants', () => { + for (const change of [{ cpuMilli: 1600 }, { generation: 'other' }, { tenantId: 'other' }]) + assert.throws( + () => renderRunResources(config, { ...request, resourceGrant: { ...request.resourceGrant, ...change } }), + /RESOURCE_GRANT_INVALID/, + ); + assert.throws( + () => renderRunResources(config, { ...request, resourceGrant: grant({ runId: 'other' }) }), + /RESOURCE_GRANT_INVALID/, + ); + assert.throws( + () => renderRunResources(config, { ...request, resourceGrant: grant({ issuedAt: 1, expiresAt: 301 }) }), + /RESOURCE_GRANT_EXPIRED/, + ); + for (const change of [{ cpuMilli: 2001 }, { memoryMiB: 4097 }, { storageMiB: 10241 }]) + assert.throws(() => renderRunResources(config, { ...request, resourceGrant: grant(change) }), /RESOURCE_LIMIT/); }); -test('pins signed previous runtime image and requires migration after its retirement',()=>{ - const previous=`registry.example.invalid/worker@sha256:${'b'.repeat(64)}`; - const requestWithPrevious={...request,resourceGrant:grant({image:previous})}; - const configured=readConfig({...env,WF_RUN_PREVIOUS_IMAGES:previous}); - assert.ok(renderRunResources(configured,requestWithPrevious).resources.filter(r=>r.kind==='Job').every(r=>r.spec.template.spec.containers[0].image===previous)); - assert.throws(()=>renderRunResources(config,requestWithPrevious),/RUNTIME_IMAGE_MIGRATION_REQUIRED/); +test('pins signed previous runtime image and requires migration after its retirement', () => { + const previous = `registry.example.invalid/worker@sha256:${'b'.repeat(64)}`; + const requestWithPrevious = { ...request, resourceGrant: grant({ image: previous }) }; + const configured = readConfig({ ...env, WF_RUN_PREVIOUS_IMAGES: previous }); + assert.ok( + renderRunResources(configured, requestWithPrevious) + .resources.filter(r => r.kind === 'Job') + .every(r => r.spec.template.spec.containers[0].image === previous), + ); + assert.throws(() => renderRunResources(config, requestWithPrevious), /RUNTIME_IMAGE_MIGRATION_REQUIRED/); }); -test('cleanup verifies ownership, stops matching jobs, retains PVCs and never fabricates measurement',async()=>{ - const {resources}=renderRunResources(config,request);const job=resources.find(r=>r.kind==='Job');const calls=[]; - const release={tenantId:request.tenantId,runId:request.runId,generation:request.generation,resourceGrant:grant({issuedAt:1,expiresAt:301,image:`registry.example.invalid/retired@sha256:${'c'.repeat(64)}`})}; - const execute=async(_config,args)=>{calls.push(args);return args[0]==='get'?JSON.stringify({items:args[1]==='jobs'?[job]:[]}):'';}; - const receipt=await releaseRun(config,release,execute); - assert.equal(receipt.status,'released');assert.equal(receipt.computeReleased,true);assert.equal(receipt.persistentStorageRetained,true); - assert.equal(receipt.computeSeconds,undefined);assert.ok(calls.every(args=>!args.includes('pvc'))); - assert.equal(calls.filter(args=>args[0]==='delete').length,1); - await assert.rejects(releaseRun(config,release,async()=>JSON.stringify({items:[{...job,metadata:{...job.metadata,namespace:'other'}}]})),/SCOPE_MISMATCH/); - const pending=await releaseRun(config,release,async(_config,args)=>JSON.stringify({items:args[1]==='pods'?[{metadata:job.metadata,status:{phase:'Running'}}]:[]})); - assert.equal(pending.status,'releasing');assert.equal(pending.computeReleased,false); +test('cleanup verifies ownership, stops matching jobs, retains PVCs and never fabricates measurement', async () => { + const { resources } = renderRunResources(config, request); + const job = resources.find(r => r.kind === 'Job'); + const calls = []; + const release = { + tenantId: request.tenantId, + runId: request.runId, + generation: request.generation, + resourceGrant: grant({ + issuedAt: 1, + expiresAt: 301, + image: `registry.example.invalid/retired@sha256:${'c'.repeat(64)}`, + }), + }; + const execute = async (_config, args) => { + calls.push(args); + return args[0] === 'get' ? JSON.stringify({ items: args[1] === 'jobs' ? [job] : [] }) : ''; + }; + const receipt = await releaseRun(config, release, execute); + assert.equal(receipt.status, 'released'); + assert.equal(receipt.computeReleased, true); + assert.equal(receipt.persistentStorageRetained, true); + assert.equal(receipt.computeSeconds, undefined); + assert.ok(calls.every(args => !args.includes('pvc'))); + assert.equal(calls.filter(args => args[0] === 'delete').length, 1); + await assert.rejects( + releaseRun(config, release, async () => + JSON.stringify({ items: [{ ...job, metadata: { ...job.metadata, namespace: 'other' } }] }), + ), + /SCOPE_MISMATCH/, + ); + const pending = await releaseRun(config, release, async (_config, args) => + JSON.stringify({ items: args[1] === 'pods' ? [{ metadata: job.metadata, status: { phase: 'Running' } }] : [] }), + ); + assert.equal(pending.status, 'releasing'); + assert.equal(pending.computeReleased, false); }); -test('serializes delayed provision with cleanup while allowing independent runs',async()=>{ - const queue=createRunOperationQueue(),order=[];let resolve; - const first=queue('tenant-a','run-1',async()=>{order.push('provision');await new Promise(r=>{resolve=r;});order.push('provisioned');}); +test('serializes delayed provision with cleanup while allowing independent runs', async () => { + const queue = createRunOperationQueue(), + order = []; + let resolve; + const first = queue('tenant-a', 'run-1', async () => { + order.push('provision'); + await new Promise(r => { + resolve = r; + }); + order.push('provisioned'); + }); await Promise.resolve(); - const cleanup=queue('tenant-a','run-1',async()=>{order.push('cleanup');}); - await queue('tenant-a','run-2',async()=>{order.push('independent');}); - assert.deepEqual(order,['provision','independent']);resolve();await Promise.all([first,cleanup]);assert.deepEqual(order,['provision','independent','provisioned','cleanup']); - await assert.rejects(queue('tenant-a','run-1',async()=>{throw new Error('fixture');}),/fixture/); - assert.equal(await queue('tenant-a','run-1',async()=> 'reconciled'),'reconciled'); + const cleanup = queue('tenant-a', 'run-1', async () => { + order.push('cleanup'); + }); + await queue('tenant-a', 'run-2', async () => { + order.push('independent'); + }); + assert.deepEqual(order, ['provision', 'independent']); + resolve(); + await Promise.all([first, cleanup]); + assert.deepEqual(order, ['provision', 'independent', 'provisioned', 'cleanup']); + await assert.rejects( + queue('tenant-a', 'run-1', async () => { + throw new Error('fixture'); + }), + /fixture/, + ); + assert.equal(await queue('tenant-a', 'run-1', async () => 'reconciled'), 'reconciled'); }); test('applies policy before jobs, retries deterministic names and propagates infrastructure failure', async () => { - const calls = []; const apply = async (cfg, resources) => { assert.equal(cfg.context, 'explicit-test-context'); calls.push(resources); }; - const first = await provisionRun(config, request, apply, async () => ({terminal:false})); const second = await provisionRun(config, request, apply, async () => ({terminal:false})); - assert.deepEqual(first, second); assert.equal(calls.length, 4); - assert.ok(calls[0].every(r => r.kind !== 'Job')); assert.ok(calls[1].every(r => r.kind === 'Job')); - await assert.rejects(provisionRun(config, request, async () => { throw new Error('KUBERNETES_APPLY_FAILED'); }, async () => ({terminal:false})), /KUBERNETES_APPLY_FAILED/); + const calls = []; + const apply = async (cfg, resources) => { + assert.equal(cfg.context, 'explicit-test-context'); + calls.push(resources); + }; + const first = await provisionRun(config, request, apply, async () => ({ terminal: false })); + const second = await provisionRun(config, request, apply, async () => ({ terminal: false })); + assert.deepEqual(first, second); + assert.equal(calls.length, 4); + assert.ok(calls[0].every(r => r.kind !== 'Job')); + assert.ok(calls[1].every(r => r.kind === 'Job')); + await assert.rejects( + provisionRun( + config, + request, + async () => { + throw new Error('KUBERNETES_APPLY_FAILED'); + }, + async () => ({ terminal: false }), + ), + /KUBERNETES_APPLY_FAILED/, + ); }); test('reconciles only old owned run jobs and refuses to pretend a terminal generation resumed', async () => { - const {resources}=renderRunResources(config,request); - const job=resources.find(r=>r.kind==='Job');const calls=[]; - const old={...job,metadata:{...job.metadata,labels:{...job.metadata.labels,'wishforge.io/generation':'old-generation'}}}; - const execute=async(cfg,args)=>{calls.push(args);return args[0]==='get'?JSON.stringify({items:[old]}):'';}; - assert.deepEqual(await reconcileRunJobs(config,request,execute),{terminal:false}); - assert.deepEqual(calls[1],['delete','job',job.metadata.name,'--cascade=foreground','--wait=true','--timeout=30s']); - const terminal=await reconcileRunJobs(config,request,async()=>JSON.stringify({items:[{...job,status:{failed:1}}]})); - assert.equal(terminal.terminal,true); - let applied=false; - const response=await provisionRun(config,request,async()=>{applied=true;},async()=>terminal); - assert.equal(response.status,'failed');assert.equal(applied,false); - await assert.rejects(reconcileRunJobs(config,request,async()=>JSON.stringify({items:[{...old,metadata:{...old.metadata,namespace:'other-tenant'}}]})),/SCOPE/); + const { resources } = renderRunResources(config, request); + const job = resources.find(r => r.kind === 'Job'); + const calls = []; + const old = { + ...job, + metadata: { ...job.metadata, labels: { ...job.metadata.labels, 'wishforge.io/generation': 'old-generation' } }, + }; + const execute = async (cfg, args) => { + calls.push(args); + return args[0] === 'get' ? JSON.stringify({ items: [old] }) : ''; + }; + assert.deepEqual(await reconcileRunJobs(config, request, execute), { terminal: false }); + assert.deepEqual(calls[1], [ + 'delete', + 'job', + job.metadata.name, + '--cascade=foreground', + '--wait=true', + '--timeout=30s', + ]); + const terminal = await reconcileRunJobs(config, request, async () => + JSON.stringify({ items: [{ ...job, status: { failed: 1 } }] }), + ); + assert.equal(terminal.terminal, true); + let applied = false; + const response = await provisionRun( + config, + request, + async () => { + applied = true; + }, + async () => terminal, + ); + assert.equal(response.status, 'failed'); + assert.equal(applied, false); + await assert.rejects( + reconcileRunJobs(config, request, async () => + JSON.stringify({ items: [{ ...old, metadata: { ...old.metadata, namespace: 'other-tenant' } }] }), + ), + /SCOPE/, + ); }); diff --git a/apps/ide/provisioner/test/workspace-lifecycle.test.mjs b/apps/ide/provisioner/test/workspace-lifecycle.test.mjs index d24dc2a..19b6b70 100644 --- a/apps/ide/provisioner/test/workspace-lifecycle.test.mjs +++ b/apps/ide/provisioner/test/workspace-lifecycle.test.mjs @@ -1,10 +1,126 @@ -import test from 'node:test';import assert from 'node:assert/strict'; -import {renderWorkspaceResources} from '../workspaces.mjs'; -import {observeWorkspaceLifecycle,reconcileWorkspaceLifecycle} from '../workspace-lifecycle.mjs'; -const config={tenantId:'alpha',namespace:'wf-alpha',context:'fixture',controlNamespace:'wf-control',runtimeClass:'kata',storageClass:'fixture',storageSize:'10Gi'}; -const input={tenantId:'alpha',projectId:'project',workspaceId:'workspace'}; -function fixture(){const rendered=renderWorkspaceResources(config,input,`registry.example/theia@sha256:${'a'.repeat(64)}`);const d=rendered.resources.find(r=>r.kind==='Deployment');d.metadata.uid='immutable-uid';d.metadata.resourceVersion='1';d.metadata.generation=1;d.status={observedGeneration:1,availableReplicas:1,updatedReplicas:1,replicas:1};let pods=[];let lose=false;const calls=[];const execute=async(_config,args)=>{calls.push(args);if(args[0]==='get'&&args[1]==='pods')return{items:pods};if(args[0]==='get')return structuredClone(d);assert.equal(args[0],'patch');assert.equal(args[1],'deployment');assert.equal(args[2],rendered.name);const patch=JSON.parse(args[args.indexOf('-p')+1]);assert.equal(patch[0].value,d.metadata.resourceVersion);assert.equal(patch[1].value,d.metadata.uid);d.metadata.annotations=patch[2].value;d.spec.replicas=patch[3].value;d.metadata.resourceVersion=String(Number(d.metadata.resourceVersion)+1);d.metadata.generation++;if(lose)throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN');return structuredClone(d);};return{d,calls,execute,setPods:value=>{pods=value;},lose:()=>{lose=true;},converge:()=>{d.status={observedGeneration:d.metadata.generation,replicas:d.spec.replicas,availableReplicas:d.spec.replicas,updatedReplicas:d.spec.replicas};}};} -test('pause only patches the owned IDE deployment, retains all PVCs, and waits for observed termination',async()=>{const f=fixture();const request={...input,revision:1,desired:'paused'};assert.equal((await reconcileWorkspaceLifecycle(config,request,{execute:f.execute})).status,'pending');f.converge();f.setPods([{status:{phase:'Running'},metadata:{deletionTimestamp:new Date().toISOString()}}]);assert.equal((await observeWorkspaceLifecycle(config,input,{execute:f.execute})).status,'pending');f.setPods([]);assert.equal((await observeWorkspaceLifecycle(config,input,{execute:f.execute})).status,'paused');assert.ok(f.calls.every(args=>!args.includes('pvc')&&!args.includes('delete')&&!args.includes('service')));}); -test('resume does not claim ready until updated and available replica is observed',async()=>{const f=fixture();await reconcileWorkspaceLifecycle(config,{...input,revision:1,desired:'paused'},{execute:f.execute});f.converge();assert.equal((await reconcileWorkspaceLifecycle(config,{...input,revision:2,desired:'running'},{execute:f.execute})).status,'pending');f.converge();assert.equal((await observeWorkspaceLifecycle(config,input,{execute:f.execute})).status,'ready');}); -test('lost mutation response reconciles by observation and does not send a duplicate patch',async()=>{const f=fixture();f.lose();const request={...input,revision:1,desired:'paused'};await assert.rejects(reconcileWorkspaceLifecycle(config,request,{execute:f.execute}),/UNKNOWN/);f.converge();assert.equal((await reconcileWorkspaceLifecycle(config,request,{execute:f.execute})).status,'paused');assert.equal(f.calls.filter(a=>a[0]==='patch').length,1);}); -test('tenant, namespace, managed labels and revision fencing reject without mutations',async()=>{const f=fixture();await assert.rejects(reconcileWorkspaceLifecycle(config,{...input,tenantId:'other',revision:1,desired:'paused'},{execute:f.execute}),/SCOPE/);f.d.metadata.labels['wishforge.io/component']='production-app';await assert.rejects(reconcileWorkspaceLifecycle(config,{...input,revision:1,desired:'paused'},{execute:f.execute}),/OWNERSHIP/);f.d.metadata.labels['wishforge.io/component']='ide';f.d.metadata.namespace='production';await assert.rejects(reconcileWorkspaceLifecycle(config,{...input,revision:1,desired:'paused'},{execute:f.execute}),/OWNERSHIP/);f.d.metadata.namespace=config.namespace;f.d.metadata.annotations={'wishforge.io/lifecycle-revision':'3','wishforge.io/lifecycle-desired':'running'};await assert.rejects(reconcileWorkspaceLifecycle(config,{...input,revision:2,desired:'paused'},{execute:f.execute}),/STALE/);await assert.rejects(reconcileWorkspaceLifecycle(config,{...input,revision:3,desired:'paused'},{execute:f.execute}),/CONFLICT/);assert.equal(f.calls.filter(a=>a[0]==='patch').length,0);}); +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { renderWorkspaceResources } from '../workspaces.mjs'; +import { observeWorkspaceLifecycle, reconcileWorkspaceLifecycle } from '../workspace-lifecycle.mjs'; +const config = { + tenantId: 'alpha', + namespace: 'wf-alpha', + context: 'fixture', + controlNamespace: 'wf-control', + runtimeClass: 'kata', + storageClass: 'fixture', + storageSize: '10Gi', +}; +const input = { tenantId: 'alpha', projectId: 'project', workspaceId: 'workspace' }; +function fixture() { + const rendered = renderWorkspaceResources(config, input, `registry.example/theia@sha256:${'a'.repeat(64)}`); + const d = rendered.resources.find(r => r.kind === 'Deployment'); + d.metadata.uid = 'immutable-uid'; + d.metadata.resourceVersion = '1'; + d.metadata.generation = 1; + d.status = { observedGeneration: 1, availableReplicas: 1, updatedReplicas: 1, replicas: 1 }; + let pods = []; + let lose = false; + const calls = []; + const execute = async (_config, args) => { + calls.push(args); + if (args[0] === 'get' && args[1] === 'pods') return { items: pods }; + if (args[0] === 'get') return structuredClone(d); + assert.equal(args[0], 'patch'); + assert.equal(args[1], 'deployment'); + assert.equal(args[2], rendered.name); + const patch = JSON.parse(args[args.indexOf('-p') + 1]); + assert.equal(patch[0].value, d.metadata.resourceVersion); + assert.equal(patch[1].value, d.metadata.uid); + d.metadata.annotations = patch[2].value; + d.spec.replicas = patch[3].value; + d.metadata.resourceVersion = String(Number(d.metadata.resourceVersion) + 1); + d.metadata.generation++; + if (lose) throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN'); + return structuredClone(d); + }; + return { + d, + calls, + execute, + setPods: value => { + pods = value; + }, + lose: () => { + lose = true; + }, + converge: () => { + d.status = { + observedGeneration: d.metadata.generation, + replicas: d.spec.replicas, + availableReplicas: d.spec.replicas, + updatedReplicas: d.spec.replicas, + }; + }, + }; +} +test('pause only patches the owned IDE deployment, retains all PVCs, and waits for observed termination', async () => { + const f = fixture(); + const request = { ...input, revision: 1, desired: 'paused' }; + assert.equal((await reconcileWorkspaceLifecycle(config, request, { execute: f.execute })).status, 'pending'); + f.converge(); + f.setPods([{ status: { phase: 'Running' }, metadata: { deletionTimestamp: new Date().toISOString() } }]); + assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'pending'); + f.setPods([]); + assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'paused'); + assert.ok(f.calls.every(args => !args.includes('pvc') && !args.includes('delete') && !args.includes('service'))); +}); +test('resume does not claim ready until updated and available replica is observed', async () => { + const f = fixture(); + await reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute }); + f.converge(); + assert.equal( + (await reconcileWorkspaceLifecycle(config, { ...input, revision: 2, desired: 'running' }, { execute: f.execute })) + .status, + 'pending', + ); + f.converge(); + assert.equal((await observeWorkspaceLifecycle(config, input, { execute: f.execute })).status, 'ready'); +}); +test('lost mutation response reconciles by observation and does not send a duplicate patch', async () => { + const f = fixture(); + f.lose(); + const request = { ...input, revision: 1, desired: 'paused' }; + await assert.rejects(reconcileWorkspaceLifecycle(config, request, { execute: f.execute }), /UNKNOWN/); + f.converge(); + assert.equal((await reconcileWorkspaceLifecycle(config, request, { execute: f.execute })).status, 'paused'); + assert.equal(f.calls.filter(a => a[0] === 'patch').length, 1); +}); +test('tenant, namespace, managed labels and revision fencing reject without mutations', async () => { + const f = fixture(); + await assert.rejects( + reconcileWorkspaceLifecycle( + config, + { ...input, tenantId: 'other', revision: 1, desired: 'paused' }, + { execute: f.execute }, + ), + /SCOPE/, + ); + f.d.metadata.labels['wishforge.io/component'] = 'production-app'; + await assert.rejects( + reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute }), + /OWNERSHIP/, + ); + f.d.metadata.labels['wishforge.io/component'] = 'ide'; + f.d.metadata.namespace = 'production'; + await assert.rejects( + reconcileWorkspaceLifecycle(config, { ...input, revision: 1, desired: 'paused' }, { execute: f.execute }), + /OWNERSHIP/, + ); + f.d.metadata.namespace = config.namespace; + f.d.metadata.annotations = { 'wishforge.io/lifecycle-revision': '3', 'wishforge.io/lifecycle-desired': 'running' }; + await assert.rejects( + reconcileWorkspaceLifecycle(config, { ...input, revision: 2, desired: 'paused' }, { execute: f.execute }), + /STALE/, + ); + await assert.rejects( + reconcileWorkspaceLifecycle(config, { ...input, revision: 3, desired: 'paused' }, { execute: f.execute }), + /CONFLICT/, + ); + assert.equal(f.calls.filter(a => a[0] === 'patch').length, 0); +}); diff --git a/apps/ide/provisioner/test/workspaces.test.mjs b/apps/ide/provisioner/test/workspaces.test.mjs index 98e6041..3f4f639 100644 --- a/apps/ide/provisioner/test/workspaces.test.mjs +++ b/apps/ide/provisioner/test/workspaces.test.mjs @@ -1,34 +1,55 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { renderWorkspaceResources, provisionWorkspace } from '../workspaces.mjs'; -const config = { tenantId: 'alpha', namespace: 'wf-alpha', controlNamespace: 'wf-control', runtimeClass: 'wishforge-kata', storageClass: 'test-storage', storageSize: '10Gi' }; +const config = { + tenantId: 'alpha', + namespace: 'wf-alpha', + controlNamespace: 'wf-control', + runtimeClass: 'wishforge-kata', + storageClass: 'test-storage', + storageSize: '10Gi', +}; const input = { tenantId: 'alpha', projectId: 'project-1', workspaceId: 'workspace-1' }; const image = `registry.invalid/theia@sha256:${'a'.repeat(64)}`; test('workspace provisioning uses the actual Theia start contract and a private isolated endpoint', () => { const rendered = renderWorkspaceResources(config, input, image); const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec; - assert.equal(pod.runtimeClassName, 'wishforge-kata'); assert.equal(pod.automountServiceAccountToken, false); + assert.equal(pod.runtimeClassName, 'wishforge-kata'); + assert.equal(pod.automountServiceAccountToken, false); const env = Object.fromEntries(pod.containers[0].env.map(e => [e.name, e.value])); - assert.equal(env.WISHFORGE_IDE_PORT, '3000'); assert.equal(env.WISHFORGE_GATEWAY_ENFORCED, 'true'); - assert.equal(env.WISHFORGE_WORKSPACE_PATH, '/home/theia/data/workspace'); assert.equal(env.WISHFORGE_COLLABORATION_ENABLED, 'false'); + assert.equal(env.WISHFORGE_IDE_PORT, '3000'); + assert.equal(env.WISHFORGE_GATEWAY_ENFORCED, 'true'); + assert.equal(env.WISHFORGE_WORKSPACE_PATH, '/home/theia/data/workspace'); + assert.equal(env.WISHFORGE_COLLABORATION_ENABLED, 'false'); assert.equal(rendered.resources.find(r => r.kind === 'Service').spec.type, 'ClusterIP'); }); test('creating manifests does not mark an unready IDE available', async () => { let applied = false; - const result = await provisionWorkspace(config, input, { image, apply: async () => { applied = true; }, observe: async () => false }); - assert.ok(applied); assert.equal(result.status, 'pending'); + const result = await provisionWorkspace(config, input, { + image, + apply: async () => { + applied = true; + }, + observe: async () => false, + }); + assert.ok(applied); + assert.equal(result.status, 'pending'); assert.throws(() => renderWorkspaceResources(config, { ...input, tenantId: 'beta' }, image)); }); test('scoped collaboration and coordination expose only public identity material to the workspace', () => { - const rendered = renderWorkspaceResources({...config,gatewayPublicKeyConfigMap:'gateway-public'}, input, image, {image,gatewayBase:'https://ide.example/'}); - const pod=rendered.resources.find(r=>r.kind==='Deployment').spec.template.spec; + const rendered = renderWorkspaceResources({ ...config, gatewayPublicKeyConfigMap: 'gateway-public' }, input, image, { + image, + gatewayBase: 'https://ide.example/', + }); + const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec; { - assert.equal(pod.containers.length,2); - assert.ok(pod.containers[0].env.some(e=>e.name==='WF_IDE_GATEWAY_PUBLIC_KEY_FILE')); + assert.equal(pod.containers.length, 2); + assert.ok(pod.containers[0].env.some(e => e.name === 'WF_IDE_GATEWAY_PUBLIC_KEY_FILE')); assert.ok(!JSON.stringify(pod).includes('PRIVATE_KEY')); - assert.ok(pod.volumes.some(v=>v.name==='coordination-state')); - const octEnv=Object.fromEntries(pod.containers[1].env.map(e=>[e.name,e.value])); - assert.equal(octEnv.WF_WORKSPACE_ID,input.workspaceId);assert.equal(octEnv.WISHFORGE_GATEWAY_ENFORCED,'true'); + assert.ok(pod.volumes.some(v => v.name === 'coordination-state')); + const octEnv = Object.fromEntries(pod.containers[1].env.map(e => [e.name, e.value])); + assert.equal(octEnv.WF_WORKSPACE_ID, input.workspaceId); + assert.equal(octEnv.WISHFORGE_GATEWAY_ENFORCED, 'true'); assert.ok(rendered.collaborationUpstreamUrl.endsWith(':8100')); } }); diff --git a/apps/ide/provisioner/workspace-lifecycle.mjs b/apps/ide/provisioner/workspace-lifecycle.mjs index 138c731..fae5c5d 100644 --- a/apps/ide/provisioner/workspace-lifecycle.mjs +++ b/apps/ide/provisioner/workspace-lifecycle.mjs @@ -1,10 +1,132 @@ import { createHash } from 'node:crypto'; import { spawn } from 'node:child_process'; -const id=/^[a-zA-Z0-9_-]{1,128}$/; -const revisionKey='wishforge.io/lifecycle-revision'; -const desiredKey='wishforge.io/lifecycle-desired'; -function scope(config,input){if(!input||input.tenantId!==config.tenantId||!id.test(input.projectId??'')||!id.test(input.workspaceId??''))throw new Error('INVALID_WORKSPACE_SCOPE');return `wf-ide-${createHash('sha256').update(`${input.tenantId}/${input.workspaceId}`).digest('hex').slice(0,24)}`;} -function assertOwned(config,name,value){if(value?.metadata?.name!==name||value.metadata.namespace!==config.namespace||value.metadata.labels?.['app.kubernetes.io/managed-by']!=='wishforge-provisioner'||value.metadata.labels?.['wishforge.io/component']!=='ide'||value.metadata.labels?.['wishforge.io/workspace']!==name)throw new Error('WORKSPACE_OWNERSHIP_MISMATCH');} -export function kubectlJson(config,args){return new Promise((resolve,reject)=>{const child=spawn('kubectl',['--context',config.context,'--namespace',config.namespace,...args],{stdio:['ignore','pipe','pipe']});let output='';let settled=false;const fail=()=>{if(!settled){settled=true;reject(new Error('KUBERNETES_LIFECYCLE_UNKNOWN'));}};const timeout=setTimeout(()=>{child.kill('SIGTERM');fail();},10000);child.stdout.on('data',part=>{output+=part;if(output.length>2*1024*1024){child.kill('SIGTERM');fail();}});child.stderr.resume();child.on('error',fail);child.on('close',code=>{clearTimeout(timeout);if(settled)return;try{if(code!==0)throw Error();const value=JSON.parse(output);settled=true;resolve(value);}catch{fail();}});});} -export async function observeWorkspaceLifecycle(config,input,options={}){const name=scope(config,input);const execute=options.execute??kubectlJson;const deployment=await execute(config,['get','deployment',name,'-o','json']);assertOwned(config,name,deployment);const revision=Number(deployment.metadata.annotations?.[revisionKey]??0);const desired=deployment.metadata.annotations?.[desiredKey]??'running';if(!Number.isSafeInteger(revision)||revision<0||!['running','paused'].includes(desired))throw new Error('INVALID_WORKSPACE_LIFECYCLE_STATE');let status='pending';const observed=deployment.status?.observedGeneration>=deployment.metadata.generation;if(desired==='running'&&deployment.spec?.replicas===1&&observed&&deployment.status.availableReplicas===1&&deployment.status.updatedReplicas===1)status='ready';if(desired==='paused'&&deployment.spec?.replicas===0&&observed&&(deployment.status.replicas??0)===0){const selector=`wishforge.io/workspace=${name},wishforge.io/component=ide,app.kubernetes.io/managed-by=wishforge-provisioner`;const pods=await execute(config,['get','pods','--selector',selector,'-o','json']);if(!Array.isArray(pods.items))throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN');if(pods.items.every(p=>['Succeeded','Failed'].includes(p.status?.phase)))status='paused';}return{workspaceId:input.workspaceId,revision,desired,status,deploymentUid:deployment.metadata.uid,resourceVersion:deployment.metadata.resourceVersion};} -export async function reconcileWorkspaceLifecycle(config,input,options={}){if(!['running','paused'].includes(input?.desired)||!Number.isSafeInteger(input.revision)||input.revision<1||Object.keys(input).some(k=>!['tenantId','projectId','workspaceId','revision','desired'].includes(k)))throw new Error('INVALID_WORKSPACE_LIFECYCLE_REQUEST');const execute=options.execute??kubectlJson;const name=scope(config,input);const deployment=await execute(config,['get','deployment',name,'-o','json']);assertOwned(config,name,deployment);const previous=Number(deployment.metadata.annotations?.[revisionKey]??0);if(!Number.isSafeInteger(previous)||previous>input.revision)throw new Error('STALE_WORKSPACE_REVISION');if(previous===input.revision){if(deployment.metadata.annotations?.[desiredKey]!==input.desired)throw new Error('WORKSPACE_REVISION_CONFLICT');return observeWorkspaceLifecycle(config,input,{execute});}if(!deployment.metadata.resourceVersion||!deployment.metadata.uid)throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN');const annotations={...deployment.metadata.annotations,[revisionKey]:String(input.revision),[desiredKey]:input.desired};const patch=[{op:'test',path:'/metadata/resourceVersion',value:deployment.metadata.resourceVersion},{op:'test',path:'/metadata/uid',value:deployment.metadata.uid},{op:'add',path:'/metadata/annotations',value:annotations},{op:'replace',path:'/spec/replicas',value:input.desired==='paused'?0:1}];await execute(config,['patch','deployment',name,'--type=json','-p',JSON.stringify(patch),'-o','json']);return observeWorkspaceLifecycle(config,input,{execute});} +const id = /^[a-zA-Z0-9_-]{1,128}$/; +const revisionKey = 'wishforge.io/lifecycle-revision'; +const desiredKey = 'wishforge.io/lifecycle-desired'; +function scope(config, input) { + if ( + !input || + input.tenantId !== config.tenantId || + !id.test(input.projectId ?? '') || + !id.test(input.workspaceId ?? '') + ) + throw new Error('INVALID_WORKSPACE_SCOPE'); + return `wf-ide-${createHash('sha256').update(`${input.tenantId}/${input.workspaceId}`).digest('hex').slice(0, 24)}`; +} +function assertOwned(config, name, value) { + if ( + value?.metadata?.name !== name || + value.metadata.namespace !== config.namespace || + value.metadata.labels?.['app.kubernetes.io/managed-by'] !== 'wishforge-provisioner' || + value.metadata.labels?.['wishforge.io/component'] !== 'ide' || + value.metadata.labels?.['wishforge.io/workspace'] !== name + ) + throw new Error('WORKSPACE_OWNERSHIP_MISMATCH'); +} +export function kubectlJson(config, args) { + return new Promise((resolve, reject) => { + const child = spawn('kubectl', ['--context', config.context, '--namespace', config.namespace, ...args], { + stdio: ['ignore', 'pipe', 'pipe'], + }); + let output = ''; + let settled = false; + const fail = () => { + if (!settled) { + settled = true; + reject(new Error('KUBERNETES_LIFECYCLE_UNKNOWN')); + } + }; + const timeout = setTimeout(() => { + child.kill('SIGTERM'); + fail(); + }, 10000); + child.stdout.on('data', part => { + output += part; + if (output.length > 2 * 1024 * 1024) { + child.kill('SIGTERM'); + fail(); + } + }); + child.stderr.resume(); + child.on('error', fail); + child.on('close', code => { + clearTimeout(timeout); + if (settled) return; + try { + if (code !== 0) throw Error(); + const value = JSON.parse(output); + settled = true; + resolve(value); + } catch { + fail(); + } + }); + }); +} +export async function observeWorkspaceLifecycle(config, input, options = {}) { + const name = scope(config, input); + const execute = options.execute ?? kubectlJson; + const deployment = await execute(config, ['get', 'deployment', name, '-o', 'json']); + assertOwned(config, name, deployment); + const revision = Number(deployment.metadata.annotations?.[revisionKey] ?? 0); + const desired = deployment.metadata.annotations?.[desiredKey] ?? 'running'; + if (!Number.isSafeInteger(revision) || revision < 0 || !['running', 'paused'].includes(desired)) + throw new Error('INVALID_WORKSPACE_LIFECYCLE_STATE'); + let status = 'pending'; + const observed = deployment.status?.observedGeneration >= deployment.metadata.generation; + if ( + desired === 'running' && + deployment.spec?.replicas === 1 && + observed && + deployment.status.availableReplicas === 1 && + deployment.status.updatedReplicas === 1 + ) + status = 'ready'; + if (desired === 'paused' && deployment.spec?.replicas === 0 && observed && (deployment.status.replicas ?? 0) === 0) { + const selector = `wishforge.io/workspace=${name},wishforge.io/component=ide,app.kubernetes.io/managed-by=wishforge-provisioner`; + const pods = await execute(config, ['get', 'pods', '--selector', selector, '-o', 'json']); + if (!Array.isArray(pods.items)) throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN'); + if (pods.items.every(p => ['Succeeded', 'Failed'].includes(p.status?.phase))) status = 'paused'; + } + return { + workspaceId: input.workspaceId, + revision, + desired, + status, + deploymentUid: deployment.metadata.uid, + resourceVersion: deployment.metadata.resourceVersion, + }; +} +export async function reconcileWorkspaceLifecycle(config, input, options = {}) { + if ( + !['running', 'paused'].includes(input?.desired) || + !Number.isSafeInteger(input.revision) || + input.revision < 1 || + Object.keys(input).some(k => !['tenantId', 'projectId', 'workspaceId', 'revision', 'desired'].includes(k)) + ) + throw new Error('INVALID_WORKSPACE_LIFECYCLE_REQUEST'); + const execute = options.execute ?? kubectlJson; + const name = scope(config, input); + const deployment = await execute(config, ['get', 'deployment', name, '-o', 'json']); + assertOwned(config, name, deployment); + const previous = Number(deployment.metadata.annotations?.[revisionKey] ?? 0); + if (!Number.isSafeInteger(previous) || previous > input.revision) throw new Error('STALE_WORKSPACE_REVISION'); + if (previous === input.revision) { + if (deployment.metadata.annotations?.[desiredKey] !== input.desired) throw new Error('WORKSPACE_REVISION_CONFLICT'); + return observeWorkspaceLifecycle(config, input, { execute }); + } + if (!deployment.metadata.resourceVersion || !deployment.metadata.uid) throw new Error('KUBERNETES_LIFECYCLE_UNKNOWN'); + const annotations = { + ...deployment.metadata.annotations, + [revisionKey]: String(input.revision), + [desiredKey]: input.desired, + }; + const patch = [ + { op: 'test', path: '/metadata/resourceVersion', value: deployment.metadata.resourceVersion }, + { op: 'test', path: '/metadata/uid', value: deployment.metadata.uid }, + { op: 'add', path: '/metadata/annotations', value: annotations }, + { op: 'replace', path: '/spec/replicas', value: input.desired === 'paused' ? 0 : 1 }, + ]; + await execute(config, ['patch', 'deployment', name, '--type=json', '-p', JSON.stringify(patch), '-o', 'json']); + return observeWorkspaceLifecycle(config, input, { execute }); +} diff --git a/apps/ide/provisioner/workspaces.mjs b/apps/ide/provisioner/workspaces.mjs index 2acc471..3ccbaa9 100644 --- a/apps/ide/provisioner/workspaces.mjs +++ b/apps/ide/provisioner/workspaces.mjs @@ -5,37 +5,217 @@ const safe = /^[a-zA-Z0-9_-]{1,128}$/; export function renderWorkspaceResources(config, input, image, collaboration) { if (!image || !/^[^\s]+@sha256:[a-f0-9]{64}$/.test(image)) throw new Error('UNCONFIGURED_THEIA_IMAGE'); - if (!input || input.tenantId !== config.tenantId || !safe.test(input.projectId ?? '') || !safe.test(input.workspaceId ?? '') || Object.keys(input).some(k => !['tenantId', 'projectId', 'workspaceId'].includes(k))) throw new Error('INVALID_WORKSPACE_REQUEST'); + if ( + !input || + input.tenantId !== config.tenantId || + !safe.test(input.projectId ?? '') || + !safe.test(input.workspaceId ?? '') || + Object.keys(input).some(k => !['tenantId', 'projectId', 'workspaceId'].includes(k)) + ) + throw new Error('INVALID_WORKSPACE_REQUEST'); const name = `wf-ide-${createHash('sha256').update(`${input.tenantId}/${input.workspaceId}`).digest('hex').slice(0, 24)}`; - const labels = { 'wishforge.io/component': 'ide', 'wishforge.io/workspace': name, 'app.kubernetes.io/managed-by': 'wishforge-provisioner' }; + const labels = { + 'wishforge.io/component': 'ide', + 'wishforge.io/workspace': name, + 'app.kubernetes.io/managed-by': 'wishforge-provisioner', + }; const metadata = n => ({ name: n, namespace: config.namespace, labels }); const resources = [ - { apiVersion: 'v1', kind: 'PersistentVolumeClaim', metadata: metadata(name), spec: { storageClassName: config.storageClass, accessModes: ['ReadWriteOnce'], resources: { requests: { storage: config.storageSize } } } }, - { apiVersion: 'networking.k8s.io/v1', kind: 'NetworkPolicy', metadata: metadata(`${name}-isolation`), spec: { podSelector: { matchLabels: { 'wishforge.io/workspace': name } }, policyTypes: ['Ingress', 'Egress'], ingress: [{ from: [{ namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } }, podSelector: { matchLabels: { 'wishforge.io/component': 'workspace-gateway' } } }], ports: [{ protocol: 'TCP', port: 3000 }] }], egress: [{ to: [{ namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' } }, podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } } }], ports: [{ protocol: 'UDP', port: 53 }, { protocol: 'TCP', port: 53 }] }] } }, - { apiVersion: 'apps/v1', kind: 'Deployment', metadata: metadata(name), spec: { replicas: 1, strategy: { type: 'Recreate' }, selector: { matchLabels: { 'wishforge.io/workspace': name } }, template: { metadata: { labels }, spec: { runtimeClassName: config.runtimeClass, automountServiceAccountToken: false, securityContext: { runAsNonRoot: true, runAsUser: 1000, runAsGroup: 1000, fsGroup: 1000, seccompProfile: { type: 'RuntimeDefault' } }, initContainers: [{ name: 'workspace-directories', image, command: ['node', '-e', "const fs=require('fs');for(const dir of ['workspace','config'])fs.mkdirSync('/home/theia/data/'+dir,{recursive:true});"], securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } }, volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }] }], containers: [{ name: 'theia', image, ports: [{ containerPort: 3000 }], env: [{ name: 'NODE_ENV', value: 'production' }, { name: 'WISHFORGE_GATEWAY_ENFORCED', value: 'true' }, { name: 'WISHFORGE_WORKSPACE_PATH', value: '/home/theia/data/workspace' }, { name: 'THEIA_CONFIG_DIR', value: '/home/theia/data/config' }, { name: 'WISHFORGE_IDE_HOST', value: '0.0.0.0' }, { name: 'WISHFORGE_IDE_PORT', value: '3000' }, { name: 'WISHFORGE_COLLABORATION_ENABLED', value: 'false' }, { name: 'COLLABORATION_SERVER_URL', value: 'https://disabled.invalid/' }], resources: { requests: { cpu: '500m', memory: '1Gi' }, limits: { cpu: '2', memory: '4Gi' } }, securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } }, readinessProbe: { httpGet: { path: '/', port: 3000 }, initialDelaySeconds: 10, periodSeconds: 5 }, volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }] }], volumes: [{ name: 'workspace', persistentVolumeClaim: { claimName: name } }] } } } }, - { apiVersion: 'v1', kind: 'Service', metadata: metadata(name), spec: { selector: { 'wishforge.io/workspace': name }, type: 'ClusterIP', ports: [{ port: 3000, targetPort: 3000 }] } }, + { + apiVersion: 'v1', + kind: 'PersistentVolumeClaim', + metadata: metadata(name), + spec: { + storageClassName: config.storageClass, + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: config.storageSize } }, + }, + }, + { + apiVersion: 'networking.k8s.io/v1', + kind: 'NetworkPolicy', + metadata: metadata(`${name}-isolation`), + spec: { + podSelector: { matchLabels: { 'wishforge.io/workspace': name } }, + policyTypes: ['Ingress', 'Egress'], + ingress: [ + { + from: [ + { + namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } }, + podSelector: { matchLabels: { 'wishforge.io/component': 'workspace-gateway' } }, + }, + ], + ports: [{ protocol: 'TCP', port: 3000 }], + }, + ], + egress: [ + { + to: [ + { + namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'kube-system' } }, + podSelector: { matchLabels: { 'k8s-app': 'kube-dns' } }, + }, + ], + ports: [ + { protocol: 'UDP', port: 53 }, + { protocol: 'TCP', port: 53 }, + ], + }, + ], + }, + }, + { + apiVersion: 'apps/v1', + kind: 'Deployment', + metadata: metadata(name), + spec: { + replicas: 1, + strategy: { type: 'Recreate' }, + selector: { matchLabels: { 'wishforge.io/workspace': name } }, + template: { + metadata: { labels }, + spec: { + runtimeClassName: config.runtimeClass, + automountServiceAccountToken: false, + securityContext: { + runAsNonRoot: true, + runAsUser: 1000, + runAsGroup: 1000, + fsGroup: 1000, + seccompProfile: { type: 'RuntimeDefault' }, + }, + initContainers: [ + { + name: 'workspace-directories', + image, + command: [ + 'node', + '-e', + "const fs=require('fs');for(const dir of ['workspace','config'])fs.mkdirSync('/home/theia/data/'+dir,{recursive:true});", + ], + securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } }, + volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }], + }, + ], + containers: [ + { + name: 'theia', + image, + ports: [{ containerPort: 3000 }], + env: [ + { name: 'NODE_ENV', value: 'production' }, + { name: 'WISHFORGE_GATEWAY_ENFORCED', value: 'true' }, + { name: 'WISHFORGE_WORKSPACE_PATH', value: '/home/theia/data/workspace' }, + { name: 'THEIA_CONFIG_DIR', value: '/home/theia/data/config' }, + { name: 'WISHFORGE_IDE_HOST', value: '0.0.0.0' }, + { name: 'WISHFORGE_IDE_PORT', value: '3000' }, + { name: 'WISHFORGE_COLLABORATION_ENABLED', value: 'false' }, + { name: 'COLLABORATION_SERVER_URL', value: 'https://disabled.invalid/' }, + ], + resources: { requests: { cpu: '500m', memory: '1Gi' }, limits: { cpu: '2', memory: '4Gi' } }, + securityContext: { allowPrivilegeEscalation: false, capabilities: { drop: ['ALL'] } }, + readinessProbe: { httpGet: { path: '/', port: 3000 }, initialDelaySeconds: 10, periodSeconds: 5 }, + volumeMounts: [{ name: 'workspace', mountPath: '/home/theia/data' }], + }, + ], + volumes: [{ name: 'workspace', persistentVolumeClaim: { claimName: name } }], + }, + }, + }, + }, + { + apiVersion: 'v1', + kind: 'Service', + metadata: metadata(name), + spec: { + selector: { 'wishforge.io/workspace': name }, + type: 'ClusterIP', + ports: [{ port: 3000, targetPort: 3000 }], + }, + }, ]; const workspacePod = resources.find(r => r.kind === 'Deployment').spec.template.spec; workspacePod.containers[0].securityContext.readOnlyRootFilesystem = true; - workspacePod.containers[0].env.push({ name: 'HOME', value: '/home/theia/data' }, { name: 'WF_TENANT_ID', value: input.tenantId }, { name: 'WF_WORKSPACE_ID', value: input.workspaceId }); + workspacePod.containers[0].env.push( + { name: 'HOME', value: '/home/theia/data' }, + { name: 'WF_TENANT_ID', value: input.tenantId }, + { name: 'WF_WORKSPACE_ID', value: input.workspaceId }, + ); workspacePod.volumes.push({ name: 'temporary', emptyDir: { sizeLimit: '2Gi' } }); workspacePod.containers[0].volumeMounts.push({ name: 'temporary', mountPath: '/tmp' }); if (config.gatewayPublicKeyConfigMap) { - if (!/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(config.gatewayPublicKeyConfigMap) || config.gatewayPublicKeyConfigMap.length > 63) throw new Error('INVALID_GATEWAY_PUBLIC_KEY_CONFIGMAP'); - resources.unshift({ apiVersion:'v1',kind:'PersistentVolumeClaim',metadata:metadata(`${name}-state`),spec:{storageClassName:config.storageClass,accessModes:['ReadWriteOnce'],resources:{requests:{storage:'1Gi'}}} }); - workspacePod.volumes.push({ name:'coordination-state',persistentVolumeClaim:{claimName:`${name}-state`} },{ name:'gateway-public-key',configMap:{name:config.gatewayPublicKeyConfigMap,items:[{key:'public.pem',path:'public.pem'}]} }); - workspacePod.containers[0].volumeMounts.push({name:'coordination-state',mountPath:'/var/lib/wishforge-coordination'},{name:'gateway-public-key',mountPath:'/run/wishforge-identity',readOnly:true}); - workspacePod.containers[0].env.push({name:'WF_COORDINATION_STATE_DIR',value:'/var/lib/wishforge-coordination'},{name:'WF_IDE_GATEWAY_PUBLIC_KEY_FILE',value:'/run/wishforge-identity/public.pem'}); + if ( + !/^[a-z0-9]([-a-z0-9]*[a-z0-9])?$/.test(config.gatewayPublicKeyConfigMap) || + config.gatewayPublicKeyConfigMap.length > 63 + ) + throw new Error('INVALID_GATEWAY_PUBLIC_KEY_CONFIGMAP'); + resources.unshift({ + apiVersion: 'v1', + kind: 'PersistentVolumeClaim', + metadata: metadata(`${name}-state`), + spec: { + storageClassName: config.storageClass, + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: '1Gi' } }, + }, + }); + workspacePod.volumes.push( + { name: 'coordination-state', persistentVolumeClaim: { claimName: `${name}-state` } }, + { + name: 'gateway-public-key', + configMap: { name: config.gatewayPublicKeyConfigMap, items: [{ key: 'public.pem', path: 'public.pem' }] }, + }, + ); + workspacePod.containers[0].volumeMounts.push( + { name: 'coordination-state', mountPath: '/var/lib/wishforge-coordination' }, + { name: 'gateway-public-key', mountPath: '/run/wishforge-identity', readOnly: true }, + ); + workspacePod.containers[0].env.push( + { name: 'WF_COORDINATION_STATE_DIR', value: '/var/lib/wishforge-coordination' }, + { name: 'WF_IDE_GATEWAY_PUBLIC_KEY_FILE', value: '/run/wishforge-identity/public.pem' }, + ); } let collaborationUpstreamUrl; if (collaboration) { - if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(collaboration.image ?? '')) throw new Error('UNCONFIGURED_OCT_WORKSPACE_IMAGE'); + if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(collaboration.image ?? '')) + throw new Error('UNCONFIGURED_OCT_WORKSPACE_IMAGE'); const gateway = new URL(collaboration.gatewayBase); - if (gateway.protocol !== 'https:' || gateway.username || gateway.password || gateway.pathname !== '/' || gateway.search || gateway.hash) throw new Error('INVALID_PUBLIC_WORKSPACE_GATEWAY'); + if ( + gateway.protocol !== 'https:' || + gateway.username || + gateway.password || + gateway.pathname !== '/' || + gateway.search || + gateway.hash + ) + throw new Error('INVALID_PUBLIC_WORKSPACE_GATEWAY'); const pod = resources.find(r => r.kind === 'Deployment').spec.template.spec; - pod.containers[0].env.push({ name: 'WF_COLLABORATION_GATEWAY_URL', value: `${gateway.origin}/workspaces/${input.workspaceId}/collaboration/` }); - pod.containers.push({ name: 'collaboration', image: collaboration.image, env: [{name:'WF_TENANT_ID',value:input.tenantId},{name:'WF_WORKSPACE_ID',value:input.workspaceId},{name:'WISHFORGE_GATEWAY_ENFORCED',value:'true'}], ports: [{ containerPort: 8100 }], resources: { requests: { cpu: '100m', memory: '128Mi' }, limits: { cpu: '500m', memory: '512Mi' } }, securityContext: { allowPrivilegeEscalation: false, readOnlyRootFilesystem: true, capabilities: { drop: ['ALL'] } }, readinessProbe: { httpGet: { path: '/api/meta', port: 8100 }, initialDelaySeconds: 3, periodSeconds: 5 } }); - resources.find(r => r.kind === 'Service').spec.ports = [{ name: 'theia', port: 3000, targetPort: 3000 }, { name: 'collaboration', port: 8100, targetPort: 8100 }]; + pod.containers[0].env.push({ + name: 'WF_COLLABORATION_GATEWAY_URL', + value: `${gateway.origin}/workspaces/${input.workspaceId}/collaboration/`, + }); + pod.containers.push({ + name: 'collaboration', + image: collaboration.image, + env: [ + { name: 'WF_TENANT_ID', value: input.tenantId }, + { name: 'WF_WORKSPACE_ID', value: input.workspaceId }, + { name: 'WISHFORGE_GATEWAY_ENFORCED', value: 'true' }, + ], + ports: [{ containerPort: 8100 }], + resources: { requests: { cpu: '100m', memory: '128Mi' }, limits: { cpu: '500m', memory: '512Mi' } }, + securityContext: { + allowPrivilegeEscalation: false, + readOnlyRootFilesystem: true, + capabilities: { drop: ['ALL'] }, + }, + readinessProbe: { httpGet: { path: '/api/meta', port: 8100 }, initialDelaySeconds: 3, periodSeconds: 5 }, + }); + resources.find(r => r.kind === 'Service').spec.ports = [ + { name: 'theia', port: 3000, targetPort: 3000 }, + { name: 'collaboration', port: 8100, targetPort: 8100 }, + ]; resources.find(r => r.kind === 'NetworkPolicy').spec.ingress[0].ports.push({ protocol: 'TCP', port: 8100 }); collaborationUpstreamUrl = `http://${name}.${config.namespace}.svc:8100`; } @@ -43,17 +223,62 @@ export function renderWorkspaceResources(config, input, image, collaboration) { } async function observe(config, name) { return new Promise((resolve, reject) => { - const child = spawn('kubectl', ['--context', config.context, '--namespace', config.namespace, 'get', 'deployment', name, '-o', 'json'], { stdio: ['ignore', 'pipe', 'pipe'] }); - let stdout = ''; const timer = setTimeout(() => { child.kill('SIGTERM'); reject(new Error('KUBERNETES_OBSERVE_TIMEOUT')); }, 10000); - child.stderr.resume(); child.stdout.on('data', b => { stdout += b; if (stdout.length > 1000000) child.kill('SIGTERM'); }); - child.on('error', () => { clearTimeout(timer); reject(new Error('KUBERNETES_OBSERVE_FAILED')); }); - child.on('close', code => { clearTimeout(timer); try { if (code !== 0) throw new Error(); const d = JSON.parse(stdout); resolve(d.status?.observedGeneration >= d.metadata.generation && d.status?.availableReplicas === 1 && d.status?.updatedReplicas === 1); } catch { reject(new Error('KUBERNETES_OBSERVE_FAILED')); } }); + const child = spawn( + 'kubectl', + ['--context', config.context, '--namespace', config.namespace, 'get', 'deployment', name, '-o', 'json'], + { stdio: ['ignore', 'pipe', 'pipe'] }, + ); + let stdout = ''; + const timer = setTimeout(() => { + child.kill('SIGTERM'); + reject(new Error('KUBERNETES_OBSERVE_TIMEOUT')); + }, 10000); + child.stderr.resume(); + child.stdout.on('data', b => { + stdout += b; + if (stdout.length > 1000000) child.kill('SIGTERM'); + }); + child.on('error', () => { + clearTimeout(timer); + reject(new Error('KUBERNETES_OBSERVE_FAILED')); + }); + child.on('close', code => { + clearTimeout(timer); + try { + if (code !== 0) throw new Error(); + const d = JSON.parse(stdout); + resolve( + d.status?.observedGeneration >= d.metadata.generation && + d.status?.availableReplicas === 1 && + d.status?.updatedReplicas === 1, + ); + } catch { + reject(new Error('KUBERNETES_OBSERVE_FAILED')); + } + }); }); } export async function provisionWorkspace(config, input, options = {}) { - const collaboration = options.collaboration ?? (process.env.WF_OCT_WORKSPACE_IMAGE ? { image: process.env.WF_OCT_WORKSPACE_IMAGE, gatewayBase: process.env.WF_WORKSPACE_GATEWAY_BASE } : undefined); - const rendered = renderWorkspaceResources({ ...config, gatewayPublicKeyConfigMap: options.gatewayPublicKeyConfigMap ?? process.env.WF_IDE_GATEWAY_PUBLIC_KEY_CONFIGMAP }, input, options.image ?? process.env.WF_THEIA_IMAGE, collaboration); + const collaboration = + options.collaboration ?? + (process.env.WF_OCT_WORKSPACE_IMAGE + ? { image: process.env.WF_OCT_WORKSPACE_IMAGE, gatewayBase: process.env.WF_WORKSPACE_GATEWAY_BASE } + : undefined); + const rendered = renderWorkspaceResources( + { + ...config, + gatewayPublicKeyConfigMap: options.gatewayPublicKeyConfigMap ?? process.env.WF_IDE_GATEWAY_PUBLIC_KEY_CONFIGMAP, + }, + input, + options.image ?? process.env.WF_THEIA_IMAGE, + collaboration, + ); await (options.apply ?? runKubectl)(config, rendered.resources); const ready = await (options.observe ?? observe)(config, rendered.name); - return { status: ready ? 'ready' : 'pending', workspaceId: input.workspaceId, upstreamUrl: rendered.upstreamUrl, collaborationUpstreamUrl: rendered.collaborationUpstreamUrl }; + return { + status: ready ? 'ready' : 'pending', + workspaceId: input.workspaceId, + upstreamUrl: rendered.upstreamUrl, + collaborationUpstreamUrl: rendered.collaborationUpstreamUrl, + }; } diff --git a/apps/ide/scripts/catalog-builtin-plugins.mjs b/apps/ide/scripts/catalog-builtin-plugins.mjs index 4d5a87b..e50d233 100644 --- a/apps/ide/scripts/catalog-builtin-plugins.mjs +++ b/apps/ide/scripts/catalog-builtin-plugins.mjs @@ -7,10 +7,21 @@ const source = { url: 'https://github.com/eclipse-theia/vscode-builtin-extensions/releases/download/1.108.2/vscode-builtin-extensions-1.108.2.tar.gz', sha256: '05a20cf3e671c8d328b04d9ce72b25688b0e762e4a3f5592f31a21ee38280367', }; -if (process.argv.length !== 3) throw new Error('Usage: node scripts/catalog-builtin-plugins.mjs /path/to/vscode-builtin-extensions-1.108.2.tar.gz'); +if (process.argv.length !== 3) + throw new Error('Usage: node scripts/catalog-builtin-plugins.mjs /path/to/vscode-builtin-extensions-1.108.2.tar.gz'); const archive = process.argv[2]; -if (createHash('sha256').update(await readFile(archive)).digest('hex') !== source.sha256) throw new Error('BUILTIN_ARCHIVE_DIGEST_REJECTED'); -const plugins = JSON.parse(execFileSync('python3', ['-c', ` +if ( + createHash('sha256') + .update(await readFile(archive)) + .digest('hex') !== source.sha256 +) + throw new Error('BUILTIN_ARCHIVE_DIGEST_REJECTED'); +const plugins = JSON.parse( + execFileSync( + 'python3', + [ + '-c', + ` import hashlib,io,json,pathlib,sys,tarfile,zipfile plugins=[] with tarfile.open(sys.argv[1],'r:gz') as tar: @@ -23,5 +34,10 @@ with tarfile.open(sys.argv[1],'r:gz') as tar: digest=hashlib.sha256(''.join(name+'\\0'+sha+'\\n' for name,sha in files).encode()).hexdigest() plugins.append({'directory':pathlib.PurePosixPath(item.name).stem,'id':manifest['publisher']+'.'+manifest['name'],'version':manifest['version'],'sha256':digest}) print(json.dumps(sorted(plugins,key=lambda p:p['directory']))) -`, archive], { encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 })); +`, + archive, + ], + { encoding: 'utf8', maxBuffer: 4 * 1024 * 1024 }, + ), +); console.log(JSON.stringify({ schemaVersion: 1, source, plugins }, null, 2)); diff --git a/apps/ide/scripts/download-python-debugger.mjs b/apps/ide/scripts/download-python-debugger.mjs index ec67b9f..eb2ecee 100644 --- a/apps/ide/scripts/download-python-debugger.mjs +++ b/apps/ide/scripts/download-python-debugger.mjs @@ -23,18 +23,34 @@ export async function installPythonDebugger({ const filename = `${lock.id}-${lock.version}@${platform}.vsix`; const dependency = lock.pythonDependency; const python = await installArchive({ - expected: dependency.sha256, filename: `${dependency.id}-${dependency.version}.vsix`, - id: dependency.id, version: dependency.version, url: dependency.url, archive: pythonArchive, directory, fetcher, + expected: dependency.sha256, + filename: `${dependency.id}-${dependency.version}.vsix`, + id: dependency.id, + version: dependency.version, + url: dependency.url, + archive: pythonArchive, + directory, + fetcher, }); const debuggerExtension = await installArchive({ - expected, filename, archive, directory, fetcher, id: lock.id, version: lock.version, + expected, + filename, + archive, + directory, + fetcher, + id: lock.id, + version: lock.version, url: `https://open-vsx.org/api/ms-python/debugpy/${platform}/${lock.version}/file/${filename}`, }); const languageServer = await installArchive({ expected: lock.languageServer.sha256, filename: `${lock.languageServer.id}-${lock.languageServer.version}.vsix`, - id: lock.languageServer.id, version: lock.languageServer.version, - url: lock.languageServer.url, archive: pyrightArchive, directory, fetcher, + id: lock.languageServer.id, + version: lock.languageServer.version, + url: lock.languageServer.url, + archive: pyrightArchive, + directory, + fetcher, }); return { version: lock.version, platform, python, debugger: debuggerExtension, languageServer }; } @@ -48,7 +64,9 @@ async function installArchive({ expected, filename, url, archive, directory, fet bytes = await readFile(target); if (sha256(bytes) !== expected) throw new Error(`Cached Python debugger checksum mismatch: ${target}`); cached = true; - } catch (error) { if (error.code !== 'ENOENT') throw error; } + } catch (error) { + if (error.code !== 'ENOENT') throw error; + } } if (archive) { if ((await stat(archive)).size > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB'); @@ -56,7 +74,8 @@ async function installArchive({ expected, filename, url, archive, directory, fet } else if (!cached) { const response = await fetcher(url, { signal: AbortSignal.timeout(120_000) }); if (!response.ok || !response.body) throw new Error(`Python debugger download failed: HTTP ${response.status}`); - if (Number(response.headers.get('content-length')) > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB'); + if (Number(response.headers.get('content-length')) > maxBytes) + throw new Error('Python debugger archive exceeds 64 MiB'); const chunks = []; let size = 0; for await (const chunk of response.body) { @@ -72,7 +91,9 @@ async function installArchive({ expected, filename, url, archive, directory, fet try { await writeFile(temporary, bytes, { flag: 'wx', mode: 0o644 }); await rename(temporary, target); - } finally { await rm(temporary, { force: true }); } + } finally { + await rm(temporary, { force: true }); + } // Theia's local-dir resolver requires unpacked system plugins. Keep VSIX files // in its explicitly ignored dot-directory and verify before every extraction. const unpacked = join(directory, `.${id}-${randomUUID()}`); @@ -80,7 +101,11 @@ async function installArchive({ expected, filename, url, archive, directory, fet const previous = `${unpacked}-previous`; let moved = false; try { - await run('python3', ['-c', ` + await run( + 'python3', + [ + '-c', + ` import json,os,pathlib,stat,sys,zipfile archive,target,expected_id,expected_version=sys.argv[1:] with zipfile.ZipFile(archive) as z: @@ -94,11 +119,30 @@ with zipfile.ZipFile(archive) as z: z.extractall(target) for item in entries: if not item.is_dir(): os.chmod(pathlib.Path(target)/item.filename, 0o755 if (item.external_attr>>16)&0o111 else 0o644) -`, target, unpacked, id, version], { timeout: 30_000 }); - try { await rename(plugin, previous); moved = true; } catch (error) { if (error.code !== 'ENOENT') throw error; } - try { await rename(unpacked, plugin); } catch (error) { if (moved) await rename(previous, plugin); throw error; } +`, + target, + unpacked, + id, + version, + ], + { timeout: 30_000 }, + ); + try { + await rename(plugin, previous); + moved = true; + } catch (error) { + if (error.code !== 'ENOENT') throw error; + } + try { + await rename(unpacked, plugin); + } catch (error) { + if (moved) await rename(previous, plugin); + throw error; + } await rm(previous, { recursive: true, force: true }); - } finally { await rm(unpacked, { recursive: true, force: true }); } + } finally { + await rm(unpacked, { recursive: true, force: true }); + } return { target: plugin, archive: target, sha256: expected, cached }; } @@ -106,11 +150,31 @@ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) const args = process.argv.slice(2); const flags = new Map(); for (let index = 0; index < args.length; index += 2) flags.set(args[index], args[index + 1]); - if (args.length % 2 || [...flags].some(([key, value]) => !['--archive', '--python-archive', '--pyright-archive'].includes(key) || !value) || flags.size * 2 !== args.length) { - console.error('Usage: node scripts/download-python-debugger.mjs [--archive /path/to/debugpy.vsix] [--python-archive /path/to/python.vsix] [--pyright-archive /path/to/pyright.vsix]'); + if ( + args.length % 2 || + [...flags].some( + ([key, value]) => !['--archive', '--python-archive', '--pyright-archive'].includes(key) || !value, + ) || + flags.size * 2 !== args.length + ) { + console.error( + 'Usage: node scripts/download-python-debugger.mjs [--archive /path/to/debugpy.vsix] [--python-archive /path/to/python.vsix] [--pyright-archive /path/to/pyright.vsix]', + ); process.exitCode = 1; } else { - try { console.log(JSON.stringify(await installPythonDebugger({ archive: flags.get('--archive'), pythonArchive: flags.get('--python-archive'), pyrightArchive: flags.get('--pyright-archive') }))); } - catch (error) { console.error(error.message); process.exitCode = 1; } + try { + console.log( + JSON.stringify( + await installPythonDebugger({ + archive: flags.get('--archive'), + pythonArchive: flags.get('--python-archive'), + pyrightArchive: flags.get('--pyright-archive'), + }), + ), + ); + } catch (error) { + console.error(error.message); + process.exitCode = 1; + } } } diff --git a/apps/ide/scripts/start.mjs b/apps/ide/scripts/start.mjs index 5607919..5074659 100644 --- a/apps/ide/scripts/start.mjs +++ b/apps/ide/scripts/start.mjs @@ -23,20 +23,29 @@ const hostname = process.env.WISHFORGE_IDE_HOST ?? '127.0.0.1'; const workspace = process.env.WISHFORGE_WORKSPACE_PATH ?? `${appRoot}workspace`; if (!process.env.WISHFORGE_WORKSPACE_PATH) mkdirSync(workspace, { recursive: true }); if (!existsSync(workspace)) throw new Error('Configured workspace path does not exist'); -const child = spawn(process.execPath, [cli, 'start', workspace, '--hostname', hostname, '--port', port, '--plugins', `local-dir:${appRoot}plugins`], { - cwd: appRoot, - detached: process.platform !== 'win32', - stdio: 'inherit', - env: { - ...process.env, - PYTHONDONTWRITEBYTECODE: '1', - WF_IDE_MANAGED_PLUGINS_DIR: `${appRoot}plugins`, - // No public/default OCT fallback while project-bound room admission is absent. - COLLABORATION_SERVER_URL: process.env.WF_COLLABORATION_GATEWAY_URL ?? 'https://collaboration-disabled.invalid/', +const child = spawn( + process.execPath, + [cli, 'start', workspace, '--hostname', hostname, '--port', port, '--plugins', `local-dir:${appRoot}plugins`], + { + cwd: appRoot, + detached: process.platform !== 'win32', + stdio: 'inherit', + env: { + ...process.env, + PYTHONDONTWRITEBYTECODE: '1', + WF_IDE_MANAGED_PLUGINS_DIR: `${appRoot}plugins`, + // No public/default OCT fallback while project-bound room admission is absent. + COLLABORATION_SERVER_URL: process.env.WF_COLLABORATION_GATEWAY_URL ?? 'https://collaboration-disabled.invalid/', + }, }, -}); -for (const signal of ['SIGINT', 'SIGTERM']) process.on(signal, () => { - try { if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, signal); else child.kill(signal); } - catch { child.kill(signal); } -}); -child.on('exit', (code) => process.exit(code ?? 1)); +); +for (const signal of ['SIGINT', 'SIGTERM']) + process.on(signal, () => { + try { + if (process.platform !== 'win32' && child.pid) process.kill(-child.pid, signal); + else child.kill(signal); + } catch { + child.kill(signal); + } + }); +child.on('exit', code => process.exit(code ?? 1)); diff --git a/apps/ide/security/assertions.cjs b/apps/ide/security/assertions.cjs index e7462c4..09a5311 100644 --- a/apps/ide/security/assertions.cjs +++ b/apps/ide/security/assertions.cjs @@ -1,21 +1,55 @@ const { createPrivateKey, createPublicKey, sign, verify } = require('node:crypto'); -const roles = new Set(['owner','admin','developer']); +const roles = new Set(['owner', 'admin', 'developer']); function signAssertion(privateKey, grant, now = Date.now()) { if (!grant.userId || !roles.has(grant.role)) throw new Error('Workspace actor is not authorized'); - const body = Buffer.from(JSON.stringify({ aud:'wishforge-workspace', purpose:grant.purpose??'interactive', tenantId:grant.tenantId, workspaceId:grant.workspaceId, actorId:grant.userId, role:grant.role, iat:Math.floor(now/1000), exp:Math.floor(now/1000)+900 })).toString('base64url'); - return `${body}.${sign(null,Buffer.from(body),createPrivateKey(privateKey)).toString('base64url')}`; + const body = Buffer.from( + JSON.stringify({ + aud: 'wishforge-workspace', + purpose: grant.purpose ?? 'interactive', + tenantId: grant.tenantId, + workspaceId: grant.workspaceId, + actorId: grant.userId, + role: grant.role, + iat: Math.floor(now / 1000), + exp: Math.floor(now / 1000) + 900, + }), + ).toString('base64url'); + return `${body}.${sign(null, Buffer.from(body), createPrivateKey(privateKey)).toString('base64url')}`; } function verifyAssertion(publicKey, value, scope, now = Date.now()) { - if(typeof value!=='string'||value.length>4096)throw new Error('WORKSPACE_IDENTITY_REQUIRED'); - const parts=value.split('.'); - if(parts.length!==2||parts.some(part=>!part||!/^[A-Za-z0-9_-]+$/.test(part)||Buffer.from(part,'base64url').toString('base64url')!==part)||Buffer.from(parts[1],'base64url').length!==64)throw new Error('WORKSPACE_IDENTITY_INVALID'); - const [body, signature]=parts; - const key=createPublicKey(publicKey); - if(key.asymmetricKeyType!=='ed25519'||!verify(null,Buffer.from(body),key,Buffer.from(signature,'base64url')))throw new Error('WORKSPACE_IDENTITY_INVALID'); - const claims=JSON.parse(Buffer.from(body,'base64url').toString()); - const time=Math.floor(now/1000); - if((claims.purpose??'interactive')!==(scope.purpose??'interactive'))throw new Error('WORKSPACE_IDENTITY_PURPOSE'); - if(claims.aud!=='wishforge-workspace'||claims.tenantId!==scope.tenantId||claims.workspaceId!==scope.workspaceId||typeof claims.actorId!=='string'||!claims.actorId||!roles.has(claims.role)||!Number.isInteger(claims.iat)||!Number.isInteger(claims.exp)||claims.iat>time+30||claims.exp<=time||claims.exp<=claims.iat||claims.exp-claims.iat>900)throw new Error('WORKSPACE_IDENTITY_SCOPE'); + if (typeof value !== 'string' || value.length > 4096) throw new Error('WORKSPACE_IDENTITY_REQUIRED'); + const parts = value.split('.'); + if ( + parts.length !== 2 || + parts.some( + part => !part || !/^[A-Za-z0-9_-]+$/.test(part) || Buffer.from(part, 'base64url').toString('base64url') !== part, + ) || + Buffer.from(parts[1], 'base64url').length !== 64 + ) + throw new Error('WORKSPACE_IDENTITY_INVALID'); + const [body, signature] = parts; + const key = createPublicKey(publicKey); + if (key.asymmetricKeyType !== 'ed25519' || !verify(null, Buffer.from(body), key, Buffer.from(signature, 'base64url'))) + throw new Error('WORKSPACE_IDENTITY_INVALID'); + const claims = JSON.parse(Buffer.from(body, 'base64url').toString()); + const time = Math.floor(now / 1000); + if ((claims.purpose ?? 'interactive') !== (scope.purpose ?? 'interactive')) + throw new Error('WORKSPACE_IDENTITY_PURPOSE'); + if ( + claims.aud !== 'wishforge-workspace' || + claims.tenantId !== scope.tenantId || + claims.workspaceId !== scope.workspaceId || + typeof claims.actorId !== 'string' || + !claims.actorId || + !roles.has(claims.role) || + !Number.isInteger(claims.iat) || + !Number.isInteger(claims.exp) || + claims.iat > time + 30 || + claims.exp <= time || + claims.exp <= claims.iat || + claims.exp - claims.iat > 900 + ) + throw new Error('WORKSPACE_IDENTITY_SCOPE'); return claims; } -module.exports={signAssertion,verifyAssertion}; +module.exports = { signAssertion, verifyAssertion }; diff --git a/apps/ide/security/create-local-keys.cjs b/apps/ide/security/create-local-keys.cjs index f04ae8b..85a68b2 100644 --- a/apps/ide/security/create-local-keys.cjs +++ b/apps/ide/security/create-local-keys.cjs @@ -1,10 +1,18 @@ -const fs=require('node:fs');const path=require('node:path');const {generateKeyPairSync,createPrivateKey,createPublicKey}=require('node:crypto'); -const directory=path.resolve(process.argv[2]??'.wishforge/gateway-keys'); -fs.mkdirSync(directory,{recursive:true,mode:0o700}); -const privateFile=path.join(directory,'private.pem'),publicFile=path.join(directory,'public.pem'); -if(!fs.existsSync(privateFile)){const keys=generateKeyPairSync('ed25519');fs.writeFileSync(privateFile,keys.privateKey.export({type:'pkcs8',format:'pem'}),{flag:'wx',mode:0o600});} -const privateKey=createPrivateKey(fs.readFileSync(privateFile));if(privateKey.asymmetricKeyType!=='ed25519')throw new Error('Existing gateway key is not Ed25519'); -const publicPem=createPublicKey(privateKey).export({type:'spki',format:'pem'}); -if(fs.existsSync(publicFile)&&fs.readFileSync(publicFile,'utf8')!==publicPem)throw new Error('Existing public key does not match; inspect key rotation explicitly'); -if(!fs.existsSync(publicFile))fs.writeFileSync(publicFile,publicPem,{flag:'wx',mode:0o644}); -console.log(JSON.stringify({privateKeyFile:privateFile,publicKeyFile:publicFile})); +const fs = require('node:fs'); +const path = require('node:path'); +const { generateKeyPairSync, createPrivateKey, createPublicKey } = require('node:crypto'); +const directory = path.resolve(process.argv[2] ?? '.wishforge/gateway-keys'); +fs.mkdirSync(directory, { recursive: true, mode: 0o700 }); +const privateFile = path.join(directory, 'private.pem'), + publicFile = path.join(directory, 'public.pem'); +if (!fs.existsSync(privateFile)) { + const keys = generateKeyPairSync('ed25519'); + fs.writeFileSync(privateFile, keys.privateKey.export({ type: 'pkcs8', format: 'pem' }), { flag: 'wx', mode: 0o600 }); +} +const privateKey = createPrivateKey(fs.readFileSync(privateFile)); +if (privateKey.asymmetricKeyType !== 'ed25519') throw new Error('Existing gateway key is not Ed25519'); +const publicPem = createPublicKey(privateKey).export({ type: 'spki', format: 'pem' }); +if (fs.existsSync(publicFile) && fs.readFileSync(publicFile, 'utf8') !== publicPem) + throw new Error('Existing public key does not match; inspect key rotation explicitly'); +if (!fs.existsSync(publicFile)) fs.writeFileSync(publicFile, publicPem, { flag: 'wx', mode: 0o644 }); +console.log(JSON.stringify({ privateKeyFile: privateFile, publicKeyFile: publicFile })); diff --git a/apps/ide/security/test/assertions.test.cjs b/apps/ide/security/test/assertions.test.cjs index 2460446..d18a30c 100644 --- a/apps/ide/security/test/assertions.test.cjs +++ b/apps/ide/security/test/assertions.test.cjs @@ -12,7 +12,16 @@ const time = 1_725_000_000_000; const scope = { tenantId: 'tenant-one', workspaceId: 'workspace-one' }; const grant = { ...scope, userId: 'actor-alice', role: 'developer' }; function claims(overrides = {}) { - return { aud: 'wishforge-workspace', purpose: 'interactive', ...scope, actorId: grant.userId, role: grant.role, iat: Math.floor(time / 1000), exp: Math.floor(time / 1000) + 900, ...overrides }; + return { + aud: 'wishforge-workspace', + purpose: 'interactive', + ...scope, + actorId: grant.userId, + role: grant.role, + iat: Math.floor(time / 1000), + exp: Math.floor(time / 1000) + 900, + ...overrides, + }; } function signedClaims(value) { const body = Buffer.from(JSON.stringify(value)).toString('base64url'); @@ -30,29 +39,63 @@ test('body tampering, forged signature and untrusted signing keys are rejected', const token = signAssertion(privateKey, grant, time); const [body, signature] = token.split('.'); const tampered = Buffer.from(JSON.stringify(claims({ actorId: 'attacker' }))).toString('base64url'); - assert.throws(() => verifyAssertion(publicKey, `${tampered}.${signature}`, scope, time), /WORKSPACE_IDENTITY_INVALID/); + assert.throws( + () => verifyAssertion(publicKey, `${tampered}.${signature}`, scope, time), + /WORKSPACE_IDENTITY_INVALID/, + ); const forgedSignature = Buffer.from(signature, 'base64url'); forgedSignature[0] ^= 1; - assert.throws(() => verifyAssertion(publicKey, `${body}.${forgedSignature.toString('base64url')}`, scope, time), /WORKSPACE_IDENTITY_INVALID/); + assert.throws( + () => verifyAssertion(publicKey, `${body}.${forgedSignature.toString('base64url')}`, scope, time), + /WORKSPACE_IDENTITY_INVALID/, + ); const untrusted = generateKeyPairSync('ed25519'); - assert.throws(() => verifyAssertion(untrusted.publicKey.export({ type: 'spki', format: 'pem' }), token, scope, time), /WORKSPACE_IDENTITY_INVALID/); + assert.throws( + () => verifyAssertion(untrusted.publicKey.export({ type: 'spki', format: 'pem' }), token, scope, time), + /WORKSPACE_IDENTITY_INVALID/, + ); }); test('expiry boundary, future issue time and overlong assertion lifetime fail closed', () => { const token = signAssertion(privateKey, grant, time); assert.equal(verifyAssertion(publicKey, token, scope, time + 899_999).actorId, grant.userId); assert.throws(() => verifyAssertion(publicKey, token, scope, time + 900_000), /WORKSPACE_IDENTITY_SCOPE/); - assert.throws(() => verifyAssertion(publicKey, signedClaims(claims({ iat: Math.floor(time / 1000) + 31 })), scope, time), /WORKSPACE_IDENTITY_SCOPE/); - assert.throws(() => verifyAssertion(publicKey, signedClaims(claims({ exp: Math.floor(time / 1000) + 901 })), scope, time), /WORKSPACE_IDENTITY_SCOPE/); - assert.throws(() => verifyAssertion(publicKey, signedClaims(claims({ exp: 'later' })), scope, time), /WORKSPACE_IDENTITY_SCOPE/); + assert.throws( + () => verifyAssertion(publicKey, signedClaims(claims({ iat: Math.floor(time / 1000) + 31 })), scope, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); + assert.throws( + () => verifyAssertion(publicKey, signedClaims(claims({ exp: Math.floor(time / 1000) + 901 })), scope, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); + assert.throws( + () => verifyAssertion(publicKey, signedClaims(claims({ exp: 'later' })), scope, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); }); test('valid signatures cannot cross tenant, workspace, audience or role boundaries', () => { const token = signAssertion(privateKey, grant, time); - assert.throws(() => verifyAssertion(publicKey, token, { ...scope, tenantId: 'tenant-two' }, time), /WORKSPACE_IDENTITY_SCOPE/); - assert.throws(() => verifyAssertion(publicKey, token, { ...scope, workspaceId: 'workspace-two' }, time), /WORKSPACE_IDENTITY_SCOPE/); - for (const overrides of [{ aud: 'another-service' }, { role: 'viewer' }, { role: 'deployer' }, { role: 'root' }, { actorId: '' }, { actorId: 5 }]) { - assert.throws(() => verifyAssertion(publicKey, signedClaims(claims(overrides)), scope, time), /WORKSPACE_IDENTITY_SCOPE/); + assert.throws( + () => verifyAssertion(publicKey, token, { ...scope, tenantId: 'tenant-two' }, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); + assert.throws( + () => verifyAssertion(publicKey, token, { ...scope, workspaceId: 'workspace-two' }, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); + for (const overrides of [ + { aud: 'another-service' }, + { role: 'viewer' }, + { role: 'deployer' }, + { role: 'root' }, + { actorId: '' }, + { actorId: 5 }, + ]) { + assert.throws( + () => verifyAssertion(publicKey, signedClaims(claims(overrides)), scope, time), + /WORKSPACE_IDENTITY_SCOPE/, + ); } assert.throws(() => signAssertion(privateKey, { ...grant, role: 'viewer' }, time), /not authorized/); assert.throws(() => signAssertion(privateKey, { ...grant, userId: '' }, time), /not authorized/); @@ -61,21 +104,52 @@ test('valid signatures cannot cross tenant, workspace, audience or role boundari test('assertions require exactly two canonical base64url segments', () => { const token = signAssertion(privateKey, grant, time); const [body, signature] = token.split('.'); - for (const value of [token + '.', token + '..', token + '!!!!', token + '=', `${body}. ${signature}`, `${body}.${signature}.extra`, `${body}.`, `.${signature}`, '', undefined, 5, 'x'.repeat(4097)]) { - assert.throws(() => verifyAssertion(publicKey, value, scope, time), undefined, `Accepted malformed assertion ${String(value).slice(-20)}`); + for (const value of [ + token + '.', + token + '..', + token + '!!!!', + token + '=', + `${body}. ${signature}`, + `${body}.${signature}.extra`, + `${body}.`, + `.${signature}`, + '', + undefined, + 5, + 'x'.repeat(4097), + ]) { + assert.throws( + () => verifyAssertion(publicKey, value, scope, time), + undefined, + `Accepted malformed assertion ${String(value).slice(-20)}`, + ); } }); test('a non-Ed25519 public key cannot authorize an otherwise valid assertion', () => { const token = signAssertion(privateKey, grant, time); const other = generateKeyPairSync('ec', { namedCurve: 'prime256v1' }); - assert.throws(() => verifyAssertion(other.publicKey.export({ type: 'spki', format: 'pem' }), token, scope, time), /WORKSPACE_IDENTITY_INVALID/); + assert.throws( + () => verifyAssertion(other.publicKey.export({ type: 'spki', format: 'pem' }), token, scope, time), + /WORKSPACE_IDENTITY_INVALID/, + ); }); - test('integration inspection assertion cannot invoke interactive coordination or debug', () => { const token = signAssertion(privateKey, { ...grant, purpose: 'integration-state' }, time); - assert.equal(verifyAssertion(publicKey, token, { ...scope, purpose: 'integration-state' }, time).purpose, 'integration-state'); + assert.equal( + verifyAssertion(publicKey, token, { ...scope, purpose: 'integration-state' }, time).purpose, + 'integration-state', + ); assert.throws(() => verifyAssertion(publicKey, token, scope, time), /WORKSPACE_IDENTITY_PURPOSE/); - assert.throws(() => verifyAssertion(publicKey, signAssertion(privateKey, grant, time), { ...scope, purpose: 'integration-state' }, time), /WORKSPACE_IDENTITY_PURPOSE/); + assert.throws( + () => + verifyAssertion( + publicKey, + signAssertion(privateKey, grant, time), + { ...scope, purpose: 'integration-state' }, + time, + ), + /WORKSPACE_IDENTITY_PURPOSE/, + ); }); diff --git a/apps/preview-gateway/main.mjs b/apps/preview-gateway/main.mjs index ae61376..9cec922 100644 --- a/apps/preview-gateway/main.mjs +++ b/apps/preview-gateway/main.mjs @@ -1,6 +1,24 @@ import { readFile } from 'node:fs/promises'; import { createPreviewGateway } from './server.mjs'; -const required=name=>{const value=process.env[name];if(!value)throw new Error(`Unconfigured: ${name}`);return value;}; -const gateway=createPreviewGateway({tenantId:required('WF_PREVIEW_TENANT_ID'),environmentId:required('WF_PREVIEW_ENVIRONMENT_ID'),projectId:required('WF_PREVIEW_PROJECT_ID'),publicOrigin:required('WF_PREVIEW_PUBLIC_ORIGIN'),upstreamOrigin:required('WF_PREVIEW_UPSTREAM_ORIGIN'),apiOrigin:required('WF_API_PUBLIC_URL'),serviceToken:required('WF_PREVIEW_GATEWAY_TOKEN'),proofPrivateKey:await readFile(required('WF_PREVIEW_PROOF_PRIVATE_KEY_FILE'),'utf8'),production:process.env.NODE_ENV==='production'}); -gateway.server.listen(Number(process.env.PORT??4460),process.env.HOST??'127.0.0.1',()=>console.log('Private preview gateway started with fixed environment routing.')); -const close=()=>void gateway.close();process.once('SIGINT',close);process.once('SIGTERM',close); +const required = name => { + const value = process.env[name]; + if (!value) throw new Error(`Unconfigured: ${name}`); + return value; +}; +const gateway = createPreviewGateway({ + tenantId: required('WF_PREVIEW_TENANT_ID'), + environmentId: required('WF_PREVIEW_ENVIRONMENT_ID'), + projectId: required('WF_PREVIEW_PROJECT_ID'), + publicOrigin: required('WF_PREVIEW_PUBLIC_ORIGIN'), + upstreamOrigin: required('WF_PREVIEW_UPSTREAM_ORIGIN'), + apiOrigin: required('WF_API_PUBLIC_URL'), + serviceToken: required('WF_PREVIEW_GATEWAY_TOKEN'), + proofPrivateKey: await readFile(required('WF_PREVIEW_PROOF_PRIVATE_KEY_FILE'), 'utf8'), + production: process.env.NODE_ENV === 'production', +}); +gateway.server.listen(Number(process.env.PORT ?? 4460), process.env.HOST ?? '127.0.0.1', () => + console.log('Private preview gateway started with fixed environment routing.'), +); +const close = () => void gateway.close(); +process.once('SIGINT', close); +process.once('SIGTERM', close); diff --git a/apps/preview-gateway/package.json b/apps/preview-gateway/package.json index 691804a..7b38e19 100644 --- a/apps/preview-gateway/package.json +++ b/apps/preview-gateway/package.json @@ -1 +1,13 @@ -{"name":"@wishforge/preview-gateway","version":"0.1.0","private":true,"type":"module","engines":{"node":">=22"},"scripts":{"start":"node main.mjs","test":"node --test test/*.test.mjs"}} +{ + "name": "@wishforge/preview-gateway", + "version": "0.1.0", + "private": true, + "type": "module", + "engines": { + "node": ">=22" + }, + "scripts": { + "start": "node main.mjs", + "test": "node --test test/*.test.mjs" + } +} diff --git a/apps/preview-gateway/proof.mjs b/apps/preview-gateway/proof.mjs index d15856a..90be5e1 100644 --- a/apps/preview-gateway/proof.mjs +++ b/apps/preview-gateway/proof.mjs @@ -3,15 +3,29 @@ export const bodySha256 = value => createHash('sha256').update(value).digest('he export function signPreviewProof(privateKey, payload) { if (createPrivateKey(privateKey).asymmetricKeyType !== 'ed25519') throw new Error('Ed25519 proof key required'); const encoded = Buffer.from(JSON.stringify(payload)).toString('base64url'); - return `${encoded}.${sign(null,Buffer.from(encoded),privateKey).toString('base64url')}`; + return `${encoded}.${sign(null, Buffer.from(encoded), privateKey).toString('base64url')}`; } -export function verifyPreviewProof(publicKey, proof, expected, now=Date.now()) { +export function verifyPreviewProof(publicKey, proof, expected, now = Date.now()) { try { - if (typeof proof!=='string'||proof.length>8192||createPublicKey(publicKey).asymmetricKeyType!=='ed25519') return undefined; - const parts=proof.split('.');if(parts.length!==2||!verify(null,Buffer.from(parts[0]),publicKey,Buffer.from(parts[1],'base64url')))return undefined; - const value=JSON.parse(Buffer.from(parts[0],'base64url').toString()); - if(value.schemaVersion!==1||!Number.isSafeInteger(value.issuedAt)||value.issuedAt>now+5000||now-value.issuedAt>120000||!Number.isSafeInteger(value.expiresAt)||value.expiresAt<=now||value.expiresAt-value.issuedAt>120000)return undefined; - for(const [key,wanted]of Object.entries(expected))if(value[key]!==wanted)return undefined; + if (typeof proof !== 'string' || proof.length > 8192 || createPublicKey(publicKey).asymmetricKeyType !== 'ed25519') + return undefined; + const parts = proof.split('.'); + if (parts.length !== 2 || !verify(null, Buffer.from(parts[0]), publicKey, Buffer.from(parts[1], 'base64url'))) + return undefined; + const value = JSON.parse(Buffer.from(parts[0], 'base64url').toString()); + if ( + value.schemaVersion !== 1 || + !Number.isSafeInteger(value.issuedAt) || + value.issuedAt > now + 5000 || + now - value.issuedAt > 120000 || + !Number.isSafeInteger(value.expiresAt) || + value.expiresAt <= now || + value.expiresAt - value.issuedAt > 120000 + ) + return undefined; + for (const [key, wanted] of Object.entries(expected)) if (value[key] !== wanted) return undefined; return value; - }catch{return undefined;} + } catch { + return undefined; + } } diff --git a/apps/preview-gateway/server.mjs b/apps/preview-gateway/server.mjs index 2c309c0..767d751 100644 --- a/apps/preview-gateway/server.mjs +++ b/apps/preview-gateway/server.mjs @@ -3,58 +3,469 @@ import https from 'node:https'; import { createPrivateKey } from 'node:crypto'; import { bodySha256, signPreviewProof } from './proof.mjs'; -const ID=/^[A-Za-z0-9_-]{1,128}$/; -const CAP=/^[A-Za-z0-9_-]{43}$/; -const CHALLENGE=/^[A-Za-z0-9_-]{32,128}$/; -const HOP=new Set(['connection','keep-alive','proxy-authenticate','proxy-authorization','te','trailer','transfer-encoding','upgrade']); -class Failure extends Error{constructor(status,message){super(message);this.status=status;}} -export function createPreviewGateway(config){ - const publicUrl=new URL(config.publicOrigin),upstream=new URL(config.upstreamOrigin),api=new URL(config.apiOrigin); - for(const value of[publicUrl,upstream,api])if(!['http:','https:'].includes(value.protocol)||value.username||value.password||value.pathname!=='/'||value.search||value.hash)throw new Error('Explicit credential-free origins required'); - if(config.production!==false&&(publicUrl.protocol!=='https:'||api.protocol!=='https:'))throw new Error('Production public and API origins require HTTPS'); - if(config.production===false&&publicUrl.protocol==='http:'&&!['127.0.0.1','localhost','[::1]'].includes(publicUrl.hostname))throw new Error('Development plaintext preview is loopback only'); - if(publicUrl.origin===upstream.origin||publicUrl.hostname===api.hostname||!ID.test(config.tenantId)||!ID.test(config.environmentId)||!ID.test(config.projectId)||typeof config.serviceToken!=='string'||config.serviceToken.length<32||createPrivateKey(config.proofPrivateKey).asymmetricKeyType!=='ed25519')throw new Error('Scoped preview configuration and independent Ed25519 key required'); - const fetcher=config.fetchImpl??fetch,secure=publicUrl.protocol==='https:',cookieName=secure?'__Host-WF_PREVIEW_SESSION':'WF_PREVIEW_SESSION'; - const pollMs=Math.max(20,Math.min(config.reauthorizeMs??3000,5000)),active=new Set(),sockets=new Set(); - function host(req){if(req.headers.host!==publicUrl.host)throw new Failure(421,'Unknown preview host');} - function origin(req,required=false){if(required&&!req.headers.origin||req.headers.origin&&req.headers.origin!==publicUrl.origin)throw new Failure(403,'Preview origin denied');} - function route(req){if(!req.url?.startsWith('/')||req.url.startsWith('//')||req.url.includes('\\'))throw new Failure(400,'Invalid preview path');const url=new URL(req.url,publicUrl);if(url.origin!==publicUrl.origin)throw new Failure(400,'Invalid preview path');return url;} - function credential(req){const values=(req.headers.cookie??'').split(';').map(value=>value.trim()).filter(value=>value.startsWith(`${cookieName}=`));if(values.length!==1)throw new Failure(401,'Private preview authentication required');const value=values[0].slice(cookieName.length+1);if(!CAP.test(value))throw new Failure(401,'Private preview authentication required');return value;} - async function authorize(kind,value,method,path,challenge){ - if(!CAP.test(value??''))throw new Failure(401,'Private preview authentication required'); - let response;try{response=await fetcher(`${api.origin}/preview-control/authorize`,{method:'POST',headers:{authorization:`Bearer ${config.serviceToken}`,'content-type':'application/json'},body:JSON.stringify({tenantId:config.tenantId,environmentId:config.environmentId,kind,credential:value,method,path,...(kind==='probe'?{challenge}:{})}),redirect:'error',signal:AbortSignal.timeout(3000)});}catch{throw new Failure(503,'Preview authorization unavailable');} - if(!response.ok)throw new Failure([401,403,404].includes(response.status)?response.status:503,'Private preview authorization denied'); - const payload=await response.json(),grant=payload.data??payload; - if(grant.tenantId!==config.tenantId||grant.environmentId!==config.environmentId||grant.projectId!==config.projectId||grant.publicOrigin!==publicUrl.origin||!grant.releaseId||!grant.bindingDigest||!Number.isFinite(Date.parse(grant.expiresAt))||Date.parse(grant.expiresAt)<=Date.now()||kind==='probe'&&grant.role!=='probe'||kind!=='probe'&&!['owner','admin','developer','deployer','viewer'].includes(grant.role))throw new Failure(403,'Preview grant mismatch'); - let destination;try{destination=new URL(grant.applicationUrl);}catch{throw new Failure(403,'Preview application URL missing');}if(destination.origin!==publicUrl.origin||destination.username||destination.password||destination.pathname.startsWith('/_wishforge/'))throw new Failure(403,'Preview application URL mismatch'); - if(grant.role==='viewer'&&!['GET','HEAD'].includes(method))throw new Failure(403,'Viewer access is read-only'); +const ID = /^[A-Za-z0-9_-]{1,128}$/; +const CAP = /^[A-Za-z0-9_-]{43}$/; +const CHALLENGE = /^[A-Za-z0-9_-]{32,128}$/; +const HOP = new Set([ + 'connection', + 'keep-alive', + 'proxy-authenticate', + 'proxy-authorization', + 'te', + 'trailer', + 'transfer-encoding', + 'upgrade', +]); +class Failure extends Error { + constructor(status, message) { + super(message); + this.status = status; + } +} +export function createPreviewGateway(config) { + const publicUrl = new URL(config.publicOrigin), + upstream = new URL(config.upstreamOrigin), + api = new URL(config.apiOrigin); + for (const value of [publicUrl, upstream, api]) + if ( + !['http:', 'https:'].includes(value.protocol) || + value.username || + value.password || + value.pathname !== '/' || + value.search || + value.hash + ) + throw new Error('Explicit credential-free origins required'); + if (config.production !== false && (publicUrl.protocol !== 'https:' || api.protocol !== 'https:')) + throw new Error('Production public and API origins require HTTPS'); + if ( + config.production === false && + publicUrl.protocol === 'http:' && + !['127.0.0.1', 'localhost', '[::1]'].includes(publicUrl.hostname) + ) + throw new Error('Development plaintext preview is loopback only'); + if ( + publicUrl.origin === upstream.origin || + publicUrl.hostname === api.hostname || + !ID.test(config.tenantId) || + !ID.test(config.environmentId) || + !ID.test(config.projectId) || + typeof config.serviceToken !== 'string' || + config.serviceToken.length < 32 || + createPrivateKey(config.proofPrivateKey).asymmetricKeyType !== 'ed25519' + ) + throw new Error('Scoped preview configuration and independent Ed25519 key required'); + const fetcher = config.fetchImpl ?? fetch, + secure = publicUrl.protocol === 'https:', + cookieName = secure ? '__Host-WF_PREVIEW_SESSION' : 'WF_PREVIEW_SESSION'; + const pollMs = Math.max(20, Math.min(config.reauthorizeMs ?? 3000, 5000)), + active = new Set(), + sockets = new Set(); + function host(req) { + if (req.headers.host !== publicUrl.host) throw new Failure(421, 'Unknown preview host'); + } + function origin(req, required = false) { + if ((required && !req.headers.origin) || (req.headers.origin && req.headers.origin !== publicUrl.origin)) + throw new Failure(403, 'Preview origin denied'); + } + function route(req) { + if (!req.url?.startsWith('/') || req.url.startsWith('//') || req.url.includes('\\')) + throw new Failure(400, 'Invalid preview path'); + const url = new URL(req.url, publicUrl); + if (url.origin !== publicUrl.origin) throw new Failure(400, 'Invalid preview path'); + return url; + } + function credential(req) { + const values = (req.headers.cookie ?? '') + .split(';') + .map(value => value.trim()) + .filter(value => value.startsWith(`${cookieName}=`)); + if (values.length !== 1) throw new Failure(401, 'Private preview authentication required'); + const value = values[0].slice(cookieName.length + 1); + if (!CAP.test(value)) throw new Failure(401, 'Private preview authentication required'); + return value; + } + async function authorize(kind, value, method, path, challenge) { + if (!CAP.test(value ?? '')) throw new Failure(401, 'Private preview authentication required'); + let response; + try { + response = await fetcher(`${api.origin}/preview-control/authorize`, { + method: 'POST', + headers: { authorization: `Bearer ${config.serviceToken}`, 'content-type': 'application/json' }, + body: JSON.stringify({ + tenantId: config.tenantId, + environmentId: config.environmentId, + kind, + credential: value, + method, + path, + ...(kind === 'probe' ? { challenge } : {}), + }), + redirect: 'error', + signal: AbortSignal.timeout(3000), + }); + } catch { + throw new Failure(503, 'Preview authorization unavailable'); + } + if (!response.ok) + throw new Failure( + [401, 403, 404].includes(response.status) ? response.status : 503, + 'Private preview authorization denied', + ); + const payload = await response.json(), + grant = payload.data ?? payload; + if ( + grant.tenantId !== config.tenantId || + grant.environmentId !== config.environmentId || + grant.projectId !== config.projectId || + grant.publicOrigin !== publicUrl.origin || + !grant.releaseId || + !grant.bindingDigest || + !Number.isFinite(Date.parse(grant.expiresAt)) || + Date.parse(grant.expiresAt) <= Date.now() || + (kind === 'probe' && grant.role !== 'probe') || + (kind !== 'probe' && !['owner', 'admin', 'developer', 'deployer', 'viewer'].includes(grant.role)) + ) + throw new Failure(403, 'Preview grant mismatch'); + let destination; + try { + destination = new URL(grant.applicationUrl); + } catch { + throw new Failure(403, 'Preview application URL missing'); + } + if ( + destination.origin !== publicUrl.origin || + destination.username || + destination.password || + destination.pathname.startsWith('/_wishforge/') + ) + throw new Failure(403, 'Preview application URL mismatch'); + if (grant.role === 'viewer' && !['GET', 'HEAD'].includes(method)) + throw new Failure(403, 'Viewer access is read-only'); return grant; } - function baseHeaders(){return{'cache-control':'no-store','referrer-policy':'no-referrer','x-content-type-options':'nosniff','origin-agent-cluster':'?1','cross-origin-opener-policy':'same-origin'};} - function proof(req,grant,decision,status,body){const challenge=req.headers['x-wishforge-preview-challenge'];if(!CHALLENGE.test(challenge??''))return undefined;const issuedAt=Date.now();return signPreviewProof(config.proofPrivateKey,{schemaVersion:1,tenantId:config.tenantId,environmentId:config.environmentId,publicOrigin:publicUrl.origin,releaseId:grant?.releaseId??null,bindingDigest:grant?.bindingDigest??null,policyDigest:grant?.policyDigest??null,capabilitySha256:decision==='authorized'?bodySha256(req.headers['x-wishforge-preview-capability']):null,challenge,decision,method:req.method,path:req.url,status,bodySha256:bodySha256(body),issuedAt,expiresAt:issuedAt+120000});} - function fail(req,res,error,anonymous=false){if(res.headersSent){res.destroy();return;}const status=error.status??502,body=Buffer.from(JSON.stringify({error:status===401?'Private preview authentication required':error.status?error.message:'Preview unavailable'})),headers={...baseHeaders(),'content-type':'application/json','content-length':String(body.length)};if(anonymous&&status===401){const signed=proof(req,undefined,'anonymous_denied',status,body);if(signed)headers['x-wishforge-preview-proof']=signed;}res.writeHead(status,headers);res.end(body);} - function proxyHeaders(req,websocket=false){const headers={...req.headers},connectionFields=String(headers.connection??'').split(',').map(value=>value.trim().toLowerCase());for(const key of Object.keys(headers))if(HOP.has(key)||connectionFields.includes(key)||['authorization','host','origin','referer'].includes(key)||key.startsWith('x-forwarded-')||key.startsWith('x-wishforge-'))delete headers[key];const appCookies=(req.headers.cookie??'').split(';').map(value=>value.trim()).filter(value=>value&&!value.startsWith(`${cookieName}=`)&&!/^(__Host-)?WF_/i.test(value));if(appCookies.length)headers.cookie=appCookies.join('; ');else delete headers.cookie;headers.host=upstream.host;headers.origin=upstream.origin;if(websocket){headers.connection='Upgrade';headers.upgrade='websocket';}return headers;} - function responseHeaders(raw){const headers={};for(const[key,value]of Object.entries(raw)){if(value===undefined||HOP.has(key)||key.startsWith('x-wishforge-')||key.startsWith('access-control-')||['server','x-powered-by','content-location','refresh','link','access-control-allow-origin','access-control-allow-credentials'].includes(key))continue;if(key==='set-cookie'){headers[key]=(Array.isArray(value)?value:[value]).filter(item=>!/^\s*(?:__Host-)?WF_/i.test(item)).map(item=>item.replace(/;\s*domain=[^;]*/ig,'')+(secure&&!/;\s*secure(?:;|$)/i.test(item)?'; Secure':''));continue;}if(key==='location'){const location=new URL(String(value),upstream);if(location.origin!==upstream.origin)throw new Failure(502,'External application redirect is not permitted');headers.location=`${publicUrl.origin}${location.pathname}${location.search}${location.hash}`;continue;}headers[key]=value;}return{...headers,...baseHeaders()};} - function watch(kind,value,method,path,stop){let busy=false,closed=false;const timer=setInterval(async()=>{if(busy||closed)return;busy=true;try{await authorize(kind,value,method,path);}catch{stop();}finally{busy=false;}},pollMs);timer.unref();const close=()=>{closed=true;clearInterval(timer);active.delete(close);};active.add(close);return close;} - const server=http.createServer(async(req,res)=>{let anonymous=false;try{ - host(req);const url=route(req); - if(url.pathname==='/_wishforge/session'&&req.method==='GET'){ - const grant=await authorize('launch',url.searchParams.get('code'),'GET','/');if(!CAP.test(grant.sessionToken??''))throw new Failure(502,'Invalid preview session'); - res.writeHead(303,{...baseHeaders(),location:new URL(grant.applicationUrl).href, 'set-cookie':`${cookieName}=${grant.sessionToken}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${Math.max(0,Math.min(900,Math.floor((Date.parse(grant.expiresAt)-Date.now())/1000)))}${secure?'; Secure':''}`});res.end();return; + function baseHeaders() { + return { + 'cache-control': 'no-store', + 'referrer-policy': 'no-referrer', + 'x-content-type-options': 'nosniff', + 'origin-agent-cluster': '?1', + 'cross-origin-opener-policy': 'same-origin', + }; + } + function proof(req, grant, decision, status, body) { + const challenge = req.headers['x-wishforge-preview-challenge']; + if (!CHALLENGE.test(challenge ?? '')) return undefined; + const issuedAt = Date.now(); + return signPreviewProof(config.proofPrivateKey, { + schemaVersion: 1, + tenantId: config.tenantId, + environmentId: config.environmentId, + publicOrigin: publicUrl.origin, + releaseId: grant?.releaseId ?? null, + bindingDigest: grant?.bindingDigest ?? null, + policyDigest: grant?.policyDigest ?? null, + capabilitySha256: decision === 'authorized' ? bodySha256(req.headers['x-wishforge-preview-capability']) : null, + challenge, + decision, + method: req.method, + path: req.url, + status, + bodySha256: bodySha256(body), + issuedAt, + expiresAt: issuedAt + 120000, + }); + } + function fail(req, res, error, anonymous = false) { + if (res.headersSent) { + res.destroy(); + return; } - if(url.pathname.startsWith('/_wishforge/'))throw new Failure(404,'Reserved preview path'); - const isProbe=typeof req.headers['x-wishforge-preview-capability']==='string'; - let value;if(isProbe){value=req.headers['x-wishforge-preview-capability'];if(!CHALLENGE.test(req.headers['x-wishforge-preview-challenge']??''))throw new Failure(400,'Release probe challenge required');if(!['GET','HEAD'].includes(req.method))throw new Failure(403,'Read-only release probe required');} - else{try{value=credential(req);}catch(error){anonymous=true;throw error;}origin(req,!['GET','HEAD'].includes(req.method));} - const kind=isProbe?'probe':'session',grant=await authorize(kind,value,req.method,req.url,req.headers['x-wishforge-preview-challenge']); - if(req.headers['content-length']!==undefined&&(!/^\d+$/.test(req.headers['content-length'])||Number(req.headers['content-length'])>8*1024*1024))throw new Failure(413,'Preview request exceeds 8 MiB'); - const target=new URL(req.url,upstream); - if(isProbe){const response=await fetcher(target,{method:req.method,headers:{accept:'*/*','accept-encoding':'identity'},redirect:'manual',signal:AbortSignal.timeout(15000)});const chunks=[];let size=0;if(response.body)for await(const chunk of response.body){size+=chunk.length;if(size>4*1024*1024)throw new Failure(502,'Preview probe exceeds body limit');chunks.push(chunk);}const body=Buffer.concat(chunks);await authorize(kind,value,req.method,req.url,req.headers['x-wishforge-preview-challenge']);const signed=proof(req,grant,'authorized',response.status,body),headers=responseHeaders(Object.fromEntries(response.headers));delete headers['content-encoding'];headers['content-length']=String(body.length);headers['x-wishforge-preview-proof']=signed;res.writeHead(response.status,headers);res.end(req.method==='HEAD'?undefined:body);return;} - const transport=target.protocol==='https:'?https:http;const forward=transport.request(target,{method:req.method,headers:proxyHeaders(req)},response=>{try{res.writeHead(response.statusCode??502,responseHeaders(response.headers));response.pipe(res);}catch(error){response.destroy();fail(req,res,error);}}); - forward.setTimeout(60000,()=>forward.destroy());forward.on('error',()=>fail(req,res,new Failure(502,'Preview upstream unavailable'))); - const endWatch=watch(kind,value,req.method,req.url,()=>{forward.destroy();res.destroy();});res.on('close',()=>{endWatch();forward.destroy();});req.on('aborted',()=>forward.destroy());let received=0;req.on('data',chunk=>{received+=chunk.length;if(received>8*1024*1024){req.unpipe(forward);forward.destroy();fail(req,res,new Failure(413,'Preview request exceeds 8 MiB'));}});req.pipe(forward); - }catch(error){fail(req,res,error,anonymous);}}); - server.on('connection',socket=>{sockets.add(socket);socket.on('close',()=>sockets.delete(socket));}); - server.on('upgrade',async(req,socket,head)=>{try{host(req);origin(req,true);const url=route(req);if(url.pathname.startsWith('/_wishforge/'))throw new Failure(404,'Reserved preview path');const value=credential(req);await authorize('session',value,'WS',req.url);const target=new URL(req.url,upstream),transport=target.protocol==='https:'?https:http;const forward=transport.request(target,{method:'GET',headers:proxyHeaders(req,true)});forward.setTimeout(10000,()=>forward.destroy());forward.on('upgrade',(response,peer,peerHead)=>{if(response.statusCode!==101){peer.destroy();socket.destroy();return;}let headers;try{headers=responseHeaders(response.headers);}catch{peer.destroy();socket.end('HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n');return;}headers.connection='Upgrade';headers.upgrade='websocket';socket.write(`HTTP/1.1 101 Switching Protocols\r\n${Object.entries(headers).flatMap(([key,value])=>(Array.isArray(value)?value:[value]).map(item=>`${key}: ${item}`)).join('\r\n')}\r\n\r\n`);if(head.length)peer.write(head);if(peerHead.length)socket.write(peerHead);socket.pipe(peer);peer.pipe(socket);const endWatch=watch('session',value,'WS',req.url,()=>{peer.destroy();socket.destroy();});socket.on('close',()=>{endWatch();peer.destroy();});peer.on('close',()=>{endWatch();socket.destroy();});peer.on('error',()=>socket.destroy());socket.on('error',()=>peer.destroy());});forward.on('response',response=>{response.resume();socket.end('HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n');});forward.on('error',()=>socket.destroy());socket.on('close',()=>forward.destroy());forward.end();}catch(error){socket.end(`HTTP/1.1 ${error.status??502} Access Denied\r\nConnection: close\r\n\r\n`);}}); - return{server,cookieName,async close(){for(const end of active)end();for(const socket of sockets)socket.destroy();if(server.listening)await new Promise(resolve=>server.close(resolve));}}; + const status = error.status ?? 502, + body = Buffer.from( + JSON.stringify({ + error: + status === 401 + ? 'Private preview authentication required' + : error.status + ? error.message + : 'Preview unavailable', + }), + ), + headers = { ...baseHeaders(), 'content-type': 'application/json', 'content-length': String(body.length) }; + if (anonymous && status === 401) { + const signed = proof(req, undefined, 'anonymous_denied', status, body); + if (signed) headers['x-wishforge-preview-proof'] = signed; + } + res.writeHead(status, headers); + res.end(body); + } + function proxyHeaders(req, websocket = false) { + const headers = { ...req.headers }, + connectionFields = String(headers.connection ?? '') + .split(',') + .map(value => value.trim().toLowerCase()); + for (const key of Object.keys(headers)) + if ( + HOP.has(key) || + connectionFields.includes(key) || + ['authorization', 'host', 'origin', 'referer'].includes(key) || + key.startsWith('x-forwarded-') || + key.startsWith('x-wishforge-') + ) + delete headers[key]; + const appCookies = (req.headers.cookie ?? '') + .split(';') + .map(value => value.trim()) + .filter(value => value && !value.startsWith(`${cookieName}=`) && !/^(__Host-)?WF_/i.test(value)); + if (appCookies.length) headers.cookie = appCookies.join('; '); + else delete headers.cookie; + headers.host = upstream.host; + headers.origin = upstream.origin; + if (websocket) { + headers.connection = 'Upgrade'; + headers.upgrade = 'websocket'; + } + return headers; + } + function responseHeaders(raw) { + const headers = {}; + for (const [key, value] of Object.entries(raw)) { + if ( + value === undefined || + HOP.has(key) || + key.startsWith('x-wishforge-') || + key.startsWith('access-control-') || + [ + 'server', + 'x-powered-by', + 'content-location', + 'refresh', + 'link', + 'access-control-allow-origin', + 'access-control-allow-credentials', + ].includes(key) + ) + continue; + if (key === 'set-cookie') { + headers[key] = (Array.isArray(value) ? value : [value]) + .filter(item => !/^\s*(?:__Host-)?WF_/i.test(item)) + .map( + item => + item.replace(/;\s*domain=[^;]*/gi, '') + (secure && !/;\s*secure(?:;|$)/i.test(item) ? '; Secure' : ''), + ); + continue; + } + if (key === 'location') { + const location = new URL(String(value), upstream); + if (location.origin !== upstream.origin) + throw new Failure(502, 'External application redirect is not permitted'); + headers.location = `${publicUrl.origin}${location.pathname}${location.search}${location.hash}`; + continue; + } + headers[key] = value; + } + return { ...headers, ...baseHeaders() }; + } + function watch(kind, value, method, path, stop) { + let busy = false, + closed = false; + const timer = setInterval(async () => { + if (busy || closed) return; + busy = true; + try { + await authorize(kind, value, method, path); + } catch { + stop(); + } finally { + busy = false; + } + }, pollMs); + timer.unref(); + const close = () => { + closed = true; + clearInterval(timer); + active.delete(close); + }; + active.add(close); + return close; + } + const server = http.createServer(async (req, res) => { + let anonymous = false; + try { + host(req); + const url = route(req); + if (url.pathname === '/_wishforge/session' && req.method === 'GET') { + const grant = await authorize('launch', url.searchParams.get('code'), 'GET', '/'); + if (!CAP.test(grant.sessionToken ?? '')) throw new Failure(502, 'Invalid preview session'); + res.writeHead(303, { + ...baseHeaders(), + location: new URL(grant.applicationUrl).href, + 'set-cookie': `${cookieName}=${grant.sessionToken}; HttpOnly; SameSite=Lax; Path=/; Max-Age=${Math.max(0, Math.min(900, Math.floor((Date.parse(grant.expiresAt) - Date.now()) / 1000)))}${secure ? '; Secure' : ''}`, + }); + res.end(); + return; + } + if (url.pathname.startsWith('/_wishforge/')) throw new Failure(404, 'Reserved preview path'); + const isProbe = typeof req.headers['x-wishforge-preview-capability'] === 'string'; + let value; + if (isProbe) { + value = req.headers['x-wishforge-preview-capability']; + if (!CHALLENGE.test(req.headers['x-wishforge-preview-challenge'] ?? '')) + throw new Failure(400, 'Release probe challenge required'); + if (!['GET', 'HEAD'].includes(req.method)) throw new Failure(403, 'Read-only release probe required'); + } else { + try { + value = credential(req); + } catch (error) { + anonymous = true; + throw error; + } + origin(req, !['GET', 'HEAD'].includes(req.method)); + } + const kind = isProbe ? 'probe' : 'session', + grant = await authorize(kind, value, req.method, req.url, req.headers['x-wishforge-preview-challenge']); + if ( + req.headers['content-length'] !== undefined && + (!/^\d+$/.test(req.headers['content-length']) || Number(req.headers['content-length']) > 8 * 1024 * 1024) + ) + throw new Failure(413, 'Preview request exceeds 8 MiB'); + const target = new URL(req.url, upstream); + if (isProbe) { + const response = await fetcher(target, { + method: req.method, + headers: { accept: '*/*', 'accept-encoding': 'identity' }, + redirect: 'manual', + signal: AbortSignal.timeout(15000), + }); + const chunks = []; + let size = 0; + if (response.body) + for await (const chunk of response.body) { + size += chunk.length; + if (size > 4 * 1024 * 1024) throw new Failure(502, 'Preview probe exceeds body limit'); + chunks.push(chunk); + } + const body = Buffer.concat(chunks); + await authorize(kind, value, req.method, req.url, req.headers['x-wishforge-preview-challenge']); + const signed = proof(req, grant, 'authorized', response.status, body), + headers = responseHeaders(Object.fromEntries(response.headers)); + delete headers['content-encoding']; + headers['content-length'] = String(body.length); + headers['x-wishforge-preview-proof'] = signed; + res.writeHead(response.status, headers); + res.end(req.method === 'HEAD' ? undefined : body); + return; + } + const transport = target.protocol === 'https:' ? https : http; + const forward = transport.request(target, { method: req.method, headers: proxyHeaders(req) }, response => { + try { + res.writeHead(response.statusCode ?? 502, responseHeaders(response.headers)); + response.pipe(res); + } catch (error) { + response.destroy(); + fail(req, res, error); + } + }); + forward.setTimeout(60000, () => forward.destroy()); + forward.on('error', () => fail(req, res, new Failure(502, 'Preview upstream unavailable'))); + const endWatch = watch(kind, value, req.method, req.url, () => { + forward.destroy(); + res.destroy(); + }); + res.on('close', () => { + endWatch(); + forward.destroy(); + }); + req.on('aborted', () => forward.destroy()); + let received = 0; + req.on('data', chunk => { + received += chunk.length; + if (received > 8 * 1024 * 1024) { + req.unpipe(forward); + forward.destroy(); + fail(req, res, new Failure(413, 'Preview request exceeds 8 MiB')); + } + }); + req.pipe(forward); + } catch (error) { + fail(req, res, error, anonymous); + } + }); + server.on('connection', socket => { + sockets.add(socket); + socket.on('close', () => sockets.delete(socket)); + }); + server.on('upgrade', async (req, socket, head) => { + try { + host(req); + origin(req, true); + const url = route(req); + if (url.pathname.startsWith('/_wishforge/')) throw new Failure(404, 'Reserved preview path'); + const value = credential(req); + await authorize('session', value, 'WS', req.url); + const target = new URL(req.url, upstream), + transport = target.protocol === 'https:' ? https : http; + const forward = transport.request(target, { method: 'GET', headers: proxyHeaders(req, true) }); + forward.setTimeout(10000, () => forward.destroy()); + forward.on('upgrade', (response, peer, peerHead) => { + if (response.statusCode !== 101) { + peer.destroy(); + socket.destroy(); + return; + } + let headers; + try { + headers = responseHeaders(response.headers); + } catch { + peer.destroy(); + socket.end('HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n'); + return; + } + headers.connection = 'Upgrade'; + headers.upgrade = 'websocket'; + socket.write( + `HTTP/1.1 101 Switching Protocols\r\n${Object.entries(headers) + .flatMap(([key, value]) => (Array.isArray(value) ? value : [value]).map(item => `${key}: ${item}`)) + .join('\r\n')}\r\n\r\n`, + ); + if (head.length) peer.write(head); + if (peerHead.length) socket.write(peerHead); + socket.pipe(peer); + peer.pipe(socket); + const endWatch = watch('session', value, 'WS', req.url, () => { + peer.destroy(); + socket.destroy(); + }); + socket.on('close', () => { + endWatch(); + peer.destroy(); + }); + peer.on('close', () => { + endWatch(); + socket.destroy(); + }); + peer.on('error', () => socket.destroy()); + socket.on('error', () => peer.destroy()); + }); + forward.on('response', response => { + response.resume(); + socket.end('HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n'); + }); + forward.on('error', () => socket.destroy()); + socket.on('close', () => forward.destroy()); + forward.end(); + } catch (error) { + socket.end(`HTTP/1.1 ${error.status ?? 502} Access Denied\r\nConnection: close\r\n\r\n`); + } + }); + return { + server, + cookieName, + async close() { + for (const end of active) end(); + for (const socket of sockets) socket.destroy(); + if (server.listening) await new Promise(resolve => server.close(resolve)); + }, + }; } diff --git a/apps/preview-gateway/test/gateway.test.mjs b/apps/preview-gateway/test/gateway.test.mjs index 72c0280..68e3f0d 100644 --- a/apps/preview-gateway/test/gateway.test.mjs +++ b/apps/preview-gateway/test/gateway.test.mjs @@ -6,33 +6,471 @@ import { createHash, generateKeyPairSync } from 'node:crypto'; import { once } from 'node:events'; import { createPreviewGateway } from '../server.mjs'; import { bodySha256, verifyPreviewProof } from '../proof.mjs'; -const serviceToken='local-fixture-preview-service-token-32-plus',launch='L'.repeat(43),session='S'.repeat(43),probe='P'.repeat(43),challenge='C'.repeat(43); -const listen=async(server,host='127.0.0.1')=>{server.listen(0,host);await once(server,'listening');return server.address().port;}; -const close=server=>new Promise(resolve=>{server.closeAllConnections?.();server.close(resolve);}); -async function fixture({secure=false,entryPath='/'}={}){ - const keys=generateKeyPairSync('ed25519'),privateKey=keys.privateKey.export({type:'pkcs8',format:'pem'}),publicKey=keys.publicKey.export({type:'spki',format:'pem'}); - let authorized=true,role='developer',used=false,expiryOverride;const seen=[],upgraded=new Set(); - const application=http.createServer((req,res)=>{seen.push({headers:req.headers,path:req.url,method:req.method});if(req.url==='/login'&&req.method==='POST'){res.writeHead(200,{'set-cookie':'application_session=logged-in; HttpOnly; Domain=internal.invalid; Path=/'});res.end('logged in');return;}if(req.url==='/account'){res.writeHead(req.headers.cookie==='application_session=logged-in'?200:401,{'content-type':'text/plain'});res.end(req.headers.cookie==='application_session=logged-in'?'private application account':'application login required');return;}if(req.url==='/stream'){res.writeHead(200,{'content-type':'text/plain'});res.write('first');const timer=setInterval(()=>res.write('next'),30);res.on('close',()=>clearInterval(timer));return;}if(req.url==='/redirect'){res.writeHead(302,{location:`http://127.0.0.1:${application.address().port}/actual`});res.end();return;}res.writeHead(req.url==='/app-401'?401:200,{'content-type':'text/plain','x-wishforge-preview-proof':'forged-application-proof','set-cookie':['app=allowed; Domain=internal.invalid; HttpOnly','WF_PREVIEW_SESSION=malicious; Path=/']});res.end('fixture application');}); - application.on('upgrade',(req,socket)=>{seen.push({headers:req.headers,path:req.url,method:'WS'});upgraded.add(socket);socket.on('close',()=>upgraded.delete(socket));const accept=createHash('sha1').update(req.headers['sec-websocket-key']+'258EAFA5-E914-47DA-95CA-C5AB0DC85B11').digest('base64');socket.write(`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n${req.url==='/bad-socket-headers'?'Location: https://external.invalid/\r\n':''}\r\n`);}); - const upstreamPort=await listen(application);let publicOrigin; - const api=http.createServer(async(req,res)=>{let input='';for await(const chunk of req)input+=chunk;const body=JSON.parse(input);if(req.url!=='/preview-control/authorize'||req.headers.authorization!==`Bearer ${serviceToken}`||body.tenantId!=='tenant-a'||body.environmentId!=='environment-a'){res.writeHead(403);res.end('{}');return;}let granted=authorized;if(body.kind==='launch'){granted&&=body.credential===launch&&!used;if(granted)used=true;}else if(body.kind==='probe')granted&&=body.credential===probe&&body.challenge===challenge&&['GET','HEAD'].includes(body.method);else granted&&=body.credential===session&&!(role==='viewer'&&!['GET','HEAD'].includes(body.method));if(!granted){res.writeHead(403);res.end('{}');return;}res.setHeader('content-type','application/json');res.end(JSON.stringify({data:{tenantId:'tenant-a',projectId:'project-a',environmentId:'environment-a',publicOrigin,applicationUrl:publicOrigin+entryPath,role:body.kind==='probe'?'probe':role,releaseId:'release-a',bindingDigest:'sha256:'+ 'a'.repeat(64),policyDigest:'sha256:'+ 'b'.repeat(64),expiresAt:expiryOverride??new Date(Date.now()+60000).toISOString(),...(body.kind==='launch'?{sessionToken:session}:{})}}));}); - const apiPort=await listen(api,'localhost');const reservation=net.createServer();const gatewayPort=await listen(reservation);await close(reservation);const requestOrigin=`http://127.0.0.1:${gatewayPort}`;publicOrigin=`${secure?'https':'http'}://127.0.0.1:${gatewayPort}`; - const gateway=createPreviewGateway({tenantId:'tenant-a',projectId:'project-a',environmentId:'environment-a',publicOrigin,upstreamOrigin:`http://127.0.0.1:${upstreamPort}`,apiOrigin:`http://localhost:${apiPort}`,serviceToken,proofPrivateKey:privateKey,production:false,reauthorizeMs:30});gateway.server.listen(gatewayPort,'127.0.0.1');await once(gateway.server,'listening'); - return{publicOrigin,publicKey,seen,gateway,setRole:value=>{role=value;},setExpiry:value=>{expiryOverride=value;},revoke:()=>{authorized=false;},async launch(){return fetch(`${requestOrigin}/_wishforge/session?code=${launch}`,{redirect:'manual'});},cookie:`${gateway.cookieName}=${session}`,async close(){await gateway.close();for(const socket of upgraded)socket.destroy();await Promise.all([close(application),close(api)]);}}; +const serviceToken = 'local-fixture-preview-service-token-32-plus', + launch = 'L'.repeat(43), + session = 'S'.repeat(43), + probe = 'P'.repeat(43), + challenge = 'C'.repeat(43); +const listen = async (server, host = '127.0.0.1') => { + server.listen(0, host); + await once(server, 'listening'); + return server.address().port; +}; +const close = server => + new Promise(resolve => { + server.closeAllConnections?.(); + server.close(resolve); + }); +async function fixture({ secure = false, entryPath = '/' } = {}) { + const keys = generateKeyPairSync('ed25519'), + privateKey = keys.privateKey.export({ type: 'pkcs8', format: 'pem' }), + publicKey = keys.publicKey.export({ type: 'spki', format: 'pem' }); + let authorized = true, + role = 'developer', + used = false, + expiryOverride; + const seen = [], + upgraded = new Set(); + const application = http.createServer((req, res) => { + seen.push({ headers: req.headers, path: req.url, method: req.method }); + if (req.url === '/login' && req.method === 'POST') { + res.writeHead(200, { 'set-cookie': 'application_session=logged-in; HttpOnly; Domain=internal.invalid; Path=/' }); + res.end('logged in'); + return; + } + if (req.url === '/account') { + res.writeHead(req.headers.cookie === 'application_session=logged-in' ? 200 : 401, { + 'content-type': 'text/plain', + }); + res.end( + req.headers.cookie === 'application_session=logged-in' + ? 'private application account' + : 'application login required', + ); + return; + } + if (req.url === '/stream') { + res.writeHead(200, { 'content-type': 'text/plain' }); + res.write('first'); + const timer = setInterval(() => res.write('next'), 30); + res.on('close', () => clearInterval(timer)); + return; + } + if (req.url === '/redirect') { + res.writeHead(302, { location: `http://127.0.0.1:${application.address().port}/actual` }); + res.end(); + return; + } + res.writeHead(req.url === '/app-401' ? 401 : 200, { + 'content-type': 'text/plain', + 'x-wishforge-preview-proof': 'forged-application-proof', + 'set-cookie': ['app=allowed; Domain=internal.invalid; HttpOnly', 'WF_PREVIEW_SESSION=malicious; Path=/'], + }); + res.end('fixture application'); + }); + application.on('upgrade', (req, socket) => { + seen.push({ headers: req.headers, path: req.url, method: 'WS' }); + upgraded.add(socket); + socket.on('close', () => upgraded.delete(socket)); + const accept = createHash('sha1') + .update(req.headers['sec-websocket-key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11') + .digest('base64'); + socket.write( + `HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n${req.url === '/bad-socket-headers' ? 'Location: https://external.invalid/\r\n' : ''}\r\n`, + ); + }); + const upstreamPort = await listen(application); + let publicOrigin; + const api = http.createServer(async (req, res) => { + let input = ''; + for await (const chunk of req) input += chunk; + const body = JSON.parse(input); + if ( + req.url !== '/preview-control/authorize' || + req.headers.authorization !== `Bearer ${serviceToken}` || + body.tenantId !== 'tenant-a' || + body.environmentId !== 'environment-a' + ) { + res.writeHead(403); + res.end('{}'); + return; + } + let granted = authorized; + if (body.kind === 'launch') { + granted &&= body.credential === launch && !used; + if (granted) used = true; + } else if (body.kind === 'probe') + granted &&= body.credential === probe && body.challenge === challenge && ['GET', 'HEAD'].includes(body.method); + else granted &&= body.credential === session && !(role === 'viewer' && !['GET', 'HEAD'].includes(body.method)); + if (!granted) { + res.writeHead(403); + res.end('{}'); + return; + } + res.setHeader('content-type', 'application/json'); + res.end( + JSON.stringify({ + data: { + tenantId: 'tenant-a', + projectId: 'project-a', + environmentId: 'environment-a', + publicOrigin, + applicationUrl: publicOrigin + entryPath, + role: body.kind === 'probe' ? 'probe' : role, + releaseId: 'release-a', + bindingDigest: 'sha256:' + 'a'.repeat(64), + policyDigest: 'sha256:' + 'b'.repeat(64), + expiresAt: expiryOverride ?? new Date(Date.now() + 60000).toISOString(), + ...(body.kind === 'launch' ? { sessionToken: session } : {}), + }, + }), + ); + }); + const apiPort = await listen(api, 'localhost'); + const reservation = net.createServer(); + const gatewayPort = await listen(reservation); + await close(reservation); + const requestOrigin = `http://127.0.0.1:${gatewayPort}`; + publicOrigin = `${secure ? 'https' : 'http'}://127.0.0.1:${gatewayPort}`; + const gateway = createPreviewGateway({ + tenantId: 'tenant-a', + projectId: 'project-a', + environmentId: 'environment-a', + publicOrigin, + upstreamOrigin: `http://127.0.0.1:${upstreamPort}`, + apiOrigin: `http://localhost:${apiPort}`, + serviceToken, + proofPrivateKey: privateKey, + production: false, + reauthorizeMs: 30, + }); + gateway.server.listen(gatewayPort, '127.0.0.1'); + await once(gateway.server, 'listening'); + return { + publicOrigin, + publicKey, + seen, + gateway, + setRole: value => { + role = value; + }, + setExpiry: value => { + expiryOverride = value; + }, + revoke: () => { + authorized = false; + }, + async launch() { + return fetch(`${requestOrigin}/_wishforge/session?code=${launch}`, { redirect: 'manual' }); + }, + cookie: `${gateway.cookieName}=${session}`, + async close() { + await gateway.close(); + for (const socket of upgraded) socket.destroy(); + await Promise.all([close(application), close(api)]); + }, + }; } -test('anonymous denial is signed by the gateway and cannot be forged by an application 401',async()=>{const f=await fixture();try{const denied=await fetch(f.publicOrigin+'/app-401',{headers:{'x-wishforge-preview-challenge':challenge}});const body=Buffer.from(await denied.arrayBuffer());assert.equal(denied.status,401);assert.equal(f.seen.length,0);const signed=denied.headers.get('x-wishforge-preview-proof');assert.ok(verifyPreviewProof(f.publicKey,signed,{decision:'anonymous_denied',status:401,path:'/app-401',challenge,publicOrigin:f.publicOrigin,bodySha256:bodySha256(body),releaseId:null}));assert.equal(verifyPreviewProof(f.publicKey,signed,{challenge:'different-challenge'}),undefined);const response=await fetch(f.publicOrigin+'/app-401',{headers:{'x-wishforge-preview-capability':probe,'x-wishforge-preview-challenge':challenge}});const appBody=Buffer.from(await response.arrayBuffer());assert.equal(response.status,401);assert.ok(verifyPreviewProof(f.publicKey,response.headers.get('x-wishforge-preview-proof'),{decision:'authorized',releaseId:'release-a',status:401,bodySha256:bodySha256(appBody)}));assert.equal(verifyPreviewProof(f.publicKey,response.headers.get('x-wishforge-preview-proof'),{decision:'anonymous_denied'}),undefined);}finally{await f.close();}}); -test('launch is one-use, sets host-only HttpOnly cookie, strips credentials and hides upstream redirects',async()=>{const f=await fixture();try{const response=await f.launch();assert.equal(response.status,303);assert.equal(response.headers.get('location'),f.publicOrigin+'/');const cookie=response.headers.get('set-cookie');assert.match(cookie,/HttpOnly/);assert.match(cookie,/SameSite=Lax/);assert.match(cookie,/Path=\//);assert.doesNotMatch(cookie,/Domain=/i);assert.equal((await f.launch()).status,403);const app=await fetch(f.publicOrigin+'/actual',{headers:{cookie:f.cookie+'; app=allowed; WF_OTHER=not-forwarded',authorization:'Bearer user-must-not-reach-app','x-wishforge-service-token':serviceToken}});assert.equal(app.status,200);await app.text();assert.equal(f.seen.at(-1).headers.authorization,undefined);assert.equal(f.seen.at(-1).headers['x-wishforge-service-token'],undefined);assert.equal(f.seen.at(-1).headers.cookie,'app=allowed');assert.doesNotMatch(app.headers.get('set-cookie'),/Domain|WF_PREVIEW_SESSION/i);assert.equal(app.headers.get('x-wishforge-preview-proof'),null);const redirect=await fetch(f.publicOrigin+'/redirect',{headers:{cookie:f.cookie},redirect:'manual'});assert.equal(redirect.headers.get('location'),f.publicOrigin+'/actual');assert.equal(await new Promise((resolve,reject)=>{const request=http.get(f.publicOrigin+'/',{headers:{host:'other-preview.invalid',cookie:f.cookie}},response=>{response.resume();resolve(response.statusCode);});request.on('error',reject);}),421);}finally{await f.close();}}); -test('Viewer reads are permitted but mutations and cross-origin requests are denied',async()=>{const f=await fixture();try{f.setRole('viewer');await f.launch();assert.equal((await fetch(f.publicOrigin+'/',{headers:{cookie:f.cookie}})).status,200);assert.equal((await fetch(f.publicOrigin+'/',{method:'POST',headers:{cookie:f.cookie,origin:f.publicOrigin},body:'unsafe mutation'})).status,403);assert.equal((await fetch(f.publicOrigin+'/',{headers:{cookie:f.cookie,origin:'https://other.preview.invalid'}})).status,403);}finally{await f.close();}}); -test('release probes bind the body, path, challenge, scope and active release, without forwarding capabilities',async()=>{const f=await fixture();try{const response=await fetch(f.publicOrigin+'/business?fixture=1',{headers:{'x-wishforge-preview-capability':probe,'x-wishforge-preview-challenge':challenge,authorization:'Bearer ignored'}});const body=Buffer.from(await response.arrayBuffer());const signed=response.headers.get('x-wishforge-preview-proof');assert.ok(verifyPreviewProof(f.publicKey,signed,{tenantId:'tenant-a',environmentId:'environment-a',releaseId:'release-a',bindingDigest:'sha256:'+'a'.repeat(64),policyDigest:'sha256:'+'b'.repeat(64),decision:'authorized',capabilitySha256:bodySha256(probe),path:'/business?fixture=1',challenge,status:200,bodySha256:bodySha256(body)}));assert.equal(verifyPreviewProof(f.publicKey,signed,{bodySha256:bodySha256('different')}),undefined);assert.equal(f.seen.at(-1).headers['x-wishforge-preview-capability'],undefined);assert.equal(f.seen.at(-1).headers.authorization,undefined);f.revoke();assert.equal((await fetch(f.publicOrigin+'/',{headers:{'x-wishforge-preview-capability':probe,'x-wishforge-preview-challenge':challenge}})).status,403);}finally{await f.close();}}); -test('revocation closes an existing streamed response',async()=>{const f=await fixture();try{await f.launch();const response=await fetch(f.publicOrigin+'/stream',{headers:{cookie:f.cookie}}),reader=response.body.getReader();assert.equal((await reader.read()).done,false);f.revoke();await assert.rejects(async()=>{for(;;){const value=await reader.read();if(value.done)throw new Error('terminated');}},/terminated/);}finally{await f.close();}}); -test('WebSocket proxy requires session and origin, strips credentials, and closes on revocation',async()=>{const f=await fixture();let socket;try{await f.launch();const req=http.request(f.publicOrigin+'/socket',{headers:{connection:'Upgrade',upgrade:'websocket','sec-websocket-version':'13','sec-websocket-key':Buffer.alloc(16).toString('base64'),cookie:f.cookie,origin:f.publicOrigin,authorization:'Bearer not-forwarded'}});const upgraded=once(req,'upgrade');req.end();const [response,peer]=await upgraded;socket=peer;assert.equal(response.statusCode,101);assert.equal(f.seen.at(-1).headers.cookie,undefined);assert.equal(f.seen.at(-1).headers.authorization,undefined);const ended=once(socket,'close');f.revoke();await ended;}finally{socket?.destroy();await f.close();}}); +test('anonymous denial is signed by the gateway and cannot be forged by an application 401', async () => { + const f = await fixture(); + try { + const denied = await fetch(f.publicOrigin + '/app-401', { + headers: { 'x-wishforge-preview-challenge': challenge }, + }); + const body = Buffer.from(await denied.arrayBuffer()); + assert.equal(denied.status, 401); + assert.equal(f.seen.length, 0); + const signed = denied.headers.get('x-wishforge-preview-proof'); + assert.ok( + verifyPreviewProof(f.publicKey, signed, { + decision: 'anonymous_denied', + status: 401, + path: '/app-401', + challenge, + publicOrigin: f.publicOrigin, + bodySha256: bodySha256(body), + releaseId: null, + }), + ); + assert.equal(verifyPreviewProof(f.publicKey, signed, { challenge: 'different-challenge' }), undefined); + const response = await fetch(f.publicOrigin + '/app-401', { + headers: { 'x-wishforge-preview-capability': probe, 'x-wishforge-preview-challenge': challenge }, + }); + const appBody = Buffer.from(await response.arrayBuffer()); + assert.equal(response.status, 401); + assert.ok( + verifyPreviewProof(f.publicKey, response.headers.get('x-wishforge-preview-proof'), { + decision: 'authorized', + releaseId: 'release-a', + status: 401, + bodySha256: bodySha256(appBody), + }), + ); + assert.equal( + verifyPreviewProof(f.publicKey, response.headers.get('x-wishforge-preview-proof'), { + decision: 'anonymous_denied', + }), + undefined, + ); + } finally { + await f.close(); + } +}); +test('launch is one-use, sets host-only HttpOnly cookie, strips credentials and hides upstream redirects', async () => { + const f = await fixture(); + try { + const response = await f.launch(); + assert.equal(response.status, 303); + assert.equal(response.headers.get('location'), f.publicOrigin + '/'); + const cookie = response.headers.get('set-cookie'); + assert.match(cookie, /HttpOnly/); + assert.match(cookie, /SameSite=Lax/); + assert.match(cookie, /Path=\//); + assert.doesNotMatch(cookie, /Domain=/i); + assert.equal((await f.launch()).status, 403); + const app = await fetch(f.publicOrigin + '/actual', { + headers: { + cookie: f.cookie + '; app=allowed; WF_OTHER=not-forwarded', + authorization: 'Bearer user-must-not-reach-app', + 'x-wishforge-service-token': serviceToken, + }, + }); + assert.equal(app.status, 200); + await app.text(); + assert.equal(f.seen.at(-1).headers.authorization, undefined); + assert.equal(f.seen.at(-1).headers['x-wishforge-service-token'], undefined); + assert.equal(f.seen.at(-1).headers.cookie, 'app=allowed'); + assert.doesNotMatch(app.headers.get('set-cookie'), /Domain|WF_PREVIEW_SESSION/i); + assert.equal(app.headers.get('x-wishforge-preview-proof'), null); + const redirect = await fetch(f.publicOrigin + '/redirect', { headers: { cookie: f.cookie }, redirect: 'manual' }); + assert.equal(redirect.headers.get('location'), f.publicOrigin + '/actual'); + assert.equal( + await new Promise((resolve, reject) => { + const request = http.get( + f.publicOrigin + '/', + { headers: { host: 'other-preview.invalid', cookie: f.cookie } }, + response => { + response.resume(); + resolve(response.statusCode); + }, + ); + request.on('error', reject); + }), + 421, + ); + } finally { + await f.close(); + } +}); +test('Viewer reads are permitted but mutations and cross-origin requests are denied', async () => { + const f = await fixture(); + try { + f.setRole('viewer'); + await f.launch(); + assert.equal((await fetch(f.publicOrigin + '/', { headers: { cookie: f.cookie } })).status, 200); + assert.equal( + ( + await fetch(f.publicOrigin + '/', { + method: 'POST', + headers: { cookie: f.cookie, origin: f.publicOrigin }, + body: 'unsafe mutation', + }) + ).status, + 403, + ); + assert.equal( + (await fetch(f.publicOrigin + '/', { headers: { cookie: f.cookie, origin: 'https://other.preview.invalid' } })) + .status, + 403, + ); + } finally { + await f.close(); + } +}); +test('release probes bind the body, path, challenge, scope and active release, without forwarding capabilities', async () => { + const f = await fixture(); + try { + const response = await fetch(f.publicOrigin + '/business?fixture=1', { + headers: { + 'x-wishforge-preview-capability': probe, + 'x-wishforge-preview-challenge': challenge, + authorization: 'Bearer ignored', + }, + }); + const body = Buffer.from(await response.arrayBuffer()); + const signed = response.headers.get('x-wishforge-preview-proof'); + assert.ok( + verifyPreviewProof(f.publicKey, signed, { + tenantId: 'tenant-a', + environmentId: 'environment-a', + releaseId: 'release-a', + bindingDigest: 'sha256:' + 'a'.repeat(64), + policyDigest: 'sha256:' + 'b'.repeat(64), + decision: 'authorized', + capabilitySha256: bodySha256(probe), + path: '/business?fixture=1', + challenge, + status: 200, + bodySha256: bodySha256(body), + }), + ); + assert.equal(verifyPreviewProof(f.publicKey, signed, { bodySha256: bodySha256('different') }), undefined); + assert.equal(f.seen.at(-1).headers['x-wishforge-preview-capability'], undefined); + assert.equal(f.seen.at(-1).headers.authorization, undefined); + f.revoke(); + assert.equal( + ( + await fetch(f.publicOrigin + '/', { + headers: { 'x-wishforge-preview-capability': probe, 'x-wishforge-preview-challenge': challenge }, + }) + ).status, + 403, + ); + } finally { + await f.close(); + } +}); +test('revocation closes an existing streamed response', async () => { + const f = await fixture(); + try { + await f.launch(); + const response = await fetch(f.publicOrigin + '/stream', { headers: { cookie: f.cookie } }), + reader = response.body.getReader(); + assert.equal((await reader.read()).done, false); + f.revoke(); + await assert.rejects(async () => { + for (;;) { + const value = await reader.read(); + if (value.done) throw new Error('terminated'); + } + }, /terminated/); + } finally { + await f.close(); + } +}); +test('WebSocket proxy requires session and origin, strips credentials, and closes on revocation', async () => { + const f = await fixture(); + let socket; + try { + await f.launch(); + const req = http.request(f.publicOrigin + '/socket', { + headers: { + connection: 'Upgrade', + upgrade: 'websocket', + 'sec-websocket-version': '13', + 'sec-websocket-key': Buffer.alloc(16).toString('base64'), + cookie: f.cookie, + origin: f.publicOrigin, + authorization: 'Bearer not-forwarded', + }, + }); + const upgraded = once(req, 'upgrade'); + req.end(); + const [response, peer] = await upgraded; + socket = peer; + assert.equal(response.statusCode, 101); + assert.equal(f.seen.at(-1).headers.cookie, undefined); + assert.equal(f.seen.at(-1).headers.authorization, undefined); + const ended = once(socket, 'close'); + f.revoke(); + await ended; + } finally { + socket?.destroy(); + await f.close(); + } +}); -test('TLS-terminated preview uses the Secure host-prefix cookie',async()=>{const f=await fixture({secure:true});try{const response=await f.launch();assert.equal(response.status,303);const cookie=response.headers.get('set-cookie');assert.match(cookie,/^__Host-WF_PREVIEW_SESSION=/);assert.match(cookie,/; Secure/);assert.match(cookie,/HttpOnly/);assert.doesNotMatch(cookie,/Domain=/i);}finally{await f.close();}}); +test('TLS-terminated preview uses the Secure host-prefix cookie', async () => { + const f = await fixture({ secure: true }); + try { + const response = await f.launch(); + assert.equal(response.status, 303); + const cookie = response.headers.get('set-cookie'); + assert.match(cookie, /^__Host-WF_PREVIEW_SESSION=/); + assert.match(cookie, /; Secure/); + assert.match(cookie, /HttpOnly/); + assert.doesNotMatch(cookie, /Domain=/i); + } finally { + await f.close(); + } +}); -test('application cookie login survives the proxy without forwarding WishForge credentials',async()=>{const f=await fixture();try{await f.launch();const before=await fetch(f.publicOrigin+'/account',{headers:{cookie:f.cookie}});assert.equal(before.status,401);const login=await fetch(f.publicOrigin+'/login',{method:'POST',headers:{cookie:f.cookie,origin:f.publicOrigin,'content-type':'application/json'},body:JSON.stringify({fixture:true})});assert.equal(login.status,200);const cookie=login.headers.get('set-cookie');assert.match(cookie,/application_session=logged-in/);assert.doesNotMatch(cookie,/Domain=/i);const account=await fetch(f.publicOrigin+'/account',{headers:{cookie:f.cookie+'; '+cookie.split(';')[0]}});assert.equal(account.status,200);assert.equal(await account.text(),'private application account');assert.equal(f.seen.at(-1).headers.cookie,'application_session=logged-in');}finally{await f.close();}}); +test('application cookie login survives the proxy without forwarding WishForge credentials', async () => { + const f = await fixture(); + try { + await f.launch(); + const before = await fetch(f.publicOrigin + '/account', { headers: { cookie: f.cookie } }); + assert.equal(before.status, 401); + const login = await fetch(f.publicOrigin + '/login', { + method: 'POST', + headers: { cookie: f.cookie, origin: f.publicOrigin, 'content-type': 'application/json' }, + body: JSON.stringify({ fixture: true }), + }); + assert.equal(login.status, 200); + const cookie = login.headers.get('set-cookie'); + assert.match(cookie, /application_session=logged-in/); + assert.doesNotMatch(cookie, /Domain=/i); + const account = await fetch(f.publicOrigin + '/account', { + headers: { cookie: f.cookie + '; ' + cookie.split(';')[0] }, + }); + assert.equal(account.status, 200); + assert.equal(await account.text(), 'private application account'); + assert.equal(f.seen.at(-1).headers.cookie, 'application_session=logged-in'); + } finally { + await f.close(); + } +}); -test('launch redirects to the release-bound non-root application entry path',async()=>{const f=await fixture({entryPath:'/catalog/start'});try{const response=await f.launch();assert.equal(response.status,303);assert.equal(response.headers.get('location'),f.publicOrigin+'/catalog/start');}finally{await f.close();}}); +test('launch redirects to the release-bound non-root application entry path', async () => { + const f = await fixture({ entryPath: '/catalog/start' }); + try { + const response = await f.launch(); + assert.equal(response.status, 303); + assert.equal(response.headers.get('location'), f.publicOrigin + '/catalog/start'); + } finally { + await f.close(); + } +}); -test('malformed authorization expiry is denied before application forwarding',async()=>{const f=await fixture();try{await f.launch();f.setExpiry('not-a-date');assert.equal((await fetch(f.publicOrigin+'/',{headers:{cookie:f.cookie}})).status,403);assert.equal(f.seen.length,0);}finally{await f.close();}}); +test('malformed authorization expiry is denied before application forwarding', async () => { + const f = await fixture(); + try { + await f.launch(); + f.setExpiry('not-a-date'); + assert.equal((await fetch(f.publicOrigin + '/', { headers: { cookie: f.cookie } })).status, 403); + assert.equal(f.seen.length, 0); + } finally { + await f.close(); + } +}); -test('malicious upgrade response headers cannot crash the preview gateway',async()=>{const f=await fixture();try{await f.launch();const status=await new Promise((resolve,reject)=>{const request=http.get(f.publicOrigin+'/bad-socket-headers',{headers:{connection:'Upgrade',upgrade:'websocket','sec-websocket-version':'13','sec-websocket-key':Buffer.alloc(16).toString('base64'),cookie:f.cookie,origin:f.publicOrigin}},response=>{response.resume();resolve(response.statusCode);});request.on('error',reject);});assert.equal(status,502);assert.equal((await fetch(f.publicOrigin+'/',{headers:{cookie:f.cookie}})).status,200);}finally{await f.close();}}); +test('malicious upgrade response headers cannot crash the preview gateway', async () => { + const f = await fixture(); + try { + await f.launch(); + const status = await new Promise((resolve, reject) => { + const request = http.get( + f.publicOrigin + '/bad-socket-headers', + { + headers: { + connection: 'Upgrade', + upgrade: 'websocket', + 'sec-websocket-version': '13', + 'sec-websocket-key': Buffer.alloc(16).toString('base64'), + cookie: f.cookie, + origin: f.publicOrigin, + }, + }, + response => { + response.resume(); + resolve(response.statusCode); + }, + ); + request.on('error', reject); + }); + assert.equal(status, 502); + assert.equal((await fetch(f.publicOrigin + '/', { headers: { cookie: f.cookie } })).status, 200); + } finally { + await f.close(); + } +}); diff --git a/apps/runner/package.json b/apps/runner/package.json index a57386b..890491b 100644 --- a/apps/runner/package.json +++ b/apps/runner/package.json @@ -1,5 +1,14 @@ { - "name": "@wishforge/runner", "version": "0.1.0", "private": true, "type": "module", - "scripts": { "dev": "tsx src/main.ts", "typecheck": "tsc --noEmit", "test": "vitest run" }, - "dependencies": { "@wishforge/runtime": "*" } + "name": "@wishforge/runner", + "version": "0.1.0", + "private": true, + "type": "module", + "scripts": { + "dev": "tsx src/main.ts", + "typecheck": "tsc --noEmit", + "test": "vitest run" + }, + "dependencies": { + "@wishforge/runtime": "*" + } } diff --git a/apps/runner/src/main.ts b/apps/runner/src/main.ts index 129fbc0..ec1c832 100644 --- a/apps/runner/src/main.ts +++ b/apps/runner/src/main.ts @@ -1,44 +1,144 @@ -import type {PrivatePreviewProbe} from '@wishforge/contracts'; +import type { PrivatePreviewProbe } from '@wishforge/contracts'; import https from 'node:https'; import { readFile } from 'node:fs/promises'; -import { ComposeDeploymentAdapter, KubernetesDeploymentAdapter, DeploymentSecretBroker, type SignedRunnerTask,type ReleaseManifest } from '@wishforge/runtime'; +import { + ComposeDeploymentAdapter, + KubernetesDeploymentAdapter, + DeploymentSecretBroker, + type SignedRunnerTask, + type ReleaseManifest, +} from '@wishforge/runtime'; import { OutboundRunner } from './runner.js'; async function main() { - const required = ['WF_CONTROL_PLANE_URL','WF_RUNNER_TOKEN','WF_TENANT_ID','WF_ENVIRONMENT_ID','WF_RUNNER_CA','WF_RUNNER_CERT','WF_RUNNER_KEY','WF_RUNNER_PUBLIC_KEYS','WF_RUNNER_STATE_DIR']; + const required = [ + 'WF_CONTROL_PLANE_URL', + 'WF_RUNNER_TOKEN', + 'WF_TENANT_ID', + 'WF_ENVIRONMENT_ID', + 'WF_RUNNER_CA', + 'WF_RUNNER_CERT', + 'WF_RUNNER_KEY', + 'WF_RUNNER_PUBLIC_KEYS', + 'WF_RUNNER_STATE_DIR', + ]; for (const key of required) if (!process.env[key]) throw new Error(`unconfigured: ${key} is required`); const base = new URL(process.env.WF_CONTROL_PLANE_URL!); if (base.protocol !== 'https:') throw new Error('Runner only connects to an HTTPS control plane with mTLS'); - const agent = new https.Agent({ ca: await readFile(process.env.WF_RUNNER_CA!), cert: await readFile(process.env.WF_RUNNER_CERT!), key: await readFile(process.env.WF_RUNNER_KEY!), rejectUnauthorized: true }); - const request = (route: string, body?: unknown): Promise => new Promise((resolve, reject) => { - const url = new URL(route, base); - if (url.origin !== base.origin) { reject(new Error('Runner request origin mismatch')); return; } - const req = https.request(url, { agent, method: body ? 'POST' : 'GET', headers: { authorization: `Bearer ${process.env.WF_RUNNER_TOKEN!}`, 'x-wishforge-tenant': process.env.WF_TENANT_ID!, 'content-type': 'application/json' }, timeout: 30_000 }, response => { - let output = ''; response.setEncoding('utf8'); - response.on('data', chunk => { output += chunk; if (output.length > 4 * 1024 * 1024) req.destroy(new Error('Response exceeded limit')); }); - response.on('end', () => { if ((response.statusCode ?? 500) >= 300) reject(new Error(`Control plane returned ${response.statusCode}`)); else { try { resolve(output ? JSON.parse(output) : null); } catch (error) { reject(error); } } }); - }); - req.on('timeout', () => req.destroy(new Error('Control plane request timed out'))); req.on('error', reject); req.end(body ? JSON.stringify(body) : undefined); + const agent = new https.Agent({ + ca: await readFile(process.env.WF_RUNNER_CA!), + cert: await readFile(process.env.WF_RUNNER_CERT!), + key: await readFile(process.env.WF_RUNNER_KEY!), + rejectUnauthorized: true, + }); + const request = (route: string, body?: unknown): Promise => + new Promise((resolve, reject) => { + const url = new URL(route, base); + if (url.origin !== base.origin) { + reject(new Error('Runner request origin mismatch')); + return; + } + const req = https.request( + url, + { + agent, + method: body ? 'POST' : 'GET', + headers: { + authorization: `Bearer ${process.env.WF_RUNNER_TOKEN!}`, + 'x-wishforge-tenant': process.env.WF_TENANT_ID!, + 'content-type': 'application/json', + }, + timeout: 30_000, + }, + response => { + let output = ''; + response.setEncoding('utf8'); + response.on('data', chunk => { + output += chunk; + if (output.length > 4 * 1024 * 1024) req.destroy(new Error('Response exceeded limit')); + }); + response.on('end', () => { + if ((response.statusCode ?? 500) >= 300) reject(new Error(`Control plane returned ${response.statusCode}`)); + else { + try { + resolve(output ? JSON.parse(output) : null); + } catch (error) { + reject(error); + } + } + }); + }, + ); + req.on('timeout', () => req.destroy(new Error('Control plane request timed out'))); + req.on('error', reject); + req.end(body ? JSON.stringify(body) : undefined); + }); + let currentTaskId: string | undefined; + const refreshPrivateProbe = async (release: ReleaseManifest): Promise => { + if (!currentTaskId) throw new Error('No active signed runner task for private probe'); + const value = (await request(`/internal/runner/releases/${encodeURIComponent(release.id)}/preview-probe`, { + taskId: currentTaskId, + })) as { data: PrivatePreviewProbe }; + return value.data; + }; + const runner = new OutboundRunner({ + tenantId: process.env.WF_TENANT_ID!, + environmentId: process.env.WF_ENVIRONMENT_ID!, + publicKeys: JSON.parse(await readFile(process.env.WF_RUNNER_PUBLIC_KEYS!, 'utf8')), + stateDirectory: process.env.WF_RUNNER_STATE_DIR!, + execute: process.env.WF_RUNNER_EXECUTE === 'true', + adapters: { + compose: new ComposeDeploymentAdapter({ + refreshPrivateProbe, + secretBroker: new DeploymentSecretBroker(), + dockerConfig: process.env.WF_DEPLOYMENT_DOCKER_CONFIG, + directory: process.env.WF_COMPOSE_DIRECTORY, + project: process.env.WF_COMPOSE_PROJECT, + healthUrl: process.env.WF_HEALTH_URL, + dockerContext: process.env.WF_DOCKER_CONTEXT, + candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE, + proxyAdminUrl: process.env.WF_CADDY_ADMIN_URL, + proxyConfigPath: process.env.WF_CADDY_PROXY_CONFIG_PATH, + proxyDialHost: process.env.WF_CADDY_DIAL_HOST, + }), + kubernetes: new KubernetesDeploymentAdapter({ + refreshPrivateProbe, + secretBroker: new DeploymentSecretBroker(), + context: process.env.WF_KUBE_CONTEXT, + namespace: process.env.WF_KUBE_NAMESPACE, + healthUrl: process.env.WF_HEALTH_URL, + candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE, + }), + }, + }); + let stopped = false; + process.once('SIGTERM', () => { + stopped = true; + }); + process.once('SIGINT', () => { + stopped = true; }); - let currentTaskId:string|undefined; - const refreshPrivateProbe=async(release:ReleaseManifest):Promise=>{if(!currentTaskId)throw new Error('No active signed runner task for private probe');const value=await request(`/internal/runner/releases/${encodeURIComponent(release.id)}/preview-probe`,{taskId:currentTaskId}) as {data:PrivatePreviewProbe};return value.data;}; - const runner = new OutboundRunner({ tenantId: process.env.WF_TENANT_ID!, environmentId: process.env.WF_ENVIRONMENT_ID!, publicKeys: JSON.parse(await readFile(process.env.WF_RUNNER_PUBLIC_KEYS!, 'utf8')), stateDirectory: process.env.WF_RUNNER_STATE_DIR!, execute: process.env.WF_RUNNER_EXECUTE === 'true', adapters: { - compose: new ComposeDeploymentAdapter({ refreshPrivateProbe, secretBroker:new DeploymentSecretBroker(),dockerConfig:process.env.WF_DEPLOYMENT_DOCKER_CONFIG, directory: process.env.WF_COMPOSE_DIRECTORY, project: process.env.WF_COMPOSE_PROJECT, healthUrl: process.env.WF_HEALTH_URL, dockerContext: process.env.WF_DOCKER_CONTEXT, candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE, proxyAdminUrl: process.env.WF_CADDY_ADMIN_URL, proxyConfigPath: process.env.WF_CADDY_PROXY_CONFIG_PATH, proxyDialHost: process.env.WF_CADDY_DIAL_HOST }), - kubernetes: new KubernetesDeploymentAdapter({ refreshPrivateProbe, secretBroker:new DeploymentSecretBroker(), context: process.env.WF_KUBE_CONTEXT, namespace: process.env.WF_KUBE_NAMESPACE, healthUrl: process.env.WF_HEALTH_URL, candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE }), - } }); - let stopped = false; process.once('SIGTERM', () => { stopped = true; }); process.once('SIGINT', () => { stopped = true; }); while (!stopped) { try { - const task = await request(`/internal/runner/tasks?environmentId=${encodeURIComponent(process.env.WF_ENVIRONMENT_ID!)}`) as SignedRunnerTask | null; + const task = (await request( + `/internal/runner/tasks?environmentId=${encodeURIComponent(process.env.WF_ENVIRONMENT_ID!)}`, + )) as SignedRunnerTask | null; if (task) { - currentTaskId=task.payload.id; + currentTaskId = task.payload.id; const result = await runner.execute(task); await request(`/internal/runner/tasks/${encodeURIComponent(task.payload.id)}/result`, { result }); } - } catch (error) { console.error(JSON.stringify({ level: 'error', message: error instanceof Error ? error.message : 'Runner poll failed' })); } - currentTaskId=undefined; + } catch (error) { + console.error( + JSON.stringify({ level: 'error', message: error instanceof Error ? error.message : 'Runner poll failed' }), + ); + } + currentTaskId = undefined; await new Promise(resolve => setTimeout(resolve, 5_000)); } agent.destroy(); } -void main().catch(error => { console.error(error.message); process.exitCode = 1; }); +void main().catch(error => { + console.error(error.message); + process.exitCode = 1; +}); diff --git a/apps/runner/src/runner.ts b/apps/runner/src/runner.ts index 5065d69..fb08f30 100644 --- a/apps/runner/src/runner.ts +++ b/apps/runner/src/runner.ts @@ -1,9 +1,24 @@ import { createHash } from 'node:crypto'; import { mkdir, open, readFile, rename, unlink, writeFile } from 'node:fs/promises'; import path from 'node:path'; -import { canonicalJson, FileNonceStore, RuntimeError, verifyRunnerTask, type DeploymentAdapter, type DeploymentResult, type SignedRunnerTask } from '@wishforge/runtime'; +import { + canonicalJson, + FileNonceStore, + RuntimeError, + verifyRunnerTask, + type DeploymentAdapter, + type DeploymentResult, + type SignedRunnerTask, +} from '@wishforge/runtime'; -export interface RunnerConfig { tenantId: string; environmentId: string; publicKeys: Record; stateDirectory: string; execute: boolean; adapters: Partial>; } +export interface RunnerConfig { + tenantId: string; + environmentId: string; + publicKeys: Record; + stateDirectory: string; + execute: boolean; + adapters: Partial>; +} export class OutboundRunner { constructor(private readonly config: RunnerConfig) {} async execute(envelope: SignedRunnerTask): Promise { @@ -15,23 +30,40 @@ export class OutboundRunner { const journal = path.join(this.config.stateDirectory, `${task.id}.json`); try { const saved = JSON.parse(await readFile(journal, 'utf8')) as { digest: string; result?: DeploymentResult }; - if (saved.digest !== digest) throw new RuntimeError('TASK_ID_REUSE', 'Task ID was reused with a different signed payload'); + if (saved.digest !== digest) + throw new RuntimeError('TASK_ID_REUSE', 'Task ID was reused with a different signed payload'); if (saved.result) return saved.result; // A previous attempt may have applied successfully before its receipt was saved. // Reconcile only; never repeat an ambiguous deployment automatically. return adapter.reconcile(task.release); - } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; } + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; + } const lockPath = path.join(this.config.stateDirectory, 'environment.lock'); - const lock = await open(lockPath, 'wx', 0o600).catch(() => { throw new RuntimeError('ENVIRONMENT_BUSY', 'An environment deployment is active; stale locks require operator reconciliation'); }); + const lock = await open(lockPath, 'wx', 0o600).catch(() => { + throw new RuntimeError( + 'ENVIRONMENT_BUSY', + 'An environment deployment is active; stale locks require operator reconciliation', + ); + }); try { - const fresh = await new FileNonceStore(path.join(this.config.stateDirectory, 'nonces')).consume(`${task.tenantId}/${task.environmentId}`, task.nonce); + const fresh = await new FileNonceStore(path.join(this.config.stateDirectory, 'nonces')).consume( + `${task.tenantId}/${task.environmentId}`, + task.nonce, + ); if (!fresh) throw new RuntimeError('TASK_REPLAY', 'Runner task nonce has already been consumed'); await writeFile(journal, JSON.stringify({ digest, state: 'started' }), { flag: 'wx', mode: 0o600 }); - const output = task.action === 'reconcile' ? await adapter.reconcile(task.release) : await adapter.deploy(task.release, { execute: this.config.execute }); + const output = + task.action === 'reconcile' + ? await adapter.reconcile(task.release) + : await adapter.deploy(task.release, { execute: this.config.execute }); const temp = `${journal}.tmp`; await writeFile(temp, JSON.stringify({ digest, state: 'finished', result: output }), { mode: 0o600 }); await rename(temp, journal); return output; - } finally { await lock.close(); await unlink(lockPath); } + } finally { + await lock.close(); + await unlink(lockPath); + } } } diff --git a/apps/runner/test/runner.test.ts b/apps/runner/test/runner.test.ts index cd7dae7..3921ade 100644 --- a/apps/runner/test/runner.test.ts +++ b/apps/runner/test/runner.test.ts @@ -10,17 +10,64 @@ import { OutboundRunner } from '../src/runner.js'; it('returns a saved receipt on duplicate delivery and reconciles an ambiguous prior operation without redeploying', async () => { const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-runner-')); try { - const keys = generateKeyPairSync('ed25519'); const now = Date.now(); - const task: RunnerTask = { schemaVersion: 1, id: 'task-1', tenantId: 'tenant', environmentId: 'env', nonce: 'nonce-1', issuedAt: new Date(now).toISOString(), expiresAt: new Date(now + 60_000).toISOString(), action: 'deploy', target: 'compose', release: { schemaVersion: 1, id: 'release', tenantId: 'tenant', environmentId: 'env', sourceCommit: 'a'.repeat(40), image: `registry/web@sha256:${'b'.repeat(64)}`, containerPort: 3000, healthPath: '/health' } }; - let deploys = 0; let reconciles = 0; - const runner = new OutboundRunner({ tenantId: 'tenant', environmentId: 'env', publicKeys: { key: keys.publicKey.export({ type: 'spki', format: 'pem' }).toString() }, stateDirectory: directory, execute: true, adapters: { compose: { render: () => '{}', deploy: async () => { deploys++; return { releaseId: 'release', target: 'compose', status: 'verified', evidence: {} }; }, reconcile: async () => { reconciles++; return { releaseId: 'release', target: 'compose', status: 'applied', evidence: {} }; } } } }); + const keys = generateKeyPairSync('ed25519'); + const now = Date.now(); + const task: RunnerTask = { + schemaVersion: 1, + id: 'task-1', + tenantId: 'tenant', + environmentId: 'env', + nonce: 'nonce-1', + issuedAt: new Date(now).toISOString(), + expiresAt: new Date(now + 60_000).toISOString(), + action: 'deploy', + target: 'compose', + release: { + schemaVersion: 1, + id: 'release', + tenantId: 'tenant', + environmentId: 'env', + sourceCommit: 'a'.repeat(40), + image: `registry/web@sha256:${'b'.repeat(64)}`, + containerPort: 3000, + healthPath: '/health', + }, + }; + let deploys = 0; + let reconciles = 0; + const runner = new OutboundRunner({ + tenantId: 'tenant', + environmentId: 'env', + publicKeys: { key: keys.publicKey.export({ type: 'spki', format: 'pem' }).toString() }, + stateDirectory: directory, + execute: true, + adapters: { + compose: { + render: () => '{}', + deploy: async () => { + deploys++; + return { releaseId: 'release', target: 'compose', status: 'verified', evidence: {} }; + }, + reconcile: async () => { + reconciles++; + return { releaseId: 'release', target: 'compose', status: 'applied', evidence: {} }; + }, + }, + }, + }); const envelope = signRunnerTask(task, 'key', keys.privateKey); expect((await runner.execute(envelope)).status).toBe('verified'); expect((await runner.execute(envelope)).status).toBe('verified'); expect(deploys).toBe(1); const ambiguous = signRunnerTask({ ...task, id: 'task-2', nonce: 'nonce-2' }, 'key', keys.privateKey); - await writeFile(path.join(directory, 'task-2.json'), JSON.stringify({ digest: createHash('sha256').update(canonicalJson(ambiguous)).digest('hex'), state: 'started' })); + await writeFile( + path.join(directory, 'task-2.json'), + JSON.stringify({ digest: createHash('sha256').update(canonicalJson(ambiguous)).digest('hex'), state: 'started' }), + ); expect((await runner.execute(ambiguous)).status).toBe('applied'); - expect(reconciles).toBe(1); expect(deploys).toBe(1); - } finally { await rm(directory, { recursive: true, force: true }); } + expect(reconciles).toBe(1); + expect(deploys).toBe(1); + } finally { + await rm(directory, { recursive: true, force: true }); + } }); diff --git a/apps/runner/tsconfig.json b/apps/runner/tsconfig.json index 7f4b8b9..58d1749 100644 --- a/apps/runner/tsconfig.json +++ b/apps/runner/tsconfig.json @@ -1 +1,5 @@ -{ "extends": "../../tsconfig.base.json", "compilerOptions": { "noEmit": true, "types": ["node"] }, "include": ["src/**/*.ts", "test/**/*.ts"] } +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { "noEmit": true, "types": ["node"] }, + "include": ["src/**/*.ts", "test/**/*.ts"] +} diff --git a/apps/web/next-env.d.ts b/apps/web/next-env.d.ts index ce4e94a..a419cbe 100644 --- a/apps/web/next-env.d.ts +++ b/apps/web/next-env.d.ts @@ -1,7 +1,7 @@ /// /// -import "./.next/types/routes.d.ts"; -import "./.next/types/root-params.d.ts"; +import "./.next/dev/types/routes.d.ts"; +import "./.next/dev/types/root-params.d.ts"; // NOTE: This file should not be edited // see https://nextjs.org/docs/app/api-reference/config/typescript for more information. diff --git a/apps/web/next.config.ts b/apps/web/next.config.ts index db9a8ea..fc7f2d1 100644 --- a/apps/web/next.config.ts +++ b/apps/web/next.config.ts @@ -1,19 +1,19 @@ -import type { NextConfig } from "next"; +import type { NextConfig } from 'next'; const config: NextConfig = { - output: "standalone", + output: 'standalone', poweredByHeader: false, agentRules: false, devIndicators: false, - experimental: { optimizePackageImports: ["lucide-react"] }, + experimental: { optimizePackageImports: ['lucide-react'] }, async headers() { return [ { - source: "/:path*", + source: '/:path*', headers: [ - { key: "X-Content-Type-Options", value: "nosniff" }, - { key: "Referrer-Policy", value: "same-origin" }, - { key: "X-Frame-Options", value: "SAMEORIGIN" }, + { key: 'X-Content-Type-Options', value: 'nosniff' }, + { key: 'Referrer-Policy', value: 'same-origin' }, + { key: 'X-Frame-Options', value: 'SAMEORIGIN' }, ], }, ]; diff --git a/apps/web/src/app/(console)/environments/page.tsx b/apps/web/src/app/(console)/environments/page.tsx index f509e2c..3c9d720 100644 --- a/apps/web/src/app/(console)/environments/page.tsx +++ b/apps/web/src/app/(console)/environments/page.tsx @@ -1,4 +1,4 @@ -import { EnvironmentsView } from "@/features/environments"; +import { EnvironmentsView } from '@/features/environments'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/evolution/page.tsx b/apps/web/src/app/(console)/evolution/page.tsx index 056bf55..640674a 100644 --- a/apps/web/src/app/(console)/evolution/page.tsx +++ b/apps/web/src/app/(console)/evolution/page.tsx @@ -1,4 +1,4 @@ -import { EvolutionView } from "@/features/evolution"; +import { EvolutionView } from '@/features/evolution'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/layout.tsx b/apps/web/src/app/(console)/layout.tsx index 7f1f5be..f498e28 100644 --- a/apps/web/src/app/(console)/layout.tsx +++ b/apps/web/src/app/(console)/layout.tsx @@ -1,4 +1,4 @@ -import { ConsoleShell } from "@/components/console-shell"; +import { ConsoleShell } from '@/components/console-shell'; export default function Layout({ children }: { children: React.ReactNode }) { return {children}; } diff --git a/apps/web/src/app/(console)/page.tsx b/apps/web/src/app/(console)/page.tsx index 76c1b2e..0ca4620 100644 --- a/apps/web/src/app/(console)/page.tsx +++ b/apps/web/src/app/(console)/page.tsx @@ -1,4 +1,4 @@ -import { DashboardView } from "@/features/dashboard"; +import { DashboardView } from '@/features/dashboard'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/projects/[id]/page.tsx b/apps/web/src/app/(console)/projects/[id]/page.tsx index 0582b0e..9d44445 100644 --- a/apps/web/src/app/(console)/projects/[id]/page.tsx +++ b/apps/web/src/app/(console)/projects/[id]/page.tsx @@ -1,9 +1,5 @@ -import { ProjectView } from "@/features/projects"; -export default async function Page({ - params, -}: { - params: Promise<{ id: string }>; -}) { +import { ProjectView } from '@/features/projects'; +export default async function Page({ params }: { params: Promise<{ id: string }> }) { const { id } = await params; return ; } diff --git a/apps/web/src/app/(console)/projects/new/page.tsx b/apps/web/src/app/(console)/projects/new/page.tsx index 87efe7a..55b1ee8 100644 --- a/apps/web/src/app/(console)/projects/new/page.tsx +++ b/apps/web/src/app/(console)/projects/new/page.tsx @@ -1,4 +1,4 @@ -import { ProjectsView } from "@/features/projects"; +import { ProjectsView } from '@/features/projects'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/projects/page.tsx b/apps/web/src/app/(console)/projects/page.tsx index d43a4ea..aa0cc78 100644 --- a/apps/web/src/app/(console)/projects/page.tsx +++ b/apps/web/src/app/(console)/projects/page.tsx @@ -1,4 +1,4 @@ -import { ProjectsView } from "@/features/projects"; +import { ProjectsView } from '@/features/projects'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/providers/page.tsx b/apps/web/src/app/(console)/providers/page.tsx index 8fc9884..bbf5f88 100644 --- a/apps/web/src/app/(console)/providers/page.tsx +++ b/apps/web/src/app/(console)/providers/page.tsx @@ -1,4 +1,4 @@ -import { ProvidersView } from "@/features/providers"; +import { ProvidersView } from '@/features/providers'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/releases/page.tsx b/apps/web/src/app/(console)/releases/page.tsx index d608bca..c3fb051 100644 --- a/apps/web/src/app/(console)/releases/page.tsx +++ b/apps/web/src/app/(console)/releases/page.tsx @@ -1,4 +1,4 @@ -import { ReleasesView } from "@/features/releases"; +import { ReleasesView } from '@/features/releases'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/runs/[id]/page.tsx b/apps/web/src/app/(console)/runs/[id]/page.tsx index 12ebe6d..3692bf2 100644 --- a/apps/web/src/app/(console)/runs/[id]/page.tsx +++ b/apps/web/src/app/(console)/runs/[id]/page.tsx @@ -1,9 +1,5 @@ -import { RunDetailView } from "@/features/runs"; -export default async function Page({ - params, -}: { - params: Promise<{ id: string }>; -}) { +import { RunDetailView } from '@/features/runs'; +export default async function Page({ params }: { params: Promise<{ id: string }> }) { const { id } = await params; return ; } diff --git a/apps/web/src/app/(console)/runs/new/page.tsx b/apps/web/src/app/(console)/runs/new/page.tsx index 08ba67d..11f7283 100644 --- a/apps/web/src/app/(console)/runs/new/page.tsx +++ b/apps/web/src/app/(console)/runs/new/page.tsx @@ -1,4 +1,4 @@ -import { NewWishView } from "@/features/runs"; +import { NewWishView } from '@/features/runs'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/runs/page.tsx b/apps/web/src/app/(console)/runs/page.tsx index c372974..80dec5a 100644 --- a/apps/web/src/app/(console)/runs/page.tsx +++ b/apps/web/src/app/(console)/runs/page.tsx @@ -1,4 +1,4 @@ -import { RunsView } from "@/features/runs"; +import { RunsView } from '@/features/runs'; export default function Page() { return ; } diff --git a/apps/web/src/app/(console)/usage/page.tsx b/apps/web/src/app/(console)/usage/page.tsx index fc12037..121e274 100644 --- a/apps/web/src/app/(console)/usage/page.tsx +++ b/apps/web/src/app/(console)/usage/page.tsx @@ -1,4 +1,4 @@ -import { UsageView } from "@/features/usage"; +import { UsageView } from '@/features/usage'; export default function Page() { return ; } diff --git a/apps/web/src/app/api/[...path]/route.ts b/apps/web/src/app/api/[...path]/route.ts index 7d1ea9c..2bf4469 100644 --- a/apps/web/src/app/api/[...path]/route.ts +++ b/apps/web/src/app/api/[...path]/route.ts @@ -1,93 +1,62 @@ -import { NextRequest, NextResponse } from "next/server"; -import { publicOrigin, tokenFor } from "@/lib/auth"; -export const runtime = "nodejs"; -export const dynamic = "force-dynamic"; +import { NextRequest, NextResponse } from 'next/server'; +import { publicOrigin, tokenFor } from '@/lib/auth'; +export const runtime = 'nodejs'; +export const dynamic = 'force-dynamic'; type Context = { params: Promise<{ path: string[] }> }; async function proxy(request: NextRequest, context: Context) { const { path } = await context.params; - if ( - path.some( - (part) => - !part || - part === ".." || - part === "." || - part.includes("/") || - part.includes("\\"), - ) - ) - return NextResponse.json( - { error: { code: "INVALID_PATH", message: "无效的 API 路径。" } }, - { status: 400 }, - ); - if (!["GET", "HEAD"].includes(request.method)) { - const origin = request.headers.get("origin"); - if ( - (origin && origin !== publicOrigin(request)) || - request.headers.get("sec-fetch-site") === "cross-site" - ) - return NextResponse.json( - { error: { code: "ORIGIN_DENIED", message: "请求来源不被允许。" } }, - { status: 403 }, - ); + if (path.some(part => !part || part === '..' || part === '.' || part.includes('/') || part.includes('\\'))) + return NextResponse.json({ error: { code: 'INVALID_PATH', message: '无效的 API 路径。' } }, { status: 400 }); + if (!['GET', 'HEAD'].includes(request.method)) { + const origin = request.headers.get('origin'); + if ((origin && origin !== publicOrigin(request)) || request.headers.get('sec-fetch-site') === 'cross-site') + return NextResponse.json({ error: { code: 'ORIGIN_DENIED', message: '请求来源不被允许。' } }, { status: 403 }); } const token = await tokenFor(request); if (!token) return NextResponse.json( { error: { - code: "UNAUTHENTICATED", - message: "需要有效的工作空间登录会话。", + code: 'UNAUTHENTICATED', + message: '需要有效的工作空间登录会话。', }, }, { status: 401 }, ); - const base = (process.env.WF_API_URL ?? "http://127.0.0.1:4000").replace( - /\/$/, - "", - ); - const url = `${base}/v1/${path.map(encodeURIComponent).join("/")}${request.nextUrl.search}`; + const base = (process.env.WF_API_URL ?? 'http://127.0.0.1:4000').replace(/\/$/, ''); + const url = `${base}/v1/${path.map(encodeURIComponent).join('/')}${request.nextUrl.search}`; const headers = new Headers({ Authorization: `Bearer ${token}` }); - for (const key of [ - "content-type", - "accept", - "idempotency-key", - "last-event-id", - ]) { + for (const key of ['content-type', 'accept', 'idempotency-key', 'last-event-id']) { const value = request.headers.get(key); if (value) headers.set(key, value); } - const stream = path.at(-1) === "stream"; + const stream = path.at(-1) === 'stream'; try { const upstream = await fetch(url, { method: request.method, headers, - body: ["GET", "HEAD"].includes(request.method) - ? undefined - : await request.arrayBuffer(), - cache: "no-store", - redirect: "manual", - signal: stream - ? request.signal - : AbortSignal.any([request.signal, AbortSignal.timeout(30000)]), + body: ['GET', 'HEAD'].includes(request.method) ? undefined : await request.arrayBuffer(), + cache: 'no-store', + redirect: 'manual', + signal: stream ? request.signal : AbortSignal.any([request.signal, AbortSignal.timeout(30000)]), }); if (upstream.status >= 300 && upstream.status < 400) return NextResponse.json( { error: { - code: "UPSTREAM_REDIRECT", - message: "控制面返回了意外重定向。", + code: 'UPSTREAM_REDIRECT', + message: '控制面返回了意外重定向。', }, }, { status: 502 }, ); const responseHeaders = new Headers({ - "Cache-Control": "no-store", - "Content-Type": - upstream.headers.get("content-type") ?? "application/json", + 'Cache-Control': 'no-store', + 'Content-Type': upstream.headers.get('content-type') ?? 'application/json', }); if (stream) { - responseHeaders.set("X-Accel-Buffering", "no"); - responseHeaders.set("Connection", "keep-alive"); + responseHeaders.set('X-Accel-Buffering', 'no'); + responseHeaders.set('Connection', 'keep-alive'); } return new Response(upstream.body, { status: upstream.status, @@ -97,8 +66,8 @@ async function proxy(request: NextRequest, context: Context) { return NextResponse.json( { error: { - code: "CONTROL_PLANE_UNAVAILABLE", - message: "无法连接控制面。请检查服务地址与运行状态。", + code: 'CONTROL_PLANE_UNAVAILABLE', + message: '无法连接控制面。请检查服务地址与运行状态。', }, }, { status: 502 }, diff --git a/apps/web/src/app/api/session/route.ts b/apps/web/src/app/api/session/route.ts index 698553e..cdddf7c 100644 --- a/apps/web/src/app/api/session/route.ts +++ b/apps/web/src/app/api/session/route.ts @@ -1,16 +1,16 @@ -import { NextRequest, NextResponse } from "next/server"; -import { getSession, isDevelopmentAuth, oidcConfigured } from "@/lib/auth"; +import { NextRequest, NextResponse } from 'next/server'; +import { getSession, isDevelopmentAuth, oidcConfigured } from '@/lib/auth'; export async function GET(request: NextRequest) { const session = await getSession(request); return NextResponse.json( { data: { authenticated: isDevelopmentAuth() || Boolean(session), - mode: isDevelopmentAuth() ? "development" : "oidc", + mode: isDevelopmentAuth() ? 'development' : 'oidc', name: session?.name, oidcConfigured: oidcConfigured(), }, }, - { headers: { "Cache-Control": "no-store" } }, + { headers: { 'Cache-Control': 'no-store' } }, ); } diff --git a/apps/web/src/app/delivery/[environmentId]/launch/route.ts b/apps/web/src/app/delivery/[environmentId]/launch/route.ts index 09b99c7..4739631 100644 --- a/apps/web/src/app/delivery/[environmentId]/launch/route.ts +++ b/apps/web/src/app/delivery/[environmentId]/launch/route.ts @@ -1,57 +1,83 @@ -import { NextRequest, NextResponse } from "next/server"; -import { tokenFor } from "@/lib/auth"; -export const runtime = "nodejs"; -export const dynamic = "force-dynamic"; -export async function GET( - request: NextRequest, - { params }: { params: Promise<{ environmentId: string }> }, -) { +import { NextRequest, NextResponse } from 'next/server'; +import { tokenFor } from '@/lib/auth'; +export const runtime = 'nodejs'; +export const dynamic = 'force-dynamic'; +export async function GET(request: NextRequest, { params }: { params: Promise<{ environmentId: string }> }) { const token = await tokenFor(request); - if (!token) return NextResponse.redirect(new URL( - `/login?returnTo=${encodeURIComponent(request.nextUrl.pathname)}`, - request.nextUrl.origin, - )); + if (!token) + return NextResponse.redirect( + new URL(`/login?returnTo=${encodeURIComponent(request.nextUrl.pathname)}`, request.nextUrl.origin), + ); const { environmentId } = await params; - const api = (process.env.WF_API_URL ?? "http://127.0.0.1:4000").replace(/\/$/, ""); + const api = (process.env.WF_API_URL ?? 'http://127.0.0.1:4000').replace(/\/$/, ''); try { const response = await fetch(`${api}/v1/environments/${encodeURIComponent(environmentId)}/preview-launch`, { - method: "POST", - headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" }, - body: JSON.stringify({}), cache: "no-store", redirect: "error", + method: 'POST', + headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' }, + body: JSON.stringify({}), + cache: 'no-store', + redirect: 'error', signal: AbortSignal.any([request.signal, AbortSignal.timeout(15000)]), }); - if (!response.ok) return NextResponse.json({ error: { - code: "PRIVATE_DELIVERY_DENIED", - message: response.status === 403 ? "你没有权限访问此私有成果。" : - response.status === 401 ? "登录凭据已失效,请重新登录后访问私有成果。" : - "私有成果尚未就绪或访问配置不可用。", - } }, { status: response.status, headers: { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer" } }); + if (!response.ok) + return NextResponse.json( + { + error: { + code: 'PRIVATE_DELIVERY_DENIED', + message: + response.status === 403 + ? '你没有权限访问此私有成果。' + : response.status === 401 + ? '登录凭据已失效,请重新登录后访问私有成果。' + : '私有成果尚未就绪或访问配置不可用。', + }, + }, + { status: response.status, headers: { 'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer' } }, + ); const payload = (await response.json()) as { data?: { launchUrl?: string; expiresAt?: string; visibility?: string }; }; - const expiresAt = Date.parse(payload.data?.expiresAt ?? ""); - if (payload.data?.visibility !== "private" || typeof payload.data.launchUrl !== "string" || - !Number.isFinite(expiresAt) || expiresAt <= Date.now() || expiresAt > Date.now() + 60000) { - throw new Error("Invalid private delivery authorization"); + const expiresAt = Date.parse(payload.data?.expiresAt ?? ''); + if ( + payload.data?.visibility !== 'private' || + typeof payload.data.launchUrl !== 'string' || + !Number.isFinite(expiresAt) || + expiresAt <= Date.now() || + expiresAt > Date.now() + 60000 + ) { + throw new Error('Invalid private delivery authorization'); } // The authenticated control plane owns environment routing and validates its // dedicated origin. No gateway map or gateway credential belongs in this BFF. const url = new URL(payload.data.launchUrl); - const localHttp = process.env.NODE_ENV !== "production" && url.protocol === "http:" && - ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname); + const localHttp = + process.env.NODE_ENV !== 'production' && + url.protocol === 'http:' && + ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname); const keys = [...url.searchParams.keys()]; - if ((url.protocol !== "https:" && !localHttp) || url.username || url.password || url.hash || - url.pathname !== "/_wishforge/session" || keys.length !== 1 || keys[0] !== "code" || - !/^[A-Za-z0-9_-]{43}$/.test(url.searchParams.get("code") ?? "")) { - throw new Error("Invalid private delivery redirect"); + if ( + (url.protocol !== 'https:' && !localHttp) || + url.username || + url.password || + url.hash || + url.pathname !== '/_wishforge/session' || + keys.length !== 1 || + keys[0] !== 'code' || + !/^[A-Za-z0-9_-]{43}$/.test(url.searchParams.get('code') ?? '') + ) { + throw new Error('Invalid private delivery redirect'); } const result = NextResponse.redirect(url, 303); - result.headers.set("Cache-Control", "no-store"); - result.headers.set("Referrer-Policy", "no-referrer"); + result.headers.set('Cache-Control', 'no-store'); + result.headers.set('Referrer-Policy', 'no-referrer'); return result; } catch { - return NextResponse.json({ error: { code: "PRIVATE_DELIVERY_UNAVAILABLE", message: "无法取得私有成果访问授权,请稍后重试。" } }, { - status: 503, headers: { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer" }, - }); + return NextResponse.json( + { error: { code: 'PRIVATE_DELIVERY_UNAVAILABLE', message: '无法取得私有成果访问授权,请稍后重试。' } }, + { + status: 503, + headers: { 'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer' }, + }, + ); } } diff --git a/apps/web/src/app/globals.css b/apps/web/src/app/globals.css index 47eda50..f0e8d50 100644 --- a/apps/web/src/app/globals.css +++ b/apps/web/src/app/globals.css @@ -13,9 +13,7 @@ --red: #be444c; --radius: 7px; --sidebar: 248px; - --font: - "DM Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", - "Microsoft YaHei", sans-serif; + --font: 'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif; } * { box-sizing: border-box; @@ -81,7 +79,7 @@ h3 { } code, pre { - font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace; + font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', monospace; font-size: 12px; } button:focus-visible, @@ -556,7 +554,7 @@ button { min-height: 62px; } .journey li:not(:last-child):before { - content: ""; + content: ''; position: absolute; top: 33px; bottom: 7px; @@ -1132,7 +1130,7 @@ button { position: relative; } .form-guide li:before { - content: "0" counter(guide); + content: '0' counter(guide); position: absolute; left: 0; top: 1px; @@ -1413,7 +1411,7 @@ button { padding-bottom: 24px; } .event-list li:not(:last-child):before { - content: ""; + content: ''; position: absolute; top: 10px; bottom: 0; @@ -2650,6 +2648,13 @@ button { border-bottom: 1px solid var(--border); } -.workspace-input-panel .form-stack { align-items: stretch; } +.workspace-input-panel .form-stack { + align-items: stretch; +} -.requirement-amendment > summary { cursor: pointer; padding: 0 22px 18px; color: var(--muted); font-size: 13px; } +.requirement-amendment > summary { + cursor: pointer; + padding: 0 22px 18px; + color: var(--muted); + font-size: 13px; +} diff --git a/apps/web/src/app/ide/[id]/launch/route.ts b/apps/web/src/app/ide/[id]/launch/route.ts index a70de39..f90f2f2 100644 --- a/apps/web/src/app/ide/[id]/launch/route.ts +++ b/apps/web/src/app/ide/[id]/launch/route.ts @@ -1,74 +1,74 @@ -import { NextRequest, NextResponse } from "next/server"; -import { tokenFor } from "@/lib/auth"; -export const runtime = "nodejs"; -export const dynamic = "force-dynamic"; -export async function GET( - request: NextRequest, - { params }: { params: Promise<{ id: string }> }, -) { +import { NextRequest, NextResponse } from 'next/server'; +import { tokenFor } from '@/lib/auth'; +export const runtime = 'nodejs'; +export const dynamic = 'force-dynamic'; +export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) { const token = await tokenFor(request); if (!token) return NextResponse.redirect( - new URL( - `/login?returnTo=${encodeURIComponent(request.nextUrl.pathname)}`, - request.nextUrl.origin, - ), + new URL(`/login?returnTo=${encodeURIComponent(request.nextUrl.pathname)}`, request.nextUrl.origin), ); const { id } = await params; - const api = (process.env.WF_API_URL ?? "http://127.0.0.1:4000").replace(/\/$/, ""); + const api = (process.env.WF_API_URL ?? 'http://127.0.0.1:4000').replace(/\/$/, ''); try { const response = await fetch(`${api}/v1/workspaces/${encodeURIComponent(id)}/launch`, { - method: "POST", + method: 'POST', headers: { Authorization: `Bearer ${token}`, - "Content-Type": "application/json", + 'Content-Type': 'application/json', }, body: JSON.stringify({}), - cache: "no-store", - redirect: "error", + cache: 'no-store', + redirect: 'error', signal: AbortSignal.any([request.signal, AbortSignal.timeout(15000)]), }); if (!response.ok) return NextResponse.json( { error: { - code: "IDE_LAUNCH_DENIED", - message: - response.status === 403 - ? "你没有权限打开此工作空间。" - : "工作空间尚未就绪或控制面拒绝了连接。", + code: 'IDE_LAUNCH_DENIED', + message: response.status === 403 ? '你没有权限打开此工作空间。' : '工作空间尚未就绪或控制面拒绝了连接。', }, }, - { status: response.status, headers: { "Cache-Control": "no-store" } }, + { status: response.status, headers: { 'Cache-Control': 'no-store' } }, ); const payload = (await response.json()) as { data?: { launchUrl?: string; expiresIn?: number }; }; - if (typeof payload.data?.launchUrl !== "string" || payload.data.expiresIn !== 30) - throw new Error("Invalid workspace launch response"); + if (typeof payload.data?.launchUrl !== 'string' || payload.data.expiresIn !== 30) + throw new Error('Invalid workspace launch response'); // The authenticated control plane resolves the tenant's gateway and verifies // its origin. This BFF never owns tenant routing or gateway service tokens. const url = new URL(payload.data.launchUrl); - const localHttp = process.env.NODE_ENV !== "production" && url.protocol === "http:" && - ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname); + const localHttp = + process.env.NODE_ENV !== 'production' && + url.protocol === 'http:' && + ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname); const query = [...url.searchParams.keys()]; - if ((url.protocol !== "https:" && !localHttp) || url.username || url.password || url.hash || - url.pathname !== "/session" || query.length !== 1 || query[0] !== "code" || - !/^[A-Za-z0-9_-]{20,256}$/.test(url.searchParams.get("code") ?? "")) - throw new Error("Invalid workspace session redirect"); + if ( + (url.protocol !== 'https:' && !localHttp) || + url.username || + url.password || + url.hash || + url.pathname !== '/session' || + query.length !== 1 || + query[0] !== 'code' || + !/^[A-Za-z0-9_-]{20,256}$/.test(url.searchParams.get('code') ?? '') + ) + throw new Error('Invalid workspace session redirect'); const result = NextResponse.redirect(url, 303); - result.headers.set("Cache-Control", "no-store"); - result.headers.set("Referrer-Policy", "no-referrer"); + result.headers.set('Cache-Control', 'no-store'); + result.headers.set('Referrer-Policy', 'no-referrer'); return result; } catch { return NextResponse.json( { error: { - code: "IDE_UNAVAILABLE", - message: "无法取得工作空间启动链接。请检查控制面与运行环境配置。", + code: 'IDE_UNAVAILABLE', + message: '无法取得工作空间启动链接。请检查控制面与运行环境配置。', }, }, - { status: 503, headers: { "Cache-Control": "no-store" } }, + { status: 503, headers: { 'Cache-Control': 'no-store' } }, ); } } diff --git a/apps/web/src/app/layout.tsx b/apps/web/src/app/layout.tsx index fc92124..306bad3 100644 --- a/apps/web/src/app/layout.tsx +++ b/apps/web/src/app/layout.tsx @@ -1,12 +1,10 @@ -import type { Metadata } from "next"; -import "./globals.css"; +import type { Metadata } from 'next'; +import './globals.css'; export const metadata: Metadata = { - title: { default: "WishForge · 工作空间", template: "%s · WishForge" }, - description: "从一句愿望,到有证据的交付。", + title: { default: 'WishForge · 工作空间', template: '%s · WishForge' }, + description: '从一句愿望,到有证据的交付。', }; -export default function RootLayout({ - children, -}: Readonly<{ children: React.ReactNode }>) { +export default function RootLayout({ children }: Readonly<{ children: React.ReactNode }>) { return ( {children} diff --git a/apps/web/src/app/login/callback/route.ts b/apps/web/src/app/login/callback/route.ts index ca23b0e..2747d24 100644 --- a/apps/web/src/app/login/callback/route.ts +++ b/apps/web/src/app/login/callback/route.ts @@ -1,4 +1,4 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest, NextResponse } from 'next/server'; import { completeLogin, cookieOptions, @@ -8,36 +8,28 @@ import { SESSION_COOKIE, unseal, type OAuthState, -} from "@/lib/auth"; +} from '@/lib/auth'; export async function GET(request: NextRequest) { - const state = await unseal( - request.cookies.get(OAUTH_COOKIE)?.value, - ); - const code = request.nextUrl.searchParams.get("code"); - if ( - !state || - !code || - state.state !== request.nextUrl.searchParams.get("state") - ) + const state = await unseal(request.cookies.get(OAUTH_COOKIE)?.value); + const code = request.nextUrl.searchParams.get('code'); + if (!state || !code || state.state !== request.nextUrl.searchParams.get('state')) return NextResponse.json( { error: { - code: "INVALID_OAUTH_STATE", - message: "登录会话已失效,请重新登录。", + code: 'INVALID_OAUTH_STATE', + message: '登录会话已失效,请重新登录。', }, }, { status: 400 }, ); try { const session = await completeLogin(request, code, state); - const response = NextResponse.redirect( - new URL(safeReturnTo(state.returnTo), publicOrigin(request)), - ); + const response = NextResponse.redirect(new URL(safeReturnTo(state.returnTo), publicOrigin(request))); response.cookies.set(SESSION_COOKIE, session.cookie, { ...cookieOptions(request), maxAge: session.ttl, }); - response.cookies.set(OAUTH_COOKIE, "", { + response.cookies.set(OAUTH_COOKIE, '', { ...cookieOptions(request), maxAge: 0, }); @@ -46,13 +38,13 @@ export async function GET(request: NextRequest) { const response = NextResponse.json( { error: { - code: "LOGIN_FAILED", - message: "登录凭据校验失败,请重新登录。", + code: 'LOGIN_FAILED', + message: '登录凭据校验失败,请重新登录。', }, }, { status: 401 }, ); - response.cookies.set(OAUTH_COOKIE, "", { + response.cookies.set(OAUTH_COOKIE, '', { ...cookieOptions(request), maxAge: 0, }); diff --git a/apps/web/src/app/login/route.ts b/apps/web/src/app/login/route.ts index b448759..c838c40 100644 --- a/apps/web/src/app/login/route.ts +++ b/apps/web/src/app/login/route.ts @@ -1,4 +1,4 @@ -import { NextRequest, NextResponse } from "next/server"; +import { NextRequest, NextResponse } from 'next/server'; import { cookieOptions, discovery, @@ -10,18 +10,17 @@ import { randomToken, safeReturnTo, seal, -} from "@/lib/auth"; +} from '@/lib/auth'; export async function GET(request: NextRequest) { - if (isDevelopmentAuth()) - return NextResponse.redirect(new URL("/", publicOrigin(request))); + if (isDevelopmentAuth()) return NextResponse.redirect(new URL('/', publicOrigin(request))); if (!oidcConfigured()) return new Response( '登录 · WishForge

连接你的工作空间

此部署尚未配置组织登录。请联系管理员配置 OIDC 身份服务。

WishForge 不提供默认公开账户或密码。

返回工作空间
', { status: 503, headers: { - "Content-Type": "text/html; charset=utf-8", - "Cache-Control": "no-store", + 'Content-Type': 'text/html; charset=utf-8', + 'Cache-Control': 'no-store', }, }, ); @@ -33,13 +32,13 @@ export async function GET(request: NextRequest) { const url = new URL(metadata.authorization_endpoint); url.search = new URLSearchParams({ client_id: process.env.OIDC_CLIENT_ID!, - response_type: "code", - scope: "openid profile email", + response_type: 'code', + scope: 'openid profile email', redirect_uri: `${publicOrigin(request)}/login/callback`, state, nonce, code_challenge: pkceChallenge(verifier), - code_challenge_method: "S256", + code_challenge_method: 'S256', }).toString(); const response = NextResponse.redirect(url); response.cookies.set( @@ -49,7 +48,7 @@ export async function GET(request: NextRequest) { state, nonce, verifier, - returnTo: safeReturnTo(request.nextUrl.searchParams.get("returnTo")), + returnTo: safeReturnTo(request.nextUrl.searchParams.get('returnTo')), }, 600, ), @@ -60,8 +59,8 @@ export async function GET(request: NextRequest) { return NextResponse.json( { error: { - code: "OIDC_UNAVAILABLE", - message: "身份服务暂时不可用,请稍后重试。", + code: 'OIDC_UNAVAILABLE', + message: '身份服务暂时不可用,请稍后重试。', }, }, { status: 503 }, diff --git a/apps/web/src/app/logout/route.ts b/apps/web/src/app/logout/route.ts index 71fa440..924b6d8 100644 --- a/apps/web/src/app/logout/route.ts +++ b/apps/web/src/app/logout/route.ts @@ -1,13 +1,9 @@ -import { NextRequest, NextResponse } from "next/server"; -import { clearAuth, publicOrigin } from "@/lib/auth"; +import { NextRequest, NextResponse } from 'next/server'; +import { clearAuth, publicOrigin } from '@/lib/auth'; export async function POST(request: NextRequest) { - const origin = request.headers.get("origin"); - if (origin && origin !== publicOrigin(request)) - return new Response("Forbidden", { status: 403 }); - const response = NextResponse.redirect( - new URL("/", publicOrigin(request)), - 303, - ); + const origin = request.headers.get('origin'); + if (origin && origin !== publicOrigin(request)) return new Response('Forbidden', { status: 403 }); + const response = NextResponse.redirect(new URL('/', publicOrigin(request)), 303); clearAuth(response, request); return response; } diff --git a/apps/web/src/components/console-shell.tsx b/apps/web/src/components/console-shell.tsx index 42db992..ee8ea0c 100644 --- a/apps/web/src/components/console-shell.tsx +++ b/apps/web/src/components/console-shell.tsx @@ -1,4 +1,4 @@ -"use client"; +'use client'; import { Activity, ArrowUpRight, @@ -14,22 +14,22 @@ import { Settings2, Sparkle, X, -} from "lucide-react"; -import Link from "next/link"; -import { usePathname } from "next/navigation"; -import { useState } from "react"; -import { useSWRConfig } from "swr"; -import type { ReactNode } from "react"; -import { useResource } from "@/lib/api"; +} from 'lucide-react'; +import Link from 'next/link'; +import { usePathname } from 'next/navigation'; +import { useState } from 'react'; +import { useSWRConfig } from 'swr'; +import type { ReactNode } from 'react'; +import { useResource } from '@/lib/api'; const navigation = [ - ["/", "工作概览", Home], - ["/projects", "项目空间", Folder], - ["/runs", "运行记录", Activity], - ["/releases", "部署与审批", Box], - ["/providers", "执行器", Settings2], - ["/evolution", "演进实验", FlaskConical], - ["/usage", "用量", BarChart3], + ['/', '工作概览', Home], + ['/projects', '项目空间', Folder], + ['/runs', '运行记录', Activity], + ['/releases', '部署与审批', Box], + ['/providers', '执行器', Settings2], + ['/evolution', '演进实验', FlaskConical], + ['/usage', '用量', BarChart3], ] as const; export function ConsoleShell({ children }: { children: ReactNode }) { const pathname = usePathname(); @@ -39,23 +39,15 @@ export function ConsoleShell({ children }: { children: ReactNode }) { authenticated: boolean; mode: string; name?: string; - }>("/session"); - const current = navigation.find(([href]) => - href === "/" ? pathname === "/" : pathname.startsWith(href), - ); + }>('/session'); + const current = navigation.find(([href]) => (href === '/' ? pathname === '/' : pathname.startsWith(href))); return (
跳转到主要内容 - {open ? ( -