Skip to content

Conversation

roh26it
Copy link
Collaborator

@roh26it roh26it commented Aug 11, 2025

No description provided.

Copy link
Contributor

matter-code-review bot commented Aug 11, 2025

Code Quality bug fix refactoring

Description

Summary By MatterAI MatterAI logo

🔄 What Changed

This pull request refines the Azure PII detection logic and cleans up test output. Specifically, it adds a null/undefined check for the response.results.documents from the Azure PII API to ensure valid data, and precisely positions the verdict = true assignment within the redaction logic. Additionally, redundant console.log statements were removed from the Azure plugin test file.

🔍 Impact of the Change

  • Improved Robustness: Enhanced error handling for invalid responses from the Azure PII API prevents unexpected behavior.
  • Clearer PII Verdict Logic: The verdict flag is now accurately set to true only when PII is detected and successfully redacted, aligning with the transformed status.
  • Cleaner Test Output: Removal of console.log statements reduces noise in test runs.

📁 Total Files Changed

  • plugins/azure/azure.test.ts: Removed console.log statements.
  • plugins/azure/pii.ts: Added response validation and refined verdict assignment logic.

🧪 Test Added

No new tests were added in this pull request; however, existing unit tests in azure.test.ts continue to validate the PII handling logic, and the removal of console.log statements cleans up test execution output.

🔒 Security Vulnerabilities

N/A - No new security vulnerabilities detected. The changes improve the robustness of PII handling.

Motivation

The changes aim to improve the reliability and clarity of the Azure PII detection plugin by adding defensive programming for API responses and ensuring the verdict flag accurately reflects the outcome of PII redaction. Test output cleanup contributes to a better developer experience.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)

How Has This Been Tested?

  • Unit Tests
  • Integration Tests
  • Manual Testing

Screenshots (if applicable)

N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Related Issues

N/A

Tanka Poem ♫

Logs cleared, code refined,
PII logic, sharp and true.
Errors caught, data flows,
Science in each line, precise.
Systems hum, a quiet hum. 🔬✨

Sequence Diagram

sequenceDiagram
    participant PH as PluginHandler
    participant PII as PII Handler (pii.ts)
    participant AZURE_API as Azure PII API

    PH->>PII: handler(context, params, eventType)
    activate PII
    Note over PII: try-catch block for PII processing

    PII->>AZURE_API: redact(documents, parameters, pluginOptions)
    activate AZURE_API
    AZURE_API-->>PII: response (results.documents, redactedText)
    deactivate AZURE_API

    alt Invalid Azure API Response
        PII->>PII: Check !response?.results?.documents
        PII--xPH: Throws Error('Invalid response from Azure PII API')
    else Valid Response
        PII->>PII: data = response.results.documents
        PII->>PII: containsPII = data.some(doc => doc.entities.length > 0)

        alt PII Detected (containsPII is true)
            PII->>PII: verdict = false (initial PII detection)
            alt Redaction Enabled (parameters.redact is true)
                PII->>PII: verdict = true (after successful redaction)
                PII->>PII: transformed = true
                PII->>PII: setCurrentContentPart(...)
            end
        end
        PII-->>PH: result (error, verdict, transformed)
    end
    deactivate PII
Loading

Copy link
Contributor

@matter-code-review matter-code-review bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good implementation of flexible PII guardrail handling and agent management. Found a few areas for improvement around error handling and test cleanup.

Comment on lines 56 to 60
const result = await piiHandler(context, params, 'beforeRequestHook');
console.log('result', result);
expect(result.error).toBeNull();
expect(result.verdict).toBe(true);
expect(result.transformed).toBe(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Dead Code

Issue: Console.log statements left in test code
Fix: Remove debug console.log statements before production
Impact: Cleaner test output and better code hygiene

Suggested change
const result = await piiHandler(context, params, 'beforeRequestHook');
console.log('result', result);
expect(result.error).toBeNull();
expect(result.verdict).toBe(true);
expect(result.transformed).toBe(false);
const result = await piiHandler(context, params, 'beforeRequestHook');
expect(result.error).toBeNull();
expect(result.verdict).toBe(true);
expect(result.transformed).toBe(false);

Comment on lines 64 to 72
const result = await piiHandler(
mockContext,
{ ...params, redact: false },
'beforeRequestHook'
);
console.log('result', result);
expect(result.error).toBeNull();
expect(result.verdict).toBe(false);
expect(result.transformed).toBe(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Dead Code

Issue: Console.log statements left in test code
Fix: Remove debug console.log statements before production
Impact: Cleaner test output and better code hygiene

Suggested change
const result = await piiHandler(
mockContext,
{ ...params, redact: false },
'beforeRequestHook'
);
console.log('result', result);
expect(result.error).toBeNull();
expect(result.verdict).toBe(false);
expect(result.transformed).toBe(false);
const result = await piiHandler(
mockContext,
{ ...params, redact: false },
'beforeRequestHook'
);
expect(result.error).toBeNull();
expect(result.verdict).toBe(false);
expect(result.transformed).toBe(false);

Comment on lines 135 to 152
try {
const response = await redact(documents, parameters, options);
const response = await redact(documents, parameters, pluginOptions);
data = response.results.documents;
if (parameters.redact) {
const containsPII =
data.length > 0 && data.some((doc: any) => doc.entities.length > 0);
if (containsPII) {
verdict = false;
}
if (parameters.redact && containsPII) {
const redactedData = (response.results.documents ?? []).map(
(doc: any) => doc.redactedText
);
setCurrentContentPart(context, eventType, transformedData, redactedData);
transformed = true;
verdict = true;
}
} catch (e) {
error = e;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Code Refactor

Issue: Missing error handling for malformed Azure API responses
Fix: Add validation for response structure before accessing nested properties
Impact: Prevents runtime crashes when Azure API returns unexpected response format

Suggested change
try {
const response = await redact(documents, parameters, options);
const response = await redact(documents, parameters, pluginOptions);
data = response.results.documents;
if (parameters.redact) {
const containsPII =
data.length > 0 && data.some((doc: any) => doc.entities.length > 0);
if (containsPII) {
verdict = false;
}
if (parameters.redact && containsPII) {
const redactedData = (response.results.documents ?? []).map(
(doc: any) => doc.redactedText
);
setCurrentContentPart(context, eventType, transformedData, redactedData);
transformed = true;
verdict = true;
}
} catch (e) {
error = e;
try {
const response = await redact(documents, parameters, pluginOptions);
if (!response?.results?.documents) {
throw new Error('Invalid response format from Azure PII API');
}
data = response.results.documents;
const containsPII =
data.length > 0 && data.some((doc: any) => doc.entities?.length > 0);
if (containsPII) {
verdict = false;
}
if (parameters.redact && containsPII) {
const redactedData = (response.results.documents ?? []).map(
(doc: any) => doc.redactedText
);
setCurrentContentPart(context, eventType, transformedData, redactedData);
transformed = true;
verdict = true;
}
} catch (e) {
error = e;

narengogi
narengogi previously approved these changes Aug 12, 2025
Copy link
Contributor

Important

PR Review Skipped

PR review skipped as per the configuration setting. Run a manually review by commenting /matter review

💡Tips to use Matter AI

Command List

  • /matter summary: Generate AI Summary for the PR
  • /matter review: Generate AI Reviews for the latest commit in the PR
  • /matter review-full: Generate AI Reviews for the complete PR
  • /matter release-notes: Generate AI release-notes for the PR
  • /matter : Chat with your PR with Matter AI Agent
  • /matter remember : Generate AI memories for the PR
  • /matter explain: Get an explanation of the PR
  • /matter help: Show the list of available commands and documentation
  • Need help? Join our Discord server: https://discord.gg/fJU5DvanU3

@VisargD VisargD merged commit cd4ca6c into main Aug 13, 2025
1 check passed
@VisargD VisargD deleted the chore/update-azure-guardrails branch August 13, 2025 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants