@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
241 lines • 12.8 kB
JavaScript
/**
* Delegated connections (senpi#5632): the engine enforces its own method-class table on a connection that speaks for
* someone else, so a forwarder (a relay host forwarding a remote viewer's records) keeps no table of its own.
*
* A connection that proved the endpoint's launch key may declare, before any record other than `get_protocol_info`
* or `authenticate`, that it acts for a principal with a set of granted classes (`declare_delegation`). A later
* declaration may only narrow it. From then on every inbound record on that connection is classified
* (`classifyRpcMethod`) and checked against the grant here, synchronously at line receipt and before anything else
* sees it: lines of one connection are not serialized, so a declaration and the record written right behind it are
* decided in order. A permitted record then runs exactly as on a local connection, schema problems included: only what
* the local path also refuses before dispatch (not an object, no string `type`) is refused here as malformed, so a
* delegated connection accepts exactly what a local one accepts.
*
* `connection_local` records follow `CONNECTION_LOCAL_REQUIREMENT`. A connection that never declares is not affected: the
* only records this module answers on it are the two methods it adds (`declare_delegation`, `classify_record`).
*
* Neither method is in `RPC_METHOD_CLASS`: adding them would change `RPC_METHOD_CLASSES_DIGEST`, and a forwarder that
* pins the digest would then refuse every record (senpi#5632 rollout split, S1a).
*
* senpi#4249 R-1b: on a delegated connection a private presenter record (`mcp_form_*`, `mcp_presenter_*`) is decided
* by `delegated-presenter-records.ts` and always answered in the private reply shape; `mcp_presenter_control_info`
* is answered only on a launch-proven connection that is not delegated.
*
* senpi#6752: the `machine_op` records (`machine-op/wire.ts`) are, like these two, outside `RPC_METHOD_CLASS`. They run
* device operations for the local relay host, so they are answered only on a launch-proven connection that has not
* declared a delegation; any other connection gets `machine_op_not_authorized` and nothing runs.
*/
import { INCARNATION_MAX } from "./connection-authority.js";
import { CLASSIFY_RECORD_COMMAND, classifyRecord } from "./delegation-classify.js";
import { parseDeclaration } from "./delegation-declaration.js";
import { closeRecord, createRecord, descriptorOf, echoedRoles, narrowRecord, rotateIncarnation, } from "./delegation-record.js";
import { refused, requestId } from "./delegation-wire.js";
import { isPrivatePresenterType, PRESENTER_CONTROL_INFO_COMMAND, privatePresenterVerdict, } from "./delegated-presenter-records.js";
import { classifyRpcMethod, RPC_METHOD_CLASSES_DIGEST, } from "./method-classes.js";
import { isMachineOpCommand, machineOpRefusal } from "./machine-op/wire.js";
import { RPC_LAUNCH_PROOF_RECORD } from "./rpc-auth-proof.js";
const DECLARE_DELEGATION_COMMAND = "declare_delegation";
/**
* What a delegated connection needs for each `connection_local` method (DS2b section 2c): `self` acts only on the
* asking connection; `subscribe_sessions` reveals the same rows as `list_sessions` (`observe`); `record_user_activity`
* changes session-wide activity the agent's timing reads (`drive`); `device_admin` reaches the host (hands a session
* over, loads a cwd's extensions, takes a session by host path, moves session files). `set_client_info` stays `self`
* and unfiltered: a viewer's roles come from its own records. Exhaustive, so a new `connection_local` method fails the
* typecheck until it is placed.
*/
const CONNECTION_LOCAL_REQUIREMENT = {
set_client_info: "self",
subscribe_sessions: "observe",
unsubscribe_sessions: "self",
record_user_activity: "drive",
detach_connection: "self",
release_session: "device_admin",
warm: "device_admin",
host_response: "self",
release_attachment: "self",
reclaim_session: "device_admin",
move_session: "device_admin",
get_recovery_page: "self",
release_recovery: "self",
cancel_attempt: "self",
reset_stream: "self",
};
/** The records a connection may send before its first declaration without making that declaration late. */
const PRE_DECLARATION = new Set(["get_protocol_info", "authenticate", RPC_LAUNCH_PROOF_RECORD]);
const PASS = { kind: "pass" };
const CONSUMED = { kind: "consumed" };
/** Per-connection delegation state of one host; keyed by the connection id the writer runs each line under. */
export class ConnectionDelegations {
constructor(options = {}) {
this.connections = new Map();
this.incarnationMax = options.incarnationMax ?? INCARNATION_MAX;
this.endConnection = options.endConnection;
}
admit(id, proofSource) {
// A re-registration under a live id keeps its state: forgetting a delegation would widen it.
if (this.connections.has(id))
return;
this.connections.set(id, {
proofSource,
proven: proofSource === "proven",
spoken: false,
delegation: undefined,
});
}
/**
* Physical close (omo-relay-daemon#307): a delegated record's current word reads retired from now on, and the record
* stays, so a record still in flight never reads "not delegated". Only `forget`, once the socket is gone, deletes it.
*/
close(id) {
const delegation = this.connections.get(id)?.delegation;
if (delegation === undefined)
this.connections.delete(id);
else
closeRecord(delegation);
}
forget(id) {
this.connections.delete(id);
}
recordOf(id) {
return this.connections.get(id)?.delegation;
}
/** What a line of connection `id` captures before its first await; undefined for a connection that never declared. */
descriptorOf(id) {
const record = this.recordOf(id);
return record === undefined ? undefined : descriptorOf(record);
}
viewOf(id) {
const record = this.recordOf(id);
return record === undefined ? undefined : { descriptor: descriptorOf(record), closed: record.closed };
}
/** After a line's answer is enqueued: ends the socket whose `detach_connection` exhausted its incarnations. */
lineAnswered(id) {
const state = id === undefined ? undefined : this.connections.get(id);
if (id === undefined || state?.endAfterAnswer !== true)
return;
delete state.endAfterAnswer;
this.endConnection?.(id);
}
isDelegated(id) {
return id !== undefined && this.connections.get(id)?.delegation !== undefined;
}
/** The answer to a line that is not JSON: today's parse error, typed `malformed` on a delegated connection. */
unparsable(id, error) {
return {
type: "response",
command: "parse",
success: false,
error,
...(this.isDelegated(id) ? { errorCode: "delegation_malformed" } : {}),
};
}
/** Decides one parsed inbound record of connection `id`. Synchronous: it runs before any await of the line. */
inspect(id, record) {
const state = id === undefined ? undefined : this.state(id);
const type = typeof record === "object" && record !== null ? Reflect.get(record, "type") : undefined;
// A connection with a proof source only receives this record from its own transport (the filters refuse it from
// clients); one proven at admission (the Windows supervisor's hop) already counts and just drops it.
if (type === RPC_LAUNCH_PROOF_RECORD && state?.proofSource !== undefined) {
state.proven = true;
return CONSUMED;
}
if (type === DECLARE_DELEGATION_COMMAND)
return { kind: "reply", response: this.declaration(state, record) };
if (type === CLASSIFY_RECORD_COMMAND) {
if (state)
state.spoken = true;
return { kind: "reply", response: classifyRecord(record) };
}
if (isMachineOpCommand(type)) {
if (state)
state.spoken = true;
const id = requestId(record);
return state?.proven === true && state.delegation === undefined
? { kind: "machine_op", id, command: type }
: { kind: "reply", response: machineOpRefusal(id, type) };
}
if (type === PRESENTER_CONTROL_INFO_COMMAND && state?.delegation === undefined) {
if (state)
state.spoken = true;
return state?.proven === true
? { kind: "control_info", id: requestId(record) }
: {
kind: "reply",
response: refused(requestId(record), PRESENTER_CONTROL_INFO_COMMAND, "presenter_not_authorized", "mcp_presenter_control_info needs a connection that proved the endpoint's launch key"),
};
}
if (state === undefined)
return PASS;
if (!PRE_DECLARATION.has(type))
state.spoken = true;
const delegation = state.delegation;
if (delegation === undefined)
return PASS;
if (isPrivatePresenterType(type))
return privatePresenterVerdict(type, requestId(record), delegation.granted);
const refusal = refuseOnDelegation(record, delegation.granted);
if (refusal !== undefined)
return { kind: "reply", response: refusal };
// A logical detach retires this incarnation at line receipt: every later line captures the next one.
if (type === "detach_connection" && !rotateIncarnation(delegation, this.incarnationMax))
state.endAfterAnswer = true;
return PASS;
}
state(id) {
let state = this.connections.get(id);
if (state === undefined) {
state = { proofSource: undefined, proven: false, spoken: false, delegation: undefined };
this.connections.set(id, state);
}
return state;
}
declaration(state, record) {
const id = requestId(record);
const parsed = parseDeclaration(record);
if (typeof parsed === "string")
return refused(id, DECLARE_DELEGATION_COMMAND, "delegation_malformed", parsed);
if (state === undefined || !state.proven)
return refused(id, DECLARE_DELEGATION_COMMAND, "delegation_unauthenticated", "declare_delegation needs a connection that proved the endpoint's launch key");
let current = state.delegation;
if (current !== undefined) {
if (!narrowRecord(current, parsed))
return refused(id, DECLARE_DELEGATION_COMMAND, "delegation_fixed", "a delegated connection keeps its principal and mode and can only narrow its grant and roles");
}
else if (state.spoken) {
return refused(id, DECLARE_DELEGATION_COMMAND, "delegation_late", "declare_delegation must come before any record other than get_protocol_info or authenticate");
}
else {
current = createRecord(parsed.principal, parsed.granted, parsed.roles);
state.delegation = current;
}
return {
...(id === undefined ? {} : { id }),
type: "response",
command: DECLARE_DELEGATION_COMMAND,
success: true,
data: { granted: [...parsed.granted], digest: RPC_METHOD_CLASSES_DIGEST, ...echoedRoles(current) },
};
}
}
function refuseOnDelegation(record, granted) {
const object = typeof record === "object" && record !== null && !Array.isArray(record) ? record : undefined;
const id = object === undefined ? undefined : requestId(object);
const type = object === undefined ? undefined : Reflect.get(object, "type");
if (typeof type !== "string")
return refused(id, "parse", "delegation_malformed", "a record is a JSON object with a string type");
const command = type;
const methodClass = classifyRpcMethod(record);
if (methodClass === undefined)
return refused(id, command, "delegation_unclassified", `the engine does not classify ${command}`);
if (methodClass !== "connection_local")
return granted.has(methodClass)
? undefined
: refused(id, command, "delegation_not_granted", `${command} needs the ${methodClass} grant`, {
class: methodClass,
});
const required = CONNECTION_LOCAL_REQUIREMENT[command];
if (required === "self" || granted.has(required))
return undefined;
return refused(id, command, "delegation_local_only", `${command} on a delegated connection needs the ${required} grant`, { class: required });
}
//# sourceMappingURL=delegated-connection.js.map