Files
unisong c590a74057 codex-history 0.1.2: a group or fallback try is filtered with the tried provider's rule (#525)
A request through a Magpie group or fallback names the group, so 0.1.1's
onRequest found no provider rule and sent old reasoning, ciphertext and item
IDs to members whose filter was on. onProvider now filters each provider try
with the tried provider's rule (`magpie:<ctx.provider.id>`, through aliases).
A direct provider/model request filtered in onRequest is marked in
ctx.state.historyFiltered and its tries pass it on. A try with no rule, a rule
off, another protocol, the compact path or history that can't move is
returned unchanged (onProvider can't reject). cases.json gains 10 synthetic
provider-try cases in the shared scripts/middleware.mjs format (no real data).

Run at the reviewed head c50f542 (on mote):
- `HOME=$(mktemp -d) bun test`: 3776 pass, 8 skip, 0 fail.
- `bun scripts/check.mjs`: exit 0.
- PR's tests against the 0.1.1 middleware: 8 fail (the group/fallback cases
  and the export check), 34 pass.
- Probes in magpie's own engine (moejs, origin/main 3bce283df,
  internal/middleware Run.Request + Run.Tried, not committed): a group with a
  filtered, an unfiltered, a rule-off member, a retry on the filtered one and
  a member with another include rule: each try filtered by its own rule, the
  retry byte-identical to the first, unfiltered tries byte-identical to the
  request. A direct request: filtered once in onRequest, its try, a retry and
  a fallback to another provider sent what onRequest left. ctx.state doesn't
  reach the next request. Red on 0.1.1 (group), red with the
  historyFiltered guard removed (fallback re-filtered) and red with
  onProvider setting the flag (retry unfiltered).
- PROVENANCE: the shipped files' sha256 match unisong/magpie-codex-history-filter
  at 43e0b5c, which is on its main.

Not tried: a real Magpie with real providers; a group whose members speak
different protocols.
2026-10-30 18:46:55 +08:00

49 lines
2.8 KiB
JavaScript

import {test} from 'node:test';
import assert from 'node:assert/strict';
import * as middleware from './codex-history.middleware.js';
const options = {providers: {'magpie:demo': {historyFilter: true, preserveEncryptedInclude: true}}, aliases: {}};
const history = model => ({model, include: ['reasoning.encrypted_content'], input: [
{type: 'reasoning', id: 'rs_old', encrypted_content: 'FOREIGN', summary: []},
{type: 'message', id: 'msg_old', role: 'assistant', content: [{type: 'output_text', text: 'KEEP'}]},
{type: 'function_call', id: 'fc_old', call_id: 'pair', name: 'shell', arguments: '{}'},
{type: 'function_call_output', id: 'fco_old', call_id: 'pair', output: 'KEEP_RESULT'},
{role: 'user', content: 'next'},
]});
const context = (extra = {}) => ({protocol: 'responses', path: '/v1/responses', options, state: {},
reject: () => assert.fail('a provider try cannot refuse a request'), ...extra});
test('a group try is sent what a direct request to that provider is', () => {
const group = history('group/sol-dedicated');
const before = structuredClone(group);
assert.equal(middleware.onRequest(group, context()), undefined, 'a group prefix has no rule of its own');
const direct = middleware.onRequest(history('demo/gpt-6-astra'), context());
const sent = middleware.onProvider(group, context({provider: {id: 'demo', name: 'demo', model: 'gpt-6-astra'}}));
assert.deepEqual(sent, {...direct, model: group.model});
assert.deepEqual(group, before, 'the hook does not change the body it is given');
});
test('a request filtered by its own prefix is not filtered again on its try', () => {
const ctx = context();
const filtered = middleware.onRequest(history('demo/gpt-6-astra'), ctx);
assert.equal(ctx.state.historyFiltered, true);
const provider = {id: 'demo', name: 'demo', model: 'gpt-6-astra'};
assert.equal(middleware.onProvider(filtered, {...ctx, provider}), undefined);
assert.deepEqual(middleware.onProvider(structuredClone(filtered), context({provider})), filtered,
'filtering the filtered body again changes nothing');
});
test('a try that has no rule, is off, or carries history that cannot move stays byte-identical', () => {
for (const [label, request, extra] of [
['no rule', history('group/sol-dedicated'), {provider: {id: 'other', model: 'gpt-6-astra'}}],
['rule off', history('group/sol-dedicated'), {provider: {id: 'demo', model: 'gpt-6-astra'},
options: {providers: {'magpie:demo': {historyFilter: false}}}}],
['cannot move', {...history('group/sol-dedicated'), previous_response_id: 'resp_old'},
{provider: {id: 'demo', model: 'gpt-6-astra'}}],
]) {
const bytes = JSON.stringify(request);
assert.equal(middleware.onProvider(request, context(extra)), undefined, label);
assert.equal(JSON.stringify(request), bytes, label);
}
});