File size: 4,008 Bytes
4c76b0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | /** Lazy runtime adapter for plugin-owned embedded-agent execution. */
import { randomUUID } from "node:crypto";
import {
createOperationalRunInstanceRef,
getAdmittedRunDelegatedAuthority,
prepareAgentRunAdmission,
type AdmittedRunContext,
} from "../../agents/admitted-run-context.js";
import { runEmbeddedAgent as runEmbeddedAgentCore } from "../../agents/embedded-agent.js";
import { recordRuntimeActionDecision } from "../../audit/runtime-action-decision.js";
import { getRuntimeConfig } from "../../config/config.js";
import { getPluginRuntimeGatewayRequestScope } from "./gateway-request-scope.js";
import type { PluginRuntime } from "./types.js";
export const runPluginEmbeddedAgent: PluginRuntime["agent"]["runEmbeddedAgent"] = async (
params,
) => {
const pluginId = getPluginRuntimeGatewayRequestScope()?.pluginId;
if (!pluginId) {
throw new Error("Plugin embedded-agent execution requires an active plugin runtime scope.");
}
if (
"admittedRunContext" in params ||
"preparedRunAdmission" in params ||
"compactionCountOwner" in params ||
"onCompactionAccounting" in params ||
"onContextAccountingEvent" in params ||
"onDeferredLifecycleOwner" in params ||
"onDeferredLifecycleAbort" in params ||
"onRetryWait" in params
) {
throw new Error("Plugin embedded-agent execution cannot supply host run authority.");
}
params.abortSignal?.throwIfAborted();
const decisionOccurrenceId = randomUUID();
let admittedRunContext: AdmittedRunContext | undefined;
const config = params.config ?? getRuntimeConfig();
const preparedRunAdmission = prepareAgentRunAdmission({
cfg: config,
operationalRunInstance: createOperationalRunInstanceRef(params.runId),
facts: {
runId: params.runId,
agentId: params.sessionTarget?.agentId ?? params.agentId ?? "main",
ingress: {
kind: "plugin",
boundary: "plugin-runtime",
rawSourceRef: pluginId,
state: "present",
},
},
onAdmitted: (context) => {
admittedRunContext = context;
const token = context.executionIdentityToken;
recordRuntimeActionDecision({
token,
family: "plugin",
operation: "run",
outcome: "allowed",
coverageState: "enforced",
reasonCode: "plugin_runtime_owner_admitted",
owner: "plugin-runtime",
decisionBoundary: "plugin.runtime.run-embedded-agent",
policyRefs: ["plugin:registered-owner", "run:admission"],
summary: "The registered plugin owner passed exact run admission.",
remediation: [],
discriminator: JSON.stringify([pluginId, params.runId, decisionOccurrenceId, "admission"]),
});
},
});
let closed = false;
const close = () => {
if (!closed) {
closed = true;
preparedRunAdmission.close();
}
};
// Abort owns authority revocation independently of core completion; the
// post-registration check closes the prepare-to-listener race.
params.abortSignal?.addEventListener("abort", close, { once: true });
try {
params.abortSignal?.throwIfAborted();
const result = await runEmbeddedAgentCore({ ...params, config, preparedRunAdmission });
if (admittedRunContext && getAdmittedRunDelegatedAuthority(admittedRunContext)) {
recordRuntimeActionDecision({
token: admittedRunContext.executionIdentityToken,
family: "plugin",
operation: "run",
outcome: "allowed",
coverageState: "attribution-only",
reasonCode: "plugin_runtime_completed",
owner: "plugin-runtime",
decisionBoundary: "plugin.runtime.run-embedded-agent",
summary: "The plugin-owned runtime completed; this is attribution, not authorization.",
remediation: [],
discriminator: JSON.stringify([pluginId, params.runId, decisionOccurrenceId, "completion"]),
});
}
return result;
} finally {
params.abortSignal?.removeEventListener("abort", close);
close();
}
};
|