feat: add controlled codex thread archive
This commit is contained in:
36
tests/fixtures/codex-app-server-runtime.mjs
vendored
36
tests/fixtures/codex-app-server-runtime.mjs
vendored
@@ -619,6 +619,42 @@ rl.on("line", (line) => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.method === "thread/archive") {
|
||||
send({
|
||||
id: message.id,
|
||||
result: {},
|
||||
});
|
||||
send({
|
||||
method: "thread/archived",
|
||||
params: {
|
||||
threadId: message.params?.threadId,
|
||||
archivedThreadSecret: "thread-archive-secret-should-not-leak",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.method === "thread/unarchive") {
|
||||
send({
|
||||
id: message.id,
|
||||
result: {
|
||||
thread: {
|
||||
id: message.params?.threadId,
|
||||
name: "private unarchived thread name should not leak",
|
||||
archived: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
send({
|
||||
method: "thread/unarchived",
|
||||
params: {
|
||||
threadId: message.params?.threadId,
|
||||
unarchivedThreadSecret: "thread-unarchive-secret-should-not-leak",
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.method === "thread/read") {
|
||||
send({
|
||||
id: message.id,
|
||||
|
||||
@@ -1573,6 +1573,51 @@ test("codex app-server runner starts thread compaction without starting a normal
|
||||
assert.equal(result.turnId, undefined);
|
||||
});
|
||||
|
||||
test("codex app-server runner archives and unarchives a thread without starting a normal turn", async () => {
|
||||
const runnerConfig = getCodexAppServerRunnerConfig(process.env, {
|
||||
codexAppServerEnabled: true,
|
||||
codexAppServerCommand: process.execPath,
|
||||
codexAppServerArgs: ["tests/fixtures/codex-app-server-runtime.mjs"],
|
||||
codexAppServerWorkdir: repoRoot,
|
||||
codexAppServerTimeoutMs: 5000,
|
||||
});
|
||||
|
||||
const archiveResult = await executeCodexAppServerTask(runnerConfig, {
|
||||
taskId: "task-thread-archive",
|
||||
taskType: "conversation_reply",
|
||||
intentCategory: "thread_archive",
|
||||
targetCodexThreadRef: "019d-app-server-thread",
|
||||
targetCodexFolderRef: repoRoot,
|
||||
threadLifecycleAction: "archive",
|
||||
executionPrompt: "归档当前 Codex 线程。",
|
||||
});
|
||||
const unarchiveResult = await executeCodexAppServerTask(runnerConfig, {
|
||||
taskId: "task-thread-unarchive",
|
||||
taskType: "conversation_reply",
|
||||
intentCategory: "thread_unarchive",
|
||||
targetCodexThreadRef: "019d-app-server-thread",
|
||||
targetCodexFolderRef: repoRoot,
|
||||
threadLifecycleAction: "unarchive",
|
||||
executionPrompt: "恢复当前 Codex 线程。",
|
||||
});
|
||||
|
||||
assert.equal(archiveResult.status, "completed");
|
||||
assert.equal(archiveResult.threadId, "019d-app-server-thread");
|
||||
assert.equal(archiveResult.turnControl, "archive");
|
||||
assert.match(archiveResult.replyBody, /已归档 Codex 线程/);
|
||||
assert.equal(archiveResult.turnId, undefined);
|
||||
assert.equal(unarchiveResult.status, "completed");
|
||||
assert.equal(unarchiveResult.threadId, "019d-app-server-thread");
|
||||
assert.equal(unarchiveResult.turnControl, "unarchive");
|
||||
assert.match(unarchiveResult.replyBody, /已恢复 Codex 线程/);
|
||||
assert.equal(unarchiveResult.turnId, undefined);
|
||||
|
||||
const serialized = JSON.stringify({ archiveResult, unarchiveResult });
|
||||
assert.doesNotMatch(serialized, /thread-archive-secret-should-not-leak/);
|
||||
assert.doesNotMatch(serialized, /thread-unarchive-secret-should-not-leak/);
|
||||
assert.doesNotMatch(serialized, /private unarchived thread name should not leak/);
|
||||
});
|
||||
|
||||
test("codex app-server runner stays disabled unless feature flag is explicit", () => {
|
||||
const runnerConfig = getCodexAppServerRunnerConfig(process.env, {
|
||||
codexAppServerCommand: process.execPath,
|
||||
|
||||
177
tests/thread-archive-route.test.ts
Normal file
177
tests/thread-archive-route.test.ts
Normal file
@@ -0,0 +1,177 @@
|
||||
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-archive/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-archive-"));
|
||||
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-archive/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-archive`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
function buildThreadProject() {
|
||||
return {
|
||||
id: "archive-project",
|
||||
name: "可归档线程",
|
||||
pinned: false,
|
||||
systemPinned: false,
|
||||
deviceIds: ["mac-studio"],
|
||||
preview: "",
|
||||
updatedAt: "2026-06-03T11:00:00+08:00",
|
||||
lastMessageAt: "2026-06-03T11:00:00+08:00",
|
||||
isGroup: false,
|
||||
threadMeta: {
|
||||
projectId: "archive-project",
|
||||
threadId: "archive-thread",
|
||||
threadDisplayName: "可归档线程",
|
||||
folderName: "boss",
|
||||
codexFolderRef: "/Users/kris/code/boss",
|
||||
codexThreadRef: "codex-archive-thread",
|
||||
updatedAt: "2026-06-03T11: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-archive queues a controlled Codex thread archive 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, {
|
||||
action: "archive",
|
||||
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_archive");
|
||||
assert.equal(payload.task.threadLifecycleAction, "archive");
|
||||
assert.equal(payload.task.threadLifecycleReason, "项目已阶段性结束,归档线程。");
|
||||
assert.equal(payload.task.targetProjectId, project.id);
|
||||
assert.equal(payload.task.targetCodexThreadRef, "codex-archive-thread");
|
||||
|
||||
const persisted = (await readState()).masterAgentTasks.find(
|
||||
(task) => task.taskId === payload.task.taskId,
|
||||
);
|
||||
assert.equal(persisted?.status, "queued");
|
||||
assert.equal(persisted?.intentCategory, "thread_archive");
|
||||
assert.equal(persisted?.threadLifecycleAction, "archive");
|
||||
});
|
||||
|
||||
test("POST /thread-archive queues a controlled Codex thread unarchive 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, {
|
||||
action: "unarchive",
|
||||
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_unarchive");
|
||||
assert.equal(payload.task.threadLifecycleAction, "unarchive");
|
||||
assert.equal(payload.task.threadLifecycleReason, "恢复线程继续开发。");
|
||||
|
||||
const persisted = (await readState()).masterAgentTasks.find(
|
||||
(task) => task.taskId === payload.task.taskId,
|
||||
);
|
||||
assert.equal(persisted?.status, "queued");
|
||||
assert.equal(persisted?.intentCategory, "thread_unarchive");
|
||||
assert.equal(persisted?.threadLifecycleAction, "unarchive");
|
||||
});
|
||||
|
||||
test("POST /thread-archive rejects unsupported lifecycle actions", 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, { action: "delete" }),
|
||||
{ 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, "THREAD_LIFECYCLE_ACTION_UNSUPPORTED");
|
||||
});
|
||||
Reference in New Issue
Block a user