File size: 9,683 Bytes
4440aec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { randomUUID } from "node:crypto";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import { ErrorCodes, errorShape } from "../../../packages/gateway-protocol/src/index.js";
import { hasGeneratedMediaCompletionEvent } from "../../agents/internal-event-contract.js";
import {
  resolveAgentMainSessionKey,
  resolveChannelResetConfig,
  resolveSessionResetPolicy,
  resolveSessionResetType,
  resolveSessionWorkStartError,
  type SessionEntry,
  type SessionFreshness,
} from "../../config/sessions.js";
import { hasSessionTranscriptEventsSync } from "../../config/sessions/session-accessor.js";
import { resolveMaintenanceConfigFromInput } from "../../config/sessions/store-maintenance.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { parseAgentSessionKey } from "../../routing/session-key.js";
import { parseCronRunScopeSuffix } from "../../sessions/session-key-utils.js";
import { sessionDeliveryChannel } from "../../utils/delivery-context.shared.js";
import {
  respondDeletedAgentSession,
  type RestoredCronContinuation,
} from "../agent-turn/agent-handler-helpers.js";
import { resolveRequestedSessionAgentId } from "../session-request-agent.js";
import { loadSessionEntry } from "../session-utils.js";
import type { AgentRunRequest } from "./agent-request-types.js";
import { evaluateAgentSessionReuse } from "./agent-session-patch.js";
import type { GatewayRequestHandlerOptions } from "./types.js";

type PreparedAgentSession = {
  cfg: OpenClawConfig;
  storePath: string;
  entry?: SessionEntry;
  canonicalKey: string;
  storeKeys?: string[];
  maintenanceConfig: ReturnType<typeof resolveMaintenanceConfigFromInput>;
  canonicalSessionAgentId: string;
  resetPolicy: ReturnType<typeof resolveSessionResetPolicy>;
  now: number;
  freshness: SessionFreshness | undefined;
  visibleRequest: boolean;
  mainSessionKey: string;
  isSystemGatewayRun: boolean;
  usableRequestedSessionId?: string;
  sessionId: string;
  isNewSession: boolean;
  rotatedSessionId: boolean;
  touchInteraction: boolean;
  sessionPersistedBeforeGatewayAdmission: boolean;
  effectiveBootstrapContextRunKind?: "default" | "heartbeat" | "cron";
  restoredCronContinuationIdentity?: Pick<
    RestoredCronContinuation,
    "lifecycleRevision" | "sessionId"
  >;
  failedSessionTranscriptMissing: (entry: SessionEntry | undefined) => boolean;
};

export function prepareAgentSession(params: {
  cfg: OpenClawConfig;
  requestedSessionKey: string;
  requestedSessionId?: string;
  expectedExistingSessionId?: string;
  agentId?: string;
  recipientChannel?: string;
  request: AgentRunRequest;
  canUseCronRunContinuation: boolean;
  lifecycleGeneration: string;
  effectiveBootstrapContextRunKind?: "default" | "heartbeat" | "cron";
  preAttachmentSession?: { canonicalKey: string; sessionId?: string };
  respond: GatewayRequestHandlerOptions["respond"];
}): PreparedAgentSession | undefined {
  const requestedSessionAgent = resolveRequestedSessionAgentId(
    params.cfg,
    params.requestedSessionKey,
    params.agentId,
  );
  if (!requestedSessionAgent.ok) {
    params.respond(false, undefined, requestedSessionAgent.error);
    return undefined;
  }
  const requestedAgentId = requestedSessionAgent.agentId;
  const { cfg, storePath, entry, canonicalKey, legacyKey, storeKeys } = loadSessionEntry(
    params.requestedSessionKey,
    { agentId: requestedAgentId, clone: false },
  );
  if (params.expectedExistingSessionId && entry?.sessionId !== params.expectedExistingSessionId) {
    params.respond(
      false,
      undefined,
      errorShape(
        ErrorCodes.UNAVAILABLE,
        `Session "${canonicalKey}" changed before expected work could start.`,
      ),
    );
    return undefined;
  }

  let effectiveBootstrapContextRunKind = params.effectiveBootstrapContextRunKind;
  let restoredCronContinuationIdentity: PreparedAgentSession["restoredCronContinuationIdentity"];
  const isGeneratedMediaCronContinuation =
    hasGeneratedMediaCompletionEvent(params.request.internalEvents) &&
    parseCronRunScopeSuffix(canonicalKey).runId !== undefined;
  if (isGeneratedMediaCronContinuation) {
    if (!params.canUseCronRunContinuation) {
      params.respond(
        false,
        undefined,
        errorShape(
          ErrorCodes.INVALID_REQUEST,
          "cron run completion handoffs are reserved for server-owned callers",
        ),
      );
      return undefined;
    }
    const marker = entry?.cronRunContinuation;
    const continuationSessionId = normalizeOptionalString(entry?.sessionId);
    const staleClaim =
      marker?.phase === "continuing" &&
      marker.ownerLifecycleGeneration !== params.lifecycleGeneration;
    if (staleClaim || (marker?.phase === "ready" && marker.basePersisted !== true)) {
      params.respond(
        false,
        undefined,
        errorShape(
          ErrorCodes.INVALID_REQUEST,
          staleClaim
            ? "cron run continuation owner was lost during gateway restart"
            : "cron run continuation base session was not persisted",
        ),
      );
      return undefined;
    }
    if (!marker || marker.phase !== "ready" || !continuationSessionId) {
      params.respond(
        false,
        undefined,
        errorShape(ErrorCodes.UNAVAILABLE, "cron run continuation is not ready"),
      );
      return undefined;
    }
    if (params.requestedSessionId && params.requestedSessionId !== continuationSessionId) {
      params.respond(
        false,
        undefined,
        errorShape(ErrorCodes.UNAVAILABLE, "cron run continuation session changed"),
      );
      return undefined;
    }
    restoredCronContinuationIdentity = {
      lifecycleRevision: marker.lifecycleRevision,
      sessionId: continuationSessionId,
    };
    effectiveBootstrapContextRunKind = "cron";
  }

  const sessionExistedBeforeAttachmentSetup =
    params.preAttachmentSession?.canonicalKey === canonicalKey
      ? params.preAttachmentSession
      : undefined;
  if (sessionExistedBeforeAttachmentSetup && !entry) {
    params.respond(
      false,
      undefined,
      errorShape(
        ErrorCodes.INVALID_REQUEST,
        `Session "${canonicalKey}" was deleted while starting work. Retry.`,
      ),
    );
    return undefined;
  }
  if (
    sessionExistedBeforeAttachmentSetup &&
    entry?.sessionId !== sessionExistedBeforeAttachmentSetup.sessionId
  ) {
    params.respond(
      false,
      undefined,
      errorShape(
        ErrorCodes.INVALID_REQUEST,
        `Session "${canonicalKey}" changed while starting work. Retry.`,
      ),
    );
    return undefined;
  }
  if (
    respondDeletedAgentSession({
      cfg,
      canonicalKey,
      entry,
      acpMetadataSessionKey: legacyKey,
      respond: params.respond,
    })
  ) {
    return undefined;
  }
  const archivedSessionError = resolveSessionWorkStartError(canonicalKey, entry);
  if (archivedSessionError) {
    params.respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, archivedSessionError));
    return undefined;
  }

  const canonicalSessionAgentId = parseAgentSessionKey(canonicalKey)?.agentId ?? requestedAgentId;
  const now = Date.now();
  const resetPolicy = resolveSessionResetPolicy({
    sessionCfg: cfg.session,
    resetType: resolveSessionResetType({ sessionKey: canonicalKey }),
    resetOverride: resolveChannelResetConfig({
      sessionCfg: cfg.session,
      channel: sessionDeliveryChannel(entry) ?? params.recipientChannel,
    }),
  });
  const visibleRequest =
    effectiveBootstrapContextRunKind !== "cron" &&
    effectiveBootstrapContextRunKind !== "heartbeat" &&
    !params.request.internalEvents?.length;
  const failedSessionTranscriptMissing = (candidateEntry: SessionEntry | undefined): boolean => {
    if (candidateEntry?.status !== "failed" || !candidateEntry.sessionId?.trim()) {
      return false;
    }
    try {
      return !hasSessionTranscriptEventsSync({
        agentId: canonicalSessionAgentId,
        sessionId: candidateEntry.sessionId,
        sessionKey: canonicalKey,
        storePath,
        sessionEntry: candidateEntry,
      });
    } catch {
      return true;
    }
  };
  const mainSessionKey = resolveAgentMainSessionKey({ cfg, agentId: canonicalSessionAgentId });
  const isSystemGatewayRun =
    effectiveBootstrapContextRunKind === "cron" || effectiveBootstrapContextRunKind === "heartbeat";
  const reuse = evaluateAgentSessionReuse({
    freshEntry: entry,
    cfg,
    sessionAgentId: canonicalSessionAgentId,
    canonicalSessionKey: canonicalKey,
    storePath,
    expectedExistingSessionId: params.expectedExistingSessionId,
    hasRestoredCronContinuation: restoredCronContinuationIdentity !== undefined,
    resetPolicy,
    now,
    requestedSessionId: params.requestedSessionId,
    isSystemGatewayRun,
    visibleRequest,
    failedSessionTranscriptMissing,
  });
  const sessionId = reuse.sessionId ?? randomUUID();
  return {
    cfg,
    storePath,
    entry,
    canonicalKey,
    storeKeys,
    maintenanceConfig: resolveMaintenanceConfigFromInput(cfg.session?.maintenance),
    canonicalSessionAgentId,
    resetPolicy,
    now,
    freshness: reuse.freshness,
    visibleRequest,
    mainSessionKey,
    isSystemGatewayRun,
    usableRequestedSessionId: reuse.usableRequestedSessionId,
    sessionId,
    isNewSession: reuse.isNewSession,
    rotatedSessionId: Boolean(entry?.sessionId && entry.sessionId !== sessionId),
    touchInteraction: visibleRequest,
    sessionPersistedBeforeGatewayAdmission: entry !== undefined,
    effectiveBootstrapContextRunKind,
    restoredCronContinuationIdentity,
    failedSessionTranscriptMissing,
  };
}