File size: 7,662 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { randomUUID } from "node:crypto";
import type { ExecutionIdentityAdmissionToken } from "../audit/execution-identity-admission.js";
import {
  validateAgentRunDelegatedAuthority,
  type AgentRunDelegatedAuthority,
} from "../infra/agent-run-registry.js";
import { resolveGlobalMap } from "../shared/global-singleton.js";
import type { AgentRuntimeSessionSpawnContext } from "./agent-runtime-session-spawn-context.js";

type AgentRuntimeExecutionLineage = {
  relation: "sessions_spawn";
  requesterRef: string;
  controllerRef: string;
  depth: number;
  applicableGrantRefs: string[];
  localPolicyRefs: string[];
  runtimeAssuranceRefs: string[];
  targetPolicyRefs: string[];
  externalNativeActions: "observable" | "unsupported";
};

const AGENT_RUNTIME_EXECUTION_LINEAGE = Symbol("agentRuntimeExecutionLineage");
const AGENT_RUNTIME_EXECUTION_LINEAGE_REDEMPTION = Symbol("agentRuntimeExecutionLineageRedemption");

const EXECUTION_LINEAGE_HANDOFF_TTL_MS = 60_000;
const MAX_EXECUTION_LINEAGE_HANDOFFS = 256;

type AgentRuntimeExecutionLineageCarrier = {
  [AGENT_RUNTIME_EXECUTION_LINEAGE]?: AgentRuntimeExecutionLineage;
};

type AgentRuntimeExecutionLineageRedemption = Readonly<{ consume: () => boolean }>;

type AgentRuntimeExecutionLineageRedemptionCarrier = {
  [AGENT_RUNTIME_EXECUTION_LINEAGE_REDEMPTION]: AgentRuntimeExecutionLineageRedemption;
};

function hasAgentRuntimeExecutionLineageRedemption(
  identity: object,
): identity is object & AgentRuntimeExecutionLineageRedemptionCarrier {
  return AGENT_RUNTIME_EXECUTION_LINEAGE_REDEMPTION in identity;
}

type ExecutionLineageHandoff = Readonly<{
  agentId: string;
  sessionKey: string;
  operationalRunInstance: Readonly<{ instanceId: string; runId: string }>;
  delegatedAuthority: AgentRunDelegatedAuthority;
  executionIdentity?: ExecutionIdentityAdmissionToken;
  sessionSpawnContext: AgentRuntimeSessionSpawnContext & AgentRuntimeExecutionLineageCarrier;
  expiresAtMs: number;
}>;

const executionLineageHandoffs = resolveGlobalMap<string, ExecutionLineageHandoff>(
  Symbol.for("openclaw.agentRuntimeExecutionLineageHandoffs"),
  (handoffs) => handoffs.clear(),
);

function sameOperationalRunInstance(
  left: Readonly<{ instanceId: string; runId: string }>,
  right: Readonly<{ instanceId: string; runId: string }>,
): boolean {
  return left.instanceId === right.instanceId && left.runId === right.runId;
}

function pruneExecutionLineageHandoffs(nowMs: number): void {
  for (const [id, handoff] of executionLineageHandoffs) {
    if (
      handoff.expiresAtMs <= nowMs ||
      !validateAgentRunDelegatedAuthority(handoff.delegatedAuthority)
    ) {
      executionLineageHandoffs.delete(id);
    }
  }
  // A lost local connection must not leave an unbounded process-lifetime registry.
  // Oldest insertion wins because Map preserves insertion order.
  while (executionLineageHandoffs.size >= MAX_EXECUTION_LINEAGE_HANDOFFS) {
    const oldest = executionLineageHandoffs.keys().next().value;
    if (typeof oldest !== "string") {
      break;
    }
    executionLineageHandoffs.delete(oldest);
  }
}

/** Add process-local lineage without expanding or serializing the spawn context. */
export function withAgentRuntimeExecutionLineage<T extends AgentRuntimeSessionSpawnContext>(
  context: T,
  lineage: AgentRuntimeExecutionLineage,
): T & AgentRuntimeExecutionLineageCarrier {
  return { ...context, [AGENT_RUNTIME_EXECUTION_LINEAGE]: lineage };
}

export function readAgentRuntimeExecutionLineage(
  context: (AgentRuntimeSessionSpawnContext & AgentRuntimeExecutionLineageCarrier) | undefined,
): AgentRuntimeExecutionLineage | undefined {
  return context?.[AGENT_RUNTIME_EXECUTION_LINEAGE];
}

/** Register a local one-shot handoff; its opaque id is correlation, never authority. */
export function createAgentRuntimeExecutionLineageHandoff(params: {
  agentId: string;
  sessionKey: string;
  operationalRunInstance: Readonly<{ instanceId: string; runId: string }>;
  delegatedAuthority: AgentRunDelegatedAuthority;
  executionIdentity?: ExecutionIdentityAdmissionToken;
  sessionSpawnContext: AgentRuntimeSessionSpawnContext;
}): Readonly<{ id: string; revoke: () => void }> | undefined {
  const lineage = readAgentRuntimeExecutionLineage(params.sessionSpawnContext);
  if (!lineage || !validateAgentRunDelegatedAuthority(params.delegatedAuthority)) {
    return undefined;
  }
  if (
    !sameOperationalRunInstance(
      params.operationalRunInstance,
      params.delegatedAuthority.operationalRunInstance,
    ) ||
    (params.executionIdentity !== undefined &&
      params.executionIdentity.runId !== params.operationalRunInstance.runId)
  ) {
    throw new Error("execution lineage handoff disagrees with its parent admission");
  }
  const nowMs = Date.now();
  pruneExecutionLineageHandoffs(nowMs);
  const id = randomUUID();
  executionLineageHandoffs.set(
    id,
    Object.freeze({
      agentId: params.agentId,
      sessionKey: params.sessionKey,
      operationalRunInstance: params.operationalRunInstance,
      delegatedAuthority: params.delegatedAuthority,
      ...(params.executionIdentity ? { executionIdentity: params.executionIdentity } : {}),
      sessionSpawnContext: params.sessionSpawnContext,
      expiresAtMs: nowMs + EXECUTION_LINEAGE_HANDOFF_TTL_MS,
    }),
  );
  return Object.freeze({
    id,
    revoke: () => {
      executionLineageHandoffs.delete(id);
    },
  });
}

/** Redeem the host-owned handoff while binding it to the exact signed parent owner. */
export function redeemAgentRuntimeExecutionLineageHandoff(params: {
  id: string;
  agentId: string;
  sessionKey: string;
  operationalRunInstance: Readonly<{ instanceId: string; runId: string }>;
  delegatedAuthority: AgentRunDelegatedAuthority;
}):
  | Readonly<{
      executionIdentity?: ExecutionIdentityAdmissionToken;
      sessionSpawnContext: AgentRuntimeSessionSpawnContext;
      redemption: AgentRuntimeExecutionLineageRedemption;
    }>
  | undefined {
  const handoff = executionLineageHandoffs.get(params.id);
  executionLineageHandoffs.delete(params.id);
  if (
    !handoff ||
    handoff.expiresAtMs <= Date.now() ||
    handoff.agentId !== params.agentId ||
    handoff.sessionKey !== params.sessionKey ||
    !sameOperationalRunInstance(handoff.operationalRunInstance, params.operationalRunInstance) ||
    handoff.delegatedAuthority.claimId !== params.delegatedAuthority.claimId ||
    handoff.delegatedAuthority.lifecycleGeneration !==
      params.delegatedAuthority.lifecycleGeneration ||
    !validateAgentRunDelegatedAuthority(handoff.delegatedAuthority)
  ) {
    return undefined;
  }
  let consumed = false;
  return Object.freeze({
    ...(handoff.executionIdentity ? { executionIdentity: handoff.executionIdentity } : {}),
    sessionSpawnContext: handoff.sessionSpawnContext,
    redemption: Object.freeze({
      consume: () => {
        if (consumed || !validateAgentRunDelegatedAuthority(handoff.delegatedAuthority)) {
          return false;
        }
        consumed = true;
        return true;
      },
    }),
  });
}

export function withAgentRuntimeExecutionLineageRedemption<T extends object>(
  identity: T,
  redemption: AgentRuntimeExecutionLineageRedemption,
): T & AgentRuntimeExecutionLineageRedemptionCarrier {
  return { ...identity, [AGENT_RUNTIME_EXECUTION_LINEAGE_REDEMPTION]: redemption };
}

/** Direct in-process lineage needs no redemption; handed-off lineage is one-shot. */
export function consumeAgentRuntimeExecutionLineage(identity: object): boolean {
  return hasAgentRuntimeExecutionLineageRedemption(identity)
    ? identity[AGENT_RUNTIME_EXECUTION_LINEAGE_REDEMPTION].consume()
    : true;
}