Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

feat(coding-agent): add host request contracts (#1238)

Seth Karten5e268e28f837
diff --git a/packages/coding-agent/src/core/kernel/index.ts b/packages/coding-agent/src/core/kernel/index.ts
index b760a2e1e2..f7916eab85 100644
--- a/packages/coding-agent/src/core/kernel/index.ts
+++ b/packages/coding-agent/src/core/kernel/index.ts
@@ -58,9 +58,85 @@ export const HOST_COMM_TARGET = "host.request";
/**
* Handles one typed request from Python code running in the kernel.
* The returned record is sent back verbatim as the comm reply payload.
+ *
+ * This legacy unary compatibility alias remains the dispatcher and registration
+ * contract while context-aware handlers are staged separately below.
*/
export type HostRequestHandler = (payload: Record<string, unknown>) => Promise<Record<string, unknown>>;
+/**
+ * Per-call authority supplied by the host-request dispatcher.
+ * `requestId` is an opaque host-minted correlation token and `isCurrent()`
+ * lets an implementation reject work after its authority is revoked.
+ */
+export interface HostRequestContext {
+ readonly requestId: string;
+ readonly generation: number;
+ readonly signal: AbortSignal;
+ isCurrent(): boolean;
+}
+
+const hostRequestHandlerBrand = Symbol("hostRequestHandler");
+
+/** A context-aware implementation that must receive dispatcher authority. */
+export type HostRequestHandlerImplementation = (
+ payload: Record<string, unknown>,
+ context: HostRequestContext,
+) => Promise<Record<string, unknown>>;
+
+/** A factory-minted, context-aware host-request handler capability. */
+type HostRequestHandlerCapability = HostRequestHandlerImplementation & { readonly [hostRequestHandlerBrand]: true };
+
+/** Runtime provenance cannot be recreated by copying the nominal symbol property. */
+const factoryCreatedHostRequestHandlers = new WeakSet<object>();
+
+function assertGenuineHostRequestContext(context: unknown): asserts context is HostRequestContext {
+ if (
+ typeof context !== "object" ||
+ context === null ||
+ typeof (context as HostRequestContext).requestId !== "string" ||
+ !(context as HostRequestContext).requestId ||
+ !Number.isSafeInteger((context as HostRequestContext).generation) ||
+ typeof (context as HostRequestContext).isCurrent !== "function" ||
+ typeof (context as HostRequestContext).signal !== "object" ||
+ (context as HostRequestContext).signal === null ||
+ typeof (context as HostRequestContext).signal.aborted !== "boolean" ||
+ typeof (context as HostRequestContext).signal.addEventListener !== "function"
+ ) {
+ throw new Error("host request context is invalid");
+ }
+}
+
+/**
+ * Creates a branded wrapper rather than mutating its implementation. Both its
+ * generic shape and runtime arity reject unary callbacks before they can run.
+ */
+export function createHostRequestHandler<T extends HostRequestHandlerImplementation>(
+ implementation: T,
+ ..._unaryRejection: Parameters<T> extends [unknown, unknown, ...unknown[]]
+ ? []
+ : ["host request handlers must accept payload and context"]
+): HostRequestHandlerCapability {
+ if (implementation.length < 2) throw new Error("host request handlers must accept payload and context");
+ const handler = async (payload: Record<string, unknown>, context: HostRequestContext) => {
+ assertGenuineHostRequestContext(context);
+ return implementation(payload, context);
+ };
+ factoryCreatedHostRequestHandlers.add(handler);
+ return Object.defineProperty(handler, hostRequestHandlerBrand, { value: true }) as HostRequestHandlerCapability;
+}
+
+/** Reject copied-symbol and raw-function forgeries before they observe authenticated payloads. */
+export function assertHostRequestHandler(value: unknown): asserts value is HostRequestHandlerCapability {
+ if (
+ typeof value !== "function" ||
+ (value as Partial<HostRequestHandlerCapability>)[hostRequestHandlerBrand] !== true ||
+ !factoryCreatedHostRequestHandlers.has(value)
+ ) {
+ throw new Error("host request handler is not a dispatcher-created capability");
+ }
+}
+
/** Host request handlers keyed by request type (e.g. "rlm.run", "goal.complete"). */
export type HostRequestHandlers = Record<string, HostRequestHandler>;
diff --git a/packages/coding-agent/test/host-request-context.ts b/packages/coding-agent/test/host-request-context.ts
new file mode 100644
index 0000000000..a74f1a4ee2
--- /dev/null
+++ b/packages/coding-agent/test/host-request-context.ts
@@ -0,0 +1,39 @@
+import {
+ createHostRequestHandler,
+ type HostRequestContext,
+ type HostRequestHandlerImplementation,
+} from "../src/core/kernel/index.js";
+
+let nextSyntheticHostRequestId = 0;
+
+/** Create a distinct, current dispatcher context for direct host-handler tests. */
+export function createSyntheticHostRequestContext(): HostRequestContext {
+ const requestNumber = ++nextSyntheticHostRequestId;
+ const controller = new AbortController();
+ return {
+ requestId: `test-host-request-${requestNumber}`,
+ generation: requestNumber,
+ signal: controller.signal,
+ isCurrent: () => !controller.signal.aborted,
+ };
+}
+
+/** Invoke a production-shaped handler with a synthetic dispatcher context. */
+export function invokeHostRequest(
+ handler: HostRequestHandlerImplementation,
+ payload: Record<string, unknown>,
+): Promise<Record<string, unknown>> {
+ return handler(payload, createSyntheticHostRequestContext());
+}
+
+/** Build staged context-aware fixture handlers through the production factory. */
+export function createTestHostHandlers<
+ T extends Record<
+ string,
+ (payload: Record<string, unknown>, context: HostRequestContext) => Promise<Record<string, unknown>>
+ >,
+>(handlers: T): Record<keyof T, HostRequestHandlerImplementation> {
+ return Object.fromEntries(
+ Object.entries(handlers).map(([type, handler]) => [type, createHostRequestHandler(handler)]),
+ ) as unknown as Record<keyof T, HostRequestHandlerImplementation>;
+}
diff --git a/packages/coding-agent/test/host-request-contract.test.ts b/packages/coding-agent/test/host-request-contract.test.ts
new file mode 100644
index 0000000000..2a22ceae02
--- /dev/null
+++ b/packages/coding-agent/test/host-request-contract.test.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+import {
+ assertHostRequestHandler,
+ createHostRequestHandler,
+ type HostRequestContext,
+} from "../src/core/kernel/index.js";
+
+describe("staged host-request handler authority", () => {
+ it("rejects unary factory inputs before they run", () => {
+ let calls = 0;
+ const unary = async (_payload: Record<string, unknown>): Promise<Record<string, unknown>> => {
+ calls += 1;
+ return {};
+ };
+
+ expect(() => {
+ // @ts-expect-error Staged handlers must accept dispatcher context as their second argument.
+ createHostRequestHandler(unary);
+ }).toThrow("host request handlers must accept payload and context");
+ expect(calls).toBe(0);
+ });
+
+ it("rejects missing or invalid context before a genuine handler runs", async () => {
+ let calls = 0;
+ const handler = createHostRequestHandler(async (_payload, _context) => {
+ calls += 1;
+ return {};
+ });
+
+ assertHostRequestHandler(handler);
+ // @ts-expect-error A staged handler cannot be invoked without dispatcher context.
+ await expect(handler({})).rejects.toThrow("host request context is invalid");
+ await expect(handler({}, {} as HostRequestContext)).rejects.toThrow("host request context is invalid");
+ expect(calls).toBe(0);
+ });
+
+ it("rejects copied-symbol forgeries through WeakSet provenance before they run", () => {
+ const genuine = createHostRequestHandler(async (_payload, _context) => ({}));
+ let calls = 0;
+ const forged = async (): Promise<Record<string, unknown>> => {
+ calls += 1;
+ return {};
+ };
+ const brand = Object.getOwnPropertySymbols(genuine)[0];
+ Object.defineProperty(forged, brand, Object.getOwnPropertyDescriptor(genuine, brand)!);
+
+ expect(() => assertHostRequestHandler(forged)).toThrow(
+ "host request handler is not a dispatcher-created capability",
+ );
+ expect(calls).toBe(0);
+ });
+});