🔀 Merge review/overhaul into feat/wiki-copilot-diagnostics
Brings the branch up to the current base (executor settings, project skills, Chinese run messages, cloud IDE removal, wish attachments) and resolves the ten conflicted files, keeping both sides: - wish-refiner: the tenant's executor settings pick the models first, then the Copilot model setting; the tenant credential still goes before the operator's login - worker providers: executor settings apply, and a run with its own credential still gets an empty home - main.ts: Copilot screenshot uploads and wish attachments each keep their own body limit - contracts, http, web types: default budget and skill auto-activation, Copilot and attachment entity kinds, ./text and ./attachments exports - globals.css: Copilot styles and attachment styles, one after the other - tests: the canceled-release reason is Chinese; the refiner's default models have no [1m] suffix, as this branch names them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
commit
13ceca5c8f
@ -13,6 +13,4 @@
|
||||
**/*.tsbuildinfo
|
||||
**/test-results
|
||||
**/coverage
|
||||
apps/ide
|
||||
infra/oct
|
||||
**/.DS_Store
|
||||
|
||||
15
.github/workflows/ci.yml
vendored
15
.github/workflows/ci.yml
vendored
@ -62,18 +62,3 @@ jobs:
|
||||
# A second run must find nothing to apply (requirements FR-K2).
|
||||
- run: npm run db:migrate | tee migrate.log && grep -q 'up to date' migrate.log
|
||||
- run: npm run test:integration
|
||||
|
||||
ide:
|
||||
name: IDE gateway, collaboration and policy tests
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
cache-dependency-path: apps/ide/package-lock.json
|
||||
# The Theia application itself is not built here; these tests need only its dependencies.
|
||||
- run: npm ci --prefix apps/ide --ignore-scripts
|
||||
- run: npm run test:ide
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,7 +1,6 @@
|
||||
node_modules/
|
||||
.next/
|
||||
dist/
|
||||
/apps/ide/lib/
|
||||
coverage/
|
||||
.env
|
||||
.env.local
|
||||
|
||||
@ -3,11 +3,6 @@ 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
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
# WishForge
|
||||
|
||||
远程、意图驱动的软件交付平台:用一句话提出愿望(目标 + 验收标准),后端编码 Agent 在隔离环境中实现,独立验证器按受保护的命令判定,失败时把失败信息交还 Agent 自修复,通过后合入代码并按需构建、审批与部署。可以从 Web 工作台、WorkBuddy/Codex(MCP)或云 IDE 发起与跟进。
|
||||
远程、意图驱动的软件交付平台:用一句话提出愿望(目标 + 验收标准),后端编码 Agent 在隔离环境中实现,独立验证器按受保护的命令判定,失败时把失败信息交还 Agent 自修复,通过后合入代码并按需构建、审批与部署。可以从 Web 工作台或 WorkBuddy/Codex(MCP)发起与跟进。
|
||||
|
||||
- 使用手册(使用者与本机服务维护者):[docs/user-manual.md](docs/user-manual.md)
|
||||
- 综合指引(使用、架构与高频 QA):[docs/wishforge-summary.md](docs/wishforge-summary.md)
|
||||
@ -67,10 +67,9 @@ npm run dev:all
|
||||
| `apps/api` | 控制面 API(REST `/v1`、内部接口 `/internal`、MCP `/mcp`、模型代理) |
|
||||
| `apps/web` | Web 工作台(Next.js,含 OIDC BFF) |
|
||||
| `apps/worker` | Temporal worker:coordinator / executor / verifier / deployer |
|
||||
| `apps/provisioner` | 执行环境与 IDE 工作区的创建(`local` 与 `kubernetes` 后端) |
|
||||
| `apps/provisioner` | 执行环境的创建(`local` 与 `kubernetes` 后端) |
|
||||
| `apps/git-service` | 平台 Git 服务 |
|
||||
| `apps/runner` | 目标环境侧的部署 Runner |
|
||||
| `apps/ide` | Theia 云 IDE、IDE 网关与协作 |
|
||||
| `packages/contracts` | 领域类型、输入校验、规范化 JSON |
|
||||
| `packages/agent` | Agent 配置与提示词组装 |
|
||||
| `packages/runtime` | 执行器适配、Git、验证、构建与部署适配 |
|
||||
@ -92,4 +91,4 @@ npm run typecheck
|
||||
npm test
|
||||
```
|
||||
|
||||
需要外部服务的集成测试默认跳过,开启方式见 [docs/development.md](docs/development.md#7-测试与检查)。
|
||||
需要外部服务的集成测试默认跳过,开启方式见 [docs/development.md](docs/development.md#6-测试与检查)。
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { resolveGatewayTarget } from './domain/gateway-target.js';
|
||||
import { createRemoteJWKSet, jwtVerify, type JWTPayload } from 'jose';
|
||||
import { DomainError, type Actor, isProduction, type Run, type RunnerRegistration } from '@wishforge/contracts';
|
||||
import { currentGeneration, sameSecret } from './domain/policy.js';
|
||||
@ -102,13 +101,6 @@ export class Authenticator {
|
||||
throw new DomainError('UNAUTHENTICATED', 'Access token is invalid or expired', 401);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* The person behind an established IDE session, with their current membership, so the
|
||||
* gateway keeps checking access (and sees revocation) after their access token expired.
|
||||
*/
|
||||
async sessionUser(tenantId: string, userId: string): Promise<Actor> {
|
||||
return this.member(tenantId, userId, 'human');
|
||||
}
|
||||
private async member(tenantId: string, userId: string, kind: Actor['kind'], clientId?: string): Promise<Actor> {
|
||||
const role = await this.store.read(tenantId, tx => tx.membership(userId));
|
||||
if (!role) throw new DomainError('FORBIDDEN', 'Tenant membership is missing or revoked', 403);
|
||||
@ -131,9 +123,6 @@ export class Authenticator {
|
||||
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' };
|
||||
// Verifiers only ever use run-scoped tokens; there is no static verifier identity.
|
||||
for (const [name, kind] of [
|
||||
['WF_WORKER_TOKEN', 'worker'],
|
||||
|
||||
7
apps/api/src/db/migrations/003_retire_cloud_ide.sql
Normal file
7
apps/api/src/db/migrations/003_retire_cloud_ide.sql
Normal file
@ -0,0 +1,7 @@
|
||||
-- Data changes below must see every tenant: fail instead of silently skipping rows under RLS.
|
||||
SET LOCAL row_security = off;
|
||||
|
||||
-- The cloud IDE was removed: its workspaces and their pending provisioning work go with it.
|
||||
UPDATE outbox SET delivered_at = now()
|
||||
WHERE type IN ('workspace.provision', 'workspace.reconcile') AND delivered_at IS NULL;
|
||||
DELETE FROM entities WHERE kind = 'workspace';
|
||||
32
apps/api/src/db/migrations/004_lessons_to_skills.sql
Normal file
32
apps/api/src/db/migrations/004_lessons_to_skills.sql
Normal file
@ -0,0 +1,32 @@
|
||||
-- Project lessons become project skills: one kind of project know-how, one prompt section.
|
||||
-- Data changes below must see every tenant: fail instead of silently skipping rows under RLS.
|
||||
SET LOCAL row_security = off;
|
||||
|
||||
-- A lesson recorded from a repaired failure becomes a pitfall skill, a convention one a convention
|
||||
-- skill. Each keeps its id, project, status, author and source run; its name is made unique from
|
||||
-- its id, its summary is both its description (shortened) and its content.
|
||||
INSERT INTO entities(tenant_id, kind, id, version, data, created_at)
|
||||
SELECT l.tenant_id, 'skill', l.id, 1,
|
||||
jsonb_strip_nulls(jsonb_build_object(
|
||||
'id', l.id,
|
||||
'tenantId', l.tenant_id,
|
||||
'createdAt', l.data->>'createdAt',
|
||||
'updatedAt', coalesce(l.data->>'updatedAt', l.data->>'createdAt'),
|
||||
'version', 1,
|
||||
'projectId', l.data->>'projectId',
|
||||
'name', CASE WHEN l.data->>'kind' = 'convention' THEN 'convention-' ELSE 'pitfall-' END
|
||||
|| substr(regexp_replace(lower(l.id), '[^a-z0-9]', '', 'g'), 1, 12),
|
||||
'description', left(regexp_replace(btrim(l.data->>'summary'), '\s+', ' ', 'g'), 300),
|
||||
'content', btrim(l.data->>'summary'),
|
||||
'category', CASE WHEN l.data->>'kind' = 'convention' THEN 'convention' ELSE 'pitfall' END,
|
||||
'status', CASE WHEN l.data->>'status' = 'disabled' THEN 'disabled' ELSE 'active' END,
|
||||
'sourceRunIds', CASE WHEN l.data ? 'sourceRunId' THEN jsonb_build_array(l.data->>'sourceRunId')
|
||||
ELSE '[]'::jsonb END,
|
||||
'createdBy', coalesce(l.data->>'createdBy', 'wishforge')
|
||||
)),
|
||||
l.created_at
|
||||
FROM entities l
|
||||
WHERE l.kind = 'lesson' AND coalesce(btrim(l.data->>'summary'), '') <> ''
|
||||
ON CONFLICT (tenant_id, kind, id) DO NOTHING;
|
||||
|
||||
DELETE FROM entities WHERE kind = 'lesson';
|
||||
@ -59,8 +59,8 @@ export interface ListFilter {
|
||||
/** Only entities created before this time: the cursor for the next page. */
|
||||
before?: string;
|
||||
}
|
||||
/** Outbox items are processed in this order: control signals first, then reconciliation, then new work. */
|
||||
export const OUTBOX_PRIORITY = ['run.signal', 'workspace.reconcile'];
|
||||
/** Outbox items are processed in this order: control signals first, then new work. */
|
||||
export const OUTBOX_PRIORITY = ['run.signal'];
|
||||
export interface Transaction {
|
||||
tenant(): Promise<Tenant>;
|
||||
membership(userId: string): Promise<Role | undefined>;
|
||||
@ -261,7 +261,7 @@ class PgTransaction implements Transaction {
|
||||
}
|
||||
async outbox(): Promise<OutboxItem[]> {
|
||||
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",
|
||||
"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') ORDER BY CASE WHEN type='run.signal' THEN 0 ELSE 1 END,created_at LIMIT 50",
|
||||
[this.tenantId],
|
||||
);
|
||||
return r.rows;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
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';
|
||||
@ -10,9 +9,7 @@ import {
|
||||
requireTenantNamespaces,
|
||||
temporalNamespace,
|
||||
temporalTlsFiles,
|
||||
type Project,
|
||||
type Run,
|
||||
type Workspace,
|
||||
} from '@wishforge/contracts';
|
||||
import { reserveExecutionGrant, recordAllocation } from './domain/entitlements.js';
|
||||
import { resolveProvisionerTarget, type ProvisionerTarget } from './domain/provisioner-target.js';
|
||||
@ -60,95 +57,9 @@ export class Dispatcher {
|
||||
}
|
||||
// Claimed items are invisible to other API replicas until acknowledged, deferred or expired.
|
||||
const pending = await this.control.store.transaction(tenant, tx =>
|
||||
tx.claimOutbox(['run.signal', 'run.start', 'workspace.provision'], 300),
|
||||
tx.claimOutbox(['run.signal', 'run.start'], 300),
|
||||
);
|
||||
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, project } = await this.control.store.transaction(tenant, async tx => {
|
||||
const currentWorkspace = await must<Workspace>(tx, 'workspace', String(item.payload.workspaceId));
|
||||
return { currentWorkspace, project: await tx.get<Project>('project', currentWorkspace.projectId) };
|
||||
});
|
||||
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,
|
||||
// The workspace then accepts credentials for this repository only.
|
||||
...(project?.repositoryStorage === 'platform' && project.repositoryUrl
|
||||
? { repositoryUrl: project.repositoryUrl }
|
||||
: {}),
|
||||
}),
|
||||
signal: AbortSignal.timeout(30000),
|
||||
});
|
||||
if (!response.ok) throw new Error('WorkspaceProvisioningFailed');
|
||||
const state = (await response.json()) as {
|
||||
status: 'ready' | 'pending';
|
||||
upstreamUrl: string;
|
||||
collaborationUpstreamUrl?: string;
|
||||
/** The local backend shares one Theia and cannot pause or resume it. */
|
||||
lifecycle?: 'unmanaged';
|
||||
};
|
||||
if (!['ready', 'pending'].includes(state.status)) throw new Error('InvalidWorkspaceReceipt');
|
||||
await this.control.store.transaction(tenant, async tx => {
|
||||
const w = await must<Workspace>(tx, 'workspace', String(item.payload.workspaceId));
|
||||
if (w.lifecycle) {
|
||||
await tx.acknowledge(item.id);
|
||||
return;
|
||||
}
|
||||
w.managedBy = state.lifecycle === 'unmanaged' ? 'local-provisioner' : '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;
|
||||
const workflowId = `wishforge:${tenant}:${String(item.payload.runId)}`;
|
||||
try {
|
||||
@ -184,7 +95,7 @@ export class Dispatcher {
|
||||
setWaiting(
|
||||
current,
|
||||
'sandbox_provisioner',
|
||||
'An isolated per-run sandbox provisioner must be connected before execution.',
|
||||
'执行前必须连接为每次运行单独开通沙箱的资源开通服务。',
|
||||
);
|
||||
await tx.put('run', touch(current));
|
||||
await tx.event(runId, 'run.waiting', current.waitingDetail!, { reason: current.waitingReason });
|
||||
@ -242,7 +153,7 @@ export class Dispatcher {
|
||||
runId,
|
||||
{
|
||||
reason: 'sandbox_provisioner',
|
||||
detail: 'The sandbox for this generation already ended; resume to provision a new one.',
|
||||
detail: '这一代执行的沙箱已经结束;请继续运行以开通新的沙箱。',
|
||||
generation: item.id,
|
||||
},
|
||||
);
|
||||
@ -296,12 +207,13 @@ export class Dispatcher {
|
||||
if (item.type === 'run.start' && error instanceof DomainError)
|
||||
await this.control.store.transaction(tenant, async tx => {
|
||||
const current = await must<Run>(tx, 'run', String(item.payload.runId));
|
||||
const detail = `无法为运行开通沙箱(${error.code}):${error.message}`;
|
||||
if (
|
||||
!['canceled', 'failed', 'succeeded', 'paused'].includes(current.status) &&
|
||||
(current.waitingReason !== 'sandbox_provisioner' || current.waitingDetail !== error.message)
|
||||
(current.waitingReason !== 'sandbox_provisioner' || current.waitingDetail !== detail)
|
||||
) {
|
||||
// Resource grants, runtime images and provisioner errors all block the sandbox.
|
||||
setWaiting(current, 'sandbox_provisioner', error.message);
|
||||
setWaiting(current, 'sandbox_provisioner', detail);
|
||||
await tx.put('run', touch(current));
|
||||
await tx.event(current.id, 'run.waiting', current.waitingDetail!, {
|
||||
reason: current.waitingReason,
|
||||
|
||||
@ -10,12 +10,11 @@ import {
|
||||
|
||||
/**
|
||||
* Validate an environment's application URL. Deployed applications are untrusted
|
||||
* code, so they may never share an origin with the console, API or IDE gateways.
|
||||
* code, so they may never share an origin with the console or API.
|
||||
*/
|
||||
export function applicationUrlFor(
|
||||
environment: Pick<Environment, 'applicationUrl' | 'production'> & { projectId?: string },
|
||||
env: NodeJS.ProcessEnv,
|
||||
additionalOrigins: string[] = [],
|
||||
): string | undefined {
|
||||
if (!environment.applicationUrl) return undefined;
|
||||
let applicationUrl: string;
|
||||
@ -39,13 +38,7 @@ export function applicationUrlFor(
|
||||
);
|
||||
}
|
||||
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,
|
||||
]) {
|
||||
for (const configured of [env.WF_PUBLIC_URL, env.WF_API_PUBLIC_URL]) {
|
||||
if (!configured) continue;
|
||||
let protectedOrigin: string;
|
||||
try {
|
||||
@ -60,7 +53,7 @@ export function applicationUrlFor(
|
||||
if (origin === protectedOrigin)
|
||||
throw new DomainError(
|
||||
'APPLICATION_ORIGIN_CONFLICT',
|
||||
'Applications must use an origin separate from the console, API and IDE authentication origins',
|
||||
'Applications must use an origin separate from the console and API authentication origins',
|
||||
400,
|
||||
);
|
||||
}
|
||||
|
||||
@ -207,7 +207,7 @@ export class Artifacts {
|
||||
storageBytes: object.size,
|
||||
at: new Date().toISOString(),
|
||||
});
|
||||
await tx.event(runId, 'artifact.stored', 'Source bundle persisted in private content-addressed object storage.', {
|
||||
await tx.event(runId, 'artifact.stored', '源码包已保存到私有的按内容寻址对象存储。', {
|
||||
artifactId: id,
|
||||
sha256: object.sha256,
|
||||
size: object.size,
|
||||
|
||||
@ -14,6 +14,7 @@ import type { Store } from '../db/store.js';
|
||||
import { record, touch, must, executionLease } from './control-plane.js';
|
||||
import { requireHuman, requireRole } from './policy.js';
|
||||
import { ProviderProxy } from './provider-proxy.js';
|
||||
import { storedExecutorSettings, tenantBaseUrl, tenantBaseUrlBlocked } from './executor-settings.js';
|
||||
|
||||
/** Production uses a Vault transit key. The development AES key never enters source control. */
|
||||
export class CredentialVault {
|
||||
@ -134,9 +135,10 @@ export class Connections {
|
||||
)
|
||||
return undefined;
|
||||
executionLease(actor, current, run.lease!.token);
|
||||
return (await tx.list<ProviderConnection>('provider_connection')).find(
|
||||
const found = (await tx.list<ProviderConnection>('provider_connection')).find(
|
||||
c => c.provider === current.provider && c.status === 'active',
|
||||
);
|
||||
return found && { ...found, settings: await storedExecutorSettings(tx, current.provider) };
|
||||
});
|
||||
if (!connection?.ciphertext) return undefined;
|
||||
if (isProduction(this.env) || this.env.WF_PROVIDER_PROXY_ENABLED === 'true') {
|
||||
@ -152,7 +154,12 @@ export class Connections {
|
||||
};
|
||||
}
|
||||
const apiKey = await this.vault.open(actor.tenantId, String(connection.ciphertext));
|
||||
const baseUrl = providerBaseUrl(this.env, run.provider);
|
||||
// The tenant's gateway goes only with the tenant's own key; otherwise the operator's, if any.
|
||||
const tenantGateway =
|
||||
connection.settings.baseUrl && !tenantBaseUrlBlocked(this.env, actor.tenantId, run.provider)
|
||||
? tenantBaseUrl(this.env, connection.settings.baseUrl)
|
||||
: undefined;
|
||||
const baseUrl = tenantGateway ?? providerBaseUrl(this.env, run.provider);
|
||||
// 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<Run>(tx, 'run', run.id);
|
||||
|
||||
@ -24,6 +24,10 @@ import {
|
||||
type Project,
|
||||
type Wish,
|
||||
type Run,
|
||||
type Attachment,
|
||||
base64Size,
|
||||
MAX_ATTACHMENT_BYTES,
|
||||
MAX_ATTACHMENTS_TOTAL_BYTES,
|
||||
type Evidence,
|
||||
type Release,
|
||||
type Environment,
|
||||
@ -52,9 +56,9 @@ import { Artifacts } from './artifacts.js';
|
||||
import { providerProxyConfigured } from './provider-proxy.js';
|
||||
import { assertVerificationProfile, verificationPolicyDigest } from './verification-policy.js';
|
||||
import { IntegrationLeases } from './integration-lease.js';
|
||||
import { captureLesson, projectLessons } from './lessons.js';
|
||||
import { projectSkills } from './skills.js';
|
||||
import { learnFromRun, projectSkills, recordSkillUse } from './skills.js';
|
||||
import { describeProviderEvent } from './provider-events.js';
|
||||
import { runExecutorSettings, storedExecutorSettings } from './executor-settings.js';
|
||||
import { backendDefined, personWishScope, requirePermission, wishScopeOf } from './permissions.js';
|
||||
|
||||
export const now = () => new Date().toISOString();
|
||||
@ -142,7 +146,20 @@ async function finishPause(tx: Transaction, run: Run, message: string) {
|
||||
await tx.event(run.id, 'run.paused', message, { revision: run.revision });
|
||||
await tx.enqueue('run.signal', { runId: run.id, action: 'pause' });
|
||||
}
|
||||
const BUDGET_EXHAUSTED = 'The run used up its token or execution-time budget.';
|
||||
const BUDGET_EXHAUSTED = '运行已用完 token 或执行时间预算。';
|
||||
/** Run commands and evidence kinds as the Chinese timeline names them. */
|
||||
const COMMAND_NAMES: Record<string, string> = {
|
||||
pause: '暂停',
|
||||
resume: '继续',
|
||||
cancel: '取消',
|
||||
answer: '补充需求后继续',
|
||||
};
|
||||
const EVIDENCE_NAMES: Record<string, string> = {
|
||||
verification: '验证',
|
||||
integration: '合入',
|
||||
build: '构建',
|
||||
deployment: '部署',
|
||||
};
|
||||
|
||||
/**
|
||||
* Tokens held against the tenant's monthly limit: only runs that can call the model now
|
||||
@ -171,8 +188,8 @@ function startBlocker(provider: ProviderInfo, project: Project): { reason: Waiti
|
||||
return {
|
||||
reason: 'git_storage',
|
||||
detail: project.repositoryError
|
||||
? `The project repository is unavailable: ${project.repositoryError}`
|
||||
: 'Provision durable Git storage or connect a repository before execution.',
|
||||
? `项目仓库不可用:${project.repositoryError}`
|
||||
: '执行前请先开通持久的 Git 存储或连接代码仓库。',
|
||||
};
|
||||
}
|
||||
|
||||
@ -328,16 +345,16 @@ export class ControlPlane {
|
||||
const installed = !binaryVariable || Boolean(this.env[binaryVariable]);
|
||||
const enabled = configured && productionAllowed && installed;
|
||||
const reason = !productionAllowed
|
||||
? 'Production execution requires a verified credential-isolation proxy and approved model configuration; raw BYOK is never sent to a production job.'
|
||||
? '生产环境执行需要经过验证的凭据隔离代理和已批准的模型配置;原始 BYOK 凭据从不发给生产任务。'
|
||||
: !configured
|
||||
? 'Connect a tenant-owned provider credential to execute tasks.'
|
||||
? '请先连接租户自有的模型凭据,才能执行任务。'
|
||||
: !installed
|
||||
? `The ${name} executable is not configured on the worker (${binaryVariable}).`
|
||||
? `执行进程上没有配置 ${name} 可执行文件(${binaryVariable})。`
|
||||
: credentialSource === 'mixed'
|
||||
? 'Native Claude account mode and a tenant credential are both configured; authentication precedence has not been verified.'
|
||||
? '同时配置了本机 Claude 账号模式和租户凭据,认证优先级尚未验证。'
|
||||
: native
|
||||
? 'Available through the explicitly configured local Claude CLI account; trusted local operators only.'
|
||||
: 'Available for worker preflight; account/model access is checked when a task starts.';
|
||||
? '通过明确配置的本机 Claude CLI 账号可用;仅限受信任的本地运维人员。'
|
||||
: '可用于执行进程预检;账号和模型权限在任务启动时检查。';
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
@ -387,11 +404,7 @@ export class ControlPlane {
|
||||
data.sourceBranch &&
|
||||
(data.sourceBranch === project.targetBranch || data.sourceBranch.startsWith('wishforge/'))
|
||||
)
|
||||
throw new DomainError(
|
||||
'INVALID_SOURCE_BRANCH',
|
||||
'Start from another branch than the target branch, for example one pushed from the IDE',
|
||||
400,
|
||||
);
|
||||
throw new DomainError('INVALID_SOURCE_BRANCH', 'Start from another branch than the target branch', 400);
|
||||
// A wish runs with one of its project's models, flash when offered unless chosen; a project
|
||||
// without models leaves the choice to the agent configuration and the executor.
|
||||
const models = project.models ?? [];
|
||||
@ -403,8 +416,13 @@ export class ControlPlane {
|
||||
: 'This project offers no choice of model',
|
||||
400,
|
||||
);
|
||||
const { model: chosenModel, ...runInput } = data;
|
||||
const { model: chosenModel, attachments: files = [], ...runInput } = data;
|
||||
const model = chosenModel ?? defaultProjectModel(models);
|
||||
const sizes = files.map(file => base64Size(file.data));
|
||||
if (sizes.some(size => size > MAX_ATTACHMENT_BYTES))
|
||||
throw new DomainError('ATTACHMENT_TOO_LARGE', 'Each attachment may be at most 5 MB', 413);
|
||||
if (sizes.reduce((sum, size) => sum + size, 0) > MAX_ATTACHMENTS_TOTAL_BYTES)
|
||||
throw new DomainError('ATTACHMENTS_TOO_LARGE', 'Attachments may be at most 10 MB together', 413);
|
||||
return idempotent(tx, `wish:${actor.userId}:${projectId}:${key ?? ''}`, { projectId, ...data }, async () => {
|
||||
const tenant = await tx.tenant();
|
||||
const runs = await tx.list<Run>('run');
|
||||
@ -424,6 +442,7 @@ export class ControlPlane {
|
||||
} as Wish;
|
||||
const identity = record(actor.tenantId);
|
||||
const bundle = await activeBundle(tx);
|
||||
const executor = await storedExecutorSettings(tx, data.provider);
|
||||
const run = {
|
||||
...identity,
|
||||
projectId,
|
||||
@ -438,6 +457,8 @@ export class ControlPlane {
|
||||
providerModel:
|
||||
model ??
|
||||
bundle.config.model ??
|
||||
// The tenant's default for this executor, then the operator's.
|
||||
executor.defaultModel ??
|
||||
(data.provider === 'codex'
|
||||
? (this.env.WF_CODEX_DEFAULT_MODEL ?? this.env.WF_PROVIDER_PROXY_ALLOWED_MODELS?.split(',')[0]?.trim())
|
||||
: undefined),
|
||||
@ -448,12 +469,31 @@ export class ControlPlane {
|
||||
...(frontendOnly !== undefined ? { frontendOnly } : {}),
|
||||
...(extras.revertOf ? { revertOf: extras.revertOf } : {}),
|
||||
} as Run;
|
||||
const attachments: Attachment[] = files.map((file, index) => ({
|
||||
...record(actor.tenantId),
|
||||
projectId,
|
||||
runId: run.id,
|
||||
name: file.name,
|
||||
mediaType: file.mediaType,
|
||||
size: sizes[index]!,
|
||||
data: file.data,
|
||||
}));
|
||||
if (attachments.length)
|
||||
run.attachments = attachments.map(({ id, name, mediaType, size }) => ({ id, name, mediaType, size }));
|
||||
const blocked = startBlocker(provider, project);
|
||||
if (blocked) setWaiting(run, blocked.reason, blocked.detail);
|
||||
else run.pendingStartId = randomUUID();
|
||||
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 });
|
||||
for (const attachment of attachments) await tx.put('attachment', attachment);
|
||||
await tx.event(
|
||||
run.id,
|
||||
'wish.submitted',
|
||||
attachments.length
|
||||
? `愿望已受理,验收标准和 ${attachments.length} 个附件已记录。`
|
||||
: '愿望已受理,验收标准已记录。',
|
||||
{ revision: 1 },
|
||||
);
|
||||
if (run.pendingStartId) await tx.enqueue('run.start', { runId: run.id, revision: 1 }, run.pendingStartId);
|
||||
else await tx.event(run.id, 'run.waiting', run.waitingDetail!, { reason: run.waitingReason });
|
||||
return { wish_id: wish.id, run_id: run.id, status_url: `/runs/${run.id}` };
|
||||
@ -486,6 +526,16 @@ export class ControlPlane {
|
||||
return run;
|
||||
});
|
||||
}
|
||||
/** One file attached to a run's wish, for someone who may see the run. */
|
||||
async attachment(actor: Actor, runId: string, attachmentId: string) {
|
||||
return this.store.read(actor.tenantId, async tx => {
|
||||
const run = await must<Run>(tx, 'run', runId);
|
||||
await projectAccess(tx, actor, run.projectId);
|
||||
const attachment = await tx.get<Attachment>('attachment', attachmentId);
|
||||
if (!attachment || attachment.runId !== run.id) throw new DomainError('NOT_FOUND', 'Attachment not found', 404);
|
||||
return attachment;
|
||||
});
|
||||
}
|
||||
async events(actor: Actor, id: string, after = 0) {
|
||||
return this.store.read(actor.tenantId, async tx => {
|
||||
const run = await must<Run>(tx, 'run', id);
|
||||
@ -517,7 +567,7 @@ export class ControlPlane {
|
||||
return idempotent(tx, `command:${actor.userId}:${id}:${key}`, data, async () => {
|
||||
const result = await this.applyCommand(tx, run, project, data, actor.userId);
|
||||
if (data.allowBackend)
|
||||
await tx.event(id, 'run.scope', 'The wish may change backend code from now on.', {
|
||||
await tx.event(id, 'run.scope', '从现在起,这个愿望可以修改后端代码。', {
|
||||
frontendOnly: false,
|
||||
changedBy: actor.userId,
|
||||
});
|
||||
@ -563,12 +613,9 @@ export class ControlPlane {
|
||||
run = touch(run);
|
||||
run.pauseRequestedAt = now();
|
||||
await tx.put('run', run);
|
||||
await tx.event(
|
||||
id,
|
||||
'run.pause',
|
||||
data.message ? redact(data.message) : 'Pause requested; the executor saves the work so far first.',
|
||||
{ revision: run.revision },
|
||||
);
|
||||
await tx.event(id, 'run.pause', data.message ? redact(data.message) : '已请求暂停;执行器会先保存目前的工作。', {
|
||||
revision: run.revision,
|
||||
});
|
||||
return run;
|
||||
}
|
||||
this.accountTime(run);
|
||||
@ -579,7 +626,7 @@ export class ControlPlane {
|
||||
const answer = [data.allowBackend ? '代码范围改为前端和后端:这个愿望可以修改后端代码。' : '', data.message ?? '']
|
||||
.filter(Boolean)
|
||||
.join('\n');
|
||||
let message = answer ? redact(answer) : `Run ${data.action} requested.`;
|
||||
let message = answer ? redact(answer) : `已请求${COMMAND_NAMES[data.action] ?? data.action}运行。`;
|
||||
const invalidatePending = async (failureReason: string) => {
|
||||
for (const release of await tx.list<Release>('release', { runId: run.id }))
|
||||
if (['awaiting_approval', 'ready'].includes(release.status))
|
||||
@ -597,7 +644,7 @@ export class ControlPlane {
|
||||
run.status = 'canceled';
|
||||
if (!run.providerProxyActiveRequest) run.reservedTokens = 0;
|
||||
clearWaiting(run);
|
||||
await invalidatePending('The run was canceled');
|
||||
await invalidatePending('运行已取消');
|
||||
} else if (data.action === 'pause') {
|
||||
run.status = 'paused';
|
||||
clearWaiting(run);
|
||||
@ -621,7 +668,7 @@ export class ControlPlane {
|
||||
run.repairCount = 0;
|
||||
run.lastFailure = undefined;
|
||||
// A review or release covered the change of the old requirement; the new one is reviewed again.
|
||||
await invalidatePending('The requirement was revised');
|
||||
await invalidatePending('需求已修改');
|
||||
} else if (
|
||||
run.status === 'waiting' &&
|
||||
(run.waitingReason === 'repair_exhausted' || run.waitingReason === 'integration_conflict') &&
|
||||
@ -631,7 +678,7 @@ export class ControlPlane {
|
||||
run.stage = 'implement';
|
||||
run.verifiedCommit = undefined;
|
||||
run.repairCount += 1;
|
||||
message = `Another repair round was authorized (round ${run.repairCount}).`;
|
||||
message = `已授权再修复一轮(第 ${run.repairCount} 轮)。`;
|
||||
}
|
||||
const provider = (await this.providerInfo(tx)).find(p => p.id === run.provider)!;
|
||||
if (run.activeMilliseconds >= run.budget.maxMinutes * 60_000)
|
||||
@ -764,7 +811,7 @@ export class ControlPlane {
|
||||
await tx.event(
|
||||
id,
|
||||
'code_review.skipped',
|
||||
`The change touches ${paths.length} backend path(s); everyone who asked for it may change backend code directly, so it needs no review.`,
|
||||
`改动涉及 ${paths.length} 个后端路径;提出需求的人都可以直接修改后端代码,因此无需审核。`,
|
||||
{
|
||||
sourceCommit: input.sourceCommit,
|
||||
requestedBy: run.requestedBy,
|
||||
@ -800,7 +847,7 @@ export class ControlPlane {
|
||||
await tx.event(
|
||||
id,
|
||||
'code_review.requested',
|
||||
`The change touches ${paths.length} path(s) held for review${run.sourceBranch ? ` and brings branch ${run.sourceBranch}` : ''}; a person reviews it before it reaches ${project.targetBranch}.`,
|
||||
`改动涉及 ${paths.length} 个需要审核的路径${run.sourceBranch ? `,并带入分支 ${run.sourceBranch}` : ''};合入 ${project.targetBranch} 之前需要人工审核。`,
|
||||
{ reviewId: review.id, sourceCommit: input.sourceCommit, paths: paths.slice(0, 50) },
|
||||
);
|
||||
return { required: true as const, reviewId: review.id, paths: review.paths };
|
||||
@ -850,8 +897,8 @@ export class ControlPlane {
|
||||
id,
|
||||
data.decision === 'approve' ? 'code_review.approved' : 'code_review.rejected',
|
||||
data.decision === 'approve'
|
||||
? `${who} approved the change ${data.sourceCommit.slice(0, 12)}${data.comment ? `: ${redact(data.comment)}` : ''}`
|
||||
: `${who} asked for changes: ${redact(data.comment ?? '')}`,
|
||||
? `${who} 批准了改动 ${data.sourceCommit.slice(0, 12)}${data.comment ? `:${redact(data.comment)}` : ''}`
|
||||
: `${who} 要求修改:${redact(data.comment ?? '')}`,
|
||||
{ reviewId: review.id, decidedBy: actor.userId },
|
||||
);
|
||||
const waiting = run.status === 'waiting' && run.waitingReason === 'code_review';
|
||||
@ -912,18 +959,13 @@ export class ControlPlane {
|
||||
throw new DomainError('QUOTA_EXCEEDED', 'Tenant token reservation limit is exhausted', 429);
|
||||
run = touch({ ...run, budget: nextBudget, reservedTokens: remaining });
|
||||
await tx.put('run', run);
|
||||
await tx.event(
|
||||
id,
|
||||
'run.budget.updated',
|
||||
'An administrator adjusted the run budget; execution remains stopped.',
|
||||
{
|
||||
actorId: actor.userId,
|
||||
oldBudget,
|
||||
newBudget: nextBudget,
|
||||
usedTokens: run.usedTokens,
|
||||
activeMilliseconds: run.activeMilliseconds,
|
||||
},
|
||||
);
|
||||
await tx.event(id, 'run.budget.updated', '管理员调整了运行预算;执行仍保持停止。', {
|
||||
actorId: actor.userId,
|
||||
oldBudget,
|
||||
newBudget: nextBudget,
|
||||
usedTokens: run.usedTokens,
|
||||
activeMilliseconds: run.activeMilliseconds,
|
||||
});
|
||||
return run;
|
||||
});
|
||||
});
|
||||
@ -948,7 +990,7 @@ export class ControlPlane {
|
||||
// The executor of a requested pause stopped before completing it: the run pauses instead.
|
||||
if (run.pauseRequestedAt) {
|
||||
this.accountTime(run);
|
||||
await finishPause(tx, run, 'The run paused; its executor stopped before it could save its work.');
|
||||
await finishPause(tx, run, '运行已暂停;执行器在保存工作之前就已停止。');
|
||||
await tx.put('run', touch(run));
|
||||
return undefined;
|
||||
}
|
||||
@ -969,7 +1011,7 @@ export class ControlPlane {
|
||||
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 });
|
||||
await tx.event(id, 'run.leased', '执行进程已接手运行。', { fencingToken: run.lease.token });
|
||||
return run;
|
||||
});
|
||||
// Thrown after the transaction, which keeps the completed pause.
|
||||
@ -983,7 +1025,7 @@ export class ControlPlane {
|
||||
if (run.status !== 'running') throw new DomainError('RUN_NOT_RUNNING', 'Run is no longer running');
|
||||
this.accountTime(run);
|
||||
if (run.pauseRequestedAt && Date.now() - Date.parse(run.pauseRequestedAt) > PAUSE_GRACE_MS) {
|
||||
await finishPause(tx, run, 'The run paused; its executor did not confirm saving its work in time.');
|
||||
await finishPause(tx, run, '运行已暂停;执行器没有及时确认保存工作。');
|
||||
await tx.put('run', touch(run));
|
||||
return run;
|
||||
}
|
||||
@ -1013,9 +1055,7 @@ export class ControlPlane {
|
||||
await finishPause(
|
||||
tx,
|
||||
run,
|
||||
saved
|
||||
? 'The run paused; the work so far was saved as a checkpoint.'
|
||||
: 'The run paused before the agent started; there was no new work to save.',
|
||||
saved ? '运行已暂停;目前的工作已保存为检查点。' : '运行在 Agent 开始之前暂停;没有需要保存的新工作。',
|
||||
);
|
||||
await tx.put('run', touch(run));
|
||||
return run;
|
||||
@ -1027,7 +1067,7 @@ export class ControlPlane {
|
||||
for (const run of await tx.list<Run>('run', { status: 'running' })) {
|
||||
if (!run.pauseRequestedAt || Date.now() - Date.parse(run.pauseRequestedAt) <= PAUSE_GRACE_MS) continue;
|
||||
this.accountTime(run);
|
||||
await finishPause(tx, run, 'The run paused; its executor did not confirm saving its work in time.');
|
||||
await finishPause(tx, run, '运行已暂停;执行器没有及时确认保存工作。');
|
||||
await tx.put('run', touch(run));
|
||||
}
|
||||
});
|
||||
@ -1056,7 +1096,7 @@ export class ControlPlane {
|
||||
// A person asked for a pause; whatever stopped the execution, the run pauses.
|
||||
if (run.pauseRequestedAt) {
|
||||
this.accountTime(run);
|
||||
await finishPause(tx, run, `The run paused; its execution stopped first (${data.reason}).`);
|
||||
await finishPause(tx, run, `运行已暂停;执行先停止了(${data.reason})。`);
|
||||
await tx.put('run', touch(run));
|
||||
return run;
|
||||
}
|
||||
@ -1135,6 +1175,9 @@ export class ControlPlane {
|
||||
}
|
||||
if (run.status === 'running') run.lastActiveAt = now();
|
||||
else run.lastActiveAt = undefined;
|
||||
// The executor starts an implementation round with the project's skills in its prompt.
|
||||
if (actor.kind === 'worker' && run.stage === 'implement' && run.status === 'running')
|
||||
await recordSkillUse(tx, run);
|
||||
await tx.put('run', run);
|
||||
await tx.event(id, 'run.transition', redact(data.message), {
|
||||
stage: run.stage,
|
||||
@ -1180,10 +1223,10 @@ export class ControlPlane {
|
||||
run,
|
||||
conflict ? 'integration_conflict' : 'repair_exhausted',
|
||||
conflict
|
||||
? `The change still conflicts with the target branch after ${run.repairCount} repair round(s).`
|
||||
? `修复 ${run.repairCount} 轮后,改动仍与目标分支冲突。`
|
||||
: failure.kind === 'scope'
|
||||
? `The change still touches backend code after ${run.repairCount} repair round(s), which this frontend-only wish may not change: ${failure.outputTail.split('\n').slice(0, 5).join(', ')}.`
|
||||
: `Verification still fails after ${run.repairCount} repair round(s): ${failure.argv?.join(' ') ?? 'a protected check'} exited ${failure.exitCode ?? 'non-zero'}.`,
|
||||
? `修复 ${run.repairCount} 轮后,改动仍涉及后端代码,而这个仅限前端的愿望不能修改它们:${failure.outputTail.split('\n').slice(0, 5).join(', ')}。`
|
||||
: `修复 ${run.repairCount} 轮后,验证仍然失败:${failure.argv?.join(' ') ?? '受保护的检查'} 退出码 ${failure.exitCode ?? '非零'}。`,
|
||||
);
|
||||
await tx.put('run', touch(run));
|
||||
await tx.event(id, 'run.waiting', run.waitingDetail!, { reason: run.waitingReason });
|
||||
@ -1198,12 +1241,12 @@ export class ControlPlane {
|
||||
await tx.event(
|
||||
id,
|
||||
'run.repair',
|
||||
`Repair round ${run.repairCount} of ${run.budget.maxRepairRounds}: ${
|
||||
`修复第 ${run.repairCount} 轮(共 ${run.budget.maxRepairRounds} 轮):${
|
||||
failure.kind === 'integration_conflict'
|
||||
? 'resolve the merge conflict with the target branch.'
|
||||
? '解决与目标分支的合并冲突。'
|
||||
: failure.kind === 'scope'
|
||||
? 'take back the changes to backend code, which this frontend-only wish may not make.'
|
||||
: `fix ${failure.argv?.join(' ') ?? 'the failing check'} (exit ${failure.exitCode ?? 'non-zero'}).`
|
||||
? '撤回对后端代码的改动,这个仅限前端的愿望不能修改它们。'
|
||||
: `修复 ${failure.argv?.join(' ') ?? '失败的检查'}(退出码 ${failure.exitCode ?? '非零'})。`
|
||||
}`,
|
||||
{ repairCount: run.repairCount, evidenceId: failure.evidenceId },
|
||||
);
|
||||
@ -1334,10 +1377,15 @@ export class ControlPlane {
|
||||
verificationPolicyDigest: verificationPolicyDigest(project),
|
||||
} as Evidence;
|
||||
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,
|
||||
});
|
||||
await tx.event(
|
||||
run.id,
|
||||
'evidence.recorded',
|
||||
`${EVIDENCE_NAMES[data.type] ?? data.type}证据:${data.passed ? '通过' : '未通过'}。`,
|
||||
{
|
||||
evidenceId: evidence.id,
|
||||
sourceCommit: data.sourceCommit,
|
||||
},
|
||||
);
|
||||
const failure = failureSummary(evidence);
|
||||
if (failure) run.lastFailure = failure;
|
||||
if (data.type === 'integration' && data.passed) {
|
||||
@ -1356,11 +1404,11 @@ export class ControlPlane {
|
||||
clearWaiting(run);
|
||||
if (run.lease) run.lease.expiresAt = now();
|
||||
if (!run.providerProxyActiveRequest) run.reservedTokens = 0;
|
||||
await captureLesson(tx, run);
|
||||
await learnFromRun(tx, run);
|
||||
await tx.event(
|
||||
run.id,
|
||||
'run.succeeded',
|
||||
`Integrated ${run.integratedCommit} into ${project.targetBranch}; the project has no delivery configuration, so the run is complete.`,
|
||||
`已将 ${run.integratedCommit} 合入 ${project.targetBranch};项目没有交付配置,运行到此完成。`,
|
||||
{ integratedCommit: run.integratedCommit },
|
||||
);
|
||||
}
|
||||
@ -1405,8 +1453,18 @@ export class ControlPlane {
|
||||
verificationProfileRequired: isProduction(this.env),
|
||||
connectionId: connection?.id ?? null,
|
||||
bundleConfig: await resolveBundle(tx, run.bundleVersion),
|
||||
lessons: await projectLessons(tx, run.projectId),
|
||||
skills: await projectSkills(tx, run.projectId),
|
||||
// Only the executor writes the files into its workspace; people fetch them one at a time.
|
||||
attachments:
|
||||
actor.kind === 'worker' && run.attachments?.length
|
||||
? await Promise.all(
|
||||
run.attachments.map(async info => {
|
||||
const file = await must<Attachment>(tx, 'attachment', info.id);
|
||||
return { id: file.id, name: file.name, mediaType: file.mediaType, data: file.data };
|
||||
}),
|
||||
)
|
||||
: [],
|
||||
executorSettings: await runExecutorSettings(tx, run.provider),
|
||||
delivery: {
|
||||
release,
|
||||
verificationEvidenceId: current('verification'),
|
||||
|
||||
@ -21,7 +21,8 @@ export const planLimitsSchema = z
|
||||
.object({
|
||||
maxMembers: positive.max(10000),
|
||||
maxProjects: positive.max(10000),
|
||||
maxWorkspaces: positive.max(10000),
|
||||
/** The retired cloud IDE's allowance: still accepted in stored snapshots and plan files, ignored. */
|
||||
maxWorkspaces: positive.max(10000).optional(),
|
||||
maxConcurrentRuns: positive.max(1000),
|
||||
monthlyModelTokens: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
||||
execution: z
|
||||
@ -58,7 +59,6 @@ export const PILOT_PLAN: CommercialPlan = {
|
||||
limits: {
|
||||
maxMembers: 10,
|
||||
maxProjects: 10,
|
||||
maxWorkspaces: 5,
|
||||
maxConcurrentRuns: 2,
|
||||
monthlyModelTokens: 2_000_000,
|
||||
execution: { cpuMilli: 2000, memoryMiB: 4096, storageMiB: 10240 },
|
||||
@ -105,7 +105,6 @@ export function effectiveEntitlements(tenant: Tenant): {
|
||||
limits: {
|
||||
...structuredClone(PILOT_PLAN.limits),
|
||||
maxProjects: 20,
|
||||
maxWorkspaces: 10,
|
||||
maxConcurrentRuns: tenant.maxConcurrentRuns,
|
||||
monthlyModelTokens: tenant.tokenLimit,
|
||||
},
|
||||
@ -128,11 +127,11 @@ export function requirePlatformOperator(actor: Actor, env = process.env) {
|
||||
403,
|
||||
);
|
||||
}
|
||||
export async function requireCapacity(tx: Transaction, kind: 'project' | 'workspace') {
|
||||
export async function requireCapacity(tx: Transaction, kind: 'project') {
|
||||
const limits = effectiveEntitlements(await tx.tenant()).limits;
|
||||
// Projects and workspaces cannot be deleted yet (project archiving is planned), so every one counts.
|
||||
// Projects cannot be deleted yet (project archiving is planned), so every one counts.
|
||||
const count = (await tx.list(kind)).length;
|
||||
if (count >= (kind === 'project' ? limits.maxProjects : limits.maxWorkspaces))
|
||||
if (count >= limits.maxProjects)
|
||||
throw new DomainError('ENTITLEMENT_LIMIT', `Tenant ${kind} allowance is exhausted`, 429);
|
||||
}
|
||||
export function heldAllocation(run: Run): boolean {
|
||||
@ -289,8 +288,12 @@ export async function recordAllocation(
|
||||
run.id,
|
||||
`resources.${status}`,
|
||||
status === 'released'
|
||||
? 'Provisioner confirmed compute Jobs stopped; retained persistent storage remains unmeasured.'
|
||||
: `Sandbox resource allocation ${status}.`,
|
||||
? '资源开通服务确认计算任务已停止;保留的持久存储暂不计量。'
|
||||
: {
|
||||
provisioned: '沙箱资源已开通。',
|
||||
unknown: '沙箱资源分配结果未知。',
|
||||
releasing: '正在释放沙箱资源。',
|
||||
}[status],
|
||||
{ generation },
|
||||
);
|
||||
}
|
||||
@ -307,7 +310,6 @@ export class Entitlements {
|
||||
counts: {
|
||||
members: (await tx.members()).filter(member => member.active).length,
|
||||
projects: (await tx.list('project')).length,
|
||||
workspaces: (await tx.list('workspace')).length,
|
||||
reservedExecutionSlots: (await tx.list<Run>('run')).filter(occupiesExecutionSlot).length,
|
||||
},
|
||||
};
|
||||
|
||||
179
apps/api/src/domain/executor-settings.ts
Normal file
179
apps/api/src/domain/executor-settings.ts
Normal file
@ -0,0 +1,179 @@
|
||||
import { z } from 'zod';
|
||||
import {
|
||||
DomainError,
|
||||
executorSettingFields,
|
||||
executorSettingsInput,
|
||||
isProduction,
|
||||
plainHttpAllowed,
|
||||
providerCatalog,
|
||||
providers,
|
||||
type Actor,
|
||||
type Entity,
|
||||
type ExecutorSettings,
|
||||
type ProviderId,
|
||||
} from '@wishforge/contracts';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { record, touch } from './control-plane.js';
|
||||
import { nativeClaudeEnabled } from './local-execution.js';
|
||||
import { requireHuman, requireRole } from './policy.js';
|
||||
|
||||
const providerParam = z.enum(providers);
|
||||
type Settings = Omit<ExecutorSettings, keyof Entity | 'provider' | 'updatedBy'>;
|
||||
|
||||
/**
|
||||
* Why a tenant's base URL cannot apply to this executor here, or undefined when it can: only with
|
||||
* the tenant's own key, sent straight to the model API. Production and the model proxy use the
|
||||
* operator's upstream, and the deployment's native Claude account must never be sent elsewhere.
|
||||
*/
|
||||
export function tenantBaseUrlBlocked(env: NodeJS.ProcessEnv, tenantId: string, provider: ProviderId) {
|
||||
if (!providerCatalog[provider].settings.includes('baseUrl')) return 'unsupported';
|
||||
if (isProduction(env) || env.WF_PROVIDER_PROXY_ENABLED === 'true') return 'operator_managed';
|
||||
if (provider === 'claude-code' && nativeClaudeEnabled(env, tenantId)) return 'native_account';
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/** An HTTPS origin with an optional path; plain HTTP only where services may use it (plainHttpAllowed). */
|
||||
export function tenantBaseUrl(env: NodeJS.ProcessEnv, value: string) {
|
||||
let url: URL;
|
||||
try {
|
||||
url = new URL(value);
|
||||
} catch {
|
||||
throw new DomainError('INVALID_BASE_URL', 'The base URL must be a URL such as https://gateway.example/v1', 400);
|
||||
}
|
||||
if (
|
||||
!(url.protocol === 'https:' || plainHttpAllowed(env, url)) ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.search ||
|
||||
url.hash
|
||||
)
|
||||
throw new DomainError(
|
||||
'INVALID_BASE_URL',
|
||||
'The base URL must use HTTPS and carry no credentials, query or fragment',
|
||||
400,
|
||||
);
|
||||
return url.href.replace(/\/$/, '');
|
||||
}
|
||||
|
||||
/** The settings of one executor, or none; tolerant of fields the executor no longer takes. */
|
||||
export async function storedExecutorSettings(tx: Transaction, provider: ProviderId): Promise<Settings> {
|
||||
const stored = await tx.get<ExecutorSettings>('executor_settings', provider);
|
||||
if (!stored) return {};
|
||||
const supported = providerCatalog[provider].settings;
|
||||
return Object.fromEntries(
|
||||
executorSettingFields.flatMap(field =>
|
||||
supported.includes(field) && stored[field] !== undefined && stored[field] !== null
|
||||
? [[field, stored[field]]]
|
||||
: [],
|
||||
),
|
||||
) as Settings;
|
||||
}
|
||||
|
||||
/** What the executor of a run applies besides the model: its run context's `executorSettings`. */
|
||||
export async function runExecutorSettings(tx: Transaction, provider: ProviderId) {
|
||||
const { smallFastModel, fallbackModel, timeoutMinutes } = await storedExecutorSettings(tx, provider);
|
||||
return {
|
||||
...(smallFastModel ? { smallFastModel } : {}),
|
||||
...(fallbackModel ? { fallbackModel } : {}),
|
||||
...(timeoutMinutes ? { timeoutMinutes } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A tenant admin's settings for each executor: gateway, models and time limit. Unset fields keep
|
||||
* the operator's configuration. Settings are not secret; keys stay in provider connections.
|
||||
*/
|
||||
export class ExecutorSettingsService {
|
||||
constructor(
|
||||
private store: Store,
|
||||
private env = process.env,
|
||||
) {}
|
||||
private view(tenantId: string, provider: ProviderId, settings: Settings, stored?: ExecutorSettings) {
|
||||
const blocked = tenantBaseUrlBlocked(this.env, tenantId, provider);
|
||||
return {
|
||||
provider,
|
||||
...settings,
|
||||
supported: providerCatalog[provider].settings,
|
||||
baseUrlAllowed: !blocked,
|
||||
...(blocked && providerCatalog[provider].settings.includes('baseUrl') ? { baseUrlBlocked: blocked } : {}),
|
||||
...(stored ? { version: stored.version, updatedAt: stored.updatedAt } : {}),
|
||||
};
|
||||
}
|
||||
list(actor: Actor) {
|
||||
requireRole(actor, ['owner', 'admin']);
|
||||
return this.store.read(actor.tenantId, async tx =>
|
||||
Promise.all(
|
||||
providers.map(async provider =>
|
||||
this.view(
|
||||
actor.tenantId,
|
||||
provider,
|
||||
await storedExecutorSettings(tx, provider),
|
||||
await tx.get<ExecutorSettings>('executor_settings', provider),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
async get(actor: Actor, provider: unknown) {
|
||||
requireRole(actor, ['owner', 'admin']);
|
||||
const id = providerParam.parse(provider);
|
||||
return this.store.read(actor.tenantId, async tx =>
|
||||
this.view(
|
||||
actor.tenantId,
|
||||
id,
|
||||
await storedExecutorSettings(tx, id),
|
||||
await tx.get<ExecutorSettings>('executor_settings', id),
|
||||
),
|
||||
);
|
||||
}
|
||||
/** Replaces the executor's settings; the same body again changes nothing. */
|
||||
async put(actor: Actor, provider: unknown, input: unknown) {
|
||||
requireHuman(actor);
|
||||
requireRole(actor, ['owner', 'admin']);
|
||||
const id = providerParam.parse(provider);
|
||||
const data = executorSettingsInput.parse(input);
|
||||
const supported = providerCatalog[id].settings;
|
||||
const unsupported = executorSettingFields.filter(field => data[field] !== undefined && !supported.includes(field));
|
||||
if (unsupported.length)
|
||||
throw new DomainError(
|
||||
'SETTING_UNSUPPORTED',
|
||||
`${providerCatalog[id].name} does not take these settings: ${unsupported.join(', ')}`,
|
||||
400,
|
||||
);
|
||||
const settings: Settings = Object.fromEntries(
|
||||
executorSettingFields.flatMap(field => (data[field] !== undefined ? [[field, data[field]]] : [])),
|
||||
);
|
||||
if (settings.baseUrl !== undefined) {
|
||||
const blocked = tenantBaseUrlBlocked(this.env, actor.tenantId, id);
|
||||
if (blocked)
|
||||
throw new DomainError(
|
||||
'BASE_URL_OPERATOR_MANAGED',
|
||||
blocked === 'native_account'
|
||||
? 'This executor runs on the deployment’s own Claude account, whose gateway the operator sets'
|
||||
: 'The operator manages the model gateway of this deployment',
|
||||
400,
|
||||
);
|
||||
settings.baseUrl = tenantBaseUrl(this.env, settings.baseUrl);
|
||||
}
|
||||
if (settings.fallbackModel && settings.fallbackModel === settings.defaultModel)
|
||||
throw new DomainError('INVALID_FALLBACK_MODEL', 'The fallback model must differ from the default model', 400);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const current = await tx.get<ExecutorSettings>('executor_settings', id);
|
||||
const same =
|
||||
current &&
|
||||
executorSettingFields.every(field => (current[field] ?? undefined) === (settings[field] ?? undefined));
|
||||
if (same) return this.view(actor.tenantId, id, settings, current);
|
||||
const next: ExecutorSettings = {
|
||||
...(current ? touch(current) : { ...record(actor.tenantId), id }),
|
||||
provider: id,
|
||||
...Object.fromEntries(executorSettingFields.map(field => [field, undefined])),
|
||||
...settings,
|
||||
updatedBy: actor.userId,
|
||||
};
|
||||
// Stored rows are JSON: unset fields are left out rather than kept as undefined.
|
||||
for (const field of executorSettingFields) if (next[field] === undefined) delete next[field];
|
||||
await tx.put('executor_settings', next);
|
||||
return this.view(actor.tenantId, id, settings, next);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,137 +0,0 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { z } from 'zod';
|
||||
import { DomainError, plainHttpAllowed } from '@wishforge/contracts';
|
||||
import { dispatchTenants } from '../tenants.js';
|
||||
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:' || plainHttpAllowed(env, value))
|
||||
)
|
||||
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<GatewayTarget | undefined> {
|
||||
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 = dispatchTenants(env);
|
||||
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<string[]> {
|
||||
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 };
|
||||
}
|
||||
@ -1,8 +1,5 @@
|
||||
import { DomainError, type Actor, type Project, type Run, plainHttpAllowed } from '@wishforge/contracts';
|
||||
|
||||
/** IDE workspaces push here; a wish starting from such a branch verifies and merges it. */
|
||||
export const IDE_BRANCH_PREFIX = 'ide/';
|
||||
|
||||
/** Only the trusted control plane can provision repositories or mint repository-scoped access. */
|
||||
export class GitStorage {
|
||||
constructor(
|
||||
@ -44,35 +41,6 @@ export class GitStorage {
|
||||
defaultBranch: project.targetBranch,
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Write access for people editing in the project's IDE workspace. The Git service lets it
|
||||
* update `ide/` branches only: the target branch changes through verified wishes alone.
|
||||
*/
|
||||
workspaceCredential(project: Project) {
|
||||
if (project.repositoryStorage !== 'platform' || !project.repositoryUrl)
|
||||
throw new DomainError(
|
||||
'WORKSPACE_REPOSITORY_UNSUPPORTED',
|
||||
'Only repositories hosted by the platform are cloned into IDE workspaces',
|
||||
409,
|
||||
);
|
||||
return this.request<{ token: string; expiresAt: string }>('/tokens', {
|
||||
tenantId: project.tenantId,
|
||||
projectId: project.id,
|
||||
permission: 'write',
|
||||
ttlSeconds: 900,
|
||||
refPrefix: `refs/heads/${IDE_BRANCH_PREFIX}`,
|
||||
});
|
||||
}
|
||||
/** The branches people pushed from the IDE, most recent first. */
|
||||
async ideBranches(project: Project) {
|
||||
if (project.repositoryStorage !== 'platform' || !project.repositoryUrl || !this.configured()) return [];
|
||||
const listed = await this.request<{ branches: { name: string; commit: string; updatedAt: string }[] }>('/refs', {
|
||||
tenantId: project.tenantId,
|
||||
projectId: project.id,
|
||||
prefix: `refs/heads/${IDE_BRANCH_PREFIX}`,
|
||||
});
|
||||
return listed.branches;
|
||||
}
|
||||
async credential(actor: Actor, project: Project, run: Run) {
|
||||
if (
|
||||
project.repositoryStorage !== 'platform' ||
|
||||
|
||||
@ -336,7 +336,7 @@ export class GitHubIntegration {
|
||||
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.', {
|
||||
await tx.event(runId, 'github.pr.requested', '已在联系 GitHub 之前记录拉取请求的创建。', {
|
||||
operationId: id,
|
||||
});
|
||||
return { operation, dispatch: true };
|
||||
@ -492,11 +492,7 @@ export class GitHubIntegration {
|
||||
run.verifiedCommit = undefined;
|
||||
run.stage = 'verify';
|
||||
if (!activeVerifier) {
|
||||
setWaiting(
|
||||
run,
|
||||
'github_review',
|
||||
'GitHub merged a commit other than the verified one; resume the run to verify the merged commit.',
|
||||
);
|
||||
setWaiting(run, 'github_review', 'GitHub 合入的提交不是验证过的那个;请继续运行以验证合入的提交。');
|
||||
run.lastActiveAt = undefined;
|
||||
if (run.lease) run.lease.expiresAt = new Date().toISOString();
|
||||
}
|
||||
@ -517,19 +513,14 @@ export class GitHubIntegration {
|
||||
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,
|
||||
},
|
||||
);
|
||||
await tx.event(run.id, 'github.pr.observed', '已核对 GitHub 拉取请求状态;合入验收与源码验证是两回事。', {
|
||||
operationId: id,
|
||||
number: snapshot.number,
|
||||
state: next.status,
|
||||
headSha: snapshot.headSha,
|
||||
baseSha: snapshot.baseSha,
|
||||
mergeCommitSha: snapshot.mergeCommitSha,
|
||||
});
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,90 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
import { lessonInput, type Actor, type Lesson, type Run } from '@wishforge/contracts';
|
||||
import { LESSON_LIMIT, repairLesson } from '@wishforge/agent';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { must, projectAccess, record, touch } from './control-plane.js';
|
||||
import { redact, requireHuman, requireRole } from './policy.js';
|
||||
|
||||
const statusInput = z.object({ status: z.enum(['active', 'disabled']) }).strict();
|
||||
const newestFirst = (a: Lesson, b: Lesson) => b.createdAt.localeCompare(a.createdAt);
|
||||
|
||||
/** Active lessons of a project, most recent first, as they are injected into prompts. */
|
||||
export async function projectLessons(tx: Transaction, projectId: string): Promise<string[]> {
|
||||
return (await tx.list<Lesson>('lesson', { projectId, status: 'active' }))
|
||||
.sort(newestFirst)
|
||||
.slice(0, LESSON_LIMIT)
|
||||
.map(lesson => lesson.summary);
|
||||
}
|
||||
|
||||
/** A run that needed a repair round before it succeeded leaves a lesson for later runs of its project. */
|
||||
export async function captureLesson(tx: Transaction, run: Run) {
|
||||
if (!run.repairCount || !run.lastFailure) return;
|
||||
const lessons = await tx.list<Lesson>('lesson', { projectId: run.projectId });
|
||||
if (lessons.some(lesson => lesson.sourceRunId === run.id && lesson.kind === 'repair')) return;
|
||||
const lesson: Lesson = {
|
||||
...record(run.tenantId),
|
||||
projectId: run.projectId,
|
||||
kind: 'repair',
|
||||
summary: redact(repairLesson(run.lastFailure, run.implementationSummary)),
|
||||
sourceRunId: run.id,
|
||||
status: 'active',
|
||||
createdBy: 'wishforge',
|
||||
};
|
||||
await tx.put('lesson', lesson);
|
||||
await tx.event(run.id, 'lesson.recorded', 'Recorded a project lesson from the repaired failure.', {
|
||||
lessonId: lesson.id,
|
||||
});
|
||||
}
|
||||
|
||||
/** Owners and admins review what later runs will be told. */
|
||||
export class Lessons {
|
||||
constructor(private readonly store: Store) {}
|
||||
async list(actor: Actor, projectId: string) {
|
||||
return this.store.read(actor.tenantId, async tx => {
|
||||
await projectAccess(tx, actor, projectId);
|
||||
return (await tx.list<Lesson>('lesson', { projectId })).sort(newestFirst);
|
||||
});
|
||||
}
|
||||
async create(actor: Actor, projectId: string, input: unknown) {
|
||||
this.authorize(actor);
|
||||
const { summary } = lessonInput.parse(input);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
await projectAccess(tx, actor, projectId);
|
||||
const lesson: Lesson = {
|
||||
...record(actor.tenantId),
|
||||
projectId,
|
||||
kind: 'convention',
|
||||
summary: redact(summary),
|
||||
status: 'active',
|
||||
createdBy: actor.userId,
|
||||
};
|
||||
await tx.put('lesson', lesson);
|
||||
return lesson;
|
||||
});
|
||||
}
|
||||
async setStatus(actor: Actor, id: string, input: unknown) {
|
||||
this.authorize(actor);
|
||||
const { status } = statusInput.parse(input);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const lesson = await must<Lesson>(tx, 'lesson', id);
|
||||
await projectAccess(tx, actor, lesson.projectId);
|
||||
if (lesson.status === status) return lesson;
|
||||
const next = { ...touch(lesson), status };
|
||||
await tx.put('lesson', next);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
async remove(actor: Actor, id: string) {
|
||||
this.authorize(actor);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const lesson = await must<Lesson>(tx, 'lesson', id);
|
||||
await projectAccess(tx, actor, lesson.projectId);
|
||||
await tx.remove('lesson', id);
|
||||
return { deleted: true };
|
||||
});
|
||||
}
|
||||
private authorize(actor: Actor) {
|
||||
requireRole(actor, ['owner', 'admin']);
|
||||
requireHuman(actor);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,11 @@
|
||||
import { countedTokens, type ProviderEventType } from '@wishforge/contracts';
|
||||
import { redact } from './policy.js';
|
||||
|
||||
/** How a file_change message starts before its paths; older events start with `Changed `. */
|
||||
export const CHANGED_PREFIX = '改动了:';
|
||||
/** The paths a file_change message lists, for events written in either language. */
|
||||
export const changedPathsOf = (message: string) => /^(?:Changed |改动了:)(.+)$/.exec(message)?.[1];
|
||||
|
||||
const text = (value: unknown, max = 2000) => (typeof value === 'string' ? redact(value).slice(0, max) : undefined);
|
||||
const count = (value: unknown) =>
|
||||
Number.isSafeInteger(value) && (value as number) >= 0 ? (value as number) : undefined;
|
||||
@ -16,12 +21,12 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
const data = event.data;
|
||||
switch (event.type) {
|
||||
case 'output':
|
||||
return { message: text(data.text, 5000)?.trim() || 'Agent message', data: {} };
|
||||
return { message: text(data.text, 5000)?.trim() || 'Agent 回复', data: {} };
|
||||
case 'command': {
|
||||
const command = text(data.command) ?? '';
|
||||
const exitCode = Number.isInteger(data.exitCode) ? (data.exitCode as number) : undefined;
|
||||
return {
|
||||
message: `$ ${command}${exitCode === undefined ? '' : ` (exit ${exitCode})`}`,
|
||||
message: `$ ${command}${exitCode === undefined ? '' : ` (退出码 ${exitCode})`}`,
|
||||
data: { command, ...(exitCode === undefined ? {} : { exitCode }) },
|
||||
};
|
||||
}
|
||||
@ -33,8 +38,8 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
});
|
||||
return {
|
||||
message: changes.length
|
||||
? `Changed ${changes.map(change => change.path).join(', ')}`.slice(0, 5000)
|
||||
: 'Changed files',
|
||||
? `${CHANGED_PREFIX}${changes.map(change => change.path).join(', ')}`.slice(0, 5000)
|
||||
: '改动了文件',
|
||||
data: { changes },
|
||||
};
|
||||
}
|
||||
@ -44,7 +49,7 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
return step ? [{ text: step, completed: (item as { completed?: unknown }).completed === true }] : [];
|
||||
});
|
||||
return {
|
||||
message: items.map(item => `${item.completed ? '[x]' : '[ ]'} ${item.text}`).join('\n') || 'Plan updated',
|
||||
message: items.map(item => `${item.completed ? '[x]' : '[ ]'} ${item.text}`).join('\n') || '计划已更新',
|
||||
data: { items },
|
||||
};
|
||||
}
|
||||
@ -57,12 +62,10 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
// which may undercount the output of the last one.
|
||||
const estimated = data.estimated === true;
|
||||
const counted = countedTokens(data);
|
||||
const label = estimated
|
||||
? 'Model usage (estimated: the session stopped before reporting its total)'
|
||||
: 'Model usage';
|
||||
const label = estimated ? '模型用量(估算:会话在报告总量之前已停止)' : '模型用量';
|
||||
if (cacheCreation || cacheRead)
|
||||
return {
|
||||
message: `${label}: ${input} input, ${output} output, ${cacheCreation} cache creation and ${cacheRead} cache read tokens; ${counted} count toward the budget (cache reads a tenth)`,
|
||||
message: `${label}:输入 ${input}、输出 ${output}、缓存创建 ${cacheCreation}、缓存读取 ${cacheRead} 个 token;计入预算 ${counted} 个(缓存读取按十分之一计)`,
|
||||
data: {
|
||||
input,
|
||||
output,
|
||||
@ -74,34 +77,34 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
},
|
||||
};
|
||||
return {
|
||||
message: `${label}: ${input} input and ${output} output tokens`,
|
||||
message: `${label}:输入 ${input}、输出 ${output} 个 token`,
|
||||
data: { input, output, ...(estimated ? { estimated } : {}) },
|
||||
};
|
||||
}
|
||||
case 'failed':
|
||||
return { message: text(data.message, 5000) ?? 'The executor reported a failure', data: {} };
|
||||
return { message: text(data.message, 5000) ?? '执行器报告失败', data: {} };
|
||||
case 'started':
|
||||
// Only development sandboxes run an executor without a command sandbox of its own: one that
|
||||
// cannot start here, or a CLI that has none.
|
||||
return data.unsandboxed === true
|
||||
? {
|
||||
message: `Executor started without its command sandbox (${text(data.reason, 300) ?? 'no details'}); the run's development sandbox is the only boundary`,
|
||||
message: `执行器启动,但没有自己的命令沙箱(${text(data.reason, 300) ?? '没有详情'});运行的开发沙箱是唯一的隔离边界`,
|
||||
data: { unsandboxed: true },
|
||||
}
|
||||
: data.commands === 'sandboxed'
|
||||
? {
|
||||
message: 'Executor started; its commands run in a sandbox without network access',
|
||||
message: '执行器已启动;它的命令在无网络的沙箱里运行',
|
||||
data: { commands: 'sandboxed' },
|
||||
}
|
||||
: data.commands === 'none'
|
||||
? {
|
||||
message: `Executor started with file tools only: its command sandbox cannot start here (${text(data.reason, 300) ?? 'no details'})`,
|
||||
message: `执行器已启动,仅能使用文件工具:它的命令沙箱在这里无法启动(${text(data.reason, 300) ?? '没有详情'})`,
|
||||
data: { commands: 'none' },
|
||||
}
|
||||
: { message: 'Executor started', data: {} };
|
||||
: { message: '执行器已启动', data: {} };
|
||||
case 'completed':
|
||||
return { message: 'Executor finished its turn', data: {} };
|
||||
return { message: '执行器完成本轮', data: {} };
|
||||
case 'canceled':
|
||||
return { message: 'Executor stopped', data: {} };
|
||||
return { message: '执行器已停止', data: {} };
|
||||
}
|
||||
}
|
||||
|
||||
@ -130,9 +130,9 @@ export async function reapStaleProxyRequests(store: Store, tenantId: string) {
|
||||
const run = await tx.get<Run>('run', op.runId);
|
||||
if (!run) continue;
|
||||
run.reservedTokens = Math.max(run.reservedTokens, op.reservedTokens);
|
||||
blockRun(run, 'A model request lost its proxy before it finished; reconcile it before the run continues.');
|
||||
blockRun(run, '一个模型请求在完成之前失去了代理进程;请先对账,运行才能继续。');
|
||||
await tx.put('run', touch(run));
|
||||
await tx.event(run.id, 'provider.proxy.unknown', 'A model request lost its proxy before it finished.', {
|
||||
await tx.event(run.id, 'provider.proxy.unknown', '一个模型请求在完成之前失去了代理进程。', {
|
||||
requestId: op.id,
|
||||
});
|
||||
}
|
||||
@ -211,7 +211,7 @@ export class ProviderProxy {
|
||||
op.unknownReason = `reconciled by ${actor.userId}${data.usageTokens === undefined ? ' (reservation charged)' : ''}`;
|
||||
await tx.put('provider_proxy_request', touch(op));
|
||||
await tx.put('run', touch(run));
|
||||
await tx.event(run.id, 'provider.proxy.reconciled', `A model request was reconciled with ${tokens} tokens.`, {
|
||||
await tx.event(run.id, 'provider.proxy.reconciled', `一个模型请求已对账,计 ${tokens} 个 token。`, {
|
||||
requestId: op.id,
|
||||
usageTokens: tokens,
|
||||
actorId: actor.userId,
|
||||
@ -548,7 +548,7 @@ export class ProviderProxy {
|
||||
const outputLimit = Math.min(parsed.data.max_output_tokens ?? 8192, remaining - inputReserve);
|
||||
if (outputLimit < 16) {
|
||||
// The run waits for a larger budget; the executor would otherwise report a bare 429.
|
||||
unaffordable = `The next model request needs ${inputReserve + 16} tokens of budget (one per request byte, plus output), but ${Math.max(0, remaining)} of ${run.budget.maxTokens} remain. Submit the wish again with a larger token budget.`;
|
||||
unaffordable = `下一个模型请求需要 ${inputReserve + 16} 个 token 的预算(请求每字节一个,另加输出),但 ${run.budget.maxTokens} 个中只剩 ${Math.max(0, remaining)} 个。请用更大的 token 预算重新提交愿望。`;
|
||||
setWaiting(run, 'budget', unaffordable);
|
||||
if (run.lease) run.lease.expiresAt = now();
|
||||
await tx.put('run', touch(run));
|
||||
@ -582,7 +582,7 @@ export class ProviderProxy {
|
||||
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.', {
|
||||
await tx.event(run.id, 'provider.proxy.dispatched', '模型请求已在发往上游之前记录。', {
|
||||
requestId,
|
||||
reservedTokens: reserved,
|
||||
});
|
||||
@ -661,10 +661,7 @@ export class ProviderProxy {
|
||||
}
|
||||
if (status === 'unknown') {
|
||||
run.reservedTokens = Math.max(run.reservedTokens, op.reservedTokens);
|
||||
blockRun(
|
||||
run,
|
||||
'A model request ended without a confirmed outcome or usage; reconcile it before the run continues.',
|
||||
);
|
||||
blockRun(run, '一个模型请求结束时没有确认的结果或用量;请先对账,运行才能继续。');
|
||||
} else if (run.providerProxyActiveRequest === requestId) {
|
||||
run.providerProxyActiveRequest = undefined;
|
||||
if (['canceled', 'failed', 'succeeded'].includes(run.status)) run.reservedTokens = 0;
|
||||
@ -673,7 +670,7 @@ export class ProviderProxy {
|
||||
run.usedTokens >= run.budget.maxTokens &&
|
||||
!['canceled', 'failed', 'succeeded', 'paused'].includes(run.status)
|
||||
) {
|
||||
setWaiting(run, 'budget', 'The run used up its model token budget.');
|
||||
setWaiting(run, 'budget', '运行已用完模型 token 预算。');
|
||||
if (run.lease) run.lease.expiresAt = now();
|
||||
}
|
||||
await tx.put('provider_proxy_request', touch(op));
|
||||
@ -681,9 +678,7 @@ export class ProviderProxy {
|
||||
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.',
|
||||
status === 'unknown' ? '上游结果或用量未知;对账之前新的模型请求会被拦下。' : '已记录上游请求的可信结果。',
|
||||
{ requestId, usageTokens, responseId: response?.id },
|
||||
);
|
||||
});
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { assertPocBuild } from './local-poc.js';
|
||||
import { z } from 'zod';
|
||||
import { requireDeploymentSecretPolicy } from './deployment-secret-policy.js';
|
||||
import { gatewayIdentityOrigins } from './gateway-target.js';
|
||||
import {
|
||||
DomainError,
|
||||
environmentInput,
|
||||
@ -32,7 +31,7 @@ import {
|
||||
setWaiting,
|
||||
clearWaiting,
|
||||
} from './control-plane.js';
|
||||
import { captureLesson } from './lessons.js';
|
||||
import { learnFromRun } from './skills.js';
|
||||
import { digest, releaseRisk, requireRole, requireHuman } from './policy.js';
|
||||
import { requirePermission } from './permissions.js';
|
||||
|
||||
@ -76,7 +75,7 @@ export function deploymentIdentity(actor: Actor, environmentId: string, runId: s
|
||||
export async function noteReleaseFailure(tx: Transaction, release: Release, detail: string) {
|
||||
const run = await tx.get<Run>('run', release.runId);
|
||||
if (!run || run.status !== 'waiting' || run.stage !== 'release') return;
|
||||
setWaiting(run, 'deployment', `${detail} Continue the run to request a new release, or submit a follow-up wish.`);
|
||||
setWaiting(run, 'deployment', `${detail}继续运行可以申请新的发布,也可以提交一个后续愿望。`);
|
||||
await tx.put('run', touch(run));
|
||||
await tx.event(run.id, 'release.failed', detail, { releaseId: release.id });
|
||||
}
|
||||
@ -89,9 +88,9 @@ export async function expireApprovals(store: Store, tenantId: string) {
|
||||
await tx.put('approval', { ...touch(approval), status: 'expired' });
|
||||
const release = await tx.get<Release>('release', approval.releaseId);
|
||||
if (release?.status !== 'awaiting_approval') continue;
|
||||
const failed = { ...touch(release), status: 'failed' as const, failureReason: 'The approval expired' };
|
||||
const failed = { ...touch(release), status: 'failed' as const, failureReason: '审批已过期' };
|
||||
await tx.put('release', failed);
|
||||
await noteReleaseFailure(tx, failed, `The approval of release ${release.id} expired.`);
|
||||
await noteReleaseFailure(tx, failed, `发布 ${release.id} 的审批已过期。`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -124,7 +123,7 @@ export class Releases {
|
||||
const env = {
|
||||
...record(actor.tenantId),
|
||||
...data,
|
||||
applicationUrl: applicationUrlFor(data, this.env, await gatewayIdentityOrigins(this.env)),
|
||||
applicationUrl: applicationUrlFor(data, this.env),
|
||||
} as Environment;
|
||||
await tx.put('environment', env);
|
||||
return env;
|
||||
@ -144,7 +143,6 @@ export class Releases {
|
||||
environment.applicationUrl = applicationUrlFor(
|
||||
{ ...environment, applicationUrl: input.applicationUrl ?? undefined },
|
||||
this.env,
|
||||
await gatewayIdentityOrigins(this.env),
|
||||
);
|
||||
await tx.put('environment', touch(environment));
|
||||
return environment;
|
||||
@ -298,7 +296,7 @@ export class Releases {
|
||||
if (env.projectId !== run.projectId)
|
||||
throw new DomainError('ENVIRONMENT_MISMATCH', 'Environment belongs to another project');
|
||||
requireDeploymentSecretPolicy(env);
|
||||
applicationUrlFor(env, this.env, await gatewayIdentityOrigins(this.env));
|
||||
applicationUrlFor(env, this.env);
|
||||
assertApplicationConfigured(
|
||||
{ applicationUrl: env.applicationUrl, deliveryRequired: isProduction(this.env) },
|
||||
this.env,
|
||||
@ -381,9 +379,7 @@ export class Releases {
|
||||
await tx.event(
|
||||
run.id,
|
||||
'release.created',
|
||||
riskReasons.length
|
||||
? 'Release requires approval of the exact artifact and environment.'
|
||||
: 'Release passed the configured automatic policy.',
|
||||
riskReasons.length ? '发布需要针对这个制品和环境的审批。' : '发布已通过配置的自动策略。',
|
||||
{ releaseId: release.id, bindingDigest: release.bindingDigest },
|
||||
);
|
||||
return release;
|
||||
@ -397,8 +393,8 @@ export class Releases {
|
||||
project = await must<Project>(tx, 'project', release.projectId);
|
||||
assertVerificationProfile(project, this.env);
|
||||
if (environment.applicationUrl !== release.applicationUrl)
|
||||
throw new DomainError('RELEASE_STALE', 'Application URL changed; request a new release');
|
||||
applicationUrlFor(environment, this.env, await gatewayIdentityOrigins(this.env));
|
||||
throw new DomainError('RELEASE_STALE', '应用地址已变化;请申请新的发布');
|
||||
applicationUrlFor(environment, this.env);
|
||||
assertApplicationConfigured(release, this.env);
|
||||
if (
|
||||
digest(releaseBinding(release)) !== release.bindingDigest ||
|
||||
@ -415,10 +411,7 @@ export class Releases {
|
||||
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',
|
||||
);
|
||||
throw new DomainError('RELEASE_STALE', '需求、代码、策略、配置、密钥或环境已变化;请申请新的发布');
|
||||
requireDeploymentSecretPolicy(release);
|
||||
await assertPocBuild(project, environment.id, release.pocRelease, this.env);
|
||||
if (requireApproval && release.approvalId) {
|
||||
@ -428,7 +421,7 @@ export class Releases {
|
||||
approval.releaseId !== release.id ||
|
||||
approval.bindingDigest !== release.bindingDigest
|
||||
)
|
||||
throw new DomainError('APPROVAL_STALE', 'Release approval is absent or invalidated');
|
||||
throw new DomainError('APPROVAL_STALE', '发布审批不存在或已失效');
|
||||
}
|
||||
return { run, environment, project };
|
||||
}
|
||||
@ -448,7 +441,7 @@ export class Releases {
|
||||
release.bindingDigest !== approval.bindingDigest ||
|
||||
release.status !== 'awaiting_approval'
|
||||
)
|
||||
throw new DomainError('APPROVAL_STALE', 'Release no longer matches this approval');
|
||||
throw new DomainError('APPROVAL_STALE', '发布已与这次审批不一致');
|
||||
// Four eyes in production: whoever asked for the release (or the wish) cannot approve it.
|
||||
if (input.decision === 'approve' && isProduction(this.env) && approval.requestedBy === actor.userId)
|
||||
throw new DomainError('SELF_APPROVAL_FORBIDDEN', 'Another person has to approve a release you requested', 403);
|
||||
@ -457,15 +450,20 @@ export class Releases {
|
||||
approval.decidedBy = actor.userId;
|
||||
release.status = input.decision === 'approve' ? 'ready' : 'failed';
|
||||
if (input.decision === 'reject') {
|
||||
release.failureReason = `Rejected by ${actor.userId}`;
|
||||
await noteReleaseFailure(tx, release, `Release ${release.id} was rejected.`);
|
||||
release.failureReason = `被 ${actor.userId} 驳回`;
|
||||
await noteReleaseFailure(tx, release, `发布 ${release.id} 已被驳回。`);
|
||||
}
|
||||
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,
|
||||
});
|
||||
await tx.event(
|
||||
release.runId,
|
||||
'release.approval',
|
||||
`已记录发布审批决定:${input.decision === 'approve' ? '批准' : '驳回'}。`,
|
||||
{
|
||||
releaseId: release.id,
|
||||
actorId: actor.userId,
|
||||
},
|
||||
);
|
||||
return release;
|
||||
});
|
||||
}
|
||||
@ -542,13 +540,13 @@ export class Releases {
|
||||
run.stage = 'accept';
|
||||
clearWaiting(run);
|
||||
run.reservedTokens = 0;
|
||||
await captureLesson(tx, run);
|
||||
await learnFromRun(tx, run);
|
||||
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.', {
|
||||
await tx.event(run.id, 'delivery.accepted', '已部署的制品通过了健康检查和业务验收。', {
|
||||
releaseId: id,
|
||||
evidenceId: evidence.id,
|
||||
});
|
||||
@ -589,7 +587,7 @@ export class Releases {
|
||||
await tx.event(
|
||||
original.runId,
|
||||
'rollback.abandoned',
|
||||
`Environment ${environment.name} was unlocked by ${actor.userId} after checking the target; the rollback was abandoned.`,
|
||||
`${actor.userId} 检查目标后解锁了环境 ${environment.name};回滚已放弃。`,
|
||||
{ operationId: rollback.id },
|
||||
);
|
||||
return { environmentId, releaseId, status: release.status };
|
||||
@ -601,14 +599,10 @@ export class Releases {
|
||||
await tx.put('runner_task', { ...touch(task), status: 'canceled' });
|
||||
environment.activeReleaseId = undefined;
|
||||
release.status = 'failed';
|
||||
release.failureReason = `Unlocked by ${actor.userId} after checking the target`;
|
||||
release.failureReason = `${actor.userId} 检查目标后解锁了环境`;
|
||||
await tx.put('environment', touch(environment));
|
||||
await tx.put('release', touch(release));
|
||||
await noteReleaseFailure(
|
||||
tx,
|
||||
release,
|
||||
`Environment ${environment.name} was unlocked; release ${release.id} is not live.`,
|
||||
);
|
||||
await noteReleaseFailure(tx, release, `环境 ${environment.name} 已解锁;发布 ${release.id} 没有上线。`);
|
||||
return { environmentId, releaseId, status: release.status };
|
||||
});
|
||||
}
|
||||
@ -711,12 +705,10 @@ export class Releases {
|
||||
env.activeReleaseId = previous.id;
|
||||
await tx.put('environment', touch(env));
|
||||
await tx.put('runner_task', operation);
|
||||
await tx.event(
|
||||
release.runId,
|
||||
'rollback.requested',
|
||||
'Rollback queued for the authenticated environment runner.',
|
||||
{ operationId: operation.id, targetReleaseId: previous.id },
|
||||
);
|
||||
await tx.event(release.runId, 'rollback.requested', '回滚已排队,等待经过认证的环境执行器处理。', {
|
||||
operationId: operation.id,
|
||||
targetReleaseId: previous.id,
|
||||
});
|
||||
return operation;
|
||||
});
|
||||
});
|
||||
|
||||
@ -75,7 +75,7 @@ export async function releaseStoppedAllocations(
|
||||
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');
|
||||
});
|
||||
@ -139,7 +139,7 @@ export async function purgeRetainedStorage(control: ControlPlane, tenantId: stri
|
||||
if (value?.generation !== allocation.generation || value.status !== 'released') return;
|
||||
latest.resourceAllocation = { ...value, storagePurgedAt: new Date().toISOString() };
|
||||
await tx.put('run', touch(latest));
|
||||
await tx.event(latest.id, 'resources.storage_purged', `Run volumes were removed ${hours} hours after release.`);
|
||||
await tx.event(latest.id, 'resources.storage_purged', `运行卷在释放 ${hours} 小时后已删除。`);
|
||||
});
|
||||
} catch {
|
||||
/* Retried at the next hourly pass. */
|
||||
|
||||
@ -378,7 +378,7 @@ export class RunnerBroker {
|
||||
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;
|
||||
@ -391,16 +391,13 @@ export class RunnerBroker {
|
||||
environment.activeReleaseId = undefined;
|
||||
await tx.put('environment', touch(environment));
|
||||
const original = await must<Release>(tx, 'release', String(operation.releaseId));
|
||||
await tx.event(
|
||||
original.runId,
|
||||
'rollback.failed',
|
||||
`Rollback failed (${reason}); the current version keeps serving.`,
|
||||
{ operationId: operation.id },
|
||||
);
|
||||
await tx.event(original.runId, 'rollback.failed', `回滚失败(${reason});当前版本继续提供服务。`, {
|
||||
operationId: operation.id,
|
||||
});
|
||||
} else {
|
||||
operation.status = 'unknown';
|
||||
const original = await must<Release>(tx, 'release', String(operation.releaseId));
|
||||
await tx.event(original.runId, 'rollback.unknown', 'Rollback requires live-state reconciliation.', {
|
||||
await tx.event(original.runId, 'rollback.unknown', '回滚需要对线上实际状态进行对账。', {
|
||||
operationId: operation.id,
|
||||
});
|
||||
}
|
||||
@ -428,11 +425,7 @@ export class RunnerBroker {
|
||||
environment.activeReleaseId = undefined;
|
||||
await tx.put('release', touch(release));
|
||||
await tx.put('environment', touch(environment));
|
||||
await noteReleaseFailure(
|
||||
tx,
|
||||
release,
|
||||
`Deployment of release ${release.id} failed (${reason}); the previous version keeps serving.`,
|
||||
);
|
||||
await noteReleaseFailure(tx, release, `发布 ${release.id} 部署失败(${reason});上一个版本继续提供服务。`);
|
||||
} else {
|
||||
if (!['deploying', 'unknown', 'failed'].includes(release.status))
|
||||
throw new DomainError('RELEASE_CONFLICT', 'Deployment receipt is obsolete');
|
||||
@ -441,7 +434,7 @@ export class RunnerBroker {
|
||||
await tx.event(
|
||||
release.runId,
|
||||
'release.failure',
|
||||
`Runner reported ${result.status}; live state must be reconciled before another deployment.`,
|
||||
`环境执行器报告 ${result.status};再次部署之前必须先对线上状态进行对账。`,
|
||||
{ releaseId: release.id, taskId },
|
||||
);
|
||||
}
|
||||
@ -475,7 +468,7 @@ export class RunnerBroker {
|
||||
release.status = 'failed';
|
||||
release.failureReason = error.message;
|
||||
await tx.put('release', touch(release));
|
||||
await noteReleaseFailure(tx, release, `Release ${release.id} went stale before deployment: ${error.message}`);
|
||||
await noteReleaseFailure(tx, release, `发布 ${release.id} 在部署之前已过期:${error.message}。`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
282
apps/api/src/domain/skill-files.ts
Normal file
282
apps/api/src/domain/skill-files.ts
Normal file
@ -0,0 +1,282 @@
|
||||
import { crc32, inflateRawSync } from 'node:zlib';
|
||||
import {
|
||||
DomainError,
|
||||
SKILL_CONTENT_LIMIT,
|
||||
skillCategories,
|
||||
skillName,
|
||||
type Skill,
|
||||
type SkillCategory,
|
||||
} from '@wishforge/contracts';
|
||||
|
||||
/**
|
||||
* Skills as files: SKILL.md with its name and purpose up front, one directory per skill in a zip,
|
||||
* as Claude Code's .claude/skills/ expects. Export writes them; import reads them back, from this
|
||||
* platform's exports or from skills written elsewhere.
|
||||
*/
|
||||
|
||||
/** What an import may hold. Larger archives and files are refused rather than read partly. */
|
||||
export const SKILL_IMPORT_LIMITS = {
|
||||
/** A zip as uploaded (before base64). */
|
||||
archiveBytes: 512 * 1024,
|
||||
/** Entries a zip's directory may list, skills or not. */
|
||||
entries: 500,
|
||||
/** Skills one import may add. */
|
||||
skills: 50,
|
||||
/** One SKILL.md, uncompressed. */
|
||||
fileBytes: 64 * 1024,
|
||||
/** Every SKILL.md of an archive together, uncompressed. */
|
||||
totalBytes: 2 * 1024 * 1024,
|
||||
} as const;
|
||||
|
||||
/** One skill read from a file, before it joins a project. */
|
||||
export interface SkillFile {
|
||||
name: string;
|
||||
description: string;
|
||||
content: string;
|
||||
category: SkillCategory;
|
||||
/** Where it came from: the file name or the path inside the archive. */
|
||||
source: string;
|
||||
}
|
||||
|
||||
/** A skill as Claude Code and other agents read it: SKILL.md with its name and purpose up front. */
|
||||
export function skillMarkdown(skill: Pick<Skill, 'name' | 'description' | 'content' | 'category'>) {
|
||||
const description = skill.description.replace(/\s+/g, ' ').trim();
|
||||
// Other tools ignore metadata; an import here restores what kind of skill it was.
|
||||
const metadata =
|
||||
skill.category && skill.category !== 'workflow' ? `metadata:\n wishforge-category: ${skill.category}\n` : '';
|
||||
return `---\nname: ${skill.name}\ndescription: ${JSON.stringify(description)}\n${metadata}---\n\n${skill.content.trim()}\n`;
|
||||
}
|
||||
|
||||
const invalid = (source: string, reason: string) => new DomainError('INVALID_SKILL_FILE', `${source}: ${reason}`, 400);
|
||||
|
||||
/** A YAML scalar as SKILL.md front matter writes it: plain, single- or double-quoted. */
|
||||
function scalar(raw: string, source: string): string {
|
||||
const value = raw.trim();
|
||||
if (value.startsWith('"')) {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(value);
|
||||
if (typeof parsed === 'string') return parsed;
|
||||
} catch {}
|
||||
throw invalid(source, 'a quoted value in the front matter is not closed');
|
||||
}
|
||||
if (value.startsWith("'")) {
|
||||
if (!value.endsWith("'") || value.length < 2)
|
||||
throw invalid(source, 'a quoted value in the front matter is not closed');
|
||||
return value.slice(1, -1).replaceAll("''", "'");
|
||||
}
|
||||
// A comment after a plain value is not part of it.
|
||||
return value.replace(/\s+#.*$/, '');
|
||||
}
|
||||
|
||||
/**
|
||||
* The front matter keys SKILL.md uses: top-level scalars, block scalars (`|`, `>`) and one level
|
||||
* of nesting (`metadata:`). Anything richer is ignored rather than misread.
|
||||
*/
|
||||
function frontMatter(lines: string[], source: string) {
|
||||
const fields: Record<string, string> = {};
|
||||
const nested: Record<string, string> = {};
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i]!;
|
||||
if (!line.trim() || line.trimStart().startsWith('#')) continue;
|
||||
const match = /^([A-Za-z0-9_-]+):(.*)$/.exec(line);
|
||||
if (!match) continue;
|
||||
const [, key, rest] = match as unknown as [string, string, string];
|
||||
const indented: string[] = [];
|
||||
while (i + 1 < lines.length && (/^\s+\S/.test(lines[i + 1]!) || !lines[i + 1]!.trim())) indented.push(lines[++i]!);
|
||||
const marker = rest.trim();
|
||||
if (/^[|>][+-]?$/.test(marker)) {
|
||||
const body = indented.map(item => item.trim());
|
||||
fields[key] = (marker.startsWith('>') ? body.join(' ') : body.join('\n')).trim();
|
||||
} else if (marker) {
|
||||
fields[key] = [scalar(marker, source), ...indented.map(item => item.trim())].join(' ').trim();
|
||||
} else
|
||||
for (const item of indented) {
|
||||
const child = /^\s+([A-Za-z0-9_-]+):(.*)$/.exec(item);
|
||||
if (child?.[2]?.trim()) nested[`${key}.${child[1]}`] = scalar(child[2], source);
|
||||
}
|
||||
}
|
||||
return { ...nested, ...fields };
|
||||
}
|
||||
|
||||
/** Lowercase words joined by hyphens, from whatever a file or directory was called. */
|
||||
export function skillSlug(value: string) {
|
||||
return value
|
||||
.normalize('NFKD')
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 64)
|
||||
.replace(/-+$/, '');
|
||||
}
|
||||
|
||||
/** The skill a path names when its front matter does not: `name/SKILL.md`, `name.SKILL.md` or `name.md`. */
|
||||
function nameFromPath(source: string) {
|
||||
const parts = source.split('/').filter(Boolean);
|
||||
const file = parts.at(-1) ?? '';
|
||||
if (/^skill\.md$/i.test(file)) return parts.at(-2) ?? '';
|
||||
return file.replace(/(\.skill)?\.md$/i, '');
|
||||
}
|
||||
|
||||
/** Reads one SKILL.md; `source` names it in errors and supplies the name when the file has none. */
|
||||
export function parseSkillMarkdown(text: string, source: string): SkillFile {
|
||||
const normalized = text.replace(/^\uFEFF/, '').replace(/\r\n?/g, '\n');
|
||||
if (Buffer.byteLength(normalized, 'utf8') > SKILL_IMPORT_LIMITS.fileBytes)
|
||||
throw invalid(source, `larger than ${SKILL_IMPORT_LIMITS.fileBytes / 1024} KiB`);
|
||||
let fields: Record<string, string> = {};
|
||||
let body = normalized;
|
||||
if (normalized.startsWith('---\n')) {
|
||||
const end = normalized.indexOf('\n---', 4);
|
||||
if (end === -1) throw invalid(source, 'the front matter is not closed with ---');
|
||||
fields = frontMatter(normalized.slice(4, end).split('\n'), source);
|
||||
body = normalized.slice(end + 4).replace(/^[^\n]*\n?/, '');
|
||||
}
|
||||
const content = body.trim();
|
||||
if (!content) throw invalid(source, 'the skill has no instructions after its front matter');
|
||||
if (content.length > SKILL_CONTENT_LIMIT)
|
||||
throw invalid(source, `the instructions are longer than ${SKILL_CONTENT_LIMIT} characters`);
|
||||
const rawName = fields.name?.trim() || nameFromPath(source);
|
||||
const name = skillName.safeParse(rawName).success ? rawName : skillSlug(rawName);
|
||||
if (!name) throw invalid(source, 'the skill has no usable name');
|
||||
// Skills written elsewhere sometimes leave the description out; their first line says what they are for.
|
||||
const description = (fields.description || content.split('\n').find(line => line.trim()) || '')
|
||||
.replace(/^#+\s*/, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
.slice(0, 500);
|
||||
if (!description) throw invalid(source, 'the skill has no description');
|
||||
const category = fields['metadata.wishforge-category'] ?? fields.category;
|
||||
return {
|
||||
name,
|
||||
description,
|
||||
content,
|
||||
category: (skillCategories as readonly string[]).includes(category ?? '')
|
||||
? (category as SkillCategory)
|
||||
: 'workflow',
|
||||
source,
|
||||
};
|
||||
}
|
||||
|
||||
/** A zip archive of text files, stored without compression: the format every system opens. */
|
||||
export function zipFiles(files: { path: string; content: string }[], at = new Date()): Buffer {
|
||||
const time = ((at.getHours() << 11) | (at.getMinutes() << 5) | (at.getSeconds() >> 1)) & 0xffff;
|
||||
const date = (((at.getFullYear() - 1980) << 9) | ((at.getMonth() + 1) << 5) | at.getDate()) & 0xffff;
|
||||
const locals: Buffer[] = [];
|
||||
const centrals: Buffer[] = [];
|
||||
let offset = 0;
|
||||
for (const file of files) {
|
||||
const name = Buffer.from(file.path, 'utf8');
|
||||
const data = Buffer.from(file.content, 'utf8');
|
||||
const checksum = crc32(data);
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt16LE(0x0800, 6); // UTF-8 names
|
||||
local.writeUInt16LE(0, 8);
|
||||
local.writeUInt16LE(time, 10);
|
||||
local.writeUInt16LE(date, 12);
|
||||
local.writeUInt32LE(checksum, 14);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(name.length, 26);
|
||||
local.writeUInt16LE(0, 28);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt16LE(0x0800, 8);
|
||||
central.writeUInt16LE(0, 10);
|
||||
central.writeUInt16LE(time, 12);
|
||||
central.writeUInt16LE(date, 14);
|
||||
central.writeUInt32LE(checksum, 16);
|
||||
central.writeUInt32LE(data.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(name.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, name, data);
|
||||
centrals.push(central, name);
|
||||
offset += local.length + name.length + data.length;
|
||||
}
|
||||
const directory = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(files.length, 8);
|
||||
end.writeUInt16LE(files.length, 10);
|
||||
end.writeUInt32LE(directory.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, directory, end]);
|
||||
}
|
||||
|
||||
const isSkillFile = (path: string) => /(^|\/)skill\.md$/i.test(path) || /\.skill\.md$/i.test(path);
|
||||
|
||||
/**
|
||||
* The SKILL.md files of a zip, stored or deflated, as this platform and common tools write them.
|
||||
* Other files (scripts, references) are counted, not read: a skill here is its instructions.
|
||||
* Encrypted, multi-disk and ZIP64 archives, and anything beyond the limits, are refused.
|
||||
*/
|
||||
export function readSkillArchive(archive: Buffer): { files: { path: string; content: string }[]; ignored: number } {
|
||||
const bad = (reason: string) => new DomainError('INVALID_SKILL_ARCHIVE', `The zip archive ${reason}`, 400);
|
||||
if (archive.length > SKILL_IMPORT_LIMITS.archiveBytes)
|
||||
throw bad(`is larger than ${SKILL_IMPORT_LIMITS.archiveBytes / 1024} KiB`);
|
||||
let end = -1;
|
||||
for (let i = archive.length - 22; i >= Math.max(0, archive.length - 22 - 0xffff); i--)
|
||||
if (archive.readUInt32LE(i) === 0x06054b50) {
|
||||
end = i;
|
||||
break;
|
||||
}
|
||||
if (end === -1) throw bad('cannot be read: it is not a zip file');
|
||||
const count = archive.readUInt16LE(end + 10);
|
||||
const size = archive.readUInt32LE(end + 12);
|
||||
const start = archive.readUInt32LE(end + 16);
|
||||
if (archive.readUInt16LE(end + 4) !== 0 || count === 0xffff || start === 0xffffffff)
|
||||
throw bad('uses multiple disks or ZIP64, which is not supported');
|
||||
if (count > SKILL_IMPORT_LIMITS.entries) throw bad(`has more than ${SKILL_IMPORT_LIMITS.entries} entries`);
|
||||
if (start + size > end) throw bad('is damaged');
|
||||
const files: { path: string; content: string }[] = [];
|
||||
let ignored = 0;
|
||||
let total = 0;
|
||||
let at = start;
|
||||
for (let n = 0; n < count; n++) {
|
||||
if (at + 46 > end || archive.readUInt32LE(at) !== 0x02014b50) throw bad('is damaged');
|
||||
const flags = archive.readUInt16LE(at + 8);
|
||||
const method = archive.readUInt16LE(at + 10);
|
||||
const checksum = archive.readUInt32LE(at + 16);
|
||||
const compressed = archive.readUInt32LE(at + 20);
|
||||
const uncompressed = archive.readUInt32LE(at + 24);
|
||||
const nameLength = archive.readUInt16LE(at + 28);
|
||||
const extraLength = archive.readUInt16LE(at + 30);
|
||||
const commentLength = archive.readUInt16LE(at + 32);
|
||||
const local = archive.readUInt32LE(at + 42);
|
||||
const path = archive.subarray(at + 46, at + 46 + nameLength).toString('utf8');
|
||||
at += 46 + nameLength + extraLength + commentLength;
|
||||
if (path.endsWith('/')) continue;
|
||||
// macOS adds resource forks under __MACOSX/; they are not skills.
|
||||
if (!isSkillFile(path) || path.startsWith('__MACOSX/')) {
|
||||
ignored++;
|
||||
continue;
|
||||
}
|
||||
if (flags & 1) throw bad(`is encrypted (${path})`);
|
||||
if (uncompressed > SKILL_IMPORT_LIMITS.fileBytes)
|
||||
throw bad(`holds ${path}, larger than ${SKILL_IMPORT_LIMITS.fileBytes / 1024} KiB`);
|
||||
total += uncompressed;
|
||||
if (total > SKILL_IMPORT_LIMITS.totalBytes)
|
||||
throw bad(`holds more than ${SKILL_IMPORT_LIMITS.totalBytes / 1024 / 1024} MiB of skills`);
|
||||
if (local + 30 > end || archive.readUInt32LE(local) !== 0x04034b50) throw bad('is damaged');
|
||||
const dataStart = local + 30 + archive.readUInt16LE(local + 26) + archive.readUInt16LE(local + 28);
|
||||
if (dataStart + compressed > end) throw bad('is damaged');
|
||||
const raw = archive.subarray(dataStart, dataStart + compressed);
|
||||
let data: Buffer;
|
||||
if (method === 0) data = raw;
|
||||
else if (method === 8)
|
||||
try {
|
||||
data = inflateRawSync(raw, { maxOutputLength: SKILL_IMPORT_LIMITS.fileBytes });
|
||||
} catch {
|
||||
throw bad(`holds ${path}, which cannot be decompressed`);
|
||||
}
|
||||
else throw bad(`compresses ${path} in a way that is not supported`);
|
||||
if (data.length !== uncompressed || crc32(data) !== checksum) throw bad(`holds ${path}, which is damaged`);
|
||||
files.push({ path, content: data.toString('utf8') });
|
||||
if (files.length > SKILL_IMPORT_LIMITS.skills)
|
||||
throw bad(`holds more than ${SKILL_IMPORT_LIMITS.skills} skills; import them in parts`);
|
||||
}
|
||||
return { files, ignored };
|
||||
}
|
||||
@ -1,103 +1,273 @@
|
||||
import { crc32 } from 'node:zlib';
|
||||
import { z } from 'zod';
|
||||
import { askClaude } from '@wishforge/runtime';
|
||||
import { SKILL_LIMIT, type PromptSkill } from '@wishforge/agent';
|
||||
import { repairLesson, selectSkills, type PromptSkill } from '@wishforge/agent';
|
||||
import {
|
||||
DomainError,
|
||||
SKILL_CONTENT_LIMIT,
|
||||
skillCategories,
|
||||
skillInput,
|
||||
skillName,
|
||||
skillUpdateInput,
|
||||
type Actor,
|
||||
type CodeReview,
|
||||
type FailureSummary,
|
||||
type Project,
|
||||
type Run,
|
||||
type Skill,
|
||||
type SkillHarvest,
|
||||
type SkillUsage,
|
||||
} from '@wishforge/contracts';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { must, projectAccess, record, touch } from './control-plane.js';
|
||||
import { idempotent, must, now, projectAccess, record, terminal, touch } from './control-plane.js';
|
||||
import { nativeClaudeEnabled } from './local-execution.js';
|
||||
import { wishScopeOf } from './permissions.js';
|
||||
import { redact, requireHuman } from './policy.js';
|
||||
import {
|
||||
SKILL_IMPORT_LIMITS,
|
||||
parseSkillMarkdown,
|
||||
readSkillArchive,
|
||||
skillMarkdown,
|
||||
skillSlug,
|
||||
zipFiles,
|
||||
type SkillFile,
|
||||
} from './skill-files.js';
|
||||
import { dispatchTenants } from '../tenants.js';
|
||||
|
||||
export { skillMarkdown, zipFiles } from './skill-files.js';
|
||||
|
||||
const recentlyChanged = (a: Skill, b: Skill) => b.updatedAt.localeCompare(a.updatedAt);
|
||||
|
||||
/** Active skills of a project, most recently changed first, as they enter prompts. */
|
||||
/** A pitfall records at most this many repaired failures, most recent first. */
|
||||
const PITFALL_ENTRIES = 8;
|
||||
/** A skill remembers at most this many runs it was learned from. */
|
||||
const SOURCE_RUNS = 20;
|
||||
/** A platform-maintained skill is retired after this many settled runs in a row that used it failed. */
|
||||
export const RETIRE_AFTER_FAILURES = 3;
|
||||
/** A platform-maintained skill no run was given for this long, while the project kept running, is retired. */
|
||||
export const RETIRE_UNUSED_DAYS = 90;
|
||||
|
||||
/** Whether the project lets the platform activate corroborated skills itself (absent: yes). */
|
||||
export const autoActivation = (project: Pick<Project, 'skillAutoActivation'>) => project.skillAutoActivation !== false;
|
||||
/** Learned by the platform and not since written by a person: the platform may rewrite or retire it. */
|
||||
export const platformMaintained = (skill: Skill) => skill.createdBy === 'wishforge' && !skill.updatedBy;
|
||||
/** A change to a skill's bookkeeping only: it keeps its place among the most recently changed. */
|
||||
const counted = (skill: Skill, changes: Partial<Skill>): Skill => ({
|
||||
...skill,
|
||||
...changes,
|
||||
version: skill.version + 1,
|
||||
});
|
||||
const lastRuns = (ids: string[], runId: string) => [...ids.filter(id => id !== runId), runId].slice(-SOURCE_RUNS);
|
||||
|
||||
/** Active skills of a project as they enter prompts: most recently changed first, within the limits. */
|
||||
async function promptSkillsOf(tx: Transaction, projectId: string): Promise<Skill[]> {
|
||||
return selectSkills((await tx.list<Skill>('skill', { projectId, status: 'active' })).sort(recentlyChanged));
|
||||
}
|
||||
export async function projectSkills(tx: Transaction, projectId: string): Promise<PromptSkill[]> {
|
||||
return (await tx.list<Skill>('skill', { projectId, status: 'active' }))
|
||||
.sort(recentlyChanged)
|
||||
.slice(0, SKILL_LIMIT)
|
||||
.map(({ name, description, content }) => ({ name, description, content }));
|
||||
return (await promptSkillsOf(tx, projectId)).map(({ name, description, content }) => ({
|
||||
name,
|
||||
description,
|
||||
content,
|
||||
}));
|
||||
}
|
||||
|
||||
/** A skill as Claude Code and other agents read it: SKILL.md with its name and purpose up front. */
|
||||
export function skillMarkdown(skill: Pick<Skill, 'name' | 'description' | 'content'>) {
|
||||
const description = skill.description.replace(/\s+/g, ' ').trim();
|
||||
return `---\nname: ${skill.name}\ndescription: ${JSON.stringify(description)}\n---\n\n${skill.content.trim()}\n`;
|
||||
/**
|
||||
* Notes which skills a run's implementation is given, once per run and skill: called when an
|
||||
* executor takes the run at its implementation stage, just before it reads them.
|
||||
*/
|
||||
export async function recordSkillUse(tx: Transaction, run: Run) {
|
||||
const skills = await promptSkillsOf(tx, run.projectId);
|
||||
const usage = await tx.get<SkillUsage>('skill_usage', run.id);
|
||||
const known = new Set(usage?.skillIds ?? []);
|
||||
const fresh = skills.filter(skill => !known.has(skill.id));
|
||||
if (!fresh.length) return;
|
||||
const time = now();
|
||||
for (const skill of fresh)
|
||||
await tx.put('skill', counted(skill, { useCount: (skill.useCount ?? 0) + 1, lastUsedAt: time }));
|
||||
const next: SkillUsage = {
|
||||
...(usage ? touch(usage) : { ...record(run.tenantId), id: run.id }),
|
||||
runId: run.id,
|
||||
projectId: run.projectId,
|
||||
skillIds: [...known, ...fresh.map(skill => skill.id)],
|
||||
};
|
||||
await tx.put('skill_usage', next);
|
||||
}
|
||||
|
||||
/** A zip archive of text files, stored without compression: the format every system opens. */
|
||||
export function zipFiles(files: { path: string; content: string }[], at = new Date()): Buffer {
|
||||
const time = ((at.getHours() << 11) | (at.getMinutes() << 5) | (at.getSeconds() >> 1)) & 0xffff;
|
||||
const date = (((at.getFullYear() - 1980) << 9) | ((at.getMonth() + 1) << 5) | at.getDate()) & 0xffff;
|
||||
const locals: Buffer[] = [];
|
||||
const centrals: Buffer[] = [];
|
||||
let offset = 0;
|
||||
for (const file of files) {
|
||||
const name = Buffer.from(file.path, 'utf8');
|
||||
const data = Buffer.from(file.content, 'utf8');
|
||||
const checksum = crc32(data);
|
||||
const local = Buffer.alloc(30);
|
||||
local.writeUInt32LE(0x04034b50, 0);
|
||||
local.writeUInt16LE(20, 4);
|
||||
local.writeUInt16LE(0x0800, 6); // UTF-8 names
|
||||
local.writeUInt16LE(0, 8);
|
||||
local.writeUInt16LE(time, 10);
|
||||
local.writeUInt16LE(date, 12);
|
||||
local.writeUInt32LE(checksum, 14);
|
||||
local.writeUInt32LE(data.length, 18);
|
||||
local.writeUInt32LE(data.length, 22);
|
||||
local.writeUInt16LE(name.length, 26);
|
||||
local.writeUInt16LE(0, 28);
|
||||
const central = Buffer.alloc(46);
|
||||
central.writeUInt32LE(0x02014b50, 0);
|
||||
central.writeUInt16LE(20, 4);
|
||||
central.writeUInt16LE(20, 6);
|
||||
central.writeUInt16LE(0x0800, 8);
|
||||
central.writeUInt16LE(0, 10);
|
||||
central.writeUInt16LE(time, 12);
|
||||
central.writeUInt16LE(date, 14);
|
||||
central.writeUInt32LE(checksum, 16);
|
||||
central.writeUInt32LE(data.length, 20);
|
||||
central.writeUInt32LE(data.length, 24);
|
||||
central.writeUInt16LE(name.length, 28);
|
||||
central.writeUInt32LE(offset, 42);
|
||||
locals.push(local, name, data);
|
||||
centrals.push(central, name);
|
||||
offset += local.length + name.length + data.length;
|
||||
/**
|
||||
* Counts how a finished run that was given skills ended. A success resets each skill's failure
|
||||
* streak; a failure (or a cancel after a failed check) extends it, and a platform-maintained skill
|
||||
* whose streak reaches RETIRE_AFTER_FAILURES is disabled. A cancel without a failure counts for
|
||||
* neither: the person changed their mind, not the skill.
|
||||
*/
|
||||
export async function settleSkillUsage(tx: Transaction, run: Run) {
|
||||
const usage = await tx.get<SkillUsage>('skill_usage', run.id);
|
||||
if (!usage || usage.outcome || !terminal(run)) return;
|
||||
const outcome: NonNullable<SkillUsage['outcome']> =
|
||||
run.status === 'succeeded'
|
||||
? 'succeeded'
|
||||
: run.status === 'failed' || run.repairCount > 0 || run.lastFailure
|
||||
? 'failed'
|
||||
: 'neutral';
|
||||
await tx.put('skill_usage', { ...touch(usage), outcome });
|
||||
if (outcome === 'neutral') return;
|
||||
const retired: Skill[] = [];
|
||||
for (const id of usage.skillIds) {
|
||||
const skill = await tx.get<Skill>('skill', id);
|
||||
if (!skill) continue;
|
||||
let next =
|
||||
outcome === 'succeeded'
|
||||
? counted(skill, { successCount: (skill.successCount ?? 0) + 1, failureStreak: 0 })
|
||||
: counted(skill, {
|
||||
failureCount: (skill.failureCount ?? 0) + 1,
|
||||
failureStreak: (skill.failureStreak ?? 0) + 1,
|
||||
});
|
||||
if (
|
||||
outcome === 'failed' &&
|
||||
next.status === 'active' &&
|
||||
platformMaintained(next) &&
|
||||
(next.failureStreak ?? 0) >= RETIRE_AFTER_FAILURES
|
||||
) {
|
||||
next = { ...touch(next), status: 'disabled', retiredAt: now(), retiredReason: 'failing' };
|
||||
retired.push(next);
|
||||
}
|
||||
await tx.put('skill', next);
|
||||
}
|
||||
const directory = Buffer.concat(centrals);
|
||||
const end = Buffer.alloc(22);
|
||||
end.writeUInt32LE(0x06054b50, 0);
|
||||
end.writeUInt16LE(files.length, 8);
|
||||
end.writeUInt16LE(files.length, 10);
|
||||
end.writeUInt32LE(directory.length, 12);
|
||||
end.writeUInt32LE(offset, 16);
|
||||
return Buffer.concat([...locals, directory, end]);
|
||||
if (retired.length)
|
||||
await tx.event(
|
||||
run.id,
|
||||
'skill.retired',
|
||||
`已停用 ${retired.map(skill => skill.name).join(', ')}:最近 ${RETIRE_AFTER_FAILURES} 次用到它的运行都失败了。`,
|
||||
{ skillIds: retired.map(skill => skill.id), reason: 'failing' },
|
||||
);
|
||||
}
|
||||
|
||||
export const HARVEST_RULES = `你是一名资深工程师,负责为一个软件项目沉淀"技能"(skill):以后在这个项目里做类似工作时,编码 Agent 可以直接照做的、可复用的做法。你会拿到一次已完成的工作记录(需求、验收标准、实现总结、改动的文件、遇到的失败和审核意见)以及项目里已有的技能。
|
||||
/**
|
||||
* What identifies a failure across runs: the failing command, or the conflicting or out-of-scope
|
||||
* files. The same failure in a later run updates the same pitfall instead of adding another.
|
||||
*/
|
||||
export function failureSignature(failure: FailureSummary): string {
|
||||
const words = (text: string) => text.toLowerCase().split(/\s+/).filter(Boolean);
|
||||
if (failure.kind === 'integration_conflict' || failure.kind === 'scope')
|
||||
return `${failure.kind}:${[...new Set(words(failure.outputTail))].sort().join(' ')}`.slice(0, 500);
|
||||
return `${failure.kind}:${words((failure.argv ?? []).join(' ')).join(' ')}`.slice(0, 500);
|
||||
}
|
||||
|
||||
const PITFALL_HEADER = '这个项目之前的运行在这里失败过,修复轮修好了它。最近的在前:';
|
||||
|
||||
/** The name and purpose of a pitfall about a failure. */
|
||||
function pitfallOf(failure: FailureSummary): { name: string; description: string } {
|
||||
const command = `\`${failure.argv?.join(' ') ?? 'verification'}\``;
|
||||
const files = failure.outputTail.split(/\s+/).filter(Boolean);
|
||||
const subject =
|
||||
failure.kind === 'integration_conflict'
|
||||
? `merge-conflict ${files[0] ?? ''}`
|
||||
: failure.kind === 'scope'
|
||||
? 'frontend-scope'
|
||||
: // Executables by name, not by where they are installed.
|
||||
`${failure.kind === 'install' ? 'install ' : ''}${failure.argv?.map(word => word.split('/').pop()).join(' ') ?? 'verification'}`;
|
||||
const description =
|
||||
failure.kind === 'integration_conflict'
|
||||
? `改动 ${files.slice(0, 5).join(', ')} 时:之前合入目标分支在这里冲突过,以及当时如何解决。`
|
||||
: failure.kind === 'scope'
|
||||
? '只改前端的愿望:之前误改过后端文件,以及如何让改动留在前端。'
|
||||
: failure.kind === 'install'
|
||||
? `改依赖清单之前:之前的运行用 ${command} 安装依赖失败过,以及怎么修好的。`
|
||||
: `完成 ${command} 会检查的改动之前:之前的运行在这里怎么失败的,又是怎么修好的。`;
|
||||
return {
|
||||
name: `pitfall-${skillSlug(subject)}`.slice(0, 64).replace(/-+$/, ''),
|
||||
description: description.slice(0, 500),
|
||||
};
|
||||
}
|
||||
|
||||
/** A pitfall's content with a new entry first: rebuilt when the platform keeps it, else prepended. */
|
||||
function withEntry(skill: Skill | undefined, entry: string) {
|
||||
const line = `- ${entry}`;
|
||||
if (!skill) return `${PITFALL_HEADER}\n\n${line}`;
|
||||
if (!platformMaintained(skill)) return `${line}\n\n${skill.content}`.slice(0, SKILL_CONTENT_LIMIT);
|
||||
const entries = skill.content.split('\n').filter(item => item.startsWith('- '));
|
||||
return `${PITFALL_HEADER}\n\n${[line, ...entries].slice(0, PITFALL_ENTRIES).join('\n')}`.slice(
|
||||
0,
|
||||
SKILL_CONTENT_LIMIT,
|
||||
);
|
||||
}
|
||||
|
||||
/** A name the project does not use yet: the name itself, or with the next free number. */
|
||||
function freeName(name: string, taken: Set<string>) {
|
||||
if (!taken.has(name)) return name;
|
||||
for (let n = 2; ; n++) {
|
||||
const suffix = `-${n}`;
|
||||
const candidate = `${name.slice(0, 64 - suffix.length).replace(/-+$/, '')}${suffix}`;
|
||||
if (!taken.has(candidate)) return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What a run that ended well teaches the project: its skills' counts include it, and when a
|
||||
* repair round fixed a failure on the way, that failure becomes (or updates) a pitfall skill.
|
||||
* The pitfall is active right away where the project lets the platform activate skills, since
|
||||
* it is drawn from a verified failure and a verified fix; otherwise it waits for a person.
|
||||
*/
|
||||
export async function learnFromRun(tx: Transaction, run: Run) {
|
||||
await settleSkillUsage(tx, run);
|
||||
if (!run.repairCount || !run.lastFailure) return;
|
||||
const project = await must<Project>(tx, 'project', run.projectId);
|
||||
const skills = await tx.list<Skill>('skill', { projectId: run.projectId });
|
||||
const signature = failureSignature(run.lastFailure);
|
||||
const entry = redact(repairLesson(run.lastFailure, run.implementationSummary));
|
||||
const existing = skills.find(skill => skill.category === 'pitfall' && skill.signature === signature);
|
||||
if (existing?.sourceRunIds.includes(run.id)) return;
|
||||
const auto = autoActivation(project);
|
||||
if (existing) {
|
||||
const activate = existing.status === 'proposed' && auto;
|
||||
const next: Skill = {
|
||||
...touch(existing),
|
||||
content: withEntry(existing, entry),
|
||||
sourceRunIds: lastRuns(existing.sourceRunIds, run.id),
|
||||
...(activate ? { status: 'active' as const } : {}),
|
||||
};
|
||||
await tx.put('skill', next);
|
||||
await tx.event(
|
||||
run.id,
|
||||
activate ? 'skill.activated' : 'skill.updated',
|
||||
`已用这次修复好的失败${activate ? '启用' : '更新'}踩坑技能 ${next.name}${next.status === 'disabled' ? ',它仍保持停用' : ''}。`,
|
||||
{ skillId: next.id },
|
||||
);
|
||||
return;
|
||||
}
|
||||
const pitfall = pitfallOf(run.lastFailure);
|
||||
const skill: Skill = {
|
||||
...record(run.tenantId),
|
||||
projectId: run.projectId,
|
||||
name: freeName(pitfall.name, new Set(skills.map(item => item.name))),
|
||||
description: redact(pitfall.description),
|
||||
content: withEntry(undefined, entry),
|
||||
category: 'pitfall',
|
||||
signature,
|
||||
status: auto ? 'active' : 'proposed',
|
||||
sourceRunIds: [run.id],
|
||||
createdBy: 'wishforge',
|
||||
};
|
||||
await tx.put('skill', skill);
|
||||
await tx.event(
|
||||
run.id,
|
||||
auto ? 'skill.activated' : 'skill.proposed',
|
||||
`已把修复好的失败记为踩坑技能 ${skill.name}${auto ? ',之后的运行都会知道' : ',等待确认'}。`,
|
||||
{ skillId: skill.id, skillIds: [skill.id] },
|
||||
);
|
||||
}
|
||||
|
||||
export const HARVEST_RULES = `你是一名资深工程师,负责为一个软件项目沉淀"技能"(skill):以后在这个项目里做类似工作时,编码 Agent 可以直接照做的、可复用的做法。你会拿到一次已完成的工作记录(需求、验收标准、实现总结、改动的文件、遇到的失败和审核意见)以及项目里已有的技能(category 为 workflow 是做法,pitfall 是踩过的坑和修法,convention 是项目约定;status 为 disabled 的是被停用的)。
|
||||
1. 只提炼以后还会用到的、对这个项目具体有用的做法:例如这个项目里某类改动该改哪些地方、按什么步骤、要注意什么坑、怎么验证。不要把这一次的具体改动本身写成技能。
|
||||
2. 通用常识(写测试、读代码、遵守代码风格之类)不算技能。已有技能已经覆盖的不要重复。没有值得沉淀的就返回空数组,这完全正常。
|
||||
3. 最多返回 2 个。name 用小写英文单词和连字符,例如 lazy-load-page-css;description 用一两句话写"什么时候用它";content 用 Markdown 写做法:适用场景、步骤、注意事项、如何验证,具体到这个项目的目录、文件和命令,不超过 3000 字。
|
||||
4. 只写工作记录里有依据的内容,不要编造文件、命令或结论。
|
||||
5. 用和需求相同的语言写 description 和 content。`;
|
||||
2. 通用常识(写测试、读代码、遵守代码风格之类)不算技能。已有技能已经覆盖的不要重复,被停用的也不要再提。没有值得沉淀的就返回空数组,这完全正常。
|
||||
3. 这次的工作让某个已有技能需要补充或修正时,不要新建:把 updates 设为那个技能的 name,name 也用它,content 写合并后的完整做法(保留原有仍然成立的内容)。全新的技能不要填 updates。
|
||||
4. 最多返回 2 个。name 用小写英文单词和连字符,例如 lazy-load-page-css;description 用一两句话写"什么时候用它";content 用 Markdown 写做法:适用场景、步骤、注意事项、如何验证,具体到这个项目的目录、文件和命令,不超过 3000 字。
|
||||
5. 只写工作记录里有依据的内容,不要编造文件、命令或结论。
|
||||
6. 用和需求相同的语言写 description 和 content。`;
|
||||
const proposal = z
|
||||
.object({
|
||||
name: skillName,
|
||||
description: z.string().trim().min(1).max(500),
|
||||
content: z.string().trim().min(1).max(8000),
|
||||
updates: z.string().trim().max(64).optional(),
|
||||
})
|
||||
.strip();
|
||||
const harvestSchema = {
|
||||
@ -113,6 +283,7 @@ const harvestSchema = {
|
||||
name: { type: 'string', description: '小写英文单词和连字符' },
|
||||
description: { type: 'string', description: '什么时候用它,一两句话' },
|
||||
content: { type: 'string', description: 'Markdown 写的做法:适用场景、步骤、注意事项、如何验证' },
|
||||
updates: { type: 'string', description: '补充已有技能时填它的 name;新技能不填' },
|
||||
},
|
||||
required: ['name', 'description', 'content'],
|
||||
additionalProperties: false,
|
||||
@ -127,20 +298,58 @@ function changedFiles(events: { type: string; message: string }[]) {
|
||||
const files = new Set<string>();
|
||||
for (const event of events)
|
||||
if (event.type === 'provider.file_change') {
|
||||
const match = /^Changed (.+)$/.exec(event.message);
|
||||
const match = /^(?:Changed |改动了:)(.+)$/.exec(event.message);
|
||||
const file = match?.[1]?.replace(/^.*\/executor\/[^/]+\/[^/]+-a\d+\//, '');
|
||||
if (file) files.add(file);
|
||||
}
|
||||
return [...files].slice(0, 100);
|
||||
}
|
||||
|
||||
const listQuery = z
|
||||
.object({
|
||||
projectId: z.string().min(1).max(128).optional(),
|
||||
status: z.enum(['proposed', 'active', 'disabled']).optional(),
|
||||
category: z.enum(skillCategories).optional(),
|
||||
q: z.string().trim().max(200).optional(),
|
||||
})
|
||||
.strict();
|
||||
const copyInput = z
|
||||
.object({ skillIds: z.array(z.string().min(1).max(128)).min(1).max(100), projectId: z.string().min(1).max(128) })
|
||||
.strict();
|
||||
const importInput = z
|
||||
.object({
|
||||
/** SKILL.md files as text, each with its file name. */
|
||||
files: z
|
||||
.array(z.object({ path: z.string().min(1).max(300), content: z.string().max(SKILL_IMPORT_LIMITS.fileBytes) }))
|
||||
.min(1)
|
||||
.max(SKILL_IMPORT_LIMITS.skills)
|
||||
.optional(),
|
||||
/** A zip, base64-encoded, as the project export writes it. */
|
||||
archive: z
|
||||
.string()
|
||||
.max(Math.ceil((SKILL_IMPORT_LIMITS.archiveBytes * 4) / 3) + 4)
|
||||
.optional(),
|
||||
/** Imported skills are active unless they should wait for a confirmation first. */
|
||||
status: z.enum(['active', 'proposed']).default('active'),
|
||||
})
|
||||
.strict()
|
||||
.refine(value => Boolean(value.files) !== Boolean(value.archive), 'Provide either SKILL.md files or a zip archive');
|
||||
/** What adding skills to a project did: added (perhaps under another name), or left out and why. */
|
||||
export interface SkillTransfer {
|
||||
created: Skill[];
|
||||
skipped: { name: string; source: string; reason: 'identical' | 'invalid'; detail?: string }[];
|
||||
/** Files in an archive that were not SKILL.md. */
|
||||
ignored: number;
|
||||
}
|
||||
|
||||
/** At most this many proposals are worked out at once on the deployment's Claude account. */
|
||||
const CONCURRENCY = 1;
|
||||
let running = 0;
|
||||
|
||||
/**
|
||||
* A project's skills: proposed by the platform from finished runs, confirmed, edited, disabled,
|
||||
* deleted and exported by the people who wish for the project.
|
||||
* Project skills, the one place a project's know-how lives: workflows the platform proposes from
|
||||
* finished runs, pitfalls it records from repaired failures, conventions people write. Whoever may
|
||||
* wish for a project confirms, edits, disables, deletes, copies, imports and exports them.
|
||||
*/
|
||||
export class Skills {
|
||||
constructor(
|
||||
@ -154,6 +363,32 @@ export class Skills {
|
||||
return (await tx.list<Skill>('skill', { projectId })).sort(recentlyChanged);
|
||||
});
|
||||
}
|
||||
/** Skills of every project the person may see, filtered as the knowledge base page asks. */
|
||||
async search(actor: Actor, query: unknown) {
|
||||
const filter = listQuery.parse(query);
|
||||
return this.store.read(actor.tenantId, async tx => {
|
||||
if (filter.projectId) await projectAccess(tx, actor, filter.projectId);
|
||||
const visible = new Set(
|
||||
(await tx.list<Project>('project'))
|
||||
.filter(p => ['owner', 'admin'].includes(actor.role) || p.memberIds.includes(actor.userId))
|
||||
.map(p => p.id),
|
||||
);
|
||||
const words = filter.q?.toLowerCase();
|
||||
return (
|
||||
await tx.list<Skill>('skill', {
|
||||
...(filter.projectId ? { projectId: filter.projectId } : {}),
|
||||
...(filter.status ? { status: filter.status } : {}),
|
||||
})
|
||||
)
|
||||
.filter(
|
||||
skill =>
|
||||
visible.has(skill.projectId) &&
|
||||
(!filter.category || (skill.category ?? 'workflow') === filter.category) &&
|
||||
(!words || [skill.name, skill.description, skill.content].some(text => text.toLowerCase().includes(words))),
|
||||
)
|
||||
.sort(recentlyChanged);
|
||||
});
|
||||
}
|
||||
async create(actor: Actor, projectId: string, input: unknown) {
|
||||
const data = skillInput.parse(input);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
@ -166,6 +401,7 @@ export class Skills {
|
||||
name: data.name,
|
||||
description: redact(data.description),
|
||||
content: redact(data.content),
|
||||
category: data.category,
|
||||
status: 'active',
|
||||
sourceRunIds: [],
|
||||
createdBy: actor.userId,
|
||||
@ -174,21 +410,32 @@ export class Skills {
|
||||
return skill;
|
||||
});
|
||||
}
|
||||
/** Edits a skill; confirming a proposal is setting it active. */
|
||||
/**
|
||||
* Edits a skill; confirming a proposal is setting it active. Changing what it says makes it a
|
||||
* person's: the platform no longer rewrites or retires it. Enabling it again starts its failure
|
||||
* streak afresh.
|
||||
*/
|
||||
async update(actor: Actor, id: string, input: unknown) {
|
||||
const data = skillUpdateInput.parse(input);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const skill = await must<Skill>(tx, 'skill', id);
|
||||
this.authorize(actor, await projectAccess(tx, actor, skill.projectId));
|
||||
if (data.name && data.name !== skill.name) await this.uniqueName(tx, skill.projectId, data.name, id);
|
||||
const edited = Boolean(data.name || data.description || data.content || data.category);
|
||||
const next: Skill = {
|
||||
...touch(skill),
|
||||
...(data.name ? { name: data.name } : {}),
|
||||
...(data.description ? { description: redact(data.description) } : {}),
|
||||
...(data.content ? { content: redact(data.content) } : {}),
|
||||
...(data.category ? { category: data.category } : {}),
|
||||
...(data.status ? { status: data.status } : {}),
|
||||
updatedBy: actor.userId,
|
||||
...(edited ? { updatedBy: actor.userId } : {}),
|
||||
};
|
||||
if (data.status === 'active' && skill.status !== 'active') {
|
||||
delete next.retiredAt;
|
||||
delete next.retiredReason;
|
||||
next.failureStreak = 0;
|
||||
}
|
||||
await tx.put('skill', next);
|
||||
return next;
|
||||
});
|
||||
@ -211,7 +458,7 @@ export class Skills {
|
||||
}
|
||||
/**
|
||||
* The project's skills, proposals left out unless asked for, as a zip with a directory per skill
|
||||
* holding its SKILL.md: what .claude/skills/ expects.
|
||||
* holding its SKILL.md: what .claude/skills/ expects, and what import reads back.
|
||||
*/
|
||||
async exportAll(actor: Actor, projectId: string, includeProposed = false) {
|
||||
return this.store.read(actor.tenantId, async tx => {
|
||||
@ -220,17 +467,125 @@ export class Skills {
|
||||
.filter(skill => skill.status === 'active' || (includeProposed && skill.status === 'proposed'))
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
if (!skills.length) throw new DomainError('NO_SKILLS', 'This project has no skills to export', 404);
|
||||
const slug = project.name
|
||||
.normalize('NFKD')
|
||||
.replace(/[^A-Za-z0-9]+/g, '-')
|
||||
.replace(/^-|-$/g, '')
|
||||
.toLowerCase();
|
||||
return {
|
||||
filename: `${slug || 'project'}-skills.zip`,
|
||||
filename: `${skillSlug(project.name) || 'project'}-skills.zip`,
|
||||
content: zipFiles(skills.map(skill => ({ path: `${skill.name}/SKILL.md`, content: skillMarkdown(skill) }))),
|
||||
};
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Adds skills from SKILL.md files or a zip of them (as export writes it) to a project. A skill
|
||||
* the project already has word for word (description and content) is skipped, so importing the
|
||||
* same files twice adds nothing; one whose name a different skill holds is added with the next
|
||||
* free number.
|
||||
*/
|
||||
async importFiles(actor: Actor, projectId: string, input: unknown, key?: string): Promise<SkillTransfer> {
|
||||
const data = importInput.parse(input);
|
||||
let sources: { path: string; content: string }[];
|
||||
let ignored = 0;
|
||||
if (data.archive) {
|
||||
const archive = Buffer.from(data.archive, 'base64');
|
||||
({ files: sources, ignored } = readSkillArchive(archive));
|
||||
} else sources = data.files!;
|
||||
const skipped: SkillTransfer['skipped'] = [];
|
||||
const parsed: SkillFile[] = [];
|
||||
for (const source of sources)
|
||||
try {
|
||||
parsed.push(parseSkillMarkdown(source.content, source.path));
|
||||
} catch (error) {
|
||||
skipped.push({
|
||||
name: source.path,
|
||||
source: source.path,
|
||||
reason: 'invalid',
|
||||
detail: error instanceof Error ? error.message : 'unreadable',
|
||||
});
|
||||
}
|
||||
if (!parsed.length)
|
||||
throw new DomainError(
|
||||
'NO_SKILLS',
|
||||
skipped.length ? `没有能读取的技能:${skipped[0]!.detail}` : '压缩包里没有 SKILL.md 文件',
|
||||
400,
|
||||
);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
this.authorize(actor, await projectAccess(tx, actor, projectId));
|
||||
return idempotent(tx, key && `skills-import:${actor.userId}:${projectId}:${key}`, data, async () => {
|
||||
const result = await this.addSkills(tx, actor, projectId, parsed, () => data.status);
|
||||
return { ...result, skipped: [...skipped, ...result.skipped], ignored };
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Copies skills to another project the person may shape, keeping what they say and whether they
|
||||
* are active, but not their counts or the runs they came from. Collisions as for an import.
|
||||
*/
|
||||
async copy(actor: Actor, input: unknown, key?: string): Promise<SkillTransfer> {
|
||||
const data = copyInput.parse(input);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
this.authorize(actor, await projectAccess(tx, actor, data.projectId));
|
||||
return idempotent(tx, key && `skills-copy:${actor.userId}:${key}`, data, async () => {
|
||||
const originals: Skill[] = [];
|
||||
for (const id of new Set(data.skillIds)) {
|
||||
const skill = await must<Skill>(tx, 'skill', id);
|
||||
await projectAccess(tx, actor, skill.projectId);
|
||||
originals.push(skill);
|
||||
}
|
||||
const files = originals.map(skill => ({
|
||||
name: skill.name,
|
||||
description: skill.description,
|
||||
content: skill.content,
|
||||
category: skill.category ?? 'workflow',
|
||||
source: skill.id,
|
||||
signature: skill.signature,
|
||||
}));
|
||||
const status = new Map(originals.map(skill => [skill.id, skill.status]));
|
||||
return {
|
||||
...(await this.addSkills(tx, actor, data.projectId, files, file => status.get(file.source)!)),
|
||||
ignored: 0,
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
private async addSkills(
|
||||
tx: Transaction,
|
||||
actor: Actor,
|
||||
projectId: string,
|
||||
files: (SkillFile & { signature?: string })[],
|
||||
status: (file: SkillFile) => Skill['status'],
|
||||
): Promise<Omit<SkillTransfer, 'ignored'>> {
|
||||
const existing = await tx.list<Skill>('skill', { projectId });
|
||||
const taken = new Set(existing.map(skill => skill.name));
|
||||
const created: Skill[] = [];
|
||||
const skipped: SkillTransfer['skipped'] = [];
|
||||
for (const file of files) {
|
||||
const description = redact(file.description);
|
||||
const content = redact(file.content);
|
||||
// Saying exactly the same under whatever name: already there.
|
||||
const same = [...existing, ...created].find(
|
||||
skill => skill.description === description && skill.content === content,
|
||||
);
|
||||
if (same) {
|
||||
skipped.push({ name: file.name, source: file.source, reason: 'identical' });
|
||||
continue;
|
||||
}
|
||||
const name = freeName(file.name, taken);
|
||||
taken.add(name);
|
||||
const skill: Skill = {
|
||||
...record(actor.tenantId),
|
||||
projectId,
|
||||
name,
|
||||
description,
|
||||
content,
|
||||
category: file.category,
|
||||
...(file.signature ? { signature: file.signature } : {}),
|
||||
status: status(file),
|
||||
sourceRunIds: [],
|
||||
createdBy: actor.userId,
|
||||
};
|
||||
await tx.put('skill', skill);
|
||||
created.push(skill);
|
||||
}
|
||||
return { created, skipped };
|
||||
}
|
||||
available(tenantId: string) {
|
||||
return nativeClaudeEnabled(this.env, tenantId);
|
||||
}
|
||||
@ -254,8 +609,12 @@ export class Skills {
|
||||
return this.harvest(actor.tenantId, runId);
|
||||
}
|
||||
/**
|
||||
* Looks for skills in one run and stores what it finds as proposals; records that it looked, so
|
||||
* the background harvest does not ask again.
|
||||
* Looks for skills in one run. A proposal that names an existing skill (or is called like one)
|
||||
* updates it instead of adding another: a proposal is rewritten, as is an active skill only the
|
||||
* platform maintains where the project lets it activate skills; a skill a person wrote or
|
||||
* disabled just records the run. A proposal seen in two succeeded runs becomes active where the
|
||||
* project allows it. Records that it looked, so the background harvest does not ask again.
|
||||
* Returns the skills it added or changed.
|
||||
*/
|
||||
async harvest(tenantId: string, runId: string): Promise<Skill[]> {
|
||||
running++;
|
||||
@ -285,9 +644,13 @@ export class Skills {
|
||||
? { kind: run.lastFailure.kind, outputTail: run.lastFailure.outputTail.slice(-2000) }
|
||||
: undefined,
|
||||
reviewComments: reviews.flatMap(review => (review.comment ? [review.comment] : [])).slice(0, 5),
|
||||
existingSkills: existing
|
||||
.filter(skill => skill.status !== 'disabled')
|
||||
.map(skill => ({ name: skill.name, description: skill.description, content: skill.content.slice(0, 1500) })),
|
||||
existingSkills: existing.sort(recentlyChanged).map(skill => ({
|
||||
name: skill.name,
|
||||
category: skill.category ?? 'workflow',
|
||||
status: skill.status,
|
||||
description: skill.description,
|
||||
...(skill.status === 'disabled' ? {} : { content: skill.content.slice(0, 1500) }),
|
||||
})),
|
||||
};
|
||||
const output = await this.ask({
|
||||
binary: this.env.WF_CLAUDE_BIN!,
|
||||
@ -307,35 +670,70 @@ export class Skills {
|
||||
})
|
||||
.slice(0, 2);
|
||||
return await this.store.transaction(tenantId, async tx => {
|
||||
const names = new Set((await tx.list<Skill>('skill', { projectId: run.projectId })).map(skill => skill.name));
|
||||
const project = await must<Project>(tx, 'project', run.projectId);
|
||||
const auto = autoActivation(project);
|
||||
const skills = await tx.list<Skill>('skill', { projectId: run.projectId });
|
||||
const byName = new Map(skills.map(skill => [skill.name, skill]));
|
||||
const succeeded = async (ids: string[]) => {
|
||||
let count = 0;
|
||||
for (const id of ids) if ((await tx.get<Run>('run', id))?.status === 'succeeded') count++;
|
||||
return count;
|
||||
};
|
||||
const created: Skill[] = [];
|
||||
const updated: Skill[] = [];
|
||||
const activated: Skill[] = [];
|
||||
for (const item of proposals) {
|
||||
// A name the project uses already gets the next free number.
|
||||
let name = item.name;
|
||||
for (let n = 2; names.has(name); n++) name = `${item.name}-${n}`;
|
||||
names.add(name);
|
||||
const description = redact(item.description);
|
||||
const content = redact(item.content).slice(0, SKILL_CONTENT_LIMIT);
|
||||
const target = (item.updates && byName.get(item.updates)) || byName.get(item.name);
|
||||
if (target) {
|
||||
const rewrite =
|
||||
target.status === 'proposed' || (target.status === 'active' && auto && platformMaintained(target));
|
||||
// Asked about the same run again: only new words are news.
|
||||
const unchanged = !rewrite || (target.content === content && target.description === description);
|
||||
if (target.sourceRunIds.includes(run.id) && unchanged) continue;
|
||||
const sourceRunIds = lastRuns(target.sourceRunIds, run.id);
|
||||
const activate = target.status === 'proposed' && auto && (await succeeded(sourceRunIds)) >= 2;
|
||||
const next: Skill = rewrite
|
||||
? { ...touch(target), description, content, sourceRunIds }
|
||||
: counted(target, { sourceRunIds });
|
||||
if (activate) next.status = 'active';
|
||||
await tx.put('skill', next);
|
||||
byName.set(next.name, next);
|
||||
(activate ? activated : updated).push(next);
|
||||
continue;
|
||||
}
|
||||
const skill: Skill = {
|
||||
...record(tenantId),
|
||||
projectId: run.projectId,
|
||||
name,
|
||||
description: redact(item.description),
|
||||
content: redact(item.content).slice(0, SKILL_CONTENT_LIMIT),
|
||||
name: freeName(item.name, new Set(byName.keys())),
|
||||
description,
|
||||
content,
|
||||
category: 'workflow',
|
||||
status: 'proposed',
|
||||
sourceRunIds: [run.id],
|
||||
createdBy: 'wishforge',
|
||||
};
|
||||
await tx.put('skill', skill);
|
||||
byName.set(skill.name, skill);
|
||||
created.push(skill);
|
||||
}
|
||||
await this.recordHarvest(tx, run, { status: 'done', skillIds: created.map(skill => skill.id) });
|
||||
const changed = [...created, ...updated, ...activated];
|
||||
await this.recordHarvest(tx, run, { status: 'done', skillIds: changed.map(skill => skill.id) });
|
||||
const names = (list: Skill[]) => list.map(skill => skill.name).join(', ');
|
||||
if (created.length)
|
||||
await tx.event(
|
||||
run.id,
|
||||
'skill.proposed',
|
||||
`Proposed ${created.length} project skill${created.length > 1 ? 's' : ''}: ${created.map(skill => skill.name).join(', ')}`,
|
||||
{ skillIds: created.map(skill => skill.id) },
|
||||
);
|
||||
return created;
|
||||
await tx.event(run.id, 'skill.proposed', `提议了 ${created.length} 个项目技能:${names(created)}`, {
|
||||
skillIds: created.map(skill => skill.id),
|
||||
});
|
||||
if (updated.length)
|
||||
await tx.event(run.id, 'skill.updated', `已用这次运行更新项目技能:${names(updated)}`, {
|
||||
skillIds: updated.map(skill => skill.id),
|
||||
});
|
||||
if (activated.length)
|
||||
await tx.event(run.id, 'skill.activated', `已启用 ${names(activated)}:又一次成功的运行再次提议了它。`, {
|
||||
skillIds: activated.map(skill => skill.id),
|
||||
});
|
||||
return changed;
|
||||
});
|
||||
} catch (error) {
|
||||
await this.store
|
||||
@ -387,6 +785,38 @@ export class Skills {
|
||||
await this.harvest(tenantId, next.id);
|
||||
return next.id;
|
||||
}
|
||||
/**
|
||||
* Keeps the skills honest: counts the outcome of every finished run that was given skills (a
|
||||
* success counts when it happens; failures and cancels are found here), and retires
|
||||
* platform-maintained skills no run was given for RETIRE_UNUSED_DAYS while their project kept
|
||||
* running, crowded out by newer ones. Returns the skills it retired.
|
||||
*/
|
||||
async maintain(tenantId: string, at = new Date()): Promise<Skill[]> {
|
||||
return this.store.transaction(tenantId, async tx => {
|
||||
const usages = await tx.list<SkillUsage>('skill_usage');
|
||||
// Oldest first, so a failure streak ends on the run that completed it.
|
||||
for (const usage of usages.filter(item => !item.outcome).reverse()) {
|
||||
const run = await tx.get<Run>('run', usage.runId);
|
||||
if (!run) await tx.put('skill_usage', { ...touch(usage), outcome: 'neutral' });
|
||||
else await settleSkillUsage(tx, run);
|
||||
}
|
||||
const cutoff = new Date(at.getTime() - RETIRE_UNUSED_DAYS * 86_400_000).toISOString();
|
||||
const live = new Set(usages.filter(usage => usage.createdAt >= cutoff).map(usage => usage.projectId));
|
||||
const retired: Skill[] = [];
|
||||
for (const skill of await tx.list<Skill>('skill', { status: 'active' }))
|
||||
if (platformMaintained(skill) && live.has(skill.projectId) && (skill.lastUsedAt ?? skill.updatedAt) < cutoff) {
|
||||
const next: Skill = {
|
||||
...touch(skill),
|
||||
status: 'disabled',
|
||||
retiredAt: at.toISOString(),
|
||||
retiredReason: 'stale',
|
||||
};
|
||||
await tx.put('skill', next);
|
||||
retired.push(next);
|
||||
}
|
||||
return retired;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Every run that succeeded is looked at once, oldest first, one at a time; a failed look is tried
|
||||
* again a few times. Runs only where the deployment's Claude account serves its tenant.
|
||||
@ -410,6 +840,28 @@ export class Skills {
|
||||
timer.unref();
|
||||
return () => clearInterval(timer);
|
||||
}
|
||||
/** Counts finished runs and retires stale skills for every tenant this deployment serves. */
|
||||
startMaintenance(intervalMs = 10 * 60_000) {
|
||||
const tenants = [
|
||||
...new Set([...(this.env.WF_TENANT_ID ? [this.env.WF_TENANT_ID] : []), ...dispatchTenants(this.env)]),
|
||||
];
|
||||
if (!tenants.length) return () => undefined;
|
||||
let busy = false;
|
||||
const tick = async () => {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
try {
|
||||
for (const tenantId of tenants) await this.maintain(tenantId);
|
||||
} catch (error) {
|
||||
console.error(`[skills] ${error instanceof Error ? error.message : 'maintenance failed'}`);
|
||||
} finally {
|
||||
busy = false;
|
||||
}
|
||||
};
|
||||
const timer = setInterval(() => void tick(), intervalMs);
|
||||
timer.unref();
|
||||
return () => clearInterval(timer);
|
||||
}
|
||||
/** Whoever may wish for the project may shape the skills its later wishes follow. */
|
||||
private authorize(actor: Actor, project: Project) {
|
||||
requireHuman(actor);
|
||||
|
||||
@ -11,6 +11,7 @@ import type { Store } from '../db/store.js';
|
||||
import { CredentialVault } from './connections.js';
|
||||
import { copilotModel } from './copilot-model.js';
|
||||
import { projectAccess } from './control-plane.js';
|
||||
import { storedExecutorSettings } from './executor-settings.js';
|
||||
import { nativeClaudeEnabled } from './local-execution.js';
|
||||
import { requireRole } from './policy.js';
|
||||
|
||||
@ -104,10 +105,16 @@ const CONCURRENCY = 2;
|
||||
/** Long enough for a model to finish the structured JSON; the console proxy waits for both attempts. */
|
||||
const ASK_TIMEOUT_MS = 180_000;
|
||||
const PRO = 'deepseek-pro@latest';
|
||||
/** The small model first; the coding model if that one times out or returns JSON that does not parse. */
|
||||
function askModels(env: NodeJS.ProcessEnv): string[] {
|
||||
const primary = copilotModel(env);
|
||||
const fallback = env.WF_CLAUDE_ASK_FALLBACK_MODEL ?? PRO;
|
||||
/**
|
||||
* The small model first; the coding model if that one times out or returns JSON that does not
|
||||
* parse. The tenant's Claude Code settings come before the operator's.
|
||||
*/
|
||||
export function askModels(
|
||||
env: NodeJS.ProcessEnv,
|
||||
tenant: { askModel?: string; askFallbackModel?: string } = {},
|
||||
): string[] {
|
||||
const primary = tenant.askModel || copilotModel(env);
|
||||
const fallback = tenant.askFallbackModel || (env.WF_CLAUDE_ASK_FALLBACK_MODEL ?? PRO);
|
||||
return [...new Set([primary, fallback].filter(Boolean))];
|
||||
}
|
||||
let running = 0;
|
||||
@ -158,7 +165,10 @@ export class WishRefiner {
|
||||
async refine(actor: Actor, projectId: string, input: unknown) {
|
||||
requireRole(actor, ['owner', 'admin', 'developer']);
|
||||
const draft = wishRefinementInput.parse(input);
|
||||
const project = await this.store.read(actor.tenantId, tx => projectAccess(tx, actor, projectId));
|
||||
const [project, settings] = await this.store.read(
|
||||
actor.tenantId,
|
||||
async tx => [await projectAccess(tx, actor, projectId), await storedExecutorSettings(tx, 'claude-code')] as const,
|
||||
);
|
||||
if (!(await this.available(actor.tenantId)))
|
||||
throw new DomainError(
|
||||
'REFINER_UNAVAILABLE',
|
||||
@ -192,7 +202,7 @@ export class WishRefiner {
|
||||
timeoutMs: ASK_TIMEOUT_MS,
|
||||
...(credentialEnv ? { credentialEnv } : {}),
|
||||
};
|
||||
const models = askModels(this.env);
|
||||
const models = askModels(this.env, settings);
|
||||
let failure: unknown;
|
||||
for (const model of models) {
|
||||
try {
|
||||
|
||||
@ -12,6 +12,7 @@ import {
|
||||
import type { Store } from '../db/store.js';
|
||||
import { must, projectAccess } from './control-plane.js';
|
||||
import { redact } from './policy.js';
|
||||
import { changedPathsOf } from './provider-events.js';
|
||||
|
||||
const STAGES: Record<string, string> = {
|
||||
clarify: '澄清',
|
||||
@ -188,7 +189,10 @@ function runSection(index: number, { run, events, evidence, reviews, releases, c
|
||||
...new Set(
|
||||
events
|
||||
.filter(event => event.type === 'provider.file_change')
|
||||
.map(event => workspacePath(event.message.replace(/^Changed /, ''))),
|
||||
.flatMap(event => {
|
||||
const paths = changedPathsOf(event.message);
|
||||
return paths ? [workspacePath(paths)] : [];
|
||||
}),
|
||||
),
|
||||
];
|
||||
if (files.length) lines.push('#### Agent 改动的文件', files.map(file => `- \`${file}\``).join('\n'));
|
||||
|
||||
@ -1,45 +0,0 @@
|
||||
import { DomainError, type Actor, type Workspace } from '@wishforge/contracts';
|
||||
import type { Authenticator } from '../auth.js';
|
||||
import type { Store } from '../db/store.js';
|
||||
import { must, projectAccess } from './control-plane.js';
|
||||
import { GitStorage } from './git-storage.js';
|
||||
import { requireHuman, requireRole } from './policy.js';
|
||||
|
||||
/** Who commits from the IDE: the person's own name and email when their sign-in carries them. */
|
||||
export function commitAuthor(actor: Actor) {
|
||||
const local = actor.userId.replace(/[^A-Za-z0-9._-]+/g, '-').slice(0, 64) || 'user';
|
||||
return { name: actor.displayName ?? actor.userId, email: actor.email ?? `${local}@users.wishforge.invalid` };
|
||||
}
|
||||
|
||||
/**
|
||||
* The IDE gateway asks for the repository credential of a workspace on behalf of a person
|
||||
* with an open editing session; their membership and role are checked again every time.
|
||||
*/
|
||||
export class WorkspaceGit {
|
||||
constructor(
|
||||
private store: Store,
|
||||
private auth: Authenticator,
|
||||
private env = process.env,
|
||||
private fetcher: typeof fetch = fetch,
|
||||
) {}
|
||||
async credential(tenantId: string, workspaceId: string, userId: string) {
|
||||
const user = await this.auth.sessionUser(tenantId, userId);
|
||||
requireHuman(user);
|
||||
requireRole(user, ['owner', 'admin', 'developer']);
|
||||
const { workspace, project } = await this.store.read(tenantId, async tx => {
|
||||
const workspace = await must<Workspace>(tx, 'workspace', workspaceId);
|
||||
return { workspace, project: await projectAccess(tx, user, workspace.projectId) };
|
||||
});
|
||||
if (workspace.status !== 'ready')
|
||||
throw new DomainError('WORKSPACE_NOT_READY', 'Workspace has not been provisioned', 503);
|
||||
const credential = await new GitStorage(this.env, this.fetcher).workspaceCredential(project);
|
||||
return {
|
||||
projectId: project.id,
|
||||
projectName: project.name,
|
||||
repositoryUrl: project.repositoryUrl!,
|
||||
branch: project.targetBranch,
|
||||
token: credential.token,
|
||||
expiresAt: credential.expiresAt,
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
import { DomainError, type Actor, type Workspace } 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<Workspace>(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());
|
||||
}
|
||||
}
|
||||
@ -1,245 +0,0 @@
|
||||
import { resolveGatewayTarget } from './gateway-target.js';
|
||||
import { resolveProvisionerTarget } from './provisioner-target.js';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import {
|
||||
DomainError,
|
||||
type Actor,
|
||||
type Workspace,
|
||||
type WorkspaceLifecycleOperation as Lifecycle,
|
||||
} from '@wishforge/contracts';
|
||||
import type { OutboxItem, Store, Transaction } from '../db/store.js';
|
||||
import { idempotent, must, projectAccess, touch } from './control-plane.js';
|
||||
import { requireHuman, requireRole } from './policy.js';
|
||||
|
||||
export const WORKSPACE_IDLE_MS = 30 * 60_000;
|
||||
interface GatewayActivity {
|
||||
workspaceId: string;
|
||||
known: boolean;
|
||||
gatewayConnections: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pause scales a provisioner-managed IDE workspace to zero; files persist on its
|
||||
* volume, unsaved editor buffers do not. Idle workspaces (no gateway connection
|
||||
* and no authorized activity for 30 minutes) are paused automatically.
|
||||
*/
|
||||
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<Workspace>(tx, 'workspace', id);
|
||||
await projectAccess(tx, { ...actor, role: currentRole }, String(workspace.projectId));
|
||||
return workspace;
|
||||
}
|
||||
private safe(w: Workspace) {
|
||||
return { id: w.id, status: w.status, lifecycle: w.lifecycle, reason: w.reason, lastActivityAt: w.lastActivityAt };
|
||||
}
|
||||
private async provisioner(tenantId: string) {
|
||||
const provisioner = await resolveProvisionerTarget(this.env, tenantId);
|
||||
if (!provisioner)
|
||||
throw new DomainError(
|
||||
'WORKSPACE_LIFECYCLE_UNCONFIGURED',
|
||||
'A scoped workspace provisioner is required for this tenant',
|
||||
503,
|
||||
);
|
||||
return provisioner;
|
||||
}
|
||||
|
||||
async manual(actor: Actor, id: string, action: 'pause' | 'resume', key: string) {
|
||||
if (!key || key.length < 8 || key.length > 200)
|
||||
throw new DomainError('IDEMPOTENCY_REQUIRED', 'Provide an Idempotency-Key of 8–200 characters', 400);
|
||||
await this.provisioner(actor.tenantId);
|
||||
const request = { workspaceId: id, action };
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const workspace = await this.access(tx, actor, id);
|
||||
return idempotent(tx, key, request, async () => {
|
||||
if (workspace.managedBy !== 'wishforge-provisioner')
|
||||
throw new DomainError(
|
||||
'WORKSPACE_LIFECYCLE_UNCONFIGURED',
|
||||
'This workspace does not have a managed lifecycle',
|
||||
503,
|
||||
);
|
||||
if (workspace.status === (action === 'pause' ? 'paused' : 'ready')) return this.safe(workspace);
|
||||
if (action === 'pause' && workspace.status !== 'ready')
|
||||
throw new DomainError(
|
||||
'WORKSPACE_OPERATION_PENDING',
|
||||
'Resolve the existing lifecycle operation before pausing',
|
||||
);
|
||||
if (!['ready', 'paused', 'pausing', 'resuming', 'unknown'].includes(String(workspace.status)))
|
||||
throw new DomainError('WORKSPACE_NOT_READY', 'Workspace provisioning must be reconciled first');
|
||||
return this.queue(tx, workspace, action === 'pause' ? 'paused' : 'running', false);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private async queue(tx: Transaction, workspace: Workspace, desired: Lifecycle['desired'], automatic: boolean) {
|
||||
const revision = (workspace.lifecycle?.revision ?? 0) + 1;
|
||||
const operationId = randomUUID();
|
||||
workspace.lifecycle = {
|
||||
revision,
|
||||
desired,
|
||||
automatic,
|
||||
requestedAt: new Date().toISOString(),
|
||||
operationId,
|
||||
state: 'queued',
|
||||
} satisfies Lifecycle;
|
||||
workspace.status = desired === 'paused' ? 'pausing' : 'resuming';
|
||||
workspace.reason = 'Waiting for the 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);
|
||||
}
|
||||
|
||||
/** Idle only when the gateway positively reports no open connection to the workspace. */
|
||||
private async idle(workspace: Workspace): Promise<boolean> {
|
||||
const gateway = await resolveGatewayTarget(this.env, workspace.tenantId);
|
||||
if (!gateway) return false;
|
||||
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),
|
||||
}).catch(() => undefined);
|
||||
if (!response?.ok) return false;
|
||||
const value = (await response.json().catch(() => undefined)) as GatewayActivity | undefined;
|
||||
return value?.workspaceId === workspace.id && value.known === true && value.gatewayConnections === 0;
|
||||
}
|
||||
|
||||
async scheduleIdle(tenantId: string) {
|
||||
if (this.env.WF_WORKSPACE_AUTO_SLEEP === 'false') return;
|
||||
if (!(await resolveProvisionerTarget(this.env, tenantId).catch(() => undefined))) return;
|
||||
const cutoff = Date.now() - WORKSPACE_IDLE_MS;
|
||||
const candidates = await this.store.transaction(tenantId, async tx =>
|
||||
(await tx.list<Workspace>('workspace')).filter(
|
||||
w =>
|
||||
w.managedBy === 'wishforge-provisioner' &&
|
||||
w.status === 'ready' &&
|
||||
typeof w.lastActivityAt === 'string' &&
|
||||
Date.parse(w.lastActivityAt) <= cutoff,
|
||||
),
|
||||
);
|
||||
for (const before of candidates) {
|
||||
if (!(await this.idle(before))) continue;
|
||||
await this.store.transaction(tenantId, async tx => {
|
||||
const w = await must<Workspace>(tx, 'workspace', before.id);
|
||||
if (w.status !== 'ready' || w.lastActivityAt !== before.lastActivityAt) return;
|
||||
await this.queue(tx, w, 'paused', true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async reconcile(tenantId: string, item: OutboxItem) {
|
||||
const workspace = await this.store.transaction(tenantId, tx =>
|
||||
must<Workspace>(tx, 'workspace', String(item.payload.workspaceId)),
|
||||
);
|
||||
const operation = workspace.lifecycle;
|
||||
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.provisioner(tenantId);
|
||||
// Give a user who reconnects right after an automatic decision a chance to keep the workspace.
|
||||
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: Workspace['status'];
|
||||
};
|
||||
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 && !(await this.idle(workspace))) {
|
||||
await this.store.transaction(tenantId, async tx => {
|
||||
const current = await must<Workspace>(tx, 'workspace', workspace.id);
|
||||
if (current.lifecycle?.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<Workspace>(tx, 'workspace', workspace.id);
|
||||
if (current.lifecycle?.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<Workspace>(tx, 'workspace', workspace.id);
|
||||
if (current.lifecycle?.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 (error) {
|
||||
console.error(`Workspace ${workspace.id} lifecycle reconcile: ${error instanceof Error ? error.message : error}`);
|
||||
await this.store.transaction(tenantId, async tx => {
|
||||
const current = await must<Workspace>(tx, 'workspace', workspace.id);
|
||||
if (current.lifecycle?.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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ import {
|
||||
Inject,
|
||||
Param,
|
||||
Patch,
|
||||
Put,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
@ -26,27 +27,26 @@ import {
|
||||
projectModelsInput,
|
||||
commandList,
|
||||
gitBranchName,
|
||||
inlineImageTypes,
|
||||
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 { ControlPlane, must, projectAccess, 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 { ExecutorSettingsService } from './domain/executor-settings.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 { Entitlements } 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';
|
||||
@ -64,10 +64,10 @@ export class Services {
|
||||
auth: Authenticator;
|
||||
releases: Releases;
|
||||
bundles: AgentBundles;
|
||||
lessons: Lessons;
|
||||
skills: Skills;
|
||||
reports: WishReports;
|
||||
connections: Connections;
|
||||
executorSettings: ExecutorSettingsService;
|
||||
runners: RunnerBroker;
|
||||
github: GitHubIntegration;
|
||||
accounts: LocalAccounts;
|
||||
@ -80,10 +80,10 @@ export class Services {
|
||||
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.executorSettings = new ExecutorSettingsService(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);
|
||||
@ -172,17 +172,6 @@ const meterBody = z
|
||||
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) {
|
||||
@ -333,6 +322,7 @@ export class ApiController {
|
||||
models: projectModelsInput.nullable().optional(),
|
||||
// The budget every wish of this project starts with; null returns to the platform default.
|
||||
defaultBudget: budgetSchema.nullable().optional(),
|
||||
skillAutoActivation: z.boolean().optional(),
|
||||
deliveryConfig: z
|
||||
.object({
|
||||
imageRepository: z.string().min(1),
|
||||
@ -374,24 +364,33 @@ export class ApiController {
|
||||
}),
|
||||
);
|
||||
}
|
||||
@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));
|
||||
}
|
||||
/** SKILL.md files, or a zip of them as the export writes it, added to the project. */
|
||||
@Post('projects/:id/skills/import') async importSkills(
|
||||
@Req() r: AuthRequest,
|
||||
@Param('id') id: string,
|
||||
@Body() b: unknown,
|
||||
) {
|
||||
return ok(await this.s.skills.importFiles(r.actor, id, b, key(r)));
|
||||
}
|
||||
/** The knowledge base: skills of every project the person may see. */
|
||||
@Get('skills') async allSkills(
|
||||
@Req() r: AuthRequest,
|
||||
@Query('project') projectId?: string,
|
||||
@Query('status') status?: string,
|
||||
@Query('category') category?: string,
|
||||
@Query('q') q?: string,
|
||||
) {
|
||||
return ok(await this.s.skills.search(r.actor, { projectId, status, category, q }));
|
||||
}
|
||||
@Post('skills/copy') async copySkills(@Req() r: AuthRequest, @Body() b: unknown) {
|
||||
return ok(await this.s.skills.copy(r.actor, b, key(r)));
|
||||
}
|
||||
/** 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,
|
||||
@ -601,6 +600,25 @@ export class ApiController {
|
||||
@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/attachments/:attachmentId') async attachment(
|
||||
@Req() r: AuthRequest,
|
||||
@Res() res: Response,
|
||||
@Param('id') id: string,
|
||||
@Param('attachmentId') attachmentId: string,
|
||||
) {
|
||||
const file = await this.s.control.attachment(r.actor, id, attachmentId);
|
||||
// Images show inline; anything else downloads, so uploaded HTML or SVG never runs here.
|
||||
const inline = inlineImageTypes.includes(file.mediaType);
|
||||
res
|
||||
.type(inline ? file.mediaType : 'application/octet-stream')
|
||||
.setHeader(
|
||||
'Content-Disposition',
|
||||
`${inline ? 'inline' : 'attachment'}; filename*=UTF-8''${encodeURIComponent(file.name)}`,
|
||||
)
|
||||
.setHeader('X-Content-Type-Options', 'nosniff')
|
||||
.setHeader('Content-Security-Policy', "default-src 'none'; sandbox")
|
||||
.send(Buffer.from(file.data, 'base64'));
|
||||
}
|
||||
@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)));
|
||||
}
|
||||
@ -706,6 +724,19 @@ export class ApiController {
|
||||
@Delete('provider-connections/:id') async revoke(@Req() r: AuthRequest, @Param('id') id: string) {
|
||||
return ok(await this.s.connections.revoke(r.actor, id));
|
||||
}
|
||||
@Get('executor-settings') async executorSettingsList(@Req() r: AuthRequest) {
|
||||
return ok(await this.s.executorSettings.list(r.actor));
|
||||
}
|
||||
@Get('executor-settings/:provider') async executorSettings(@Req() r: AuthRequest, @Param('provider') p: string) {
|
||||
return ok(await this.s.executorSettings.get(r.actor, p));
|
||||
}
|
||||
@Put('executor-settings/:provider') async putExecutorSettings(
|
||||
@Req() r: AuthRequest,
|
||||
@Param('provider') p: string,
|
||||
@Body() b: unknown,
|
||||
) {
|
||||
return ok(await this.s.executorSettings.put(r.actor, p, b));
|
||||
}
|
||||
@Get('environments') async envs(@Req() r: AuthRequest) {
|
||||
return ok(await this.s.releases.environments(r.actor));
|
||||
}
|
||||
@ -844,44 +875,6 @@ export class ApiController {
|
||||
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')
|
||||
@ -1031,63 +1024,4 @@ export class InternalController {
|
||||
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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,8 +14,7 @@ const STATIC_ROUTES: Partial<Record<Actor['kind'], RegExp[]>> = {
|
||||
/**
|
||||
* Resolves the identity behind an `/internal` request and confines it: run-scoped
|
||||
* execution tokens address only their own run, static service identities only their
|
||||
* routes, the IDE gateway only workspace authorization and Git credentials, and environment runners only
|
||||
* their deployment tasks.
|
||||
* routes, and environment runners only their deployment tasks.
|
||||
*/
|
||||
export async function internalActor(
|
||||
auth: Authenticator,
|
||||
@ -28,10 +27,7 @@ export async function internalActor(
|
||||
(request.path === '/evidence' ? (request.body as { runId?: unknown } | undefined)?.runId : undefined);
|
||||
if (addressed !== actor.runId) throw new DomainError('FORBIDDEN', 'Execution token is scoped to a single run', 403);
|
||||
}
|
||||
if (actor.userId === 'workspace-gateway') {
|
||||
if (!/^\/workspaces\/[^/]+\/(authorize|git-credential)$/.test(request.path))
|
||||
throw new DomainError('FORBIDDEN', 'Gateway identity is limited to workspace authorization', 403);
|
||||
} else if (actor.userId.startsWith('runner:')) {
|
||||
if (actor.userId.startsWith('runner:')) {
|
||||
if (!request.path.startsWith('/runner/'))
|
||||
throw new DomainError('FORBIDDEN', 'Runner identity is limited to assigned deployment tasks', 403);
|
||||
} else if (!actor.runId && !(STATIC_ROUTES[actor.kind] ?? []).some(route => route.test(request.path)))
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { WorkspaceLaunchController } from './workspace-launch-http.js';
|
||||
import 'reflect-metadata';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
@ -17,8 +16,6 @@ import { localUser } from './auth.js';
|
||||
import { observability } from './telemetry.js';
|
||||
import { ProviderProxy } from './domain/provider-proxy.js';
|
||||
import { once } from 'node:events';
|
||||
import { WorkspaceLifecycleController } from './workspace-lifecycle-http.js';
|
||||
import { WorkspaceLifecycleWorker } from './workspace-lifecycle-worker.js';
|
||||
import { ResourceReconciliationWorker } from './resource-reconciliation-worker.js';
|
||||
|
||||
export async function bootstrap() {
|
||||
@ -37,27 +34,33 @@ export async function bootstrap() {
|
||||
const service = new Services(new ControlPlane(store));
|
||||
// Succeeded runs are looked at for skills worth keeping, one at a time, in the background.
|
||||
service.skills.startHarvesting();
|
||||
// Finished runs count for the skills they were given; skills that keep failing or go unused retire.
|
||||
service.skills.startMaintenance();
|
||||
const telemetry = await observability();
|
||||
@Module({
|
||||
controllers: [ApiController, InternalController, WorkspaceLifecycleController, WorkspaceLaunchController],
|
||||
controllers: [ApiController, InternalController],
|
||||
providers: [{ provide: Services, useValue: service }],
|
||||
})
|
||||
class AppModule {}
|
||||
const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log'], bodyParser: false });
|
||||
// Bodies are parsed before authentication, so only the checkpoint upload may be large.
|
||||
// Bodies are parsed before authentication, so only checkpoint uploads and wishes with files may be large.
|
||||
const checkpointBody = express.json({ limit: '16mb' }),
|
||||
proxyBody = express.json({ limit: '2mb' }),
|
||||
// A Copilot screenshot arrives as the picture itself, not as a JSON-wrapped base64 string.
|
||||
attachmentBody = express.raw({ limit: '4mb', type: ['image/png', 'image/jpeg', 'image/webp'] }),
|
||||
// A wish may carry up to 10 MB of attachments, base64-encoded.
|
||||
wishBody = express.json({ limit: '15mb' }),
|
||||
defaultBody = express.json({ limit: '1mb' });
|
||||
app.use((req: express.Request, res: express.Response, next: express.NextFunction) =>
|
||||
(/^\/internal\/runs\/[^/]+\/checkpoint$/.test(req.path)
|
||||
? checkpointBody
|
||||
: /^\/v1\/projects\/[^/]+\/copilot\/sessions\/[^/]+\/attachments$/.test(req.path)
|
||||
? attachmentBody
|
||||
: req.path.startsWith('/provider-proxy/')
|
||||
? proxyBody
|
||||
: defaultBody)(req, res, next),
|
||||
: req.method === 'POST' && /^\/v1\/projects\/[^/]+\/wishes$/.test(req.path)
|
||||
? wishBody
|
||||
: req.path.startsWith('/provider-proxy/')
|
||||
? proxyBody
|
||||
: defaultBody)(req, res, next),
|
||||
);
|
||||
app.useGlobalFilters(new Errors());
|
||||
const raw = app.getHttpAdapter().getInstance();
|
||||
@ -184,8 +187,6 @@ export async function bootstrap() {
|
||||
});
|
||||
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);
|
||||
@ -196,7 +197,6 @@ export async function bootstrap() {
|
||||
const shutdown = async () => {
|
||||
notifier.close();
|
||||
await resourceReconciliation.close();
|
||||
await workspaceLifecycle.close();
|
||||
await dispatcher.close();
|
||||
await app.close();
|
||||
await store.close();
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
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') ?? '',
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
import { Body, Controller, Inject, Param, Post, Req } from '@nestjs/common';
|
||||
import { z } from 'zod';
|
||||
import type { Request } from 'express';
|
||||
import type { Actor } from '@wishforge/contracts';
|
||||
import { Services } from './http.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') ?? '',
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
import type { ControlPlane } from './domain/control-plane.js';
|
||||
import { WorkspaceLifecycle } from './domain/workspace-lifecycle.js';
|
||||
import { dispatchTenants } from './tenants.js';
|
||||
/** Independent of Temporal availability; durable outbox reconciliation owns no application deployment. */
|
||||
export class WorkspaceLifecycleWorker {
|
||||
private timer?: ReturnType<typeof setTimeout>;
|
||||
private stopped = false;
|
||||
private busy?: Promise<void>;
|
||||
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 dispatchTenants(this.control.env)) {
|
||||
try {
|
||||
const pending = await this.control.store.transaction(tenant, tx =>
|
||||
tx.claimOutbox(['workspace.reconcile'], 120),
|
||||
);
|
||||
for (const item of pending) 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;
|
||||
}
|
||||
}
|
||||
@ -23,7 +23,7 @@ export interface GitClaims {
|
||||
iat: number;
|
||||
exp: number;
|
||||
runId?: string;
|
||||
/** Pushes may only update refs under this prefix: an IDE workspace pushes `refs/heads/ide/*`. */
|
||||
/** Pushes may only update refs under this prefix, for example `refs/heads/ide/*`. */
|
||||
refPrefix?: string;
|
||||
}
|
||||
const REF_PREFIX = /^refs\/heads\/[A-Za-z0-9][A-Za-z0-9._-]{0,63}\/$/;
|
||||
@ -54,7 +54,7 @@ const gitEnv = () => ({
|
||||
|
||||
/**
|
||||
* Every push runs this hook. A credential scoped to a ref prefix cannot update anything else,
|
||||
* so people's pushes from the IDE reach the target branch only through a verified wish.
|
||||
* so such pushes reach the target branch only through a verified wish.
|
||||
*/
|
||||
export const PRE_RECEIVE_HOOK = [
|
||||
'#!/bin/sh',
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
**/node_modules
|
||||
lib
|
||||
src-gen
|
||||
plugins
|
||||
workspace
|
||||
**/test
|
||||
11
apps/ide/.gitignore
vendored
11
apps/ide/.gitignore
vendored
@ -1,11 +0,0 @@
|
||||
node_modules/
|
||||
plugins/
|
||||
lib/
|
||||
src-gen/
|
||||
workspace/
|
||||
webpack.config.js
|
||||
gen-webpack.config.js
|
||||
tsconfig.json
|
||||
*.tsbuildinfo
|
||||
esbuild.mjs
|
||||
gen-esbuild.*.mjs
|
||||
@ -1,29 +0,0 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
# Operators supply reviewed immutable Debian-based Node images (build and slim runtime).
|
||||
ARG NODE_IMAGE
|
||||
ARG NODE_SLIM_IMAGE
|
||||
# The Theia workspace (`--target theia`); Git clones the project and serves the Git extension.
|
||||
FROM ${NODE_IMAGE} AS theia
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends git python3 make g++ libsecret-1-dev libx11-dev libxkbfile-dev libudev-dev && rm -rf /var/lib/apt/lists/*
|
||||
WORKDIR /opt/wishforge-ide
|
||||
ENV PUPPETEER_SKIP_DOWNLOAD=true
|
||||
COPY package.json package-lock.json ./
|
||||
COPY policy ./policy
|
||||
COPY collaboration ./collaboration
|
||||
RUN npm ci --no-audit --no-fund
|
||||
COPY scripts ./scripts
|
||||
RUN npm run plugins:download && npm run build:production && mkdir /workspace && chown -R node:node /workspace && chmod -R go-w /opt/wishforge-ide
|
||||
USER node
|
||||
ENV NODE_ENV=production WISHFORGE_IDE_HOST=0.0.0.0 WISHFORGE_IDE_PORT=4300 WISHFORGE_WORKSPACE_PATH=/workspace
|
||||
EXPOSE 4300
|
||||
CMD ["node", "scripts/start.mjs"]
|
||||
|
||||
# The workspace gateway needs only its own files and the WebSocket library.
|
||||
FROM ${NODE_SLIM_IMAGE} AS gateway
|
||||
WORKDIR /opt/wishforge-gateway
|
||||
COPY --from=theia /opt/wishforge-ide/node_modules/ws ./node_modules/ws
|
||||
COPY gateway ./gateway
|
||||
USER node
|
||||
ENV NODE_ENV=production WF_IDE_GATEWAY_HOST=0.0.0.0 WF_IDE_GATEWAY_PORT=4400
|
||||
EXPOSE 4400
|
||||
CMD ["node", "gateway/main.mjs"]
|
||||
@ -1,80 +0,0 @@
|
||||
# 云 IDE(Theia)
|
||||
|
||||
基于 Eclipse Theia **1.76.0** 的浏览器 IDE,包含 `@theia/collaboration`。它是独立的 npm 包(自己的 `package-lock.json`),不属于根目录的 npm workspaces,因为 Theia 依赖树很大且含原生模块。
|
||||
|
||||
| 目录 | 内容 |
|
||||
|---|---|
|
||||
| `src-gen/`、`lib/` | Theia 构建产物(`npm run ide:build` 生成) |
|
||||
| `policy/` | 策略扩展:扩展白名单、按工作区隔离的协作,见 [policy/README.md](policy/README.md) |
|
||||
| `gateway/` | IDE 网关:浏览器访问 Theia 的唯一入口,见 [gateway/README.md](gateway/README.md) |
|
||||
| `collaboration/` | 每个工作区独立的 OCT 协作服务,见 [collaboration/README.md](collaboration/README.md) |
|
||||
| `scripts/` | 启动脚本、扩展下载与校验 |
|
||||
| `plugins/` | 镜像内固定的扩展(`plugins:download` 生成) |
|
||||
|
||||
## 本地运行
|
||||
|
||||
```bash
|
||||
npm run ide:install
|
||||
```
|
||||
|
||||
```bash
|
||||
npm run ide:build
|
||||
```
|
||||
|
||||
```bash
|
||||
npm run dev:ide
|
||||
```
|
||||
|
||||
`dev:ide` 启动 Theia(`http://127.0.0.1:4300`,只能经网关访问)与网关(`http://127.0.0.1:4400`),所有工作区共用 `.wishforge/ide-workspace` 目录,每个项目克隆到其中以项目命名的文件夹(工作分支 `ide/work`),仓库令牌放在 `.wishforge/ide-credentials`。原生模块的编译需要 Python、make/C++ 与系统开发库(Linux 依赖见 `Dockerfile`)。
|
||||
|
||||
在项目页创建开发环境后点击「打开 IDE」。
|
||||
|
||||
- **克隆**:平台托管仓库的项目第一次打开时会被克隆到以项目命名的文件夹,通常几秒内出现在资源管理器中,并处在 `ide/work` 分支上。外部仓库(如 GitHub)不克隆,那里的改动应走 PR。
|
||||
- **推送**:提交后直接 `git push`(或 Git 面板的「发布分支」),推送到 `ide/work`。也可以建其他 `ide/` 开头的分支;推送目标分支或其他分支会被平台 Git 拒绝。
|
||||
- **合入**:在项目页的 IDE 分支列表点「提交愿望合入」,或提交愿望时把「起点」选成这个分支。运行从分支的最新提交开始,Agent 在上面继续实现,验证通过后一并合入目标分支;验证失败时照常进入修复轮。
|
||||
- **同步**:Agent 合入了新的改动后,用 `git pull origin <目标分支>` 把它们拉进 `ide/work`。
|
||||
- **凭据**:会话期间网关持续送来 15 分钟的仓库令牌,存放在仓库之外(见 [policy/README.md](policy/README.md))。没有人打开 IDE 时令牌会过期,`git push` 也随之失败。
|
||||
- **提交身份**:取最近打开会话的用户,用 `git config user.name`、`git config user.email` 可以改成自己的。
|
||||
|
||||
IDE 与 Agent 之间只通过 Git commit 交接,不存在 IDE 快照之类的旁路。
|
||||
|
||||
## 扩展
|
||||
|
||||
只能使用镜像中预置并校验过的扩展,运行时的安装、卸载、启用与停用请求都会被后端拒绝(`EXTENSIONS_MANAGED_BY_ADMIN`)。
|
||||
|
||||
- 内置扩展来自官方 `vscode-builtin-extensions` **1.108.2** 归档(SHA-256 `05a20cf3e671c8d328b04d9ce72b25688b0e762e4a3f5592f31a21ee38280367`),提供 Git、语言支持与 JavaScript 调试,每个扩展的 ID、版本与内容摘要记录在 `policy/managed-plugins.lock.json`;`node scripts/catalog-builtin-plugins.mjs <归档>` 在校验归档后重新生成该清单。
|
||||
- Python 支持:`ms-python.python` 2026.4.0、`ms-python.debugpy` 2026.6.0(调试器)、`ms-pyright.pyright` 1.1.402(补全、悬停与诊断),在 `scripts/python-debugger.lock.json` 中为 Linux x64/ARM64 与 macOS x64/ARM64 固定了官方 VSIX 的校验和;其他平台明确失败。Python 解释器本身需要在工作区镜像或项目虚拟环境中提供。
|
||||
|
||||
```bash
|
||||
npm run plugins:download --prefix apps/ide
|
||||
```
|
||||
|
||||
```bash
|
||||
npm run plugins:verify --prefix apps/ide
|
||||
```
|
||||
|
||||
离线构建时可以把已下载的 VSIX 传给 `npm run plugins:download:python -- --archive <debugpy.vsix> --python-archive <python.vsix> --pyright-archive <pyright.vsix>`。下载脚本校验 SHA-256、包身份、大小与解压路径,并原子地解包(Theia 的 `local-dir` 加载器不会加载未解包的 VSIX)。缺少、多出或被修改的扩展都会让启动失败。
|
||||
|
||||
## 镜像
|
||||
|
||||
`apps/ide/Dockerfile` 以 `apps/ide` 为构建上下文,运维提供 `NODE_IMAGE` 与 `NODE_SLIM_IMAGE`:
|
||||
|
||||
- `theia` 目标:Theia 工作区(含 Git)。应用、扩展与清单归 root 所有,只有 `/workspace` 交给运行用户;生产还需要 provisioner 设置只读根文件系统。
|
||||
- `gateway` 目标:IDE 网关。它是最后一个阶段,所以不指定 `--target` 时构建的是网关。
|
||||
|
||||
## 生产边界
|
||||
|
||||
- 不要直接暴露 4300 端口:终端、文件、插件与调试接口都必须在网关之后。启动脚本在 `NODE_ENV=production` 时要求 `WISHFORGE_GATEWAY_ENFORCED=true`——这只是运维的声明,真正的保护来自网关与网络策略。
|
||||
- 只有项目的 owner/admin/developer 能进入编辑会话或协作房间;viewer 不会拿到 IDE 会话。Theia 内置协作的只读控制并不完整,隐藏按钮不是授权边界。
|
||||
- OCT 0.3.1 的房间状态只在内存中:服务重启后需要新建房间,未保存的编辑可能丢失。
|
||||
- Theia 后端与用户终端以同一个系统用户运行,同一工作区内的协作者之间没有隔离;工作区只应授予可信成员编辑权限。
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
npm run test:ide
|
||||
```
|
||||
|
||||
运行网关、协作客户端与策略扩展的 Node 测试(需要先 `npm run ide:install`)。Theia 的界面行为(编辑器、终端、调试、Python 语言服务)没有自动化测试,修改依赖或扩展后需要在浏览器中手动验证。
|
||||
|
||||
参考:[Theia 架构](https://theia-ide.org/docs/architecture/)、[Theia 1.76 协作源码](https://github.com/eclipse-theia/theia/blob/v1.76.0/packages/collaboration/src/browser/collaboration-frontend-contribution.ts)、[OCT 0.3.1](https://github.com/eclipse-oct/open-collaboration-tools/tree/ef757242e1bc5bd4a74f1baf068aa047decaad13)、[内置扩展发布](https://github.com/eclipse-theia/vscode-builtin-extensions/releases/tag/1.108.2)。
|
||||
@ -1,6 +0,0 @@
|
||||
# Build the pinned upstream runtime first with infra/oct/Dockerfile.
|
||||
FROM wishforge/oct:0.3.1-wf1
|
||||
COPY --chown=node:node start-isolated.mjs /opt/oct/start-isolated.mjs
|
||||
ENV WF_OCT_HOST=0.0.0.0
|
||||
EXPOSE 8100
|
||||
CMD ["node", "/opt/oct/start-isolated.mjs"]
|
||||
@ -1,57 +0,0 @@
|
||||
# 按工作区隔离的 OCT 协作
|
||||
|
||||
本目录提供协作服务的启动器(`start-isolated.mjs`)与策略扩展使用的地址前缀适配器(`client-factory.cjs`)。每个工作区运行一个独立的 Open Collaboration Tools(OCT)0.3.1 进程,房间注册表与签名密钥不在工作区之间共享。身份以 WishForge 网关会话为准;OCT 的 `user` 显示名只是未经校验的展示文本,不代表租户身份或权限。
|
||||
|
||||
## 与上游实现相关的约束
|
||||
|
||||
以下行为来自固定版本的 Theia 1.76.0 与 OCT 0.3.1 源码,适配器据此设计:
|
||||
|
||||
- OCT 服务的 HTTP 路由都在根路径(`/api/meta`、`/api/login/*`、`/api/session/*`),没有 basepath 选项;Socket.IO 使用默认的 `/socket.io/` 路径,独立的 `/websocket` 服务在上游被注释掉,所以只支持 Socket.IO 传输。
|
||||
- 上游的 Socket.IO 传输调用 `io(url)` 时不传 `path`,会把 URL 路径当作命名空间;Theia 的登录表单也会把绝对端点解析到源站根路径、丢掉工作区前缀。适配器因此显式设置 Socket.IO 路径为 `/workspaces/<id>/collaboration/socket.io`,并把登录端点改写为相对的 `api/login/simple`,不允许任意 OAuth 地址。
|
||||
- 上游默认使用固定的开发签名密钥且 JWT 不过期。`start-isolated.mjs` 为每个进程生成随机密钥、JWT 8 小时过期,只开启显示名登录(`OCT_ACTIVATE_SIMPLE_LOGIN=true`)。服务重启后旧的房间凭据失效,需要重新创建或加入房间。它还绕开了 OCT 0.3.1 命令行 `parseInt` 端口参数的问题。
|
||||
- Theia 使用的协议 0.3.0 与服务端 0.3.1 兼容(0.x 系列按主次版本号校验)。
|
||||
|
||||
源码位置:[OCT 服务](https://github.com/eclipse-oct/open-collaboration-tools/blob/ef757242e1bc5bd4a74f1baf068aa047decaad13/packages/open-collaboration-server/src/collaboration-server.ts)、[显示名登录](https://github.com/eclipse-oct/open-collaboration-tools/blob/ef757242e1bc5bd4a74f1baf068aa047decaad13/packages/open-collaboration-server/src/auth-endpoints/simple-login-endpoint.ts)、[凭据管理](https://github.com/eclipse-oct/open-collaboration-tools/blob/ef757242e1bc5bd4a74f1baf068aa047decaad13/packages/open-collaboration-server/src/credentials-manager.ts)、[协议连接](https://github.com/eclipse-oct/open-collaboration-tools/blob/ef757242e1bc5bd4a74f1baf068aa047decaad13/packages/open-collaboration-protocol/src/connection-provider.ts)、[Theia 协作贡献](https://github.com/eclipse-theia/theia/blob/v1.76.0/packages/collaboration/src/browser/collaboration-frontend-contribution.ts)。
|
||||
|
||||
## 网关与客户端约定
|
||||
|
||||
API 的工作区授权结果给出可信的上游地址:
|
||||
|
||||
```json
|
||||
{
|
||||
"tenantId": "tenant-id",
|
||||
"projectId": "project-id",
|
||||
"workspaceId": "workspace-id",
|
||||
"role": "developer",
|
||||
"upstreamUrl": "http://workspace-service:3000",
|
||||
"collaborationUpstreamUrl": "http://workspace-service:8100"
|
||||
}
|
||||
```
|
||||
|
||||
只有 provisioner 能设置这些上游,公开调用方不能指定 OCT 地址。网关对 `/workspaces/<id>/collaboration/` 下的每个 HTTP 请求与 WebSocket 升级都要求 `edit` 权限,去掉前缀后转发到该工作区的 `collaborationUpstreamUrl`;保留 `x-oct-*` 协议头,去掉平台的 bearer 令牌与 Cookie,并定期复核已升级的连接。viewer 不能进入协作。
|
||||
|
||||
Theia 后端收到 `WF_COLLABORATION_GATEWAY_URL=https://ide.example/workspaces/<id>/collaboration/`。客户端适配器要求该地址与浏览器同源且属于当前工作区;初次握手使用轮询(便于携带 OCT 请求头),之后升级为 WebSocket。
|
||||
|
||||
## 运行
|
||||
|
||||
每个工作区一个协作进程,配置 `WF_WORKSPACE_ID`、`WF_TENANT_ID`、`WF_OCT_PORT=8100`、`WF_OCT_HOST=0.0.0.0` 与 `WISHFORGE_GATEWAY_ENFORCED=true`。在 Kubernetes 中放在工作区的 Kata Pod 内,8100 端口只允许网关访问,不对外暴露 Ingress;不需要工作区卷,根文件系统可以只读,去掉 Linux capability 并禁止提权。本机只在回环地址监听时可以使用默认的 `WF_OCT_HOST=127.0.0.1`。
|
||||
|
||||
```bash
|
||||
docker build -t wishforge/oct-workspace:0.3.1-wf1 apps/ide/collaboration
|
||||
```
|
||||
|
||||
`infra/compose.yaml` 中共享的开发 OCT 容器只用于协议检查,不承载工作区的房间。
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
node --test apps/ide/collaboration/test/client-factory.test.cjs
|
||||
```
|
||||
|
||||
```bash
|
||||
WF_OCT_INTEGRATION=1 node --test --test-force-exit apps/ide/collaboration/test/isolated-oct.test.mjs
|
||||
```
|
||||
|
||||
第二个测试需要 Docker:启动两个只监听回环地址的 OCT 容器,经真实的网关、不透明会话、固定版本的协议客户端、Socket.IO 加密与 Yjs 文档,验证主客双向编辑收敛、viewer 被拒绝、其他工作区的 Cookie 与复制的邀请被拒绝、跨实例 JWT 被拒绝,以及撤销权限后连接被关闭。其中控制面的授权接口是测试替身,因此它验证的是协议与路由,不是生产 OIDC 或 Kata 环境。Node 测试中 Engine.IO 在 `withCredentials=true` 时会替换手工注入的 Cookie,所以测试把它设为 false;浏览器客户端保持 true,依赖网关真实的 HttpOnly Cookie。
|
||||
|
||||
OCT 0.3.1 的房间只保存在内存中,不支持持久化的房间恢复。
|
||||
@ -1,59 +0,0 @@
|
||||
/** Prefix-aware OCT adapter for Theia 1.76 / open-collaboration-protocol 0.3.x.
|
||||
* Platform authentication remains the workspace gateway's HttpOnly session.
|
||||
* OCT display names and room tokens never grant access to a workspace.
|
||||
*/
|
||||
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');
|
||||
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' }));
|
||||
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);
|
||||
},
|
||||
};
|
||||
}
|
||||
function createPrefixConnectionProvider(deps, options) {
|
||||
const base = collaborationBase(options.url, options.browserLocation);
|
||||
return new deps.ConnectionProvider({
|
||||
url: base.href,
|
||||
client: options.client,
|
||||
fetch: (url, init) => options.fetch(url, { ...init, credentials: 'same-origin' }),
|
||||
authenticationHandler: (token, metadata) => options.authenticationHandler(token, normalizeAuthMetadata(metadata)),
|
||||
transports: [createPrefixTransport(deps, base.href)],
|
||||
userToken: options.userToken,
|
||||
});
|
||||
}
|
||||
module.exports = { collaborationBase, normalizeAuthMetadata, createPrefixTransport, createPrefixConnectionProvider };
|
||||
@ -1,34 +0,0 @@
|
||||
import 'reflect-metadata';
|
||||
import { randomBytes, webcrypto } from 'node:crypto';
|
||||
import { Container } from 'inversify';
|
||||
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');
|
||||
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');
|
||||
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
|
||||
// capabilities because upstream room state is in memory and cannot be recovered.
|
||||
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 container = new Container();
|
||||
container.load(serverModule);
|
||||
// This deployment deliberately has one display-name form behind the gateway;
|
||||
// no external OAuth providers or account registration flows are activated.
|
||||
container.rebind(AuthEndpoint).toService(SimpleLoginEndpoint);
|
||||
const credentials = container.get(CredentialsManager);
|
||||
credentials.getJwtExpiration = async () => '8h';
|
||||
const server = container.get(CollaborationServer);
|
||||
// Call the typed API instead of the 0.3.1 Commander parseInt/radix port bug.
|
||||
server.startServer({ hostname, port });
|
||||
@ -1,55 +0,0 @@
|
||||
const { test } = require('node:test');
|
||||
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));
|
||||
});
|
||||
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' },
|
||||
],
|
||||
});
|
||||
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.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/',
|
||||
);
|
||||
const transport = adapter.createTransport('ignored', { 'x-oct-jwt': 'room-token' });
|
||||
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');
|
||||
});
|
||||
@ -1,281 +0,0 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { randomUUID, webcrypto } from 'node:crypto';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { promisify } from 'node:util';
|
||||
import { createRequire } from 'node:module';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { createGateway } from '../../gateway/server.mjs';
|
||||
import factory from '../client-factory.cjs';
|
||||
const require = createRequire(new URL('../../package.json', import.meta.url));
|
||||
const protocol = await import(pathToFileURL(require.resolve('open-collaboration-protocol')).href);
|
||||
const { io } = await import(pathToFileURL(require.resolve('socket.io-client')).href);
|
||||
const { OpenCollaborationYjsProvider } = await import(pathToFileURL(require.resolve('open-collaboration-yjs')).href);
|
||||
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 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}`;
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
},
|
||||
);
|
||||
@ -1,49 +0,0 @@
|
||||
# IDE 网关
|
||||
|
||||
浏览器访问 Theia 的唯一入口,负责 HTTP 与 WebSocket 的身份校验。每个租户运行一个网关。本地用 `npm run dev:ide` 与 Theia 一起启动。
|
||||
|
||||
| 变量 | 说明 |
|
||||
|---|---|
|
||||
| `WF_DEPLOYMENT_MODE` | 只有 `development` 允许 HTTP 公网地址与默认的本机 Theia |
|
||||
| `WF_API_URL` | 控制面 API 地址 |
|
||||
| `WF_TENANT_ID` | 网关所属租户 |
|
||||
| `WF_WORKSPACE_GATEWAY_TOKEN` | 调用 API `/internal/workspaces/*` 的网关令牌 |
|
||||
| `WF_IDE_PUBLIC_URL` | 浏览器访问网关的地址,生产必须是 HTTPS(Cookie 才会带 Secure) |
|
||||
| `WF_IDE_ALLOWED_UPSTREAM_ORIGINS` | 允许的 Theia 上游地址(本地为 `http://127.0.0.1:4300`) |
|
||||
| `WF_IDE_ALLOWED_SERVICE_NAMESPACE` | Kubernetes 中工作区 Service 所在命名空间:上游必须是 `wf-ide-<24 位十六进制>.<命名空间>.svc:3000`,协作端口为 8100 |
|
||||
| `WF_COLLABORATION_ALLOWED_UPSTREAM_ORIGINS` | 本地允许的协作服务上游 |
|
||||
| `WF_IDE_GATEWAY_HOST`、`WF_IDE_GATEWAY_PORT` | 监听地址,默认 `127.0.0.1:4400` |
|
||||
|
||||
## 会话
|
||||
|
||||
1. Web 服务端用用户的 access token 调用 `POST /launch`(body `{workspaceId}`),网关向 API 校验后返回 `launchUrl`,其中只有 30 秒有效的一次性随机码,不含用户令牌。
|
||||
2. 浏览器访问 `GET /session?code=…`,网关再次校验、消费启动码,设置不透明的 HttpOnly、SameSite=Strict Cookie 并跳转到 `/workspaces/:id/`。访问日志必须隐去启动码查询串。
|
||||
3. 会话空闲 15 分钟失效;每次成功的请求与打开的 WebSocket 的周期复核都会续期,最长 12 小时。会话建立后,网关按已授权的用户 ID 复核,用户的 access token 过期不会中断正在使用的 IDE。
|
||||
|
||||
## 校验
|
||||
|
||||
- 每个 HTTP 请求与 WebSocket 握手都以网关令牌和固定的 `X-WishForge-Tenant` 调用 API `POST /internal/workspaces/:id/authorize`。打开的 WebSocket 每秒复核一次(复核超时 3 秒);被拒绝、会话过期、上游变化或控制面不可用时立即断开,因此撤销成员资格后现有连接会被关闭。
|
||||
- 上游地址只来自 API 的授权结果,并且必须在允许列表内;调用方不能选择上游,也不能把操作降级为只读。viewer 不能进入编辑会话。
|
||||
- 转发前去掉用户凭据、浏览器 Cookie 与调用方伪造的 `x-wishforge-*` 头。Theia 自己的 `theia-connection-token` 保存在网关进程中,不会下发给浏览器,也不会转发给协作服务。
|
||||
- `/workspaces/:id/collaboration/*` 把 HTTP 与 Socket.IO 转发到授权结果中该工作区专用的 OCT 实例,每个请求都要求编辑权限。
|
||||
- `POST /internal/lifecycle-state`(网关令牌)告诉控制面某个工作区当前的连接数,用于空闲自动暂停。
|
||||
|
||||
## 仓库凭据
|
||||
|
||||
- **申请**:会话建立时,以及打开的 WebSocket 复核时令牌快到期(剩 5 分钟),网关以会话用户的身份调用 API `POST /internal/workspaces/:id/git-credential`,取得 15 分钟的仓库令牌。这个令牌只能推送 `ide/` 开头的分支,由平台 Git 执行这条限制。
|
||||
- **投递**:网关把令牌交给工作区的 `POST /wishforge-internal/git-credential`,并附带 Theia 的连接令牌(没有就先请求一次 Theia 首页取得;Theia 重启后会自动重新取得)。投递内容还包括项目、仓库地址、目标分支和提交身份。
|
||||
- **提交身份**:取最近建立的会话用户,来自 API 首次授权时按 OIDC name/email 给出的 `author`。
|
||||
- **失败与例外**:失败时 30 秒后重试。外部仓库的项目 API 返回 409;工作区没有配置仓库时 Theia 返回 404。这两种情况在 30 分钟内不再申请,重新打开 IDE 会立即再试。
|
||||
- **路径保护**:浏览器请求 `/workspaces/:id/wishforge-internal/*` 一律返回 404,只有网关自己能投递凭据。
|
||||
|
||||
## 限制
|
||||
|
||||
- 会话与启动码保存在进程内存中:网关重启后需要重新打开 IDE;多副本需要共享会话存储或会话亲和。
|
||||
- 生产中必须让 Theia 与 OCT 只能经由网关访问,这需要在真实集群上验证网络隔离。
|
||||
|
||||
## 测试
|
||||
|
||||
需要先 `npm run ide:install`,然后运行 `npm run test:ide`。
|
||||
|
||||
- `gateway.test.mjs` 使用真实的本地 HTTP/WebSocket 服务,覆盖凭据不转发、启动码不可重放、viewer 拒绝、上游允许列表、Origin 检查、撤销后断开、使用中的会话续期与生命周期接口。
|
||||
- `gateway-handler.test.mjs` 不打开端口,直接驱动网关的请求处理器并使用伪造的 `fetch`,覆盖启动时提交用户令牌、仓库凭据的申请与投递、Theia 重启后重新取得连接令牌、凭据路径对浏览器不可见以及外部仓库不投递。
|
||||
@ -1,129 +0,0 @@
|
||||
// While someone has a workspace open, the gateway keeps its Git credential fresh: the control
|
||||
// plane mints a short-lived repository token for the session's user, and the gateway hands it
|
||||
// to the workspace backend, which clones the project on the first delivery. Nothing in the
|
||||
// workspace can mint a credential itself, so access ends with the last session.
|
||||
const CONNECTION_COOKIE = 'theia-connection-token';
|
||||
export const CREDENTIAL_PATH = '/wishforge-internal/git-credential';
|
||||
const REFRESH_BEFORE_MS = 5 * 60_000;
|
||||
const RETRY_MS = 30_000;
|
||||
// A project without a platform repository, or a workspace without the endpoint, keeps
|
||||
// answering the same way; ask again rarely.
|
||||
const UNSUPPORTED_RETRY_MS = 30 * 60_000;
|
||||
|
||||
export function createGitCredentials(config) {
|
||||
const fetchImpl = config.fetchImpl ?? fetch;
|
||||
const now = config.now ?? Date.now;
|
||||
const state = new Map();
|
||||
const authors = new Map();
|
||||
|
||||
function connectionToken(response) {
|
||||
for (const cookie of response.headers.getSetCookie?.() ?? []) {
|
||||
const token = new RegExp(`^${CONNECTION_COOKIE}=([^;\\s]{1,512})(?:;|$)`).exec(cookie)?.[1];
|
||||
if (token) return token;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
/** Theia accepts the delivery only with its connection token, which the gateway alone holds. */
|
||||
async function bootstrapToken(upstream) {
|
||||
const response = await fetchImpl(upstream.href, { redirect: 'manual', signal: AbortSignal.timeout(5000) });
|
||||
await response.body?.cancel();
|
||||
const token = connectionToken(response);
|
||||
if (token) config.setConnectionToken(upstream.href, token);
|
||||
return token;
|
||||
}
|
||||
async function credentialFor(workspaceId, userId) {
|
||||
const response = await fetchImpl(
|
||||
`${config.apiOrigin}/internal/workspaces/${encodeURIComponent(workspaceId)}/git-credential`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: `Bearer ${config.serviceToken}`,
|
||||
'content-type': 'application/json',
|
||||
'X-WishForge-Tenant': config.tenantId,
|
||||
},
|
||||
body: JSON.stringify({ userId }),
|
||||
signal: AbortSignal.timeout(5000),
|
||||
redirect: 'error',
|
||||
},
|
||||
);
|
||||
if (response.status === 409) return { unsupported: true };
|
||||
if (!response.ok) throw new Error('GIT_CREDENTIAL_UNAVAILABLE');
|
||||
const payload = await response.json();
|
||||
return payload.data ?? payload;
|
||||
}
|
||||
async function deliver(upstream, body) {
|
||||
const target = new URL(upstream);
|
||||
target.pathname = `${target.pathname.replace(/\/$/, '')}${CREDENTIAL_PATH}`;
|
||||
for (let attempt = 0; attempt < 2; attempt++) {
|
||||
const token = (attempt === 0 && config.connectionToken(upstream.href)) || (await bootstrapToken(upstream));
|
||||
if (!token) throw new Error('WORKSPACE_CONNECTION_TOKEN_UNAVAILABLE');
|
||||
const response = await fetchImpl(target, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', cookie: `${CONNECTION_COOKIE}=${token}` },
|
||||
body: JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
redirect: 'error',
|
||||
});
|
||||
await response.body?.cancel();
|
||||
// A restarted Theia has a new connection token; fetch it once and try again.
|
||||
if (response.status === 403 && attempt === 0) continue;
|
||||
// A workspace provisioned without the project's repository has no endpoint to deliver to.
|
||||
if (response.status === 404) return { unsupported: true };
|
||||
if (!response.ok) throw new Error('WORKSPACE_GIT_DELIVERY_FAILED');
|
||||
return {};
|
||||
}
|
||||
}
|
||||
async function refresh(workspaceId, upstream, userId) {
|
||||
const current = state.get(workspaceId);
|
||||
try {
|
||||
const credential = await credentialFor(workspaceId, userId);
|
||||
if (credential.unsupported) {
|
||||
current.nextAt = now() + UNSUPPORTED_RETRY_MS;
|
||||
return;
|
||||
}
|
||||
const author = authors.get(workspaceId);
|
||||
const delivered = await deliver(upstream, {
|
||||
projectId: credential.projectId,
|
||||
projectName: credential.projectName,
|
||||
repositoryUrl: credential.repositoryUrl,
|
||||
branch: credential.branch,
|
||||
token: credential.token,
|
||||
expiresAt: credential.expiresAt,
|
||||
...(author ? { author } : {}),
|
||||
});
|
||||
current.nextAt = delivered.unsupported
|
||||
? now() + UNSUPPORTED_RETRY_MS
|
||||
: Math.max(now() + 60_000, Date.parse(credential.expiresAt) - REFRESH_BEFORE_MS);
|
||||
} catch {
|
||||
current.nextAt = now() + RETRY_MS;
|
||||
}
|
||||
}
|
||||
return {
|
||||
/** A new session also makes its user the author of the workspace's next commits. */
|
||||
opened(session, upstream) {
|
||||
if (session.author) authors.set(session.workspaceId, session.author);
|
||||
const current = state.get(session.workspaceId);
|
||||
if (current) {
|
||||
current.nextAt = 0;
|
||||
// A delivery already under way may carry the previous author; send another after it.
|
||||
if (current.inFlight) current.again = true;
|
||||
}
|
||||
return this.due(session, upstream);
|
||||
},
|
||||
/** Called whenever a session is used; refreshes at most one delivery per workspace at a time. */
|
||||
due(session, upstream) {
|
||||
if (!session.userId) return undefined;
|
||||
const current = state.get(session.workspaceId) ?? { nextAt: 0 };
|
||||
state.set(session.workspaceId, current);
|
||||
if (current.inFlight || now() < current.nextAt) return current.inFlight;
|
||||
current.inFlight = refresh(session.workspaceId, upstream, session.userId).finally(() => {
|
||||
current.inFlight = undefined;
|
||||
if (current.again) {
|
||||
current.again = false;
|
||||
current.nextAt = 0;
|
||||
}
|
||||
});
|
||||
return current.inFlight;
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
import { createGateway } from './server.mjs';
|
||||
|
||||
// Fail closed: only an explicit development deployment may use plain HTTP and the local Theia default.
|
||||
const production = process.env.WF_DEPLOYMENT_MODE !== 'development';
|
||||
const publicUrl = process.env.WF_IDE_PUBLIC_URL ?? 'http://127.0.0.1:4400';
|
||||
if (production && !publicUrl.startsWith('https://')) throw new Error('Production gateway requires HTTPS public URL');
|
||||
const gateway = createGateway({
|
||||
publicUrl,
|
||||
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),
|
||||
allowedServiceNamespace: process.env.WF_IDE_ALLOWED_SERVICE_NAMESPACE,
|
||||
allowedCollaborationOrigins: (process.env.WF_COLLABORATION_ALLOWED_UPSTREAM_ORIGINS ?? '').split(',').filter(Boolean),
|
||||
});
|
||||
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);
|
||||
});
|
||||
36
apps/ide/gateway/package-lock.json
generated
36
apps/ide/gateway/package-lock.json
generated
@ -1,36 +0,0 @@
|
||||
{
|
||||
"name": "@wishforge/ide-gateway",
|
||||
"version": "0.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@wishforge/ide-gateway",
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"ws": "8.21.0"
|
||||
}
|
||||
},
|
||||
"node_modules/ws": {
|
||||
"version": "8.21.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz",
|
||||
"integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10.0.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"bufferutil": "^4.0.1",
|
||||
"utf-8-validate": ">=5.0.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"bufferutil": {
|
||||
"optional": true
|
||||
},
|
||||
"utf-8-validate": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
{
|
||||
"name": "@wishforge/ide-gateway",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node main.mjs",
|
||||
"test": "node --test test/*.test.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"ws": "8.21.0"
|
||||
}
|
||||
}
|
||||
@ -1,483 +0,0 @@
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
import { createGitCredentials, CREDENTIAL_PATH } from './git-credentials.mjs';
|
||||
|
||||
const COOKIE = 'WF_IDE_SESSION';
|
||||
const ID = /^[a-zA-Z0-9_-]{1,128}$/;
|
||||
// The roles defined by @wishforge/contracts that may edit, use terminals and debug.
|
||||
const EDIT_ROLES = new Set(['owner', 'admin', 'developer']);
|
||||
class HttpError extends Error {
|
||||
constructor(status, message) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
const random = () => randomBytes(32).toString('base64url');
|
||||
/** The commit identity the control plane derived from the person's sign-in, if it is usable. */
|
||||
function validAuthor(author) {
|
||||
const text = value =>
|
||||
typeof value === 'string' && value.length > 0 && value.length <= 200 && !/[\u0000-\u001f\u007f<>]/.test(value);
|
||||
return author && text(author.name) && text(author.email) && /^[^\s@]+@[^\s@]+$/.test(author.email)
|
||||
? { name: author.name, email: author.email }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
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');
|
||||
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 (!upstreamOrigins.size && !serviceNamespace) throw new Error('Explicit upstream allowlist required');
|
||||
const fetchImpl = config.fetchImpl ?? fetch;
|
||||
const sessions = new Map();
|
||||
const launches = new Map();
|
||||
// Theia's own process token is distinct from the caller's authentication.
|
||||
// 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 rawSockets = new Set();
|
||||
// A session ends after 15 idle minutes; while it is used it is renewed, for at most 12 hours.
|
||||
const sessionTtl = Math.min(config.sessionTtlMs ?? 900000, 900000);
|
||||
const sessionMaxMs = Math.min(config.sessionMaxMs ?? 12 * 3600_000, 12 * 3600_000);
|
||||
const pollMs = Math.min(config.reauthorizeMs ?? 1000, 1000);
|
||||
// An upstream that stays silent this long is given up. OCT holds each login poll open for 30 s
|
||||
// before answering, so collaboration requests get longer than that.
|
||||
const upstreamTimeoutMs = config.upstreamTimeoutMs ?? 30_000;
|
||||
const collaborationTimeoutMs = Math.max(upstreamTimeoutMs, 65_000);
|
||||
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);
|
||||
};
|
||||
const timer = setInterval(purge, 5000);
|
||||
timer.unref();
|
||||
// One person cannot use up the capacity everyone shares: beyond these, their own oldest
|
||||
// launch code or session ends. An ended session's open connections close at their next check.
|
||||
const LAUNCHES_PER_USER = 20,
|
||||
SESSIONS_PER_USER = 20;
|
||||
const keepNewest = (map, userId, limit, age) => {
|
||||
const own = [...map].filter(([, value]) => value.userId === userId).sort(([, a], [, b]) => age(a) - age(b));
|
||||
for (const [key, value] of own.slice(0, Math.max(0, own.length - limit))) {
|
||||
value.expiresAt = 0;
|
||||
map.delete(key);
|
||||
}
|
||||
};
|
||||
const wss = new WebSocketServer({ noServer: true, maxPayload: 16 * 1024 * 1024, perMessageDeflate: false });
|
||||
const gitCredentials = createGitCredentials({
|
||||
apiOrigin: config.apiOrigin,
|
||||
tenantId: config.tenantId,
|
||||
serviceToken: config.serviceToken,
|
||||
fetchImpl,
|
||||
now,
|
||||
connectionToken: href => upstreamTokens.get(href),
|
||||
setConnectionToken: (href, token) => upstreamTokens.set(href, token),
|
||||
});
|
||||
|
||||
/**
|
||||
* `credential` is the user's access token for a new session, or the user id an established
|
||||
* session was granted to: the API still checks the current membership and role each time.
|
||||
*/
|
||||
async function authorize(workspaceId, credential, operation, collaboration = false) {
|
||||
const { userToken, userId } = credential;
|
||||
if (!ID.test(workspaceId) || (!userToken && !userId) || (userToken?.length ?? 0) > 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(userId ? { userId, operation } : { 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');
|
||||
let 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');
|
||||
return { ...grant, upstream };
|
||||
}
|
||||
/** Checks access for a request or socket, and renews a cookie session that is in use. */
|
||||
async function authorizeSession(session, workspaceId, operation, collaboration) {
|
||||
const grant = await authorize(
|
||||
workspaceId,
|
||||
session.userId ? { userId: session.userId } : { userToken: session.userToken },
|
||||
operation,
|
||||
collaboration,
|
||||
);
|
||||
if (session.createdAt !== undefined) {
|
||||
if (session.userId === undefined) session.userId = grant.userId;
|
||||
else if (session.userId !== grant.userId) throw new HttpError(403, 'Workspace grant mismatch');
|
||||
session.expiresAt = Math.min(now() + sessionTtl, session.createdAt + sessionMaxMs);
|
||||
}
|
||||
return grant;
|
||||
}
|
||||
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);
|
||||
const session = opaque && sessions.get(opaque);
|
||||
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');
|
||||
}
|
||||
function route(req) {
|
||||
const parsed = new URL(req.url, publicUrl);
|
||||
const match = parsed.pathname.match(/^\/workspaces\/([a-zA-Z0-9_-]{1,128})(\/.*)?$/);
|
||||
if (!match) throw new HttpError(404, 'Workspace route not found');
|
||||
let suffix = match[2] ?? '/';
|
||||
// Only the gateway itself delivers Git credentials to the workspace backend. Theia's
|
||||
// Express routes ignore case, so this check does too.
|
||||
const internal = suffix.toLowerCase();
|
||||
if (
|
||||
internal === CREDENTIAL_PATH ||
|
||||
internal === '/wishforge-internal' ||
|
||||
internal.startsWith('/wishforge-internal/')
|
||||
)
|
||||
throw new HttpError(404, 'Workspace route not found');
|
||||
const collaboration = suffix === '/collaboration' || suffix.startsWith('/collaboration/');
|
||||
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';
|
||||
return { workspaceId: match[1], suffix, query: parsed.search, operation, collaboration };
|
||||
}
|
||||
function targetFor(grant, routing) {
|
||||
const target = new URL(grant.upstream);
|
||||
target.pathname = `${target.pathname.replace(/\/$/, '')}${routing.suffix}`;
|
||||
target.search = routing.query;
|
||||
return target;
|
||||
}
|
||||
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];
|
||||
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;
|
||||
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',
|
||||
});
|
||||
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');
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
// The control plane asks whether a workspace is idle before pausing it automatically.
|
||||
// Only this gateway knows how many browser connections are open.
|
||||
if (url.pathname === '/internal/lifecycle-state' && req.method === 'POST') {
|
||||
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))
|
||||
throw new HttpError(401, 'Service authentication required');
|
||||
const body = await readJson(req);
|
||||
if (body.tenantId !== config.tenantId || !ID.test(body.workspaceId ?? ''))
|
||||
throw new HttpError(400, 'Invalid workspace scope');
|
||||
res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
workspaceId: body.workspaceId,
|
||||
known: true,
|
||||
gatewayConnections: workspaceSockets.get(body.workspaceId)?.size ?? 0,
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (url.pathname === '/launch' && req.method === 'POST') {
|
||||
originCheck(req);
|
||||
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');
|
||||
const { userId } = await authorize(body.workspaceId, { userToken }, 'edit');
|
||||
purge();
|
||||
keepNewest(launches, userId, LAUNCHES_PER_USER - 1, launch => launch.expiresAt);
|
||||
if (launches.size >= 1000 || sessions.size >= 1000) throw new HttpError(429, 'Session capacity reached');
|
||||
const code = random();
|
||||
launches.set(code, { workspaceId: body.workspaceId, userToken, userId, 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);
|
||||
if (!launch || launch.expiresAt <= now()) throw new HttpError(401, 'Launch code is invalid or expired');
|
||||
const grant = await authorize(launch.workspaceId, { userToken: launch.userToken }, 'edit');
|
||||
if (typeof grant.userId !== 'string' || !grant.userId) throw new HttpError(403, 'Workspace grant mismatch');
|
||||
const opaque = random();
|
||||
// The token was just checked; the session keeps only the person it was granted to.
|
||||
const session = {
|
||||
workspaceId: launch.workspaceId,
|
||||
userId: grant.userId,
|
||||
author: validAuthor(grant.author),
|
||||
createdAt: now(),
|
||||
expiresAt: now() + sessionTtl,
|
||||
};
|
||||
sessions.set(opaque, session);
|
||||
keepNewest(sessions, session.userId, SESSIONS_PER_USER, value => value.createdAt);
|
||||
void gitCredentials.opened(session, grant.upstream);
|
||||
res.writeHead(303, {
|
||||
location: `/workspaces/${launch.workspaceId}/`,
|
||||
'set-cookie': `${COOKIE}=${opaque}; HttpOnly; SameSite=Strict; Path=/workspaces/${launch.workspaceId}/; Max-Age=${Math.floor(sessionMaxMs / 1000)}${publicUrl.protocol === 'https:' ? '; Secure' : ''}`,
|
||||
'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 authorizeSession(session, routing.workspaceId, 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);
|
||||
},
|
||||
);
|
||||
proxy.on('error', () => fail(res, new HttpError(502, 'Workspace upstream unavailable')));
|
||||
proxy.setTimeout(routing.collaboration ? collaborationTimeoutMs : upstreamTimeoutMs, () => proxy.destroy());
|
||||
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('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 grant = await authorizeSession(session, routing.workspaceId, 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,
|
||||
});
|
||||
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 });
|
||||
});
|
||||
if (!routing.collaboration) void gitCredentials.due(session, grant.upstream);
|
||||
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 authorizeSession(
|
||||
session,
|
||||
routing.workspaceId,
|
||||
routing.operation,
|
||||
routing.collaboration,
|
||||
);
|
||||
if (fresh.upstream.href !== grant.upstream.href) throw new Error('Workspace moved');
|
||||
if (!routing.collaboration) void gitCredentials.due(session, fresh.upstream);
|
||||
} 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,
|
||||
};
|
||||
}
|
||||
@ -1,215 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Readable } from 'node:stream';
|
||||
import { createGateway } from '../server.mjs';
|
||||
|
||||
// These drive the gateway's request handler directly with a fake fetch, so they also run where
|
||||
// opening sockets is not permitted. gateway.test.mjs covers proxying over real connections.
|
||||
const publicUrl = 'http://gateway.test:4400';
|
||||
const apiOrigin = 'http://api.test:4000';
|
||||
const upstreamUrl = 'http://theia.test:3000/';
|
||||
|
||||
function fakeFetch(options = {}) {
|
||||
const state = { calls: [], deliveries: [], connectionToken: 'connection-token-1', rejectDelivery: 0 };
|
||||
const json = (value, status = 200) =>
|
||||
new Response(JSON.stringify(value), { status, headers: { 'content-type': 'application/json' } });
|
||||
state.fetch = async (input, init = {}) => {
|
||||
const url = String(input);
|
||||
const body = init.body ? JSON.parse(init.body) : undefined;
|
||||
state.calls.push({ url, method: init.method ?? 'GET', headers: init.headers ?? {}, body });
|
||||
if (url === `${apiOrigin}/internal/workspaces/w1/authorize`)
|
||||
return json({
|
||||
data: {
|
||||
tenantId: 'tenant-a',
|
||||
projectId: 'p1',
|
||||
workspaceId: 'w1',
|
||||
// The launch presents a person's token; later checks name the person it was granted to.
|
||||
userId: body.userToken ? (options.userFor?.(body.userToken) ?? 'authenticated-user') : body.userId,
|
||||
role: 'developer',
|
||||
upstreamUrl,
|
||||
...(body.userToken ? { author: { name: 'Ada Lovelace', email: 'ada@example.com' } } : {}),
|
||||
},
|
||||
});
|
||||
if (url === `${apiOrigin}/internal/workspaces/w1/git-credential`) {
|
||||
if (options.unsupported) return json({ error: 'WORKSPACE_REPOSITORY_UNSUPPORTED' }, 409);
|
||||
return json({
|
||||
data: {
|
||||
projectId: 'p1',
|
||||
projectName: 'Todo App',
|
||||
repositoryUrl: 'https://git.test/git/tenant-a/p1.git',
|
||||
branch: 'main',
|
||||
token: 'payload.signature',
|
||||
expiresAt: new Date(Date.now() + 900_000).toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url === upstreamUrl)
|
||||
return new Response('<html></html>', {
|
||||
headers: { 'set-cookie': `theia-connection-token=${state.connectionToken}; HttpOnly; SameSite=Strict; Path=/` },
|
||||
});
|
||||
if (url === `${upstreamUrl}wishforge-internal/git-credential`) {
|
||||
if (state.rejectDelivery > 0 || init.headers.cookie !== `theia-connection-token=${state.connectionToken}`) {
|
||||
state.rejectDelivery -= 1;
|
||||
return new Response('', { status: 403 });
|
||||
}
|
||||
state.deliveries.push({ cookie: init.headers.cookie, body });
|
||||
return json({ status: 'cloning' });
|
||||
}
|
||||
throw new Error(`Unexpected fetch ${url}`);
|
||||
};
|
||||
return state;
|
||||
}
|
||||
|
||||
function send(gateway, { method = 'GET', url, headers = {}, body }) {
|
||||
const req = Readable.from(body === undefined ? [] : [Buffer.from(JSON.stringify(body))]);
|
||||
Object.assign(req, { method, url, headers: { host: 'gateway.test:4400', ...headers } });
|
||||
return new Promise(resolve => {
|
||||
const res = {
|
||||
headersSent: false,
|
||||
writeHead(status, responseHeaders = {}) {
|
||||
this.status = status;
|
||||
this.headers = responseHeaders;
|
||||
this.headersSent = true;
|
||||
return this;
|
||||
},
|
||||
end(chunk) {
|
||||
resolve({ status: this.status, headers: this.headers, body: chunk ? String(chunk) : '' });
|
||||
},
|
||||
destroy() {
|
||||
resolve({ status: 0, headers: {}, body: '' });
|
||||
},
|
||||
on() {
|
||||
return this;
|
||||
},
|
||||
};
|
||||
gateway.server.emit('request', req, res);
|
||||
});
|
||||
}
|
||||
|
||||
async function eventually(check) {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
if (check()) return;
|
||||
await new Promise(resolve => setTimeout(resolve, 5));
|
||||
}
|
||||
assert.fail('condition was not reached');
|
||||
}
|
||||
|
||||
function setup(t, options) {
|
||||
const state = fakeFetch(options);
|
||||
const gateway = createGateway({
|
||||
publicUrl,
|
||||
apiOrigin,
|
||||
tenantId: 'tenant-a',
|
||||
serviceToken: 'gateway-secret',
|
||||
allowedUpstreamOrigins: [new URL(upstreamUrl).origin],
|
||||
fetchImpl: state.fetch,
|
||||
});
|
||||
t.after(() => gateway.close());
|
||||
return { gateway, state };
|
||||
}
|
||||
|
||||
async function launch(gateway, token = 'user-token') {
|
||||
const response = await send(gateway, {
|
||||
method: 'POST',
|
||||
url: '/launch',
|
||||
headers: { origin: publicUrl, authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
||||
body: { workspaceId: 'w1' },
|
||||
});
|
||||
assert.equal(response.status, 200, response.body);
|
||||
return new URL(JSON.parse(response.body).launchUrl).searchParams.get('code');
|
||||
}
|
||||
const redeem = (gateway, code) => send(gateway, { url: `/session?code=${encodeURIComponent(code)}` });
|
||||
|
||||
async function openSession(gateway, token) {
|
||||
const session = await redeem(gateway, await launch(gateway, token));
|
||||
assert.equal(session.status, 303, session.body);
|
||||
return session.headers['set-cookie'].split(';')[0];
|
||||
}
|
||||
|
||||
test('launching presents the person’s token, and the session keeps only who it was granted to', async t => {
|
||||
const { gateway, state } = setup(t);
|
||||
await openSession(gateway);
|
||||
const checks = state.calls.filter(call => call.url.endsWith('/authorize'));
|
||||
assert.deepEqual(
|
||||
checks.map(call => call.body),
|
||||
[
|
||||
{ userToken: 'user-token', operation: 'edit' },
|
||||
{ userToken: 'user-token', operation: 'edit' },
|
||||
],
|
||||
);
|
||||
assert.equal(checks[0].headers.authorization, 'Bearer gateway-secret');
|
||||
});
|
||||
|
||||
test('a new session delivers a fresh repository credential and its author to the workspace', async t => {
|
||||
const { gateway, state } = setup(t);
|
||||
await openSession(gateway);
|
||||
await eventually(() => state.deliveries.length === 1);
|
||||
const minted = state.calls.find(call => call.url.endsWith('/git-credential'));
|
||||
assert.deepEqual(minted.body, { userId: 'authenticated-user' });
|
||||
assert.equal(minted.headers['X-WishForge-Tenant'], 'tenant-a');
|
||||
const [delivery] = state.deliveries;
|
||||
assert.equal(delivery.cookie, 'theia-connection-token=connection-token-1');
|
||||
assert.deepEqual(delivery.body, {
|
||||
projectId: 'p1',
|
||||
projectName: 'Todo App',
|
||||
repositoryUrl: 'https://git.test/git/tenant-a/p1.git',
|
||||
branch: 'main',
|
||||
token: 'payload.signature',
|
||||
expiresAt: delivery.body.expiresAt,
|
||||
author: { name: 'Ada Lovelace', email: 'ada@example.com' },
|
||||
});
|
||||
});
|
||||
|
||||
test('a restarted workspace gets the delivery after the gateway fetches its new connection token', async t => {
|
||||
const { gateway, state } = setup(t);
|
||||
state.rejectDelivery = 1;
|
||||
await openSession(gateway);
|
||||
await eventually(() => state.deliveries.length === 1);
|
||||
assert.equal(state.calls.filter(call => call.url === upstreamUrl).length, 2);
|
||||
});
|
||||
|
||||
test('people cannot reach the credential endpoint of the workspace through the gateway', async t => {
|
||||
const { gateway, state } = setup(t);
|
||||
const cookie = await openSession(gateway);
|
||||
await eventually(() => state.deliveries.length === 1);
|
||||
const before = state.calls.length;
|
||||
for (const path of [
|
||||
'/workspaces/w1/wishforge-internal/git-credential',
|
||||
'/workspaces/w1/wishforge-internal/other',
|
||||
'/workspaces/w1/wishforge-internal',
|
||||
// Theia's Express routes match regardless of case.
|
||||
'/workspaces/w1/Wishforge-Internal/git-credential',
|
||||
'/workspaces/w1/WISHFORGE-INTERNAL/GIT-CREDENTIAL',
|
||||
]) {
|
||||
const response = await send(gateway, { method: 'POST', url: path, headers: { origin: publicUrl, cookie } });
|
||||
assert.equal(response.status, 404);
|
||||
}
|
||||
assert.equal(state.calls.length, before);
|
||||
});
|
||||
|
||||
test('a project without a platform repository is not delivered anything', async t => {
|
||||
const { gateway, state } = setup(t, { unsupported: true });
|
||||
await openSession(gateway);
|
||||
await eventually(() => state.calls.some(call => call.url.endsWith('/git-credential')));
|
||||
await new Promise(resolve => setTimeout(resolve, 20));
|
||||
assert.equal(state.deliveries.length, 0);
|
||||
assert.equal(
|
||||
state.calls.some(call => call.url.startsWith(upstreamUrl)),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('one person cannot use up the capacity everyone shares; their own oldest codes and sessions end', async t => {
|
||||
const { gateway } = setup(t, { userFor: token => (token === 'other-token' ? 'other-user' : 'authenticated-user') });
|
||||
const codes = [];
|
||||
for (let i = 0; i < 21; i++) codes.push(await launch(gateway));
|
||||
assert.equal((await redeem(gateway, codes[0])).status, 401);
|
||||
const cookies = [];
|
||||
for (let i = 0; i < 21; i++) cookies.push(await openSession(gateway));
|
||||
assert.equal(gateway.sessionCount(), 20);
|
||||
const page = cookie => send(gateway, { url: '/workspaces/w1/', headers: { origin: publicUrl, cookie } });
|
||||
assert.equal((await page(cookies[0])).status, 401);
|
||||
// Someone else still gets in.
|
||||
await openSession(gateway, 'other-token');
|
||||
assert.equal(gateway.sessionCount(), 21);
|
||||
});
|
||||
@ -1,307 +0,0 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { once } from 'node:events';
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
import { createGateway } from '../server.mjs';
|
||||
|
||||
async function listen(server) {
|
||||
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: [], credentialRequests: [], receivedHeaders: undefined };
|
||||
const upstream = http.createServer(async (req, res) => {
|
||||
state.receivedHeaders = req.headers;
|
||||
// Like OCT's login poll, which answers only after holding the request open.
|
||||
if (state.delayMs && (req.url === '/slow' || req.url.startsWith('/api/login/poll/')))
|
||||
await new Promise(resolve => setTimeout(resolve, state.delayMs));
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
// Repository credentials are covered by gateway-handler.test.mjs; this project has none,
|
||||
// so the gateway never contacts Theia on its own here.
|
||||
if (req.url.endsWith('/git-credential')) {
|
||||
state.credentialRequests.push(body);
|
||||
res.writeHead(409);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
state.calls.push({ path: req.url, headers: req.headers, body });
|
||||
// A new session presents the user's token; an established one names the granted user.
|
||||
if (state.revoked || (body.userToken !== 'user-token' && body.userId !== 'authenticated-user')) {
|
||||
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,
|
||||
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],
|
||||
reauthorizeMs: 50,
|
||||
...options,
|
||||
});
|
||||
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' }),
|
||||
});
|
||||
async function session() {
|
||||
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);
|
||||
return { launchUrl, exchanged, cookie: exchanged.headers.get('set-cookie').split(';')[0] };
|
||||
}
|
||||
return { state, publicUrl, requestLaunch, session };
|
||||
}
|
||||
|
||||
test('gives up on a silent Theia upstream but waits out an OCT login poll', async t => {
|
||||
const f = await fixture(t, { upstreamTimeoutMs: 100 });
|
||||
const s = await f.session();
|
||||
f.state.delayMs = 400;
|
||||
const headers = { cookie: s.cookie, origin: f.publicUrl };
|
||||
assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/slow`, { headers })).status, 502);
|
||||
const poll = await fetch(`${f.publicUrl}/workspaces/w1/collaboration/api/login/poll/token-1`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
});
|
||||
assert.equal(poll.status, 200);
|
||||
assert.deepEqual(await poll.json(), { path: '/api/login/poll/token-1' });
|
||||
});
|
||||
|
||||
test('requires authentication, exchanges a one-time code, and never forwards bearer/cookie', async t => {
|
||||
const f = await fixture(t);
|
||||
assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/`)).status, 401);
|
||||
const s = await f.session();
|
||||
assert.match(s.exchanged.headers.get('set-cookie'), /HttpOnly/);
|
||||
assert.match(s.exchanged.headers.get('set-cookie'), /SameSite=Strict/);
|
||||
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(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.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';
|
||||
assert.equal((await f.requestLaunch()).status, 403);
|
||||
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' }),
|
||||
});
|
||||
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,
|
||||
);
|
||||
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 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(protectedResponse.status, 200);
|
||||
assert.equal(f.state.receivedHeaders.cookie, 'theia-connection-token=server-only-process-token');
|
||||
});
|
||||
|
||||
test('strips caller-supplied workspace identity headers before reaching Theia', async t => {
|
||||
const f = await fixture(t);
|
||||
const s = await f.session();
|
||||
const response = await fetch(`${f.publicUrl}/workspaces/w1/`, {
|
||||
headers: { cookie: s.cookie, 'x-wishforge-tenant': 'tenant-b', 'x-wishforge-user': 'attacker' },
|
||||
});
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(f.state.receivedHeaders['x-wishforge-tenant'], 'tenant-a');
|
||||
assert.equal(f.state.receivedHeaders['x-wishforge-workspace'], 'w1');
|
||||
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;
|
||||
assert.ok(f.state.calls.length >= 4);
|
||||
});
|
||||
|
||||
test(
|
||||
'an IDE in use outlives the idle timeout, rechecking the granted user rather than their token',
|
||||
{ timeout: 5000 },
|
||||
async t => {
|
||||
const f = await fixture(t, { sessionTtlMs: 300 });
|
||||
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');
|
||||
// Longer than the idle timeout: the open socket's periodic checks keep renewing the session.
|
||||
await new Promise(resolve => setTimeout(resolve, 800));
|
||||
ws.send('still-working');
|
||||
const [frame] = await once(ws, 'message');
|
||||
assert.equal(frame.toString(), 'still-working');
|
||||
assert.equal((await fetch(`${f.publicUrl}/workspaces/w1/`, { headers: { cookie: s.cookie } })).status, 200);
|
||||
const last = f.state.calls.at(-1).body;
|
||||
assert.equal(last.userId, 'authenticated-user');
|
||||
assert.equal(last.userToken, undefined);
|
||||
// Revocation still ends the session.
|
||||
const closed = once(ws, 'close');
|
||||
f.state.revoked = true;
|
||||
await closed;
|
||||
},
|
||||
);
|
||||
|
||||
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('lifecycle inspection is service authenticated, scoped, and counts every live workspace socket', async t => {
|
||||
const f = await fixture(t);
|
||||
const inspect = (token = 'gateway-secret', tenantId = 'tenant-a') =>
|
||||
fetch(`${f.publicUrl}/internal/lifecycle-state`, {
|
||||
method: 'POST',
|
||||
headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ tenantId, workspaceId: 'w1' }),
|
||||
});
|
||||
assert.equal((await inspect('user-token')).status, 401);
|
||||
assert.equal((await inspect('gateway-secret', 'tenant-b')).status, 400);
|
||||
let response = await inspect();
|
||||
assert.equal(response.status, 200);
|
||||
assert.deepEqual(await response.json(), { workspaceId: 'w1', known: true, gatewayConnections: 0 });
|
||||
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);
|
||||
});
|
||||
12978
apps/ide/package-lock.json
generated
12978
apps/ide/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,69 +0,0 @@
|
||||
{
|
||||
"name": "@wishforge/ide",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"engines": {
|
||||
"node": ">=22.14.0 <27"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "theia build --mode development",
|
||||
"build:production": "theia build --mode production",
|
||||
"start": "node scripts/start.mjs",
|
||||
"test": "node --test gateway/test/*.test.mjs policy/test/*.test.cjs collaboration/test/client-factory.test.cjs",
|
||||
"plugins:download": "theia download:plugins && node scripts/download-python-debugger.mjs && node scripts/verify-managed-plugins.mjs",
|
||||
"plugins:download:python": "node scripts/download-python-debugger.mjs",
|
||||
"plugins:verify": "node scripts/verify-managed-plugins.mjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"@theia/core": "1.76.0",
|
||||
"@theia/editor": "1.76.0",
|
||||
"@theia/filesystem": "1.76.0",
|
||||
"@theia/workspace": "1.76.0",
|
||||
"@theia/navigator": "1.76.0",
|
||||
"@theia/preferences": "1.76.0",
|
||||
"@theia/messages": "1.76.0",
|
||||
"@theia/terminal": "1.76.0",
|
||||
"@theia/process": "1.76.0",
|
||||
"@theia/monaco": "1.76.0",
|
||||
"@theia/scm": "1.76.0",
|
||||
"@theia/debug": "1.76.0",
|
||||
"@theia/markers": "1.76.0",
|
||||
"@theia/search-in-workspace": "1.76.0",
|
||||
"@theia/outline-view": "1.76.0",
|
||||
"@theia/callhierarchy": "1.76.0",
|
||||
"@theia/typehierarchy": "1.76.0",
|
||||
"@theia/file-search": "1.76.0",
|
||||
"@theia/mini-browser": "1.76.0",
|
||||
"@theia/plugin-ext": "1.76.0",
|
||||
"@theia/plugin-ext-vscode": "1.76.0",
|
||||
"@theia/vsx-registry": "1.76.0",
|
||||
"@theia/collaboration": "1.76.0",
|
||||
"@theia/editor-preview": "1.76.0",
|
||||
"@wishforge/ide-policy": "file:./policy"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@theia/cli": "1.76.0"
|
||||
},
|
||||
"theia": {
|
||||
"target": "browser",
|
||||
"frontend": {
|
||||
"config": {
|
||||
"applicationName": "WishForge IDE",
|
||||
"defaultTheme": "dark",
|
||||
"defaultIconTheme": "theia-file-icons",
|
||||
"applicationVersion": "0.1.0",
|
||||
"preferences": {
|
||||
"telemetry.telemetryLevel": "off"
|
||||
}
|
||||
}
|
||||
},
|
||||
"backend": {
|
||||
"config": {
|
||||
"frontendConnectionTimeout": 0
|
||||
}
|
||||
}
|
||||
},
|
||||
"theiaPlugins": {
|
||||
"vscode-builtin-extensions": "https://github.com/eclipse-theia/vscode-builtin-extensions/releases/download/1.108.2/vscode-builtin-extensions-1.108.2.tar.gz"
|
||||
}
|
||||
}
|
||||
@ -1,27 +0,0 @@
|
||||
# IDE 策略扩展(`@wishforge/ide-policy`)
|
||||
|
||||
随 IDE 一起构建的 Theia 1.76 前后端扩展,做三件事:
|
||||
|
||||
1. **扩展白名单**(FR-H5):后端替换 Theia 的插件服务与部署器。安装、卸载、启用、停用一律返回 `EXTENSIONS_MANAGED_BY_ADMIN`;启动时只加载 `WF_IDE_MANAGED_PLUGINS_DIR` 中、列在 `managed-plugins.lock.json`(以及 `scripts/python-debugger.lock.json`)里的扩展,并逐个核对扩展 ID、版本与目录内容摘要。符号链接与特殊文件被拒绝,目录未配置时后端拒绝启动插件部署。前端在用户尝试安装扩展时说明扩展由管理员通过镜像构建统一管理。
|
||||
2. **按工作区隔离的协作**(FR-H3):替换 `@theia/collaboration` 的前端贡献,只有当 `WF_COLLABORATION_GATEWAY_URL` 指向当前已认证工作区的网关协作路由时才注册房间命令与状态栏入口,否则协作保持关闭,不会退回公共 OCT 服务。OCT 令牌按实例保存,不复用其他工作区的令牌。协作连接的地址计算在 `../collaboration/client-factory.cjs` 中。
|
||||
3. **项目仓库**(FR-H2):后端提供 `POST /wishforge-internal/git-credential`,接收网关投递的仓库令牌(见 [gateway/README.md](../gateway/README.md))。
|
||||
- **访问控制**:要求 Theia 的连接令牌,这个令牌只有网关持有。
|
||||
- **校验**:请求必须严格符合格式,仓库地址必须与工作区配置一致。provisioner 设置 `WISHFORGE_GIT_REMOTE` 时只接受这一个仓库;本地共享的 Theia 设置 `WISHFORGE_GIT_ORIGIN`,接受这个 Git 服务上属于所投递项目的仓库。
|
||||
- **令牌存放**:写入 `WISHFORGE_GIT_CREDENTIAL_DIR` 下的 `<项目>.gitconfig`(权限 0600,内容是 `http.<仓库>.extraHeader` 与提交身份),克隆通过 `include.path` 引用它。令牌不进入命令行、仓库文件或 `.git/config`;Kubernetes 中这个目录是内存卷。
|
||||
- **克隆**:工作区中还没有该仓库的克隆时,把项目克隆到以项目命名的文件夹。同名文件夹已被占用时在名称后加上项目 ID 的前 8 位,两者都被占用时不克隆,从不覆盖已有文件。之后的投递只刷新令牌,项目改名后仍沿用原来的克隆。
|
||||
- **工作分支**:克隆后切换到 `ide/work`(远端已有这个分支时跟踪它,否则从目标分支创建),并设置 `push.default=current` 与 `push.autoSetupRemote=true`,这样不带参数的 `git push` 就会发布这个分支。令牌只能推送 `ide/` 开头的分支。
|
||||
- **身份覆盖**:用户自己设置的 `git config user.name` / `user.email` 优先于平台给的身份。
|
||||
|
||||
IDE 与 Agent 之间只通过 Git commit 交接,扩展不提供保存屏障、草稿恢复或调试控制租约。
|
||||
|
||||
## 测试
|
||||
|
||||
```bash
|
||||
node --test apps/ide/policy/test/*.test.cjs
|
||||
```
|
||||
|
||||
测试覆盖插件安装接口被拒绝、白名单内容摘要校验与符号链接拒绝,以及用真实 Git 验证仓库的克隆、令牌刷新、提交身份、工作分支的发布与继续、不覆盖已有文件夹和拒绝其他仓库。需要先安装 IDE 依赖(`npm run ide:install`);`npm run test:ide` 会一并运行。
|
||||
|
||||
## 边界
|
||||
|
||||
Theia 后端与用户终端以同一个系统用户运行,扩展的限制可以被同一工作区中的恶意终端绕过;它约束的是正常使用路径,不是同一工作区内协作者之间的隔离。工作区之间的隔离依赖每个工作区独立的 Kata Pod 与网关。
|
||||
@ -1,12 +0,0 @@
|
||||
const { ContainerModule } = require('@theia/core/shared/inversify');
|
||||
const { BackendApplicationContribution } = require('@theia/core/lib/node/backend-application');
|
||||
const { GitCredentialEndpoint } = require('./git-credential-endpoint.cjs');
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
exports.default = new ContainerModule((bind, _unbind, _isBound, rebind) => {
|
||||
// Only operator-pinned, digest-verified extensions may be installed (see managed-plugins.cjs).
|
||||
require('./plugin-install-policy.cjs').bindManagedPlugins(rebind);
|
||||
// The project's repository is cloned and kept authorized while the IDE is open (see git-workspace.cjs).
|
||||
bind(GitCredentialEndpoint).toSelf().inSingletonScope();
|
||||
bind(BackendApplicationContribution).toService(GitCredentialEndpoint);
|
||||
});
|
||||
@ -1,59 +0,0 @@
|
||||
const { ContainerModule, injectable } = require('@theia/core/shared/inversify');
|
||||
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');
|
||||
const { io } = require('socket.io-client');
|
||||
const { collaborationBase, createPrefixConnectionProvider } = require('../collaboration/client-factory.cjs');
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
// Register room commands only for this workspace's authenticated gateway route.
|
||||
// An absent/mismatched URL keeps collaboration disabled with no public fallback.
|
||||
class ScopedCollaborationContribution extends CollaborationFrontendContribution {
|
||||
async scopedUrl() {
|
||||
const url = await this.getCollaborationServerUrl();
|
||||
return collaborationBase(url, window.location.href).href;
|
||||
}
|
||||
init() {
|
||||
this.scopedUrl()
|
||||
.then(() => this.setStatusBarEntryDefault())
|
||||
.catch(() => {});
|
||||
}
|
||||
registerCommands(commands) {
|
||||
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,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
injectable()(DedicatedWorkspaceService);
|
||||
module.exports.default = new ContainerModule((_bind, _unbind, _isBound, rebind) => {
|
||||
require('./plugin-install-frontend.cjs').bindManagedExtensionNotice(_bind);
|
||||
rebind(CollaborationWorkspaceService).to(DedicatedWorkspaceService).inSingletonScope();
|
||||
rebind(CollaborationFrontendContribution).to(ScopedCollaborationContribution).inSingletonScope();
|
||||
});
|
||||
@ -1,43 +0,0 @@
|
||||
'use strict';
|
||||
const { decorate, inject, injectable } = require('@theia/core/shared/inversify');
|
||||
const express = require('@theia/core/shared/express');
|
||||
const { HttpConnectionValidator } = require('@theia/core/lib/node');
|
||||
const {
|
||||
CREDENTIAL_PATH,
|
||||
GitWorkspaceError,
|
||||
createGitWorkspace,
|
||||
readGitWorkspaceConfig,
|
||||
} = require('./git-workspace.cjs');
|
||||
|
||||
/**
|
||||
* Receives the repository credential from the workspace gateway. Theia's connection token,
|
||||
* which only the gateway holds, is required; the gateway refuses to forward this path for people.
|
||||
*/
|
||||
class GitCredentialEndpoint {
|
||||
configure(app) {
|
||||
const config = readGitWorkspaceConfig(process.env);
|
||||
if (!config) return;
|
||||
const workspace = createGitWorkspace(config);
|
||||
app.post(
|
||||
CREDENTIAL_PATH,
|
||||
(req, res, next) => this.connectionValidator.validateRequest(req, res, next),
|
||||
express.json({ limit: '16kb' }),
|
||||
async (req, res) => {
|
||||
try {
|
||||
const { status, directory } = await workspace.apply(req.body);
|
||||
res.status(200).json({ status, directory });
|
||||
} catch (error) {
|
||||
const known = error instanceof GitWorkspaceError;
|
||||
if (!known || error.code !== 'INVALID_GIT_CREDENTIAL')
|
||||
console.error(`WishForge Git workspace update failed: ${error.message}`);
|
||||
res
|
||||
.status(known && error.code === 'INVALID_GIT_CREDENTIAL' ? 400 : 500)
|
||||
.json({ error: known ? error.code : 'GIT_WORKSPACE_FAILED' });
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
decorate(inject(HttpConnectionValidator), GitCredentialEndpoint.prototype, 'connectionValidator');
|
||||
decorate(injectable(), GitCredentialEndpoint);
|
||||
module.exports = { GitCredentialEndpoint };
|
||||
@ -1,241 +0,0 @@
|
||||
'use strict';
|
||||
// Brings the project's repository into the IDE workspace. While someone has the workspace open,
|
||||
// the gateway delivers a short-lived repository token (see gateway/git-credentials.mjs); it is
|
||||
// written to a Git config file outside the repository that the clone includes, so nothing secret
|
||||
// lands in the repository or its .git/config. The first delivery clones the project into a folder
|
||||
// named after it and switches to the ide/work branch: the token may push ide/ branches only, and
|
||||
// a wish starting from such a branch verifies and merges it into the target branch.
|
||||
const { spawn } = require('node:child_process');
|
||||
const fs = require('node:fs/promises');
|
||||
const path = require('node:path');
|
||||
|
||||
const CREDENTIAL_PATH = '/wishforge-internal/git-credential';
|
||||
const WORK_BRANCH = 'ide/work';
|
||||
const ID = /^[A-Za-z0-9_-]{1,128}$/;
|
||||
const TOKEN = /^[A-Za-z0-9_-]{1,4096}\.[A-Za-z0-9_-]{1,512}$/;
|
||||
const PLATFORM_REPOSITORY = /^\/git\/[A-Za-z0-9][A-Za-z0-9_-]{0,100}\/([A-Za-z0-9][A-Za-z0-9_-]{0,100})\.git$/;
|
||||
|
||||
class GitWorkspaceError extends Error {
|
||||
constructor(code, message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
const invalid = message => new GitWorkspaceError('INVALID_GIT_CREDENTIAL', message);
|
||||
|
||||
function branchName(value) {
|
||||
if (
|
||||
typeof value !== 'string' ||
|
||||
!/^[A-Za-z0-9][A-Za-z0-9._/-]{0,199}$/.test(value) ||
|
||||
/\.\.|\/\/|\/\.|@\{/.test(value) ||
|
||||
/(?:\/|\.|\.lock)$/.test(value)
|
||||
)
|
||||
throw invalid('Invalid branch');
|
||||
return value;
|
||||
}
|
||||
function personText(value) {
|
||||
return typeof value === 'string' && value.length > 0 && value.length <= 200 && !/[\u0000-\u001f\u007f<>]/.test(value);
|
||||
}
|
||||
|
||||
/** The workspace's Git settings; without them the credential endpoint stays off. */
|
||||
function readGitWorkspaceConfig(env) {
|
||||
const root = env.WISHFORGE_WORKSPACE_PATH;
|
||||
const credentialDir = env.WISHFORGE_GIT_CREDENTIAL_DIR;
|
||||
if (!root || !credentialDir) return undefined;
|
||||
// A workspace of its own accepts exactly its project's repository; the one local Theia
|
||||
// shared by every project accepts any repository of the platform Git service it is given.
|
||||
if (env.WISHFORGE_GIT_REMOTE) return { root, credentialDir, remote: new URL(env.WISHFORGE_GIT_REMOTE).href };
|
||||
if (env.WISHFORGE_GIT_ORIGIN) return { root, credentialDir, origin: new URL(env.WISHFORGE_GIT_ORIGIN).origin };
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function validate(payload, config) {
|
||||
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) throw invalid('Expected an object');
|
||||
const fields = ['projectId', 'projectName', 'repositoryUrl', 'branch', 'token', 'expiresAt', 'author'];
|
||||
if (Object.keys(payload).some(key => !fields.includes(key))) throw invalid('Unexpected field');
|
||||
if (!ID.test(payload.projectId ?? '')) throw invalid('Invalid project');
|
||||
if (
|
||||
payload.projectName !== undefined &&
|
||||
(typeof payload.projectName !== 'string' || payload.projectName.length > 200)
|
||||
)
|
||||
throw invalid('Invalid project name');
|
||||
let repository;
|
||||
try {
|
||||
repository = new URL(payload.repositoryUrl);
|
||||
} catch {
|
||||
throw invalid('Invalid repository');
|
||||
}
|
||||
if (config.remote) {
|
||||
if (repository.href !== config.remote) throw invalid('This workspace belongs to another repository');
|
||||
} else if (
|
||||
repository.origin !== config.origin ||
|
||||
repository.username ||
|
||||
repository.password ||
|
||||
repository.search ||
|
||||
repository.hash ||
|
||||
PLATFORM_REPOSITORY.exec(repository.pathname)?.[1] !== payload.projectId
|
||||
)
|
||||
throw invalid('Repository is not the project’s platform repository');
|
||||
if (!TOKEN.test(payload.token ?? '')) throw invalid('Invalid token');
|
||||
if (!Number.isFinite(Date.parse(payload.expiresAt))) throw invalid('Invalid expiry');
|
||||
const author = payload.author;
|
||||
if (
|
||||
author !== undefined &&
|
||||
(!author ||
|
||||
typeof author !== 'object' ||
|
||||
Object.keys(author).some(key => !['name', 'email'].includes(key)) ||
|
||||
!personText(author.name) ||
|
||||
!personText(author.email) ||
|
||||
!/^[^\s@]+@[^\s@]+$/.test(author.email))
|
||||
)
|
||||
throw invalid('Invalid author');
|
||||
return {
|
||||
projectId: payload.projectId,
|
||||
projectName: payload.projectName,
|
||||
repositoryUrl: repository.href,
|
||||
branch: branchName(payload.branch),
|
||||
token: payload.token,
|
||||
author: author && { name: author.name, email: author.email },
|
||||
};
|
||||
}
|
||||
|
||||
/** Quotes a value or subsection name for a Git config file. */
|
||||
const quote = value => `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
||||
|
||||
function runGit(args, { cwd, include } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const env = { ...process.env, GIT_TERMINAL_PROMPT: '0', GCM_INTERACTIVE: 'never' };
|
||||
// Passing the credential file by path keeps the token out of the command line.
|
||||
if (include)
|
||||
Object.assign(env, { GIT_CONFIG_COUNT: '1', GIT_CONFIG_KEY_0: 'include.path', GIT_CONFIG_VALUE_0: include });
|
||||
const child = spawn('git', args, { cwd, env, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
let stdout = '';
|
||||
let stderr = '';
|
||||
child.stdout.on('data', chunk => {
|
||||
stdout = (stdout + chunk).slice(-65536);
|
||||
});
|
||||
child.stderr.on('data', chunk => {
|
||||
stderr = (stderr + chunk).slice(-2048);
|
||||
});
|
||||
const timer = setTimeout(() => child.kill('SIGKILL'), 10 * 60_000);
|
||||
child.on('error', () => {
|
||||
clearTimeout(timer);
|
||||
reject(new GitWorkspaceError('GIT_UNAVAILABLE', 'Git is not installed in this workspace'));
|
||||
});
|
||||
child.on('close', code => {
|
||||
clearTimeout(timer);
|
||||
if (code === 0) resolve(stdout.trim());
|
||||
else reject(new GitWorkspaceError('GIT_FAILED', `git ${args[0]} exited ${code}: ${stderr.trim()}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function exists(file) {
|
||||
return fs.lstat(file).then(
|
||||
() => true,
|
||||
error => {
|
||||
if (error.code === 'ENOENT') return false;
|
||||
throw error;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function createGitWorkspace(config, { git = runGit } = {}) {
|
||||
const clones = new Map();
|
||||
const authors = new Map();
|
||||
|
||||
async function writeCredential(file, input) {
|
||||
// A delivery without an author (for example after the gateway restarted) keeps the last one.
|
||||
const author = input.author ?? authors.get(input.projectId);
|
||||
if (author) authors.set(input.projectId, author);
|
||||
const lines = [
|
||||
'# Written by WishForge while the IDE is open; the token expires on its own.',
|
||||
`[http ${quote(input.repositoryUrl)}]`,
|
||||
`\textraHeader = ${quote(`Authorization: Bearer ${input.token}`)}`,
|
||||
...(author ? ['[user]', `\tname = ${quote(author.name)}`, `\temail = ${quote(author.email)}`] : []),
|
||||
];
|
||||
await fs.mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
||||
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
|
||||
await fs.writeFile(temporary, `${lines.join('\n')}\n`, { mode: 0o600 });
|
||||
await fs.rename(temporary, file);
|
||||
}
|
||||
async function origin(directory) {
|
||||
if (!(await exists(path.join(directory, '.git')))) return undefined;
|
||||
return git(['config', '--get', 'remote.origin.url'], { cwd: directory }).catch(() => undefined);
|
||||
}
|
||||
/** An earlier clone keeps working even after the project was renamed or the folder moved. */
|
||||
async function findClone(repositoryUrl) {
|
||||
if ((await origin(config.root)) === repositoryUrl) return config.root;
|
||||
const entries = await fs.readdir(config.root, { withFileTypes: true }).catch(() => []);
|
||||
for (const entry of entries.slice(0, 200))
|
||||
if (entry.isDirectory() && !entry.name.startsWith('.')) {
|
||||
const directory = path.join(config.root, entry.name);
|
||||
if ((await origin(directory)) === repositoryUrl) return directory;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
async function freeDirectory(input) {
|
||||
const name =
|
||||
(input.projectName ?? '')
|
||||
.normalize('NFKC')
|
||||
.toLowerCase()
|
||||
.replace(/[^\p{L}\p{N}._-]+/gu, '-')
|
||||
.replace(/^[-.]+|[-.]+$/g, '')
|
||||
.slice(0, 60) || 'project';
|
||||
for (const candidate of [name, `${name}-${input.projectId.slice(0, 8)}`]) {
|
||||
const directory = path.join(config.root, candidate);
|
||||
if (!(await exists(directory))) return directory;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
/** People commit on ide/work; a plain `git push` publishes it, `git pull origin <target>` updates it. */
|
||||
async function startWorkBranch(directory) {
|
||||
const published = await git(['rev-parse', '--verify', '--quiet', `refs/remotes/origin/${WORK_BRANCH}`], {
|
||||
cwd: directory,
|
||||
}).catch(() => '');
|
||||
await git(
|
||||
published
|
||||
? ['switch', '--create', WORK_BRANCH, '--track', `origin/${WORK_BRANCH}`]
|
||||
: ['switch', '--create', WORK_BRANCH],
|
||||
{ cwd: directory },
|
||||
);
|
||||
await git(['config', '--local', 'push.default', 'current'], { cwd: directory });
|
||||
await git(['config', '--local', 'push.autoSetupRemote', 'true'], { cwd: directory });
|
||||
}
|
||||
async function includeCredential(directory, file) {
|
||||
const included = await git(['config', '--local', '--get-all', 'include.path'], { cwd: directory }).catch(() => '');
|
||||
if (!included.split('\n').includes(file))
|
||||
await git(['config', '--local', '--add', 'include.path', file], { cwd: directory });
|
||||
}
|
||||
|
||||
return {
|
||||
/** Stores the delivered credential and clones the repository when the workspace lacks it. */
|
||||
async apply(payload) {
|
||||
const input = validate(payload, config);
|
||||
const file = path.join(config.credentialDir, `${input.projectId}.gitconfig`);
|
||||
await writeCredential(file, input);
|
||||
const pending = clones.get(input.repositoryUrl);
|
||||
if (pending) return { status: 'cloning', directory: pending.directory, cloning: pending.done };
|
||||
const existing = await findClone(input.repositoryUrl);
|
||||
if (existing) {
|
||||
await includeCredential(existing, file);
|
||||
return { status: 'ready', directory: existing };
|
||||
}
|
||||
const directory = await freeDirectory(input);
|
||||
if (!directory) return { status: 'conflict' };
|
||||
const done = (async () => {
|
||||
await git(['clone', '--origin', 'origin', '--branch', input.branch, '--', input.repositoryUrl, directory], {
|
||||
include: file,
|
||||
});
|
||||
await includeCredential(directory, file);
|
||||
await startWorkBranch(directory);
|
||||
})().finally(() => clones.delete(input.repositoryUrl));
|
||||
clones.set(input.repositoryUrl, { directory, done });
|
||||
// A failed clone is retried with the next delivery.
|
||||
done.catch(error => console.error(`WishForge could not clone the project: ${error.message}`));
|
||||
return { status: 'cloning', directory, cloning: done };
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { CREDENTIAL_PATH, WORK_BRANCH, GitWorkspaceError, readGitWorkspaceConfig, createGitWorkspace };
|
||||
@ -1,108 +0,0 @@
|
||||
'use strict';
|
||||
const fs = require('node:fs');
|
||||
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.');
|
||||
function treeDigest(root) {
|
||||
const files = [];
|
||||
function visit(directory, prefix = '') {
|
||||
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
||||
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
|
||||
const name = path.join(directory, entry.name);
|
||||
if (entry.isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED');
|
||||
if (entry.isDirectory()) visit(name, relative);
|
||||
else if (entry.isFile()) files.push([relative, sha256(fs.readFileSync(name))]);
|
||||
else throw new Error('EXTENSION_SPECIAL_FILE_REJECTED');
|
||||
}
|
||||
}
|
||||
visit(root);
|
||||
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');
|
||||
}
|
||||
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,
|
||||
},
|
||||
);
|
||||
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');
|
||||
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 (pythonLock) {
|
||||
const expected = pythonLock.platforms[platform];
|
||||
if (!expected) throw new Error('EXTENSION_PLATFORM_NOT_PINNED');
|
||||
const dependency = pythonLock.pythonDependency;
|
||||
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,
|
||||
},
|
||||
];
|
||||
// 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',
|
||||
`
|
||||
import hashlib,json,pathlib,stat,sys,zipfile
|
||||
root=pathlib.Path(sys.argv[1])
|
||||
for package in json.loads(sys.argv[2]):
|
||||
archive=root/'.downloads'/package['file']
|
||||
if archive.is_symlink() or archive.stat().st_size>67108864: raise ValueError('EXTENSION_ARCHIVE_REJECTED')
|
||||
if hashlib.sha256(archive.read_bytes()).hexdigest()!=package['sha256']: raise ValueError('EXTENSION_ARCHIVE_DIGEST_REJECTED')
|
||||
target=root/package['id']
|
||||
with zipfile.ZipFile(archive) as z:
|
||||
expected={i.filename:hashlib.sha256(z.read(i)).hexdigest() for i in z.infolist() if not i.is_dir()}
|
||||
actual={}
|
||||
for p in target.rglob('*'):
|
||||
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'] },
|
||||
);
|
||||
}
|
||||
return extensions;
|
||||
}
|
||||
module.exports = { treeDigest, assertIdentity, validateManagedPlugins, managedError };
|
||||
@ -1,561 +0,0 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"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"
|
||||
},
|
||||
"plugins": [
|
||||
{
|
||||
"directory": "ms-vscode.js-debug",
|
||||
"id": "ms-vscode.js-debug",
|
||||
"version": "1.105.0",
|
||||
"sha256": "47b974a9cf4f2d030efbbd00d21ad7c4c8b406dade21652aa95686427fd96a76"
|
||||
},
|
||||
{
|
||||
"directory": "ms-vscode.js-debug-companion",
|
||||
"id": "ms-vscode.js-debug-companion",
|
||||
"version": "1.1.3",
|
||||
"sha256": "3d567809ed536ad5ead4732f83b4433113f6bba0d56ee57ba80cdb7fc90e87b6"
|
||||
},
|
||||
{
|
||||
"directory": "ms-vscode.vscode-js-profile-table",
|
||||
"id": "ms-vscode.vscode-js-profile-table",
|
||||
"version": "1.0.10",
|
||||
"sha256": "6db1bdc953c0c055480eca4a867059ea36a71b2f28eb22631abe5d807d9d992a"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.bat",
|
||||
"id": "vscode.bat",
|
||||
"version": "1.108.2",
|
||||
"sha256": "049a52d9bd4934464eabe1318ccc164f734d0e080e39df9c02f54c32d6a1480a"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.builtin-notebook-renderers",
|
||||
"id": "vscode.builtin-notebook-renderers",
|
||||
"version": "1.108.2",
|
||||
"sha256": "28198f48d05ba683c4a1ce490377543f4c89fd65e44c18da0a1b9f3faa010f2b"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.clojure",
|
||||
"id": "vscode.clojure",
|
||||
"version": "1.108.2",
|
||||
"sha256": "5ba5793cf1797861aa58410079cc1a542784053bc9b038afd758db5336319dd5"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.coffeescript",
|
||||
"id": "vscode.coffeescript",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ef1d7e0149349a73550c7359c5d9186c406c941dba3771230ce82f9e8cd709cf"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.configuration-editing",
|
||||
"id": "vscode.configuration-editing",
|
||||
"version": "1.108.2",
|
||||
"sha256": "d5c82fa70a7ac06e53d55dd0ad943be42b7206da918887582701b63dd318ced9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.cpp",
|
||||
"id": "vscode.cpp",
|
||||
"version": "1.108.2",
|
||||
"sha256": "e890a89907e8bed3718962f7f56fea93cd639eb8d3f87078b24c4fc26767d30a"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.csharp",
|
||||
"id": "vscode.csharp",
|
||||
"version": "1.108.2",
|
||||
"sha256": "f70330001f788a62592b5d5d9639fdabddbffa616ece32fb3deed47edd15d926"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.css",
|
||||
"id": "vscode.css",
|
||||
"version": "1.108.2",
|
||||
"sha256": "e16cc9e6ba83af3b8b2dac7a58326d8d49dccf11d0ce4ddc450c3a529bc43b12"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.css-language-features",
|
||||
"id": "vscode.css-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "9c61d13d105e4fa35b6d430e3afde416e47e5691ca72cff84226a54a8a7a2925"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.dart",
|
||||
"id": "vscode.dart",
|
||||
"version": "1.108.2",
|
||||
"sha256": "65594c659e862f0eaebbcc1b68652ebe70e6c9d1a90340d078eb1697c1166913"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.debug-auto-launch",
|
||||
"id": "vscode.debug-auto-launch",
|
||||
"version": "1.108.2",
|
||||
"sha256": "408b192f4c5913b6fc3294ad661dabddea56d7c57ea8bbe10919bc06a9239660"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.debug-server-ready",
|
||||
"id": "vscode.debug-server-ready",
|
||||
"version": "1.108.2",
|
||||
"sha256": "8deb733ffbf4198cef3cbb73d6b5307de9ba6c18d8a4249432d71ba47d06b864"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.diff",
|
||||
"id": "vscode.diff",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ecbdda82e1072bce5d375a4b6d267da041a936c3cabfda3011dddb21e5a48e98"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.docker",
|
||||
"id": "vscode.docker",
|
||||
"version": "1.108.2",
|
||||
"sha256": "2a957acd235c9a5d3580e46389f16cb840a34d3a70f9d66df8c0691bcede878d"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.dotenv",
|
||||
"id": "vscode.dotenv",
|
||||
"version": "1.108.2",
|
||||
"sha256": "eb84848cc43176288f815c554ab03636a5d5472c6314e956041d95b2d7b0bf8f"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.emmet",
|
||||
"id": "vscode.emmet",
|
||||
"version": "1.108.2",
|
||||
"sha256": "b6b5dc5df353e4ecbca48aca599bfe51cdcba7f63b9fb0ef79b8022ac9a4ddf1"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.extension-editing",
|
||||
"id": "vscode.extension-editing",
|
||||
"version": "1.108.2",
|
||||
"sha256": "2885dabf8b502f45a402f500a4bd43b338cc9b64764d758ef250c8b1aad0bcf6"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.fsharp",
|
||||
"id": "vscode.fsharp",
|
||||
"version": "1.108.2",
|
||||
"sha256": "1167d5c4c0bf022aeacf859803d5552071d42cec848684546a68a4ebb99628a1"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.git",
|
||||
"id": "vscode.git",
|
||||
"version": "1.108.2",
|
||||
"sha256": "882cbb7556376f513629ed3fba1711c77b80890cb284646b6391518276609075"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.git-base",
|
||||
"id": "vscode.git-base",
|
||||
"version": "1.108.2",
|
||||
"sha256": "26d3c189c0890b8bff9ec56391cea74f378b815c5aedbd5ec583ca297ae23f55"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.github",
|
||||
"id": "vscode.github",
|
||||
"version": "1.108.2",
|
||||
"sha256": "616a6f2b139be59b00725aa28111512e11f8354d3433f84681a9dee54956f422"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.github-authentication",
|
||||
"id": "vscode.github-authentication",
|
||||
"version": "1.108.2",
|
||||
"sha256": "7f772b6f9241509efba786f4ff9184374740516084b2e3b1c231163f143da208"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.go",
|
||||
"id": "vscode.go",
|
||||
"version": "1.108.2",
|
||||
"sha256": "c848c972ae6d559fbfb10702e3dcf95ad94d16cd0edb2dde45dcc72054efa1e3"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.groovy",
|
||||
"id": "vscode.groovy",
|
||||
"version": "1.108.2",
|
||||
"sha256": "00ca5455163d0ec0dbc5d50fade93f94e2b93f1dc5bce3d22395508e256ec619"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.grunt",
|
||||
"id": "vscode.grunt",
|
||||
"version": "1.108.2",
|
||||
"sha256": "68bc5e71189089032abc187ff97666cc873b1cf6bfecd37a93721e8710633f86"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.gulp",
|
||||
"id": "vscode.gulp",
|
||||
"version": "1.108.2",
|
||||
"sha256": "fd073848831e8d3c148e4daf085e7f54fdc48bfc91a3136f6491df99f99f9c01"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.handlebars",
|
||||
"id": "vscode.handlebars",
|
||||
"version": "1.108.2",
|
||||
"sha256": "c9446c5192f64cb8cd70300577a69961ef83fab430898db36421d07b62660fdf"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.hlsl",
|
||||
"id": "vscode.hlsl",
|
||||
"version": "1.108.2",
|
||||
"sha256": "482db38e94f8505f12458b051793bb3d937c3e2e560e6adeaa31e41609396441"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.html",
|
||||
"id": "vscode.html",
|
||||
"version": "1.108.2",
|
||||
"sha256": "3420b09f3f1fd441dacf842041db14045584dea25b2d3f675453a9ba6fd9b599"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.html-language-features",
|
||||
"id": "vscode.html-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ccc11f2b9c7ba04f6f9f31c49e16587a165fd4f71a5d8345314d2509393b01bb"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.ini",
|
||||
"id": "vscode.ini",
|
||||
"version": "1.108.2",
|
||||
"sha256": "00f9170d7311b1a75484e63c2ac0abe374934bb38dff5a0c342153f93fc69164"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.ipynb",
|
||||
"id": "vscode.ipynb",
|
||||
"version": "1.108.2",
|
||||
"sha256": "d5be113eeaf2d9735044c5db42983a3f7c9f81b5fc0ce6c7864ff69f15ac5e27"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.jake",
|
||||
"id": "vscode.jake",
|
||||
"version": "1.108.2",
|
||||
"sha256": "9ff40dd4dcbb130f52b4a39cbf630fab468d9d5f1ef91d6b1ac4433ffda55a9c"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.java",
|
||||
"id": "vscode.java",
|
||||
"version": "1.108.2",
|
||||
"sha256": "af9b242d4bfcd33683013f3af4ceddae96324f6a242e9055718a431a157dca75"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.javascript",
|
||||
"id": "vscode.javascript",
|
||||
"version": "1.108.2",
|
||||
"sha256": "9936e0f60e9f6130edb3cc5c1a8727582519c29cb882ea53f8ab2728193695e5"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.json",
|
||||
"id": "vscode.json",
|
||||
"version": "1.108.2",
|
||||
"sha256": "0047b7b7e590fed1fd5a30c7d06d167c9fb23f9d6155a65e9b57a3b9703da68d"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.json-language-features",
|
||||
"id": "vscode.json-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "51971aef66c5af444e04042b6a3d6cebc7e20422d0c2cee4219b775caf567446"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.julia",
|
||||
"id": "vscode.julia",
|
||||
"version": "1.108.2",
|
||||
"sha256": "109bf99c4cbe2e02082d952d72da97984bf90bbfe1c5a53683040a1b98e18c82"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.latex",
|
||||
"id": "vscode.latex",
|
||||
"version": "1.108.2",
|
||||
"sha256": "8e664d4c99645338f2d85d7013270e072a494df4db61be027cfccafab3a1dcb4"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.less",
|
||||
"id": "vscode.less",
|
||||
"version": "1.108.2",
|
||||
"sha256": "10027163ff231ab5ad5b964f87538793dd4f0430ea71a0575ec66514c63dbe6e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.log",
|
||||
"id": "vscode.log",
|
||||
"version": "1.108.2",
|
||||
"sha256": "f48bcc16c41b1789496341d883141dc1105d458529390299ddd924909c5e31c8"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.lua",
|
||||
"id": "vscode.lua",
|
||||
"version": "1.108.2",
|
||||
"sha256": "2de7c36f55c75a9167f9a58ad17012c6567a87928a5bfdc0a42ea8839c8069c5"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.make",
|
||||
"id": "vscode.make",
|
||||
"version": "1.108.2",
|
||||
"sha256": "a4f6189f96df05bf08e2c6d8fa0e0d5eabe2e1d61056654fc5f8e5581f279937"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.markdown",
|
||||
"id": "vscode.markdown",
|
||||
"version": "1.108.2",
|
||||
"sha256": "d4b364c6e2edad2b1ae50ece5cee20b130aa94efc5f8f8eb1b696b0eda563ba9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.markdown-language-features",
|
||||
"id": "vscode.markdown-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ced48a41ed75021d1a4ea5afd7f51c08631f70f04e323d351d7260824083a7a6"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.markdown-math",
|
||||
"id": "vscode.markdown-math",
|
||||
"version": "1.108.2",
|
||||
"sha256": "b58711f19ecca7df14e13979ec675c1bdaa9f3968a6e756ff5c7105105dbb86e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.media-preview",
|
||||
"id": "vscode.media-preview",
|
||||
"version": "1.108.2",
|
||||
"sha256": "179e0778ac606e254f59018fde83ea9caf5f90e0f99ac0ecc347d5ad66af330c"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.merge-conflict",
|
||||
"id": "vscode.merge-conflict",
|
||||
"version": "1.108.2",
|
||||
"sha256": "a1b347a40c96cccf73a102808b768140bf5d3c1dfeddbfa1dca1da2b7f7f477b"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.mermaid-chat-features",
|
||||
"id": "vscode.mermaid-chat-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "b6d18d40c0b1e0b2585b99a0488b984d1b30a2a1140206ca34c9557165b78455"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.npm",
|
||||
"id": "vscode.npm",
|
||||
"version": "1.108.2",
|
||||
"sha256": "0f11c99fe30648d7c444361298749d0b8d5ff60472b5a0eabc3904d07397876e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.objective-c",
|
||||
"id": "vscode.objective-c",
|
||||
"version": "1.108.2",
|
||||
"sha256": "8319b21c30bb8fcb897687991165909e359ec0c075c1d014602f38da9865864a"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.perl",
|
||||
"id": "vscode.perl",
|
||||
"version": "1.108.2",
|
||||
"sha256": "5727730f86da49921388ea8276a1c11bb1438796c02815f980fd5239ebd2e430"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.php",
|
||||
"id": "vscode.php",
|
||||
"version": "1.108.2",
|
||||
"sha256": "70c19b21bc32837872145a0a1e76ee216b51d012321087f4ef7380e289d77715"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.php-language-features",
|
||||
"id": "vscode.php-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "01159d120c597f2663367277c1e9d4f8e9866a88c3c30776d48485d061bbd8ad"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.powershell",
|
||||
"id": "vscode.powershell",
|
||||
"version": "1.108.2",
|
||||
"sha256": "83d8cbf4801aaddee86ddd3c3f61a9fcfde74a1049497c8db4e890598beeffe6"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.prompt",
|
||||
"id": "vscode.prompt",
|
||||
"version": "1.108.2",
|
||||
"sha256": "a0d70ebb1f4189419dbb963e1024e0b3a5e7d977697b32b1d9686a9f315c8fa9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.pug",
|
||||
"id": "vscode.pug",
|
||||
"version": "1.108.2",
|
||||
"sha256": "288d461d6a7ec29b7ec2f4d11a3065690e9315c719e64cf1baa1dea542d1b57a"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.python",
|
||||
"id": "vscode.python",
|
||||
"version": "1.108.2",
|
||||
"sha256": "8683bf05b9c8ab797d2a75ddf23eee1f979d4272fbbcb5b0b963f62e16ea8f2f"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.r",
|
||||
"id": "vscode.r",
|
||||
"version": "1.108.2",
|
||||
"sha256": "a2f642e8905e67bb73737e457fa079bf76aad644fdaa2f1cdf0fd0ecdcfc73f5"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.razor",
|
||||
"id": "vscode.razor",
|
||||
"version": "1.108.2",
|
||||
"sha256": "6d24d115419c70dd22329d82d5a06b965c8f4c4890543edb11ff0bf4f71fa7bb"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.references-view",
|
||||
"id": "vscode.references-view",
|
||||
"version": "1.108.2",
|
||||
"sha256": "d2bdc0161a107eaa844c6bb2f4c287bb8a1319f7107370ee9f60a3621859e42c"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.restructuredtext",
|
||||
"id": "vscode.restructuredtext",
|
||||
"version": "1.108.2",
|
||||
"sha256": "19a9dbcd2fc1f7ac087dd3f056e4d69ea50aaac43195cb7762513245f39dd089"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.ruby",
|
||||
"id": "vscode.ruby",
|
||||
"version": "1.108.2",
|
||||
"sha256": "958e0bbe7c1cd09da68e7794abd85a99ed8fb0f588d7a3d73bf36b118a842caa"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.rust",
|
||||
"id": "vscode.rust",
|
||||
"version": "1.108.2",
|
||||
"sha256": "bb29d84d161754274f80f605b627cc11de67458b826fa13e0eebf30e12ab979e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.scss",
|
||||
"id": "vscode.scss",
|
||||
"version": "1.108.2",
|
||||
"sha256": "a66ee14fedc6225c672ddc73fd457034baf90f9fac23b2987d6404c8a937b737"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.search-result",
|
||||
"id": "vscode.search-result",
|
||||
"version": "1.108.2",
|
||||
"sha256": "4f579e97277485d9861554bdedb9cbecf761b37e70be74d5144c66affdc00d7e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.shaderlab",
|
||||
"id": "vscode.shaderlab",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ce9c866d5f4fff9d3c2e3fd4f5f5a2e0c789be7daf0db88387d74e884d0b29e8"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.shellscript",
|
||||
"id": "vscode.shellscript",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ac6048f34e998b969fda2698583983627b5a957db35270d3d056df7946f7b0f6"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.simple-browser",
|
||||
"id": "vscode.simple-browser",
|
||||
"version": "1.108.2",
|
||||
"sha256": "64a0ad33a16501227125ac2c994b668ba8772b213bb246fa60281b4aaba9a9f9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.sql",
|
||||
"id": "vscode.sql",
|
||||
"version": "1.108.2",
|
||||
"sha256": "fb9285cd41cff6e70da9a6f7017307be12cbf49abe06627854ea8eb254db1013"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.swift",
|
||||
"id": "vscode.swift",
|
||||
"version": "1.108.2",
|
||||
"sha256": "fc74ef0b507b235e07dd905b82e8efc69824f7f7dfdbbd12d6a5cd7b23bbece1"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.terminal-suggest",
|
||||
"id": "vscode.terminal-suggest",
|
||||
"version": "1.108.2",
|
||||
"sha256": "12b7713afd5028bc3978c652d555ec4737bc4d29d2c236318a60b20c5c7e2d80"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-abyss",
|
||||
"id": "vscode.theme-abyss",
|
||||
"version": "1.108.2",
|
||||
"sha256": "f4f5256d0a95c11442f8b758cae95c22240cc209e45f284ac9afe70b242cf4fa"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-defaults",
|
||||
"id": "vscode.theme-defaults",
|
||||
"version": "1.108.2",
|
||||
"sha256": "220a90a481dd4ffb0587d727ca02122f03d7befc26a6649d709f13e3a50c29f0"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-kimbie-dark",
|
||||
"id": "vscode.theme-kimbie-dark",
|
||||
"version": "1.108.2",
|
||||
"sha256": "eb1cf1a0362f82b6e23f5383cc3b691afb6a0d60d79955aa858263ecfbd797ec"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-monokai",
|
||||
"id": "vscode.theme-monokai",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ba7abbb9ab04ece745aced2269003d65316ee487638308fc0f1a08476a91de83"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-monokai-dimmed",
|
||||
"id": "vscode.theme-monokai-dimmed",
|
||||
"version": "1.108.2",
|
||||
"sha256": "00d5d056d6f3773c5314feaae1adae3c061a8188ba3231961f4a5707176f62e0"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-quietlight",
|
||||
"id": "vscode.theme-quietlight",
|
||||
"version": "1.108.2",
|
||||
"sha256": "f0f018f4de1a3f93de5a9ed40069dc8614d981339273f8bfc244c1f5cad8d2aa"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-red",
|
||||
"id": "vscode.theme-red",
|
||||
"version": "1.108.2",
|
||||
"sha256": "3065330894fdb026a9b6278108be00e5dbc71bfa218575b7b0db6f29f2d3d7be"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-solarized-dark",
|
||||
"id": "vscode.theme-solarized-dark",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ff0b771b8ac188e5025c9d2f79aff66dbb9092cee54fdad5c5ef55493e741576"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-solarized-light",
|
||||
"id": "vscode.theme-solarized-light",
|
||||
"version": "1.108.2",
|
||||
"sha256": "90d275f62c97c6db813f4b8328dfbb6a2416d3ffbe3086f5b6b0c336e0b129d9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.theme-tomorrow-night-blue",
|
||||
"id": "vscode.theme-tomorrow-night-blue",
|
||||
"version": "1.108.2",
|
||||
"sha256": "ec253677124f41a815d63dfa970f0a25a98bf805d9172c9e539b4ed037320c7e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.tunnel-forwarding",
|
||||
"id": "vscode.tunnel-forwarding",
|
||||
"version": "1.108.2",
|
||||
"sha256": "dc6f8419f6544455b42afd1b729b50d07705be1ce001007ddc208e2594397fb9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.typescript",
|
||||
"id": "vscode.typescript",
|
||||
"version": "1.108.2",
|
||||
"sha256": "879b9686a08845f43c2d9f89334f3bf747aaf9de9ea12fb3c3b161bc19e4ea48"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.typescript-language-features",
|
||||
"id": "vscode.typescript-language-features",
|
||||
"version": "1.108.2",
|
||||
"sha256": "819e93858b013f24a6e102a71e8e1922515b310071d11ccbb98a82be6ab99dd9"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.vb",
|
||||
"id": "vscode.vb",
|
||||
"version": "1.108.2",
|
||||
"sha256": "49b14b8e8668434f86fa997e1168a86168f92c237662af6d8b6c0d25790caf30"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.vscode-theme-seti",
|
||||
"id": "vscode.vscode-theme-seti",
|
||||
"version": "1.108.2",
|
||||
"sha256": "e0383c41fedc343b6b2e02ef6ba77e5edac78ce72c06ae45d84fd940f2b8975e"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.xml",
|
||||
"id": "vscode.xml",
|
||||
"version": "1.108.2",
|
||||
"sha256": "4656d86b85078ba1673a36050439013339aa58e855e7695a6ef201e9333d6e6c"
|
||||
},
|
||||
{
|
||||
"directory": "vscode.yaml",
|
||||
"id": "vscode.yaml",
|
||||
"version": "1.108.2",
|
||||
"sha256": "b03408b1cc8cb82c2c1961ea480e6b04d9a054ebc034af71f4d91cb29408aca6"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
{
|
||||
"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"
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
'use strict';
|
||||
const { injectable, inject } = require('@theia/core/shared/inversify');
|
||||
const { WidgetManager } = require('@theia/core/lib/browser/widget-manager');
|
||||
const { FrontendApplicationContribution } = require('@theia/core/lib/browser/frontend-application-contribution');
|
||||
const React = require('@theia/core/shared/react');
|
||||
class ManagedExtensionsNotice {
|
||||
onStart() {
|
||||
const label = widget => {
|
||||
if (widget.id !== 'vsx-extensions-view-container') return;
|
||||
widget.title.label = 'Extensions · 管理员管理';
|
||||
widget.title.caption = '扩展由管理员固定版本安装。可查看已安装扩展;安装、更新与移除须通过平台镜像发布。';
|
||||
// ViewContainer restores its title from saved layout/part titles. Put the
|
||||
// durable notice in the extension search widget as well as the caption.
|
||||
if (!widget.searchBar || widget.searchBar.wishforgeManagedNotice) return;
|
||||
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.update();
|
||||
};
|
||||
this.widgets.getWidgets('vsx-extensions-view-container').forEach(label);
|
||||
this.widgets.onDidCreateWidget(({ widget }) => label(widget));
|
||||
}
|
||||
}
|
||||
injectable()(ManagedExtensionsNotice);
|
||||
inject(WidgetManager)(ManagedExtensionsNotice.prototype, 'widgets');
|
||||
function bindManagedExtensionNotice(bind) {
|
||||
bind(ManagedExtensionsNotice).toSelf().inSingletonScope();
|
||||
bind(FrontendApplicationContribution).toService(ManagedExtensionsNotice);
|
||||
}
|
||||
module.exports = { bindManagedExtensionNotice };
|
||||
@ -1,72 +0,0 @@
|
||||
'use strict';
|
||||
const path = require('node:path');
|
||||
const { injectable } = require('@theia/core/shared/inversify');
|
||||
const { PluginServer, PluginDeployer, PluginType } = require('@theia/plugin-ext/lib/common/plugin-protocol');
|
||||
const { PluginServerImpl } = require('@theia/plugin-ext/lib/main/node/plugin-server-impl');
|
||||
const { PluginDeployerImpl } = require('@theia/plugin-ext/lib/main/node/plugin-deployer-impl');
|
||||
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();
|
||||
}
|
||||
}
|
||||
injectable()(ManagedPluginServer);
|
||||
class ManagedPluginDeployer extends PluginDeployerImpl {
|
||||
async doStart() {
|
||||
if (!process.env.WF_IDE_MANAGED_PLUGINS_DIR) throw new Error('MANAGED_EXTENSIONS_UNCONFIGURED');
|
||||
const root = path.resolve(process.env.WF_IDE_MANAGED_PLUGINS_DIR);
|
||||
const allowed = validateManagedPlugins(root);
|
||||
await this.initResolvers();
|
||||
// Do not consult THEIA_PLUGINS, user directories, extension recommendations
|
||||
// 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)
|
||||
) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
injectable()(ManagedPluginDeployer);
|
||||
function bindManagedPlugins(rebind) {
|
||||
rebind(PluginServer).to(ManagedPluginServer).inSingletonScope();
|
||||
rebind(PluginDeployer).to(ManagedPluginDeployer).inSingletonScope();
|
||||
}
|
||||
module.exports = { ManagedPluginServer, ManagedPluginDeployer, bindManagedPlugins };
|
||||
@ -1,191 +0,0 @@
|
||||
'use strict';
|
||||
require('reflect-metadata');
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { execFileSync } = require('node:child_process');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { createGitWorkspace, readGitWorkspaceConfig } = require('../git-workspace.cjs');
|
||||
|
||||
const git = (cwd, ...args) =>
|
||||
execFileSync('git', args, {
|
||||
cwd,
|
||||
encoding: 'utf8',
|
||||
env: { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' },
|
||||
}).trim();
|
||||
|
||||
function fixture(t) {
|
||||
const base = fs.mkdtempSync(path.join(os.tmpdir(), 'wishforge-git-workspace-'));
|
||||
t.after(() => fs.rmSync(base, { recursive: true, force: true }));
|
||||
const source = path.join(base, 'p1.git');
|
||||
git(base, 'init', '--quiet', '--bare', '--initial-branch=main', source);
|
||||
const seed = path.join(base, 'seed');
|
||||
git(base, 'clone', '--quiet', source, seed);
|
||||
fs.writeFileSync(path.join(seed, 'README.md'), '# Todo\n');
|
||||
git(seed, 'add', 'README.md');
|
||||
git(seed, '-c', 'user.name=Seed', '-c', 'user.email=seed@example.com', 'commit', '--quiet', '-m', 'Initial');
|
||||
git(seed, 'push', '--quiet', 'origin', 'HEAD:main');
|
||||
const root = path.join(base, 'workspace');
|
||||
fs.mkdirSync(root);
|
||||
const config = { root, credentialDir: path.join(base, 'credentials'), remote: `file://${source}` };
|
||||
const delivery = (overrides = {}) => ({
|
||||
projectId: 'p1',
|
||||
projectName: 'Todo App',
|
||||
repositoryUrl: config.remote,
|
||||
branch: 'main',
|
||||
token: 'payload.signature-1',
|
||||
expiresAt: new Date(Date.now() + 900_000).toISOString(),
|
||||
author: { name: 'Ada Lovelace', email: 'ada@example.com' },
|
||||
...overrides,
|
||||
});
|
||||
return { base, source, root, config, delivery };
|
||||
}
|
||||
|
||||
test('the first delivery clones the target branch and keeps the token out of the repository', async t => {
|
||||
const f = fixture(t);
|
||||
const workspace = createGitWorkspace(f.config);
|
||||
const result = await workspace.apply(f.delivery());
|
||||
assert.equal(result.status, 'cloning');
|
||||
await result.cloning;
|
||||
const clone = path.join(f.root, 'todo-app');
|
||||
assert.equal(result.directory, clone);
|
||||
assert.equal(fs.readFileSync(path.join(clone, 'README.md'), 'utf8'), '# Todo\n');
|
||||
const credential = path.join(f.config.credentialDir, 'p1.gitconfig');
|
||||
assert.equal(fs.statSync(credential).mode & 0o777, 0o600);
|
||||
assert.doesNotMatch(fs.readFileSync(path.join(clone, '.git/config'), 'utf8'), /payload\.signature/);
|
||||
assert.equal(
|
||||
git(clone, 'config', `http.${f.config.remote}.extraHeader`),
|
||||
'Authorization: Bearer payload.signature-1',
|
||||
);
|
||||
assert.equal(git(clone, 'config', 'user.name'), 'Ada Lovelace');
|
||||
assert.equal(git(clone, 'config', 'user.email'), 'ada@example.com');
|
||||
});
|
||||
|
||||
test('later deliveries only refresh the token, even after the project was renamed', async t => {
|
||||
const f = fixture(t);
|
||||
const workspace = createGitWorkspace(f.config);
|
||||
await (
|
||||
await workspace.apply(f.delivery())
|
||||
).cloning;
|
||||
const result = await workspace.apply(
|
||||
f.delivery({ projectName: 'Renamed', token: 'payload.signature-2', author: undefined }),
|
||||
);
|
||||
assert.deepEqual(result, { status: 'ready', directory: path.join(f.root, 'todo-app') });
|
||||
assert.equal(
|
||||
git(result.directory, 'config', `http.${f.config.remote}.extraHeader`),
|
||||
'Authorization: Bearer payload.signature-2',
|
||||
);
|
||||
// A delivery without an author keeps the last one.
|
||||
assert.equal(git(result.directory, 'config', 'user.name'), 'Ada Lovelace');
|
||||
assert.equal(git(result.directory, 'config', '--get-all', 'include.path').split('\n').length, 1);
|
||||
});
|
||||
|
||||
test('people commit on ide/work, and a plain push publishes it without touching the target branch', async t => {
|
||||
const f = fixture(t);
|
||||
const workspace = createGitWorkspace(f.config);
|
||||
const { directory, cloning } = await workspace.apply(f.delivery());
|
||||
await cloning;
|
||||
assert.equal(git(directory, 'branch', '--show-current'), 'ide/work');
|
||||
const target = git(f.source, 'rev-parse', 'main');
|
||||
git(directory, 'config', 'user.name', 'Grace Hopper');
|
||||
fs.writeFileSync(path.join(directory, 'notes.md'), 'edited in the IDE\n');
|
||||
git(directory, 'add', 'notes.md');
|
||||
git(directory, 'commit', '--quiet', '-m', 'Edit in the IDE');
|
||||
git(directory, 'push', '--quiet');
|
||||
assert.equal(
|
||||
git(f.source, 'log', '-1', '--format=%an <%ae> %s', 'ide/work'),
|
||||
'Grace Hopper <ada@example.com> Edit in the IDE',
|
||||
);
|
||||
assert.equal(git(f.source, 'rev-parse', 'main'), target);
|
||||
// A later clone, for example of a recreated workspace, continues the published branch.
|
||||
fs.rmSync(directory, { recursive: true });
|
||||
const again = await workspace.apply(f.delivery());
|
||||
await again.cloning;
|
||||
assert.equal(git(again.directory, 'rev-parse', 'HEAD'), git(f.source, 'rev-parse', 'ide/work'));
|
||||
assert.equal(git(again.directory, 'rev-parse', '--abbrev-ref', '@{upstream}'), 'origin/ide/work');
|
||||
});
|
||||
|
||||
test('an existing folder with the project’s name is never overwritten', async t => {
|
||||
const f = fixture(t);
|
||||
fs.mkdirSync(path.join(f.root, 'todo-app'));
|
||||
fs.writeFileSync(path.join(f.root, 'todo-app', 'mine.txt'), 'keep\n');
|
||||
const workspace = createGitWorkspace(f.config);
|
||||
const result = await workspace.apply(f.delivery());
|
||||
await result.cloning;
|
||||
assert.equal(result.directory, path.join(f.root, 'todo-app-p1'));
|
||||
assert.equal(fs.readFileSync(path.join(f.root, 'todo-app', 'mine.txt'), 'utf8'), 'keep\n');
|
||||
fs.mkdirSync(path.join(f.root, 'other'));
|
||||
fs.rmSync(result.directory, { recursive: true });
|
||||
fs.mkdirSync(result.directory);
|
||||
assert.deepEqual(await workspace.apply(f.delivery()), { status: 'conflict' });
|
||||
});
|
||||
|
||||
test('deliveries for another repository or with unexpected content are refused', async t => {
|
||||
const f = fixture(t);
|
||||
const workspace = createGitWorkspace(f.config);
|
||||
for (const payload of [
|
||||
f.delivery({ repositoryUrl: 'https://git.example/git/tenant/other.git' }),
|
||||
f.delivery({ branch: '../main' }),
|
||||
f.delivery({ token: 'not a token' }),
|
||||
f.delivery({ author: { name: 'Eve <eve@example.com>', email: 'eve@example.com' } }),
|
||||
{ ...f.delivery(), command: 'rm -rf /' },
|
||||
])
|
||||
await assert.rejects(workspace.apply(payload), { code: 'INVALID_GIT_CREDENTIAL' });
|
||||
assert.deepEqual(fs.readdirSync(f.root), []);
|
||||
|
||||
const shared = createGitWorkspace({ ...f.config, remote: undefined, origin: 'http://127.0.0.1:4504' });
|
||||
await assert.rejects(
|
||||
shared.apply(f.delivery({ repositoryUrl: 'http://127.0.0.1:4504/git/demo/p2.git' })),
|
||||
{ code: 'INVALID_GIT_CREDENTIAL' },
|
||||
'the repository must belong to the delivered project',
|
||||
);
|
||||
await assert.rejects(shared.apply(f.delivery({ repositoryUrl: 'http://127.0.0.1:9999/git/demo/p1.git' })), {
|
||||
code: 'INVALID_GIT_CREDENTIAL',
|
||||
});
|
||||
});
|
||||
|
||||
test('the endpoint is only configured for a workspace with Git settings', () => {
|
||||
assert.equal(readGitWorkspaceConfig({ WISHFORGE_WORKSPACE_PATH: '/w' }), undefined);
|
||||
assert.deepEqual(
|
||||
readGitWorkspaceConfig({
|
||||
WISHFORGE_WORKSPACE_PATH: '/w',
|
||||
WISHFORGE_GIT_CREDENTIAL_DIR: '/run/wishforge/git',
|
||||
WISHFORGE_GIT_REMOTE: 'https://git.example/git/t/p1.git',
|
||||
}),
|
||||
{ root: '/w', credentialDir: '/run/wishforge/git', remote: 'https://git.example/git/t/p1.git' },
|
||||
);
|
||||
const { GitCredentialEndpoint } = require('../git-credential-endpoint.cjs');
|
||||
const routes = [];
|
||||
const app = { post: (...args) => routes.push(args) };
|
||||
const keys = [
|
||||
'WISHFORGE_WORKSPACE_PATH',
|
||||
'WISHFORGE_GIT_CREDENTIAL_DIR',
|
||||
'WISHFORGE_GIT_REMOTE',
|
||||
'WISHFORGE_GIT_ORIGIN',
|
||||
];
|
||||
const previous = Object.fromEntries(keys.map(key => [key, process.env[key]]));
|
||||
try {
|
||||
delete process.env.WISHFORGE_GIT_REMOTE;
|
||||
delete process.env.WISHFORGE_GIT_ORIGIN;
|
||||
new GitCredentialEndpoint().configure(app);
|
||||
assert.equal(routes.length, 0);
|
||||
Object.assign(process.env, {
|
||||
WISHFORGE_WORKSPACE_PATH: '/w',
|
||||
WISHFORGE_GIT_CREDENTIAL_DIR: '/run/wishforge/git',
|
||||
WISHFORGE_GIT_ORIGIN: 'http://127.0.0.1:4504',
|
||||
});
|
||||
const endpoint = new GitCredentialEndpoint();
|
||||
let validated = false;
|
||||
endpoint.connectionValidator = { validateRequest: () => (validated = true) };
|
||||
endpoint.configure(app);
|
||||
assert.equal(routes.length, 1);
|
||||
assert.equal(routes[0][0], '/wishforge-internal/git-credential');
|
||||
routes[0][1]({}, {}, () => {});
|
||||
assert.equal(validated, true, 'Theia’s connection token is checked first');
|
||||
} finally {
|
||||
for (const key of keys)
|
||||
if (previous[key] === undefined) delete process.env[key];
|
||||
else process.env[key] = previous[key];
|
||||
}
|
||||
});
|
||||
@ -1,91 +0,0 @@
|
||||
'use strict';
|
||||
require('reflect-metadata');
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { treeDigest, validateManagedPlugins } = require('../managed-plugins.cjs');
|
||||
const { ManagedPluginServer, ManagedPluginDeployer } = require('../plugin-install-policy.cjs');
|
||||
|
||||
function fixture(t) {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'wishforge-managed-plugins-'));
|
||||
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/main.js'), 'exports.activate = () => {};\n');
|
||||
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'));
|
||||
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();');
|
||||
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' }),
|
||||
);
|
||||
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'));
|
||||
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',
|
||||
]) {
|
||||
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/);
|
||||
});
|
||||
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',
|
||||
]) {
|
||||
await assert.rejects(deployer[method]({ id: 'local-dir:/tmp/evil', type: 0 }), /EXTENSIONS_MANAGED_BY_ADMIN/);
|
||||
}
|
||||
});
|
||||
test('installed plugin reads still reach the normal Theia implementation', async () => {
|
||||
const server = new ManagedPluginServer();
|
||||
server.pluginDeployerHandler = { getDeployedPluginIds: () => ['test.approved@1.0.0'] };
|
||||
assert.deepEqual(await server.getInstalledPlugins(), ['test.approved@1.0.0']);
|
||||
});
|
||||
@ -1,43 +0,0 @@
|
||||
// Operator-only lockfile maintenance. Input must be the exact official release
|
||||
// archive; this never signs or approves an arbitrary installed runtime directory.
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
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');
|
||||
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',
|
||||
`
|
||||
import hashlib,io,json,pathlib,sys,tarfile,zipfile
|
||||
plugins=[]
|
||||
with tarfile.open(sys.argv[1],'r:gz') as tar:
|
||||
for item in tar.getmembers():
|
||||
if item.isdir(): continue
|
||||
if not item.isfile() or not item.name.endswith('.vsix'): raise ValueError('Unexpected release entry')
|
||||
with zipfile.ZipFile(io.BytesIO(tar.extractfile(item).read())) as z:
|
||||
manifest=json.loads(z.read('extension/package.json'))
|
||||
files=sorted((i.filename,hashlib.sha256(z.read(i)).hexdigest()) for i in z.infolist() if not i.is_dir())
|
||||
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 },
|
||||
),
|
||||
);
|
||||
console.log(JSON.stringify({ schemaVersion: 1, source, plugins }, null, 2));
|
||||
@ -1,180 +0,0 @@
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { readFile, writeFile, mkdir, rename, rm, stat } from 'node:fs/promises';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
import { promisify } from 'node:util';
|
||||
|
||||
const lock = JSON.parse(await readFile(new URL('./python-debugger.lock.json', import.meta.url), 'utf8'));
|
||||
const maxBytes = 64 * 1024 * 1024;
|
||||
const sha256 = bytes => createHash('sha256').update(bytes).digest('hex');
|
||||
const run = promisify(execFile);
|
||||
|
||||
export async function installPythonDebugger({
|
||||
platform = `${process.platform}-${process.arch}`,
|
||||
archive,
|
||||
pythonArchive,
|
||||
pyrightArchive,
|
||||
directory = fileURLToPath(new URL('../plugins/', import.meta.url)),
|
||||
fetcher = fetch,
|
||||
} = {}) {
|
||||
const expected = lock.platforms[platform];
|
||||
if (!expected) throw new Error(`Python debugger package is not pinned for ${platform}`);
|
||||
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,
|
||||
});
|
||||
const debuggerExtension = await installArchive({
|
||||
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,
|
||||
});
|
||||
return { version: lock.version, platform, python, debugger: debuggerExtension, languageServer };
|
||||
}
|
||||
|
||||
async function installArchive({ expected, filename, url, archive, directory, fetcher, id, version }) {
|
||||
const target = join(directory, '.downloads', filename);
|
||||
let bytes;
|
||||
let cached = false;
|
||||
if (!archive) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (archive) {
|
||||
if ((await stat(archive)).size > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB');
|
||||
bytes = await readFile(archive);
|
||||
} 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');
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of response.body) {
|
||||
size += chunk.length;
|
||||
if (size > maxBytes) throw new Error('Python debugger archive exceeds 64 MiB');
|
||||
chunks.push(chunk);
|
||||
}
|
||||
bytes = Buffer.concat(chunks);
|
||||
}
|
||||
if (sha256(bytes) !== expected) throw new Error('Python debugger checksum mismatch; nothing was installed');
|
||||
await mkdir(dirname(target), { recursive: true });
|
||||
const temporary = `${target}.${randomUUID()}.partial`;
|
||||
try {
|
||||
await writeFile(temporary, bytes, { flag: 'wx', mode: 0o644 });
|
||||
await rename(temporary, target);
|
||||
} 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()}`);
|
||||
const plugin = join(directory, id);
|
||||
const previous = `${unpacked}-previous`;
|
||||
let moved = false;
|
||||
try {
|
||||
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:
|
||||
entries=z.infolist()
|
||||
if len(entries)>100000 or sum(i.file_size for i in entries)>268435456: raise ValueError('Plugin extraction size limit')
|
||||
for item in entries:
|
||||
p=pathlib.PurePosixPath(item.filename)
|
||||
if p.is_absolute() or '..' in p.parts or '\\\\' in item.filename or stat.S_ISLNK(item.external_attr>>16): raise ValueError('Unsafe plugin archive path')
|
||||
manifest=json.loads(z.read('extension/package.json'))
|
||||
if manifest.get('publisher')+'.'+manifest.get('name')!=expected_id or manifest.get('version')!=expected_version: raise ValueError('Plugin identity mismatch')
|
||||
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;
|
||||
}
|
||||
await rm(previous, { recursive: true, force: true });
|
||||
} finally {
|
||||
await rm(unpacked, { recursive: true, force: true });
|
||||
}
|
||||
return { target: plugin, archive: target, sha256: expected, cached };
|
||||
}
|
||||
|
||||
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]',
|
||||
);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
{
|
||||
"id": "ms-python.debugpy",
|
||||
"version": "2026.6.0",
|
||||
"vscodeEngine": "^1.92.0",
|
||||
"source": "https://open-vsx.org/extension/ms-python/debugpy/2026.6.0",
|
||||
"pythonDependency": {
|
||||
"id": "ms-python.python",
|
||||
"version": "2026.4.0",
|
||||
"vscodeEngine": "^1.95.0",
|
||||
"url": "https://open-vsx.org/api/ms-python/python/2026.4.0/file/ms-python.python-2026.4.0.vsix",
|
||||
"sha256": "232aeafb01f069824fdd92d3e628c1c442bbcfa1d3cc945ff97076340bb2b4a6"
|
||||
},
|
||||
"languageServer": {
|
||||
"id": "ms-pyright.pyright",
|
||||
"version": "1.1.402",
|
||||
"vscodeEngine": "^1.99.0",
|
||||
"url": "https://open-vsx.org/api/ms-pyright/pyright/1.1.402/file/ms-pyright.pyright-1.1.402.vsix",
|
||||
"sha256": "7ffa5a8649fa800b282c25ad70ce290367e036238d52727da7cdcac3d57439e9"
|
||||
},
|
||||
"platforms": {
|
||||
"darwin-arm64": "3425b5251b0f0642b569e64397effe72ab141d138abd966b567041baa5609b4b",
|
||||
"darwin-x64": "7a87aeba6a4dc1f46cfb8e6d457e53256c80b5be75a1781d51d3e0ad5dbb1574",
|
||||
"linux-x64": "c7744af4bf72978f5792624a71c80e2b622a1118574fada3a903d70ac03d5bca",
|
||||
"linux-arm64": "4e7aa92b0459c1010ee010a79b35ecc16328b943a84589bd9e8ead3d4eb0010b"
|
||||
}
|
||||
}
|
||||
@ -1,51 +0,0 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { existsSync, mkdirSync } from 'node:fs';
|
||||
|
||||
const appRoot = fileURLToPath(new URL('../', import.meta.url));
|
||||
const cli = fileURLToPath(new URL('../node_modules/@theia/cli/bin/theia.js', import.meta.url));
|
||||
const production = process.env.NODE_ENV === 'production';
|
||||
if (!existsSync(cli) || !existsSync(`${appRoot}lib/frontend/index.html`)) {
|
||||
// Theia releases may emit frontend assets under lib; use backend generation
|
||||
// as the authoritative build prerequisite below.
|
||||
if (!existsSync(`${appRoot}src-gen/backend/main.js`)) {
|
||||
console.error('Install and build this independent package first: npm install && npm run build');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (production && process.env.WISHFORGE_GATEWAY_ENFORCED !== 'true') {
|
||||
console.error('Production requires an authenticated workspace gateway. See README.md.');
|
||||
process.exit(1);
|
||||
}
|
||||
const port = process.env.WISHFORGE_IDE_PORT ?? '4300';
|
||||
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535) throw new Error('Invalid WISHFORGE_IDE_PORT');
|
||||
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/',
|
||||
},
|
||||
},
|
||||
);
|
||||
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));
|
||||
@ -1,5 +0,0 @@
|
||||
import { createRequire } from 'node:module';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
const { validateManagedPlugins } = createRequire(import.meta.url)('../policy/managed-plugins.cjs');
|
||||
const plugins = validateManagedPlugins(fileURLToPath(new URL('../plugins', import.meta.url)));
|
||||
console.log(`Verified ${plugins.length} operator-managed extensions against pinned IDs, versions and content digests.`);
|
||||
@ -1,11 +1,11 @@
|
||||
# Provisioner
|
||||
|
||||
为每个运行代次创建执行环境(一个 executor、一个 verifier),并为云 IDE 创建工作区。控制面的派发器通过 HTTP 调用它;它本身不访问数据库,也不持有令牌签名密钥。
|
||||
为每个运行代次创建执行环境(一个 executor、一个 verifier)。控制面的派发器通过 HTTP 调用它;它本身不访问数据库,也不持有令牌签名密钥。
|
||||
|
||||
| 后端 | `WF_PROVISIONER_BACKEND` | 用途 |
|
||||
|---|---|---|
|
||||
| `local` | `local` | 本地开发。为每个代次在本机启动两个 worker 进程(`WF_SANDBOX_MODE=host`),**没有隔离**;只在 `WF_DEPLOYMENT_MODE=development` 下启动。所有工作区指向同一个本机 Theia。 |
|
||||
| `kubernetes` | `kubernetes` | 生产。用 `kubectl --context … --namespace …` 创建 Kata Job、PVC、Secret、ServiceAccount 与 NetworkPolicy,IDE 工作区为独立的 Deployment。 |
|
||||
| `local` | `local` | 本地开发。为每个代次在本机启动两个 worker 进程(`WF_SANDBOX_MODE=host`),**没有隔离**;只在 `WF_DEPLOYMENT_MODE=development` 下启动。 |
|
||||
| `kubernetes` | `kubernetes` | 生产。用 `kubectl --context … --namespace …` 创建 Kata Job、PVC、Secret、ServiceAccount 与 NetworkPolicy。 |
|
||||
|
||||
## 接口
|
||||
|
||||
@ -16,8 +16,6 @@
|
||||
| `POST /runs` | `{tenantId, runId, generation, executorToken, verifierToken, resourceGrant}` | 为该代次启动 executor 与 verifier。同一代次重复请求不会重复启动。返回 `{status: "provisioned", …}`。 |
|
||||
| `POST /runs/release` | `{tenantId, runId, generation, resourceGrant}` | 停止该代次的计算资源。返回 `{status: "released", computeReleased, persistentStorageRetained}`,控制面收到后才归还并发名额。 |
|
||||
| `POST /runs/purge` | 同上(使用最近一个代次的授权,允许过期) | 删除该运行所有代次保留的卷(本地后端删除运行目录)。运行仍有活动的 Pod 或进程时返回 `{status: "busy"}`,否则返回 `{status: "purged", persistentStorageRetained: false}`。控制面在保留期过后调用。 |
|
||||
| `POST /workspaces` | `{tenantId, projectId, workspaceId}` | 创建或返回工作区,`{status: "ready" \| "pending", upstreamUrl}`;`local` 后端另返回 `lifecycle: "unmanaged"`(不支持暂停/恢复)。 |
|
||||
| `GET/PUT /workspaces/:id/lifecycle` | — | 仅 `kubernetes` 后端:观察与调整工作区的暂停/运行状态。 |
|
||||
| `GET /healthz` | — | `{status: "ok", service: "wishforge-provisioner", backend}` |
|
||||
|
||||
资源授权(`resourceGrant`)由 API 用 `WF_RESOURCE_GRANT_SECRET` 做 HMAC 签名,绑定租户、运行、代次、套餐、CPU/内存/存储与运行镜像,有效期 5 分钟。两个后端都校验签名、租户与有效期;`kubernetes` 后端还要求镜像在当前或上一版的允许列表内,并按授权限制资源(两个角色各一半)。
|
||||
@ -35,7 +33,6 @@ executor 与 verifier 各自只拿到自己的运行级令牌(由 API 签发
|
||||
| `WF_TENANT_ID` | 本服务的唯一租户(本地为 `demo`) |
|
||||
| `WF_RESOURCE_GRANT_SECRET` | 与 API 共享的资源授权签名密钥 |
|
||||
| `WF_LOCAL_RUN_ROOT` | 运行目录,默认 `~/.local/share/wishforge/runs`,必须位于 WishForge 源码目录之外;每个代次为 `<租户>/<运行>/<代次>/{executor,verifier}` |
|
||||
| `WF_LOCAL_IDE_URL` | 本机 Theia 地址(`npm run dev:ide` 启动在 `http://127.0.0.1:4300/`);能访问时工作区才变为 ready |
|
||||
| `WF_CONTROL_PLANE_URL`、`TEMPORAL_ADDRESS`、`TEMPORAL_NAMESPACE` | 传给 worker 进程 |
|
||||
|
||||
worker 进程只继承 `PATH`、`HOME`、`LANG`、`TMPDIR`、代理变量、`WF_GIT_ALLOW_LOCAL_HTTP`、`WF_CODEBUDDY_BIN`、`WF_CLAUDE_BIN`、`WF_DOCKER_CONFIG` 与 Temporal 的 TLS 设置,拿不到数据库、签名密钥等其他配置。代次结束后进程被停止,目录保留在磁盘上便于排查,保留期过后由控制面清理(同 `POST /runs/purge`)。provisioner 重启时会停止它启动的进程;仍在进行的运行会在 5 分钟后进入 `worker_unavailable` 等待,点击「继续」即可用新的代次重新执行。
|
||||
@ -51,7 +48,7 @@ worker 进程只继承 `PATH`、`HOME`、`LANG`、`TMPDIR`、代理变量、`WF_
|
||||
| `WF_RUN_WORKER_IMAGE` | worker 镜像,必须是 `仓库/镜像@sha256:…` |
|
||||
| `WF_RUN_PREVIOUS_IMAGES` | 可选,仍允许运行的上一版镜像(逗号分隔) |
|
||||
| `WF_RUN_STORAGE_CLASS` | PVC 存储类 |
|
||||
| `WF_RUN_STORAGE_SIZE` | 单个运行的存储上限(两个角色各一半)与 IDE 工作区卷的大小,默认 `10Gi` |
|
||||
| `WF_RUN_STORAGE_SIZE` | 单个运行的存储上限(两个角色各一半),默认 `10Gi` |
|
||||
| `WF_CONTROL_PLANE_URL` | API 的 HTTPS 地址 |
|
||||
| `WF_CONTROL_NAMESPACE` | 控制面命名空间(网络策略放行其中的 API、Temporal 与平台 Git) |
|
||||
| `WF_GIT_SERVICE_PORT` | 平台 Git 服务端口,默认 4504 |
|
||||
@ -63,21 +60,9 @@ worker 进程只继承 `PATH`、`HOME`、`LANG`、`TMPDIR`、代理变量、`WF_
|
||||
| `WF_VERIFIER_DOCKER_CONFIG_SECRET` | 可选,仅 verifier 挂载的镜像仓库凭据(`DOCKER_CONFIG`) |
|
||||
| `WF_VERIFIER_DOCKER_HOST`、`WF_VERIFIER_DOCKER_TLS_SECRET`、`WF_VERIFIER_BUILDER_NAMESPACE` | 可选,远程构建器(`tcp://主机:端口`,Pod 标签 `wishforge.io/component: image-builder`)及其 TLS 客户端证书(`ca.pem`、`cert.pem`、`key.pem`,生产必填)和所在命名空间(默认控制面命名空间);只有 verifier 能连到它 |
|
||||
| `WF_RUN_INSTALL_PROXY_URL`、`WF_RUN_INSTALL_PROXY_NAMESPACE` | 可选,依赖安装使用的出站代理(`http(s)://主机:端口`,Pod 标签 `wishforge.io/component: egress-proxy`)及其命名空间(默认控制面命名空间) |
|
||||
| `WF_THEIA_IMAGE` | IDE 工作区镜像(`apps/ide/Dockerfile` 默认目标),必须是 digest 引用 |
|
||||
| `WF_OCT_WORKSPACE_IMAGE`、`WF_IDE_PUBLIC_URL` | 可选,协作 sidecar 镜像(digest)与网关的公开 HTTPS 源站(与网关的 `WF_IDE_PUBLIC_URL` 相同);`WF_OCT_WORKSPACE_IMAGE` 配置后工作区启用协作。本地开发集群(`WF_ALLOW_INSECURE_HTTP=true`)中网关地址与工作区的平台 Git 地址可以是 HTTP |
|
||||
|
||||
每个代次创建:两个 RWO PVC、两个能力 Secret(各含一个角色的令牌)、两个不挂载令牌的 ServiceAccount、两个 Kata Job(`backoffLimit: 0`,非 root,只读根文件系统,丢弃全部 capability)以及一条 NetworkPolicy。新代次开始前只删除同一运行的旧 Job。`POST /runs/release` 删除属于该代次标签的 Job,Pod 全部结束后再删除该代次的 Secret、ServiceAccount 与 NetworkPolicy。PVC 保留用于排查,保留期过后由控制面通过 `POST /runs/purge` 按运行标签删除。
|
||||
|
||||
每个 IDE 工作区创建一个 PVC、一个 Kata Deployment(Theia,可选 OCT sidecar)、一个 Service 与一条 NetworkPolicy(只允许控制面中 `wishforge.io/component: workspace-gateway` 的 Pod 访问 3000/8100 端口,出站只有 DNS)。所需的 RBAC 与命名空间基线见 [infra/k8s](../../infra/k8s/README.md)。
|
||||
|
||||
请求带 `repositoryUrl`(API 只为平台托管仓库发送,地址必须是 HTTPS 的 `/git/<租户>/<项目>.git`)时:
|
||||
|
||||
- Theia 收到 `WISHFORGE_GIT_REMOTE`,只接受这个仓库的凭据;
|
||||
- 内存卷挂载为 `WISHFORGE_GIT_CREDENTIAL_DIR`(`/run/wishforge/git`),令牌只存在这里;
|
||||
- 网络策略额外放行控制面中 `wishforge.io/component: git-storage` 的 Pod(`WF_GIT_SERVICE_PORT`)。
|
||||
|
||||
克隆与凭据的投递见 [apps/ide/README.md](../ide/README.md)。
|
||||
|
||||
网络策略只放行 DNS、控制面 API(含模型代理)、Temporal 与平台 Git 服务(`WF_GIT_SERVICE_PORT`,默认 4504,Pod 标签 `wishforge.io/component: git-storage`)。配置了出站代理与构建器时,再分别放行它们:代理给两个角色,构建器只给 verifier(单独的 `<前缀>-builder` 策略,释放时一并删除)。代理与构建器本身由运维提供(见 [operations.md](../../docs/operations.md))。
|
||||
|
||||
## 测试
|
||||
|
||||
@ -71,7 +71,6 @@ export function readLocalConfig(env = process.env, cwd = process.cwd()) {
|
||||
runRoot: path.resolve(cwd, env.WF_LOCAL_RUN_ROOT ?? path.join(homedir(), '.local/share/wishforge/runs')),
|
||||
cwd,
|
||||
workerEntry: path.resolve(cwd, env.WF_LOCAL_WORKER_ENTRY ?? 'apps/worker/src/main.ts'),
|
||||
ideUrl: env.WF_LOCAL_IDE_URL,
|
||||
claudeAuthHome: env.WF_CLAUDE_LOCAL_AUTH_HOME,
|
||||
passthrough: Object.fromEntries(PASSTHROUGH.filter(name => env[name]).map(name => [name, env[name]])),
|
||||
// Grants are verified like in production, but the host has no image and no fixed caps.
|
||||
@ -95,11 +94,6 @@ export function readLocalConfig(env = process.env, cwd = process.cwd()) {
|
||||
throw new Error('WF_CLAUDE_LOCAL_AUTH_HOME must be an absolute path');
|
||||
if (config.resourceGrantSecret.length < 32)
|
||||
throw new Error('WF_RESOURCE_GRANT_SECRET must be at least 32 characters');
|
||||
if (config.ideUrl) {
|
||||
const url = new URL(config.ideUrl);
|
||||
if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
|
||||
throw new Error('WF_LOCAL_IDE_URL must be an HTTP(S) URL without credentials');
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
@ -127,10 +121,7 @@ function stopProcess(child, timeoutMs = 10_000) {
|
||||
});
|
||||
}
|
||||
|
||||
export function createLocalBackend(
|
||||
config,
|
||||
{ spawnProcess = spawn, log = line => console.log(line), fetcher = fetch } = {},
|
||||
) {
|
||||
export function createLocalBackend(config, { spawnProcess = spawn, log = line => console.log(line) } = {}) {
|
||||
/** Live worker processes per tenant, run and generation. */
|
||||
const generations = new Map();
|
||||
return {
|
||||
@ -234,24 +225,6 @@ export function createLocalBackend(
|
||||
persistentStorageRetained: true,
|
||||
};
|
||||
},
|
||||
/** Every workspace opens the one local Theia, once it answers; its lifecycle is not managed. */
|
||||
async provisionWorkspace(input) {
|
||||
if (
|
||||
!input ||
|
||||
// The one local Theia is told the Git service origin instead (scripts/dev-ide.mjs).
|
||||
Object.keys(input).some(key => !['tenantId', 'projectId', 'workspaceId', 'repositoryUrl'].includes(key)) ||
|
||||
input.tenantId !== config.tenantId ||
|
||||
!id.test(input.projectId ?? '') ||
|
||||
!id.test(input.workspaceId ?? '')
|
||||
)
|
||||
throw new Error('INVALID_WORKSPACE_REQUEST');
|
||||
if (!config.ideUrl) return { status: 'pending', upstreamUrl: '', lifecycle: 'unmanaged' };
|
||||
const reachable = await fetcher(config.ideUrl, { signal: AbortSignal.timeout(2000) }).then(
|
||||
response => response.status < 500,
|
||||
() => false,
|
||||
);
|
||||
return { status: reachable ? 'ready' : 'pending', upstreamUrl: config.ideUrl, lifecycle: 'unmanaged' };
|
||||
},
|
||||
async close() {
|
||||
await Promise.all([...generations.values()].flat().map(child => stopProcess(child, 3000)));
|
||||
generations.clear();
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
import http from 'node:http';
|
||||
import { timingSafeEqual } from 'node:crypto';
|
||||
import { readConfig, provisionRun } from './kubernetes.mjs';
|
||||
import { provisionWorkspace } from './workspaces.mjs';
|
||||
import { purgeRunStorage, releaseRun } from './run-release.mjs';
|
||||
import { observeWorkspaceLifecycle, reconcileWorkspaceLifecycle } from './workspace-lifecycle.mjs';
|
||||
import { createRunOperationQueue } from './run-operations.mjs';
|
||||
import { createLocalBackend, readLocalConfig } from './local.mjs';
|
||||
|
||||
/** `local` runs workers as host processes for development; `kubernetes` creates Kata Jobs and IDE Deployments. */
|
||||
/** `local` runs workers as host processes for development; `kubernetes` creates Kata Jobs. */
|
||||
function backendFor(name) {
|
||||
if (name === 'local') return createLocalBackend(readLocalConfig());
|
||||
if (name === 'kubernetes') {
|
||||
@ -17,9 +15,6 @@ function backendFor(name) {
|
||||
provisionRun: input => provisionRun(config, input),
|
||||
releaseRun: input => releaseRun(config, input),
|
||||
purgeRun: input => purgeRunStorage(config, input),
|
||||
provisionWorkspace: input => provisionWorkspace(config, input),
|
||||
observeWorkspace: input => observeWorkspaceLifecycle(config, input),
|
||||
reconcileWorkspace: input => reconcileWorkspaceLifecycle(config, input),
|
||||
async close() {},
|
||||
};
|
||||
}
|
||||
@ -60,40 +55,19 @@ const server = http.createServer(async (req, res) => {
|
||||
res.end(JSON.stringify({ error: 'UNAUTHORIZED' }));
|
||||
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 && backend.observeWorkspace && req.method === 'GET') {
|
||||
const input = {
|
||||
tenantId: target.searchParams.get('tenantId'),
|
||||
projectId: target.searchParams.get('projectId'),
|
||||
workspaceId: lifecycle[1],
|
||||
};
|
||||
res.end(JSON.stringify(await backend.observeWorkspace(input)));
|
||||
return;
|
||||
}
|
||||
if (lifecycle && backend.reconcileWorkspace && req.method === 'PUT') {
|
||||
const input = await body(req, 4000);
|
||||
if (input.workspaceId !== lifecycle[1]) throw new Error('INVALID_WORKSPACE_SCOPE');
|
||||
res.writeHead(202);
|
||||
res.end(JSON.stringify(await backend.reconcileWorkspace(input)));
|
||||
return;
|
||||
}
|
||||
if (!['/runs', '/runs/release', '/runs/purge', '/workspaces'].includes(req.url) || req.method !== 'POST') {
|
||||
if (!['/runs', '/runs/release', '/runs/purge'].includes(req.url) || req.method !== 'POST') {
|
||||
res.writeHead(404);
|
||||
res.end(JSON.stringify({ error: 'NOT_FOUND' }));
|
||||
return;
|
||||
}
|
||||
const input = await body(req, 40000);
|
||||
const result =
|
||||
req.url === '/workspaces'
|
||||
? await backend.provisionWorkspace(input)
|
||||
: await runOperation(input.tenantId, input.runId, () =>
|
||||
req.url === '/runs/release'
|
||||
? backend.releaseRun(input)
|
||||
: req.url === '/runs/purge'
|
||||
? backend.purgeRun(input)
|
||||
: backend.provisionRun(input),
|
||||
);
|
||||
const result = await runOperation(input.tenantId, input.runId, () =>
|
||||
req.url === '/runs/release'
|
||||
? backend.releaseRun(input)
|
||||
: req.url === '/runs/purge'
|
||||
? backend.purgeRun(input)
|
||||
: backend.provisionRun(input),
|
||||
);
|
||||
res.writeHead(202);
|
||||
res.end(JSON.stringify(result));
|
||||
} catch (error) {
|
||||
|
||||
@ -244,21 +244,3 @@ test('rejects forged, foreign or malformed requests before starting anything', a
|
||||
await f.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
test('points workspaces at the local Theia once it answers, without a managed lifecycle', async () => {
|
||||
const f = await fixture({ WF_LOCAL_IDE_URL: 'http://127.0.0.1:4300/' });
|
||||
try {
|
||||
const input = { tenantId: 'demo', projectId: 'project-1', workspaceId: 'workspace-1' };
|
||||
const down = createLocalBackend(f.config, { fetcher: async () => Promise.reject(new Error('ECONNREFUSED')) });
|
||||
assert.deepEqual(await down.provisionWorkspace(input), {
|
||||
status: 'pending',
|
||||
upstreamUrl: 'http://127.0.0.1:4300/',
|
||||
lifecycle: 'unmanaged',
|
||||
});
|
||||
const up = createLocalBackend(f.config, { fetcher: async () => new Response('ok') });
|
||||
assert.equal((await up.provisionWorkspace(input)).status, 'ready');
|
||||
await assert.rejects(up.provisionWorkspace({ ...input, tenantId: 'other' }), /INVALID_WORKSPACE_REQUEST/);
|
||||
} finally {
|
||||
await f.cleanup();
|
||||
}
|
||||
});
|
||||
|
||||
@ -1,126 +0,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);
|
||||
});
|
||||
@ -1,102 +0,0 @@
|
||||
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',
|
||||
gitServicePort: 4504,
|
||||
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);
|
||||
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(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');
|
||||
assert.throws(() => renderWorkspaceResources(config, { ...input, tenantId: 'beta' }, image));
|
||||
});
|
||||
test('a workspace of a platform repository may reach the Git service and keeps its token in memory', () => {
|
||||
const repositoryUrl = 'https://git.example/git/alpha/project-1.git';
|
||||
const rendered = renderWorkspaceResources(config, { ...input, repositoryUrl }, image);
|
||||
const pod = rendered.resources.find(r => r.kind === 'Deployment').spec.template.spec;
|
||||
const env = Object.fromEntries(pod.containers[0].env.map(e => [e.name, e.value]));
|
||||
assert.equal(env.WISHFORGE_GIT_REMOTE, repositoryUrl);
|
||||
assert.equal(env.WISHFORGE_GIT_CREDENTIAL_DIR, '/run/wishforge/git');
|
||||
assert.deepEqual(pod.volumes.find(v => v.name === 'git-credentials').emptyDir, {
|
||||
medium: 'Memory',
|
||||
sizeLimit: '1Mi',
|
||||
});
|
||||
const egress = rendered.resources.find(r => r.kind === 'NetworkPolicy').spec.egress;
|
||||
assert.deepEqual(egress.at(-1), {
|
||||
to: [
|
||||
{
|
||||
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': 'wf-control' } },
|
||||
podSelector: { matchLabels: { 'wishforge.io/component': 'git-storage' } },
|
||||
},
|
||||
],
|
||||
ports: [{ protocol: 'TCP', port: 4504 }],
|
||||
});
|
||||
// Without a platform repository nothing beyond DNS is reachable.
|
||||
const plain = renderWorkspaceResources(config, input, image);
|
||||
assert.equal(plain.resources.find(r => r.kind === 'NetworkPolicy').spec.egress.length, 1);
|
||||
for (const other of [
|
||||
'http://git.example/git/alpha/project-1.git',
|
||||
'https://token@git.example/git/alpha/project-1.git',
|
||||
'https://git.example/git/beta/project-1.git',
|
||||
'https://git.example/git/alpha/project-2.git',
|
||||
])
|
||||
assert.throws(() => renderWorkspaceResources(config, { ...input, repositoryUrl: other }, image), /REPOSITORY/);
|
||||
});
|
||||
test('only a local development cluster serves the repository and the gateway over plain HTTP', () => {
|
||||
const repositoryUrl = 'http://git-storage.wf-control.svc.cluster.local:4504/git/alpha/project-1.git';
|
||||
const collaboration = { image, gatewayBase: 'http://localhost:4400/' };
|
||||
const local = { ...config, runtimeClass: undefined, insecureHttp: true };
|
||||
const rendered = renderWorkspaceResources(local, { ...input, repositoryUrl }, image, collaboration);
|
||||
const env = Object.fromEntries(
|
||||
rendered.resources
|
||||
.find(r => r.kind === 'Deployment')
|
||||
.spec.template.spec.containers[0].env.map(e => [e.name, e.value]),
|
||||
);
|
||||
assert.equal(env.WISHFORGE_GIT_REMOTE, repositoryUrl);
|
||||
assert.equal(env.WF_COLLABORATION_GATEWAY_URL, 'http://localhost:4400/workspaces/workspace-1/collaboration/');
|
||||
assert.throws(() => renderWorkspaceResources(config, { ...input, repositoryUrl }, image), /REPOSITORY/);
|
||||
assert.throws(() => renderWorkspaceResources(config, input, image, collaboration), /GATEWAY/);
|
||||
});
|
||||
test('scoped collaboration runs as a sidecar without any platform credential in the workspace', () => {
|
||||
const rendered = renderWorkspaceResources(config, 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(!JSON.stringify(pod).includes('PRIVATE_KEY'));
|
||||
assert.ok(!JSON.stringify(pod).includes('TOKEN'));
|
||||
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'));
|
||||
}
|
||||
});
|
||||
@ -1,132 +0,0 @@
|
||||
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 });
|
||||
}
|
||||
@ -1,290 +0,0 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { runKubectl } from './kubernetes.mjs';
|
||||
const safe = /^[a-zA-Z0-9_-]{1,128}$/;
|
||||
|
||||
/** HTTPS, or plain HTTP on a local development cluster (WF_ALLOW_INSECURE_HTTP) inside the cluster. */
|
||||
const secureEnough = (config, url) =>
|
||||
url.protocol === 'https:' || (url.protocol === 'http:' && config.insecureHttp === true);
|
||||
|
||||
/** The project's platform repository; the workspace accepts credentials for it alone. */
|
||||
function workspaceRepository(config, input) {
|
||||
if (input.repositoryUrl === undefined) return undefined;
|
||||
let url;
|
||||
try {
|
||||
url = new URL(input.repositoryUrl);
|
||||
} catch {
|
||||
throw new Error('INVALID_WORKSPACE_REPOSITORY');
|
||||
}
|
||||
if (
|
||||
!secureEnough(config, url) ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.search ||
|
||||
url.hash ||
|
||||
url.pathname !== `/git/${input.tenantId}/${input.projectId}.git`
|
||||
)
|
||||
throw new Error('INVALID_WORKSPACE_REPOSITORY');
|
||||
return url.href;
|
||||
}
|
||||
|
||||
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', 'repositoryUrl'].includes(k))
|
||||
)
|
||||
throw new Error('INVALID_WORKSPACE_REQUEST');
|
||||
const repository = workspaceRepository(config, input);
|
||||
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 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: {
|
||||
...(config.runtimeClass ? { 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.volumes.push({ name: 'temporary', emptyDir: { sizeLimit: '2Gi' } });
|
||||
workspacePod.containers[0].volumeMounts.push({ name: 'temporary', mountPath: '/tmp' });
|
||||
if (repository) {
|
||||
// The gateway delivers a short-lived token while the IDE is open; it is kept in memory only.
|
||||
workspacePod.containers[0].env.push(
|
||||
{ name: 'WISHFORGE_GIT_REMOTE', value: repository },
|
||||
{ name: 'WISHFORGE_GIT_CREDENTIAL_DIR', value: '/run/wishforge/git' },
|
||||
);
|
||||
workspacePod.volumes.push({ name: 'git-credentials', emptyDir: { medium: 'Memory', sizeLimit: '1Mi' } });
|
||||
workspacePod.containers[0].volumeMounts.push({ name: 'git-credentials', mountPath: '/run/wishforge/git' });
|
||||
resources
|
||||
.find(r => r.kind === 'NetworkPolicy')
|
||||
.spec.egress.push({
|
||||
to: [
|
||||
{
|
||||
namespaceSelector: { matchLabels: { 'kubernetes.io/metadata.name': config.controlNamespace } },
|
||||
podSelector: { matchLabels: { 'wishforge.io/component': 'git-storage' } },
|
||||
},
|
||||
],
|
||||
ports: [{ protocol: 'TCP', port: config.gitServicePort ?? 4504 }],
|
||||
});
|
||||
}
|
||||
let collaborationUpstreamUrl;
|
||||
if (collaboration) {
|
||||
if (!/^[^\s]+@sha256:[a-f0-9]{64}$/.test(collaboration.image ?? ''))
|
||||
throw new Error('UNCONFIGURED_OCT_WORKSPACE_IMAGE');
|
||||
const gateway = new URL(collaboration.gatewayBase);
|
||||
if (
|
||||
!secureEnough(config, gateway) ||
|
||||
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 },
|
||||
];
|
||||
resources.find(r => r.kind === 'NetworkPolicy').spec.ingress[0].ports.push({ protocol: 'TCP', port: 8100 });
|
||||
collaborationUpstreamUrl = `http://${name}.${config.namespace}.svc:8100`;
|
||||
}
|
||||
return { name, resources, upstreamUrl: `http://${name}.${config.namespace}.svc:3000`, collaborationUpstreamUrl };
|
||||
}
|
||||
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'));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
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_IDE_PUBLIC_URL }
|
||||
: undefined);
|
||||
const rendered = renderWorkspaceResources(config, 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,
|
||||
};
|
||||
}
|
||||
4
apps/web/src/app/(console)/skills/page.tsx
Normal file
4
apps/web/src/app/(console)/skills/page.tsx
Normal file
@ -0,0 +1,4 @@
|
||||
import { SkillsLibrary } from '@/features/skills';
|
||||
export default function Page() {
|
||||
return <SkillsLibrary />;
|
||||
}
|
||||
@ -70,6 +70,10 @@ async function proxy(request: NextRequest, context: Context) {
|
||||
});
|
||||
const disposition = upstream.headers.get('content-disposition');
|
||||
if (disposition) responseHeaders.set('Content-Disposition', disposition);
|
||||
// Files people uploaded come back as what they say they are, and never run as a page.
|
||||
responseHeaders.set('X-Content-Type-Options', 'nosniff');
|
||||
const policy = upstream.headers.get('content-security-policy');
|
||||
if (policy) responseHeaders.set('Content-Security-Policy', policy);
|
||||
if (stream) {
|
||||
responseHeaders.set('X-Accel-Buffering', 'no');
|
||||
responseHeaders.set('Connection', 'keep-alive');
|
||||
|
||||
@ -999,48 +999,6 @@ button {
|
||||
padding: 14px 22px;
|
||||
border-top: 1px solid #edf1f5;
|
||||
}
|
||||
.workspace-list {
|
||||
padding: 0 22px 20px;
|
||||
}
|
||||
.branch-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0 22px 16px;
|
||||
}
|
||||
.branch-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
font-size: 12px;
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid #edf1f5;
|
||||
}
|
||||
.branch-list li > span {
|
||||
color: var(--muted);
|
||||
flex: 1;
|
||||
}
|
||||
.workspace-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 18px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.workspace-row strong {
|
||||
font-size: 13px;
|
||||
display: block;
|
||||
}
|
||||
.workspace-row .status {
|
||||
margin-top: 5px;
|
||||
}
|
||||
.workspace-row p {
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
margin-top: 5px;
|
||||
}
|
||||
.workspace-row > span {
|
||||
font-size: 12px;
|
||||
}
|
||||
.form-columns {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1.7fr) minmax(250px, 1fr);
|
||||
@ -1621,6 +1579,12 @@ button {
|
||||
color: var(--muted);
|
||||
line-height: 1.7;
|
||||
}
|
||||
.provider-actions {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
.inline-explanation {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
@ -2257,13 +2221,6 @@ button {
|
||||
padding: 0 17px 15px;
|
||||
font-size: 11px;
|
||||
}
|
||||
.workspace-list {
|
||||
padding: 0 17px 16px;
|
||||
}
|
||||
.workspace-row {
|
||||
flex-wrap: wrap;
|
||||
gap: 12px;
|
||||
}
|
||||
.panel > .notice {
|
||||
margin: 0 17px 13px;
|
||||
}
|
||||
@ -2439,6 +2396,9 @@ button {
|
||||
flex-basis: 100%;
|
||||
font-size: 11px;
|
||||
}
|
||||
.provider-actions {
|
||||
width: 100%;
|
||||
}
|
||||
.provider-bottom .button {
|
||||
width: 100%;
|
||||
min-height: 35px;
|
||||
@ -3133,6 +3093,21 @@ fieldset:disabled label {
|
||||
.skill-list li.skill-proposed > svg {
|
||||
color: var(--amber);
|
||||
}
|
||||
.skill-list li > input[type='checkbox'] {
|
||||
margin-top: 5px;
|
||||
flex: none;
|
||||
}
|
||||
.skill-category {
|
||||
display: inline-block;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 999px;
|
||||
padding: 0 8px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.skill-warning {
|
||||
color: var(--amber);
|
||||
}
|
||||
.skill-body {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
@ -4341,3 +4316,85 @@ fieldset:disabled label {
|
||||
border-radius: 12px;
|
||||
}
|
||||
}
|
||||
.attachment-drop {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
padding: 12px 14px;
|
||||
border: 1px dashed #c8d3df;
|
||||
border-radius: var(--radius);
|
||||
background: var(--bg);
|
||||
transition:
|
||||
border-color 0.15s,
|
||||
background 0.15s;
|
||||
}
|
||||
.attachment-drop.dragging {
|
||||
border-color: var(--teal);
|
||||
background: var(--teal-pale);
|
||||
}
|
||||
.attachment-drop .field-hint {
|
||||
flex: 1 1 260px;
|
||||
}
|
||||
.attachment-pick {
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
}
|
||||
.attachment-pick input {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
opacity: 0;
|
||||
cursor: pointer;
|
||||
width: 100%;
|
||||
}
|
||||
.attachment-list {
|
||||
list-style: none;
|
||||
margin: 4px 0 0;
|
||||
padding: 0;
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
|
||||
gap: 8px;
|
||||
}
|
||||
.attachment-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
min-width: 0;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
background: var(--surface);
|
||||
}
|
||||
.attachment-list img,
|
||||
.attachment-icon {
|
||||
flex: none;
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
border-radius: 4px;
|
||||
object-fit: cover;
|
||||
background: var(--bg);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
color: var(--muted);
|
||||
}
|
||||
.attachment-list a {
|
||||
display: contents;
|
||||
color: inherit;
|
||||
}
|
||||
.attachment-name {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
font-size: 12px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.attachment-name small {
|
||||
color: var(--muted);
|
||||
font-size: 11px;
|
||||
}
|
||||
.panel-body-list {
|
||||
padding: 0 22px 4px;
|
||||
}
|
||||
|
||||
@ -1,69 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { authorizationFor, identityHeaders, isDevelopment } 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, localUser } = await authorizationFor(request);
|
||||
if (!token)
|
||||
return NextResponse.redirect(
|
||||
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(/\/$/, '');
|
||||
try {
|
||||
const response = await fetch(`${api}/v1/workspaces/${encodeURIComponent(id)}/launch`, {
|
||||
method: 'POST',
|
||||
headers: { ...identityHeaders(token, localUser), '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: 'IDE_LAUNCH_DENIED',
|
||||
message: response.status === 403 ? '你没有权限打开此工作空间。' : '工作空间尚未就绪或控制面拒绝了连接。',
|
||||
},
|
||||
},
|
||||
{ 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');
|
||||
// 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 =
|
||||
isDevelopment() && 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');
|
||||
const result = NextResponse.redirect(url, 303);
|
||||
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: '无法取得工作空间启动链接。请检查控制面与运行环境配置。',
|
||||
},
|
||||
},
|
||||
{ status: 503, headers: { 'Cache-Control': 'no-store' } },
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ import {
|
||||
Activity,
|
||||
ArrowUpRight,
|
||||
BarChart3,
|
||||
BookOpen,
|
||||
Bot,
|
||||
Box,
|
||||
ChevronRight,
|
||||
@ -28,6 +29,7 @@ const navigation = [
|
||||
['/', '工作概览', Home],
|
||||
['/projects', '项目空间', Folder],
|
||||
['/runs', '运行记录', Activity],
|
||||
['/skills', '知识库', BookOpen],
|
||||
['/releases', '部署与审批', Box],
|
||||
['/providers', '模型连接', Settings2],
|
||||
['/agent', 'Agent 配置', Bot],
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
'use client';
|
||||
import { History, Plus } from 'lucide-react';
|
||||
import { useState } from 'react';
|
||||
import { MODEL_NAME_PATTERN } from '@wishforge/contracts/models';
|
||||
import { messageOf, mutateApi, shortId, formatTime, useResource } from '@/lib/api';
|
||||
import type { AgentBundleList } from '@/lib/types';
|
||||
import {
|
||||
@ -182,7 +183,13 @@ function CreateBundleDialog({ onClose, onSaved }: { onClose: () => void; onSaved
|
||||
<textarea rows={3} maxLength={16000} value={form.repairer} onChange={update('repairer')} />
|
||||
</Field>
|
||||
<Field label="模型" hint="留空则使用执行器默认模型。">
|
||||
<input maxLength={160} pattern="[a-zA-Z0-9_.:/-]+" value={form.model} onChange={update('model')} />
|
||||
<input
|
||||
maxLength={160}
|
||||
pattern={MODEL_NAME_PATTERN}
|
||||
placeholder="例如 deepseek-pro@latest[1m]"
|
||||
value={form.model}
|
||||
onChange={update('model')}
|
||||
/>
|
||||
</Field>
|
||||
<FormError message={error} />
|
||||
<div className="modal-actions">
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
import Link from 'next/link';
|
||||
import { ArrowRight, CircleAlert, GitBranch, Laptop, Plus, Settings2 } from 'lucide-react';
|
||||
import { ArrowRight, CircleAlert, GitBranch, Plus, Settings2 } from 'lucide-react';
|
||||
import { formatNumber, useResource } from '@/lib/api';
|
||||
import { resourceState } from '@/lib/readiness';
|
||||
import type { Dashboard } from '@/lib/types';
|
||||
@ -151,10 +151,10 @@ export function DashboardView() {
|
||||
},
|
||||
{
|
||||
number: '03',
|
||||
Icon: Laptop,
|
||||
title: '打开云 IDE',
|
||||
text: '在云端开发环境中构建与协作。',
|
||||
href: '/projects',
|
||||
Icon: Plus,
|
||||
title: '提交第一个愿望',
|
||||
text: '写下目标和验收标准,Agent 实现后经验证合入。',
|
||||
href: '/runs/new',
|
||||
},
|
||||
].map(({ number, Icon, title, text, href }) => (
|
||||
<Link href={href} key={number}>
|
||||
|
||||
@ -1,149 +0,0 @@
|
||||
'use client';
|
||||
import Link from 'next/link';
|
||||
import { useState } from 'react';
|
||||
import { BookOpenCheck, Plus, Trash2 } from 'lucide-react';
|
||||
import { formatTime, messageOf, mutateApi, useResource } from '@/lib/api';
|
||||
import type { Lesson } from '@/lib/types';
|
||||
import { Button, EmptyState, ErrorNotice, Field, FormError, Loading, Modal, Panel, Status } from '@/components/ui';
|
||||
|
||||
/**
|
||||
* What later runs of this project are told: lessons recorded from repaired failures and
|
||||
* conventions added by admins. Disabled lessons stay listed but are no longer used.
|
||||
*/
|
||||
export function LessonsPanel({ projectId }: { projectId: string }) {
|
||||
const lessons = useResource<Lesson[]>(`/projects/${projectId}/lessons`, 15000);
|
||||
const [adding, setAdding] = useState(false);
|
||||
const [summary, setSummary] = useState('');
|
||||
const [busy, setBusy] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
async function run(key: string, action: () => Promise<unknown>) {
|
||||
setBusy(key);
|
||||
setError('');
|
||||
try {
|
||||
await action();
|
||||
await lessons.mutate();
|
||||
return true;
|
||||
} catch (failure) {
|
||||
setError(messageOf(failure));
|
||||
return false;
|
||||
} finally {
|
||||
setBusy('');
|
||||
}
|
||||
}
|
||||
return (
|
||||
<Panel
|
||||
title="项目经验"
|
||||
aside={
|
||||
<Button variant="ghost" onClick={() => setAdding(true)}>
|
||||
<Plus size={15} />
|
||||
添加约定
|
||||
</Button>
|
||||
}
|
||||
>
|
||||
<p className="panel-intro">
|
||||
新运行的提示词会带上最近 5 条启用中的经验。经历过修复的运行成功后会自动记录一条;不准确的可以停用或删除。
|
||||
</p>
|
||||
{lessons.error ? (
|
||||
<ErrorNotice error={lessons.error} retry={() => void lessons.mutate()} />
|
||||
) : lessons.isLoading ? (
|
||||
<Loading />
|
||||
) : lessons.data?.length ? (
|
||||
<ul className="lesson-list">
|
||||
{lessons.data.map(lesson => (
|
||||
<li key={lesson.id}>
|
||||
<BookOpenCheck size={18} />
|
||||
<div>
|
||||
<p>{lesson.summary}</p>
|
||||
<p className="muted text-small">
|
||||
{lesson.kind === 'repair' ? '来自修复' : '项目约定'} · {formatTime(lesson.createdAt)}
|
||||
{lesson.sourceRunId ? (
|
||||
<>
|
||||
{' · '}
|
||||
<Link className="text-link" href={`/runs/${lesson.sourceRunId}`}>
|
||||
来源运行
|
||||
</Link>
|
||||
</>
|
||||
) : null}
|
||||
</p>
|
||||
</div>
|
||||
<div className="button-group">
|
||||
<Status
|
||||
value={lesson.status === 'active' ? 'ready' : 'disabled'}
|
||||
label={lesson.status === 'active' ? '启用' : '已停用'}
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
busy={busy === `status:${lesson.id}`}
|
||||
onClick={() =>
|
||||
void run(`status:${lesson.id}`, () =>
|
||||
mutateApi(
|
||||
`/lessons/${lesson.id}`,
|
||||
{ status: lesson.status === 'active' ? 'disabled' : 'active' },
|
||||
'PATCH',
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
{lesson.status === 'active' ? '停用' : '启用'}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
aria-label="删除这条经验"
|
||||
busy={busy === `delete:${lesson.id}`}
|
||||
onClick={() =>
|
||||
void run(`delete:${lesson.id}`, () => mutateApi(`/lessons/${lesson.id}`, undefined, 'DELETE'))
|
||||
}
|
||||
>
|
||||
<Trash2 size={15} />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<EmptyState compact title="还没有项目经验" description="运行经历修复并最终成功后,会在这里记录经验。" />
|
||||
)}
|
||||
{!adding ? <FormError message={error} /> : null}
|
||||
{adding ? (
|
||||
<Modal
|
||||
title="添加项目约定"
|
||||
description="写一条之后的执行器在本项目工作时需要遵守的约定。"
|
||||
onClose={() => setAdding(false)}
|
||||
>
|
||||
<form
|
||||
className="form-stack"
|
||||
onSubmit={event => {
|
||||
event.preventDefault();
|
||||
void run('create', () => mutateApi(`/projects/${projectId}/lessons`, { summary: summary.trim() })).then(
|
||||
saved => {
|
||||
if (!saved) return;
|
||||
setSummary('');
|
||||
setAdding(false);
|
||||
},
|
||||
);
|
||||
}}
|
||||
>
|
||||
<Field label="约定" hint="最多 1000 字,例如「运行测试前先执行 npm run build」。">
|
||||
<textarea
|
||||
required
|
||||
maxLength={1000}
|
||||
rows={4}
|
||||
value={summary}
|
||||
onChange={event => setSummary(event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<FormError message={error} />
|
||||
<div className="modal-actions">
|
||||
<Button type="button" variant="secondary" onClick={() => setAdding(false)}>
|
||||
取消
|
||||
</Button>
|
||||
<Button busy={busy === 'create'} disabled={!summary.trim()}>
|
||||
保存
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
) : null}
|
||||
</Panel>
|
||||
);
|
||||
}
|
||||
@ -66,6 +66,7 @@ export function ProjectSettings({
|
||||
accounts.data?.find(account => account.id === userId)?.displayName ?? personName(userId);
|
||||
const [wishRules, setWishRules] = useState(project.wishRules ?? '');
|
||||
const [models, setModels] = useState(project.models ?? []);
|
||||
const [skillAutoActivation, setSkillAutoActivation] = useState(project.skillAutoActivation !== false);
|
||||
const [delivery, setDelivery] = useState(Boolean(config));
|
||||
const [repository, setRepository] = useState(config?.imageRepository ?? '');
|
||||
const [architecture, setArchitecture] = useState(config?.architecture ?? 'linux/amd64');
|
||||
@ -116,6 +117,7 @@ export function ProjectSettings({
|
||||
paths.length || trusted.length ? { paths, exceptPaths: exceptions, trustedMembers: trusted } : null,
|
||||
wishRules: wishRules.trim(),
|
||||
models: models.length ? orderProjectModels(models) : null,
|
||||
skillAutoActivation,
|
||||
...(delivery
|
||||
? {
|
||||
deliveryConfig: {
|
||||
@ -315,6 +317,16 @@ export function ProjectSettings({
|
||||
>
|
||||
<textarea rows={4} maxLength={4000} value={wishRules} onChange={event => setWishRules(event.target.value)} />
|
||||
</Field>
|
||||
<label className="checkbox-label">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={skillAutoActivation}
|
||||
onChange={event => setSkillAutoActivation(event.target.checked)}
|
||||
/>
|
||||
<span>
|
||||
自动启用可信的项目技能:修复过的失败记成的踩坑记录、被两次成功运行提炼出的技能直接启用,并由平台持续更新;关闭后它们都先等待确认。
|
||||
</span>
|
||||
</label>
|
||||
<label className="checkbox-label">
|
||||
<input
|
||||
type="checkbox"
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
'use client';
|
||||
import { useRef, useState } from 'react';
|
||||
import { useState } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { ArrowRight, ArrowUpRight, FolderGit2, GitBranch, Laptop, Plus, Search, Settings2 } from 'lucide-react';
|
||||
import { ArrowRight, ArrowUpRight, FolderGit2, GitBranch, Plus, Search, Settings2 } from 'lucide-react';
|
||||
import { formatTime, messageOf, mutateApi, safeUrl, useResource } from '@/lib/api';
|
||||
import type { IdeBranch, Identity, Project, Run, Workspace } from '@/lib/types';
|
||||
import type { Identity, Project, Run } from '@/lib/types';
|
||||
import {
|
||||
Button,
|
||||
ButtonLink,
|
||||
@ -22,9 +22,7 @@ import {
|
||||
} from '@/components/ui';
|
||||
import { RunTable } from '@/components/run-table';
|
||||
import { ProjectSettings } from './project-settings';
|
||||
import { LessonsPanel } from './lessons';
|
||||
import { SkillsPanel } from './skills';
|
||||
import { browserId } from '@/lib/browser-id';
|
||||
import { ProjectSkillsSummary } from './skills';
|
||||
export function ProjectsView({ create = false }: { create?: boolean }) {
|
||||
const { data, error, isLoading, mutate } = useResource<Project[]>('/projects');
|
||||
const [search, setSearch] = useState('');
|
||||
@ -171,8 +169,6 @@ export function ProjectView({ id }: { id: string }) {
|
||||
const mayWish = identity.data?.permissions?.wish !== 'none';
|
||||
const projects = useResource<Project[]>('/projects');
|
||||
const runs = useResource<Run[]>(`/runs?project=${id}`, 10000);
|
||||
const workspaces = useResource<Workspace[]>(`/projects/${id}/workspaces`, 10000);
|
||||
const branches = useResource<IdeBranch[]>(`/projects/${id}/branches`, 30000);
|
||||
const projectResource = useResource<Project>(`/projects/${id}`);
|
||||
const project = projectResource.data ?? projects.data?.find(item => item.id === id);
|
||||
const [showSettings, setShowSettings] = useState(false);
|
||||
@ -190,18 +186,6 @@ export function ProjectView({ id }: { id: string }) {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
async function createWorkspace() {
|
||||
setBusy(true);
|
||||
setActionError('');
|
||||
try {
|
||||
await mutateApi(`/projects/${id}/workspaces`, {});
|
||||
await workspaces.mutate();
|
||||
} catch (error) {
|
||||
setActionError(messageOf(error));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
if (projects.isLoading) return <Loading />;
|
||||
if (projects.error) return <ErrorNotice error={projects.error} retry={() => void projects.mutate()} />;
|
||||
if (!project)
|
||||
@ -239,139 +223,76 @@ export function ProjectView({ id }: { id: string }) {
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
<div className="detail-columns">
|
||||
<Panel title="代码仓库">
|
||||
<dl className="details-list">
|
||||
<KeyValue label="仓库">
|
||||
{safeUrl(project.repositoryUrl) ? (
|
||||
<a
|
||||
className="text-link break-all"
|
||||
href={safeUrl(project.repositoryUrl)}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
{project.repositoryUrl}
|
||||
<ArrowUpRight size={15} />
|
||||
</a>
|
||||
) : (
|
||||
(project.repositoryUrl ?? '尚未关联')
|
||||
)}
|
||||
</KeyValue>
|
||||
<KeyValue label="目标分支">
|
||||
<span className="inline-icon">
|
||||
<GitBranch size={15} />
|
||||
{project.targetBranch}
|
||||
</span>
|
||||
</KeyValue>
|
||||
<KeyValue label="仓库状态">
|
||||
<Status
|
||||
value={
|
||||
project.repositoryStatus === 'ready'
|
||||
? 'ready'
|
||||
: project.repositoryStatus === 'unavailable'
|
||||
? 'failed'
|
||||
: 'unconfigured'
|
||||
}
|
||||
label={
|
||||
{ ready: '可用', unavailable: '不可用', unconfigured: '未创建' }[project.repositoryStatus ?? 'ready']
|
||||
}
|
||||
/>
|
||||
</KeyValue>
|
||||
<KeyValue label="代码权限">
|
||||
{project.codeReview?.paths.length ? (
|
||||
<>
|
||||
后端代码 <code>{project.codeReview.paths.join('、')}</code>
|
||||
{project.codeReview.exceptPaths.length ? (
|
||||
<>
|
||||
(放行 <code>{project.codeReview.exceptPaths.join('、')}</code>)
|
||||
</>
|
||||
) : null}
|
||||
算后端代码:只能改前端的人碰不到它,其他人改到它时按各自的权限直接合入或先审核(见
|
||||
<Link className="text-link" href="/members">
|
||||
成员与权限
|
||||
</Link>
|
||||
)
|
||||
</>
|
||||
) : (
|
||||
'不区分前后端,验证通过后直接合入'
|
||||
)}
|
||||
</KeyValue>
|
||||
<KeyValue label="创建时间">{formatTime(project.createdAt)}</KeyValue>
|
||||
<KeyValue label="项目 ID">
|
||||
<code>{project.id}</code>
|
||||
</KeyValue>
|
||||
</dl>
|
||||
{project.repositoryStatus && project.repositoryStatus !== 'ready' ? (
|
||||
<>
|
||||
<Notice tone="warning">
|
||||
{project.repositoryError ?? '平台 Git 仓库尚未创建,提交的愿望会等待仓库就绪。'}
|
||||
</Notice>
|
||||
<div className="panel-bottom-action">
|
||||
<Button variant="secondary" busy={busy} onClick={() => void retryRepository()}>
|
||||
重试创建仓库
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
</Panel>
|
||||
<Panel title="云 IDE" aside={<Laptop size={19} />}>
|
||||
<p className="panel-intro">
|
||||
在 IDE 中提交并推送到 <code>ide/</code> 开头的分支(默认 <code>ide/work</code>),再从该分支提交愿望:
|
||||
验证通过后连同 Agent 的改动一起合入 {project.targetBranch}。
|
||||
</p>
|
||||
{branches.data?.length ? (
|
||||
<ul className="branch-list">
|
||||
{branches.data.map(branch => (
|
||||
<li key={branch.name}>
|
||||
<GitBranch size={15} />
|
||||
<code>{branch.name}</code>
|
||||
<span>
|
||||
{branch.commit.slice(0, 7)} · {formatTime(branch.updatedAt)}
|
||||
</span>
|
||||
{mayWish ? (
|
||||
<ButtonLink secondary href={`/runs/new?project=${id}&branch=${encodeURIComponent(branch.name)}`}>
|
||||
提交愿望合入
|
||||
</ButtonLink>
|
||||
) : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
{workspaces.data?.length ? (
|
||||
<div className="workspace-list">
|
||||
{workspaces.data.map(workspace => (
|
||||
<WorkspaceRow
|
||||
key={workspace.id}
|
||||
workspace={workspace}
|
||||
onChanged={async updated => {
|
||||
await workspaces.mutate(
|
||||
current => current?.map(item => (item.id === updated.id ? { ...item, ...updated } : item)),
|
||||
{ revalidate: false },
|
||||
);
|
||||
void workspaces.mutate();
|
||||
}}
|
||||
/>
|
||||
))}
|
||||
<Panel title="代码仓库">
|
||||
<dl className="details-list">
|
||||
<KeyValue label="仓库">
|
||||
{safeUrl(project.repositoryUrl) ? (
|
||||
<a className="text-link break-all" href={safeUrl(project.repositoryUrl)} target="_blank" rel="noreferrer">
|
||||
{project.repositoryUrl}
|
||||
<ArrowUpRight size={15} />
|
||||
</a>
|
||||
) : (
|
||||
(project.repositoryUrl ?? '尚未关联')
|
||||
)}
|
||||
</KeyValue>
|
||||
<KeyValue label="目标分支">
|
||||
<span className="inline-icon">
|
||||
<GitBranch size={15} />
|
||||
{project.targetBranch}
|
||||
</span>
|
||||
</KeyValue>
|
||||
<KeyValue label="仓库状态">
|
||||
<Status
|
||||
value={
|
||||
project.repositoryStatus === 'ready'
|
||||
? 'ready'
|
||||
: project.repositoryStatus === 'unavailable'
|
||||
? 'failed'
|
||||
: 'unconfigured'
|
||||
}
|
||||
label={
|
||||
{ ready: '可用', unavailable: '不可用', unconfigured: '未创建' }[project.repositoryStatus ?? 'ready']
|
||||
}
|
||||
/>
|
||||
</KeyValue>
|
||||
<KeyValue label="代码权限">
|
||||
{project.codeReview?.paths.length ? (
|
||||
<>
|
||||
后端代码 <code>{project.codeReview.paths.join('、')}</code>
|
||||
{project.codeReview.exceptPaths.length ? (
|
||||
<>
|
||||
(放行 <code>{project.codeReview.exceptPaths.join('、')}</code>)
|
||||
</>
|
||||
) : null}
|
||||
算后端代码:只能改前端的人碰不到它,其他人改到它时按各自的权限直接合入或先审核(见
|
||||
<Link className="text-link" href="/members">
|
||||
成员与权限
|
||||
</Link>
|
||||
)
|
||||
</>
|
||||
) : (
|
||||
'不区分前后端,验证通过后直接合入'
|
||||
)}
|
||||
</KeyValue>
|
||||
<KeyValue label="创建时间">{formatTime(project.createdAt)}</KeyValue>
|
||||
<KeyValue label="项目 ID">
|
||||
<code>{project.id}</code>
|
||||
</KeyValue>
|
||||
</dl>
|
||||
{project.repositoryStatus && project.repositoryStatus !== 'ready' ? (
|
||||
<>
|
||||
<Notice tone="warning">
|
||||
{project.repositoryError ?? '平台 Git 仓库尚未创建,提交的愿望会等待仓库就绪。'}
|
||||
</Notice>
|
||||
<div className="panel-bottom-action">
|
||||
<Button variant="secondary" busy={busy} onClick={() => void retryRepository()}>
|
||||
重试创建仓库
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<Notice>尚无可用的 IDE 会话。需要先连接支持工作空间的运行环境。</Notice>
|
||||
<div className="panel-bottom-action">
|
||||
<Button variant="secondary" busy={busy} onClick={createWorkspace}>
|
||||
<Plus size={16} />
|
||||
创建开发环境
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
{workspaces.error ? (
|
||||
<p className="field-hint">
|
||||
{workspaces.error.status === 404 ? '工作空间服务尚未启用。' : workspaces.error.message}
|
||||
</p>
|
||||
) : null}
|
||||
<FormError message={actionError} />
|
||||
</Panel>
|
||||
</div>
|
||||
</>
|
||||
) : null}
|
||||
<FormError message={actionError} />
|
||||
</Panel>
|
||||
<Panel
|
||||
title="项目运行"
|
||||
aside={
|
||||
@ -389,8 +310,7 @@ export function ProjectView({ id }: { id: string }) {
|
||||
<RunTable runs={runs.data ?? []} projects={[project]} />
|
||||
)}
|
||||
</Panel>
|
||||
<SkillsPanel projectId={id} />
|
||||
<LessonsPanel projectId={id} />
|
||||
<ProjectSkillsSummary projectId={id} />
|
||||
{showSettings ? (
|
||||
<ProjectSettings
|
||||
project={project}
|
||||
@ -404,130 +324,3 @@ export function ProjectView({ id }: { id: string }) {
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function WorkspaceRow({
|
||||
workspace,
|
||||
onChanged,
|
||||
}: {
|
||||
workspace: Workspace;
|
||||
onChanged: (workspace: Workspace) => Promise<void>;
|
||||
}) {
|
||||
const [confirmPause, setConfirmPause] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const operationKey = useRef<{ action: 'pause' | 'resume'; key: string } | null>(null);
|
||||
const managed = workspace.managedBy === 'wishforge-provisioner';
|
||||
const reconciling = ['waiting', 'pausing', 'resuming', 'unknown'].includes(workspace.status);
|
||||
function closePause() {
|
||||
if (busy) return;
|
||||
setConfirmPause(false);
|
||||
setError('');
|
||||
}
|
||||
async function change(action: 'pause' | 'resume') {
|
||||
if (busy || !managed) return;
|
||||
if (action === 'pause' && workspace.status !== 'ready') {
|
||||
setError('工作区状态已变化,请等待对账完成后重试。');
|
||||
return;
|
||||
}
|
||||
if (operationKey.current?.action !== action) {
|
||||
operationKey.current = { action, key: browserId() };
|
||||
}
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
const updated = await mutateApi<Workspace>(
|
||||
`/workspaces/${workspace.id}/lifecycle`,
|
||||
{ action, confirmation: 'change-workspace-lifecycle' },
|
||||
'POST',
|
||||
operationKey.current.key,
|
||||
);
|
||||
if (
|
||||
updated.id !== workspace.id ||
|
||||
!['ready', 'paused', 'pausing', 'resuming', 'unknown'].includes(updated.status)
|
||||
) {
|
||||
throw new Error('控制面返回了无法确认的工作区状态,请刷新后核对。');
|
||||
}
|
||||
await onChanged(updated);
|
||||
operationKey.current = null;
|
||||
setConfirmPause(false);
|
||||
} catch (error) {
|
||||
setError(messageOf(error));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<div data-workspace-id={workspace.id}>
|
||||
<div className="workspace-row">
|
||||
<div>
|
||||
<strong>开发环境</strong>
|
||||
<Status value={workspace.status} label={reconciling ? '对账中' : undefined} />
|
||||
{workspace.reason ? <p>{workspace.reason}</p> : null}
|
||||
{reconciling ? <p role="status">工作区对账中,确认完成后才能打开 IDE。</p> : null}
|
||||
{workspace.status === 'paused' ? <p>工作区已暂停,已保存的文件保留。</p> : null}
|
||||
</div>
|
||||
<div className="button-group">
|
||||
{workspace.status === 'ready' ? (
|
||||
<a
|
||||
className="button button-secondary"
|
||||
href={`/ide/${workspace.id}/launch`}
|
||||
target="_blank"
|
||||
rel="noreferrer"
|
||||
>
|
||||
打开 IDE
|
||||
<ArrowUpRight size={15} />
|
||||
</a>
|
||||
) : !reconciling && workspace.status !== 'paused' ? (
|
||||
<span className="muted text-small">环境尚未就绪</span>
|
||||
) : null}
|
||||
{managed && workspace.status === 'ready' ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
disabled={busy}
|
||||
onClick={() => {
|
||||
setError('');
|
||||
setConfirmPause(true);
|
||||
}}
|
||||
>
|
||||
暂停工作区
|
||||
</Button>
|
||||
) : null}
|
||||
{managed && workspace.status === 'paused' ? (
|
||||
<Button variant="secondary" busy={busy} onClick={() => void change('resume')}>
|
||||
恢复工作区
|
||||
</Button>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
{!confirmPause ? <FormError message={error} /> : null}
|
||||
{confirmPause ? (
|
||||
<Modal
|
||||
title="暂停这个工作区?"
|
||||
description="暂停后工作区不再占用计算资源,恢复后可以继续使用。"
|
||||
onClose={closePause}
|
||||
>
|
||||
<div className="form-stack">
|
||||
<KeyValue label="工作区">
|
||||
<code className="break-all">{workspace.id}</code>
|
||||
</KeyValue>
|
||||
<Notice>暂停会结束终端和调试进程,编辑器中未保存的修改会丢失;已保存的文件保留在工作区存储中。</Notice>
|
||||
<FormError message={error} />
|
||||
<div className="modal-actions">
|
||||
<Button variant="secondary" disabled={busy} onClick={closePause}>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
variant="danger"
|
||||
busy={busy}
|
||||
disabled={workspace.status !== 'ready'}
|
||||
onClick={() => void change('pause')}
|
||||
>
|
||||
暂停工作区
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
'use client';
|
||||
import { ArrowRight, Check, CheckCircle2, Cpu, KeyRound, Minus, Terminal } from 'lucide-react';
|
||||
import { ArrowRight, Check, CheckCircle2, Cpu, KeyRound, Minus, Settings2, Terminal } from 'lucide-react';
|
||||
import { useState } from 'react';
|
||||
import { providerCatalog } from '@wishforge/contracts/providers';
|
||||
import { EXECUTOR_TIMEOUT_MINUTES, providerCatalog, type ExecutorSettingField } from '@wishforge/contracts/providers';
|
||||
import { MODEL_NAME_PATTERN } from '@wishforge/contracts/models';
|
||||
import { messageOf, mutateApi, useResource } from '@/lib/api';
|
||||
import { credentialSourceLabel } from '@/lib/readiness';
|
||||
import type { ProviderConnection, ProviderInfo } from '@/lib/types';
|
||||
import type { ExecutorSettings, ProviderConnection, ProviderInfo } from '@/lib/types';
|
||||
import {
|
||||
Button,
|
||||
EmptyState,
|
||||
@ -35,6 +36,10 @@ export function ProvidersView() {
|
||||
const { data: cachedData, error, isLoading, isValidating, mutate } = useResource<ProviderInfo[]>('/providers', 10000);
|
||||
const data = error && [401, 403].includes(error.status) ? undefined : cachedData;
|
||||
const [selected, setSelected] = useState<ProviderInfo | null>(null);
|
||||
const [configuring, setConfiguring] = useState<ProviderInfo | null>(null);
|
||||
// Owners and admins only; for anyone else the request fails and the settings stay hidden.
|
||||
const settings = useResource<ExecutorSettings[]>('/executor-settings');
|
||||
const settingsOf = (id: string) => settings.data?.find(entry => entry.provider === id);
|
||||
return (
|
||||
<>
|
||||
<PageHeader title="模型连接" description="连接模型与执行引擎,按真实能力调度每一次任务。" />
|
||||
@ -77,21 +82,31 @@ export function ProvidersView() {
|
||||
? '以下配置来自上次成功读取,当前状态尚未确认。'
|
||||
: credentialSourceLabel(provider.credentialSource)}
|
||||
</p>
|
||||
<ExecutorSettingsSummary settings={settingsOf(provider.id)} />
|
||||
<div className="provider-bottom">
|
||||
<p>{provider.reason || '连接配置已由控制面确认。'}</p>
|
||||
{provider.credentialSource === 'native_account' ? (
|
||||
<p>本机账号由运行主机维护,账号与模型权限会在任务启动时检查。</p>
|
||||
) : (
|
||||
<Button variant="secondary" disabled={Boolean(error)} onClick={() => setSelected(provider)}>
|
||||
<KeyRound size={15} />
|
||||
{provider.credentialSource === 'mixed'
|
||||
? '管理租户 API Key'
|
||||
: provider.credentialSource === 'tenant_credential'
|
||||
? '更新 API Key'
|
||||
: '配置 API Key'}
|
||||
<ArrowRight size={14} />
|
||||
</Button>
|
||||
)}
|
||||
) : null}
|
||||
<div className="provider-actions">
|
||||
{settingsOf(provider.id) ? (
|
||||
<Button variant="secondary" disabled={Boolean(error)} onClick={() => setConfiguring(provider)}>
|
||||
<Settings2 size={15} />
|
||||
执行器设置
|
||||
</Button>
|
||||
) : null}
|
||||
{provider.credentialSource === 'native_account' ? null : (
|
||||
<Button variant="secondary" disabled={Boolean(error)} onClick={() => setSelected(provider)}>
|
||||
<KeyRound size={15} />
|
||||
{provider.credentialSource === 'mixed'
|
||||
? '管理租户 API Key'
|
||||
: provider.credentialSource === 'tenant_credential'
|
||||
? '更新 API Key'
|
||||
: '配置 API Key'}
|
||||
<ArrowRight size={14} />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
))}
|
||||
@ -114,9 +129,181 @@ export function ProvidersView() {
|
||||
{selected ? (
|
||||
<CredentialDialog provider={selected} onClose={() => setSelected(null)} onSaved={() => void mutate()} />
|
||||
) : null}
|
||||
{configuring && settingsOf(configuring.id) ? (
|
||||
<ExecutorSettingsDialog
|
||||
provider={configuring}
|
||||
settings={settingsOf(configuring.id)!}
|
||||
onClose={() => setConfiguring(null)}
|
||||
onSaved={() => void settings.mutate()}
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
/** The console's words for each executor setting, in catalog order. */
|
||||
const settingFields: Record<
|
||||
ExecutorSettingField,
|
||||
{ label: string; hint: string; summary: string; kind: 'url' | 'model' | 'minutes' }
|
||||
> = {
|
||||
baseUrl: {
|
||||
label: '模型网关地址',
|
||||
summary: '网关',
|
||||
hint: '只随本工作空间的 API Key 一起使用,需为 HTTPS。留空则使用运行环境配置的网关。',
|
||||
kind: 'url',
|
||||
},
|
||||
defaultModel: {
|
||||
label: '默认模型',
|
||||
summary: '默认模型',
|
||||
hint: '许愿和 Agent 配置都没有指定模型时使用。留空则使用运行环境的默认模型。',
|
||||
kind: 'model',
|
||||
},
|
||||
smallFastModel: {
|
||||
label: '快速模型',
|
||||
summary: '快速模型',
|
||||
hint: 'Claude Code 处理后台小任务(如生成标题、摘要)时使用的模型。留空则沿用运行环境配置。',
|
||||
kind: 'model',
|
||||
},
|
||||
fallbackModel: {
|
||||
label: '备用模型',
|
||||
summary: '备用模型',
|
||||
hint: '主模型过载或不可用时自动改用这个模型。留空则不切换。',
|
||||
kind: 'model',
|
||||
},
|
||||
timeoutMinutes: {
|
||||
label: '单次执行时限(分钟)',
|
||||
summary: '时限',
|
||||
hint: `Agent 一次执行最多运行多久,${EXECUTOR_TIMEOUT_MINUTES.min} 到 ${EXECUTOR_TIMEOUT_MINUTES.max} 分钟。留空为 ${EXECUTOR_TIMEOUT_MINUTES.default} 分钟。`,
|
||||
kind: 'minutes',
|
||||
},
|
||||
askModel: {
|
||||
label: 'AI 优化模型',
|
||||
summary: 'AI 优化',
|
||||
hint: '许愿时「AI 优化」改写愿望和验收标准所用的模型。留空则使用运行环境配置。',
|
||||
kind: 'model',
|
||||
},
|
||||
askFallbackModel: {
|
||||
label: 'AI 优化备用模型',
|
||||
summary: 'AI 优化备用',
|
||||
hint: 'AI 优化模型超时或没有给出可用结果时改用的模型。留空则使用运行环境配置。',
|
||||
kind: 'model',
|
||||
},
|
||||
};
|
||||
const baseUrlBlockedHints: Record<NonNullable<ExecutorSettings['baseUrlBlocked']>, string> = {
|
||||
native_account: '本执行器使用运行主机的本机账号,模型网关由运行主机配置,不能在这里修改。',
|
||||
operator_managed: '本部署的模型网关由运营方统一管理,不能在这里修改。',
|
||||
};
|
||||
function ExecutorSettingsSummary({ settings }: { settings?: ExecutorSettings }) {
|
||||
if (!settings) return null;
|
||||
const set = settings.supported.filter(field => settings[field] !== undefined && settings[field] !== '');
|
||||
if (!set.length) return null;
|
||||
return (
|
||||
<p className="panel-intro">
|
||||
执行器设置:
|
||||
{set
|
||||
.map(
|
||||
field =>
|
||||
`${settingFields[field].summary} ${field === 'timeoutMinutes' ? `${settings[field]} 分钟` : settings[field]}`,
|
||||
)
|
||||
.join(';')}
|
||||
</p>
|
||||
);
|
||||
}
|
||||
function ExecutorSettingsDialog({
|
||||
provider,
|
||||
settings,
|
||||
onClose,
|
||||
onSaved,
|
||||
}: {
|
||||
provider: ProviderInfo;
|
||||
settings: ExecutorSettings;
|
||||
onClose: () => void;
|
||||
onSaved: () => void;
|
||||
}) {
|
||||
const [form, setForm] = useState<Partial<Record<ExecutorSettingField, string>>>(() =>
|
||||
Object.fromEntries(settings.supported.map(field => [field, String(settings[field] ?? '')])),
|
||||
);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
async function submit(event: React.FormEvent) {
|
||||
event.preventDefault();
|
||||
setBusy(true);
|
||||
setError('');
|
||||
// The whole record is replaced: a blank field goes back to the operator's configuration.
|
||||
const body = Object.fromEntries(
|
||||
settings.supported.flatMap(field => {
|
||||
const value = form[field]?.trim();
|
||||
if (!value || (field === 'baseUrl' && !settings.baseUrlAllowed)) return [];
|
||||
return [[field, field === 'timeoutMinutes' ? Number(value) : value]];
|
||||
}),
|
||||
);
|
||||
try {
|
||||
await mutateApi(`/executor-settings/${provider.id}`, body, 'PUT');
|
||||
onSaved();
|
||||
onClose();
|
||||
} catch (error) {
|
||||
setError(messageOf(error));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<Modal
|
||||
title={`${provider.name} 执行器设置`}
|
||||
description="只影响本工作空间之后发起的运行。留空的项目沿用运行环境的配置。"
|
||||
onClose={onClose}
|
||||
>
|
||||
<form className="form-stack" onSubmit={submit}>
|
||||
{settings.supported.map(field => {
|
||||
const { label, hint, kind } = settingFields[field];
|
||||
const blocked = field === 'baseUrl' && !settings.baseUrlAllowed;
|
||||
return (
|
||||
<Field
|
||||
key={field}
|
||||
label={label}
|
||||
hint={blocked && settings.baseUrlBlocked ? baseUrlBlockedHints[settings.baseUrlBlocked] : hint}
|
||||
>
|
||||
{kind === 'minutes' ? (
|
||||
<input
|
||||
type="number"
|
||||
inputMode="numeric"
|
||||
min={EXECUTOR_TIMEOUT_MINUTES.min}
|
||||
max={EXECUTOR_TIMEOUT_MINUTES.max}
|
||||
step={1}
|
||||
placeholder={String(EXECUTOR_TIMEOUT_MINUTES.default)}
|
||||
value={form[field] ?? ''}
|
||||
onChange={event => setForm(current => ({ ...current, [field]: event.target.value }))}
|
||||
/>
|
||||
) : (
|
||||
<input
|
||||
type={kind === 'url' ? 'url' : 'text'}
|
||||
autoComplete="off"
|
||||
spellCheck={false}
|
||||
maxLength={kind === 'url' ? 2048 : 160}
|
||||
disabled={blocked}
|
||||
{...(kind === 'model'
|
||||
? { pattern: MODEL_NAME_PATTERN, placeholder: '例如 deepseek-pro@latest[1m]' }
|
||||
: { placeholder: 'https://<网关地址>' })}
|
||||
value={form[field] ?? ''}
|
||||
onChange={event => setForm(current => ({ ...current, [field]: event.target.value }))}
|
||||
/>
|
||||
)}
|
||||
</Field>
|
||||
);
|
||||
})}
|
||||
<FormError message={error} />
|
||||
<div className="modal-actions">
|
||||
<Button type="button" variant="secondary" onClick={onClose}>
|
||||
取消
|
||||
</Button>
|
||||
<Button busy={busy}>
|
||||
<Settings2 size={15} />
|
||||
保存设置
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
function CredentialDialog({
|
||||
provider,
|
||||
onClose,
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
import Link from 'next/link';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import {
|
||||
ArrowRight,
|
||||
@ -11,6 +11,7 @@ import {
|
||||
FileDown,
|
||||
GitBranch,
|
||||
LockOpen,
|
||||
Paperclip,
|
||||
Pause,
|
||||
Play,
|
||||
Plus,
|
||||
@ -27,7 +28,6 @@ import { downloadApi, formatTime, messageOf, mutateApi, safeUrl, shortId, useRes
|
||||
import type {
|
||||
CommandEvidence,
|
||||
Evidence,
|
||||
IdeBranch,
|
||||
Identity,
|
||||
RunArtifacts,
|
||||
RunDelivery,
|
||||
@ -66,6 +66,14 @@ import { credentialSourceLabel, initialWishProvider, wishProviderReady } from '@
|
||||
import { personName } from '@/lib/people';
|
||||
import { mayWishBackend, useMayWish, wishScopeFor } from '@/lib/permissions';
|
||||
import type { CodeReview } from '@/lib/types';
|
||||
import { inlineImageTypes } from '@wishforge/contracts/attachments';
|
||||
import {
|
||||
addAttachments,
|
||||
attachmentLimitsText,
|
||||
formatBytes,
|
||||
pastedFiles,
|
||||
type PendingAttachment,
|
||||
} from '@/lib/attachments';
|
||||
|
||||
export function RunsView() {
|
||||
const mayWish = useMayWish();
|
||||
@ -422,6 +430,86 @@ function RefineAction({
|
||||
</div>
|
||||
);
|
||||
}
|
||||
/** Files for the agent to look at: chosen, dropped, or pasted into the wish. */
|
||||
function AttachmentField({
|
||||
attachments,
|
||||
onAdd,
|
||||
onRemove,
|
||||
problem,
|
||||
}: {
|
||||
attachments: PendingAttachment[];
|
||||
onAdd: (files: File[]) => void;
|
||||
onRemove: (key: string) => void;
|
||||
problem?: string;
|
||||
}) {
|
||||
const [dragging, setDragging] = useState(false);
|
||||
return (
|
||||
<div className="field">
|
||||
<span className="field-label">附件</span>
|
||||
<div
|
||||
className={dragging ? 'attachment-drop dragging' : 'attachment-drop'}
|
||||
onDragOver={event => {
|
||||
if (![...event.dataTransfer.types].includes('Files')) return;
|
||||
event.preventDefault();
|
||||
setDragging(true);
|
||||
}}
|
||||
onDragLeave={() => setDragging(false)}
|
||||
onDrop={event => {
|
||||
event.preventDefault();
|
||||
setDragging(false);
|
||||
onAdd([...event.dataTransfer.files]);
|
||||
}}
|
||||
>
|
||||
<label className="button button-secondary attachment-pick">
|
||||
<Paperclip size={15} />
|
||||
添加文件
|
||||
<input
|
||||
type="file"
|
||||
multiple
|
||||
aria-label="添加附件"
|
||||
onChange={event => {
|
||||
onAdd([...(event.target.files ?? [])]);
|
||||
event.target.value = '';
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
<span className="field-hint">
|
||||
截图、设计稿、日志或文档都可以:点按钮选择、拖到这里,或在「你的愿望」「验收标准」里直接粘贴截图。
|
||||
{attachmentLimitsText}。
|
||||
</span>
|
||||
</div>
|
||||
<FormError message={problem} />
|
||||
{attachments.length ? (
|
||||
<ul className="attachment-list" aria-label="已添加的附件">
|
||||
{attachments.map(file => (
|
||||
<li key={file.key}>
|
||||
{file.preview ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img src={file.preview} alt="" />
|
||||
) : (
|
||||
<span className="attachment-icon">
|
||||
<Paperclip size={16} />
|
||||
</span>
|
||||
)}
|
||||
<span className="attachment-name" title={file.name}>
|
||||
{file.name}
|
||||
<small>{formatBytes(file.size)}</small>
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="icon-button"
|
||||
aria-label={`移除附件 ${file.name}`}
|
||||
onClick={() => onRemove(file.key)}
|
||||
>
|
||||
<X size={15} />
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
export function NewWishView() {
|
||||
const projects = useResource<Project[]>('/projects');
|
||||
const providers = useResource<ProviderInfo[]>('/providers');
|
||||
@ -435,7 +523,6 @@ export function NewWishView() {
|
||||
const [chosenMaxTokens, setMaxTokens] = useState<number>();
|
||||
const [maxMinutes, setMaxMinutes] = useState(30);
|
||||
const [maxRepairRounds, setMaxRepairRounds] = useState(3);
|
||||
const [sourceBranch, setSourceBranch] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [idempotencyKey] = useState(browserId);
|
||||
@ -446,12 +533,46 @@ export function NewWishView() {
|
||||
const [frontendOnly, setFrontendOnly] = useState(true);
|
||||
const [refining, setRefining] = useState<RefineFocus>();
|
||||
const [suggestion, setSuggestion] = useState<WishSuggestion & { focus: RefineFocus }>();
|
||||
const [attachments, setAttachments] = useState<PendingAttachment[]>([]);
|
||||
const [attachmentProblem, setAttachmentProblem] = useState<string>();
|
||||
const [reading, setReading] = useState(0);
|
||||
// Adds run one after another on the latest list, so a paste and a drop in quick succession both count.
|
||||
const latestAttachments = useRef<PendingAttachment[]>([]);
|
||||
const attaching = useRef<Promise<void>>(Promise.resolve());
|
||||
function attach(files: File[]) {
|
||||
if (!files.length) return;
|
||||
setReading(count => count + 1);
|
||||
attaching.current = attaching.current.then(async () => {
|
||||
try {
|
||||
const result = await addAttachments(latestAttachments.current, files);
|
||||
latestAttachments.current = result.attachments;
|
||||
setAttachments(result.attachments);
|
||||
setAttachmentProblem(result.problem);
|
||||
} catch {
|
||||
setAttachmentProblem('读取文件失败,请重试。');
|
||||
} finally {
|
||||
setReading(count => count - 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
function detach(key: string) {
|
||||
const removed = latestAttachments.current.find(file => file.key === key);
|
||||
if (removed?.preview) URL.revokeObjectURL(removed.preview);
|
||||
latestAttachments.current = latestAttachments.current.filter(file => file.key !== key);
|
||||
setAttachments(latestAttachments.current);
|
||||
setAttachmentProblem(undefined);
|
||||
}
|
||||
/** A screenshot pasted into a text box becomes an attachment; text pastes as usual. */
|
||||
const onPaste = (event: React.ClipboardEvent) => {
|
||||
const files = pastedFiles(event.clipboardData);
|
||||
if (!files.length) return;
|
||||
event.preventDefault();
|
||||
attach(files);
|
||||
};
|
||||
useEffect(() => {
|
||||
const query = new URLSearchParams(window.location.search);
|
||||
const selected = query.get('project');
|
||||
if (selected) setProjectId(selected);
|
||||
const branch = query.get('branch');
|
||||
if (branch) setSourceBranch(branch);
|
||||
}, []);
|
||||
const effectiveProject = projectId || projects.data?.[0]?.id || '';
|
||||
const project = projects.data?.find(item => item.id === effectiveProject);
|
||||
@ -471,8 +592,6 @@ export function NewWishView() {
|
||||
const scope = wishScopeFor(identity.data, project);
|
||||
const mayWish = scope !== 'none';
|
||||
const maxTokens = chosenMaxTokens ?? (localPoc && provider === 'claude-code' ? 100000000 : 300000);
|
||||
const branches = useResource<IdeBranch[]>(effectiveProject ? `/projects/${effectiveProject}/branches` : null);
|
||||
const targetBranch = projects.data?.find(project => project.id === effectiveProject)?.targetBranch ?? 'main';
|
||||
const selectedProvider = providers.data?.find(item => item.id === provider);
|
||||
const providerReady = wishProviderReady(selectedProvider, providers.error, projects.error);
|
||||
// Executors this deployment cannot run stay out of the way; the chosen one always shows.
|
||||
@ -527,8 +646,10 @@ export function NewWishView() {
|
||||
provider,
|
||||
...(model ? { model } : {}),
|
||||
budget: { maxTokens, maxMinutes, maxRepairRounds },
|
||||
...(sourceBranch ? { sourceBranch } : {}),
|
||||
...(scopePolicy ? { frontendOnly: frontendOnly || scope === 'frontend' } : {}),
|
||||
...(attachments.length
|
||||
? { attachments: attachments.map(({ name, mediaType, data }) => ({ name, mediaType, data })) }
|
||||
: {}),
|
||||
},
|
||||
'POST',
|
||||
idempotencyKey,
|
||||
@ -558,7 +679,6 @@ export function NewWishView() {
|
||||
value={effectiveProject}
|
||||
onChange={event => {
|
||||
setProjectId(event.target.value);
|
||||
setSourceBranch('');
|
||||
setProvider(undefined);
|
||||
}}
|
||||
>
|
||||
@ -587,9 +707,11 @@ export function NewWishView() {
|
||||
}
|
||||
value={goal}
|
||||
onChange={event => setGoal(event.target.value)}
|
||||
onPaste={onPaste}
|
||||
/>
|
||||
</Field>
|
||||
{refineAction('goal')}
|
||||
<AttachmentField attachments={attachments} onAdd={attach} onRemove={detach} problem={attachmentProblem} />
|
||||
{scopePolicy && scope && scope !== 'none' ? (
|
||||
<ScopeChoice
|
||||
policy={scopePolicy}
|
||||
@ -598,22 +720,6 @@ export function NewWishView() {
|
||||
onChange={setFrontendOnly}
|
||||
/>
|
||||
) : null}
|
||||
<Field
|
||||
label="起点"
|
||||
hint={`从 IDE 推送的分支开始时,Agent 在这些改动的基础上工作,验证通过后一并合入 ${targetBranch}。`}
|
||||
>
|
||||
<select value={sourceBranch} onChange={event => setSourceBranch(event.target.value)}>
|
||||
<option value="">{targetBranch} 的最新代码</option>
|
||||
{sourceBranch && !branches.data?.some(branch => branch.name === sourceBranch) ? (
|
||||
<option value={sourceBranch}>{sourceBranch}</option>
|
||||
) : null}
|
||||
{branches.data?.map(branch => (
|
||||
<option value={branch.name} key={branch.name}>
|
||||
{branch.name} · {branch.commit.slice(0, 7)} · {formatTime(branch.updatedAt)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
<Field
|
||||
label="验收标准"
|
||||
hint="每行一条,写成能检查的句子。标准会交给 Agent,并在审核和审批时展示;能否通过以项目的受保护检查为准。"
|
||||
@ -624,6 +730,7 @@ export function NewWishView() {
|
||||
placeholder={'搜索结果遵循当前用户的项目权限\n筛选条件在刷新后保留\n新增功能通过集成测试'}
|
||||
value={criteria}
|
||||
onChange={event => setCriteria(event.target.value)}
|
||||
onPaste={onPaste}
|
||||
/>
|
||||
</Field>
|
||||
{refineAction('criteria')}
|
||||
@ -738,7 +845,9 @@ export function NewWishView() {
|
||||
<p>提交后可在运行详情中补充信息、暂停或取消。</p>
|
||||
<Button
|
||||
busy={busy}
|
||||
disabled={!effectiveProject || !goal.trim() || !criteria.trim() || !providerReady || !mayWish}
|
||||
disabled={
|
||||
!effectiveProject || !goal.trim() || !criteria.trim() || !providerReady || !mayWish || reading > 0
|
||||
}
|
||||
>
|
||||
<Send size={16} />
|
||||
提交愿望
|
||||
@ -941,7 +1050,7 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
.then(created =>
|
||||
setSkillNotice(
|
||||
created.length
|
||||
? `提炼出 ${created.length} 条技能,已加入项目技能,等待确认。`
|
||||
? `提炼出 ${created.length} 条技能(新增或更新了已有的),已记入知识库。`
|
||||
: '这次运行里没有值得沉淀的新技能。',
|
||||
),
|
||||
)
|
||||
@ -959,8 +1068,8 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
{skillNotice ? (
|
||||
<Notice>
|
||||
{skillNotice}{' '}
|
||||
<Link className="text-link" href={`/projects/${run.projectId}`}>
|
||||
去项目页查看
|
||||
<Link className="text-link" href={`/skills?project=${run.projectId}`}>
|
||||
去知识库查看
|
||||
</Link>
|
||||
</Notice>
|
||||
) : null}
|
||||
@ -1113,6 +1222,34 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
<p className="pre-wrap">{plainText(wishDetails(run.goal))}</p>
|
||||
</Panel>
|
||||
) : null}
|
||||
{tab === 'overview' && run.attachments?.length ? (
|
||||
<Panel title={`附件(${run.attachments.length})`}>
|
||||
<ul className="attachment-list panel-body-list">
|
||||
{run.attachments.map(file => {
|
||||
const href = `/api/runs/${run.id}/attachments/${file.id}`;
|
||||
return (
|
||||
<li key={file.id}>
|
||||
<a href={href} target="_blank" rel="noreferrer" title={file.name}>
|
||||
{inlineImageTypes.includes(file.mediaType) ? (
|
||||
// eslint-disable-next-line @next/next/no-img-element
|
||||
<img src={href} alt={file.name} loading="lazy" />
|
||||
) : (
|
||||
<span className="attachment-icon">
|
||||
<Paperclip size={16} />
|
||||
</span>
|
||||
)}
|
||||
<span className="attachment-name">
|
||||
{file.name}
|
||||
<small>{formatBytes(file.size)}</small>
|
||||
</span>
|
||||
</a>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
<p className="panel-note">提交愿望时附上的文件,执行器会在工作区里读取它们。</p>
|
||||
</Panel>
|
||||
) : null}
|
||||
{tab === 'overview' && run.implementationSummary ? (
|
||||
<Panel title="改动说明">
|
||||
<p className="pre-wrap">{plainText(run.implementationSummary)}</p>
|
||||
|
||||
@ -1,29 +1,94 @@
|
||||
'use client';
|
||||
import Link from 'next/link';
|
||||
import { useState } from 'react';
|
||||
import { Check, Download, Pencil, Plus, Sparkles, Trash2 } from 'lucide-react';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { ArrowRight, Check, Copy, Download, Pencil, Plus, Search, Sparkles, Trash2, Upload } from 'lucide-react';
|
||||
import { downloadApi, formatTime, messageOf, mutateApi, useResource } from '@/lib/api';
|
||||
import { Markdown } from '@/lib/markdown';
|
||||
import type { Skill } from '@/lib/types';
|
||||
import { Button, EmptyState, ErrorNotice, Field, FormError, Loading, Modal, Panel, Status } from '@/components/ui';
|
||||
import type { Project, Skill, SkillCategory, SkillTransfer } from '@/lib/types';
|
||||
import {
|
||||
Button,
|
||||
EmptyState,
|
||||
ErrorNotice,
|
||||
Field,
|
||||
FormError,
|
||||
Loading,
|
||||
Modal,
|
||||
Notice,
|
||||
PageHeader,
|
||||
Panel,
|
||||
Status,
|
||||
} from '@/components/ui';
|
||||
|
||||
const STATUS: Record<Skill['status'], [string, string]> = {
|
||||
proposed: ['waiting', '待确认'],
|
||||
active: ['ready', '启用'],
|
||||
disabled: ['disabled', '已停用'],
|
||||
};
|
||||
type Draft = Pick<Skill, 'name' | 'description' | 'content'>;
|
||||
export const CATEGORY: Record<SkillCategory, string> = {
|
||||
workflow: '做法',
|
||||
pitfall: '踩坑记录',
|
||||
convention: '项目约定',
|
||||
};
|
||||
const RETIRED: Record<NonNullable<Skill['retiredReason']>, string> = {
|
||||
failing: '连续几次用到它的运行都失败了,平台已自动停用',
|
||||
stale: '很久没有进入过提示词(被更新的技能挤掉),平台已自动停用',
|
||||
};
|
||||
type Draft = Pick<Skill, 'name' | 'description' | 'content'> & { category: SkillCategory; projectId: string };
|
||||
|
||||
/** A file's bytes as base64, for the JSON body of an import. */
|
||||
async function base64(file: File) {
|
||||
const bytes = new Uint8Array(await file.arrayBuffer());
|
||||
let binary = '';
|
||||
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
||||
return btoa(binary);
|
||||
}
|
||||
const transferred = (result: SkillTransfer) => {
|
||||
const identical = result.skipped.filter(item => item.reason === 'identical').length;
|
||||
const invalid = result.skipped.filter(item => item.reason === 'invalid');
|
||||
return [
|
||||
`已添加 ${result.created.length} 条技能`,
|
||||
identical ? `跳过 ${identical} 条内容完全相同的` : '',
|
||||
invalid.length ? `${invalid.length} 个文件无法读取(${invalid.map(item => item.source).join('、')})` : '',
|
||||
result.ignored ? `忽略了 ${result.ignored} 个不是 SKILL.md 的文件` : '',
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(',')
|
||||
.concat('。');
|
||||
};
|
||||
|
||||
/**
|
||||
* The project's skills: know-how the platform proposes from finished runs and people confirm,
|
||||
* edit or write themselves. Active skills enter the prompts of the project's later wishes.
|
||||
* The knowledge base: every skill of the projects the person may see. Workflows the platform
|
||||
* proposes from finished runs, pitfalls it records from repaired failures, and conventions people
|
||||
* write all live here; active ones enter the prompts of their project's later wishes.
|
||||
*/
|
||||
export function SkillsPanel({ projectId }: { projectId: string }) {
|
||||
const skills = useResource<Skill[]>(`/projects/${projectId}/skills`, 15000);
|
||||
export function SkillsLibrary() {
|
||||
const projects = useResource<Project[]>('/projects');
|
||||
const [project, setProject] = useState('all');
|
||||
const [status, setStatus] = useState('all');
|
||||
const [category, setCategory] = useState('all');
|
||||
const [search, setSearch] = useState('');
|
||||
useEffect(() => {
|
||||
const query = new URLSearchParams(window.location.search);
|
||||
const selected = query.get('project');
|
||||
if (selected) setProject(selected);
|
||||
const wanted = query.get('status');
|
||||
if (wanted && wanted in STATUS) setStatus(wanted);
|
||||
}, []);
|
||||
const params = new URLSearchParams({
|
||||
...(project !== 'all' ? { project } : {}),
|
||||
...(status !== 'all' ? { status } : {}),
|
||||
...(category !== 'all' ? { category } : {}),
|
||||
}).toString();
|
||||
const skills = useResource<Skill[]>(`/skills${params ? `?${params}` : ''}`, 15000);
|
||||
const [editing, setEditing] = useState<Skill | 'new'>();
|
||||
const [importing, setImporting] = useState(false);
|
||||
const [copying, setCopying] = useState(false);
|
||||
const [selected, setSelected] = useState<string[]>([]);
|
||||
const [open, setOpen] = useState<string>();
|
||||
const [busy, setBusy] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [notice, setNotice] = useState('');
|
||||
const projectName = (id: string) => projects.data?.find(item => item.id === id)?.name ?? '未知项目';
|
||||
async function run(key: string, action: () => Promise<unknown>) {
|
||||
setBusy(key);
|
||||
setError('');
|
||||
@ -38,154 +103,252 @@ export function SkillsPanel({ projectId }: { projectId: string }) {
|
||||
setBusy('');
|
||||
}
|
||||
}
|
||||
const proposed = skills.data?.filter(skill => skill.status === 'proposed').length ?? 0;
|
||||
const exportable = skills.data?.some(skill => skill.status === 'active');
|
||||
const words = search.trim().toLowerCase();
|
||||
const shown = useMemo(
|
||||
() =>
|
||||
(skills.data ?? []).filter(
|
||||
skill => !words || `${skill.name} ${skill.description} ${skill.content}`.toLowerCase().includes(words),
|
||||
),
|
||||
[skills.data, words],
|
||||
);
|
||||
const proposed = shown.filter(skill => skill.status === 'proposed').length;
|
||||
const exportable = project !== 'all' && shown.some(skill => skill.status === 'active');
|
||||
return (
|
||||
<Panel
|
||||
title="项目技能"
|
||||
aside={
|
||||
<div className="button-group">
|
||||
<Button
|
||||
variant="ghost"
|
||||
disabled={!exportable}
|
||||
busy={busy === 'export'}
|
||||
onClick={() => void run('export', () => downloadApi(`/projects/${projectId}/skills/export`, 'skills.zip'))}
|
||||
>
|
||||
<Download size={15} />
|
||||
导出全部
|
||||
</Button>
|
||||
<Button variant="ghost" onClick={() => setEditing('new')}>
|
||||
<Plus size={15} />
|
||||
新建技能
|
||||
</Button>
|
||||
<>
|
||||
<PageHeader
|
||||
title="知识库"
|
||||
description="项目技能:平台从完成的运行里提炼的做法、从修复过的失败里记下的坑,以及大家写下的项目约定。启用中的技能会进入所属项目之后每个愿望的提示词。"
|
||||
action={
|
||||
<div className="button-group">
|
||||
<Button variant="secondary" onClick={() => setImporting(true)}>
|
||||
<Upload size={16} />
|
||||
导入
|
||||
</Button>
|
||||
<Button onClick={() => setEditing('new')}>
|
||||
<Plus size={16} />
|
||||
新建技能
|
||||
</Button>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
{projects.error ? <ErrorNotice error={projects.error} retry={() => void projects.mutate()} /> : null}
|
||||
{notice ? <Notice tone="success">{notice}</Notice> : null}
|
||||
<Panel>
|
||||
<div className="list-toolbar wrap">
|
||||
<div className="search-field">
|
||||
<Search size={17} />
|
||||
<input
|
||||
aria-label="搜索技能"
|
||||
placeholder="搜索名称、用途或做法…"
|
||||
value={search}
|
||||
onChange={event => setSearch(event.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="filter-controls">
|
||||
<select aria-label="筛选项目" value={project} onChange={event => setProject(event.target.value)}>
|
||||
<option value="all">全部项目</option>
|
||||
{projects.data?.map(item => (
|
||||
<option key={item.id} value={item.id}>
|
||||
{item.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<select aria-label="筛选状态" value={status} onChange={event => setStatus(event.target.value)}>
|
||||
<option value="all">全部状态</option>
|
||||
<option value="proposed">待确认</option>
|
||||
<option value="active">启用</option>
|
||||
<option value="disabled">已停用</option>
|
||||
</select>
|
||||
<select aria-label="筛选类型" value={category} onChange={event => setCategory(event.target.value)}>
|
||||
<option value="all">全部类型</option>
|
||||
{Object.entries(CATEGORY).map(([value, label]) => (
|
||||
<option key={value} value={value}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<p className="panel-intro">
|
||||
平台会从完成的运行里提炼这个项目可复用的做法,作为「待确认」的技能;确认后,之后的愿望都会参考启用中的技能(最近修改的
|
||||
8 条)。可以编辑、停用、删除,或导出为 SKILL.md(全部导出为 zip,可直接放进 .claude/skills/)。
|
||||
{proposed ? <strong> 有 {proposed} 条待确认。</strong> : null}
|
||||
</p>
|
||||
{skills.error ? (
|
||||
<ErrorNotice error={skills.error} retry={() => void skills.mutate()} />
|
||||
) : skills.isLoading ? (
|
||||
<Loading />
|
||||
) : skills.data?.length ? (
|
||||
<ul className="skill-list">
|
||||
{skills.data.map(skill => {
|
||||
const [tone, label] = STATUS[skill.status];
|
||||
return (
|
||||
<li key={skill.id} className={skill.status === 'proposed' ? 'skill-proposed' : undefined}>
|
||||
<Sparkles size={18} />
|
||||
<div className="skill-body">
|
||||
<p>
|
||||
<strong>{skill.name}</strong> <Status value={tone} label={label} />
|
||||
</p>
|
||||
<p>{skill.description}</p>
|
||||
<p className="muted text-small">
|
||||
{skill.createdBy === 'wishforge' ? '平台提炼' : '手动添加'} · 更新于 {formatTime(skill.updatedAt)}
|
||||
{skill.sourceRunIds.map(runId => (
|
||||
<span key={runId}>
|
||||
{' · '}
|
||||
<Link className="text-link" href={`/runs/${runId}`}>
|
||||
来源运行
|
||||
</Link>
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
className="text-button"
|
||||
onClick={() => setOpen(open === skill.id ? undefined : skill.id)}
|
||||
>
|
||||
{open === skill.id ? '收起做法' : '查看做法'}
|
||||
</button>
|
||||
{open === skill.id ? (
|
||||
<div className="markdown skill-content">
|
||||
<Markdown source={skill.content} />
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="button-group">
|
||||
{skill.status === 'proposed' ? (
|
||||
<Button
|
||||
variant="secondary"
|
||||
busy={busy === `confirm:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`confirm:${skill.id}`, () =>
|
||||
mutateApi(`/skills/${skill.id}`, { status: 'active' }, 'PATCH'),
|
||||
)
|
||||
}
|
||||
>
|
||||
<Check size={15} />
|
||||
确认启用
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
busy={busy === `status:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`status:${skill.id}`, () =>
|
||||
mutateApi(
|
||||
`/skills/${skill.id}`,
|
||||
{ status: skill.status === 'active' ? 'disabled' : 'active' },
|
||||
'PATCH',
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
{skill.status === 'active' ? '停用' : '启用'}
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" aria-label="编辑这个技能" onClick={() => setEditing(skill)}>
|
||||
<Pencil size={15} />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
aria-label="导出 SKILL.md"
|
||||
busy={busy === `export:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`export:${skill.id}`, () =>
|
||||
downloadApi(`/skills/${skill.id}/export`, `${skill.name}.SKILL.md`),
|
||||
<div className="list-toolbar wrap">
|
||||
<p className="muted text-small">
|
||||
每个项目最多 12
|
||||
条启用中的技能(最近修改的优先)进入提示词。项目设置里可以关闭「自动启用」:默认情况下,修复过的失败记成的踩坑记录、以及被两次成功运行提炼出的技能会自动启用,其余等待确认。
|
||||
{proposed ? <strong> 当前有 {proposed} 条待确认。</strong> : null}
|
||||
</p>
|
||||
<div className="button-group">
|
||||
<Button
|
||||
variant="ghost"
|
||||
disabled={!selected.length}
|
||||
onClick={() => {
|
||||
setNotice('');
|
||||
setCopying(true);
|
||||
}}
|
||||
>
|
||||
<Copy size={15} />
|
||||
复制到其他项目{selected.length ? `(${selected.length})` : ''}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
disabled={!exportable}
|
||||
title={project === 'all' ? '先选择一个项目' : undefined}
|
||||
busy={busy === 'export'}
|
||||
onClick={() => void run('export', () => downloadApi(`/projects/${project}/skills/export`, 'skills.zip'))}
|
||||
>
|
||||
<Download size={15} />
|
||||
导出项目技能
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
{!editing && !importing && !copying ? <FormError message={error} /> : null}
|
||||
{skills.error ? (
|
||||
<ErrorNotice error={skills.error} retry={() => void skills.mutate()} />
|
||||
) : skills.isLoading ? (
|
||||
<Loading />
|
||||
) : shown.length ? (
|
||||
<ul className="skill-list">
|
||||
{shown.map(skill => {
|
||||
const [tone, label] = STATUS[skill.status];
|
||||
return (
|
||||
<li key={skill.id} className={skill.status === 'proposed' ? 'skill-proposed' : undefined}>
|
||||
<input
|
||||
type="checkbox"
|
||||
aria-label={`选择 ${skill.name}`}
|
||||
checked={selected.includes(skill.id)}
|
||||
onChange={event =>
|
||||
setSelected(
|
||||
event.target.checked ? [...selected, skill.id] : selected.filter(id => id !== skill.id),
|
||||
)
|
||||
}
|
||||
>
|
||||
<Download size={15} />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
aria-label="删除这个技能"
|
||||
busy={busy === `delete:${skill.id}`}
|
||||
onClick={() => {
|
||||
if (window.confirm(`删除技能「${skill.name}」?`))
|
||||
void run(`delete:${skill.id}`, () => mutateApi(`/skills/${skill.id}`, undefined, 'DELETE'));
|
||||
}}
|
||||
>
|
||||
<Trash2 size={15} />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
) : (
|
||||
<EmptyState
|
||||
compact
|
||||
title="还没有项目技能"
|
||||
description="运行成功后,平台会自动提炼值得沉淀的做法;也可以在运行页点「提炼技能」,或在这里新建。"
|
||||
/>
|
||||
)}
|
||||
{!editing ? <FormError message={error} /> : null}
|
||||
/>
|
||||
<Sparkles size={18} />
|
||||
<div className="skill-body">
|
||||
<p>
|
||||
<strong>{skill.name}</strong> <Status value={tone} label={label} />{' '}
|
||||
<span className="skill-category">{CATEGORY[skill.category ?? 'workflow']}</span>
|
||||
</p>
|
||||
<p>{skill.description}</p>
|
||||
<p className="muted text-small">
|
||||
<Link className="text-link" href={`/projects/${skill.projectId}`}>
|
||||
{projectName(skill.projectId)}
|
||||
</Link>
|
||||
{' · '}
|
||||
{skill.createdBy === 'wishforge' ? '平台提炼' : '手动添加'} · 更新于 {formatTime(skill.updatedAt)}
|
||||
{skill.useCount
|
||||
? ` · 用过 ${skill.useCount} 次(成功 ${skill.successCount ?? 0},失败 ${skill.failureCount ?? 0}),最近 ${formatTime(skill.lastUsedAt)}`
|
||||
: ' · 还没有运行用过'}
|
||||
{skill.sourceRunIds.slice(-3).map(runId => (
|
||||
<span key={runId}>
|
||||
{' · '}
|
||||
<Link className="text-link" href={`/runs/${runId}`}>
|
||||
来源运行
|
||||
</Link>
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
{skill.retiredReason && skill.status === 'disabled' ? (
|
||||
<p className="text-small skill-warning">
|
||||
{RETIRED[skill.retiredReason]}({formatTime(skill.retiredAt)})。确认仍然有用可以重新启用。
|
||||
</p>
|
||||
) : skill.status === 'active' && (skill.failureStreak ?? 0) >= 2 ? (
|
||||
<p className="text-small skill-warning">
|
||||
最近 {skill.failureStreak} 次用到它的运行都失败了,请检查它是否过时。
|
||||
</p>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
className="text-button"
|
||||
onClick={() => setOpen(open === skill.id ? undefined : skill.id)}
|
||||
>
|
||||
{open === skill.id ? '收起做法' : '查看做法'}
|
||||
</button>
|
||||
{open === skill.id ? (
|
||||
<div className="markdown skill-content">
|
||||
<Markdown source={skill.content} />
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="button-group">
|
||||
{skill.status === 'proposed' ? (
|
||||
<Button
|
||||
variant="secondary"
|
||||
busy={busy === `confirm:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`confirm:${skill.id}`, () =>
|
||||
mutateApi(`/skills/${skill.id}`, { status: 'active' }, 'PATCH'),
|
||||
)
|
||||
}
|
||||
>
|
||||
<Check size={15} />
|
||||
确认启用
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
busy={busy === `status:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`status:${skill.id}`, () =>
|
||||
mutateApi(
|
||||
`/skills/${skill.id}`,
|
||||
{ status: skill.status === 'active' ? 'disabled' : 'active' },
|
||||
'PATCH',
|
||||
),
|
||||
)
|
||||
}
|
||||
>
|
||||
{skill.status === 'active' ? '停用' : '启用'}
|
||||
</Button>
|
||||
)}
|
||||
<Button variant="ghost" aria-label="编辑这个技能" onClick={() => setEditing(skill)}>
|
||||
<Pencil size={15} />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
aria-label="导出 SKILL.md"
|
||||
busy={busy === `export:${skill.id}`}
|
||||
onClick={() =>
|
||||
void run(`export:${skill.id}`, () =>
|
||||
downloadApi(`/skills/${skill.id}/export`, `${skill.name}.SKILL.md`),
|
||||
)
|
||||
}
|
||||
>
|
||||
<Download size={15} />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
aria-label="删除这个技能"
|
||||
busy={busy === `delete:${skill.id}`}
|
||||
onClick={() => {
|
||||
if (window.confirm(`删除技能「${skill.name}」?`))
|
||||
void run(`delete:${skill.id}`, () => mutateApi(`/skills/${skill.id}`, undefined, 'DELETE'));
|
||||
}}
|
||||
>
|
||||
<Trash2 size={15} />
|
||||
</Button>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
) : (
|
||||
<EmptyState
|
||||
compact
|
||||
title={skills.data?.length ? '没有符合条件的技能' : '还没有项目技能'}
|
||||
description="运行成功后,平台会自动提炼值得沉淀的做法;修复过的失败会记成踩坑记录。也可以在运行页点「提炼技能」、在这里新建,或导入 SKILL.md / zip。"
|
||||
/>
|
||||
)}
|
||||
</Panel>
|
||||
{editing ? (
|
||||
<SkillEditor
|
||||
skill={editing === 'new' ? undefined : editing}
|
||||
projects={projects.data ?? []}
|
||||
defaultProject={project !== 'all' ? project : projects.data?.[0]?.id}
|
||||
busy={busy === 'save'}
|
||||
error={error}
|
||||
onClose={() => {
|
||||
setEditing(undefined);
|
||||
setError('');
|
||||
}}
|
||||
onSave={draft =>
|
||||
onSave={({ projectId, ...draft }) =>
|
||||
void run('save', () =>
|
||||
editing === 'new'
|
||||
? mutateApi(`/projects/${projectId}/skills`, draft)
|
||||
@ -196,18 +359,197 @@ export function SkillsPanel({ projectId }: { projectId: string }) {
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
</Panel>
|
||||
{importing ? (
|
||||
<SkillImport
|
||||
projects={projects.data ?? []}
|
||||
defaultProject={project !== 'all' ? project : projects.data?.[0]?.id}
|
||||
busy={busy === 'import'}
|
||||
error={error}
|
||||
onClose={() => {
|
||||
setImporting(false);
|
||||
setError('');
|
||||
}}
|
||||
onImport={(projectId, body) =>
|
||||
void run('import', async () =>
|
||||
setNotice(transferred(await mutateApi<SkillTransfer>(`/projects/${projectId}/skills/import`, body))),
|
||||
).then(done => {
|
||||
if (done) setImporting(false);
|
||||
})
|
||||
}
|
||||
/>
|
||||
) : null}
|
||||
{copying ? (
|
||||
<Modal
|
||||
title={`复制 ${selected.length} 条技能到其他项目`}
|
||||
description="复制内容、类型和启用状态,不复制使用统计和来源运行。目标项目里已有内容完全相同的同名技能会跳过;名字被其他技能占用时,自动加上序号,例如 name-2。"
|
||||
onClose={() => {
|
||||
setCopying(false);
|
||||
setError('');
|
||||
}}
|
||||
>
|
||||
<CopyForm
|
||||
projects={(projects.data ?? []).filter(
|
||||
item => !(skills.data ?? []).some(skill => selected.includes(skill.id) && skill.projectId === item.id),
|
||||
)}
|
||||
busy={busy === 'copy'}
|
||||
error={error}
|
||||
onCopy={projectId =>
|
||||
void run('copy', async () => {
|
||||
setNotice(
|
||||
transferred(await mutateApi<SkillTransfer>('/skills/copy', { skillIds: selected, projectId })),
|
||||
);
|
||||
}).then(done => {
|
||||
if (done) {
|
||||
setCopying(false);
|
||||
setSelected([]);
|
||||
}
|
||||
})
|
||||
}
|
||||
/>
|
||||
</Modal>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function CopyForm({
|
||||
projects,
|
||||
busy,
|
||||
error,
|
||||
onCopy,
|
||||
}: {
|
||||
projects: Project[];
|
||||
busy: boolean;
|
||||
error: string;
|
||||
onCopy: (projectId: string) => void;
|
||||
}) {
|
||||
const [target, setTarget] = useState(projects[0]?.id ?? '');
|
||||
return (
|
||||
<form
|
||||
className="form-stack"
|
||||
onSubmit={event => {
|
||||
event.preventDefault();
|
||||
onCopy(target);
|
||||
}}
|
||||
>
|
||||
<Field label="目标项目">
|
||||
<select required value={target} onChange={event => setTarget(event.target.value)}>
|
||||
{projects.map(item => (
|
||||
<option key={item.id} value={item.id}>
|
||||
{item.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
<FormError message={error} />
|
||||
<div className="modal-actions">
|
||||
<Button busy={busy} disabled={!target}>
|
||||
复制
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
function SkillImport({
|
||||
projects,
|
||||
defaultProject,
|
||||
busy,
|
||||
error,
|
||||
onClose,
|
||||
onImport,
|
||||
}: {
|
||||
projects: Project[];
|
||||
defaultProject?: string;
|
||||
busy: boolean;
|
||||
error: string;
|
||||
onClose: () => void;
|
||||
onImport: (projectId: string, body: unknown) => void;
|
||||
}) {
|
||||
const [projectId, setProjectId] = useState(defaultProject ?? '');
|
||||
const [files, setFiles] = useState<File[]>([]);
|
||||
const [status, setStatus] = useState<'active' | 'proposed'>('active');
|
||||
const [problem, setProblem] = useState('');
|
||||
const zip = files.find(file => /\.zip$/i.test(file.name));
|
||||
return (
|
||||
<Modal
|
||||
title="导入技能"
|
||||
description="选择一个或多个 SKILL.md(.md),或一个 zip(例如从这里导出的项目技能,每个技能一个目录)。zip 最大 512 KiB,每个 SKILL.md 最大 64 KiB,一次最多 50 条。已有内容完全相同的技能会跳过,同名但内容不同的会加上序号。"
|
||||
onClose={onClose}
|
||||
>
|
||||
<form
|
||||
className="form-stack"
|
||||
onSubmit={event => {
|
||||
event.preventDefault();
|
||||
setProblem('');
|
||||
if (zip && files.length > 1) return setProblem('zip 需要单独导入。');
|
||||
void (async () => {
|
||||
try {
|
||||
const body = zip
|
||||
? { archive: await base64(zip), status }
|
||||
: {
|
||||
files: await Promise.all(
|
||||
files.map(async file => ({ path: file.name, content: await file.text() })),
|
||||
),
|
||||
status,
|
||||
};
|
||||
onImport(projectId, body);
|
||||
} catch (failure) {
|
||||
setProblem(messageOf(failure));
|
||||
}
|
||||
})();
|
||||
}}
|
||||
>
|
||||
<Field label="导入到项目">
|
||||
<select required value={projectId} onChange={event => setProjectId(event.target.value)}>
|
||||
{projects.map(item => (
|
||||
<option key={item.id} value={item.id}>
|
||||
{item.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
<Field label="文件" hint="SKILL.md 或 zip">
|
||||
<input
|
||||
type="file"
|
||||
required
|
||||
multiple
|
||||
accept=".md,.zip,text/markdown,application/zip"
|
||||
onChange={event => setFiles([...(event.target.files ?? [])])}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="导入后">
|
||||
<select value={status} onChange={event => setStatus(event.target.value as 'active' | 'proposed')}>
|
||||
<option value="active">直接启用</option>
|
||||
<option value="proposed">先放进待确认</option>
|
||||
</select>
|
||||
</Field>
|
||||
<FormError message={problem || error} />
|
||||
<div className="modal-actions">
|
||||
<Button type="button" variant="secondary" onClick={onClose}>
|
||||
取消
|
||||
</Button>
|
||||
<Button busy={busy} disabled={!projectId || !files.length}>
|
||||
导入
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
function SkillEditor({
|
||||
skill,
|
||||
projects,
|
||||
defaultProject,
|
||||
busy,
|
||||
error,
|
||||
onClose,
|
||||
onSave,
|
||||
}: {
|
||||
skill?: Skill;
|
||||
projects: Project[];
|
||||
defaultProject?: string;
|
||||
busy: boolean;
|
||||
error: string;
|
||||
onClose: () => void;
|
||||
@ -217,25 +559,57 @@ function SkillEditor({
|
||||
name: skill?.name ?? '',
|
||||
description: skill?.description ?? '',
|
||||
content: skill?.content ?? '',
|
||||
category: skill?.category ?? 'workflow',
|
||||
projectId: skill?.projectId ?? defaultProject ?? '',
|
||||
});
|
||||
const set = (field: keyof Draft) => (event: { target: { value: string } }) =>
|
||||
setDraft(previous => ({ ...previous, [field]: event.target.value }));
|
||||
return (
|
||||
<Modal
|
||||
title={skill ? `编辑技能 ${skill.name}` : '新建技能'}
|
||||
description="写清什么时候用、怎么做。启用中的技能会进入这个项目之后每个愿望的提示词。"
|
||||
description="写清什么时候用、怎么做。启用中的技能会进入这个项目之后每个愿望的提示词。编辑过的技能平台不会再改写或自动停用。"
|
||||
onClose={onClose}
|
||||
>
|
||||
<form
|
||||
className="form-stack"
|
||||
onSubmit={event => {
|
||||
event.preventDefault();
|
||||
onSave({ name: draft.name.trim(), description: draft.description.trim(), content: draft.content.trim() });
|
||||
const { projectId, category } = draft;
|
||||
onSave({
|
||||
projectId,
|
||||
category,
|
||||
name: draft.name.trim(),
|
||||
description: draft.description.trim(),
|
||||
content: draft.content.trim(),
|
||||
});
|
||||
}}
|
||||
>
|
||||
{skill ? null : (
|
||||
<Field label="项目">
|
||||
<select required value={draft.projectId} onChange={set('projectId')}>
|
||||
{projects.map(item => (
|
||||
<option key={item.id} value={item.id}>
|
||||
{item.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
)}
|
||||
<Field label="名称" hint="小写英文单词和连字符,例如 lazy-load-page-css;导出时作为目录名。">
|
||||
<input required maxLength={64} pattern="[a-z0-9]+(-[a-z0-9]+)*" value={draft.name} onChange={set('name')} />
|
||||
</Field>
|
||||
<Field
|
||||
label="类型"
|
||||
hint="做法:某类改动怎么做;踩坑记录:失败过的地方和修法;项目约定:这个项目必须遵守的规则。"
|
||||
>
|
||||
<select value={draft.category} onChange={set('category')}>
|
||||
{Object.entries(CATEGORY).map(([value, label]) => (
|
||||
<option key={value} value={value}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</Field>
|
||||
<Field label="什么时候用" hint="一两句话,最多 500 字。">
|
||||
<textarea required maxLength={500} rows={2} value={draft.description} onChange={set('description')} />
|
||||
</Field>
|
||||
@ -247,7 +621,10 @@ function SkillEditor({
|
||||
<Button type="button" variant="secondary" onClick={onClose}>
|
||||
取消
|
||||
</Button>
|
||||
<Button busy={busy} disabled={!draft.name.trim() || !draft.description.trim() || !draft.content.trim()}>
|
||||
<Button
|
||||
busy={busy}
|
||||
disabled={!draft.projectId || !draft.name.trim() || !draft.description.trim() || !draft.content.trim()}
|
||||
>
|
||||
保存
|
||||
</Button>
|
||||
</div>
|
||||
@ -255,3 +632,43 @@ function SkillEditor({
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
/** On a project's page: how much know-how the project has, with the way to the knowledge base. */
|
||||
export function ProjectSkillsSummary({ projectId }: { projectId: string }) {
|
||||
const skills = useResource<Skill[]>(`/projects/${projectId}/skills`, 30000);
|
||||
const active = skills.data?.filter(skill => skill.status === 'active') ?? [];
|
||||
const proposed = skills.data?.filter(skill => skill.status === 'proposed').length ?? 0;
|
||||
const count = (category: SkillCategory) => active.filter(skill => (skill.category ?? 'workflow') === category).length;
|
||||
return (
|
||||
<Panel
|
||||
title="项目技能"
|
||||
aside={
|
||||
<Link className="text-link" href={`/skills?project=${projectId}`}>
|
||||
在知识库中管理
|
||||
<ArrowRight size={15} />
|
||||
</Link>
|
||||
}
|
||||
>
|
||||
{skills.error ? (
|
||||
<ErrorNotice error={skills.error} retry={() => void skills.mutate()} />
|
||||
) : skills.isLoading ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<p className="panel-intro">
|
||||
{active.length
|
||||
? `启用中 ${active.length} 条:${CATEGORY.workflow} ${count('workflow')}、${CATEGORY.pitfall} ${count('pitfall')}、${CATEGORY.convention} ${count('convention')}。`
|
||||
: '还没有启用中的技能。'}
|
||||
之后的愿望会参考启用中的技能。
|
||||
{proposed ? (
|
||||
<>
|
||||
{' '}
|
||||
<Link className="text-link" href={`/skills?project=${projectId}&status=proposed`}>
|
||||
<strong>{proposed} 条待确认</strong>
|
||||
</Link>
|
||||
</>
|
||||
) : null}
|
||||
</p>
|
||||
)}
|
||||
</Panel>
|
||||
);
|
||||
}
|
||||
|
||||
@ -192,9 +192,6 @@ export function UsageView() {
|
||||
<KeyValue label="项目">
|
||||
{formatNumber(entitlement.counts.projects)} / {formatNumber(entitlement.limits.maxProjects)}
|
||||
</KeyValue>
|
||||
<KeyValue label="工作区">
|
||||
{formatNumber(entitlement.counts.workspaces)} / {formatNumber(entitlement.limits.maxWorkspaces)}
|
||||
</KeyValue>
|
||||
<KeyValue label="执行名额">
|
||||
{formatNumber(entitlement.counts.reservedExecutionSlots)} /{' '}
|
||||
{formatNumber(entitlement.limits.maxConcurrentRuns)}
|
||||
|
||||
99
apps/web/src/lib/attachments.ts
Normal file
99
apps/web/src/lib/attachments.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import {
|
||||
MAX_ATTACHMENT_BYTES,
|
||||
MAX_ATTACHMENTS,
|
||||
MAX_ATTACHMENTS_TOTAL_BYTES,
|
||||
inlineImageTypes,
|
||||
} from '@wishforge/contracts/attachments';
|
||||
|
||||
/** A file chosen for a wish, read and ready to send. */
|
||||
export interface PendingAttachment {
|
||||
key: string;
|
||||
name: string;
|
||||
mediaType: string;
|
||||
size: number;
|
||||
/** base64 */
|
||||
data: string;
|
||||
/** An object URL for images the browser shows, revoked when the file is removed. */
|
||||
preview?: string;
|
||||
}
|
||||
|
||||
export const attachmentLimitsText = `最多 ${MAX_ATTACHMENTS} 个,单个不超过 ${MAX_ATTACHMENT_BYTES / 1024 / 1024} MB,合计不超过 ${MAX_ATTACHMENTS_TOTAL_BYTES / 1024 / 1024} MB`;
|
||||
|
||||
export function formatBytes(size: number) {
|
||||
if (size < 1024) return `${size} B`;
|
||||
if (size < 1024 * 1024) return `${(size / 1024).toFixed(size < 10 * 1024 ? 1 : 0)} KB`;
|
||||
return `${(size / 1024 / 1024).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
/** A pasted screenshot has no useful name; give it one with the time it was pasted. */
|
||||
function nameOf(file: File, index: number) {
|
||||
if (file.name && file.name !== 'image.png') return file.name;
|
||||
const extension = file.type.split('/')[1]?.replace('jpeg', 'jpg') ?? 'png';
|
||||
const now = new Date();
|
||||
const two = (n: number) => String(n).padStart(2, '0');
|
||||
const stamp = `${now.getFullYear()}${two(now.getMonth() + 1)}${two(now.getDate())}-${two(now.getHours())}${two(now.getMinutes())}${two(now.getSeconds())}`;
|
||||
return `粘贴图片-${stamp}${index ? `-${index + 1}` : ''}.${extension}`;
|
||||
}
|
||||
|
||||
async function base64Of(file: Blob) {
|
||||
const bytes = new Uint8Array(await file.arrayBuffer());
|
||||
let binary = '';
|
||||
for (let i = 0; i < bytes.length; i += 0x8000) binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds files to those already chosen, within the limits; returns the new list and, when some
|
||||
* did not fit, why.
|
||||
*/
|
||||
export async function addAttachments(
|
||||
current: PendingAttachment[],
|
||||
files: File[],
|
||||
): Promise<{ attachments: PendingAttachment[]; problem?: string }> {
|
||||
const attachments = [...current];
|
||||
const problems: string[] = [];
|
||||
let total = current.reduce((sum, file) => sum + file.size, 0);
|
||||
for (const [index, file] of files.entries()) {
|
||||
const name = nameOf(file, index);
|
||||
if (attachments.length >= MAX_ATTACHMENTS) {
|
||||
problems.push(`最多只能附 ${MAX_ATTACHMENTS} 个文件`);
|
||||
break;
|
||||
}
|
||||
if (file.size === 0) {
|
||||
problems.push(`「${name}」是空文件`);
|
||||
continue;
|
||||
}
|
||||
if (file.size > MAX_ATTACHMENT_BYTES) {
|
||||
problems.push(`「${name}」超过 ${MAX_ATTACHMENT_BYTES / 1024 / 1024} MB`);
|
||||
continue;
|
||||
}
|
||||
if (total + file.size > MAX_ATTACHMENTS_TOTAL_BYTES) {
|
||||
problems.push(`附件合计不能超过 ${MAX_ATTACHMENTS_TOTAL_BYTES / 1024 / 1024} MB`);
|
||||
continue;
|
||||
}
|
||||
const mediaType = file.type || 'application/octet-stream';
|
||||
attachments.push({
|
||||
key: `${Date.now()}-${Math.random().toString(36).slice(2)}`,
|
||||
name,
|
||||
mediaType,
|
||||
size: file.size,
|
||||
data: await base64Of(file),
|
||||
...(inlineImageTypes.includes(mediaType) ? { preview: URL.createObjectURL(file) } : {}),
|
||||
});
|
||||
total += file.size;
|
||||
}
|
||||
return { attachments, ...(problems.length ? { problem: `${[...new Set(problems)].join(';')}。` } : {}) };
|
||||
}
|
||||
|
||||
/**
|
||||
* The files a paste carries, unless it is text: copying cells or a document often brings a
|
||||
* picture of them along, and then the text is what the person meant.
|
||||
*/
|
||||
export function pastedFiles(data: DataTransfer | null): File[] {
|
||||
if (!data) return [];
|
||||
const files = [...data.files];
|
||||
if (!files.length) return [];
|
||||
const text = data.getData('text/plain').trim();
|
||||
if (text && !files.some(file => file.name === text)) return [];
|
||||
return files;
|
||||
}
|
||||
@ -6,13 +6,31 @@ describe('timeline event kinds', () => {
|
||||
it('names every kind of executor event and the run’s own steps in Chinese', () => {
|
||||
for (const type of [
|
||||
...providerEventTypes.map(kind => `provider.${kind}`),
|
||||
...['dispatched', 'completed', 'rejected', 'unknown', 'reconciled'].map(status => `provider.proxy.${status}`),
|
||||
'wish.submitted',
|
||||
'run.transition',
|
||||
'run.waiting',
|
||||
'resources.released',
|
||||
...['leased', 'transition', 'waiting', 'pause', 'paused', 'resume', 'cancel', 'answer', 'scope', 'repair'].map(
|
||||
step => `run.${step}`,
|
||||
),
|
||||
'run.succeeded',
|
||||
'run.budget.updated',
|
||||
...['provisioned', 'unknown', 'releasing', 'released', 'storage_purged', 'reconciliation_required'].map(
|
||||
status => `resources.${status}`,
|
||||
),
|
||||
'artifact.stored',
|
||||
'evidence.recorded',
|
||||
'release.created',
|
||||
'lesson.recorded',
|
||||
'skill.proposed',
|
||||
...['created', 'approval', 'failed', 'failure', 'rolled_back'].map(step => `release.${step}`),
|
||||
'delivery.accepted',
|
||||
...['requested', 'failed', 'unknown', 'abandoned'].map(step => `rollback.${step}`),
|
||||
...['requested', 'approved', 'rejected', 'skipped'].map(step => `code_review.${step}`),
|
||||
'github.pr.requested',
|
||||
'github.pr.observed',
|
||||
'lesson.recorded',
|
||||
'skill.proposed',
|
||||
'skill.activated',
|
||||
'skill.updated',
|
||||
'skill.retired',
|
||||
])
|
||||
expect(eventLabel(type), type).toMatch(/[一-鿿]/);
|
||||
});
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user