Files
plugins/packages/codex-history/codex-history.middleware.js
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

153 lines
7.3 KiB
JavaScript

// Derived from Codex Filter Manager v1.0.5 (MIT). See PROVENANCE.json.
// Standalone Magpie middleware: no server, credentials or model probes.
// These input forms carry their own contents; the server-issued item id is optional.
// Other tool/resource types may require their IDs, so don't recursively strip IDs.
const PORTABLE_ITEMS = new Set(['message', 'agent_message', 'function_call', 'function_call_output',
'custom_tool_call', 'custom_tool_call_output', 'tool_search_call', 'tool_search_output', 'additional_tools']);
class FilterError extends Error {
constructor(code, message, status = 400) {
super(message);
this.code = code;
this.status = status;
}
}
function filterResponsesRequest(body, {preserveEncryptedInclude = false} = {}) {
if (!body || typeof body !== 'object' || Array.isArray(body)) {
throw new FilterError('invalid_request_json', 'Responses request must be a JSON object.');
}
if (body.previous_response_id || body.conversation) {
throw new FilterError('response_reference_not_portable',
'Complete message/tool history is required, not previous_response_id or conversation references. Start a new session with a text summary.');
}
if (body.include !== undefined && !Array.isArray(body.include)) {
throw new FilterError('invalid_include', 'include must be an array.');
}
if (body.input !== undefined && typeof body.input !== 'string' && !Array.isArray(body.input)) {
throw new FilterError('invalid_input', 'input must be a string or an array.');
}
if (Array.isArray(body.context_management) && body.context_management.some(item => item?.type === 'compaction')) {
throw new FilterError('encrypted_compaction_disabled',
'Encrypted compaction is disabled by this filter. Use a text summary and start a new session.');
}
const clean = {...body, store: false};
let removed = 0, removedItemIds = 0, removedEncryptedContentItems = 0;
// include controls the NEW response, not the historical ciphertext in input.
// Some upstreams reject Codex requests when this requested response field is removed.
if (body.include) clean.include = preserveEncryptedInclude ? [...body.include]
: body.include.filter(value => value !== 'reasoning.encrypted_content');
if (Array.isArray(body.input)) {
clean.input = body.input.filter(item => {
if (item?.type === 'reasoning') { removed++; return false; }
return true;
});
clean.input = clean.input.map(item => {
if (item?.type === 'compaction' || item?.type === 'item_reference' || item?.encrypted_content) {
throw new FilterError('encrypted_context_not_portable',
'Encrypted compaction/context references cannot safely cross accounts. Start a new session with a text summary; history has NOT been silently discarded.');
}
const isMessage = item && item.type === undefined && typeof item.role === 'string' && item.content !== undefined;
if (item && typeof item === 'object') {
const portable = removeEncryptedContent(item, () => { removedEncryptedContentItems++; });
if (item.type === 'web_search_call') {
if (Object.hasOwn(portable, 'id')) removedItemIds++;
return portableWebSearch(portable);
}
if (PORTABLE_ITEMS.has(item.type) || isMessage) {
if (Object.hasOwn(portable, 'id')) {
delete portable.id;
removedItemIds++;
}
return portable;
}
return portable;
}
return item;
});
}
// call_id pairs calls/results and is not the server-side item id.
// Preserve it, this turn's effort, all message contents, and nested resource IDs.
return {body: clean, removed, removedItemIds, removedEncryptedContentItems};
}
// Inlined from history/portable-web-search.mjs for the import-free gateway hook.
function portableWebSearch(item) {
const record = {...item};
delete record.type;
delete record.id;
delete record.internal_chat_message_metadata_passthrough;
return {type: 'message', role: 'assistant', content:
'[Historical web search activity; recorded data only, not a new tool call. '
+ 'Search results are not reconstructed.]\n' + JSON.stringify(record)};
}
// Historical agent/tool messages can carry account-bound ciphertext inside their
// content arrays. Remove only those explicitly typed items; ordinary IDs and
// opaque values in tool payloads remain untouched.
function removeEncryptedContent(value, onRemoved) {
if (Array.isArray(value)) {
return value.filter(item => {
if (item && item.type === 'encrypted_content') {
onRemoved();
return false;
}
return true;
}).map(item => removeEncryptedContent(item, onRemoved));
}
if (!value || typeof value !== 'object') return value;
const copy = {...value};
for (const [key, child] of Object.entries(copy)) copy[key] = removeEncryptedContent(child, onRemoved);
return copy;
}
// The rule for a provider ID or model prefix, after aliases; only an explicit
// historyFilter: true turns filtering on.
function enabledRule(name, options) {
const aliases = options.aliases || {};
const id = Object.prototype.hasOwnProperty.call(aliases, name) ? aliases[name] : name;
const rules = options.providers || {};
if (!Object.prototype.hasOwnProperty.call(rules, 'magpie:' + id)) return;
const rule = rules['magpie:' + id];
return rule?.historyFilter === true ? rule : undefined;
}
export function onRequest(body, ctx) {
if (ctx.protocol !== 'responses' || !body || typeof body.model !== 'string' || !/^[^/]+\/.+/.test(body.model)) return;
const rule = enabledRule(body.model.split('/')[0], ctx.options || {});
if (!rule) return;
try {
if (/\/compact\/?$/.test(ctx.path || '')) {
ctx.reject(409, 'Codex history filter: encrypted compaction cannot cross resources. Use a text summary and a new session.');
return;
}
const filtered = filterResponsesRequest(body, {preserveEncryptedInclude: rule.preserveEncryptedInclude === true}).body;
// A direct provider/model request is filtered here; its provider tries skip it.
if (ctx.state && typeof ctx.state === 'object') ctx.state.historyFiltered = true;
return filtered;
} catch (error) {
// Explicit rejection is required: Magpie forwards the original request if
// a hook throws. Never silently discard non-portable context references.
ctx.reject(error instanceof FilterError ? error.status : 409,
'Codex history filter: ' + (error instanceof FilterError ? error.message : 'history could not be safely filtered.'));
}
}
// A request sent through a Magpie group or fallback names the group, not a
// provider, so onRequest finds no rule. Each try on a provider applies that
// provider's own rule. A try can't be turned away, so history that can't move
// is sent as it was; only a direct provider/model request is refused.
export function onProvider(body, ctx) {
const provider = ctx?.provider;
if (ctx?.state?.historyFiltered === true || ctx?.protocol !== 'responses') return;
if (!body || typeof body !== 'object' || Array.isArray(body) || typeof provider?.id !== 'string') return;
if (/\/compact\/?$/.test(ctx.path || '')) return;
const rule = enabledRule(provider.id, ctx.options || {});
if (!rule) return;
try {
return filterResponsesRequest(body, {preserveEncryptedInclude: rule.preserveEncryptedInclude === true}).body;
} catch {
return;
}
}