openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
236 lines (235 loc) • 7.94 kB
JavaScript
import { r as normalizeCronScheduledToolCallerOrigin, s as resolveCronScheduledToolPolicy } from "./scheduled-tool-policy-C_pucbBp.mjs";
import { t as AUTOMATIONS_TOOL_NAME } from "./automations-tool-name-DNRDs0aJ.mjs";
import { a as isRuntimeToolAllowed } from "./tool-policy-match-DzwVnbV4.mjs";
import { t as decodeGatewayOperatorRecoverySource } from "./operator-run-recovery-source.codec-qx3kHk7N.mjs";
import { n as resolveCronRunAdmissionSource } from "./run-authority-CJXQKUzJ.mjs";
import { s as resolveCronSessionTargetSessionKey } from "./session-target-C-4MLah6.mjs";
//#region src/cron/execution-context.ts
/** Which requester an occurrence must recheck; the caller runs that owner's check. */
function resolveScheduledRequester(e) {
if (e.operatorSource) return {
kind: "operator-source",
source: decodeGatewayOperatorRecoverySource(e.operatorSource.source)
};
const t = e.toolsAllowProvenance?.channelRequester;
if (t) return {
kind: "channel",
requester: t
};
const n = profileIdOf(e.createdActor);
return n ? {
kind: "profile",
profileId: n
} : { kind: "unknown" };
}
function profileIdOf(e) {
return e?.type === "human" && e.source === "profile" ? e.id : void 0;
}
/** The occurrence's principal; occurrence admission and the builder share this resolution. */
function resolveScheduledPrincipal(e, t) {
if (t.trigger === "event") return t.ingress ? {
kind: "ingress",
ingress: t.ingress
} : { error: "event-triggered runs must name their ingress" };
if (e.operatorSource) {
let t;
try {
t = decodeGatewayOperatorRecoverySource(e.operatorSource.source);
} catch {
return { error: "stored operator source is invalid" };
}
return {
kind: "parent",
parent: {
agentId: t.agentId,
sessionKey: t.sessionKey
},
requester: {
kind: "operator-source",
source: t
}
};
}
const n = e.sourceConversation;
if (n) return {
kind: "parent",
parent: {
agentId: t.agentId,
sessionKey: n.sessionKey
},
requester: resolveScheduledRequester(e)
};
const r = e.scheduledToolPolicy;
const i = (r?.mode === "account" ? r.ownerSessionKey : void 0) ?? e.owner?.sessionKey;
if (i && r?.mode !== "trusted") return {
kind: "unbound-parent",
parentSessionKey: i
};
return {
kind: "operator",
basis: r?.mode === "trusted" ? "trusted-policy" : "unlinked-legacy"
};
}
/**
* A parent link names its conversation by key, so edits, branch switches and resets keep
* it current; its stored session id and lifecycle revision are ignored. Archive or
* deletion pauses the automation.
*/
function classifyScheduledParent(e) {
if (e.missing) return "removed";
return e.archived ? "archived" : e;
}
function resolveAuthority(e, t, n) {
const r = n.trigger === "cron" ? resolveCronRunAdmissionSource(e) : void 0;
const withSource = (e, t) => ({
owner: e,
basis: t,
...r ? { admissionSource: r } : {}
});
if (t.kind === "parent") {
const { requester: r } = t;
if (r.kind === "unknown") return e.scheduledToolPolicy?.mode === "trusted" ? withSource(true, "trusted-policy") : withSource(false, "none");
const i = n.requesterCheck?.owner === true;
const a = r.kind === "operator-source" ? "operator-source" : r.kind === "channel" ? "channel-owner" : "profile-role";
return withSource(i, i ? a : "none");
}
if (t.kind === "unbound-parent" || t.kind === "operator") return withSource(true, "legacy");
return withSource(false, "none");
}
function resolvePlacement(e, t) {
if (e.sessionTarget === "main" || resolveCronSessionTargetSessionKey(e.sessionTarget)) return "in-session";
return t.kind === "parent" ? "child" : "detached";
}
function resolveAutomationsScope(e, t, n, r) {
if (!isRuntimeToolAllowed("automations", r)) return { scope: "none" };
if (t.kind === "parent" && n.owner) return {
scope: "self-and-parent-wake",
jobId: e.id,
parentSessionKey: t.parent.sessionKey
};
return {
scope: "self",
jobId: e.id
};
}
function resolveInference(e) {
if (e.inferenceBinding) return {
kind: "bound",
binding: e.inferenceBinding
};
if (e.runtimeAuthority) return {
kind: "runtime",
authority: e.runtimeAuthority
};
if (e.runtimeAuthorityRecoveryRequired) return { kind: "runtime-recovery-required" };
return { kind: "none" };
}
/** Binds a stored parent link to the parent's current transcript and incarnation. */
function bindScheduledParent(e, t) {
if (e.kind !== "parent") return { principal: e };
if (!t) return {
status: "fail",
error: "parent session facts are required for a parent-linked job"
};
const n = classifyScheduledParent(t);
if (typeof n === "string") return {
status: "pause",
reason: n,
parentSessionKey: e.parent.sessionKey
};
const r = {
...e.parent,
sessionId: n.sessionId,
...n.lifecycleRevision ? { lifecycleRevision: n.lifecycleRevision } : {}
};
return {
principal: {
...e,
parent: r
},
liveRow: n
};
}
/** Builds the execution facts for one occurrence from stored fields and admission facts. */
function buildScheduledExecutionContext(e, t) {
const n = resolveScheduledPrincipal(e, t);
if ("error" in n) return {
status: "fail",
error: n.error
};
const r = bindScheduledParent(n, t.parentRow);
if ("status" in r) return r;
const { principal: i, liveRow: a } = r;
if (a?.worktree && !a.worktree.exists && i.kind === "parent") return {
status: "pause",
reason: "worktree-missing",
parentSessionKey: i.parent.sessionKey,
worktreePath: a.worktree.path
};
const o = resolvePlacement(e, i);
const s = resolveAuthority(e, i, t);
const c = t.resolvedToolsAllow ?? (e.payload.kind === "agentTurn" ? e.payload.toolsAllow : void 0);
const l = resolveCronScheduledToolPolicy({
toolsAllow: e.payload.kind === "agentTurn" ? e.payload.toolsAllow : void 0,
scheduledToolPolicy: e.scheduledToolPolicy,
owner: e.owner
});
const u = i.kind === "parent" && o === "child";
const d = o === "detached";
const f = u ? i.parent.sessionKey : t.runSessionKey;
const p = [];
if (t.agentSandboxMode === "all" || t.agentSandboxMode === "non-main" && t.runSessionKey !== t.agentMainSessionKey) p.push("agent-config");
if (t.creatorRoleSandbox === "required") p.push("creator-role");
if (a?.sandboxRequired) p.push("parent-row");
const m = e.toolsAllowProvenance?.channelRequester;
const h = l?.mode === "account" ? l.ownerAccountId : void 0;
return {
status: "ready",
facts: {
version: 1,
jobId: e.id,
occurrence: t.occurrence,
principal: i,
placement: o,
authority: s,
toolPolicy: {
...c ? { allow: c } : {},
...l ? { policy: l } : {},
callerOrigin: normalizeCronScheduledToolCallerOrigin(e.toolsAllowProvenance?.callerOrigin),
...e.toolsAllowExecTarget ? { execTarget: e.toolsAllowExecTarget } : {},
automations: resolveAutomationsScope(e, i, s, c)
},
resources: {
memory: i.kind === "parent" ? {
source: "parent",
parent: i.parent,
owner: s.owner
} : { source: "run-session" },
boardSessionKey: f,
mediaSessionKey: f,
eventTargetSessionKey: d ? t.agentMainSessionKey : f,
workspace: u && a?.workspaceDir ? {
source: "parent-row",
dir: a.workspaceDir,
...a.cwd ? { cwd: a.cwd } : {},
...a.worktree ? { worktreePath: a.worktree.path } : {}
} : { source: "run-session" },
fileScope: o === "in-session" ? "session" : "root",
bootstrap: d ? "cron-allowlist" : "default",
sandbox: {
required: p.length > 0,
basis: p
},
...e.toolsAllowExecTarget ? { execHost: e.toolsAllowExecTarget } : {},
messageAccounts: {
...h ? { ownerAccountId: h } : {},
...m ? { channelRequester: m } : {}
},
delivery: e.sourceConversation ? { sourceConversation: e.sourceConversation } : {}
},
inference: resolveInference(e)
}
};
}
//#endregion
export { resolveScheduledRequester as i, classifyScheduledParent as n, resolveScheduledPrincipal as r, buildScheduledExecutionContext as t };