-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsandbox-tool-policy.ts
More file actions
327 lines (296 loc) · 13.4 KB
/
Copy pathsandbox-tool-policy.ts
File metadata and controls
327 lines (296 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { isPlainObject, stringList } from "./object-utils.js"
export const SANDBOX_TOOL_POLICY_SCHEMA = "wp-codebox/sandbox-tool-policy/v1" as const
export const SANDBOX_TOOL_POLICY_VERSION = 1 as const
export const TOOL_BRIDGE_SCHEMA = "wp-codebox/tool-bridge/v1" as const
export const TOOL_BRIDGE_VERSION = 1 as const
export const AGENTS_API_RUNTIME_ENVIRONMENT = "environment" as const
export const AGENTS_API_RUNTIME_CAPABILITY_SCOPE = "capability_scope" as const
export const AGENTS_API_RUNTIME_LOCAL = "runtime_local" as const
export const AGENTS_API_CONTROL_PLANE = "control_plane" as const
export type SandboxToolExecutionLocation = "sandbox" | "parent" | "external" | (string & {})
export type SandboxToolTransportVisibility = "sandbox" | "parent" | "both" | "hidden" | (string & {})
export type AgentsApiRuntimeEnvironment = typeof AGENTS_API_RUNTIME_LOCAL | typeof AGENTS_API_CONTROL_PLANE | (string & {})
export interface AgentsApiRuntimeToolMetadata {
[AGENTS_API_RUNTIME_ENVIRONMENT]?: AgentsApiRuntimeEnvironment
[AGENTS_API_RUNTIME_CAPABILITY_SCOPE]?: AgentsApiRuntimeEnvironment
[key: string]: unknown
}
export interface SandboxToolPolicyTool {
id: string
runtime_tool_id: string
aliases?: string[]
execution_location: SandboxToolExecutionLocation
transport_visibility: SandboxToolTransportVisibility
allowed: boolean
runtime?: AgentsApiRuntimeToolMetadata
risk?: string
action?: string
metadata?: Record<string, unknown>
}
export interface SandboxToolPolicySnapshot {
schema: typeof SANDBOX_TOOL_POLICY_SCHEMA
version: typeof SANDBOX_TOOL_POLICY_VERSION
tools: SandboxToolPolicyTool[]
metadata: Record<string, unknown>
}
export interface SandboxToolPolicyIssue {
code: "invalid-policy" | "invalid-tool" | "duplicate-tool"
field: string
message: string
}
export interface SandboxToolPolicyValidationResult {
valid: boolean
issues: SandboxToolPolicyIssue[]
}
export interface RuntimeToolDescriptor {
id: string
runtimeToolId: string
aliases: string[]
allowed: boolean
executionLocation: SandboxToolExecutionLocation
transportVisibility: SandboxToolTransportVisibility
visible: boolean
parentOnly: boolean
hidden: boolean
runtime: Required<Pick<AgentsApiRuntimeToolMetadata, typeof AGENTS_API_RUNTIME_ENVIRONMENT | typeof AGENTS_API_RUNTIME_CAPABILITY_SCOPE>>
schema?: string
policy?: Record<string, unknown>
metadata?: Record<string, unknown>
}
export interface EffectiveRuntimeToolPolicy {
schema: typeof SANDBOX_TOOL_POLICY_SCHEMA
version: typeof SANDBOX_TOOL_POLICY_VERSION
tools: RuntimeToolDescriptor[]
allowedRuntimeToolIds: string[]
visibleRuntimeToolIds: string[]
parentOnlyRuntimeToolIds: string[]
hiddenRuntimeToolIds: string[]
metadata: Record<string, unknown>
}
export interface RuntimeToolInput {
allowed_tools: string[]
runtime_tools: RuntimeToolDescriptor[]
}
export interface ToolBridgeContract {
schema: typeof TOOL_BRIDGE_SCHEMA
version: typeof TOOL_BRIDGE_VERSION
allowed_tools: string[]
sandbox_tool_policy: SandboxToolPolicySnapshot
dispatcher: {
owner: "wp-codebox"
callback: "wp_codebox_browser_runtime_tool_callback"
location: "sandbox"
}
authorization: {
mode: "allowlist"
notes: string
}
redaction: {
notes: string
}
metadata?: Record<string, unknown>
}
export class SandboxToolPolicyValidationError extends Error {
readonly code = "sandbox-tool-policy-invalid" as const
constructor(readonly issues: SandboxToolPolicyIssue[]) {
super(`Sandbox tool policy is invalid: ${issues.map((issue) => issue.message).join("; ")}`)
this.name = "SandboxToolPolicyValidationError"
}
toJSON(): { code: "sandbox-tool-policy-invalid"; issues: SandboxToolPolicyIssue[]; message: string; name: string } {
return {
code: this.code,
issues: this.issues,
message: this.message,
name: this.name,
}
}
}
export function normalizeSandboxToolPolicySnapshot(input: unknown): SandboxToolPolicySnapshot {
assertSandboxToolPolicySnapshot(input)
return input
}
export function validateSandboxToolPolicySnapshot(input: unknown): SandboxToolPolicyValidationResult {
const issues: SandboxToolPolicyIssue[] = []
if (!isPlainObject(input)) {
return {
valid: false,
issues: [{ code: "invalid-policy", field: "sandbox_tool_policy", message: "sandbox_tool_policy must be an object." }],
}
}
if (input.schema !== SANDBOX_TOOL_POLICY_SCHEMA) {
issues.push({ code: "invalid-policy", field: "schema", message: `sandbox_tool_policy.schema must be ${SANDBOX_TOOL_POLICY_SCHEMA}.` })
}
if (input.version !== SANDBOX_TOOL_POLICY_VERSION) {
issues.push({ code: "invalid-policy", field: "version", message: `sandbox_tool_policy.version must be ${SANDBOX_TOOL_POLICY_VERSION}.` })
}
if (!Array.isArray(input.tools) || input.tools.length === 0) {
issues.push({ code: "invalid-policy", field: "tools", message: "sandbox_tool_policy.tools must be a non-empty array." })
}
const seen = new Set<string>()
for (const [index, tool] of (Array.isArray(input.tools) ? input.tools : []).entries()) {
const field = `tools[${index}]`
if (!isPlainObject(tool)) {
issues.push({ code: "invalid-tool", field, message: `${field} must be an object.` })
continue
}
const id = typeof tool.id === "string" ? tool.id.trim() : ""
const runtimeToolId = typeof tool.runtime_tool_id === "string" ? tool.runtime_tool_id.trim() : ""
const executionLocation = typeof tool.execution_location === "string" ? tool.execution_location.trim() : ""
const runtime = isPlainObject(tool.runtime) ? tool.runtime : {}
const runtimeEnvironment = typeof runtime[AGENTS_API_RUNTIME_ENVIRONMENT] === "string" ? runtime[AGENTS_API_RUNTIME_ENVIRONMENT].trim() : ""
const runtimeCapabilityScope = typeof runtime[AGENTS_API_RUNTIME_CAPABILITY_SCOPE] === "string" ? runtime[AGENTS_API_RUNTIME_CAPABILITY_SCOPE].trim() : ""
if (!id) {
issues.push({ code: "invalid-tool", field: `${field}.id`, message: `${field}.id must be a non-empty string.` })
} else if (seen.has(id)) {
issues.push({ code: "duplicate-tool", field: `${field}.id`, message: `Duplicate sandbox tool policy id: ${id}.` })
}
seen.add(id)
if (!runtimeToolId) {
issues.push({ code: "invalid-tool", field: `${field}.runtime_tool_id`, message: `${field}.runtime_tool_id must be a non-empty string.` })
}
if (executionLocation !== "sandbox" && executionLocation !== "parent") {
issues.push({ code: "invalid-tool", field: `${field}.execution_location`, message: `${field}.execution_location must be sandbox or parent.` })
}
const canonicalRuntime = runtimeMetadataForExecutionLocation(executionLocation)
if (!runtimeEnvironment) {
issues.push({ code: "invalid-tool", field: `${field}.runtime.environment`, message: `${field}.runtime.environment must be a non-empty string.` })
} else if (canonicalRuntime && runtimeEnvironment !== canonicalRuntime.environment) {
issues.push({ code: "invalid-tool", field: `${field}.runtime.environment`, message: `${field}.runtime.environment must be ${canonicalRuntime.environment} for ${executionLocation} tools.` })
}
if (!runtimeCapabilityScope) {
issues.push({ code: "invalid-tool", field: `${field}.runtime.capability_scope`, message: `${field}.runtime.capability_scope must be a non-empty string.` })
} else if (canonicalRuntime && runtimeCapabilityScope !== canonicalRuntime.capability_scope) {
issues.push({ code: "invalid-tool", field: `${field}.runtime.capability_scope`, message: `${field}.runtime.capability_scope must be ${canonicalRuntime.capability_scope} for ${executionLocation} tools.` })
}
if (typeof tool.allowed !== "boolean") {
issues.push({ code: "invalid-tool", field: `${field}.allowed`, message: `${field}.allowed must be boolean.` })
}
}
return { valid: issues.length === 0, issues }
}
export function assertSandboxToolPolicySnapshot(input: unknown): asserts input is SandboxToolPolicySnapshot {
const result = validateSandboxToolPolicySnapshot(input)
if (!result.valid) {
throw new SandboxToolPolicyValidationError(result.issues)
}
}
export function sandboxAllowedRuntimeToolIds(policy: SandboxToolPolicySnapshot): string[] {
return resolveEffectiveRuntimeToolPolicy(policy).allowedRuntimeToolIds
}
export function runtimeToolInputFromSandboxToolPolicy(policy: SandboxToolPolicySnapshot): RuntimeToolInput {
const effective = resolveEffectiveRuntimeToolPolicy(policy)
return {
allowed_tools: effective.allowedRuntimeToolIds,
runtime_tools: effective.tools.filter((tool) => tool.visible),
}
}
export function sandboxToolPolicyFromAllowedTools(allowedTools: string[], metadata: Record<string, unknown> = {}): SandboxToolPolicySnapshot {
return {
schema: SANDBOX_TOOL_POLICY_SCHEMA,
version: SANDBOX_TOOL_POLICY_VERSION,
tools: stringList(allowedTools).map((tool) => ({
id: tool,
runtime_tool_id: providerSafeRuntimeToolId(tool),
execution_location: "sandbox",
transport_visibility: "sandbox",
allowed: true,
runtime: {
[AGENTS_API_RUNTIME_ENVIRONMENT]: AGENTS_API_RUNTIME_LOCAL,
[AGENTS_API_RUNTIME_CAPABILITY_SCOPE]: AGENTS_API_RUNTIME_LOCAL,
},
})),
metadata,
}
}
export function toolBridgeFromSandboxToolPolicy(policy: SandboxToolPolicySnapshot, allowedTools: string[] = []): ToolBridgeContract {
return {
schema: TOOL_BRIDGE_SCHEMA,
version: TOOL_BRIDGE_VERSION,
allowed_tools: stringList(allowedTools.length > 0 ? allowedTools : policy.tools.map((tool) => tool.id)),
sandbox_tool_policy: policy,
dispatcher: {
owner: "wp-codebox",
callback: "wp_codebox_browser_runtime_tool_callback",
location: "sandbox",
},
authorization: {
mode: "allowlist",
notes: "Only sandbox-visible tools in sandbox_tool_policy are exposed to the runtime agent. Parent control-plane actions remain outside the sandbox bridge.",
},
redaction: {
notes: "Secret values are passed through environment allowlists only and must not be embedded in tool bridge payloads, logs, or dispatcher metadata.",
},
}
}
export function resolveEffectiveRuntimeToolPolicy(policy: SandboxToolPolicySnapshot): EffectiveRuntimeToolPolicy {
const tools = policy.tools.map(runtimeToolDescriptor)
return {
schema: policy.schema,
version: policy.version,
tools,
allowedRuntimeToolIds: stringList(tools.filter((tool) => tool.allowed && tool.visible).map((tool) => tool.runtimeToolId)),
visibleRuntimeToolIds: stringList(tools.filter((tool) => tool.visible).map((tool) => tool.runtimeToolId)),
parentOnlyRuntimeToolIds: stringList(tools.filter((tool) => tool.parentOnly).map((tool) => tool.runtimeToolId)),
hiddenRuntimeToolIds: stringList(tools.filter((tool) => tool.hidden).map((tool) => tool.runtimeToolId)),
metadata: policy.metadata,
}
}
export function resolveRuntimeToolAlias(policy: EffectiveRuntimeToolPolicy | SandboxToolPolicySnapshot, value: string): RuntimeToolDescriptor | undefined {
const trimmed = value.trim()
if (!trimmed) return undefined
const effective = "allowedRuntimeToolIds" in policy ? policy : resolveEffectiveRuntimeToolPolicy(policy)
return effective.tools.find((tool) => tool.id === trimmed || tool.runtimeToolId === trimmed || tool.aliases.includes(trimmed))
}
export function sandboxToolRuntimeMetadata(tool: SandboxToolPolicyTool): Required<Pick<AgentsApiRuntimeToolMetadata, typeof AGENTS_API_RUNTIME_ENVIRONMENT | typeof AGENTS_API_RUNTIME_CAPABILITY_SCOPE>> {
return runtimeMetadataForExecutionLocation(tool.execution_location) ?? {
environment: "",
capability_scope: "",
}
}
export function runtimeMetadataForExecutionLocation(executionLocation: SandboxToolExecutionLocation): Required<Pick<AgentsApiRuntimeToolMetadata, typeof AGENTS_API_RUNTIME_ENVIRONMENT | typeof AGENTS_API_RUNTIME_CAPABILITY_SCOPE>> | undefined {
if (executionLocation === "sandbox") {
return {
environment: AGENTS_API_RUNTIME_LOCAL,
capability_scope: AGENTS_API_RUNTIME_LOCAL,
}
}
if (executionLocation === "parent") {
return {
environment: AGENTS_API_CONTROL_PLANE,
capability_scope: AGENTS_API_CONTROL_PLANE,
}
}
return undefined
}
function runtimeToolDescriptor(tool: SandboxToolPolicyTool): RuntimeToolDescriptor {
const runtime = sandboxToolRuntimeMetadata(tool)
const aliases = stringList([
tool.id,
tool.runtime_tool_id,
...stringList(tool.aliases),
...stringList(isPlainObject(tool.metadata) ? tool.metadata.aliases : undefined),
])
const parentOnly = runtime.environment !== AGENTS_API_RUNTIME_LOCAL || runtime.capability_scope !== AGENTS_API_RUNTIME_LOCAL
const hidden = tool.transport_visibility === "hidden"
const visible = !parentOnly && !hidden && (tool.transport_visibility === "sandbox" || tool.transport_visibility === "both")
const schema = typeof tool.metadata?.schema === "string" ? tool.metadata.schema : undefined
const policy = isPlainObject(tool.metadata?.policy) ? tool.metadata.policy : undefined
return {
id: tool.id,
runtimeToolId: tool.runtime_tool_id,
aliases,
allowed: tool.allowed,
executionLocation: tool.execution_location,
transportVisibility: tool.transport_visibility,
visible,
parentOnly,
hidden,
runtime,
...(schema ? { schema } : {}),
...(policy ? { policy } : {}),
...(tool.metadata ? { metadata: tool.metadata } : {}),
}
}
function providerSafeRuntimeToolId(toolId: string): string {
return toolId.replace(/[^A-Za-z0-9_]+/g, "_").replace(/^_+|_+$/g, "")
}