惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
D
Docker
J
Java Code Geeks
腾讯CDC
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
博客园 - Franky
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(tasks): clean orphaned parent-owned acp sessions · op...
steipete · 2026-04-29 · via Recent Commits to openclaw:main

@@ -1,5 +1,9 @@

11

import { getAcpSessionManager } from "../acp/control-plane/manager.js";

2-

import { readAcpSessionEntry } from "../acp/runtime/session-meta.js";

2+

import {

3+

listAcpSessionEntries,

4+

readAcpSessionEntry,

5+

type AcpSessionStoreEntry,

6+

} from "../acp/runtime/session-meta.js";

37

import { loadSessionStore, resolveStorePath } from "../config/sessions.js";

48

import type { OpenClawConfig } from "../config/types.openclaw.js";

59

import { isCronJobActive } from "../cron/active-jobs.js";

@@ -55,6 +59,7 @@ let configuredCronStorePath: string | undefined;

5559

let configuredCronRuntimeAuthoritative = false;

56605761

type TaskRegistryMaintenanceRuntime = {

62+

listAcpSessionEntries: typeof listAcpSessionEntries;

5863

readAcpSessionEntry: typeof readAcpSessionEntry;

5964

closeAcpSession?: (params: {

6065

cfg: OpenClawConfig;

@@ -85,6 +90,7 @@ type TaskRegistryMaintenanceRuntime = {

8590

};

86918792

const defaultTaskRegistryMaintenanceRuntime: TaskRegistryMaintenanceRuntime = {

93+

listAcpSessionEntries,

8894

readAcpSessionEntry,

8995

closeAcpSession: async ({ cfg, sessionKey, reason }) => {

9096

await getAcpSessionManager().closeSession({

@@ -421,6 +427,24 @@ function hasActiveTaskForChildSession(task: TaskRecord, tasks: TaskRecord[]): bo

421427

);

422428

}

423429430+

function hasActiveTaskForChildSessionKey(sessionKey: string, tasks: TaskRecord[]): boolean {

431+

const normalized = normalizeOptionalString(sessionKey);

432+

if (!normalized) {

433+

return false;

434+

}

435+

return tasks.some(

436+

(candidate) =>

437+

isActiveTask(candidate) && getNormalizedTaskChildSessionKey(candidate) === normalized,

438+

);

439+

}

440+441+

function getAcpSessionParentKeys(acpEntry: Pick<AcpSessionStoreEntry, "entry">): string[] {

442+

return [

443+

normalizeOptionalString(acpEntry.entry?.spawnedBy),

444+

normalizeOptionalString(acpEntry.entry?.parentSessionKey),

445+

].filter((value): value is string => Boolean(value));

446+

}

447+424448

function isParentOwnedAcpSessionTask(

425449

task: TaskRecord,

426450

acpEntry: ReturnType<typeof readAcpSessionEntry>,

@@ -431,13 +455,14 @@ function isParentOwnedAcpSessionTask(

431455

}

432456

const ownerKey = normalizeOptionalString(task.ownerKey);

433457

const requesterKey = normalizeOptionalString(task.requesterSessionKey);

434-

const parentKeys = [

435-

normalizeOptionalString(entry.spawnedBy),

436-

normalizeOptionalString(entry.parentSessionKey),

437-

].filter((value): value is string => Boolean(value));

458+

const parentKeys = getAcpSessionParentKeys({ entry });

438459

return parentKeys.some((parentKey) => parentKey === ownerKey || parentKey === requesterKey);

439460

}

440461462+

function isParentOwnedAcpSessionEntry(acpEntry: Pick<AcpSessionStoreEntry, "entry">): boolean {

463+

return getAcpSessionParentKeys(acpEntry).length > 0;

464+

}

465+441466

function hasActiveSessionBinding(sessionKey: string): boolean {

442467

const listBindings = taskRegistryMaintenanceRuntime.listSessionBindingsBySession;

443468

if (!listBindings) {

@@ -471,6 +496,23 @@ function shouldCloseTerminalAcpSession(task: TaskRecord, tasks: TaskRecord[]): b

471496

return !hasActiveSessionBinding(sessionKey);

472497

}

473498499+

function shouldCloseOrphanedParentOwnedAcpSession(

500+

acpEntry: AcpSessionStoreEntry,

501+

tasks: TaskRecord[],

502+

): boolean {

503+

if (!acpEntry.entry || !acpEntry.acp || !isParentOwnedAcpSessionEntry(acpEntry)) {

504+

return false;

505+

}

506+

const sessionKey = normalizeOptionalString(acpEntry.sessionKey);

507+

if (!sessionKey || hasActiveTaskForChildSessionKey(sessionKey, tasks)) {

508+

return false;

509+

}

510+

if (acpEntry.acp.mode === "oneshot") {

511+

return true;

512+

}

513+

return !hasActiveSessionBinding(sessionKey);

514+

}

515+474516

async function cleanupTerminalAcpSession(task: TaskRecord, tasks: TaskRecord[]): Promise<void> {

475517

if (!shouldCloseTerminalAcpSession(task, tasks)) {

476518

return;

@@ -512,6 +554,55 @@ async function cleanupTerminalAcpSession(task: TaskRecord, tasks: TaskRecord[]):

512554

}

513555

}

514556557+

async function cleanupOrphanedParentOwnedAcpSessions(tasks: TaskRecord[]): Promise<void> {

558+

let acpSessions: AcpSessionStoreEntry[];

559+

try {

560+

acpSessions = await taskRegistryMaintenanceRuntime.listAcpSessionEntries({});

561+

} catch (error) {

562+

log.warn("Failed to list ACP sessions during task maintenance", { error });

563+

return;

564+

}

565+

const seenSessionKeys = new Set<string>();

566+

for (const acpEntry of acpSessions) {

567+

const sessionKey = normalizeOptionalString(acpEntry.sessionKey);

568+

if (!sessionKey || seenSessionKeys.has(sessionKey)) {

569+

continue;

570+

}

571+

seenSessionKeys.add(sessionKey);

572+

if (!shouldCloseOrphanedParentOwnedAcpSession(acpEntry, tasks)) {

573+

continue;

574+

}

575+

const closeAcpSession = taskRegistryMaintenanceRuntime.closeAcpSession;

576+

if (!closeAcpSession) {

577+

continue;

578+

}

579+

try {

580+

await closeAcpSession({

581+

cfg: acpEntry.cfg,

582+

sessionKey,

583+

reason: "orphaned-parent-task-cleanup",

584+

});

585+

} catch (error) {

586+

log.warn("Failed to close orphaned parent-owned ACP session during task maintenance", {

587+

sessionKey,

588+

error,

589+

});

590+

continue;

591+

}

592+

try {

593+

await taskRegistryMaintenanceRuntime.unbindSessionBindings?.({

594+

targetSessionKey: sessionKey,

595+

reason: "orphaned-parent-task-cleanup",

596+

});

597+

} catch (error) {

598+

log.warn("Failed to unbind orphaned parent-owned ACP session during task maintenance", {

599+

sessionKey,

600+

error,

601+

});

602+

}

603+

}

604+

}

605+515606

function markTaskLost(task: TaskRecord, now: number): TaskRecord {

516607

const cleanupAfter = task.cleanupAfter ?? projectTaskLost(task, now).cleanupAfter;

517608

const updated =

@@ -754,6 +845,7 @@ export async function runTaskRegistryMaintenance(): Promise<TaskRegistryMaintena

754845

await yieldToEventLoop();

755846

}

756847

}

848+

await cleanupOrphanedParentOwnedAcpSessions(taskRegistryMaintenanceRuntime.listTaskRecords());

757849

return { reconciled, recovered, cleanupStamped, pruned };

758850

}

759851