mirror of
https://github.com/stablyai/orca.git
synced 2026-10-31 09:07:40 +00:00
A manifest under the 768 KiB validation cap can still overflow the host's 1 MiB inbound frame once sealed (base64 adds a third), more so when a commit carries its blob list. The host answers such a frame by closing the socket, so the move failed with a lost answer. The catalog client now measures the request against REMOTE_RUNTIME_MAX_INBOUND_REQUEST_JSON_BYTES and refuses it with the existing 'too large' guidance, unsent.
99 lines
4.3 KiB
TypeScript
99 lines
4.3 KiB
TypeScript
import {
|
|
JsonStringifyByteLimitError,
|
|
stringifyJsonWithinByteLimit
|
|
} from './node-bounded-json-stringify'
|
|
import { RemoteRuntimeClientError } from './remote-runtime-client-error'
|
|
import type { RuntimeOrchestrationEnvelope } from './runtime-rpc-envelope'
|
|
|
|
export const REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES = 4 * 1024 * 1024
|
|
// Why: a host refuses a larger client frame by closing the socket (1009), failing every call on it.
|
|
export const REMOTE_RUNTIME_MAX_INBOUND_FRAME_BYTES = 1024 * 1024
|
|
// Why: a sealed frame is base64 of nonce, MAC and (v2) a 42-byte header around the request JSON.
|
|
const REMOTE_RUNTIME_MAX_SEALING_OVERHEAD_BYTES = 128
|
|
/** The largest request JSON that still fits a host's inbound frame once sealed. */
|
|
export const REMOTE_RUNTIME_MAX_INBOUND_REQUEST_JSON_BYTES =
|
|
Math.floor(REMOTE_RUNTIME_MAX_INBOUND_FRAME_BYTES / 4) * 3 -
|
|
REMOTE_RUNTIME_MAX_SEALING_OVERHEAD_BYTES
|
|
// Why: sealing inflates a request by up to 4/3, so this much JSON-escaped content still fits a frame.
|
|
export const REMOTE_RUNTIME_MAX_FILE_WRITE_CONTENT_BYTES = 720 * 1024
|
|
export const REMOTE_RUNTIME_MAX_WEBSOCKET_FRAME_BYTES = 8 * 1024 * 1024 + 64
|
|
export const REMOTE_RUNTIME_MAX_SUBSCRIPTIONS = 256
|
|
export const REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES = 1024 * 1024
|
|
export const REMOTE_RUNTIME_MAX_RETAINED_SUBSCRIPTION_BYTES = 16 * 1024 * 1024
|
|
export const REMOTE_RUNTIME_MAX_PENDING_REQUESTS = 256
|
|
export const REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES = 32 * 1024 * 1024
|
|
export const REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES = REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES
|
|
export const REMOTE_RUNTIME_MAX_PROCESS_PENDING_REQUESTS = REMOTE_RUNTIME_MAX_PENDING_REQUESTS * 2
|
|
export const REMOTE_RUNTIME_MAX_PROCESS_PENDING_RPC_BYTES = REMOTE_RUNTIME_MAX_PENDING_RPC_BYTES * 2
|
|
export const REMOTE_RUNTIME_MAX_READY_WAITERS =
|
|
REMOTE_RUNTIME_MAX_PENDING_REQUESTS + REMOTE_RUNTIME_MAX_SUBSCRIPTIONS
|
|
export const REMOTE_RUNTIME_MAX_OUTBOUND_BINARY_FRAME_BYTES = 8 * 1024 * 1024
|
|
|
|
export function serializeRemoteRuntimePayload(value: unknown): string {
|
|
try {
|
|
return stringifyJsonWithinByteLimit(value, REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES).serialized
|
|
} catch (error) {
|
|
if (error instanceof JsonStringifyByteLimitError) {
|
|
throw new RemoteRuntimeClientError(
|
|
'invalid_argument',
|
|
`Remote runtime JSON payload exceeds ${REMOTE_RUNTIME_MAX_OUTBOUND_JSON_BYTES} bytes.`
|
|
)
|
|
}
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
throw new RemoteRuntimeClientError(
|
|
'invalid_argument',
|
|
`Remote runtime JSON payload could not be serialized: ${message}`
|
|
)
|
|
}
|
|
}
|
|
|
|
export function measureRemoteRuntimeSubscriptionParams(params: unknown): number {
|
|
if (params === undefined) {
|
|
return 0
|
|
}
|
|
try {
|
|
return stringifyJsonWithinByteLimit(params, REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES)
|
|
.byteLength
|
|
} catch (error) {
|
|
if (error instanceof JsonStringifyByteLimitError) {
|
|
throw new RemoteRuntimeClientError(
|
|
'invalid_argument',
|
|
`Remote runtime subscription parameters exceed ${REMOTE_RUNTIME_MAX_SUBSCRIPTION_PARAM_BYTES} bytes.`
|
|
)
|
|
}
|
|
const message = error instanceof Error ? error.message : String(error)
|
|
throw new RemoteRuntimeClientError(
|
|
'invalid_argument',
|
|
`Remote runtime subscription parameters could not be serialized: ${message}`
|
|
)
|
|
}
|
|
}
|
|
|
|
export function serializeRemoteRuntimeRpcRequest(args: {
|
|
requestId: string
|
|
deviceToken: string
|
|
method: string
|
|
params: unknown
|
|
envelope?: RuntimeOrchestrationEnvelope
|
|
}): string {
|
|
return serializeRemoteRuntimePayload({
|
|
id: args.requestId,
|
|
deviceToken: args.deviceToken,
|
|
method: args.method,
|
|
params: args.params,
|
|
orchestrationCapability: args.envelope?.orchestrationCapability,
|
|
orchestrationContractVersion: args.envelope?.orchestrationContractVersion,
|
|
orchestrationRequestId: args.envelope?.orchestrationRequestId,
|
|
compatibilityInvocationId: args.envelope?.compatibilityInvocationId,
|
|
orchestrationCompatibilityEvidence: args.envelope?.orchestrationCompatibilityEvidence
|
|
})
|
|
}
|
|
|
|
export function retainedRemoteRuntimeJsonStringBytes(value: string): number {
|
|
return value.length * 3
|
|
}
|
|
|
|
export function isRemoteRuntimeBinaryFrameWithinLimit(bytes: Uint8Array<ArrayBufferLike>): boolean {
|
|
return bytes.byteLength <= REMOTE_RUNTIME_MAX_OUTBOUND_BINARY_FRAME_BYTES
|
|
}
|