Repository navigation
Expand file tree
/
Copy pathpermissions.js
More file actions
104 lines (95 loc) · 5.87 KB
/
Copy pathpermissions.js
File metadata and controls
104 lines (95 loc) · 5.87 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
// permissions.js — pure trading-permission policy (no network), shared by the MCP gate and tests.
// enforcePermissions() in mcp-server.js fetches `perms` from the agent server, then delegates
// the actual policy decision here so it can be unit-tested without a running server.
export const ORDER_TOOLS = ['place_buy_order', 'place_sell_order', 'place_options_order', 'place_managed_position', 'close_managed_position'];
// US equity options are 100-share contracts, so an options order is worth premium × 100 × contracts.
export const OPTION_CONTRACT_MULTIPLIER = 100;
// OCC option symbol: ROOT (≤6) + YYMMDD + C/P + 8-digit strike, e.g. SPY251219C00680000.
const OCC_SYMBOL = /^[A-Z.]{1,6}\d{6}[CP]\d{8}$/;
export function isOptionSymbol(symbol) {
return typeof symbol === 'string' && OCC_SYMBOL.test(symbol);
}
// Guardrails the agent may only tighten through update_permissions. Loosening one (enabling a
// trading type, dropping confirmation, raising or removing a cap, unblocking a tool) is left to
// the operator in the dashboard: text in the agent's context can ask it to loosen its own rules,
// and the agent cannot tell that text from the operator.
const LOOSEN_IF_TRUE = ['allowLiveTrading', 'allowOptions', 'allowStocks', 'allow0DTE'];
const CAPS = ['maxPositionPct', 'maxDeployedPct', 'maxDailyLoss', 'maxOpenPositions', 'maxOrderValue']; // 0 = no cap
function tightensCap(value, current) {
return typeof value === 'number' && value > 0 && (!(current > 0) || value <= current);
}
// Throws an Error naming every field of `update` that would loosen `current` (or that the agent
// may not edit at all); returns undefined if the update only tightens or repeats current values.
export function checkPermissionUpdate(update = {}, current = {}) {
const refused = [];
for (const [field, value] of Object.entries(update)) {
if (JSON.stringify(value) === JSON.stringify(current[field])) continue;
if (LOOSEN_IF_TRUE.includes(field) && value === false) continue;
if (field === 'requireConfirmation' && value === true) continue;
if (CAPS.includes(field) && tightensCap(value, current[field])) continue;
if (field === 'blockedTools' && Array.isArray(value) && (current.blockedTools || []).every(t => value.includes(t))) continue;
refused.push(field);
}
if (refused.length) {
throw new Error(`Permission change refused for ${refused.join(', ')}: the agent can only tighten guardrails. Ask the operator to loosen them in the dashboard.`);
}
}
// Throws an Error describing the violation if the call is not permitted; returns undefined if allowed.
// `now` is injectable so the 0DTE (same-day expiry) rule is deterministic in tests.
// `args.estimated_price` is an optional quote the MCP server attaches to market orders so
// maxOrderValue can value them; it is never forwarded to the broker.
export function checkPermissions(toolName, args = {}, perms = {}, now = new Date()) {
// Blocked tools
if (perms.blockedTools?.length && perms.blockedTools.includes(toolName)) {
throw new Error(`Tool "${toolName}" is blocked by permissions. Blocked tools: ${perms.blockedTools.join(', ')}`);
}
// Everything below is order-specific
if (!ORDER_TOOLS.includes(toolName)) return;
// Live trading disabled
if (!perms.allowLiveTrading) {
throw new Error('Live trading is DISABLED (read-only mode). Cannot place orders. Change permissions to enable.');
}
// Options check
if (!perms.allowOptions && (toolName === 'place_options_order' || (args.symbol && args.symbol.length > 10))) {
throw new Error('Options trading is DISABLED by permissions.');
}
// Stock check
if (!perms.allowStocks && (toolName === 'place_buy_order' || toolName === 'place_sell_order')) {
throw new Error('Stock trading is DISABLED by permissions.');
}
// 0DTE check for options — OCC format: SYMBOL + YYMMDD + C/P + strike
if (!perms.allow0DTE && toolName === 'place_options_order' && args.symbol) {
const match = args.symbol.match(/(\d{6})[CP]/);
if (match) {
const expStr = match[1]; // YYMMDD
// Build the expiry as a LOCAL calendar date. `new Date('YYYY-MM-DD')` parses as UTC
// midnight, which in any US timezone is the previous evening — that let same-day
// expiries through and flagged next-day ones instead.
const expDate = new Date(2000 + Number(expStr.slice(0, 2)), Number(expStr.slice(2, 4)) - 1, Number(expStr.slice(4, 6)));
const today = new Date(now);
today.setHours(0, 0, 0, 0);
if (expDate.getTime() === today.getTime()) {
throw new Error('0DTE options are NOT allowed by permissions.');
}
}
}
// Require confirmation
if (perms.requireConfirmation) {
throw new Error('Order requires operator confirmation (requireConfirmation is enabled). Tell the operator what you want to do and wait for them to disable this setting or approve via the dashboard.');
}
// Max order value. Closing a managed position is an exit (risk-reducing, no size argument), so
// it is exempt; every other order must be valued or it fails closed.
if (perms.maxOrderValue > 0 && toolName !== 'close_managed_position') {
const quantity = Number(args.quantity ?? args.qty ?? 0);
const unitPrice = Number(args.limit_price ?? args.entry_price ?? args.estimated_price ?? 0);
const multiplier = (toolName === 'place_options_order' || isOptionSymbol(args.symbol)) ? OPTION_CONTRACT_MULTIPLIER : 1;
const allocValue = Number(args.allocation_dollars || 0);
const checkValue = allocValue || unitPrice * quantity * multiplier;
if (!(checkValue > 0)) {
throw new Error('maxOrderValue is set but this order carries no price to value it against (market order with no quote available). Use a limit order with limit_price, or retry.');
}
if (checkValue > perms.maxOrderValue) {
throw new Error(`Order value $${checkValue.toFixed(2)} exceeds max allowed $${perms.maxOrderValue}. Reduce size or change permissions.`);
}
}
}