@silmaril-security/sdk 0.11.1 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.11.2
4
+
5
+ - Keep protect() responsive to caller cancellation through operation and output handling.
6
+
3
7
  ## 0.11.1
4
8
 
5
9
  - Pin the Vercel AI SDK install recipes to compatible AI SDK and OpenAI
package/README.md CHANGED
@@ -35,10 +35,10 @@ This SDK is distributed as an npm package.
35
35
  npm install @silmaril-security/sdk
36
36
  ```
37
37
 
38
- Pin version 0.11.1 for reproducible installs:
38
+ Pin version 0.11.2 for reproducible installs:
39
39
 
40
40
  ```sh
41
- npm install @silmaril-security/sdk@0.11.1
41
+ npm install @silmaril-security/sdk@0.11.2
42
42
  ```
43
43
 
44
44
  Requires Node 20 or later.
@@ -591,7 +591,14 @@ classification that is still waiting.
591
591
 
592
592
  Aborting the `signal` you pass stops a classification that is still waiting
593
593
  for its turn, rejects `protect()` with the signal's reason, and aborts your
594
- operation's signal. Classifications queued after it keep their order.
594
+ operation's signal. This holds at any point before `protect()` settles: while
595
+ the operation is running after the input verdict, while the `output` check is
596
+ pending, and in Shadow and Warn. A signal that is already aborted rejects
597
+ `protect()` before your operation starts. A block that lands first still
598
+ rejects with `FirewallBlockedException`. Classifications queued after the
599
+ aborted one keep their order, and other `protect()` calls are unaffected. In
600
+ Shadow and Warn the input check already started keeps running in the
601
+ background after an abort, and no output check is started.
595
602
 
596
603
  A background classification retries once, never rejects, logs its failure with
597
604
  `console.warn`, and does not follow the caller's `signal`. One that would
@@ -13,7 +13,7 @@ import * as langchain from 'langchain';
13
13
  import { CreateAgentParams } from 'langchain';
14
14
  import * as _langchain_core_tools from '@langchain/core/tools';
15
15
  import { StructuredTool } from '@langchain/core/tools';
16
- import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BD4YdAmB.cjs';
16
+ import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-D8XAQ9gj.cjs';
17
17
 
18
18
  declare const SAFE_TOOL_MESSAGE = "Silmaril Firewall blocked this tool interaction. Choose a different safe action.";
19
19
  declare const SAFE_FINAL_MESSAGE = "Silmaril Firewall stopped this request after repeated unsafe actions.";
@@ -13,7 +13,7 @@ import * as langchain from 'langchain';
13
13
  import { CreateAgentParams } from 'langchain';
14
14
  import * as _langchain_core_tools from '@langchain/core/tools';
15
15
  import { StructuredTool } from '@langchain/core/tools';
16
- import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-BD4YdAmB.js';
16
+ import { h as FirewallMode, b as ClassifyEvent, e as Firewall } from '../firewall-D8XAQ9gj.js';
17
17
 
18
18
  declare const SAFE_TOOL_MESSAGE = "Silmaril Firewall blocked this tool interaction. Choose a different safe action.";
19
19
  declare const SAFE_FINAL_MESSAGE = "Silmaril Firewall stopped this request after repeated unsafe actions.";
@@ -1,6 +1,6 @@
1
1
  import { RunnableConfig, Runnable, RunnableBinding } from '@langchain/core/runnables';
2
2
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
3
- import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BD4YdAmB.cjs';
3
+ import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-D8XAQ9gj.cjs';
4
4
 
5
5
  declare function createLangChainHandler(firewall: Firewall, options?: LangChainAdapterOptions): Promise<BaseCallbackHandler>;
6
6
  /** Protect a runnable using its actual invocation config, including direct model/tool calls. */
@@ -1,6 +1,6 @@
1
1
  import { RunnableConfig, Runnable, RunnableBinding } from '@langchain/core/runnables';
2
2
  import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
3
- import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-BD4YdAmB.js';
3
+ import { e as Firewall, L as LangChainAdapterOptions } from '../firewall-D8XAQ9gj.js';
4
4
 
5
5
  declare function createLangChainHandler(firewall: Firewall, options?: LangChainAdapterOptions): Promise<BaseCallbackHandler>;
6
6
  /** Protect a runnable using its actual invocation config, including direct model/tool calls. */
@@ -1,4 +1,4 @@
1
- import { M as MiddlewareOptions, e as Firewall } from '../firewall-BD4YdAmB.cjs';
1
+ import { M as MiddlewareOptions, e as Firewall } from '../firewall-D8XAQ9gj.cjs';
2
2
 
3
3
  /**
4
4
  * Vercel AI SDK 7 middleware integration.
@@ -1,4 +1,4 @@
1
- import { M as MiddlewareOptions, e as Firewall } from '../firewall-BD4YdAmB.js';
1
+ import { M as MiddlewareOptions, e as Firewall } from '../firewall-D8XAQ9gj.js';
2
2
 
3
3
  /**
4
4
  * Vercel AI SDK 7 middleware integration.
@@ -1 +1 @@
1
- export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BD4YdAmB.cjs';
1
+ export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-D8XAQ9gj.cjs';
@@ -1 +1 @@
1
- export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-BD4YdAmB.js';
1
+ export { g as FirewallMiddleware, S as createMiddleware } from '../firewall-D8XAQ9gj.js';
@@ -143,6 +143,14 @@ interface ClassifyOptions {
143
143
  signal?: AbortSignal;
144
144
  }
145
145
  interface ProtectOptions<TResult> extends ClassifyOptions {
146
+ /**
147
+ * Cancels this `protect()` call only, at any point before it settles. Aborting
148
+ * aborts the operation's signal and rejects with the signal's reason, even
149
+ * after the input verdict has passed. A signal that is already aborted rejects
150
+ * before the operation starts. A background classification in Shadow or Warn is
151
+ * not cancelled.
152
+ */
153
+ signal?: AbortSignal;
146
154
  /** Input classified while the operation runs. */
147
155
  text: string;
148
156
  /** Extracts model output to classify as llm_output before the result is returned. */
@@ -314,7 +322,8 @@ declare class Firewall {
314
322
  classify(text: string, options?: ClassifyOptions): Promise<BlockResult>;
315
323
  /**
316
324
  * Run an operation while its input is classified and withhold a blocked result.
317
- * The operation's signal aborts when a block arrives before it finishes.
325
+ * The operation's signal aborts when a block arrives before it finishes, and
326
+ * when the caller's `signal` aborts at any point before `protect()` settles.
318
327
  */
319
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
320
329
  /** Wait for classifications still running in the background. */
@@ -143,6 +143,14 @@ interface ClassifyOptions {
143
143
  signal?: AbortSignal;
144
144
  }
145
145
  interface ProtectOptions<TResult> extends ClassifyOptions {
146
+ /**
147
+ * Cancels this `protect()` call only, at any point before it settles. Aborting
148
+ * aborts the operation's signal and rejects with the signal's reason, even
149
+ * after the input verdict has passed. A signal that is already aborted rejects
150
+ * before the operation starts. A background classification in Shadow or Warn is
151
+ * not cancelled.
152
+ */
153
+ signal?: AbortSignal;
146
154
  /** Input classified while the operation runs. */
147
155
  text: string;
148
156
  /** Extracts model output to classify as llm_output before the result is returned. */
@@ -314,7 +322,8 @@ declare class Firewall {
314
322
  classify(text: string, options?: ClassifyOptions): Promise<BlockResult>;
315
323
  /**
316
324
  * Run an operation while its input is classified and withhold a blocked result.
317
- * The operation's signal aborts when a block arrives before it finishes.
325
+ * The operation's signal aborts when a block arrives before it finishes, and
326
+ * when the caller's `signal` aborts at any point before `protect()` settles.
318
327
  */
319
328
  protect<TResult>(operation: (signal: AbortSignal) => PromiseLike<TResult> | TResult, options: ProtectOptions<TResult>): Promise<TResult>;
320
329
  /** Wait for classifications still running in the background. */
package/dist/index.cjs CHANGED
@@ -1580,7 +1580,7 @@ function sanitizeText(text) {
1580
1580
  }
1581
1581
 
1582
1582
  // src/firewall.ts
1583
- var SDK_VERSION = "0.11.1";
1583
+ var SDK_VERSION = "0.11.2";
1584
1584
  var DEFAULT_TIMEOUT_MS = 1e4;
1585
1585
  var DEFAULT_MAX_RETRIES = 5;
1586
1586
  var MAX_BACKOFF_SECONDS = 30;
@@ -1906,6 +1906,24 @@ function createAttemptSignal(timeoutMs, callerSignal) {
1906
1906
  }
1907
1907
  };
1908
1908
  }
1909
+ function linkCaller(callerSignal, controller) {
1910
+ if (callerSignal === void 0) {
1911
+ return { until: (work) => Promise.resolve(work), dispose: () => void 0 };
1912
+ }
1913
+ let onAbort = () => void 0;
1914
+ const cancelled = new Promise((_resolve, reject) => {
1915
+ onAbort = () => {
1916
+ reject(callerSignal.reason);
1917
+ controller.abort(callerSignal.reason);
1918
+ };
1919
+ });
1920
+ cancelled.catch(() => void 0);
1921
+ callerSignal.addEventListener("abort", onAbort, { once: true });
1922
+ return {
1923
+ until: (work) => Promise.race([work, cancelled]),
1924
+ dispose: () => callerSignal.removeEventListener("abort", onAbort)
1925
+ };
1926
+ }
1909
1927
  function sleep(ms, signal) {
1910
1928
  return new Promise((resolve, reject) => {
1911
1929
  if (signal?.aborted) {
@@ -1969,43 +1987,59 @@ var Firewall = class {
1969
1987
  }
1970
1988
  /**
1971
1989
  * Run an operation while its input is classified and withhold a blocked result.
1972
- * The operation's signal aborts when a block arrives before it finishes.
1990
+ * The operation's signal aborts when a block arrives before it finishes, and
1991
+ * when the caller's `signal` aborts at any point before `protect()` settles.
1973
1992
  */
1974
1993
  async protect(operation, options) {
1975
1994
  const { text, output, ...request } = options;
1976
1995
  const requestedMode = validateMode(options.mode) ?? this.mode;
1996
+ const callerSignal = request.signal;
1997
+ callerSignal?.throwIfAborted();
1977
1998
  const controller = new AbortController();
1978
- if (requestedMode === "shadow" || requestedMode === "warn") {
1979
- const observed = this.observable(request);
1980
- if (observed !== void 0) {
1981
- void this.background(text, observed);
1999
+ const caller = linkCaller(callerSignal, controller);
2000
+ try {
2001
+ if (requestedMode === "shadow" || requestedMode === "warn") {
2002
+ const observed = this.observable(request);
2003
+ if (observed !== void 0) {
2004
+ void this.background(text, observed);
2005
+ }
2006
+ const result2 = await caller.until(operation(controller.signal));
2007
+ callerSignal?.throwIfAborted();
2008
+ if (observed !== void 0 && output !== void 0) {
2009
+ const generated = output(result2);
2010
+ callerSignal?.throwIfAborted();
2011
+ void this.background(generated, outputRequest(observed));
2012
+ }
2013
+ return result2;
1982
2014
  }
1983
- const result2 = await operation(controller.signal);
1984
- if (observed !== void 0 && output !== void 0) {
1985
- void this.background(output(result2), outputRequest(observed));
2015
+ const input = snapshotRequest(request);
2016
+ const conversation = conversationId(input.metadata);
2017
+ const outputOptions = outputRequest(input);
2018
+ const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
2019
+ const [, result] = await caller.until(Promise.all([
2020
+ verdict.catch((error) => {
2021
+ controller.abort(error);
2022
+ throw error;
2023
+ }),
2024
+ Promise.resolve().then(() => {
2025
+ callerSignal?.throwIfAborted();
2026
+ return operation(controller.signal);
2027
+ })
2028
+ ]));
2029
+ callerSignal?.throwIfAborted();
2030
+ if (output !== void 0) {
2031
+ const generated = output(result);
2032
+ callerSignal?.throwIfAborted();
2033
+ await caller.until(this.pending.submit(
2034
+ () => this.enforce(generated, outputOptions),
2035
+ conversation,
2036
+ input.signal
2037
+ ));
1986
2038
  }
1987
- return result2;
1988
- }
1989
- const input = snapshotRequest(request);
1990
- const conversation = conversationId(input.metadata);
1991
- const outputOptions = outputRequest(input);
1992
- const verdict = this.pending.submit(() => this.enforce(text, input), conversation, input.signal);
1993
- const [, result] = await Promise.all([
1994
- verdict.catch((error) => {
1995
- controller.abort(error);
1996
- throw error;
1997
- }),
1998
- Promise.resolve().then(() => operation(controller.signal))
1999
- ]);
2000
- if (output !== void 0) {
2001
- const generated = output(result);
2002
- await this.pending.submit(
2003
- () => this.enforce(generated, outputOptions),
2004
- conversation,
2005
- input.signal
2006
- );
2039
+ return result;
2040
+ } finally {
2041
+ caller.dispose();
2007
2042
  }
2008
- return result;
2009
2043
  }
2010
2044
  /** Wait for classifications still running in the background. */
2011
2045
  flush() {