openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
103 lines (102 loc) • 3.91 kB
JavaScript
import { a as resolveGlobalSingleton, r as resolveGlobalMap } from "./global-singleton-D7yf05nR.mjs";
import { n as resolveIdentityPathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs";
import { o as racePromiseWithAbortSignal } from "./src-BPmQBLJp.mjs";
import "./abort-signal-D4Q6SEw7.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { n as enqueueKeyedTask } from "./keyed-async-queue-DewLnYvW.mjs";
import { n as ToolInputError } from "./tool-input-error-B1hgavSw.mjs";
import { r as getAgentToolExecutionContext } from "./tool-execution-context-D0eUSg2-.mjs";
import { createHash } from "node:crypto";
//#region src/agents/sessions/tools/file-mutation-queue.ts
/**
* Per-file mutation queue.
*
* Serializes reads and mutations targeting the same real file while allowing independent files to run in parallel.
*/
const log = createSubsystemLogger("agents/file-queue");
const FILE_QUEUE_WAIT_MS = 18e4;
const fileMutationTails = resolveGlobalMap(Symbol.for("openclaw.fileMutationTails"), "close-only");
const keyAdmissions = resolveGlobalSingleton(Symbol.for("openclaw.fileMutationKeyAdmissions"), () => ({
fallbackScope: {},
tails: /* @__PURE__ */ new WeakMap()
}));
async function resolveFileMutationQueueKey(e, t, n) {
return await (t?.(e, n) ?? resolveIdentityPathViaExistingAncestorSync(e));
}
/**
* Preserve source-call admission while backend-owned physical identities resolve concurrently.
* Registration is ordered per assistant message; file operations still use only fileMutationTails.
*/
async function withFileMutationQueueKeyResolution(e, t, n) {
return await withFileMutationQueueKeysResolution(e.then((e) => [e]), t, n);
}
async function withFileMutationQueueKeysResolution(e, t, n) {
const r = getAgentToolExecutionContext()?.assistantMessage ?? keyAdmissions.fallbackScope;
e.catch(() => void 0);
let i;
await enqueueKeyedTask({
tails: keyAdmissions.tails,
key: r,
signal: n,
task: async () => {
const r = [...new Set(await racePromiseWithAbortSignal(e, n))].toSorted();
let a = r.flatMap((e) => {
const t = fileMutationTails.get(e);
return t ? [{
key: e,
tail: t
}] : [];
});
const o = Date.now();
let s;
const c = new AbortController();
const l = n ? AbortSignal.any([n, c.signal]) : c.signal;
const runningSince = () => {
const e = a.flatMap(({ tail: e }) => {
const t = e.runningSince();
return t === void 0 ? [] : [t];
});
return s ?? (e.length ? Math.min(...e) : void 0);
};
const u = a[0];
const d = u ? setTimeout(() => {
const e = Date.now();
const t = a.find(({ tail: e }) => e.runningSince() !== void 0) ?? u;
const n = e - (t.tail.runningSince() ?? o);
log.warn("File queue wait expired", {
keyHash: createHash("sha256").update(t.key).digest("hex").slice(0, 16),
holderAgeMs: n,
waitMs: e - o
});
c.abort(new ToolInputError(`The file is busy with an earlier operation that has not finished (running for ${Math.floor(n / 1e3)} seconds). Retry later, work on another file, or ask the operator to restart the Gateway if this persists.`));
}, FILE_QUEUE_WAIT_MS) : void 0;
i = enqueueKeyedTask({
tails: {
get: (e) => fileMutationTails.get(e),
set: (e, t) => {
t.then(() => {
a = [];
s = void 0;
});
fileMutationTails.set(e, Object.assign(t, { runningSince }));
},
delete: (e) => fileMutationTails.delete(e)
},
keys: r,
signal: l,
task: async () => {
clearTimeout(d);
a = [];
s = Date.now();
return t();
}
}).catch((e) => {
if (c.signal.aborted) throw c.signal.reason;
throw e;
}).finally(() => clearTimeout(d));
}
});
return await i;
}
//#endregion
export { withFileMutationQueueKeyResolution as n, withFileMutationQueueKeysResolution as r, resolveFileMutationQueueKey as t };