|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +const STATE_FILE = path.resolve(process.cwd(), "runtime", "consensus_state.json"); |
| 5 | + |
| 6 | +const DEFAULT_OPTIONS = [ |
| 7 | + { option_id: "opt-1", description: "Local speculative serving path (preferred)", friction: "low" }, |
| 8 | + { option_id: "opt-2", description: "Quantized fall-back path", friction: "medium" }, |
| 9 | +]; |
| 10 | + |
| 11 | +function ensureDir(dir) { |
| 12 | + if (!fs.existsSync(dir)) { |
| 13 | + fs.mkdirSync(dir, { recursive: true }); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +export function getConsensusState() { |
| 18 | + try { |
| 19 | + if (fs.existsSync(STATE_FILE)) { |
| 20 | + const content = fs.readFileSync(STATE_FILE, "utf8"); |
| 21 | + return JSON.parse(content); |
| 22 | + } |
| 23 | + } catch (error) { |
| 24 | + console.error("Failed to read consensus state, resetting to default:", error.message); |
| 25 | + } |
| 26 | + |
| 27 | + // Default state initialization |
| 28 | + const defaultState = { |
| 29 | + ok: true, |
| 30 | + signing_chain: { |
| 31 | + codex: "SIGNED", |
| 32 | + openclaude: "SIGNED", |
| 33 | + antigravity: "PENDING", |
| 34 | + }, |
| 35 | + consensus_status: "Awaiting Operator", |
| 36 | + options: DEFAULT_OPTIONS, |
| 37 | + }; |
| 38 | + saveConsensusState(defaultState); |
| 39 | + return defaultState; |
| 40 | +} |
| 41 | + |
| 42 | +export function saveConsensusState(state) { |
| 43 | + try { |
| 44 | + ensureDir(path.dirname(STATE_FILE)); |
| 45 | + // Write atomically using a temporary file to prevent torn-writes |
| 46 | + const tmpFile = `${STATE_FILE}.tmp`; |
| 47 | + fs.writeFileSync(tmpFile, JSON.stringify(state, null, 2), "utf8"); |
| 48 | + fs.renameSync(tmpFile, STATE_FILE); |
| 49 | + return true; |
| 50 | + } catch (error) { |
| 51 | + console.error("Failed to save consensus state:", error); |
| 52 | + return false; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +export function signOffOperator() { |
| 57 | + const state = getConsensusState(); |
| 58 | + state.signing_chain.antigravity = "SIGNED"; |
| 59 | + state.consensus_status = "Consensus Reached"; |
| 60 | + saveConsensusState(state); |
| 61 | + return { |
| 62 | + ok: true, |
| 63 | + message: "Operator signed off successfully. Consensus reached.", |
| 64 | + signing_chain: state.signing_chain, |
| 65 | + consensus_status: state.consensus_status, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +export function vetoOperator() { |
| 70 | + const state = getConsensusState(); |
| 71 | + state.signing_chain.codex = "VETOED"; |
| 72 | + state.signing_chain.openclaude = "VETOED"; |
| 73 | + state.signing_chain.antigravity = "VETOED"; |
| 74 | + state.consensus_status = "Vetoed"; |
| 75 | + saveConsensusState(state); |
| 76 | + return { |
| 77 | + ok: true, |
| 78 | + message: "Operator veto override initiated. Reverting state commit...", |
| 79 | + signing_chain: state.signing_chain, |
| 80 | + consensus_status: state.consensus_status, |
| 81 | + }; |
| 82 | +} |
| 83 | + |
| 84 | +export function initializeNewProposal(options = DEFAULT_OPTIONS) { |
| 85 | + const newState = { |
| 86 | + ok: true, |
| 87 | + signing_chain: { |
| 88 | + codex: "SIGNED", |
| 89 | + openclaude: "SIGNED", |
| 90 | + antigravity: "PENDING", |
| 91 | + }, |
| 92 | + consensus_status: "Awaiting Operator", |
| 93 | + options: options, |
| 94 | + }; |
| 95 | + saveConsensusState(newState); |
| 96 | + return newState; |
| 97 | +} |
0 commit comments