/** Owned executable versions; the discovery registry never supplies execution authority. */ import { ConvexError, v } from "convex/values"; import { fullRootProvenanceFor, rootProvenanceEncodings, rootProvenanceEqual, rootProvenanceMatches } from "./runtimePolicySchema"; import { z } from "zod"; import { mutation } from "./_generated/server"; import type { QueryCtx } from "./_generated/server"; import type { Doc } from "./_generated/dataModel"; import type { RuntimeAuthorityReference } from "../src/auth/verified"; import { canonicalModelJson, MODEL_OPERATION_KEYS, resolveModelPolicy } from "../src/domain/model-policy"; import type { ModelOperationKey, TrustedModelPolicyRecord } from "../src/domain/model-policy"; import { semanticHash } from "../src/domain/profile"; import { assertUnicode, boundedIdentifier, clientRequestKey, utf8Bytes } from "../src/domain/transcript"; import { requireDataAuthority, dataAuthorityReference } from "./runtimeDataAuth"; import { recheckAuthority, readDeploymentPolicyRows, readTenantPolicyRows, readExecutableDefinitionRows } from "./runtimeAuth"; import { lookupDeploymentManifest } from "./runtimeModelPolicyBootstrap"; import { NATIVE_TOOL_NAMES_V2 as NATIVE_TOOL_NAMES } from "../src/domain/agent-tools"; import { validateMemoryEffortPolicy } from "../src/domain/memory-effort"; import { modelInputPolicy } from "../src/domain/model-input-selection"; export const EXECUTABLE_REGISTRY = Object.freeze({ version: 1, tools: Object.freeze({ "memory.remember": 1, "memory.recall": 1 }), contexts: Object.freeze({ "cortex.transcript": 1 }) }); export async function executableRegistryHash(): Promise { return await semanticHash(canonicalModelJson(EXECUTABLE_REGISTRY)); } export const EXECUTABLE_REGISTRY_V2 = Object.freeze({ version: 2, tools: Object.freeze({ ...EXECUTABLE_REGISTRY.tools, "media.image.generate": 1, "media.video.generate": 1, "artifact.read": 1 }), contexts: EXECUTABLE_REGISTRY.contexts }); export async function executableRegistryHashV2(): Promise { return await semanticHash(canonicalModelJson(EXECUTABLE_REGISTRY_V2)); } export const EXECUTABLE_REGISTRY_V3 = Object.freeze({ version: 3, tools: EXECUTABLE_REGISTRY_V2.tools, contexts: Object.freeze({ ...EXECUTABLE_REGISTRY.contexts, "cortex.transcript.relevant": 1 }) }); export async function executableRegistryHashV3(): Promise { return await semanticHash(canonicalModelJson(EXECUTABLE_REGISTRY_V3)); } export const EXECUTABLE_REGISTRY_V4 = Object.freeze({version:4,tools:EXECUTABLE_REGISTRY_V3.tools,contexts:Object.freeze({...EXECUTABLE_REGISTRY_V3.contexts,"cortex.transcript.relevant":2})}); export async function executableRegistryHashV4():Promise{return await semanticHash(canonicalModelJson(EXECUTABLE_REGISTRY_V4));} function denied(code = "POLICY_DENIED"): never { throw new ConvexError({ version: 1, code, message: "Agent definition control failed.", retryable: false, outcome: "not_dispatched" }); } function unicode(s: string): boolean { try { assertUnicode(s); return true; } catch { return false; } } const text = z.string().min(1).max(256).refine(s => s.trim() === s && unicode(s)); const positive = z.number().int().min(1).max(Number.MAX_SAFE_INTEGER); const count = z.number().int().min(0).max(Number.MAX_SAFE_INTEGER); const rules = z.object({ modelId: text.optional(), nativeInterface: z.enum(["chat", "responses", "messages", "embedding", "image", "video"]).optional(), allowedModels: z.array(text).min(1).optional(), interfaces: z.array(z.enum(["chat", "responses", "messages", "embedding", "image", "video"])).min(1).optional(), capabilities: z.array(z.enum(["text", "structured", "tools", "embedding", "image", "video"])).min(1).optional(), allowedOptions: z.array(z.enum(["temperature", "topP", "seed", "stopSequences"])).optional(), allowedTools: z.array(text).optional(), fallbackModels: z.array(text).optional(), limits: z.object({ inputTokens: positive, contextTokens: positive, outputTokens: positive, toolSteps: count, timeoutMs: positive, operationBudgetUnits: positive, runBudgetUnits: positive, tenantBudgetUnits: positive, concurrency: positive }).partial().strict().optional(), pins: z.object({ promptVersion: text, schemaVersion: text, extractionVersion: text, toolRegistryVersion: text, priceRevision: text, boundRevision: text }).partial().strict().optional(), }).strict(); const policyInput = z.object({ policyId: text, version: positive, operationKey: z.enum(MODEL_OPERATION_KEYS).optional(), rules }).strict(); const selectionInput = z.object({ version: z.literal(1), recentMessages: z.number().int().min(1).max(16), relevantMemories: z.number().int().min(0).max(10), relevantFacts: z.number().int().min(0).max(10), pendingEntries: z.number().int().min(0).max(32), maxMessages: z.number().int().min(1).max(32) }).strict(); const effortPolicyInput=z.object({version:z.literal(1),maxDepth:positive.optional(),maxEntries:count.optional(),maxContextTokens:positive.optional(),maxWork:positive.optional(),retrievalDeadlineMs:positive.optional()}).strict(); const selectionInputV2=z.object({version:z.literal(2),effort:effortPolicyInput.optional(),recentMessages:positive.optional(),relevantMemories:count.optional(),relevantFacts:count.optional(),pendingEntries:count.optional(),maxMessages:positive.optional(),optionalEntries:count.nullable().optional()}).strict(); const definitionInput = z.object({ instructions: z.object({ text: z.string().max(60_000).refine(unicode), promptVersion: text }).strict(), tools: z.array(z.object({ toolId: text, toolVersion: positive, registryHash: z.string().regex(/^[a-f0-9]{64}$/), requiredCapability: z.literal("tool"), approval: z.enum(["none", "explicit"]) }).strict()).max(32), context: z.object({ handlerId: text, handlerVersion: positive, registryHash: z.string().regex(/^[a-f0-9]{64}$/), memoryMode: z.enum(["indexed-plus-pending", "strict"]), requiredStages: z.array(z.enum(["facts", "vectors", "graph"])).max(3), maxContextTokens: positive, barrier: z.object({ deadlineMs: z.number().int().min(1).max(120_000), onFailure: z.enum(["fail", "incomplete"]) }).strict().optional(), inputSelection: z.union([selectionInput,selectionInputV2]).optional() }).strict(), policies: z.array(policyInput).min(1).max(32), }).strict(); export type ExecutableDefinitionV1 = Omit, "policies"> & { version: 1; agentId: string; agentVersion: number; tenantId: string; memorySpaceId: string; ownerPrincipalId: string; policies: TrustedModelPolicyRecord[]; }; function one(rows: T[]): T | undefined { if (rows.length > 1) denied(); return rows[0]; } function decodeInput(input: unknown): z.infer { let detached: unknown; try { const bytes = canonicalModelJson(input); if (utf8Bytes(bytes) > 180_000) denied("INVALID_INPUT"); detached = JSON.parse(bytes); } catch { return denied("INVALID_INPUT"); } const parsed = definitionInput.safeParse(detached); if (!parsed.success) denied("INVALID_INPUT"); if(parsed.data.context.handlerId==="cortex.transcript.relevant" && parsed.data.context.handlerVersion===2) { const selected=parsed.data.context.inputSelection; if(selected && selected.version!==2)denied("INVALID_INPUT"); const policy=modelInputPolicy({version:2,effort:validateMemoryEffortPolicy(selected && selected.version===2?selected.effort:undefined)} as Parameters[0]); parsed.data.context.inputSelection=policy; } return parsed.data; } async function registry(input: z.infer): Promise { const hash = input.context.registryHash; const catalog = hash === await executableRegistryHash() ? EXECUTABLE_REGISTRY : hash === await executableRegistryHashV2() ? EXECUTABLE_REGISTRY_V2 : hash === await executableRegistryHashV3() ? EXECUTABLE_REGISTRY_V3 : hash === await executableRegistryHashV4() ? EXECUTABLE_REGISTRY_V4 : undefined; if (!catalog) denied(); const seen = new Set(); for (const tool of input.tools) { if (seen.has(tool.toolId) || !Object.prototype.hasOwnProperty.call(catalog.tools, tool.toolId) || catalog.tools[tool.toolId as keyof typeof catalog.tools] !== tool.toolVersion || tool.registryHash !== hash) denied(); seen.add(tool.toolId); } if (!Object.prototype.hasOwnProperty.call(catalog.contexts, input.context.handlerId) || catalog.contexts[input.context.handlerId as keyof typeof catalog.contexts] !== input.context.handlerVersion || input.context.registryHash !== hash || new Set(input.context.requiredStages).size !== input.context.requiredStages.length) denied(); if (input.context.handlerId === "cortex.transcript.relevant") { try { const policy=modelInputPolicy(input.context.inputSelection as Parameters[0]); if(policy.version !== input.context.handlerVersion)denied("INVALID_INPUT"); } catch { denied("INVALID_INPUT"); } } else if (input.context.inputSelection !== undefined) denied("INVALID_INPUT"); if (new Set(input.policies.map(p => p.operationKey ?? "*")).size !== input.policies.length || !input.policies.some(p => p.operationKey === undefined || p.operationKey === "agent.respond")) denied(); } /** Same root/tenant narrowing as the existing policy resolver, with no second budget implementation. */ async function narrowPolicies(ctx: Pick, reference: RuntimeAuthorityReference, definition: ExecutableDefinitionV1): Promise { const roots = await readDeploymentPolicyRows(ctx); const root = roots[0]?.root; const manifest = root && lookupDeploymentManifest(root.manifestId, root.revision); if (!manifest || !roots.length || roots.length > 64) denied("CAPABILITY_UNAVAILABLE"); const manifestCanonical = canonicalModelJson(manifest); const expected = rootProvenanceEncodings(fullRootProvenanceFor(manifest, manifestCanonical, await semanticHash(manifestCanonical)), canonicalModelJson); if (roots.some(row => row.revokedAt !== undefined || !rootProvenanceEqual(canonicalModelJson(row.root), canonicalModelJson(root), expected))) denied("CAPABILITY_UNAVAILABLE"); if (!rootProvenanceMatches(canonicalModelJson(root), expected)) denied(); const tenantRows = await readTenantPolicyRows(ctx, reference.tenantId); if (tenantRows.length > 64 || tenantRows.some(row => row.revokedAt !== undefined || !rootProvenanceEqual(canonicalModelJson(row.root), canonicalModelJson(root), expected))) denied(); const tenantPolicies = tenantRows.map(row => JSON.parse(row.canonical) as TrustedModelPolicyRecord); const keys = [...new Set(definition.policies.map(p => p.operationKey ?? "agent.respond"))]; for (const key of keys) { const policies = [...manifest.policies.filter(p => p.scope === "deployment" && (!p.operationKey || p.operationKey === key)), ...tenantPolicies.filter(p => !p.operationKey || p.operationKey === key), ...definition.policies.filter(p => !p.operationKey || p.operationKey === key)]; try { const resolved = resolveModelPolicy({ context: { scope: reference, agent: { agentId: definition.agentId, version: definition.agentVersion }, operationKey: key, runBudgetId: "definition-validation", authorizedOverrides: [] }, policies, qualifications: manifest.qualifications }); if (key === "agent.respond" && (definition.context.maxContextTokens > resolved.limits.contextTokens || definition.tools.some(t => !resolved.allowedTools.includes(NATIVE_TOOL_NAMES[t.toolId as keyof typeof NATIVE_TOOL_NAMES])))) denied(); } catch { denied(); } } } async function rowFor(ctx: Pick, reference: RuntimeAuthorityReference, agent: { agentId: string; version: number }) { return one(await readExecutableDefinitionRows(ctx, reference.tenantId, reference.memorySpaceId!, agent.agentId, agent.version)); } async function checkRow(ctx: Pick, reference: RuntimeAuthorityReference, row: Doc<"runtimeAgentDefinitions">, capability: "run" | "admin", allowRevoked = false): Promise { if (!reference.memorySpaceId || row.tenantId !== reference.tenantId || row.memorySpaceId !== reference.memorySpaceId || (!allowRevoked && row.revokedAt !== undefined)) denied(); await recheckAuthority(ctx, reference, { capability, resource: { resourceType: "source", resourceId: canonicalModelJson(["executable-agent", row.agentId, row.version]), tenantId: row.tenantId, memorySpaceId: row.memorySpaceId, ownerPrincipalId: row.ownerPrincipalId } }); let value: ExecutableDefinitionV1; try { value = JSON.parse(row.definitionCanonical) as ExecutableDefinitionV1; } catch { return denied(); } if (canonicalModelJson(value) !== row.definitionCanonical || await semanticHash(row.definitionCanonical) !== row.definitionHash || value.version !== 1 || value.agentId !== row.agentId || value.agentVersion !== row.version || value.tenantId !== row.tenantId || value.memorySpaceId !== row.memorySpaceId || value.ownerPrincipalId !== row.ownerPrincipalId) denied(); const { version: _version, agentId: _agentId, agentVersion: _agentVersion, tenantId: _tenantId, memorySpaceId: _memorySpaceId, ownerPrincipalId: _owner, policies: fullPolicies, ...input } = value; const policies = fullPolicies.map(policy => { if (policy.scope !== "agent" || policy.tenantId !== row.tenantId || policy.agentId !== row.agentId || policy.agentVersion !== row.version || policy.override) denied(); const { scope: _scope, tenantId: _tenant, agentId: _agent, agentVersion: _agentV, ...body } = policy; return body; }); const decoded = decodeInput({ ...input, policies }); await registry(decoded); return value; } export async function readCurrentExecutableDefinition(ctx: Pick, reference: RuntimeAuthorityReference, selector: { agentId: string; version: number }, requirement: { capability: "run" | "admin"; operationKey?: ModelOperationKey }) { boundedIdentifier(selector.agentId); if (!Number.isSafeInteger(selector.version) || selector.version < 1) denied("INVALID_INPUT"); const row = await rowFor(ctx, reference, selector); if (!row) denied("CAPABILITY_UNAVAILABLE"); const definition = await checkRow(ctx, reference, row, requirement.capability); await narrowPolicies(ctx, reference, definition); await recheckAuthority(ctx, reference, { capability: requirement.capability, tenantId: reference.tenantId, memorySpaceId: reference.memorySpaceId }); return { definitionDocId: row._id, definitionHash: row.definitionHash, definitionCanonical: row.definitionCanonical, definition, tenantId: row.tenantId, agent: selector, policies: definition.policies }; } export const publishVersion = mutation({ args: { memorySpaceId: v.string(), clientMutationId: v.string(), agentId: v.optional(v.string()), expectedPreviousVersion: v.number(), definitionInput: v.any() }, handler: async (ctx, args) => { const actor = await requireDataAuthority(ctx, { memorySpaceId: args.memorySpaceId }, "admin"); const reference = dataAuthorityReference(actor); clientRequestKey(args.clientMutationId); if (!Number.isSafeInteger(args.expectedPreviousVersion) || args.expectedPreviousVersion < 0) denied("INVALID_INPUT"); if (args.agentId !== undefined) boundedIdentifier(args.agentId); const input = decodeInput(args.definitionInput); await registry(input); const identity = canonicalModelJson(["agent.publish.v1", actor.tenantId, actor.memorySpaceId, actor.principalId, args.agentId === undefined ? ["create"] : ["existing", args.agentId], args.clientMutationId]); const hash = await semanticHash(identity); const request = canonicalModelJson({ identity, expectedPreviousVersion: args.expectedPreviousVersion, definitionInput: input }); if (utf8Bytes(request) > 180_000) denied("INVALID_INPUT"); const replay = one(await ctx.db.query("runtimeAgentDefinitions").withIndex("by_scoped_publish", q => q.eq("tenantId", actor.tenantId) .eq("memorySpaceId", actor.memorySpaceId).eq("publishPrincipalId", actor.principalId).eq("publishIdentityHash", hash)).take(2)); if (replay) { if (replay.publishIdentityCanonical !== identity || replay.publishRequestCanonical !== request) denied("IDEMPOTENCY_CONFLICT"); await checkRow(ctx, reference, replay, "admin"); return { agentId: replay.agentId, version: replay.version, definitionHash: replay.definitionHash }; } let owner = actor.principalId; if (args.agentId !== undefined) { const previous = one(await ctx.db.query("runtimeAgentDefinitions").withIndex("by_scoped_version", q => q.eq("tenantId", actor.tenantId) .eq("memorySpaceId", actor.memorySpaceId).eq("agentId", args.agentId!)).order("desc").take(1)); if (!previous || previous.version !== args.expectedPreviousVersion) denied("IDEMPOTENCY_CONFLICT"); await checkRow(ctx, reference, previous, "admin"); owner = previous.ownerPrincipalId; } else if (args.expectedPreviousVersion !== 0) denied("IDEMPOTENCY_CONFLICT"); const agentId = args.agentId ?? `agent:${hash}`; const version = args.expectedPreviousVersion + 1; if (!Number.isSafeInteger(version)) denied("INVALID_INPUT"); const definition: ExecutableDefinitionV1 = { version: 1, agentId, agentVersion: version, tenantId: actor.tenantId, memorySpaceId: actor.memorySpaceId, ownerPrincipalId: owner, ...input, policies: input.policies.map(policy => ({ ...policy, scope: "agent", tenantId: actor.tenantId, agentId, agentVersion: version })) }; const encoded = canonicalModelJson(definition); if (utf8Bytes(encoded) > 180_000) denied("INVALID_INPUT"); await narrowPolicies(ctx, reference, definition); const definitionHash = await semanticHash(encoded); await recheckAuthority(ctx, reference, { capability: "admin", tenantId: actor.tenantId, memorySpaceId: actor.memorySpaceId }); await ctx.db.insert("runtimeAgentDefinitions", { tenantId: actor.tenantId, memorySpaceId: actor.memorySpaceId, ownerPrincipalId: owner, agentId, version, definitionCanonical: encoded, definitionHash, authorReference: reference, createdAt: Date.now(), publishPrincipalId: actor.principalId, publishIdentityCanonical: identity, publishIdentityHash: hash, publishRequestCanonical: request }); return { agentId, version, definitionHash }; } }); export const revokeVersion = mutation({ args: { memorySpaceId: v.string(), agentId: v.string(), version: v.number(), expectedDefinitionHash: v.string(), reason: v.string() }, handler: async (ctx, args) => { const actor = await requireDataAuthority(ctx, { memorySpaceId: args.memorySpaceId }, "admin"); const reference = dataAuthorityReference(actor); boundedIdentifier(args.reason, 1024); const row = await rowFor(ctx, reference, { agentId: args.agentId, version: args.version }); if (!row) denied(); await checkRow(ctx, reference, row, "admin", true); if (row.definitionHash !== args.expectedDefinitionHash) denied("IDEMPOTENCY_CONFLICT"); if (row.revokedAt === undefined) await ctx.db.patch("runtimeAgentDefinitions", row._id, { revokedAt: Date.now(), revocationActorReference: reference, revocationReason: args.reason }); return { revoked: true as const }; } }); export const get = mutation({ args: { memorySpaceId: v.string(), tenantId: v.optional(v.string()), agentId: v.string(), version: v.number() }, handler: async (ctx, args) => { const actor = await requireDataAuthority(ctx, { tenantId: args.tenantId, memorySpaceId: args.memorySpaceId }, "run"); const current = await readCurrentExecutableDefinition(ctx, dataAuthorityReference(actor), { agentId: args.agentId, version: args.version }, { capability: "run" }); return { agentId: args.agentId, version: args.version, definitionHash: current.definitionHash, tools: current.definition.tools.map(tool => ({ toolId: tool.toolId, toolVersion: tool.toolVersion })), memoryMode: current.definition.context.memoryMode, ...(current.definition.context.handlerVersion===2 ? {inputSelectionVersion:2 as const,memoryEffortPolicyVersion:1 as const}: {}) }; } });