fix: reduce commander task polling noise (#157)

Co-authored-by: Codex <codex@noreply.local>
This commit is contained in:
Lyon
2026-05-24 02:48:16 +08:00
committed by GitHub
parent 68e38c8674
commit 7b6f2e55ff
4 changed files with 206 additions and 51 deletions
@@ -1,6 +1,7 @@
import { codexTasksQueryForTest } from "./src/code-queue";
type JsonRecord = Record<string, unknown>;
type RequestRecord = { path: string; method: string };
function assertCondition(condition: unknown, message: string, detail: JsonRecord = {}): void {
if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
@@ -73,7 +74,8 @@ function summaryForTask(taskId: string): JsonRecord {
};
}
function noisyCommanderFixture(path: string): JsonRecord {
function noisyCommanderFixture(path: string, requests: RequestRecord[] = []): JsonRecord {
requests.push({ path, method: "GET" });
if (path.includes("/summary")) {
const taskId = decodeURIComponent(path.split("/api/tasks/")[1]?.split("/")[0] ?? "unknown");
return summaryForTask(taskId);
@@ -140,12 +142,24 @@ function noisyCommanderFixture(path: string): JsonRecord {
}
export function runCodeQueueCommanderViewContract(): JsonRecord {
const commander = codexTasksQueryForTest(["--view", "commander", "--limit", "260"], noisyCommanderFixture);
const supervisor = codexTasksQueryForTest(["--view", "supervisor", "--limit", "260"], noisyCommanderFixture);
const full = codexTasksQueryForTest(["--view", "full", "--limit", "260"], noisyCommanderFixture);
const commanderRequests: RequestRecord[] = [];
const commanderLimit8Requests: RequestRecord[] = [];
const fetchCommander = (path: string): JsonRecord => noisyCommanderFixture(path, commanderRequests);
const fetchCommanderLimit8 = (path: string): JsonRecord => noisyCommanderFixture(path, commanderLimit8Requests);
const fetchNoisy = (path: string): JsonRecord => noisyCommanderFixture(path);
const commander = codexTasksQueryForTest(["--view", "commander", "--limit", "260"], fetchCommander);
const supervisor = codexTasksQueryForTest(["--view", "supervisor", "--limit", "260"], fetchNoisy);
const full = codexTasksQueryForTest(["--view", "full", "--limit", "260"], fetchNoisy);
const commanderLimit8 = codexTasksQueryForTest(["--view", "commander", "--limit", "8"], fetchCommanderLimit8);
const fullLimit8 = codexTasksQueryForTest(["--view", "full", "--limit", "8"], fetchNoisy);
const unreadLimit8 = codexTasksQueryForTest(["--unread", "--limit", "8"], fetchNoisy);
const commanderBody = JSON.stringify(commander);
const commanderLimit8Body = JSON.stringify(commanderLimit8);
const fullLimit8Body = JSON.stringify(fullLimit8);
const unreadLimit8Body = JSON.stringify(unreadLimit8);
const fullBody = JSON.stringify(full);
const commanderView = asRecord(asRecord(commander).commander);
const commanderLimit8View = asRecord(asRecord(commanderLimit8).commander);
const supervisorView = asRecord(asRecord(supervisor).supervisor);
const filters = asRecord(commanderView.filters);
const activeRunners = asRecord(commanderView.activeRunners);
@@ -164,8 +178,14 @@ export function runCodeQueueCommanderViewContract(): JsonRecord {
const recentCompletedSection = asRecord(sections.recentCompleted);
const recentIds = asArray(recentCompletedSection.items).map((item) => String(asRecord(item).id ?? ""));
const terminalIds = asArray(terminalUnreadSection.items).map((item) => String(asRecord(item).id ?? ""));
const activeItems = asArray(activeRunners.items).map(asRecord);
const runningRisk = attentionItems.find((item) => item.id === "task-running-risk") ?? {};
const failedUnread = attentionItems.find((item) => item.id === "task-failed-unread") ?? {};
const limit8ActiveRunners = asRecord(commanderLimit8View.activeRunners);
const limit8Sections = asRecord(commanderLimit8View.sections);
const limit8TerminalUnread = asRecord(limit8Sections.terminalUnread);
const limit8Commands = asRecord(commanderLimit8View.commands);
const limit8Attention = asRecord(commanderLimit8View.attention);
const limit8AttentionItems = asArray(limit8Attention.items).map(asRecord);
assertCondition(commanderBody.length < 30_000, "commander output should stay under the noisy fixture budget", { chars: commanderBody.length });
assertCondition(commanderBody.length < fullBody.length * 0.65, "commander output should stay materially smaller than full output", { commanderChars: commanderBody.length, fullChars: fullBody.length });
@@ -173,7 +193,8 @@ export function runCodeQueueCommanderViewContract(): JsonRecord {
assertCondition(activeRunners.count === 14 && activeRunners.exact === true && activeRunners.source === "database-active", "commander view should expose exact active runner count and source/disposition", activeRunners);
assertCondition(backlog.queued === 18 && backlog.retryWait === 4 && backlog.total === 22 && backlog.exact === true, "commander view should expose queued/retry_wait exact counts", backlog);
assertCondition(terminalUnread.total === 8 && terminalUnread.rowsReturned === 3 && terminalUnread.rowsOmitted === 5 && terminalUnread.exact === true, "commander view should expose terminal unread count plus omitted rows", terminalUnread);
assertCondition(attentionCounts.total === 7 && attentionCounts.returned === 7 && attentionCounts.omitted === 0, "commander attention counts should preserve total/returned/omitted", attentionCounts);
assertCondition(activeItems.some((item) => item.id === "task-running-risk") && activeItems.some((item) => item.id === "task-running-watch"), "commander activeRunners should include compact active task items", activeRunners);
assertCondition(attentionCounts.total === 4 && attentionCounts.returned === 4 && attentionCounts.omitted === 0, "commander attention counts should preserve non-terminal attention totals", attentionCounts);
assertCondition(highPriorityIssues.present === true && highPriorityIssues.matchedCount === 7, "commander should surface tracked high-priority issues", highPriorityIssues);
assertCondition(Number(byCategory["business-user-facing"] ?? 0) >= 1
&& Number(byCategory["deployment-artifact"] ?? 0) >= 1
@@ -188,28 +209,47 @@ export function runCodeQueueCommanderViewContract(): JsonRecord {
assertCondition(String(commands.rawOverview ?? "").includes("microservice proxy code-queue") && String(commands.rawOverview ?? "").includes("--raw"), "commander should expose raw overview drilldown", commands);
assertCondition(String(commands.traceTemplate ?? "").includes("codex task <taskId> --trace"), "commander should expose trace drilldown template", commands);
assertCondition(String(commands.outputTemplate ?? "").includes("codex output <taskId>"), "commander should expose output drilldown template", commands);
assertCondition(asRecord(runningRisk.commands).show === "bun scripts/cli.ts codex task task-running-risk", "attention row should include task drilldown command", runningRisk);
assertCondition(String(commands.showTemplate ?? "").includes("codex task <taskId>"), "commander should include task drilldown template for attention rows", commands);
assertCondition(asArray(runningRisk.riskSignals).includes("stale-recovery-candidate") && asArray(runningRisk.riskSignals).includes("blocked"), "active risk row should expose stale/blocker signals", runningRisk);
assertCondition(asRecord(failedUnread.commands).read === "bun scripts/cli.ts codex read task-failed-unread", "failed unread row should include read command", failedUnread);
assertCondition(!attentionItems.some((item) => item.id === "task-failed-unread"), "default commander attention should not expand terminal unread items", { attentionItems });
assertCondition(!commanderBody.includes("raw-prompt-task-running-risk-20"), "commander output should not dump long raw prompt bodies", { chars: commanderBody.length });
assertCondition(!commanderBody.includes("summary-final-task-running-risk-20"), "commander output should not dump long final response bodies", { chars: commanderBody.length });
assertCondition(!commanderBody.includes("\"prompt\""), "commander output should not include prompt preview fields by default", { commanderBody });
assertCondition(!commanderBody.includes("\"last\""), "commander output should not include final-response preview fields by default", { commanderBody });
assertCondition(!recentIds.some((id) => terminalIds.includes(id)), "recentCompleted section must not duplicate terminalUnread rows", { recentIds, terminalIds });
assertCondition(recentIds.length === 3, "recentCompleted commander section should be independently capped", { recentIds });
assertCondition(terminalUnreadSection.returned === 0 && asArray(terminalUnreadSection.items).length === 0, "default commander terminal unread section should omit item details", terminalUnreadSection);
assertCondition(String(asRecord(terminalUnreadSection.commands).unread ?? "").includes("codex unread"), "terminal unread section should point to codex unread drill-down", terminalUnreadSection);
assertCondition(asRecord(supervisorView.completedUnread).count === 3 && asRecord(supervisorView.recentCompleted).count === 5, "supervisor view should remain available and keep separate unread/recent sections", supervisorView);
assertCondition(commanderLimit8Body.length < 16_000, "commander --limit 8 output should stay compact for polling", { chars: commanderLimit8Body });
assertCondition(asRecord(commanderLimit8View.filters).requestedLimit === 8, "commander --limit 8 should preserve requested limit disclosure", commanderLimit8View);
assertCondition(asArray(limit8ActiveRunners.items).some((item) => asRecord(item).id === "task-running-risk"), "commander --limit 8 should keep active items", limit8ActiveRunners);
assertCondition(limit8TerminalUnread.returned === 0 && asArray(limit8TerminalUnread.items).length === 0, "commander --limit 8 should not expand terminal unread item details", limit8TerminalUnread);
assertCondition(!limit8AttentionItems.some((item) => String(item.id ?? "").includes("unread")), "commander --limit 8 attention should omit terminal unread rows", { limit8AttentionItems });
assertCondition(String(limit8Commands.unread ?? "").includes("codex unread"), "commander --limit 8 should keep unread drill-down command", limit8Commands);
assertCondition(String(limit8Commands.full ?? "").includes("--view full"), "commander --limit 8 should keep full drill-down command", limit8Commands);
assertCondition(!commanderLimit8Body.includes("RAW_PROMPT_SHOULD_NOT_LEAK") && !commanderLimit8Body.includes("raw-prompt-task-failed-unread"), "commander --limit 8 should not print unread prompt details", { commanderLimit8Body });
assertCondition(!commanderLimit8Body.includes("summary-final-task-failed-unread"), "commander --limit 8 should not print unread final-response details", { commanderLimit8Body });
assertCondition(fullLimit8Body.includes("raw-prompt-task-failed-unread") || fullLimit8Body.includes("display-prompt-task-failed-unread"), "--view full should still expose task detail previews", { fullLimit8Body });
assertCondition(unreadLimit8Body.includes("task-failed-unread") && unreadLimit8Body.includes("readTemplate"), "supervisor unread drill-down should still expose terminal unread task ids", { unreadLimit8Body });
assertCondition(!commanderLimit8Requests.some((request) => request.path.includes("task-failed-unread") && request.path.includes("/summary")), "default commander --limit 8 should not fetch terminal unread summaries", { commanderLimit8Requests });
return {
ok: true,
checks: [
"commander view is explicit and bounded",
"exact active/queued/retry_wait/terminal-unread counts are preserved",
"attention rows expose stale, heartbeat, terminal-unread and blocker signals",
"attention rows expose active, queued/retry_wait and blocker signals",
"high-priority issue refs are surfaced",
"deterministic classifier emits requested categories",
"drilldown commands are present without prompt/final-response flood",
"commander --limit 8 omits terminal unread details and prompt previews",
"full and unread drill-down paths still expose details",
"recent completed does not duplicate terminal unread",
"supervisor/full views remain available",
],
commanderChars: commanderBody.length,
commanderLimit8Chars: commanderLimit8Body.length,
fullChars: fullBody.length,
};
}