- Drop the WorkBuddy Cloud executor stub: it always reported "unverified" and could never run. WorkBuddy remains an MCP client for submitting wishes. Providers are now codex and codebuddy; an unknown provider waits with an explicit reason instead of a fake adapter. - Remove internal routes nobody calls (/internal/releases/:id/begin, /accept, /failure and /internal/workspaces/:id/provisioned) with their domain methods, the never-consumed release.ready and release.rollback outbox events, an unreachable idempotency branch, dead helpers in runtime/deploy.ts and an unused RunnerBroker dependency. - Enable noUnusedLocals in the base and web TypeScript configs; the runtime package now extends the base config. - Add tests/tsconfig.json and include it in `npm run typecheck`. This surfaced tests still calling the deleted begin/accept path (now exercised through the Runner protocol) and five latent type errors. - Web: provider capability labels match the API's keys and the wish form disables unavailable providers. - Delete the invalid example Runner Job manifest; stop tracking the generated apps/web/next-env.d.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
247 lines
11 KiB
TypeScript
247 lines
11 KiB
TypeScript
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import type { Actor } from '@wishforge/contracts';
|
|
import { MemoryStore } from './helpers/memory-store.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 { AgentBundles } from '../apps/api/src/domain/agent-bundles.js';
|
|
import { digest } from '../apps/api/src/domain/policy.js';
|
|
|
|
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
|
|
const worker: Actor = { ...owner, kind: 'worker', userId: 'worker' },
|
|
verifier: Actor = { ...owner, kind: 'verifier', userId: 'verifier' };
|
|
let db: MemoryStore, control: ControlPlane, releases: Releases, connections: Connections;
|
|
const goal = {
|
|
goal: 'Add search to the catalog',
|
|
acceptanceCriteria: ['Searching an existing item returns its row'],
|
|
provider: 'codex',
|
|
};
|
|
beforeEach(() => {
|
|
db = new MemoryStore();
|
|
db.seed('alpha');
|
|
db.seed('beta');
|
|
const env = { WF_VAULT_KEY: 'a'.repeat(64), NODE_ENV: 'test' };
|
|
control = new ControlPlane(db, env);
|
|
connections = new Connections(db, env);
|
|
releases = new Releases(db);
|
|
});
|
|
async function setup(connected = false) {
|
|
const p = await control.createProject(owner, {
|
|
name: 'Catalog',
|
|
repositoryUrl: 'https://example.org/catalog.git',
|
|
verificationCommands: [['node', '--test']],
|
|
});
|
|
if (connected)
|
|
await connections.create(owner, {
|
|
provider: 'codex',
|
|
label: 'Test envelope only',
|
|
apiKey: 'test-not-a-real-provider-key',
|
|
});
|
|
const result = await control.submit(owner, p.id, goal, 'submission-001');
|
|
return { p, runId: result.run_id };
|
|
}
|
|
|
|
describe('tenant-scoped durable task invariants', () => {
|
|
it('deduplicates concurrent submissions and reserves budget once', async () => {
|
|
const p = await control.createProject(owner, { name: 'Catalog' });
|
|
const results = await Promise.all(
|
|
Array.from({ length: 10 }, () => control.submit(owner, p.id, goal, 'same-submission')),
|
|
);
|
|
expect(new Set(results.map(r => r.run_id)).size).toBe(1);
|
|
expect((await control.usage(owner)).reservedTokens).toBe(100000);
|
|
expect(await control.runs(owner)).toHaveLength(1);
|
|
});
|
|
it('rejects reused keys with different intent', async () => {
|
|
const { p } = await setup();
|
|
await expect(
|
|
control.submit(owner, p.id, { ...goal, goal: 'Delete the catalog' }, 'submission-001'),
|
|
).rejects.toMatchObject({ code: 'IDEMPOTENCY_CONFLICT' });
|
|
});
|
|
it('does not execute an unconfigured provider or report fabricated completion', async () => {
|
|
const { runId } = await setup();
|
|
expect(await control.run(owner, runId)).toMatchObject({ status: 'waiting', waitingReason: 'credentials' });
|
|
await expect(control.lease(worker, runId, 'executor')).rejects.toMatchObject({ code: 'RUN_NOT_RUNNABLE' });
|
|
});
|
|
it('denies access across tenants and to nonmember project users', async () => {
|
|
const { runId } = await setup();
|
|
await expect(control.run({ ...owner, tenantId: 'beta' }, runId)).rejects.toMatchObject({ code: 'NOT_FOUND' });
|
|
await expect(control.run({ ...owner, userId: 'viewer', role: 'viewer' }, runId)).rejects.toMatchObject({
|
|
code: 'NOT_FOUND',
|
|
});
|
|
});
|
|
it('checks capacity atomically under simultaneous claims', async () => {
|
|
const p = await control.createProject(owner, { name: 'Catalog', repositoryUrl: 'https://example.org/catalog.git' });
|
|
await connections.create(owner, { provider: 'codex', label: 'test', apiKey: 'test-fixture-value' });
|
|
const ids = await Promise.all([1, 2, 3].map(n => control.submit(owner, p.id, goal, `capacity-${n}`)));
|
|
const results = await Promise.allSettled(ids.map(({ run_id }) => control.lease(worker, run_id, run_id)));
|
|
expect(results.filter(r => r.status === 'fulfilled')).toHaveLength(2);
|
|
expect(results.filter(r => r.status === 'rejected')).toHaveLength(1);
|
|
});
|
|
it('revokes an old worker after pause and does not discard the project', async () => {
|
|
const { runId, p } = await setup(true);
|
|
const run = await control.lease(worker, runId, 'executor');
|
|
await control.command(owner, runId, { action: 'pause' }, 'pause-001');
|
|
await expect(
|
|
control.transition(worker, runId, {
|
|
leaseToken: run.lease!.token,
|
|
stage: 'plan',
|
|
status: 'running',
|
|
message: 'late worker',
|
|
}),
|
|
).rejects.toMatchObject({ code: 'STALE_LEASE' });
|
|
expect((await control.projects(owner))[0].id).toBe(p.id);
|
|
});
|
|
it('requires verifier identity and protected commands before accepting evidence', async () => {
|
|
const { runId } = await setup(true);
|
|
const lease = await control.lease(verifier, runId, 'verifier');
|
|
const data = {
|
|
leaseToken: lease.lease!.token,
|
|
runId,
|
|
revision: 1,
|
|
sourceCommit: 'a'.repeat(40),
|
|
type: 'verification',
|
|
passed: true,
|
|
commands: [],
|
|
};
|
|
await expect(control.evidence(worker, data)).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
await expect(control.evidence(verifier, data)).rejects.toMatchObject({ code: 'INCOMPLETE_VERIFICATION' });
|
|
});
|
|
it('cannot skip phases or let a worker declare delivery success', async () => {
|
|
const { runId } = await setup(true);
|
|
const run = await control.lease(worker, runId, 'executor');
|
|
await expect(
|
|
control.transition(worker, runId, {
|
|
leaseToken: run.lease!.token,
|
|
stage: 'build',
|
|
status: 'running',
|
|
message: 'skip',
|
|
}),
|
|
).rejects.toMatchObject({ code: 'INVALID_TRANSITION' });
|
|
await expect(
|
|
control.transition(worker, runId, {
|
|
leaseToken: run.lease!.token,
|
|
stage: 'accept',
|
|
status: 'succeeded',
|
|
message: 'done',
|
|
}),
|
|
).rejects.toMatchObject({ code: 'DELIVERY_EVIDENCE_REQUIRED' });
|
|
});
|
|
it('versions requirements and clears stale verification', async () => {
|
|
const { runId } = await setup(true);
|
|
const lease = await control.lease(verifier, runId, 'verifier');
|
|
await control.evidence(verifier, {
|
|
leaseToken: lease.lease!.token,
|
|
runId,
|
|
revision: 1,
|
|
sourceCommit: 'a'.repeat(40),
|
|
type: 'verification',
|
|
passed: true,
|
|
commands: [{ argv: ['node', '--test'], exitCode: 0, outputDigest: digest('output') }],
|
|
});
|
|
const next = await control.command(
|
|
owner,
|
|
runId,
|
|
{ action: 'answer', message: 'Search must ignore case' },
|
|
'answer-001',
|
|
);
|
|
expect(next.revision).toBe(2);
|
|
expect(next.verifiedCommit).toBeUndefined();
|
|
});
|
|
it('does not return secrets and rejects another tenant decrypting the envelope', async () => {
|
|
const c = await connections.create(owner, {
|
|
provider: 'codex',
|
|
label: 'tenant-owned',
|
|
apiKey: 'sensitive-fixture-key',
|
|
});
|
|
expect(JSON.stringify(c)).not.toContain('sensitive-fixture-key');
|
|
const raw = await db.transaction('alpha', tx => tx.get('provider_connection', c.id));
|
|
await expect(connections.vault.open('beta', String(raw!.ciphertext))).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe('artifact-bound publication', () => {
|
|
async function prepared() {
|
|
const { p, runId } = await setup(true);
|
|
const lease = await control.lease(verifier, runId, 'verifier');
|
|
const commit = 'a'.repeat(40),
|
|
image = `registry.example/catalog@sha256:${'b'.repeat(64)}`;
|
|
const v = await control.evidence(verifier, {
|
|
leaseToken: lease.lease!.token,
|
|
runId,
|
|
revision: 1,
|
|
sourceCommit: commit,
|
|
type: 'verification',
|
|
passed: true,
|
|
commands: [{ argv: ['node', '--test'], exitCode: 0, outputDigest: digest('passed') }],
|
|
});
|
|
const b = await control.evidence(verifier, {
|
|
leaseToken: lease.lease!.token,
|
|
runId,
|
|
revision: 1,
|
|
sourceCommit: commit,
|
|
type: 'build',
|
|
passed: true,
|
|
imageDigest: image,
|
|
});
|
|
const env = await releases.createEnvironment(owner, {
|
|
projectId: p.id,
|
|
name: 'production',
|
|
type: 'compose',
|
|
production: true,
|
|
});
|
|
const rel = await releases.request(
|
|
owner,
|
|
{ runId, environmentId: env.id, verificationEvidenceId: v.id, buildEvidenceId: b.id, changedPaths: [] },
|
|
'release-001',
|
|
);
|
|
return { rel, env, runId, commit, image };
|
|
}
|
|
it('does not trust caller risk classification and prohibits connector self-approval', async () => {
|
|
const { rel } = await prepared();
|
|
expect(rel.status).toBe('awaiting_approval');
|
|
expect(rel.riskReasons).toContain('Missing trusted change classification');
|
|
await expect(
|
|
releases.decide({ ...owner, kind: 'connector' }, rel.approvalId!, {
|
|
decision: 'approve',
|
|
bindingDigest: rel.bindingDigest,
|
|
}),
|
|
).rejects.toMatchObject({ code: 'HUMAN_APPROVAL_REQUIRED' });
|
|
});
|
|
it('invalidates release approval when requirements change', async () => {
|
|
const { rel, runId } = await prepared();
|
|
await control.command(owner, runId, { action: 'answer', message: 'Also add sorting' }, 'answer-release');
|
|
await expect(
|
|
releases.decide(owner, rel.approvalId!, { decision: 'approve', bindingDigest: rel.bindingDigest }),
|
|
).rejects.toMatchObject({ code: 'APPROVAL_EXPIRED' });
|
|
});
|
|
});
|
|
|
|
describe('agent configuration versions', () => {
|
|
const config = { schemaVersion: 1, prompts: { builder: 'Prefer small, reviewed commits.' } };
|
|
it('pins the active version at submission and keeps it after a later rollback', async () => {
|
|
const bundles = new AgentBundles(db);
|
|
const created = await bundles.create(owner, { name: 'Small commits', config });
|
|
expect(created.version).toMatch(/^sha256:[0-9a-f]{64}$/);
|
|
expect((await bundles.create(owner, { name: 'Duplicate', config })).created).toBe(false);
|
|
await bundles.activate(owner, created.version);
|
|
const { p } = await setup(true);
|
|
const { run_id } = await control.submit(owner, p.id, goal, 'bundle-pinned-run');
|
|
expect((await control.run(owner, run_id)).bundleVersion).toBe(created.version);
|
|
|
|
expect(await bundles.rollback(owner)).toEqual({ activeVersion: 'baseline-v1', previousVersion: created.version });
|
|
expect((await control.run(owner, run_id)).bundleVersion).toBe(created.version);
|
|
const second = await control.submit(owner, p.id, goal, 'bundle-baseline-run');
|
|
expect((await control.run(owner, second.run_id)).bundleVersion).toBe('baseline-v1');
|
|
});
|
|
it('restricts configuration changes to human owners and admins', async () => {
|
|
const bundles = new AgentBundles(db);
|
|
await expect(bundles.create({ ...owner, kind: 'connector' }, { name: 'x', config })).rejects.toMatchObject({
|
|
status: 403,
|
|
});
|
|
await expect(bundles.create({ ...owner, role: 'developer' }, { name: 'x', config })).rejects.toMatchObject({
|
|
status: 403,
|
|
});
|
|
await expect(bundles.activate(owner, digest('missing'))).rejects.toMatchObject({ code: 'BUNDLE_UNAVAILABLE' });
|
|
});
|
|
});
|