UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

139 lines (134 loc) • 7.13 kB
/** * Does an update own the runtime it is about to stop and restart? * * Both updaters ask this: `src/update/index.ts` on the Bun path and `bin/ocx.mjs` on the * npm and pnpm path. It lives here as plain ESM for the same reason `stop-decision.mjs` * does — the Node launcher has to be able to import it, and two lanes deciding the same * situation separately is how a fix ships on one side only. */ function desktopSupervisionBlocked(supervision) { return supervision === true || supervision?.kind === "desktop" || (supervision?.kind === "unknown" && supervision.desktopSeen); } /** * Decide how an update treats a runtime it may not own. * * `ocx update` replaces the package's files and then puts the proxy back: it stops the * running server first, and afterwards runs `ocx service repair` to re-register and restart * the background service. Under a desktop owner both halves are wrong. The running server is * the app's own bundled sidecar rather than anything this package installed, so stopping it * takes down a runtime the update has no way to bring back; and the repair would re-enable * the npm launcher the takeover superseded, which is the exact reactivation the ownership * marker exists to prevent. Neither half is needed either — the app updates its own runtime. * * The service registration itself is untouched in every case. It is kept by decision, not by * accident, so a user who later runs `ocx service install` gets their npm service back. * * Durable ownership and live supervision are separate. Desktop supervision denies package * replacement, stop and restoration even without a claim. A latch retains that veto through * inconclusive probes until a positive none. Plain unknown supervision alone keeps existing * behavior; unknown durable ownership still vetoes. Explicit service install recovers a stale * durable claim after Desktop has quit. * * The three returned flags are separate authorities, not commands. In particular, leaving * a runtime running is not permission to replace the package it may be executing from. * * A recorded desktop claim does not prove which binary is live. Until the bundled resolver * carries installation identity, it therefore blocks package replacement as well as stop and * restoration; the notice tells a stale-marker user how to take ownership back explicitly. * * @param {{ ownership: { owner: string, installId: string, consentGeneration: number } | null, ownershipUnknown?: boolean, serviceInstalled: boolean, supervision?: boolean | ReturnType<typeof import('../service/desktop-supervision.mjs').inspectDesktopSupervision> }} input * @returns {{ mayReplacePackage: boolean, mayStopRuntime: boolean, mayRestoreService: boolean, notice: string | null }} */ export function planUpdateRuntimeHandling({ ownership, ownershipUnknown = false, serviceInstalled, supervision }) { if (desktopSupervisionBlocked(supervision)) { return { mayReplacePackage: false, mayStopRuntime: false, mayRestoreService: false, notice: "OpenCodex Desktop runs this proxy from its own bundle; use the app's updater (tray → Check for Updates). " + "This npm/Bun install was left unchanged; quit OpenCodex first to update it.", }; } // Unreadable, malformed or contradictory is not "nobody owns it". Reading it that way is // how a permissions error reactivates the npm launcher over a consented takeover. if (ownershipUnknown) { return { mayReplacePackage: false, mayStopRuntime: false, mayRestoreService: false, notice: "⚠️ The background runtime's recorded owner could not be determined, so it was " + "left running and the service registration was not touched. " + "Run 'ocx service install' to re-register the service and take the runtime back.", }; } // Any owner that is not this CLI. Reading it this way rather than testing for "desktop" // keeps a third kind of owner from silently falling into the branch that touches the npm // registration. if (ownership && ownership.owner !== "cli") { return { // A claim does not prove which binary is live. A stale desktop marker beside a // manually started npm proxy would otherwise replace that proxy's executing files. mayReplacePackage: false, mayStopRuntime: false, mayRestoreService: false, notice: `🖥️ The desktop app owns the background runtime (install ${ownership.installId}, ` + `consent generation ${ownership.consentGeneration}). It and the npm package were left unchanged, and the ` + "service registration was neither re-enabled nor restarted. " + "If the desktop app is gone, run 'ocx service install' to take the runtime back.", }; } return { mayReplacePackage: true, mayStopRuntime: true, mayRestoreService: serviceInstalled, notice: null, }; } /** Decide recovery after this updater already stopped the prior CLI-owned runtime. */ export function planStoppedRuntimeRecovery({ stopAttempted, supervision, ownership, ownershipUnknown = false, sameOwner, liveness, serviceInstalled, launcherUsable, hadRuntimeState, }) { if (!stopAttempted) return { action: "none", reason: "not-stopped" }; if (desktopSupervisionBlocked(supervision)) return { action: "none", reason: "desktop-supervised" }; if (ownershipUnknown) return { action: "manual", reason: "ownership-unknown" }; if (!sameOwner || (ownership && ownership.owner !== "cli")) { return { action: "none", reason: "ownership-transferred" }; } if (liveness !== "dead") return { action: "manual", reason: `runtime-${liveness}` }; if (!launcherUsable) return { action: "manual", reason: "launcher-unavailable" }; if (serviceInstalled) return { action: "service", reason: "same-cli-owner" }; if (hadRuntimeState) return { action: "direct", reason: "same-cli-owner" }; return { action: "none", reason: "nothing-to-restore" }; } /** * Re-read the current package runtime before probing. The result keeps an absent current * record distinct from a dead captured endpoint while still projecting one fail-closed * liveness verdict for replacement and recovery decisions. */ export function inspectPackageRuntimeLiveness({ capturedTarget, readCurrentTarget, probe }) { const currentTarget = readCurrentTarget(); const observations = new Map(); const inspect = target => { const key = `${target.hostname}:${target.port}`; if (!observations.has(key)) observations.set(key, probe(target)); return observations.get(key); }; // Probe the fresh record first. It is the address a replacement runtime may have // published while the updater was waiting on the ownership lease. const current = currentTarget.kind === "target" ? inspect(currentTarget.target) : currentTarget.kind; const captured = inspect(capturedTarget); const verdicts = current === "absent" ? [captured] : [current, captured]; const overall = verdicts.includes("live") ? "live" : verdicts.includes("unknown") ? "unknown" : "dead"; return { current, captured, overall }; }