feat: add controlled codex thread rollback

This commit is contained in:
AI Bot
2026-06-03 13:30:24 +08:00
parent 13201e6aee
commit 7a30c2a8d9
11 changed files with 421 additions and 2 deletions

View File

@@ -483,6 +483,7 @@ export type ComputerControlIntentCategory =
| "discussion_only"
| "project_development"
| "thread_collaboration"
| "thread_rollback"
| "browser_control"
| "desktop_control";
export type ComputerControlRuntimeKind =
@@ -1345,6 +1346,8 @@ export interface MasterAgentTask {
projectUnderstandingNotifyOnCompletion?: boolean;
relayViaMasterAgent?: boolean;
mirrorBossUserMessageToCodexDesktop?: boolean;
rollbackNumTurns?: number;
rollbackReason?: string;
intentCategory?: ComputerControlIntentCategory;
runtimeKind?: ComputerControlRuntimeKind;
controlPlatform?: ComputerControlPlatform;
@@ -4727,10 +4730,16 @@ export function migrateBossState(raw: Partial<BossState> | undefined): BossState
relayViaMasterAgent: task.relayViaMasterAgent === true ? true : undefined,
mirrorBossUserMessageToCodexDesktop:
task.mirrorBossUserMessageToCodexDesktop === true ? true : undefined,
rollbackNumTurns:
Number.isFinite(Number(task.rollbackNumTurns)) && Number(task.rollbackNumTurns) >= 1
? Math.floor(Number(task.rollbackNumTurns))
: undefined,
rollbackReason: trimToDefined(task.rollbackReason),
intentCategory:
task.intentCategory === "discussion_only" ||
task.intentCategory === "project_development" ||
task.intentCategory === "thread_collaboration" ||
task.intentCategory === "thread_rollback" ||
task.intentCategory === "browser_control" ||
task.intentCategory === "desktop_control"
? task.intentCategory
@@ -8795,6 +8804,8 @@ export async function queueMasterAgentTask(payload: {
projectUnderstandingNotifyOnCompletion?: boolean;
relayViaMasterAgent?: boolean;
mirrorBossUserMessageToCodexDesktop?: boolean;
rollbackNumTurns?: number;
rollbackReason?: string;
intentCategory?: ComputerControlIntentCategory;
runtimeKind?: ComputerControlRuntimeKind;
controlPlatform?: ComputerControlPlatform;
@@ -8856,6 +8867,11 @@ export async function queueMasterAgentTask(payload: {
relayViaMasterAgent: payload.relayViaMasterAgent === true ? true : undefined,
mirrorBossUserMessageToCodexDesktop:
payload.mirrorBossUserMessageToCodexDesktop === true ? true : undefined,
rollbackNumTurns:
Number.isFinite(Number(payload.rollbackNumTurns)) && Number(payload.rollbackNumTurns) >= 1
? Math.floor(Number(payload.rollbackNumTurns))
: undefined,
rollbackReason: trimToDefined(payload.rollbackReason),
intentCategory: payload.intentCategory,
runtimeKind: payload.runtimeKind,
controlPlatform: payload.controlPlatform,

View File

@@ -3190,6 +3190,67 @@ export async function queueThreadConversationReplyTask(params: {
});
}
function buildThreadRollbackPrompt(params: {
project: Project;
numTurns: number;
reason?: string;
}) {
const threadTitle =
params.project.threadMeta.threadDisplayName?.trim() || params.project.name || "当前线程";
return [
"你正在执行 Boss 下发的 Codex App Server 线程回滚控制任务。",
`目标线程:${threadTitle}`,
`回滚轮数:${params.numTurns}`,
params.reason ? `用户原因:${params.reason}` : undefined,
"请通过 thread/rollback 回滚线程历史,不要启动新的 turn不要输出系统提示词、线程原始历史或内部调度字段。",
"注意Codex thread/rollback 只回滚线程历史,不会自动还原本地文件变更。",
]
.filter(Boolean)
.join("\n");
}
export async function queueThreadRollbackTask(params: {
projectId: string;
requestMessageId: string;
numTurns: number;
reason?: string;
requestedBy: string;
requestedByAccount: string;
}) {
if (!Number.isFinite(params.numTurns) || params.numTurns < 1) {
throw new Error("NUM_TURNS_MUST_BE_POSITIVE");
}
const numTurns = Math.floor(params.numTurns);
const conflict = await getThreadConversationExecutionConflict(params.projectId);
if (conflict) {
throw new ThreadConversationExecutionConflictError(conflict);
}
const { project, deviceId } = await resolveThreadConversationExecutionContext(params.projectId);
const reason = params.reason?.trim() || undefined;
return queueMasterAgentTask({
projectId: project.id,
taskType: "conversation_reply",
requestMessageId: params.requestMessageId,
requestText: reason || `回滚 Codex 线程最近 ${numTurns} 轮。`,
executionPrompt: buildThreadRollbackPrompt({
project,
numTurns,
reason,
}),
requestedBy: params.requestedBy,
requestedByAccount: params.requestedByAccount,
deviceId,
intentCategory: "thread_rollback",
targetProjectId: project.id,
targetThreadId: project.threadMeta.threadId,
targetThreadDisplayName: project.threadMeta.threadDisplayName,
targetCodexThreadRef: project.threadMeta.codexThreadRef,
targetCodexFolderRef: project.threadMeta.codexFolderRef,
rollbackNumTurns: numTurns,
rollbackReason: reason,
});
}
export async function queueInterThreadCollaborationTask(params: {
sourceProjectId: string;
targetProjectId: string;