Skip to content

Commit c8b1713

Browse files
authored
Merge branch 'main' into codex/autoqa-cron-autodisabled
2 parents b4fb276 + 3289e08 commit c8b1713

43 files changed

Lines changed: 1848 additions & 1340 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/plan/computer-use.md‎

Lines changed: 182 additions & 0 deletions
Large diffs are not rendered by default.

‎extensions/codex/src/app-server/bounded-turn.ts‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
22
import path from "node:path";
33
import type { AuthProfileStore } from "openclaw/plugin-sdk/agent-runtime";
44
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
5+
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
56
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
67
import { readStringField as readString } from "openclaw/plugin-sdk/string-coerce-runtime";
78
import { resolvePreferredOpenClawTmpDir, withTempWorkspace } from "openclaw/plugin-sdk/temp-path";
@@ -514,10 +515,7 @@ function createCodexBoundedTurnCollector(
514515
const completedItems = new Map<string, CodexThreadItem>();
515516
const assistantTextByItem = new Map<string, string>();
516517
const assistantItemOrder: string[] = [];
517-
let resolveCompletion: (() => void) | undefined;
518-
const completion = new Promise<void>((resolve) => {
519-
resolveCompletion = resolve;
520-
});
518+
const { promise: completion, resolve: resolveCompletion } = createDeferred<void>();
521519

522520
const rememberAssistantText = (itemId: string, text: string) => {
523521
if (!text) {
@@ -566,7 +564,7 @@ function createCodexBoundedTurnCollector(
566564
if (notification.method === "turn/completed") {
567565
completedTurn =
568566
readCodexTurnCompletedNotification(notification.params)?.turn ?? completedTurn;
569-
resolveCompletion?.();
567+
resolveCompletion();
570568
return;
571569
}
572570
if (notification.method === "error") {
@@ -576,7 +574,7 @@ function createCodexBoundedTurnCollector(
576574
promptError =
577575
readCodexErrorNotification(notification.params)?.error.message ??
578576
`codex app-server ${taskLabel} turn failed`;
579-
resolveCompletion?.();
577+
resolveCompletion();
580578
}
581579
};
582580

‎extensions/codex/src/app-server/compact.ts‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { resolveAgentDir, resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
1212
import { createDedupeCache } from "openclaw/plugin-sdk/dedupe-runtime";
1313
import { coerceErrorMessage } from "openclaw/plugin-sdk/error-runtime";
14+
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
1415
import { KeyedAsyncQueue } from "openclaw/plugin-sdk/keyed-async-queue";
1516
import { asOptionalRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
1617
import { isIncognitoSessionKey } from "../incognito-session.js";
@@ -87,10 +88,8 @@ function watchCodexNativeCompactionCompletion(params: {
8788
let compactionItemId: string | undefined;
8889
let compactionItemCompleted = false;
8990
let tokensAfter: number | undefined;
90-
let resolveCompletion = (_result: CodexNativeCompactionCompletion) => {};
91-
const completion = new Promise<CodexNativeCompactionCompletion>((resolve) => {
92-
resolveCompletion = resolve;
93-
});
91+
const { promise: completion, resolve: resolveCompletion } =
92+
createDeferred<CodexNativeCompactionCompletion>();
9493
let removeNotificationHandler = () => {};
9594
let removeCloseHandler = () => {};
9695
let removeAbortHandler = () => {};
@@ -110,6 +109,7 @@ function watchCodexNativeCompactionCompletion(params: {
110109
};
111110
const complete = () =>
112111
finish({ completed: true, ...(tokensAfter !== undefined ? { tokensAfter } : {}) });
112+
const fail = (reason: string) => finish({ completed: false, reason });
113113
const retireUnconfirmed = (reason: string) => {
114114
if (settled || retirementStarted) {
115115
return;
@@ -118,7 +118,7 @@ function watchCodexNativeCompactionCompletion(params: {
118118
// Timers started under the short-lived binding lease inherit its async
119119
// owner. Remote retirement must not reuse that already-released token.
120120
void runOutsideBindingLease(() => params.retireUnconfirmed())
121-
.then(() => finish({ completed: false, reason }))
121+
.then(() => fail(reason))
122122
.catch((error: unknown) => {
123123
embeddedAgentLog.error("failed to retire unconfirmed codex app-server compaction", {
124124
threadId: params.threadId,
@@ -150,11 +150,9 @@ function watchCodexNativeCompactionCompletion(params: {
150150
complete();
151151
return;
152152
}
153-
finish({
154-
completed: false,
155-
reason:
156-
"codex app-server compaction reached terminal state without a completed compaction item",
157-
});
153+
fail(
154+
"codex app-server compaction reached terminal state without a completed compaction item",
155+
);
158156
return;
159157
}
160158
embeddedAgentLog.warn("codex app-server compaction interrupt request failed", {
@@ -251,10 +249,7 @@ function watchCodexNativeCompactionCompletion(params: {
251249
const turn = isJsonObject(notification.params.turn) ? notification.params.turn : undefined;
252250
const status = typeof turn?.status === "string" ? turn.status : undefined;
253251
if (status !== "completed") {
254-
finish({
255-
completed: false,
256-
reason: `codex app-server compaction turn ended with status ${status ?? "unknown"}`,
257-
});
252+
fail(`codex app-server compaction turn ended with status ${status ?? "unknown"}`);
258253
return;
259254
}
260255
const incompleteReason = !compactionItemId
@@ -263,7 +258,7 @@ function watchCodexNativeCompactionCompletion(params: {
263258
? "codex app-server compaction turn completed before its compaction item"
264259
: undefined;
265260
if (incompleteReason) {
266-
finish({ completed: false, reason: incompleteReason });
261+
fail(incompleteReason);
267262
return;
268263
}
269264
complete();
@@ -291,15 +286,14 @@ function watchCodexNativeCompactionCompletion(params: {
291286
beginInterruptGrace();
292287
}
293288
},
294-
confirmRequestRejected: () =>
295-
finish({ completed: false, reason: "codex app-server rejected the compaction request" }),
289+
confirmRequestRejected: () => fail("codex app-server rejected the compaction request"),
296290
retireUnconfirmedRequest: async (reason: string) => {
297291
retireUnconfirmed(reason);
298292
return await completion;
299293
},
300294
cancel: () => {
301295
if (!requestStarted) {
302-
finish({ completed: false, reason: "compaction request did not start" });
296+
fail("compaction request did not start");
303297
}
304298
},
305299
};

‎extensions/codex/src/app-server/event-projector-native-tool-lifecycle.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ import {
1919
emitCodexNativePreToolUseFailureDiagnostic,
2020
type CodexNativePreToolUseFailure,
2121
} from "./native-hook-relay.js";
22-
import {
23-
readCodexNotificationThreadId,
24-
readCodexNotificationTurnId,
25-
} from "./notification-correlation.js";
22+
import { isCodexNotificationForTurn } from "./notification-correlation.js";
2623
import { readCodexTurn } from "./protocol-validators.js";
2724
import {
2825
isJsonObject,
@@ -73,11 +70,7 @@ export class CodexNativeToolLifecycleProjector {
7370

7471
handleNotification(notification: CodexServerNotification): void {
7572
const params = isJsonObject(notification.params) ? notification.params : undefined;
76-
if (
77-
!params ||
78-
readCodexNotificationThreadId(params) !== this.threadId ||
79-
readCodexNotificationTurnId(params) !== this.turnId
80-
) {
73+
if (!params || !isCodexNotificationForTurn(params, this.threadId, this.turnId)) {
8174
return;
8275
}
8376
if (notification.method === "turn/completed") {

‎extensions/codex/src/app-server/event-projector.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ import {
4141
} from "./event-projector-values.js";
4242
import type { CodexNativePreToolUseFailure } from "./native-hook-relay.js";
4343
import {
44+
isCodexNotificationForTurn,
4445
readCodexNotificationThreadId,
45-
readCodexNotificationTurnId,
4646
} from "./notification-correlation.js";
4747
import { readCodexTurn } from "./protocol-validators.js";
4848
import {
@@ -221,7 +221,7 @@ export class CodexAppServerEventProjector {
221221
if (readCodexNotificationThreadId(params) !== this.threadId) {
222222
return;
223223
}
224-
} else if (!this.isNotificationForTurn(params)) {
224+
} else if (!isCodexNotificationForTurn(params, this.threadId, this.turnId)) {
225225
return;
226226
}
227227
this.nativeToolLifecycleProjector.handleNotification(notification);
@@ -642,12 +642,6 @@ export class CodexAppServerEventProjector {
642642
}
643643
}
644644

645-
private isNotificationForTurn(params: JsonObject): boolean {
646-
const threadId = readCodexNotificationThreadId(params);
647-
const turnId = readCodexNotificationTurnId(params);
648-
return threadId === this.threadId && turnId === this.turnId;
649-
}
650-
651645
private isHookNotificationForCurrentThread(params: JsonObject): boolean {
652646
const threadId = readString(params, "threadId");
653647
const turnId = params.turnId;

‎extensions/codex/src/app-server/run-attempt-turn-state.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { emitTrustedDiagnosticEvent } from "openclaw/plugin-sdk/diagnostic-runtime";
2+
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
23
import {
34
CODEX_APP_SERVER_INTERRUPT_TIMEOUT_MS,
45
closeCodexStartupClientBestEffort,
@@ -81,10 +82,7 @@ export function createCodexAttemptTurnState(resources: CodexAttemptResources) {
8182
terminalDynamicToolReleaseCheckScheduled: false,
8283
currentTurnHadNonTerminalDynamicToolResult: false,
8384
};
84-
let resolveCompletion!: () => void;
85-
const completion = new Promise<void>((resolve) => {
86-
resolveCompletion = resolve;
87-
});
85+
const { promise: completion, resolve: resolveCompletion } = createDeferred<void>();
8886
const turnCompletionIdleTimeoutMs = resolveCodexTurnCompletionIdleTimeoutMs(
8987
options.turnCompletionIdleTimeoutMs ?? appServer.turnCompletionIdleTimeoutMs,
9088
);

‎extensions/codex/src/app-server/side-question.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ import {
7474
emitCodexNativePreToolUseFailureDiagnostic,
7575
type CodexNativePreToolUseFailure,
7676
} from "./native-hook-relay.js";
77-
import {
78-
readCodexNotificationThreadId,
79-
readCodexNotificationTurnId,
80-
} from "./notification-correlation.js";
77+
import { isCodexNotificationForTurn } from "./notification-correlation.js";
8178
import {
8279
buildCodexPluginAppsConfigPatchFromPolicyContext,
8380
mergeCodexThreadConfigs,
@@ -1219,7 +1216,7 @@ class CodexSideQuestionCollector {
12191216
this.pendingNotifications.push(notification);
12201217
return;
12211218
}
1222-
if (!isNotificationForTurn(params, this.threadId, this.turnId)) {
1219+
if (!isCodexNotificationForTurn(params, this.threadId, this.turnId)) {
12231220
return;
12241221
}
12251222
if (notification.method === "item/agentMessage/delta") {
@@ -1347,16 +1344,6 @@ function collectAssistantText(turn: CodexTurn): string {
13471344
return messages.at(-1) ?? "";
13481345
}
13491346

1350-
function isNotificationForTurn(params: JsonObject, threadId: string, turnId: string): boolean {
1351-
return (
1352-
readCodexNotificationThreadId(params) === threadId && readNotificationTurnId(params) === turnId
1353-
);
1354-
}
1355-
1356-
function readNotificationTurnId(record: JsonObject): string | undefined {
1357-
return readCodexNotificationTurnId(record);
1358-
}
1359-
13601347
function formatCodexErrorMessage(params: JsonObject, rateLimits: JsonValue | undefined): Error {
13611348
const error = isJsonObject(params.error) ? params.error : undefined;
13621349
const message =

‎extensions/codex/src/app-server/turn-router.ts‎

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Keyed routing for all turn traffic on one shared Codex app-server client. */
22
import { embeddedAgentLog } from "openclaw/plugin-sdk/agent-harness-runtime";
3+
import { createDeferred } from "openclaw/plugin-sdk/extension-shared";
34
import type { CodexAppServerClient } from "./client.js";
45
import { redactCodexEventKind } from "./event-projector-diagnostics.js";
56
import {
@@ -75,7 +76,7 @@ type CodexNativeTurnCompletionWatch = {
7576
cancel: () => void;
7677
};
7778

78-
type Deferred = { promise: Promise<void>; resolve: () => void };
79+
type Deferred = ReturnType<typeof createDeferred<void>>;
7980
type PendingNotification = {
8081
notification: CodexServerNotification;
8182
receivedAtMs: number;
@@ -143,8 +144,8 @@ class ClientTurnRouter implements CodexAppServerTurnRouter {
143144
const route: Route = {
144145
threadId,
145146
controller: new AbortController(),
146-
ended: deferred(),
147-
activated: deferred(),
147+
ended: createDeferred<void>(),
148+
activated: createDeferred<void>(),
148149
gate: "open",
149150
pending: [],
150151
notificationTail: Promise.resolve(),
@@ -196,10 +197,7 @@ class ClientTurnRouter implements CodexAppServerTurnRouter {
196197
if (this.routes.get(threadId)?.completedNativeTurnIds.delete(turnId)) {
197198
return { completion: Promise.resolve(true), cancel: () => {} };
198199
}
199-
let settle!: (completed: boolean) => void;
200-
const completion = new Promise<boolean>((resolve) => {
201-
settle = resolve;
202-
});
200+
const { promise: completion, resolve: settle } = createDeferred<boolean>();
203201
const watchers =
204202
this.nativeTurnCompletionWatchers.get(threadId) ?? new Set<NativeTurnCompletionWatcher>();
205203
this.nativeTurnCompletionWatchers.set(threadId, watchers);
@@ -281,7 +279,7 @@ class ClientTurnRouter implements CodexAppServerTurnRouter {
281279
if (route.observedNativeTurn?.completed) {
282280
route.observedNativeTurn = undefined;
283281
}
284-
route.binding = deferred();
282+
route.binding = createDeferred<void>();
285283
}
286284

287285
private async cancelTurn(route: Route): Promise<void> {
@@ -589,14 +587,6 @@ async function waitForPromiseOrAbort(
589587
}
590588
}
591589

592-
function deferred(): Deferred {
593-
let resolve!: () => void;
594-
const promise = new Promise<void>((resolvePromise) => {
595-
resolve = resolvePromise;
596-
});
597-
return { promise, resolve };
598-
}
599-
600590
function abortReason(signal: AbortSignal): Error {
601591
return signal.reason instanceof Error
602592
? signal.reason

‎packages/ai/src/providers/openai-chatgpt-responses.ts‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { responsesPromptObserver } from "../transports/openai-responses-contract
4646
import { ResponsesStreamFailure } from "../transports/openai-responses-debug.js";
4747
import { createResponsesPromptEgressObserver } from "../transports/openai-responses-prompt-observer-internal.js";
4848
import {
49+
commitResponsesEncryptedContentAttempt,
4950
isInvalidEncryptedContentError,
5051
resolveNextResponsesEncryptedContentAttempt,
5152
type ResponsesEncryptedContentAttempt,
@@ -313,6 +314,10 @@ export const streamOpenAICodexResponses: StreamFunction<
313314
kind: "initial",
314315
request: await buildBody("checkpoint"),
315316
};
317+
const commitSemanticAttempt = (attempt: ResponsesEncryptedContentAttempt<RequestBody>) =>
318+
commitResponsesEncryptedContentAttempt(attempt, (checkpoint) =>
319+
suppressOpenAIResponsesCompaction(output, model, options, checkpoint),
320+
);
316321
const observePromptEgress = createResponsesPromptEgressObserver(
317322
options,
318323
context.systemPrompt,
@@ -356,14 +361,7 @@ export const streamOpenAICodexResponses: StreamFunction<
356361
stream,
357362
model,
358363
() => {
359-
if (activeAttempt.kind === "compaction-stripped") {
360-
suppressOpenAIResponsesCompaction(
361-
output,
362-
model,
363-
options,
364-
activeAttempt.rejectedCompaction,
365-
);
366-
}
364+
commitSemanticAttempt(activeAttempt);
367365
websocketStarted = true;
368366
},
369367
requestOptions,
@@ -561,14 +559,7 @@ export const streamOpenAICodexResponses: StreamFunction<
561559
if (activeSignal?.aborted) {
562560
throw transportAbortError(activeSignal);
563561
}
564-
if (activeAttempt.kind === "compaction-stripped") {
565-
suppressOpenAIResponsesCompaction(
566-
output,
567-
model,
568-
options,
569-
activeAttempt.rejectedCompaction,
570-
);
571-
}
562+
commitSemanticAttempt(activeAttempt);
572563
break;
573564
}
574565

0 commit comments

Comments
 (0)