|
| 1 | +import type { managedWorktrees } from "../agents/worktrees/service.js"; |
1 | 2 | import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 3 | +import { runExclusiveSessionLifecycleMutation } from "../sessions/session-lifecycle-admission.js"; |
| 4 | +import type * as sessionUtils from "./session-utils.js"; |
| 5 | +import type { WorkerPlacementExecutionMode } from "./worker-environments/placement-record.js"; |
| 6 | +import type * as placementSessionRuntime from "./worker-environments/placement-session-runtime.js"; |
2 | 7 |
|
3 | 8 | export class WorkerDispatchTargetChangedError extends Error { |
4 | 9 | readonly code = "invalid_state"; |
5 | 10 | } |
6 | 11 |
|
| 12 | +type WorkerPlacementSessionRuntime = { |
| 13 | + resolveWorkerPlacementExecutionMode: typeof placementSessionRuntime.resolveWorkerPlacementExecutionMode; |
| 14 | + managedWorktrees: typeof managedWorktrees; |
| 15 | + resolveWorkerPlacementSessionRuntime: typeof placementSessionRuntime.resolveWorkerPlacementSessionRuntime; |
| 16 | + resolveCanonicalSessionEntryFromStoreKeys: typeof sessionUtils.resolveCanonicalSessionEntryFromStoreKeys; |
| 17 | + resolveGatewaySessionStoreTargetWithStore: typeof sessionUtils.resolveGatewaySessionStoreTargetWithStore; |
| 18 | +}; |
| 19 | +type WorkerPlacementWorktree = NonNullable<ReturnType<typeof managedWorktrees.findLiveByOwner>>; |
| 20 | + |
| 21 | +export async function runWorkerPlacementSessionBarrier<T>(params: { |
| 22 | + sessionRuntime: WorkerPlacementSessionRuntime; |
| 23 | + getConfig: () => OpenClawConfig; |
| 24 | + sessionId: string; |
| 25 | + sessionKey: string; |
| 26 | + agentId: string; |
| 27 | + executionMode: WorkerPlacementExecutionMode; |
| 28 | + action: "activation" | "recovery"; |
| 29 | + run: (worktree: WorkerPlacementWorktree) => T | Promise<T>; |
| 30 | +}): Promise<T> { |
| 31 | + const target = params.sessionRuntime.resolveGatewaySessionStoreTargetWithStore({ |
| 32 | + cfg: params.getConfig(), |
| 33 | + key: params.sessionKey, |
| 34 | + agentId: params.agentId, |
| 35 | + clone: false, |
| 36 | + }); |
| 37 | + return await runExclusiveSessionLifecycleMutation({ |
| 38 | + scope: target.storePath, |
| 39 | + identities: [params.sessionKey, target.canonicalKey, ...target.storeKeys, params.sessionId], |
| 40 | + run: async () => { |
| 41 | + const { |
| 42 | + config, |
| 43 | + target: currentTarget, |
| 44 | + entry, |
| 45 | + worktree, |
| 46 | + } = resolveWorkerPlacementSessionTarget({ |
| 47 | + sessionRuntime: params.sessionRuntime, |
| 48 | + config: params.getConfig(), |
| 49 | + sessionId: params.sessionId, |
| 50 | + sessionKey: params.sessionKey, |
| 51 | + agentId: params.agentId, |
| 52 | + expectedTarget: target, |
| 53 | + errorMessage: `Session ${params.sessionKey} changed before cloud worker ${params.action}. Retry.`, |
| 54 | + }); |
| 55 | + if (entry.archivedAt !== undefined) { |
| 56 | + throw new WorkerDispatchTargetChangedError( |
| 57 | + `Session ${params.sessionKey} was archived before cloud worker ${params.action}. Retry.`, |
| 58 | + ); |
| 59 | + } |
| 60 | + const currentRuntime = params.sessionRuntime.resolveWorkerPlacementSessionRuntime({ |
| 61 | + cfg: config, |
| 62 | + entry, |
| 63 | + agentId: currentTarget.agentId, |
| 64 | + sessionKey: currentTarget.canonicalKey, |
| 65 | + }); |
| 66 | + if ( |
| 67 | + params.sessionRuntime.resolveWorkerPlacementExecutionMode(currentRuntime) !== |
| 68 | + params.executionMode |
| 69 | + ) { |
| 70 | + throw new WorkerDispatchTargetChangedError( |
| 71 | + `Session ${params.sessionKey} runtime changed to ${currentRuntime} before cloud worker ${params.action}. Retry.`, |
| 72 | + ); |
| 73 | + } |
| 74 | + return await params.run(worktree); |
| 75 | + }, |
| 76 | + }); |
| 77 | +} |
| 78 | + |
7 | 79 | type SessionEntryShape = { |
8 | 80 | sessionId?: string; |
9 | 81 | archivedAt?: number; |
|
0 commit comments