wishforge-ide/apps/runner/test/runner.test.ts
eust-w 82d35a3708 fix: use one canonical JSON implementation for digests and signatures
The API signed Runner tasks with keys sorted by localeCompare, while the
runner verified with code-unit order; environment keys such as
API_URL/API2 or DB_HOST/DBA sort differently, so valid deployment tasks
were rejected with INVALID_SIGNATURE. A third copy lived in the agent
package.

canonicalJson now lives in @wishforge/contracts (code-unit key order,
undefined properties omitted, non-JSON values rejected) and is used by
the API digest, Runner task signing/verification, the runner's state
digest and agent bundle digests. Binding digests computed by the old
locale-dependent order may differ, so releases requested before this
change must be requested again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 14:04:36 +08:00

75 lines
2.7 KiB
TypeScript

import { it, expect } from 'vitest';
import { generateKeyPairSync } from 'node:crypto';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { canonicalJson } from '@wishforge/contracts';
import { signRunnerTask, type RunnerTask } from '@wishforge/runtime';
import { createHash } from 'node:crypto';
import { OutboundRunner } from '../src/runner.js';
it('returns a saved receipt on duplicate delivery and reconciles an ambiguous prior operation without redeploying', async () => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-runner-'));
try {
const keys = generateKeyPairSync('ed25519');
const now = Date.now();
const task: RunnerTask = {
schemaVersion: 1,
id: 'task-1',
tenantId: 'tenant',
environmentId: 'env',
nonce: 'nonce-1',
issuedAt: new Date(now).toISOString(),
expiresAt: new Date(now + 60_000).toISOString(),
action: 'deploy',
target: 'compose',
release: {
schemaVersion: 1,
id: 'release',
tenantId: 'tenant',
environmentId: 'env',
sourceCommit: 'a'.repeat(40),
image: `registry/web@sha256:${'b'.repeat(64)}`,
containerPort: 3000,
healthPath: '/health',
},
};
let deploys = 0;
let reconciles = 0;
const runner = new OutboundRunner({
tenantId: 'tenant',
environmentId: 'env',
publicKeys: { key: keys.publicKey.export({ type: 'spki', format: 'pem' }).toString() },
stateDirectory: directory,
execute: true,
adapters: {
compose: {
render: () => '{}',
deploy: async () => {
deploys++;
return { releaseId: 'release', target: 'compose', status: 'verified', evidence: {} };
},
reconcile: async () => {
reconciles++;
return { releaseId: 'release', target: 'compose', status: 'applied', evidence: {} };
},
},
},
});
const envelope = signRunnerTask(task, 'key', keys.privateKey);
expect((await runner.execute(envelope)).status).toBe('verified');
expect((await runner.execute(envelope)).status).toBe('verified');
expect(deploys).toBe(1);
const ambiguous = signRunnerTask({ ...task, id: 'task-2', nonce: 'nonce-2' }, 'key', keys.privateKey);
await writeFile(
path.join(directory, 'task-2.json'),
JSON.stringify({ digest: createHash('sha256').update(canonicalJson(ambiguous)).digest('hex'), state: 'started' }),
);
expect((await runner.execute(ambiguous)).status).toBe('applied');
expect(reconciles).toBe(1);
expect(deploys).toBe(1);
} finally {
await rm(directory, { recursive: true, force: true });
}
});