fix: fail closed on invalid codex resume bindings
This commit is contained in:
@@ -1,7 +1,83 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { mkdtemp, mkdir, rm } from "node:fs/promises";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
|
||||
import { buildCodexTaskExecution } from "../local-agent/codex-task-runner.mjs";
|
||||
import {
|
||||
buildCodexTaskExecution,
|
||||
prepareCodexTaskExecution,
|
||||
} from "../local-agent/codex-task-runner.mjs";
|
||||
|
||||
let runtimeRoot = "";
|
||||
|
||||
async function ensureRuntimeRoot() {
|
||||
if (!runtimeRoot) {
|
||||
runtimeRoot = await mkdtemp(path.join(os.tmpdir(), "boss-local-agent-task-runner-"));
|
||||
}
|
||||
return runtimeRoot;
|
||||
}
|
||||
|
||||
test.after(async () => {
|
||||
if (runtimeRoot) {
|
||||
await rm(runtimeRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
async function createCodexStateDb(threads) {
|
||||
const root = await ensureRuntimeRoot();
|
||||
const dbPath = path.join(root, `state-${Math.random().toString(16).slice(2)}.sqlite`);
|
||||
const db = new DatabaseSync(dbPath);
|
||||
db.exec(`
|
||||
CREATE TABLE threads (
|
||||
id TEXT PRIMARY KEY,
|
||||
rollout_path TEXT NOT NULL,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
source TEXT NOT NULL,
|
||||
model_provider TEXT NOT NULL,
|
||||
cwd TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
sandbox_policy TEXT NOT NULL,
|
||||
approval_mode TEXT NOT NULL,
|
||||
tokens_used INTEGER NOT NULL DEFAULT 0,
|
||||
has_user_event INTEGER NOT NULL DEFAULT 0,
|
||||
archived INTEGER NOT NULL DEFAULT 0,
|
||||
archived_at INTEGER,
|
||||
git_sha TEXT,
|
||||
git_branch TEXT,
|
||||
git_origin_url TEXT,
|
||||
cli_version TEXT NOT NULL DEFAULT '',
|
||||
first_user_message TEXT NOT NULL DEFAULT '',
|
||||
agent_nickname TEXT,
|
||||
agent_role TEXT,
|
||||
memory_mode TEXT NOT NULL DEFAULT 'enabled',
|
||||
model TEXT,
|
||||
reasoning_effort TEXT,
|
||||
agent_path TEXT
|
||||
);
|
||||
`);
|
||||
const insertThread = db.prepare(`
|
||||
INSERT INTO threads (
|
||||
id, rollout_path, created_at, updated_at, source, model_provider, cwd, title,
|
||||
sandbox_policy, approval_mode, tokens_used, has_user_event, archived,
|
||||
cli_version, first_user_message, agent_nickname, agent_role, memory_mode, model, reasoning_effort
|
||||
) VALUES (?, ?, ?, ?, 'desktop', 'openai', ?, ?, 'workspace-write', 'never', 0, 1, 0, '0.118.0', '', '', '', 'enabled', 'gpt-5.4', 'medium')
|
||||
`);
|
||||
for (const thread of threads) {
|
||||
insertThread.run(
|
||||
thread.id,
|
||||
thread.rolloutPath ?? path.join(root, `${thread.id}.jsonl`),
|
||||
1774845600,
|
||||
1774845618,
|
||||
thread.cwd,
|
||||
thread.title ?? thread.id,
|
||||
);
|
||||
}
|
||||
db.close();
|
||||
return dbPath;
|
||||
}
|
||||
|
||||
test("conversation reply resumes the real Codex thread when thread ref is available", () => {
|
||||
const execution = buildCodexTaskExecution(
|
||||
@@ -91,3 +167,111 @@ test("master agent reply without target thread stays on ephemeral exec", () => {
|
||||
"你是主 Agent",
|
||||
]);
|
||||
});
|
||||
|
||||
test("conversation reply preflight fails closed when target cwd is missing", async () => {
|
||||
const missingFolder = "/tmp/boss-local-agent-missing-workdir";
|
||||
const result = await prepareCodexTaskExecution(
|
||||
{
|
||||
masterAgentWorkdir: "/Users/kris/code/boss",
|
||||
masterAgentSandbox: "workspace-write",
|
||||
},
|
||||
{
|
||||
taskType: "conversation_reply",
|
||||
executionPrompt: "请回复用户",
|
||||
targetCodexThreadRef: "019d-thread-real",
|
||||
targetCodexFolderRef: missingFolder,
|
||||
},
|
||||
"/tmp/reply.txt",
|
||||
);
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.code, "LOCAL_AGENT_CODEX_WORKDIR_INVALID");
|
||||
assert.match(result.error.message, /LOCAL_AGENT_CODEX_WORKDIR_INVALID/);
|
||||
assert.match(result.error.message, /missing-workdir/);
|
||||
});
|
||||
|
||||
test("dispatch execution preflight fails closed when target thread ref is missing", async () => {
|
||||
const result = await prepareCodexTaskExecution(
|
||||
{
|
||||
masterAgentWorkdir: "/Users/kris/code/boss",
|
||||
masterAgentSandbox: "workspace-write",
|
||||
},
|
||||
{
|
||||
taskType: "dispatch_execution",
|
||||
executionPrompt: "请执行群聊任务",
|
||||
targetCodexFolderRef: "/Users/kris/code/boss",
|
||||
},
|
||||
"/tmp/reply.txt",
|
||||
);
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.code, "LOCAL_AGENT_CODEX_THREAD_BINDING_MISSING");
|
||||
assert.match(result.error.message, /LOCAL_AGENT_CODEX_THREAD_BINDING_MISSING/);
|
||||
});
|
||||
|
||||
test("conversation reply preflight fails closed when target thread ref is stale in local Codex state", async () => {
|
||||
const root = await ensureRuntimeRoot();
|
||||
const validCwd = path.join(root, "project-stale");
|
||||
await mkdir(validCwd, { recursive: true });
|
||||
const stateDbPath = await createCodexStateDb([
|
||||
{
|
||||
id: "019d-thread-other",
|
||||
cwd: validCwd,
|
||||
title: "Other thread",
|
||||
},
|
||||
]);
|
||||
|
||||
const result = await prepareCodexTaskExecution(
|
||||
{
|
||||
masterAgentWorkdir: "/Users/kris/code/boss",
|
||||
masterAgentSandbox: "workspace-write",
|
||||
codexStateDbPath: stateDbPath,
|
||||
},
|
||||
{
|
||||
taskType: "conversation_reply",
|
||||
executionPrompt: "请回复用户",
|
||||
targetCodexThreadRef: "019d-thread-stale",
|
||||
targetCodexFolderRef: validCwd,
|
||||
},
|
||||
"/tmp/reply.txt",
|
||||
);
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.code, "LOCAL_AGENT_CODEX_THREAD_BINDING_STALE");
|
||||
assert.match(result.error.message, /LOCAL_AGENT_CODEX_THREAD_BINDING_STALE/);
|
||||
});
|
||||
|
||||
test("conversation reply preflight fails closed when target cwd mismatches the live Codex thread binding", async () => {
|
||||
const root = await ensureRuntimeRoot();
|
||||
const liveCwd = path.join(root, "project-live");
|
||||
const staleCwd = path.join(root, "project-stale-mismatch");
|
||||
await mkdir(liveCwd, { recursive: true });
|
||||
await mkdir(staleCwd, { recursive: true });
|
||||
const stateDbPath = await createCodexStateDb([
|
||||
{
|
||||
id: "019d-thread-live",
|
||||
cwd: liveCwd,
|
||||
title: "Live thread",
|
||||
},
|
||||
]);
|
||||
|
||||
const result = await prepareCodexTaskExecution(
|
||||
{
|
||||
masterAgentWorkdir: "/Users/kris/code/boss",
|
||||
masterAgentSandbox: "workspace-write",
|
||||
codexStateDbPath: stateDbPath,
|
||||
},
|
||||
{
|
||||
taskType: "conversation_reply",
|
||||
executionPrompt: "请回复用户",
|
||||
targetCodexThreadRef: "019d-thread-live",
|
||||
targetCodexFolderRef: staleCwd,
|
||||
},
|
||||
"/tmp/reply.txt",
|
||||
);
|
||||
|
||||
assert.equal(result.ok, false);
|
||||
assert.equal(result.error.code, "LOCAL_AGENT_CODEX_THREAD_BINDING_MISMATCH");
|
||||
assert.match(result.error.message, /LOCAL_AGENT_CODEX_THREAD_BINDING_MISMATCH/);
|
||||
assert.match(result.error.message, /project-live/);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user