feat: fork codex threads

This commit is contained in:
AI Bot
2026-06-03 14:49:43 +08:00
parent 5537fde7a6
commit 0c3437a36f
11 changed files with 393 additions and 1 deletions

View File

@@ -705,6 +705,32 @@ rl.on("line", (line) => {
return;
}
if (message.method === "thread/fork") {
send({
id: message.id,
result: {
thread: {
id: `${message.params?.threadId}-fork`,
sessionId: "fork-session",
forkedFromId: message.params?.threadId,
name: "Forked working thread",
preview: "Fork preview should be safe",
ephemeral: message.params?.ephemeral === true,
status: "idle",
path: "/private/path/that-should-not-leak",
cwd: "/private/cwd/that-should-not-leak",
turns: [],
internalForkSecret: "thread-fork-secret-should-not-leak",
},
model: "gpt-5.4",
modelProvider: "openai",
cwd: "/private/cwd/that-should-not-leak",
instructionSources: ["/private/AGENTS.md"],
},
});
return;
}
if (message.method === "thread/read") {
send({
id: message.id,

View File

@@ -1714,6 +1714,36 @@ test("codex app-server runner syncs thread git metadata without starting a norma
assert.doesNotMatch(JSON.stringify(result), /thread-metadata-secret-should-not-leak/);
});
test("codex app-server runner forks 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 result = await executeCodexAppServerTask(runnerConfig, {
taskId: "task-thread-fork",
taskType: "conversation_reply",
intentCategory: "thread_fork",
targetCodexThreadRef: "019d-app-server-thread",
targetCodexFolderRef: repoRoot,
threadForkEphemeral: false,
executionPrompt: "分叉当前 Codex 线程。",
});
assert.equal(result.status, "completed");
assert.equal(result.threadId, "019d-app-server-thread");
assert.equal(result.turnControl, "fork");
assert.equal(result.threadFork?.sourceThreadId, "019d-app-server-thread");
assert.equal(result.threadFork?.forkedThreadId, "019d-app-server-thread-fork");
assert.equal(result.threadFork?.forkedThreadName, "Forked working thread");
assert.equal(result.threadFork?.ephemeral, false);
assert.equal(result.turnId, undefined);
assert.doesNotMatch(JSON.stringify(result), /thread-fork-secret-should-not-leak/);
});
test("codex app-server runner stays disabled unless feature flag is explicit", () => {
const runnerConfig = getCodexAppServerRunnerConfig(process.env, {
codexAppServerCommand: process.execPath,

View File

@@ -0,0 +1,126 @@
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-fork/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-fork-"));
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-fork/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-fork`, {
method: "POST",
headers: {
"content-type": "application/json",
cookie: `${AUTH_SESSION_COOKIE}=${session.sessionToken}`,
},
body: JSON.stringify(body),
});
}
function buildThreadProject() {
return {
id: "fork-project",
name: "可分叉线程",
pinned: false,
systemPinned: false,
deviceIds: ["mac-studio"],
preview: "",
updatedAt: "2026-06-03T14:30:00+08:00",
lastMessageAt: "2026-06-03T14:30:00+08:00",
isGroup: false,
threadMeta: {
projectId: "fork-project",
threadId: "fork-thread",
threadDisplayName: "可分叉线程",
folderName: "boss",
codexFolderRef: "/Users/kris/code/boss",
codexThreadRef: "codex-fork-source-thread",
updatedAt: "2026-06-03T14:30: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-fork queues a controlled Codex thread fork 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, {
reason: "从当前状态分叉一条验证线程。",
ephemeral: false,
}),
{ 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_fork");
assert.equal(payload.task.threadForkReason, "从当前状态分叉一条验证线程。");
assert.equal(payload.task.threadForkEphemeral, false);
assert.equal(payload.task.targetProjectId, project.id);
assert.equal(payload.task.targetCodexThreadRef, "codex-fork-source-thread");
const persisted = (await readState()).masterAgentTasks.find(
(task) => task.taskId === payload.task.taskId,
);
assert.equal(persisted?.status, "queued");
assert.equal(persisted?.intentCategory, "thread_fork");
assert.equal(persisted?.threadForkReason, "从当前状态分叉一条验证线程。");
assert.equal(persisted?.threadForkEphemeral, false);
});