Skip to content

Commit a5bac03

Browse files
committed
fix: preserve Codex compaction activity
1 parent 3ae7ba3 commit a5bac03

8 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Docs: https://docs.openclaw.ai
6969

7070
### Fixes
7171

72+
- **Control UI Codex compaction history:** preserve successful native context compactions as durable, model-excluded activity inside completed work traces after the composer status clears or the session reloads. Fixes #127206. Thanks @shakkernerd.
7273
- **Control UI Codex steering:** preserve pre-steer commentary and tool activity in durable transcript order, keep it visible while active, and collapse it before the steering message after completion. Fixes #126938. Thanks @shakkernerd.
7374
- **Onboarding migration menu:** group Claude, Codex, Hermes, and plugin-provided imports under a single **Import from another agent** setup choice while preserving detected source hints, manual paths, and Back navigation before import begins. Fixes #126440. Thanks @shakkernerd.
7475
- **Onboarding provider hook loading:** scope selected-model hook fallback to the chosen provider so metadata-only setup providers do not load unrelated plugins before configuration completes. Fixes #126408. Thanks @shakkernerd.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
embeddedAgentLog,
3+
formatErrorMessage,
4+
type EmbeddedRunAttemptParamsV2 as EmbeddedRunAttemptParams,
5+
} from "openclaw/plugin-sdk/agent-harness-runtime";
6+
import {
7+
appendSessionTranscriptMessageByIdentity,
8+
publishSessionTranscriptUpdateByIdentity,
9+
} from "openclaw/plugin-sdk/session-transcript-runtime";
10+
11+
const CONTEXT_COMPACTION_CUSTOM_TYPE = "openclaw.context-compaction";
12+
13+
export async function persistCodexContextCompactionActivity(params: {
14+
run: EmbeddedRunAttemptParams;
15+
threadId: string;
16+
turnId: string;
17+
itemId: string;
18+
timestamp: number;
19+
}): Promise<void> {
20+
const target = params.run.sessionTarget;
21+
if (!target?.sessionId || !target.sessionKey || !target.storePath) {
22+
return;
23+
}
24+
const activityId = `codex-context-compaction:${params.threadId}:${params.turnId}:${params.itemId}`;
25+
const message = {
26+
role: "custom" as const,
27+
customType: CONTEXT_COMPACTION_CUSTOM_TYPE,
28+
content: "Context compacted",
29+
display: true,
30+
excludeFromContext: true,
31+
details: {
32+
kind: "context_compaction",
33+
backend: "codex-app-server",
34+
threadId: params.threadId,
35+
turnId: params.turnId,
36+
itemId: params.itemId,
37+
runId: params.run.runId,
38+
},
39+
timestamp: params.timestamp,
40+
idempotencyKey: activityId,
41+
};
42+
try {
43+
const appended = await appendSessionTranscriptMessageByIdentity({
44+
agentId: target.agentId,
45+
sessionId: target.sessionId,
46+
sessionKey: target.sessionKey,
47+
storePath: target.storePath,
48+
config: params.run.config,
49+
cwd: params.run.workspaceDir,
50+
eventId: activityId,
51+
message,
52+
});
53+
if (!appended?.appended) {
54+
return;
55+
}
56+
await publishSessionTranscriptUpdateByIdentity({
57+
agentId: target.agentId,
58+
sessionId: target.sessionId,
59+
sessionKey: target.sessionKey,
60+
storePath: target.storePath,
61+
update: {
62+
message: appended.message,
63+
messageId: appended.messageId,
64+
runId: params.run.runId,
65+
},
66+
});
67+
} catch (error) {
68+
embeddedAgentLog.warn("failed to persist codex context compaction activity", {
69+
error: formatErrorMessage(error),
70+
itemId: params.itemId,
71+
});
72+
}
73+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "openclaw/plugin-sdk/agent-harness-runtime";
1111
import { readStringField as readString } from "openclaw/plugin-sdk/string-coerce-runtime";
1212
import type { AttemptFailureSource, EmbeddedRunAttemptResult } from "./attempt-terminal.js";
13+
import { persistCodexContextCompactionActivity } from "./context-compaction-activity.js";
1314
import { CodexAssistantProjection } from "./event-projector-assistant.js";
1415
import { CodexProjectionDiagnostics } from "./event-projector-diagnostics.js";
1516
import { CodexEventProjection } from "./event-projector-events.js";
@@ -520,6 +521,13 @@ export class CodexAppServerEventProjector {
520521
channelId: this.params.messageChannel ?? this.params.messageProvider ?? undefined,
521522
},
522523
});
524+
await persistCodexContextCompactionActivity({
525+
run: this.params,
526+
threadId: this.threadId,
527+
turnId: this.turnId,
528+
itemId,
529+
timestamp: this.nextTranscriptTimestamp(),
530+
});
523531
this.emitCompactionEnd(itemId, true);
524532
}
525533
this.toolProgressProjection.recordToolMeta(item);

‎packages/agent-core/src/harness/messages.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ export function convertToLlm(messages: AgentMessage[]): Message[] {
143143
timestamp: message.timestamp,
144144
};
145145
case "custom": {
146+
if (message.excludeFromContext) {
147+
return undefined;
148+
}
146149
const content =
147150
typeof message.content === "string"
148151
? [{ type: "text" as const, text: message.content }]

‎packages/agent-core/src/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,8 @@ export interface CustomMessage<T = unknown> {
427427
content: string | (TextContent | ImageContent)[];
428428
/** Whether UI surfaces should display this message. */
429429
display: boolean;
430+
/** Keep display-only application activity out of future model context. */
431+
excludeFromContext?: boolean;
430432
/** Optional application-specific metadata. */
431433
details?: T;
432434
/** Millisecond timestamp for transcript ordering. */

‎ui/src/pages/chat/chat-progress.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { asNullableRecord as asRecord } from "@openclaw/normalization-core/record-coerce";
12
import { t } from "../../i18n/index.ts";
23
import type { ChatItem, ChatQueueItem } from "../../lib/chat/chat-types.ts";
34
import { formatCompactTokenCount } from "../../lib/format.ts";
@@ -11,6 +12,25 @@ type WorkingProgressCache = WorkingProgress & {
1112
runId: string | null;
1213
};
1314

15+
const CONTEXT_COMPACTION_CUSTOM_TYPE = "openclaw.context-compaction";
16+
17+
export function projectContextCompactionActivity(message: unknown): unknown {
18+
const record = asRecord(message);
19+
if (record?.role !== "custom" || record.customType !== CONTEXT_COMPACTION_CUSTOM_TYPE) {
20+
return message;
21+
}
22+
const metadata = asRecord(record["__openclaw"]);
23+
return {
24+
...record,
25+
role: "assistant",
26+
content: [{ type: "text", text: t("chat.composer.contextCompacted") }],
27+
__openclaw: {
28+
...metadata,
29+
runtimeActivityKind: "context_compaction",
30+
},
31+
};
32+
}
33+
1434
const workingProgressBySession = new Map<string, WorkingProgressCache>();
1535
let anonymousWorkingProgressId = 0;
1636

‎ui/src/pages/chat/chat-thread-build.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
buildCompactionDividerItem,
2525
buildResetDividerItem,
2626
clearWorkingProgress,
27+
projectContextCompactionActivity,
2728
resolveWorkingProgress,
2829
shouldRenderQueuedSendInThread,
2930
} from "./chat-progress.ts";
@@ -214,7 +215,7 @@ export function buildChatItems(props: BuildChatItemsProps): Array<ChatItem | Mes
214215
}
215216
}
216217
for (let i = 0; i < history.length; i++) {
217-
const msg = history[i];
218+
const msg = projectContextCompactionActivity(history[i]);
218219
const itemKey = historyKeys[i] ?? messageKey(msg, i);
219220
const normalized = safeNormalizeMessage(msg);
220221
if (!normalized) {

‎ui/src/pages/chat/chat-thread-grouping.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ export function assistantGroupCanOwnActiveRunStatus(group: MessageGroup): boolea
542542
return (
543543
group.role.toLowerCase() === "assistant" &&
544544
!assistantGroupIsForwardedBoundary(group) &&
545+
!group.messages.every(
546+
({ message }) =>
547+
asRecord(asRecord(message)?.["__openclaw"])?.runtimeActivityKind === "context_compaction",
548+
) &&
545549
groupHasVisibleReplyContent(group)
546550
);
547551
}

0 commit comments

Comments
 (0)