File size: 2,870 Bytes
e249c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  OpenClawStateLeaseError,
  withOpenClawStateLease,
} from "../../state/openclaw-state-lease.js";
import type { WorktreeFilesystemOptions } from "./filesystem-backend.types.js";

const WORKTREE_CREATE_LEASE_SCOPE = "core:managed-worktrees:create";
const WORKTREE_CREATE_LEASE_MS = 60_000;
const WORKTREE_CREATE_LEASE_WAIT_MS = 10 * 60_000;

export type WorktreeAllocationGuard = WorktreeFilesystemOptions & {
  rollbackGuard: () => void;
};

/** Serialize managed worktree allocations across repositories and processes. */
export async function withWorktreeAllocationLease<T>(
  params: {
    env: NodeJS.ProcessEnv;
    signal?: AbortSignal;
    commitGuard?: () => void;
  },
  run: (guard: WorktreeAllocationGuard) => Promise<T>,
): Promise<T> {
  // Disk headroom is shared across repositories. Hold one renewable lease
  // through checkout, setup, snapshots, and publication, including CLI processes.
  const acquisition = new AbortController();
  const abortAcquisition = () => acquisition.abort(params.signal?.reason);
  params.signal?.addEventListener("abort", abortAcquisition, { once: true });
  if (params.signal?.aborted) {
    abortAcquisition();
  }
  try {
    return await withOpenClawStateLease(
      {
        scope: WORKTREE_CREATE_LEASE_SCOPE,
        key: "capacity",
        database: { scope: "shared", options: { env: params.env } },
        leaseMs: WORKTREE_CREATE_LEASE_MS,
        waitMs: WORKTREE_CREATE_LEASE_WAIT_MS,
        leaseLabel: "managed worktree allocation lease",
        operationLabel: "agents.worktrees.allocation",
        signal: acquisition.signal,
      },
      async (lease) => {
        // Caller cancellation stops new work; allocation ownership survives until
        // its subprocesses and rollback settle. Lease loss still fences both.
        params.signal?.removeEventListener("abort", abortAcquisition);
        const signal = params.signal
          ? AbortSignal.any([params.signal, lease.signal])
          : lease.signal;
        try {
          const result = await run({
            signal,
            commitGuard: () => {
              lease.assertOwned();
              signal.throwIfAborted();
              params.commitGuard?.();
            },
            rollbackGuard: () => lease.assertOwned(),
          });
          signal.throwIfAborted();
          return result;
        } catch (error) {
          if (params.signal?.aborted) {
            lease.assertOwned();
            throw new OpenClawStateLeaseError(
              "managed worktree allocation lease operation was aborted",
              {
                code: "OPENCLAW_STATE_LEASE_ABORTED",
                cause: params.signal.reason,
              },
            );
          }
          throw error;
        }
      },
    );
  } finally {
    params.signal?.removeEventListener("abort", abortAcquisition);
  }
}