mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 21:48:27 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -45,13 +45,21 @@ function persistSubagentRuns() {
|
||||
const resumedRuns = new Set<string>();
|
||||
|
||||
function resumeSubagentRun(runId: string) {
|
||||
if (!runId || resumedRuns.has(runId)) return;
|
||||
if (!runId || resumedRuns.has(runId)) {
|
||||
return;
|
||||
}
|
||||
const entry = subagentRuns.get(runId);
|
||||
if (!entry) return;
|
||||
if (entry.cleanupCompletedAt) return;
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
if (entry.cleanupCompletedAt) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof entry.endedAt === "number" && entry.endedAt > 0) {
|
||||
if (!beginSubagentCleanup(runId)) return;
|
||||
if (!beginSubagentCleanup(runId)) {
|
||||
return;
|
||||
}
|
||||
const requesterOrigin = normalizeDeliveryContext(entry.requesterOrigin);
|
||||
void runSubagentAnnounceFlow({
|
||||
childSessionKey: entry.childSessionKey,
|
||||
@@ -82,13 +90,19 @@ function resumeSubagentRun(runId: string) {
|
||||
}
|
||||
|
||||
function restoreSubagentRunsOnce() {
|
||||
if (restoreAttempted) return;
|
||||
if (restoreAttempted) {
|
||||
return;
|
||||
}
|
||||
restoreAttempted = true;
|
||||
try {
|
||||
const restored = loadSubagentRegistryFromDisk();
|
||||
if (restored.size === 0) return;
|
||||
if (restored.size === 0) {
|
||||
return;
|
||||
}
|
||||
for (const [runId, entry] of restored.entries()) {
|
||||
if (!runId || !entry) continue;
|
||||
if (!runId || !entry) {
|
||||
continue;
|
||||
}
|
||||
// Keep any newer in-memory entries.
|
||||
if (!subagentRuns.has(runId)) {
|
||||
subagentRuns.set(runId, entry);
|
||||
@@ -111,7 +125,9 @@ function restoreSubagentRunsOnce() {
|
||||
function resolveArchiveAfterMs(cfg?: ReturnType<typeof loadConfig>) {
|
||||
const config = cfg ?? loadConfig();
|
||||
const minutes = config.agents?.defaults?.subagents?.archiveAfterMinutes ?? 60;
|
||||
if (!Number.isFinite(minutes) || minutes <= 0) return undefined;
|
||||
if (!Number.isFinite(minutes) || minutes <= 0) {
|
||||
return undefined;
|
||||
}
|
||||
return Math.max(1, Math.floor(minutes)) * 60_000;
|
||||
}
|
||||
|
||||
@@ -123,7 +139,9 @@ function resolveSubagentWaitTimeoutMs(
|
||||
}
|
||||
|
||||
function startSweeper() {
|
||||
if (sweeper) return;
|
||||
if (sweeper) {
|
||||
return;
|
||||
}
|
||||
sweeper = setInterval(() => {
|
||||
void sweepSubagentRuns();
|
||||
}, 60_000);
|
||||
@@ -131,7 +149,9 @@ function startSweeper() {
|
||||
}
|
||||
|
||||
function stopSweeper() {
|
||||
if (!sweeper) return;
|
||||
if (!sweeper) {
|
||||
return;
|
||||
}
|
||||
clearInterval(sweeper);
|
||||
sweeper = null;
|
||||
}
|
||||
@@ -140,7 +160,9 @@ async function sweepSubagentRuns() {
|
||||
const now = Date.now();
|
||||
let mutated = false;
|
||||
for (const [runId, entry] of subagentRuns.entries()) {
|
||||
if (!entry.archiveAtMs || entry.archiveAtMs > now) continue;
|
||||
if (!entry.archiveAtMs || entry.archiveAtMs > now) {
|
||||
continue;
|
||||
}
|
||||
subagentRuns.delete(runId);
|
||||
mutated = true;
|
||||
try {
|
||||
@@ -153,8 +175,12 @@ async function sweepSubagentRuns() {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
if (mutated) persistSubagentRuns();
|
||||
if (subagentRuns.size === 0) stopSweeper();
|
||||
if (mutated) {
|
||||
persistSubagentRuns();
|
||||
}
|
||||
if (subagentRuns.size === 0) {
|
||||
stopSweeper();
|
||||
}
|
||||
}
|
||||
|
||||
function ensureListener() {
|
||||
@@ -163,7 +189,9 @@ function ensureListener() {
|
||||
}
|
||||
listenerStarted = true;
|
||||
listenerStop = onAgentEvent((evt) => {
|
||||
if (!evt || evt.stream !== "lifecycle") return;
|
||||
if (!evt || evt.stream !== "lifecycle") {
|
||||
return;
|
||||
}
|
||||
const entry = subagentRuns.get(evt.runId);
|
||||
if (!entry) {
|
||||
return;
|
||||
@@ -177,7 +205,9 @@ function ensureListener() {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (phase !== "end" && phase !== "error") return;
|
||||
if (phase !== "end" && phase !== "error") {
|
||||
return;
|
||||
}
|
||||
const endedAt = typeof evt.data?.endedAt === "number" ? evt.data.endedAt : Date.now();
|
||||
entry.endedAt = endedAt;
|
||||
if (phase === "error") {
|
||||
@@ -214,7 +244,9 @@ function ensureListener() {
|
||||
|
||||
function finalizeSubagentCleanup(runId: string, cleanup: "delete" | "keep", didAnnounce: boolean) {
|
||||
const entry = subagentRuns.get(runId);
|
||||
if (!entry) return;
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
if (cleanup === "delete") {
|
||||
subagentRuns.delete(runId);
|
||||
persistSubagentRuns();
|
||||
@@ -232,9 +264,15 @@ function finalizeSubagentCleanup(runId: string, cleanup: "delete" | "keep", didA
|
||||
|
||||
function beginSubagentCleanup(runId: string) {
|
||||
const entry = subagentRuns.get(runId);
|
||||
if (!entry) return false;
|
||||
if (entry.cleanupCompletedAt) return false;
|
||||
if (entry.cleanupHandled) return false;
|
||||
if (!entry) {
|
||||
return false;
|
||||
}
|
||||
if (entry.cleanupCompletedAt) {
|
||||
return false;
|
||||
}
|
||||
if (entry.cleanupHandled) {
|
||||
return false;
|
||||
}
|
||||
entry.cleanupHandled = true;
|
||||
persistSubagentRuns();
|
||||
return true;
|
||||
@@ -273,7 +311,9 @@ export function registerSubagentRun(params: {
|
||||
});
|
||||
ensureListener();
|
||||
persistSubagentRuns();
|
||||
if (archiveAfterMs) startSweeper();
|
||||
if (archiveAfterMs) {
|
||||
startSweeper();
|
||||
}
|
||||
// Wait for subagent completion via gateway RPC (cross-process).
|
||||
// The in-process lifecycle listener is a fallback for embedded runs.
|
||||
void waitForSubagentCompletion(params.runId, waitTimeoutMs);
|
||||
@@ -290,9 +330,13 @@ async function waitForSubagentCompletion(runId: string, waitTimeoutMs: number) {
|
||||
},
|
||||
timeoutMs: timeoutMs + 10_000,
|
||||
});
|
||||
if (wait?.status !== "ok" && wait?.status !== "error") return;
|
||||
if (wait?.status !== "ok" && wait?.status !== "error") {
|
||||
return;
|
||||
}
|
||||
const entry = subagentRuns.get(runId);
|
||||
if (!entry) return;
|
||||
if (!entry) {
|
||||
return;
|
||||
}
|
||||
let mutated = false;
|
||||
if (typeof wait.startedAt === "number") {
|
||||
entry.startedAt = wait.startedAt;
|
||||
@@ -309,8 +353,12 @@ async function waitForSubagentCompletion(runId: string, waitTimeoutMs: number) {
|
||||
entry.outcome =
|
||||
wait.status === "error" ? { status: "error", error: wait.error } : { status: "ok" };
|
||||
mutated = true;
|
||||
if (mutated) persistSubagentRuns();
|
||||
if (!beginSubagentCleanup(runId)) return;
|
||||
if (mutated) {
|
||||
persistSubagentRuns();
|
||||
}
|
||||
if (!beginSubagentCleanup(runId)) {
|
||||
return;
|
||||
}
|
||||
const requesterOrigin = normalizeDeliveryContext(entry.requesterOrigin);
|
||||
void runSubagentAnnounceFlow({
|
||||
childSessionKey: entry.childSessionKey,
|
||||
@@ -354,13 +402,19 @@ export function addSubagentRunForTests(entry: SubagentRunRecord) {
|
||||
|
||||
export function releaseSubagentRun(runId: string) {
|
||||
const didDelete = subagentRuns.delete(runId);
|
||||
if (didDelete) persistSubagentRuns();
|
||||
if (subagentRuns.size === 0) stopSweeper();
|
||||
if (didDelete) {
|
||||
persistSubagentRuns();
|
||||
}
|
||||
if (subagentRuns.size === 0) {
|
||||
stopSweeper();
|
||||
}
|
||||
}
|
||||
|
||||
export function listSubagentRunsForRequester(requesterSessionKey: string): SubagentRunRecord[] {
|
||||
const key = requesterSessionKey.trim();
|
||||
if (!key) return [];
|
||||
if (!key) {
|
||||
return [];
|
||||
}
|
||||
return [...subagentRuns.values()].filter((entry) => entry.requesterSessionKey === key);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user