@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
90 lines • 4.59 kB
JavaScript
/**
* One schema per inbound RPC record type, and the validator built from it (senpi#4324).
*
* The record is exhaustive over `RpcMethodType` and checked against the TypeScript record types at compile time: a
* schema must accept every record its type allows (so no well-typed record is refused) and name exactly that type's
* fields, with the same ones required. Unknown fields stay accepted, as they are on the local connection, so
* additive fields keep working against older hosts. Typed fields are bounded (`RPC_INBOUND_LIMITS`); the whole record
* (unknown additive fields and free-form payloads included) is bounded by the host's own line limit, measured as its
* JSON.
*
* Nothing runs this validator yet: the local connection keeps parsing records as it always has, and where a forwarded
* record is validated (and whether the package root exports it) is still to be decided.
*/
import { Check, Errors } from "typebox/value";
import { MAX_RPC_LINE_CHARACTERS } from "./jsonl.js";
import { HOST_RECORD_SCHEMAS } from "./method-classes-schema-host.js";
import { RECOVERY_RECORD_SCHEMAS } from "./method-classes-schema-recovery.js";
import { SESSION_RECORD_SCHEMAS } from "./method-classes-schema-session.js";
import { rpcCommandPayloadError, rpcCommandShapeError, rpcMessageLengthError, sessionContextError, sessionRetryFallbackError, } from "./rpc-input-validation.js";
import { sessionEnvShapeError } from "./session-caller-env.js";
export { RPC_INBOUND_LIMITS, RPC_QUESTION_ANSWER_LIMITS } from "./method-classes-schema-fields.js";
/** Fails the typecheck, naming the type, when a schema is missing or disagrees with its record type. */
function agreeingSchemas(schemas) {
return schemas;
}
export const RPC_INBOUND_SCHEMAS = agreeingSchemas({
...HOST_RECORD_SCHEMAS,
...SESSION_RECORD_SCHEMAS,
...RECOVERY_RECORD_SCHEMAS,
});
const MAX_REASON_TYPE_CHARACTERS = 64;
function refuse(reason) {
return { ok: false, reason };
}
/** Bounds the schema leaves to the host's own field validators, so both paths refuse the same values. */
function fieldBoundsError(record) {
const payload = rpcCommandPayloadError(record) ?? rpcMessageLengthError(record);
if (payload !== undefined)
return payload;
if (record.type === "open_session" ||
record.type === "warm" ||
record.type === "park_session" ||
record.type === "claim_session") {
const context = sessionContextError(record.context);
if (context !== undefined)
return context;
}
if (record.type === "open_session" || record.type === "claim_session")
return sessionRetryFallbackError(record.retryFallback) ?? sessionEnvShapeError(record.env);
if (record.type === "set_retry_fallback")
return sessionRetryFallbackError(record.retryFallback);
return undefined;
}
/**
* Whether a caller other than the local interactive user may pass `record` on: an object whose `type` has a
* schema, that matches it, and whose fields are inside the bounds the host enforces. A refusal names the type and
* the first failing field, never a field's value.
*/
export function validateRpcInboundRecord(record) {
const shape = rpcCommandShapeError(record);
if (shape !== undefined)
return refuse(shape);
// This serializes the record once to measure it; a caller that already holds the raw line may cap that instead.
let size;
try {
size = JSON.stringify(record).length;
}
catch {
return refuse("the record is not JSON-serializable.");
}
if (size > MAX_RPC_LINE_CHARACTERS)
return refuse(`the record is ${size} characters of JSON, at most ${MAX_RPC_LINE_CHARACTERS}.`);
const value = record;
const type = value.type;
if (typeof type !== "string")
return refuse("type must be a string.");
if (!Object.hasOwn(RPC_INBOUND_SCHEMAS, type)) {
const shown = type.length > MAX_REASON_TYPE_CHARACTERS ? `${type.slice(0, MAX_REASON_TYPE_CHARACTERS)}...` : type;
return refuse(`unknown record type: ${JSON.stringify(shown)}.`);
}
const schema = RPC_INBOUND_SCHEMAS[type];
if (!Check(schema, record)) {
const [first] = Errors(schema, record);
const at = first?.instancePath ? ` at ${first.instancePath}` : "";
return refuse(`${type}: does not match its schema${at}${first ? `: ${first.message}` : ""}.`);
}
const bounds = fieldBoundsError(value);
return bounds === undefined ? { ok: true } : refuse(`${type}: ${bounds}`);
}
//# sourceMappingURL=method-classes-schema.js.map