Route agent requests through Codex app turns
This commit is contained in:
parent
f8d6fd633e
commit
0221f298a5
4 changed files with 877 additions and 0 deletions
|
|
@ -96,6 +96,9 @@ async function main() {
|
|||
case "claim":
|
||||
await claim(cache, options);
|
||||
return;
|
||||
case "release":
|
||||
await releaseRequest(cache, options);
|
||||
return;
|
||||
case "close":
|
||||
await closeRequest(cache, options);
|
||||
return;
|
||||
|
|
@ -217,6 +220,30 @@ async function closeRequest(cache, options) {
|
|||
printJson(closed);
|
||||
}
|
||||
|
||||
async function releaseRequest(cache, options) {
|
||||
const id = requireOption(options, "id");
|
||||
const current = cache.get(updateRequestDefinition, id);
|
||||
if (!current) {
|
||||
throw new Error(`No update request found for id "${id}".`);
|
||||
}
|
||||
|
||||
if (current.status !== "claimed") {
|
||||
throw new Error(`Only claimed requests can be released; "${id}" is ${current.status}.`);
|
||||
}
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const released = parseUpdateRequest({
|
||||
...current,
|
||||
status: "queued",
|
||||
claimedByAgent: undefined,
|
||||
claimedAt: undefined,
|
||||
closeNote: optionalString(options.note),
|
||||
updatedAt: now,
|
||||
});
|
||||
await cache.put(updateRequestDefinition, released.id, released);
|
||||
printJson(released);
|
||||
}
|
||||
|
||||
async function writeSnapshot(cache, options) {
|
||||
const message = cultNetRegistry.createRawSnapshotResponse(
|
||||
cache,
|
||||
|
|
@ -429,6 +456,7 @@ Commands:
|
|||
list List requests, optionally filtered by --repo, --agent, --status
|
||||
claim Claim the highest-priority queued request for --repo
|
||||
close Mark a request completed or cancelled
|
||||
release Return a claimed request to the queue after dispatch setup fails
|
||||
snapshot Write or print a CultNet raw snapshot response
|
||||
apply-snapshot Apply a CultNet raw snapshot response from --in
|
||||
schema Print the document type and schema id
|
||||
|
|
|
|||
|
|
@ -3,12 +3,18 @@ import { spawnSync } from "node:child_process";
|
|||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const PERSONA_WEBHOOK_NAME = "Bifrost Persona Pipe";
|
||||
const PERSONA_WEBHOOK_CACHE_PATH = resolve(".bifrost/discord-webhook-cache.json");
|
||||
const THREAD_CHANNEL_TYPES = new Set([10, 11, 12]);
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const repoRoot = resolve(scriptDir, "..");
|
||||
const projectsRoot = resolve(repoRoot, "..");
|
||||
|
||||
async function main() {
|
||||
loadLocalEnv(resolve(repoRoot, ".env"));
|
||||
loadLocalEnv(resolve(projectsRoot, "VoidBot", ".env"));
|
||||
const [command, ...rawArgs] = process.argv.slice(2);
|
||||
const options = parseArgs(rawArgs);
|
||||
|
||||
|
|
@ -564,6 +570,38 @@ function printJson(value) {
|
|||
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
||||
}
|
||||
|
||||
function loadLocalEnv(path) {
|
||||
const normalizedPath = path.startsWith("/") && /^[A-Za-z]:/.test(path.slice(1))
|
||||
? path.slice(1)
|
||||
: path;
|
||||
if (!existsSync(normalizedPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const line of readFileSync(normalizedPath, "utf8").split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
const separator = trimmed.indexOf("=");
|
||||
if (separator === -1) {
|
||||
continue;
|
||||
}
|
||||
const key = trimmed.slice(0, separator).trim();
|
||||
if (process.env[key]) {
|
||||
continue;
|
||||
}
|
||||
let value = trimmed.slice(separator + 1).trim();
|
||||
if (
|
||||
value.length >= 2 &&
|
||||
((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")))
|
||||
) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
process.stdout.write(`Bifrost bridge
|
||||
|
||||
|
|
|
|||
759
tools/dispatch-agent-requests.mjs
Normal file
759
tools/dispatch-agent-requests.mjs
Normal file
|
|
@ -0,0 +1,759 @@
|
|||
#!/usr/bin/env node
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const bifrostRoot = resolve(scriptDir, "..");
|
||||
const transportCli = resolve(bifrostRoot, "tools", "agent-transport.mjs");
|
||||
const bridgeCli = resolve(bifrostRoot, "tools", "bifrost-bridge.mjs");
|
||||
const defaultStatusDir = resolve(bifrostRoot, ".bifrost", "agent-dispatch");
|
||||
const defaultProjectsRoot = resolve(bifrostRoot, "..");
|
||||
const defaultAquariumChannelId = "1501196543150264332";
|
||||
const defaultPersonaName = "Bifrost";
|
||||
|
||||
async function main() {
|
||||
loadLocalEnv(resolve(bifrostRoot, ".env"));
|
||||
loadLocalEnv(resolve(defaultProjectsRoot, "VoidBot", ".env"));
|
||||
const [command, ...rawArgs] = process.argv.slice(2);
|
||||
const options = parseArgs(rawArgs);
|
||||
|
||||
if (!command || command === "help" || command === "--help" || command === "-h") {
|
||||
printHelp();
|
||||
return;
|
||||
}
|
||||
|
||||
switch (command) {
|
||||
case "dispatch":
|
||||
await dispatchQueuedRequests(options);
|
||||
return;
|
||||
case "run-claimed":
|
||||
await runClaimedRequest(options);
|
||||
return;
|
||||
default:
|
||||
throw new Error(`Unknown command "${command}". Run "node tools/dispatch-agent-requests.mjs help".`);
|
||||
}
|
||||
}
|
||||
|
||||
async function dispatchQueuedRequests(options) {
|
||||
const max = parseInteger(options.max ?? "1", "max");
|
||||
const statusDir = resolveOptionPath(options["status-dir"] ?? defaultStatusDir);
|
||||
await mkdir(statusDir, { recursive: true });
|
||||
|
||||
const dispatched = [];
|
||||
for (let index = 0; index < max; index += 1) {
|
||||
const request = claimNextRequest(options);
|
||||
if (!request) {
|
||||
break;
|
||||
}
|
||||
|
||||
const repoRoot = resolveRepoRoot(request, options);
|
||||
const runDir = resolve(statusDir, request.id);
|
||||
mkdirSync(runDir, { recursive: true });
|
||||
const requestPath = resolve(runDir, "request.json");
|
||||
const promptPath = resolve(runDir, "prompt.md");
|
||||
const logPath = resolve(runDir, "codex.log");
|
||||
writeFileSync(requestPath, `${JSON.stringify(request, null, 2)}\n`, "utf8");
|
||||
writeFileSync(promptPath, buildCodexPrompt(request, repoRoot), "utf8");
|
||||
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
fileURLToPath(import.meta.url),
|
||||
"run-claimed",
|
||||
"--request-file", requestPath,
|
||||
"--repo-root", repoRoot,
|
||||
"--prompt-file", promptPath,
|
||||
"--log", logPath,
|
||||
...optionalArg("--codex-executable", options["codex-executable"]),
|
||||
...optionalArg("--codex-exec-args", options["codex-exec-args"]),
|
||||
...optionalArg("--model", options.model),
|
||||
...optionalArg("--reasoning-effort", options["reasoning-effort"]),
|
||||
...optionalArg("--sandbox", options.sandbox),
|
||||
...optionalArg("--launch-mode", options["launch-mode"]),
|
||||
...optionalArg("--channel-id", options["channel-id"]),
|
||||
...optionalArg("--persona-name", options["persona-name"]),
|
||||
...optionalArg("--persona-avatar-url", options["persona-avatar-url"]),
|
||||
...(options["no-discord"] === "true" ? ["--no-discord", "true"] : []),
|
||||
],
|
||||
{
|
||||
cwd: bifrostRoot,
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
windowsHide: true,
|
||||
},
|
||||
);
|
||||
child.unref();
|
||||
|
||||
const dispatchRecord = {
|
||||
requestId: request.id,
|
||||
title: request.title,
|
||||
repoRoot,
|
||||
promptPath,
|
||||
logPath,
|
||||
pid: child.pid,
|
||||
dispatchedAt: new Date().toISOString(),
|
||||
};
|
||||
writeFileSync(resolve(runDir, "dispatch.json"), `${JSON.stringify(dispatchRecord, null, 2)}\n`, "utf8");
|
||||
|
||||
dispatched.push(dispatchRecord);
|
||||
}
|
||||
|
||||
printJson({
|
||||
ok: true,
|
||||
dispatchedCount: dispatched.length,
|
||||
dispatched,
|
||||
});
|
||||
}
|
||||
|
||||
async function runClaimedRequest(options) {
|
||||
const requestPath = resolveOptionPath(requireOption(options, "request-file"));
|
||||
const repoRoot = resolveOptionPath(requireOption(options, "repo-root"));
|
||||
const promptPath = resolveOptionPath(requireOption(options, "prompt-file"));
|
||||
const logPath = resolveOptionPath(requireOption(options, "log"));
|
||||
const request = JSON.parse(await readFile(requestPath, "utf8"));
|
||||
const launchMode = options["launch-mode"] ?? process.env.BIFROST_CODEX_LAUNCH_MODE ?? "app-server";
|
||||
|
||||
if (launchMode === "codex-exec") {
|
||||
await runClaimedViaCodexExec(request, repoRoot, promptPath, logPath, options);
|
||||
return;
|
||||
}
|
||||
|
||||
if (launchMode !== "app-server") {
|
||||
throw new Error(`Unsupported --launch-mode "${launchMode}". Use app-server or codex-exec.`);
|
||||
}
|
||||
|
||||
await runClaimedViaAppServer(request, repoRoot, promptPath, logPath, options);
|
||||
}
|
||||
|
||||
async function runClaimedViaCodexExec(request, repoRoot, promptPath, logPath, options) {
|
||||
const codexExecutable = options["codex-executable"] ?? process.env.CODEX_EXECUTABLE ?? "codex";
|
||||
const codexExecArgs = splitCommandArgs(options["codex-exec-args"] ?? process.env.CODEX_EXEC_ARGS ?? "");
|
||||
const model = options.model ?? process.env.CODEX_MODEL ?? "gpt-5.4";
|
||||
const reasoningEffort = options["reasoning-effort"] ?? process.env.CODEX_MODEL_REASONING_EFFORT ?? "medium";
|
||||
const sandbox = options.sandbox ?? "workspace-write";
|
||||
const startedAt = new Date().toISOString();
|
||||
|
||||
await mkdir(dirname(logPath), { recursive: true });
|
||||
appendLog(logPath, `started ${startedAt}`);
|
||||
appendLog(logPath, `request ${request.id}: ${request.title}`);
|
||||
appendLog(logPath, `repoRoot ${repoRoot}`);
|
||||
|
||||
const prompt = await readFile(promptPath, "utf8");
|
||||
const result = spawnSync(
|
||||
codexExecutable,
|
||||
[
|
||||
...codexExecArgs,
|
||||
"exec",
|
||||
"-m", model,
|
||||
"-c", 'approval_policy="never"',
|
||||
"-c", `model_reasoning_effort=${JSON.stringify(reasoningEffort)}`,
|
||||
"--skip-git-repo-check",
|
||||
"-s", sandbox,
|
||||
"-",
|
||||
],
|
||||
{
|
||||
cwd: repoRoot,
|
||||
input: prompt,
|
||||
encoding: "utf8",
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
windowsHide: true,
|
||||
},
|
||||
);
|
||||
|
||||
appendLog(logPath, result.stdout ?? "");
|
||||
appendLog(logPath, result.stderr ?? "");
|
||||
const ok = result.status === 0;
|
||||
const closeStatus = ok ? "completed" : "cancelled";
|
||||
const note = ok
|
||||
? `Codex dispatch completed from Bifrost. Log: ${logPath}`
|
||||
: `Codex dispatch failed with exit ${result.status ?? "unknown"}. Log: ${logPath}`;
|
||||
const close = runNodeJson([
|
||||
transportCli,
|
||||
"close",
|
||||
"--id", request.id,
|
||||
"--status", closeStatus,
|
||||
"--note", note,
|
||||
], bifrostRoot);
|
||||
await writeFile(
|
||||
resolve(dirname(logPath), "result.json"),
|
||||
`${JSON.stringify({
|
||||
requestId: request.id,
|
||||
ok,
|
||||
exitCode: result.status,
|
||||
launchMode: "codex-exec",
|
||||
startedAt,
|
||||
finishedAt: new Date().toISOString(),
|
||||
close,
|
||||
}, null, 2)}\n`,
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
async function runClaimedViaAppServer(request, repoRoot, promptPath, logPath, options) {
|
||||
const model = options.model ?? process.env.CODEX_MODEL ?? "gpt-5.4";
|
||||
const reasoningEffort = options["reasoning-effort"] ?? process.env.CODEX_MODEL_REASONING_EFFORT ?? "medium";
|
||||
const sandbox = options.sandbox ?? "danger-full-access";
|
||||
const startedAt = new Date().toISOString();
|
||||
const prompt = await readFile(promptPath, "utf8");
|
||||
const client = new CodexAppServerClient({
|
||||
logPath,
|
||||
command: resolveCodexCommand(options),
|
||||
});
|
||||
|
||||
await mkdir(dirname(logPath), { recursive: true });
|
||||
appendLog(logPath, `started ${startedAt}`);
|
||||
appendLog(logPath, `request ${request.id}: ${request.title}`);
|
||||
appendLog(logPath, `repoRoot ${repoRoot}`);
|
||||
appendLog(logPath, "launchMode app-server");
|
||||
|
||||
let threadId;
|
||||
let turnId;
|
||||
try {
|
||||
await client.start();
|
||||
await client.request("initialize", {
|
||||
clientInfo: {
|
||||
name: "bifrost-dispatcher",
|
||||
title: "Bifrost Dispatcher",
|
||||
version: "0.1.0",
|
||||
},
|
||||
capabilities: {
|
||||
experimentalApi: true,
|
||||
},
|
||||
});
|
||||
|
||||
const threadStart = await client.request("thread/start", {
|
||||
cwd: repoRoot,
|
||||
approvalPolicy: "never",
|
||||
approvalsReviewer: "user",
|
||||
sandbox,
|
||||
model,
|
||||
personality: "pragmatic",
|
||||
ephemeral: false,
|
||||
sessionStartSource: "startup",
|
||||
experimentalRawEvents: false,
|
||||
persistExtendedHistory: true,
|
||||
});
|
||||
threadId = threadStart.thread?.id;
|
||||
if (!threadId) {
|
||||
throw new Error("Codex app-server thread/start returned no thread id.");
|
||||
}
|
||||
|
||||
const turnStart = await client.request("turn/start", {
|
||||
threadId,
|
||||
cwd: repoRoot,
|
||||
approvalPolicy: "never",
|
||||
approvalsReviewer: "user",
|
||||
sandboxPolicy: sandboxPolicyFromMode(sandbox),
|
||||
model,
|
||||
effort: reasoningEffort,
|
||||
personality: "pragmatic",
|
||||
input: [
|
||||
{
|
||||
type: "text",
|
||||
text: prompt,
|
||||
text_elements: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
turnId = turnStart.turn?.id;
|
||||
if (!turnId) {
|
||||
throw new Error("Codex app-server turn/start returned no turn id.");
|
||||
}
|
||||
|
||||
await writeFile(
|
||||
resolve(dirname(logPath), "app-server-dispatch.json"),
|
||||
`${JSON.stringify({
|
||||
requestId: request.id,
|
||||
launchMode: "app-server",
|
||||
threadId,
|
||||
turnId,
|
||||
startedAt,
|
||||
}, null, 2)}\n`,
|
||||
"utf8",
|
||||
);
|
||||
|
||||
if (options["no-discord"] !== "true") {
|
||||
postDispatchReceipt(request, { threadId, turnId, startedAt }, options);
|
||||
}
|
||||
|
||||
const completed = await client.waitForTurnCompleted(threadId, turnId);
|
||||
const ok = completed.turn?.status === "completed";
|
||||
const closeStatus = ok ? "completed" : "cancelled";
|
||||
const note = ok
|
||||
? `Codex app turn completed for ${threadId}/${turnId}.`
|
||||
: `Codex app turn ended as ${completed.turn?.status ?? "unknown"} for ${threadId}/${turnId}.`;
|
||||
const close = runNodeJson([
|
||||
transportCli,
|
||||
"close",
|
||||
"--id", request.id,
|
||||
"--status", closeStatus,
|
||||
"--note", note,
|
||||
], bifrostRoot);
|
||||
await writeDispatchResult(logPath, {
|
||||
requestId: request.id,
|
||||
ok,
|
||||
launchMode: "app-server",
|
||||
threadId,
|
||||
turnId,
|
||||
startedAt,
|
||||
finishedAt: new Date().toISOString(),
|
||||
close,
|
||||
});
|
||||
} catch (error) {
|
||||
appendLog(logPath, error instanceof Error ? error.stack ?? error.message : String(error));
|
||||
if (!threadId || !turnId) {
|
||||
releaseClaimedRequest(request.id, `Visible Codex app turn did not start: ${error instanceof Error ? error.message : String(error)}`);
|
||||
} else {
|
||||
runNodeJson([
|
||||
transportCli,
|
||||
"close",
|
||||
"--id", request.id,
|
||||
"--status", "cancelled",
|
||||
"--note", `Codex app dispatch failed after turn start for ${threadId}/${turnId}.`,
|
||||
], bifrostRoot);
|
||||
}
|
||||
await writeDispatchResult(logPath, {
|
||||
requestId: request.id,
|
||||
ok: false,
|
||||
launchMode: "app-server",
|
||||
threadId,
|
||||
turnId,
|
||||
startedAt,
|
||||
finishedAt: new Date().toISOString(),
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
function claimNextRequest(options) {
|
||||
const repo = optionalString(options.repo);
|
||||
const agent = optionalString(options.agent);
|
||||
if (!repo || repo === "*") {
|
||||
const queued = runNodeJson([
|
||||
transportCli,
|
||||
"list",
|
||||
"--status", "queued",
|
||||
...optionalArg("--store", options.store),
|
||||
], bifrostRoot);
|
||||
const next = queued[0];
|
||||
if (!next) {
|
||||
return null;
|
||||
}
|
||||
return runNodeJson([
|
||||
transportCli,
|
||||
"claim",
|
||||
"--repo", next.targetRepoName,
|
||||
...optionalArg("--agent", agent ?? next.targetAgentIdentity),
|
||||
"--claimed-by", options["claimed-by"] ?? "bifrost-dispatcher",
|
||||
...optionalArg("--store", options.store),
|
||||
], bifrostRoot);
|
||||
}
|
||||
|
||||
const args = [
|
||||
transportCli,
|
||||
"claim",
|
||||
"--repo", repo,
|
||||
...optionalArg("--agent", agent),
|
||||
"--claimed-by", options["claimed-by"] ?? "bifrost-dispatcher",
|
||||
...optionalArg("--store", options.store),
|
||||
];
|
||||
return runNodeJson(args, bifrostRoot);
|
||||
}
|
||||
|
||||
function resolveRepoRoot(request, options) {
|
||||
if (options["repo-root"]) {
|
||||
return resolveOptionPath(options["repo-root"]);
|
||||
}
|
||||
const projectsRoot = resolveOptionPath(options["projects-root"] ?? defaultProjectsRoot);
|
||||
return resolve(projectsRoot, request.targetRepoName);
|
||||
}
|
||||
|
||||
function buildCodexPrompt(request, repoRoot) {
|
||||
return `You are ${request.targetAgentIdentity ?? "Codex"}, receiving a Bifrost-dispatched update request.
|
||||
|
||||
Target repo: ${request.targetRepoName}
|
||||
Target workspace: ${repoRoot}
|
||||
Bifrost request id: ${request.id}
|
||||
Priority: ${request.priority}
|
||||
|
||||
Work the request in this workspace. Prefer a small coherent change, create or update tests/docs when appropriate, and leave the repo in a reviewable state. If this is only an analysis/proposal request, produce the artifact requested. Bifrost will close the transport request after this Codex turn exits.
|
||||
|
||||
Transport policy: do not push to GitHub, open a pull request, or publish external changes unless this specific request explicitly asks for that. A Bifrost-dispatched turn may prepare local changes and report the commit/PR command it would run, but the bridge owns publication policy.
|
||||
|
||||
${request.requestMarkdown}
|
||||
`;
|
||||
}
|
||||
|
||||
function postDispatchReceipt(request, dispatchRecord, options) {
|
||||
const channelId = options["channel-id"] ?? defaultAquariumChannelId;
|
||||
const personaName = options["persona-name"] ?? defaultPersonaName;
|
||||
const topic = summarizeRequestTopic(request);
|
||||
const content = [
|
||||
`Bifrost dispatch: ${request.targetAgentIdentity ?? "repo agent"} has this.`,
|
||||
``,
|
||||
`**${request.targetRepoName}: ${topic}**`,
|
||||
``,
|
||||
`A Codex turn is open in the ${request.targetRepoName} workspace. Keep sharpening the answer in here; the work is no longer waiting for a claimant.`,
|
||||
].join("\n");
|
||||
|
||||
runNodeJson([
|
||||
bridgeCli,
|
||||
"discord-post",
|
||||
"--channel-id", channelId,
|
||||
"--persona-name", personaName,
|
||||
"--content", content,
|
||||
...optionalArg("--persona-avatar-url", options["persona-avatar-url"]),
|
||||
], bifrostRoot);
|
||||
}
|
||||
|
||||
class CodexAppServerClient {
|
||||
constructor({ command, logPath }) {
|
||||
this.command = command;
|
||||
this.logPath = logPath;
|
||||
this.child = undefined;
|
||||
this.nextId = 1;
|
||||
this.buffer = "";
|
||||
this.pending = new Map();
|
||||
this.turnWaiters = [];
|
||||
}
|
||||
|
||||
async start() {
|
||||
this.child = spawn(this.command.exe, [...this.command.args, "app-server", "--listen", "stdio://"], {
|
||||
cwd: bifrostRoot,
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
this.child.stdout.on("data", (chunk) => this.readStdout(chunk.toString()));
|
||||
this.child.stderr.on("data", (chunk) => appendLog(this.logPath, chunk.toString()));
|
||||
this.child.on("exit", (code, signal) => {
|
||||
const error = new Error(`codex app-server exited with ${code ?? signal ?? "unknown"}`);
|
||||
for (const waiter of this.pending.values()) {
|
||||
waiter.reject(error);
|
||||
}
|
||||
this.pending.clear();
|
||||
for (const waiter of this.turnWaiters) {
|
||||
waiter.reject(error);
|
||||
}
|
||||
this.turnWaiters = [];
|
||||
});
|
||||
this.child.on("error", (error) => {
|
||||
for (const waiter of this.pending.values()) {
|
||||
waiter.reject(error);
|
||||
}
|
||||
this.pending.clear();
|
||||
});
|
||||
}
|
||||
|
||||
request(method, params) {
|
||||
if (!this.child?.stdin.writable) {
|
||||
return Promise.reject(new Error("codex app-server stdin is not writable."));
|
||||
}
|
||||
|
||||
const id = this.nextId;
|
||||
this.nextId += 1;
|
||||
appendLog(this.logPath, `client-request ${method}#${id}`);
|
||||
this.child.stdin.write(`${JSON.stringify({ id, method, params })}\n`);
|
||||
return new Promise((resolvePromise, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(id);
|
||||
reject(new Error(`Timed out waiting for ${method}#${id}`));
|
||||
}, 60000);
|
||||
this.pending.set(id, {
|
||||
resolve: (value) => {
|
||||
clearTimeout(timer);
|
||||
resolvePromise(value);
|
||||
},
|
||||
reject: (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
waitForTurnCompleted(threadId, turnId) {
|
||||
return new Promise((resolvePromise, reject) => {
|
||||
this.turnWaiters.push({ threadId, turnId, resolve: resolvePromise, reject });
|
||||
});
|
||||
}
|
||||
|
||||
readStdout(text) {
|
||||
this.buffer += text;
|
||||
while (true) {
|
||||
const newline = this.buffer.indexOf("\n");
|
||||
if (newline === -1) {
|
||||
return;
|
||||
}
|
||||
const line = this.buffer.slice(0, newline).trim();
|
||||
this.buffer = this.buffer.slice(newline + 1);
|
||||
if (line) {
|
||||
this.handleMessage(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleMessage(line) {
|
||||
let message;
|
||||
try {
|
||||
message = JSON.parse(line);
|
||||
} catch {
|
||||
appendLog(this.logPath, `app-server non-json stdout: ${line}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Object.hasOwn(message, "id") && (Object.hasOwn(message, "result") || Object.hasOwn(message, "error"))) {
|
||||
const waiter = this.pending.get(message.id);
|
||||
if (!waiter) {
|
||||
appendLog(this.logPath, `app-server response for unknown id ${message.id}`);
|
||||
return;
|
||||
}
|
||||
this.pending.delete(message.id);
|
||||
if (message.error) {
|
||||
waiter.reject(new Error(JSON.stringify(message.error)));
|
||||
} else {
|
||||
waiter.resolve(message.result);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
appendLog(this.logPath, `app-server ${message.method ?? "message"}`);
|
||||
if (message.method === "turn/completed") {
|
||||
const params = message.params ?? {};
|
||||
const completedTurnId = params.turn?.id;
|
||||
const remaining = [];
|
||||
for (const waiter of this.turnWaiters) {
|
||||
if (waiter.threadId === params.threadId && waiter.turnId === completedTurnId) {
|
||||
waiter.resolve(params);
|
||||
} else {
|
||||
remaining.push(waiter);
|
||||
}
|
||||
}
|
||||
this.turnWaiters = remaining;
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
this.child?.kill();
|
||||
}
|
||||
}
|
||||
|
||||
function resolveCodexCommand(options) {
|
||||
const executable = options["codex-executable"] ?? process.env.CODEX_EXECUTABLE;
|
||||
const extraArgs = splitCommandArgs(options["codex-exec-args"] ?? process.env.CODEX_EXEC_ARGS ?? "");
|
||||
if (executable) {
|
||||
return { exe: executable, args: extraArgs };
|
||||
}
|
||||
|
||||
const codexJs = resolve(process.env.APPDATA ?? "", "npm", "node_modules", "@openai", "codex", "bin", "codex.js");
|
||||
if (existsSync(codexJs)) {
|
||||
return { exe: process.execPath, args: [codexJs] };
|
||||
}
|
||||
|
||||
return { exe: "codex", args: [] };
|
||||
}
|
||||
|
||||
function sandboxPolicyFromMode(mode) {
|
||||
if (mode === "danger-full-access") {
|
||||
return { type: "dangerFullAccess" };
|
||||
}
|
||||
if (mode === "read-only") {
|
||||
return { type: "readOnly", access: { type: "fullAccess" }, networkAccess: true };
|
||||
}
|
||||
return {
|
||||
type: "workspaceWrite",
|
||||
writableRoots: [],
|
||||
readOnlyAccess: { type: "fullAccess" },
|
||||
networkAccess: true,
|
||||
excludeTmpdirEnvVar: false,
|
||||
excludeSlashTmp: false,
|
||||
};
|
||||
}
|
||||
|
||||
function releaseClaimedRequest(id, note) {
|
||||
runNodeJson([
|
||||
transportCli,
|
||||
"release",
|
||||
"--id", id,
|
||||
"--note", note,
|
||||
], bifrostRoot);
|
||||
}
|
||||
|
||||
async function writeDispatchResult(logPath, result) {
|
||||
await writeFile(
|
||||
resolve(dirname(logPath), "result.json"),
|
||||
`${JSON.stringify(result, null, 2)}\n`,
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
function summarizeRequestTopic(request) {
|
||||
const title = String(request.title ?? "").trim();
|
||||
if (title && !/^Recent\b/i.test(title)) {
|
||||
return title;
|
||||
}
|
||||
|
||||
const text = String(request.requestMarkdown ?? "");
|
||||
const section = text.match(/## What Needs To Be Done\s+([\s\S]*?)(?:\n## |\n```|$)/i)?.[1]?.trim();
|
||||
const firstParagraph = section
|
||||
?.split(/\n\s*\n/)
|
||||
.map((entry) => entry.replace(/\s+/g, " ").trim())
|
||||
.find(Boolean);
|
||||
if (firstParagraph) {
|
||||
return summarizeAction(firstParagraph);
|
||||
}
|
||||
|
||||
const heading = text.match(/^#\s+(.+)$/m)?.[1]?.trim();
|
||||
return truncate(heading || "routed update request", 96);
|
||||
}
|
||||
|
||||
function truncate(value, maxLength) {
|
||||
return value.length > maxLength ? `${value.slice(0, maxLength - 3)}...` : value;
|
||||
}
|
||||
|
||||
function summarizeAction(value) {
|
||||
const normalized = value.replace(/\s+/g, " ").trim();
|
||||
const match =
|
||||
normalized.match(/^Implement the ([^:]+?) as /i) ??
|
||||
normalized.match(/^Update ([^:]+?) from /i) ??
|
||||
normalized.match(/^Add (.+?)(?:\.|$)/i);
|
||||
return truncate(stripTrailingRepoName(match?.[1] ?? normalized), 96);
|
||||
}
|
||||
|
||||
function stripTrailingRepoName(value) {
|
||||
return value.replace(/\s+for\s+[A-Z][A-Za-z0-9._-]+$/g, "");
|
||||
}
|
||||
|
||||
function runNodeJson(args, cwd) {
|
||||
const result = spawnSync(process.execPath, args, {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
windowsHide: true,
|
||||
timeout: 30000,
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
const reason = result.error?.message ?? result.stderr ?? result.stdout;
|
||||
throw new Error(`node ${args.join(" ")} failed with ${result.status ?? "unknown"}:\n${reason}`);
|
||||
}
|
||||
return JSON.parse(result.stdout);
|
||||
}
|
||||
|
||||
function appendLog(path, text) {
|
||||
mkdirSync(dirname(path), { recursive: true });
|
||||
writeFileSync(path, `${text ?? ""}\n`, { encoding: "utf8", flag: "a" });
|
||||
}
|
||||
|
||||
function parseArgs(args) {
|
||||
const options = {};
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const token = args[index];
|
||||
if (!token.startsWith("--")) {
|
||||
throw new Error(`Unexpected argument "${token}".`);
|
||||
}
|
||||
const key = token.slice(2);
|
||||
const next = args[index + 1];
|
||||
if (!next || next.startsWith("--")) {
|
||||
options[key] = "true";
|
||||
continue;
|
||||
}
|
||||
options[key] = next;
|
||||
index += 1;
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
function requireOption(options, name) {
|
||||
const value = optionalString(options[name]);
|
||||
if (!value) {
|
||||
throw new Error(`Missing required option --${name}.`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function optionalArg(flag, value) {
|
||||
const normalized = optionalString(value);
|
||||
return normalized ? [flag, normalized] : [];
|
||||
}
|
||||
|
||||
function optionalString(value) {
|
||||
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function parseInteger(value, name) {
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
if (!Number.isInteger(parsed) || parsed < 1) {
|
||||
throw new Error(`--${name} must be a positive integer.`);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function splitCommandArgs(value) {
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
return [];
|
||||
}
|
||||
return value
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function resolveOptionPath(path) {
|
||||
return resolve(process.cwd(), path);
|
||||
}
|
||||
|
||||
function printJson(value) {
|
||||
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
||||
}
|
||||
|
||||
function loadLocalEnv(path) {
|
||||
if (!existsSync(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const line of readFileSync(path, "utf8").split(/\r?\n/)) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
const separator = trimmed.indexOf("=");
|
||||
if (separator === -1) {
|
||||
continue;
|
||||
}
|
||||
const key = trimmed.slice(0, separator).trim();
|
||||
if (process.env[key]) {
|
||||
continue;
|
||||
}
|
||||
let value = trimmed.slice(separator + 1).trim();
|
||||
if (
|
||||
value.length >= 2 &&
|
||||
((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")))
|
||||
) {
|
||||
value = value.slice(1, -1);
|
||||
}
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
function printHelp() {
|
||||
process.stdout.write(`Bifrost agent request dispatcher
|
||||
|
||||
Commands:
|
||||
dispatch Claim queued Bifrost requests and launch detached Codex turns
|
||||
run-claimed Internal worker for one claimed request
|
||||
|
||||
Example:
|
||||
node tools/dispatch-agent-requests.mjs dispatch --repo AquaSynth --agent aqua --max 1
|
||||
`);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
52
tools/install-agent-dispatch-task.ps1
Normal file
52
tools/install-agent-dispatch-task.ps1
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
param(
|
||||
[string] $TaskName = "Bifrost Agent Dispatch",
|
||||
[string] $Repo = "*",
|
||||
[string] $Agent = "",
|
||||
[int] $IntervalMinutes = 1
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$repoRoot = Split-Path -Parent $PSScriptRoot
|
||||
$node = (Get-Command node.exe -ErrorAction Stop).Source
|
||||
$script = Join-Path $repoRoot "tools\dispatch-agent-requests.mjs"
|
||||
|
||||
if (-not (Test-Path $script)) {
|
||||
throw "Missing dispatcher script at $script"
|
||||
}
|
||||
|
||||
$argumentList = @(
|
||||
"`"$script`"",
|
||||
"dispatch",
|
||||
"--repo", "`"$Repo`"",
|
||||
"--max", "1"
|
||||
)
|
||||
if (-not [string]::IsNullOrWhiteSpace($Agent)) {
|
||||
$argumentList += @("--agent", "`"$Agent`"")
|
||||
}
|
||||
|
||||
$action = New-ScheduledTaskAction `
|
||||
-Execute $node `
|
||||
-Argument ($argumentList -join " ") `
|
||||
-WorkingDirectory $repoRoot
|
||||
|
||||
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
|
||||
-RepetitionInterval (New-TimeSpan -Minutes $IntervalMinutes) `
|
||||
-RepetitionDuration (New-TimeSpan -Days 3650)
|
||||
|
||||
$settings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-MultipleInstances IgnoreNew `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 5)
|
||||
|
||||
Register-ScheduledTask `
|
||||
-TaskName $TaskName `
|
||||
-Action $action `
|
||||
-Trigger $trigger `
|
||||
-Settings $settings `
|
||||
-Description "Claims Bifrost agent transport requests, starts target Codex turns, and posts dispatch receipts." `
|
||||
-Force | Out-Null
|
||||
|
||||
Write-Host "Installed scheduled task '$TaskName' every $IntervalMinutes minute(s)."
|
||||
Loading…
Reference in a new issue