staging-org / ocean-landing-serve / src/ lib/ caissons-model.ts

import type { PullCommit, PullFile } from './pulls-types';

export type CaissonState = 'editing' | 'waiting' | 'review' | 'merged' | 'stopped';
export type SessionKind = 'open' | 'read' | 'edit' | 'commit' | 'pull' | 'ask' | 'stop' | 'merge';
export type SessionStep = { at: string; kind: SessionKind; target: string; note?: string };

// Who opened a caisson: an agent the customer runs, signing in with one of their API keys, or a person in the browser.
export type Opener = { via: 'key'; key: string; owner: string } | { via: 'browser'; person: string };

export type Caisson = {
	id: string;
	repo: string;
	openedBy: Opener;
	title: string;
	branch: string;
	base: { branch: string; sha: string };
	state: CaissonState;
	openedAt: string;
	updatedAt: string;
	scope: string[];
	pull: { repo: string; number: number } | null;
	ask: { path: string; why: string } | null;
	stopped: string | null;
	files: PullFile[];
	commits: PullCommit[];
	session: SessionStep[];
};

export const stats = (files: PullFile[]) => {
	let added = 0;
	let removed = 0;
	for (const f of files)
		for (const h of f.hunks)
			for (const l of h.lines) {
				if (l.kind === 'add') added++;
				if (l.kind === 'del') removed++;
			}
	return { added, removed };
};

export type PathRule = { pattern: string; rule: 'ask' | 'never'; why: string; fixed: boolean };

// Rules for writes that arrive through a caisson. They protect the code, whoever's agent is writing.
export const caissonPolicy = {
	paths: [
		{ pattern: 'migrations/**', rule: 'ask', why: 'Schema changes wait for a person, always.', fixed: true },
		{ pattern: '.sailfork/**', rule: 'never', why: 'Repository settings change only in a reviewed pull request.', fixed: true },
		{ pattern: '**/*.env', rule: 'never', why: 'Secrets stay out of reach.', fixed: false },
		{ pattern: 'infra/**', rule: 'ask', why: 'Production infrastructure.', fixed: false }
	] satisfies PathRule[],
	maxLive: 8,
	idleMinutes: 120
};

export const opener = (o: Opener) => (o.via === 'key' ? o.key : o.person);
export const openerLong = (o: Opener) => (o.via === 'key' ? `the ${o.key} key, ${o.owner}’s` : `${o.person} in the browser`);