feat: add controlled codex thread rollback
This commit is contained in:
23
tests/fixtures/codex-app-server-runtime.mjs
vendored
23
tests/fixtures/codex-app-server-runtime.mjs
vendored
@@ -569,6 +569,29 @@ rl.on("line", (line) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.method === "thread/rollback") {
|
||||
send({
|
||||
id: message.id,
|
||||
result: {
|
||||
thread: {
|
||||
id: message.params?.threadId,
|
||||
name: "rollback fixture thread",
|
||||
turns: [
|
||||
{
|
||||
id: "rollback-private-turn",
|
||||
status: { type: "completed" },
|
||||
userInput: "private rollback turn text should not leak",
|
||||
},
|
||||
],
|
||||
rollbackApplied: {
|
||||
numTurns: message.params?.numTurns,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.method === "thread/read") {
|
||||
send({
|
||||
id: message.id,
|
||||
|
||||
@@ -1516,6 +1516,35 @@ test("codex app-server runner retries transient overloaded JSON-RPC requests", a
|
||||
}
|
||||
});
|
||||
|
||||
test("codex app-server runner rolls back a thread without starting a new turn or leaking history", async () => {
|
||||
const runnerConfig = getCodexAppServerRunnerConfig(process.env, {
|
||||
codexAppServerEnabled: true,
|
||||
codexAppServerCommand: process.execPath,
|
||||
codexAppServerArgs: ["tests/fixtures/codex-app-server-runtime.mjs"],
|
||||
codexAppServerWorkdir: repoRoot,
|
||||
codexAppServerTimeoutMs: 5000,
|
||||
});
|
||||
|
||||
const result = await executeCodexAppServerTask(runnerConfig, {
|
||||
taskId: "task-thread-rollback",
|
||||
taskType: "conversation_reply",
|
||||
intentCategory: "thread_rollback",
|
||||
targetCodexThreadRef: "019d-app-server-thread",
|
||||
targetCodexFolderRef: repoRoot,
|
||||
rollbackNumTurns: 2,
|
||||
executionPrompt: "回滚最近 2 轮 Codex 线程历史。",
|
||||
});
|
||||
|
||||
assert.equal(result.status, "completed");
|
||||
assert.equal(result.threadId, "019d-app-server-thread");
|
||||
assert.equal(result.turnControl, "rollback");
|
||||
assert.equal(result.rollback?.numTurns, 2);
|
||||
assert.match(result.replyBody, /已回滚 Codex 线程最近 2 轮/);
|
||||
assert.match(result.replyBody, /不会自动还原本地文件变更/);
|
||||
assert.doesNotMatch(result.replyBody, /private rollback turn text/);
|
||||
assert.equal(result.turnId, undefined);
|
||||
});
|
||||
|
||||
test("codex app-server runner stays disabled unless feature flag is explicit", () => {
|
||||
const runnerConfig = getCodexAppServerRunnerConfig(process.env, {
|
||||
codexAppServerCommand: process.execPath,
|
||||
|
||||
146
tests/thread-rollback-route.test.ts
Normal file
146
tests/thread-rollback-route.test.ts
Normal file
@@ -0,0 +1,146 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
let runtimeRoot = "";
|
||||
let postRoute: (typeof import("../src/app/api/v1/projects/[projectId]/thread-rollback/route"))["POST"];
|
||||
let createAuthSession: (typeof import("../src/lib/boss-data"))["createAuthSession"];
|
||||
let readState: (typeof import("../src/lib/boss-data"))["readState"];
|
||||
let writeState: (typeof import("../src/lib/boss-data"))["writeState"];
|
||||
let AUTH_SESSION_COOKIE = "";
|
||||
|
||||
async function setup() {
|
||||
if (runtimeRoot) return;
|
||||
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-thread-rollback-"));
|
||||
process.env.BOSS_RUNTIME_ROOT = runtimeRoot;
|
||||
process.env.BOSS_STATE_FILE = path.join(runtimeRoot, "boss-state.json");
|
||||
|
||||
const [route, data, auth] = await Promise.all([
|
||||
import("../src/app/api/v1/projects/[projectId]/thread-rollback/route.ts"),
|
||||
import("../src/lib/boss-data.ts"),
|
||||
import("../src/lib/boss-auth.ts"),
|
||||
]);
|
||||
postRoute = route.POST;
|
||||
createAuthSession = data.createAuthSession;
|
||||
readState = data.readState;
|
||||
writeState = data.writeState;
|
||||
AUTH_SESSION_COOKIE = auth.AUTH_SESSION_COOKIE;
|
||||
}
|
||||
|
||||
test.after(async () => {
|
||||
if (runtimeRoot) {
|
||||
await rm(runtimeRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test.beforeEach(async () => {
|
||||
await setup();
|
||||
await rm(runtimeRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function createAuthedRequest(projectId: string, body: unknown) {
|
||||
const session = await createAuthSession({
|
||||
account: "krisolo",
|
||||
role: "highest_admin",
|
||||
displayName: "Boss 超级管理员",
|
||||
loginMethod: "password",
|
||||
});
|
||||
return new NextRequest(`http://127.0.0.1:3000/api/v1/projects/${projectId}/thread-rollback`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
function buildThreadProject() {
|
||||
return {
|
||||
id: "rollback-project",
|
||||
name: "可回滚线程",
|
||||
pinned: false,
|
||||
systemPinned: false,
|
||||
deviceIds: ["mac-studio"],
|
||||
preview: "",
|
||||
updatedAt: "2026-06-03T10:00:00+08:00",
|
||||
lastMessageAt: "2026-06-03T10:00:00+08:00",
|
||||
isGroup: false,
|
||||
threadMeta: {
|
||||
projectId: "rollback-project",
|
||||
threadId: "rollback-thread",
|
||||
threadDisplayName: "可回滚线程",
|
||||
folderName: "boss",
|
||||
codexFolderRef: "/Users/kris/code/boss",
|
||||
codexThreadRef: "codex-rollback-thread",
|
||||
updatedAt: "2026-06-03T10:00:00+08:00",
|
||||
},
|
||||
groupMembers: [],
|
||||
createdByAgent: false,
|
||||
collaborationMode: "development" as const,
|
||||
approvalState: "not_required" as const,
|
||||
unreadCount: 0,
|
||||
riskLevel: "low" as const,
|
||||
messages: [],
|
||||
goals: [],
|
||||
versions: [],
|
||||
};
|
||||
}
|
||||
|
||||
test("POST /thread-rollback queues a controlled Codex thread rollback task", async () => {
|
||||
const state = await readState();
|
||||
const project = buildThreadProject();
|
||||
state.projects = [
|
||||
...state.projects.filter((item) => item.id === "master-agent"),
|
||||
project,
|
||||
];
|
||||
await writeState(state);
|
||||
|
||||
const response = await postRoute(
|
||||
await createAuthedRequest(project.id, {
|
||||
numTurns: 2,
|
||||
reason: "撤回刚才误触发的两轮对话。",
|
||||
}),
|
||||
{ params: Promise.resolve({ projectId: project.id }) },
|
||||
);
|
||||
const payload = await response.json();
|
||||
|
||||
assert.equal(response.status, 200);
|
||||
assert.equal(payload.ok, true);
|
||||
assert.equal(payload.task.intentCategory, "thread_rollback");
|
||||
assert.equal(payload.task.rollbackNumTurns, 2);
|
||||
assert.equal(payload.task.rollbackReason, "撤回刚才误触发的两轮对话。");
|
||||
assert.equal(payload.task.targetProjectId, project.id);
|
||||
assert.equal(payload.task.targetCodexThreadRef, "codex-rollback-thread");
|
||||
assert.equal(payload.task.targetCodexFolderRef, "/Users/kris/code/boss");
|
||||
|
||||
const persisted = (await readState()).masterAgentTasks.find(
|
||||
(task) => task.taskId === payload.task.taskId,
|
||||
);
|
||||
assert.equal(persisted?.status, "queued");
|
||||
assert.equal(persisted?.intentCategory, "thread_rollback");
|
||||
assert.equal(persisted?.rollbackNumTurns, 2);
|
||||
});
|
||||
|
||||
test("POST /thread-rollback rejects zero-turn rollback", async () => {
|
||||
const state = await readState();
|
||||
const project = buildThreadProject();
|
||||
state.projects = [
|
||||
...state.projects.filter((item) => item.id === "master-agent"),
|
||||
project,
|
||||
];
|
||||
await writeState(state);
|
||||
|
||||
const response = await postRoute(
|
||||
await createAuthedRequest(project.id, { numTurns: 0 }),
|
||||
{ params: Promise.resolve({ projectId: project.id }) },
|
||||
);
|
||||
const payload = await response.json();
|
||||
|
||||
assert.equal(response.status, 400);
|
||||
assert.equal(payload.ok, false);
|
||||
assert.equal(payload.message, "NUM_TURNS_MUST_BE_POSITIVE");
|
||||
});
|
||||
Reference in New Issue
Block a user