UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

371 lines • 18.6 kB
/** * The engine's own classification of every inbound RPC record (senpi#4319). * * A client that speaks for someone other than the local user (a forwarder, a remote viewer, an automation) needs * to know which records it may accept from that someone. A list kept outside the engine drifts the moment a method * lands, so the table lives here and is exhaustive at compile time: it `satisfies` a mapped type over * `RpcInboundRecord["type"]`, so a record type added without a class fails the typecheck. An entry is a class, or a * versioned rule (a pure function of the record) when the class depends on its fields. The engine only classifies; * it does not gate its own local connections with this table. * * Classes, least to most dangerous: * - `observe`: side-effect-free reads. * - `drive`: runs, steers or edits the current session (prompts, queue and user-message edits, session-scoped * settings; a setter that writes the device-wide settings file is `device_admin`). A prompt that can run a slash command is * `device_admin`: extension command handlers run with the same unbounded context as `extension_request`. * - `answer`: replies to a question or dialog the engine asked. The engine's permission prompts are such dialogs, so * a caller granted `answer` can approve what `device_admin` would otherwise gate: never treat it as harmless. * - `thread`: creates, opens, attaches, forks or closes sessions by id. A record that names a host path (a session * file, a cwd, an import source, a parent session), a permission preset, or the environment of the session's tool * subprocesses is `device_admin`: it reads or writes host files, or changes what the agent may do without asking. * - `device_admin`: acts on the device outside the agent's own permission flow (direct shell, filesystem writes and * probes, credentials, device-wide settings, extension reloads, forged session entries: an assistant reply or custom message the engine did not produce, whether * appended, sent or edited in). A remote caller reaches it * only when it is a caller granted the class. * - `connection_local`: configures or tears down the caller's own connection or host ownership; meaningful only to * the connection that sends it, so it is never permitted on someone else's behalf. * * At runtime a record the table does not classify (unknown type, non-string type, not an object) is treated as more * dangerous than every class: `rpcMethodPermitted` refuses it whatever is granted. */ import { createHash } from "node:crypto"; /** Bumped when the meaning of a class changes; a reassignment changes only the digest. */ export const RPC_METHOD_CLASSES_VERSION = 1; /** How the table description names an entry whose class depends on the record. */ const DYNAMIC = "dynamic"; /** * Which session entry types a user could author through the session's own input. Exhaustive over * `SessionEntry["type"]`, so a new entry type has to be placed here before it typechecks. */ const ENTRY_AUTHORSHIP = { message: "user_role_only", thinking_level_change: "engine", configuration_update: "engine", model_change: "engine", model_change_rejected: "engine", usage: "engine", compaction: "engine", branch_summary: "engine", custom: "engine", custom_message: "engine", context_edit: "engine", label: "engine", session_info: "engine", }; const USER_ROLE = "user"; /** * A user-authored message entry drives the session; any other entry is a forged record of engine work, the same class as * `edit_assistant_message` and `send_custom_message`, which write that record by another route. */ function appendEntryClass(record) { const entry = Reflect.get(record, "entry"); if (typeof entry !== "object" || entry === null) return "device_admin"; const entryType = Reflect.get(entry, "type"); if (typeof entryType !== "string" || !Object.hasOwn(ENTRY_AUTHORSHIP, entryType)) return "device_admin"; if (ENTRY_AUTHORSHIP[entryType] !== "user_role_only") return "device_admin"; const message = Reflect.get(entry, "message"); if (typeof message !== "object" || message === null) return "device_admin"; return Reflect.get(message, "role") === USER_ROLE ? "drive" : "device_admin"; } /** A field that names a host path or a permission preset; present means any value but `undefined` (null included). */ function namesAny(record, fields) { return fields.some((field) => Object.hasOwn(record, field) && Reflect.get(record, field) !== undefined); } /** * Opening by id is a thread operation; naming a session file, a cwd, a permission preset, a browser engine (the * user's own signed-in browser) or an environment for the session's tool subprocesses (`env`, version 2) acts on * the device. */ function openSessionClass(record) { return namesAny(record, ["sessionPath", "cwd", "permissionPreset", "browserEngine", "env"]) ? "device_admin" : "thread"; } /** `scope: "turn"` changes only this turn; any other scope remembers the model's level device-wide. */ function thinkingLevelClass(record) { return Reflect.get(record, "scope") === "turn" ? "drive" : "device_admin"; } /** `scope: "session"` switches only this session's model; any other scope persists the device's default model. */ function setModelClass(record) { return Reflect.get(record, "scope") === "session" ? "drive" : "device_admin"; } /** An effect looked up by session id is a read; one looked up in a named session file opens a host path. */ function effectStatusClass(record) { return namesAny(record, ["sessionPath"]) ? "device_admin" : "observe"; } /** * Prompt text is `drive`; text that can dispatch a registered slash command is `device_admin`, because the handler runs * with a full command context (`/mcp` writes the MCP config and starts servers, `/account` edits credentials, `/ir` * reads any host path). Mirrors the session's own dispatch check: templates expand unless `expandPromptTemplates` is exactly * `false`, and the raw message starts with `/`. Whether the command exists is left to the session, so an unregistered * name is not a reason to answer lower; a message that is not a string cannot be judged. */ function promptClass(record) { const message = Reflect.get(record, "message"); if (typeof message !== "string") return "device_admin"; if (Reflect.get(record, "expandPromptTemplates") === false) return "drive"; return message.startsWith("/") ? "device_admin" : "drive"; } /** * `steer`/`follow_up` with `whenIdle: "start"` take the prompt path, which expands commands (with no template option); * the queue path refuses registered commands. So a turn-starting message that is not a string or starts with `/` is * `device_admin`, like `prompt`; anything else is `drive`. */ function idleStartClass(record) { if (Reflect.get(record, "whenIdle") !== "start") return "drive"; const message = Reflect.get(record, "message"); return typeof message !== "string" || message.startsWith("/") ? "device_admin" : "drive"; } /** A new session is a thread operation; naming a parent session file reads a host path. */ function newSessionClass(record) { return namesAny(record, ["parentSession"]) ? "device_admin" : "thread"; } export const RPC_METHOD_CLASS = { // observe get_protocol_info: "observe", get_subscription_limits: "observe", list_sessions: "observe", list_hands: "observe", get_state: "observe", get_snapshot: "observe", get_entries: "observe", get_messages: "observe", get_tree: "observe", get_fork_messages: "observe", get_last_assistant_text: "observe", get_session_stats: "observe", // observe by session id; device_admin when it names a session file (it opens that host path) get_effect_status: { classify: effectStatusClass, version: 1 }, get_pending_inputs: "observe", get_follow_up_messages: "observe", get_steering_messages: "observe", get_commands: "observe", get_loaded_surfaces: "observe", // the session's own effective trust and saved decision, as get_state already shows projectTrusted get_project_trust: "observe", get_available_models: "observe", get_available_thinking_levels: "observe", get_model_thinking_level: "observe", get_fast_mode: "observe", get_media: "observe", memory_report: "observe", "eval_cells.list": "observe", "eval_cells.peek": "observe", // drive // drive for text; device_admin when the text can run a slash command (rule prompt@1) prompt: { classify: promptClass, version: 1 }, // drive, except a turn-starting slash command (rules steer@1 / follow_up@1) steer: { classify: idleStartClass, version: 1 }, follow_up: { classify: idleStartClass, version: 1 }, abort: "drive", interrupt: "drive", abort_retry: "drive", abort_compaction: "drive", abort_branch_summary: "drive", compact: "drive", clear_queue: "drive", dequeue_message: "drive", edit_queued_message: "drive", hold_input: "drive", withdraw_held_input: "drive", append_user_message: "drive", edit_user_message: "drive", continue_from_leaf: "drive", navigate_tree: "drive", set_label: "drive", set_session_name: "drive", set_session_archived: "drive", set_session_settled: "drive", // drive only for this turn's level; otherwise it remembers the model's level in the device-wide settings set_thinking_level: { classify: thinkingLevelClass, version: 1 }, // drive only for this session's model; otherwise it persists the default model in the device-wide settings set_model: { classify: setModelClass, version: 1 }, // a session override; set_steering_mode, set_follow_up_mode and set_auto_retry write global settings (device_admin) set_auto_compaction: "drive", // like a preset-naming open_session: it changes what the agent may do without asking set_permission_preset: "device_admin", set_retry_fallback: "drive", pause_for_run: "drive", resume_for_run: "drive", wake: "drive", // the engine writes its own fixed delivery entry: no role=user, input event, command or template expansion (senpi#6233) admit_external_message: "drive", release_tool_barrier: "drive", request_self_settle: "drive", "eval_cells.stop": "drive", // drive for a user-authored message entry, device_admin for any other entry append_session_entry: { classify: appendEntryClass, version: 1 }, // answer extension_ui_response: "answer", extension_ui_progress: "answer", // thread // thread by id; device_admin when the record names a host path, a permission preset or a tool environment new_session: { classify: newSessionClass, version: 1 }, open_session: { classify: openSessionClass, version: 2 }, // Both commands require a cwd; claim has the same device authority as a cwd-naming open. claim_session: "device_admin", park_session: "device_admin", resume_session: "thread", fork: "thread", clone: "thread", close_session: "thread", unload_session: "thread", set_session_context: "thread", attach_session: "thread", // device_admin // Moves where the session's tools run (onto another machine): it changes what the agent can reach. select_hand: "device_admin", bash: "device_admin", abort_bash: "device_admin", record_bash_result: "device_admin", cleanup_bash_output: "device_admin", extension_request: "device_admin", reload: "device_admin", check_reload_veto: "device_admin", resolve_path: "device_admin", // Answers a session file's path and its folder, which is what makes a path-naming open_session device_admin. find_session: "device_admin", login_start: "device_admin", login_cancel: "device_admin", login_api_key: "device_admin", logout: "device_admin", verify_api_key: "device_admin", get_auth_providers: "device_admin", get_provider_accounts: "device_admin", account_order: "device_admin", account_pin: "device_admin", account_remove: "device_admin", // clears the stored account's cooldown/block state through the auth storage, like account_remove account_reset: "device_admin", // runs the sign-in connection test on a stored account and writes its result through the auth storage test_provider_account: "device_admin", set_favorite_models: "device_admin", set_scoped_models: "device_admin", // each writes the device-wide settings file, not a session override (cycle_model persists the default model, // set_fast_mode the service tier) cycle_model: "device_admin", cycle_thinking_level: "device_admin", set_fast_mode: "device_admin", set_steering_mode: "device_admin", set_follow_up_mode: "device_admin", set_auto_retry: "device_admin", // a consent decision saved in the device's trust store: it decides whether project code runs on the device set_project_trust: "device_admin", // reads a file from the session's cwd; writes one into it get_project_settings: "device_admin", set_project_setting: "device_admin", // reads or replaces an instruction file in the session's cwd (AGENTS.md, CLAUDE.md, .omo/rules/**) read_project_file: "device_admin", write_project_file: "device_admin", // reads or edits the session folder's project MCP file, or the file of a skill the engine lists inside that folder get_project_mcp_servers: "device_admin", set_project_mcp_servers: "device_admin", read_project_skill_file: "device_admin", write_project_skill_file: "device_admin", // reads or edits the file of a user-global skill the engine lists; a write from another device needs this device's approval read_user_skill_file: "device_admin", write_user_skill_file: "device_admin", // switch_session opens (and appends to) the named session file; import_jsonl reads any host file into a session // both write what the model reads as engine work: an assistant reply it never gave, a custom context message // (append_session_entry classes the same entries appended directly as device_admin) edit_assistant_message: "device_admin", send_custom_message: "device_admin", switch_session: "device_admin", import_jsonl: "device_admin", // both forms write a file on the host: without `outputPath`, into the session's cwd export_html: "device_admin", export_jsonl: "device_admin", // connection_local set_client_info: "connection_local", // a forwarder serves session streams itself; per-connection subscription state cannot be forwarded subscribe_sessions: "connection_local", unsubscribe_sessions: "connection_local", record_user_activity: "connection_local", detach_connection: "connection_local", release_session: "connection_local", // loads the extension graph for a caller-named cwd (device_admin in effect); safe only because connection_local is // never permitted on someone else's behalf warm: "connection_local", host_response: "connection_local", release_attachment: "connection_local", reclaim_session: "connection_local", move_session: "connection_local", // pages a recovery token this connection holds; every answer is scoped to the asking connection's peer incarnation get_recovery_page: "connection_local", // the receipt for an event lease this connection holds; it releases only that connection's lease release_recovery: "connection_local", // cancels an admission attempt of the asking connection's own socket epoch cancel_attempt: "connection_local", // resets the asking connection's own stream under an admission it completed reset_stream: "connection_local", }; /** * The class of an inbound record, or `undefined` when the engine cannot classify it: not a plain object, a * non-string `type`, a type the table does not own (own keys only, never the prototype's), or a record whose rule * throws (a hostile getter or proxy): a rule that cannot decide refuses. */ export function classifyRpcMethod(record) { if (typeof record !== "object" || record === null || Array.isArray(record)) return undefined; try { const type = Reflect.get(record, "type"); if (typeof type !== "string" || !Object.hasOwn(RPC_METHOD_CLASS, type)) return undefined; const entry = RPC_METHOD_CLASS[type]; return typeof entry === "string" ? entry : entry.classify(record); } catch { return undefined; } } /** * Whether a caller granted `granted` may send `record`. Fails closed: an unclassified record is refused whatever is * granted, `connection_local` is never permitted, and every other class only when the caller is granted that class. */ export function rpcMethodPermitted(record, granted) { const methodClass = classifyRpcMethod(record); return methodClass !== undefined && methodClass !== "connection_local" && granted.has(methodClass); } /** * sha256 (hex) of the sorted `type=value\n` lines, where `value` is the class, or `dynamic@<version>` for a * record-dependent entry. */ export function rpcMethodClassesDigest(table) { const lines = Object.entries(table) .map(([type, methodClass]) => `${type}=${methodClass}\n`) .sort(); return createHash("sha256").update(lines.join("")).digest("hex"); } /** The digest input of one entry: its class, or `dynamic@<version>` for a record-dependent entry. */ function digestValue(entry) { return typeof entry === "string" ? entry : `${DYNAMIC}@${entry.version}`; } export const RPC_METHOD_CLASSES_DIGEST = rpcMethodClassesDigest(Object.fromEntries(Object.entries(RPC_METHOD_CLASS).map(([type, entry]) => [type, digestValue(entry)]))); /** The whole table as data: what a client compares against its own copy. */ export function describeRpcMethodClasses() { const classes = {}; const dynamicVersions = {}; for (const [type, entry] of Object.entries(RPC_METHOD_CLASS)) { if (typeof entry === "string") { classes[type] = entry; } else { classes[type] = DYNAMIC; dynamicVersions[type] = entry.version; } } return { version: RPC_METHOD_CLASSES_VERSION, digest: RPC_METHOD_CLASSES_DIGEST, classes: classes, dynamic_versions: dynamicVersions, }; } //# sourceMappingURL=method-classes.js.map