Verification ran git inside the tree under test, kept no command output and treated every infrastructure fault as a test failure; repairs went through an executor lease the coordinator should not hold; and a project without delivery could never succeed. This reworks the worker execution path and the control-plane contracts it depends on. - Verification exports the exact commit to a directory without .git, installs dependencies (network allowed) and runs the protected commands under bubblewrap in kata mode or directly in host mode; evidence records exit code, duration, digest, a redacted output tail and the isolation used. Production rejects host evidence. A failed installation waits as infrastructure. - Waiting reasons are an enum with a redacted detail; dispatcher, GitHub and model-proxy states map onto it, and the console explains each one with a way out. - Repairs are requested by the coordinator through /internal/runs/:id/repair; the control plane derives the failure summary from the verifier's evidence and enforces the repair budget. Continuing after the budget is spent grants one more round; a new requirement revision starts a fresh budget. - Execution identities may only move a run within their own stages and only to running or waiting; success comes from integration evidence (no delivery configured) or deployment acceptance. The unused preview stage is gone. - Integration fast-forwards when the target has not moved, merges and re-verifies otherwise, retries when a push finds the target moved, and turns merge conflicts into a repair round that merges the current target for the agent to resolve. - Successful runs that needed a repair leave a project lesson; active lessons are injected into later prompts, and admins can list, add, disable and delete them. - Executor progress (messages, commands, file changes, plan updates) is stored as normalized events; previously agent messages were recorded as the literal word "output". Provider failures keep the work as a checkpoint and wait as provider_error instead of failing the run. - Workers declare WF_SANDBOX_MODE=host|kata (host refused in production), heartbeat with retries until the lease could have expired, and retry safe control-plane reads with backoff. - tests/local-loop.test.ts runs the loop end to end with real Git, the in-process control plane behind its internal routes and a scripted agent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
244 lines
12 KiB
TypeScript
244 lines
12 KiB
TypeScript
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
import { generateKeyPairSync } from 'node:crypto';
|
|
import { mkdtemp, writeFile, rm } from 'node:fs/promises';
|
|
import type { Actor, Release } from '@wishforge/contracts';
|
|
import { verifyRunnerTask, type SignedRunnerTask } from '@wishforge/runtime';
|
|
import { MemoryStore } from './helpers/memory-store.js';
|
|
import { deliverable } from './helpers/delivery.js';
|
|
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
|
|
import { Connections } from '../apps/api/src/domain/connections.js';
|
|
import { Releases } from '../apps/api/src/domain/releases.js';
|
|
import { RunnerBroker } from '../apps/api/src/domain/runner-broker.js';
|
|
import { applicationUrlFor, assertApplicationConfigured } from '../apps/api/src/domain/application-access.js';
|
|
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' },
|
|
deployer: Actor = { ...owner, userId: 'deployer', kind: 'deployer' };
|
|
const dirs: string[] = [];
|
|
afterEach(async () => {
|
|
for (const dir of dirs.splice(0)) await rm(dir, { recursive: true, force: true });
|
|
});
|
|
async function fixture() {
|
|
const db = new MemoryStore();
|
|
db.seed('alpha');
|
|
const temp = await mkdtemp(join(tmpdir(), 'wishforge-access-'));
|
|
dirs.push(temp);
|
|
await writeFile(
|
|
`${temp}/key.pem`,
|
|
generateKeyPairSync('ed25519').privateKey.export({ type: 'pkcs8', format: 'pem' }),
|
|
);
|
|
const env = { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64), WF_RUNNER_SIGNING_KEY_FILE: `${temp}/key.pem` };
|
|
const control = new ControlPlane(db, env),
|
|
releases = new Releases(db, env),
|
|
broker = new RunnerBroker(db, releases, env);
|
|
const project = await control.createProject(owner, {
|
|
name: 'Application',
|
|
repositoryUrl: 'https://example.test/repo.git',
|
|
verificationCommands: [['node', '--test']],
|
|
});
|
|
await db.transaction('alpha', tx =>
|
|
tx.put('project', {
|
|
...project,
|
|
deliveryConfig: {
|
|
containerPort: 3000,
|
|
healthPath: '/health',
|
|
smokeChecks: [{ path: '/catalog', expectedStatus: 200 }],
|
|
},
|
|
}),
|
|
);
|
|
await new Connections(db, env).create(owner, { provider: 'codex', label: 'fixture', apiKey: 'not-a-real-key' });
|
|
const { run_id: id } = await control.submit(
|
|
owner,
|
|
project.id,
|
|
{ goal: 'Deliver a catalog', acceptanceCriteria: ['Catalog contains records'] },
|
|
'url-fixture-submit',
|
|
);
|
|
const { verification, build } = await deliverable(control, 'alpha', id, {
|
|
image: `registry.example/web@sha256:${'b'.repeat(64)}`,
|
|
});
|
|
const environment = await releases.createEnvironment(owner, {
|
|
projectId: project.id,
|
|
name: 'URL fixture',
|
|
type: 'compose',
|
|
applicationUrl: 'https://app.example.test/catalog',
|
|
});
|
|
const input = {
|
|
runId: id,
|
|
environmentId: environment.id,
|
|
verificationEvidenceId: verification.id,
|
|
buildEvidenceId: build.id,
|
|
};
|
|
return { db, env, control, releases, broker, project, environment, id, input };
|
|
}
|
|
const access = (url: string) => ({
|
|
applicationUrl: url,
|
|
stableOrigin: new URL(url).origin,
|
|
checkedAt: new Date().toISOString(),
|
|
checks: ['application-url', 'public-access'],
|
|
});
|
|
describe('immutable release delivery URL and access evidence', () => {
|
|
it('requires human owner/admin configuration and rejects invalid URLs', async () => {
|
|
const f = await fixture();
|
|
const environment = await f.releases.createEnvironment(owner, {
|
|
projectId: f.project.id,
|
|
name: 'Without URL',
|
|
type: 'compose',
|
|
});
|
|
expect(environment.applicationUrl).toBeUndefined();
|
|
await expect(
|
|
f.releases.createEnvironment(
|
|
{ ...owner, kind: 'connector' },
|
|
{ projectId: f.project.id, name: 'Forbidden', type: 'compose' },
|
|
),
|
|
).rejects.toMatchObject({ code: 'HUMAN_APPROVAL_REQUIRED' });
|
|
await expect(
|
|
f.releases.updateApplicationUrl({ ...owner, role: 'developer' }, environment.id, {
|
|
applicationUrl: 'https://app.example.test/',
|
|
}),
|
|
).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
await expect(
|
|
f.releases.updateApplicationUrl(owner, environment.id, { applicationUrl: 'https://service.ns.svc/' }),
|
|
).rejects.toMatchObject({ code: 'APPLICATION_URL_INVALID' });
|
|
});
|
|
it('rejects application URLs on console, API and IDE authentication origins', () => {
|
|
for (const key of ['WF_PUBLIC_URL', 'WF_API_PUBLIC_URL', 'WF_IDE_PUBLIC_URL', 'WF_WORKSPACE_GATEWAY_BASE'])
|
|
expect(() =>
|
|
applicationUrlFor(
|
|
{ applicationUrl: 'https://shared.example.test/app', production: true },
|
|
{ [key]: 'https://shared.example.test/console' },
|
|
),
|
|
).toThrow('separate');
|
|
});
|
|
it('requires an explicit URL in production', () => {
|
|
expect(() => assertApplicationConfigured({}, { NODE_ENV: 'production' })).toThrow('explicit application URL');
|
|
expect(() => assertApplicationConfigured({}, { NODE_ENV: 'development' })).not.toThrow();
|
|
});
|
|
it('binds the application URL into approval and rejects a changed or removed URL', async () => {
|
|
for (const change of [{ applicationUrl: 'https://next.example.test/' }, { applicationUrl: null }]) {
|
|
const f = await fixture();
|
|
const release = await f.releases.request(owner, f.input, 'binding-url-release');
|
|
expect(release).toMatchObject({ applicationUrl: f.environment.applicationUrl, deliveryRequired: true });
|
|
await f.releases.updateApplicationUrl(owner, f.environment.id, change);
|
|
await expect(
|
|
f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest }),
|
|
).rejects.toMatchObject({ code: 'RELEASE_STALE' });
|
|
}
|
|
});
|
|
it('signature covers the URL and a healthy receipt without an access check cannot deliver', async () => {
|
|
const f = await fixture();
|
|
const release = await f.releases.request(owner, f.input, 'signed-url-release');
|
|
await f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest });
|
|
const registration = await f.broker.register(owner, f.environment.id);
|
|
const runner = { ...deployer, userId: `runner:${f.environment.id}`, clientId: registration.id };
|
|
const envelope = (await f.broker.poll(runner, f.environment.id))!;
|
|
const scope = { tenantId: 'alpha', environmentId: f.environment.id, publicKeys: registration.publicKeys };
|
|
expect(verifyRunnerTask(envelope as SignedRunnerTask, scope).release.applicationUrl).toBe(
|
|
f.environment.applicationUrl,
|
|
);
|
|
const tampered = structuredClone(envelope);
|
|
tampered.payload.release.applicationUrl = 'https://attacker.example.test/';
|
|
expect(() => verifyRunnerTask(tampered as SignedRunnerTask, scope)).toThrow('signature');
|
|
const result = {
|
|
releaseId: release.id,
|
|
status: 'verified',
|
|
target: 'compose',
|
|
evidence: { identityMatched: true, trafficSwitched: true, checks: ['health', 'business-smoke'] },
|
|
};
|
|
expect(await f.broker.result(runner, envelope.payload.id, { result })).toMatchObject({ delivered: false });
|
|
expect(await f.db.transaction('alpha', tx => tx.get('release', release.id))).not.toHaveProperty('deliveryUrl');
|
|
});
|
|
it('records and returns only the exact application URL after matching access acceptance', async () => {
|
|
const f = await fixture();
|
|
const release = await f.releases.request(owner, f.input, 'accepted-url-release');
|
|
await f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest });
|
|
const registration = await f.broker.register(owner, f.environment.id);
|
|
const runner = { ...deployer, userId: `runner:${f.environment.id}`, clientId: registration.id };
|
|
const envelope = (await f.broker.poll(runner, f.environment.id))!;
|
|
await expect(
|
|
f.releases.updateApplicationUrl(owner, f.environment.id, { applicationUrl: null }),
|
|
).rejects.toMatchObject({ code: 'ENVIRONMENT_CONFLICT' });
|
|
const result = {
|
|
releaseId: release.id,
|
|
status: 'verified',
|
|
target: 'compose',
|
|
evidence: {
|
|
identityMatched: true,
|
|
trafficSwitched: true,
|
|
checks: ['health', 'business-smoke'],
|
|
accessEvidence: access(f.environment.applicationUrl!),
|
|
},
|
|
};
|
|
expect(await f.broker.result(runner, envelope.payload.id, { result })).toMatchObject({
|
|
delivered: true,
|
|
url: f.environment.applicationUrl,
|
|
});
|
|
const saved = await f.db.transaction('alpha', tx => tx.get<Release>('release', release.id));
|
|
expect(saved).toMatchObject({ status: 'healthy', deliveryUrl: f.environment.applicationUrl });
|
|
expect(await f.releases.delivery(owner, f.id)).toMatchObject({ url: f.environment.applicationUrl, accepted: true });
|
|
await f.releases.updateApplicationUrl(owner, f.environment.id, { applicationUrl: null });
|
|
expect(await f.releases.delivery(owner, f.id)).not.toHaveProperty('url');
|
|
expect((await f.releases.list(owner))[0]).not.toHaveProperty('deliveryUrl');
|
|
});
|
|
it('binds operator secret policy into approval and the signed Runner manifest', async () => {
|
|
const f = await fixture(),
|
|
policy = `sha256:${'c'.repeat(64)}`;
|
|
await expect(
|
|
f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: ['database@version-7'],
|
|
deploymentSecretPolicyDigest: null,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'DEPLOYMENT_SECRET_POLICY_REQUIRED' });
|
|
await f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: ['database@version-7'],
|
|
deploymentSecretPolicyDigest: policy,
|
|
});
|
|
const release = await f.releases.request(owner, f.input, 'secret-policy-signed');
|
|
expect(release).toMatchObject({ secretVersionRefs: ['database@version-7'], deploymentSecretPolicyDigest: policy });
|
|
await f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest });
|
|
const registration = await f.broker.register(owner, f.environment.id),
|
|
runner = { ...deployer, userId: `runner:${f.environment.id}`, clientId: registration.id };
|
|
const envelope = (await f.broker.poll(runner, f.environment.id))!;
|
|
expect(envelope.payload.release.deploymentSecretPolicyDigest).toBe(policy);
|
|
await expect(
|
|
f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: [],
|
|
deploymentSecretPolicyDigest: null,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'ENVIRONMENT_CONFLICT' });
|
|
});
|
|
it('only human Owner/Admin may update references and changed policy invalidates old approval', async () => {
|
|
const f = await fixture(),
|
|
policy = `sha256:${'c'.repeat(64)}`;
|
|
await expect(
|
|
f.releases.updateDeploymentSecrets({ ...owner, kind: 'connector' }, f.environment.id, {
|
|
secretVersionRefs: [],
|
|
deploymentSecretPolicyDigest: null,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'HUMAN_APPROVAL_REQUIRED' });
|
|
await expect(
|
|
f.releases.updateDeploymentSecrets({ ...owner, role: 'developer' }, f.environment.id, {
|
|
secretVersionRefs: [],
|
|
deploymentSecretPolicyDigest: null,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
await f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: ['database@version-7'],
|
|
deploymentSecretPolicyDigest: policy,
|
|
});
|
|
const release = await f.releases.request(owner, f.input, 'secret-policy-change');
|
|
await f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: ['database@version-7'],
|
|
deploymentSecretPolicyDigest: `sha256:${'d'.repeat(64)}`,
|
|
});
|
|
await expect(
|
|
f.releases.decide(owner, release.approvalId!, { decision: 'approve', bindingDigest: release.bindingDigest }),
|
|
).rejects.toMatchObject({ code: 'RELEASE_STALE' });
|
|
const cleared = await f.releases.updateDeploymentSecrets(owner, f.environment.id, {
|
|
secretVersionRefs: [],
|
|
deploymentSecretPolicyDigest: null,
|
|
});
|
|
expect(cleared.secretVersionRefs).toEqual([]);
|
|
expect(cleared.deploymentSecretPolicyDigest).toBeUndefined();
|
|
});
|
|
});
|