#!/usr/bin/env node import fs from "node:fs/promises"; const chunks = []; for await (const chunk of process.stdin) { chunks.push(typeof chunk === "string" ? chunk : chunk.toString("utf8")); } const payload = JSON.parse(chunks.join("") || "{}"); const stateFile = process.env.BOSS_CODEX_REFRESH_FLAKY_STATE; let count = 0; if (stateFile) { try { const state = JSON.parse(await fs.readFile(stateFile, "utf8")); count = Number.isFinite(state.count) ? state.count : 0; } catch { count = 0; } } count += 1; if (stateFile) { await fs.writeFile(stateFile, `${JSON.stringify({ count })}\n`, "utf8"); } if (count === 1) { process.stdout.write( `${JSON.stringify({ status: "failed", targetThreadRef: payload.targetThreadRef, appName: payload.appName, error: "transient desktop refresh failure", })}\n`, ); process.exit(0); } process.stdout.write( `${JSON.stringify({ status: "completed", targetThreadRef: payload.targetThreadRef, appName: payload.appName, deepLink: `codex://threads/${payload.targetThreadRef}`, detail: `flaky refresh accepted after ${count} attempts`, attemptCount: count, })}\n`, );