Files
openclaw/src/agents/agent-tools.ring-zero-context.ts
T
Peter Steinberger 96f0983a85 fix(onboarding): skip setup for configured gateways and require inference first (#102883)
* fix(crestodian): keep onboarding RPCs restart-safe

* fix(profiles): isolate approval state migrations

* fix(crestodian): bypass configured gateway setup

* test(crestodian): type onboarding mocks

* fix(onboarding): require inference before Crestodian

* fix(onboarding): enforce verified inference handoff

* fix(macos): reset setup on gateway endpoint edits

* chore(i18n): refresh native source inventory

* fix(gateway): keep socket on request cancellation

* test(packaging): require workspace templates

* fix(onboarding): bind setup to verified inference

* fix(onboarding): align inference gate contracts

* fix(crestodian): classify concurrent policy rejection

* test(crestodian): expect registry restoration

* fix(onboarding): bind setup to configured gateways

* fix(codex): preserve startup phase deadlines

* test(crestodian): match fail-closed policy ordering

* test(onboarding): assert bound gateway handoff

* fix(codex): bind runtime resolution to spawn cwd

* test(crestodian): assert policy rejection order

* fix(cli): preserve gateway routing across restarts

* fix(macos): fail closed during gateway edits

* test(macos): cover gateway route generation races

* chore: keep release notes out of onboarding PR

* fix(ci): refresh onboarding generated checks

* style(swift): align gateway channel formatting

* fix(ci): refresh plugin SDK surface budgets

* fix(ci): resync native string inventory

* refactor(swift): split gateway channel support

* test(doctor): isolate plugin compatibility registry

* test(macos): isolate gateway onboarding fixtures

* test(macos): assert gateway lease health ordering

* fix(codex): reconcile computer-use startup changes
2026-07-11 10:25:14 -07:00

85 lines
2.9 KiB
TypeScript

import { AsyncLocalStorage } from "node:async_hooks";
import type { AnyAgentTool } from "./tools/common.js";
type AgentRingZeroToolScope = {
active: boolean;
tools: readonly AnyAgentTool[];
};
const activeRingZeroTools = new AsyncLocalStorage<AgentRingZeroToolScope>();
function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
if ((typeof value !== "object" || value === null) && typeof value !== "function") {
return false;
}
return "then" in value && typeof value.then === "function";
}
class HostScopedAgentToolAuthorizationError extends Error {
readonly status = 403;
constructor(message: string) {
super(message);
this.name = "HostScopedAgentToolAuthorizationError";
}
}
function bindToolToScope(tool: AnyAgentTool, scope: AgentRingZeroToolScope): AnyAgentTool {
const execute = tool.execute;
return {
...tool,
execute: async (toolCallId, params, signal, onUpdate) => {
// Plugins receive executable tool handles, so revoking discovery alone
// is insufficient. Possession remains valid while the harness promise is
// active because SDK callbacks can run on async resources created outside
// this ALS store; every retained handle fails after run settlement.
if (!scope.active) {
throw new HostScopedAgentToolAuthorizationError(
`host-scoped tool "${tool.name}" is no longer authorized for this run`,
);
}
return await execute(toolCallId, params, signal, onUpdate);
},
};
}
/**
* Bind host-owned tools to one selected harness run. The SDK reads this scope
* during tool construction, so plugins never receive private authority objects.
*/
export function runWithAgentRingZeroTools<T>(tools: readonly AnyAgentTool[], run: () => T): T {
const scope: AgentRingZeroToolScope = { active: true, tools: [] };
scope.tools = tools.map((tool) => bindToolToScope(tool, scope));
try {
const result = activeRingZeroTools.run(scope, run);
if (isPromiseLike(result)) {
return Promise.resolve(result).finally(() => {
scope.active = false;
}) as T;
}
scope.active = false;
return result;
} catch (error) {
scope.active = false;
throw error;
}
}
/** Read the host-owned tools bound to the current harness run. */
export function getActiveAgentRingZeroTools(): readonly AnyAgentTool[] {
const scope = activeRingZeroTools.getStore();
return scope?.active === true ? scope.tools : [];
}
/**
* Read a host-owned tool fact for the current run. This does not activate or
* grant a tool; only the host can bind executable authority to the run scope.
*/
export function isHostScopedAgentToolActive(toolName: string): boolean {
const normalizedName = toolName.trim().toLowerCase();
return (
normalizedName.length > 0 &&
getActiveAgentRingZeroTools().some((tool) => tool.name.trim().toLowerCase() === normalizedName)
);
}