mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-22 02:15:26 -06:00
fix: persist manual Codex compactions (#127230)
This commit is contained in:
@@ -32,6 +32,7 @@ import {
|
||||
type CodexAppServerLiveThreadOwnership,
|
||||
} from "./client-runtime.js";
|
||||
import { CodexAppServerRpcError, type CodexAppServerClient } from "./client.js";
|
||||
import { persistCodexContextCompactionActivity } from "./context-compaction-activity.js";
|
||||
import { readCodexThreadContextSnapshot } from "./event-projector-usage.js";
|
||||
import {
|
||||
readCodexNotificationThreadId,
|
||||
@@ -70,7 +71,7 @@ type CodexAppServerCompactOptions = {
|
||||
};
|
||||
|
||||
type CodexNativeCompactionCompletion =
|
||||
| { completed: true; tokensAfter?: number }
|
||||
| { completed: true; turnId?: string; itemId?: string; tokensAfter?: number }
|
||||
| { completed: false; reason: string };
|
||||
|
||||
function watchCodexNativeCompactionCompletion(params: {
|
||||
@@ -111,7 +112,12 @@ function watchCodexNativeCompactionCompletion(params: {
|
||||
resolveCompletion(result);
|
||||
};
|
||||
const complete = () =>
|
||||
finish({ completed: true, ...(tokensAfter !== undefined ? { tokensAfter } : {}) });
|
||||
finish({
|
||||
completed: true,
|
||||
...(compactionTurnId ? { turnId: compactionTurnId } : {}),
|
||||
...(compactionItemId ? { itemId: compactionItemId } : {}),
|
||||
...(tokensAfter !== undefined ? { tokensAfter } : {}),
|
||||
});
|
||||
const fail = (reason: string) => finish({ completed: false, reason });
|
||||
const retireUnconfirmed = (reason: string) => {
|
||||
if (settled || retirementStarted) {
|
||||
@@ -742,6 +748,18 @@ async function compactCodexNativeThread(
|
||||
throw new Error(completion.reason);
|
||||
}
|
||||
tokensAfter = completion.tokensAfter;
|
||||
if (completion.turnId && completion.itemId) {
|
||||
await persistCodexContextCompactionActivity({
|
||||
sessionTarget: params.sessionTarget,
|
||||
config: params.config,
|
||||
cwd: params.workspaceDir,
|
||||
runId: params.runId,
|
||||
threadId: binding.threadId,
|
||||
turnId: completion.turnId,
|
||||
itemId: completion.itemId,
|
||||
timestamp: Date.now(),
|
||||
});
|
||||
}
|
||||
embeddedAgentLog.info("completed codex app-server compaction", {
|
||||
sessionId: params.sessionId,
|
||||
threadId: binding.threadId,
|
||||
|
||||
@@ -28,15 +28,13 @@ describe("persistCodexContextCompactionActivity", () => {
|
||||
messageId: "activity-message",
|
||||
});
|
||||
const params = {
|
||||
run: {
|
||||
runId: "run-1",
|
||||
workspaceDir: "/workspace",
|
||||
sessionTarget: {
|
||||
agentId: "main",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "agent:main:dashboard:session-1",
|
||||
storePath: "/state/openclaw-agent.sqlite",
|
||||
},
|
||||
runId: "run-1",
|
||||
cwd: "/workspace",
|
||||
sessionTarget: {
|
||||
agentId: "main",
|
||||
sessionId: "session-1",
|
||||
sessionKey: "agent:main:dashboard:session-1",
|
||||
storePath: "/state/openclaw-agent.sqlite",
|
||||
},
|
||||
threadId: "thread-1",
|
||||
turnId: "turn-1",
|
||||
|
||||
@@ -11,13 +11,16 @@ import {
|
||||
const CONTEXT_COMPACTION_CUSTOM_TYPE = "openclaw.context-compaction";
|
||||
|
||||
export async function persistCodexContextCompactionActivity(params: {
|
||||
run: EmbeddedRunAttemptParams;
|
||||
sessionTarget?: EmbeddedRunAttemptParams["sessionTarget"];
|
||||
config?: EmbeddedRunAttemptParams["config"];
|
||||
cwd?: string;
|
||||
runId?: string;
|
||||
threadId: string;
|
||||
turnId: string;
|
||||
itemId: string;
|
||||
timestamp: number;
|
||||
}): Promise<void> {
|
||||
const target = params.run.sessionTarget;
|
||||
const target = params.sessionTarget;
|
||||
if (!target?.sessionId || !target.sessionKey || !target.storePath) {
|
||||
return;
|
||||
}
|
||||
@@ -34,7 +37,7 @@ export async function persistCodexContextCompactionActivity(params: {
|
||||
threadId: params.threadId,
|
||||
turnId: params.turnId,
|
||||
itemId: params.itemId,
|
||||
runId: params.run.runId,
|
||||
...(params.runId ? { runId: params.runId } : {}),
|
||||
},
|
||||
timestamp: params.timestamp,
|
||||
idempotencyKey: activityId,
|
||||
@@ -45,8 +48,8 @@ export async function persistCodexContextCompactionActivity(params: {
|
||||
sessionId: target.sessionId,
|
||||
sessionKey: target.sessionKey,
|
||||
storePath: target.storePath,
|
||||
config: params.run.config,
|
||||
cwd: params.run.workspaceDir,
|
||||
config: params.config,
|
||||
cwd: params.cwd,
|
||||
eventId: activityId,
|
||||
message,
|
||||
});
|
||||
@@ -61,7 +64,7 @@ export async function persistCodexContextCompactionActivity(params: {
|
||||
update: {
|
||||
message: appended.message,
|
||||
messageId: appended.messageId,
|
||||
runId: params.run.runId,
|
||||
...(params.runId ? { runId: params.runId } : {}),
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -522,7 +522,10 @@ export class CodexAppServerEventProjector {
|
||||
},
|
||||
});
|
||||
await persistCodexContextCompactionActivity({
|
||||
run: this.params,
|
||||
sessionTarget: this.params.sessionTarget,
|
||||
config: this.params.config,
|
||||
cwd: this.params.workspaceDir,
|
||||
runId: this.params.runId,
|
||||
threadId: this.threadId,
|
||||
turnId: this.turnId,
|
||||
itemId,
|
||||
|
||||
Reference in New Issue
Block a user