Skip to content

Bug: fixing updating topic relevance validator#144

Merged
nishika26 merged 8 commits intomainfrom
bug/update_validator
May 5, 2026
Merged

Bug: fixing updating topic relevance validator#144
nishika26 merged 8 commits intomainfrom
bug/update_validator

Conversation

@nishika26
Copy link
Copy Markdown
Contributor

@nishika26 nishika26 commented May 4, 2026

Target Issue is #128

Notes

  • Bug Fixes

    • Improved guardrail configuration save and update behavior with better error handling and state management.
    • The PATCH request body now uses buildValidatorUpdatePayload(configValues) to send only the permitted fields.
  • Refactor

    • Enhanced validator configuration update logic for more reliable saved configuration refreshing and selection handling.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed validator filtering to properly exclude unsupported validator types.
    • Improved saved configuration loading with more reliable error handling.
  • Improvements

    • Configurations now refresh automatically after being saved or updated, ensuring the latest state is displayed.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 4, 2026

Warning

Rate limit exceeded

@Ayush8923 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 28 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1c48ac60-bdd2-436d-9c7c-c055e847be71

📥 Commits

Reviewing files that changed from the base of the PR and between e385d79 and 75793e2.

📒 Files selected for processing (2)
  • app/lib/types/guardrails.ts
  • app/lib/utils/guardrails.ts
📝 Walkthrough

Walkthrough

Refactors saved-configs loading to an async useCallback that normalizes multiple response shapes and loading state; adds ValidatorUpdatePayload type and buildValidatorUpdatePayload() helper; PATCH saves use the new payload builder; post-save the saved-configs list is refreshed and selection updated or cleared; filters out "llm_critic" validators when loading.

Changes

Validator Configuration Payload & Save Flow

Layer / File(s) Summary
Data Shape
app/lib/types/guardrails.ts
Added export interface ValidatorUpdatePayload { name?: string; type?: string; stage?: string; on_fail_action?: string; is_enabled?: boolean }.
Payload Builder
app/lib/utils/guardrails.ts
Added export function buildValidatorUpdatePayload(configValues: Record<string, unknown>): ValidatorUpdatePayload which picks name, type, stage, on_fail_action, is_enabled and returns them with explicit casts.
Async Fetching & Filtering
app/(main)/guardrails/page.tsx
Converted fetchSavedConfigs to an async useCallback returning Promise<SavedValidatorConfig[]>; normalizes response shapes (data.data?.configs / data.configs / data), sets savedConfigsLoading with try/catch/finally, returns [] on missing query string or errors, and filters out validators with type === "llm_critic".
Save Integration & Refresh
app/(main)/guardrails/page.tsx
handleSaveConfig uses buildValidatorUpdatePayload(configValues) for PATCH requests (creates still send full configValues); after successful save/update awaits fetchSavedConfigs() and updates selectedSavedConfig to the refreshed matching config on update or clears it on create.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

ready-for-review

Suggested reviewers

  • AkhileshNegi

"I nibbled at a payload bright,
Async hops through day and night,
Types aligned and fetches sing,
Updated configs on soft spring,
A rabbit cheers the cleaner spring!" 🐇✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title describes a specific bug fix (updating topic relevance validator) but the actual changes are broader—refactoring saved-config loading logic, adding a validator update payload builder, and improving state management across multiple files. Consider a more accurate title such as 'Fix validator config save/update behavior and payload handling' or 'Refactor guardrail config updates with proper state management' to reflect the broader scope of changes.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bug/update_validator

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nishika26 nishika26 self-assigned this May 4, 2026
@nishika26 nishika26 added the bug Something isn't working label May 4, 2026
@nishika26
Copy link
Copy Markdown
Contributor Author

nishika26 commented May 5, 2026

@coderabbitai review this pr and add pr description

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Tip

For best results, initiate chat on the files or code changes.

@nishika26 It looks like your message only contains a mention. How can I help you? Feel free to ask me to review the PR, answer questions about the code, or anything else! 🐇

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get PR details
gh pr view 144 --repo ProjectTech4DevAI/kaapi-frontend --json title,body,files,additions,deletions,commits

echo "---DIFF---"
gh pr diff 144 --repo ProjectTech4DevAI/kaapi-frontend

Length of output: 6293


@nishika26 Here's an updated PR description I'm adding now, and I'll also kick off a full review!


Proposed PR Description:


Bug: Fixing Updating Topic Relevance Validator

Fixes #128

Problem

Two bugs were affecting the update flow for saved validator configurations:

  1. Incorrect PATCH payload — The full configValues object was being sent in the PATCH request body, which could include extra or read-only fields not accepted by the API for updates.
  2. Selection cleared after update — After a successful update, setSelectedSavedConfig(null) was always called, losing the selected config context and forcing the user to re-select it manually.

Changes

app/lib/types/guardrails.ts

  • Added a ValidatorUpdatePayload interface that defines the allowed fields for a PATCH request (name, type, stage, on_fail_action, is_enabled).
  • Added a buildValidatorUpdatePayload() helper function to extract only those fields from the full config values object.

app/(main)/guardrails/page.tsx

  • Refactored fetchSavedConfigs from a fire-and-forget callback to an async function returning Promise<SavedValidatorConfig[]>, enabling callers to await the fresh list.
  • On a successful update (PATCH): the saved config ID is captured before the request, the fresh config list is awaited, and the matching config is re-selected — preserving the user's context.
  • On a successful create (POST): selection is cleared as before.
  • The PATCH request body now uses buildValidatorUpdatePayload(configValues) to send only the permitted fields.

Testing

  • Update an existing validator config and verify the config remains selected with the latest values after saving.
  • Create a new validator config and verify the selection is cleared after saving.

✅ Actions performed

Summary regeneration triggered.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
app/(main)/guardrails/page.tsx (1)

78-106: 💤 Low value

fetchSavedConfigs refactor looks good overall; ternary chain can be simplified.

The async/useCallback conversion and the try/finally guarding setSavedConfigsLoading are both correct improvements.

One small nit: the three-level ternary at lines 89–97 (with the redundant non-null assertion data.configs!) is hard to follow. A flat helper reduces cognitive load and removes the assertion:

♻️ Simplified extraction
-      const nested = data?.data;
-      const list: SavedValidatorConfig[] = Array.isArray(
-        (nested as { configs?: SavedValidatorConfig[] })?.configs,
-      )
-        ? (nested as { configs: SavedValidatorConfig[] }).configs
-        : Array.isArray(nested)
-          ? (nested as SavedValidatorConfig[])
-          : Array.isArray(data?.configs)
-            ? data.configs!
-            : [];
+      const nested = data?.data;
+      const list: SavedValidatorConfig[] =
+        Array.isArray((nested as { configs?: SavedValidatorConfig[] })?.configs)
+          ? (nested as { configs: SavedValidatorConfig[] }).configs
+          : Array.isArray(nested)
+            ? (nested as SavedValidatorConfig[])
+            : Array.isArray(data?.configs)
+              ? (data.configs as SavedValidatorConfig[])
+              : [];
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/`(main)/guardrails/page.tsx around lines 78 - 106, The ternary chain in
fetchSavedConfigs that extracts the configs from guardrailsFetch's response is
hard to read and uses a non-null assertion; replace it with a small clear
extraction helper: inspect data, data.data, and data.data.configs in order and
return the first found array (or []), then call setSavedConfigs(list) as before.
Update fetchSavedConfigs to use that helper to compute list (keeping useCallback
deps configsQueryString and apiKey) and remove the Array.isArray nesting and the
data.configs! assertion; keep the existing try/catch/finally behavior and the
setSavedConfigsLoading/setSavedConfigs calls.
app/lib/types/guardrails.ts (1)

66-72: ⚡ Quick win

Use concrete types instead of unknown for ValidatorUpdatePayload fields.

All five fields are typed as unknown, providing zero type safety at call sites. The correct types are already defined in SavedValidatorConfig in the same file.

♻️ Proposed fix
 export interface ValidatorUpdatePayload {
-  name?: unknown;
-  type?: unknown;
-  stage?: unknown;
-  on_fail_action?: unknown;
-  is_enabled?: unknown;
+  name?: string;
+  type?: string;
+  stage?: string;
+  on_fail_action?: string;
+  is_enabled?: boolean;
 }

As per coding guidelines, TypeScript should be used in strict mode for type safety across all source files.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/lib/types/guardrails.ts` around lines 66 - 72, ValidatorUpdatePayload
fields are all typed as unknown; replace each field's type with the
corresponding concrete types from SavedValidatorConfig (e.g., use
SavedValidatorConfig['name'], ['type'], ['stage'], ['on_fail_action'], and
['is_enabled'] or the explicit primitives/union types declared on
SavedValidatorConfig) so callers get proper type checking; update the
ValidatorUpdatePayload declaration to reference SavedValidatorConfig for each
property (or inline the exact types) while keeping them optional as currently
declared.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/lib/types/guardrails.ts`:
- Around line 74-79: buildValidatorUpdatePayload currently strips
validator-specific parameters; change it to preserve the full config by
returning the extracted fields plus a config property containing the remaining
config values (e.g., return { name, type, stage, on_fail_action, is_enabled,
config: configValues }); also update the ValidatorUpdatePayload type (or add an
optional config field) to accept that config object so PATCH requests carry the
same validator parameters as CREATE requests.

---

Nitpick comments:
In `@app/`(main)/guardrails/page.tsx:
- Around line 78-106: The ternary chain in fetchSavedConfigs that extracts the
configs from guardrailsFetch's response is hard to read and uses a non-null
assertion; replace it with a small clear extraction helper: inspect data,
data.data, and data.data.configs in order and return the first found array (or
[]), then call setSavedConfigs(list) as before. Update fetchSavedConfigs to use
that helper to compute list (keeping useCallback deps configsQueryString and
apiKey) and remove the Array.isArray nesting and the data.configs! assertion;
keep the existing try/catch/finally behavior and the
setSavedConfigsLoading/setSavedConfigs calls.

In `@app/lib/types/guardrails.ts`:
- Around line 66-72: ValidatorUpdatePayload fields are all typed as unknown;
replace each field's type with the corresponding concrete types from
SavedValidatorConfig (e.g., use SavedValidatorConfig['name'], ['type'],
['stage'], ['on_fail_action'], and ['is_enabled'] or the explicit
primitives/union types declared on SavedValidatorConfig) so callers get proper
type checking; update the ValidatorUpdatePayload declaration to reference
SavedValidatorConfig for each property (or inline the exact types) while keeping
them optional as currently declared.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ec838dd1-3fc7-4c23-a78e-b60de0a54ff0

📥 Commits

Reviewing files that changed from the base of the PR and between c61a2b8 and 096de0f.

📒 Files selected for processing (2)
  • app/(main)/guardrails/page.tsx
  • app/lib/types/guardrails.ts

Comment thread app/lib/types/guardrails.ts Outdated
@Ayush8923
Copy link
Copy Markdown
Collaborator

@nishika26 Is this PR ready for review? If yes, can you please add the description of the PR and also add the ready-for-review label?

@nishika26 nishika26 linked an issue May 5, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@Ayush8923 Ayush8923 left a comment

Choose a reason for hiding this comment

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

Added the few comments.

Comment thread app/lib/types/guardrails.ts Outdated
Comment thread app/lib/types/guardrails.ts Outdated
Comment on lines +74 to +79
export function buildValidatorUpdatePayload(
configValues: Record<string, unknown>,
): ValidatorUpdatePayload {
const { name, type, stage, on_fail_action, is_enabled } = configValues;
return { name, type, stage, on_fail_action, is_enabled };
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don’t think this function should be inside the types declaration. It’s simply picking objects from configValues, so it would be better placed in utils, or kept within the same file if it’s only used there.

This function doesn’t have any direct relation to types. The types defined here can be imported and used wherever needed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added it to utils instead

@Ayush8923 Ayush8923 added reviewed When the PR has been reviewed by reviewer.. and removed ready-for-review labels May 5, 2026
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
app/lib/utils/guardrails.ts (1)

52-58: ⚡ Quick win

Avoid unchecked as casts when building PATCH payload—use runtime type guards instead.

At lines 52–58, direct assertions (as string / as boolean) from destructured unknown values can include undefined or mistyped fields in the update payload. Since ValidatorUpdatePayload fields are optional, the current approach technically works but sends more data than necessary. Prefer runtime narrowing to only include valid typed fields.

Proposed safer payload builder
 export function buildValidatorUpdatePayload(
   configValues: Record<string, unknown>,
 ): ValidatorUpdatePayload {
   const { name, type, stage, on_fail_action, is_enabled } = configValues;
-  return {
-    name: name as string,
-    type: type as string,
-    stage: stage as string,
-    on_fail_action: on_fail_action as string,
-    is_enabled: is_enabled as boolean,
-  };
+  const payload: ValidatorUpdatePayload = {};
+  if (typeof name === "string") payload.name = name;
+  if (typeof type === "string") payload.type = type;
+  if (typeof stage === "string") payload.stage = stage;
+  if (typeof on_fail_action === "string")
+    payload.on_fail_action = on_fail_action;
+  if (typeof is_enabled === "boolean") payload.is_enabled = is_enabled;
+  return payload;
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/lib/utils/guardrails.ts` around lines 52 - 58, The current builder
destructures unknown configValues into name, type, stage, on_fail_action,
is_enabled and force-casts them with `as` which can send undefined/mistyped
fields; replace the `as` casts with runtime type checks (e.g., typeof checks and
explicit boolean checks) and only include each property in the returned
ValidatorUpdatePayload when the value passes the appropriate guard (e.g.,
include name/type/stage/on_fail_action if typeof value === "string", include
is_enabled if typeof value === "boolean"), so the resulting payload contains
only validated fields instead of blanket-casted values.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@app/lib/utils/guardrails.ts`:
- Around line 52-58: The current builder destructures unknown configValues into
name, type, stage, on_fail_action, is_enabled and force-casts them with `as`
which can send undefined/mistyped fields; replace the `as` casts with runtime
type checks (e.g., typeof checks and explicit boolean checks) and only include
each property in the returned ValidatorUpdatePayload when the value passes the
appropriate guard (e.g., include name/type/stage/on_fail_action if typeof value
=== "string", include is_enabled if typeof value === "boolean"), so the
resulting payload contains only validated fields instead of blanket-casted
values.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b0ab465c-2e1f-4f82-a9a1-be01907f7edd

📥 Commits

Reviewing files that changed from the base of the PR and between 096de0f and e385d79.

📒 Files selected for processing (3)
  • app/(main)/guardrails/page.tsx
  • app/lib/types/guardrails.ts
  • app/lib/utils/guardrails.ts
✅ Files skipped from review due to trivial changes (1)
  • app/lib/types/guardrails.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/(main)/guardrails/page.tsx

Copy link
Copy Markdown
Collaborator

@Ayush8923 Ayush8923 left a comment

Choose a reason for hiding this comment

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

Final review.

Comment thread app/lib/types/guardrails.ts Outdated
name?: string;
type?: string;
stage?: string;
on_fail_action?: string;
Copy link
Copy Markdown
Collaborator

@Ayush8923 Ayush8923 May 5, 2026

Choose a reason for hiding this comment

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

If this is a string, I think we should rename the variable from on_fail_action to something like fail_action_status etc. Right now, the on_ prefix suggests it represents a function or handler, so the naming should reflect that more clearly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

on fail action is the name we have kept everywhere from start, from the past many months we have been working on guardrails, are you suggesting changing it just here, while keeping the core name on fail action itself, or changing it everywhere?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Got it. I initially thought this was just a simple form value variable. If this is a small change with no breaking impact, you can go ahead and update it. Otherwise, you can add it to your TODOs to fix the variable naming in the future.

Comment thread app/lib/utils/guardrails.ts Outdated
Comment on lines +49 to +60
export function buildValidatorUpdatePayload(
configValues: Record<string, unknown>,
): ValidatorUpdatePayload {
const { name, type, stage, on_fail_action, is_enabled } = configValues;
return {
name: name as string,
type: type as string,
stage: stage as string,
on_fail_action: on_fail_action as string,
is_enabled: is_enabled as boolean,
};
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Move this function after the buildDefaultValues, because the JS comment above belongs to the buildDefaultValues function, please either move the comment below or update it accordingly.

Comment thread app/lib/utils/guardrails.ts Outdated
Co-authored-by: Ayush <80516839+Ayush8923@users.noreply.github.com>
Comment thread app/lib/types/guardrails.ts Outdated
Co-authored-by: Ayush <80516839+Ayush8923@users.noreply.github.com>
@nishika26 nishika26 merged commit cde9771 into main May 5, 2026
2 checks passed
@nishika26 nishika26 deleted the bug/update_validator branch May 5, 2026 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working reviewed When the PR has been reviewed by reviewer..

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guardrails: Fix update error for validator

2 participants