Intelligence

Artifacts

Browse the repository, read documents, and manage the governance folders. Source, runtime, and infrastructure are read-only.

Repository
README.md
CONSTITUTION_COMPLIANCE_AUDIT_V1.mdREADME.md
repositories/aaf-holdings/hq01/lib/executives/proposals.ts
3.6 KB
import fs from "node:fs";
import path from "node:path";
import { PROPOSALS_DIR } from "./config";
import { officeById } from "./offices";
import { activateOffice } from "./registry";
import type { ExecutiveProposal, RegistryEntry } from "./types";

/**
 * Approval-gated executive office proposals. When the router needs an office
 * that is not yet staffed, a proposal is recorded here — it is NEVER
 * auto-approved. Only an explicit approve action activates the office.
 */

function stamp(): string {
  return new Date().toISOString().replace(/[:.]/g, "-");
}

function proposalPath(id: string): string {
  return path.join(PROPOSALS_DIR, `${id}.json`);
}

function writeProposal(p: ExecutiveProposal): void {
  fs.mkdirSync(PROPOSALS_DIR, { recursive: true });
  const tmp = `${proposalPath(p.id)}.tmp`;
  fs.writeFileSync(tmp, JSON.stringify(p, null, 2) + "\n", "utf8");
  fs.renameSync(tmp, proposalPath(p.id));
}

export interface CreateProposalInput {
  proposedExecutiveId: string;
  reason: string;
  originatingInstruction: string;
}

/** Record a pending proposal for an office derived from the catalogue. */
export function createProposal(input: CreateProposalInput): ExecutiveProposal {
  const office = officeById(input.proposedExecutiveId);
  if (!office) {
    throw new Error(`Unknown office "${input.proposedExecutiveId}".`);
  }
  const proposal: ExecutiveProposal = {
    id: `${stamp()}-${office.id}`,
    proposedExecutiveId: office.id,
    office: office.office,
    department: office.department,
    reason: input.reason,
    originatingInstruction: input.originatingInstruction,
    proposedAuthority: [
      `Decisions within the ${office.department} department.`,
      `Ownership of: ${office.routes.join(", ")}.`,
    ],
    proposedBoundaries: [
      "No production deploys or production CRM changes.",
      "No authority over another office's charter.",
      "No self-modification of canonical doctrine.",
      "Escalates beyond charter to its parent office.",
    ],
    proposedRoutes: office.routes,
    status: "pending_approval",
    createdAt: new Date().toISOString(),
  };
  writeProposal(proposal);
  return proposal;
}

export function listProposals(): ExecutiveProposal[] {
  let names: string[];
  try {
    names = fs.readdirSync(PROPOSALS_DIR);
  } catch {
    return [];
  }
  const out: ExecutiveProposal[] = [];
  for (const name of names) {
    if (!name.endsWith(".json")) continue;
    try {
      out.push(
        JSON.parse(
          fs.readFileSync(path.join(PROPOSALS_DIR, name), "utf8"),
        ) as ExecutiveProposal,
      );
    } catch {
      /* skip unreadable */
    }
  }
  return out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
}

export function getProposal(id: string): ExecutiveProposal | null {
  try {
    return JSON.parse(
      fs.readFileSync(proposalPath(id), "utf8"),
    ) as ExecutiveProposal;
  } catch {
    return null;
  }
}

/** Approve a proposal: activate the office in the registry. */
export function approveProposal(id: string): {
  proposal: ExecutiveProposal;
  entry: RegistryEntry;
} {
  const proposal = getProposal(id);
  if (!proposal) throw new Error("Proposal not found.");
  if (proposal.status === "rejected") {
    throw new Error("Proposal was rejected.");
  }
  const entry = activateOffice(proposal.proposedExecutiveId, {
    routes: proposal.proposedRoutes,
  });
  proposal.status = "approved";
  writeProposal(proposal);
  return { proposal, entry };
}

export function rejectProposal(id: string): ExecutiveProposal {
  const proposal = getProposal(id);
  if (!proposal) throw new Error("Proposal not found.");
  proposal.status = "rejected";
  writeProposal(proposal);
  return proposal;
}

root · /srv/aaf