Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
fix(ts-sdk): merge overlapping PII spans before redacting (#1427) (#1429)
Tai Ancb3e2a155ef3
diff --git a/sdk/typescript/src/guard/__tests__/pii.test.ts b/sdk/typescript/src/guard/__tests__/pii.test.tsindex b26121a9..93880ec6 100644--- a/sdk/typescript/src/guard/__tests__/pii.test.ts+++ b/sdk/typescript/src/guard/__tests__/pii.test.ts@@ -123,4 +123,30 @@ describe('PII Guard', () => {expect(result.action).toBe(GuardAction.WARN);expect(result.transformedText).toBeNull();});+ it('redacts the whole value when two patterns overlap', () => {+ // The env-secret span covers the email span; replacing the inner one first used+ // to resize the string and leave the tail of the value behind.+ const result = guard.evaluate('password: [email protected]:hunter2SuperSecret');+ expect(result.transformedText).toBe('[REDACTED:env-secret]');+ });++ it('redacts the whole value when an ipv4 match nests inside a secret', () => {+ const result = guard.evaluate('secret: 1.2.3.4-prod-fallback-value');+ expect(result.transformedText).toBe('[REDACTED:env-secret]');+ });++ it('does not emit an orphan bracket when spans overlap partially', () => {+ const result = guard.evaluate('my password: [email protected] then continue');+ expect(result.transformedText).toBe('my [REDACTED:env-secret] then continue');+ });++ it('keeps the more specific label when two patterns match the same span', () => {+ const result = guard.evaluate('Key: sk-ant-abcdefghij1234567890abcde');+ expect(result.transformedText).toBe('Key: [REDACTED:anthropic-api-key]');+ });++ it('still redacts disjoint matches separately', () => {+ const result = guard.evaluate('contact [email protected] or call 415-555-0100');+ expect(result.transformedText).toBe('contact [REDACTED:email] or call [REDACTED:phone-us]');+ });});diff --git a/sdk/typescript/src/guard/pii.ts b/sdk/typescript/src/guard/pii.tsindex 61b7a5d8..cd9e3793 100644--- a/sdk/typescript/src/guard/pii.ts+++ b/sdk/typescript/src/guard/pii.ts@@ -90,9 +90,26 @@ export class PII extends Guard {let transformedText: string | null = null;if (this._action === GuardAction.REDACT) {- const sortedMatches = [...matches].sort((a, b) => b.start - a.start);+ // Patterns can match overlapping spans of the same text. Every span's offsets+ // refer to the original string, so replacing them one at a time lets an earlier+ // replacement resize the string out from under a later one -- the later slice+ // then stops short and leaves the tail of the value in the output. Merge+ // overlapping spans first so each replacement covers a region no other touches.+ const ordered = matches+ .map((match, index) => ({ ...match, index }))+ .sort((a, b) => a.start - b.start || b.end - a.end || b.index - a.index);+ const spans: Array<{ label: string; start: number; end: number }> = [];+ for (const { label, start, end } of ordered) {+ const previous = spans[spans.length - 1];+ if (previous && start < previous.end) {+ previous.end = Math.max(previous.end, end);+ } else {+ spans.push({ label, start, end });+ }+ }let resultText = text;- for (const { label, start, end } of sortedMatches) {+ for (let i = spans.length - 1; i >= 0; i--) {+ const { label, start, end } = spans[i];resultText = resultText.slice(0, start) + `[REDACTED:${label}]` + resultText.slice(end);}transformedText = resultText;