fullcourtdefense 2.1.2 → 2.2.0

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/dist/protect.d.ts CHANGED
@@ -53,6 +53,12 @@ export declare function inferToolName(target: unknown, explicit?: string): strin
53
53
  * the positional list under `args`.
54
54
  */
55
55
  export declare function defaultToolArgs(args: unknown[]): Record<string, unknown> | undefined;
56
+ /**
57
+ * The model provider's tool-call id a framework passes in its trailing context argument (Vercel AI SDK
58
+ * `execute(args, { toolCallId })`, LangChain `invoke(input, { toolCall: { id } })`), or undefined. Recorded so one
59
+ * action is one record when a local AI gateway also saw the same call; never used to decide.
60
+ */
61
+ export declare function frameworkToolCallId(args: unknown[]): string | undefined;
56
62
  /**
57
63
  * Wrap a plain function or a framework tool object. Returns the same shape the
58
64
  * caller passed in (a function, or a copy of the tool with its entry methods
package/dist/protect.js CHANGED
@@ -17,6 +17,7 @@ exports.openAIAgentsToolArgs = openAIAgentsToolArgs;
17
17
  exports.detectFramework = detectFramework;
18
18
  exports.inferToolName = inferToolName;
19
19
  exports.defaultToolArgs = defaultToolArgs;
20
+ exports.frameworkToolCallId = frameworkToolCallId;
20
21
  exports.protectTarget = protectTarget;
21
22
  /** Methods a framework tool may expose as its "run" entry point, in wrap order. */
22
23
  const TOOL_ENTRY_METHODS = ['invoke', 'call', 'execute', 'run'];
@@ -176,6 +177,22 @@ function defaultToolArgs(args) {
176
177
  return args[0];
177
178
  return { args };
178
179
  }
180
+ /**
181
+ * The model provider's tool-call id a framework passes in its trailing context argument (Vercel AI SDK
182
+ * `execute(args, { toolCallId })`, LangChain `invoke(input, { toolCall: { id } })`), or undefined. Recorded so one
183
+ * action is one record when a local AI gateway also saw the same call; never used to decide.
184
+ */
185
+ function frameworkToolCallId(args) {
186
+ for (const value of args.slice(1)) {
187
+ if (!value || typeof value !== 'object')
188
+ continue;
189
+ const context = value;
190
+ const id = context.toolCallId ?? context.tool_call_id ?? context.toolCall?.id;
191
+ if (typeof id === 'string' && id.trim() && id.length <= 200)
192
+ return id.trim();
193
+ }
194
+ return undefined;
195
+ }
179
196
  /** A trailing argument that is framework context, not data: nothing, an object (options, signal) or a callback. */
180
197
  function isFrameworkContext(value) {
181
198
  return value === undefined || value === null || typeof value === 'object' || typeof value === 'function';
@@ -209,6 +226,7 @@ async function guardedInvoke(fcd, resolved, options, args, invoke) {
209
226
  sessionId: options.sessionId,
210
227
  framework: resolved.framework,
211
228
  toolArgs,
229
+ ...(frameworkToolCallId(args) ? { toolCallId: frameworkToolCallId(args) } : {}),
212
230
  chainId: options.chainId,
213
231
  parentActionId: options.parentActionId,
214
232
  userRole: options.userRole,
@@ -0,0 +1,53 @@
1
+ /**
2
+ * The records of locally decided tool calls (owner order 2026-11-01): queued in memory and sent in the
3
+ * background, in small batches, to the existing record endpoint (`POST /api/agent-security/runtime/tool-call`).
4
+ * Nothing here is ever awaited on a tool call's path.
5
+ *
6
+ * - bounded: past `max` the OLDEST record is dropped and counted (`lost`), never the agent's memory;
7
+ * - coalesced: a push schedules one send `delayMs` later, so a burst goes out as one batch of up to `batch`
8
+ * records, `concurrency` requests at a time;
9
+ * - outage: a record the API did not answer (no answer, timeout, 408, 429, 5xx) goes back to the FRONT of
10
+ * the queue and sending pauses (1 s doubling to 30 s, or until `resume()`: the bundle refresh answered);
11
+ * - a definite refusal (another 4xx) drops that record: resending it would be refused again;
12
+ * - exit: `flush()` sends everything now (the caller bounds the wait), `drain()` hands what is left to the
13
+ * local spool (recordSpool.ts) so the next process sends it.
14
+ */
15
+ export interface QueuedRecord {
16
+ body: Record<string, unknown>;
17
+ at: string;
18
+ }
19
+ export type SendOutcome = 'sent' | 'outage' | 'refused';
20
+ export interface RecordQueueOptions {
21
+ max?: number;
22
+ batch?: number;
23
+ concurrency?: number;
24
+ delayMs?: number;
25
+ }
26
+ export declare class RecordQueue {
27
+ private readonly send;
28
+ private readonly items;
29
+ private readonly max;
30
+ private readonly batch;
31
+ private readonly concurrency;
32
+ private readonly delayMs;
33
+ private timer?;
34
+ private running?;
35
+ private pausedUntil;
36
+ private backoffMs;
37
+ /** Records dropped because the queue was full (oldest first). */
38
+ lost: number;
39
+ /** Records sent / refused since the start (observability, tests). */
40
+ sent: number;
41
+ refused: number;
42
+ constructor(send: (body: Record<string, unknown>) => Promise<SendOutcome>, options?: RecordQueueOptions);
43
+ get size(): number;
44
+ push(body: Record<string, unknown>): void;
45
+ /** The API answered again (a bundle refresh): send now instead of waiting out the backoff. */
46
+ resume(): void;
47
+ /** Send everything queued now (exit); resolves when every batch was tried once. Never rejects. */
48
+ flush(): Promise<void>;
49
+ /** Everything still queued, removed from the queue (handed to the local spool at exit). */
50
+ drain(): QueuedRecord[];
51
+ private schedule;
52
+ private runOnce;
53
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RecordQueue = void 0;
4
+ const BACKOFF_MIN_MS = 1000;
5
+ const BACKOFF_MAX_MS = 30000;
6
+ class RecordQueue {
7
+ constructor(send, options = {}) {
8
+ this.send = send;
9
+ this.items = [];
10
+ this.pausedUntil = 0;
11
+ this.backoffMs = BACKOFF_MIN_MS;
12
+ /** Records dropped because the queue was full (oldest first). */
13
+ this.lost = 0;
14
+ /** Records sent / refused since the start (observability, tests). */
15
+ this.sent = 0;
16
+ this.refused = 0;
17
+ this.max = Math.max(1, options.max ?? 10000);
18
+ this.batch = Math.max(1, options.batch ?? 100);
19
+ this.concurrency = Math.max(1, options.concurrency ?? 4);
20
+ this.delayMs = Math.max(0, options.delayMs ?? 100);
21
+ }
22
+ get size() {
23
+ return this.items.length;
24
+ }
25
+ push(body) {
26
+ if (this.items.length >= this.max) {
27
+ this.items.shift();
28
+ this.lost += 1;
29
+ }
30
+ this.items.push({ body, at: new Date().toISOString() });
31
+ this.schedule(this.delayMs);
32
+ }
33
+ /** The API answered again (a bundle refresh): send now instead of waiting out the backoff. */
34
+ resume() {
35
+ if (this.pausedUntil === 0)
36
+ return;
37
+ this.pausedUntil = 0;
38
+ this.backoffMs = BACKOFF_MIN_MS;
39
+ if (this.items.length > 0)
40
+ this.schedule(0);
41
+ }
42
+ /** Send everything queued now (exit); resolves when every batch was tried once. Never rejects. */
43
+ async flush() {
44
+ this.pausedUntil = 0;
45
+ if (this.running)
46
+ await this.running;
47
+ let guard = Math.ceil(this.items.length / this.batch) + 1;
48
+ while (this.items.length > 0 && guard-- > 0) {
49
+ const before = this.items.length;
50
+ await this.runOnce();
51
+ if (this.items.length >= before)
52
+ break; // the API does not answer: the caller spools the rest
53
+ }
54
+ }
55
+ /** Everything still queued, removed from the queue (handed to the local spool at exit). */
56
+ drain() {
57
+ if (this.timer)
58
+ clearTimeout(this.timer);
59
+ this.timer = undefined;
60
+ return this.items.splice(0, this.items.length);
61
+ }
62
+ schedule(delayMs) {
63
+ if (this.timer || this.running)
64
+ return;
65
+ const wait = Math.max(delayMs, this.pausedUntil - Date.now(), 0);
66
+ const timer = setTimeout(() => {
67
+ this.timer = undefined;
68
+ void this.runOnce().then(() => {
69
+ if (this.items.length > 0)
70
+ this.schedule(this.pausedUntil > Date.now() ? 0 : this.delayMs);
71
+ });
72
+ }, wait);
73
+ // A queue must never keep a process alive on its own; the exit hooks flush or spool what is left.
74
+ timer.unref?.();
75
+ this.timer = timer;
76
+ }
77
+ runOnce() {
78
+ if (this.running)
79
+ return this.running;
80
+ const run = (async () => {
81
+ const take = this.items.splice(0, this.batch);
82
+ if (take.length === 0)
83
+ return;
84
+ const back = [];
85
+ let index = 0;
86
+ let outage = false;
87
+ const worker = async () => {
88
+ while (index < take.length) {
89
+ const item = take[index++];
90
+ if (outage) {
91
+ back.push(item);
92
+ continue;
93
+ }
94
+ let result;
95
+ try {
96
+ result = await this.send(item.body);
97
+ }
98
+ catch {
99
+ result = 'outage';
100
+ }
101
+ if (result === 'sent')
102
+ this.sent += 1;
103
+ else if (result === 'refused')
104
+ this.refused += 1;
105
+ else {
106
+ outage = true;
107
+ back.push(item);
108
+ }
109
+ }
110
+ };
111
+ await Promise.all(Array.from({ length: Math.min(this.concurrency, take.length) }, worker));
112
+ if (back.length > 0) {
113
+ // Keep the order: what could not be sent goes back to the front.
114
+ back.sort((a, b) => take.indexOf(a) - take.indexOf(b));
115
+ this.items.unshift(...back);
116
+ while (this.items.length > this.max) {
117
+ this.items.shift();
118
+ this.lost += 1;
119
+ }
120
+ this.pausedUntil = Date.now() + this.backoffMs;
121
+ this.backoffMs = Math.min(BACKOFF_MAX_MS, this.backoffMs * 2);
122
+ }
123
+ else {
124
+ this.backoffMs = BACKOFF_MIN_MS;
125
+ this.pausedUntil = 0;
126
+ }
127
+ })();
128
+ this.running = run.finally(() => { this.running = undefined; });
129
+ return this.running;
130
+ }
131
+ }
132
+ exports.RecordQueue = RecordQueue;
package/dist/types.d.ts CHANGED
@@ -71,6 +71,26 @@ export interface FullCourtDefenseConfig {
71
71
  * When only `scanTimeoutMs` is set, it applies to the check too.
72
72
  */
73
73
  toolCheckTimeoutMs?: number;
74
+ /**
75
+ * Where a Block-mode tool call is decided (owner order 2026-11-01; env `FCD_DECISION_MODE`):
76
+ * - `'local'` (the default): in this process, from the shield's policy bundle the SDK fetches at start and
77
+ * refreshes every 60 s in the background, with the server's exact semantics (the same policy engine). No
78
+ * request on the tool call's path; the record is sent afterwards in background batches. Only a call that
79
+ * needs a manager's approval, and a call the bundle cannot decide (a command / file / web tool while the
80
+ * built-in safety rules are on, a policy that reads the agent's history, a suspended shield), go to the
81
+ * server. A server kill switch (`sdkGate.decisions: 'server'` in the bundle) turns this off fleet-wide.
82
+ * - `'server'`: every Block-mode call is checked by `POST /runtime/check-tool-call` (SDK 2.1 behaviour).
83
+ * Monitor mode never waits either way.
84
+ */
85
+ decisionMode?: 'local' | 'server';
86
+ /**
87
+ * Local decisions, cold start only: how long after the client is built a Block-mode call may wait for the FIRST
88
+ * policy bundle (default 1500 ms, the bound the first call already had in SDK 2.1; env `FCD_COLD_START_WAIT_MS`).
89
+ * After that deadline, or once a bundle arrived, no call ever waits: a call with no bundle runs and is reported.
90
+ * A shield recorded as Monitor (`FCD_MODE_AT_PROTECT=monitor`) never waits. `0` = never wait, even on the first
91
+ * call of a short-lived agent (its first calls then run unchecked until the bundle arrives, and are reported).
92
+ */
93
+ coldStartWaitMs?: number;
74
94
  /**
75
95
  * Risk-tiered fail direction (block mode only). When the backend is
76
96
  * unreachable/slow/erroring (or the circuit breaker is open), calls whose
@@ -409,6 +429,16 @@ export interface AgentToolCallInput {
409
429
  reason?: string;
410
430
  argsSummary?: Record<string, unknown>;
411
431
  toolArgs?: Record<string, unknown>;
432
+ /**
433
+ * The model provider's id of this tool call (OpenAI `tool_call_id`, Anthropic `tool_use` id, Vercel AI
434
+ * `toolCallId`). Sent on the record so one action is one record when a local AI gateway also sees the same
435
+ * call (mixed SDK + gateway); also keys a manager approval to this call (`attemptId`).
436
+ */
437
+ toolCallId?: string;
438
+ /** The client's own id of this attempt; the SDK sets it from `toolCallId` (the server keys a manager approval to it). */
439
+ attemptId?: string;
440
+ /** Which enforcement point sent this (set by the SDK: `sdk`; a local AI gateway sends `gateway`). Never `source`. */
441
+ decisionSource?: 'sdk';
412
442
  environment?: string;
413
443
  chainId?: string;
414
444
  parentActionId?: string;
@@ -450,6 +480,19 @@ export interface AgentSecurityActionPolicyResult {
450
480
  resourceType?: string;
451
481
  matchedRule?: string;
452
482
  reason?: string;
483
+ /** `require_approval` only: who approves (`developer` = asks the developer; an SDK agent has none, so the organization). */
484
+ approval?: {
485
+ scope?: 'developer' | 'org';
486
+ remember?: string;
487
+ onTimeout?: string;
488
+ };
489
+ /** Monitor-stage policies that matched: what they WOULD have done. */
490
+ monitorMatches?: Array<{
491
+ policyId?: string;
492
+ policyName?: string;
493
+ verdict: string;
494
+ matchedRule?: string;
495
+ }>;
453
496
  }
454
497
  export interface AgentSecurityApiResult<T = unknown> {
455
498
  success: boolean;
@@ -500,6 +543,10 @@ export interface AgentToolCallCheckResult {
500
543
  * than 32 levels, more than 20,000 values, or not encodable as JSON): stopped on Block, never an outage.
501
544
  */
502
545
  argsProblem?: string;
546
+ /** Where this answer was decided: `local` (this process, from the policy bundle) or `server` (check-tool-call). */
547
+ decidedBy?: 'local' | 'server';
548
+ /** The shared outcome word (sdks/shared/policy-parity): allow, warn, monitor, ask_developer, manager_approval, block, hard_block. */
549
+ outcome?: 'allow' | 'warn' | 'monitor' | 'ask_developer' | 'manager_approval' | 'block' | 'hard_block';
503
550
  }
504
551
  export interface AgentApprovalStatusResult {
505
552
  approval: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fullcourtdefense",
3
- "version": "2.1.2",
4
- "description": "Tool-call control for AI agents: every tool call (MCP, LangChain, OpenAI Agents SDK, LlamaIndex, AutoGen, CrewAI, Semantic Kernel, Bedrock action groups, plain functions) is checked against your organisation's action policy before it runs. Block, require a manager's approval, or monitor. Zero-code preload, no runtime dependencies.",
3
+ "version": "2.2.0",
4
+ "description": "Control what your AI agents do: check every tool call against your company's FullCourtDefense policy (Monitor or Enforce).",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "bin": {
@@ -16,16 +16,17 @@
16
16
  ],
17
17
  "scripts": {
18
18
  "clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
19
- "build": "npm run clean && tsc",
20
- "build:lib": "tsc",
19
+ "build": "npm run clean && npm run compile",
20
+ "build:lib": "npm run compile",
21
21
  "typecheck": "tsc --noEmit",
22
- "test": "tsc && node scripts/run-tests.js",
22
+ "test": "npm run compile && node scripts/run-tests.js",
23
23
  "test:pack": "node scripts/check-pack.js",
24
- "test:prove-actions-drill": "tsc && node scripts/test-prove-actions.js",
25
- "test:auto-preload": "tsc && node scripts/test-auto-preload.js",
26
- "test:auto-real-packages": "tsc && node scripts/test-auto-real-packages.js",
27
- "prove:actions": "tsc && node scripts/prove-actions.mjs",
28
- "prepublishOnly": "npm run build && node scripts/check-pack.js"
24
+ "test:prove-actions-drill": "npm run compile && node scripts/test-prove-actions.js",
25
+ "test:auto-preload": "npm run compile && node scripts/test-auto-preload.js",
26
+ "test:auto-real-packages": "npm run compile && node scripts/test-auto-real-packages.js",
27
+ "prove:actions": "npm run compile && node scripts/prove-actions.mjs",
28
+ "prepublishOnly": "npm run build && node scripts/check-pack.js",
29
+ "compile": "tsc && node scripts/vendor-acorn.js"
29
30
  },
30
31
  "keywords": [
31
32
  "ai-agent-security",
@@ -39,6 +40,7 @@
39
40
  "mcp",
40
41
  "model-context-protocol",
41
42
  "langchain",
43
+ "langgraph",
42
44
  "openai-agents",
43
45
  "llamaindex",
44
46
  "autogen",
@@ -46,6 +48,8 @@
46
48
  "semantic-kernel",
47
49
  "bedrock-agents",
48
50
  "runtime-security",
51
+ "monitor-mode",
52
+ "policy-enforcement",
49
53
  "fullcourtdefense"
50
54
  ],
51
55
  "homepage": "https://fullcourtdefense.ai",
@@ -58,6 +62,8 @@
58
62
  },
59
63
  "license": "MIT",
60
64
  "devDependencies": {
65
+ "@types/node": "^18.19.130",
66
+ "acorn": "^8.18.0",
61
67
  "typescript": "^5.3.0"
62
68
  },
63
69
  "engines": {