feat: multi-turn conversation support (0.4.0)

anonymizeTurn(text, session) threads a serializable AnonymizerSession
({mapping, legend, history}) so one entity keeps one id across a whole chat
(applyKnown reuse + usedIds + de-collision merge). Adds conversation()
in-memory wrapper and an optional LlmProvider.anonymizeInConversation(text, ctx)
for rich cross-turn context; providers without it fall back to the batch path.

openAICompatibleProvider gains includeMappingInContext (default false — only
send real values to a trusted anonymizer endpoint) + historyMaxTurns (default
10). Verified live vs Gemma 4: Nora stays PER_1 across 4 turns (name/email/AVS/
IBAN), Yanis = PER_2; 41 tests, 98% coverage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Mobiletic
2026-07-01 14:26:41 +01:00
parent bc34d9470e
commit 5ed8001101
9 changed files with 415 additions and 2 deletions

View File

@@ -1,5 +1,11 @@
import { describe, it, expect, vi } from 'vitest';
import { Anonymizer, AnonymizationError, presets, type LlmProvider } from '../src/index.js';
import {
Anonymizer,
AnonymizationError,
presets,
type LlmProvider,
type AnonymizerSession,
} from '../src/index.js';
/** An LLM provider that is configured but always fails → forces the regex fallback. */
const failingLlm = (overrides: Partial<LlmProvider> = {}): LlmProvider => ({
@@ -179,3 +185,113 @@ describe('Anonymizer (Swiss preset)', () => {
});
});
});
describe('multi-turn conversation (anonymizeTurn)', () => {
it('keeps stable ids across turns and accumulates the session', async () => {
const anonymizeInConversation = vi
.fn()
.mockResolvedValueOnce({
anon: 'Je suis [PER_1.NOM:F]',
mapping: { '[PER_1.NOM:F]': 'Nora Steiner' },
legend: { PER: 'Personne', NOM: 'Nom de famille', F: 'Féminin' },
})
.mockResolvedValueOnce({
anon: 'Ma collègue [PER_2.NOM:F] a aussi un souci',
mapping: { '[PER_2.NOM:F]': 'Yanis Berger' },
legend: { PER: 'Personne', NOM: 'Nom de famille', F: 'Féminin' },
});
const llm = failingLlm({ anonymizeInConversation });
const a = new Anonymizer({ llm });
const t1 = await a.anonymizeTurn('Je suis Nora Steiner');
expect(t1.mapping).toEqual({ '[PER_1.NOM:F]': 'Nora Steiner' });
const t2 = await a.anonymizeTurn('Ma collègue Yanis Berger a aussi un souci', t1.session);
expect(t2.mapping['[PER_1.NOM:F]']).toBe('Nora Steiner'); // stable across turns
expect(t2.mapping['[PER_2.NOM:F]']).toBe('Yanis Berger'); // new person, no collision
expect(t2.session.history).toHaveLength(2);
// the second call was told PER_1 is already used
expect(anonymizeInConversation.mock.calls[1][1].usedIds).toContain('[PER_1.NOM:F]');
});
it('conversation() wrapper threads the session and de-anonymizes', async () => {
const anonymizeInConversation = vi
.fn()
.mockResolvedValueOnce({
anon: '[PER_1.PRENOM:M]',
mapping: { '[PER_1.PRENOM:M]': 'Idris' },
legend: {},
})
.mockResolvedValueOnce({
anon: '[PER_1.PRENOM:M] et [PER_2.PRENOM:F]',
mapping: { '[PER_2.PRENOM:F]': 'Lina' },
legend: {},
});
const conv = new Anonymizer({ llm: failingLlm({ anonymizeInConversation }) }).conversation();
await conv.anonymize('Idris');
await conv.anonymize('Idris et Lina');
expect(conv.session().mapping).toEqual({ '[PER_1.PRENOM:M]': 'Idris', '[PER_2.PRENOM:F]': 'Lina' });
expect(conv.deanonymize('[PER_1.PRENOM:M]')).toBe('Idris');
});
it('falls back to the batch path when the provider has no anonymizeInConversation', async () => {
const anonymizeBatch = vi.fn().mockResolvedValue({
segments: ['[PER_1.NOM:M]'],
mapping: { '[PER_1.NOM:M]': 'Bruno Keller' },
legend: {},
});
const a = new Anonymizer({ llm: failingLlm({ anonymizeBatch }) });
const t = await a.anonymizeTurn('Bruno Keller');
expect(anonymizeBatch).toHaveBeenCalledWith(['Bruno Keller'], []);
expect(t.mapping['[PER_1.NOM:M]']).toBe('Bruno Keller');
});
it('realistic e-learning chat: stable coreference + attribute attribution + round-trips', async () => {
const turns = [
{
text: "Bonjour, je suis Nora Steiner, inscrite à la formation « Assistante médicale ». Je n'ai pas reçu ma convocation.",
anon: "Bonjour, je suis [PER_1.PRENOM:F] [PER_1.NOM:F], inscrite à la formation « Assistante médicale ». Je n'ai pas reçu ma convocation.",
mapping: { '[PER_1.PRENOM:F]': 'Nora', '[PER_1.NOM:F]': 'Steiner' },
},
{
text: 'Mon e-mail est nora.steiner@hotmail.ch et mon numéro AVS 756.2233.4455.66 au cas où.',
anon: 'Mon e-mail est [PER_1.EMAIL:F] et mon numéro AVS [PER_1.AVS:F] au cas où.',
mapping: { '[PER_1.EMAIL:F]': 'nora.steiner@hotmail.ch', '[PER_1.AVS:F]': '756.2233.4455.66' },
},
{
text: "En fait c'est aussi pour ma collègue Yanis Berger — elle veut s'inscrire, son tél. 078 111 22 33.",
anon: "En fait c'est aussi pour ma collègue [PER_2.PRENOM:F] [PER_2.NOM:F] — elle veut s'inscrire, son tél. [PER_2.TELEPHONE:F].",
mapping: {
'[PER_2.PRENOM:F]': 'Yanis',
'[PER_2.NOM:F]': 'Berger',
'[PER_2.TELEPHONE:F]': '078 111 22 33',
},
},
{
text: 'Le paiement se fera depuis mon IBAN CH88 0900 0000 1234 5678 9. Merci !',
anon: 'Le paiement se fera depuis mon IBAN [PER_1.IBAN:F]. Merci !',
mapping: { '[PER_1.IBAN:F]': 'CH88 0900 0000 1234 5678 9' },
},
];
const anonymizeInConversation = vi.fn();
for (const t of turns) {
anonymizeInConversation.mockResolvedValueOnce({ anon: t.anon, mapping: t.mapping, legend: {} });
}
const a = new Anonymizer({ llm: failingLlm({ anonymizeInConversation }) });
let session: AnonymizerSession = { mapping: {}, legend: {}, history: [] };
for (const t of turns) {
const r = await a.anonymizeTurn(t.text, session);
session = r.session;
// each turn restores to the exact original
expect(a.deanonymize(r.anon, r.mapping)).toBe(t.text);
}
// Nora = PER_1 throughout (name + email + AVS + IBAN attached to her); Yanis = PER_2.
expect(session.mapping['[PER_1.PRENOM:F]']).toBe('Nora');
expect(session.mapping['[PER_1.EMAIL:F]']).toBe('nora.steiner@hotmail.ch');
expect(session.mapping['[PER_1.IBAN:F]']).toBe('CH88 0900 0000 1234 5678 9');
expect(session.mapping['[PER_2.PRENOM:F]']).toBe('Yanis');
expect(session.mapping['[PER_2.TELEPHONE:F]']).toBe('078 111 22 33');
expect(session.history).toHaveLength(4);
});
});