wishforge 9dde59e8c6 ✨ Diagnose reported problems from read-only production evidence
Add the read-only Fanyu diagnostic client (cluster_overview, task_diagnosis, request_trace, egress) with private file-based key loading, mutual CA verification, a 40 req/min token bucket, short TTL caches, and strict separation between unavailable telemetry and healthy state.

Feed the last ten conversation turns into streaming answers, verify every reported fact against the server-side evidence ledger before persisting a diagnosis, withhold model thinking frames from the client stream, classify stream error codes, and cover secret isolation and diagnosis downgrades with dedicated unit tests.
2026-11-11 07:59:03 -08:00

1094 lines
45 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
HttpCode,
Inject,
Param,
Patch,
Post,
Query,
Req,
Res,
type ExceptionFilter,
Catch,
HttpException,
type ArgumentsHost,
} from '@nestjs/common';
import type { Request, Response } from 'express';
import { z, ZodError } from 'zod';
import {
DomainError,
budgetSchema,
codeReviewPolicyInput,
orderProjectModels,
projectModelsInput,
commandList,
gitBranchName,
wishRulesInput,
type Actor,
type Run,
isProduction,
type Workspace,
} from '@wishforge/contracts';
import { Authenticator, localUser } from './auth.js';
import { ControlPlane, must, projectAccess, record, touch } from './domain/control-plane.js';
import { Releases } from './domain/releases.js';
import { AgentBundles } from './domain/agent-bundles.js';
import { Lessons } from './domain/lessons.js';
import { Skills } from './domain/skills.js';
import { WishReports } from './domain/wish-report.js';
import { Connections } from './domain/connections.js';
import { RunnerBroker } from './domain/runner-broker.js';
import { GitStorage } from './domain/git-storage.js';
import { WorkspaceGit, commitAuthor } from './domain/workspace-git.js';
import { GitHubIntegration } from './domain/github.js';
import { Usage } from './domain/usage.js';
import { ProviderProxy } from './domain/provider-proxy.js';
import { Entitlements, requireCapacity } from './domain/entitlements.js';
import { requireRole, requireHuman } from './domain/policy.js';
import { configuredVerificationProfiles, selectVerificationProfile } from './domain/verification-policy.js';
import { LocalAccounts, localAccountsEnabled } from './domain/local-accounts.js';
import { permissionsOf } from './domain/permissions.js';
import { WishRefiner } from './domain/wish-refiner.js';
import { ModelCatalog } from './domain/model-catalog.js';
import { notificationsConfigured } from './domain/notifications.js';
import { Copilot } from './domain/copilot.js';
import { streamErrorCode } from './domain/copilot-stream.js';
import { CodeWiki, CODE_WIKI_KINDS, type CodeWikiKind } from './domain/code-wiki.js';
import { codeClient } from './domain/copilot-tools.js';
import { ProjectWiki } from './domain/project-wiki.js';
export class Services {
auth: Authenticator;
releases: Releases;
bundles: AgentBundles;
lessons: Lessons;
skills: Skills;
reports: WishReports;
connections: Connections;
runners: RunnerBroker;
github: GitHubIntegration;
accounts: LocalAccounts;
refiner: WishRefiner;
modelCatalog: ModelCatalog;
copilot: Copilot;
codeWiki: CodeWiki;
wiki: ProjectWiki;
constructor(readonly control: ControlPlane) {
this.auth = new Authenticator(control.store, control.env);
this.releases = new Releases(control.store, control.env);
this.bundles = new AgentBundles(control.store);
this.lessons = new Lessons(control.store);
this.skills = new Skills(control.store, control.env);
this.reports = new WishReports(control.store);
this.connections = new Connections(control.store, control.env);
this.runners = new RunnerBroker(control.store, this.releases, control.env);
this.github = new GitHubIntegration(control.store, control.env);
this.accounts = new LocalAccounts(control.store, control.env);
this.refiner = new WishRefiner(control.store, control.env);
this.modelCatalog = new ModelCatalog(control.env);
this.copilot = new Copilot(control, control.env);
this.wiki = new ProjectWiki(control.store, control.env);
this.codeWiki = new CodeWiki({
store: control.store,
env: control.env,
wiki: this.wiki,
code: codeClient(control.env),
vault: this.connections.vault,
runs: (actor, projectId) => this.control.runs(actor, { projectId, limit: 20 }),
});
}
}
type AuthRequest = Request & { actor: Actor };
const ok = <T>(data: T) => ({ data });
const key = (req: Request) => req.header('Idempotency-Key');
const leaseToken = z.number().int().positive();
const commitSha = z.string().regex(/^[0-9a-f]{40,64}$/);
const leaseBody = z.object({ leaseToken }).strict();
const fencedBody = z.object({ leaseToken, fence: z.number().int().positive() }).strict();
const leaseRequest = z
.object({ workerId: z.string().min(1).max(200), generation: z.string().max(200).optional() })
.strict();
const checkpointBody = z
.object({
leaseToken,
workspace: z.string().max(4096).optional(),
checkpoint: z
.object({
schemaVersion: z.literal(1),
runId: z.string().min(1).max(200),
provider: z.string().min(1).max(40),
sourceCommit: commitSha,
baseCommit: commitSha,
patch: z.string().max(4 * 1024 * 1024),
patchSha256: z.string().regex(/^[a-f0-9]{64}$/),
gitBundleBase64: z.string().optional(),
gitBundleSha256: z
.string()
.regex(/^[a-f0-9]{64}$/)
.optional(),
createdAt: z.string().max(64),
sessionId: z.string().max(200).optional(),
providerCompletion: z
.object({
status: z.enum(['completed', 'failed', 'canceled']),
revision: z.number().int().positive(),
repairCount: z.number().int().nonnegative(),
summary: z.string().max(4000).optional(),
finalUsage: z
.array(
z
.object({
operationId: z.string().min(1).max(300),
tokens: z.number().int().nonnegative().safe(),
})
.strict(),
)
.max(100),
})
.strict()
.optional(),
})
.strict(),
})
.strict();
const classificationBody = z
.object({
leaseToken,
sourceCommit: commitSha,
changedPaths: z.array(z.string().min(1).max(4096)).max(10000),
migration: z.boolean(),
permissionsExpanded: z.boolean(),
partial: z.boolean().optional(),
})
.strict();
const meterBody = z
.object({
leaseToken,
operationId: z.string().min(1).max(300),
tokens: z.number().int().min(0),
computeSeconds: z.number().min(0),
})
.strict();
/** The first check of an IDE session carries the user's token; later checks name the established user. */
const authorizeBody = z
.object({
userToken: z.string().min(1).max(16384).optional(),
userId: z.string().min(1).max(200).optional(),
operation: z.enum(['edit', 'terminal', 'debug']),
})
.strict()
.refine(body => Boolean(body.userToken) !== Boolean(body.userId), 'Provide either userToken or userId');
const gatewayUserBody = z.object({ userId: z.string().min(1).max(200) }).strict();
@Catch()
export class Errors implements ExceptionFilter {
catch(error: unknown, host: ArgumentsHost) {
const res = host.switchToHttp().getResponse<Response>();
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 } });
// Framework errors (unknown route, wrong method) keep their status with the same envelope.
if (error instanceof HttpException) {
const status = error.getStatus();
const code = status === 404 ? 'NOT_FOUND' : status === 405 ? 'METHOD_NOT_ALLOWED' : `HTTP_${status}`;
return res.status(status).json({ error: { code, message: status === 404 ? 'Route not found' : error.message } });
}
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.' } });
}
}
@Controller('v1')
export class ApiController {
constructor(@Inject(Services) private s: Services) {}
@Get('identity') identity(@Req() req: AuthRequest) {
const { userId, role, kind, displayName } = req.actor;
return ok({
userId,
role,
kind,
...(displayName ? { displayName } : {}),
permissions: permissionsOf(req.actor),
// Permissions of one's own account apply as they are; a role's can be raised by a project.
permissionSource: req.actor.permissions ? 'account' : 'role',
});
}
/** What this deployment offers, so the console shows only what works here. */
@Get('capabilities') async capabilities(@Req() req: AuthRequest) {
return ok({
wishRefinement: await this.s.refiner.available(req.actor.tenantId),
copilot: await this.s.copilot.available(req.actor.tenantId),
modelCatalog: this.s.modelCatalog.available(req.actor.tenantId),
localAccounts: localAccountsEnabled(this.s.control.env),
notifications: notificationsConfigured(this.s.control.env),
});
}
/** The models the gateway offers, for choosing a project's models. */
@Get('models') async models(@Req() req: AuthRequest) {
return ok(await this.s.modelCatalog.list(req.actor));
}
@Get('dashboard') async dashboard(@Req() req: AuthRequest) {
const a = req.actor;
const [projects, runs, releases, providers, 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.control.usage(a),
]);
return ok({
projects,
runs,
releases,
providers,
usage,
system: {
mode: isProduction(this.s.control.env) ? '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);
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;
}),
);
}
@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.read(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({
targetBranch: gitBranchName.optional(),
verificationCommands: commandList.optional(),
installCommands: commandList.optional(),
memberIds: z.array(z.string().min(1)).optional(),
codeReview: codeReviewPolicyInput.nullable().optional(),
wishRules: wishRulesInput.optional(),
models: projectModelsInput.nullable().optional(),
// The budget every wish of this project starts with; null returns to the platform default.
defaultBudget: budgetSchema.nullable().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('.'),
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 { codeReview, models, defaultBudget, ...rest } = data;
const next = { ...touch(p), ...rest };
// null clears the budget: wishes of this project spend the platform default again.
if (defaultBudget !== undefined) next.defaultBudget = defaultBudget ?? undefined;
// null or none clears the list: wishes run with the executor's default model again.
if (models !== undefined) next.models = models?.length ? orderProjectModels(models) : undefined;
// null clears the policy: changes no longer wait for a review, and nobody's permission is kept.
if (codeReview !== undefined)
next.codeReview = codeReview?.paths.length || codeReview?.trustedMembers.length ? codeReview : undefined;
await tx.put('project', next);
return next;
}),
);
}
@Get('projects/:id/lessons') async lessons(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.lessons.list(r.actor, id));
}
@Post('projects/:id/lessons') async createLesson(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.lessons.create(r.actor, id, b));
}
@Patch('lessons/:id') async updateLesson(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.lessons.setStatus(r.actor, id, b));
}
@Delete('lessons/:id') async deleteLesson(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.lessons.remove(r.actor, id));
}
@Get('projects/:id/skills') async skills(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.skills.list(r.actor, id));
}
@Post('projects/:id/skills') async createSkill(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.skills.create(r.actor, id, b));
}
/** Every active skill (and with ?proposed=1 the proposals) as a zip, one directory per skill. */
@Get('projects/:id/skills/export') async exportSkills(
@Req() r: AuthRequest,
@Res() res: Response,
@Param('id') id: string,
@Query('proposed') proposed?: string,
) {
const exported = await this.s.skills.exportAll(r.actor, id, proposed === '1');
res.type('application/zip').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`);
res.send(exported.content);
}
@Patch('skills/:id') async updateSkill(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.skills.update(r.actor, id, b));
}
@Delete('skills/:id') async deleteSkill(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.skills.remove(r.actor, id));
}
@Get('skills/:id/export') async exportSkill(@Req() r: AuthRequest, @Res() res: Response, @Param('id') id: string) {
const exported = await this.s.skills.exportOne(r.actor, id);
res
.type('text/markdown; charset=utf-8')
.setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`);
res.send(exported.content);
}
@Post('runs/:id/skill-suggestions') @HttpCode(200) async suggestSkills(
@Req() r: AuthRequest,
@Param('id') id: string,
) {
return ok(await this.s.skills.suggest(r.actor, id));
}
/** Every run of a wish, its stages and the AI's work, as Markdown. */
@Get('wishes/:id/report') async wishReport(@Req() r: AuthRequest, @Res() res: Response, @Param('id') id: string) {
const report = await this.s.reports.markdown(r.actor, id);
res
.type('text/markdown; charset=utf-8')
.setHeader('Content-Disposition', `attachment; filename="${report.filename}"`);
res.send(report.content);
}
@Post('projects/:id/wish-refinements') @HttpCode(200) async refineWish(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
const refined = await this.s.refiner.refine(r.actor, id, b);
// The code layer is refreshed after the wish is refined, never before: an optimizer that waits
// on page generation is a slower optimizer for no benefit to the customer.
void this.s.codeWiki.afterRefinement(r.actor, id).catch(() => undefined);
return ok(refined);
}
/** Regenerates the code layer of the Wiki: the project overview, its rules, its flows, its changes. */
@Post('projects/:id/wiki/code') @HttpCode(202) async generateCodeWiki(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
requireRole(r.actor, ['owner', 'admin', 'developer']);
const body = z
.object({
kinds: z
.array(z.enum(['overview', 'constraints', 'flows', 'changes']))
.max(4)
.optional(),
})
.parse(b ?? {});
const kinds = (body.kinds ?? CODE_WIKI_KINDS) as CodeWikiKind[];
return ok(await this.s.codeWiki.refresh(r.actor, id, kinds));
}
@Post('projects/:id/copilot/sessions') async createCopilotSession(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.copilot.createSession(r.actor, id));
}
@Get('projects/:id/copilot/sessions/:sessionId') async copilotSession(
@Req() r: AuthRequest,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
) {
return ok(await this.s.copilot.session(r.actor, id, sessionId));
}
@Post('projects/:id/copilot/sessions/:sessionId/messages') @HttpCode(200) async copilotMessage(
@Req() r: AuthRequest,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
@Body() b: unknown,
) {
return ok(await this.s.copilot.ask(r.actor, id, sessionId, b));
}
@Post('projects/:id/copilot/sessions/:sessionId/stream') async copilotStream(
@Req() r: AuthRequest,
@Res() res: Response,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
@Body() b: unknown,
) {
// Server-sent events: the first frame reaches the browser as soon as the model speaks, and the
// answer is stored once, by the domain method, when the last frame has been written.
res.status(200);
res.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
res.setHeader('Cache-Control', 'no-store');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders();
const abort = new AbortController();
res.once('close', () => {
if (!res.writableEnded) abort.abort();
});
const send = (event: unknown) => {
if (res.writableEnded) return;
res.write(`data: ${JSON.stringify(event)}\n\n`);
};
try {
await this.s.copilot.stream(r.actor, id, sessionId, b, send, abort.signal);
} catch (error) {
// Headers are already out, so a domain error becomes a final event rather than a status code.
send({
type: 'error',
code: streamErrorCode(error),
message: '这次没能回答上,请稍后重试;你的问题已保留。',
});
} finally {
if (!res.writableEnded) res.end();
}
}
/**
* A screenshot for one question. The body is the picture itself, so a 4 MiB image never has to
* become a 5 MiB JSON string; the type is decided by the bytes, not by the request header.
*/
@Post('projects/:id/copilot/sessions/:sessionId/attachments')
@HttpCode(201)
async copilotAttachmentUpload(
@Req() r: AuthRequest,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
@Query('name') name: string | undefined,
@Body() body: unknown,
) {
if (!Buffer.isBuffer(body)) throw new DomainError('ATTACHMENT_EMPTY', '图片没有上传成功,请重试', 400);
const attachment = await this.s.copilot.attachments.upload(r.actor, id, sessionId, body, name);
return ok({
...attachment,
url: `/api/projects/${id}/copilot/sessions/${sessionId}/attachments/${attachment.id}`,
});
}
@Get('projects/:id/copilot/sessions/:sessionId/attachments/:attachmentId')
async copilotAttachment(
@Req() r: AuthRequest,
@Res() res: Response,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
@Param('attachmentId') attachmentId: string,
) {
const { attachment, bytes } = await this.s.copilot.attachments.read(r.actor, id, sessionId, attachmentId);
res.status(200);
res.setHeader('Content-Type', attachment.contentType);
// Private: the picture belongs to one person's session, and only they may see it again.
res.setHeader('Cache-Control', 'private, max-age=600');
res.setHeader('X-Content-Type-Options', 'nosniff');
res.setHeader('Content-Disposition', 'inline');
res.end(bytes);
}
@Post('projects/:id/copilot/sessions/:sessionId/wish') @HttpCode(202) async copilotWish(
@Req() r: AuthRequest,
@Param('id') id: string,
@Param('sessionId') sessionId: string,
@Body() b: unknown,
) {
const wish = await this.s.copilot.createWish(r.actor, id, sessionId, b);
// A new wish changes what the project is working on, so the change log is refreshed. It is
// deterministic (a log table), so it costs no model call and never delays the wish.
void this.s.codeWiki.refresh(r.actor, id, ['changes']).catch(() => undefined);
return ok(wish);
}
@Get('projects/:id/wiki-gaps') async wikiGaps(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.copilot.wikiGaps(r.actor, id));
}
@Get('projects/:id/wiki') async wikiSource(
@Req() r: AuthRequest,
@Param('id') id: string,
@Query('path') path: string,
@Query('version') version?: string,
) {
return ok(await this.s.wiki.source(r.actor, id, z.string().min(1).max(256).parse(path), version));
}
@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,
@Query('project') projectId?: string,
@Query('limit') limit?: string,
@Query('before') before?: string,
) {
const query = z
.object({
projectId: z.string().min(1).max(128).optional(),
limit: z.coerce.number().int().min(1).max(500).optional(),
before: z.string().datetime().optional(),
})
.parse({ projectId, limit, before });
return ok(await this.s.control.runs(r.actor, query));
}
@Get('runs/:id') async run(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.control.run(r.actor, id));
}
@Patch('runs/:id/budget') async updateBudget(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.updateBudget(r.actor, id, b, key(r)));
}
@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'),
localUser(name => r.header(name)),
);
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/revert') @HttpCode(202) async revert(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.control.revert(r.actor, id, key(r)));
}
@Get('runs/:id/code-review') async codeReview(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.control.codeReview(r.actor, id));
}
@Post('runs/:id/code-review') async decideCodeReview(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.control.decideCodeReview(r.actor, id, b, key(r)));
}
@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('runs/:id/provider-requests') async providerRequests(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await new ProviderProxy(this.s.control.store, this.s.control.env).requests(r.actor, id));
}
@Post('runs/:id/provider-requests/reconcile') async reconcileProviderRequest(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await new ProviderProxy(this.s.control.store, this.s.control.env).reconcile(r.actor, id, b));
}
@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/application-url') async environmentApplicationUrl(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() body: unknown,
) {
const input = z
.object({ applicationUrl: z.string().max(2048).nullable() })
.strict()
.parse(body);
return ok(await this.s.releases.updateApplicationUrl(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));
}
@Patch('environments/:id/release-policy') async environmentReleasePolicy(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.releases.updateReleasePolicy(r.actor, id, b));
}
@Post('environments/:id/unlock') async unlockEnvironment(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.releases.unlock(r.actor, id, b));
}
@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)));
}
@Get('releases/:id/review') async releaseReview(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.releases.review(r.actor, id));
}
@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('agent-bundles') async agentBundles(@Req() r: AuthRequest) {
return ok(await this.s.bundles.list(r.actor));
}
@Post('agent-bundles') async createAgentBundle(@Req() r: AuthRequest, @Body() b: unknown) {
return ok(await this.s.bundles.create(r.actor, b));
}
@Post('agent-bundles/rollback') async rollbackAgentBundle(@Req() r: AuthRequest) {
return ok(await this.s.bundles.rollback(r.actor));
}
@Post('agent-bundles/:version/activate') async activateAgentBundle(
@Req() r: AuthRequest,
@Param('version') version: string,
) {
return ok(await this.s.bundles.activate(r.actor, version));
}
@Get('entitlements') async entitlements(@Req() r: AuthRequest) {
return ok(await new Entitlements(this.s.control.store, this.s.control.env).view(r.actor));
}
@Get('plans') async plans(@Req() r: AuthRequest) {
return ok(await new Entitlements(this.s.control.store, this.s.control.env).plans(r.actor));
}
@Post('admin/members') async admitMember(@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)));
}
@Get('local-accounts') async localAccounts(@Req() r: AuthRequest) {
return ok(await this.s.accounts.list(r.actor));
}
@Post('local-accounts') async createLocalAccount(@Req() r: AuthRequest, @Body() b: unknown) {
return ok(await this.s.accounts.create(r.actor, b));
}
@Patch('local-accounts/:id') async updateLocalAccount(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.accounts.update(r.actor, id, b));
}
@Post('local-accounts/:id/password') async resetLocalPassword(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(await this.s.accounts.resetPassword(r.actor, id));
}
/** Only the web console's own sign-in calls this; its proxy never forwards a browser here. */
@Post('local-access/login') @HttpCode(200) async localLogin(@Req() r: AuthRequest, @Body() b: unknown) {
return ok(await this.s.accounts.verify(r.actor, b));
}
@Post('admin/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('usage') async usage(@Req() r: AuthRequest) {
return ok(await this.s.control.usage(r.actor));
}
@Get('usage/lines') async usageLines(@Req() r: AuthRequest, @Query('period') period?: string) {
return ok(await new Usage(this.s.control.store).report(r.actor, period));
}
@Get('usage/export') async exportUsage(
@Req() r: AuthRequest,
@Res() res: Response,
@Query('period') period?: string,
) {
const exported = await new Usage(this.s.control.store).exportCsv(r.actor, period);
res.type('text/csv').setHeader('Content-Disposition', `attachment; filename="${exported.filename}"`);
res.send(exported.content);
}
@Get('projects/:id/branches') async branches(@Req() r: AuthRequest, @Param('id') id: string) {
const project = await this.s.control.store.read(r.actor.tenantId, tx => projectAccess(tx, r.actor, id));
return ok(await new GitStorage(this.s.control.env).ideBranches(project));
}
@Get('projects/:id/workspaces') async workspaces(@Req() r: AuthRequest, @Param('id') id: string) {
return ok(
await this.s.control.store.read(r.actor.tenantId, async tx => {
await projectAccess(tx, r.actor, id);
return (await tx.list<Workspace>('workspace', { projectId: id })).map(publicWorkspace);
}),
);
}
@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>('workspace', { projectId: id }))[0];
if (existing) return publicWorkspace(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;
}),
);
}
}
/** A workspace as people see it: its internal upstream addresses are only for the gateway. */
function publicWorkspace({ upstreamUrl: _, collaborationUpstreamUrl: _collaboration, ...workspace }: Workspace) {
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: unknown) {
return ok(await this.s.github.workerState(r.actor, id, leaseBody.parse(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<string, unknown> } },
) {
return this.s.runners.result(r.actor, id, b);
}
@Post('runs/:id/lease') async lease(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
const { workerId, generation } = leaseRequest.parse(b);
return ok(await this.s.control.lease(r.actor, id, workerId, generation));
}
@Post('runs/:id/lease/release') async releaseLease(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.control.releaseLease(r.actor, id, leaseBody.parse(b).leaseToken));
}
@Post('runs/:id/heartbeat') async heartbeat(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.heartbeat(r.actor, id, leaseBody.parse(b).leaseToken));
}
@Post('runs/:id/pause') async completePause(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.completePause(r.actor, id, b));
}
@Post('runs/:id/wait') async wait(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.waitRun(r.actor, id, b));
}
@Post('runs/:id/repair') async repair(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.repair(r.actor, id, b));
}
@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.read(r.actor.tenantId, async tx => {
const run = await must<Run>(tx, 'run', id);
return {
status: run.status,
generation: run.executionGeneration,
waitingReason: run.waitingReason,
leaseExpiresAt: run.lease?.expiresAt,
};
}),
);
}
@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: unknown) {
const { leaseToken, checkpoint, workspace } = checkpointBody.parse(b);
return ok(await this.s.control.checkpoint(r.actor, id, checkpoint, leaseToken, workspace));
}
@Post('runs/:id/code-review') async requestCodeReview(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
const data = z
.object({
leaseToken,
sourceCommit: commitSha,
baseCommit: commitSha,
changedPaths: z.array(z.string().min(1).max(4096)).max(10000),
})
.strict()
.parse(b);
return ok(await this.s.control.requestCodeReview(r.actor, id, data));
}
@Post('runs/:id/classification') async classification(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
return ok(await this.s.control.classify(r.actor, id, classificationBody.parse(b)));
}
@Post('runs/:id/events') async events(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.providerEvent(r.actor, id, b));
}
@Post('runs/:id/meter') async meter(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
return ok(await this.s.control.meter(r.actor, id, meterBody.parse(b)));
}
@Post('runs/:id/integration-lease/acquire') async acquireIntegration(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
const { leaseToken } = leaseBody.parse(b);
return ok(await this.s.control.integration.acquire(r.actor, id, leaseToken));
}
@Post('runs/:id/integration-lease/renew') async renewIntegration(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
const { leaseToken, fence } = fencedBody.parse(b);
return ok(await this.s.control.integration.renew(r.actor, id, leaseToken, fence));
}
@Post('runs/:id/integration-lease/release') async releaseIntegration(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() b: unknown,
) {
const { leaseToken, fence } = fencedBody.parse(b);
return ok(await this.s.control.integration.release(r.actor, id, leaseToken, fence));
}
@Post('evidence') async evidence(@Req() r: AuthRequest, @Body() b: unknown) {
return ok(await this.s.control.evidence(r.actor, b));
}
@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('workspaces/:id/authorize') async authorizeWorkspace(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() body: unknown,
) {
const b = authorizeBody.parse(body);
if (r.actor.userId !== 'workspace-gateway')
throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403);
const user = b.userToken
? await this.s.auth.authenticate(`Bearer ${b.userToken}`)
: await this.s.auth.sessionUser(r.actor.tenantId, b.userId!);
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<Workspace>(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,
// Known only from a sign-in token: the gateway keeps it for the session's commits.
...(b.userToken ? { author: commitAuthor(user) } : {}),
};
}),
);
}
@Post('workspaces/:id/git-credential') async workspaceGitCredential(
@Req() r: AuthRequest,
@Param('id') id: string,
@Body() body: unknown,
) {
if (r.actor.userId !== 'workspace-gateway')
throw new DomainError('FORBIDDEN', 'Workspace gateway identity required', 403);
const { userId } = gatewayUserBody.parse(body);
return ok(
await new WorkspaceGit(this.s.control.store, this.s.auth, this.s.control.env).credential(
r.actor.tenantId,
id,
userId,
),
);
}
}