wishforge-ide/tests/artifacts.test.ts
eust-w 30369c1430 ✨ Save the agent's work before a run pauses
Pausing revoked the executor at once: the agent was killed and the work
of the current attempt stayed only in the executor's local workspace,
so continuing started that attempt over.

A pause of a run whose executor holds the lease now drains it first:

- The run keeps `running` with pauseRequestedAt (the console shows
  pausing). Its agent gets no model credential and no new model
  request; continuing is refused until the pause completes; cancelling
  still stops it at once.
- The executor learns of it from its heartbeat, stops the agent, saves
  a checkpoint with its live lease and completes the pause through
  /internal/runs/:id/pause. Continuing restores that checkpoint.
- The workflow is stopped only after the pause completes; stopping it
  earlier would abort the save. A model request interrupted by the
  stop no longer expires the pausing run's lease.
- If the executor does not complete it within two minutes, or stopped,
  the heartbeat, the next lease, the coordinator's wait or the
  background sweep pause the run anyway.

Runs no executor is working on still pause at once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:38:06 +08:00

113 lines
5.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { S3Client } from '@aws-sdk/client-s3';
import type { Actor, Run } from '@wishforge/contracts';
import { S3Artifacts, sha256 } from '../packages/artifacts/src/index.js';
import { ControlPlane } from '../apps/api/src/domain/control-plane.js';
import { Connections } from '../apps/api/src/domain/connections.js';
import { Artifacts } from '../apps/api/src/domain/artifacts.js';
import { MemoryStore } from './helpers/memory-store.js';
const scope = { tenantId: 'alpha', projectId: 'project-1', runId: 'run-1' },
body = Buffer.from('fixture git bundle');
const owner: Actor = { tenantId: 'alpha', userId: 'owner', role: 'owner', kind: 'human' },
worker: Actor = { ...owner, userId: 'worker', kind: 'worker' };
let db: MemoryStore, control: ControlPlane, run: Run;
beforeEach(async () => {
db = new MemoryStore();
db.seed('alpha');
const env = { NODE_ENV: 'test', WF_VAULT_KEY: 'a'.repeat(64) };
control = new ControlPlane(db, env);
await new Connections(db, env).create(owner, { provider: 'codex', label: 'fixture', apiKey: 'not-a-real-key' });
const p = await control.createProject(owner, { name: 'Artifacts', repositoryUrl: 'https://example.org/fixture.git' });
const w = await control.submit(
owner,
p.id,
{ goal: 'Persist source', acceptanceCriteria: ['Source restores exactly'] },
'artifact-fixture',
);
run = await control.lease(worker, w.run_id, 'fixture-worker');
});
describe('private content-addressed artifacts', () => {
it('rejects unconfigured storage, a false checksum and oversized payload before I/O', async () => {
const send = vi.fn();
const client = { send } as unknown as S3Client;
await expect(new S3Artifacts({}, client).put(scope, body, sha256(body))).rejects.toMatchObject({
code: 'ARTIFACT_STORAGE_UNCONFIGURED',
});
const s3 = new S3Artifacts({ S3_BUCKET: 'private', S3_REGION: 'test' }, client);
await expect(s3.put(scope, body, 'a'.repeat(64))).rejects.toMatchObject({ code: 'ARTIFACT_DIGEST_MISMATCH' });
const large = Buffer.alloc(4 * 1024 * 1024 + 1);
await expect(s3.put(scope, large, sha256(large))).rejects.toMatchObject({ code: 'ARTIFACT_TOO_LARGE' });
expect(send).not.toHaveBeenCalled();
});
it('requires explicit KMS and HTTPS in production and verifies encryption acknowledgement', async () => {
const send = vi.fn().mockResolvedValue({});
const client = { send } as unknown as S3Client;
for (const extra of [
{},
{ S3_SSE: 'AES256' },
{ S3_SSE: 'aws:kms', S3_KMS_KEY_ID: 'key', S3_ENDPOINT: 'http://minio' },
]) {
await expect(
new S3Artifacts({ NODE_ENV: 'production', S3_BUCKET: 'private', S3_REGION: 'test', ...extra }, client).put(
scope,
body,
sha256(body),
),
).rejects.toMatchObject({ code: 'ARTIFACT_KMS_REQUIRED' });
}
const kms = new S3Artifacts(
{ NODE_ENV: 'production', S3_BUCKET: 'private', S3_REGION: 'test', S3_SSE: 'aws:kms', S3_KMS_KEY_ID: 'key' },
client,
);
await expect(kms.put(scope, body, sha256(body))).rejects.toMatchObject({ code: 'ARTIFACT_ENCRYPTION_MISMATCH' });
expect(send.mock.calls[0][0].input).toMatchObject({
ServerSideEncryption: 'aws:kms',
SSEKMSKeyId: 'key',
IfNoneMatch: '*',
});
});
it('validates object ownership and checks downloaded bytes independently of metadata', async () => {
const checksum = sha256(body),
send = vi.fn();
const s3 = new S3Artifacts({ S3_BUCKET: 'private', S3_REGION: 'test' }, { send } as unknown as S3Client);
const stored = {
...scope,
bucket: 'private',
key: s3.key(scope, checksum),
sha256: checksum,
size: body.length,
contentType: 'application/x-git-bundle',
encryption: 'none' as const,
};
await expect(s3.get({ ...stored, projectId: 'another-project' })).rejects.toMatchObject({
code: 'ARTIFACT_SCOPE_MISMATCH',
});
expect(send).not.toHaveBeenCalled();
send.mockResolvedValue({
ContentLength: body.length,
Metadata: { tenant: scope.tenantId, project: scope.projectId, run: scope.runId, sha256: checksum },
Body: (async function* () {
yield Buffer.alloc(body.length);
})(),
});
await expect(s3.get(stored)).rejects.toMatchObject({ code: 'ARTIFACT_DIGEST_MISMATCH' });
});
it('permits explicitly marked inline development bundles but rejects a false digest', async () => {
const cp = { gitBundleBase64: body.toString('base64'), gitBundleSha256: sha256(body) };
const result = await control.checkpoint(worker, run.id, cp, run.lease!.token);
expect(result.checkpoint).toMatchObject({ ...cp, artifactStorage: 'inline-development' });
await expect(
control.checkpoint(worker, run.id, { ...cp, gitBundleSha256: 'f'.repeat(64) }, run.lease!.token),
).rejects.toMatchObject({ code: 'ARTIFACT_DIGEST_MISMATCH' });
await expect(
new Artifacts(db, { NODE_ENV: 'production' }).uploadCheckpointBundle(worker, run.id, run.lease!.token, cp),
).rejects.toMatchObject({ code: 'ARTIFACT_STORAGE_UNCONFIGURED' });
});
it('rejects upload after lease expiry and cross-run artifact reuse', async () => {
await control.command(owner, run.id, { action: 'cancel' }, 'artifact-cancel');
await expect(control.checkpoint(worker, run.id, {}, run.lease!.token)).rejects.toMatchObject({
code: 'STALE_LEASE',
});
});
});