wishforge-ide/tests/execution-generation.test.ts
eust-w cce214f8fb feat: close the main loop from wish to integrated commit
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>
2026-10-19 15:15:54 +08:00

78 lines
3.7 KiB
TypeScript

import { expect, it } from 'vitest';
import { Authenticator } from '../apps/api/src/auth.js';
import { ServiceTokens } from '../apps/api/src/service-tokens.js';
import { ControlPlane, record } from '../apps/api/src/domain/control-plane.js';
import { MemoryStore } from './helpers/memory-store.js';
import type { Actor, Run } from '@wishforge/contracts';
async function setup() {
const store = new MemoryStore();
store.seed('alpha');
const env = { WF_INTERNAL_TOKEN_KEY: 't'.repeat(64), WF_DISPATCH_TENANTS: 'alpha' };
const actor: Actor = { userId: 'owner', tenantId: 'alpha', role: 'owner', kind: 'human' };
const control = new ControlPlane(store, env);
const project = await control.createProject(actor, {
name: 'generation fixture',
repositoryUrl: 'https://example.invalid/repo.git',
});
const { run_id } = await control.submit(
actor,
project.id,
{ goal: 'Exercise generation fencing', acceptanceCriteria: ['Old execution tokens are rejected'] },
'generation-wish',
);
await store.transaction('alpha', async tx => {
const run = (await tx.get<Run>('run', run_id))!;
run.executionGeneration = 'generation-1';
run.status = 'queued';
await tx.put('run', run);
});
return { store, env, actor, control, runId: run_id };
}
it('revokes old generation tokens and denies terminal-run execution credentials', async () => {
const { store, env, runId } = await setup();
const tokens = new ServiceTokens(env.WF_INTERNAL_TOKEN_KEY);
const auth = new Authenticator(store, env);
const old = await tokens.issue('alpha', runId, 'worker', 'generation-1');
expect(await auth.internal(`Bearer ${old}`, 'alpha')).toMatchObject({ runId, credentialId: 'generation-1' });
await store.transaction('alpha', async tx => {
const run = (await tx.get<Run>('run', runId))!;
run.executionGeneration = 'generation-2';
await tx.put('run', run);
});
await expect(auth.internal(`Bearer ${old}`, 'alpha')).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
const fresh = await tokens.issue('alpha', runId, 'worker', 'generation-2');
await store.transaction('alpha', async tx => {
const run = (await tx.get<Run>('run', runId))!;
run.status = 'succeeded';
await tx.put('run', run);
});
await expect(auth.internal(`Bearer ${fresh}`, 'alpha')).rejects.toMatchObject({ code: 'UNAUTHENTICATED' });
});
it('an old trusted coordinator cannot lease or overwrite a newly queued generation', async () => {
const { control, runId } = await setup();
const coordinator: Actor = { tenantId: 'alpha', userId: 'worker', kind: 'worker', role: 'admin' };
await expect(control.lease(coordinator, runId, 'old-worker', 'obsolete')).rejects.toMatchObject({
code: 'STALE_GENERATION',
});
const waiting = await control.waitRun(coordinator, runId, {
reason: 'ambiguous_execution',
detail: 'Old attempt failed',
generation: 'obsolete',
});
expect(waiting.status).toBe('queued');
expect((await control.lease(coordinator, runId, 'new-worker', 'generation-1')).status).toBe('running');
});
it('a superseded start operation cannot be confused with the newest request', async () => {
const { store, control, actor, runId } = await setup();
await store.transaction('alpha', async tx => {
await tx.put('provider_connection', { ...record('alpha'), provider: 'codex', status: 'active' });
const r = (await tx.get<Run>('run', runId))!;
r.status = 'waiting';
await tx.put('run', r);
});
const resumed = await control.command(actor, runId, { action: 'resume' }, 'resume-generation-1');
const starts = await store.transaction('alpha', tx => tx.outbox());
expect(starts.find(e => e.type === 'run.start')?.id).toBe(resumed.pendingStartId);
});