✨ Enable local Claude delivery to Gitea and POC
Add authenticated LAN access, native Claude execution, checkpoint and budget recovery, and signed POC build/deploy/rollback contracts. Include local service management, regression coverage, and evidence from the completed POC delivery.
This commit is contained in:
parent
71cc6de5e2
commit
4e5ab1719c
@ -7,6 +7,7 @@
|
||||
- 本地开发:[docs/development.md](docs/development.md)
|
||||
- 生产运维:[docs/operations.md](docs/operations.md)
|
||||
- 专题参考:[docs/reference/](docs/reference/)
|
||||
- 本机 Claude → Gitea POC:[运行说明](docs/local-poc-operations.md) · [真实交付验收](docs/reference/local-poc-acceptance-2026-10-22.md)
|
||||
|
||||
## 快速开始
|
||||
|
||||
|
||||
@ -25,7 +25,8 @@
|
||||
"pg": "^8.16.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.2",
|
||||
"zod": "^3.25.0"
|
||||
"zod": "^3.25.0",
|
||||
"@wishforge/runtime": "*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^5.0.0"
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { readLocalPocBinding } from '@wishforge/runtime';
|
||||
import {
|
||||
DomainError,
|
||||
normalizeApplicationUrl,
|
||||
@ -12,14 +13,22 @@ import {
|
||||
* code, so they may never share an origin with the console, API or IDE gateways.
|
||||
*/
|
||||
export function applicationUrlFor(
|
||||
environment: Pick<Environment, 'applicationUrl' | 'production'>,
|
||||
environment: Pick<Environment, 'applicationUrl' | 'production'> & { projectId?: string },
|
||||
env: NodeJS.ProcessEnv,
|
||||
additionalOrigins: string[] = [],
|
||||
): string | undefined {
|
||||
if (!environment.applicationUrl) return undefined;
|
||||
let applicationUrl: string;
|
||||
try {
|
||||
applicationUrl = normalizeApplicationUrl(environment.applicationUrl, !isProduction(env) && !environment.production);
|
||||
const binding = env.WF_LOCAL_POC_BINDING_FILE ? readLocalPocBinding(env.WF_LOCAL_POC_BINDING_FILE, env) : undefined;
|
||||
const localPoc =
|
||||
binding &&
|
||||
binding.projectId === environment.projectId &&
|
||||
!environment.production &&
|
||||
environment.applicationUrl === 'http://120.48.164.19';
|
||||
applicationUrl = localPoc
|
||||
? environment.applicationUrl
|
||||
: normalizeApplicationUrl(environment.applicationUrl, !isProduction(env) && !environment.production);
|
||||
} catch {
|
||||
throw new DomainError(
|
||||
'APPLICATION_URL_INVALID',
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { assertPocBuild, boundPocProject } from './local-poc.js';
|
||||
import { nativeClaudeEnabled } from './local-execution.js';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import {
|
||||
DomainError,
|
||||
@ -6,6 +8,8 @@ import {
|
||||
stages,
|
||||
projectInput,
|
||||
wishInput,
|
||||
budgetPatchSchema,
|
||||
budgetSchema,
|
||||
commandInput,
|
||||
transitionInput,
|
||||
evidenceInput,
|
||||
@ -33,7 +37,7 @@ import {
|
||||
type CheckpointRecord,
|
||||
} from '@wishforge/contracts';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { checkLease, digest, redact, requireRole } from './policy.js';
|
||||
import { checkLease, digest, redact, requireHuman, requireRole } from './policy.js';
|
||||
import { GitStorage } from './git-storage.js';
|
||||
import { requireCapacity, effectiveEntitlements } from './entitlements.js';
|
||||
import { validateRepositoryUrl } from '../../../../packages/runtime/src/git.js';
|
||||
@ -267,10 +271,12 @@ export class ControlPlane {
|
||||
}
|
||||
async providerInfo(tx: Transaction): Promise<ProviderInfo[]> {
|
||||
const connections = await tx.list<ProviderConnection>('provider_connection');
|
||||
const awaitedTenantId = (await tx.tenant()).id;
|
||||
const connected = (id: string) => connections.some(c => c.provider === id && c.status === 'active');
|
||||
return providers.map(id => {
|
||||
const { name, binaryVariable, proxied } = providerCatalog[id];
|
||||
const configured = connected(id);
|
||||
const native = id === 'claude-code' && nativeClaudeEnabled(this.env, awaitedTenantId);
|
||||
const configured = connected(id) || native;
|
||||
const productionAllowed = !isProduction(this.env) || (proxied && providerProxyConfigured(this.env));
|
||||
const installed = !binaryVariable || Boolean(this.env[binaryVariable]);
|
||||
const enabled = configured && productionAllowed && installed;
|
||||
@ -280,7 +286,9 @@ export class ControlPlane {
|
||||
? 'Connect a tenant-owned provider credential to execute tasks.'
|
||||
: !installed
|
||||
? `The ${name} executable is not configured on the worker (${binaryVariable}).`
|
||||
: 'Available for worker preflight; account/model access is checked when a task starts.';
|
||||
: 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.';
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
@ -292,7 +300,7 @@ export class ControlPlane {
|
||||
resume: enabled,
|
||||
cancel: enabled,
|
||||
eventReplay: false,
|
||||
usage: proxied && enabled,
|
||||
usage: (proxied || native) && enabled,
|
||||
},
|
||||
};
|
||||
});
|
||||
@ -303,6 +311,12 @@ export class ControlPlane {
|
||||
this.validateKey(key);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
const project = await projectAccess(tx, actor, projectId);
|
||||
const localPoc = boundPocProject(project, this.env);
|
||||
if (localPoc && (data.sourceBranch || data.provider !== 'claude-code'))
|
||||
throw new DomainError(
|
||||
'POC_EXECUTION_POLICY',
|
||||
'This local POC project runs Claude CLI from its fixed poc target branch',
|
||||
);
|
||||
if (
|
||||
data.sourceBranch &&
|
||||
(data.sourceBranch === project.targetBranch || data.sourceBranch.startsWith('wishforge/'))
|
||||
@ -484,6 +498,67 @@ export class ControlPlane {
|
||||
});
|
||||
});
|
||||
}
|
||||
/** Changes limits only; continuing execution remains a separate, quota-checked command. */
|
||||
async updateBudget(actor: Actor, id: string, input: unknown, key?: string) {
|
||||
requireHuman(actor);
|
||||
requireRole(actor, ['owner', 'admin']);
|
||||
const data = budgetPatchSchema.parse(input);
|
||||
this.validateKey(key);
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
let run = await must<Run>(tx, 'run', id);
|
||||
await projectAccess(tx, actor, run.projectId);
|
||||
return idempotent(tx, `budget:${actor.userId}:${id}:${key}`, data, async () => {
|
||||
if (!['waiting', 'paused'].includes(run.status))
|
||||
throw new DomainError('BUDGET_UPDATE_REQUIRES_STOPPED_RUN', 'Pause the run before changing its budget', 409);
|
||||
if (run.providerProxyActiveRequest)
|
||||
throw new DomainError(
|
||||
'BUDGET_UPDATE_PENDING_USAGE',
|
||||
'Settle the active model request before changing its budget',
|
||||
409,
|
||||
);
|
||||
this.accountTime(run);
|
||||
const oldBudget = { ...run.budget };
|
||||
const nextBudget = budgetSchema.parse({ ...run.budget, ...data });
|
||||
if (nextBudget.maxTokens < run.usedTokens)
|
||||
throw new DomainError('BUDGET_BELOW_USAGE', 'Token limit cannot be lower than the recorded usage', 400);
|
||||
if (nextBudget.maxMinutes * 60_000 < run.activeMilliseconds)
|
||||
throw new DomainError(
|
||||
'BUDGET_BELOW_USAGE',
|
||||
'Time limit cannot be lower than the recorded active execution time',
|
||||
400,
|
||||
);
|
||||
if (data.maxRepairRounds !== undefined && data.maxRepairRounds < run.repairCount)
|
||||
throw new DomainError(
|
||||
'BUDGET_BELOW_USAGE',
|
||||
'Repair limit cannot be lower than the completed repair count',
|
||||
400,
|
||||
);
|
||||
const remaining = Math.max(0, nextBudget.maxTokens - run.usedTokens);
|
||||
const others = (await tx.list<Run>('run')).filter(other => other.id !== run.id);
|
||||
const used = (await tx.usage(this.period())).reduce((sum, item) => sum + item.tokens, 0);
|
||||
if (
|
||||
reservedTokens(others) + used + remaining >
|
||||
effectiveEntitlements(await tx.tenant()).limits.monthlyModelTokens
|
||||
)
|
||||
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,
|
||||
},
|
||||
);
|
||||
return run;
|
||||
});
|
||||
});
|
||||
}
|
||||
async lease(actor: Actor, id: string, workerId: string, generation?: string): Promise<Run> {
|
||||
if (!['worker', 'verifier', 'deployer'].includes(actor.kind))
|
||||
throw new DomainError('FORBIDDEN', 'Execution identity required', 403);
|
||||
@ -743,6 +818,11 @@ export class ControlPlane {
|
||||
);
|
||||
const release = await must<Release>(tx, 'release', data.releaseId),
|
||||
environment = await must<Environment>(tx, 'environment', data.environmentId);
|
||||
if (release.pocRelease)
|
||||
throw new DomainError(
|
||||
'POC_RUNNER_RECEIPT_REQUIRED',
|
||||
'POC deployment evidence is created only from the signed component Runner receipt',
|
||||
);
|
||||
if (actor.userId.startsWith('runner:') && actor.userId !== `runner:${environment.id}`)
|
||||
throw new DomainError('FORBIDDEN', 'Runner cannot attest another environment', 403);
|
||||
if (
|
||||
@ -801,6 +881,18 @@ export class ControlPlane {
|
||||
(project.verificationProfile && run.verifiedPolicyDigest !== verificationPolicyDigest(project)))
|
||||
)
|
||||
throw new DomainError('UNVERIFIED_BUILD', 'Build must bind the verified commit and an immutable image digest');
|
||||
if (data.type === 'build' && data.passed) {
|
||||
await assertPocBuild(project, project.deliveryConfig?.environmentId, data.pocRelease, this.env);
|
||||
if (
|
||||
data.pocRelease &&
|
||||
(data.pocRelease.sourceCommit !== data.sourceCommit || data.pocRelease.images.api !== data.imageDigest)
|
||||
)
|
||||
throw new DomainError(
|
||||
'POC_BUILD_MISMATCH',
|
||||
'All component evidence must bind the verified source and API image',
|
||||
);
|
||||
} else if (data.pocRelease)
|
||||
throw new DomainError('POC_EVIDENCE_TYPE', 'Only build evidence may introduce a POC component release');
|
||||
const evidence = {
|
||||
...record(actor.tenantId),
|
||||
...data,
|
||||
|
||||
14
apps/api/src/domain/local-execution.ts
Normal file
14
apps/api/src/domain/local-execution.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { isAbsolute } from 'node:path';
|
||||
|
||||
/** Local account authority is explicitly assigned to one tenant; it is not a fake BYOK connection. */
|
||||
export function nativeClaudeEnabled(env: NodeJS.ProcessEnv, tenantId: string): boolean {
|
||||
return (
|
||||
env.WF_DEPLOYMENT_MODE === 'development' &&
|
||||
env.NODE_ENV !== 'production' &&
|
||||
env.WF_PROVISIONER_BACKEND === 'local' &&
|
||||
env.WF_TENANT_ID === tenantId &&
|
||||
!!env.WF_CLAUDE_BIN &&
|
||||
!!env.WF_CLAUDE_LOCAL_AUTH_HOME &&
|
||||
isAbsolute(env.WF_CLAUDE_LOCAL_AUTH_HOME)
|
||||
);
|
||||
}
|
||||
38
apps/api/src/domain/local-poc.ts
Normal file
38
apps/api/src/domain/local-poc.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import type { Project, PocReleaseSet } from '@wishforge/contracts';
|
||||
import { DomainError } from '@wishforge/contracts';
|
||||
import { readLocalPocBinding, localPocForProject, loadPocProfile, profileDigest } from '@wishforge/runtime';
|
||||
|
||||
export function boundPocProject(project: Project, env: NodeJS.ProcessEnv) {
|
||||
return localPocForProject(readLocalPocBinding(env.WF_LOCAL_POC_BINDING_FILE, env), project);
|
||||
}
|
||||
|
||||
export async function assertPocBuild(
|
||||
project: Project,
|
||||
environmentId: string | undefined,
|
||||
release: PocReleaseSet | undefined,
|
||||
env: NodeJS.ProcessEnv,
|
||||
) {
|
||||
const binding = boundPocProject(project, env);
|
||||
if (!binding && !release) return;
|
||||
if (!binding || !release || (environmentId && binding.environmentId !== environmentId))
|
||||
throw new DomainError(
|
||||
'POC_BINDING_REQUIRED',
|
||||
'POC component releases require the exact operator-bound project and environment',
|
||||
);
|
||||
const profile = await loadPocProfile(binding.profileFile);
|
||||
if (release.profileDigest !== profileDigest(profile) || !release.expectedTemplateDigests)
|
||||
throw new DomainError(
|
||||
'POC_POLICY_CHANGED',
|
||||
'The POC profile or baseline configuration fingerprint is missing or changed',
|
||||
);
|
||||
for (const [component, repository] of Object.entries({
|
||||
api: 'droboticflow-api',
|
||||
worker: 'droboticflow-worker',
|
||||
coordinator: 'drosync-coordinator',
|
||||
})) {
|
||||
for (const images of [release.images, release.expectedImages]) {
|
||||
if (!images[component as keyof typeof images].startsWith(`${profile.registry}/${repository}@sha256:`))
|
||||
throw new DomainError('POC_IMAGE_SCOPE', 'A POC component image is outside the fixed registry repository');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -51,6 +51,13 @@ export function describeProviderEvent(event: { type: ProviderEventType; data: Re
|
||||
case 'usage': {
|
||||
const input = count(data.input_tokens) ?? 0;
|
||||
const output = count(data.output_tokens) ?? 0;
|
||||
const cacheCreation = count(data.cache_creation_input_tokens) ?? 0;
|
||||
const cacheRead = count(data.cache_read_input_tokens) ?? 0;
|
||||
if (cacheCreation || cacheRead)
|
||||
return {
|
||||
message: `Model usage: ${input} input, ${output} output, ${cacheCreation} cache creation and ${cacheRead} cache read tokens`,
|
||||
data: { input, output, cacheCreation, cacheRead, total: input + output + cacheCreation + cacheRead },
|
||||
};
|
||||
return { message: `Model usage: ${input} input and ${output} output tokens`, data: { input, output } };
|
||||
}
|
||||
case 'failed':
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import { assertPocBuild } from './local-poc.js';
|
||||
import { z } from 'zod';
|
||||
import { requireDeploymentSecretPolicy } from './deployment-secret-policy.js';
|
||||
import { gatewayIdentityOrigins } from './gateway-target.js';
|
||||
@ -15,9 +16,20 @@ import {
|
||||
isProduction,
|
||||
type CheckpointRecord,
|
||||
type RunnerTask,
|
||||
type PocReleaseSet,
|
||||
} from '@wishforge/contracts';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { record, touch, must, projectAccess, idempotent, now, executionScope, setWaiting } from './control-plane.js';
|
||||
import {
|
||||
record,
|
||||
touch,
|
||||
must,
|
||||
projectAccess,
|
||||
idempotent,
|
||||
now,
|
||||
executionScope,
|
||||
setWaiting,
|
||||
clearWaiting,
|
||||
} from './control-plane.js';
|
||||
import { captureLesson } from './lessons.js';
|
||||
import { digest, releaseRisk, requireRole, requireHuman } from './policy.js';
|
||||
|
||||
@ -26,6 +38,7 @@ import { assertVerificationProfile, verificationPolicyDigest } from './verificat
|
||||
|
||||
export function releaseBinding(release: Release) {
|
||||
return {
|
||||
pocRelease: release.pocRelease,
|
||||
deploymentSecretPolicyDigest: release.deploymentSecretPolicyDigest,
|
||||
applicationUrl: release.applicationUrl,
|
||||
deliveryRequired: release.deliveryRequired,
|
||||
@ -289,7 +302,9 @@ export class Releases {
|
||||
classification.sourceCommit === run.verifiedCommit,
|
||||
preauthorized: env.autoDeploy,
|
||||
});
|
||||
await assertPocBuild(project, env.id, build.pocRelease, this.env);
|
||||
const binding = {
|
||||
pocRelease: build.pocRelease,
|
||||
deploymentSecretPolicyDigest: env.deploymentSecretPolicyDigest,
|
||||
applicationUrl: env.applicationUrl,
|
||||
deliveryRequired: isProduction(this.env) || !!env.applicationUrl,
|
||||
@ -371,6 +386,7 @@ export class Releases {
|
||||
'Requirements, code, policy, configuration, secrets or environment changed; request a new release',
|
||||
);
|
||||
requireDeploymentSecretPolicy(release);
|
||||
await assertPocBuild(project, environment.id, release.pocRelease, this.env);
|
||||
if (requireApproval && release.approvalId) {
|
||||
const approval = await must<Approval>(tx, 'approval', release.approvalId);
|
||||
if (
|
||||
@ -458,6 +474,27 @@ export class Releases {
|
||||
'ACCEPTANCE_REQUIRED',
|
||||
'Health and business smoke evidence must bind this exact release and environment',
|
||||
);
|
||||
if (release.pocRelease) {
|
||||
const task = evidence.runnerTaskId ? await tx.get<RunnerTask>('runner_task', evidence.runnerTaskId) : undefined;
|
||||
const result = task?.result as { status?: string; evidence?: Record<string, unknown> } | undefined;
|
||||
const envelope = task?.envelope as { payload?: { release?: { pocRelease?: unknown } } } | undefined;
|
||||
if (
|
||||
actor.userId !== `runner:${env.id}` ||
|
||||
!task ||
|
||||
task.runnerId !== env.runnerId ||
|
||||
task.releaseId !== release.id ||
|
||||
task.environmentId !== env.id ||
|
||||
result?.status !== 'verified' ||
|
||||
task.resultDigest !== digest(result) ||
|
||||
digest(result.evidence?.pocRelease) !== digest(release.pocRelease) ||
|
||||
result.evidence?.bundleDigest !== digest(release.pocRelease) ||
|
||||
digest(envelope?.payload?.release?.pocRelease) !== digest(release.pocRelease)
|
||||
)
|
||||
throw new DomainError(
|
||||
'POC_RUNNER_RECEIPT_REQUIRED',
|
||||
'POC acceptance requires the registered Runner receipt for the entire signed component set',
|
||||
);
|
||||
}
|
||||
assertApplicationAccepted(release, evidence.accessEvidence, this.env);
|
||||
if (release.applicationUrl) release.deliveryUrl = release.applicationUrl;
|
||||
release.status = 'healthy';
|
||||
@ -469,6 +506,7 @@ export class Releases {
|
||||
throw new DomainError('MODEL_USAGE_UNCONFIRMED', 'Outstanding model usage must be reconciled before acceptance');
|
||||
run.status = 'succeeded';
|
||||
run.stage = 'accept';
|
||||
clearWaiting(run);
|
||||
run.reservedTokens = 0;
|
||||
await captureLesson(tx, run);
|
||||
run.lastActiveAt = undefined;
|
||||
@ -516,6 +554,52 @@ export class Releases {
|
||||
return { environmentId, releaseId, status: release.status };
|
||||
});
|
||||
}
|
||||
/** Reconstruct the same immutable recovery bundle at dispatch, retries and receipt acceptance. */
|
||||
async rollbackPocManifest(
|
||||
tx: Transaction,
|
||||
current: Release,
|
||||
target: Release,
|
||||
operationId: string,
|
||||
): Promise<PocReleaseSet | undefined> {
|
||||
if (!current.pocRelease && !target.pocRelease) return undefined;
|
||||
const from = current.pocRelease,
|
||||
to = target.pocRelease;
|
||||
if (
|
||||
!from ||
|
||||
!to ||
|
||||
!from.rollbackCompatible ||
|
||||
current.projectId !== target.projectId ||
|
||||
current.environmentId !== target.environmentId ||
|
||||
current.previousReleaseId !== target.id ||
|
||||
digest(releaseBinding(current)) !== current.bindingDigest ||
|
||||
digest(releaseBinding(target)) !== target.bindingDigest ||
|
||||
from.sourceCommit !== current.sourceCommit ||
|
||||
to.sourceCommit !== target.sourceCommit ||
|
||||
from.images.api !== current.imageDigest ||
|
||||
to.images.api !== target.imageDigest ||
|
||||
from.profileDigest !== to.profileDigest ||
|
||||
!from.expectedTemplateDigests ||
|
||||
!to.expectedTemplateDigests ||
|
||||
digest(from.expectedTemplateDigests) !== digest(to.expectedTemplateDigests) ||
|
||||
digest(from.expectedImages) !== digest(to.images)
|
||||
)
|
||||
throw new DomainError(
|
||||
'ROLLBACK_REQUIRES_OPERATOR',
|
||||
'POC rollback requires a compatible direct predecessor, complete component history and unchanged configuration fingerprints',
|
||||
);
|
||||
const project = await must<Project>(tx, 'project', current.projectId);
|
||||
await assertPocBuild(project, current.environmentId, from, this.env);
|
||||
await assertPocBuild(project, target.environmentId, to, this.env);
|
||||
return {
|
||||
...to,
|
||||
id: `rollback-${operationId}`,
|
||||
images: { ...to.images },
|
||||
expectedImages: { ...from.images },
|
||||
expectedTemplateDigests: { ...from.expectedTemplateDigests },
|
||||
// If restoring the prior version fails, compensation restores the compatible current version.
|
||||
rollbackCompatible: true,
|
||||
};
|
||||
}
|
||||
async rollback(actor: Actor, id: string, key?: string) {
|
||||
requireRole(actor, ['owner', 'admin', 'deployer']);
|
||||
requireHuman(actor);
|
||||
@ -559,8 +643,11 @@ export class Releases {
|
||||
environmentId: env.id,
|
||||
expectedReleaseId: id,
|
||||
targetBindingDigest: previous.bindingDigest,
|
||||
sourceBindingDigest: release.bindingDigest,
|
||||
status: 'pending',
|
||||
};
|
||||
// Validate before acquiring the environment lock. Dispatch persists the actual recovery manifest.
|
||||
await this.rollbackPocManifest(tx, release, previous, operation.id);
|
||||
// Acquiring the environment lock here prevents a ready forward release racing the rollback.
|
||||
env.activeReleaseId = previous.id;
|
||||
await tx.put('environment', touch(env));
|
||||
|
||||
@ -11,6 +11,7 @@ import {
|
||||
type Release,
|
||||
type RunnerRegistration,
|
||||
type RunnerTask,
|
||||
type PocReleaseSet,
|
||||
} from '@wishforge/contracts';
|
||||
import type { Store, Transaction } from '../db/store.js';
|
||||
import { must, projectAccess, record, touch, now } from './control-plane.js';
|
||||
@ -29,6 +30,7 @@ interface Envelope {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
type PocRollbackOperation = RunnerTask & { sourceBindingDigest?: string; pocRelease?: PocReleaseSet };
|
||||
const receipt = z
|
||||
.object({
|
||||
result: z
|
||||
@ -104,6 +106,23 @@ export class RunnerBroker {
|
||||
throw new DomainError('RUNNER_REVOKED', 'Runner registration is inactive or replaced', 403);
|
||||
return registration;
|
||||
}
|
||||
private async rollbackPoc(
|
||||
tx: Transaction,
|
||||
operation: RunnerTask,
|
||||
target: Release,
|
||||
): Promise<PocReleaseSet | undefined> {
|
||||
const stored = operation as PocRollbackOperation;
|
||||
const current = await must<Release>(tx, 'release', String(operation.expectedReleaseId));
|
||||
if (current.pocRelease && stored.sourceBindingDigest !== current.bindingDigest)
|
||||
throw new DomainError('ROLLBACK_STALE', 'The current POC artifact binding changed after rollback was requested');
|
||||
const recovery = await this.releases.rollbackPocManifest(tx, current, target, operation.id);
|
||||
if (stored.pocRelease && digest(stored.pocRelease) !== digest(recovery ?? null))
|
||||
throw new DomainError(
|
||||
'ROLLBACK_STALE',
|
||||
'The persisted POC recovery manifest no longer matches its source and target',
|
||||
);
|
||||
return recovery;
|
||||
}
|
||||
async poll(actor: Actor, environmentId: string) {
|
||||
const key = await this.signingKey();
|
||||
return this.store.transaction(actor.tenantId, async tx => {
|
||||
@ -130,6 +149,12 @@ export class RunnerBroker {
|
||||
digest(target.secretVersionRefs) !== digest(environment.secretVersionRefs)
|
||||
)
|
||||
throw new DomainError('ROLLBACK_STALE', 'Rollback state changed');
|
||||
const recovery = await this.rollbackPoc(tx, operation, target);
|
||||
if (digest(envelope.payload.release.pocRelease ?? null) !== digest(recovery ?? null))
|
||||
throw new DomainError(
|
||||
'ROLLBACK_STALE',
|
||||
'Signed rollback manifest differs from the current recovery plan',
|
||||
);
|
||||
}
|
||||
return envelope;
|
||||
}
|
||||
@ -146,6 +171,7 @@ export class RunnerBroker {
|
||||
let release: Release | undefined;
|
||||
let action: 'deploy' | 'reconcile';
|
||||
let previous: string | undefined;
|
||||
let recoveryPoc: PocReleaseSet | undefined;
|
||||
if (rollback) {
|
||||
release = await must<Release>(tx, 'release', String(rollback.targetReleaseId));
|
||||
if (
|
||||
@ -166,6 +192,8 @@ export class RunnerBroker {
|
||||
);
|
||||
action = rollback.status === 'pending' ? 'deploy' : 'reconcile';
|
||||
previous = String(rollback.expectedReleaseId);
|
||||
recoveryPoc = await this.rollbackPoc(tx, rollback, release);
|
||||
if (recoveryPoc) Object.assign(rollback, { pocRelease: recoveryPoc });
|
||||
} else {
|
||||
const releases = await tx.list<Release>('release');
|
||||
release = environment.activeReleaseId
|
||||
@ -210,6 +238,7 @@ export class RunnerBroker {
|
||||
environmentId,
|
||||
sourceCommit: release.sourceCommit,
|
||||
image: release.imageDigest,
|
||||
pocRelease: rollback ? recoveryPoc : release.pocRelease,
|
||||
architecture: release.architecture,
|
||||
configVersion: release.configVersion,
|
||||
deploymentSecretPolicyDigest: release.deploymentSecretPolicyDigest,
|
||||
@ -293,14 +322,28 @@ export class RunnerBroker {
|
||||
Array.isArray(result.evidence.checks) &&
|
||||
result.evidence.checks.includes('health') &&
|
||||
result.evidence.checks.includes('business-smoke');
|
||||
const rollbackOperation = task.rollbackOperationId
|
||||
? await must<RunnerTask>(tx, 'runner_task', String(task.rollbackOperationId))
|
||||
: undefined;
|
||||
const expectedPoc = rollbackOperation
|
||||
? await this.rollbackPoc(tx, rollbackOperation, release)
|
||||
: release.pocRelease;
|
||||
if (digest(payload.release.pocRelease ?? null) !== digest(expectedPoc ?? null))
|
||||
throw new DomainError(
|
||||
'RESULT_MISMATCH',
|
||||
'The signed component bundle no longer matches this deployment operation',
|
||||
);
|
||||
const verified =
|
||||
basicVerified &&
|
||||
(!expectedPoc ||
|
||||
(digest(result.evidence.pocRelease ?? null) === digest(expectedPoc) &&
|
||||
result.evidence.bundleDigest === digest(expectedPoc))) &&
|
||||
matchesApplicationAccess(release, result.evidence.accessEvidence) &&
|
||||
(!isProduction(this.env) || !!release.applicationUrl);
|
||||
|
||||
let rolledBack = false;
|
||||
if (task.rollbackOperationId) {
|
||||
const operation = await must<RunnerTask>(tx, 'runner_task', String(task.rollbackOperationId));
|
||||
if (rollbackOperation) {
|
||||
const operation = rollbackOperation;
|
||||
if (
|
||||
operation.status !== 'dispatching' ||
|
||||
operation.lastTaskId !== task.id ||
|
||||
@ -324,6 +367,8 @@ export class RunnerBroker {
|
||||
operation.completedAt = now();
|
||||
const evidence = this.deploymentEvidence(actor, release, environment, task.id, result.evidence);
|
||||
await tx.put('evidence', evidence);
|
||||
release.evidenceIds.push(evidence.id);
|
||||
release.deployedAt = now();
|
||||
operation.evidenceId = evidence.id;
|
||||
await tx.put('release', touch(original));
|
||||
await tx.put('release', touch(release));
|
||||
@ -344,6 +389,10 @@ export class RunnerBroker {
|
||||
}
|
||||
await tx.put('runner_task', touch(operation));
|
||||
} else if (verified) {
|
||||
// Persist the bound receipt in this same transaction before acceptance checks it.
|
||||
task.result = result;
|
||||
task.resultDigest = digest(result);
|
||||
await tx.put('runner_task', touch(task));
|
||||
await this.releases.assertCurrent(tx, release);
|
||||
const evidence = this.deploymentEvidence(actor, release, environment, task.id, result.evidence);
|
||||
await tx.put('evidence', evidence);
|
||||
|
||||
@ -91,6 +91,25 @@ const checkpointBody = z
|
||||
.optional(),
|
||||
createdAt: z.string().max(64),
|
||||
sessionId: z.string().max(200).optional(),
|
||||
providerCompletion: z
|
||||
.object({
|
||||
status: z.enum(['completed', 'failed', 'canceled']),
|
||||
revision: z.number().int().positive(),
|
||||
repairCount: z.number().int().nonnegative(),
|
||||
summary: z.string().max(4000).optional(),
|
||||
finalUsage: z
|
||||
.array(
|
||||
z
|
||||
.object({
|
||||
operationId: z.string().min(1).max(300),
|
||||
tokens: z.number().int().nonnegative().safe(),
|
||||
})
|
||||
.strict(),
|
||||
)
|
||||
.max(100),
|
||||
})
|
||||
.strict()
|
||||
.optional(),
|
||||
})
|
||||
.strict(),
|
||||
})
|
||||
@ -152,6 +171,10 @@ export class Errors implements ExceptionFilter {
|
||||
@Controller('v1')
|
||||
export class ApiController {
|
||||
constructor(@Inject(Services) private s: Services) {}
|
||||
@Get('identity') identity(@Req() req: AuthRequest) {
|
||||
const { userId, role, kind } = req.actor;
|
||||
return ok({ userId, role, kind });
|
||||
}
|
||||
@Get('dashboard') async dashboard(@Req() req: AuthRequest) {
|
||||
const a = req.actor;
|
||||
const [projects, runs, releases, providers, usage] = await Promise.all([
|
||||
@ -312,6 +335,9 @@ export class ApiController {
|
||||
@Get('runs/:id') async run(@Req() r: AuthRequest, @Param('id') id: string) {
|
||||
return ok(await this.s.control.run(r.actor, id));
|
||||
}
|
||||
@Patch('runs/:id/budget') async updateBudget(@Req() r: AuthRequest, @Param('id') id: string, @Body() b: unknown) {
|
||||
return ok(await this.s.control.updateBudget(r.actor, id, b, key(r)));
|
||||
}
|
||||
@Get('runs/:id/events') async events(@Req() r: AuthRequest, @Param('id') id: string, @Query('after') after?: string) {
|
||||
return ok(await this.s.control.events(r.actor, id, Math.max(0, Number(after) || 0)));
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ executor 与 verifier 各自只拿到自己的运行级令牌(由 API 签发
|
||||
| `WF_PROVISIONER_TOKEN` | API 调用本服务的令牌(至少 32 字符) |
|
||||
| `WF_TENANT_ID` | 本服务的唯一租户(本地为 `demo`) |
|
||||
| `WF_RESOURCE_GRANT_SECRET` | 与 API 共享的资源授权签名密钥 |
|
||||
| `WF_LOCAL_RUN_ROOT` | 运行目录,默认 `.wishforge/runs`;每个代次为 `<租户>/<运行>/<代次>/{executor,verifier}` |
|
||||
| `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 进程 |
|
||||
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { mkdir, rm } from 'node:fs/promises';
|
||||
import { homedir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { verifyResourceGrant } from './resource-grants.mjs';
|
||||
|
||||
const id = /^[A-Za-z0-9_-]{1,128}$/;
|
||||
const SOURCE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
|
||||
/** Process environment a worker inherits from this host; nothing else from the provisioner's own environment. */
|
||||
const PASSTHROUGH = [
|
||||
'PATH',
|
||||
@ -14,6 +17,7 @@ const PASSTHROUGH = [
|
||||
'HTTPS_PROXY',
|
||||
'NO_PROXY',
|
||||
'WF_GIT_ALLOW_LOCAL_HTTP',
|
||||
'WF_LOCAL_POC_BINDING_FILE',
|
||||
'WF_CODEBUDDY_BIN',
|
||||
'WF_CLAUDE_BIN',
|
||||
'WF_DOCKER_CONFIG',
|
||||
@ -43,10 +47,11 @@ export function readLocalConfig(env = process.env, cwd = process.cwd()) {
|
||||
controlPlaneUrl: env.WF_CONTROL_PLANE_URL ?? 'http://127.0.0.1:4000',
|
||||
temporalAddress: env.TEMPORAL_ADDRESS ?? '127.0.0.1:7233',
|
||||
temporalNamespace: env.TEMPORAL_NAMESPACE ?? 'default',
|
||||
runRoot: path.resolve(cwd, env.WF_LOCAL_RUN_ROOT ?? '.wishforge/runs'),
|
||||
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.
|
||||
acceptAnyImage: true,
|
||||
@ -54,7 +59,19 @@ export function readLocalConfig(env = process.env, cwd = process.cwd()) {
|
||||
memoryMiBCap: 262144,
|
||||
storageSize: '1024Gi',
|
||||
};
|
||||
// Node and TypeScript search ancestor node_modules. A nested project would silently
|
||||
// inherit WishForge's React/types instead of checking only its own locked dependencies.
|
||||
const relativeToSource = path.relative(SOURCE_ROOT, config.runRoot);
|
||||
if (
|
||||
!relativeToSource ||
|
||||
(!relativeToSource.startsWith(`..${path.sep}`) && relativeToSource !== '..' && !path.isAbsolute(relativeToSource))
|
||||
)
|
||||
throw new Error(
|
||||
'WF_LOCAL_RUN_ROOT must be outside the WishForge source tree; use ~/.local/share/wishforge/runs and recover old attempts from persisted checkpoints',
|
||||
);
|
||||
if (!id.test(config.tenantId)) throw new Error('Invalid WF_TENANT_ID');
|
||||
if (config.claudeAuthHome && !path.isAbsolute(config.claudeAuthHome))
|
||||
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) {
|
||||
@ -125,7 +142,10 @@ export function createLocalBackend(
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_WORKER_ROLE: role,
|
||||
...(role === 'executor'
|
||||
? { WF_WORKER_TOKEN: request.executorToken }
|
||||
? {
|
||||
WF_WORKER_TOKEN: request.executorToken,
|
||||
...(config.claudeAuthHome ? { WF_CLAUDE_LOCAL_AUTH_HOME: config.claudeAuthHome } : {}),
|
||||
}
|
||||
: { WF_VERIFIER_TOKEN: request.verifierToken }),
|
||||
WF_SANDBOX_MODE: 'host',
|
||||
WF_SANDBOX_TENANT_ID: request.tenantId,
|
||||
|
||||
@ -5,6 +5,7 @@ import { createHmac } from 'node:crypto';
|
||||
import { mkdtemp, rm, stat } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { createLocalBackend, readLocalConfig } from '../local.mjs';
|
||||
import { resourceGrantPayload } from '../resource-grants.mjs';
|
||||
|
||||
@ -94,8 +95,22 @@ test('refuses to run outside development and requires the grant secret', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('places default workspaces outside platform dependencies and rejects nested roots', () => {
|
||||
const env = { WF_DEPLOYMENT_MODE: 'development', WF_TENANT_ID: 'demo', WF_RESOURCE_GRANT_SECRET: secret };
|
||||
const source = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
|
||||
assert.equal(readLocalConfig(env, source).runRoot, path.join(os.homedir(), '.local/share/wishforge/runs'));
|
||||
for (const runRoot of [source, path.join(source, '.wishforge/runs'), '.wishforge/runs'])
|
||||
assert.throws(
|
||||
() => readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: runRoot }, source),
|
||||
/outside the WishForge source tree/,
|
||||
);
|
||||
assert.equal(readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: `${source}-runs` }, source).runRoot, `${source}-runs`);
|
||||
const temporary = path.join(os.tmpdir(), 'standalone-local-test');
|
||||
assert.equal(readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: 'runs' }, temporary).runRoot, path.join(temporary, 'runs'));
|
||||
});
|
||||
|
||||
test('starts one executor and one verifier per generation with only their own credentials', async () => {
|
||||
const f = await fixture();
|
||||
const f = await fixture({ WF_CLAUDE_LOCAL_AUTH_HOME: '/home/operator' });
|
||||
try {
|
||||
assert.deepEqual(await f.backend.provisionRun(request()), {
|
||||
status: 'provisioned',
|
||||
@ -112,6 +127,8 @@ test('starts one executor and one verifier per generation with only their own cr
|
||||
assert.equal(executor.options.env.WF_VERIFIER_TOKEN, undefined);
|
||||
assert.equal(verifier.options.env.WF_VERIFIER_TOKEN, 'verifier-token');
|
||||
assert.equal(verifier.options.env.WF_WORKER_TOKEN, undefined);
|
||||
assert.equal(executor.options.env.WF_CLAUDE_LOCAL_AUTH_HOME, '/home/operator');
|
||||
assert.equal(verifier.options.env.WF_CLAUDE_LOCAL_AUTH_HOME, undefined);
|
||||
for (const child of f.started) {
|
||||
assert.equal(child.options.env.WF_SANDBOX_MODE, 'host');
|
||||
assert.equal(child.options.env.WF_SANDBOX_GENERATION, 'generation-1');
|
||||
|
||||
40
apps/runner/src/adapters.ts
Normal file
40
apps/runner/src/adapters.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import path from 'node:path';
|
||||
import {
|
||||
ComposeDeploymentAdapter,
|
||||
KubernetesDeploymentAdapter,
|
||||
DeploymentSecretBroker,
|
||||
loadPocProfile,
|
||||
PocRunnerDeploymentAdapter,
|
||||
} from '@wishforge/runtime';
|
||||
import type { RunnerConfig } from './runner.js';
|
||||
|
||||
/** An adopted POC runner only accepts the operator's fixed Kubernetes profile. */
|
||||
export async function createRunnerAdapters(env: Record<string, string | undefined>): Promise<RunnerConfig['adapters']> {
|
||||
if (env.WF_POC_PROFILE_FILE) {
|
||||
if (!path.isAbsolute(env.WF_POC_PROFILE_FILE)) throw new Error('WF_POC_PROFILE_FILE must be an absolute path');
|
||||
if (!env.WF_RUNNER_STATE_DIR) throw new Error('WF_RUNNER_STATE_DIR is required');
|
||||
const profile = await loadPocProfile(env.WF_POC_PROFILE_FILE);
|
||||
return { kubernetes: new PocRunnerDeploymentAdapter(profile, path.join(env.WF_RUNNER_STATE_DIR, 'poc')) };
|
||||
}
|
||||
return {
|
||||
compose: new ComposeDeploymentAdapter({
|
||||
secretBroker: new DeploymentSecretBroker(),
|
||||
dockerConfig: env.WF_DEPLOYMENT_DOCKER_CONFIG,
|
||||
directory: env.WF_COMPOSE_DIRECTORY,
|
||||
project: env.WF_COMPOSE_PROJECT,
|
||||
healthUrl: env.WF_HEALTH_URL,
|
||||
dockerContext: env.WF_DOCKER_CONTEXT,
|
||||
candidateHealthUrlTemplate: env.WF_CANDIDATE_HEALTH_URL_TEMPLATE,
|
||||
proxyAdminUrl: env.WF_CADDY_ADMIN_URL,
|
||||
proxyConfigPath: env.WF_CADDY_PROXY_CONFIG_PATH,
|
||||
proxyDialHost: env.WF_CADDY_DIAL_HOST,
|
||||
}),
|
||||
kubernetes: new KubernetesDeploymentAdapter({
|
||||
secretBroker: new DeploymentSecretBroker(),
|
||||
context: env.WF_KUBE_CONTEXT,
|
||||
namespace: env.WF_KUBE_NAMESPACE,
|
||||
healthUrl: env.WF_HEALTH_URL,
|
||||
candidateHealthUrlTemplate: env.WF_CANDIDATE_HEALTH_URL_TEMPLATE,
|
||||
}),
|
||||
};
|
||||
}
|
||||
@ -1,13 +1,14 @@
|
||||
import https from 'node:https';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import {
|
||||
ComposeDeploymentAdapter,
|
||||
KubernetesDeploymentAdapter,
|
||||
DeploymentSecretBroker,
|
||||
loadPocProfile,
|
||||
profileDigest,
|
||||
type SignedRunnerTask,
|
||||
type TrustedPocDeliveryPolicy,
|
||||
} from '@wishforge/runtime';
|
||||
import { requireDeploymentMode } from '@wishforge/contracts';
|
||||
import { OutboundRunner } from './runner.js';
|
||||
import { createRunnerTransport } from './transport.js';
|
||||
import { createRunnerAdapters } from './adapters.js';
|
||||
|
||||
async function main() {
|
||||
requireDeploymentMode(process.env);
|
||||
@ -16,90 +17,26 @@ async function main() {
|
||||
'WF_RUNNER_TOKEN',
|
||||
'WF_TENANT_ID',
|
||||
'WF_ENVIRONMENT_ID',
|
||||
'WF_RUNNER_CA',
|
||||
'WF_RUNNER_CERT',
|
||||
'WF_RUNNER_KEY',
|
||||
'WF_RUNNER_PUBLIC_KEYS',
|
||||
'WF_RUNNER_STATE_DIR',
|
||||
];
|
||||
for (const key of required) if (!process.env[key]) throw new Error(`unconfigured: ${key} is required`);
|
||||
const base = new URL(process.env.WF_CONTROL_PLANE_URL!);
|
||||
if (base.protocol !== 'https:') throw new Error('Runner only connects to an HTTPS control plane with mTLS');
|
||||
const agent = new https.Agent({
|
||||
ca: await readFile(process.env.WF_RUNNER_CA!),
|
||||
cert: await readFile(process.env.WF_RUNNER_CERT!),
|
||||
key: await readFile(process.env.WF_RUNNER_KEY!),
|
||||
rejectUnauthorized: true,
|
||||
});
|
||||
const request = (route: string, body?: unknown): Promise<unknown> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const url = new URL(route, base);
|
||||
if (url.origin !== base.origin) {
|
||||
reject(new Error('Runner request origin mismatch'));
|
||||
return;
|
||||
}
|
||||
const req = https.request(
|
||||
url,
|
||||
{
|
||||
agent,
|
||||
method: body ? 'POST' : 'GET',
|
||||
headers: {
|
||||
authorization: `Bearer ${process.env.WF_RUNNER_TOKEN!}`,
|
||||
'x-wishforge-tenant': process.env.WF_TENANT_ID!,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
timeout: 30_000,
|
||||
},
|
||||
response => {
|
||||
let output = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', chunk => {
|
||||
output += chunk;
|
||||
if (output.length > 4 * 1024 * 1024) req.destroy(new Error('Response exceeded limit'));
|
||||
});
|
||||
response.on('end', () => {
|
||||
if ((response.statusCode ?? 500) >= 300) reject(new Error(`Control plane returned ${response.statusCode}`));
|
||||
else {
|
||||
try {
|
||||
resolve(output ? JSON.parse(output) : null);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on('timeout', () => req.destroy(new Error('Control plane request timed out')));
|
||||
req.on('error', reject);
|
||||
req.end(body ? JSON.stringify(body) : undefined);
|
||||
});
|
||||
const { request, close } = await createRunnerTransport(process.env);
|
||||
let trustedPocDelivery: TrustedPocDeliveryPolicy | undefined;
|
||||
if (process.env.WF_POC_PROFILE_FILE) {
|
||||
if (process.env.WF_DEPLOYMENT_MODE !== 'development' || process.env.NODE_ENV === 'production')
|
||||
throw new Error('Adopted POC delivery requires explicit local development mode');
|
||||
const profile = await loadPocProfile(process.env.WF_POC_PROFILE_FILE);
|
||||
trustedPocDelivery = { profileDigest: profileDigest(profile), applicationUrl: profile.applicationUrl };
|
||||
}
|
||||
const runner = new OutboundRunner({
|
||||
tenantId: process.env.WF_TENANT_ID!,
|
||||
environmentId: process.env.WF_ENVIRONMENT_ID!,
|
||||
publicKeys: JSON.parse(await readFile(process.env.WF_RUNNER_PUBLIC_KEYS!, 'utf8')),
|
||||
stateDirectory: process.env.WF_RUNNER_STATE_DIR!,
|
||||
execute: process.env.WF_RUNNER_EXECUTE === 'true',
|
||||
adapters: {
|
||||
compose: new ComposeDeploymentAdapter({
|
||||
secretBroker: new DeploymentSecretBroker(),
|
||||
dockerConfig: process.env.WF_DEPLOYMENT_DOCKER_CONFIG,
|
||||
directory: process.env.WF_COMPOSE_DIRECTORY,
|
||||
project: process.env.WF_COMPOSE_PROJECT,
|
||||
healthUrl: process.env.WF_HEALTH_URL,
|
||||
dockerContext: process.env.WF_DOCKER_CONTEXT,
|
||||
candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE,
|
||||
proxyAdminUrl: process.env.WF_CADDY_ADMIN_URL,
|
||||
proxyConfigPath: process.env.WF_CADDY_PROXY_CONFIG_PATH,
|
||||
proxyDialHost: process.env.WF_CADDY_DIAL_HOST,
|
||||
}),
|
||||
kubernetes: new KubernetesDeploymentAdapter({
|
||||
secretBroker: new DeploymentSecretBroker(),
|
||||
context: process.env.WF_KUBE_CONTEXT,
|
||||
namespace: process.env.WF_KUBE_NAMESPACE,
|
||||
healthUrl: process.env.WF_HEALTH_URL,
|
||||
candidateHealthUrlTemplate: process.env.WF_CANDIDATE_HEALTH_URL_TEMPLATE,
|
||||
}),
|
||||
},
|
||||
adapters: await createRunnerAdapters(process.env),
|
||||
trustedPocDelivery,
|
||||
});
|
||||
let stopped = false;
|
||||
process.once('SIGTERM', () => {
|
||||
@ -124,7 +61,7 @@ async function main() {
|
||||
}
|
||||
await new Promise(resolve => setTimeout(resolve, 5_000));
|
||||
}
|
||||
agent.destroy();
|
||||
close();
|
||||
}
|
||||
void main().catch(error => {
|
||||
console.error(error.message);
|
||||
|
||||
@ -9,6 +9,7 @@ import {
|
||||
type DeploymentAdapter,
|
||||
type DeploymentResult,
|
||||
type SignedRunnerTask,
|
||||
type TrustedPocDeliveryPolicy,
|
||||
} from '@wishforge/runtime';
|
||||
|
||||
export interface RunnerConfig {
|
||||
@ -18,6 +19,7 @@ export interface RunnerConfig {
|
||||
stateDirectory: string;
|
||||
execute: boolean;
|
||||
adapters: Partial<Record<'compose' | 'kubernetes', DeploymentAdapter>>;
|
||||
trustedPocDelivery?: TrustedPocDeliveryPolicy;
|
||||
}
|
||||
export class OutboundRunner {
|
||||
constructor(private readonly config: RunnerConfig) {}
|
||||
|
||||
86
apps/runner/src/transport.ts
Normal file
86
apps/runner/src/transport.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import http from 'node:http';
|
||||
import https from 'node:https';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { requireDeploymentMode } from '@wishforge/contracts';
|
||||
|
||||
type Env = Record<string, string | undefined>;
|
||||
|
||||
export function runnerTransportPolicy(env: Env) {
|
||||
const mode = requireDeploymentMode(env);
|
||||
const value = env.WF_CONTROL_PLANE_URL;
|
||||
if (!value) throw new Error('unconfigured: WF_CONTROL_PLANE_URL is required');
|
||||
const base = new URL(value);
|
||||
if (base.username || base.password || base.search || base.hash || base.pathname !== '/')
|
||||
throw new Error('Runner control plane URL must be an origin without credentials, path, query or fragment');
|
||||
// Check the original authority too: URL normalizes alternate IPv4 spellings such as 127.1.
|
||||
const localHttp =
|
||||
mode === 'development' &&
|
||||
env.WF_RUNNER_ALLOW_LOCAL_HTTP === 'true' &&
|
||||
/^http:\/\/(?:127\.0\.0\.1|localhost)(?::[0-9]+)?\/?$/.test(value);
|
||||
if (base.protocol !== 'https:' && !localHttp)
|
||||
throw new Error('Runner requires HTTPS with mTLS; explicit development HTTP is limited to 127.0.0.1 or localhost');
|
||||
if (!localHttp) {
|
||||
for (const key of ['WF_RUNNER_CA', 'WF_RUNNER_CERT', 'WF_RUNNER_KEY'])
|
||||
if (!env[key]) throw new Error(`unconfigured: ${key} is required for Runner mTLS`);
|
||||
}
|
||||
return { base, localHttp };
|
||||
}
|
||||
|
||||
export async function createRunnerTransport(env: Env) {
|
||||
const { base, localHttp } = runnerTransportPolicy(env);
|
||||
for (const key of ['WF_RUNNER_TOKEN', 'WF_TENANT_ID'])
|
||||
if (!env[key]) throw new Error(`unconfigured: ${key} is required`);
|
||||
const agent = localHttp
|
||||
? new http.Agent()
|
||||
: new https.Agent({
|
||||
ca: await readFile(env.WF_RUNNER_CA!),
|
||||
cert: await readFile(env.WF_RUNNER_CERT!),
|
||||
key: await readFile(env.WF_RUNNER_KEY!),
|
||||
rejectUnauthorized: true,
|
||||
});
|
||||
const transportRequest = localHttp ? http.request : https.request;
|
||||
const request = (route: string, body?: unknown): Promise<unknown> =>
|
||||
new Promise((resolve, reject) => {
|
||||
const url = new URL(route, base);
|
||||
if (url.origin !== base.origin) {
|
||||
reject(new Error('Runner request origin mismatch'));
|
||||
return;
|
||||
}
|
||||
const req = transportRequest(
|
||||
url,
|
||||
{
|
||||
agent,
|
||||
method: body === undefined ? 'GET' : 'POST',
|
||||
headers: {
|
||||
authorization: `Bearer ${env.WF_RUNNER_TOKEN!}`,
|
||||
'x-wishforge-tenant': env.WF_TENANT_ID!,
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
timeout: 30_000,
|
||||
},
|
||||
response => {
|
||||
let output = '';
|
||||
response.setEncoding('utf8');
|
||||
response.on('data', chunk => {
|
||||
output += chunk;
|
||||
if (output.length > 4 * 1024 * 1024) req.destroy(new Error('Response exceeded limit'));
|
||||
});
|
||||
response.on('error', reject);
|
||||
response.on('end', () => {
|
||||
if ((response.statusCode ?? 500) >= 300) reject(new Error(`Control plane returned ${response.statusCode}`));
|
||||
else {
|
||||
try {
|
||||
resolve(output ? JSON.parse(output) : null);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
req.on('timeout', () => req.destroy(new Error('Control plane request timed out')));
|
||||
req.on('error', reject);
|
||||
req.end(body === undefined ? undefined : JSON.stringify(body));
|
||||
});
|
||||
return { request, close: () => agent.destroy() };
|
||||
}
|
||||
147
apps/runner/test/local-entry.test.ts
Normal file
147
apps/runner/test/local-entry.test.ts
Normal file
@ -0,0 +1,147 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import http from 'node:http';
|
||||
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { PocRunnerDeploymentAdapter, KubernetesDeploymentAdapter, ComposeDeploymentAdapter } from '@wishforge/runtime';
|
||||
import { createRunnerTransport, runnerTransportPolicy } from '../src/transport.js';
|
||||
import { createRunnerAdapters } from '../src/adapters.js';
|
||||
|
||||
const local = {
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_RUNNER_ALLOW_LOCAL_HTTP: 'true',
|
||||
WF_CONTROL_PLANE_URL: 'http://127.0.0.1:4000',
|
||||
};
|
||||
const dirs: string[] = [];
|
||||
afterEach(async () => {
|
||||
await Promise.all(dirs.splice(0).map(directory => rm(directory, { recursive: true, force: true })));
|
||||
});
|
||||
|
||||
describe('local runner transport boundary', () => {
|
||||
it('requires explicit development and a literal loopback HTTP origin', () => {
|
||||
expect(runnerTransportPolicy(local).localHttp).toBe(true);
|
||||
expect(runnerTransportPolicy({ ...local, WF_CONTROL_PLANE_URL: 'http://localhost:4000/' }).localHttp).toBe(true);
|
||||
for (const value of [
|
||||
'http://192.168.1.2:4000',
|
||||
'http://example.test',
|
||||
'http://127.1:4000',
|
||||
'http://2130706433:4000',
|
||||
'http://[::1]:4000',
|
||||
'http://localhost.evil.test',
|
||||
'https://user:pass@example.test',
|
||||
'http://localhost:4000/?x=1',
|
||||
'http://localhost:4000/api',
|
||||
])
|
||||
expect(() => runnerTransportPolicy({ ...local, WF_CONTROL_PLANE_URL: value })).toThrow();
|
||||
expect(() => runnerTransportPolicy({ ...local, WF_RUNNER_ALLOW_LOCAL_HTTP: undefined })).toThrow('HTTPS');
|
||||
expect(() => runnerTransportPolicy({ ...local, WF_DEPLOYMENT_MODE: 'production' })).toThrow('HTTPS');
|
||||
expect(() => runnerTransportPolicy({ ...local, WF_DEPLOYMENT_MODE: undefined })).toThrow('Set WF_DEPLOYMENT_MODE');
|
||||
});
|
||||
it('retains mandatory mTLS for every HTTPS connection and never inherits global insecure-HTTP opt-in', () => {
|
||||
expect(() => runnerTransportPolicy({ ...local, WF_CONTROL_PLANE_URL: 'https://localhost:4000' })).toThrow(
|
||||
'WF_RUNNER_CA',
|
||||
);
|
||||
expect(() =>
|
||||
runnerTransportPolicy({ ...local, WF_RUNNER_ALLOW_LOCAL_HTTP: undefined, WF_ALLOW_INSECURE_HTTP: 'true' }),
|
||||
).toThrow('HTTPS');
|
||||
expect(
|
||||
runnerTransportPolicy({
|
||||
...local,
|
||||
WF_DEPLOYMENT_MODE: 'production',
|
||||
WF_CONTROL_PLANE_URL: 'https://control.example.test',
|
||||
WF_RUNNER_CA: '/ca',
|
||||
WF_RUNNER_CERT: '/cert',
|
||||
WF_RUNNER_KEY: '/key',
|
||||
}).localHttp,
|
||||
).toBe(false);
|
||||
});
|
||||
it('keeps registered bearer and tenant headers on local requests, rejects redirects and origin escapes', async () => {
|
||||
const seen: Array<{ method?: string; url?: string; token?: string; tenant?: string; body: string }> = [];
|
||||
const server = http.createServer((req, res) => {
|
||||
let body = '';
|
||||
req.on('data', chunk => {
|
||||
body += String(chunk);
|
||||
});
|
||||
req.on('end', () => {
|
||||
seen.push({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
token: req.headers.authorization,
|
||||
tenant: String(req.headers['x-wishforge-tenant']),
|
||||
body,
|
||||
});
|
||||
if (req.url === '/redirect') {
|
||||
res.writeHead(302, { location: 'http://example.test/leak' });
|
||||
res.end();
|
||||
} else {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({ accepted: true }));
|
||||
}
|
||||
});
|
||||
});
|
||||
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') throw new Error('Expected TCP test server');
|
||||
const transport = await createRunnerTransport({
|
||||
...local,
|
||||
WF_CONTROL_PLANE_URL: `http://127.0.0.1:${address.port}`,
|
||||
WF_RUNNER_TOKEN: 'test-only-registered-runner-token',
|
||||
WF_TENANT_ID: 'tenant-poc',
|
||||
});
|
||||
try {
|
||||
expect(await transport.request('/internal/runner/tasks?environmentId=env-poc')).toEqual({ accepted: true });
|
||||
await transport.request('/internal/runner/tasks/task-1/result', { result: { status: 'verified' } });
|
||||
expect(seen[0]).toMatchObject({
|
||||
method: 'GET',
|
||||
token: 'Bearer test-only-registered-runner-token',
|
||||
tenant: 'tenant-poc',
|
||||
});
|
||||
expect(JSON.parse(seen[1].body)).toEqual({ result: { status: 'verified' } });
|
||||
await expect(transport.request('http://example.test/leak')).rejects.toThrow('origin mismatch');
|
||||
await expect(transport.request('/redirect')).rejects.toThrow('302');
|
||||
expect(seen).toHaveLength(3);
|
||||
} finally {
|
||||
transport.close();
|
||||
await new Promise<void>((resolve, reject) => server.close(error => (error ? reject(error) : resolve())));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('operator POC runner adapter wiring', () => {
|
||||
it('loads the fixed operator profile and enables only the POC Kubernetes adapter', async () => {
|
||||
const directory = await mkdtemp(path.join(tmpdir(), 'wishforge-runner-entry-'));
|
||||
dirs.push(directory);
|
||||
const file = path.join(directory, 'profile.json');
|
||||
const uid = '11111111-1111-4111-8111-111111111111';
|
||||
await writeFile(
|
||||
file,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
id: 'droboticflow-poc',
|
||||
repository: 'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
branch: 'poc',
|
||||
kubeconfig: '/operator/poc.kubeconfig',
|
||||
clusterServer: 'https://120.48.181.105:6443',
|
||||
namespace: 'droboticflow',
|
||||
namespaceUid: uid,
|
||||
deployments: { api: uid, worker: uid, coordinator: uid },
|
||||
registry: 'ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc',
|
||||
registrySecret: 'droboticflow-registry',
|
||||
applicationUrl: 'http://120.48.164.19',
|
||||
kanikoImage: `registry.test/kaniko@sha256:${'a'.repeat(64)}`,
|
||||
}),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
const adapters = await createRunnerAdapters({ WF_POC_PROFILE_FILE: file, WF_RUNNER_STATE_DIR: directory });
|
||||
expect(Object.keys(adapters)).toEqual(['kubernetes']);
|
||||
expect(adapters.kubernetes).toBeInstanceOf(PocRunnerDeploymentAdapter);
|
||||
await expect(
|
||||
createRunnerAdapters({ WF_POC_PROFILE_FILE: 'relative.json', WF_RUNNER_STATE_DIR: directory }),
|
||||
).rejects.toThrow('absolute path');
|
||||
});
|
||||
it('preserves generic adapters only when an adopted POC profile is absent', async () => {
|
||||
const adapters = await createRunnerAdapters({});
|
||||
expect(adapters.compose).toBeInstanceOf(ComposeDeploymentAdapter);
|
||||
expect(adapters.kubernetes).toBeInstanceOf(KubernetesDeploymentAdapter);
|
||||
});
|
||||
});
|
||||
@ -1,10 +1,18 @@
|
||||
import type { NextConfig } from 'next';
|
||||
|
||||
const config: NextConfig = {
|
||||
output: 'standalone',
|
||||
output: process.env.WF_LOCAL_ACCESS === 'true' ? undefined : 'standalone',
|
||||
poweredByHeader: false,
|
||||
agentRules: false,
|
||||
devIndicators: false,
|
||||
...(process.env.WF_LOCAL_ACCESS === 'true'
|
||||
? {
|
||||
allowedDevOrigins: (process.env.WF_LOCAL_ALLOWED_ORIGINS ?? '')
|
||||
.split(',')
|
||||
.filter(Boolean)
|
||||
.map(value => new URL(value.trim()).hostname),
|
||||
}
|
||||
: {}),
|
||||
experimental: {
|
||||
optimizePackageImports: ['lucide-react'],
|
||||
// Local development clusters run a development build (the image's WEB_BUILD=development):
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getSession, isDevelopmentAuth, oidcConfigured } from '@/lib/auth';
|
||||
import { localAccessEnabled } from '@/lib/local-access';
|
||||
export async function GET(request: NextRequest) {
|
||||
const session = await getSession(request);
|
||||
return NextResponse.json(
|
||||
{
|
||||
data: {
|
||||
authenticated: isDevelopmentAuth() || Boolean(session),
|
||||
mode: isDevelopmentAuth() ? 'development' : 'oidc',
|
||||
mode: localAccessEnabled() ? 'local' : isDevelopmentAuth() ? 'development' : 'oidc',
|
||||
name: session?.name,
|
||||
oidcConfigured: oidcConfigured(),
|
||||
},
|
||||
|
||||
@ -10,8 +10,52 @@ import {
|
||||
randomToken,
|
||||
safeReturnTo,
|
||||
seal,
|
||||
SESSION_COOKIE,
|
||||
} from '@/lib/auth';
|
||||
import {
|
||||
clearLoginFailures,
|
||||
localAccessEnabled,
|
||||
localCredentialId,
|
||||
localCredentials,
|
||||
LOCAL_SESSION_SECONDS,
|
||||
localRequestAllowed,
|
||||
loginRateLimit,
|
||||
recordLoginFailure,
|
||||
verifyLocalPassword,
|
||||
} from '@/lib/local-access';
|
||||
|
||||
const escapeHtml = (value: string) =>
|
||||
value.replace(/[&<>"']/g, char => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[char]!);
|
||||
|
||||
function localLoginPage(request: NextRequest, message = '', status = 200) {
|
||||
const returnTo = safeReturnTo(request.nextUrl.searchParams.get('returnTo'));
|
||||
return new Response(
|
||||
`<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>登录 · WishForge</title><style>
|
||||
*{box-sizing:border-box}body{margin:0;background:#f7f9fc;color:#172a3a;font:16px/1.6 system-ui,-apple-system,sans-serif;min-height:100vh;display:grid;place-items:center;padding:28px}main{width:100%;max-width:460px;background:#fff;border:1px solid #e3e9ed;border-radius:20px;padding:36px;box-shadow:0 16px 60px #172a3a09}.brand{font-weight:750;color:#087d70;letter-spacing:.03em}h1{font-size:28px;line-height:1.3;margin:24px 0 12px}p{color:#526474;margin:0 0 24px}label{display:block;font-size:14px;font-weight:600;margin:18px 0 7px}input{font:inherit;width:100%;padding:11px 12px;border:1px solid #cbd6dd;border-radius:9px}input:focus{outline:3px solid #c7eee6;border-color:#087d70}button{border:0;border-radius:9px;width:100%;padding:13px;background:#087d70;color:white;font:600 16px system-ui;cursor:pointer;margin-top:24px}button:focus-visible{outline:3px solid #91d9cc;outline-offset:3px}.error{color:#9b2828;background:#fff0f0;border-radius:8px;padding:10px 12px;margin:18px 0 0}.note{font-size:12px;margin:20px 0 0;color:#768593}@media(max-width:480px){main{padding:24px}h1{font-size:25px}}
|
||||
</style></head><body><main><div class="brand">✦ WishForge</div><h1>让愿望成为现实</h1><p>登录本机工作空间,提交愿望并查看实现与交付进度。</p>${message ? `<p class="error" role="alert">${escapeHtml(message)}</p>` : ''}<form action="/login?returnTo=${escapeHtml(encodeURIComponent(returnTo))}" method="post"><label for="username">管理员账号</label><input id="username" name="username" autocomplete="username" required maxlength="64" autofocus><label for="password">访问密码</label><input id="password" name="password" type="password" autocomplete="current-password" required maxlength="256"><button type="submit">进入工作空间</button></form><p class="note">仅向获准的协作者提供访问密码。登录会话在 1 小时后失效。</p></main></body></html>`,
|
||||
{
|
||||
status,
|
||||
headers: {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
'Cache-Control': 'no-store',
|
||||
'Content-Security-Policy':
|
||||
"default-src 'none'; style-src 'unsafe-inline'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'",
|
||||
'Referrer-Policy': 'no-referrer',
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
if (localAccessEnabled()) {
|
||||
if (!localRequestAllowed(request)) return new Response('Forbidden', { status: 403 });
|
||||
try {
|
||||
localCredentials();
|
||||
return localLoginPage(request);
|
||||
} catch {
|
||||
return localLoginPage(request, '本机访问凭据尚未配置,请联系管理员。', 503);
|
||||
}
|
||||
}
|
||||
if (isDevelopmentAuth()) return NextResponse.redirect(new URL('/', publicOrigin(request)));
|
||||
if (!oidcConfigured())
|
||||
return new Response(
|
||||
@ -67,3 +111,72 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
if (!localAccessEnabled()) return new Response('Method not allowed', { status: 405 });
|
||||
if (!localRequestAllowed(request)) return new Response('Forbidden', { status: 403 });
|
||||
const retry = loginRateLimit();
|
||||
if (retry) {
|
||||
const response = localLoginPage(request, '登录尝试过多,请稍后重试。', 429);
|
||||
response.headers.set('Retry-After', String(retry));
|
||||
return response;
|
||||
}
|
||||
if (!request.headers.get('content-type')?.startsWith('application/x-www-form-urlencoded'))
|
||||
return new Response('Unsupported content type', { status: 415 });
|
||||
// Bound the streamed input, even if Content-Length is omitted or forged.
|
||||
const reader = request.body?.getReader();
|
||||
if (!reader) return new Response('Missing form', { status: 400 });
|
||||
let text = '';
|
||||
let size = 0;
|
||||
const decoder = new TextDecoder();
|
||||
while (true) {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) break;
|
||||
size += chunk.value.byteLength;
|
||||
if (size > 4096) {
|
||||
await reader.cancel();
|
||||
return new Response('Form too large', { status: 413 });
|
||||
}
|
||||
text += decoder.decode(chunk.value, { stream: true });
|
||||
}
|
||||
text += decoder.decode();
|
||||
const form = new URLSearchParams(text);
|
||||
const username = form.get('username') ?? '';
|
||||
const password = form.get('password') ?? '';
|
||||
// Concurrent requests may finish reading after another request exhausted the window.
|
||||
const currentRetry = loginRateLimit();
|
||||
if (currentRetry) {
|
||||
const response = localLoginPage(request, '登录尝试过多,请稍后重试。', 429);
|
||||
response.headers.set('Retry-After', String(currentRetry));
|
||||
return response;
|
||||
}
|
||||
try {
|
||||
if (username.length > 64 || password.length > 256 || !verifyLocalPassword(username, password)) {
|
||||
recordLoginFailure();
|
||||
return localLoginPage(request, '账号或访问密码不正确。', 401);
|
||||
}
|
||||
const expiresAt = Math.floor(Date.now() / 1000) + LOCAL_SESSION_SECONDS;
|
||||
const cookie = await seal(
|
||||
{
|
||||
authMode: 'local',
|
||||
credentialId: localCredentialId(),
|
||||
accessToken: '',
|
||||
expiresAt,
|
||||
sessionExpiresAt: expiresAt,
|
||||
sub: 'local-administrator',
|
||||
name: '本机管理员',
|
||||
},
|
||||
LOCAL_SESSION_SECONDS,
|
||||
);
|
||||
clearLoginFailures();
|
||||
const response = NextResponse.redirect(
|
||||
new URL(safeReturnTo(request.nextUrl.searchParams.get('returnTo')), publicOrigin(request)),
|
||||
303,
|
||||
);
|
||||
response.cookies.set(SESSION_COOKIE, cookie, { ...cookieOptions(request), maxAge: LOCAL_SESSION_SECONDS });
|
||||
response.headers.set('Cache-Control', 'no-store');
|
||||
return response;
|
||||
} catch {
|
||||
return localLoginPage(request, '本机访问凭据尚未配置,请联系管理员。', 503);
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import {
|
||||
import { RunTable } from '@/components/run-table';
|
||||
import { ProjectSettings } from './project-settings';
|
||||
import { LessonsPanel } from './lessons';
|
||||
import { browserId } from '@/lib/browser-id';
|
||||
export function ProjectsView({ create = false }: { create?: boolean }) {
|
||||
const { data, error, isLoading, mutate } = useResource<Project[]>('/projects');
|
||||
const [search, setSearch] = useState('');
|
||||
@ -400,7 +401,7 @@ function WorkspaceRow({
|
||||
return;
|
||||
}
|
||||
if (operationKey.current?.action !== action) {
|
||||
operationKey.current = { action, key: crypto.randomUUID() };
|
||||
operationKey.current = { action, key: browserId() };
|
||||
}
|
||||
setBusy(true);
|
||||
setError('');
|
||||
|
||||
@ -4,6 +4,7 @@ import { useState } from 'react';
|
||||
import { ArrowRight, ArrowUpRight, Box, Check, ChevronDown, ShieldCheck, X } from 'lucide-react';
|
||||
import { formatTime, messageOf, mutateApi, shortId, safeUrl, useResource } from '@/lib/api';
|
||||
import type { Environment, Release } from '@/lib/types';
|
||||
import { browserId } from '@/lib/browser-id';
|
||||
|
||||
type ReleaseReview = {
|
||||
release: Release;
|
||||
@ -162,7 +163,7 @@ function ApprovalDialog({ release, onClose, onSaved }: { release: Release; onClo
|
||||
const [confirmed, setConfirmed] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [key] = useState(() => crypto.randomUUID());
|
||||
const [key] = useState(browserId);
|
||||
async function decide(decision: 'approve' | 'reject') {
|
||||
setBusy(true);
|
||||
setError('');
|
||||
|
||||
@ -49,6 +49,7 @@ import {
|
||||
import { providers as providerIds } from '@wishforge/contracts/providers';
|
||||
import { providerNames, RunTable, stages } from '@/components/run-table';
|
||||
import { waitingGuide } from '@/lib/waiting';
|
||||
import { browserId } from '@/lib/browser-id';
|
||||
|
||||
export function RunsView() {
|
||||
const runs = useResource<Run[]>('/runs', 5000);
|
||||
@ -125,18 +126,19 @@ export function RunsView() {
|
||||
export function NewWishView() {
|
||||
const projects = useResource<Project[]>('/projects');
|
||||
const providers = useResource<ProviderInfo[]>('/providers');
|
||||
const session = useResource<{ mode: string }>('/session');
|
||||
const [projectId, setProjectId] = useState('');
|
||||
const [provider, setProvider] = useState<ProviderId>('codex');
|
||||
const [chosenProvider, setProvider] = useState<ProviderId>();
|
||||
const [goal, setGoal] = useState('');
|
||||
const [criteria, setCriteria] = useState('');
|
||||
// About twenty Codex model turns: each request first reserves one token per byte it sends.
|
||||
const [maxTokens, setMaxTokens] = useState(300000);
|
||||
// Keep an explicit edit across project/provider changes; otherwise derive the relevant default.
|
||||
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(() => crypto.randomUUID());
|
||||
const [idempotencyKey] = useState(browserId);
|
||||
const router = useRouter();
|
||||
useEffect(() => {
|
||||
const query = new URLSearchParams(window.location.search);
|
||||
@ -146,6 +148,18 @@ export function NewWishView() {
|
||||
if (branch) setSourceBranch(branch);
|
||||
}, []);
|
||||
const effectiveProject = projectId || projects.data?.[0]?.id || '';
|
||||
const project = projects.data?.find(item => item.id === effectiveProject);
|
||||
const localPoc =
|
||||
session.data?.mode === 'local' &&
|
||||
project?.targetBranch === 'poc' &&
|
||||
[
|
||||
'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
].includes(project.repositoryUrl ?? '');
|
||||
const provider =
|
||||
chosenProvider ??
|
||||
(localPoc && providers.data?.some(item => item.id === 'claude-code' && item.enabled) ? 'claude-code' : 'codex');
|
||||
const maxTokens = chosenMaxTokens ?? (localPoc && provider === 'claude-code' ? 1000000 : 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);
|
||||
@ -195,6 +209,7 @@ export function NewWishView() {
|
||||
onChange={event => {
|
||||
setProjectId(event.target.value);
|
||||
setSourceBranch('');
|
||||
setProvider(undefined);
|
||||
}}
|
||||
>
|
||||
<option value="" disabled>
|
||||
@ -272,7 +287,11 @@ export function NewWishView() {
|
||||
<div className="budget-fields">
|
||||
<Field
|
||||
label="Token 上限"
|
||||
hint="每次模型请求会先按请求大小预留 token(Codex 一次约 5 万),预算不够预留时运行会停下。"
|
||||
hint={
|
||||
localPoc && provider === 'claude-code'
|
||||
? 'Claude CLI 计量包含输入、输出和缓存读取(cache read)。本项目默认 100 万 Token,仍受租户剩余额度限制。'
|
||||
: '每次模型请求会先按请求大小预留 token(Codex 一次约 5 万),预算不够预留时运行会停下。'
|
||||
}
|
||||
>
|
||||
<input
|
||||
type="number"
|
||||
@ -349,6 +368,7 @@ export function NewWishView() {
|
||||
}
|
||||
export function RunDetailView({ id }: { id: string }) {
|
||||
const runResource = useResource<Run>(`/runs/${id}`, 5000);
|
||||
const identity = useResource<{ kind: string; role: string }>('/identity');
|
||||
const events = useResource<RunEvent[]>(`/runs/${id}/events`, 5000);
|
||||
const artifacts = useResource<RunArtifacts>(`/runs/${id}/artifacts`, 10000);
|
||||
const [tab, setTab] = useState('overview');
|
||||
@ -391,6 +411,13 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
const errorText = typeof run.error === 'string' ? run.error : run.error?.message;
|
||||
const guide = waitingGuide(run.waitingReason);
|
||||
const waitingLink = guide.link?.(run);
|
||||
const budgetExhausted =
|
||||
(run.usedTokens ?? 0) >= (run.budget.maxTokens ?? 0) ||
|
||||
(run.activeMilliseconds ?? 0) >= (run.budget.maxMinutes ?? 0) * 60_000;
|
||||
const canUpdateBudget =
|
||||
identity.data?.kind === 'human' &&
|
||||
['owner', 'admin'].includes(identity.data.role) &&
|
||||
['waiting', 'paused'].includes(run.status);
|
||||
return (
|
||||
<>
|
||||
<PageHeader
|
||||
@ -400,7 +427,7 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
action={
|
||||
<div className="button-group">
|
||||
{run.status === 'paused' || (run.status === 'waiting' && guide.resumable) ? (
|
||||
<Button variant="secondary" busy={busy} onClick={() => void command('resume')}>
|
||||
<Button variant="secondary" busy={busy} disabled={budgetExhausted} onClick={() => void command('resume')}>
|
||||
<Play size={16} />
|
||||
继续
|
||||
</Button>
|
||||
@ -474,6 +501,15 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
</details>
|
||||
</Panel>
|
||||
) : null}
|
||||
{canUpdateBudget ? (
|
||||
<RunBudgetEditor
|
||||
key={`${run.id}:${run.budget.maxTokens}:${run.budget.maxMinutes}:${run.budget.maxRepairRounds}`}
|
||||
run={run}
|
||||
onSaved={async () => {
|
||||
await Promise.all([runResource.mutate(), events.mutate()]);
|
||||
}}
|
||||
/>
|
||||
) : null}
|
||||
<RunDeliveryPanel runId={id} />
|
||||
<div className="tabs" role="tablist" aria-label="运行详情">
|
||||
{[
|
||||
@ -544,7 +580,9 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
</KeyValue>
|
||||
) : null}
|
||||
<KeyValue label="Token 上限">{run.budget.maxTokens?.toLocaleString() ?? '—'}</KeyValue>
|
||||
<KeyValue label="已用 Token">{run.usedTokens?.toLocaleString() ?? '—'}</KeyValue>
|
||||
<KeyValue label="时间上限">{run.budget.maxMinutes ?? '—'} 分钟</KeyValue>
|
||||
<KeyValue label="已执行时间">{Math.ceil((run.activeMilliseconds ?? 0) / 1000)} 秒</KeyValue>
|
||||
</dl>
|
||||
</Panel>
|
||||
</div>
|
||||
@ -625,6 +663,83 @@ export function RunDetailView({ id }: { id: string }) {
|
||||
</>
|
||||
);
|
||||
}
|
||||
function RunBudgetEditor({ run, onSaved }: { run: Run; onSaved: () => Promise<void> }) {
|
||||
const [draft, setDraft] = useState({
|
||||
maxTokens: run.budget.maxTokens ?? 100000,
|
||||
maxMinutes: run.budget.maxMinutes ?? 60,
|
||||
maxRepairRounds: run.budget.maxRepairRounds ?? 3,
|
||||
});
|
||||
const [key, setKey] = useState(browserId);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
function change(field: keyof typeof draft, value: string) {
|
||||
setDraft(previous => ({ ...previous, [field]: Number(value) }));
|
||||
setKey(browserId());
|
||||
}
|
||||
async function save(event: React.FormEvent) {
|
||||
event.preventDefault();
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
await mutateApi(`/runs/${run.id}/budget`, draft, 'PATCH', key);
|
||||
await onSaved();
|
||||
} catch (error) {
|
||||
setError(messageOf(error));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<Panel title="运行预算">
|
||||
<p className="panel-intro">
|
||||
已使用 {(run.usedTokens ?? 0).toLocaleString()} Token,已执行 {Math.ceil((run.activeMilliseconds ?? 0) / 1000)}{' '}
|
||||
秒。调整不会清除用量,也不会自动继续运行。
|
||||
</p>
|
||||
<details className="requirement-amendment">
|
||||
<summary>调整预算</summary>
|
||||
<form onSubmit={event => void save(event)} className="form-stack">
|
||||
<div className="budget-fields">
|
||||
<Field label="新的 Token 上限">
|
||||
<input
|
||||
type="number"
|
||||
required
|
||||
min={Math.max(1000, run.usedTokens ?? 0)}
|
||||
max={2000000}
|
||||
step={1}
|
||||
value={draft.maxTokens}
|
||||
onChange={event => change('maxTokens', event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="新的时间上限(分钟)">
|
||||
<input
|
||||
type="number"
|
||||
required
|
||||
min={Math.max(1, Math.ceil((run.activeMilliseconds ?? 0) / 60000))}
|
||||
max={180}
|
||||
step={1}
|
||||
value={draft.maxMinutes}
|
||||
onChange={event => change('maxMinutes', event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="新的修复轮次上限">
|
||||
<input
|
||||
type="number"
|
||||
required
|
||||
min={run.repairCount}
|
||||
max={3}
|
||||
step={1}
|
||||
value={draft.maxRepairRounds}
|
||||
onChange={event => change('maxRepairRounds', event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
<FormError message={error} />
|
||||
<Button busy={busy}>保存预算</Button>
|
||||
</form>
|
||||
</details>
|
||||
</Panel>
|
||||
);
|
||||
}
|
||||
function RunDeliveryPanel({ runId }: { runId: string }) {
|
||||
const resource = useResource<RunDelivery>(`/runs/${runId}/delivery`, 10000);
|
||||
const delivery = resource.data?.run.id === runId ? resource.data : undefined;
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
'use client';
|
||||
import useSWR from 'swr';
|
||||
import { browserId } from './browser-id';
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
@ -41,7 +42,7 @@ export function useResource<T>(path: string | null, poll = 0) {
|
||||
keepPreviousData: true,
|
||||
});
|
||||
}
|
||||
export function mutateApi<T>(path: string, body: unknown, method = 'POST', idempotencyKey = crypto.randomUUID()) {
|
||||
export function mutateApi<T>(path: string, body: unknown, method = 'POST', idempotencyKey = browserId()) {
|
||||
return api<T>(path, {
|
||||
method,
|
||||
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
||||
|
||||
@ -16,6 +16,7 @@ import {
|
||||
beforeEach(() => {
|
||||
vi.stubEnv('WF_SESSION_SECRET', 'test-session-key-must-be-at-least-32-characters');
|
||||
vi.stubEnv('WF_DEV_AUTH_TOKEN', '');
|
||||
vi.stubEnv('WF_LOCAL_ACCESS', 'false');
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
});
|
||||
afterEach(() => {
|
||||
|
||||
@ -2,6 +2,13 @@ import 'server-only';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { createRemoteJWKSet, EncryptJWT, jwtDecrypt, jwtVerify } from 'jose';
|
||||
import type { NextRequest, NextResponse } from 'next/server';
|
||||
import {
|
||||
localAccessEnabled,
|
||||
localCredentialId,
|
||||
localCredentials,
|
||||
localRequestAllowed,
|
||||
localRequestOrigin,
|
||||
} from './local-access';
|
||||
|
||||
export const SESSION_COOKIE = 'wf_session';
|
||||
export const OAUTH_COOKIE = 'wf_oauth';
|
||||
@ -14,6 +21,8 @@ type Session = {
|
||||
sessionExpiresAt?: number;
|
||||
name?: string;
|
||||
sub: string;
|
||||
authMode?: 'local';
|
||||
credentialId?: string;
|
||||
};
|
||||
/** A signed-in browser stays signed in this long while it keeps being used (requirements FR-A5). */
|
||||
const SESSION_SECONDS = 12 * 60 * 60;
|
||||
@ -31,21 +40,22 @@ export function isDevelopment() {
|
||||
return process.env.WF_DEPLOYMENT_MODE === 'development' && process.env.NODE_ENV !== 'production';
|
||||
}
|
||||
export function isDevelopmentAuth() {
|
||||
return isDevelopment() && Boolean(process.env.WF_DEV_AUTH_TOKEN);
|
||||
return !localAccessEnabled() && isDevelopment() && Boolean(process.env.WF_DEV_AUTH_TOKEN);
|
||||
}
|
||||
export function publicOrigin(request: NextRequest) {
|
||||
if (localAccessEnabled()) return localRequestOrigin(request);
|
||||
return process.env.WF_PUBLIC_URL ? new URL(process.env.WF_PUBLIC_URL).origin : request.nextUrl.origin;
|
||||
}
|
||||
export function cookieOptions(request: NextRequest) {
|
||||
return {
|
||||
httpOnly: true,
|
||||
secure: new URL(publicOrigin(request)).protocol === 'https:',
|
||||
sameSite: 'lax' as const,
|
||||
sameSite: localAccessEnabled() ? ('strict' as const) : ('lax' as const),
|
||||
path: '/',
|
||||
};
|
||||
}
|
||||
function sessionKey() {
|
||||
const secret = process.env.WF_SESSION_SECRET;
|
||||
const secret = localAccessEnabled() ? localCredentials().sessionSecret : process.env.WF_SESSION_SECRET;
|
||||
if (!secret || secret.length < 32) throw new Error('WF_SESSION_SECRET must contain at least 32 characters.');
|
||||
return createHash('sha256').update(secret).digest();
|
||||
}
|
||||
@ -68,6 +78,9 @@ export async function unseal<T>(value?: string): Promise<T | null> {
|
||||
export async function getSession(request: NextRequest) {
|
||||
const session = await unseal<Session>(request.cookies.get(SESSION_COOKIE)?.value);
|
||||
if (!session) return null;
|
||||
if (localAccessEnabled() && (session.authMode !== 'local' || session.credentialId !== localCredentialId()))
|
||||
return null;
|
||||
if (!localAccessEnabled() && session.authMode === 'local') return null;
|
||||
const now = nowSeconds();
|
||||
const live = session.expiresAt > now;
|
||||
const renewable = !!session.refreshToken && (session.sessionExpiresAt ?? 0) > now;
|
||||
@ -113,6 +126,10 @@ export async function authorizationFor(
|
||||
if (isDevelopmentAuth()) return { token: process.env.WF_DEV_AUTH_TOKEN };
|
||||
const session = await getSession(request);
|
||||
if (!session) return {};
|
||||
if (localAccessEnabled())
|
||||
return localRequestAllowed(request)
|
||||
? { token: process.env.WF_LOCAL_API_TOKEN || process.env.WF_DEV_AUTH_TOKEN }
|
||||
: {};
|
||||
const now = nowSeconds();
|
||||
if (session.expiresAt - REFRESH_MARGIN_SECONDS > now || !session.refreshToken)
|
||||
return session.expiresAt > now ? { token: session.accessToken } : {};
|
||||
|
||||
11
apps/web/src/lib/browser-id.test.ts
Normal file
11
apps/web/src/lib/browser-id.test.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { afterEach, expect, it, vi } from 'vitest';
|
||||
import { browserId } from './browser-id';
|
||||
|
||||
afterEach(() => vi.unstubAllGlobals());
|
||||
it('creates valid independent UUIDs on HTTP LAN browsers without crypto.randomUUID', () => {
|
||||
const getRandomValues = crypto.getRandomValues.bind(crypto);
|
||||
vi.stubGlobal('crypto', { getRandomValues });
|
||||
const first = browserId();
|
||||
expect(first).toMatch(/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/);
|
||||
expect(browserId()).not.toBe(first);
|
||||
});
|
||||
9
apps/web/src/lib/browser-id.ts
Normal file
9
apps/web/src/lib/browser-id.ts
Normal file
@ -0,0 +1,9 @@
|
||||
/** randomUUID requires HTTPS; getRandomValues remains available on the trusted LAN HTTP entry. */
|
||||
export function browserId(): string {
|
||||
if (typeof crypto.randomUUID === 'function') return crypto.randomUUID();
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
||||
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
||||
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
||||
const hex = [...bytes].map(byte => byte.toString(16).padStart(2, '0')).join('');
|
||||
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
||||
}
|
||||
142
apps/web/src/lib/local-access.test.ts
Normal file
142
apps/web/src/lib/local-access.test.ts
Normal file
@ -0,0 +1,142 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mkdtempSync, writeFileSync, rmSync, chmodSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { scryptSync } from 'node:crypto';
|
||||
import { NextRequest } from 'next/server';
|
||||
|
||||
vi.mock('server-only', () => ({}));
|
||||
import { authorizationFor, getSession, isDevelopmentAuth, seal, SESSION_COOKIE } from './auth';
|
||||
import {
|
||||
clearLoginFailures,
|
||||
localCredentialId,
|
||||
localCredentials,
|
||||
localRequestAllowed,
|
||||
loginRateLimit,
|
||||
recordLoginFailure,
|
||||
verifyLocalPassword,
|
||||
} from './local-access';
|
||||
import { proxy } from '../proxy';
|
||||
|
||||
let directory: string;
|
||||
let credentialsFile: string;
|
||||
const salt = 'ab'.repeat(32);
|
||||
const credentials = {
|
||||
username: 'admin',
|
||||
salt,
|
||||
passwordHash: scryptSync('test-local-password', salt, 64).toString('hex'),
|
||||
sessionSecret: 'test-only-local-secret-with-more-than-forty-three-characters',
|
||||
};
|
||||
beforeEach(() => {
|
||||
directory = mkdtempSync(join(tmpdir(), 'wishforge-web-auth-'));
|
||||
credentialsFile = join(directory, 'access.json');
|
||||
writeFileSync(credentialsFile, JSON.stringify(credentials), { mode: 0o600 });
|
||||
vi.stubEnv('WF_LOCAL_ACCESS', 'true');
|
||||
vi.stubEnv('WF_LOCAL_ACCESS_FILE', credentialsFile);
|
||||
vi.stubEnv('WF_LOCAL_ALLOWED_ORIGINS', 'http://127.0.0.1:3000,http://192.168.1.20:3000');
|
||||
vi.stubEnv('WF_DEPLOYMENT_MODE', 'development');
|
||||
vi.stubEnv('WF_DEV_AUTH_TOKEN', 'private-control-api-token');
|
||||
vi.stubEnv('WF_LOCAL_API_TOKEN', '');
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
clearLoginFailures();
|
||||
});
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
clearLoginFailures();
|
||||
rmSync(directory, { force: true, recursive: true });
|
||||
});
|
||||
const request = (path = '/', init?: ConstructorParameters<typeof NextRequest>[1]) => {
|
||||
const headers = new Headers(init?.headers);
|
||||
if (!headers.has('host')) headers.set('host', '127.0.0.1:3000');
|
||||
return new NextRequest(`http://127.0.0.1:3000${path}`, { ...init, headers });
|
||||
};
|
||||
async function authenticated(path = '/', overrides: Record<string, unknown> = {}) {
|
||||
const req = request(path);
|
||||
req.cookies.set(
|
||||
SESSION_COOKIE,
|
||||
await seal(
|
||||
{
|
||||
authMode: 'local',
|
||||
credentialId: localCredentialId(),
|
||||
sub: 'local-administrator',
|
||||
accessToken: '',
|
||||
expiresAt: Math.floor(Date.now() / 1000) + 60,
|
||||
...overrides,
|
||||
},
|
||||
60,
|
||||
),
|
||||
);
|
||||
return req;
|
||||
}
|
||||
|
||||
describe('local operator browser boundary', () => {
|
||||
it('requires a private credential file and compares administrator passwords', () => {
|
||||
expect(verifyLocalPassword('admin', 'test-local-password')).toBe(true);
|
||||
expect(verifyLocalPassword('unknown', 'test-local-password')).toBe(false);
|
||||
expect(verifyLocalPassword('admin', 'wrong')).toBe(false);
|
||||
chmodSync(credentialsFile, 0o644);
|
||||
expect(() => localCredentials()).toThrow('mode 0600');
|
||||
});
|
||||
it('never exposes the development API token without a local session', async () => {
|
||||
expect(isDevelopmentAuth()).toBe(false);
|
||||
expect(await authorizationFor(request('/api/projects'))).toEqual({});
|
||||
expect(await authorizationFor(await authenticated('/api/projects'))).toEqual({
|
||||
token: 'private-control-api-token',
|
||||
});
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
expect(isDevelopmentAuth()).toBe(false);
|
||||
expect(await authorizationFor(request('/api/projects'))).toEqual({});
|
||||
expect(await authorizationFor(await authenticated('/api/projects'))).toEqual({
|
||||
token: 'private-control-api-token',
|
||||
});
|
||||
});
|
||||
it('rejects OIDC, expired, and credential-rotated sessions in local mode', async () => {
|
||||
expect(await getSession(await authenticated('/', { authMode: undefined }))).toBeNull();
|
||||
expect(await getSession(await authenticated('/', { expiresAt: 1 }))).toBeNull();
|
||||
expect(await getSession(await authenticated('/', { credentialId: 'old-credentials' }))).toBeNull();
|
||||
const localSession = await authenticated();
|
||||
vi.stubEnv('WF_LOCAL_ACCESS', 'false');
|
||||
expect(await getSession(localSession)).toBeNull();
|
||||
});
|
||||
it('requires an exact allowed Host and Origin, including direct local API calls', async () => {
|
||||
expect(localRequestAllowed(request())).toBe(true);
|
||||
expect(localRequestAllowed(request('/', { headers: { host: 'evil.example:3000' } }))).toBe(false);
|
||||
expect(localRequestAllowed(request('/', { headers: { host: 'invalid/host' } }))).toBe(false);
|
||||
expect(localRequestAllowed(request('/', { headers: { origin: 'http://evil.example' } }))).toBe(false);
|
||||
expect(localRequestAllowed(request('/', { headers: { 'sec-fetch-site': 'cross-site' } }))).toBe(false);
|
||||
expect(localRequestAllowed(request('/api/runs', { method: 'POST' }))).toBe(false);
|
||||
expect(
|
||||
localRequestAllowed(request('/api/runs', { method: 'POST', headers: { origin: 'http://127.0.0.1:3000' } })),
|
||||
).toBe(true);
|
||||
const invalid = await authenticated('/api/runs');
|
||||
invalid.headers.set('host', 'evil.example');
|
||||
expect(await authorizationFor(invalid)).toEqual({});
|
||||
});
|
||||
it('fails closed without the configured origins', () => {
|
||||
vi.stubEnv('WF_LOCAL_ALLOWED_ORIGINS', '');
|
||||
expect(localRequestAllowed(request())).toBe(false);
|
||||
});
|
||||
it('protects pages, API reads, streams, IDE and callback routes while leaving login and bundles accessible', async () => {
|
||||
for (const path of ['/', '/runs/new', '/ide/id/launch', '/login/callback']) {
|
||||
const result = await proxy(request(path));
|
||||
expect(result.status).toBe(307);
|
||||
expect(new URL(result.headers.get('location')!).pathname).toBe('/login');
|
||||
}
|
||||
for (const path of ['/api/session', '/api/projects', '/api/runs/id/stream', '/api/releases/id/download'])
|
||||
expect((await proxy(request(path))).status).toBe(401);
|
||||
for (const path of ['/login', '/_next/static/chunks/app.js', '/icon.svg'])
|
||||
expect((await proxy(request(path))).headers.get('x-middleware-next')).toBe('1');
|
||||
expect((await proxy(await authenticated('/runs/new'))).headers.get('x-middleware-next')).toBe('1');
|
||||
});
|
||||
it('leaves existing OIDC routing unchanged when local access is disabled', async () => {
|
||||
vi.stubEnv('WF_LOCAL_ACCESS', 'false');
|
||||
expect((await proxy(request('/projects'))).headers.get('x-middleware-next')).toBe('1');
|
||||
});
|
||||
it('caps failed guesses globally without trusting forwarded IP headers', () => {
|
||||
for (let i = 0; i < 8; i++) recordLoginFailure(1000);
|
||||
expect(loginRateLimit(2000)).toBe(899);
|
||||
expect(loginRateLimit(901001)).toBe(0);
|
||||
clearLoginFailures();
|
||||
expect(loginRateLimit(2000)).toBe(0);
|
||||
});
|
||||
});
|
||||
98
apps/web/src/lib/local-access.ts
Normal file
98
apps/web/src/lib/local-access.ts
Normal file
@ -0,0 +1,98 @@
|
||||
import 'server-only';
|
||||
import { createHash, scryptSync, timingSafeEqual } from 'node:crypto';
|
||||
import { readFileSync, statSync } from 'node:fs';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
export const LOCAL_SESSION_SECONDS = 60 * 60;
|
||||
export const localAccessEnabled = () => process.env.WF_LOCAL_ACCESS === 'true';
|
||||
|
||||
type LocalCredentials = {
|
||||
username: string;
|
||||
passwordHash: string;
|
||||
salt: string;
|
||||
sessionSecret: string;
|
||||
};
|
||||
|
||||
/** Only the operator-owned 0600 file may provide the local administrator identity. */
|
||||
export function localCredentials(): LocalCredentials {
|
||||
const file = process.env.WF_LOCAL_ACCESS_FILE;
|
||||
if (!file || !file.startsWith('/')) throw new Error('An absolute WF_LOCAL_ACCESS_FILE is required.');
|
||||
const stat = statSync(file);
|
||||
if (!stat.isFile() || (stat.mode & 0o077) !== 0 || (process.getuid && stat.uid !== process.getuid()))
|
||||
throw new Error('The local access credential file must be owned by this user and have mode 0600.');
|
||||
const data = JSON.parse(readFileSync(file, 'utf8')) as LocalCredentials;
|
||||
if (
|
||||
typeof data.username !== 'string' ||
|
||||
!/^[a-zA-Z0-9_-]{1,64}$/.test(data.username) ||
|
||||
!/^[a-f0-9]{128}$/.test(data.passwordHash) ||
|
||||
!/^[a-f0-9]{64}$/.test(data.salt) ||
|
||||
typeof data.sessionSecret !== 'string' ||
|
||||
data.sessionSecret.length < 43
|
||||
)
|
||||
throw new Error('Invalid local access credential file.');
|
||||
return data;
|
||||
}
|
||||
|
||||
export function localCredentialId() {
|
||||
return createHash('sha256').update(localCredentials().passwordHash).digest('hex');
|
||||
}
|
||||
|
||||
export function verifyLocalPassword(username: string, password: string) {
|
||||
const credentials = localCredentials();
|
||||
const actual = scryptSync(password, credentials.salt, 64);
|
||||
const expected = Buffer.from(credentials.passwordHash, 'hex');
|
||||
// Always derive and compare the password, including unknown usernames.
|
||||
return timingSafeEqual(actual, expected) && username === credentials.username;
|
||||
}
|
||||
|
||||
/** Explicit origins prevent Host-header / DNS-rebinding access to the privileged BFF. */
|
||||
export function localAllowedOrigins() {
|
||||
const origins = (process.env.WF_LOCAL_ALLOWED_ORIGINS ?? '')
|
||||
.split(',')
|
||||
.map(value => value.trim())
|
||||
.filter(Boolean);
|
||||
if (!origins.length) throw new Error('WF_LOCAL_ALLOWED_ORIGINS must contain the browser entry origins.');
|
||||
return origins.map(value => {
|
||||
const url = new URL(value);
|
||||
if (!['http:', 'https:'].includes(url.protocol) || url.origin !== value || url.username || url.password)
|
||||
throw new Error('Local access origins must be exact HTTP(S) origins.');
|
||||
return url.origin;
|
||||
});
|
||||
}
|
||||
|
||||
export function localRequestOrigin(request: NextRequest) {
|
||||
const url = new URL(request.url);
|
||||
const host = request.headers.get('host');
|
||||
// Do not normalize a malformed Host into the request URL's trusted fallback.
|
||||
return host ? `${url.protocol}//${host}` : url.origin;
|
||||
}
|
||||
|
||||
export function localRequestAllowed(request: NextRequest) {
|
||||
try {
|
||||
const origin = localRequestOrigin(request);
|
||||
if (!localAllowedOrigins().includes(origin)) return false;
|
||||
const suppliedOrigin = request.headers.get('origin');
|
||||
if (suppliedOrigin && suppliedOrigin !== origin) return false;
|
||||
if (request.headers.get('sec-fetch-site') === 'cross-site') return false;
|
||||
// Cookie-authenticated writes must carry the browser's exact Origin.
|
||||
return ['GET', 'HEAD', 'OPTIONS'].includes(request.method) || suppliedOrigin === origin;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// One local operator identity: limit globally rather than trusting spoofable forwarded IPs.
|
||||
// The state lives across development hot reloads; an operator restart clears the window.
|
||||
const state = globalThis as typeof globalThis & { wfLocalLoginAttempts?: { failures: number; until: number } };
|
||||
export function loginRateLimit(now = Date.now()) {
|
||||
const attempts = state.wfLocalLoginAttempts;
|
||||
return attempts && attempts.until > now && attempts.failures >= 8 ? Math.ceil((attempts.until - now) / 1000) : 0;
|
||||
}
|
||||
export function recordLoginFailure(now = Date.now()) {
|
||||
if (!state.wfLocalLoginAttempts || state.wfLocalLoginAttempts.until <= now)
|
||||
state.wfLocalLoginAttempts = { failures: 0, until: now + 15 * 60 * 1000 };
|
||||
state.wfLocalLoginAttempts.failures++;
|
||||
}
|
||||
export function clearLoginFailures() {
|
||||
delete state.wfLocalLoginAttempts;
|
||||
}
|
||||
@ -42,6 +42,8 @@ export type Run = {
|
||||
provider: ProviderId;
|
||||
repairCount: number;
|
||||
budget: { maxTokens?: number; maxMinutes?: number; maxRepairRounds?: number };
|
||||
usedTokens?: number;
|
||||
activeMilliseconds?: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
waitingReason?: string;
|
||||
|
||||
@ -38,9 +38,8 @@ export const waitingGuides: Record<WaitingReason, WaitingGuide> = {
|
||||
},
|
||||
budget: {
|
||||
title: '预算已用完',
|
||||
action: '这个运行的 token 或执行时长预算已用完。请提交一个新的愿望,并设置合适的预算。',
|
||||
link: run => ({ href: `/runs/new?project=${run.projectId}`, label: '提交新愿望' }),
|
||||
resumable: false,
|
||||
action: '所有者或管理员可以调整本次运行的预算,再点击「继续」。已记录的用量和执行时间会保留。',
|
||||
resumable: true,
|
||||
},
|
||||
clarification: {
|
||||
title: '执行器需要你的补充',
|
||||
|
||||
28
apps/web/src/proxy.ts
Normal file
28
apps/web/src/proxy.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getSession } from './lib/auth';
|
||||
import { localAccessEnabled, localRequestAllowed, localRequestOrigin } from './lib/local-access';
|
||||
|
||||
/** Local access is opt-in. Production OIDC continues to use its existing route/session flow. */
|
||||
export async function proxy(request: NextRequest) {
|
||||
if (!localAccessEnabled()) return NextResponse.next();
|
||||
if (!localRequestAllowed(request))
|
||||
return NextResponse.json({ error: { code: 'ORIGIN_DENIED', message: '请求来源不被允许。' } }, { status: 403 });
|
||||
const path = request.nextUrl.pathname;
|
||||
if (path === '/login' || path.startsWith('/_next/static/') || path === '/icon.svg') return NextResponse.next();
|
||||
const session = await getSession(request);
|
||||
if (session) {
|
||||
const response = NextResponse.next();
|
||||
response.headers.set('Cache-Control', 'no-store');
|
||||
return response;
|
||||
}
|
||||
if (path.startsWith('/api/'))
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'UNAUTHENTICATED', message: '请先登录本机工作空间。' } },
|
||||
{ status: 401, headers: { 'Cache-Control': 'no-store' } },
|
||||
);
|
||||
const login = new URL('/login', localRequestOrigin(request));
|
||||
login.searchParams.set('returnTo', `${path}${request.nextUrl.search}`);
|
||||
return NextResponse.redirect(login);
|
||||
}
|
||||
|
||||
export const config = { matcher: ['/:path*'] };
|
||||
@ -3,6 +3,8 @@ import type { Project, ProviderId, Run, VerificationProfile, WaitingReason } fro
|
||||
import type { AgentBundleConfig } from '@wishforge/agent';
|
||||
import {
|
||||
GitWorkspace,
|
||||
readLocalPocBinding,
|
||||
localPocForProject,
|
||||
RuntimeError,
|
||||
safeIdentifier,
|
||||
type CommandExecutor,
|
||||
@ -65,6 +67,7 @@ export interface ActivityOutcome {
|
||||
/** `host` runs commands as the current user (development only); `kata` is the per-run production sandbox. */
|
||||
export type SandboxMode = 'host' | 'kata';
|
||||
export interface WorkerConfig {
|
||||
localPocBindingFile?: string;
|
||||
controlPlaneUrl: string;
|
||||
workerToken?: string;
|
||||
verifierToken?: string;
|
||||
@ -80,6 +83,8 @@ export interface WorkerConfig {
|
||||
verifierBinary?: string;
|
||||
/** Executables of the CLI executors (see providers.ts). */
|
||||
providerBinaries?: Partial<Record<ProviderId, string>>;
|
||||
/** Trusted local operator's Claude login; only supplied to development host executors. */
|
||||
claudeAuthHome?: string;
|
||||
dockerConfig?: string;
|
||||
/** Docker CLI context of the image builder; operator configuration, never project settings. */
|
||||
dockerContext?: string;
|
||||
@ -126,7 +131,15 @@ export function tenantRoot(config: WorkerConfig, input: RunInput) {
|
||||
return path.join(config.workspaceRoot, safeIdentifier(input.tenantId));
|
||||
}
|
||||
export function gitFor(config: WorkerConfig, context: RunContext, execute: CommandExecutor) {
|
||||
const poc = localPocForProject(readLocalPocBinding(config.localPocBindingFile), context.project);
|
||||
return new GitWorkspace(execute, {
|
||||
localSshTransport: poc
|
||||
? {
|
||||
repositoryUrl: poc.repositoryUrl,
|
||||
sshUrl: 'ssh://git@gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
home: poc.gitHome,
|
||||
}
|
||||
: undefined,
|
||||
allowLocalHttp: config.allowLocalGitHttp,
|
||||
credential:
|
||||
context.gitCredential && context.project.repositoryUrl
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import path from 'node:path';
|
||||
import { rm } from 'node:fs/promises';
|
||||
import { rm, writeFile } from 'node:fs/promises';
|
||||
import type { Run } from '@wishforge/contracts';
|
||||
import { BASELINE_BUNDLE, BASELINE_VERSION, bundleDigest, bundlePrompt, questionsIn } from '@wishforge/agent';
|
||||
import {
|
||||
@ -15,6 +15,7 @@ import {
|
||||
type CommandExecutor,
|
||||
type CommandResult,
|
||||
type ProviderAdapter,
|
||||
type PortableCheckpoint,
|
||||
} from '@wishforge/runtime';
|
||||
import {
|
||||
client,
|
||||
@ -148,6 +149,9 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
};
|
||||
keeper.signal.addEventListener('abort', abort, { once: true });
|
||||
let workspace: string | undefined;
|
||||
let checkpointPersisted = false;
|
||||
let recoveryBaseCommit: string | undefined;
|
||||
let recoveryCheckpoint: PortableCheckpoint | undefined;
|
||||
const transition = (stage: Run['stage'], message: string, extras: Record<string, unknown> = {}) =>
|
||||
api.request<Run>(`/internal/runs/${input.runId}/transition`, {
|
||||
leaseToken,
|
||||
@ -156,6 +160,18 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
message,
|
||||
...extras,
|
||||
});
|
||||
const settleFinalUsage = async (measurements: Array<{ operationId: string; tokens: number }>) => {
|
||||
for (const measurement of measurements) {
|
||||
await api.request(
|
||||
`/internal/runs/${input.runId}/meter`,
|
||||
{ leaseToken, ...measurement, computeSeconds: 0 },
|
||||
{ retries: 2 },
|
||||
);
|
||||
// A final total may consume the remaining budget. Source is already durable; do not
|
||||
// continue to verification with the now-expired lease. Replays use the same operation ID.
|
||||
await keeper.ensure();
|
||||
}
|
||||
};
|
||||
try {
|
||||
const context = await api.request<RunContext>(`/internal/runs/${input.runId}/context`, undefined, { retries: 2 });
|
||||
let run = context.run;
|
||||
@ -166,6 +182,29 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
} catch (error) {
|
||||
return waiting('git_storage', error instanceof Error ? error.message : 'The project repository is not usable');
|
||||
}
|
||||
const previousCompletion = context.checkpoint?.providerCompletion;
|
||||
if (previousCompletion) {
|
||||
await settleFinalUsage(previousCompletion.finalUsage);
|
||||
if (
|
||||
previousCompletion.status === 'completed' &&
|
||||
previousCompletion.revision === run.revision &&
|
||||
previousCompletion.repairCount === run.repairCount &&
|
||||
run.stage === 'implement'
|
||||
) {
|
||||
const questions = run.repairCount ? [] : questionsIn(previousCompletion.summary);
|
||||
if (questions.length && context.checkpoint!.sourceCommit === context.checkpoint!.baseCommit)
|
||||
return waiting('clarification', `The executor needs answers before implementing:\n${questions.join('\n')}`);
|
||||
await transition(
|
||||
'verify',
|
||||
'Saved implementation and final usage reconciled; independent verification is next',
|
||||
{
|
||||
sourceCommit: context.checkpoint!.sourceCommit,
|
||||
...(previousCompletion.summary ? { summary: previousCompletion.summary } : {}),
|
||||
},
|
||||
);
|
||||
return { status: 'verify' };
|
||||
}
|
||||
}
|
||||
provider = createProvider(config, context, isolatedHome);
|
||||
if (!provider) return waiting('credentials', `The ${run.provider} executor is not available on this worker.`);
|
||||
const capabilities = provider.capabilities();
|
||||
@ -202,6 +241,7 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
});
|
||||
workspace = prepared.workspace;
|
||||
let baseCommit = prepared.baseCommit;
|
||||
recoveryBaseCommit = baseCommit;
|
||||
let conflicts: string[] = [];
|
||||
let lastFailure = run.repairCount ? run.lastFailure : undefined;
|
||||
// After a merge conflict the repair round starts by merging the current target branch;
|
||||
@ -209,6 +249,7 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
if (run.repairCount && run.lastFailure?.kind === 'integration_conflict') {
|
||||
const merged = await git.mergeTarget(workspace, context.project.targetBranch);
|
||||
baseCommit = merged.targetCommit;
|
||||
recoveryBaseCommit = baseCommit;
|
||||
conflicts = merged.conflicts;
|
||||
// The prompt names the conflicts of this merge; the target may have moved since integration.
|
||||
lastFailure = {
|
||||
@ -251,39 +292,67 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
runId: input.runId,
|
||||
tenantId: input.tenantId,
|
||||
workspace,
|
||||
prompt,
|
||||
prompt:
|
||||
config.claudeAuthHome && run.provider === 'claude-code'
|
||||
? `${prompt}\n\nLocal execution contract: only Read/Edit/Write/Glob/Grep file tools are available. Implement the requested source changes using those tools. A separate trusted verifier runs installation, tests, Git commit/push and deployment afterwards; do not ask for Bash, Git, deployment credentials or permission to run those commands. Do not claim tests were run by you. Preserve unrelated code and follow the repository versioning instructions.`
|
||||
: prompt,
|
||||
baseCommit,
|
||||
model: bundle.model,
|
||||
});
|
||||
providerStarted = true;
|
||||
if (keeper.error) abort();
|
||||
let summary: string | undefined;
|
||||
const finalUsage: Array<{ operationId: string; tokens: number }> = [];
|
||||
for await (const event of provider.events(input.runId)) {
|
||||
activity?.heartbeat({ runId: input.runId, sequence: event.sequence, leaseToken });
|
||||
if (event.type === 'output' && typeof event.data.text === 'string') summary = event.data.text;
|
||||
// Only normalized events leave the sandbox; reasoning and raw process output are not exported.
|
||||
await api.request(`/internal/runs/${input.runId}/events`, { leaseToken, event }, { retries: 2 });
|
||||
if (event.type === 'usage' && context.providerCredential?.metering !== 'proxy') {
|
||||
const tokens = Number(event.data.input_tokens ?? 0) + Number(event.data.output_tokens ?? 0);
|
||||
if (Number.isSafeInteger(tokens) && tokens >= 0)
|
||||
await api.request(
|
||||
`/internal/runs/${input.runId}/meter`,
|
||||
{
|
||||
leaseToken,
|
||||
operationId: `provider-${input.runId}-${leaseToken}-${event.sequence}`,
|
||||
tokens,
|
||||
computeSeconds: 0,
|
||||
},
|
||||
{ retries: 2 },
|
||||
);
|
||||
const tokens =
|
||||
Number(event.data.input_tokens ?? 0) +
|
||||
Number(event.data.output_tokens ?? 0) +
|
||||
Number(event.data.cache_creation_input_tokens ?? 0) +
|
||||
Number(event.data.cache_read_input_tokens ?? 0);
|
||||
if (Number.isSafeInteger(tokens) && tokens >= 0) {
|
||||
const measurement = { operationId: `provider-${input.runId}-${leaseToken}-${event.sequence}`, tokens };
|
||||
if (event.data.final === true) finalUsage.push(measurement);
|
||||
else
|
||||
await api.request(
|
||||
`/internal/runs/${input.runId}/meter`,
|
||||
{
|
||||
leaseToken,
|
||||
...measurement,
|
||||
computeSeconds: 0,
|
||||
},
|
||||
{ retries: 2 },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (keeper.error) throw keeper.error;
|
||||
const providerRun = await provider.inspect(input.runId);
|
||||
// The work so far is kept even when the executor failed, so continuing does not start over.
|
||||
const checkpoint = await provider.exportCheckpoint(input.runId);
|
||||
if (providerRun.status === 'running')
|
||||
throw new RuntimeError(
|
||||
'WORKSPACE_NOT_QUIESCENT',
|
||||
'The provider stream ended before the workspace stopped changing',
|
||||
);
|
||||
const checkpoint: PortableCheckpoint = {
|
||||
...(await provider.exportCheckpoint(input.runId)),
|
||||
providerCompletion: {
|
||||
status: providerRun.status,
|
||||
revision: run.revision,
|
||||
repairCount: run.repairCount,
|
||||
...(summary ? { summary: summary.slice(0, 4000) } : {}),
|
||||
finalUsage,
|
||||
},
|
||||
};
|
||||
recoveryCheckpoint = checkpoint;
|
||||
await keeper.ensure();
|
||||
await api.request(`/internal/runs/${input.runId}/checkpoint`, { leaseToken, checkpoint, workspace });
|
||||
checkpointPersisted = true;
|
||||
await settleFinalUsage(finalUsage);
|
||||
if (providerRun.status !== 'completed')
|
||||
return waiting(
|
||||
'provider_error',
|
||||
@ -304,7 +373,7 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
return { status: 'verify' };
|
||||
} catch (error) {
|
||||
abort();
|
||||
return outcomeForError(
|
||||
return await outcomeForError(
|
||||
keeper.error ?? error,
|
||||
async () => (await api.request<RunContext>(`/internal/runs/${input.runId}/context`)).run,
|
||||
);
|
||||
@ -312,6 +381,34 @@ export async function executeRun(config: WorkerConfig, input: RunInput): Promise
|
||||
keeper.close();
|
||||
keeper.signal.removeEventListener('abort', abort);
|
||||
await api.request(`/internal/runs/${input.runId}/lease/release`, { leaseToken }).catch(() => undefined);
|
||||
if (workspace) await rm(workspace, { recursive: true, force: true }).catch(() => undefined);
|
||||
if (workspace && checkpointPersisted) await rm(workspace, { recursive: true, force: true }).catch(() => undefined);
|
||||
else if (workspace) {
|
||||
// Lease loss, cancellation and ambiguous uploads must never erase the only copy.
|
||||
// Keep a private sidecar outside the agent tree so operators can recover this exact attempt.
|
||||
const providerRun = providerStarted ? await provider?.inspect(input.runId).catch(() => undefined) : undefined;
|
||||
await writeFile(
|
||||
`${workspace}.recovery.json`,
|
||||
JSON.stringify(
|
||||
{
|
||||
schemaVersion: 1,
|
||||
tenantId: input.tenantId,
|
||||
runId: input.runId,
|
||||
generation: input.generation,
|
||||
leaseToken,
|
||||
workspace,
|
||||
baseCommit: recoveryBaseCommit,
|
||||
provider: providerRun?.provider,
|
||||
providerStatus: providerRun?.status,
|
||||
sessionId: providerRun?.sessionId,
|
||||
...(recoveryCheckpoint ? { checkpoint: recoveryCheckpoint } : {}),
|
||||
retainedAt: new Date().toISOString(),
|
||||
reason: 'Checkpoint persistence was not confirmed; workspace retained',
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
{ mode: 0o600 },
|
||||
).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import { NativeConnection, Worker, bundleWorkflowCode } from '@temporalio/worker
|
||||
import { isProduction, requireDeploymentMode } from '@wishforge/contracts';
|
||||
import { createActivities, type SandboxMode } from './activities.js';
|
||||
import { temporalTls, workerNamespaces } from './temporal.js';
|
||||
import { providerBinaries } from './providers.js';
|
||||
import { claudeLocalAuthHome, providerBinaries } from './providers.js';
|
||||
|
||||
async function main() {
|
||||
requireDeploymentMode(process.env);
|
||||
@ -35,12 +35,14 @@ async function main() {
|
||||
sandboxMode = mode;
|
||||
}
|
||||
const namespaces = workerNamespaces(process.env, role);
|
||||
const claudeAuthHome = claudeLocalAuthHome(process.env);
|
||||
const connection = await NativeConnection.connect({
|
||||
address: process.env.TEMPORAL_ADDRESS ?? 'localhost:7233',
|
||||
tls: await temporalTls(process.env),
|
||||
});
|
||||
try {
|
||||
const activities = createActivities({
|
||||
localPocBindingFile: process.env.WF_LOCAL_POC_BINDING_FILE,
|
||||
controlPlaneUrl: process.env.WF_CONTROL_PLANE_URL ?? 'http://127.0.0.1:4000',
|
||||
workerToken: role === 'executor' || role === 'coordinator' ? token : undefined,
|
||||
verifierToken: role === 'verifier' ? token : undefined,
|
||||
@ -65,6 +67,7 @@ async function main() {
|
||||
}
|
||||
: undefined,
|
||||
providerBinaries: providerBinaries(process.env),
|
||||
claudeAuthHome,
|
||||
dockerConfig: process.env.WF_DOCKER_CONFIG,
|
||||
dockerContext: process.env.WF_DOCKER_CONTEXT,
|
||||
dockerHost: process.env.WF_DOCKER_HOST,
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import path from 'node:path';
|
||||
import { providerCatalog, providers, type ProviderId } from '@wishforge/contracts';
|
||||
import {
|
||||
ClaudeCodeAdapter,
|
||||
@ -51,10 +52,28 @@ export const providerRegistry: Record<ProviderId, Factory> = {
|
||||
binary: config.providerBinaries?.['claude-code'],
|
||||
isolatedHome,
|
||||
credentialEnv: cliCredential(context, 'ANTHROPIC_API_KEY', 'ANTHROPIC_BASE_URL'),
|
||||
...(config.claudeAuthHome && config.sandboxMode === 'host'
|
||||
? { localAuth: { home: config.claudeAuthHome, developmentHost: true as const } }
|
||||
: {}),
|
||||
isolationConfirmed: true,
|
||||
}),
|
||||
};
|
||||
|
||||
/** Reject native login configuration before connecting a worker to Temporal. */
|
||||
export function claudeLocalAuthHome(env: NodeJS.ProcessEnv): string | undefined {
|
||||
const home = env.WF_CLAUDE_LOCAL_AUTH_HOME;
|
||||
if (!home) return undefined;
|
||||
if (
|
||||
env.WF_DEPLOYMENT_MODE !== 'development' ||
|
||||
env.NODE_ENV === 'production' ||
|
||||
env.WF_SANDBOX_MODE !== 'host' ||
|
||||
env.WF_WORKER_ROLE !== 'executor' ||
|
||||
!path.isAbsolute(home)
|
||||
)
|
||||
throw new Error('WF_CLAUDE_LOCAL_AUTH_HOME requires an absolute HOME and a development host executor');
|
||||
return home;
|
||||
}
|
||||
|
||||
/** The executables of CLI executors, from the variables the catalog names. */
|
||||
export function providerBinaries(env: NodeJS.ProcessEnv): Partial<Record<ProviderId, string>> {
|
||||
return Object.fromEntries(
|
||||
|
||||
@ -3,6 +3,10 @@ import { mkdir, realpath, rm } from 'node:fs/promises';
|
||||
import type { GitHubDeliveryState, Run } from '@wishforge/contracts';
|
||||
import {
|
||||
buildDockerImage,
|
||||
buildPocRelease,
|
||||
loadPocProfile,
|
||||
readLocalPocBinding,
|
||||
localPocForProject,
|
||||
executeCommand,
|
||||
isolatedVerifierExecutor,
|
||||
MergeConflictError,
|
||||
@ -287,6 +291,46 @@ async function deliver(session: Session, integrated: Verified): Promise<Activity
|
||||
permissionsExpanded: changedPaths.some(file => /(^|\/)(security|auth|infra)(\/|$)/i.test(file)),
|
||||
});
|
||||
await session.keeper.ensure();
|
||||
const localPoc = localPocForProject(readLocalPocBinding(session.config.localPocBindingFile), session.context.project);
|
||||
if (localPoc) {
|
||||
if (session.config.sandboxMode !== 'host' || delivery.environmentId !== localPoc.environmentId)
|
||||
throw new RuntimeError('LOCAL_POC_POLICY', 'POC delivery requires its bound local execution and environment');
|
||||
const profile = await loadPocProfile(localPoc.profileFile);
|
||||
// Shared server/internal changes rebuild the coherent set. Web-only changes retain the two existing consumers.
|
||||
const apiOnly = changedPaths.every(file =>
|
||||
/^(src\/|tests\/frontend\/|tools\/drosync\/Cargo\.(toml|lock)$)/.test(file),
|
||||
);
|
||||
const { release, evidence } = await buildPocRelease(
|
||||
{
|
||||
profile,
|
||||
workspace: integrated.workspace,
|
||||
sourceCommit: integrated.commit,
|
||||
releaseId: `run-${session.input.runId}-${session.leaseToken}`,
|
||||
stateDirectory: localPoc.stateDirectory,
|
||||
components: apiOnly ? ['api'] : ['api', 'worker', 'coordinator'],
|
||||
rollbackCompatible:
|
||||
!integrated.migrationRequired &&
|
||||
!changedPaths.some(file => /(^|\/)(migrations?|store|auth|security)(\/|$)/i.test(file)),
|
||||
signal: session.keeper.signal,
|
||||
onProgress: message => session.transition('build', message).then(() => undefined),
|
||||
},
|
||||
session.execute,
|
||||
);
|
||||
const build = await session.evidence({
|
||||
type: 'build',
|
||||
passed: true,
|
||||
sourceCommit: integrated.commit,
|
||||
imageDigest: release.images.api,
|
||||
artifactUri: `poc-release:${evidence.bundleDigest}`,
|
||||
pocRelease: release,
|
||||
});
|
||||
return {
|
||||
status: 'release',
|
||||
verificationEvidenceId: integrated.evidenceId,
|
||||
buildEvidenceId: build.id,
|
||||
environmentId: delivery.environmentId,
|
||||
};
|
||||
}
|
||||
const image = await buildDockerImage(
|
||||
{
|
||||
workspace: integrated.workspace,
|
||||
|
||||
25
apps/worker/test/providers.test.ts
Normal file
25
apps/worker/test/providers.test.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { claudeLocalAuthHome } from '../src/providers.js';
|
||||
|
||||
describe('local Claude worker configuration', () => {
|
||||
const env = {
|
||||
WF_CLAUDE_LOCAL_AUTH_HOME: '/home/operator',
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_SANDBOX_MODE: 'host',
|
||||
WF_WORKER_ROLE: 'executor',
|
||||
};
|
||||
it('accepts only an explicitly configured development host executor', () => {
|
||||
expect(claudeLocalAuthHome(env)).toBe('/home/operator');
|
||||
expect(claudeLocalAuthHome({})).toBeUndefined();
|
||||
for (const change of [
|
||||
{ WF_DEPLOYMENT_MODE: 'production' },
|
||||
{ WF_DEPLOYMENT_MODE: undefined },
|
||||
{ NODE_ENV: 'production' },
|
||||
{ WF_SANDBOX_MODE: 'kata' },
|
||||
{ WF_WORKER_ROLE: 'verifier' },
|
||||
{ WF_WORKER_ROLE: 'coordinator' },
|
||||
{ WF_CLAUDE_LOCAL_AUTH_HOME: 'relative' },
|
||||
])
|
||||
expect(() => claudeLocalAuthHome({ ...env, ...change })).toThrow('development host executor');
|
||||
});
|
||||
});
|
||||
49
docs/local-browser-access.md
Normal file
49
docs/local-browser-access.md
Normal file
@ -0,0 +1,49 @@
|
||||
# 本机工作空间的协作者访问
|
||||
|
||||
本机模式面向获准的可信操作者。网页可监听局域网地址;控制 API、数据库、Temporal、Provisioner 等服务仍监听本机回环地址。平台在服务端验证本机管理员登录后,才会向控制 API 附加既有凭据。浏览器收不到该凭据。
|
||||
|
||||
## 配置与启动
|
||||
|
||||
运行 `node scripts/setup-local-access.mjs`,默认生成 `.wishforge/local-access.json`。文件由当前用户持有,权限为 `0600`,含随机管理员密码及独立会话密钥。生成器仅输出文件路径,重复运行保留原文件。管理员在本机私下读取其中的 `username`、`password`;不要将该文件提交仓库或粘贴到日志中。
|
||||
|
||||
在根目录私有 `.env.local` 中配置:
|
||||
|
||||
```dotenv
|
||||
WF_LOCAL_ACCESS=true
|
||||
WF_LOCAL_ACCESS_FILE=/absolute/path/to/wishforge-ide/.wishforge/local-access.json
|
||||
WF_LOCAL_ALLOWED_ORIGINS=http://localhost:3000,http://127.0.0.1:3000,http://192.168.1.20:3000
|
||||
WF_WEB_HOST=0.0.0.0
|
||||
WF_WEB_PORT=3000
|
||||
WF_WEB_SERVE_MODE=production
|
||||
```
|
||||
|
||||
将示例地址替换为本机实际局域网地址。白名单必须是完整、精确的 HTTP(S) origin,不含路径或结尾斜杠。先执行 `npm run build -w @wishforge/web`,再执行 `npm run dev:all` 启动。`WF_WEB_SERVE_MODE=production` 使用构建好的 Next 应用,避免开发热更新影响持续运行;控制 API 和执行服务仍使用显式 development 配置。本机会话验证独立于 Node 的开发模式,生产 Web 不会放行匿名 dev token。
|
||||
|
||||
代码变更后先停止旧 Web,再重新构建和启动。环境变更后也需要重启 Web;已运行的进程不会自动继承新环境。直接运行 Next 时必须传入这些 `WF_LOCAL_*` 变量。省略 `WF_WEB_SERVE_MODE` 时保留原有开发服务器行为。
|
||||
|
||||
所有入口包括页面、API、事件流、IDE 启动、交付文件均经过本机会话检查。登录页、框架静态资源和站点图标允许未登录访问。`/api/session` 未登录返回 `401`,因此本机模式的启动探活使用 `/login`。
|
||||
|
||||
## 登录与退出
|
||||
|
||||
协作者访问 `http://本机局域网地址:3000`,使用本机生成的管理员账号和密码登录。该身份拥有当前本机工作空间的操作权限;它不是多用户权限系统。登录会话为加密、HttpOnly、SameSite=Strict 的 cookie,1 小时到期。HTTPS 入口额外设置 Secure。退出入口清除会话 cookie。
|
||||
|
||||
连续 8 次错误密码会触发全局 15 分钟窗口限制,不信任客户端提供的转发 IP。限制存在当前 Web 进程内,进程重启会清空限制。更换凭据文件(尤其 passwordHash 或 sessionSecret)会使旧会话失效。所有写入请求必须携带与访问入口完全一致的 Origin;未在白名单的 Host 或跨站请求被拒绝。
|
||||
|
||||
局域网 HTTP 会在网络上传输登录凭据,访问范围应限于受信任网络。若需要互联网访问,使用已批准的 HTTPS 入口,并将该 HTTPS origin 加入白名单;公网隧道、DNS、防火墙和端口转发不由此配置自动创建。代理必须保留正确的 Host,并与 Next 的 HTTP(S) origin 配置一致。
|
||||
|
||||
未启用 `WF_LOCAL_ACCESS` 时保留原有 OIDC 登录逻辑。开发启动器拒绝在关闭本机鉴权时绑定非回环地址。
|
||||
|
||||
## 同一次运行调整预算
|
||||
|
||||
运行进入等待或暂停后,human Owner/Admin 可在运行详情展开“调整预算”,修改 Token、时间或修复轮次上限。平台保留已有用量和执行时间,校验每次运行上限及租户剩余额度,并记录旧值、新值和操作者。保存预算不会恢复执行,确认后仍需点击“继续”。未结算的模型请求需要先完成对账。
|
||||
|
||||
对应接口为 `PATCH /v1/runs/:id/budget`,请求体可包含 `maxTokens`、`maxMinutes`、`maxRepairRounds` 的一个或多个字段,必须提供 `Idempotency-Key`。浏览器通过受保护的 `/api/runs/:id/budget` 调用。已完成、正在运行或排队中的任务不接受预算修改;增加预算不会清零用量或自动扩大其他任务的额度。
|
||||
|
||||
## 验证
|
||||
|
||||
```sh
|
||||
npx vitest run apps/web/src/lib/auth.test.ts apps/web/src/lib/local-access.test.ts
|
||||
npm run typecheck -w @wishforge/web
|
||||
```
|
||||
|
||||
覆盖凭据权限、密码校验、会话过期与轮换、dev token 隔离、Host/Origin 校验、未登录 API 与 IDE 保护、静态资源访问、全局登录限流及原有 OIDC 行为。
|
||||
57
docs/local-poc-operations.md
Normal file
57
docs/local-poc-operations.md
Normal file
@ -0,0 +1,57 @@
|
||||
# 本机许愿服务运行说明
|
||||
|
||||
本机运行 WishForge 网页、控制面、Claude CLI 和发布 Runner。用户通过网页登录后提交愿望;项目固定接入 Gitea 的 `longtao.wu/droboticflow` 仓库 `poc` 分支,目标环境为既有 POC。Claude 修改独立工作区,受保护检查通过后由交付流程处理 Git 提交和发布。
|
||||
|
||||
## 后台运行
|
||||
|
||||
入口为 `/Users/d-robotics/workSpace/wishforge-ide/scripts/local-poc-service.mjs`。不带参数只读取状态。以下命令需在本机已登录的 macOS 用户下执行,无需 sudo:
|
||||
|
||||
```sh
|
||||
node scripts/local-poc-service.mjs status
|
||||
node scripts/local-poc-service.mjs install
|
||||
node scripts/local-poc-service.mjs stop
|
||||
node scripts/local-poc-service.mjs start
|
||||
```
|
||||
|
||||
`install` 写入当前用户的 `~/Library/LaunchAgents/cc.d-robotics.wishforge.local-poc.plist`,注册并启动服务。重复执行会保留正在运行的相同配置。`stop` 禁用并卸载这个 LaunchAgent,防止 KeepAlive 立即重启;`start` 重新启用。更新配置后的重启使用先 `stop`、再 `start`;脚本或 LaunchAgent 内容变化时使用先 `stop`、再 `install`。停止前应让当前发布完成,避免中断正在进行的集群更新;中断后的任务按控制面状态和发布回执恢复。
|
||||
|
||||
首次切换到后台服务时,先由原启动终端正常结束本任务的前台 `scripts/dev.mjs` 与 Runner,再执行 `install`。不要使用 `pkill node`、按端口批量杀进程或停止 Colima。已有依赖服务继续运行;本脚本不启动、停止或重建 Docker、Colima、数据库和 Temporal。
|
||||
|
||||
监督进程只启动两个直接子进程:`scripts/dev.mjs` 与 `node --env-file=.wishforge/local-poc/runner.env --import tsx apps/runner/src/main.ts`。前者自行读取根目录 `.env.local`;后者只获得最小系统环境和独立 `runner.env`。Runner 不继承根目录 API、数据库或模型凭据。子进程退出后整个本任务服务组退出,由 launchd 在节流间隔后重新启动。停止仅处理该 LaunchAgent 及它创建的子进程。
|
||||
|
||||
## 入口与登录
|
||||
|
||||
本机入口为 `http://localhost:3000`;同一可达局域网的其他设备使用 `http://本机局域网IP:3000`。是否跨网可达由现有路由和防火墙决定。本项目没有创建公网隧道或额外公网转发。
|
||||
|
||||
登录凭据保存在 `.wishforge/local-access.json`,只允许本机当前用户读取(0600)。文件含用户名、密码和会话签名材料,不应上传到 Gitea、放入网页静态目录或复制到日志。文件不存在时可运行 `node scripts/setup-local-access.mjs` 创建;重复运行保留已有凭据。`.env.local` 的 `WF_LOCAL_ACCESS_FILE` 指向该文件的绝对路径。
|
||||
|
||||
网络切换导致本机 IP 改变时:
|
||||
|
||||
1. 使用系统网络设置或 `ipconfig getifaddr en0` 确认当前接口地址;使用其他接口时查询相应接口。
|
||||
2. 更新 `.env.local` 中 `WF_LOCAL_ALLOWED_ORIGINS`,保留需要的 `http://localhost:3000`、`http://127.0.0.1:3000`,将局域网地址更新为完整的 `http://IP:3000`。不使用通配符。
|
||||
3. 确认 `WF_WEB_HOST=0.0.0.0`、`WF_LOCAL_ACCESS=true`,停止并重新启动后台服务。
|
||||
4. 从另一台设备打开新地址并实际登录、刷新项目列表,确认网络和会话都可用。
|
||||
|
||||
网页使用 `WF_WEB_SERVE_MODE=production` 时,修改 WishForge 网页源码后先在项目根运行 `node scripts/build-local-web.mjs`,再重启本机服务。此开关只决定 Next.js 使用预构建网页;控制面的本机运行模式仍为 `WF_DEPLOYMENT_MODE=development`。
|
||||
|
||||
## 配置与日志
|
||||
|
||||
- `.env.local`:本机控制面和网页配置,0600。
|
||||
- `.wishforge/local-poc/runner.env`:Runner 独立配置,0600。
|
||||
- `.wishforge/local-poc/` 下的 POC profile、签名公钥和执行回执:由交付流程维护,避免在运行任务中修改绑定。
|
||||
- `.wishforge/local-poc/logs/platform.log`、`runner.log`、`supervisor.log`:目录 0700、文件 0600。每个文件达到 5 MiB 时轮转,保留 3 个历史文件。
|
||||
- `.wishforge/local-poc/service-state.json`:监督进程和直接子进程状态;`status` 以 launchd 实时状态为准,不用陈旧 PID 杀进程。
|
||||
|
||||
`status` 只输出运行状态、PID 和日志位置,不显示任何凭据。排查时先看 `supervisor.log` 的启动/退出记录,再看对应服务日志。持续重启通常意味着依赖服务、构建产物、环境文件或权限未满足。请修复原因后再启动,不要复制完整环境文件到聊天或工单。
|
||||
|
||||
## 验证和电脑睡眠
|
||||
|
||||
本地执行目录默认是 `~/.local/share/wishforge/runs`。不要把 `WF_LOCAL_RUN_ROOT` 放在 WishForge 源码目录下;Node/TypeScript 会沿父目录查找 `node_modules`,使项目意外继承平台的类型定义。本地 provisioner 会拒绝这种嵌套配置。
|
||||
|
||||
旧配置从 `.wishforge/runs` 迁移时,等待当前代次停止,在 `.env.local` 中把 `WF_LOCAL_RUN_ROOT` 改为仓库外的绝对路径,再重启本机服务。不要移动、删除或复用旧代次目录;它们保留作为恢复资料。已有运行通过控制面已持久化的 checkpoint 在新目录还原,已完成实现的运行应继续验证,不必重新调用模型。只有 `setup-local-poc.ts` 的明确 POC 配置流程会更新该设置;通用 `setup-local.mjs` 保留已存在的配置,需要操作人主动迁移。
|
||||
|
||||
受保护的项目安装、检查入口是 `scripts/poc-project-check.mjs install|verify`,项目设置使用该文件的绝对路径。它固定运行依赖安装、TypeScript 检查、前端测试、前端构建和 Go 测试,共享 `.wishforge/poc-check-cache/` 缓存,不调用仓库部署或真实环境验收脚本。线上 POC 健康和发布版本须另行检查,不能以本地测试通过替代。
|
||||
|
||||
LaunchAgent 随当前用户登录运行,退出登录后停止;电脑关机或进入系统睡眠时,本机网页及任务执行不可用。需要持续接收愿望时保持电脑供电,并在系统设置中选择合适的睡眠策略。本脚本不修改睡眠设置,也不创建 Codex 定时任务。唤醒后检查 `status`、网页登录和依赖服务,运行中的愿望以控制面记录为准继续处理。
|
||||
|
||||
本机实验套餐由 operator CLI 管理:`node --import tsx scripts/configure-local-poc-plan.ts 20000000` 设置每月 2000 万计量 Token 和单并发;这不购买模型额度或扣款。单次愿望默认 100 万 Token,包含 CLI 缓存读取。CLI 最终计量在回合结束时上报,超预算会阻止后续执行并保留检查点,不能视为供应商逐请求实时硬上限。
|
||||
63
docs/reference/local-poc-acceptance-2026-10-22.md
Normal file
63
docs/reference/local-poc-acceptance-2026-10-22.md
Normal file
@ -0,0 +1,63 @@
|
||||
# 本机 Claude 到 POC 交付验收
|
||||
|
||||
日期:2026-10-22,Asia/Shanghai。已完成本机网页 → Claude CLI → 独立验证 → Gitea `poc` → 现有 POC 部署链路。适用范围是可信协作者使用的本机开发模式,不提供不可信租户的操作系统级隔离。
|
||||
|
||||
## 入口与目标
|
||||
|
||||
- 本机:<http://localhost:3000>;当前局域网:<http://172.20.10.5:3000>。
|
||||
- 项目:`DroboticFlow POC · Claude 本机`。
|
||||
- 登录信息:本机 `.wishforge/local-access.json`,0600,不进入版本库。
|
||||
- 仓库:`https://gitea.d-robotics.cc/longtao.wu/droboticflow.git`,固定 `poc` 分支。
|
||||
- 目标:现有 `droboticflow` namespace,<http://120.48.164.19>。
|
||||
- 后台服务:`cc.d-robotics.wishforge.local-poc` 用户 LaunchAgent,见 [运维说明](../local-poc-operations.md)。
|
||||
|
||||
## 首次真实交付
|
||||
|
||||
| 字段 | 结果 |
|
||||
|---|---|
|
||||
| Run | `f40620ef-d04c-4903-a7e9-0c81551b0830` |
|
||||
| 愿望 | 系统状态检查时间、手动重试、失败保留旧值并标记过期、健康与版本差异分开显示 |
|
||||
| 执行器 | 本机 Claude CLI 2.1.292,沿用本机授权 |
|
||||
| 结果 | `succeeded / accept`,交付 `accepted=true` |
|
||||
| 实际合入与发布源码 | `7613677dfe6412355f52319e3889d3d36d6f8817` |
|
||||
| Release | `33959763-fd8a-4e65-9140-794d882486ea` |
|
||||
| API/Web 镜像 | `sha256:28442975ba18e91261e6729b64f1d87b2e4ddabcd90e82b22ec1439c868c575b` |
|
||||
| Worker、Coordinator | 沿用核对过的原镜像,三个 Deployment 均就绪 |
|
||||
| CLI 计量 | 该业务 run 两轮执行报告 1,466,304 Token,包含缓存创建/读取;不是货币账单 |
|
||||
|
||||
运行期间目标分支有其他提交,平台对实际合并 commit 重新验证,再推送、构建和发布。后续下载文档提交 `f553e404` 仍保留在分支中,发布版本明确指向上表的已验证 commit。
|
||||
|
||||
`/api/health`、`/api/ready` 的 `buildCommit` 与交付一致。既有 `health.image` 字段仍来自旧环境配置,本次没有覆盖该配置;镜像验收采用 Deployment/Pod 的真实 digest,不把旧字段当作运行镜像证明。
|
||||
|
||||
## 实际过程与修复
|
||||
|
||||
1. 通过正式 adapter 验证本机 Claude 修改临时仓库、导出 checkpoint;固定 HTTPS 到 SSH 映射完成 Gitea 只读克隆。
|
||||
2. POC 基线通过类型检查、262 项前端测试、前端构建和 Go 测试。独立 Kaniko 预检成功,临时 Pod 已清理;未使用预检产物发布。
|
||||
3. 首次最终用量超过初始预算,暴露检查点保存顺序问题。恢复了该会话 18 次成功文件操作涉及的 5 个文件,逐一核对哈希并记录操作人恢复事件,没有伪造验证证据或重发愿望。
|
||||
4. 执行器已修复为先保存停止后的实现,再幂等结算最终用量;保存失败/取消保留工作区和恢复材料。流式/代理计量仍即时执行。
|
||||
5. 初次独立类型检查受平台父目录 React 类型污染;相同源码移到外部目录后通过。运行根目录现为 `~/.local/share/wishforge/runs`,禁止嵌套在平台源码内。错误归因产生的项目经验已停用,原失败证据保留。
|
||||
6. 同一 run 通过正式预算接口继续,原用量未清零。持久化检查点支持直接恢复验证,没有第三轮模型执行。
|
||||
7. 最终代码通过验证及合并 SHA 复验、构建、签名 Runner、POC rollout、组件与网页入口检查。
|
||||
|
||||
本次包含平台故障修复和操作人恢复,不是“首轮零人工干预”的结果。
|
||||
|
||||
## 浏览器验收
|
||||
|
||||
真实 Chrome 在局域网登录并只提交一次愿望;运行页最终显示已交付和正确 POC 链接。POC 的公开状态面板实际验证了手动检查的加载态、成功时间更新及版本差异独立显示。仅对测试浏览器断网后,旧值与成功时间保留并标记“数据已过期”;恢复网络后重试正常。没有登录组织或操作业务数据。
|
||||
|
||||
本机证据:
|
||||
|
||||
- `/private/tmp/wishforge-poc-delivery-summary.json`
|
||||
- `/private/tmp/wishforge-first-poc-final.json`
|
||||
- `/private/tmp/wishforge-first-poc-ui-submission.json`
|
||||
- `/tmp/wishforge-real-delivery-completed.png`
|
||||
- `/tmp/droboticflow-status-live-ready.png`
|
||||
- `/tmp/droboticflow-status-browser-offline.png`
|
||||
|
||||
## 回归与运行条件
|
||||
|
||||
主 Vitest 388 通过、17 个显式集成开关跳过;Provisioner 27 通过;全仓类型检查、正式网页构建和脚本契约测试通过。POC 人工回滚的独立恢复 manifest 回归通过,本次未对已交付版本执行真实人工回滚。
|
||||
|
||||
未配置公网隧道。电脑需保持运行并能访问 Gitea、模型服务和 POC 集群。CLI 最终用量在回合结束时上报,预算会阻止后续执行,但不能保证供应商逐请求实时硬上限;缓存读取计入额度。单次 POC 愿望默认 100 万 Token,可在等待/暂停时由管理员调整。
|
||||
|
||||
本地实验 operator 套餐采用单并发、每月 2000 万计量 Token,仍校验额度预留。设置不购买模型额度或扣款;实际费用或订阅限制由本机 Claude 账号决定。
|
||||
50
docs/reference/poc-delivery-profile.md
Normal file
50
docs/reference/poc-delivery-profile.md
Normal file
@ -0,0 +1,50 @@
|
||||
# Existing DroboticFlow POC delivery
|
||||
|
||||
The local POC mode adopts the existing `droboticflow` Kubernetes namespace. The operator profile fixes the Gitea repository, `poc` branch, API server, namespace UID, three Deployment UIDs, container names, registry namespace, public URL, and pinned Kaniko executor. It is a private local file; web requests and repository manifests cannot replace it.
|
||||
|
||||
`packages/runtime/src/poc-profile.ts` defines the profile. `PocRunnerDeploymentAdapter` plugs into the normal signed Runner queue. The complete `pocRelease` component set participates in the signed manifest; its API image and source commit must match the outer release. This mode does not recreate Services, Secrets, RBAC, PVCs, or application configuration.
|
||||
|
||||
## Source and build
|
||||
|
||||
1. The verifier commits and pushes the tested source to Gitea `poc` using the ordinary branch precondition.
|
||||
2. `buildPocRelease` checks the operator repository, local HEAD, and live remote `poc` HEAD against the complete source SHA. It archives that exact commit, excluding untracked files.
|
||||
3. Each selected component builds in a separate bounded Kaniko Pod with an `emptyDir`. The Pod mounts only the existing registry credential reference and has service-account token automount disabled. It has no business PVC or application secret mounts.
|
||||
4. Kaniko records the pushed immutable digest through `/dev/termination-log`. The builder saves source-archive, component, Pod UID, and bundle evidence locally, then deletes its own Pod using a UID precondition. Logs remain in private operator state and are not returned to the website.
|
||||
5. Components not selected for rebuilding remain pinned to their observed digests in the same release set. The API, Worker, and Coordinator are always included in the manifest.
|
||||
|
||||
The component names accepted by the builder are `api`, `worker`, and `coordinator`. Default builds include all three. New feature builds must follow the target repository's versioning instructions; a preflight reconstruction of an existing commit is recorded separately from feature delivery.
|
||||
|
||||
## Apply and verification
|
||||
|
||||
Before the first mutation, the adapter verifies the configured cluster server, namespace UID, each Deployment UID, current immutable images, readiness, and the preserved configuration fingerprints. Fingerprints cover each Deployment's complete `spec`, normalizing only its named container image and WishForge's own audit annotations. Replicas, sidecars, volumes, security settings, labels, other annotations, and environment configuration remain covered.
|
||||
|
||||
Every image update is a JSON Patch with `test` operations for UID, resourceVersion, container name, and previous image. Updates touch only the named image and `wishforge.d-robotics.cc/*` audit annotations. The sequence is Coordinator, Worker, then API. Multiple Kubernetes Deployments are not a transaction; the local execution policy must reject incompatible schema or cross-service protocol changes before release.
|
||||
|
||||
Completion requires all three rollouts, exact ready replica counts, live Pod digests, unchanged configuration fingerprints, public health/readiness/application responses, and the protected business smoke checks from the signed release. When the API changes, the public `/api/health.buildCommit` must equal the delivered source SHA. The pre-existing `DROBOTICFLOW_IMAGE` environment variable is preserved; Pod digests and the image-embedded build commit are authoritative for delivery.
|
||||
|
||||
## Durable state and recovery
|
||||
|
||||
Release journals are written atomically with private permissions and filesystem synchronization. A release ID cannot be reused with another manifest. An environment lock prevents overlapping local deployments. Intent is persisted before each remote patch, so an interrupted write is reconciled by observation instead of repeated blindly.
|
||||
|
||||
Possible final journal states are:
|
||||
|
||||
| State | Meaning |
|
||||
| --- | --- |
|
||||
| `succeeded` | The desired component set passed runtime and business checks. |
|
||||
| `rolled_back` | An explicitly compatible prior image set was restored and passed runtime checks. |
|
||||
| `needs_reconciliation` | A receipt is uncertain, configuration changed, an external release appeared, or rollback compatibility was not declared. |
|
||||
|
||||
Rollback only changes images still owned by the attempted release, and never overwrites a newer external image. It does not revert the Gitea commit or database changes. Configuration drift stops automatic compensation. After a process crash, an operator must inspect any stale lock before removing it; `reconcile` does not bypass active locks. Signed Runner recovery also requires the persisted release to match the signed component bundle exactly.
|
||||
|
||||
## Operator CLI
|
||||
|
||||
```sh
|
||||
npx tsx scripts/poc-release.ts inspect /absolute/operator/poc-profile.json /absolute/operator/poc-state
|
||||
npx tsx scripts/poc-release.ts build /absolute/operator/poc-profile.json /absolute/operator/poc-state /absolute/clean/checkout FULL_SHA RELEASE_ID
|
||||
npx tsx scripts/poc-release.ts deploy /absolute/operator/poc-profile.json /absolute/operator/poc-state /absolute/operator/poc-state/builds/RELEASE_ID/release.json
|
||||
npx tsx scripts/poc-release.ts reconcile /absolute/operator/poc-profile.json /absolute/operator/poc-state RELEASE_ID
|
||||
```
|
||||
|
||||
The CLI is for local operator use. The website uses the signed Runner path. `PocRunnerDeploymentAdapter.render()` is read-only; deployment requires `{ execute: true }`. The dedicated POC HTTP exception belongs to the local operator binding, not the general application URL policy.
|
||||
|
||||
Focused verification is `npx vitest run packages/runtime/test/poc-release.test.ts`. The tests exercise baseline drift, immutable image and configuration binding, JSON Patch preconditions, preserved resources, uncertain committed writes, compatible rollback, newer external releases, duplicate receipts, protected smoke failure, and Runner evidence.
|
||||
1
package-lock.json
generated
1
package-lock.json
generated
@ -44,6 +44,7 @@
|
||||
"@temporalio/client": "^1.13.0",
|
||||
"@wishforge/agent": "*",
|
||||
"@wishforge/contracts": "*",
|
||||
"@wishforge/runtime": "*",
|
||||
"jose": "^6.0.0",
|
||||
"pg": "^8.16.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
import { pocReleaseInput, type PocReleaseSet } from './poc-delivery.js';
|
||||
export { pocReleaseInput, type PocReleaseSet } from './poc-delivery.js';
|
||||
import type { ApplicationAccessEvidence } from './application-access.js';
|
||||
import type { DeliveryConfig, IntegrationLease, ResourceAllocation } from './entities.js';
|
||||
import { z } from 'zod';
|
||||
@ -210,6 +212,7 @@ export interface Environment extends Entity {
|
||||
secretVersionRefs: string[];
|
||||
}
|
||||
export interface Evidence extends Entity {
|
||||
pocRelease?: PocReleaseSet;
|
||||
releaseId?: string;
|
||||
environmentId?: string;
|
||||
bindingDigest?: string;
|
||||
@ -244,6 +247,7 @@ export interface CommandEvidence {
|
||||
outputTail?: string;
|
||||
}
|
||||
export interface Release extends Entity {
|
||||
pocRelease?: PocReleaseSet;
|
||||
deploymentSecretPolicyDigest?: string;
|
||||
applicationUrl?: string;
|
||||
deliveryRequired?: boolean;
|
||||
@ -323,6 +327,15 @@ export const budgetSchema = z
|
||||
maxRepairRounds: z.number().int().min(0).max(3).default(3),
|
||||
})
|
||||
.strict();
|
||||
/** Manual, bounded changes to a stopped run; omitted fields retain their existing limits. */
|
||||
export const budgetPatchSchema = z
|
||||
.object({
|
||||
maxTokens: budgetSchema.shape.maxTokens.removeDefault().optional(),
|
||||
maxMinutes: budgetSchema.shape.maxMinutes.removeDefault().optional(),
|
||||
maxRepairRounds: budgetSchema.shape.maxRepairRounds.removeDefault().optional(),
|
||||
})
|
||||
.strict()
|
||||
.refine(value => Object.values(value).some(field => field !== undefined), 'Provide at least one budget limit');
|
||||
export const projectInput = z
|
||||
.object({
|
||||
name: text.max(120),
|
||||
@ -445,6 +458,7 @@ const commandEvidence = z
|
||||
.strict();
|
||||
export const evidenceInput = z
|
||||
.object({
|
||||
pocRelease: pocReleaseInput.optional(),
|
||||
accessEvidence: z
|
||||
.object({
|
||||
applicationUrl: z.string().max(2048),
|
||||
|
||||
19
packages/contracts/src/poc-delivery.ts
Normal file
19
packages/contracts/src/poc-delivery.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const immutableImage = z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:/-]*@sha256:[a-f0-9]{64}$/);
|
||||
const imageSet = z.object({ api: immutableImage, worker: immutableImage, coordinator: immutableImage }).strict();
|
||||
const digest = z.string().regex(/^sha256:[a-f0-9]{64}$/);
|
||||
/** The entire component set, including unchanged components, participates in approval and runner signatures. */
|
||||
export const pocReleaseInput = z
|
||||
.object({
|
||||
schemaVersion: z.literal(1),
|
||||
id: z.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/),
|
||||
sourceCommit: z.string().regex(/^[a-f0-9]{40}$/),
|
||||
profileDigest: digest,
|
||||
images: imageSet,
|
||||
expectedImages: imageSet,
|
||||
expectedTemplateDigests: z.object({ api: digest, worker: digest, coordinator: digest }).strict().optional(),
|
||||
rollbackCompatible: z.boolean(),
|
||||
})
|
||||
.strict();
|
||||
export type PocReleaseSet = z.infer<typeof pocReleaseInput>;
|
||||
@ -1,9 +1,20 @@
|
||||
import { normalizeApplicationUrl, type ApplicationAccessEvidence, isProduction } from '@wishforge/contracts';
|
||||
import { requireCommit, safeIdentifier } from './git.js';
|
||||
import { RuntimeError, type ReleaseManifest } from './types.js';
|
||||
import { RuntimeError, type ReleaseManifest, type TrustedPocDeliveryPolicy } from './types.js';
|
||||
|
||||
export function validateRelease(release: ReleaseManifest): void {
|
||||
if (release.applicationUrl) {
|
||||
export function validateRelease(
|
||||
release: ReleaseManifest,
|
||||
options: { trustedPocDelivery?: TrustedPocDeliveryPolicy } = {},
|
||||
): void {
|
||||
const trustedPoc = options.trustedPocDelivery;
|
||||
const fixedPocUrl =
|
||||
!!trustedPoc &&
|
||||
!isProduction(process.env) &&
|
||||
trustedPoc.applicationUrl === 'http://120.48.164.19' &&
|
||||
/^sha256:[a-f0-9]{64}$/.test(trustedPoc.profileDigest) &&
|
||||
release.pocRelease?.profileDigest === trustedPoc.profileDigest &&
|
||||
release.applicationUrl === trustedPoc.applicationUrl;
|
||||
if (release.applicationUrl && !fixedPocUrl) {
|
||||
try {
|
||||
if (
|
||||
normalizeApplicationUrl(
|
||||
|
||||
@ -73,10 +73,54 @@ export class MergeConflictError extends RuntimeError {
|
||||
}
|
||||
export interface GitOptions {
|
||||
credential?: { token: string; repositoryUrl: string; username?: 'x-access-token' };
|
||||
/** Operator configuration only: a single HTTPS repository may use the host's SSH login in development. */
|
||||
localSshTransport?: LocalSshTransport;
|
||||
allowLocalHttp?: boolean;
|
||||
/** Clone, fetch and push of large repositories can take minutes; local commands finish far sooner. */
|
||||
timeoutMs?: number;
|
||||
}
|
||||
export interface LocalSshTransport {
|
||||
repositoryUrl: string;
|
||||
/** Canonical ssh://git@host/owner/repository.git, using the standard SSH port. */
|
||||
sshUrl: string;
|
||||
home: string;
|
||||
}
|
||||
|
||||
function validateLocalSshTransport(transport: LocalSshTransport): void {
|
||||
if (process.env.WF_DEPLOYMENT_MODE !== 'development' || process.env.NODE_ENV === 'production')
|
||||
throw new RuntimeError(
|
||||
'LOCAL_SSH_POLICY',
|
||||
'Local SSH authentication is only available in explicit development mode',
|
||||
);
|
||||
const repository = validateRepositoryUrl(transport.repositoryUrl);
|
||||
let ssh: URL;
|
||||
try {
|
||||
ssh = new URL(transport.sshUrl);
|
||||
} catch {
|
||||
throw new RuntimeError('LOCAL_SSH_POLICY', 'The operator SSH transport requires a canonical ssh://git@host URL');
|
||||
}
|
||||
if (
|
||||
repository.protocol !== 'https:' ||
|
||||
repository.port ||
|
||||
repository.href !== transport.repositoryUrl ||
|
||||
!/^\/[A-Za-z0-9][A-Za-z0-9_.-]*\/[A-Za-z0-9][A-Za-z0-9_.-]*\.git$/.test(repository.pathname) ||
|
||||
ssh.protocol !== 'ssh:' ||
|
||||
ssh.username !== 'git' ||
|
||||
ssh.password ||
|
||||
(ssh.port && ssh.port !== '22') ||
|
||||
ssh.hostname !== repository.hostname ||
|
||||
ssh.pathname !== repository.pathname ||
|
||||
ssh.search ||
|
||||
ssh.hash ||
|
||||
/[\u0000-\u0020\u007f]/.test(transport.sshUrl) ||
|
||||
!path.isAbsolute(transport.home) ||
|
||||
/[\u0000-\u001f\u007f]/.test(transport.home)
|
||||
)
|
||||
throw new RuntimeError(
|
||||
'LOCAL_SSH_POLICY',
|
||||
'Local SSH must use git on port 22 for the exact configured HTTPS host and repository, with an absolute HOME',
|
||||
);
|
||||
}
|
||||
/** Inline checkpoint transport limits (raw bytes); larger changes need durable artifact storage first. */
|
||||
export const CHECKPOINT_BUNDLE_LIMIT = 4 * 1024 * 1024;
|
||||
export const CHECKPOINT_PATCH_LIMIT = 4 * 1024 * 1024;
|
||||
@ -84,12 +128,17 @@ export class GitWorkspace {
|
||||
constructor(
|
||||
private readonly execute: CommandExecutor = executeCommand,
|
||||
private readonly options: GitOptions = {},
|
||||
) {}
|
||||
) {
|
||||
if (options.localSshTransport) validateLocalSshTransport(options.localSshTransport);
|
||||
}
|
||||
setCredential(credential: GitOptions['credential']) {
|
||||
this.options.credential = credential;
|
||||
}
|
||||
private validateRepository(repository: string) {
|
||||
return validateRepositoryUrl(repository, { allowLocalHttp: this.options.allowLocalHttp });
|
||||
const url = validateRepositoryUrl(repository, { allowLocalHttp: this.options.allowLocalHttp });
|
||||
if (this.options.localSshTransport && repository !== this.options.localSshTransport.repositoryUrl)
|
||||
throw new RuntimeError('LOCAL_SSH_SCOPE', 'The repository differs from the operator-pinned local SSH transport');
|
||||
return url;
|
||||
}
|
||||
private command(cwd: string, args: string[]): Command {
|
||||
const authEnv: Record<string, string> = {
|
||||
@ -106,6 +155,18 @@ export class GitWorkspace {
|
||||
? `Authorization: Basic ${Buffer.from(`x-access-token:${this.options.credential.token}`).toString('base64')}`
|
||||
: `Authorization: Bearer ${this.options.credential.token}`;
|
||||
}
|
||||
const transport = this.options.localSshTransport;
|
||||
if (transport) {
|
||||
validateLocalSshTransport(transport);
|
||||
const count = Number(authEnv.GIT_CONFIG_COUNT);
|
||||
authEnv[`GIT_CONFIG_KEY_${count}`] = `url.${transport.sshUrl}.insteadOf`;
|
||||
authEnv[`GIT_CONFIG_VALUE_${count}`] = transport.repositoryUrl;
|
||||
authEnv.GIT_CONFIG_COUNT = String(count + 1);
|
||||
// Use the pinned URL directly: remote.origin.url/pushurl are multi-valued and cannot be
|
||||
// safely overridden by adding another config value. Workspace config is never authority.
|
||||
if (['fetch', 'push', 'ls-remote'].includes(args[0] ?? ''))
|
||||
args = args.map(arg => (arg === 'origin' ? transport.repositoryUrl : arg));
|
||||
}
|
||||
return {
|
||||
executable: 'git',
|
||||
args: [
|
||||
@ -129,6 +190,9 @@ export class GitWorkspace {
|
||||
GIT_TERMINAL_PROMPT: '0',
|
||||
GIT_CONFIG_NOSYSTEM: '1',
|
||||
GIT_CONFIG_GLOBAL: '/dev/null',
|
||||
...(transport
|
||||
? { HOME: transport.home, GIT_SSH_COMMAND: 'ssh -oBatchMode=yes -oStrictHostKeyChecking=yes' }
|
||||
: {}),
|
||||
...authEnv,
|
||||
},
|
||||
};
|
||||
@ -291,7 +355,7 @@ export class GitWorkspace {
|
||||
const merging =
|
||||
(await this.execute(this.command(workspace, ['rev-parse', '-q', '--verify', 'MERGE_HEAD']))).exitCode === 0;
|
||||
if (changed || merging)
|
||||
await this.git(workspace, ['commit', '-m', `WishForge checkpoint ${safeIdentifier(runId)}`]);
|
||||
await this.git(workspace, ['commit', '-m', `✨ WishForge checkpoint ${safeIdentifier(runId)}`]);
|
||||
const sourceCommit = requireCommit(await this.git(workspace, ['rev-parse', 'HEAD']));
|
||||
const diff = await this.execute({
|
||||
executable: 'git',
|
||||
@ -441,7 +505,16 @@ export class GitWorkspace {
|
||||
if (await this.isAncestor(input.repository, actual, input.sourceCommit))
|
||||
return { commit: input.sourceCommit, baseCommit: actual, fastForward: true };
|
||||
await this.git(input.repository, ['worktree', 'add', '--detach', input.path, actual]);
|
||||
const merge = await this.execute(this.command(input.path, ['merge', '--no-ff', '--no-edit', input.sourceCommit]));
|
||||
const merge = await this.execute(
|
||||
this.command(input.path, [
|
||||
'merge',
|
||||
'--no-ff',
|
||||
...(this.options.localSshTransport
|
||||
? ['-m', `✨ Integrate verified WishForge commit ${input.sourceCommit.slice(0, 12)}`]
|
||||
: ['--no-edit']),
|
||||
input.sourceCommit,
|
||||
]),
|
||||
);
|
||||
if (merge.exitCode !== 0) {
|
||||
const conflicts = await this.conflicts(input.path).catch(() => []);
|
||||
await this.removeWorktree(input.repository, input.path);
|
||||
|
||||
@ -12,3 +12,9 @@ export * from './verification-profile.js';
|
||||
export * from './project-manifest.js';
|
||||
|
||||
export * from './deployment-secrets.js';
|
||||
export * from './poc-profile.js';
|
||||
export * from './poc-release.js';
|
||||
export * from './poc-build.js';
|
||||
export * from './poc-runner.js';
|
||||
|
||||
export * from './local-poc-binding.js';
|
||||
|
||||
46
packages/runtime/src/local-poc-binding.ts
Normal file
46
packages/runtime/src/local-poc-binding.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { readFileSync, statSync } from 'node:fs';
|
||||
import { z } from 'zod';
|
||||
import { RuntimeError } from './types.js';
|
||||
|
||||
const absolute = z
|
||||
.string()
|
||||
.startsWith('/')
|
||||
.refine(v => !/[\r\n\0]/.test(v));
|
||||
export const localPocBindingSchema = z
|
||||
.object({
|
||||
schemaVersion: z.literal(1),
|
||||
tenantId: z.string().regex(/^[A-Za-z0-9_-]{1,128}$/),
|
||||
projectId: z.string().uuid(),
|
||||
environmentId: z.string().uuid(),
|
||||
repositoryUrl: z.literal('https://gitea.d-robotics.cc/longtao.wu/droboticflow.git'),
|
||||
targetBranch: z.literal('poc'),
|
||||
profileFile: absolute,
|
||||
stateDirectory: absolute,
|
||||
gitHome: absolute,
|
||||
})
|
||||
.strict();
|
||||
export type LocalPocBinding = z.infer<typeof localPocBindingSchema>;
|
||||
|
||||
/** A local operator's file, never a project manifest or an HTTP parameter. */
|
||||
export function readLocalPocBinding(file: string | undefined, env = process.env): LocalPocBinding | undefined {
|
||||
if (!file) return undefined;
|
||||
if (env.WF_DEPLOYMENT_MODE !== 'development' || env.NODE_ENV === 'production')
|
||||
throw new RuntimeError('LOCAL_POC_POLICY', 'Native POC delivery is restricted to explicit development mode');
|
||||
const info = statSync(file);
|
||||
if (!info.isFile() || info.size > 16384 || (info.mode & 0o022) !== 0)
|
||||
throw new RuntimeError('LOCAL_POC_POLICY', 'POC binding must be an operator file without group/world write access');
|
||||
return localPocBindingSchema.parse(JSON.parse(readFileSync(file, 'utf8')));
|
||||
}
|
||||
|
||||
export function localPocForProject(
|
||||
binding: LocalPocBinding | undefined,
|
||||
project: { id: string; tenantId: string; repositoryUrl?: string; targetBranch: string },
|
||||
): LocalPocBinding | undefined {
|
||||
if (!binding || project.id !== binding.projectId || project.tenantId !== binding.tenantId) return undefined;
|
||||
if (project.repositoryUrl !== binding.repositoryUrl || project.targetBranch !== binding.targetBranch)
|
||||
throw new RuntimeError(
|
||||
'LOCAL_POC_POLICY',
|
||||
'POC project repository or target branch changed; operator review is required',
|
||||
);
|
||||
return binding;
|
||||
}
|
||||
311
packages/runtime/src/poc-build.ts
Normal file
311
packages/runtime/src/poc-build.ts
Normal file
@ -0,0 +1,311 @@
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { mkdir, readFile, stat } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { setTimeout as delay } from 'node:timers/promises';
|
||||
import { requireCommit, safeIdentifier } from './git.js';
|
||||
import { executeCommand, type CommandExecutor } from './process.js';
|
||||
import {
|
||||
PocDeploymentAdapter,
|
||||
pocReleaseSchema,
|
||||
writePocJson,
|
||||
type PocImages,
|
||||
type PocRelease,
|
||||
} from './poc-release.js';
|
||||
import { pocComponents, pocComponentNames, profileDigest, type PocProfile, type PocComponent } from './poc-profile.js';
|
||||
import { pocReleaseDigest } from './poc-runner.js';
|
||||
|
||||
export interface PocBuildEvidence {
|
||||
schemaVersion: 1;
|
||||
releaseId: string;
|
||||
sourceCommit: string;
|
||||
profileDigest: string;
|
||||
architecture: 'linux/amd64';
|
||||
archiveSha256: string;
|
||||
startedAt: string;
|
||||
completedAt: string;
|
||||
components: Partial<Record<PocComponent, { image: string; pod: string; uid: string; dockerfile: string }>>;
|
||||
retainedImages: Partial<PocImages>;
|
||||
bundleDigest: string;
|
||||
}
|
||||
export interface PocBuildInput {
|
||||
profile: PocProfile;
|
||||
workspace: string;
|
||||
sourceCommit: string;
|
||||
releaseId: string;
|
||||
stateDirectory: string;
|
||||
components?: PocComponent[];
|
||||
rollbackCompatible?: boolean;
|
||||
signal?: AbortSignal;
|
||||
onProgress?: (message: string) => void | Promise<void>;
|
||||
}
|
||||
|
||||
interface BuildPod {
|
||||
metadata: { uid: string; labels?: Record<string, string> };
|
||||
status?: {
|
||||
phase?: string;
|
||||
containerStatuses?: {
|
||||
state?: {
|
||||
terminated?: { exitCode: number; message?: string };
|
||||
waiting?: { reason?: string; message?: string };
|
||||
running?: unknown;
|
||||
};
|
||||
}[];
|
||||
};
|
||||
}
|
||||
|
||||
/** Immutable source only; no business PVC, application secrets or service-account token are mounted. */
|
||||
export function createPocBuildPod(
|
||||
profile: PocProfile,
|
||||
input: { name: string; releaseId: string; component: PocComponent; sourceCommit: string; buildTime: string },
|
||||
) {
|
||||
const component = pocComponents[input.component];
|
||||
return {
|
||||
apiVersion: 'v1',
|
||||
kind: 'Pod',
|
||||
metadata: {
|
||||
name: input.name,
|
||||
namespace: profile.namespace,
|
||||
labels: { 'app.kubernetes.io/managed-by': 'wishforge-poc', 'wishforge-release': input.releaseId },
|
||||
},
|
||||
spec: {
|
||||
automountServiceAccountToken: false,
|
||||
restartPolicy: 'Never',
|
||||
activeDeadlineSeconds: profile.buildTimeoutSeconds,
|
||||
nodeSelector: { 'kubernetes.io/arch': 'amd64', 'kubernetes.io/os': 'linux' },
|
||||
containers: [
|
||||
{
|
||||
name: 'kaniko',
|
||||
image: profile.kanikoImage,
|
||||
command: ['/busybox/sh', '-c'],
|
||||
args: [
|
||||
'until [ -f /context/ready ]; do /busybox/sleep 1; done; exec /kaniko/executor "$@"',
|
||||
'--',
|
||||
'--context=dir:///context/source',
|
||||
`--dockerfile=/context/source/${component.dockerfile}`,
|
||||
`--destination=${profile.registry}/${component.repository}:wishforge-${input.sourceCommit.slice(0, 12)}-${input.releaseId.slice(-12)}`,
|
||||
'--digest-file=/dev/termination-log',
|
||||
`--build-arg=DROBOTICFLOW_BUILD_COMMIT=${input.sourceCommit}`,
|
||||
`--build-arg=DROBOTICFLOW_BUILD_TIME=${input.buildTime}`,
|
||||
'--build-arg=GOPROXY=https://goproxy.cn,direct',
|
||||
...profile.registryMirrors.map(mirror => `--registry-mirror=${mirror}`),
|
||||
...(profile.registryMirrors.length ? ['--skip-default-registry-fallback'] : []),
|
||||
'--snapshot-mode=full',
|
||||
'--verbosity=warn',
|
||||
],
|
||||
resources: {
|
||||
requests: { cpu: '500m', memory: '1Gi' },
|
||||
limits: { cpu: '4', memory: '8Gi', 'ephemeral-storage': '12Gi' },
|
||||
},
|
||||
volumeMounts: [
|
||||
{ name: 'context', mountPath: '/context' },
|
||||
{ name: 'registry', mountPath: '/kaniko/.docker', readOnly: true },
|
||||
],
|
||||
terminationMessagePolicy: 'File',
|
||||
},
|
||||
],
|
||||
volumes: [
|
||||
{ name: 'context', emptyDir: { sizeLimit: '4Gi' } },
|
||||
{
|
||||
name: 'registry',
|
||||
secret: { secretName: profile.registrySecret, items: [{ key: '.dockerconfigjson', path: 'config.json' }] },
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function buildPocRelease(
|
||||
input: PocBuildInput,
|
||||
execute: CommandExecutor = executeCommand,
|
||||
): Promise<{ release: PocRelease; evidence: PocBuildEvidence }> {
|
||||
requireCommit(input.sourceCommit);
|
||||
safeIdentifier(input.releaseId);
|
||||
if (!/^[a-z0-9][a-z0-9-]{0,62}$/.test(input.releaseId)) throw Error('Invalid POC release ID');
|
||||
const components = input.components ?? [...pocComponentNames];
|
||||
if (
|
||||
!components.length ||
|
||||
new Set(components).size !== components.length ||
|
||||
components.some(c => !pocComponentNames.includes(c))
|
||||
)
|
||||
throw Error('Invalid POC build components');
|
||||
const adapter = new PocDeploymentAdapter(input.profile, path.join(input.stateDirectory, 'deployments'), command =>
|
||||
execute({ ...command, signal: input.signal }),
|
||||
);
|
||||
const baseline = await adapter.snapshot();
|
||||
if (pocComponentNames.some(c => !baseline.deployments[c].ready))
|
||||
throw Error('POC must be fully ready before a build');
|
||||
const buildDirectory = path.join(input.stateDirectory, 'builds', input.releaseId);
|
||||
// Reusing an ID with an incomplete build is deliberately refused; preserve the evidence for inspection.
|
||||
await mkdir(path.dirname(buildDirectory), { recursive: true, mode: 0o700 });
|
||||
await mkdir(buildDirectory, { mode: 0o700 });
|
||||
const command = async (executable: string, args: string[], timeoutMs = 120_000) => {
|
||||
input.signal?.throwIfAborted();
|
||||
const result = await execute({ executable, args, cwd: input.workspace, timeoutMs, signal: input.signal });
|
||||
if (result.exitCode !== 0)
|
||||
throw Error(`${executable} ${args[0]} failed (${result.exitCode}): ${result.stderr.slice(0, 1000)}`);
|
||||
return result.stdout.trim();
|
||||
};
|
||||
const remote = await command('git', ['remote', 'get-url', 'origin']);
|
||||
if (
|
||||
![
|
||||
input.profile.repository,
|
||||
'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
].includes(remote)
|
||||
)
|
||||
throw Error('Build workspace origin does not match operator POC repository');
|
||||
if ((await command('git', ['rev-parse', 'HEAD'])) !== input.sourceCommit) throw Error('Build workspace HEAD changed');
|
||||
const target = await command('git', ['ls-remote', 'origin', 'refs/heads/poc']);
|
||||
if (target.split(/\s/)[0] !== input.sourceCommit)
|
||||
throw Error('POC build source must match the committed Gitea poc branch HEAD');
|
||||
const archive = path.join(buildDirectory, 'source.tar.gz');
|
||||
await command('git', ['archive', '--format=tar.gz', `--output=${archive}`, input.sourceCommit]);
|
||||
if ((await stat(archive)).size > 512 * 1024 * 1024) throw Error('POC source archive exceeds 512 MiB');
|
||||
const startedAt = new Date().toISOString();
|
||||
const evidence: PocBuildEvidence = {
|
||||
schemaVersion: 1,
|
||||
releaseId: input.releaseId,
|
||||
sourceCommit: input.sourceCommit,
|
||||
profileDigest: profileDigest(input.profile),
|
||||
architecture: 'linux/amd64',
|
||||
archiveSha256: `sha256:${createHash('sha256')
|
||||
.update(await readFile(archive))
|
||||
.digest('hex')}`,
|
||||
startedAt,
|
||||
completedAt: '',
|
||||
components: {},
|
||||
retainedImages: {},
|
||||
bundleDigest: '',
|
||||
};
|
||||
const images = { ...baseline.images };
|
||||
for (const component of pocComponentNames)
|
||||
if (!components.includes(component)) evidence.retainedImages[component] = images[component];
|
||||
await writePocJson(path.join(buildDirectory, 'build.json'), evidence);
|
||||
for (const component of components) {
|
||||
const name = `wishforge-build-${component}-${randomUUID().slice(0, 8)}`;
|
||||
let uid: string | undefined;
|
||||
try {
|
||||
await input.onProgress?.(`Building ${component} from ${input.sourceCommit.slice(0, 12)}`);
|
||||
const podSpec = createPocBuildPod(input.profile, {
|
||||
name,
|
||||
releaseId: input.releaseId,
|
||||
component,
|
||||
sourceCommit: input.sourceCommit,
|
||||
buildTime: startedAt,
|
||||
});
|
||||
const created = JSON.parse(
|
||||
await adapter.kubectl(['create', '-f', '-', '-o', 'json'], JSON.stringify(podSpec)),
|
||||
) as BuildPod;
|
||||
uid = created.metadata.uid;
|
||||
await writePocJson(path.join(buildDirectory, `${component}-pod.json`), {
|
||||
name,
|
||||
uid,
|
||||
sourceCommit: input.sourceCommit,
|
||||
state: 'created',
|
||||
});
|
||||
const deadline = Date.now() + input.profile.buildTimeoutSeconds * 1000;
|
||||
const getPod = async () => {
|
||||
const pod = JSON.parse(await adapter.kubectl(['get', 'pod', name, '-o', 'json'])) as BuildPod;
|
||||
if (pod.metadata.uid !== uid) throw Error('Build Pod UID changed');
|
||||
return pod;
|
||||
};
|
||||
for (;;) {
|
||||
input.signal?.throwIfAborted();
|
||||
const pod = await getPod();
|
||||
if (pod.status?.containerStatuses?.[0]?.state?.running) break;
|
||||
if (pod.status?.phase === 'Failed' || Date.now() > deadline) throw Error(`Build Pod ${name} could not start`);
|
||||
const reason = pod.status?.containerStatuses?.[0]?.state?.waiting?.reason;
|
||||
if (['ErrImagePull', 'ImagePullBackOff', 'CreateContainerConfigError'].includes(reason ?? ''))
|
||||
throw Error(`Build Pod ${name}: ${reason}`);
|
||||
await delay(2000, undefined, { signal: input.signal });
|
||||
}
|
||||
await adapter.kubectl(
|
||||
['cp', archive, `${name}:/context/source.tar.gz`, '-c', 'kaniko', '--retries=2'],
|
||||
undefined,
|
||||
5 * 60_000,
|
||||
);
|
||||
await adapter.kubectl(
|
||||
[
|
||||
'exec',
|
||||
name,
|
||||
'-c',
|
||||
'kaniko',
|
||||
'--',
|
||||
'/busybox/sh',
|
||||
'-c',
|
||||
'mkdir -p /context/source && tar -xzf /context/source.tar.gz -C /context/source && touch /context/ready',
|
||||
],
|
||||
undefined,
|
||||
120_000,
|
||||
);
|
||||
let digest = '';
|
||||
for (;;) {
|
||||
input.signal?.throwIfAborted();
|
||||
const pod = await getPod();
|
||||
const termination = pod.status?.containerStatuses?.[0]?.state?.terminated;
|
||||
if (termination) {
|
||||
if (termination.exitCode !== 0)
|
||||
throw Error(`Build ${component} failed in ${name}; inspect its bounded build log locally`);
|
||||
digest = termination.message?.trim() ?? '';
|
||||
if (!/^sha256:[a-f0-9]{64}$/.test(digest))
|
||||
throw Error('Kaniko completed without an immutable digest receipt');
|
||||
break;
|
||||
}
|
||||
if (Date.now() > deadline || pod.status?.phase === 'Failed')
|
||||
throw Error(`Build ${component} timed out or failed`);
|
||||
await delay(3000, undefined, { signal: input.signal });
|
||||
}
|
||||
images[component] = `${input.profile.registry}/${pocComponents[component].repository}@${digest}`;
|
||||
evidence.components[component] = {
|
||||
image: images[component],
|
||||
pod: name,
|
||||
uid,
|
||||
dockerfile: pocComponents[component].dockerfile,
|
||||
};
|
||||
await writePocJson(path.join(buildDirectory, 'build.json'), evidence);
|
||||
await input.onProgress?.(`Built ${component}: ${digest}`);
|
||||
} finally {
|
||||
if (uid) {
|
||||
// Keep diagnostics, but never print a build log to web clients: dependency scripts may emit credentials.
|
||||
const cleanup = new PocDeploymentAdapter(input.profile, input.stateDirectory, execute);
|
||||
try {
|
||||
const logs = await cleanup.kubectl(['logs', name, '-c', 'kaniko', '--tail=2000']);
|
||||
await writePocJson(path.join(buildDirectory, `${component}-log.json`), { pod: name, uid, logs });
|
||||
const live = JSON.parse(await cleanup.kubectl(['get', 'pod', name, '-o', 'json'])) as BuildPod;
|
||||
if (live.metadata.uid === uid) {
|
||||
// Raw DELETE uses Kubernetes UID preconditions so a replaced Pod cannot be removed accidentally.
|
||||
await cleanup.kubectl(
|
||||
['delete', '--raw', `/api/v1/namespaces/${input.profile.namespace}/pods/${name}`, '-f', '-'],
|
||||
JSON.stringify({ apiVersion: 'v1', kind: 'DeleteOptions', preconditions: { uid } }),
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
/* ActiveDeadlineSeconds bounds leftovers; their UID and name are retained for reconciliation. */
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const release = pocReleaseSchema.parse({
|
||||
schemaVersion: 1,
|
||||
id: input.releaseId,
|
||||
sourceCommit: input.sourceCommit,
|
||||
profileDigest: profileDigest(input.profile),
|
||||
images,
|
||||
expectedImages: baseline.images,
|
||||
expectedTemplateDigests: baseline.templateDigests,
|
||||
rollbackCompatible: input.rollbackCompatible ?? false,
|
||||
});
|
||||
evidence.completedAt = new Date().toISOString();
|
||||
evidence.bundleDigest = pocReleaseDigest(release);
|
||||
await writePocJson(path.join(buildDirectory, 'release.json'), release);
|
||||
await writePocJson(path.join(buildDirectory, 'build.json'), evidence);
|
||||
return { release, evidence };
|
||||
}
|
||||
|
||||
export async function deliverPoc(input: PocBuildInput, execute: CommandExecutor = executeCommand) {
|
||||
const built = await buildPocRelease(input, execute);
|
||||
input.signal?.throwIfAborted();
|
||||
const adapter = new PocDeploymentAdapter(input.profile, path.join(input.stateDirectory, 'deployments'), execute);
|
||||
const deployment = await adapter.deploy(built.release);
|
||||
return { ...built, deployment };
|
||||
}
|
||||
67
packages/runtime/src/poc-profile.ts
Normal file
67
packages/runtime/src/poc-profile.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { z } from 'zod';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { readFile, stat } from 'node:fs/promises';
|
||||
|
||||
export const pocComponentNames = ['api', 'worker', 'coordinator'] as const;
|
||||
export type PocComponent = (typeof pocComponentNames)[number];
|
||||
export const pocComponents = {
|
||||
api: {
|
||||
deployment: 'droboticflow-api',
|
||||
container: 'api',
|
||||
dockerfile: 'Dockerfile.api',
|
||||
repository: 'droboticflow-api',
|
||||
},
|
||||
worker: {
|
||||
deployment: 'droboticflow-worker',
|
||||
container: 'worker',
|
||||
dockerfile: 'Dockerfile.worker',
|
||||
repository: 'droboticflow-worker',
|
||||
},
|
||||
coordinator: {
|
||||
deployment: 'drosync-coordinator',
|
||||
container: 'coordinator',
|
||||
dockerfile: 'Dockerfile.drosync-coordinator',
|
||||
repository: 'drosync-coordinator',
|
||||
},
|
||||
} as const;
|
||||
|
||||
const absolutePath = z
|
||||
.string()
|
||||
.startsWith('/')
|
||||
.refine(v => !/[\x00\r\n]/.test(v));
|
||||
const uid = z.string().uuid();
|
||||
export const pocProfileSchema = z
|
||||
.object({
|
||||
schemaVersion: z.literal(1),
|
||||
id: z.literal('droboticflow-poc'),
|
||||
repository: z.enum([
|
||||
'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
]),
|
||||
branch: z.literal('poc'),
|
||||
kubeconfig: absolutePath,
|
||||
clusterServer: z.literal('https://120.48.181.105:6443'),
|
||||
namespace: z.literal('droboticflow'),
|
||||
namespaceUid: uid,
|
||||
deployments: z.object({ api: uid, worker: uid, coordinator: uid }).strict(),
|
||||
registry: z.literal('ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc'),
|
||||
registrySecret: z.literal('droboticflow-registry'),
|
||||
applicationUrl: z.literal('http://120.48.164.19'),
|
||||
kanikoImage: z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:/-]*@sha256:[a-f0-9]{64}$/),
|
||||
registryMirrors: z.array(z.string().regex(/^[a-z0-9][a-z0-9.-]+(?::[0-9]+)?$/)).default(['mirror.gcr.io']),
|
||||
rolloutTimeoutSeconds: z.number().int().min(30).max(1800).default(600),
|
||||
buildTimeoutSeconds: z.number().int().min(60).max(3600).default(1800),
|
||||
})
|
||||
.strict();
|
||||
export type PocProfile = z.infer<typeof pocProfileSchema>;
|
||||
export function profileDigest(profile: PocProfile): string {
|
||||
return `sha256:${createHash('sha256')
|
||||
.update(JSON.stringify(pocProfileSchema.parse(profile)))
|
||||
.digest('hex')}`;
|
||||
}
|
||||
export async function loadPocProfile(file: string): Promise<PocProfile> {
|
||||
const info = await stat(file);
|
||||
if (!info.isFile() || (info.mode & 0o022) !== 0 || (process.getuid && info.uid !== process.getuid()))
|
||||
throw Error('POC profile must be an operator-owned file without group/world write permission');
|
||||
return pocProfileSchema.parse(JSON.parse(await readFile(file, 'utf8')));
|
||||
}
|
||||
521
packages/runtime/src/poc-release.ts
Normal file
521
packages/runtime/src/poc-release.ts
Normal file
@ -0,0 +1,521 @@
|
||||
import { mkdir, open, readFile, rename, unlink } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { canonicalJson } from '@wishforge/contracts';
|
||||
import { z } from 'zod';
|
||||
import { executeCommand, type CommandExecutor } from './process.js';
|
||||
import {
|
||||
pocComponentNames,
|
||||
pocComponents,
|
||||
pocProfileSchema,
|
||||
profileDigest,
|
||||
type PocComponent,
|
||||
type PocProfile,
|
||||
} from './poc-profile.js';
|
||||
|
||||
const digestImage = z.string().regex(/^[a-zA-Z0-9][a-zA-Z0-9._:/-]*@sha256:[a-f0-9]{64}$/);
|
||||
const imageSet = z.object({ api: digestImage, worker: digestImage, coordinator: digestImage }).strict();
|
||||
const templateDigestSet = z
|
||||
.object({
|
||||
api: z.string().regex(/^sha256:[a-f0-9]{64}$/),
|
||||
worker: z.string().regex(/^sha256:[a-f0-9]{64}$/),
|
||||
coordinator: z.string().regex(/^sha256:[a-f0-9]{64}$/),
|
||||
})
|
||||
.strict();
|
||||
export const pocReleaseSchema = z
|
||||
.object({
|
||||
schemaVersion: z.literal(1),
|
||||
id: z.string().regex(/^[a-z0-9][a-z0-9-]{0,62}$/),
|
||||
sourceCommit: z.string().regex(/^[a-f0-9]{40}$/),
|
||||
profileDigest: z.string().regex(/^sha256:[a-f0-9]{64}$/),
|
||||
images: imageSet,
|
||||
expectedImages: imageSet,
|
||||
expectedTemplateDigests: templateDigestSet.optional(),
|
||||
rollbackCompatible: z.boolean(),
|
||||
})
|
||||
.strict();
|
||||
export type PocRelease = z.infer<typeof pocReleaseSchema>;
|
||||
export type PocImages = z.infer<typeof imageSet>;
|
||||
|
||||
interface Deployment {
|
||||
metadata: { uid: string; resourceVersion: string; generation: number; annotations?: Record<string, string> };
|
||||
spec: {
|
||||
replicas?: number;
|
||||
selector: { matchLabels: Record<string, string> };
|
||||
template: {
|
||||
metadata?: { annotations?: Record<string, string> };
|
||||
spec: { containers: { name: string; image: string }[] };
|
||||
};
|
||||
};
|
||||
status?: {
|
||||
observedGeneration?: number;
|
||||
replicas?: number;
|
||||
updatedReplicas?: number;
|
||||
readyReplicas?: number;
|
||||
availableReplicas?: number;
|
||||
unavailableReplicas?: number;
|
||||
};
|
||||
}
|
||||
export interface PocSnapshot {
|
||||
images: PocImages;
|
||||
templateDigests: z.infer<typeof templateDigestSet>;
|
||||
deployments: Record<PocComponent, { uid: string; resourceVersion: string; generation: number; ready: boolean }>;
|
||||
}
|
||||
export type PocJournalState =
|
||||
'prepared' | 'applying' | 'verifying' | 'succeeded' | 'rolling_back' | 'rolled_back' | 'needs_reconciliation';
|
||||
export interface PocJournal {
|
||||
schemaVersion: 1;
|
||||
release: PocRelease;
|
||||
state: PocJournalState;
|
||||
startedAt: string;
|
||||
updatedAt: string;
|
||||
applied: PocComponent[];
|
||||
pending?: PocComponent;
|
||||
error?: string;
|
||||
verification?: { checkedAt: string; checks: string[] };
|
||||
}
|
||||
const annotationPrefix = 'wishforge.d-robotics.cc';
|
||||
const escapePointer = (value: string) => value.replaceAll('~', '~0').replaceAll('/', '~1');
|
||||
|
||||
export function pocTemplateDigest(component: PocComponent, item: Deployment): string {
|
||||
const spec = structuredClone(item.spec);
|
||||
for (const container of spec.template.spec.containers)
|
||||
if (container.name === pocComponents[component].container) container.image = '<release-image>';
|
||||
const annotations = spec.template.metadata?.annotations;
|
||||
if (annotations) {
|
||||
for (const key of Object.keys(annotations)) if (key.startsWith(`${annotationPrefix}/`)) delete annotations[key];
|
||||
if (!Object.keys(annotations).length) delete spec.template.metadata!.annotations;
|
||||
}
|
||||
if (spec.template.metadata && !Object.keys(spec.template.metadata).length) delete spec.template.metadata;
|
||||
return `sha256:${createHash('sha256').update(canonicalJson(spec)).digest('hex')}`;
|
||||
}
|
||||
|
||||
export async function writePocJson(file: string, value: unknown): Promise<void> {
|
||||
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
||||
const temporary = `${file}.${process.pid}.${crypto.randomUUID()}.tmp`;
|
||||
const handle = await open(temporary, 'wx', 0o600);
|
||||
try {
|
||||
await handle.writeFile(JSON.stringify(value, null, 2) + '\n');
|
||||
await handle.sync();
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
await rename(temporary, file);
|
||||
const directory = await open(path.dirname(file), 'r');
|
||||
try {
|
||||
await directory.sync();
|
||||
} finally {
|
||||
await directory.close();
|
||||
}
|
||||
}
|
||||
|
||||
/** Adoption updates only fixed container images and audit annotations, guarded by UID/RV/image tests. */
|
||||
export class PocDeploymentAdapter {
|
||||
readonly profile: PocProfile;
|
||||
constructor(
|
||||
profile: PocProfile,
|
||||
readonly stateDirectory: string,
|
||||
readonly execute: CommandExecutor = executeCommand,
|
||||
readonly fetcher: typeof fetch = fetch,
|
||||
) {
|
||||
this.profile = pocProfileSchema.parse(profile);
|
||||
}
|
||||
async kubectl(args: string[], input?: string, timeoutMs = 30_000): Promise<string> {
|
||||
const result = await this.execute({
|
||||
executable: 'kubectl',
|
||||
args: [
|
||||
'--kubeconfig',
|
||||
this.profile.kubeconfig,
|
||||
`--request-timeout=${Math.ceil(timeoutMs / 1000)}s`,
|
||||
'-n',
|
||||
this.profile.namespace,
|
||||
...args,
|
||||
],
|
||||
input,
|
||||
timeoutMs,
|
||||
});
|
||||
if (result.exitCode !== 0)
|
||||
throw Error(`kubectl ${args[0]} failed (${result.exitCode}): ${result.stderr.slice(0, 1000)}`);
|
||||
return result.stdout.trim();
|
||||
}
|
||||
async assertIdentity(): Promise<void> {
|
||||
const config = JSON.parse(await this.kubectl(['config', 'view', '--minify', '-o', 'json'])) as {
|
||||
clusters: { cluster: { server: string } }[];
|
||||
};
|
||||
if (config.clusters.length !== 1 || config.clusters[0]?.cluster.server !== this.profile.clusterServer)
|
||||
throw Error('POC cluster identity changed');
|
||||
const ns = JSON.parse(await this.kubectl(['get', 'namespace', this.profile.namespace, '-o', 'json'])) as {
|
||||
metadata: { uid: string };
|
||||
};
|
||||
if (ns.metadata.uid !== this.profile.namespaceUid) throw Error('POC namespace UID changed');
|
||||
}
|
||||
private async deployment(component: PocComponent): Promise<Deployment> {
|
||||
const item = JSON.parse(
|
||||
await this.kubectl(['get', 'deployment', pocComponents[component].deployment, '-o', 'json']),
|
||||
) as Deployment;
|
||||
if (item.metadata.uid !== this.profile.deployments[component])
|
||||
throw Error(`POC ${component} Deployment UID changed`);
|
||||
if (!item.spec.template.spec.containers.some(c => c.name === pocComponents[component].container))
|
||||
throw Error(`POC ${component} container missing`);
|
||||
return item;
|
||||
}
|
||||
private image(component: PocComponent, item: Deployment): string {
|
||||
return item.spec.template.spec.containers.find(c => c.name === pocComponents[component].container)!.image;
|
||||
}
|
||||
private ready(item: Deployment): boolean {
|
||||
const count = item.spec.replicas ?? 1;
|
||||
return (
|
||||
count > 0 &&
|
||||
(item.status?.observedGeneration ?? 0) >= item.metadata.generation &&
|
||||
item.status?.updatedReplicas === count &&
|
||||
item.status.readyReplicas === count &&
|
||||
item.status.availableReplicas === count &&
|
||||
item.status.replicas === count &&
|
||||
!item.status.unavailableReplicas
|
||||
);
|
||||
}
|
||||
async snapshot(): Promise<PocSnapshot> {
|
||||
await this.assertIdentity();
|
||||
const images = {} as PocImages;
|
||||
const templateDigests = {} as PocSnapshot['templateDigests'];
|
||||
const deployments = {} as PocSnapshot['deployments'];
|
||||
for (const component of pocComponentNames) {
|
||||
const item = await this.deployment(component);
|
||||
images[component] = digestImage.parse(this.image(component, item));
|
||||
templateDigests[component] = pocTemplateDigest(component, item);
|
||||
deployments[component] = {
|
||||
uid: item.metadata.uid,
|
||||
resourceVersion: item.metadata.resourceVersion,
|
||||
generation: item.metadata.generation,
|
||||
ready: this.ready(item),
|
||||
};
|
||||
}
|
||||
return { images, templateDigests, deployments };
|
||||
}
|
||||
private assertTemplates(release: PocRelease, snapshot: PocSnapshot): void {
|
||||
if (
|
||||
release.expectedTemplateDigests &&
|
||||
pocComponentNames.some(c => release.expectedTemplateDigests![c] !== snapshot.templateDigests[c])
|
||||
)
|
||||
throw Error('POC deployment configuration changed; reconciliation required');
|
||||
}
|
||||
private validate(release: PocRelease): void {
|
||||
pocReleaseSchema.parse(release);
|
||||
if (release.profileDigest !== profileDigest(this.profile)) throw Error('Release profile digest mismatch');
|
||||
for (const component of pocComponentNames) {
|
||||
const prefix = `${this.profile.registry}/${pocComponents[component].repository}@sha256:`;
|
||||
if (!release.images[component].startsWith(prefix) || !release.expectedImages[component].startsWith(prefix))
|
||||
throw Error(`POC ${component} image repository mismatch`);
|
||||
}
|
||||
}
|
||||
journalPath(id: string): string {
|
||||
if (!/^[a-z0-9][a-z0-9-]{0,62}$/.test(id)) throw Error('Invalid release ID');
|
||||
return path.join(this.stateDirectory, `${id}.json`);
|
||||
}
|
||||
private async persist(journal: PocJournal): Promise<void> {
|
||||
journal.updatedAt = new Date().toISOString();
|
||||
await writePocJson(this.journalPath(journal.release.id), journal);
|
||||
}
|
||||
private async locked<T>(run: () => Promise<T>): Promise<T> {
|
||||
await mkdir(this.stateDirectory, { recursive: true, mode: 0o700 });
|
||||
const lockPath = path.join(this.stateDirectory, 'poc-deployment.lock');
|
||||
const lock = await open(lockPath, 'wx', 0o600).catch(() => {
|
||||
throw Error('POC deployment locked; inspect the journal and reconcile before clearing a stale lock');
|
||||
});
|
||||
try {
|
||||
await lock.writeFile(JSON.stringify({ pid: process.pid, startedAt: new Date().toISOString() }));
|
||||
await lock.sync();
|
||||
return await run();
|
||||
} finally {
|
||||
await lock.close();
|
||||
await unlink(lockPath);
|
||||
}
|
||||
}
|
||||
private async patch(
|
||||
component: PocComponent,
|
||||
item: Deployment,
|
||||
expectedImage: string,
|
||||
nextImage: string,
|
||||
release: PocRelease,
|
||||
rollback = false,
|
||||
): Promise<void> {
|
||||
const containers = item.spec.template.spec.containers;
|
||||
const index = containers.findIndex(c => c.name === pocComponents[component].container);
|
||||
const annotationPath = '/spec/template/metadata/annotations';
|
||||
const patches: unknown[] = [
|
||||
{ op: 'test', path: '/metadata/uid', value: this.profile.deployments[component] },
|
||||
{ op: 'test', path: '/metadata/resourceVersion', value: item.metadata.resourceVersion },
|
||||
{ op: 'test', path: `/spec/template/spec/containers/${index}/name`, value: pocComponents[component].container },
|
||||
{ op: 'test', path: `/spec/template/spec/containers/${index}/image`, value: expectedImage },
|
||||
{ op: 'replace', path: `/spec/template/spec/containers/${index}/image`, value: nextImage },
|
||||
];
|
||||
if (!item.spec.template.metadata) patches.push({ op: 'add', path: '/spec/template/metadata', value: {} });
|
||||
if (!item.spec.template.metadata?.annotations) patches.push({ op: 'add', path: annotationPath, value: {} });
|
||||
for (const [name, value] of Object.entries({
|
||||
'release-id': release.id,
|
||||
'source-commit': rollback ? 'unknown-restored-baseline' : release.sourceCommit,
|
||||
'attempted-source-commit': release.sourceCommit,
|
||||
'profile-digest': release.profileDigest,
|
||||
operation: rollback ? 'rollback' : 'deploy',
|
||||
}))
|
||||
patches.push({ op: 'add', path: `${annotationPath}/${escapePointer(`${annotationPrefix}/${name}`)}`, value });
|
||||
await this.kubectl([
|
||||
'patch',
|
||||
'deployment',
|
||||
pocComponents[component].deployment,
|
||||
'--type=json',
|
||||
'-p',
|
||||
JSON.stringify(patches),
|
||||
]);
|
||||
}
|
||||
private releaseOwns(item: Deployment, release: PocRelease): boolean {
|
||||
return item.spec.template.metadata?.annotations?.[`${annotationPrefix}/release-id`] === release.id;
|
||||
}
|
||||
private async verify(
|
||||
images: PocImages,
|
||||
templateDigests?: PocRelease['expectedTemplateDigests'],
|
||||
expectedApiSourceCommit?: string,
|
||||
): Promise<{ checkedAt: string; checks: string[] }> {
|
||||
for (const component of pocComponentNames) {
|
||||
await this.kubectl(
|
||||
[
|
||||
'rollout',
|
||||
'status',
|
||||
`deployment/${pocComponents[component].deployment}`,
|
||||
`--timeout=${this.profile.rolloutTimeoutSeconds}s`,
|
||||
],
|
||||
undefined,
|
||||
(this.profile.rolloutTimeoutSeconds + 30) * 1000,
|
||||
);
|
||||
const item = await this.deployment(component);
|
||||
if (templateDigests && pocTemplateDigest(component, item) !== templateDigests[component])
|
||||
throw Error(`POC ${component} configuration changed during verification`);
|
||||
if (this.image(component, item) !== images[component] || !this.ready(item))
|
||||
throw Error(`POC ${component} has not converged to the expected ready image`);
|
||||
const selector = Object.entries(item.spec.selector.matchLabels)
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join(',');
|
||||
const pods = JSON.parse(await this.kubectl(['get', 'pods', '-l', selector, '-o', 'json'])) as {
|
||||
items: {
|
||||
metadata: { deletionTimestamp?: string };
|
||||
spec: { containers: { name: string; image: string }[] };
|
||||
status?: { containerStatuses?: { name: string; ready: boolean; imageID?: string }[] };
|
||||
}[];
|
||||
};
|
||||
const running = pods.items.filter(p => !p.metadata.deletionTimestamp);
|
||||
if (
|
||||
running.length !== (item.spec.replicas ?? 1) ||
|
||||
running.some(p => {
|
||||
const status = p.status?.containerStatuses?.find(c => c.name === pocComponents[component].container);
|
||||
return (
|
||||
!status?.ready ||
|
||||
!status.imageID?.endsWith(images[component].split('@')[1]!) ||
|
||||
p.spec.containers.find(c => c.name === pocComponents[component].container)?.image !== images[component]
|
||||
);
|
||||
})
|
||||
)
|
||||
throw Error(`POC ${component} Pod digest/readiness mismatch`);
|
||||
}
|
||||
for (const endpoint of ['/api/health', '/api/ready', '/']) {
|
||||
const response = await this.fetcher(new URL(endpoint, this.profile.applicationUrl), {
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
redirect: 'error',
|
||||
});
|
||||
if (response.status !== 200) throw Error(`POC ${endpoint} returned ${response.status}`);
|
||||
if (endpoint === '/api/health' && expectedApiSourceCommit) {
|
||||
const health = (await response.json()) as { buildCommit?: string };
|
||||
if (health.buildCommit !== expectedApiSourceCommit)
|
||||
throw Error('POC public API source commit does not match the deployed artifact');
|
||||
} else await response.body?.cancel();
|
||||
}
|
||||
return {
|
||||
checkedAt: new Date().toISOString(),
|
||||
checks: [
|
||||
'three-deployment-rollouts',
|
||||
'pod-digests',
|
||||
'api-health',
|
||||
'api-ready',
|
||||
'application-entry',
|
||||
...(expectedApiSourceCommit ? ['api-build-commit'] : []),
|
||||
],
|
||||
};
|
||||
}
|
||||
private async rollback(journal: PocJournal): Promise<void> {
|
||||
this.assertTemplates(journal.release, await this.snapshot());
|
||||
journal.state = 'rolling_back';
|
||||
await this.persist(journal);
|
||||
for (const component of [...pocComponentNames].reverse()) {
|
||||
if (journal.release.images[component] === journal.release.expectedImages[component]) continue;
|
||||
const item = await this.deployment(component);
|
||||
const current = this.image(component, item);
|
||||
if (current === journal.release.expectedImages[component]) continue;
|
||||
if (current !== journal.release.images[component] || !this.releaseOwns(item, journal.release))
|
||||
throw Error(`POC ${component} changed externally; automatic rollback stopped`);
|
||||
journal.pending = component;
|
||||
await this.persist(journal);
|
||||
await this.patch(component, item, current, journal.release.expectedImages[component], journal.release, true);
|
||||
delete journal.pending;
|
||||
await this.persist(journal);
|
||||
}
|
||||
journal.verification = await this.verify(journal.release.expectedImages, journal.release.expectedTemplateDigests);
|
||||
journal.state = 'rolled_back';
|
||||
await this.persist(journal);
|
||||
}
|
||||
async deploy(input: PocRelease, protectedVerification?: () => Promise<void>): Promise<PocJournal> {
|
||||
const release = pocReleaseSchema.parse(input);
|
||||
this.validate(release);
|
||||
return this.locked(async () => {
|
||||
let journal: PocJournal;
|
||||
try {
|
||||
journal = JSON.parse(await readFile(this.journalPath(release.id), 'utf8')) as PocJournal;
|
||||
if (JSON.stringify(journal.release) !== JSON.stringify(release))
|
||||
throw Error('Release ID already exists with different content');
|
||||
if (journal.state === 'succeeded') {
|
||||
await this.assertIdentity();
|
||||
await this.verify(
|
||||
release.images,
|
||||
release.expectedTemplateDigests,
|
||||
release.images.api !== release.expectedImages.api ? release.sourceCommit : undefined,
|
||||
);
|
||||
await protectedVerification?.();
|
||||
return journal;
|
||||
}
|
||||
if (journal.state === 'rolled_back') return journal;
|
||||
throw Error('Incomplete release requires reconcile; it will not be deployed twice');
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
|
||||
journal = {
|
||||
schemaVersion: 1,
|
||||
release,
|
||||
state: 'prepared',
|
||||
startedAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
applied: [],
|
||||
};
|
||||
}
|
||||
const before = await this.snapshot();
|
||||
this.assertTemplates(release, before);
|
||||
if (pocComponentNames.some(c => before.images[c] !== release.expectedImages[c] || !before.deployments[c].ready))
|
||||
throw Error('POC baseline changed or is not fully ready; rebuild the release plan');
|
||||
await this.persist(journal);
|
||||
try {
|
||||
journal.state = 'applying';
|
||||
await this.persist(journal);
|
||||
// Existing API supports mixed versions; incompatible API/schema changes are rejected by the caller's profile policy.
|
||||
for (const component of ['coordinator', 'worker', 'api'] as const) {
|
||||
if (release.images[component] === release.expectedImages[component]) continue;
|
||||
journal.pending = component;
|
||||
await this.persist(journal);
|
||||
const item = await this.deployment(component);
|
||||
if (
|
||||
release.expectedTemplateDigests &&
|
||||
pocTemplateDigest(component, item) !== release.expectedTemplateDigests[component]
|
||||
)
|
||||
throw Error(`POC ${component} configuration changed before update`);
|
||||
if (this.image(component, item) !== release.expectedImages[component])
|
||||
throw Error(`POC ${component} changed before update`);
|
||||
await this.patch(component, item, release.expectedImages[component], release.images[component], release);
|
||||
journal.applied.push(component);
|
||||
delete journal.pending;
|
||||
await this.persist(journal);
|
||||
}
|
||||
journal.state = 'verifying';
|
||||
await this.persist(journal);
|
||||
journal.verification = await this.verify(
|
||||
release.images,
|
||||
release.expectedTemplateDigests,
|
||||
release.images.api !== release.expectedImages.api ? release.sourceCommit : undefined,
|
||||
);
|
||||
if (protectedVerification) {
|
||||
await protectedVerification();
|
||||
journal.verification.checks.push('protected-business-smoke');
|
||||
}
|
||||
journal.state = 'succeeded';
|
||||
await this.persist(journal);
|
||||
} catch (error) {
|
||||
journal.error = error instanceof Error ? error.message : String(error);
|
||||
// A failed write can still have committed server-side. Rollback first reads every image + release ownership.
|
||||
if (release.rollbackCompatible) {
|
||||
try {
|
||||
await this.rollback(journal);
|
||||
} catch (rollbackError) {
|
||||
journal.error += `; rollback: ${String(rollbackError)}`;
|
||||
journal.state = 'needs_reconciliation';
|
||||
await this.persist(journal);
|
||||
}
|
||||
} else {
|
||||
journal.state = 'needs_reconciliation';
|
||||
await this.persist(journal);
|
||||
}
|
||||
}
|
||||
return journal;
|
||||
});
|
||||
}
|
||||
/** Explicit recovery after an interrupted process; observes current state before any compensating update. */
|
||||
async reconcile(
|
||||
id: string,
|
||||
expectedRelease?: PocRelease,
|
||||
protectedVerification?: () => Promise<void>,
|
||||
): Promise<PocJournal> {
|
||||
return this.locked(async () => {
|
||||
const journal = JSON.parse(await readFile(this.journalPath(id), 'utf8')) as PocJournal;
|
||||
this.validate(journal.release);
|
||||
if (expectedRelease && canonicalJson(expectedRelease) !== canonicalJson(journal.release))
|
||||
throw Error('Reconcile release does not match the persisted signed release');
|
||||
await this.assertIdentity();
|
||||
const actual = await this.snapshot();
|
||||
try {
|
||||
this.assertTemplates(journal.release, actual);
|
||||
} catch (error) {
|
||||
journal.state = 'needs_reconciliation';
|
||||
journal.error = String(error);
|
||||
await this.persist(journal);
|
||||
return journal;
|
||||
}
|
||||
if (pocComponentNames.every(c => actual.images[c] === journal.release.images[c])) {
|
||||
try {
|
||||
journal.verification = await this.verify(
|
||||
journal.release.images,
|
||||
journal.release.expectedTemplateDigests,
|
||||
journal.release.images.api !== journal.release.expectedImages.api
|
||||
? journal.release.sourceCommit
|
||||
: undefined,
|
||||
);
|
||||
if (protectedVerification) {
|
||||
await protectedVerification();
|
||||
journal.verification.checks.push('protected-business-smoke');
|
||||
}
|
||||
journal.state = 'succeeded';
|
||||
delete journal.pending;
|
||||
await this.persist(journal);
|
||||
} catch (error) {
|
||||
journal.error = String(error);
|
||||
if (journal.release.rollbackCompatible) {
|
||||
try {
|
||||
await this.rollback(journal);
|
||||
} catch (rollbackError) {
|
||||
journal.error += `; rollback: ${String(rollbackError)}`;
|
||||
journal.state = 'needs_reconciliation';
|
||||
await this.persist(journal);
|
||||
}
|
||||
} else {
|
||||
journal.state = 'needs_reconciliation';
|
||||
await this.persist(journal);
|
||||
}
|
||||
}
|
||||
} else if (journal.release.rollbackCompatible) {
|
||||
try {
|
||||
await this.rollback(journal);
|
||||
} catch (error) {
|
||||
journal.state = 'needs_reconciliation';
|
||||
journal.error = String(error);
|
||||
await this.persist(journal);
|
||||
}
|
||||
} else {
|
||||
journal.state = 'needs_reconciliation';
|
||||
await this.persist(journal);
|
||||
}
|
||||
return journal;
|
||||
});
|
||||
}
|
||||
}
|
||||
162
packages/runtime/src/poc-runner.ts
Normal file
162
packages/runtime/src/poc-runner.ts
Normal file
@ -0,0 +1,162 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
import { canonicalJson } from '@wishforge/contracts';
|
||||
import { verifyHttp } from './deploy.js';
|
||||
import { executeCommand, type CommandExecutor } from './process.js';
|
||||
import { PocDeploymentAdapter, pocReleaseSchema, type PocJournal, type PocRelease } from './poc-release.js';
|
||||
import { profileDigest, type PocProfile } from './poc-profile.js';
|
||||
import type { DeploymentAdapter, DeploymentResult, ReleaseManifest } from './types.js';
|
||||
|
||||
export function pocReleaseDigest(release: PocRelease): string {
|
||||
return `sha256:${createHash('sha256')
|
||||
.update(canonicalJson(pocReleaseSchema.parse(release)))
|
||||
.digest('hex')}`;
|
||||
}
|
||||
|
||||
/** Runner's signature/tenant/environment binding remains unchanged; only the fixed adopted POC target differs. */
|
||||
export class PocRunnerDeploymentAdapter implements DeploymentAdapter {
|
||||
private readonly adapter: PocDeploymentAdapter;
|
||||
constructor(
|
||||
private readonly profile: PocProfile,
|
||||
stateDirectory: string,
|
||||
execute: CommandExecutor = executeCommand,
|
||||
private readonly fetcher: typeof fetch = fetch,
|
||||
) {
|
||||
this.adapter = new PocDeploymentAdapter(profile, stateDirectory, execute, fetcher);
|
||||
}
|
||||
private manifest(release: ReleaseManifest): PocRelease {
|
||||
const inner = pocReleaseSchema.parse((release as ReleaseManifest & { pocRelease?: unknown }).pocRelease);
|
||||
if (!inner.expectedTemplateDigests)
|
||||
throw Error('Signed POC releases require complete baseline configuration fingerprints');
|
||||
if (
|
||||
inner.sourceCommit !== release.sourceCommit ||
|
||||
inner.images.api !== release.image ||
|
||||
inner.profileDigest !== profileDigest(this.profile)
|
||||
)
|
||||
throw Error('Signed POC release does not match its source, API image or operator profile');
|
||||
if (
|
||||
release.applicationUrl !== this.profile.applicationUrl ||
|
||||
release.architecture !== 'linux/amd64' ||
|
||||
release.containerPort !== 3001 ||
|
||||
release.healthPath !== '/api/health'
|
||||
)
|
||||
throw Error('Signed POC release delivery target differs from the operator profile');
|
||||
if (release.secretVersionRefs?.length || Object.keys(release.config ?? {}).length)
|
||||
throw Error('Adopted POC releases preserve existing configuration and secrets');
|
||||
if (
|
||||
!release.smokeChecks?.length ||
|
||||
release.smokeChecks.some(
|
||||
check => !check.path.startsWith('/') || check.path.startsWith('//') || /[\r\n?#\\]/.test(check.path),
|
||||
)
|
||||
)
|
||||
throw Error('POC release requires safe same-origin protected business checks');
|
||||
return inner;
|
||||
}
|
||||
render(release: ReleaseManifest): string {
|
||||
const inner = this.manifest(release);
|
||||
return JSON.stringify(
|
||||
{
|
||||
strategy: 'adopt-existing-poc',
|
||||
namespace: this.profile.namespace,
|
||||
release: inner,
|
||||
permittedMutations: ['named-container-images', 'wishforge-audit-annotations'],
|
||||
},
|
||||
null,
|
||||
2,
|
||||
);
|
||||
}
|
||||
private async verifyBusiness(release: ReleaseManifest): Promise<void> {
|
||||
const http = await verifyHttp(release, this.profile.applicationUrl, this.fetcher);
|
||||
if (!http.passed) throw Error(http.reason ?? 'POC protected business smoke failed');
|
||||
}
|
||||
private async result(release: ReleaseManifest, journal: PocJournal): Promise<DeploymentResult> {
|
||||
if (journal.state !== 'succeeded')
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: journal.state === 'needs_reconciliation' ? 'applied' : 'failed',
|
||||
evidence: {
|
||||
strategy: 'adopt-existing-poc',
|
||||
journalState: journal.state,
|
||||
reason: journal.error,
|
||||
rolledBack: journal.state === 'rolled_back',
|
||||
identityMatched: false,
|
||||
trafficSwitched: false,
|
||||
pocRelease: journal.release,
|
||||
bundleDigest: pocReleaseDigest(journal.release),
|
||||
},
|
||||
};
|
||||
const http = await verifyHttp(release, this.profile.applicationUrl, this.fetcher);
|
||||
if (!http.passed)
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: 'applied',
|
||||
evidence: {
|
||||
identityMatched: true,
|
||||
trafficSwitched: true,
|
||||
...http,
|
||||
journalState: journal.state,
|
||||
pocRelease: journal.release,
|
||||
bundleDigest: pocReleaseDigest(journal.release),
|
||||
},
|
||||
};
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: 'verified',
|
||||
evidence: {
|
||||
identityMatched: true,
|
||||
trafficSwitched: true,
|
||||
strategy: 'adopt-existing-poc',
|
||||
checks: [...http.checks, ...(journal.verification?.checks ?? []), 'application-url', 'public-access'],
|
||||
deliveryUrl: this.profile.applicationUrl,
|
||||
accessEvidence: {
|
||||
applicationUrl: this.profile.applicationUrl,
|
||||
stableOrigin: new URL(this.profile.applicationUrl).origin,
|
||||
checkedAt: new Date().toISOString(),
|
||||
checks: ['application-url', 'public-access'],
|
||||
},
|
||||
pocRelease: journal.release,
|
||||
bundleDigest: pocReleaseDigest(journal.release),
|
||||
journalState: journal.state,
|
||||
},
|
||||
};
|
||||
}
|
||||
async deploy(release: ReleaseManifest, options: { execute?: boolean } = {}): Promise<DeploymentResult> {
|
||||
const inner = this.manifest(release);
|
||||
if (!options.execute)
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: 'dry-run',
|
||||
rendered: this.render(release),
|
||||
evidence: { executed: false, strategy: 'adopt-existing-poc' },
|
||||
};
|
||||
try {
|
||||
return await this.result(release, await this.adapter.deploy(inner, () => this.verifyBusiness(release)));
|
||||
} catch (error) {
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: 'conflict',
|
||||
evidence: { reason: String(error), identityMatched: false, trafficSwitched: false },
|
||||
};
|
||||
}
|
||||
}
|
||||
async reconcile(release: ReleaseManifest): Promise<DeploymentResult> {
|
||||
const inner = this.manifest(release);
|
||||
try {
|
||||
return await this.result(
|
||||
release,
|
||||
await this.adapter.reconcile(inner.id, inner, () => this.verifyBusiness(release)),
|
||||
);
|
||||
} catch (error) {
|
||||
return {
|
||||
releaseId: release.id,
|
||||
target: 'kubernetes',
|
||||
status: (error as NodeJS.ErrnoException).code === 'ENOENT' ? 'not-found' : 'conflict',
|
||||
evidence: { reason: String(error), identityMatched: false, trafficSwitched: false },
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { access, mkdir } from 'node:fs/promises';
|
||||
import { access, mkdir, readFile } from 'node:fs/promises';
|
||||
import { constants } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { StringDecoder } from 'node:string_decoder';
|
||||
import type { ProviderId } from '@wishforge/contracts';
|
||||
import { AGENT_GIT_IDENTITY, ManagedProvider } from './base.js';
|
||||
import { RuntimeError, type ProviderStart } from '../types.js';
|
||||
@ -11,11 +13,12 @@ interface CliDialect {
|
||||
label: string;
|
||||
/** Credential variables it may receive; nothing else from the worker's environment is passed. */
|
||||
credentialVariable: RegExp;
|
||||
args(input: ProviderStart, allowedTools?: string[]): string[];
|
||||
args(input: ProviderStart, allowedTools?: string[], nativeHost?: boolean): string[];
|
||||
}
|
||||
const options = (input: ProviderStart, allowedTools?: string[]) => [
|
||||
...(allowedTools?.length ? ['--allowedTools', allowedTools.join(',')] : []),
|
||||
...(input.resumeSessionId ? ['--resume', input.resumeSessionId] : []),
|
||||
...(input.model ? ['--model', input.model] : []),
|
||||
];
|
||||
const CODEBUDDY: CliDialect = {
|
||||
provider: 'codebuddy',
|
||||
@ -29,15 +32,26 @@ const CLAUDE_CODE: CliDialect = {
|
||||
credentialVariable: /^ANTHROPIC_[A-Z0-9_]+$/,
|
||||
// Print mode streams JSON only with --verbose. Permissions are bypassed because the CLI runs
|
||||
// inside the per-run sandbox, like CodeBuddy's -y.
|
||||
args: (input, tools) => [
|
||||
args: (input, tools, nativeHost) => [
|
||||
'-p',
|
||||
input.prompt,
|
||||
'--output-format',
|
||||
'stream-json',
|
||||
'--verbose',
|
||||
'--permission-mode',
|
||||
'bypassPermissions',
|
||||
...options(input, tools),
|
||||
nativeHost ? 'dontAsk' : 'bypassPermissions',
|
||||
...(nativeHost
|
||||
? [
|
||||
'--safe-mode',
|
||||
'--restricted',
|
||||
'--strict-mcp-config',
|
||||
'--mcp-config',
|
||||
'{"mcpServers":{}}',
|
||||
'--tools',
|
||||
LOCAL_FILE_TOOLS.join(','),
|
||||
]
|
||||
: []),
|
||||
...options(input, nativeHost ? LOCAL_FILE_TOOLS : tools),
|
||||
],
|
||||
};
|
||||
|
||||
@ -50,6 +64,27 @@ export interface CodeBuddyConfig {
|
||||
timeoutMs?: number;
|
||||
allowedTools?: string[];
|
||||
}
|
||||
const LOCAL_FILE_TOOLS = ['Read', 'Edit', 'Write', 'Glob', 'Grep'];
|
||||
export interface ClaudeCodeConfig extends CodeBuddyConfig {
|
||||
/** Operator-only opt-in. Native login is never available to production sandboxes. */
|
||||
localAuth?: { home: string; developmentHost: true };
|
||||
}
|
||||
|
||||
/** Safe mode retains native OAuth/keychain auth but skips settings.env. Read only its auth keys. */
|
||||
async function localCredentialEnv(home: string): Promise<Record<string, string>> {
|
||||
try {
|
||||
const settings: unknown = JSON.parse(await readFile(path.join(home, '.claude', 'settings.json'), 'utf8'));
|
||||
const env = (settings as { env?: Record<string, unknown> } | null)?.env ?? {};
|
||||
return Object.fromEntries(
|
||||
['ANTHROPIC_API_KEY', 'ANTHROPIC_AUTH_TOKEN', 'ANTHROPIC_BASE_URL', 'ANTHROPIC_MODEL'].flatMap(name =>
|
||||
typeof env[name] === 'string' && env[name] ? [[name, env[name]]] : [],
|
||||
),
|
||||
);
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return {};
|
||||
throw new RuntimeError('LOCAL_AUTH_CONFIG', 'Cannot read the local Claude authentication settings');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* A headless CLI executor that streams Claude-Code-style JSON (CodeBuddy, Claude Code). The caller
|
||||
* must confirm the per-run isolation before headless writes are allowed.
|
||||
@ -57,16 +92,16 @@ export interface CodeBuddyConfig {
|
||||
export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
constructor(
|
||||
private readonly dialect: CliDialect,
|
||||
private readonly config: CodeBuddyConfig = {},
|
||||
private readonly config: ClaudeCodeConfig = {},
|
||||
) {
|
||||
super();
|
||||
}
|
||||
capabilities() {
|
||||
const nativeHost = this.nativeHost();
|
||||
const configured = Boolean(
|
||||
this.config.binary &&
|
||||
this.config.isolatedHome &&
|
||||
this.config.isolationConfirmed &&
|
||||
Object.keys(this.config.credentialEnv ?? {}).length,
|
||||
(nativeHost || (this.config.isolatedHome && Object.keys(this.config.credentialEnv ?? {}).length)),
|
||||
);
|
||||
return {
|
||||
provider: this.dialect.provider,
|
||||
@ -78,16 +113,25 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
...(!configured
|
||||
? {
|
||||
reason:
|
||||
'Requires an explicit executable, tenant credentials, an isolated HOME and a confirmed per-run sandbox',
|
||||
'Requires an executable and sandbox credentials, or an explicitly enabled local Claude login on a development host',
|
||||
}
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
private nativeHost() {
|
||||
return Boolean(
|
||||
this.dialect.provider === 'claude-code' &&
|
||||
this.config.localAuth?.developmentHost === true &&
|
||||
path.isAbsolute(this.config.localAuth.home),
|
||||
);
|
||||
}
|
||||
async start(input: ProviderStart) {
|
||||
if (this.capabilities().availability !== 'available')
|
||||
throw new RuntimeError('UNCONFIGURED', this.capabilities().reason!);
|
||||
await access(this.config.binary!, constants.X_OK);
|
||||
await mkdir(this.config.isolatedHome!, { recursive: true, mode: 0o700 });
|
||||
const nativeHost = this.nativeHost();
|
||||
const home = nativeHost ? this.config.localAuth!.home : this.config.isolatedHome!;
|
||||
if (!nativeHost) await mkdir(home, { recursive: true, mode: 0o700 });
|
||||
for (const name of Object.keys(this.config.credentialEnv ?? {})) {
|
||||
if (!this.dialect.credentialVariable.test(name))
|
||||
throw new RuntimeError(
|
||||
@ -95,17 +139,21 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
'Unsupported credential variable; control-plane environment is never inherited',
|
||||
);
|
||||
}
|
||||
const credentialEnv = {
|
||||
...(nativeHost ? await localCredentialEnv(home) : {}),
|
||||
...this.config.credentialEnv,
|
||||
};
|
||||
const run = this.create(input);
|
||||
const child = spawn(this.config.binary!, this.dialect.args(input, this.config.allowedTools), {
|
||||
const child = spawn(this.config.binary!, this.dialect.args(input, this.config.allowedTools, nativeHost), {
|
||||
cwd: input.workspace,
|
||||
shell: false,
|
||||
detached: process.platform !== 'win32',
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
PATH: process.env.PATH,
|
||||
HOME: this.config.isolatedHome!,
|
||||
HOME: home,
|
||||
...AGENT_GIT_IDENTITY,
|
||||
...this.config.credentialEnv,
|
||||
...credentialEnv,
|
||||
},
|
||||
});
|
||||
const kill = () => {
|
||||
@ -120,8 +168,10 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
let sawSuccess = false;
|
||||
let failed = false;
|
||||
let outputBytes = 0;
|
||||
let failureReason: string | undefined;
|
||||
const decoder = new StringDecoder('utf8');
|
||||
const redact = (value: string) =>
|
||||
Object.values(this.config.credentialEnv ?? {})
|
||||
Object.values(credentialEnv)
|
||||
.filter(Boolean)
|
||||
.reduce((text, key) => text.replaceAll(key, '[REDACTED]'), value);
|
||||
// Stream-json assistant messages carry text and tool calls; tool results and raw diagnostics stay private.
|
||||
@ -151,18 +201,35 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
if (event.type === 'result') {
|
||||
sawSuccess = event.subtype === 'success' && event.is_error !== true;
|
||||
failed ||= !sawSuccess;
|
||||
if (!sawSuccess)
|
||||
failureReason =
|
||||
typeof event.result === 'string'
|
||||
? redact(event.result).slice(0, 1000)
|
||||
: `${this.dialect.label} result: ${String(event.subtype ?? 'error').slice(0, 120)}`;
|
||||
if (typeof event.result === 'string') this.emit(run, 'output', { text: redact(event.result) });
|
||||
if (typeof event.usage === 'object' && event.usage !== null)
|
||||
this.emit(run, 'usage', event.usage as Record<string, unknown>);
|
||||
if (typeof event.usage === 'object' && event.usage !== null) {
|
||||
const usage = event.usage as Record<string, unknown>;
|
||||
const fields = ['input_tokens', 'output_tokens', 'cache_creation_input_tokens', 'cache_read_input_tokens'];
|
||||
this.emit(run, 'usage', {
|
||||
final: true,
|
||||
...Object.fromEntries(
|
||||
fields.flatMap(name =>
|
||||
Number.isSafeInteger(usage[name]) && Number(usage[name]) >= 0 ? [[name, usage[name]]] : [],
|
||||
),
|
||||
),
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
failed = true;
|
||||
failureReason ??= 'CLI emitted invalid stream-json';
|
||||
kill();
|
||||
}
|
||||
};
|
||||
const timer = setTimeout(
|
||||
() => {
|
||||
failed = true;
|
||||
failureReason = 'CLI exceeded its time limit';
|
||||
kill();
|
||||
},
|
||||
this.config.timeoutMs ?? 30 * 60_000,
|
||||
@ -175,10 +242,11 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
outputBytes += chunk.length;
|
||||
if (outputBytes > 16 * 1024 * 1024) {
|
||||
failed = true;
|
||||
failureReason = 'CLI output exceeded 16 MiB';
|
||||
kill();
|
||||
return;
|
||||
}
|
||||
pending += chunk.toString();
|
||||
pending += decoder.write(chunk);
|
||||
const lines = pending.split('\n');
|
||||
pending = lines.pop() ?? '';
|
||||
lines.forEach(parse);
|
||||
@ -191,14 +259,14 @@ export class StreamJsonCliAdapter extends ManagedProvider {
|
||||
child.on('close', code => {
|
||||
clearTimeout(timer);
|
||||
run.controller.signal.removeEventListener('abort', abort);
|
||||
parse(pending);
|
||||
parse(pending + decoder.end());
|
||||
if (run.controller.signal.aborted) this.finish(run, 'canceled');
|
||||
else if (code === 0 && sawSuccess && !failed) this.finish(run, 'completed');
|
||||
else
|
||||
this.finish(
|
||||
run,
|
||||
'failed',
|
||||
`${this.dialect.label} exited ${code ?? 'without a code'} without a verified success result`,
|
||||
failureReason ?? `${this.dialect.label} exited ${code ?? 'without a code'} without a verified success result`,
|
||||
);
|
||||
});
|
||||
return { ...run.public };
|
||||
@ -212,9 +280,9 @@ export class CodeBuddyAdapter extends StreamJsonCliAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/** Headless Claude Code CLI (`claude -p … --permission-mode bypassPermissions`). */
|
||||
/** Headless Claude Code CLI with sandbox credentials or explicitly enabled native development login. */
|
||||
export class ClaudeCodeAdapter extends StreamJsonCliAdapter {
|
||||
constructor(config: CodeBuddyConfig = {}) {
|
||||
constructor(config: ClaudeCodeConfig = {}) {
|
||||
super(CLAUDE_CODE, config);
|
||||
}
|
||||
}
|
||||
|
||||
@ -160,7 +160,7 @@ export class CodexAdapter extends ManagedProvider {
|
||||
this.progress(run, event.item);
|
||||
if (event.type === 'turn.completed') {
|
||||
completed = true;
|
||||
this.emit(run, 'usage', event.usage ?? {});
|
||||
this.emit(run, 'usage', { ...event.usage, final: true });
|
||||
}
|
||||
if (event.type === 'turn.failed' || event.type === 'error')
|
||||
failure = event.error?.message ?? event.message ?? 'Codex reported a failed turn';
|
||||
|
||||
@ -2,7 +2,7 @@ import { createHash, createPublicKey, sign, verify, type KeyLike } from 'node:cr
|
||||
import { mkdir, open } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { canonicalJson } from '@wishforge/contracts';
|
||||
import { RuntimeError, type ReleaseManifest } from './types.js';
|
||||
import { RuntimeError, type ReleaseManifest, type TrustedPocDeliveryPolicy } from './types.js';
|
||||
import { safeIdentifier } from './git.js';
|
||||
import { validateRelease } from './deploy.js';
|
||||
|
||||
@ -38,6 +38,7 @@ export function verifyRunnerTask(
|
||||
publicKeys: Record<string, string>;
|
||||
now?: number;
|
||||
maxTtlMs?: number;
|
||||
trustedPocDelivery?: TrustedPocDeliveryPolicy;
|
||||
},
|
||||
): RunnerTask {
|
||||
const pem = policy.publicKeys[envelope.keyId];
|
||||
@ -83,7 +84,7 @@ export function verifyRunnerTask(
|
||||
expires - issued > (policy.maxTtlMs ?? 10 * 60_000)
|
||||
)
|
||||
throw new RuntimeError('TASK_EXPIRED', 'Runner task lifetime is invalid or expired');
|
||||
validateRelease(task.release);
|
||||
validateRelease(task.release, { trustedPocDelivery: policy.trustedPocDelivery });
|
||||
return task;
|
||||
}
|
||||
export interface NonceStore {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { ProviderEventType } from '@wishforge/contracts';
|
||||
import type { ProviderEventType, PocReleaseSet } from '@wishforge/contracts';
|
||||
|
||||
export class RuntimeError extends Error {
|
||||
constructor(
|
||||
@ -54,6 +54,14 @@ export interface PortableCheckpoint {
|
||||
gitBundleSha256?: string;
|
||||
createdAt: string;
|
||||
sessionId?: string;
|
||||
/** Terminal provider accounting travels with source so a lost response cannot lose work or usage. */
|
||||
providerCompletion?: {
|
||||
status: 'completed' | 'failed' | 'canceled';
|
||||
revision: number;
|
||||
repairCount: number;
|
||||
summary?: string;
|
||||
finalUsage: Array<{ operationId: string; tokens: number }>;
|
||||
};
|
||||
}
|
||||
export interface ProviderAdapter {
|
||||
capabilities(): CapabilityReport;
|
||||
@ -65,6 +73,7 @@ export interface ProviderAdapter {
|
||||
}
|
||||
|
||||
export interface ReleaseManifest {
|
||||
pocRelease?: PocReleaseSet;
|
||||
deploymentSecretPolicyDigest?: string;
|
||||
applicationUrl?: string;
|
||||
deliveryRequired?: boolean;
|
||||
@ -86,6 +95,11 @@ export interface ReleaseManifest {
|
||||
secretVersionRefs?: string[];
|
||||
bindingDigest?: string;
|
||||
}
|
||||
/** Local operator authority; never populated from an unsigned request or the task payload itself. */
|
||||
export interface TrustedPocDeliveryPolicy {
|
||||
profileDigest: string;
|
||||
applicationUrl: 'http://120.48.164.19';
|
||||
}
|
||||
export type DeploymentStatus =
|
||||
'dry-run' | 'applied' | 'verified' | 'failed' | 'unconfigured' | 'not-found' | 'conflict';
|
||||
export interface DeploymentResult {
|
||||
|
||||
124
packages/runtime/test/claude-native.test.ts
Normal file
124
packages/runtime/test/claude-native.test.ts
Normal file
@ -0,0 +1,124 @@
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { ClaudeCodeAdapter, type ProviderEvent } from '../src/index.js';
|
||||
|
||||
const dirs: string[] = [];
|
||||
afterEach(async () => {
|
||||
await Promise.all(dirs.splice(0).map(directory => rm(directory, { recursive: true, force: true })));
|
||||
});
|
||||
async function fixture(source: string, timeoutMs = 5000) {
|
||||
const home = await mkdtemp(path.join(os.tmpdir(), 'wishforge-claude-native-'));
|
||||
dirs.push(home);
|
||||
await mkdir(path.join(home, '.claude'));
|
||||
const settings = JSON.stringify({
|
||||
env: {
|
||||
ANTHROPIC_AUTH_TOKEN: 'fixture-secret-token',
|
||||
ANTHROPIC_BASE_URL: 'https://example.invalid',
|
||||
DATABASE_URL: 'private',
|
||||
},
|
||||
hooks: { mustNeverExecute: true },
|
||||
});
|
||||
await writeFile(path.join(home, '.claude/settings.json'), settings);
|
||||
const binary = path.join(home, 'claude');
|
||||
await writeFile(binary, `#!/usr/bin/env node\n${source}\n`);
|
||||
await chmod(binary, 0o700);
|
||||
const adapter = new ClaudeCodeAdapter({
|
||||
binary,
|
||||
isolationConfirmed: true,
|
||||
localAuth: { home, developmentHost: true },
|
||||
timeoutMs,
|
||||
});
|
||||
const start = () =>
|
||||
adapter.start({
|
||||
runId: 'run',
|
||||
tenantId: 'tenant',
|
||||
workspace: home,
|
||||
prompt: 'Make the requested edit',
|
||||
baseCommit: 'a'.repeat(40),
|
||||
model: 'sonnet',
|
||||
resumeSessionId: 'previous-session',
|
||||
});
|
||||
const collect = async () => {
|
||||
const events: ProviderEvent[] = [];
|
||||
for await (const event of adapter.events('run')) events.push(event);
|
||||
return events;
|
||||
};
|
||||
return { home, binary, adapter, start, collect, settings };
|
||||
}
|
||||
|
||||
describe('Claude native login on the explicit development host', () => {
|
||||
it('uses local auth without loading repository hooks or exposing host tools and redacts streamed secrets', async () => {
|
||||
const f = await fixture(`
|
||||
const assert = require('node:assert/strict');
|
||||
const args = process.argv.slice(2);
|
||||
for (const flag of ['--safe-mode', '--restricted', '--strict-mcp-config', 'dontAsk']) assert.ok(args.includes(flag));
|
||||
assert.ok(!args.includes('bypassPermissions'));
|
||||
assert.equal(args[args.indexOf('--tools') + 1], 'Read,Edit,Write,Glob,Grep');
|
||||
assert.equal(args[args.indexOf('--allowedTools') + 1], 'Read,Edit,Write,Glob,Grep');
|
||||
assert.equal(args[args.indexOf('--model') + 1], 'sonnet');
|
||||
assert.equal(args[args.indexOf('--resume') + 1], 'previous-session');
|
||||
assert.equal(process.env.ANTHROPIC_AUTH_TOKEN, 'fixture-secret-token');
|
||||
assert.equal(process.env.DATABASE_URL, undefined);
|
||||
assert.equal(process.env.WF_WORKER_TOKEN, undefined);
|
||||
console.log(JSON.stringify({type:'system',subtype:'init',session_id:'native-session'}));
|
||||
const result = Buffer.from(JSON.stringify({type:'result',subtype:'success',is_error:false,result:'已完成 fixture-secret-token',usage:{input_tokens:2,output_tokens:3,cache_creation_input_tokens:4,cache_read_input_tokens:5,private:'not exported'}})+'\\n');
|
||||
const split = result.indexOf(Buffer.from('已')) + 1;
|
||||
process.stdout.write(result.subarray(0, split));
|
||||
setTimeout(() => process.stdout.write(result.subarray(split)), 20);
|
||||
`);
|
||||
expect(f.adapter.capabilities().coding).toBe(true);
|
||||
await f.start();
|
||||
const events = await f.collect();
|
||||
expect(events.find(e => e.type === 'output')?.data.text).toBe('已完成 [REDACTED]');
|
||||
expect(events.find(e => e.type === 'usage')?.data).toEqual({
|
||||
final: true,
|
||||
input_tokens: 2,
|
||||
output_tokens: 3,
|
||||
cache_creation_input_tokens: 4,
|
||||
cache_read_input_tokens: 5,
|
||||
});
|
||||
expect(await f.adapter.inspect('run')).toMatchObject({ status: 'completed', sessionId: 'native-session' });
|
||||
expect(await readFile(path.join(f.home, '.claude/settings.json'), 'utf8')).toBe(f.settings);
|
||||
});
|
||||
|
||||
it('requires the explicit host opt-in and refuses relative auth homes', async () => {
|
||||
const f = await fixture('');
|
||||
expect(
|
||||
new ClaudeCodeAdapter({ binary: f.binary, localAuth: { home: f.home, developmentHost: true } }).capabilities()
|
||||
.coding,
|
||||
).toBe(false);
|
||||
expect(
|
||||
new ClaudeCodeAdapter({
|
||||
binary: f.binary,
|
||||
isolationConfirmed: true,
|
||||
localAuth: { home: 'relative', developmentHost: true },
|
||||
}).capabilities().coding,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('does not mistake an authentication error with success subtype for completed work', async () => {
|
||||
const f = await fixture(
|
||||
`console.log(JSON.stringify({type:'result',subtype:'success',is_error:true,result:'Not logged in'}));`,
|
||||
);
|
||||
await f.start();
|
||||
await f.collect();
|
||||
expect((await f.adapter.inspect('run')).status).toBe('failed');
|
||||
});
|
||||
|
||||
it('reports timeouts and canceled processes separately', async () => {
|
||||
const timeout = await fixture('setInterval(() => {}, 1000);', 150);
|
||||
await timeout.start();
|
||||
await timeout.collect();
|
||||
expect(await timeout.adapter.inspect('run')).toMatchObject({
|
||||
status: 'failed',
|
||||
error: 'CLI exceeded its time limit',
|
||||
});
|
||||
const canceled = await fixture('setInterval(() => {}, 1000);');
|
||||
await canceled.start();
|
||||
await canceled.adapter.cancel('run');
|
||||
await canceled.collect();
|
||||
expect((await canceled.adapter.inspect('run')).status).toBe('canceled');
|
||||
});
|
||||
});
|
||||
@ -2,7 +2,14 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import { GitWorkspace, executeCommand, requireBranchName, validateRepositoryUrl } from '../src/index.js';
|
||||
import {
|
||||
GitWorkspace,
|
||||
executeCommand,
|
||||
requireBranchName,
|
||||
validateRepositoryUrl,
|
||||
type Command,
|
||||
type LocalSshTransport,
|
||||
} from '../src/index.js';
|
||||
import { projectInput } from '../../contracts/src/index.js';
|
||||
const dirs: string[] = [];
|
||||
async function temporary() {
|
||||
@ -12,6 +19,102 @@ async function temporary() {
|
||||
}
|
||||
afterEach(async () => {
|
||||
await Promise.all(dirs.splice(0).map(dir => rm(dir, { recursive: true, force: true })));
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
describe('operator-pinned local SSH authentication', () => {
|
||||
const transport: LocalSshTransport = {
|
||||
repositoryUrl: 'https://gitea.example.org/team/project.git',
|
||||
sshUrl: 'ssh://git@gitea.example.org/team/project.git',
|
||||
home: '/home/operator',
|
||||
};
|
||||
const development = () => {
|
||||
vi.stubEnv('WF_DEPLOYMENT_MODE', 'development');
|
||||
vi.stubEnv('NODE_ENV', 'test');
|
||||
};
|
||||
it('leaves project SSH unsupported and requires explicit development mode', () => {
|
||||
for (const mode of [undefined, 'production']) {
|
||||
vi.stubEnv('WF_DEPLOYMENT_MODE', mode);
|
||||
expect(() => new GitWorkspace(undefined, { localSshTransport: transport })).toThrow('explicit development');
|
||||
}
|
||||
development();
|
||||
vi.stubEnv('NODE_ENV', 'production');
|
||||
expect(() => new GitWorkspace(undefined, { localSshTransport: transport })).toThrow('explicit development');
|
||||
development();
|
||||
expect(() => validateRepositoryUrl(transport.sshUrl)).toThrow('SSH repository transport is not supported');
|
||||
});
|
||||
it('rejects host, repository, credentials, port and HOME changes in the operator mapping', () => {
|
||||
development();
|
||||
for (const change of [
|
||||
{ sshUrl: 'ssh://git@other.example.org/team/project.git' },
|
||||
{ sshUrl: 'ssh://git@gitea.example.org/team/other.git' },
|
||||
{ sshUrl: 'ssh://root@gitea.example.org/team/project.git' },
|
||||
{ sshUrl: 'ssh://git:secret@gitea.example.org/team/project.git' },
|
||||
{ sshUrl: 'ssh://git@gitea.example.org:2222/team/project.git' },
|
||||
{ sshUrl: 'ssh://git@gitea.example.org/team/project.git?upload-pack=evil' },
|
||||
{ sshUrl: 'git@gitea.example.org:team/project.git' },
|
||||
{ repositoryUrl: 'https://gitea.example.org:444/team/project.git' },
|
||||
{ repositoryUrl: 'https://gitea.example.org/team/pro%6aect.git' },
|
||||
{ home: 'relative' },
|
||||
{ home: '/home/operator\nEVIL=1' },
|
||||
])
|
||||
expect(() => new GitWorkspace(undefined, { localSshTransport: { ...transport, ...change } })).toThrow();
|
||||
});
|
||||
it('keeps rewrites and SSH options inside Git processes, and binds clone/fetch/push to the pinned repository', async () => {
|
||||
development();
|
||||
const commands: Command[] = [];
|
||||
const execute = vi.fn(async (command: Command) => {
|
||||
commands.push(command);
|
||||
return {
|
||||
exitCode: 0,
|
||||
stdout: command.args.includes('--is-bare-repository') ? 'true' : 'a'.repeat(40),
|
||||
stderr: '',
|
||||
};
|
||||
});
|
||||
const git = new GitWorkspace(execute, { localSshTransport: transport });
|
||||
const root = await temporary();
|
||||
await git.create({ repository: transport.repositoryUrl, targetBranch: 'poc', root, runId: 'clone' });
|
||||
expect(commands[0]?.args).toContain(transport.repositoryUrl);
|
||||
expect(commands[0]?.env).toMatchObject({
|
||||
HOME: transport.home,
|
||||
GIT_SSH_COMMAND: 'ssh -oBatchMode=yes -oStrictHostKeyChecking=yes',
|
||||
GIT_CONFIG_COUNT: '2',
|
||||
GIT_CONFIG_KEY_1: `url.${transport.sshUrl}.insteadOf`,
|
||||
GIT_CONFIG_VALUE_1: transport.repositoryUrl,
|
||||
GIT_CONFIG_GLOBAL: '/dev/null',
|
||||
});
|
||||
await git.initializeIntegration({
|
||||
repository: path.join(root, 'integration.git'),
|
||||
originalRepository: transport.repositoryUrl,
|
||||
workspace: path.join(root, 'clone'),
|
||||
sourceCommit: 'b'.repeat(40),
|
||||
baseCommit: 'a'.repeat(40),
|
||||
targetRef: 'refs/heads/poc',
|
||||
});
|
||||
const fetch = commands.find(
|
||||
command => command.args.includes('fetch') && command.args.includes('+refs/heads/poc:refs/heads/poc'),
|
||||
);
|
||||
expect(fetch?.args).toContain(transport.repositoryUrl);
|
||||
expect(fetch?.args).not.toContain('origin');
|
||||
await git.pushVerifiedCandidate({
|
||||
repository: root,
|
||||
targetRef: 'refs/heads/poc',
|
||||
expectedTargetCommit: 'a'.repeat(40),
|
||||
candidateCommit: 'b'.repeat(40),
|
||||
verifiedCommit: 'b'.repeat(40),
|
||||
});
|
||||
const push = commands.find(command => command.args.includes('push'));
|
||||
expect(push?.args).toContain(transport.repositoryUrl);
|
||||
expect(push?.args).not.toContain('origin');
|
||||
await expect(
|
||||
git.create({ repository: `${transport.repositoryUrl}-other`, root, runId: 'foreign' }),
|
||||
).rejects.toMatchObject({ code: 'LOCAL_SSH_SCOPE' });
|
||||
await expect(git.create({ repository: transport.sshUrl, root, runId: 'ssh-project' })).rejects.toMatchObject({
|
||||
code: 'REPOSITORY_TRANSPORT_UNSUPPORTED',
|
||||
});
|
||||
vi.stubEnv('WF_DEPLOYMENT_MODE', 'production');
|
||||
await expect(git.head(root)).rejects.toMatchObject({ code: 'LOCAL_SSH_POLICY' });
|
||||
});
|
||||
});
|
||||
async function git(cwd: string, args: string[]) {
|
||||
const result = await executeCommand({ executable: 'git', args: ['-c', 'core.hooksPath=/dev/null', ...args], cwd });
|
||||
|
||||
350
packages/runtime/test/poc-release.test.ts
Normal file
350
packages/runtime/test/poc-release.test.ts
Normal file
@ -0,0 +1,350 @@
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it } from 'vitest';
|
||||
import { PocDeploymentAdapter, writePocJson, type PocRelease } from '../src/poc-release.js';
|
||||
import { createPocBuildPod } from '../src/poc-build.js';
|
||||
import { PocRunnerDeploymentAdapter, pocReleaseDigest } from '../src/poc-runner.js';
|
||||
import type { ReleaseManifest } from '../src/types.js';
|
||||
import {
|
||||
loadPocProfile,
|
||||
pocComponents,
|
||||
pocComponentNames,
|
||||
pocProfileSchema,
|
||||
profileDigest,
|
||||
type PocComponent,
|
||||
type PocProfile,
|
||||
} from '../src/poc-profile.js';
|
||||
import type { Command, CommandExecutor } from '../src/process.js';
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(async () => {
|
||||
await Promise.all(roots.splice(0).map(p => rm(p, { recursive: true, force: true })));
|
||||
});
|
||||
const profile: PocProfile = pocProfileSchema.parse({
|
||||
schemaVersion: 1,
|
||||
id: 'droboticflow-poc',
|
||||
repository: 'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
branch: 'poc',
|
||||
kubeconfig: '/operator/poc.config',
|
||||
clusterServer: 'https://120.48.181.105:6443',
|
||||
namespace: 'droboticflow',
|
||||
namespaceUid: '15f3b2eb-f6b9-4a7c-89c3-fc5ba38bce71',
|
||||
deployments: {
|
||||
api: '6bed36ca-24e0-49a6-8edb-e463a7debf22',
|
||||
worker: '9b5b0f9b-0211-4a47-85e9-2fec82fef8bd',
|
||||
coordinator: '454b4c0c-719b-4203-8a1d-2a190a9cd304',
|
||||
},
|
||||
registry: 'ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc',
|
||||
registrySecret: 'droboticflow-registry',
|
||||
applicationUrl: 'http://120.48.164.19',
|
||||
kanikoImage: `gcr.io/kaniko-project/executor@sha256:${'a'.repeat(64)}`,
|
||||
});
|
||||
function images(letter: string) {
|
||||
return Object.fromEntries(
|
||||
pocComponentNames.map(c => [c, `${profile.registry}/${pocComponents[c].repository}@sha256:${letter.repeat(64)}`]),
|
||||
) as PocRelease['images'];
|
||||
}
|
||||
function release(overrides: Partial<PocRelease> = {}): PocRelease {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
id: 'run-1',
|
||||
sourceCommit: 'a'.repeat(40),
|
||||
profileDigest: profileDigest(profile),
|
||||
images: images('b'),
|
||||
expectedImages: images('a'),
|
||||
rollbackCompatible: true,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function fixture() {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'wishforge-poc-'));
|
||||
roots.push(root);
|
||||
const calls: Command[] = [];
|
||||
const state = Object.fromEntries(
|
||||
pocComponentNames.map(component => [
|
||||
component,
|
||||
{
|
||||
metadata: { uid: profile.deployments[component], resourceVersion: '1', generation: 1 },
|
||||
spec: {
|
||||
replicas: 2,
|
||||
selector: { matchLabels: { app: pocComponents[component].deployment } },
|
||||
template: {
|
||||
metadata: { annotations: { 'existing.example/keep': 'yes' } },
|
||||
spec: {
|
||||
containers: [
|
||||
{ name: pocComponents[component].container, image: images('a')[component] },
|
||||
{ name: 'untouched-sidecar', image: 'example/sidecar:stable' },
|
||||
],
|
||||
volumes: [{ name: 'existing', persistentVolumeClaim: { claimName: 'business-data' } }],
|
||||
},
|
||||
},
|
||||
},
|
||||
status: { observedGeneration: 1, replicas: 2, updatedReplicas: 2, readyReplicas: 2, availableReplicas: 2 },
|
||||
},
|
||||
]),
|
||||
) as Record<PocComponent, any>;
|
||||
const failures = {
|
||||
loseReceipt: false,
|
||||
failRolloutOnce: false,
|
||||
wrongPodDigest: false,
|
||||
externalImageBeforeRollback: false,
|
||||
};
|
||||
const execute: CommandExecutor = async cmd => {
|
||||
calls.push(cmd);
|
||||
const args = cmd.args.slice(5);
|
||||
let data: unknown = {};
|
||||
if (args[0] === 'config') data = { clusters: [{ cluster: { server: profile.clusterServer } }] };
|
||||
else if (args[0] === 'get' && args[1] === 'namespace') data = { metadata: { uid: profile.namespaceUid } };
|
||||
else if (args[0] === 'get' && args[1] === 'deployment')
|
||||
data = state[pocComponentNames.find(c => pocComponents[c].deployment === args[2])!];
|
||||
else if (args[0] === 'patch') {
|
||||
const component = pocComponentNames.find(c => pocComponents[c].deployment === args[2])!;
|
||||
const item = state[component];
|
||||
const operations = JSON.parse(args[args.indexOf('-p') + 1]!);
|
||||
for (const operation of operations) {
|
||||
const keys = operation.path
|
||||
.slice(1)
|
||||
.split('/')
|
||||
.map((k: string) => k.replaceAll('~1', '/').replaceAll('~0', '~'));
|
||||
let current = item;
|
||||
for (const key of keys.slice(0, -1)) current = current[key];
|
||||
if (operation.op === 'test') {
|
||||
if (current[keys.at(-1)!] !== operation.value)
|
||||
return { exitCode: 1, stdout: '', stderr: 'JSON patch precondition failed' };
|
||||
} else current[keys.at(-1)!] = operation.value;
|
||||
}
|
||||
item.metadata.resourceVersion = String(Number(item.metadata.resourceVersion) + 1);
|
||||
item.metadata.generation++;
|
||||
item.status.observedGeneration++;
|
||||
if (failures.loseReceipt) {
|
||||
failures.loseReceipt = false;
|
||||
throw Error('connection lost after committed patch');
|
||||
}
|
||||
data = item;
|
||||
} else if (args[0] === 'rollout' && failures.failRolloutOnce) {
|
||||
failures.failRolloutOnce = false;
|
||||
if (failures.externalImageBeforeRollback) state.api.spec.template.spec.containers[0].image = images('c').api;
|
||||
return { exitCode: 1, stdout: '', stderr: 'progress deadline exceeded' };
|
||||
} else if (args[0] === 'get' && args[1] === 'pods') {
|
||||
const component = pocComponentNames.find(
|
||||
c => `app=${pocComponents[c].deployment}` === args[args.indexOf('-l') + 1],
|
||||
)!;
|
||||
const item = state[component];
|
||||
data = {
|
||||
items: Array.from({ length: 2 }, () => ({
|
||||
metadata: {},
|
||||
spec: { containers: item.spec.template.spec.containers },
|
||||
status: {
|
||||
containerStatuses: [
|
||||
{
|
||||
name: pocComponents[component].container,
|
||||
ready: true,
|
||||
imageID: failures.wrongPodDigest ? images('c')[component] : item.spec.template.spec.containers[0].image,
|
||||
},
|
||||
],
|
||||
},
|
||||
})),
|
||||
};
|
||||
}
|
||||
return { exitCode: 0, stdout: JSON.stringify(data), stderr: '' };
|
||||
};
|
||||
const adapter = new PocDeploymentAdapter(profile, root, execute, async () =>
|
||||
Response.json({ buildCommit: 'a'.repeat(40) }),
|
||||
);
|
||||
return { root, state, calls, failures, adapter, execute };
|
||||
}
|
||||
|
||||
describe('operator POC release', () => {
|
||||
it('changes only three named container images and own annotations, then verifies three rollouts and Pod digests', async () => {
|
||||
const f = await fixture();
|
||||
const before = structuredClone(f.state);
|
||||
const result = await f.adapter.deploy(release());
|
||||
expect(result.state).toBe('succeeded');
|
||||
expect(result.verification?.checks).toContain('pod-digests');
|
||||
for (const c of pocComponentNames) {
|
||||
expect(f.state[c].spec.template.spec.containers[0].image).toBe(images('b')[c]);
|
||||
expect(f.state[c].spec.template.spec.containers[1]).toEqual(before[c].spec.template.spec.containers[1]);
|
||||
expect(f.state[c].spec.template.spec.volumes).toEqual(before[c].spec.template.spec.volumes);
|
||||
expect(f.state[c].spec.template.metadata.annotations['existing.example/keep']).toBe('yes');
|
||||
}
|
||||
const patches = f.calls.filter(c => c.args.includes('patch'));
|
||||
expect(patches).toHaveLength(3);
|
||||
for (const p of patches)
|
||||
expect(JSON.parse(p.args.at(-1)!)).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({ op: 'test', path: '/metadata/uid' }),
|
||||
expect.objectContaining({ op: 'test', path: '/metadata/resourceVersion' }),
|
||||
expect.objectContaining({ op: 'test', path: '/spec/template/spec/containers/0/image' }),
|
||||
]),
|
||||
);
|
||||
expect(JSON.parse(await readFile(path.join(f.root, 'run-1.json'), 'utf8')).state).toBe('succeeded');
|
||||
});
|
||||
it('refuses changed baseline, mismatched profile and other registry without patching', async () => {
|
||||
const f = await fixture();
|
||||
f.state.worker.spec.template.spec.containers[0].image = images('c').worker;
|
||||
await expect(f.adapter.deploy(release())).rejects.toThrow('baseline changed');
|
||||
await expect(f.adapter.deploy(release({ profileDigest: `sha256:${'b'.repeat(64)}` }))).rejects.toThrow(
|
||||
'profile digest',
|
||||
);
|
||||
await expect(
|
||||
f.adapter.deploy(release({ images: { ...images('b'), api: `registry.example/api@sha256:${'b'.repeat(64)}` } })),
|
||||
).rejects.toThrow('repository mismatch');
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(0);
|
||||
});
|
||||
it('recovers an uncertain committed patch using observation and compatible rollback', async () => {
|
||||
const f = await fixture();
|
||||
f.failures.loseReceipt = true;
|
||||
const result = await f.adapter.deploy(release());
|
||||
expect(result.state).toBe('rolled_back');
|
||||
expect(f.state.coordinator.spec.template.spec.containers[0].image).toBe(images('a').coordinator);
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(2);
|
||||
});
|
||||
it('does not roll back when compatibility is not declared', async () => {
|
||||
const f = await fixture();
|
||||
f.failures.failRolloutOnce = true;
|
||||
const result = await f.adapter.deploy(release({ rollbackCompatible: false }));
|
||||
expect(result.state).toBe('needs_reconciliation');
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(3);
|
||||
});
|
||||
it('refuses to overwrite a newer external image during rollback', async () => {
|
||||
const f = await fixture();
|
||||
f.failures.failRolloutOnce = true;
|
||||
f.failures.externalImageBeforeRollback = true;
|
||||
const result = await f.adapter.deploy(release());
|
||||
expect(result.state).toBe('needs_reconciliation');
|
||||
expect(f.state.api.spec.template.spec.containers[0].image).toBe(images('c').api);
|
||||
});
|
||||
it('returns repeated completed release without patching and rejects an ID collision', async () => {
|
||||
const f = await fixture();
|
||||
await f.adapter.deploy(release());
|
||||
await f.adapter.deploy(release());
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(3);
|
||||
await expect(f.adapter.deploy(release({ sourceCommit: 'b'.repeat(40) }))).rejects.toThrow('different content');
|
||||
});
|
||||
it('reconciles an interrupted journal without blindly repeating mutations', async () => {
|
||||
const f = await fixture();
|
||||
const r = release();
|
||||
await writePocJson(path.join(f.root, 'run-1.json'), {
|
||||
schemaVersion: 1,
|
||||
release: r,
|
||||
state: 'applying',
|
||||
startedAt: '',
|
||||
updatedAt: '',
|
||||
applied: [],
|
||||
pending: 'coordinator',
|
||||
});
|
||||
await expect(f.adapter.deploy(r)).rejects.toThrow('requires reconcile');
|
||||
const result = await f.adapter.reconcile('run-1');
|
||||
expect(result.state).toBe('rolled_back');
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(0);
|
||||
});
|
||||
it('fails digest verification even if kubectl rollout reports success', async () => {
|
||||
const f = await fixture();
|
||||
f.failures.wrongPodDigest = true;
|
||||
const result = await f.adapter.deploy(release({ rollbackCompatible: false }));
|
||||
expect(result.state).toBe('needs_reconciliation');
|
||||
expect(result.error).toContain('Pod digest');
|
||||
});
|
||||
it('keeps unmodified components pinned without restarting them', async () => {
|
||||
const f = await fixture();
|
||||
const r = release({ images: { ...images('a'), api: images('b').api } });
|
||||
expect((await f.adapter.deploy(r)).state).toBe('succeeded');
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(1);
|
||||
expect(f.calls.filter(c => c.args.includes('rollout'))).toHaveLength(3);
|
||||
});
|
||||
it('detects preserved configuration drift before mutation', async () => {
|
||||
const f = await fixture();
|
||||
const baseline = await f.adapter.snapshot();
|
||||
f.state.worker.spec.template.spec.volumes[0].persistentVolumeClaim.claimName = 'changed-by-another-operator';
|
||||
await expect(f.adapter.deploy(release({ expectedTemplateDigests: baseline.templateDigests }))).rejects.toThrow(
|
||||
'configuration changed',
|
||||
);
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(0);
|
||||
});
|
||||
it('template fingerprints remain stable across release images and audit annotation changes', async () => {
|
||||
const f = await fixture();
|
||||
const before = await f.adapter.snapshot();
|
||||
expect((await f.adapter.deploy(release({ expectedTemplateDigests: before.templateDigests }))).state).toBe(
|
||||
'succeeded',
|
||||
);
|
||||
expect((await f.adapter.snapshot()).templateDigests).toEqual(before.templateDigests);
|
||||
});
|
||||
it('includes protected business checks inside the compatible rollback boundary', async () => {
|
||||
const f = await fixture();
|
||||
const result = await f.adapter.deploy(release(), async () => {
|
||||
throw Error('expected business behavior absent');
|
||||
});
|
||||
expect(result.state).toBe('rolled_back');
|
||||
expect((await f.adapter.snapshot()).images).toEqual(images('a'));
|
||||
});
|
||||
it('rejects recovery with a signed manifest different from the stored journal', async () => {
|
||||
const f = await fixture();
|
||||
await f.adapter.deploy(release());
|
||||
await expect(f.adapter.reconcile('run-1', release({ sourceCommit: 'b'.repeat(40) }))).rejects.toThrow(
|
||||
'persisted signed release',
|
||||
);
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(3);
|
||||
});
|
||||
it('Runner wrapper binds the main source/image to the component bundle and returns broker acceptance evidence', async () => {
|
||||
const f = await fixture();
|
||||
const inner = release({ expectedTemplateDigests: (await f.adapter.snapshot()).templateDigests });
|
||||
const manifest: ReleaseManifest & { pocRelease: PocRelease } = {
|
||||
schemaVersion: 1,
|
||||
id: 'outer-release-1',
|
||||
tenantId: 'tenant',
|
||||
environmentId: 'poc',
|
||||
sourceCommit: inner.sourceCommit,
|
||||
image: inner.images.api,
|
||||
architecture: 'linux/amd64',
|
||||
containerPort: 3001,
|
||||
healthPath: '/api/health',
|
||||
applicationUrl: profile.applicationUrl,
|
||||
smokeChecks: [{ path: '/api/ready', expectedStatus: 200 }],
|
||||
pocRelease: inner,
|
||||
};
|
||||
const wrapper = new PocRunnerDeploymentAdapter(profile, f.root, f.execute, async () =>
|
||||
Response.json({ buildCommit: 'a'.repeat(40) }),
|
||||
);
|
||||
expect((await wrapper.deploy(manifest)).status).toBe('dry-run');
|
||||
expect(f.calls.filter(c => c.args.includes('patch'))).toHaveLength(0);
|
||||
await expect(wrapper.deploy({ ...manifest, image: images('c').api }, { execute: true })).rejects.toThrow(
|
||||
'does not match',
|
||||
);
|
||||
const result = await wrapper.deploy(manifest, { execute: true });
|
||||
expect(result.status).toBe('verified');
|
||||
expect(result.evidence).toMatchObject({
|
||||
identityMatched: true,
|
||||
trafficSwitched: true,
|
||||
bundleDigest: pocReleaseDigest(inner),
|
||||
pocRelease: inner,
|
||||
});
|
||||
expect(result.evidence.checks).toEqual(
|
||||
expect.arrayContaining(['health', 'business-smoke', 'pod-digests', 'protected-business-smoke']),
|
||||
);
|
||||
});
|
||||
it('requires an operator profile without group/world write access', async () => {
|
||||
const f = await fixture();
|
||||
const file = path.join(f.root, 'profile.json');
|
||||
await writeFile(file, JSON.stringify(profile), { mode: 0o666 });
|
||||
const { chmod } = await import('node:fs/promises');
|
||||
await chmod(file, 0o666);
|
||||
await expect(loadPocProfile(file)).rejects.toThrow('operator-owned');
|
||||
});
|
||||
it('builds with emptyDir and only registry secret, pinned executor, no service account or business PVC', () => {
|
||||
const pod = createPocBuildPod(profile, {
|
||||
name: 'build',
|
||||
releaseId: 'run-1',
|
||||
component: 'api',
|
||||
sourceCommit: 'a'.repeat(40),
|
||||
buildTime: '2026-10-22T00:00:00.000Z',
|
||||
});
|
||||
expect(pod.spec.automountServiceAccountToken).toBe(false);
|
||||
expect(pod.spec.volumes).toHaveLength(2);
|
||||
expect(JSON.stringify(pod)).not.toContain('persistentVolumeClaim');
|
||||
expect(pod.spec.containers[0]?.args).toContain('--digest-file=/dev/termination-log');
|
||||
expect(pod.spec.containers[0]?.image).toContain('@sha256:');
|
||||
expect(pod.spec.activeDeadlineSeconds).toBe(1800);
|
||||
});
|
||||
});
|
||||
32
scripts/build-local-web.mjs
Normal file
32
scripts/build-local-web.mjs
Normal file
@ -0,0 +1,32 @@
|
||||
import { spawn } from 'node:child_process';
|
||||
import { resolve } from 'node:path';
|
||||
import { environment, loadLocalEnvironment } from './local-services.mjs';
|
||||
|
||||
loadLocalEnvironment();
|
||||
// Load env in this parent, not Node's --env-file argv: Next forwards execArgv to
|
||||
// worker NODE_OPTIONS, where Node rejects --env-file. No database credentials go to Web.
|
||||
const child = spawn(process.execPath, [resolve('node_modules/next/dist/bin/next'), 'build'], {
|
||||
cwd: resolve('apps/web'),
|
||||
env: environment(
|
||||
[
|
||||
/^WF_LOCAL_ACCESS/,
|
||||
/^WF_LOCAL_ALLOWED_ORIGINS$/,
|
||||
/^WF_DEV_AUTH_TOKEN$/,
|
||||
/^WF_SESSION_SECRET$/,
|
||||
/^WF_PUBLIC_URL$/,
|
||||
/^WF_API_URL$/,
|
||||
/^WF_WEB_/,
|
||||
/^WF_IDE_/,
|
||||
/^OIDC_/,
|
||||
],
|
||||
{ NODE_ENV: 'production', NEXT_TELEMETRY_DISABLED: '1' },
|
||||
),
|
||||
stdio: 'inherit',
|
||||
});
|
||||
child.on('error', error => {
|
||||
console.error(error.message);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
child.on('exit', code => {
|
||||
process.exitCode = code ?? 1;
|
||||
});
|
||||
41
scripts/configure-local-poc-plan.ts
Normal file
41
scripts/configure-local-poc-plan.ts
Normal file
@ -0,0 +1,41 @@
|
||||
/** Explicit local operator quota configuration; does not run a model or purchase credits. */
|
||||
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { PILOT_PLAN } from '../apps/api/src/domain/entitlements.js';
|
||||
import { runTenantAdmin } from './tenant-admin.js';
|
||||
import { readLocalPocBinding } from '../packages/runtime/src/local-poc-binding.js';
|
||||
|
||||
process.loadEnvFile('.env.local');
|
||||
const tokens = Number(process.argv[2]);
|
||||
if (
|
||||
process.env.WF_DEPLOYMENT_MODE !== 'development' ||
|
||||
!Number.isSafeInteger(tokens) ||
|
||||
tokens < 2000000 ||
|
||||
tokens > 100000000
|
||||
)
|
||||
throw Error('Usage: configure-local-poc-plan <monthly-token-limit 2000000..100000000>; local development only');
|
||||
const binding = readLocalPocBinding(process.env.WF_LOCAL_POC_BINDING_FILE)!;
|
||||
if (!binding) throw Error('Configure the operator-bound POC project first');
|
||||
const file = path.resolve('.wishforge/local-poc/plans.json');
|
||||
const plan = {
|
||||
...structuredClone(PILOT_PLAN),
|
||||
id: 'local-poc',
|
||||
version: 'local-poc-v1',
|
||||
name: '本机 Claude POC 实验',
|
||||
limits: { ...PILOT_PLAN.limits, maxConcurrentRuns: 1, monthlyModelTokens: tokens },
|
||||
};
|
||||
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
||||
await writeFile(file, JSON.stringify({ schemaVersion: 1, plans: [PILOT_PLAN, plan] }, null, 2) + '\n', { mode: 0o600 });
|
||||
const subject = '11111111-1111-4111-8111-111111111111';
|
||||
const result = await runTenantAdmin('plan', [binding.tenantId, plan.id, subject], {
|
||||
...process.env,
|
||||
WF_PLAN_CATALOG_FILE: file,
|
||||
WF_OPERATOR_SUBJECTS: subject,
|
||||
});
|
||||
const env = await readFile('.env.local', 'utf8');
|
||||
const lines = env.split('\n').filter(line => !line.startsWith('WF_PLAN_CATALOG_FILE='));
|
||||
lines.push(`WF_PLAN_CATALOG_FILE=${file}`);
|
||||
await writeFile('.env.local', lines.join('\n') + '\n', { mode: 0o600 });
|
||||
console.log(
|
||||
JSON.stringify({ result, monthlyModelTokens: tokens, maxConcurrentRuns: 1, modelCalls: 0, purchasedCredits: false }),
|
||||
);
|
||||
@ -3,6 +3,15 @@ import { environment, loadLocalEnvironment, startServices } from './local-servic
|
||||
|
||||
loadLocalEnvironment();
|
||||
const root = process.cwd();
|
||||
const productionWeb = process.env.WF_WEB_SERVE_MODE === 'production';
|
||||
if (process.env.WF_WEB_SERVE_MODE && !['production', 'development'].includes(process.env.WF_WEB_SERVE_MODE))
|
||||
throw new Error('WF_WEB_SERVE_MODE must be production or development.');
|
||||
if (
|
||||
process.env.WF_WEB_HOST &&
|
||||
!['127.0.0.1', 'localhost', '::1'].includes(process.env.WF_WEB_HOST) &&
|
||||
process.env.WF_LOCAL_ACCESS !== 'true'
|
||||
)
|
||||
throw new Error('A non-loopback development web listener requires WF_LOCAL_ACCESS=true.');
|
||||
const worker = (role, token) => ({
|
||||
name: role,
|
||||
cwd: root,
|
||||
@ -44,6 +53,7 @@ await startServices(
|
||||
/^WF_GIT_ALLOW_LOCAL_HTTP$/,
|
||||
/^WF_CODEBUDDY_BIN$/,
|
||||
/^WF_CLAUDE_BIN$/,
|
||||
/^WF_CLAUDE_LOCAL_AUTH_HOME$/,
|
||||
]),
|
||||
url: 'http://127.0.0.1:4450/healthz',
|
||||
identity: 'wishforge-provisioner',
|
||||
@ -53,9 +63,19 @@ await startServices(
|
||||
{
|
||||
name: 'web',
|
||||
cwd: resolve(root, 'apps/web'),
|
||||
args: [resolve(root, 'node_modules/next/dist/bin/next'), 'dev', '--hostname', '127.0.0.1', '--port', '3000'],
|
||||
env: environment([]),
|
||||
url: 'http://127.0.0.1:3000/api/session',
|
||||
args: [
|
||||
resolve(root, 'node_modules/next/dist/bin/next'),
|
||||
productionWeb ? 'start' : 'dev',
|
||||
'--hostname',
|
||||
process.env.WF_WEB_HOST || '127.0.0.1',
|
||||
'--port',
|
||||
process.env.WF_WEB_PORT || '3000',
|
||||
],
|
||||
env: environment(
|
||||
[/^WF_LOCAL_ACCESS$/, /^WF_LOCAL_ACCESS_FILE$/, /^WF_LOCAL_ALLOWED_ORIGINS$/, /^WF_LOCAL_API_TOKEN$/],
|
||||
{ NODE_ENV: productionWeb ? 'production' : 'development' },
|
||||
),
|
||||
url: `http://127.0.0.1:${process.env.WF_WEB_PORT || '3000'}${process.env.WF_LOCAL_ACCESS === 'true' ? '/login' : '/api/session'}`,
|
||||
},
|
||||
],
|
||||
'WishForge console: http://localhost:3000 · API: http://127.0.0.1:4000 · Temporal: http://127.0.0.1:8233',
|
||||
|
||||
270
scripts/local-poc-service.mjs
Normal file
270
scripts/local-poc-service.mjs
Normal file
@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env node
|
||||
// This is an opt-in user LaunchAgent; importing or running without arguments only reports status.
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import {
|
||||
appendFileSync,
|
||||
chmodSync,
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
readFileSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
writeFileSync,
|
||||
} from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
export const LABEL = 'cc.d-robotics.wishforge.local-poc';
|
||||
export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const NODE = '/opt/homebrew/bin/node';
|
||||
const SCRIPT = path.join(ROOT, 'scripts/local-poc-service.mjs');
|
||||
const STATE_ROOT = path.join(ROOT, '.wishforge/local-poc');
|
||||
const LOG_ROOT = path.join(STATE_ROOT, 'logs');
|
||||
const STATE_FILE = path.join(STATE_ROOT, 'service-state.json');
|
||||
const RUNNER_ENV = path.join(STATE_ROOT, 'runner.env');
|
||||
const PLIST = path.join(homedir(), 'Library/LaunchAgents', `${LABEL}.plist`);
|
||||
const DOMAIN = `gui/${process.getuid?.()}`;
|
||||
const SERVICE = `${DOMAIN}/${LABEL}`;
|
||||
const MAX_LOG_BYTES = 5 * 1024 * 1024;
|
||||
|
||||
export function parseServiceAction(argv) {
|
||||
if (!argv.length) return 'status';
|
||||
if (argv.length !== 1 || !['install', 'start', 'status', 'stop', 'run'].includes(argv[0]))
|
||||
throw new Error('Usage: local-poc-service.mjs install|start|status|stop');
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
export function minimalEnvironment(home = homedir()) {
|
||||
return {
|
||||
PATH: '/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin',
|
||||
HOME: home,
|
||||
LANG: 'en_US.UTF-8',
|
||||
TMPDIR: path.join(STATE_ROOT, 'tmp'),
|
||||
};
|
||||
}
|
||||
|
||||
const escapeXml = value =>
|
||||
String(value).replace(
|
||||
/[<>&"']/g,
|
||||
character => ({ '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' })[character],
|
||||
);
|
||||
export function launchAgentPlist(root = ROOT, home = homedir()) {
|
||||
const strings = values => values.map(value => `<string>${escapeXml(value)}</string>`).join('');
|
||||
const env = minimalEnvironment(home);
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0"><dict>
|
||||
<key>Label</key><string>${LABEL}</string>
|
||||
<key>ProgramArguments</key><array>${strings([NODE, path.join(root, 'scripts/local-poc-service.mjs'), 'run'])}</array>
|
||||
<key>WorkingDirectory</key><string>${escapeXml(root)}</string>
|
||||
<key>EnvironmentVariables</key><dict>${Object.entries(env)
|
||||
.map(([key, value]) => `<key>${key}</key><string>${escapeXml(value)}</string>`)
|
||||
.join('')}</dict>
|
||||
<key>RunAtLoad</key><true/>
|
||||
<key>KeepAlive</key><true/>
|
||||
<key>ThrottleInterval</key><integer>15</integer>
|
||||
<key>ExitTimeOut</key><integer>30</integer>
|
||||
<key>Umask</key><integer>63</integer>
|
||||
<key>ProcessType</key><string>Background</string>
|
||||
<key>StandardOutPath</key><string>/dev/null</string>
|
||||
<key>StandardErrorPath</key><string>/dev/null</string>
|
||||
</dict></plist>\n`;
|
||||
}
|
||||
|
||||
export function rotateAppend(file, bytes, maxBytes = MAX_LOG_BYTES, backups = 3) {
|
||||
const content = Buffer.isBuffer(bytes) ? bytes : Buffer.from(String(bytes));
|
||||
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
||||
chmodSync(path.dirname(file), 0o700);
|
||||
if (existsSync(file) && statSync(file).size + content.length > maxBytes) {
|
||||
rmSync(`${file}.${backups}`, { force: true });
|
||||
for (let index = backups - 1; index >= 1; index--)
|
||||
if (existsSync(`${file}.${index}`)) renameSync(`${file}.${index}`, `${file}.${index + 1}`);
|
||||
renameSync(file, `${file}.1`);
|
||||
}
|
||||
appendFileSync(file, content.subarray(Math.max(0, content.length - maxBytes)), { mode: 0o600 });
|
||||
chmodSync(file, 0o600);
|
||||
}
|
||||
const log = message => rotateAppend(path.join(LOG_ROOT, 'supervisor.log'), `${new Date().toISOString()} ${message}\n`);
|
||||
|
||||
function requirePrivateFile(file) {
|
||||
const info = statSync(file);
|
||||
if (!info.isFile() || info.uid !== process.getuid() || info.mode & 0o077)
|
||||
throw new Error(`${file} must be owned by the current user with permissions 0600`);
|
||||
}
|
||||
|
||||
function preflight() {
|
||||
if (process.platform !== 'darwin') throw new Error('This service uses the macOS user LaunchAgent domain');
|
||||
for (const file of [NODE, SCRIPT, path.join(ROOT, 'scripts/dev.mjs'), path.join(ROOT, 'apps/runner/src/main.ts')])
|
||||
if (!statSync(file).isFile()) throw new Error(`Missing required file: ${file}`);
|
||||
requirePrivateFile(path.join(ROOT, '.env.local'));
|
||||
requirePrivateFile(RUNNER_ENV);
|
||||
const keys = readFileSync(RUNNER_ENV, 'utf8')
|
||||
.split('\n')
|
||||
.filter(line => line.trim() && !line.trim().startsWith('#'))
|
||||
.map(line => line.match(/^([A-Z][A-Z0-9_]*)=/)?.[1]);
|
||||
if (
|
||||
keys.some(
|
||||
key =>
|
||||
!key ||
|
||||
!/^(WF_RUNNER_[A-Z0-9_]+|WF_POC_PROFILE_FILE|WF_CONTROL_PLANE_URL|WF_TENANT_ID|WF_ENVIRONMENT_ID|WF_DEPLOYMENT_MODE)$/.test(
|
||||
key,
|
||||
),
|
||||
)
|
||||
)
|
||||
throw new Error('runner.env contains unsupported variables; keep API, model and database credentials out of it');
|
||||
for (const directory of [STATE_ROOT, LOG_ROOT, minimalEnvironment().TMPDIR]) {
|
||||
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
||||
chmodSync(directory, 0o700);
|
||||
}
|
||||
}
|
||||
|
||||
function launchctl(args, checked = true) {
|
||||
const result = spawnSync('/bin/launchctl', args, { encoding: 'utf8', env: minimalEnvironment(), timeout: 35000 });
|
||||
if (checked && result.status !== 0)
|
||||
throw new Error(`launchctl ${args[0]} failed: ${(result.stderr || result.error?.message || '').trim()}`);
|
||||
return result;
|
||||
}
|
||||
const registered = () => launchctl(['print', SERVICE], false).status === 0;
|
||||
|
||||
function status() {
|
||||
const result = launchctl(['print', SERVICE], false);
|
||||
let state = null;
|
||||
try {
|
||||
state = JSON.parse(readFileSync(STATE_FILE, 'utf8'));
|
||||
} catch {
|
||||
/* A stopped or never-started service has no state. */
|
||||
}
|
||||
return {
|
||||
label: LABEL,
|
||||
installed: existsSync(PLIST),
|
||||
registered: result.status === 0,
|
||||
state: result.status === 0 ? (result.stdout.match(/^\s*state = (.+)$/m)?.[1] ?? 'unknown') : 'stopped',
|
||||
pid: result.status === 0 ? Number(result.stdout.match(/^\s*pid = (\d+)$/m)?.[1]) || null : null,
|
||||
lastExitCode: result.stdout?.match(/^\s*last exit code = (.+)$/m)?.[1] ?? null,
|
||||
workspace: ROOT,
|
||||
logs: LOG_ROOT,
|
||||
children: result.status === 0 ? (state?.children ?? []) : [],
|
||||
};
|
||||
}
|
||||
|
||||
async function supervise() {
|
||||
preflight();
|
||||
process.umask(0o077);
|
||||
const children = [];
|
||||
let stopping = false;
|
||||
let finish;
|
||||
const stopped = new Promise(resolve => {
|
||||
finish = resolve;
|
||||
});
|
||||
const stop = async code => {
|
||||
if (stopping) return;
|
||||
stopping = true;
|
||||
log(`Stopping this supervisor's ${children.length} children`);
|
||||
await Promise.all(
|
||||
children.map(
|
||||
child =>
|
||||
new Promise(resolve => {
|
||||
if (child.exitCode !== null || child.signalCode !== null) return resolve();
|
||||
const timer = setTimeout(() => child.kill('SIGKILL'), 20000);
|
||||
child.once('close', () => {
|
||||
clearTimeout(timer);
|
||||
resolve();
|
||||
});
|
||||
child.kill('SIGTERM');
|
||||
}),
|
||||
),
|
||||
);
|
||||
rmSync(STATE_FILE, { force: true });
|
||||
finish(code);
|
||||
};
|
||||
for (const signal of ['SIGTERM', 'SIGINT'])
|
||||
process.once(signal, () => {
|
||||
void stop(0);
|
||||
});
|
||||
const specifications = [
|
||||
{ name: 'platform', args: [path.join(ROOT, 'scripts/dev.mjs')] },
|
||||
{
|
||||
name: 'runner',
|
||||
args: [`--env-file=${RUNNER_ENV}`, '--import', 'tsx', path.join(ROOT, 'apps/runner/src/main.ts')],
|
||||
},
|
||||
];
|
||||
for (const specification of specifications) {
|
||||
// Deliberately build a fresh environment for each child. Only dev.mjs loads .env.local;
|
||||
// Node loads the separate runner.env directly for the runner process.
|
||||
const child = spawn(NODE, specification.args, {
|
||||
cwd: ROOT,
|
||||
env: minimalEnvironment(),
|
||||
shell: false,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
children.push(child);
|
||||
for (const stream of [child.stdout, child.stderr])
|
||||
stream.on('data', bytes => rotateAppend(path.join(LOG_ROOT, `${specification.name}.log`), bytes));
|
||||
child.once('error', error => {
|
||||
log(`${specification.name}: ${error.message}`);
|
||||
void stop(1);
|
||||
});
|
||||
child.once('exit', code => {
|
||||
log(`${specification.name} exited ${code ?? 'by signal'}`);
|
||||
if (!stopping) void stop(code || 1);
|
||||
});
|
||||
log(`Started ${specification.name} pid=${child.pid}`);
|
||||
}
|
||||
writeFileSync(
|
||||
STATE_FILE,
|
||||
`${JSON.stringify({ supervisorPid: process.pid, startedAt: new Date().toISOString(), children: children.map((child, index) => ({ name: specifications[index].name, pid: child.pid })) }, null, 2)}\n`,
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
return stopped;
|
||||
}
|
||||
|
||||
export async function main(argv = process.argv.slice(2)) {
|
||||
const action = parseServiceAction(argv);
|
||||
if (action === 'run') return supervise();
|
||||
if (process.platform !== 'darwin') throw new Error('This service uses macOS launchd');
|
||||
if (action === 'install') {
|
||||
preflight();
|
||||
const content = launchAgentPlist();
|
||||
if (existsSync(PLIST) && readFileSync(PLIST, 'utf8') !== content && registered())
|
||||
throw new Error('Stop this LaunchAgent before changing its installed configuration');
|
||||
mkdirSync(path.dirname(PLIST), { recursive: true });
|
||||
const temporary = `${PLIST}.tmp`;
|
||||
writeFileSync(temporary, content, { mode: 0o600 });
|
||||
chmodSync(temporary, 0o600);
|
||||
const validated = spawnSync('/usr/bin/plutil', ['-lint', temporary], { encoding: 'utf8' });
|
||||
if (validated.status !== 0) {
|
||||
rmSync(temporary, { force: true });
|
||||
throw new Error('Generated LaunchAgent plist failed validation');
|
||||
}
|
||||
renameSync(temporary, PLIST);
|
||||
launchctl(['enable', SERVICE]);
|
||||
if (!registered()) launchctl(['bootstrap', DOMAIN, PLIST]);
|
||||
launchctl(['kickstart', SERVICE]);
|
||||
} else if (action === 'start') {
|
||||
preflight();
|
||||
if (!existsSync(PLIST)) throw new Error('Run install once to create this user LaunchAgent');
|
||||
if (readFileSync(PLIST, 'utf8') !== launchAgentPlist())
|
||||
throw new Error('Installed LaunchAgent differs from this checkout; stop and install it again');
|
||||
launchctl(['enable', SERVICE]);
|
||||
if (!registered()) launchctl(['bootstrap', DOMAIN, PLIST]);
|
||||
launchctl(['kickstart', SERVICE]);
|
||||
} else if (action === 'stop') {
|
||||
launchctl(['disable', SERVICE]);
|
||||
if (registered()) launchctl(['bootout', SERVICE]);
|
||||
}
|
||||
console.log(JSON.stringify(status(), null, 2));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url)
|
||||
main()
|
||||
.then(code => {
|
||||
process.exitCode = code;
|
||||
})
|
||||
.catch(error => {
|
||||
if (process.argv[2] === 'run') log(error.message);
|
||||
else console.error(error.message);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
48
scripts/local-poc-service.test.mjs
Normal file
48
scripts/local-poc-service.test.mjs
Normal file
@ -0,0 +1,48 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { LABEL, launchAgentPlist, minimalEnvironment, parseServiceAction, rotateAppend } from './local-poc-service.mjs';
|
||||
|
||||
test('defaults to read-only status and rejects arbitrary service commands', () => {
|
||||
assert.equal(parseServiceAction([]), 'status');
|
||||
for (const action of ['install', 'start', 'stop', 'status']) assert.equal(parseServiceAction([action]), action);
|
||||
for (const args of [['restart', 'other-service'], ['start', '--force'], ['killall']])
|
||||
assert.throws(() => parseServiceAction(args));
|
||||
});
|
||||
|
||||
test('launchd plist contains only fixed commands and is valid without registration', () => {
|
||||
const temporary = mkdtempSync(path.join(os.tmpdir(), 'wishforge-launchd-test-'));
|
||||
try {
|
||||
const plist = launchAgentPlist('/owned/path & space/wishforge', '/owned/home');
|
||||
assert.ok(plist.includes(LABEL));
|
||||
assert.ok(plist.includes('/owned/path & space/wishforge/scripts/local-poc-service.mjs'));
|
||||
assert.ok(plist.includes('<key>KeepAlive</key><true/>'));
|
||||
assert.ok(plist.includes('<key>ThrottleInterval</key><integer>15</integer>'));
|
||||
assert.ok(!plist.includes('WF_RUNNER_TOKEN'));
|
||||
assert.ok(!plist.includes('.env.local'));
|
||||
const file = path.join(temporary, 'agent.plist');
|
||||
writeFileSync(file, plist);
|
||||
if (process.platform === 'darwin') assert.equal(spawnSync('/usr/bin/plutil', ['-lint', file]).status, 0);
|
||||
assert.deepEqual(Object.keys(minimalEnvironment()).sort(), ['HOME', 'LANG', 'PATH', 'TMPDIR']);
|
||||
} finally {
|
||||
rmSync(temporary, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('private logs rotate with a bounded number of retained files', () => {
|
||||
const temporary = mkdtempSync(path.join(os.tmpdir(), 'wishforge-log-test-'));
|
||||
try {
|
||||
const file = path.join(temporary, 'logs/service.log');
|
||||
for (const marker of ['1111', '2222', '3333', '4444', '5555']) rotateAppend(file, marker, 5, 2);
|
||||
assert.deepEqual(readdirSync(path.dirname(file)).sort(), ['service.log', 'service.log.1', 'service.log.2']);
|
||||
assert.equal(readFileSync(file, 'utf8'), '5555');
|
||||
assert.equal(readFileSync(`${file}.2`, 'utf8'), '3333');
|
||||
assert.equal(statSync(file).mode & 0o777, 0o600);
|
||||
assert.equal(statSync(path.dirname(file)).mode & 0o777, 0o700);
|
||||
} finally {
|
||||
rmSync(temporary, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
195
scripts/poc-project-check.mjs
Normal file
195
scripts/poc-project-check.mjs
Normal file
@ -0,0 +1,195 @@
|
||||
#!/usr/bin/env node
|
||||
// Protected host-side commands. Project settings use this file's absolute path; never copy it into the agent workspace.
|
||||
import { spawn } from 'node:child_process';
|
||||
import { access, mkdir, readFile, readdir, realpath } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath, pathToFileURL } from 'node:url';
|
||||
|
||||
const APPROVED_ORIGINS = new Set([
|
||||
'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
'ssh://git@gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
]);
|
||||
const TOOLS = {
|
||||
git: '/usr/bin/git',
|
||||
node: '/opt/homebrew/bin/node',
|
||||
npm: '/opt/homebrew/bin/npm',
|
||||
go: '/opt/homebrew/bin/go',
|
||||
};
|
||||
const CACHE = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../.wishforge/poc-check-cache');
|
||||
|
||||
export function parseCheckMode(argv) {
|
||||
if (argv.length !== 1 || !['install', 'verify'].includes(argv[0]))
|
||||
throw new Error('Usage: poc-project-check.mjs install|verify (no custom commands or targets)');
|
||||
return argv[0];
|
||||
}
|
||||
|
||||
export function checkEnvironment(cacheRoot = CACHE, inherited = process.env, network = false) {
|
||||
return {
|
||||
PATH: '/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin',
|
||||
HOME: path.join(cacheRoot, 'home'),
|
||||
TMPDIR: path.join(cacheRoot, 'tmp'),
|
||||
LANG: 'C.UTF-8',
|
||||
CI: 'true',
|
||||
GOPATH: path.join(cacheRoot, 'go'),
|
||||
GOMODCACHE: path.join(cacheRoot, 'go/pkg/mod'),
|
||||
GOCACHE: path.join(cacheRoot, 'go-build'),
|
||||
GOTOOLCHAIN: 'local',
|
||||
GOWORK: 'off',
|
||||
GOFLAGS: '-mod=readonly',
|
||||
GOPROXY: network ? 'https://proxy.golang.org,direct' : 'off',
|
||||
GOSUMDB: 'sum.golang.org',
|
||||
npm_config_cache: path.join(cacheRoot, 'npm'),
|
||||
npm_config_userconfig: path.join(cacheRoot, 'home/user.npmrc'),
|
||||
npm_config_globalconfig: path.join(cacheRoot, 'home/global.npmrc'),
|
||||
npm_config_ignore_scripts: 'true',
|
||||
GIT_CONFIG_NOSYSTEM: '1',
|
||||
GIT_CONFIG_GLOBAL: '/dev/null',
|
||||
GIT_TERMINAL_PROMPT: '0',
|
||||
...(network
|
||||
? Object.fromEntries(
|
||||
['HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY'].filter(key => inherited[key]).map(key => [key, inherited[key]]),
|
||||
)
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
function capture(args, cwd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn(TOOLS.git, ['-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', ...args], {
|
||||
cwd,
|
||||
env: checkEnvironment(),
|
||||
shell: false,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
let output = '';
|
||||
const timer = setTimeout(() => child.kill('SIGKILL'), 10000);
|
||||
child.stdout.on('data', bytes => {
|
||||
output += String(bytes);
|
||||
if (output.length > 16000) child.kill('SIGKILL');
|
||||
});
|
||||
child.stderr.resume();
|
||||
child.once('error', error => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
child.once('close', code => {
|
||||
clearTimeout(timer);
|
||||
code === 0 ? resolve(output.trim()) : reject(new Error('The current directory is not an approved Git workspace'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function assertApprovedWorkspace(cwd) {
|
||||
const workspace = await realpath(cwd);
|
||||
const manifest = JSON.parse(await readFile(path.join(workspace, 'package.json'), 'utf8'));
|
||||
const goModule = await readFile(path.join(workspace, 'go.mod'), 'utf8');
|
||||
if (manifest.name !== 'droboticflow' || !/^module droboticflow\s*$/m.test(goModule))
|
||||
throw new Error('Protected checks require the droboticflow Node and Go project');
|
||||
// verifyWorkspace exports the exact checkpoint without .git. The caller establishes
|
||||
// repository/SHA authority before export; these checks are sanity checks, not a sandbox.
|
||||
const hasGit = await access(path.join(workspace, '.git')).then(
|
||||
() => true,
|
||||
() => false,
|
||||
);
|
||||
if (!hasGit) return { workspace, head: null };
|
||||
const top = await realpath(await capture(['rev-parse', '--show-toplevel'], workspace));
|
||||
if (workspace !== top) throw new Error('Run the protected checks at the Git repository root');
|
||||
const origins = (await capture(['config', '--local', '--get-all', 'remote.origin.url'], workspace)).split('\n');
|
||||
if (origins.length !== 1 || !APPROVED_ORIGINS.has(origins[0]))
|
||||
throw new Error('The Git origin must be the approved droboticflow repository');
|
||||
const head = await capture(['rev-parse', '--verify', 'HEAD^{commit}'], workspace);
|
||||
if (!/^[a-f0-9]{40}$/.test(head)) throw new Error('The Git workspace has no immutable HEAD');
|
||||
return { workspace, head };
|
||||
}
|
||||
|
||||
export function checkPlan(mode, workspace, frontendTests = []) {
|
||||
if (mode === 'install')
|
||||
return [
|
||||
{
|
||||
label: 'npm ci (lifecycle scripts disabled)',
|
||||
executable: TOOLS.npm,
|
||||
args: ['ci', '--ignore-scripts', '--no-audit', '--no-fund'],
|
||||
},
|
||||
{ label: 'Go module download', executable: TOOLS.go, args: ['mod', 'download'] },
|
||||
];
|
||||
if (mode !== 'verify' || !frontendTests.length)
|
||||
throw new Error('Verification requires the protected frontend test suite');
|
||||
return [
|
||||
{
|
||||
label: 'lint:web',
|
||||
executable: TOOLS.node,
|
||||
args: [path.join(workspace, 'node_modules/typescript/bin/tsc'), '--noEmit'],
|
||||
},
|
||||
{ label: 'test:frontend', executable: TOOLS.node, args: ['--test', ...frontendTests] },
|
||||
{
|
||||
label: 'build:web',
|
||||
executable: TOOLS.node,
|
||||
args: [path.join(workspace, 'node_modules/vite/bin/vite.js'), 'build'],
|
||||
},
|
||||
{ label: 'go test ./...', executable: TOOLS.go, args: ['test', './...'] },
|
||||
];
|
||||
}
|
||||
|
||||
async function runCheck(check, cwd, env) {
|
||||
const started = Date.now();
|
||||
console.log(`[poc-check] START ${check.label}`);
|
||||
return new Promise(resolve => {
|
||||
const child = spawn(check.executable, check.args, {
|
||||
cwd,
|
||||
env,
|
||||
shell: false,
|
||||
stdio: ['ignore', 'inherit', 'inherit'],
|
||||
});
|
||||
const heartbeat = setInterval(
|
||||
() => console.log(`[poc-check] RUNNING ${check.label} (${Math.round((Date.now() - started) / 1000)}s)`),
|
||||
30000,
|
||||
);
|
||||
const timer = setTimeout(() => child.kill('SIGKILL'), 9 * 60_000);
|
||||
child.once('error', error => console.error(`[poc-check] ${error.message}`));
|
||||
child.once('close', code => {
|
||||
clearInterval(heartbeat);
|
||||
clearTimeout(timer);
|
||||
const result = { check: check.label, exitCode: code ?? 1, durationMs: Date.now() - started };
|
||||
console.log(`[poc-check] RESULT ${JSON.stringify(result)}`);
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function main(argv = process.argv.slice(2)) {
|
||||
const mode = parseCheckMode(argv);
|
||||
const { workspace, head } = await assertApprovedWorkspace(process.cwd());
|
||||
const env = checkEnvironment(CACHE, process.env, mode === 'install');
|
||||
for (const directory of [CACHE, env.HOME, env.TMPDIR, env.GOPATH, env.GOMODCACHE, env.GOCACHE, env.npm_config_cache])
|
||||
await mkdir(directory, { recursive: true, mode: 0o700 });
|
||||
const tests =
|
||||
mode === 'verify'
|
||||
? (await readdir(path.join(workspace, 'tests/frontend')))
|
||||
.filter(file => /^[A-Za-z0-9_.-]+\.test\.mjs$/.test(file))
|
||||
.sort()
|
||||
.map(file => `tests/frontend/${file}`)
|
||||
: [];
|
||||
console.log(`[poc-check] ${JSON.stringify({ mode, sourceCommit: head, workspace })}`);
|
||||
const results = [];
|
||||
for (const check of checkPlan(mode, workspace, tests)) {
|
||||
const result = await runCheck(check, workspace, env);
|
||||
results.push(result);
|
||||
// Keep the failing command's diagnostics at the end of the evidence tail. Later
|
||||
// successful build/test output would otherwise erase the error needed by repair.
|
||||
if (result.exitCode !== 0) break;
|
||||
}
|
||||
const passed = results.every(result => result.exitCode === 0);
|
||||
console.log(`[poc-check] SUMMARY ${JSON.stringify({ mode, sourceCommit: head, passed, results })}`);
|
||||
return passed ? 0 : 1;
|
||||
}
|
||||
|
||||
if (process.argv[1] && pathToFileURL(path.resolve(process.argv[1])).href === import.meta.url)
|
||||
main()
|
||||
.then(code => {
|
||||
process.exitCode = code;
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(`[poc-check] ${error.message}`);
|
||||
process.exitCode = 2;
|
||||
});
|
||||
102
scripts/poc-project-check.test.mjs
Normal file
102
scripts/poc-project-check.test.mjs
Normal file
@ -0,0 +1,102 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { assertApprovedWorkspace, checkEnvironment, checkPlan, parseCheckMode } from './poc-project-check.mjs';
|
||||
|
||||
test('only fixed install and verification modes are accepted', () => {
|
||||
assert.equal(parseCheckMode(['install']), 'install');
|
||||
assert.equal(parseCheckMode(['verify']), 'verify');
|
||||
for (const args of [[], ['deploy'], ['verify', '--target', 'production'], ['sh', '-c', 'anything']])
|
||||
assert.throws(() => parseCheckMode(args), /Usage/);
|
||||
const plan = checkPlan('verify', '/owned/workspace', ['tests/frontend/check.test.mjs']);
|
||||
assert.deepEqual(
|
||||
plan.map(item => item.label),
|
||||
['lint:web', 'test:frontend', 'build:web', 'go test ./...'],
|
||||
);
|
||||
assert.ok(plan.every(item => !item.args.includes('run')));
|
||||
assert.ok(checkPlan('install', '/owned/workspace')[0].args.includes('--ignore-scripts'));
|
||||
});
|
||||
|
||||
test('installation and verification share caches without inheriting host credentials', () => {
|
||||
const inherited = {
|
||||
HOME: '/operator',
|
||||
KUBECONFIG: '/secret',
|
||||
ANTHROPIC_AUTH_TOKEN: 'secret',
|
||||
DATABASE_URL: 'secret',
|
||||
NODE_OPTIONS: '--require evil',
|
||||
HTTPS_PROXY: 'http://proxy.invalid',
|
||||
};
|
||||
const install = checkEnvironment('/private/cache', inherited, true);
|
||||
const verify = checkEnvironment('/private/cache', inherited, false);
|
||||
for (const name of ['HOME', 'GOPATH', 'GOMODCACHE', 'GOCACHE', 'npm_config_cache'])
|
||||
assert.equal(install[name], verify[name]);
|
||||
for (const name of ['KUBECONFIG', 'ANTHROPIC_AUTH_TOKEN', 'DATABASE_URL', 'NODE_OPTIONS'])
|
||||
assert.equal(install[name], undefined);
|
||||
assert.equal(verify.GOPROXY, 'off');
|
||||
assert.equal(install.HTTPS_PROXY, inherited.HTTPS_PROXY);
|
||||
assert.equal(verify.HTTPS_PROXY, undefined);
|
||||
});
|
||||
|
||||
test('stops verification at the first failure so repair evidence retains the actual diagnostic', async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), 'poc-check-failure-'));
|
||||
try {
|
||||
await writeFile(path.join(directory, 'package.json'), JSON.stringify({ name: 'droboticflow' }));
|
||||
await writeFile(path.join(directory, 'go.mod'), 'module droboticflow\n');
|
||||
await mkdir(path.join(directory, 'node_modules/typescript/bin'), { recursive: true });
|
||||
await mkdir(path.join(directory, 'tests/frontend'), { recursive: true });
|
||||
await writeFile(
|
||||
path.join(directory, 'node_modules/typescript/bin/tsc'),
|
||||
'process.stderr.write("src/Layout.tsx(1,1): error TS2322: fixture type error\\n"); process.exit(2);',
|
||||
);
|
||||
await writeFile(
|
||||
path.join(directory, 'tests/frontend/never-run.test.mjs'),
|
||||
'throw new Error("Later checks must not run");',
|
||||
);
|
||||
const result = spawnSync(
|
||||
'/opt/homebrew/bin/node',
|
||||
[fileURLToPath(new URL('./poc-project-check.mjs', import.meta.url)), 'verify'],
|
||||
{
|
||||
cwd: directory,
|
||||
encoding: 'utf8',
|
||||
},
|
||||
);
|
||||
assert.equal(result.status, 1);
|
||||
assert.match(result.stderr, /TS2322: fixture type error/);
|
||||
assert.ok(!result.stdout.includes('START test:frontend'));
|
||||
assert.ok(!result.stdout.includes('START build:web'));
|
||||
assert.ok(!result.stdout.includes('START go test'));
|
||||
assert.match(result.stdout, /"passed":false/);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('accepts a verifier export, checks a repository origin when metadata exists, and rejects another project', async () => {
|
||||
const directory = await mkdtemp(path.join(os.tmpdir(), 'poc-check-contract-'));
|
||||
const git = args => {
|
||||
const result = spawnSync('/usr/bin/git', ['-c', 'core.hooksPath=/dev/null', ...args], {
|
||||
cwd: directory,
|
||||
encoding: 'utf8',
|
||||
});
|
||||
assert.equal(result.status, 0, result.stderr);
|
||||
};
|
||||
try {
|
||||
await writeFile(path.join(directory, 'package.json'), JSON.stringify({ name: 'droboticflow' }));
|
||||
await writeFile(path.join(directory, 'go.mod'), 'module droboticflow\n\ngo 1.24.0\n');
|
||||
assert.equal((await assertApprovedWorkspace(directory)).head, null);
|
||||
git(['init']);
|
||||
git(['-c', 'user.name=Fixture', '-c', 'user.email=fixture@invalid', 'commit', '--allow-empty', '-m', 'Fixture']);
|
||||
git(['remote', 'add', 'origin', 'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git']);
|
||||
assert.match((await assertApprovedWorkspace(directory)).head, /^[a-f0-9]{40}$/);
|
||||
git(['remote', 'set-url', 'origin', 'https://example.org/other.git']);
|
||||
await assert.rejects(assertApprovedWorkspace(directory), /approved droboticflow/);
|
||||
await writeFile(path.join(directory, 'package.json'), JSON.stringify({ name: 'other' }));
|
||||
await assert.rejects(assertApprovedWorkspace(directory), /Node and Go project/);
|
||||
} finally {
|
||||
await rm(directory, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
48
scripts/poc-release.ts
Normal file
48
scripts/poc-release.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { loadPocProfile } from '../packages/runtime/src/poc-profile.js';
|
||||
import { PocDeploymentAdapter, pocReleaseSchema } from '../packages/runtime/src/poc-release.js';
|
||||
import { buildPocRelease } from '../packages/runtime/src/poc-build.js';
|
||||
|
||||
const [operation, profileFile, stateDirectory, workspaceOrRelease, sourceCommit, releaseId] = process.argv.slice(2);
|
||||
if (!operation || !profileFile || !stateDirectory)
|
||||
throw Error(
|
||||
'Usage: tsx scripts/poc-release.ts inspect|build|deploy|reconcile PROFILE STATE_DIRECTORY [WORKSPACE SOURCE_SHA RELEASE_ID | RELEASE_FILE | RELEASE_ID]',
|
||||
);
|
||||
const profile = await loadPocProfile(profileFile);
|
||||
const adapter = new PocDeploymentAdapter(profile, path.join(stateDirectory, 'deployments'));
|
||||
switch (operation) {
|
||||
case 'inspect':
|
||||
console.log(JSON.stringify(await adapter.snapshot(), null, 2));
|
||||
break;
|
||||
case 'build': {
|
||||
if (!workspaceOrRelease || !sourceCommit || !releaseId)
|
||||
throw Error('build requires workspace, full source SHA and release ID');
|
||||
const result = await buildPocRelease({
|
||||
profile,
|
||||
stateDirectory,
|
||||
workspace: workspaceOrRelease,
|
||||
sourceCommit,
|
||||
releaseId,
|
||||
onProgress: message => console.error(message),
|
||||
});
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
break;
|
||||
}
|
||||
case 'deploy': {
|
||||
if (!workspaceOrRelease) throw Error('deploy requires a release JSON file');
|
||||
const journal = await adapter.deploy(
|
||||
pocReleaseSchema.parse(JSON.parse(await readFile(workspaceOrRelease, 'utf8'))),
|
||||
);
|
||||
console.log(JSON.stringify(journal, null, 2));
|
||||
if (journal.state !== 'succeeded') process.exitCode = 1;
|
||||
break;
|
||||
}
|
||||
case 'reconcile': {
|
||||
if (!workspaceOrRelease) throw Error('reconcile requires release ID');
|
||||
console.log(JSON.stringify(await adapter.reconcile(workspaceOrRelease), null, 2));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw Error('Unknown POC operation');
|
||||
}
|
||||
94
scripts/recover-local-poc-run.ts
Normal file
94
scripts/recover-local-poc-run.ts
Normal file
@ -0,0 +1,94 @@
|
||||
/** Operator repair of an interrupted local attempt; never marks code verified or deployed. */
|
||||
import { readFile, mkdir, rm } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { PgStore } from '../apps/api/src/db/store.js';
|
||||
import { record, touch } from '../apps/api/src/domain/control-plane.js';
|
||||
import { GitWorkspace, readLocalPocBinding, type PortableCheckpoint } from '../packages/runtime/src/index.js';
|
||||
import type { Run } from '../packages/contracts/src/index.js';
|
||||
|
||||
process.loadEnvFile('.env.local');
|
||||
const [runId] = process.argv.slice(2);
|
||||
if (!runId || process.env.WF_DEPLOYMENT_MODE !== 'development')
|
||||
throw Error('Usage: recover-local-poc-run <run-id>, development only');
|
||||
const binding = readLocalPocBinding(process.env.WF_LOCAL_POC_BINDING_FILE)!;
|
||||
if (!binding) throw Error('POC operator binding required');
|
||||
const recovery = path.resolve('.wishforge/local-poc/recovery', runId);
|
||||
if (path.dirname(recovery) !== path.resolve('.wishforge/local-poc/recovery')) throw Error('Invalid run');
|
||||
const checkpoint = JSON.parse(await readFile(path.join(recovery, 'checkpoint.json'), 'utf8')) as PortableCheckpoint;
|
||||
if (checkpoint.runId !== runId || checkpoint.provider !== 'claude-code')
|
||||
throw Error('Checkpoint belongs to another task/provider');
|
||||
const store = new PgStore(process.env.DATABASE_URL!);
|
||||
const operator = '11111111-1111-4111-8111-111111111111';
|
||||
let workspace: string | undefined;
|
||||
try {
|
||||
const current = await store.read(binding.tenantId, tx => tx.get<Run>('run', runId));
|
||||
if (
|
||||
!current ||
|
||||
current.projectId !== binding.projectId ||
|
||||
!['waiting', 'paused'].includes(current.status) ||
|
||||
current.checkpointId
|
||||
)
|
||||
throw Error('Only an interrupted bound POC run without a durable checkpoint may be recovered');
|
||||
if (current.usedTokens >= current.budget.maxTokens) throw Error('Adjust the run budget explicitly before recovery');
|
||||
const git = new GitWorkspace(undefined, {
|
||||
localSshTransport: {
|
||||
repositoryUrl: binding.repositoryUrl,
|
||||
sshUrl: 'ssh://git@gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
home: binding.gitHome,
|
||||
},
|
||||
});
|
||||
await mkdir(path.join(recovery, 'operator-validation'), { recursive: true, mode: 0o700 });
|
||||
const restored = await git.restore({
|
||||
checkpoint,
|
||||
repository: binding.repositoryUrl,
|
||||
root: path.join(recovery, 'operator-validation'),
|
||||
runId: runId + '-recovery',
|
||||
});
|
||||
workspace = restored.workspace;
|
||||
await store.transaction(binding.tenantId, async tx => {
|
||||
if (!['owner', 'admin'].includes((await tx.membership(operator)) ?? ''))
|
||||
throw Error('Operator membership required');
|
||||
const run = await tx.get<Run>('run', runId);
|
||||
if (!run || run.version !== current.version || run.checkpointId || !['waiting', 'paused'].includes(run.status))
|
||||
throw Error('Run changed during recovery');
|
||||
const cp = {
|
||||
...record(binding.tenantId),
|
||||
runId,
|
||||
revision: run.revision,
|
||||
checkpoint,
|
||||
recoveredBy: operator,
|
||||
recoveryReason:
|
||||
'Recovered successful file operations from the completed local Claude attempt after budget settlement interrupted checkpoint persistence',
|
||||
};
|
||||
await tx.put('checkpoint', cp);
|
||||
run.checkpointId = cp.id;
|
||||
run.sourceCommit = checkpoint.sourceCommit;
|
||||
run.stage = 'verify';
|
||||
if (run.lease) run.lease = { ...run.lease, token: run.lease.token + 1, expiresAt: new Date().toISOString() };
|
||||
await tx.put('run', touch(run));
|
||||
await tx.event(
|
||||
runId,
|
||||
'checkpoint.recovered',
|
||||
'Operator restored the completed Claude file changes; independent verification is still required.',
|
||||
{
|
||||
checkpointId: cp.id,
|
||||
sourceCommit: checkpoint.sourceCommit,
|
||||
baseCommit: checkpoint.baseCommit,
|
||||
operatorId: operator,
|
||||
},
|
||||
);
|
||||
});
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
runId,
|
||||
sourceCommit: checkpoint.sourceCommit,
|
||||
stage: 'verify',
|
||||
resumed: false,
|
||||
verified: false,
|
||||
deployed: false,
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
await store.close();
|
||||
if (workspace) await rm(workspace, { recursive: true, force: true });
|
||||
}
|
||||
38
scripts/setup-local-access.mjs
Normal file
38
scripts/setup-local-access.mjs
Normal file
@ -0,0 +1,38 @@
|
||||
import { randomBytes, scryptSync } from 'node:crypto';
|
||||
import { mkdir, open, stat } from 'node:fs/promises';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
|
||||
const file = resolve(process.env.WF_LOCAL_ACCESS_FILE || '.wishforge/local-access.json');
|
||||
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
||||
try {
|
||||
const existing = await stat(file);
|
||||
if (!existing.isFile() || (existing.mode & 0o077) !== 0 || existing.uid !== process.getuid())
|
||||
throw new Error('Existing credentials must be operator-owned with mode 0600.');
|
||||
console.log(`Existing local access credentials retained: ${file}`);
|
||||
} catch (error) {
|
||||
if (error.code !== 'ENOENT') throw error;
|
||||
const password = randomBytes(32).toString('base64url');
|
||||
const salt = randomBytes(32).toString('hex');
|
||||
const handle = await open(file, 'wx', 0o600);
|
||||
try {
|
||||
await handle.writeFile(
|
||||
`${JSON.stringify(
|
||||
{
|
||||
username: 'admin',
|
||||
password,
|
||||
salt,
|
||||
passwordHash: scryptSync(password, salt, 64).toString('hex'),
|
||||
sessionSecret: randomBytes(48).toString('base64url'),
|
||||
},
|
||||
null,
|
||||
2,
|
||||
)}\n`,
|
||||
);
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
console.log(`Created private local access credentials (0600): ${file}`);
|
||||
}
|
||||
console.log(
|
||||
'Set WF_LOCAL_ACCESS=true, WF_LOCAL_ACCESS_FILE to this absolute path, and WF_LOCAL_ALLOWED_ORIGINS to the exact browser origins. Credentials are never printed.',
|
||||
);
|
||||
179
scripts/setup-local-poc.ts
Normal file
179
scripts/setup-local-poc.ts
Normal file
@ -0,0 +1,179 @@
|
||||
/** Explicit local operator bootstrap; no wish, Git push, build or POC deployment is started here. */
|
||||
import { mkdir, readFile, writeFile, chmod } from 'node:fs/promises';
|
||||
import { homedir, networkInterfaces } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { PgStore } from '../apps/api/src/db/store.js';
|
||||
import { ControlPlane, touch } from '../apps/api/src/domain/control-plane.js';
|
||||
import { Releases } from '../apps/api/src/domain/releases.js';
|
||||
import { RunnerBroker } from '../apps/api/src/domain/runner-broker.js';
|
||||
import { loadPocProfile, localPocBindingSchema } from '../packages/runtime/src/index.js';
|
||||
import type { Actor, Environment, Project } from '../packages/contracts/src/index.js';
|
||||
|
||||
process.loadEnvFile('.env.local');
|
||||
if (process.env.WF_DEPLOYMENT_MODE !== 'development') throw Error('Explicit development mode required');
|
||||
const root = process.cwd(),
|
||||
directory = path.join(root, '.wishforge', 'local-poc');
|
||||
const bindingFile = path.join(directory, 'project.json');
|
||||
const profileFile = path.join(homedir(), '.config/wishforge/poc-profile.json');
|
||||
await loadPocProfile(profileFile);
|
||||
await mkdir(directory, { recursive: true, mode: 0o700 });
|
||||
const env = {
|
||||
...process.env,
|
||||
WF_CLAUDE_BIN: '/opt/homebrew/bin/claude',
|
||||
WF_CLAUDE_LOCAL_AUTH_HOME: homedir(),
|
||||
WF_PROVISIONER_BACKEND: 'local',
|
||||
WF_LOCAL_RUN_ROOT: path.join(homedir(), '.local/share/wishforge/runs'),
|
||||
};
|
||||
delete env.WF_LOCAL_POC_BINDING_FILE;
|
||||
const store = new PgStore(env.DATABASE_URL!);
|
||||
const actor: Actor = { tenantId: 'demo', userId: '11111111-1111-4111-8111-111111111111', kind: 'human', role: 'owner' };
|
||||
try {
|
||||
await store.assertSafeRole();
|
||||
if (!(await store.read(actor.tenantId, tx => tx.membership(actor.userId))))
|
||||
throw Error('Run db:seed before this bootstrap');
|
||||
const control = new ControlPlane(store, env),
|
||||
releases = new Releases(store, env);
|
||||
const existing = await readFile(bindingFile, 'utf8')
|
||||
.then(text => localPocBindingSchema.parse(JSON.parse(text)))
|
||||
.catch(() => undefined);
|
||||
let project = existing
|
||||
? await control.projects(actor).then(items => items.find(p => p.id === existing.projectId))
|
||||
: undefined;
|
||||
if (!project)
|
||||
project = await control.createProject(actor, {
|
||||
name: 'DroboticFlow POC · Claude 本机',
|
||||
repositoryUrl: 'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
targetBranch: 'poc',
|
||||
});
|
||||
let target = existing ? (await releases.environments(actor)).find(e => e.id === existing.environmentId) : undefined;
|
||||
if (!target)
|
||||
target = await releases.createEnvironment(actor, {
|
||||
projectId: project.id,
|
||||
name: '现有 POC · droboticflow',
|
||||
type: 'kubernetes',
|
||||
production: false,
|
||||
autoDeploy: true,
|
||||
architecture: 'linux/amd64',
|
||||
configVersion: 'adopt-existing-v1',
|
||||
secretVersionRefs: [],
|
||||
});
|
||||
const binding = localPocBindingSchema.parse({
|
||||
schemaVersion: 1,
|
||||
tenantId: actor.tenantId,
|
||||
projectId: project.id,
|
||||
environmentId: target.id,
|
||||
repositoryUrl: project.repositoryUrl,
|
||||
targetBranch: 'poc',
|
||||
profileFile,
|
||||
stateDirectory: path.join(homedir(), '.local/share/wishforge/poc'),
|
||||
gitHome: homedir(),
|
||||
});
|
||||
await writeFile(bindingFile, JSON.stringify(binding, null, 2) + '\n', { mode: 0o600 });
|
||||
env.WF_LOCAL_POC_BINDING_FILE = bindingFile;
|
||||
await new Releases(store, env).updateApplicationUrl(actor, target.id, { applicationUrl: 'http://120.48.164.19' });
|
||||
const checkScript = path.join(root, 'scripts/poc-project-check.mjs');
|
||||
await store.transaction(actor.tenantId, async tx => {
|
||||
const p = await tx.get<Project>('project', project!.id);
|
||||
if (!p) throw Error('Project disappeared');
|
||||
if (p.repositoryUrl !== binding.repositoryUrl || p.targetBranch !== 'poc')
|
||||
throw Error('Existing project repository changed');
|
||||
p.installCommands = [[process.execPath, checkScript, 'install']];
|
||||
p.verificationCommands = [[process.execPath, checkScript, 'verify']];
|
||||
p.autoDeploy = true;
|
||||
p.deliveryConfig = {
|
||||
environmentId: target!.id,
|
||||
imageRepository: 'ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc/droboticflow-api',
|
||||
architecture: 'linux/amd64',
|
||||
dockerfile: 'Dockerfile.api',
|
||||
sourceContext: '.',
|
||||
containerPort: 3001,
|
||||
healthPath: '/api/health',
|
||||
smokeChecks: [
|
||||
{ path: '/api/ready', expectedStatus: 200, bodyIncludes: '"ready":true' },
|
||||
{ path: '/', expectedStatus: 200, bodyIncludes: '<div id="root">' },
|
||||
],
|
||||
};
|
||||
await tx.put('project', touch(p));
|
||||
});
|
||||
const runnerFile = path.join(directory, 'runner.env'),
|
||||
keysFile = path.join(directory, 'runner-public.json');
|
||||
const configuredRunner = await readFile(runnerFile, 'utf8')
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
if (!configuredRunner) {
|
||||
const registration = await new RunnerBroker(store, new Releases(store, env), env).register(actor, target.id);
|
||||
await writeFile(keysFile, JSON.stringify(registration.publicKeys, null, 2) + '\n', { mode: 0o600 });
|
||||
const values = {
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_CONTROL_PLANE_URL: 'http://127.0.0.1:4000',
|
||||
WF_RUNNER_ALLOW_LOCAL_HTTP: 'true',
|
||||
WF_RUNNER_TOKEN: registration.token,
|
||||
WF_TENANT_ID: actor.tenantId,
|
||||
WF_ENVIRONMENT_ID: target.id,
|
||||
WF_RUNNER_PUBLIC_KEYS: keysFile,
|
||||
WF_RUNNER_STATE_DIR: path.join(directory, 'runner-state'),
|
||||
WF_POC_PROFILE_FILE: profileFile,
|
||||
WF_RUNNER_EXECUTE: 'true',
|
||||
};
|
||||
await writeFile(
|
||||
runnerFile,
|
||||
Object.entries(values)
|
||||
.map(([k, v]) => `${k}=${v}`)
|
||||
.join('\n') + '\n',
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
}
|
||||
const addresses = Object.entries(networkInterfaces())
|
||||
.filter(([name]) => /^en\d+$/.test(name))
|
||||
.flatMap(([, items]) => (items ?? []).filter(i => i.family === 'IPv4' && !i.internal).map(i => i.address));
|
||||
const origins = [
|
||||
'http://localhost:3000',
|
||||
'http://127.0.0.1:3000',
|
||||
...addresses.map(address => `http://${address}:3000`),
|
||||
];
|
||||
const settings = {
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_PROVISIONER_BACKEND: 'local',
|
||||
WF_LOCAL_RUN_ROOT: path.join(homedir(), '.local/share/wishforge/runs'),
|
||||
WF_TENANT_ID: 'demo',
|
||||
WF_CLAUDE_BIN: '/opt/homebrew/bin/claude',
|
||||
WF_CLAUDE_LOCAL_AUTH_HOME: homedir(),
|
||||
WF_LOCAL_POC_BINDING_FILE: bindingFile,
|
||||
WF_WEB_HOST: '0.0.0.0',
|
||||
WF_WEB_PORT: '3000',
|
||||
WF_WEB_SERVE_MODE: 'production',
|
||||
WF_LOCAL_ACCESS: 'true',
|
||||
WF_LOCAL_ACCESS_FILE: path.join(root, '.wishforge/local-access.json'),
|
||||
WF_LOCAL_ALLOWED_ORIGINS: origins.join(','),
|
||||
HOST: '127.0.0.1',
|
||||
};
|
||||
const old = await readFile('.env.local', 'utf8');
|
||||
let lines = old.split('\n');
|
||||
for (const [k, v] of Object.entries(settings)) {
|
||||
const i = lines.findIndex(line => line.startsWith(k + '='));
|
||||
if (i < 0) lines.push(`${k}=${v}`);
|
||||
else lines[i] = `${k}=${v}`;
|
||||
}
|
||||
await writeFile('.env.local', lines.join('\n') + '\n', { mode: 0o600 });
|
||||
await chmod('.env.local', 0o600);
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{
|
||||
status: 'configured',
|
||||
projectId: project.id,
|
||||
environmentId: target.id,
|
||||
branch: 'poc',
|
||||
provider: 'claude-code',
|
||||
webOrigins: origins,
|
||||
bindingFile,
|
||||
runnerConfigFile: runnerFile,
|
||||
credentialsFile: settings.WF_LOCAL_ACCESS_FILE,
|
||||
started: false,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
await store.close();
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
// Creates or completes the local-only configuration. Existing values are never
|
||||
// changed, so it is safe to run again after pulling new versions.
|
||||
import { generateKeyPairSync, randomBytes } from 'node:crypto';
|
||||
import { homedir } from 'node:os';
|
||||
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
@ -82,7 +83,7 @@ add('S3_SECRET_ACCESS_KEY', 'wishforge-minio-dev-only');
|
||||
// Local execution: the local provisioner runs each generation's executor and verifier on this host.
|
||||
add('WF_PROVISIONER_BACKEND', 'local');
|
||||
add('WF_PROVISIONER_URL', 'http://127.0.0.1:4450');
|
||||
add('WF_LOCAL_RUN_ROOT', resolve(root, '.wishforge/runs'));
|
||||
add('WF_LOCAL_RUN_ROOT', resolve(homedir(), '.local/share/wishforge/runs'));
|
||||
add('WF_LOCAL_IDE_URL', 'http://127.0.0.1:4300/');
|
||||
|
||||
// Cloud IDE gateway (npm run dev:ide).
|
||||
|
||||
@ -53,7 +53,10 @@ const git = (cwd: string, ...args: string[]) =>
|
||||
type Agent = (input: ProviderStart) => Promise<string>;
|
||||
/** Stands in for Codex: edits the workspace through a script and reports like an adapter. */
|
||||
class ScriptedAgent extends ManagedProvider {
|
||||
constructor(private readonly agent: Agent) {
|
||||
constructor(
|
||||
private readonly agent: Agent,
|
||||
private readonly usage: Record<string, unknown> = { input_tokens: 100, output_tokens: 20 },
|
||||
) {
|
||||
super();
|
||||
}
|
||||
capabilities() {
|
||||
@ -74,7 +77,7 @@ class ScriptedAgent extends ManagedProvider {
|
||||
this.emit(attempt, 'command', { command: 'node edit.mjs', exitCode: 0 });
|
||||
this.emit(attempt, 'file_change', { changes: [{ path: 'greeting.txt', kind: 'update' }] });
|
||||
this.emit(attempt, 'output', { text: summary });
|
||||
this.emit(attempt, 'usage', { input_tokens: 100, output_tokens: 20 });
|
||||
this.emit(attempt, 'usage', this.usage);
|
||||
this.finish(attempt, 'completed');
|
||||
} catch (error) {
|
||||
this.finish(attempt, 'failed', error instanceof Error ? error.message : String(error));
|
||||
@ -163,7 +166,11 @@ async function submit(projectId: string, goal: string, key: string, extra: Recor
|
||||
return run_id;
|
||||
}
|
||||
/** What the dispatcher and the local provisioner do: reserve resources and issue per-generation identities. */
|
||||
async function provision(runId: string, agent: Agent, settings: { installProxy?: string } = {}) {
|
||||
async function provision(
|
||||
runId: string,
|
||||
agent: Agent,
|
||||
settings: { installProxy?: string; usage?: Record<string, unknown>; fetcher?: typeof fetch } = {},
|
||||
) {
|
||||
const generation = (await services.control.run(owner, runId)).pendingStartId!;
|
||||
await store.transaction(TENANT, tx => reserveExecutionGrant(tx, runId, generation, env));
|
||||
const tokens = new ServiceTokens(env.WF_INTERNAL_TOKEN_KEY);
|
||||
@ -192,7 +199,7 @@ async function provision(runId: string, agent: Agent, settings: { installProxy?:
|
||||
new ScriptedAgent(async start => {
|
||||
prompts.push(start.prompt);
|
||||
return agent(start);
|
||||
}),
|
||||
}, settings.usage),
|
||||
});
|
||||
const verifier = createActivities({
|
||||
...shared,
|
||||
@ -231,6 +238,122 @@ async function evidenceOf(runId: string) {
|
||||
}
|
||||
|
||||
describe('local main loop', () => {
|
||||
it('persists a completed checkpoint before final usage exhausts budget and resumes without another model call', async () => {
|
||||
const project = await createProject();
|
||||
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-final-budget', {
|
||||
budget: { maxTokens: 1000 },
|
||||
});
|
||||
let workspace = '';
|
||||
const session = await provision(
|
||||
runId,
|
||||
async start => {
|
||||
workspace = start.workspace;
|
||||
return writeGreeting('hello world', 'Completed the greeting.')(start);
|
||||
},
|
||||
{ usage: { input_tokens: 100, output_tokens: 100, cache_read_input_tokens: 1300, final: true } },
|
||||
);
|
||||
expect(await session.executor.executeRun(session.input)).toEqual({ status: 'waiting' });
|
||||
const waiting = await services.control.run(owner, runId);
|
||||
expect(waiting).toMatchObject({ status: 'waiting', waitingReason: 'budget', usedTokens: 1500, stage: 'implement' });
|
||||
expect(waiting.checkpointId).toBeTruthy();
|
||||
const checkpoint = await store.transaction(TENANT, tx =>
|
||||
tx.get<CheckpointRecord>('checkpoint', waiting.checkpointId!),
|
||||
);
|
||||
expect(checkpoint?.checkpoint).toMatchObject({
|
||||
patch: expect.stringContaining('+hello world'),
|
||||
providerCompletion: { status: 'completed', finalUsage: [{ tokens: 1500, operationId: expect.any(String) }] },
|
||||
});
|
||||
expect(existsSync(workspace)).toBe(false); // Removal is safe because the source bundle was confirmed durable.
|
||||
await store.transaction(TENANT, async tx => {
|
||||
const current = (await tx.get<Run>('run', runId))!;
|
||||
current.budget.maxTokens = 5000;
|
||||
await tx.put('run', current);
|
||||
});
|
||||
await services.control.command(owner, runId, { action: 'resume' }, 'resume-final-budget');
|
||||
await release(runId, session.generation);
|
||||
const resumed = await provision(runId, async () => {
|
||||
throw new Error('Completed source must not call a model again');
|
||||
});
|
||||
expect(await resumed.executor.executeRun(resumed.input)).toEqual({ status: 'verify' });
|
||||
expect((await services.control.run(owner, runId)).usedTokens).toBe(1500); // The original operation ID is idempotent.
|
||||
expect(prompts).toHaveLength(1);
|
||||
expect(await resumed.verifier.verifyRun(resumed.input)).toEqual({ status: 'complete' });
|
||||
});
|
||||
|
||||
it('continues metering non-final usage immediately and retains uncheckpointed work when budget cancels the lease', async () => {
|
||||
const project = await createProject();
|
||||
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-stream-budget', {
|
||||
budget: { maxTokens: 1000 },
|
||||
});
|
||||
let workspace = '';
|
||||
const session = await provision(
|
||||
runId,
|
||||
async start => {
|
||||
workspace = start.workspace;
|
||||
return writeGreeting('hello world', 'Work before streaming budget exhaustion.')(start);
|
||||
},
|
||||
{ usage: { input_tokens: 1500, output_tokens: 0 } },
|
||||
);
|
||||
expect(await session.executor.executeRun(session.input)).toEqual({ status: 'waiting' });
|
||||
const waiting = await services.control.run(owner, runId);
|
||||
expect(waiting).toMatchObject({ waitingReason: 'budget', usedTokens: 1500 });
|
||||
expect(waiting.checkpointId).toBeUndefined();
|
||||
expect(await readFile(path.join(workspace, 'greeting.txt'), 'utf8')).toBe('hello world\n');
|
||||
expect(JSON.parse(await readFile(`${workspace}.recovery.json`, 'utf8'))).toMatchObject({
|
||||
runId,
|
||||
workspace,
|
||||
reason: expect.stringContaining('retained'),
|
||||
});
|
||||
});
|
||||
|
||||
it('retains completed source when checkpoint persistence fails', async () => {
|
||||
const project = await createProject();
|
||||
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-checkpoint-down');
|
||||
const delegate = internalFetch(services);
|
||||
let workspace = '';
|
||||
const session = await provision(
|
||||
runId,
|
||||
async start => {
|
||||
workspace = start.workspace;
|
||||
return writeGreeting('hello world', 'Completed source retained.')(start);
|
||||
},
|
||||
{
|
||||
usage: { input_tokens: 100, output_tokens: 20, final: true },
|
||||
fetcher: async (url, init) =>
|
||||
String(url).endsWith('/checkpoint')
|
||||
? Response.json(
|
||||
{ error: { code: 'ARTIFACT_STORAGE_UNAVAILABLE', message: 'Storage unavailable' } },
|
||||
{ status: 503 },
|
||||
)
|
||||
: delegate(url, init),
|
||||
},
|
||||
);
|
||||
await expect(session.executor.executeRun(session.input)).rejects.toMatchObject({
|
||||
domainCode: 'ARTIFACT_STORAGE_UNAVAILABLE',
|
||||
});
|
||||
expect(await readFile(path.join(workspace, 'greeting.txt'), 'utf8')).toBe('hello world\n');
|
||||
expect(JSON.parse(await readFile(`${workspace}.recovery.json`, 'utf8'))).toMatchObject({
|
||||
checkpoint: { providerCompletion: { finalUsage: [{ tokens: 120, operationId: expect.any(String) }] } },
|
||||
});
|
||||
});
|
||||
|
||||
it('retains the workspace when cancellation revokes the lease before checkpoint upload', async () => {
|
||||
const project = await createProject();
|
||||
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-canceled-source');
|
||||
let workspace = '';
|
||||
const session = await provision(runId, async start => {
|
||||
workspace = start.workspace;
|
||||
await writeGreeting('hello world', 'Work before cancellation.')(start);
|
||||
await services.control.command(owner, runId, { action: 'cancel' }, 'cancel-before-checkpoint');
|
||||
return 'Work stopped.';
|
||||
});
|
||||
// Cancel also revokes the scoped credential, so the worker cannot read run state again.
|
||||
await expect(session.executor.executeRun(session.input)).rejects.toMatchObject({ domainCode: 'UNAUTHENTICATED' });
|
||||
expect((await services.control.run(owner, runId)).status).toBe('canceled');
|
||||
expect(await readFile(path.join(workspace, 'greeting.txt'), 'utf8')).toBe('hello world\n');
|
||||
expect(existsSync(`${workspace}.recovery.json`)).toBe(true);
|
||||
});
|
||||
|
||||
it('repairs a failed check with the verifier output, integrates the change and learns from it', async () => {
|
||||
const project = await createProject();
|
||||
const runId = await submit(project.id, 'Make the greeting say hello world', 'wish-repair-0001');
|
||||
|
||||
589
tests/local-poc-delivery.test.ts
Normal file
589
tests/local-poc-delivery.test.ts
Normal file
@ -0,0 +1,589 @@
|
||||
import { generateKeyPairSync, randomUUID } from 'node:crypto';
|
||||
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
matchesApplicationAccess,
|
||||
type Actor,
|
||||
type Release,
|
||||
type Run,
|
||||
type Environment,
|
||||
type RunnerTask,
|
||||
} from '@wishforge/contracts';
|
||||
import {
|
||||
pocProfileSchema,
|
||||
profileDigest,
|
||||
pocReleaseDigest,
|
||||
verifyRunnerTask,
|
||||
signRunnerTask,
|
||||
type PocProfile,
|
||||
type PocRelease,
|
||||
type SignedRunnerTask,
|
||||
} from '@wishforge/runtime';
|
||||
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
||||
import { Releases, releaseBinding } from '../apps/api/src/domain/releases.js';
|
||||
import { RunnerBroker } from '../apps/api/src/domain/runner-broker.js';
|
||||
import { assertPocBuild } from '../apps/api/src/domain/local-poc.js';
|
||||
import { applicationUrlFor } from '../apps/api/src/domain/application-access.js';
|
||||
import { digest } from '../apps/api/src/domain/policy.js';
|
||||
import { MemoryStore } from './helpers/memory-store.js';
|
||||
|
||||
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
|
||||
const worker: Actor = { ...owner, userId: 'worker', kind: 'worker' };
|
||||
const verifier: Actor = { ...owner, userId: 'verifier', kind: 'verifier' };
|
||||
const roots: string[] = [];
|
||||
afterEach(async () => {
|
||||
vi.unstubAllEnvs();
|
||||
await Promise.all(roots.splice(0).map(dir => rm(dir, { recursive: true, force: true })));
|
||||
});
|
||||
const registry = 'ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow-poc';
|
||||
function images(letter: string) {
|
||||
return {
|
||||
api: `${registry}/droboticflow-api@sha256:${letter.repeat(64)}`,
|
||||
worker: `${registry}/droboticflow-worker@sha256:${letter.repeat(64)}`,
|
||||
coordinator: `${registry}/drosync-coordinator@sha256:${letter.repeat(64)}`,
|
||||
};
|
||||
}
|
||||
function bundle(profile: PocProfile): PocRelease {
|
||||
return {
|
||||
schemaVersion: 1,
|
||||
id: 'run-poc-fixture',
|
||||
sourceCommit: 'a'.repeat(40),
|
||||
profileDigest: profileDigest(profile),
|
||||
images: images('b'),
|
||||
expectedImages: images('a'),
|
||||
expectedTemplateDigests: {
|
||||
api: `sha256:${'1'.repeat(64)}`,
|
||||
worker: `sha256:${'2'.repeat(64)}`,
|
||||
coordinator: `sha256:${'3'.repeat(64)}`,
|
||||
},
|
||||
rollbackCompatible: true,
|
||||
};
|
||||
}
|
||||
|
||||
async function fixture(bound = true) {
|
||||
const root = await mkdtemp(path.join(tmpdir(), 'wishforge-poc-domain-'));
|
||||
roots.push(root);
|
||||
const keys = generateKeyPairSync('ed25519');
|
||||
await writeFile(path.join(root, 'signer.pem'), keys.privateKey.export({ type: 'pkcs8', format: 'pem' }), {
|
||||
mode: 0o600,
|
||||
});
|
||||
const profile = pocProfileSchema.parse({
|
||||
schemaVersion: 1,
|
||||
id: 'droboticflow-poc',
|
||||
repository: 'git@gitea.d-robotics.cc:longtao.wu/droboticflow.git',
|
||||
branch: 'poc',
|
||||
kubeconfig: '/operator/poc.config',
|
||||
clusterServer: 'https://120.48.181.105:6443',
|
||||
namespace: 'droboticflow',
|
||||
namespaceUid: randomUUID(),
|
||||
deployments: { api: randomUUID(), worker: randomUUID(), coordinator: randomUUID() },
|
||||
registry,
|
||||
registrySecret: 'droboticflow-registry',
|
||||
applicationUrl: 'http://120.48.164.19',
|
||||
kanikoImage: `gcr.io/kaniko-project/executor@sha256:${'a'.repeat(64)}`,
|
||||
});
|
||||
const profileFile = path.join(root, 'profile.json');
|
||||
await writeFile(profileFile, JSON.stringify(profile), { mode: 0o600 });
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
NODE_ENV: 'test',
|
||||
WF_DEPLOYMENT_MODE: 'development',
|
||||
WF_VAULT_KEY: 'a'.repeat(64),
|
||||
WF_RUNNER_SIGNING_KEY_FILE: path.join(root, 'signer.pem'),
|
||||
WF_TENANT_ID: 'alpha',
|
||||
WF_PROVISIONER_BACKEND: 'local',
|
||||
WF_CLAUDE_BIN: '/operator/claude',
|
||||
WF_CLAUDE_LOCAL_AUTH_HOME: root,
|
||||
};
|
||||
const db = new MemoryStore();
|
||||
db.seed('alpha');
|
||||
const control = new ControlPlane(db, env),
|
||||
releases = new Releases(db, env),
|
||||
broker = new RunnerBroker(db, releases, env);
|
||||
let project = await control.createProject(owner, {
|
||||
name: 'POC fixture',
|
||||
repositoryUrl: 'https://gitea.d-robotics.cc/longtao.wu/droboticflow.git',
|
||||
targetBranch: 'poc',
|
||||
verificationCommands: [['node', '--test']],
|
||||
});
|
||||
let environment = await releases.createEnvironment(owner, {
|
||||
projectId: project.id,
|
||||
name: 'POC',
|
||||
type: 'kubernetes',
|
||||
applicationUrl: 'https://poc.example.test/',
|
||||
});
|
||||
project = {
|
||||
...project,
|
||||
deliveryConfig: {
|
||||
imageRepository: `${registry}/droboticflow-api`,
|
||||
architecture: 'linux/amd64',
|
||||
environmentId: environment.id,
|
||||
dockerfile: 'Dockerfile.api',
|
||||
sourceContext: '.',
|
||||
containerPort: 3001,
|
||||
healthPath: '/api/health',
|
||||
smokeChecks: [{ path: '/api/ready', expectedStatus: 200 }],
|
||||
},
|
||||
};
|
||||
await db.transaction('alpha', tx => tx.put('project', project));
|
||||
const bindingFile = path.join(root, 'binding.json');
|
||||
await writeFile(
|
||||
bindingFile,
|
||||
JSON.stringify({
|
||||
schemaVersion: 1,
|
||||
tenantId: 'alpha',
|
||||
projectId: bound ? project.id : randomUUID(),
|
||||
environmentId: environment.id,
|
||||
repositoryUrl: project.repositoryUrl,
|
||||
targetBranch: 'poc',
|
||||
profileFile,
|
||||
stateDirectory: root,
|
||||
gitHome: root,
|
||||
}),
|
||||
{ mode: 0o600 },
|
||||
);
|
||||
env.WF_LOCAL_POC_BINDING_FILE = bindingFile;
|
||||
if (bound)
|
||||
environment = await releases.updateApplicationUrl(owner, environment.id, {
|
||||
applicationUrl: profile.applicationUrl,
|
||||
});
|
||||
const { run_id: runId } = await control.submit(
|
||||
owner,
|
||||
project.id,
|
||||
{
|
||||
provider: 'claude-code',
|
||||
goal: 'Improve system status display',
|
||||
acceptanceCriteria: ['The refresh button is usable'],
|
||||
},
|
||||
'poc-fixture-submit',
|
||||
);
|
||||
let run = await control.lease(worker, runId, 'executor');
|
||||
for (const stage of ['plan', 'implement', 'verify'] as const)
|
||||
run = await control.transition(worker, runId, {
|
||||
leaseToken: run.lease!.token,
|
||||
stage,
|
||||
status: 'running',
|
||||
message: `Fixture ${stage}`,
|
||||
});
|
||||
await control.releaseLease(worker, runId, run.lease!.token);
|
||||
run = await control.lease(verifier, runId, 'verifier');
|
||||
const leaseToken = run.lease!.token;
|
||||
const evidence = (data: Record<string, unknown>) =>
|
||||
control.evidence(verifier, { runId, revision: 1, leaseToken, sourceCommit: 'a'.repeat(40), ...data });
|
||||
const verification = await evidence({
|
||||
type: 'verification',
|
||||
passed: true,
|
||||
isolation: 'sandbox',
|
||||
commands: [{ argv: ['node', '--test'], exitCode: 0, outputDigest: digest('passed') }],
|
||||
});
|
||||
const integration = await control.integration.acquire(verifier, runId, leaseToken);
|
||||
await control.transition(verifier, runId, {
|
||||
leaseToken,
|
||||
stage: 'integrate',
|
||||
status: 'running',
|
||||
message: 'integrate',
|
||||
});
|
||||
await evidence({ type: 'integration', passed: true, targetRef: 'refs/heads/poc' });
|
||||
await control.transition(verifier, runId, { leaseToken, stage: 'build', status: 'running', message: 'build' });
|
||||
const poc = bundle(profile);
|
||||
async function prepare() {
|
||||
const build = await evidence({
|
||||
type: 'build',
|
||||
passed: true,
|
||||
imageDigest: poc.images.api,
|
||||
...(bound ? { pocRelease: poc } : {}),
|
||||
});
|
||||
await control.integration.release(verifier, runId, leaseToken, integration.fence);
|
||||
await control.releaseLease(verifier, runId, leaseToken);
|
||||
const release = await releases.request(
|
||||
owner,
|
||||
{ runId, environmentId: environment.id, verificationEvidenceId: verification.id, buildEvidenceId: build.id },
|
||||
'poc-fixture-release',
|
||||
);
|
||||
return release;
|
||||
}
|
||||
async function dispatch() {
|
||||
const release = await prepare();
|
||||
await releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest });
|
||||
const registration = await broker.register(owner, environment.id);
|
||||
const runner: Actor = { ...owner, userId: `runner:${environment.id}`, kind: 'deployer', clientId: registration.id };
|
||||
const envelope = (await broker.poll(runner, environment.id))!;
|
||||
return { release, registration, runner, envelope };
|
||||
}
|
||||
return {
|
||||
root,
|
||||
keys,
|
||||
profile,
|
||||
profileFile,
|
||||
env,
|
||||
db,
|
||||
control,
|
||||
releases,
|
||||
broker,
|
||||
project,
|
||||
environment,
|
||||
runId,
|
||||
poc,
|
||||
evidence,
|
||||
prepare,
|
||||
dispatch,
|
||||
};
|
||||
}
|
||||
|
||||
function receipt(release: Release, poc: PocRelease) {
|
||||
return {
|
||||
result: {
|
||||
releaseId: release.id,
|
||||
status: 'verified',
|
||||
target: 'kubernetes',
|
||||
evidence: {
|
||||
identityMatched: true,
|
||||
trafficSwitched: true,
|
||||
checks: ['health', 'business-smoke', 'pod-digests', 'protected-business-smoke'],
|
||||
pocRelease: poc,
|
||||
bundleDigest: pocReleaseDigest(poc),
|
||||
accessEvidence: {
|
||||
applicationUrl: 'http://120.48.164.19',
|
||||
stableOrigin: 'http://120.48.164.19',
|
||||
checkedAt: new Date().toISOString(),
|
||||
checks: ['application-url', 'public-access'],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function rollbackFixture(change?: (poc: PocRelease) => void) {
|
||||
const f = await fixture();
|
||||
const first = await f.dispatch();
|
||||
await f.broker.result(first.runner, first.envelope.payload.id, receipt(first.release, f.poc));
|
||||
const { run_id: runId } = await f.control.submit(
|
||||
owner,
|
||||
f.project.id,
|
||||
{
|
||||
provider: 'claude-code',
|
||||
goal: 'Add a second verified POC improvement',
|
||||
acceptanceCriteria: ['The second revision is delivered'],
|
||||
},
|
||||
'poc-second-release',
|
||||
);
|
||||
let run = await f.control.lease(worker, runId, 'second-executor');
|
||||
for (const stage of ['plan', 'implement', 'verify'] as const)
|
||||
run = await f.control.transition(worker, runId, {
|
||||
leaseToken: run.lease!.token,
|
||||
stage,
|
||||
status: 'running',
|
||||
message: stage,
|
||||
});
|
||||
await f.control.releaseLease(worker, runId, run.lease!.token);
|
||||
run = await f.control.lease(verifier, runId, 'second-verifier');
|
||||
const leaseToken = run.lease!.token;
|
||||
const evidence = (data: Record<string, unknown>) =>
|
||||
f.control.evidence(verifier, { runId, revision: 1, leaseToken, sourceCommit: 'b'.repeat(40), ...data });
|
||||
const verified = await evidence({
|
||||
type: 'verification',
|
||||
passed: true,
|
||||
isolation: 'sandbox',
|
||||
commands: [{ argv: ['node', '--test'], exitCode: 0, outputDigest: digest('second passed') }],
|
||||
});
|
||||
const lease = await f.control.integration.acquire(verifier, runId, leaseToken);
|
||||
await f.control.transition(verifier, runId, {
|
||||
leaseToken,
|
||||
stage: 'integrate',
|
||||
status: 'running',
|
||||
message: 'integrate',
|
||||
});
|
||||
await evidence({ type: 'integration', passed: true, targetRef: 'refs/heads/poc' });
|
||||
await f.control.transition(verifier, runId, { leaseToken, stage: 'build', status: 'running', message: 'build' });
|
||||
const currentPoc: PocRelease = {
|
||||
...structuredClone(f.poc),
|
||||
id: 'run-second-poc-fixture',
|
||||
sourceCommit: 'b'.repeat(40),
|
||||
images: images('c'),
|
||||
expectedImages: { ...f.poc.images },
|
||||
};
|
||||
change?.(currentPoc);
|
||||
const built = await evidence({
|
||||
type: 'build',
|
||||
passed: true,
|
||||
imageDigest: currentPoc.images.api,
|
||||
pocRelease: currentPoc,
|
||||
});
|
||||
await f.control.integration.release(verifier, runId, leaseToken, lease.fence);
|
||||
await f.control.releaseLease(verifier, runId, leaseToken);
|
||||
const current = await f.releases.request(
|
||||
owner,
|
||||
{ runId, environmentId: f.environment.id, verificationEvidenceId: verified.id, buildEvidenceId: built.id },
|
||||
'poc-second-request',
|
||||
);
|
||||
await f.releases.decide(owner, current.approvalId!, { decision: 'approve', bindingDigest: current.bindingDigest });
|
||||
const envelope = (await f.broker.poll(first.runner, f.environment.id))!;
|
||||
await f.broker.result(first.runner, envelope.payload.id, receipt(current, currentPoc));
|
||||
return { ...f, first: first.release, runner: first.runner, registration: first.registration, current, currentPoc };
|
||||
}
|
||||
|
||||
describe('operator-bound local POC delivery', () => {
|
||||
it('rejects a generic project trying to introduce a POC bundle', async () => {
|
||||
const f = await fixture(false);
|
||||
await expect(assertPocBuild(f.project, f.environment.id, f.poc, f.env)).rejects.toMatchObject({
|
||||
code: 'POC_BINDING_REQUIRED',
|
||||
});
|
||||
await expect(
|
||||
f.evidence({ type: 'build', passed: true, imageDigest: f.poc.images.api, pocRelease: f.poc }),
|
||||
).rejects.toMatchObject({ code: 'POC_BINDING_REQUIRED' });
|
||||
});
|
||||
it('requires complete configuration fingerprints and consistent main source/API image', async () => {
|
||||
const f = await fixture();
|
||||
await expect(
|
||||
f.evidence({
|
||||
type: 'build',
|
||||
passed: true,
|
||||
imageDigest: f.poc.images.api,
|
||||
pocRelease: { ...f.poc, expectedTemplateDigests: undefined },
|
||||
}),
|
||||
).rejects.toMatchObject({ code: 'POC_POLICY_CHANGED' });
|
||||
await expect(
|
||||
f.evidence({
|
||||
type: 'build',
|
||||
passed: true,
|
||||
imageDigest: f.poc.images.api,
|
||||
pocRelease: { ...f.poc, sourceCommit: 'b'.repeat(40) },
|
||||
}),
|
||||
).rejects.toMatchObject({ code: 'POC_BUILD_MISMATCH' });
|
||||
await expect(
|
||||
f.evidence({ type: 'build', passed: true, imageDigest: images('c').api, pocRelease: f.poc }),
|
||||
).rejects.toMatchObject({ code: 'POC_BUILD_MISMATCH' });
|
||||
});
|
||||
it('invalidates a release when the operator profile changes after build', async () => {
|
||||
const f = await fixture();
|
||||
const release = await f.prepare();
|
||||
await writeFile(
|
||||
f.profileFile,
|
||||
JSON.stringify({ ...f.profile, rolloutTimeoutSeconds: f.profile.rolloutTimeoutSeconds + 1 }),
|
||||
);
|
||||
await expect(
|
||||
f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest }),
|
||||
).rejects.toMatchObject({ code: 'POC_POLICY_CHANGED' });
|
||||
});
|
||||
it('binds every desired/previous component digest and preserved configuration fingerprint into approval', async () => {
|
||||
const f = await fixture();
|
||||
const release = await f.prepare();
|
||||
expect(release.pocRelease).toEqual(f.poc);
|
||||
expect(digest(releaseBinding(release))).toBe(release.bindingDigest);
|
||||
for (const component of ['api', 'worker', 'coordinator'] as const) {
|
||||
for (const group of ['images', 'expectedImages', 'expectedTemplateDigests'] as const) {
|
||||
const changed = structuredClone(release);
|
||||
changed.pocRelease![group]![component] =
|
||||
group === 'expectedTemplateDigests' ? `sha256:${'e'.repeat(64)}` : images('e')[component];
|
||||
expect(digest(releaseBinding(changed))).not.toBe(release.bindingDigest);
|
||||
}
|
||||
}
|
||||
});
|
||||
it('allows the exact legacy HTTP URL only for the bound development project', async () => {
|
||||
const f = await fixture();
|
||||
expect(applicationUrlFor({ ...f.environment, applicationUrl: f.profile.applicationUrl }, f.env)).toBe(
|
||||
f.profile.applicationUrl,
|
||||
);
|
||||
for (const environment of [
|
||||
{ ...f.environment, projectId: randomUUID(), applicationUrl: f.profile.applicationUrl },
|
||||
{ ...f.environment, production: true, applicationUrl: f.profile.applicationUrl },
|
||||
{ ...f.environment, applicationUrl: 'http://120.48.164.19/other' },
|
||||
{ ...f.environment, applicationUrl: 'http://120.48.164.20' },
|
||||
])
|
||||
expect(() => applicationUrlFor(environment, f.env)).toThrow();
|
||||
expect(() =>
|
||||
applicationUrlFor(
|
||||
{ ...f.environment, applicationUrl: f.profile.applicationUrl },
|
||||
{ ...f.env, WF_DEPLOYMENT_MODE: 'production', NODE_ENV: 'production' },
|
||||
),
|
||||
).toThrow();
|
||||
});
|
||||
it('requires operator HTTP authority in addition to a valid task signature and matching profile', async () => {
|
||||
const f = await fixture();
|
||||
const { envelope, registration } = await f.dispatch();
|
||||
const signed = envelope as SignedRunnerTask;
|
||||
const policy = { tenantId: 'alpha', environmentId: f.environment.id, publicKeys: registration.publicKeys };
|
||||
expect(() => verifyRunnerTask(signed, policy)).toThrow('HTTPS');
|
||||
const trustedPocDelivery = { profileDigest: profileDigest(f.profile), applicationUrl: f.profile.applicationUrl };
|
||||
expect(verifyRunnerTask(signed, { ...policy, trustedPocDelivery }).release.pocRelease).toEqual(f.poc);
|
||||
expect(() =>
|
||||
verifyRunnerTask(signed, {
|
||||
...policy,
|
||||
trustedPocDelivery: { ...trustedPocDelivery, profileDigest: `sha256:${'e'.repeat(64)}` },
|
||||
}),
|
||||
).toThrow('HTTPS');
|
||||
const changed = structuredClone(signed);
|
||||
changed.payload.release.pocRelease!.images.worker = images('e').worker;
|
||||
expect(() => verifyRunnerTask(changed, { ...policy, trustedPocDelivery })).toThrow('signature');
|
||||
const validDifferentUrl = signRunnerTask(
|
||||
{ ...signed.payload, release: { ...signed.payload.release, applicationUrl: 'http://120.48.164.19/other' } },
|
||||
signed.keyId,
|
||||
f.keys.privateKey,
|
||||
);
|
||||
expect(() => verifyRunnerTask(validDifferentUrl, { ...policy, trustedPocDelivery })).toThrow('HTTPS');
|
||||
vi.stubEnv('WF_DEPLOYMENT_MODE', 'production');
|
||||
expect(() => verifyRunnerTask(signed, { ...policy, trustedPocDelivery })).toThrow('HTTPS');
|
||||
});
|
||||
it.each(['bundle', 'digest'] as const)('never marks delivery accepted when the receipt %s is forged', async kind => {
|
||||
const f = await fixture();
|
||||
const { release, runner, envelope } = await f.dispatch();
|
||||
const input = receipt(release, f.poc);
|
||||
if (kind === 'bundle')
|
||||
input.result.evidence.pocRelease = {
|
||||
...f.poc,
|
||||
images: { ...f.poc.images, coordinator: images('e').coordinator },
|
||||
};
|
||||
else input.result.evidence.bundleDigest = `sha256:${'e'.repeat(64)}`;
|
||||
expect(await f.broker.result(runner, envelope.payload.id, input)).toMatchObject({ delivered: false });
|
||||
const saved = await f.db.read('alpha', tx => tx.get<Run>('run', f.runId));
|
||||
expect(saved?.status).not.toBe('succeeded');
|
||||
expect(await f.releases.delivery(owner, f.runId)).toMatchObject({ accepted: false });
|
||||
});
|
||||
it('keeps the run incomplete until the matching component and HTTP access receipt is accepted', async () => {
|
||||
const f = await fixture();
|
||||
const { release, runner, envelope } = await f.dispatch();
|
||||
expect((await f.db.read('alpha', tx => tx.get<Run>('run', f.runId)))?.status).not.toBe('succeeded');
|
||||
expect(await f.releases.delivery(owner, f.runId)).not.toHaveProperty('url');
|
||||
const input = receipt(release, f.poc);
|
||||
expect(matchesApplicationAccess(release, input.result.evidence.accessEvidence)).toBe(true);
|
||||
expect(input.result.evidence.bundleDigest).toBe(digest(release.pocRelease));
|
||||
expect(await f.broker.result(runner, envelope.payload.id, input)).toMatchObject({
|
||||
delivered: true,
|
||||
url: f.profile.applicationUrl,
|
||||
});
|
||||
expect(await f.releases.delivery(owner, f.runId)).toMatchObject({ accepted: true, url: f.profile.applicationUrl });
|
||||
});
|
||||
it('does not allow an ordinary deployer to bypass the signed POC receipt with generic deployment evidence', async () => {
|
||||
const f = await fixture();
|
||||
const { release } = await f.dispatch();
|
||||
await expect(
|
||||
f.control.evidence(
|
||||
{ ...owner, userId: 'deployer', kind: 'deployer' },
|
||||
{
|
||||
runId: f.runId,
|
||||
revision: 1,
|
||||
type: 'deployment',
|
||||
passed: true,
|
||||
sourceCommit: release.sourceCommit,
|
||||
imageDigest: release.imageDigest,
|
||||
releaseId: release.id,
|
||||
environmentId: f.environment.id,
|
||||
bindingDigest: release.bindingDigest,
|
||||
checks: ['health', 'business-smoke'],
|
||||
accessEvidence: receipt(release, f.poc).result.evidence.accessEvidence,
|
||||
},
|
||||
),
|
||||
).rejects.toThrow();
|
||||
expect((await f.db.read('alpha', tx => tx.get<Run>('run', f.runId)))?.status).not.toBe('succeeded');
|
||||
});
|
||||
it('does not invent a previous successful release before the first WishForge delivery', async () => {
|
||||
const f = await fixture();
|
||||
const { release, runner, envelope } = await f.dispatch();
|
||||
await f.broker.result(runner, envelope.payload.id, receipt(release, f.poc));
|
||||
await expect(f.releases.rollback(owner, release.id, 'first-release-rollback')).rejects.toMatchObject({
|
||||
code: 'ROLLBACK_UNAVAILABLE',
|
||||
});
|
||||
});
|
||||
it('signs a new rollback journal with previous desired images and the complete current baseline', async () => {
|
||||
const f = await rollbackFixture();
|
||||
const operation = await f.releases.rollback(owner, f.current.id, 'poc-rollback-request');
|
||||
const envelope = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
const task = verifyRunnerTask(envelope as SignedRunnerTask, {
|
||||
tenantId: 'alpha',
|
||||
environmentId: f.environment.id,
|
||||
publicKeys: f.registration.publicKeys,
|
||||
trustedPocDelivery: { profileDigest: profileDigest(f.profile), applicationUrl: f.profile.applicationUrl },
|
||||
});
|
||||
const recovery = task.release.pocRelease!;
|
||||
expect(recovery).toEqual({
|
||||
...f.poc,
|
||||
id: `rollback-${operation.id}`,
|
||||
expectedImages: f.currentPoc.images,
|
||||
expectedTemplateDigests: f.currentPoc.expectedTemplateDigests,
|
||||
});
|
||||
expect(recovery.id).not.toBe(f.poc.id);
|
||||
expect(recovery.id).not.toBe(f.currentPoc.id);
|
||||
expect(task.release.sourceCommit).toBe(f.first.sourceCommit);
|
||||
expect(await f.broker.result(f.runner, envelope.payload.id, receipt(f.first, recovery))).toMatchObject({
|
||||
rolledBack: true,
|
||||
delivered: false,
|
||||
});
|
||||
const state = await f.db.read('alpha', async tx => ({
|
||||
environment: await tx.get<Environment>('environment', f.environment.id),
|
||||
previous: await tx.get<Release>('release', f.first.id),
|
||||
current: await tx.get<Release>('release', f.current.id),
|
||||
}));
|
||||
expect(state.environment).toMatchObject({ currentReleaseId: f.first.id });
|
||||
expect(state.environment?.activeReleaseId).toBeUndefined();
|
||||
expect(state.current?.status).toBe('rolled_back');
|
||||
expect(state.previous).toMatchObject({ status: 'healthy', pocRelease: f.poc });
|
||||
expect(digest(releaseBinding(state.previous!))).toBe(f.first.bindingDigest);
|
||||
});
|
||||
it('rejects an old release journal receipt and reuses the new recovery manifest when reconciling', async () => {
|
||||
const f = await rollbackFixture();
|
||||
await f.releases.rollback(owner, f.current.id, 'poc-old-journal-rollback');
|
||||
const envelope = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
expect(await f.broker.result(f.runner, envelope.payload.id, receipt(f.first, f.poc))).toMatchObject({
|
||||
rolledBack: false,
|
||||
delivered: false,
|
||||
});
|
||||
expect(
|
||||
(await f.db.read('alpha', tx => tx.get<Environment>('environment', f.environment.id)))?.currentReleaseId,
|
||||
).toBe(f.current.id);
|
||||
const next = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
expect(next.payload.action).toBe('reconcile');
|
||||
expect(next.payload.release.pocRelease).toEqual(envelope.payload.release.pocRelease);
|
||||
expect(next.payload.id).not.toBe(envelope.payload.id);
|
||||
});
|
||||
it('keeps recovery journal identity stable after a lost or expired task receipt', async () => {
|
||||
const f = await rollbackFixture();
|
||||
await f.releases.rollback(owner, f.current.id, 'poc-expired-rollback');
|
||||
const first = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
await f.db.transaction('alpha', async tx => {
|
||||
const task = (await tx.get<RunnerTask>('runner_task', first.payload.id))!;
|
||||
const envelope = structuredClone(first);
|
||||
envelope.payload.expiresAt = new Date(Date.now() - 1000).toISOString();
|
||||
await tx.put('runner_task', { ...task, envelope });
|
||||
});
|
||||
const recovered = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
expect(recovered.payload.action).toBe('reconcile');
|
||||
expect(recovered.payload.release.pocRelease).toEqual(first.payload.release.pocRelease);
|
||||
expect(recovered.payload.id).not.toBe(first.payload.id);
|
||||
});
|
||||
it.each(['configuration', 'compatibility', 'external-baseline'] as const)(
|
||||
'refuses incompatible POC rollback before acquiring a lock: %s',
|
||||
async kind => {
|
||||
const f = await rollbackFixture(poc => {
|
||||
if (kind === 'configuration') poc.expectedTemplateDigests!.worker = `sha256:${'f'.repeat(64)}`;
|
||||
if (kind === 'compatibility') poc.rollbackCompatible = false;
|
||||
if (kind === 'external-baseline') poc.expectedImages.coordinator = images('d').coordinator;
|
||||
});
|
||||
await expect(f.releases.rollback(owner, f.current.id, `poc-incompatible-${kind}`)).rejects.toMatchObject({
|
||||
code: 'ROLLBACK_REQUIRES_OPERATOR',
|
||||
});
|
||||
expect(
|
||||
(await f.db.read('alpha', tx => tx.get<Environment>('environment', f.environment.id)))?.activeReleaseId,
|
||||
).toBeUndefined();
|
||||
},
|
||||
);
|
||||
it('invalidates an already signed rollback when the operator profile changes before acceptance', async () => {
|
||||
const f = await rollbackFixture();
|
||||
await f.releases.rollback(owner, f.current.id, 'poc-profile-rollback');
|
||||
const envelope = (await f.broker.poll(f.runner, f.environment.id))!;
|
||||
await writeFile(
|
||||
f.profileFile,
|
||||
JSON.stringify({ ...f.profile, rolloutTimeoutSeconds: f.profile.rolloutTimeoutSeconds + 1 }),
|
||||
);
|
||||
await expect(
|
||||
f.broker.result(
|
||||
f.runner,
|
||||
envelope.payload.id,
|
||||
receipt(f.first, envelope.payload.release.pocRelease as PocRelease),
|
||||
),
|
||||
).rejects.toMatchObject({ code: 'POC_POLICY_CHANGED' });
|
||||
expect(
|
||||
(await f.db.read('alpha', tx => tx.get<Environment>('environment', f.environment.id)))?.currentReleaseId,
|
||||
).toBe(f.current.id);
|
||||
});
|
||||
});
|
||||
164
tests/run-budget.test.ts
Normal file
164
tests/run-budget.test.ts
Normal file
@ -0,0 +1,164 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { budgetPatchSchema, type Actor, type Run } from '@wishforge/contracts';
|
||||
import { MemoryStore } from './helpers/memory-store.js';
|
||||
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
||||
|
||||
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
|
||||
let store: MemoryStore, control: ControlPlane, id: string;
|
||||
beforeEach(async () => {
|
||||
store = new MemoryStore();
|
||||
store.seed('alpha');
|
||||
store.seed('beta');
|
||||
control = new ControlPlane(store, { NODE_ENV: 'test' });
|
||||
const project = await control.createProject(owner, {
|
||||
name: 'Budget test',
|
||||
repositoryUrl: 'https://example.test/app.git',
|
||||
});
|
||||
({ run_id: id } = await control.submit(
|
||||
owner,
|
||||
project.id,
|
||||
{
|
||||
goal: 'Improve the status display',
|
||||
acceptanceCriteria: ['Errors keep the previous status'],
|
||||
budget: { maxTokens: 200000, maxMinutes: 30, maxRepairRounds: 2 },
|
||||
},
|
||||
'budget-fixture',
|
||||
));
|
||||
await store.transaction('alpha', async tx => {
|
||||
const run = (await tx.get<Run>('run', id))!;
|
||||
await tx.put('run', {
|
||||
...run,
|
||||
status: 'waiting',
|
||||
waitingReason: 'budget',
|
||||
usedTokens: 732366,
|
||||
activeMilliseconds: 61000,
|
||||
reservedTokens: 0,
|
||||
});
|
||||
await tx.meter({
|
||||
id: 'model-final-usage',
|
||||
runId: id,
|
||||
tokens: 732366,
|
||||
computeSeconds: 61,
|
||||
storageBytes: 0,
|
||||
at: new Date().toISOString(),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('manual stopped-run budget adjustments', () => {
|
||||
it('preserves recorded usage, time, limits not patched, and status while auditing one idempotent change', async () => {
|
||||
const changed = await control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-increase');
|
||||
expect(changed).toMatchObject({
|
||||
status: 'waiting',
|
||||
waitingReason: 'budget',
|
||||
usedTokens: 732366,
|
||||
activeMilliseconds: 61000,
|
||||
reservedTokens: 267634,
|
||||
budget: { maxTokens: 1000000, maxMinutes: 30, maxRepairRounds: 2 },
|
||||
});
|
||||
expect(await control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-increase')).toEqual(changed);
|
||||
const events = (await control.events(owner, id)).filter(event => event.type === 'run.budget.updated');
|
||||
expect(events).toHaveLength(1);
|
||||
expect(events[0].data).toMatchObject({
|
||||
actorId: 'owner',
|
||||
oldBudget: { maxTokens: 200000, maxMinutes: 30, maxRepairRounds: 2 },
|
||||
newBudget: { maxTokens: 1000000, maxMinutes: 30, maxRepairRounds: 2 },
|
||||
usedTokens: 732366,
|
||||
});
|
||||
expect(await control.usage(owner)).toMatchObject({ tokens: 732366, reservedTokens: 0 });
|
||||
const starts = await store.read('alpha', async tx => (await tx.outbox()).filter(item => item.type === 'run.start'));
|
||||
expect(starts).toHaveLength(0);
|
||||
});
|
||||
it('accepts a human admin, rejects lesser roles and service/connector identities', async () => {
|
||||
for (const actor of [
|
||||
{ ...owner, role: 'developer' as const },
|
||||
{ ...owner, role: 'viewer' as const },
|
||||
{ ...owner, kind: 'worker' as const },
|
||||
{ ...owner, kind: 'connector' as const },
|
||||
])
|
||||
await expect(control.updateBudget(actor, id, { maxTokens: 1000000 }, 'budget-forbidden')).rejects.toMatchObject({
|
||||
status: 403,
|
||||
});
|
||||
expect(
|
||||
(await control.updateBudget({ ...owner, role: 'admin' }, id, { maxTokens: 1000000 }, 'budget-admin')).budget
|
||||
.maxTokens,
|
||||
).toBe(1000000);
|
||||
await expect(
|
||||
control.updateBudget({ ...owner, tenantId: 'beta' }, id, { maxTokens: 1000000 }, 'budget-other-tenant'),
|
||||
).rejects.toMatchObject({ code: 'NOT_FOUND' });
|
||||
});
|
||||
it('rejects missing or conflicting idempotency keys and unsupported patch shapes', async () => {
|
||||
await expect(control.updateBudget(owner, id, { maxTokens: 1000000 })).rejects.toMatchObject({
|
||||
code: 'IDEMPOTENCY_REQUIRED',
|
||||
});
|
||||
await control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-reused-key');
|
||||
await expect(control.updateBudget(owner, id, { maxTokens: 1100000 }, 'budget-reused-key')).rejects.toMatchObject({
|
||||
code: 'IDEMPOTENCY_CONFLICT',
|
||||
});
|
||||
for (const input of [{}, { usedTokens: 0 }, { maxTokens: 2000001 }, { maxMinutes: 181 }, { maxRepairRounds: 4 }])
|
||||
expect(budgetPatchSchema.safeParse(input).success).toBe(false);
|
||||
expect(budgetPatchSchema.parse({ maxTokens: 1000000 })).toEqual({ maxTokens: 1000000 });
|
||||
});
|
||||
it('rejects running, queued, and completed runs without changing their budgets', async () => {
|
||||
for (const status of ['queued', 'running', 'succeeded', 'failed', 'canceled'] as const) {
|
||||
await store.transaction('alpha', async tx => {
|
||||
const run = (await tx.get<Run>('run', id))!;
|
||||
await tx.put('run', { ...run, status });
|
||||
});
|
||||
await expect(
|
||||
control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-status-' + status),
|
||||
).rejects.toMatchObject({ code: 'BUDGET_UPDATE_REQUIRES_STOPPED_RUN' });
|
||||
expect((await control.run(owner, id)).budget.maxTokens).toBe(200000);
|
||||
}
|
||||
});
|
||||
it('cannot lower limits below usage or clear unsettled model reservations', async () => {
|
||||
await expect(control.updateBudget(owner, id, { maxTokens: 700000 }, 'budget-below-tokens')).rejects.toMatchObject({
|
||||
code: 'BUDGET_BELOW_USAGE',
|
||||
});
|
||||
await expect(
|
||||
control.updateBudget(owner, id, { maxTokens: 1000000, maxMinutes: 1 }, 'budget-below-time'),
|
||||
).rejects.toMatchObject({ code: 'BUDGET_BELOW_USAGE' });
|
||||
await store.transaction('alpha', async tx => {
|
||||
const run = (await tx.get<Run>('run', id))!;
|
||||
await tx.put('run', { ...run, providerProxyActiveRequest: 'request-pending' });
|
||||
});
|
||||
await expect(control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-pending-model')).rejects.toMatchObject(
|
||||
{ code: 'BUDGET_UPDATE_PENDING_USAGE' },
|
||||
);
|
||||
expect((await control.run(owner, id)).usedTokens).toBe(732366);
|
||||
});
|
||||
it('accounts remaining active time before validating paused-run limits', async () => {
|
||||
const now = Date.now();
|
||||
await store.transaction('alpha', async tx => {
|
||||
const run = (await tx.get<Run>('run', id))!;
|
||||
await tx.put('run', {
|
||||
...run,
|
||||
status: 'paused',
|
||||
lastActiveAt: new Date(now - 70000).toISOString(),
|
||||
lease: { token: 1, workerId: 'worker', expiresAt: new Date(now - 1000).toISOString() },
|
||||
});
|
||||
});
|
||||
await expect(
|
||||
control.updateBudget(owner, id, { maxTokens: 1000000, maxMinutes: 2 }, 'budget-account-time'),
|
||||
).rejects.toMatchObject({ code: 'BUDGET_BELOW_USAGE' });
|
||||
const changed = await control.updateBudget(
|
||||
owner,
|
||||
id,
|
||||
{ maxTokens: 1000000, maxMinutes: 3 },
|
||||
'budget-account-time-ok',
|
||||
);
|
||||
expect(changed.activeMilliseconds).toBe(130000);
|
||||
expect(changed.status).toBe('paused');
|
||||
});
|
||||
it('checks the tenant quota including other active reservations', async () => {
|
||||
await store.transaction('alpha', async tx => {
|
||||
const run = (await tx.get<Run>('run', id))!;
|
||||
await tx.put('run', { ...run, id: 'other-run', status: 'queued', usedTokens: 0, reservedTokens: 1200000 });
|
||||
});
|
||||
await expect(control.updateBudget(owner, id, { maxTokens: 1000000 }, 'budget-over-quota')).rejects.toMatchObject({
|
||||
code: 'QUOTA_EXCEEDED',
|
||||
});
|
||||
expect((await control.run(owner, id)).budget.maxTokens).toBe(200000);
|
||||
expect((await control.events(owner, id)).some(event => event.type === 'run.budget.updated')).toBe(false);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user