UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

178 lines (177 loc) • 6.93 kB
import { t as hasErrnoCode } from "./errno-DFcoqIT9.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import { w as root } from "./fs-safe-BbWvmdVn.mjs"; import { l as resolveAgentIdFromSessionKey } from "./session-key-CqAyZeVk.mjs"; import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs"; import { c as sha256File } from "./directory-durability-DlnWwNmv.mjs"; import { t as shellEscape } from "./shell-quote-DeRN90_2.mjs"; import { u as parseWorkspaceManifestPair } from "./workspace-manifest-worker-CfX0osDw.mjs"; import { _ as isDerivedWorkspacePath, n as MAX_RECONCILIATION_FILE_BYTES, o as manifestPath } from "./workspace-manifest-C118sSRH.mjs"; import { l as workspaceSizeRecoveryPaths, t as OVERSIZED_WORKSPACE_TOTAL_ERROR_PREFIX } from "./workspace-manifest-comparison-BpzYeKoQ.mjs"; import { a as hasWorkerWorkspaceResultRef } from "./workspace-result-staging-C8wToOO8.mjs"; import { i as withLocalWorkspaceProjection, r as resolveLocalWorkspaceOwner } from "./local-workspace-projection-d69eRBoO.mjs"; import path from "node:path"; //#region src/gateway/worker-environments/local-workspace-recovery.ts function recoveryPath(e) { const t = manifestPath(e); if (t.split("/").some((e) => e.toLowerCase() === ".git") || isDerivedWorkspacePath(t, false, true)) throw new Error("Select a retained workspace file and a UUID recovery ID"); return t; } async function hasAggregateOverflow(e) { const t = await e.baselineDocument(); const n = await e.buildCacheInputs(); const r = await e.capture("canonical"); try { await parseWorkspaceManifestPair({ baseRaw: t.rawManifest, baseRef: t.manifestRef, currentRaw: r.rawManifest, currentRef: r.manifestRef, buildCacheInputs: n }); } catch (e) { if (e instanceof Error && e.message.startsWith("Cloud workspace staged result exceeds its byte limit")) return true; throw e; } return false; } async function openRecoverableFile(e, t, n, r, i) { const a = t.current(); if (a.journal_json || !a.pending_ref || a.pending_target !== "canonical" || await hasWorkerWorkspaceResultRef({ root: e.worktree.repoRoot, stagedResultRef: a.pending_ref })) throw new Error("Recovery requires an unstaged guest checkpoint; existing receipts were preserved"); if ((await t.canonicalPaths()).has(r)) throw new Error("Cannot relocate a source file already admitted to the canonical workspace"); const o = path.relative(n.rootReal, a.projection_path) + "/" + r; const s = await n.open(o); try { if (s.stat.size <= 67108864 && (s.stat.size === 0 || !(i ?? await hasAggregateOverflow(t)))) throw new Error("Selected file does not exceed the 64 MiB limit and no aggregate overflow requires recovery"); } catch (e) { await s[Symbol.asyncDispose](); throw e; } return s; } /** Advice uses the same eligibility checks as relocation, under the existing workspace lease. */ async function describeLocalWorkspaceRecovery(e, t, n) { const r = workspaceSizeRecoveryPaths(e); for (const i of r) try { const r = recoveryPath(i); await (await openRecoverableFile(t, n, await root(resolveStateDir(t.env), { symlinks: "reject", hardlinks: "reject" }), r, e.message.startsWith(OVERSIZED_WORKSPACE_TOTAL_ERROR_PREFIX)))[Symbol.asyncDispose](); n.current(); return new Error(e.message + "\nPending workspace changes were preserved. Ask a Gateway administrator to run:\nopenclaw sandbox recover --session " + shellEscape(t.sessionKey) + " --path " + shellEscape(r) + "\nRun this command in Bash or zsh on the Gateway host, then retry the session. The selected file is preserved outside workspace synchronization. Repeat for another selected file if the remaining payload is still too large.", { cause: e }); } catch {} return e; } /** Explicit operator recovery, before settlement: never discard or ignore a guest file. */ async function recoverLocalWorkspaceFile(e) { try { var t = _usingCtx(); const n = recoveryPath(e.path); if (!/^[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/u.test(e.recoveryId)) throw new Error("Select a retained workspace file and a UUID recovery ID"); const r = t.u(await resolveLocalWorkspaceOwner({ cfg: e.cfg, agentId: resolveAgentIdFromSessionKey(e.sessionKey), sessionKey: e.sessionKey, env: e.env, assertCurrent: e.assertCurrent })); if (!r) throw new Error("Session has no local managed workspace to recover"); return await withLocalWorkspaceProjection(r, async (t) => { try { var i = _usingCtx(); const current = () => { e.signal?.throwIfAborted(); return t.current(); }; const o = current(); const s = await root(resolveStateDir(r.env), { symlinks: "reject", mutationSymlinks: "reject", hardlinks: "reject", assertBeforeMutation: current }); const c = path.relative(s.rootReal, o.projection_path) + "/" + n; const l = "workspace-recovery/" + r.worktree.id + "/" + e.recoveryId + "/" + n; const u = path.join(s.rootReal, l); const result = async () => { try { var e = _usingCtx(); const t = e.a(await s.open(l)); current(); return { path: u, bytes: t.stat.size }; } catch (t) { e.e = t; } finally { await e.d(); } }; if (await s.exists(l)) { if (await s.exists(c)) throw new Error("Both file names exist; inspect retained artifact at " + u); return await result(); } const d = i.a(await openRecoverableFile(r, t, s, n)); await s.mkdir(path.dirname(l), { private: true }); current(); try { try { await s.move(c, l); } catch (t) { try { var a = _usingCtx(); if (!hasErrnoCode(t, "EXDEV")) throw t; await s.copyIn(l, { root: s, relativePath: c }, { overwrite: false, maxBytes: d.stat.size, preserveSourceMode: true, signal: e.signal }); const n = a.a(await s.open(l)); await n.handle.sync(); const r = await sha256File(d.handle, { maxBytes: d.stat.size, signal: e.signal }); const i = await sha256File(n.handle, { maxBytes: d.stat.size, signal: e.signal }); if (r.bytes !== d.stat.size || i.bytes !== r.bytes || i.digest !== r.digest) throw new Error("Recovery copy does not match the retained file", { cause: t }); await s.remove(c); } catch (e) { a.e = e; } finally { await a.d(); } } } catch (e) { throw new Error("Recovery move did not finish; inspect " + u + " before retrying", { cause: e }); } current(); return { path: u, bytes: d.stat.size }; } catch (e) { i.e = e; } finally { await i.d(); } }, { signal: e.signal }); } catch (e) { t.e = e; } finally { t.d(); } } //#endregion export { recoverLocalWorkspaceFile as n, describeLocalWorkspaceRecovery as t };