Files
gstack/test/llm-judge-abort.test.ts
T
Garry Tanandcapy-ai[bot] 043feff8b3 v1.91.309.0 fix(flaky): one rule each in the judged review and Eng prose; a cancelled judge names what it was waiting on (GSTA-139 S3) (#3544)
* fix(flaky): give the judged review and Eng prose one rule each, and name what a cancelled judge received (GSTA-139 S3)

- review: Step 5.8 records an unresolved non-gate defect as open, and a repeat
  pass's reruns are revalidation that run after smoke expiry in the same
  PROBE_DIR with materialize once on the final pass (every sample of #3527's
  red panel 40617200857 and its rerun named one of these). Nearby prose trimmed
  so union, skeleton and host pins do not grow.
- plan-eng-review: drop the Eng-only em-dash Voice variant so the shared Voice
  hoists; the Scope gate's Format precedence names em dashes (5 of 6 samples of
  40634787316 named the near-copy Voice). Host size pins lowered.
- llm-judge: a cancelled judge request prints llm-judge-aborted with timing,
  429 waits and stream event counts (no content), so the next workflow-judge
  deadline (train 35 gate 40660626349) names its cause.
- benchmark: the Phase 6 example no longer lazy-loads the hero image.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(review): earlier passes link checkpoints; only the final pass materializes (all 3 samples of the focused judge run at 1bdc4ce)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): Unreleased entry for the GSTA-139 S3 judge prose repairs

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test: record llm-judge abort diagnostic as non-behavioral; abort tests wait on events, not sleeps

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: pre-landing review fixes

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs: name the llm-judge-aborted line in TESTING_INTERNALS

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-10-29 05:50:41 +00:00

126 lines
6.1 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, spyOn, test } from 'bun:test';
import { Messages } from '@anthropic-ai/sdk/resources/messages';
import { resolveEvalModel } from '../lib/eval-model';
import { callJudge } from './helpers/llm-judge';
// Stub the SDK request method, not fetch: every test stays local even when an
// operator has API credentials configured. Restore the prototype after each.
const response = { content: [{ type: 'text', text: 'Result: {"passed":true}' }] };
let create: ReturnType<typeof spyOn>;
let random: ReturnType<typeof spyOn>;
beforeEach(() => {
create = spyOn(Messages.prototype, 'create').mockResolvedValue(response as any);
random = spyOn(Math, 'random').mockReturnValue(0);
});
afterEach(() => {
create.mockRestore();
random.mockRestore();
});
describe('callJudge cancellation', () => {
test('preserves default requests and JSON parsing without a signal', async () => {
expect(await callJudge('Judge this.')).toEqual({ passed: true });
expect(create).toHaveBeenCalledTimes(1);
expect(create.mock.calls[0]).toEqual([{
model: resolveEvalModel('judge'),
max_tokens: 8192,
messages: [{ role: 'user', content: 'Judge this.' }],
}, undefined]);
});
test('forwards the signal as an SDK request option while preserving model and generation options', async () => {
const controller = new AbortController();
expect(await callJudge('Bounded judgment.', 'test-judge-model', {
signal: controller.signal, temperature: 0, max_tokens: 256,
})).toEqual({ passed: true });
expect(create.mock.calls[0]).toEqual([{
model: 'test-judge-model', max_tokens: 256, temperature: 0,
messages: [{ role: 'user', content: 'Bounded judgment.' }],
}, { signal: controller.signal }]);
});
test('rejects a pre-aborted signal without invoking the SDK', async () => {
const controller = new AbortController();
const reason = new Error('Judge deadline already expired.');
controller.abort(reason);
await expect(callJudge('Do not dispatch.', undefined, { signal: controller.signal })).rejects.toBe(reason);
expect(create).not.toHaveBeenCalled();
});
test('aborts an active SDK request without retrying and preserves the abort reason', async () => {
const controller = new AbortController();
const reason = new Error('Judge deadline expired.');
create.mockImplementation((_body: unknown, options: { signal: AbortSignal }) => new Promise((_resolve, reject) => {
options.signal.addEventListener('abort', () => reject(new Error('SDK request aborted')), { once: true });
}));
const pending = callJudge('Cancelable request.', undefined, { signal: controller.signal });
controller.abort(reason);
await expect(pending).rejects.toBe(reason);
expect(create).toHaveBeenCalledTimes(1);
});
test('rejects a late successful SDK response after cancellation', async () => {
const controller = new AbortController();
const reason = new Error('Expired before result delivery.');
create.mockImplementation((_body: unknown, options: { signal: AbortSignal }) => new Promise(resolve => {
options.signal.addEventListener('abort', () => resolve(response), { once: true });
}));
const pending = callJudge('Late response.', undefined, { signal: controller.signal });
controller.abort(reason);
await expect(pending).rejects.toBe(reason);
expect(create).toHaveBeenCalledTimes(1);
});
test('aborts during a 429 retry delay without making another request', async () => {
const controller = new AbortController();
const reason = new Error('Expired during rate-limit backoff.');
create.mockRejectedValue({ status: 429, headers: { 'retry-after': '30' } });
const pending = callJudge('Rate-limited request.', undefined, { signal: controller.signal });
// Let the rejected request enter its retry delay before canceling.
await new Promise<void>(resolve => setImmediate(resolve));
controller.abort(reason);
await expect(pending).rejects.toBe(reason);
expect(create).toHaveBeenCalledTimes(1);
}, 3000);
test('an abort during a 429 wait retains the wait it interrupted', async () => {
const diagnostics = spyOn(console, 'error').mockImplementation(() => {});
try {
const controller = new AbortController();
create.mockRejectedValue({ status: 429, headers: { 'retry-after': '30' } });
const pending = callJudge('Rate-limited request.', 'test-judge-model', { signal: controller.signal });
await new Promise<void>(resolve => setImmediate(resolve));
controller.abort(new Error('Expired during rate-limit backoff.'));
await expect(pending).rejects.toThrow('Expired during rate-limit backoff.');
expect(diagnostics).toHaveBeenCalledTimes(1);
expect(JSON.parse(diagnostics.mock.calls[0]![0])).toMatchObject({ type: 'llm-judge-aborted', model: 'test-judge-model',
stream: false, attempts: 1, httpRequests: 0, retryWaitsMs: [30000], firstEventMs: null, reason: 'Expired during rate-limit backoff.' });
create.mockResolvedValue(response as any);
diagnostics.mockClear();
expect(await callJudge('Judge this.', undefined, { signal: new AbortController().signal })).toEqual({ passed: true });
expect(diagnostics).not.toHaveBeenCalled();
} finally { diagnostics.mockRestore(); }
}, 3000);
test('keeps three 429 retries for callers without a signal', async () => {
const rateLimit = { status: 429, headers: { 'retry-after': '0.001' } };
create.mockRejectedValue(rateLimit);
await expect(callJudge('Retry as before.')).rejects.toBe(rateLimit);
expect(create).toHaveBeenCalledTimes(4);
});
test('still returns a successful retried result and does not retry other errors', async () => {
create.mockRejectedValueOnce({ status: 429, headers: { 'retry-after': '0.001' } });
expect(await callJudge('Transient rate limit.')).toEqual({ passed: true });
expect(create).toHaveBeenCalledTimes(2);
create.mockClear();
const failure = new Error('Provider request failed.');
create.mockRejectedValue(failure);
await expect(callJudge('Non-rate-limit failure.')).rejects.toBe(failure);
expect(create).toHaveBeenCalledTimes(1);
});
});