wishforge-ide/tests/integration-lease.test.ts
eust-w 5df77ad761 🔒 Authenticate internal callers without the write lock; retire superseded generations
- Looking up a run-scoped token or a runner token ran in a write
  transaction, which holds the tenant's advisory lock: anyone with a
  long enough token could tie up every change of the tenant, and each
  executor heartbeat, event and meter took that lock just to
  authenticate. The lookups now only read.
- Continuing or answering a run queues a new generation, but until the
  dispatcher started it, the old generation's token still worked and
  could even lease the run again for the new requirement revision. A
  generation now stops acting as soon as a newer one is pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 19:11:12 +08:00

88 lines
3.9 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import type { Actor, Run } from '@wishforge/contracts';
import { MemoryStore } from './helpers/memory-store.js';
import { ControlPlane, record } from '../apps/api/src/domain/control-plane.js';
import { Connections } from '../apps/api/src/domain/connections.js';
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' };
async function fixture() {
const db = new MemoryStore();
db.seed('alpha');
const control = new ControlPlane(db, { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64) });
const project = await control.createProject(owner, {
name: 'Integration queue',
repositoryUrl: 'https://example.test/repo.git',
verificationCommands: [['node', '--test']],
});
await new Connections(db, control.env).create(owner, {
provider: 'codex',
label: 'fixture',
apiKey: 'fixture-only-key',
});
/** Submits a run and leases it to a run-scoped verifier of a fixed generation. */
async function verifierFor(key: string) {
const { run_id: id } = await control.submit(
owner,
project.id,
{ goal: `Change ${key}`, acceptanceCriteria: ['It works'] },
`integration-${key}`,
);
const generation = `generation-${key}`;
await db.transaction('alpha', async tx => {
const run = (await tx.get<Run>('run', id))!;
// As the dispatcher starts the pending generation (reserveExecutionGrant).
run.executionGeneration = run.pendingStartId = generation;
await tx.put('run', run);
});
const actor: Actor = { ...owner, kind: 'verifier', userId: `verifier-${key}`, runId: id, credentialId: generation };
const leased = await control.lease(actor, id, `worker-${key}`, generation);
return { id, actor, token: leased.lease!.token };
}
return { db, control, project, verifierFor };
}
describe('project integration lease', () => {
it('admits one integrating run per project and fences stale owners', async () => {
const f = await fixture();
const first = await f.verifierFor('one');
const second = await f.verifierFor('two');
const lease = await f.control.integration.acquire(first.actor, first.id, first.token);
expect(lease).toMatchObject({ runId: first.id, fence: 1 });
expect(await f.control.integration.acquire(first.actor, first.id, first.token)).toMatchObject({ fence: 1 });
await expect(f.control.integration.acquire(second.actor, second.id, second.token)).rejects.toMatchObject({
code: 'INTEGRATION_BUSY',
});
await expect(f.control.integration.renew(first.actor, first.id, first.token, 99)).rejects.toMatchObject({
code: 'STALE_INTEGRATION_FENCE',
});
expect(await f.control.integration.release(first.actor, first.id, first.token, 1)).toEqual({ released: true });
expect(await f.control.integration.acquire(second.actor, second.id, second.token)).toMatchObject({ fence: 2 });
});
it('requires the live lease of this execution before integrating or building', async () => {
const f = await fixture();
const run = await f.verifierFor('gate');
await expect(f.control.integration.enforce(run.actor, run.id, run.token)).rejects.toMatchObject({
code: 'INTEGRATION_LEASE_REQUIRED',
});
await f.control.integration.acquire(run.actor, run.id, run.token);
await expect(f.control.integration.enforce(run.actor, run.id, run.token)).resolves.toBeUndefined();
});
it('does not let an open IDE workspace block agent integration', async () => {
const f = await fixture();
const run = await f.verifierFor('ide');
await f.db.transaction('alpha', tx =>
tx.put('workspace', {
...record('alpha'),
projectId: f.project.id,
status: 'ready',
upstreamUrl: 'http://127.0.0.1:4300',
}),
);
await f.control.integration.acquire(run.actor, run.id, run.token);
await expect(f.control.integration.enforce(run.actor, run.id, run.token)).resolves.toBeUndefined();
});
});