import { h as workerWorkspaceCommandSucceeded, p as waitForQuiescenceRenewal, y as workspaceSyncError } from "./workspace-sync-helpers-Do847gm2.js";
import path from "node:path";
//#region src/gateway/worker-environments/workspace-quiescence-scripts.ts
const REMOTE_QUIESCENCE_PS_JS = String.raw`function processes() {
  const output = childProcess.execFileSync("ps", ["-axo", "pid=,ppid=,uid=,stat=,lstart="], {
    encoding: "utf8",
    maxBuffer: 4 * 1024 * 1024,
    timeout: 2000,
    killSignal: "SIGKILL",
  });
  const rows = new Map();
  for (const line of output.split("\n")) {
    const match = line.trim().match(/^(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.+)$/);
    if (!match) continue;
    rows.set(Number(match[1]), {
      ppid: Number(match[2]),
      uid: Number(match[3]),
      state: match[4],
      start: match[5],
    });
  }
  return rows;
}
function ancestors(rows) {
  const result = new Set();
  let pid = process.pid;
  while (pid > 0 && !result.has(pid)) {
    result.add(pid);
    pid = rows.get(pid)?.ppid || 0;
  }
  return result;
}
function processIdentity(pid) {
  try {
    // Identity gates every thaw, and execFileSync's timeout only signals before waiting for
    // the child: under the default SIGTERM a ps that ignores it still blocks forever, so every
    // probe here must be killable to stay bounded.
    const start = require("node:child_process").execFileSync("ps", ["-o", "lstart=", "-p", String(pid)], {
      encoding: "utf8",
      maxBuffer: 4096,
      timeout: 2000,
      killSignal: "SIGKILL",
    }).trim();
    return start || null;
  } catch (error) {
    if (error && error.status === 1) return null;
    throw error;
  }
}
function processStatus(pid) {
  try {
    const output = childProcess.execFileSync("ps", ["-o", "stat=,lstart=", "-p", String(pid)], { encoding: "utf8", maxBuffer: 4096, timeout: 2000, killSignal: "SIGKILL" }).trim();
    const match = /^(\S+)\s+(.+)$/u.exec(output);
    return match ? { state: match[1], start: match[2] } : null;
  } catch (error) {
    if (error && error.status === 1) return null;
    throw error;
  }
}
function quiescenceCandidates(rows, expectedUid, excludedPids, frozen) {
  const preserved = ancestors(rows);
  return [...rows.entries()].filter(
    ([pid, row]) =>
      row.uid === expectedUid &&
      !preserved.has(pid) &&
      row.ppid !== process.pid &&
      !excludedPids.has(pid) &&
      (!frozen || !frozen.has(pid)) &&
      !row.state.startsWith("T") &&
      !row.state.startsWith("Z") &&
      !row.state.startsWith("X"),
  );
}`;
const REMOTE_QUIESCENCE_LEASE_JS = String.raw`function validProcessReference(value) {
  return value && Number.isSafeInteger(value.pid) && value.pid > 0 && typeof value.start === "string" && value.start.length > 0 && value.start.length <= 128;
}
function parseLease(raw, expectedNonce, options = {}) {
  const lease = JSON.parse(raw);
  if (
    !lease ||
    lease.version !== 1 ||
    lease.nonce !== expectedNonce ||
    (lease.sharedHost !== undefined && typeof lease.sharedHost !== "boolean") ||
    !Array.isArray(lease.processes) ||
    lease.processes.length > 4096 ||
    lease.processes.some((entry) => !validProcessReference(entry)) ||
    (lease.watchdog !== null && !validProcessReference(lease.watchdog)) ||
    (options.requireWatchdog && lease.watchdog === null) ||
    !Number.isSafeInteger(lease.expiresAtMs) ||
    lease.expiresAtMs < 1 ||
    (options.minimumRemainingMs && lease.expiresAtMs - Date.now() < options.minimumRemainingMs)
  ) {
    throw new Error(options.errorMessage || "invalid workspace quiescence lease");
  }
  return lease;
}
function persistLease(targetPath, lease, verifyCurrent) {
  if (verifyCurrent) verifyCurrent(JSON.parse(fs.readFileSync(targetPath, "utf8")));
  const temporary = targetPath + "." + process.pid + "." + crypto.randomBytes(8).toString("hex");
  fs.writeFileSync(temporary, JSON.stringify(lease), { mode: 0o600, flag: "wx" });
  fs.renameSync(temporary, targetPath);
}
function withWindowsWorkspaceLease(databasePath, workspaceKey, run) {
  const { DatabaseSync } = require("node:sqlite");
  const database = new DatabaseSync(databasePath);
  database.exec("PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL; CREATE TABLE IF NOT EXISTS workspace_leases (workspace_key TEXT PRIMARY KEY, lease_json TEXT NOT NULL); BEGIN IMMEDIATE");
  try {
    database
      .prepare("DELETE FROM workspace_leases WHERE json_extract(lease_json, '$.expiresAtMs') <= ?")
      .run(Date.now());
    const row = database
      .prepare("SELECT lease_json FROM workspace_leases WHERE workspace_key = ?")
      .get(workspaceKey);
    const next = run(row ? row.lease_json : null);
    if (next === null) {
      database.prepare("DELETE FROM workspace_leases WHERE workspace_key = ?").run(workspaceKey);
    } else if (next !== undefined) {
      database
        .prepare("INSERT INTO workspace_leases (workspace_key, lease_json) VALUES (?, ?) ON CONFLICT(workspace_key) DO UPDATE SET lease_json = excluded.lease_json")
        .run(workspaceKey, next);
    }
    database.exec("COMMIT");
    return next;
  } catch (error) {
    database.exec("ROLLBACK");
    throw error;
  } finally {
    database.close();
  }
}`;
const REMOTE_WORKSPACE_QUIESCE_JS = String.raw`const childProcess = require("node:child_process");
const crypto = require("node:crypto");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const root = fs.realpathSync(process.argv[1]);
const sleeper = new Int32Array(new SharedArrayBuffer(4));
const leaseDirectory = path.join(os.homedir(), ".openclaw-worker", "quiescence");
fs.mkdirSync(leaseDirectory, { recursive: true, mode: 0o700 });
fs.chmodSync(leaseDirectory, 0o700);
const workspaceKey = crypto.createHash("sha256").update(root).digest("hex");
const nonce = crypto.randomBytes(16).toString("hex");
const watchdogTimeoutMs = Number(process.argv[2] || 12 * 60 * 1000);
if (!Number.isSafeInteger(watchdogTimeoutMs) || watchdogTimeoutMs < 1) throw new Error("invalid watchdog timeout");
const isolationMode = process.argv[3] || "dedicated";
if (isolationMode !== "dedicated" && isolationMode !== "shared-host") throw new Error("invalid workspace quiescence isolation mode");
const sharedHost = isolationMode === "shared-host";
const windowsLeaseDatabasePath = path.join(leaseDirectory, "windows-shared-host.sqlite");
const leasePath = path.join(leaseDirectory, workspaceKey + "." + nonce + ".json");
${REMOTE_QUIESCENCE_LEASE_JS}
if (process.platform === "win32" && sharedHost) {
  withWindowsWorkspaceLease(windowsLeaseDatabasePath, workspaceKey, (raw) => {
    if (raw !== null) {
      const parsed = JSON.parse(raw);
      if (!/^[a-f0-9]{32}$/.test(parsed?.nonce || "")) {
        throw new Error("invalid Windows shared-host workspace quiescence lease");
      }
      const candidate = parseLease(raw, parsed.nonce);
      if (
        candidate.sharedHost !== true ||
        candidate.processes.length !== 0 ||
        candidate.watchdog !== null
      ) {
        throw new Error("invalid Windows shared-host workspace quiescence lease");
      }
      if (candidate.expiresAtMs > Date.now()) {
        throw new Error("workspace quiescence lease is already active");
      }
    }
    const lease = {
      version: 1,
      nonce,
      sharedHost: true,
      processes: [],
      watchdog: null,
      expiresAtMs: Date.now() + watchdogTimeoutMs,
    };
    return JSON.stringify(lease);
  });
  process.stderr.write("workspace quiescence: Windows shared host declared; using manifest fences without process freezing\n");
  process.stdout.write("quiesced " + nonce + "\n");
  process.exit(0);
}
if (typeof process.getuid !== "function") throw new Error("workspace quiescence requires POSIX");
const uid = process.getuid();
if (uid === 0) throw new Error("workspace quiescence refuses root-owned worker sessions");
${REMOTE_QUIESCENCE_PS_JS}
const frozen = new Map();
let watchdogReference = null;
function writeLease(expiresAtMs = Date.now() + watchdogTimeoutMs) {
  persistLease(leasePath, {
    version: 1,
    nonce,
    sharedHost,
    processes: [...frozen].map(([pid, start]) => ({ pid, start })),
    watchdog: watchdogReference,
    expiresAtMs,
  });
}
// EPERM on SIGCONT implies the target was never ours to freeze: kill permission checks are
// identical for SIGSTOP and SIGCONT, so any process this uid successfully stopped can be resumed.
function resumeProcesses(entries) {
  for (const entry of entries) {
    if (processIdentity(entry.pid) !== entry.start) continue;
    try {
      process.kill(entry.pid, "SIGCONT");
    } catch (error) {
      if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error;
    }
  }
}
const orphanNames = fs.readdirSync(leaseDirectory).filter((name) =>
  name.startsWith(workspaceKey + ".") && name.endsWith(".json"),
);
if (orphanNames.length > 16) throw new Error("too many workspace quiescence leases");
let sawUnverifiedEmptyLeaseWatchdog = false;
for (const name of orphanNames) {
  const match = name.match(/^[a-f0-9]{64}\.([a-f0-9]{32})\.json$/);
  if (!match) continue;
  const orphanPath = path.join(leaseDirectory, name);
  const lease = parseLease(fs.readFileSync(orphanPath, "utf8"), match[1]);
  resumeProcesses(lease.processes);
  let retainLeaseForRetry = false;
  if (lease.watchdog !== null) {
    try {
      let watchdogMatches = processIdentity(lease.watchdog.pid) === lease.watchdog.start;
      if (watchdogMatches) {
        try { process.kill(lease.watchdog.pid, "SIGTERM"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
        for (let attempt = 0; attempt < 100 && watchdogMatches; attempt += 1) {
          Atomics.wait(sleeper, 0, 0, 10);
          watchdogMatches = processIdentity(lease.watchdog.pid) === lease.watchdog.start;
        }
        if (watchdogMatches) throw new Error("prior workspace quiescence watchdog did not retire");
      }
    } catch (error) {
      if (lease.processes.length > 0) throw error;
      sawUnverifiedEmptyLeaseWatchdog = true;
      retainLeaseForRetry = !sharedHost;
    }
  }
  if (retainLeaseForRetry) continue;
  // The orphan's own watchdog can resume and unlink first; a lease that is already gone
  // is the outcome we wanted, so it must not fail this sweep.
  try { fs.unlinkSync(orphanPath); } catch (error) { if (!error || error.code !== "ENOENT") throw error; }
}
// Shared-host replacements can remove empty leases during a ps outage because they never sweep
// processes. Dedicated replacements retain the watchdog identity so a retry can exclude it.
if (!sharedHost && sawUnverifiedEmptyLeaseWatchdog) {
  throw new Error("could not verify prior workspace quiescence watchdog retirement; retry when ps is available");
}
writeLease();
const watchdog = childProcess.spawn(
  process.execPath,
  ["-e", processIdentity.toString() + "\n(" + watchdogMain.toString() + ")(process.argv[1], process.argv[2])", leasePath, nonce],
  { detached: true, stdio: "ignore" },
);
watchdog.unref();
if (!Number.isSafeInteger(watchdog.pid) || watchdog.pid < 1) {
  fs.unlinkSync(leasePath);
  throw new Error("workspace quiescence watchdog did not start");
}
let watchdogStart = null;
try {
  for (let attempt = 0; attempt < 100 && !watchdogStart; attempt += 1) {
    watchdogStart = processIdentity(watchdog.pid);
    if (!watchdogStart) Atomics.wait(sleeper, 0, 0, 10);
  }
  if (!watchdogStart) {
    throw new Error("workspace quiescence watchdog identity was not observable");
  }
  watchdogReference = { pid: watchdog.pid, start: watchdogStart };
  writeLease();
} catch (error) {
  try { process.kill(watchdog.pid, "SIGTERM"); } catch (killError) { if (!killError || (killError.code !== "ESRCH" && killError.code !== "EPERM")) throw killError; }
  try { fs.unlinkSync(leasePath); } catch (unlinkError) { if (!unlinkError || unlinkError.code !== "ENOENT") throw unlinkError; }
  throw error;
}
let quietScans = 0;
try {
  if (sharedHost) {
    // The worker has already published its terminal result. Manifest stability fences around
    // transfer, apply, renewal, and publication reject later writes; only the uid-wide SIGSTOP
    // sweep is skipped because this provider explicitly declared processes the lease does not own.
    process.stderr.write("workspace quiescence: shared host declared; skipping process freeze sweep\n");
    quietScans = 3;
  }
  for (let attempt = 0; !sharedHost && attempt < 250 && quietScans < 3; attempt += 1) {
    const candidates = quiescenceCandidates(
      processes(),
      uid,
      new Set([watchdog.pid]),
      frozen,
    );
    if (candidates.length + frozen.size > 4096) {
      throw new Error("too many worker processes to quiesce safely");
    }
    for (const [pid, row] of candidates) {
      try {
        frozen.set(pid, row.start);
        writeLease();
        if (processIdentity(pid) !== row.start) {
          frozen.delete(pid);
          writeLease();
          continue;
        }
        process.kill(pid, "SIGSTOP");
      } catch (error) {
        if (error && error.code === "EPERM") {
          frozen.delete(pid);
          writeLease();
          continue;
        }
        if (!error || error.code !== "ESRCH") throw error;
      }
    }
    Atomics.wait(sleeper, 0, 0, 20);
    const writable = quiescenceCandidates(
      processes(),
      uid,
      new Set([watchdog.pid]),
    ).length > 0;
    quietScans = writable ? 0 : quietScans + 1;
  }
  if (quietScans < 3) {
    throw new Error("worker processes did not reach a quiescent state");
  }
} catch (error) {
  // Thaw before retiring the watchdog: a bounded identity probe can throw here, and
  // retiring first would leave a stopped worker with no remaining resumer.
  resumeProcesses([...frozen].map(([pid, start]) => ({ pid, start })));
  if (processIdentity(watchdog.pid) === watchdogStart) {
    try { process.kill(watchdog.pid, "SIGTERM"); } catch (killError) { if (!killError || (killError.code !== "ESRCH" && killError.code !== "EPERM")) throw killError; }
  }
  try { fs.unlinkSync(leasePath); } catch (unlinkError) { if (!unlinkError || unlinkError.code !== "ENOENT") throw unlinkError; }
  throw error;
}
function watchdogMain(watchedLeasePath, watchedNonce) {
  let retryDelayMs = 1000;
  const check = () => {
    try {
      const watchdogFs = require("node:fs");
      const lease = JSON.parse(watchdogFs.readFileSync(watchedLeasePath, "utf8"));
      if (
        !lease ||
        lease.version !== 1 ||
        lease.nonce !== watchedNonce ||
        !Array.isArray(lease.processes) ||
        !Number.isSafeInteger(lease.expiresAtMs)
      ) return;
      const remainingMs = lease.expiresAtMs - Date.now();
      if (remainingMs > 0) {
        setTimeout(check, Math.min(remainingMs, 60 * 1000));
        return;
      }
      // Re-read at expiry so a renewal that raced this wake-up wins before SIGCONT.
      const latest = JSON.parse(watchdogFs.readFileSync(watchedLeasePath, "utf8"));
      if (
        latest &&
        latest.version === 1 &&
        latest.nonce === watchedNonce &&
        Array.isArray(latest.processes) &&
        Number.isSafeInteger(latest.expiresAtMs) &&
        latest.expiresAtMs > Date.now()
      ) {
        setTimeout(check, Math.min(latest.expiresAtMs - Date.now(), 60 * 1000));
        return;
      }
      for (const entry of lease.processes) {
        if (
          !entry ||
          !Number.isSafeInteger(entry.pid) ||
          entry.pid < 1 ||
          typeof entry.start !== "string" ||
          processIdentity(entry.pid) !== entry.start
        ) continue;
        try { process.kill(entry.pid, "SIGCONT"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
      }
      watchdogFs.unlinkSync(watchedLeasePath);
    } catch (error) {
      // Only the lease disappearing or being unusable retires this watchdog. A missing ps also throws
      // ENOENT, and treating that as "someone else finished" would exit with the workers
      // still stopped, which is the freeze this loop exists to prevent.
      if (error && error.code === "ENOENT" && error.path === watchedLeasePath) return;
      // An unreadable lease is terminal: the pids to resume live in that file, so retrying
      // cannot recover them and would leave this detached process alive forever.
      if (error instanceof SyntaxError) return;
      // Otherwise this is the lease's last resumer, so it retries with backoff until the sweep
      // completes or the lease file is gone. Any attempt cap would just re-create the permanent
      // freeze for a longer stall; whoever removes the lease retires this watchdog next tick.
      setTimeout(check, retryDelayMs);
      retryDelayMs = Math.min(retryDelayMs * 2, 60000);
    }
  };
  check();
}
process.stdout.write("quiesced " + nonce + "\n");
`;
const REMOTE_WORKSPACE_RENEW_QUIESCENCE_JS = String.raw`const childProcess = require("node:child_process");
const crypto = require("node:crypto");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const root = fs.realpathSync(process.argv[1]);
const nonce = process.argv[2];
const timeoutMs = Number(process.argv[3] || 12 * 60 * 1000);
const validationMode = process.argv[4] || "final";
const isolationMode = process.argv[5] || "dedicated";
if (!/^[a-f0-9]{32}$/.test(nonce || "")) throw new Error("invalid workspace quiescence nonce");
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 10 * 1000) throw new Error("invalid watchdog timeout");
if (validationMode !== "heartbeat" && validationMode !== "final") throw new Error("invalid workspace quiescence validation mode");
if (isolationMode !== "dedicated" && isolationMode !== "shared-host") throw new Error("invalid workspace quiescence isolation mode");
const sharedHost = isolationMode === "shared-host";
const workspaceKey = crypto.createHash("sha256").update(root).digest("hex");
const leaseDirectory = path.join(os.homedir(), ".openclaw-worker", "quiescence");
const windowsLeaseDatabasePath = path.join(leaseDirectory, "windows-shared-host.sqlite");
const leasePath = path.join(leaseDirectory, workspaceKey + "." + nonce + ".json");
${REMOTE_QUIESCENCE_LEASE_JS}
if (process.platform === "win32" && sharedHost) {
  withWindowsWorkspaceLease(windowsLeaseDatabasePath, workspaceKey, (raw) => {
    if (raw === null) throw new Error("workspace quiescence lease is no longer active");
    const input = parseLease(raw, nonce, {
      minimumRemainingMs: 5000,
      errorMessage: "workspace quiescence lease is no longer active",
    });
    if (input.sharedHost !== true || input.processes.length !== 0 || input.watchdog !== null) {
      throw new Error("invalid Windows shared-host workspace quiescence lease");
    }
    const renewed = { ...input, expiresAtMs: Date.now() + timeoutMs };
    return JSON.stringify(renewed);
  });
  process.stdout.write("renewed " + nonce + "\n");
  process.exit(0);
}
if (typeof process.getuid !== "function") throw new Error("workspace quiescence requires POSIX");
const uid = process.getuid();
${REMOTE_QUIESCENCE_PS_JS}
const input = parseLease(fs.readFileSync(leasePath, "utf8"), nonce, {
  requireWatchdog: true,
  minimumRemainingMs: 5000,
  errorMessage: "workspace quiescence lease is no longer active",
});
if ((input.sharedHost === true) !== sharedHost) throw new Error("workspace quiescence isolation mode changed");
function writeLease(processes, expiresAtMs) {
  // renewalQueue is the nonce's only writer; the watchdog only reads this lease.
  persistLease(leasePath, { ...input, processes, expiresAtMs }, (current) => {
    if (current.nonce !== nonce || current.watchdog?.pid !== input.watchdog.pid || current.watchdog?.start !== input.watchdog.start) {
      throw new Error("workspace quiescence lease changed during renewal");
    }
  });
}
function assertWatchdogActive() {
  const status = processStatus(input.watchdog.pid);
  if (!status || status.start !== input.watchdog.start) {
    throw new Error("workspace quiescence watchdog identity changed unexpectedly");
  }
  try { process.kill(input.watchdog.pid, 0); } catch (error) {
    if (error && error.code === "ESRCH") throw new Error("workspace quiescence watchdog exited unexpectedly");
    throw error;
  }
}
function refreshLease(processes) {
  assertWatchdogActive();
  input.expiresAtMs = Date.now() + timeoutMs;
  writeLease(processes, input.expiresAtMs);
}
for (const entry of input.processes) {
  const status = processStatus(entry.pid);
  if (!status || status.start !== entry.start) continue;
  if (status.state && !status.state.startsWith("T")) throw new Error("workspace quiescence process resumed unexpectedly");
}
refreshLease(input.processes);
if (validationMode === "final" && !sharedHost) {
  const frozen = new Map(input.processes.map((entry) => [entry.pid, entry.start]));
  let quietScans = 0;
  const sleeper = new Int32Array(new SharedArrayBuffer(4));
  // A control tunnel can reconnect after the initial freeze; enroll every late process.
  for (let attempt = 0; attempt < 250 && quietScans < 3; attempt += 1) {
    const candidates = quiescenceCandidates(
      processes(),
      uid,
      new Set([input.watchdog.pid]),
    );
    if (candidates.length + frozen.size > 4096) {
      throw new Error("too many worker processes to quiesce safely");
    }
    for (const [pid, row] of candidates) frozen.set(pid, row.start);
    let frozenEntries = [...frozen].map(([pid, start]) => ({ pid, start }));
    refreshLease(frozenEntries);
    for (const [pid, row] of candidates) {
      try {
        if (input.expiresAtMs - Date.now() < 5000) refreshLease(frozenEntries);
        const current = processStatus(pid);
        if (!current || current.start !== row.start) {
          frozen.delete(pid);
          continue;
        }
        if (input.expiresAtMs - Date.now() < 2500) refreshLease(frozenEntries);
        process.kill(pid, "SIGSTOP");
      } catch (error) {
        if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error;
        // Fail-closed either way: the candidate scan below runs without the frozen filter,
        // so an EPERM-live process re-registers as a candidate and blocks quiescence.
        frozen.delete(pid);
      }
    }
    frozenEntries = [...frozen].map(([pid, start]) => ({ pid, start }));
    refreshLease(frozenEntries);
    Atomics.wait(sleeper, 0, 0, 20);
    const unknownProcess = quiescenceCandidates(
      processes(),
      uid,
      new Set([input.watchdog.pid]),
    ).length > 0;
    quietScans = candidates.length > 0 || unknownProcess ? 0 : quietScans + 1;
  }
  if (quietScans < 3) {
    throw new Error("worker processes did not return to a quiescent state");
  }
  input.processes = [...frozen].map(([pid, start]) => ({ pid, start }));
}
const renewed = { ...input, expiresAtMs: Date.now() + timeoutMs };
refreshLease(renewed.processes);
renewed.expiresAtMs = input.expiresAtMs;
const confirmed = JSON.parse(fs.readFileSync(leasePath, "utf8"));
if (confirmed.nonce !== nonce || confirmed.expiresAtMs !== renewed.expiresAtMs) {
  throw new Error("workspace quiescence renewal was not durable");
}
process.stdout.write("renewed " + nonce + "\n");
`;
const REMOTE_WORKSPACE_RESUME_JS = String.raw`const childProcess = require("node:child_process");
const crypto = require("node:crypto");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const root = fs.realpathSync(process.argv[1]);
const nonce = process.argv[2];
if (!/^[a-f0-9]{32}$/.test(nonce || "")) throw new Error("invalid workspace quiescence nonce");
const workspaceKey = crypto.createHash("sha256").update(root).digest("hex");
const leaseDirectory = path.join(os.homedir(), ".openclaw-worker", "quiescence");
const windowsLeaseDatabasePath = path.join(leaseDirectory, "windows-shared-host.sqlite");
const leasePath = path.join(leaseDirectory, workspaceKey + "." + nonce + ".json");
${REMOTE_QUIESCENCE_LEASE_JS}
if (process.platform === "win32") {
  withWindowsWorkspaceLease(windowsLeaseDatabasePath, workspaceKey, (raw) => {
    if (raw === null) return;
    const input = parseLease(raw, nonce);
    if (input.sharedHost !== true || input.processes.length !== 0 || input.watchdog !== null) {
      throw new Error("invalid Windows shared-host workspace quiescence lease");
    }
    return null;
  });
  process.exit(0);
}
if (typeof process.getuid !== "function") throw new Error("workspace quiescence requires POSIX");
${REMOTE_QUIESCENCE_PS_JS}
let raw;
try { raw = fs.readFileSync(leasePath, "utf8"); } catch (error) {
  if (error && error.code === "ENOENT") process.exit(0);
  throw error;
}
const input = parseLease(raw, nonce);
// Thaw before retiring the watchdog: a bounded identity lookup can still fail, and
// retiring the last resumer first would strand whatever the aborted sweep never reached.
for (const entry of input.processes) {
  if (processIdentity(entry.pid) !== entry.start) continue;
  try { process.kill(entry.pid, "SIGCONT"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
}
let watchdogStart = null;
try { if (input.watchdog !== null) watchdogStart = processIdentity(input.watchdog.pid); } catch (error) {
  // An empty lease has nothing to strand, so ps cannot block its release.
  if (input.processes.length > 0) throw error;
}
if (input.watchdog !== null && watchdogStart === input.watchdog.start) {
  try { process.kill(input.watchdog.pid, "SIGTERM"); } catch (error) { if (!error || (error.code !== "ESRCH" && error.code !== "EPERM")) throw error; }
}
// The watchdog stays alive across the whole resume loop now, so it can win the unlink race.
// Everything is thawed either way; a missing lease must not fail the sync.
try { fs.unlinkSync(leasePath); } catch (error) { if (!error || error.code !== "ENOENT") throw error; }
`;
//#endregion
//#region src/gateway/worker-environments/workspace-quiescence.ts
const WORKSPACE_QUIESCENCE_TIMEOUT_MS = 12 * 6e4;
const WORKSPACE_QUIESCENCE_RENEW_INTERVAL_MS = 4 * 6e4;
function createWorkerWorkspaceQuiescence(params) {
	return async (remoteWorkspaceDir) => {
		const posixAbsolute = path.posix.isAbsolute(remoteWorkspaceDir);
		const windowsAbsolute = path.win32.isAbsolute(remoteWorkspaceDir);
		if (!posixAbsolute && !windowsAbsolute) throw new Error("Worker workspace quiescence path must be absolute");
		if (!posixAbsolute && windowsAbsolute && !params.sharedHost) throw new Error("Windows worker workspace quiescence requires a shared host");
		const hostMode = params.sharedHost ? "shared-host" : "dedicated";
		const run = async (argv) => {
			const result = await params.runWorkspaceCommand({
				transportRetry: "never",
				argv
			});
			if (!workerWorkspaceCommandSucceeded(result)) throw workspaceSyncError(result);
			return result;
		};
		const result = await run([
			"node",
			"-e",
			REMOTE_WORKSPACE_QUIESCE_JS,
			remoteWorkspaceDir,
			String(WORKSPACE_QUIESCENCE_TIMEOUT_MS),
			hostMode
		]);
		const acknowledgement = /^quiesced ([a-f0-9]{32})$/u.exec(result.stdout.trim());
		if (!acknowledgement) throw new Error("Worker workspace quiescence returned an invalid acknowledgement");
		const nonce = acknowledgement[1];
		let releasePromise;
		let renewalFailure;
		const renewalAbort = new AbortController();
		const renewalSignal = AbortSignal.any([params.ownerSignal, renewalAbort.signal]);
		let renewalQueue = Promise.resolve();
		const renew = (validationMode) => {
			const operation = renewalQueue.then(async () => {
				if ((await run([
					"node",
					"-e",
					REMOTE_WORKSPACE_RENEW_QUIESCENCE_JS,
					remoteWorkspaceDir,
					nonce,
					String(WORKSPACE_QUIESCENCE_TIMEOUT_MS),
					validationMode,
					hostMode
				])).stdout.trim() !== `renewed ${nonce}`) throw new Error("Worker workspace quiescence renewal returned an invalid acknowledgement");
			});
			renewalQueue = operation.catch(() => void 0);
			return operation;
		};
		const renewalLoop = (async () => {
			while (!renewalSignal.aborted) {
				if (!await waitForQuiescenceRenewal(renewalSignal, WORKSPACE_QUIESCENCE_RENEW_INTERVAL_MS)) return;
				try {
					await renew("heartbeat");
				} catch (error) {
					renewalFailure = error;
					return;
				}
			}
		})();
		return {
			assertActive: async () => {
				if (renewalSignal.aborted) throw new Error("Worker workspace quiescence was already released");
				if (renewalFailure) throw new Error("Worker workspace quiescence renewal failed", { cause: renewalFailure });
				await renew("final");
			},
			resume: async () => {
				releasePromise ??= (async () => {
					renewalAbort.abort();
					await renewalLoop;
					await renewalQueue;
					if (!params.ownerSignal.aborted) await run([
						"node",
						"-e",
						REMOTE_WORKSPACE_RESUME_JS,
						remoteWorkspaceDir,
						nonce
					]);
				})().catch((error) => {
					if (params.ownerSignal.aborted) return;
					releasePromise = void 0;
					throw error;
				});
				await releasePromise;
			}
		};
	};
}
//#endregion
export { createWorkerWorkspaceQuiescence as t };
