

























@@ -11,6 +11,7 @@ import {
1111type resolveSandboxContext,
1212} from "openclaw/plugin-sdk/agent-harness-runtime";
1313import { defaultCodexAppInventoryCache } from "./app-inventory-cache.js";
14+import { closeCodexStartupClientBestEffort } from "./attempt-client-cleanup.js";
1415import { buildCodexPluginThreadConfigEligibilityLogData } from "./attempt-diagnostics.js";
1516import { withCodexStartupTimeout } from "./attempt-timeouts.js";
1617import type { CodexAppServerClientFactory } from "./client-factory.js";
@@ -52,10 +53,8 @@ import {
5253type CodexSandboxExecEnvironment,
5354} from "./sandbox-exec-server.js";
5455import {
55-clearSharedCodexAppServerClientIfCurrentAndUnclaimed,
5656clearSharedCodexAppServerClientIfCurrent,
5757releaseLeasedSharedCodexAppServerClient,
58-retireSharedCodexAppServerClientIfCurrent,
5958} from "./shared-client.js";
6059import {
6160startOrResumeThread,
@@ -136,7 +135,7 @@ export async function startCodexAttemptThread(params: {
136135await releaseStartupResourcesOnTimeout?.();
137136releaseSharedClientLease?.();
138137releaseSharedClientLease = undefined;
139-await closeAbandonedStartupClient(startupClientForAbandonedRequestCleanup);
138+await closeCodexStartupClientBestEffort(startupClientForAbandonedRequestCleanup);
140139startupClientForAbandonedRequestCleanup = undefined;
141140},
142141operation: async () => {
@@ -188,7 +187,7 @@ export async function startCodexAttemptThread(params: {
188187// close any late-arriving client instead of leaking a lease.
189188startupClientForAbandonedRequestCleanup = client;
190189if (startupAbandoned || startupAbandonController.signal.aborted) {
191-void closeAbandonedStartupClient(client);
190+void closeCodexStartupClientBestEffort(client);
192191}
193192},
194193abandonSignal: startupAbandonController.signal,
@@ -346,7 +345,7 @@ export async function startCodexAttemptThread(params: {
346345activeStartupClient.request(method, requestParams, {
347346timeoutMs: params.appServer.requestTimeoutMs,
348347 signal,
349-}),
348+ }),
350349appCache: defaultCodexAppInventoryCache,
351350appCacheKey: pluginAppCacheKey,
352351}),
@@ -394,7 +393,7 @@ export async function startCodexAttemptThread(params: {
394393if (startupClientForAbandonedRequestCleanup === startupClient) {
395394startupClientForAbandonedRequestCleanup = undefined;
396395}
397-await closeAbandonedStartupClient(startupClient);
396+await closeCodexStartupClientBestEffort(startupClient);
398397} else if (
399398shouldClearSharedClientAfterStartupRace(startupAttemptError) ||
400399shouldClearSharedClientAfterStartupFailure({
@@ -405,7 +404,7 @@ export async function startCodexAttemptThread(params: {
405404if (startupClientForAbandonedRequestCleanup === startupClient) {
406405startupClientForAbandonedRequestCleanup = undefined;
407406}
408-await evictFailedStartupClient(startupClient);
407+await closeCodexStartupClientBestEffort(startupClient);
409408}
410409}
411410}
@@ -467,7 +466,7 @@ export async function startCodexAttemptThread(params: {
467466if (params.signal.aborted || shouldClearSharedClientAfterStartupAbandon(error)) {
468467releaseSharedClientLease?.();
469468releaseSharedClientLease = undefined;
470-await closeAbandonedStartupClient(startupClientForAbandonedRequestCleanup);
469+await closeCodexStartupClientBestEffort(startupClientForAbandonedRequestCleanup);
471470startupClientForAbandonedRequestCleanup = undefined;
472471} else if (
473472shouldClearSharedClientAfterStartupRace(error) ||
@@ -478,7 +477,7 @@ export async function startCodexAttemptThread(params: {
478477) {
479478releaseSharedClientLease?.();
480479releaseSharedClientLease = undefined;
481-await evictFailedStartupClient(startupClientForAbandonedRequestCleanup);
480+await closeCodexStartupClientBestEffort(startupClientForAbandonedRequestCleanup);
482481startupClientForAbandonedRequestCleanup = undefined;
483482}
484483throw error;
@@ -487,80 +486,6 @@ export async function startCodexAttemptThread(params: {
487486}
488487}
489488490-async function closeAbandonedStartupClient(
491-client: CodexAppServerClient | undefined,
492-): Promise<void> {
493-if (!client) {
494-return;
495-}
496-const unclaimedSharedClient = clearSharedCodexAppServerClientIfCurrentAndUnclaimed(client);
497-if (unclaimedSharedClient.closed) {
498-await closeClientAndWaitIfAvailable(client);
499-return;
500-}
501-if (unclaimedSharedClient.found) {
502-const retired = retireSharedCodexAppServerClientIfCurrent(client);
503-if (retired?.closed) {
504-await closeClientAndWaitIfAvailable(client);
505-}
506-return;
507-}
508-const retiredSharedClient = retireSharedCodexAppServerClientIfCurrent(client);
509-if (retiredSharedClient) {
510-if (retiredSharedClient.closed) {
511-await closeClientAndWaitIfAvailable(client);
512-}
513-return;
514-}
515-if (clearSharedCodexAppServerClientIfCurrent(client)) {
516-await closeClientAndWaitIfAvailable(client);
517-return;
518-}
519-await closeClientAndWaitIfAvailable(client);
520-}
521-522-async function closeClientAndWaitIfAvailable(client: CodexAppServerClient): Promise<void> {
523-const closeable = client as {
524-close?: CodexAppServerClient["close"];
525-closeAndWait?: CodexAppServerClient["closeAndWait"];
526-};
527-if (typeof closeable.closeAndWait === "function") {
528-await closeable.closeAndWait();
529-return;
530-}
531-closeable.close?.();
532-}
533-534-async function evictFailedStartupClient(client: CodexAppServerClient | undefined): Promise<void> {
535-if (!client) {
536-return;
537-}
538-const unclaimedSharedClient = clearSharedCodexAppServerClientIfCurrentAndUnclaimed(client);
539-if (unclaimedSharedClient.closed) {
540-await closeClientAndWaitIfAvailable(client);
541-return;
542-}
543-if (unclaimedSharedClient.found) {
544-const retired = retireSharedCodexAppServerClientIfCurrent(client);
545-if (retired?.closed) {
546-await closeClientAndWaitIfAvailable(client);
547-}
548-return;
549-}
550-const retiredSharedClient = retireSharedCodexAppServerClientIfCurrent(client);
551-if (retiredSharedClient) {
552-if (retiredSharedClient.closed) {
553-await closeClientAndWaitIfAvailable(client);
554-}
555-return;
556-}
557-if (clearSharedCodexAppServerClientIfCurrent(client)) {
558-await closeClientAndWaitIfAvailable(client);
559-return;
560-}
561-await closeClientAndWaitIfAvailable(client);
562-}
563-564489function shouldClearSharedClientAfterStartupAbandon(error: unknown): boolean {
565490return (
566491error instanceof Error &&
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。