`npm run format:check` flagged infra/k8s/cluster-poc/platform.yaml, tests/model-catalog.test.ts, tests/run-budget.test.ts and tests/wish-refiner.test.ts (prettier reflow only). The control-hardening test still expected the release failure reason the broker used to write before canceling a run began failing its own releases directly — it is now 「运行已取消」, matching run-lifecycle.test.ts. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
172 lines
7.4 KiB
TypeScript
172 lines
7.4 KiB
TypeScript
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: 500_000_001 },
|
|
{ maxMinutes: 181 },
|
|
{ maxRepairRounds: 4 },
|
|
])
|
|
expect(budgetPatchSchema.safeParse(input).success).toBe(false);
|
|
expect(budgetPatchSchema.parse({ maxTokens: 1000000 })).toEqual({ maxTokens: 1000000 });
|
|
expect(budgetPatchSchema.parse({ maxTokens: 500_000_000 })).toEqual({ maxTokens: 500_000_000 });
|
|
});
|
|
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);
|
|
});
|
|
});
|