fix: stabilize code queue runtime and trace flow
This commit is contained in:
@@ -27,8 +27,15 @@ interface CodexOutputOptions {
|
||||
maxTextChars: number;
|
||||
}
|
||||
|
||||
type CodexResponseFetcher = (path: string) => unknown;
|
||||
type AsyncCodexResponseFetcher = (path: string) => Promise<unknown>;
|
||||
interface CodexJudgeOptions {
|
||||
attempt: number | null;
|
||||
dryRun: boolean;
|
||||
includePrompt: boolean;
|
||||
}
|
||||
|
||||
type CodexRequestInit = { method?: string; body?: unknown };
|
||||
type CodexResponseFetcher = (path: string, init?: CodexRequestInit) => unknown;
|
||||
type AsyncCodexResponseFetcher = (path: string, init?: CodexRequestInit) => Promise<unknown>;
|
||||
|
||||
function requireTaskId(value: string | undefined, command: string): string {
|
||||
if (value === undefined || value.trim().length === 0) throw new Error(`${command} requires task id`);
|
||||
@@ -432,6 +439,21 @@ function parseOutputOptions(args: string[]): CodexOutputOptions {
|
||||
};
|
||||
}
|
||||
|
||||
function parseJudgeOptions(args: string[]): CodexJudgeOptions {
|
||||
const rawAttempt = optionValue(args, ["--attempt", "--attempt-id", "--attemptIndex"]) ?? positionalArgs(args)[0];
|
||||
let attempt: number | null = null;
|
||||
if (rawAttempt !== undefined) {
|
||||
const value = Number(rawAttempt);
|
||||
if (!Number.isInteger(value) || value <= 0) throw new Error("--attempt must be a positive integer");
|
||||
attempt = value;
|
||||
}
|
||||
return {
|
||||
attempt,
|
||||
dryRun: hasFlag(args, "--dry-run") || hasFlag(args, "--no-call"),
|
||||
includePrompt: hasFlag(args, "--include-prompt"),
|
||||
};
|
||||
}
|
||||
|
||||
function queryString(params: Record<string, string | number | boolean | null | undefined>): string {
|
||||
const search = new URLSearchParams();
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
@@ -506,6 +528,16 @@ function codexTaskOutput(taskId: string, options: CodexOutputOptions, fetcher: C
|
||||
return { upstream: response.upstream, outputPage: compactOutputPage(response.body, taskId, options.limit) };
|
||||
}
|
||||
|
||||
function codexTaskJudge(taskId: string, options: CodexJudgeOptions, fetcher: CodexResponseFetcher): unknown {
|
||||
const params = queryString({
|
||||
attempt: options.attempt,
|
||||
dryRun: options.dryRun ? 1 : undefined,
|
||||
includePrompt: options.includePrompt ? 1 : undefined,
|
||||
});
|
||||
const response = unwrapCodexResponse(fetcher(`/api/microservices/code-queue/proxy/api/tasks/${encodeURIComponent(taskId)}/judge${params}`, { method: "POST" }));
|
||||
return { upstream: response.upstream, judgeReplay: response.body };
|
||||
}
|
||||
|
||||
export function codexTaskQuery(taskId: string, optionArgs: string[], fetcher: CodexResponseFetcher = coreInternalFetch): unknown {
|
||||
return codexTaskSummary(taskId, parseTaskOptions(optionArgs), fetcher);
|
||||
}
|
||||
@@ -514,6 +546,10 @@ export function codexOutputQuery(taskId: string, optionArgs: string[], fetcher:
|
||||
return codexTaskOutput(taskId, parseOutputOptions(optionArgs), fetcher);
|
||||
}
|
||||
|
||||
export function codexJudgeQuery(taskId: string, optionArgs: string[], fetcher: CodexResponseFetcher = coreInternalFetch): unknown {
|
||||
return codexTaskJudge(taskId, parseJudgeOptions(optionArgs), fetcher);
|
||||
}
|
||||
|
||||
async function codexTaskSummaryAsync(taskId: string, options: CodexTaskOptions, fetcher: AsyncCodexResponseFetcher): Promise<unknown> {
|
||||
const summaryPath = `/api/microservices/code-queue/proxy/api/tasks/${encodeURIComponent(taskId)}/summary${queryString({ toolLimit: options.toolLimit })}`;
|
||||
const summaryResponse = unwrapCodexResponse(await fetcher(summaryPath));
|
||||
@@ -548,6 +584,16 @@ async function codexTaskOutputAsync(taskId: string, options: CodexOutputOptions,
|
||||
return { upstream: response.upstream, outputPage: compactOutputPage(response.body, taskId, options.limit) };
|
||||
}
|
||||
|
||||
async function codexTaskJudgeAsync(taskId: string, options: CodexJudgeOptions, fetcher: AsyncCodexResponseFetcher): Promise<unknown> {
|
||||
const params = queryString({
|
||||
attempt: options.attempt,
|
||||
dryRun: options.dryRun ? 1 : undefined,
|
||||
includePrompt: options.includePrompt ? 1 : undefined,
|
||||
});
|
||||
const response = unwrapCodexResponse(await fetcher(`/api/microservices/code-queue/proxy/api/tasks/${encodeURIComponent(taskId)}/judge${params}`, { method: "POST" }));
|
||||
return { upstream: response.upstream, judgeReplay: response.body };
|
||||
}
|
||||
|
||||
export async function codexTaskQueryAsync(taskId: string, optionArgs: string[], fetcher: AsyncCodexResponseFetcher): Promise<unknown> {
|
||||
return codexTaskSummaryAsync(taskId, parseTaskOptions(optionArgs), fetcher);
|
||||
}
|
||||
@@ -556,6 +602,10 @@ export async function codexOutputQueryAsync(taskId: string, optionArgs: string[]
|
||||
return codexTaskOutputAsync(taskId, parseOutputOptions(optionArgs), fetcher);
|
||||
}
|
||||
|
||||
export async function codexJudgeQueryAsync(taskId: string, optionArgs: string[], fetcher: AsyncCodexResponseFetcher): Promise<unknown> {
|
||||
return codexTaskJudgeAsync(taskId, parseJudgeOptions(optionArgs), fetcher);
|
||||
}
|
||||
|
||||
function requireQueueId(args: string[], command: string): string {
|
||||
const index = args.indexOf("--queue");
|
||||
const raw = index === -1 ? args[0] : args[index + 1];
|
||||
@@ -625,6 +675,10 @@ export async function runCodeQueueCommand(_config: UniDeskConfig, args: string[]
|
||||
const taskId = requireTaskId(taskIdArg, "codex output");
|
||||
return codexOutputQuery(taskId, args.slice(2));
|
||||
}
|
||||
if (action === "judge") {
|
||||
const taskId = requireTaskId(taskIdArg, "codex judge");
|
||||
return codexJudgeQuery(taskId, args.slice(2));
|
||||
}
|
||||
if (action === "queues") return codeQueues();
|
||||
if (action === "queue") {
|
||||
const sub = taskIdArg ?? "list";
|
||||
@@ -639,5 +693,5 @@ export async function runCodeQueueCommand(_config: UniDeskConfig, args: string[]
|
||||
const taskId = requireTaskId(taskIdArg, "codex move");
|
||||
return codexMoveTask(taskId, requireQueueId(args.slice(2), "codex move"));
|
||||
}
|
||||
throw new Error("codex command must be one of: task, summary, show, output, queues, queue list, queue create, queue merge, move");
|
||||
throw new Error("codex command must be one of: task, summary, show, output, judge, queues, queue list, queue create, queue merge, move");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user