openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
236 lines (235 loc) • 11.6 kB
JavaScript
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs";
import { n as createTrustedCronScheduledToolPolicy, t as createAccountCronScheduledToolPolicy } from "./scheduled-tool-policy-C_pucbBp.mjs";
import { n as bindGatewayDeviceRevocation } from "./device-revocation-Cbich35f.mjs";
import { i as tryResolveCronJobEffectiveAgentId, r as resolveCronJobEffectiveAgentId } from "./agent-id-CX8HtxAT.mjs";
import { n as resolveCronRunAdmissionSource } from "./run-authority-CJXQKUzJ.mjs";
import { a as getCronManagementAuthority, c as getCronManagementStreamExecPolicy, h as resolveCronCreatorStreamExecPolicy, m as resolveCronCreatorAuthorityGrantProvenance, o as getCronManagementCallerOrigin, r as consumeCronCreatorAuthorityGrant, s as getCronManagementChannelRequester } from "./cron-creator-authority-grant-pGPjtQX_.mjs";
import { r as resolveCronJobConfigRevision } from "./config-revision-ChGg5L01.mjs";
import { t as CRON_STREAM_EXEC_AUTHORITY_ERROR } from "./stream-exec-authority-CRx6XpZo.mjs";
import { t as assertActiveAgentRuntimeAuthority } from "./agent-runtime-authority-CtOnoFP2.mjs";
//#region src/gateway/server-methods/cron-caller-scope.ts
/** Check stored and requested kinds: metadata-only edits must not revive command jobs. */
function assertCronModelJobAllowed(e, t, n, r) {
if (e?.internal?.agentRuntimeIdentity && t.tools?.gatewayPolicy?.automations?.allowCommandJobs === false && (n.payload.kind === "command" || n.schedule.kind === "on-exit" || r?.payload?.kind === "command" || r?.schedule?.kind === "on-exit")) throw new TypeError("Gateway tool policy denies model changes to or runs of command automations.");
}
function resolveCronCreatorAuthorityCapture(e) {
const t = e?.cronCreatorAuthorityGrant;
if (!t) return;
if (resolveCronCreatorAuthorityGrantProvenance(t, t.runId)?.capturesRuntimeAuthority === false) return;
if (e.toolsAllowProvenance?.source !== "final-executable-surface") throw new TypeError("cron creator authority grant is missing tool-surface provenance");
let n;
return {
captureRuntimeAuthority() {
n = consumeCronCreatorAuthorityGrant(t);
return n.authority;
},
assertCurrent() {
n?.assertCurrent();
}
};
}
function resolveCronStreamExecPolicy(e, t, n) {
const r = e?.internal?.agentRuntimeIdentity;
if (!r || !t) return;
const i = getCronManagementAuthority(r) ? getCronManagementStreamExecPolicy(r) : r.cronCreatorAuthorityGrant ? resolveCronCreatorStreamExecPolicy(r.cronCreatorAuthorityGrant) : void 0;
if (!i) {
assertActiveAgentRuntimeAuthority(e, n);
throw new TypeError(CRON_STREAM_EXEC_AUTHORITY_ERROR);
}
return i;
}
function resolveCronMutationCommitGuard(e, t, n, r) {
const i = e?.internal?.agentRuntimeIdentity && t.validateAgentRuntimeApprovalAuthority;
const a = e?.internal?.agentRuntimeIdentity;
const o = a ? getCronManagementAuthority(a) : void 0;
const s = a?.cronCreatorAuthorityGrant;
const c = s && a && resolveCronCreatorAuthorityGrantProvenance(s, a.operationalRunInstance.runId)?.capturesRuntimeAuthority === false ? s : void 0;
if (!i && !n?.callerScope && !o && !c && !r?.sessionMutationCommitGuard && !r?.hasCurrentClientAuthority) return;
let l;
return bindGatewayDeviceRevocation(() => {
r?.sessionMutationCommitGuard?.();
if (r?.hasCurrentClientAuthority?.() === false) throw new TypeError("Gateway caller authority is no longer active.");
o?.();
if (a && r?.modelJobPolicy) {
const { config: n, jobId: i, patch: a } = r.modelJobPolicy;
const o = t.cron.getJob(i);
if (!o) throw new TypeError(`unknown cron job id: ${i}`);
assertCronModelJobAllowed(e, n, o, a);
}
if (i) assertActiveAgentRuntimeAuthority(e, t);
if (n?.callerScope) {
const r = readCronCallerScope(e);
const i = t.cron.getJob(n.jobId);
if (!r || !i || n.expectedConfigRevision !== void 0 && resolveCronJobConfigRevision(i) !== n.expectedConfigRevision || !cronJobMatchesCallerScope({
job: i,
callerScope: r,
defaultAgentId: t.cron.getDefaultAgentId(),
allowCurrentJob: n.allowCurrentJob
})) throw new TypeError(`unknown cron job id: ${n.jobId}`);
}
if (c) {
if (l) l.assertCurrent();
else l = consumeCronCreatorAuthorityGrant(c);
}
}, r?.hasCurrentClientAuthority);
}
function readCronCallerScope(e) {
const t = e?.internal?.agentRuntimeIdentity;
if (!t?.agentId) return;
const n = t.cronSelfManagementContext;
const r = n && Date.now() < n.expiresAtMs ? n.jobId.trim() || void 0 : void 0;
const i = t.turnSourceChannel?.trim().toLowerCase();
const a = getCronManagementAuthority(t);
const o = i ? {
kind: "external",
channel: i
} : t.turnSourceLocal === true ? { kind: "local" } : { kind: "unknown" };
const s = t.cronCreatorAuthorityGrant ? resolveCronCreatorAuthorityGrantProvenance(t.cronCreatorAuthorityGrant, t.operationalRunInstance.runId) : void 0;
const c = a ? getCronManagementChannelRequester(t) : s?.channelRequester;
const l = a ? getCronManagementCallerOrigin(t) : s?.callerOrigin;
const u = l ?? o;
const d = c && c.channel === i && c.accountId === normalizeAccountId(t.turnSourceAccountId) ? c : void 0;
const f = t.cronToolsAllowCapture === "final-executable-surface" ? {
version: 1,
source: "final-executable-surface",
callerOrigin: u
} : void 0;
const p = l ? {
version: 1,
source: "authenticated-requester",
callerOrigin: l,
...d ? { channelRequester: d } : {}
} : d ? {
version: 1,
source: "authenticated-requester",
channelRequester: d
} : void 0;
const m = f ? {
...f,
...d ? { channelRequester: d } : {}
} : p;
return {
kind: "agentTool",
...s?.creatorProfileId ? { creatorProfileId: s.creatorProfileId } : {},
agentId: normalizeAgentId(t.agentId),
sessionKey: t.sessionKey?.trim() || void 0,
accountId: normalizeAccountId(t.turnSourceAccountId),
currentJobId: r,
manageAll: a,
...m ? { toolsAllowProvenance: m } : {},
...f && t.cronExecToolTarget?.host === "gateway" ? { toolsAllowExecTarget: {
version: 1,
...t.cronExecToolTarget
} } : {},
...t.cronCreatorAuthorityGrant ? { cronCreatorAuthorityGrant: t.cronCreatorAuthorityGrant } : {}
};
}
/** Management access can reauthorize origin, but cannot lend another account native identity. */
function resolveCronRequesterProvenanceForJob(e, t) {
if (t?.manageAll && resolveCronRunAdmissionSource(e) === "operator-schedule") return;
const n = t?.toolsAllowProvenance;
if (!n?.channelRequester) return n;
if (e.owner?.sessionKey === t?.sessionKey && normalizeAccountId(e.owner?.accountId) === t?.accountId) return n;
if (n.source === "final-executable-surface") {
const { channelRequester: e, ...t } = n;
return t;
}
return n.callerOrigin ? {
version: 1,
source: "authenticated-requester",
callerOrigin: n.callerOrigin
} : void 0;
}
/** Converts the authenticated gateway caller into server-only scheduled authority provenance. */
function resolveCronScheduledToolPolicyForCaller(e) {
if (!e) return createTrustedCronScheduledToolPolicy();
const t = e.sessionKey ? createAccountCronScheduledToolPolicy({
ownerSessionKey: e.sessionKey,
ownerAccountId: e.accountId
}) : void 0;
if (!t) throw new TypeError("agent-runtime cron mutations require an authenticated session identity");
return t;
}
function parseAgentIdFromSessionRef(e, t) {
const n = e?.trim();
return n ? parseAgentSessionKey(n)?.agentId ?? t : void 0;
}
function resolveCronJobOwnerAgentId(e) {
const t = e.owner?.agentId?.trim() || parseAgentIdFromSessionRef(e.owner?.sessionKey);
return t ? normalizeAgentId(t) : void 0;
}
function isOperatorCommandCronJob(e) {
return e.payload.kind === "command" || e.schedule.kind === "on-exit" || e.schedule.kind === "stream" && e.scheduledToolPolicy?.mode !== "account";
}
/**
* Whether the caller is a trusted job's recorded owner conversation. Visibility accepts owner
* records written before account stamping; changing what runs needs the proven account.
*/
function cronTrustedJobOwnedByCaller(e, t, n = {}) {
const r = t.sessionKey?.trim();
const i = e.owner?.accountId;
return Boolean(r) && e.owner?.sessionKey?.trim() === r && (i ? normalizeAccountId(i) === t.accountId : !n.requireAccount);
}
/** Whether the caller holds a live scheduled-run claim for this job. */
function cronCallerRunsJob(e, t, n) {
return t?.currentJobId === e.id && tryResolveCronJobEffectiveAgentId(e, n) === t.agentId;
}
function cronJobMatchesCallerScope(e) {
if (!e.callerScope) return true;
if (e.callerScope.manageAll) {
e.callerScope.manageAll();
return true;
}
if (isOperatorCommandCronJob(e.job)) return false;
const t = tryResolveCronJobEffectiveAgentId(e.job, e.defaultAgentId);
const n = e.job.scheduledToolPolicy;
if (e.allowCurrentJob === true && cronCallerRunsJob(e.job, e.callerScope, e.defaultAgentId)) return true;
if (n && (n.mode === "trusted" ? !cronTrustedJobOwnedByCaller(e.job, e.callerScope) : e.callerScope.sessionKey?.trim() !== n.ownerSessionKey || e.job.owner?.sessionKey?.trim() !== n.ownerSessionKey || e.callerScope.accountId !== normalizeAccountId(n.ownerAccountId))) return false;
const r = e.job.owner?.accountId;
if (r && normalizeAccountId(r) !== e.callerScope.accountId) return false;
const i = resolveCronJobOwnerAgentId(e.job);
if (i) return i === e.callerScope.agentId;
if (t !== e.callerScope.agentId) return false;
return cronPatchSessionRefsMatchCaller(e.job, e.callerScope);
}
function cronJobMatchesDeclarationScope(e) {
if (e.callerScope) return (Boolean(e.callerScope.manageAll) || e.job.scheduledToolPolicy?.mode !== "trusted") && cronJobMatchesCallerScope(e);
if (normalizeAccountId(e.job.owner?.accountId) !== normalizeAccountId(e.input.owner?.accountId)) return false;
const t = e.input.owner?.sessionKey;
const n = resolveCronJobOwnerAgentId(e.input);
if (t && !n) return e.job.owner?.sessionKey === t;
const r = n ?? resolveCronJobEffectiveAgentId(e.input, e.defaultAgentId);
return (resolveCronJobOwnerAgentId(e.job) ?? tryResolveCronJobEffectiveAgentId(e.job, e.defaultAgentId)) === r;
}
function cronCreateMatchesCallerScope(e) {
if (!e.callerScope) return true;
if (resolveCronJobEffectiveAgentId(e.job, e.defaultAgentId) !== e.callerScope.agentId) return false;
return cronPatchSessionRefsMatchCaller(e.job, e.callerScope);
}
function applyCronCreateCallerScopeDefault(e, t) {
if (!t) return e;
return {
...e,
agentId: e.agentId?.trim() ? e.agentId : t.agentId,
owner: {
agentId: t.agentId,
...t.sessionKey ? { sessionKey: t.sessionKey } : {},
accountId: t.accountId
}
};
}
function cronPatchSessionRefsMatchCaller(e, t) {
if (!t || t.manageAll) {
t?.manageAll?.();
return true;
}
const n = e.sessionTarget?.trim();
return [e.sessionKey, n?.startsWith("session:") ? n.slice(8) : void 0].every((e) => {
const n = parseAgentIdFromSessionRef(e, t.agentId);
return !n || normalizeAgentId(n) === t.agentId;
});
}
//#endregion
export { cronJobMatchesCallerScope as a, cronTrustedJobOwnedByCaller as c, resolveCronJobOwnerAgentId as d, resolveCronMutationCommitGuard as f, resolveCronStreamExecPolicy as h, cronCreateMatchesCallerScope as i, readCronCallerScope as l, resolveCronScheduledToolPolicyForCaller as m, assertCronModelJobAllowed as n, cronJobMatchesDeclarationScope as o, resolveCronRequesterProvenanceForJob as p, cronCallerRunsJob as r, cronPatchSessionRefsMatchCaller as s, applyCronCreateCallerScopeDefault as t, resolveCronCreatorAuthorityCapture as u };