@enclave-vm/core 0.0.1
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/LICENSE +201 -0
- package/README.md +732 -0
- package/SECURITY-AUDIT.md +916 -0
- package/package.json +59 -0
- package/src/adapters/vm-adapter.d.ts +32 -0
- package/src/adapters/vm-adapter.js +1140 -0
- package/src/adapters/vm-adapter.js.map +1 -0
- package/src/adapters/worker-pool/config.d.ts +149 -0
- package/src/adapters/worker-pool/config.js +129 -0
- package/src/adapters/worker-pool/config.js.map +1 -0
- package/src/adapters/worker-pool/errors.d.ts +109 -0
- package/src/adapters/worker-pool/errors.js +181 -0
- package/src/adapters/worker-pool/errors.js.map +1 -0
- package/src/adapters/worker-pool/execution-queue.d.ts +85 -0
- package/src/adapters/worker-pool/execution-queue.js +179 -0
- package/src/adapters/worker-pool/execution-queue.js.map +1 -0
- package/src/adapters/worker-pool/index.d.ts +48 -0
- package/src/adapters/worker-pool/index.js +73 -0
- package/src/adapters/worker-pool/index.js.map +1 -0
- package/src/adapters/worker-pool/memory-monitor.d.ts +106 -0
- package/src/adapters/worker-pool/memory-monitor.js +184 -0
- package/src/adapters/worker-pool/memory-monitor.js.map +1 -0
- package/src/adapters/worker-pool/protocol.d.ts +314 -0
- package/src/adapters/worker-pool/protocol.js +143 -0
- package/src/adapters/worker-pool/protocol.js.map +1 -0
- package/src/adapters/worker-pool/rate-limiter.d.ts +60 -0
- package/src/adapters/worker-pool/rate-limiter.js +101 -0
- package/src/adapters/worker-pool/rate-limiter.js.map +1 -0
- package/src/adapters/worker-pool/safe-deserialize.d.ts +50 -0
- package/src/adapters/worker-pool/safe-deserialize.js +141 -0
- package/src/adapters/worker-pool/safe-deserialize.js.map +1 -0
- package/src/adapters/worker-pool/worker-pool-adapter.d.ts +84 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js +506 -0
- package/src/adapters/worker-pool/worker-pool-adapter.js.map +1 -0
- package/src/adapters/worker-pool/worker-script.d.ts +12 -0
- package/src/adapters/worker-pool/worker-script.js +770 -0
- package/src/adapters/worker-pool/worker-script.js.map +1 -0
- package/src/adapters/worker-pool/worker-slot.d.ts +116 -0
- package/src/adapters/worker-pool/worker-slot.js +301 -0
- package/src/adapters/worker-pool/worker-slot.js.map +1 -0
- package/src/babel/babel-wrapper.d.ts +118 -0
- package/src/babel/babel-wrapper.js +395 -0
- package/src/babel/babel-wrapper.js.map +1 -0
- package/src/babel/index.d.ts +9 -0
- package/src/babel/index.js +17 -0
- package/src/babel/index.js.map +1 -0
- package/src/babel/multi-file-transform.d.ts +122 -0
- package/src/babel/multi-file-transform.js +344 -0
- package/src/babel/multi-file-transform.js.map +1 -0
- package/src/double-vm/double-vm-wrapper.d.ts +65 -0
- package/src/double-vm/double-vm-wrapper.js +523 -0
- package/src/double-vm/double-vm-wrapper.js.map +1 -0
- package/src/double-vm/index.d.ts +31 -0
- package/src/double-vm/index.js +34 -0
- package/src/double-vm/index.js.map +1 -0
- package/src/double-vm/parent-vm-bootstrap.d.ts +63 -0
- package/src/double-vm/parent-vm-bootstrap.js +1695 -0
- package/src/double-vm/parent-vm-bootstrap.js.map +1 -0
- package/src/double-vm/suspicious-patterns.d.ts +57 -0
- package/src/double-vm/suspicious-patterns.js +304 -0
- package/src/double-vm/suspicious-patterns.js.map +1 -0
- package/src/double-vm/types.d.ts +178 -0
- package/src/double-vm/types.js +11 -0
- package/src/double-vm/types.js.map +1 -0
- package/src/double-vm/wrap-adapter.d.ts +36 -0
- package/src/double-vm/wrap-adapter.js +59 -0
- package/src/double-vm/wrap-adapter.js.map +1 -0
- package/src/enclave.d.ts +159 -0
- package/src/enclave.js +671 -0
- package/src/enclave.js.map +1 -0
- package/src/globals-validator.d.ts +80 -0
- package/src/globals-validator.js +206 -0
- package/src/globals-validator.js.map +1 -0
- package/src/index.d.ts +29 -0
- package/src/index.js +130 -0
- package/src/index.js.map +1 -0
- package/src/memory-proxy.d.ts +69 -0
- package/src/memory-proxy.js +310 -0
- package/src/memory-proxy.js.map +1 -0
- package/src/memory-tracker.d.ts +170 -0
- package/src/memory-tracker.js +230 -0
- package/src/memory-tracker.js.map +1 -0
- package/src/safe-error.d.ts +18 -0
- package/src/safe-error.js +76 -0
- package/src/safe-error.js.map +1 -0
- package/src/safe-runtime.d.ts +72 -0
- package/src/safe-runtime.js +656 -0
- package/src/safe-runtime.js.map +1 -0
- package/src/scoring/cache.d.ts +68 -0
- package/src/scoring/cache.js +158 -0
- package/src/scoring/cache.js.map +1 -0
- package/src/scoring/feature-extractor.d.ts +61 -0
- package/src/scoring/feature-extractor.js +366 -0
- package/src/scoring/feature-extractor.js.map +1 -0
- package/src/scoring/index.d.ts +33 -0
- package/src/scoring/index.js +53 -0
- package/src/scoring/index.js.map +1 -0
- package/src/scoring/scorer.interface.d.ts +65 -0
- package/src/scoring/scorer.interface.js +49 -0
- package/src/scoring/scorer.interface.js.map +1 -0
- package/src/scoring/scorers/disabled.scorer.d.ts +19 -0
- package/src/scoring/scorers/disabled.scorer.js +34 -0
- package/src/scoring/scorers/disabled.scorer.js.map +1 -0
- package/src/scoring/scorers/external-api.scorer.d.ts +29 -0
- package/src/scoring/scorers/external-api.scorer.js +141 -0
- package/src/scoring/scorers/external-api.scorer.js.map +1 -0
- package/src/scoring/scorers/index.d.ts +10 -0
- package/src/scoring/scorers/index.js +22 -0
- package/src/scoring/scorers/index.js.map +1 -0
- package/src/scoring/scorers/local-llm.scorer.d.ts +116 -0
- package/src/scoring/scorers/local-llm.scorer.js +474 -0
- package/src/scoring/scorers/local-llm.scorer.js.map +1 -0
- package/src/scoring/scorers/progressive.scorer.d.ts +77 -0
- package/src/scoring/scorers/progressive.scorer.js +199 -0
- package/src/scoring/scorers/progressive.scorer.js.map +1 -0
- package/src/scoring/scorers/rule-based.scorer.d.ts +66 -0
- package/src/scoring/scorers/rule-based.scorer.js +316 -0
- package/src/scoring/scorers/rule-based.scorer.js.map +1 -0
- package/src/scoring/scoring-gate.d.ts +76 -0
- package/src/scoring/scoring-gate.js +246 -0
- package/src/scoring/scoring-gate.js.map +1 -0
- package/src/scoring/types.d.ts +690 -0
- package/src/scoring/types.js +107 -0
- package/src/scoring/types.js.map +1 -0
- package/src/secure-proxy.d.ts +157 -0
- package/src/secure-proxy.js +609 -0
- package/src/secure-proxy.js.map +1 -0
- package/src/session/channels/embedded-channel.d.ts +76 -0
- package/src/session/channels/embedded-channel.js +130 -0
- package/src/session/channels/embedded-channel.js.map +1 -0
- package/src/session/channels/index.d.ts +9 -0
- package/src/session/channels/index.js +14 -0
- package/src/session/channels/index.js.map +1 -0
- package/src/session/index.d.ts +15 -0
- package/src/session/index.js +27 -0
- package/src/session/index.js.map +1 -0
- package/src/session/session-emitter.d.ts +119 -0
- package/src/session/session-emitter.js +253 -0
- package/src/session/session-emitter.js.map +1 -0
- package/src/session/session-state-machine.d.ts +108 -0
- package/src/session/session-state-machine.js +182 -0
- package/src/session/session-state-machine.js.map +1 -0
- package/src/session/session.d.ts +135 -0
- package/src/session/session.js +398 -0
- package/src/session/session.js.map +1 -0
- package/src/session-types.d.ts +313 -0
- package/src/session-types.js +39 -0
- package/src/session-types.js.map +1 -0
- package/src/sidecar/index.d.ts +12 -0
- package/src/sidecar/index.js +31 -0
- package/src/sidecar/index.js.map +1 -0
- package/src/sidecar/reference-config.d.ts +81 -0
- package/src/sidecar/reference-config.js +118 -0
- package/src/sidecar/reference-config.js.map +1 -0
- package/src/sidecar/reference-resolver.d.ts +115 -0
- package/src/sidecar/reference-resolver.js +262 -0
- package/src/sidecar/reference-resolver.js.map +1 -0
- package/src/sidecar/reference-sidecar.d.ts +161 -0
- package/src/sidecar/reference-sidecar.js +226 -0
- package/src/sidecar/reference-sidecar.js.map +1 -0
- package/src/tool-bridge.d.ts +4 -0
- package/src/tool-bridge.js +306 -0
- package/src/tool-bridge.js.map +1 -0
- package/src/types.d.ts +775 -0
- package/src/types.js +187 -0
- package/src/types.js.map +1 -0
- package/src/value-sanitizer.d.ts +133 -0
- package/src/value-sanitizer.js +421 -0
- package/src/value-sanitizer.js.map +1 -0
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Types for Streaming Runtime
|
|
3
|
+
*
|
|
4
|
+
* Types for running Enclave in session mode, which supports:
|
|
5
|
+
* - Continuous execution with streaming events
|
|
6
|
+
* - Async tool calls with "waiting for tool" state
|
|
7
|
+
* - Session lifecycle management
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
import type { SessionId, CallId, SessionLimits, StreamEvent, RuntimeChannelMessage } from '@enclave-vm/types';
|
|
12
|
+
import type { CreateEnclaveOptions, ExecutionStats } from './types';
|
|
13
|
+
/**
|
|
14
|
+
* Session state machine states
|
|
15
|
+
*
|
|
16
|
+
* The session progresses through these states:
|
|
17
|
+
* - starting: Session created, runtime initializing
|
|
18
|
+
* - running: Code is executing
|
|
19
|
+
* - waiting_for_tool: Blocked on async tool call
|
|
20
|
+
* - completed: Execution finished successfully
|
|
21
|
+
* - cancelled: Session was cancelled externally
|
|
22
|
+
* - failed: Execution failed with error
|
|
23
|
+
*/
|
|
24
|
+
export type SessionStateValue = 'starting' | 'running' | 'waiting_for_tool' | 'completed' | 'cancelled' | 'failed';
|
|
25
|
+
/**
|
|
26
|
+
* Session configuration
|
|
27
|
+
*
|
|
28
|
+
* Extends standard Enclave options with session-specific settings.
|
|
29
|
+
*/
|
|
30
|
+
export interface SessionConfig extends CreateEnclaveOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Session resource limits
|
|
33
|
+
* Overrides individual config values when provided
|
|
34
|
+
*/
|
|
35
|
+
limits?: Partial<SessionLimits>;
|
|
36
|
+
/**
|
|
37
|
+
* Heartbeat interval in milliseconds
|
|
38
|
+
* Set to 0 to disable heartbeats
|
|
39
|
+
* @default 15000
|
|
40
|
+
*/
|
|
41
|
+
heartbeatIntervalMs?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Enable encryption for streaming events
|
|
44
|
+
* @default false
|
|
45
|
+
*/
|
|
46
|
+
encryption?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Pending tool call information
|
|
50
|
+
*
|
|
51
|
+
* Represents a tool call that the runtime is waiting for.
|
|
52
|
+
*/
|
|
53
|
+
export interface PendingToolCall {
|
|
54
|
+
/**
|
|
55
|
+
* Unique call identifier
|
|
56
|
+
*/
|
|
57
|
+
callId: CallId;
|
|
58
|
+
/**
|
|
59
|
+
* Name of the tool being called
|
|
60
|
+
*/
|
|
61
|
+
toolName: string;
|
|
62
|
+
/**
|
|
63
|
+
* Arguments passed to the tool (may contain refs)
|
|
64
|
+
*/
|
|
65
|
+
args: Record<string, unknown>;
|
|
66
|
+
/**
|
|
67
|
+
* Timestamp when the call was initiated
|
|
68
|
+
*/
|
|
69
|
+
timestamp: number;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Async tool handler for session mode
|
|
73
|
+
*
|
|
74
|
+
* Unlike the synchronous ToolHandler, this returns void and the result
|
|
75
|
+
* is submitted separately via the session's submitToolResult method.
|
|
76
|
+
* This allows for long-running tool calls that don't block the session.
|
|
77
|
+
*/
|
|
78
|
+
export type AsyncToolHandler = (callId: CallId, toolName: string, args: Record<string, unknown>) => void | Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Tool result to submit back to the session
|
|
81
|
+
*/
|
|
82
|
+
export interface ToolResult {
|
|
83
|
+
/**
|
|
84
|
+
* The call ID this result is for
|
|
85
|
+
*/
|
|
86
|
+
callId: CallId;
|
|
87
|
+
/**
|
|
88
|
+
* Whether the tool call succeeded
|
|
89
|
+
*/
|
|
90
|
+
success: boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Result value (if successful)
|
|
93
|
+
*/
|
|
94
|
+
value?: unknown;
|
|
95
|
+
/**
|
|
96
|
+
* Error information (if failed)
|
|
97
|
+
*/
|
|
98
|
+
error?: {
|
|
99
|
+
message: string;
|
|
100
|
+
code?: string;
|
|
101
|
+
data?: Record<string, unknown>;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Session event emitter interface
|
|
106
|
+
*
|
|
107
|
+
* Provides typed event emission for session lifecycle.
|
|
108
|
+
*/
|
|
109
|
+
export interface SessionEventEmitter {
|
|
110
|
+
/**
|
|
111
|
+
* Emit a stream event
|
|
112
|
+
*/
|
|
113
|
+
emit(event: StreamEvent): void;
|
|
114
|
+
/**
|
|
115
|
+
* Subscribe to stream events
|
|
116
|
+
*/
|
|
117
|
+
on(handler: (event: StreamEvent) => void): () => void;
|
|
118
|
+
/**
|
|
119
|
+
* Get all emitted events (for testing/debugging)
|
|
120
|
+
*/
|
|
121
|
+
getEmittedEvents(): StreamEvent[];
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Session instance interface
|
|
125
|
+
*
|
|
126
|
+
* Represents an active execution session that can receive tool results
|
|
127
|
+
* and emit streaming events.
|
|
128
|
+
*/
|
|
129
|
+
export interface Session {
|
|
130
|
+
/**
|
|
131
|
+
* Unique session identifier
|
|
132
|
+
*/
|
|
133
|
+
readonly sessionId: SessionId;
|
|
134
|
+
/**
|
|
135
|
+
* Current session state
|
|
136
|
+
*/
|
|
137
|
+
readonly state: SessionStateValue;
|
|
138
|
+
/**
|
|
139
|
+
* Current sequence number for events
|
|
140
|
+
*/
|
|
141
|
+
readonly seq: number;
|
|
142
|
+
/**
|
|
143
|
+
* Pending tool call (when in waiting_for_tool state)
|
|
144
|
+
*/
|
|
145
|
+
readonly pendingToolCall: PendingToolCall | null;
|
|
146
|
+
/**
|
|
147
|
+
* Session creation timestamp
|
|
148
|
+
*/
|
|
149
|
+
readonly createdAt: number;
|
|
150
|
+
/**
|
|
151
|
+
* Session expiration timestamp
|
|
152
|
+
*/
|
|
153
|
+
readonly expiresAt: number;
|
|
154
|
+
/**
|
|
155
|
+
* Event emitter for this session
|
|
156
|
+
*/
|
|
157
|
+
readonly events: SessionEventEmitter;
|
|
158
|
+
/**
|
|
159
|
+
* Submit a tool result to resume execution
|
|
160
|
+
*
|
|
161
|
+
* @param result - The tool result to submit
|
|
162
|
+
* @throws Error if session is not in waiting_for_tool state
|
|
163
|
+
* @throws Error if result callId doesn't match pending call
|
|
164
|
+
*/
|
|
165
|
+
submitToolResult(result: ToolResult): Promise<void>;
|
|
166
|
+
/**
|
|
167
|
+
* Cancel the session
|
|
168
|
+
*
|
|
169
|
+
* @param reason - Optional cancellation reason
|
|
170
|
+
*/
|
|
171
|
+
cancel(reason?: string): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Wait for the session to complete
|
|
174
|
+
*
|
|
175
|
+
* @returns Final execution result
|
|
176
|
+
*/
|
|
177
|
+
wait(): Promise<SessionFinalResult>;
|
|
178
|
+
/**
|
|
179
|
+
* Get current execution statistics
|
|
180
|
+
*/
|
|
181
|
+
getStats(): ExecutionStats;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Final result when session completes
|
|
185
|
+
*/
|
|
186
|
+
export interface SessionFinalResult {
|
|
187
|
+
/**
|
|
188
|
+
* Whether execution was successful
|
|
189
|
+
*/
|
|
190
|
+
success: boolean;
|
|
191
|
+
/**
|
|
192
|
+
* Return value from the script (if successful)
|
|
193
|
+
*/
|
|
194
|
+
value?: unknown;
|
|
195
|
+
/**
|
|
196
|
+
* Error that occurred (if failed)
|
|
197
|
+
*/
|
|
198
|
+
error?: {
|
|
199
|
+
message: string;
|
|
200
|
+
name: string;
|
|
201
|
+
code?: string;
|
|
202
|
+
data?: Record<string, unknown>;
|
|
203
|
+
};
|
|
204
|
+
/**
|
|
205
|
+
* Execution statistics
|
|
206
|
+
*/
|
|
207
|
+
stats: ExecutionStats;
|
|
208
|
+
/**
|
|
209
|
+
* Final session state
|
|
210
|
+
*/
|
|
211
|
+
finalState: 'completed' | 'cancelled' | 'failed';
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Runtime channel abstraction
|
|
215
|
+
*
|
|
216
|
+
* Provides bidirectional communication between the broker and runtime.
|
|
217
|
+
* Different implementations support embedded (in-process) and remote
|
|
218
|
+
* (WebSocket) runtime execution.
|
|
219
|
+
*/
|
|
220
|
+
export interface RuntimeChannel {
|
|
221
|
+
/**
|
|
222
|
+
* Send a message to the runtime
|
|
223
|
+
*/
|
|
224
|
+
send(message: RuntimeChannelMessage): void;
|
|
225
|
+
/**
|
|
226
|
+
* Subscribe to messages from the runtime
|
|
227
|
+
*/
|
|
228
|
+
onMessage(handler: (event: StreamEvent) => void): () => void;
|
|
229
|
+
/**
|
|
230
|
+
* Close the channel
|
|
231
|
+
*/
|
|
232
|
+
close(): void;
|
|
233
|
+
/**
|
|
234
|
+
* Whether the channel is open
|
|
235
|
+
*/
|
|
236
|
+
readonly isOpen: boolean;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Embedded runtime channel configuration
|
|
240
|
+
*
|
|
241
|
+
* For in-process execution where the Enclave runs in the same process.
|
|
242
|
+
*/
|
|
243
|
+
export interface EmbeddedChannelConfig {
|
|
244
|
+
type: 'embedded';
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* WebSocket runtime channel configuration
|
|
248
|
+
*
|
|
249
|
+
* For remote execution where the runtime is in a separate process/worker.
|
|
250
|
+
*/
|
|
251
|
+
export interface WebSocketChannelConfig {
|
|
252
|
+
type: 'websocket';
|
|
253
|
+
/**
|
|
254
|
+
* WebSocket URL to connect to
|
|
255
|
+
*/
|
|
256
|
+
url: string;
|
|
257
|
+
/**
|
|
258
|
+
* Reconnection options
|
|
259
|
+
*/
|
|
260
|
+
reconnect?: {
|
|
261
|
+
maxRetries?: number;
|
|
262
|
+
initialDelayMs?: number;
|
|
263
|
+
maxDelayMs?: number;
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Runtime channel configuration union
|
|
268
|
+
*/
|
|
269
|
+
export type RuntimeChannelConfig = EmbeddedChannelConfig | WebSocketChannelConfig;
|
|
270
|
+
/**
|
|
271
|
+
* Session manager interface
|
|
272
|
+
*
|
|
273
|
+
* Manages multiple concurrent sessions.
|
|
274
|
+
*/
|
|
275
|
+
export interface SessionManager {
|
|
276
|
+
/**
|
|
277
|
+
* Create a new session
|
|
278
|
+
*
|
|
279
|
+
* @param code - Code to execute
|
|
280
|
+
* @param config - Session configuration
|
|
281
|
+
* @param toolHandler - Handler for tool calls
|
|
282
|
+
* @returns Created session
|
|
283
|
+
*/
|
|
284
|
+
createSession(code: string, config?: SessionConfig, toolHandler?: AsyncToolHandler): Promise<Session>;
|
|
285
|
+
/**
|
|
286
|
+
* Get an existing session by ID
|
|
287
|
+
*/
|
|
288
|
+
getSession(sessionId: SessionId): Session | undefined;
|
|
289
|
+
/**
|
|
290
|
+
* List all active sessions
|
|
291
|
+
*/
|
|
292
|
+
listSessions(): Session[];
|
|
293
|
+
/**
|
|
294
|
+
* Terminate a session
|
|
295
|
+
*/
|
|
296
|
+
terminateSession(sessionId: SessionId, reason?: string): Promise<void>;
|
|
297
|
+
/**
|
|
298
|
+
* Dispose of the manager and all sessions
|
|
299
|
+
*/
|
|
300
|
+
dispose(): Promise<void>;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Type guard for checking if an object is a Session
|
|
304
|
+
*/
|
|
305
|
+
export declare function isSession(obj: unknown): obj is Session;
|
|
306
|
+
/**
|
|
307
|
+
* Type guard for waiting_for_tool state
|
|
308
|
+
*/
|
|
309
|
+
export declare function isWaitingForTool(session: Session): boolean;
|
|
310
|
+
/**
|
|
311
|
+
* Type guard for terminal states
|
|
312
|
+
*/
|
|
313
|
+
export declare function isTerminalState(state: SessionStateValue): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Session Types for Streaming Runtime
|
|
4
|
+
*
|
|
5
|
+
* Types for running Enclave in session mode, which supports:
|
|
6
|
+
* - Continuous execution with streaming events
|
|
7
|
+
* - Async tool calls with "waiting for tool" state
|
|
8
|
+
* - Session lifecycle management
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.isSession = isSession;
|
|
14
|
+
exports.isWaitingForTool = isWaitingForTool;
|
|
15
|
+
exports.isTerminalState = isTerminalState;
|
|
16
|
+
/**
|
|
17
|
+
* Type guard for checking if an object is a Session
|
|
18
|
+
*/
|
|
19
|
+
function isSession(obj) {
|
|
20
|
+
return (typeof obj === 'object' &&
|
|
21
|
+
obj !== null &&
|
|
22
|
+
'sessionId' in obj &&
|
|
23
|
+
'state' in obj &&
|
|
24
|
+
'submitToolResult' in obj &&
|
|
25
|
+
typeof obj.submitToolResult === 'function');
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Type guard for waiting_for_tool state
|
|
29
|
+
*/
|
|
30
|
+
function isWaitingForTool(session) {
|
|
31
|
+
return session.state === 'waiting_for_tool' && session.pendingToolCall !== null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Type guard for terminal states
|
|
35
|
+
*/
|
|
36
|
+
function isTerminalState(state) {
|
|
37
|
+
return state === 'completed' || state === 'cancelled' || state === 'failed';
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=session-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-types.js","sourceRoot":"","sources":["../../src/session-types.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;AAkWH,8BASC;AAKD,4CAEC;AAKD,0CAEC;AA1BD;;GAEG;AACH,SAAgB,SAAS,CAAC,GAAY;IACpC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;QACvB,GAAG,KAAK,IAAI;QACZ,WAAW,IAAI,GAAG;QAClB,OAAO,IAAI,GAAG;QACd,kBAAkB,IAAI,GAAG;QACzB,OAAQ,GAAe,CAAC,gBAAgB,KAAK,UAAU,CACxD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,SAAgB,gBAAgB,CAAC,OAAgB;IAC/C,OAAO,OAAO,CAAC,KAAK,KAAK,kBAAkB,IAAI,OAAO,CAAC,eAAe,KAAK,IAAI,CAAC;AAClF,CAAC;AAED;;GAEG;AACH,SAAgB,eAAe,CAAC,KAAwB;IACtD,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,WAAW,IAAI,KAAK,KAAK,QAAQ,CAAC;AAC9E,CAAC","sourcesContent":["/**\n * Session Types for Streaming Runtime\n *\n * Types for running Enclave in session mode, which supports:\n * - Continuous execution with streaming events\n * - Async tool calls with \"waiting for tool\" state\n * - Session lifecycle management\n *\n * @packageDocumentation\n */\n\nimport type {\n SessionId,\n CallId,\n SessionLimits,\n StreamEvent,\n ToolCallPayload,\n RuntimeChannelMessage,\n} from '@enclave-vm/types';\nimport type { CreateEnclaveOptions, ExecutionStats } from './types';\n\n/**\n * Session state machine states\n *\n * The session progresses through these states:\n * - starting: Session created, runtime initializing\n * - running: Code is executing\n * - waiting_for_tool: Blocked on async tool call\n * - completed: Execution finished successfully\n * - cancelled: Session was cancelled externally\n * - failed: Execution failed with error\n */\nexport type SessionStateValue = 'starting' | 'running' | 'waiting_for_tool' | 'completed' | 'cancelled' | 'failed';\n\n/**\n * Session configuration\n *\n * Extends standard Enclave options with session-specific settings.\n */\nexport interface SessionConfig extends CreateEnclaveOptions {\n /**\n * Session resource limits\n * Overrides individual config values when provided\n */\n limits?: Partial<SessionLimits>;\n\n /**\n * Heartbeat interval in milliseconds\n * Set to 0 to disable heartbeats\n * @default 15000\n */\n heartbeatIntervalMs?: number;\n\n /**\n * Enable encryption for streaming events\n * @default false\n */\n encryption?: boolean;\n}\n\n/**\n * Pending tool call information\n *\n * Represents a tool call that the runtime is waiting for.\n */\nexport interface PendingToolCall {\n /**\n * Unique call identifier\n */\n callId: CallId;\n\n /**\n * Name of the tool being called\n */\n toolName: string;\n\n /**\n * Arguments passed to the tool (may contain refs)\n */\n args: Record<string, unknown>;\n\n /**\n * Timestamp when the call was initiated\n */\n timestamp: number;\n}\n\n/**\n * Async tool handler for session mode\n *\n * Unlike the synchronous ToolHandler, this returns void and the result\n * is submitted separately via the session's submitToolResult method.\n * This allows for long-running tool calls that don't block the session.\n */\nexport type AsyncToolHandler = (\n callId: CallId,\n toolName: string,\n args: Record<string, unknown>,\n) => void | Promise<void>;\n\n/**\n * Tool result to submit back to the session\n */\nexport interface ToolResult {\n /**\n * The call ID this result is for\n */\n callId: CallId;\n\n /**\n * Whether the tool call succeeded\n */\n success: boolean;\n\n /**\n * Result value (if successful)\n */\n value?: unknown;\n\n /**\n * Error information (if failed)\n */\n error?: {\n message: string;\n code?: string;\n data?: Record<string, unknown>;\n };\n}\n\n/**\n * Session event emitter interface\n *\n * Provides typed event emission for session lifecycle.\n */\nexport interface SessionEventEmitter {\n /**\n * Emit a stream event\n */\n emit(event: StreamEvent): void;\n\n /**\n * Subscribe to stream events\n */\n on(handler: (event: StreamEvent) => void): () => void;\n\n /**\n * Get all emitted events (for testing/debugging)\n */\n getEmittedEvents(): StreamEvent[];\n}\n\n/**\n * Session instance interface\n *\n * Represents an active execution session that can receive tool results\n * and emit streaming events.\n */\nexport interface Session {\n /**\n * Unique session identifier\n */\n readonly sessionId: SessionId;\n\n /**\n * Current session state\n */\n readonly state: SessionStateValue;\n\n /**\n * Current sequence number for events\n */\n readonly seq: number;\n\n /**\n * Pending tool call (when in waiting_for_tool state)\n */\n readonly pendingToolCall: PendingToolCall | null;\n\n /**\n * Session creation timestamp\n */\n readonly createdAt: number;\n\n /**\n * Session expiration timestamp\n */\n readonly expiresAt: number;\n\n /**\n * Event emitter for this session\n */\n readonly events: SessionEventEmitter;\n\n /**\n * Submit a tool result to resume execution\n *\n * @param result - The tool result to submit\n * @throws Error if session is not in waiting_for_tool state\n * @throws Error if result callId doesn't match pending call\n */\n submitToolResult(result: ToolResult): Promise<void>;\n\n /**\n * Cancel the session\n *\n * @param reason - Optional cancellation reason\n */\n cancel(reason?: string): Promise<void>;\n\n /**\n * Wait for the session to complete\n *\n * @returns Final execution result\n */\n wait(): Promise<SessionFinalResult>;\n\n /**\n * Get current execution statistics\n */\n getStats(): ExecutionStats;\n}\n\n/**\n * Final result when session completes\n */\nexport interface SessionFinalResult {\n /**\n * Whether execution was successful\n */\n success: boolean;\n\n /**\n * Return value from the script (if successful)\n */\n value?: unknown;\n\n /**\n * Error that occurred (if failed)\n */\n error?: {\n message: string;\n name: string;\n code?: string;\n data?: Record<string, unknown>;\n };\n\n /**\n * Execution statistics\n */\n stats: ExecutionStats;\n\n /**\n * Final session state\n */\n finalState: 'completed' | 'cancelled' | 'failed';\n}\n\n/**\n * Runtime channel abstraction\n *\n * Provides bidirectional communication between the broker and runtime.\n * Different implementations support embedded (in-process) and remote\n * (WebSocket) runtime execution.\n */\nexport interface RuntimeChannel {\n /**\n * Send a message to the runtime\n */\n send(message: RuntimeChannelMessage): void;\n\n /**\n * Subscribe to messages from the runtime\n */\n onMessage(handler: (event: StreamEvent) => void): () => void;\n\n /**\n * Close the channel\n */\n close(): void;\n\n /**\n * Whether the channel is open\n */\n readonly isOpen: boolean;\n}\n\n/**\n * Embedded runtime channel configuration\n *\n * For in-process execution where the Enclave runs in the same process.\n */\nexport interface EmbeddedChannelConfig {\n type: 'embedded';\n}\n\n/**\n * WebSocket runtime channel configuration\n *\n * For remote execution where the runtime is in a separate process/worker.\n */\nexport interface WebSocketChannelConfig {\n type: 'websocket';\n\n /**\n * WebSocket URL to connect to\n */\n url: string;\n\n /**\n * Reconnection options\n */\n reconnect?: {\n maxRetries?: number;\n initialDelayMs?: number;\n maxDelayMs?: number;\n };\n}\n\n/**\n * Runtime channel configuration union\n */\nexport type RuntimeChannelConfig = EmbeddedChannelConfig | WebSocketChannelConfig;\n\n/**\n * Session manager interface\n *\n * Manages multiple concurrent sessions.\n */\nexport interface SessionManager {\n /**\n * Create a new session\n *\n * @param code - Code to execute\n * @param config - Session configuration\n * @param toolHandler - Handler for tool calls\n * @returns Created session\n */\n createSession(code: string, config?: SessionConfig, toolHandler?: AsyncToolHandler): Promise<Session>;\n\n /**\n * Get an existing session by ID\n */\n getSession(sessionId: SessionId): Session | undefined;\n\n /**\n * List all active sessions\n */\n listSessions(): Session[];\n\n /**\n * Terminate a session\n */\n terminateSession(sessionId: SessionId, reason?: string): Promise<void>;\n\n /**\n * Dispose of the manager and all sessions\n */\n dispose(): Promise<void>;\n}\n\n/**\n * Type guard for checking if an object is a Session\n */\nexport function isSession(obj: unknown): obj is Session {\n return (\n typeof obj === 'object' &&\n obj !== null &&\n 'sessionId' in obj &&\n 'state' in obj &&\n 'submitToolResult' in obj &&\n typeof (obj as Session).submitToolResult === 'function'\n );\n}\n\n/**\n * Type guard for waiting_for_tool state\n */\nexport function isWaitingForTool(session: Session): boolean {\n return session.state === 'waiting_for_tool' && session.pendingToolCall !== null;\n}\n\n/**\n * Type guard for terminal states\n */\nexport function isTerminalState(state: SessionStateValue): boolean {\n return state === 'completed' || state === 'cancelled' || state === 'failed';\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference Sidecar Module
|
|
3
|
+
*
|
|
4
|
+
* Provides pass-by-reference support for the enclave execution environment.
|
|
5
|
+
* Large data is stored in the sidecar with opaque reference IDs, and
|
|
6
|
+
* references are resolved at the callTool boundary.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
export { ReferenceConfig, REFERENCE_CONFIGS, REF_ID_PREFIX, REF_ID_SUFFIX, REF_ID_PATTERN, isReferenceId, getReferenceConfig, } from './reference-config';
|
|
11
|
+
export { ReferenceSidecar, ReferenceSource, ReferenceMetadata, SidecarLimitError, ReferenceNotFoundError, } from './reference-sidecar';
|
|
12
|
+
export { ReferenceResolver, ResolutionLimitError, CompositeHandle, isCompositeHandle } from './reference-resolver';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reference Sidecar Module
|
|
4
|
+
*
|
|
5
|
+
* Provides pass-by-reference support for the enclave execution environment.
|
|
6
|
+
* Large data is stored in the sidecar with opaque reference IDs, and
|
|
7
|
+
* references are resolved at the callTool boundary.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.isCompositeHandle = exports.ResolutionLimitError = exports.ReferenceResolver = exports.ReferenceNotFoundError = exports.SidecarLimitError = exports.ReferenceSidecar = exports.getReferenceConfig = exports.isReferenceId = exports.REF_ID_PATTERN = exports.REF_ID_SUFFIX = exports.REF_ID_PREFIX = exports.REFERENCE_CONFIGS = void 0;
|
|
13
|
+
// Configuration
|
|
14
|
+
var reference_config_1 = require("./reference-config");
|
|
15
|
+
Object.defineProperty(exports, "REFERENCE_CONFIGS", { enumerable: true, get: function () { return reference_config_1.REFERENCE_CONFIGS; } });
|
|
16
|
+
Object.defineProperty(exports, "REF_ID_PREFIX", { enumerable: true, get: function () { return reference_config_1.REF_ID_PREFIX; } });
|
|
17
|
+
Object.defineProperty(exports, "REF_ID_SUFFIX", { enumerable: true, get: function () { return reference_config_1.REF_ID_SUFFIX; } });
|
|
18
|
+
Object.defineProperty(exports, "REF_ID_PATTERN", { enumerable: true, get: function () { return reference_config_1.REF_ID_PATTERN; } });
|
|
19
|
+
Object.defineProperty(exports, "isReferenceId", { enumerable: true, get: function () { return reference_config_1.isReferenceId; } });
|
|
20
|
+
Object.defineProperty(exports, "getReferenceConfig", { enumerable: true, get: function () { return reference_config_1.getReferenceConfig; } });
|
|
21
|
+
// Sidecar
|
|
22
|
+
var reference_sidecar_1 = require("./reference-sidecar");
|
|
23
|
+
Object.defineProperty(exports, "ReferenceSidecar", { enumerable: true, get: function () { return reference_sidecar_1.ReferenceSidecar; } });
|
|
24
|
+
Object.defineProperty(exports, "SidecarLimitError", { enumerable: true, get: function () { return reference_sidecar_1.SidecarLimitError; } });
|
|
25
|
+
Object.defineProperty(exports, "ReferenceNotFoundError", { enumerable: true, get: function () { return reference_sidecar_1.ReferenceNotFoundError; } });
|
|
26
|
+
// Resolver
|
|
27
|
+
var reference_resolver_1 = require("./reference-resolver");
|
|
28
|
+
Object.defineProperty(exports, "ReferenceResolver", { enumerable: true, get: function () { return reference_resolver_1.ReferenceResolver; } });
|
|
29
|
+
Object.defineProperty(exports, "ResolutionLimitError", { enumerable: true, get: function () { return reference_resolver_1.ResolutionLimitError; } });
|
|
30
|
+
Object.defineProperty(exports, "isCompositeHandle", { enumerable: true, get: function () { return reference_resolver_1.isCompositeHandle; } });
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/sidecar/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;AAEH,gBAAgB;AAChB,uDAQ4B;AAN1B,qHAAA,iBAAiB,OAAA;AACjB,iHAAA,aAAa,OAAA;AACb,iHAAA,aAAa,OAAA;AACb,kHAAA,cAAc,OAAA;AACd,iHAAA,aAAa,OAAA;AACb,sHAAA,kBAAkB,OAAA;AAGpB,UAAU;AACV,yDAM6B;AAL3B,qHAAA,gBAAgB,OAAA;AAGhB,sHAAA,iBAAiB,OAAA;AACjB,2HAAA,sBAAsB,OAAA;AAGxB,WAAW;AACX,2DAAmH;AAA1G,uHAAA,iBAAiB,OAAA;AAAE,0HAAA,oBAAoB,OAAA;AAAmB,uHAAA,iBAAiB,OAAA","sourcesContent":["/**\n * Reference Sidecar Module\n *\n * Provides pass-by-reference support for the enclave execution environment.\n * Large data is stored in the sidecar with opaque reference IDs, and\n * references are resolved at the callTool boundary.\n *\n * @packageDocumentation\n */\n\n// Configuration\nexport {\n ReferenceConfig,\n REFERENCE_CONFIGS,\n REF_ID_PREFIX,\n REF_ID_SUFFIX,\n REF_ID_PATTERN,\n isReferenceId,\n getReferenceConfig,\n} from './reference-config';\n\n// Sidecar\nexport {\n ReferenceSidecar,\n ReferenceSource,\n ReferenceMetadata,\n SidecarLimitError,\n ReferenceNotFoundError,\n} from './reference-sidecar';\n\n// Resolver\nexport { ReferenceResolver, ResolutionLimitError, CompositeHandle, isCompositeHandle } from './reference-resolver';\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference Sidecar Configuration
|
|
3
|
+
*
|
|
4
|
+
* Defines security-level-specific configurations for the reference sidecar.
|
|
5
|
+
* These settings control how large data is handled in the pass-by-reference system.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import type { SecurityLevel } from '../types';
|
|
10
|
+
/**
|
|
11
|
+
* Configuration for the reference sidecar system
|
|
12
|
+
*/
|
|
13
|
+
export interface ReferenceConfig {
|
|
14
|
+
/**
|
|
15
|
+
* Maximum total size of all stored references in bytes
|
|
16
|
+
* Prevents memory exhaustion from excessive data storage
|
|
17
|
+
*/
|
|
18
|
+
maxTotalSize: number;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum size of a single reference in bytes
|
|
21
|
+
* Prevents individual large allocations
|
|
22
|
+
*/
|
|
23
|
+
maxReferenceSize: number;
|
|
24
|
+
/**
|
|
25
|
+
* Threshold in bytes to trigger extraction from source code
|
|
26
|
+
* Strings larger than this are lifted to the sidecar
|
|
27
|
+
*/
|
|
28
|
+
extractionThreshold: number;
|
|
29
|
+
/**
|
|
30
|
+
* Maximum expanded size when resolving references for tool calls
|
|
31
|
+
* Prevents predictive expansion attacks
|
|
32
|
+
*/
|
|
33
|
+
maxResolvedSize: number;
|
|
34
|
+
/**
|
|
35
|
+
* Whether to allow composite handles from concatenation
|
|
36
|
+
* If false, __safe_concat__ throws when references are detected
|
|
37
|
+
*/
|
|
38
|
+
allowComposites: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Maximum number of references in a single execution
|
|
41
|
+
* Prevents reference flooding attacks
|
|
42
|
+
*/
|
|
43
|
+
maxReferenceCount: number;
|
|
44
|
+
/**
|
|
45
|
+
* Maximum depth when resolving nested references
|
|
46
|
+
* Prevents recursive reference bombs
|
|
47
|
+
*/
|
|
48
|
+
maxResolutionDepth: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Reference configurations per security level
|
|
52
|
+
*
|
|
53
|
+
* These configurations align with the enclave's security levels
|
|
54
|
+
* and provide appropriate limits for different trust scenarios.
|
|
55
|
+
*/
|
|
56
|
+
export declare const REFERENCE_CONFIGS: Record<SecurityLevel, ReferenceConfig>;
|
|
57
|
+
/**
|
|
58
|
+
* Reference ID prefix constant
|
|
59
|
+
*
|
|
60
|
+
* Reference IDs follow the format: __REF_[UUIDv4]__
|
|
61
|
+
* The __REF_ prefix is protected by ReservedPrefixRule in ast-guard
|
|
62
|
+
*/
|
|
63
|
+
export declare const REF_ID_PREFIX = "__REF_";
|
|
64
|
+
/**
|
|
65
|
+
* Reference ID suffix constant
|
|
66
|
+
*
|
|
67
|
+
* Used with REF_ID_PREFIX to form complete reference IDs: __REF_[UUIDv4]__
|
|
68
|
+
*/
|
|
69
|
+
export declare const REF_ID_SUFFIX = "__";
|
|
70
|
+
/**
|
|
71
|
+
* Regular expression to match reference IDs
|
|
72
|
+
*/
|
|
73
|
+
export declare const REF_ID_PATTERN: RegExp;
|
|
74
|
+
/**
|
|
75
|
+
* Check if a string is a reference ID
|
|
76
|
+
*/
|
|
77
|
+
export declare function isReferenceId(value: unknown): value is string;
|
|
78
|
+
/**
|
|
79
|
+
* Get reference config for a security level, with optional overrides
|
|
80
|
+
*/
|
|
81
|
+
export declare function getReferenceConfig(securityLevel: SecurityLevel, overrides?: Partial<ReferenceConfig>): ReferenceConfig;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Reference Sidecar Configuration
|
|
4
|
+
*
|
|
5
|
+
* Defines security-level-specific configurations for the reference sidecar.
|
|
6
|
+
* These settings control how large data is handled in the pass-by-reference system.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.REF_ID_PATTERN = exports.REF_ID_SUFFIX = exports.REF_ID_PREFIX = exports.REFERENCE_CONFIGS = void 0;
|
|
12
|
+
exports.isReferenceId = isReferenceId;
|
|
13
|
+
exports.getReferenceConfig = getReferenceConfig;
|
|
14
|
+
/**
|
|
15
|
+
* Reference configurations per security level
|
|
16
|
+
*
|
|
17
|
+
* These configurations align with the enclave's security levels
|
|
18
|
+
* and provide appropriate limits for different trust scenarios.
|
|
19
|
+
*/
|
|
20
|
+
exports.REFERENCE_CONFIGS = {
|
|
21
|
+
/**
|
|
22
|
+
* STRICT: Maximum security for untrusted code
|
|
23
|
+
*
|
|
24
|
+
* - Very small limits to prevent any memory-based attacks
|
|
25
|
+
* - No composite handles (references cannot be concatenated)
|
|
26
|
+
* - Aggressive extraction threshold
|
|
27
|
+
*/
|
|
28
|
+
STRICT: {
|
|
29
|
+
maxTotalSize: 16 * 1024 * 1024, // 16MB total
|
|
30
|
+
maxReferenceSize: 4 * 1024 * 1024, // 4MB per reference
|
|
31
|
+
extractionThreshold: 64 * 1024, // 64KB extraction threshold
|
|
32
|
+
maxResolvedSize: 8 * 1024 * 1024, // 8MB max resolved
|
|
33
|
+
allowComposites: false, // No concatenation of references
|
|
34
|
+
maxReferenceCount: 50, // Max 50 references
|
|
35
|
+
maxResolutionDepth: 5, // Shallow resolution
|
|
36
|
+
},
|
|
37
|
+
/**
|
|
38
|
+
* SECURE: High security with more headroom
|
|
39
|
+
*
|
|
40
|
+
* - Moderate limits for trusted AI code
|
|
41
|
+
* - No composite handles by default
|
|
42
|
+
* - Standard extraction threshold
|
|
43
|
+
*/
|
|
44
|
+
SECURE: {
|
|
45
|
+
maxTotalSize: 64 * 1024 * 1024, // 64MB total
|
|
46
|
+
maxReferenceSize: 16 * 1024 * 1024, // 16MB per reference
|
|
47
|
+
extractionThreshold: 256 * 1024, // 256KB extraction threshold
|
|
48
|
+
maxResolvedSize: 32 * 1024 * 1024, // 32MB max resolved
|
|
49
|
+
allowComposites: false, // No concatenation of references
|
|
50
|
+
maxReferenceCount: 100, // Max 100 references
|
|
51
|
+
maxResolutionDepth: 10, // Moderate resolution depth
|
|
52
|
+
},
|
|
53
|
+
/**
|
|
54
|
+
* STANDARD: Balanced security and functionality
|
|
55
|
+
*
|
|
56
|
+
* - Standard limits for internal tools
|
|
57
|
+
* - Composite handles allowed
|
|
58
|
+
* - Larger extraction threshold
|
|
59
|
+
*/
|
|
60
|
+
STANDARD: {
|
|
61
|
+
maxTotalSize: 256 * 1024 * 1024, // 256MB total
|
|
62
|
+
maxReferenceSize: 64 * 1024 * 1024, // 64MB per reference
|
|
63
|
+
extractionThreshold: 1024 * 1024, // 1MB extraction threshold
|
|
64
|
+
maxResolvedSize: 128 * 1024 * 1024, // 128MB max resolved
|
|
65
|
+
allowComposites: true, // Allow concatenation
|
|
66
|
+
maxReferenceCount: 500, // Max 500 references
|
|
67
|
+
maxResolutionDepth: 20, // Deep resolution
|
|
68
|
+
},
|
|
69
|
+
/**
|
|
70
|
+
* PERMISSIVE: Minimal restrictions
|
|
71
|
+
*
|
|
72
|
+
* - Large limits for development/debugging
|
|
73
|
+
* - All features enabled
|
|
74
|
+
* - High extraction threshold
|
|
75
|
+
*/
|
|
76
|
+
PERMISSIVE: {
|
|
77
|
+
maxTotalSize: 1024 * 1024 * 1024, // 1GB total
|
|
78
|
+
maxReferenceSize: 256 * 1024 * 1024, // 256MB per reference
|
|
79
|
+
extractionThreshold: 4 * 1024 * 1024, // 4MB extraction threshold
|
|
80
|
+
maxResolvedSize: 512 * 1024 * 1024, // 512MB max resolved
|
|
81
|
+
allowComposites: true, // Allow concatenation
|
|
82
|
+
maxReferenceCount: 1000, // Max 1000 references
|
|
83
|
+
maxResolutionDepth: 50, // Very deep resolution
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Reference ID prefix constant
|
|
88
|
+
*
|
|
89
|
+
* Reference IDs follow the format: __REF_[UUIDv4]__
|
|
90
|
+
* The __REF_ prefix is protected by ReservedPrefixRule in ast-guard
|
|
91
|
+
*/
|
|
92
|
+
exports.REF_ID_PREFIX = '__REF_';
|
|
93
|
+
/**
|
|
94
|
+
* Reference ID suffix constant
|
|
95
|
+
*
|
|
96
|
+
* Used with REF_ID_PREFIX to form complete reference IDs: __REF_[UUIDv4]__
|
|
97
|
+
*/
|
|
98
|
+
exports.REF_ID_SUFFIX = '__';
|
|
99
|
+
/**
|
|
100
|
+
* Regular expression to match reference IDs
|
|
101
|
+
*/
|
|
102
|
+
exports.REF_ID_PATTERN = /^__REF_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}__$/i;
|
|
103
|
+
/**
|
|
104
|
+
* Check if a string is a reference ID
|
|
105
|
+
*/
|
|
106
|
+
function isReferenceId(value) {
|
|
107
|
+
return typeof value === 'string' && exports.REF_ID_PATTERN.test(value);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get reference config for a security level, with optional overrides
|
|
111
|
+
*/
|
|
112
|
+
function getReferenceConfig(securityLevel, overrides) {
|
|
113
|
+
return {
|
|
114
|
+
...exports.REFERENCE_CONFIGS[securityLevel],
|
|
115
|
+
...overrides,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=reference-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reference-config.js","sourceRoot":"","sources":["../../../src/sidecar/reference-config.ts"],"names":[],"mappings":";AAAA;;;;;;;GAOG;;;AAsJH,sCAEC;AAKD,gDAQC;AAlHD;;;;;GAKG;AACU,QAAA,iBAAiB,GAA2C;IACvE;;;;;;OAMG;IACH,MAAM,EAAE;QACN,YAAY,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,aAAa;QAC7C,gBAAgB,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,oBAAoB;QACvD,mBAAmB,EAAE,EAAE,GAAG,IAAI,EAAE,4BAA4B;QAC5D,eAAe,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,mBAAmB;QACrD,eAAe,EAAE,KAAK,EAAE,iCAAiC;QACzD,iBAAiB,EAAE,EAAE,EAAE,oBAAoB;QAC3C,kBAAkB,EAAE,CAAC,EAAE,qBAAqB;KAC7C;IAED;;;;;;OAMG;IACH,MAAM,EAAE;QACN,YAAY,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,aAAa;QAC7C,gBAAgB,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,qBAAqB;QACzD,mBAAmB,EAAE,GAAG,GAAG,IAAI,EAAE,6BAA6B;QAC9D,eAAe,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,oBAAoB;QACvD,eAAe,EAAE,KAAK,EAAE,iCAAiC;QACzD,iBAAiB,EAAE,GAAG,EAAE,qBAAqB;QAC7C,kBAAkB,EAAE,EAAE,EAAE,4BAA4B;KACrD;IAED;;;;;;OAMG;IACH,QAAQ,EAAE;QACR,YAAY,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,cAAc;QAC/C,gBAAgB,EAAE,EAAE,GAAG,IAAI,GAAG,IAAI,EAAE,qBAAqB;QACzD,mBAAmB,EAAE,IAAI,GAAG,IAAI,EAAE,2BAA2B;QAC7D,eAAe,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,qBAAqB;QACzD,eAAe,EAAE,IAAI,EAAE,sBAAsB;QAC7C,iBAAiB,EAAE,GAAG,EAAE,qBAAqB;QAC7C,kBAAkB,EAAE,EAAE,EAAE,kBAAkB;KAC3C;IAED;;;;;;OAMG;IACH,UAAU,EAAE;QACV,YAAY,EAAE,IAAI,GAAG,IAAI,GAAG,IAAI,EAAE,YAAY;QAC9C,gBAAgB,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,sBAAsB;QAC3D,mBAAmB,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,2BAA2B;QACjE,eAAe,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI,EAAE,qBAAqB;QACzD,eAAe,EAAE,IAAI,EAAE,sBAAsB;QAC7C,iBAAiB,EAAE,IAAI,EAAE,sBAAsB;QAC/C,kBAAkB,EAAE,EAAE,EAAE,uBAAuB;KAChD;CACF,CAAC;AAEF;;;;;GAKG;AACU,QAAA,aAAa,GAAG,QAAQ,CAAC;AAEtC;;;;GAIG;AACU,QAAA,aAAa,GAAG,IAAI,CAAC;AAElC;;GAEG;AACU,QAAA,cAAc,GAAG,yEAAyE,CAAC;AAExG;;GAEG;AACH,SAAgB,aAAa,CAAC,KAAc;IAC1C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,sBAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACjE,CAAC;AAED;;GAEG;AACH,SAAgB,kBAAkB,CAChC,aAA4B,EAC5B,SAAoC;IAEpC,OAAO;QACL,GAAG,yBAAiB,CAAC,aAAa,CAAC;QACnC,GAAG,SAAS;KACb,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Reference Sidecar Configuration\n *\n * Defines security-level-specific configurations for the reference sidecar.\n * These settings control how large data is handled in the pass-by-reference system.\n *\n * @packageDocumentation\n */\n\nimport type { SecurityLevel } from '../types';\n\n/**\n * Configuration for the reference sidecar system\n */\nexport interface ReferenceConfig {\n /**\n * Maximum total size of all stored references in bytes\n * Prevents memory exhaustion from excessive data storage\n */\n maxTotalSize: number;\n\n /**\n * Maximum size of a single reference in bytes\n * Prevents individual large allocations\n */\n maxReferenceSize: number;\n\n /**\n * Threshold in bytes to trigger extraction from source code\n * Strings larger than this are lifted to the sidecar\n */\n extractionThreshold: number;\n\n /**\n * Maximum expanded size when resolving references for tool calls\n * Prevents predictive expansion attacks\n */\n maxResolvedSize: number;\n\n /**\n * Whether to allow composite handles from concatenation\n * If false, __safe_concat__ throws when references are detected\n */\n allowComposites: boolean;\n\n /**\n * Maximum number of references in a single execution\n * Prevents reference flooding attacks\n */\n maxReferenceCount: number;\n\n /**\n * Maximum depth when resolving nested references\n * Prevents recursive reference bombs\n */\n maxResolutionDepth: number;\n}\n\n/**\n * Reference configurations per security level\n *\n * These configurations align with the enclave's security levels\n * and provide appropriate limits for different trust scenarios.\n */\nexport const REFERENCE_CONFIGS: Record<SecurityLevel, ReferenceConfig> = {\n /**\n * STRICT: Maximum security for untrusted code\n *\n * - Very small limits to prevent any memory-based attacks\n * - No composite handles (references cannot be concatenated)\n * - Aggressive extraction threshold\n */\n STRICT: {\n maxTotalSize: 16 * 1024 * 1024, // 16MB total\n maxReferenceSize: 4 * 1024 * 1024, // 4MB per reference\n extractionThreshold: 64 * 1024, // 64KB extraction threshold\n maxResolvedSize: 8 * 1024 * 1024, // 8MB max resolved\n allowComposites: false, // No concatenation of references\n maxReferenceCount: 50, // Max 50 references\n maxResolutionDepth: 5, // Shallow resolution\n },\n\n /**\n * SECURE: High security with more headroom\n *\n * - Moderate limits for trusted AI code\n * - No composite handles by default\n * - Standard extraction threshold\n */\n SECURE: {\n maxTotalSize: 64 * 1024 * 1024, // 64MB total\n maxReferenceSize: 16 * 1024 * 1024, // 16MB per reference\n extractionThreshold: 256 * 1024, // 256KB extraction threshold\n maxResolvedSize: 32 * 1024 * 1024, // 32MB max resolved\n allowComposites: false, // No concatenation of references\n maxReferenceCount: 100, // Max 100 references\n maxResolutionDepth: 10, // Moderate resolution depth\n },\n\n /**\n * STANDARD: Balanced security and functionality\n *\n * - Standard limits for internal tools\n * - Composite handles allowed\n * - Larger extraction threshold\n */\n STANDARD: {\n maxTotalSize: 256 * 1024 * 1024, // 256MB total\n maxReferenceSize: 64 * 1024 * 1024, // 64MB per reference\n extractionThreshold: 1024 * 1024, // 1MB extraction threshold\n maxResolvedSize: 128 * 1024 * 1024, // 128MB max resolved\n allowComposites: true, // Allow concatenation\n maxReferenceCount: 500, // Max 500 references\n maxResolutionDepth: 20, // Deep resolution\n },\n\n /**\n * PERMISSIVE: Minimal restrictions\n *\n * - Large limits for development/debugging\n * - All features enabled\n * - High extraction threshold\n */\n PERMISSIVE: {\n maxTotalSize: 1024 * 1024 * 1024, // 1GB total\n maxReferenceSize: 256 * 1024 * 1024, // 256MB per reference\n extractionThreshold: 4 * 1024 * 1024, // 4MB extraction threshold\n maxResolvedSize: 512 * 1024 * 1024, // 512MB max resolved\n allowComposites: true, // Allow concatenation\n maxReferenceCount: 1000, // Max 1000 references\n maxResolutionDepth: 50, // Very deep resolution\n },\n};\n\n/**\n * Reference ID prefix constant\n *\n * Reference IDs follow the format: __REF_[UUIDv4]__\n * The __REF_ prefix is protected by ReservedPrefixRule in ast-guard\n */\nexport const REF_ID_PREFIX = '__REF_';\n\n/**\n * Reference ID suffix constant\n *\n * Used with REF_ID_PREFIX to form complete reference IDs: __REF_[UUIDv4]__\n */\nexport const REF_ID_SUFFIX = '__';\n\n/**\n * Regular expression to match reference IDs\n */\nexport const REF_ID_PATTERN = /^__REF_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}__$/i;\n\n/**\n * Check if a string is a reference ID\n */\nexport function isReferenceId(value: unknown): value is string {\n return typeof value === 'string' && REF_ID_PATTERN.test(value);\n}\n\n/**\n * Get reference config for a security level, with optional overrides\n */\nexport function getReferenceConfig(\n securityLevel: SecurityLevel,\n overrides?: Partial<ReferenceConfig>,\n): ReferenceConfig {\n return {\n ...REFERENCE_CONFIGS[securityLevel],\n ...overrides,\n };\n}\n"]}
|