mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-20 23:21:44 +00:00
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
// Resolves task runtime scope for agent harness launches.
|
|
import { normalizeDeliveryContext } from "../utils/delivery-context.shared.js";
|
|
import type { DeliveryContext } from "../utils/delivery-context.types.js";
|
|
|
|
const scopeRegistryKey = Symbol.for("openclaw.agentHarnessTaskRuntimeScope.registry");
|
|
|
|
// Host-issued scopes prevent plugins from fabricating requester ownership for task runs.
|
|
type ScopeRegistry = {
|
|
hostIssuedScopes: WeakSet<object>;
|
|
};
|
|
|
|
type GlobalWithScopeRegistry = typeof globalThis & {
|
|
[scopeRegistryKey]?: ScopeRegistry;
|
|
};
|
|
|
|
function getScopeRegistry(): ScopeRegistry {
|
|
const globalState = globalThis as GlobalWithScopeRegistry;
|
|
globalState[scopeRegistryKey] ??= {
|
|
hostIssuedScopes: new WeakSet<object>(),
|
|
};
|
|
return globalState[scopeRegistryKey];
|
|
}
|
|
|
|
export type AgentHarnessTaskRuntimeScope = {
|
|
readonly requesterSessionKey: string;
|
|
readonly requesterOrigin?: DeliveryContext;
|
|
};
|
|
|
|
/** Creates a host-issued task runtime scope for agent harness task execution. */
|
|
export function createAgentHarnessTaskRuntimeScope(params: {
|
|
requesterSessionKey: string;
|
|
requesterOrigin?: DeliveryContext;
|
|
}): AgentHarnessTaskRuntimeScope {
|
|
const requesterSessionKey = params.requesterSessionKey.trim();
|
|
if (!requesterSessionKey) {
|
|
throw new Error("Agent harness task runtime scope requires requesterSessionKey");
|
|
}
|
|
const requesterOrigin = normalizeDeliveryContext(params.requesterOrigin);
|
|
const scope: AgentHarnessTaskRuntimeScope = {
|
|
requesterSessionKey,
|
|
...(requesterOrigin ? { requesterOrigin } : {}),
|
|
};
|
|
getScopeRegistry().hostIssuedScopes.add(scope);
|
|
return scope;
|
|
}
|
|
|
|
export function assertAgentHarnessTaskRuntimeScope(
|
|
scope: AgentHarnessTaskRuntimeScope,
|
|
): AgentHarnessTaskRuntimeScope {
|
|
if (!getScopeRegistry().hostIssuedScopes.has(scope)) {
|
|
throw new Error("Agent harness task runtime requires a host-issued scope");
|
|
}
|
|
return scope;
|
|
}
|