Bind model results to immutable source and policy snapshots #421
@@ -49,7 +49,9 @@ export class Inf0GatewayClient {
|
||||
inputTokens: number;
|
||||
messages: readonly Message[];
|
||||
}): Promise<Inf0GatewayReceipt> {
|
||||
const { context, admission, policy, reservation, messages, inputTokens } = input;
|
||||
// Preserve the admitted scope, policy, reservation and messages until the
|
||||
// response is checked; callers must not change those limits in flight.
|
||||
const { context, admission, policy, reservation, messages, inputTokens } = structuredClone(input);
|
||||
const decision = admitModelExecution(admission, policy, reservation);
|
||||
if (!decision.allowed) throw new Error(`Model execution denied: ${decision.reason}`);
|
||||
if (context.tenantId !== admission.tenantId || context.projectId !== admission.projectId
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -43,4 +43,17 @@ describe('INF0 gateway adapter', () => {
|
||||
await expect(new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: failing }).complete(input)).rejects.toBeInstanceOf(Inf0OutcomeUnknownError);
|
||||
expect(failing).toHaveBeenCalledOnce();
|
||||
});
|
||||
it('checks the provider and usage against the policy fixed at dispatch time', async () => {
|
||||
const mutable = structuredClone(input);
|
||||
const fetchMock = vi.fn(async () => {
|
||||
mutable.policy.destination = 'external';
|
||||
mutable.admission.maxOutputTokens = 1000;
|
||||
return Response.json({ ...result, routeDecision: { route: 'local_only', providerKind: 'external' },
|
||||
cost: { promptTokens: 10, completionTokens: 100, totalTokens: 110 } });
|
||||
});
|
||||
const client = new Inf0GatewayClient({ baseUrl: 'https://inf0.example.test', serviceToken: 'test', fetchImpl: fetchMock });
|
||||
await expect(client.complete(mutable)).rejects.toBeInstanceOf(Inf0OutcomeUnknownError);
|
||||
expect(fetchMock).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -45,4 +45,33 @@ describe('structure document workflow', () => {
|
||||
await expect(structureDocument({ ...base, fields: [], gateway: { complete } })).rejects.toThrow('Invalid structure');
|
||||
expect(complete).not.toHaveBeenCalled();
|
||||
});
|
||||
it('validates against the exact source and field contract supplied before async tokenization', async () => {
|
||||
const sources = [{ id: 'page1', text: 'Name: Evil' }];
|
||||
const fields = [{ name: 'name', type: 'string' as const, required: true }];
|
||||
const countTokens = vi.fn(async () => { sources[0]!.text = 'Name: Acme'; fields[0]!.required = false; return 50; });
|
||||
const complete = vi.fn<Inf0GatewayClient['complete']>(async () => response);
|
||||
const result = await structureDocument({ ...base, sources, fields, countTokens, gateway: { complete } });
|
||||
expect(result).toMatchObject({ status: 'needs_review', issues: [{ field: 'name', code: 'evidence_mismatch' }] });
|
||||
expect(complete.mock.calls[0]?.[0]?.messages[1]?.content).toContain('Name: Evil');
|
||||
});
|
||||
it('uses the original admission and policy even if the caller mutates its request during tokenization', async () => {
|
||||
const mutableRequest = structuredClone(request);
|
||||
const countTokens = vi.fn(async () => { mutableRequest.admission.modelId = 'changed'; mutableRequest.policy.allowedModelIds.push('changed'); return 50; });
|
||||
const complete = vi.fn<Inf0GatewayClient['complete']>(async () => response);
|
||||
await structureDocument({ ...base, request: mutableRequest, countTokens, gateway: { complete } });
|
||||
expect(complete.mock.calls[0]?.[0]?.admission.modelId).toBe('local_only');
|
||||
expect(complete.mock.calls[0]?.[0]?.policy.allowedModelIds).toEqual(['local_only']);
|
||||
});
|
||||
|
||||
it('keeps tokenizer input identical to the message sent to the gateway', async () => {
|
||||
const countTokens = vi.fn(async (messages: Parameters<Inf0GatewayClient['complete']>[0]['messages']) => {
|
||||
expect(Object.isFrozen(messages)).toBe(true);
|
||||
expect(Object.isFrozen(messages[1])).toBe(true);
|
||||
return 50;
|
||||
});
|
||||
const complete = vi.fn<Inf0GatewayClient['complete']>(async () => response);
|
||||
await structureDocument({ ...base, countTokens, gateway: { complete } });
|
||||
expect(complete.mock.calls[0]?.[0]?.messages).toEqual(countTokens.mock.calls[0]?.[0]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user