eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 8.04 kB
JavaScript
import{createLogger}from"#internal/logging.js";import{contextStorage,loadContext}from"#context/container.js";import{SessionTraceSeedKey}from"#context/keys.js";import{ContextKey}from"#context/key.js";import{decisionToTraceContentCeiling,resolveForwardedTraceSeed}from"#shared/forwarded-trace-policy.js";import{getWorkflowToolRuns}from"#harness/workflow-tool-runs.js";import{getSessionTaskIndex}from"#tasks/session-index.js";import{deriveTaskId}from"#tasks/task-id.js";import{actionIdempotencyKey}from"#instrumentation/lifecycle.js";import{AGENT_TRACE_CONTEXT_KEY,deserializeAgentTraceContextState,emptyAgentTraceContextState,serializeAgentTraceContextState}from"#tracing/agent-trace-context-codec.js";const AgentTraceContextKey=new ContextKey(AGENT_TRACE_CONTEXT_KEY,{codec:{deserialize:deserializeAgentTraceContextState,serialize:serializeAgentTraceContextState}});function pruneAgentTraceState(e,t,n){try{pruneTraceOwnership(e,t,n)}catch(e){createLogger(`tracing.retention`).warn(`could not reconcile trace ownership; preserving trace state`,{error:e})}}function pruneTraceOwnership(e,t,n){let r=e.get(AgentTraceContextKey);if(r===void 0)return;let i=new Set(getWorkflowToolRuns(n).map(e=>e.callId)),a=new Set(getSessionTaskIndex(n).filter(e=>e.terminalView===void 0).map(e=>e.taskId)),o=Object.fromEntries(Object.entries(r.actionAnchors).filter(([e,n])=>n.sessionId!==t||r.actions[e]!==void 0||i.has(n.callId)||a.has(deriveTaskId({callId:n.callId,parentSessionId:t,parentTurnId:n.turnId})))),s=new Set(Object.values(o).filter(e=>e.sessionId===t).map(e=>e.callId)),c=Object.fromEntries(Object.entries(r.invocations).filter(([,e])=>e.sessionId!==t||e.terminal!==void 0||s.has(e.parentActionCallId)));e.set(AgentTraceContextKey,{...r,actionAnchors:o,invocations:c})}function readCurrentSessionTraceDecision(e){let t=contextStorage.getStore();return t===void 0?void 0:readSessionTraceDecision(t,e)}function readSessionTraceDecision(e,t){return e.get(AgentTraceContextKey)?.sessions[t]?.decision}function preserveSerializedAgentTraceState(e,t){let n=t[AgentTraceContextKey.name];return n===void 0?e:{...e,[AgentTraceContextKey.name]:n}}function readTurnTraceContext(e,t,n){let r=e[AgentTraceContextKey.name];if(r===void 0)return;let i=deserializeAgentTraceContextState(r),a=i.turns[turnKey(t,n)];return a===void 0?void 0:withTraceDecision(e,a.context,i.sessions[t]?.decision)}function readActionTraceContext(e,t,n,r){let i=e[AgentTraceContextKey.name];if(i===void 0)return;let a=deserializeAgentTraceContextState(i),o=actionIdempotencyKey(t,n,r),s=a.invocations[o]??a.actions[o]??a.actionAnchors[o];if(s!==void 0)return withTraceDecision(e,{isRemote:!1,spanId:s.spanId,traceFlags:s.parent.traceFlags,traceId:s.parent.traceId},a.sessions[s.sessionId]?.decision)}function readTaskActionTrace(e,t,n){let r=deserializeAgentTraceContextState(e[AgentTraceContextKey.name]);return Object.values(r.actionAnchors).find(e=>e.sessionId===t&&deriveTaskId({callId:e.callId,parentSessionId:t,parentTurnId:e.turnId})===n)}function recordNestedAgentInvocation(e){let t=e.serializedContext[AgentTraceContextKey.name];if(t===void 0)return e.serializedContext;let n=deserializeAgentTraceContextState(t),r=actionIdempotencyKey(e.sessionId,e.turnId,e.outerCallId),i=n.actions[r]??n.actionAnchors[r];if(i===void 0)return e.serializedContext;let a=actionIdempotencyKey(e.sessionId,e.turnId,e.callId),promote=e=>e.isWorkflowTool===!0&&e.kind===`tool-call`?{...e,workflowName:e.name}:e,o=n.actions[r]===void 0?n.actions:{...n.actions,[r]:promote(n.actions[r])},s=n.actionAnchors[r]===void 0?n.actionAnchors:{...n.actionAnchors,[r]:promote(n.actionAnchors[r])};if(n.invocations[a]!==void 0)return{...e.serializedContext,[AgentTraceContextKey.name]:serializeAgentTraceContextState({...n,actionAnchors:s,actions:o})};let c={attemptIndex:i.attemptIndex,callId:e.callId,channelAudience:i.channelAudience,kind:e.kind,name:e.name,parent:{spanId:i.spanId,traceFlags:i.parent.traceFlags,traceId:i.parent.traceId},parentActionCallId:e.outerCallId,recordOutputs:e.recordOutputs===!0,rootSessionId:i.rootSessionId,sessionId:i.sessionId,spanId:e.spanId,startTimeMs:e.startTimeMs,stepIndex:i.stepIndex,turnId:i.turnId};return{...e.serializedContext,[AgentTraceContextKey.name]:serializeAgentTraceContextState({...n,actionAnchors:s,actions:o,invocations:{...n.invocations,[a]:c}})}}function recordActionInvocationKind(e){let t=e.serializedContext[AgentTraceContextKey.name];if(t===void 0)return e.serializedContext;let n=deserializeAgentTraceContextState(t),update=t=>t.sessionId===e.sessionId&&t.turnId===e.turnId&&t.callId===e.callId?{...t,isWorkflowTool:void 0,kind:e.kind,workflowName:void 0}:t;return{...e.serializedContext,[AgentTraceContextKey.name]:serializeAgentTraceContextState({...n,actionAnchors:Object.fromEntries(Object.entries(n.actionAnchors).map(([e,t])=>[e,update(t)])),actions:Object.fromEntries(Object.entries(n.actions).map(([e,t])=>[e,update(t)]))})}}function withTraceDecision(e,t,n){let r=e[SessionTraceSeedKey.name],i=resolveForwardedTraceSeed({decision:n??r?.decision,forwardedTracePolicy:r?.forwardedTracePolicy,traceFlags:t.traceFlags}),a=i.decision,o=i.forwardedTracePolicy,s=decisionToTraceContentCeiling(a),c={...t,traceFlags:i.traceFlags};if(o===void 0)return a===void 0?c:{...c,decision:a};let l=s===void 0?o:{...o,ceiling:s};return a===void 0?{...c,forwardedTracePolicy:l}:{...c,decision:a,forwardedTracePolicy:l}}var ContextAgentTraceStateStore=class{deleteAction(e){updateState(t=>{let n={...t.actions};return delete n[e],{...t,actions:n}})}deleteActionAnchors(e){updateState(t=>({...t,actionAnchors:Object.fromEntries(Object.entries(t.actionAnchors).filter(([,t])=>t.sessionId!==e))}))}deleteActions(e,t){updateState(n=>{let r={...n.actions};for(let[n,i]of Object.entries(r))i.sessionId===e&&(t===void 0||i.turnId===t)&&delete r[n];return{...n,actions:r}})}deleteInvocation(e){updateState(t=>{let n={...t.invocations};return delete n[e],{...t,invocations:n}})}deleteInvocations(e,t){updateState(n=>({...n,invocations:Object.fromEntries(Object.entries(n.invocations).filter(([,n])=>n.sessionId!==e||t!==void 0&&n.turnId!==t))}))}deleteSession(e){updateState(t=>{let n={...t.sessions};return delete n[e],{...t,sessions:n}})}deleteTurn(e,t){updateState(n=>{let r={...n.turns};return delete r[turnKey(e,t)],{...n,turns:r}})}findAction(e,t){return Object.values(contextStorage.getStore()?.get(AgentTraceContextKey)?.actions??{}).find(n=>n.sessionId===e&&n.callId===t)}findActionAnchor(e,t,n){return Object.values(contextStorage.getStore()?.get(AgentTraceContextKey)?.actionAnchors??{}).find(r=>r.sessionId===e&&r.turnId===t&&r.callId===n)}findInvocations(e,t,n){return Object.values(contextStorage.getStore()?.get(AgentTraceContextKey)?.invocations??{}).filter(r=>(e===void 0||r.sessionId===e)&&(t===void 0||r.turnId===t)&&(n===void 0||r.parentActionCallId===n))}getAction(e){return contextStorage.getStore()?.get(AgentTraceContextKey)?.actions[e]}getSession(e){return contextStorage.getStore()?.get(AgentTraceContextKey)?.sessions[e]}getTurn(e,t){return contextStorage.getStore()?.get(AgentTraceContextKey)?.turns[turnKey(e,t)]}setAction(e,t){updateState(n=>({...n,actions:{...n.actions,[e]:t}}))}setActionAnchor(e,t){updateState(n=>({...n,actionAnchors:{...n.actionAnchors,[e]:t}}))}setInvocation(e,t){updateState(n=>({...n,invocations:{...n.invocations,[e]:t}}))}setSession(e,t){updateState(n=>({...n,sessions:{...n.sessions,[e]:t}}))}setTurn(e,t,n){updateState(r=>({...r,turns:{...r.turns,[turnKey(e,t)]:n}}))}updateTurn(e,t,n){updateState(r=>{let i=turnKey(e,t),a=r.turns[i];return a===void 0?r:{...r,turns:{...r.turns,[i]:n(a)}}})}};function updateState(e){loadContext().set(AgentTraceContextKey,t=>e(t??emptyAgentTraceContextState()))}function turnKey(e,t){return`${e}\0${t}`}export{ContextAgentTraceStateStore,preserveSerializedAgentTraceState,pruneAgentTraceState,readActionTraceContext,readCurrentSessionTraceDecision,readSessionTraceDecision,readTaskActionTrace,readTurnTraceContext,recordActionInvocationKind,recordNestedAgentInvocation};