fullcourtdefense 2.1.2 → 2.3.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/README.md +35 -4
- package/dist/anthropic.js +2 -0
- package/dist/client.d.ts +80 -1
- package/dist/client.js +425 -12
- package/dist/localDecision.d.ts +100 -0
- package/dist/localDecision.js +246 -0
- package/dist/localGate.d.ts +8 -0
- package/dist/localGate.js +41 -4
- package/dist/localSafety.d.ts +48 -0
- package/dist/localSafety.js +220 -0
- package/dist/policy/acorn.js +6365 -0
- package/dist/policy/actionPolicyEngine.d.ts +448 -0
- package/dist/policy/actionPolicyEngine.js +6560 -0
- package/dist/policy/detectionData.d.ts +21 -0
- package/dist/policy/detectionData.js +71 -0
- package/dist/policy/detectorRuntime.d.ts +27 -0
- package/dist/policy/detectorRuntime.js +58 -0
- package/dist/policy/deterministicGuard.d.ts +406 -0
- package/dist/policy/deterministicGuard.js +6829 -0
- package/dist/protect.d.ts +6 -0
- package/dist/protect.js +18 -0
- package/dist/recordQueue.d.ts +108 -0
- package/dist/recordQueue.js +296 -0
- package/dist/types.d.ts +57 -1
- package/package.json +16 -10
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LOCAL_SAFETY_RULE_PREFIX = void 0;
|
|
4
|
+
exports.parseServedLocalSafety = parseServedLocalSafety;
|
|
5
|
+
exports.evaluateLocalSafety = evaluateLocalSafety;
|
|
6
|
+
exports.localSafetyFailure = localSafetyFailure;
|
|
7
|
+
exports.withLocalSafety = withLocalSafety;
|
|
8
|
+
/**
|
|
9
|
+
* The built-in safety rules (Local Safety) on an SDK tool call, decided in-process: the twin of the gate's G-08 glue
|
|
10
|
+
* (backend modules/policies/sdk-agent-call.ts `evaluateSdkLocalSafety`, `withSdkLocalSafety`, `sdkLocalSafetyFailure`;
|
|
11
|
+
* local-safety-approval.ts `managerApprovalRuleIds`, `localSafetyManagerApproval`, `localSafetyApprovalCheck`).
|
|
12
|
+
* The guard itself is the byte-identical copy (./policy/deterministicGuard.ts = backend deterministic-guard.ts), and
|
|
13
|
+
* the snapshot is the one the gate uses, served in the bundle (`sdkGate.localSafety`, backend
|
|
14
|
+
* services/sdk-local-safety-served.ts). sdks/shared/policy-parity/vectors.json pins every answer.
|
|
15
|
+
*/
|
|
16
|
+
const actionPolicyEngine_1 = require("./policy/actionPolicyEngine");
|
|
17
|
+
const deterministicGuard_1 = require("./policy/deterministicGuard");
|
|
18
|
+
exports.LOCAL_SAFETY_RULE_PREFIX = 'local-safety:';
|
|
19
|
+
const NO_FINDING = Object.freeze({ wouldHave: [], ruleIds: [] });
|
|
20
|
+
function parseServedLocalSafety(raw) {
|
|
21
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw))
|
|
22
|
+
return undefined;
|
|
23
|
+
const value = raw;
|
|
24
|
+
if (value.stage !== 'enforce' && value.stage !== 'warn' && value.stage !== 'off')
|
|
25
|
+
return undefined;
|
|
26
|
+
if (!value.snapshot || typeof value.snapshot !== 'object' || Array.isArray(value.snapshot))
|
|
27
|
+
return undefined;
|
|
28
|
+
const snapshot = value.snapshot;
|
|
29
|
+
if (!Array.isArray(snapshot.categories) || !Array.isArray(snapshot.customBlocks) || !Array.isArray(snapshot.disabledBuiltInItemIds))
|
|
30
|
+
return undefined;
|
|
31
|
+
return {
|
|
32
|
+
stage: value.stage,
|
|
33
|
+
snapshot,
|
|
34
|
+
...(Array.isArray(value.wouldManagerIds) ? { wouldManagerIds: new Set(value.wouldManagerIds.filter((id) => typeof id === 'string')) } : {}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// ── local-safety-approval.ts twin ──
|
|
38
|
+
function managerApprovalRuleIds(snapshot) {
|
|
39
|
+
const listed = new Set(snapshot.managerApprovalIds || []);
|
|
40
|
+
const ids = new Set();
|
|
41
|
+
if (listed.size === 0)
|
|
42
|
+
return ids;
|
|
43
|
+
const disabled = new Set(snapshot.disabledBuiltInItemIds || []);
|
|
44
|
+
for (const [id, action] of Object.entries(snapshot.itemActions || {})) {
|
|
45
|
+
if (action === 'ask' && listed.has(id) && !disabled.has(id))
|
|
46
|
+
ids.add(id);
|
|
47
|
+
}
|
|
48
|
+
for (const block of snapshot.customBlocks || []) {
|
|
49
|
+
if (block.action === 'ask' && listed.has(block.id))
|
|
50
|
+
ids.add(block.id);
|
|
51
|
+
}
|
|
52
|
+
return ids;
|
|
53
|
+
}
|
|
54
|
+
function localSafetyManagerApproval(snapshot, ref) {
|
|
55
|
+
if (!managerApprovalRuleIds(snapshot).has(ref))
|
|
56
|
+
return undefined;
|
|
57
|
+
const custom = (snapshot.customBlocks || []).find((block) => block.id === ref);
|
|
58
|
+
if (custom)
|
|
59
|
+
return { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${custom.id}`, policyName: `Command contains “${custom.pattern}”`, itemId: custom.id };
|
|
60
|
+
for (const category of snapshot.categories || []) {
|
|
61
|
+
const item = category.items.find((entry) => entry.id === ref);
|
|
62
|
+
if (item)
|
|
63
|
+
return { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${item.id}`, policyName: item.label, itemId: item.id };
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
function localSafetyApprovalCheck(rule) {
|
|
68
|
+
return {
|
|
69
|
+
allowed: false,
|
|
70
|
+
verdict: 'require_approval',
|
|
71
|
+
policyId: rule.policyId,
|
|
72
|
+
policyName: rule.policyName,
|
|
73
|
+
resourceType: 'custom',
|
|
74
|
+
matchedRule: rule.itemId,
|
|
75
|
+
reason: `Shell-command rule "${rule.policyName}" needs a manager's OK before this runs`,
|
|
76
|
+
approval: { ...actionPolicyEngine_1.DEFAULT_ACTION_POLICY_APPROVAL, scope: 'org' },
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// ── sdk-agent-call.ts twin ──
|
|
80
|
+
function ruleRef(snapshot, finding) {
|
|
81
|
+
const itemId = finding.itemId;
|
|
82
|
+
if (finding.source === 'custom') {
|
|
83
|
+
const block = (snapshot.customBlocks || []).find((entry) => entry.id === itemId);
|
|
84
|
+
return { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${itemId}`, policyName: block?.pattern ? `Contains “${block.pattern}”` : 'Custom safety rule', itemId };
|
|
85
|
+
}
|
|
86
|
+
for (const category of snapshot.categories || []) {
|
|
87
|
+
const item = category.items.find((entry) => entry.id === itemId);
|
|
88
|
+
if (item)
|
|
89
|
+
return { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${item.id}`, policyName: item.label, itemId };
|
|
90
|
+
}
|
|
91
|
+
return { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${itemId}`, policyName: 'Built-in safety rule', itemId };
|
|
92
|
+
}
|
|
93
|
+
function stripStop(text) {
|
|
94
|
+
return String(text || '').trim().replace(/[.\s]+$/, '');
|
|
95
|
+
}
|
|
96
|
+
function blockCheck(rule, reason) {
|
|
97
|
+
return { allowed: false, verdict: 'block', policyId: rule.policyId, policyName: rule.policyName, resourceType: 'custom', matchedRule: rule.itemId, reason };
|
|
98
|
+
}
|
|
99
|
+
function warnCheck(rule, reason) {
|
|
100
|
+
return { allowed: true, verdict: 'log', policyId: rule.policyId, policyName: rule.policyName, resourceType: 'custom', matchedRule: rule.itemId, reason };
|
|
101
|
+
}
|
|
102
|
+
function wouldMatch(snapshot, finding, wouldManagerIds) {
|
|
103
|
+
const would = snapshot.wouldActions?.[finding.itemId];
|
|
104
|
+
if (!would || would === 'warn')
|
|
105
|
+
return undefined;
|
|
106
|
+
const rule = ruleRef(snapshot, finding);
|
|
107
|
+
const verdict = would === 'ask' && wouldManagerIds?.has(finding.itemId) ? 'require_approval' : 'block';
|
|
108
|
+
return { policyId: rule.policyId, policyName: rule.policyName, verdict, matchedRule: rule.itemId };
|
|
109
|
+
}
|
|
110
|
+
function evaluateLocalSafety(call, input) {
|
|
111
|
+
if (input.stage === 'off')
|
|
112
|
+
return NO_FINDING;
|
|
113
|
+
const { snapshot } = input;
|
|
114
|
+
const enforce = input.stage === 'enforce';
|
|
115
|
+
const mcpServerName = call.mcpServer && call.mcpServer.trim() ? call.mcpServer.trim() : undefined;
|
|
116
|
+
const oversize = (0, deterministicGuard_1.oversizedAction)(call.toolName, call.toolArgs, mcpServerName ? { mcpServerName } : undefined);
|
|
117
|
+
if (oversize) {
|
|
118
|
+
const words = (0, deterministicGuard_1.oversizeWording)(oversize, 'tool call');
|
|
119
|
+
const rule = { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}${words.ruleId}`, policyName: 'Too large to check safely', itemId: words.ruleId };
|
|
120
|
+
if (enforce)
|
|
121
|
+
return { check: blockCheck(rule, words.blockedRecord), wouldHave: [], ruleIds: [words.ruleId] };
|
|
122
|
+
return {
|
|
123
|
+
check: warnCheck(rule, words.monitorRecord),
|
|
124
|
+
wouldHave: snapshot.wouldActions ? [{ policyId: rule.policyId, policyName: rule.policyName, verdict: 'block', matchedRule: rule.itemId }] : [],
|
|
125
|
+
ruleIds: [words.ruleId],
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const options = {
|
|
129
|
+
disabledBuiltInItemIds: snapshot.disabledBuiltInItemIds,
|
|
130
|
+
itemActions: snapshot.itemActions,
|
|
131
|
+
snapshotPresent: true,
|
|
132
|
+
customBlocks: snapshot.customBlocks,
|
|
133
|
+
policyHash: snapshot.policyHash,
|
|
134
|
+
...(mcpServerName ? { mcpServerName } : {}),
|
|
135
|
+
// As on the server: no filesystem reads, no decoys.
|
|
136
|
+
inspectScripts: false,
|
|
137
|
+
};
|
|
138
|
+
const outcome = (0, deterministicGuard_1.resolveDeterministicOutcome)((opts) => (0, deterministicGuard_1.scanDeterministicToolCall)(call.toolName, call.toolArgs, opts), options);
|
|
139
|
+
const ruleIds = [
|
|
140
|
+
...(outcome.blockingFinding ? [outcome.blockingFinding.ruleId] : []),
|
|
141
|
+
...(outcome.askFindings || []).map((finding) => finding.ruleId),
|
|
142
|
+
...outcome.warnings.map((finding) => finding.ruleId),
|
|
143
|
+
];
|
|
144
|
+
if (!enforce) {
|
|
145
|
+
const findings = [
|
|
146
|
+
...(outcome.blockingFinding ? [outcome.blockingFinding] : []),
|
|
147
|
+
...(outcome.askFindings || []),
|
|
148
|
+
...outcome.warnings,
|
|
149
|
+
];
|
|
150
|
+
if (findings.length === 0)
|
|
151
|
+
return NO_FINDING;
|
|
152
|
+
const rule = ruleRef(snapshot, findings[0]);
|
|
153
|
+
return {
|
|
154
|
+
check: warnCheck(rule, `Built-in safety rule "${rule.policyName}" (warn only here): ${stripStop(findings[0].reason)}`),
|
|
155
|
+
wouldHave: findings.map((finding) => wouldMatch(snapshot, finding, input.wouldManagerIds)).filter((match) => Boolean(match)),
|
|
156
|
+
ruleIds,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (outcome.blockingFinding) {
|
|
160
|
+
const finding = outcome.blockingFinding;
|
|
161
|
+
const rule = ruleRef(snapshot, finding);
|
|
162
|
+
const masked = (finding.action || 'block') === 'mask';
|
|
163
|
+
return {
|
|
164
|
+
check: blockCheck(rule, `Built-in safety rule "${rule.policyName}": ${stripStop(finding.reason)}${masked ? ' (set to Mask, which cannot rewrite a tool call, so it stops it)' : ''}`),
|
|
165
|
+
wouldHave: [],
|
|
166
|
+
ruleIds,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (outcome.askFinding) {
|
|
170
|
+
const managerIds = managerApprovalRuleIds(snapshot);
|
|
171
|
+
const managerFinding = (outcome.askFindings || []).find((finding) => managerIds.has(finding.itemId));
|
|
172
|
+
const manager = managerFinding ? localSafetyManagerApproval(snapshot, managerFinding.itemId) : undefined;
|
|
173
|
+
if (manager) {
|
|
174
|
+
const check = localSafetyApprovalCheck(manager);
|
|
175
|
+
return { check: { ...check, reason: `Built-in safety rule "${manager.policyName}" needs a manager's OK before this runs` }, wouldHave: [], ruleIds };
|
|
176
|
+
}
|
|
177
|
+
const finding = (outcome.askFindings || [])[0] || outcome.askFinding;
|
|
178
|
+
const rule = ruleRef(snapshot, finding);
|
|
179
|
+
return {
|
|
180
|
+
check: blockCheck(rule, `Built-in safety rule "${rule.policyName}" asks the developer; an AI agent with the SDK in its code has nobody to answer, so it is stopped: ${stripStop(finding.reason)}`),
|
|
181
|
+
wouldHave: [],
|
|
182
|
+
ruleIds,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (outcome.warnings.length === 0)
|
|
186
|
+
return NO_FINDING;
|
|
187
|
+
const first = outcome.warnings[0];
|
|
188
|
+
const rule = ruleRef(snapshot, first);
|
|
189
|
+
return { check: warnCheck(rule, `Built-in safety rule "${rule.policyName}" (warn only): ${stripStop(first.reason)}`), wouldHave: [], ruleIds };
|
|
190
|
+
}
|
|
191
|
+
function localSafetyFailure(stage, error) {
|
|
192
|
+
if (stage === 'off')
|
|
193
|
+
return NO_FINDING;
|
|
194
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
195
|
+
const rule = { policyId: `${exports.LOCAL_SAFETY_RULE_PREFIX}check-failed`, policyName: 'Built-in safety rules could not run', itemId: 'check-failed' };
|
|
196
|
+
const reason = `The built-in safety rules could not check this tool call (${message.slice(0, 160)})`;
|
|
197
|
+
return stage === 'enforce'
|
|
198
|
+
? { check: blockCheck(rule, `${reason}; it is stopped`), wouldHave: [], ruleIds: [rule.itemId] }
|
|
199
|
+
: { check: warnCheck(rule, `${reason}; recorded only (the rules only warn here)`), wouldHave: [], ruleIds: [rule.itemId] };
|
|
200
|
+
}
|
|
201
|
+
/** The action-policy result with the built-in safety rules folded in (the gate's order; the strictest decides). */
|
|
202
|
+
function withLocalSafety(actionPolicy, outcome) {
|
|
203
|
+
const monitorMatches = [...outcome.wouldHave, ...(actionPolicy.monitorMatches || [])];
|
|
204
|
+
const withMonitor = (result) => {
|
|
205
|
+
const rest = { ...result };
|
|
206
|
+
delete rest.monitorMatches;
|
|
207
|
+
return monitorMatches.length > 0 ? { ...rest, monitorMatches } : rest;
|
|
208
|
+
};
|
|
209
|
+
const check = outcome.check;
|
|
210
|
+
if (!check)
|
|
211
|
+
return outcome.wouldHave.length > 0 ? withMonitor(actionPolicy) : actionPolicy;
|
|
212
|
+
if (check.verdict === 'block')
|
|
213
|
+
return withMonitor(check);
|
|
214
|
+
if (check.verdict === 'require_approval') {
|
|
215
|
+
const policyHolds = actionPolicy.verdict === 'block'
|
|
216
|
+
|| (actionPolicy.verdict === 'require_approval' && actionPolicy.approval?.scope !== 'developer');
|
|
217
|
+
return withMonitor(policyHolds ? actionPolicy : check);
|
|
218
|
+
}
|
|
219
|
+
return withMonitor(actionPolicy.verdict === 'allow' ? check : actionPolicy);
|
|
220
|
+
}
|