Skip to content

Commit 3e3e2b3

Browse files
committed
fix(guardrails): forward the caller's abort signal to hallucination scoring
1 parent 6594289 commit 3e3e2b3

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

apps/sim/app/api/guardrails/validate/route.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
284284
piiLanguage,
285285
piiCustomPatterns,
286286
authHeaders,
287-
requestId
287+
requestId,
288+
request.signal
288289
)
289290

290291
/**
@@ -397,7 +398,8 @@ async function executeValidation(
397398
piiLanguage: string | undefined,
398399
piiCustomPatterns: CustomPiiPattern[] | undefined,
399400
authHeaders: { cookie?: string; authorization?: string; billingAttribution?: string } | undefined,
400-
requestId: string
401+
requestId: string,
402+
abortSignal: AbortSignal | undefined
401403
): Promise<{
402404
passed: boolean
403405
error?: string
@@ -446,6 +448,7 @@ async function executeValidation(
446448
workspaceId,
447449
authHeaders,
448450
requestId,
451+
abortSignal,
449452
})
450453
}
451454
if (validationType === 'pii') {

apps/sim/lib/guardrails/validate_hallucination.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export interface HallucinationValidationInput {
4444
billingAttribution?: string
4545
}
4646
requestId: string
47+
/**
48+
* The caller's cancellation signal, forwarded to the scoring model exactly as the
49+
* agent handler forwards `ctx.abortSignal`. Without it the scoring request outlives
50+
* a cancelled request and keeps burning a provider slot until the transport gives up.
51+
*/
52+
abortSignal?: AbortSignal
4753
}
4854

4955
/**
@@ -113,7 +119,8 @@ async function scoreHallucinationWithLLM(
113119
apiKey: string | undefined,
114120
providerCredentials: HallucinationValidationInput['providerCredentials'],
115121
workspaceId: string | undefined,
116-
requestId: string
122+
requestId: string,
123+
abortSignal: AbortSignal | undefined
117124
): Promise<{ score: number; reasoning: string; cost: number }> {
118125
try {
119126
const contextText = ragContext.join('\n\n---\n\n')
@@ -186,6 +193,7 @@ Evaluate the consistency and provide your score and reasoning in JSON format.`
186193
bedrockSecretKey: providerCredentials?.bedrockSecretKey,
187194
bedrockRegion: providerCredentials?.bedrockRegion,
188195
workspaceId,
196+
abortSignal,
189197
})
190198

191199
if (response instanceof ReadableStream || ('stream' in response && 'execution' in response)) {
@@ -248,6 +256,7 @@ export async function validateHallucination(
248256
workspaceId,
249257
authHeaders,
250258
requestId,
259+
abortSignal,
251260
} = input
252261

253262
try {
@@ -290,7 +299,8 @@ export async function validateHallucination(
290299
apiKey,
291300
providerCredentials,
292301
workspaceId,
293-
requestId
302+
requestId,
303+
abortSignal
294304
)
295305

296306
logger.info(`[${requestId}] Confidence score: ${score}`, {

0 commit comments

Comments
 (0)