staging-tester / ocean-landing-serve / src/ lib/ caissons.ts

export const caissonHero = {
	title: 'Agents edit the repository where it lives.',
	lead: 'A caisson is an online worktree: a working copy that stays on Sailfork. Agents read and write files there directly, so nothing is cloned to a disk and no sandbox has to start first.',
	primary: 'Ask for early access',
	secondary: 'See how it works'
};

export const earlyAccessMessage = 'We would like early access to caissons for our agents. ';
export const earlyAccess = `/contact?message=${encodeURIComponent(earlyAccessMessage)}`;

export type SessionLine = { kind: 'branch' | 'read' | 'edit' | 'commit' | 'pull'; verb: string; target: string; note?: string };

export const session = {
	repository: 'acme-pay/api',
	lines: [
		{ kind: 'branch', verb: 'open', target: 'agent/round-totals', note: 'caisson' },
		{ kind: 'read', verb: 'read', target: 'src/billing/invoice.ts' },
		{ kind: 'read', verb: 'read', target: 'src/billing/money.ts' },
		{ kind: 'edit', verb: 'edit', target: 'src/billing/invoice.ts', note: '+4 −2' },
		{ kind: 'edit', verb: 'edit', target: 'src/billing/invoice.test.ts', note: '+18' },
		{ kind: 'commit', verb: 'commit', target: 'Round invoice totals' },
		{ kind: 'pull', verb: 'open', target: 'pull request #418' }
	] satisfies SessionLine[]
};

export const agents = ['round-totals', 'retry-webhooks', 'drop-legacy-auth', 'docs-for-refunds'];

export type CostInput = { checkoutGb: string; agents: string; tasks: string; hours: string; price: string };
export type CostField = keyof CostInput;

export const costPresets = [
	{ id: 'team', label: 'A team', input: { checkoutGb: '3', agents: '8', tasks: '40', hours: '2', price: '0.15' } },
	{ id: 'org', label: 'An organization', input: { checkoutGb: '12', agents: '10,000', tasks: '200,000', hours: '2', price: '0.15' } },
	{ id: 'millions', label: 'Millions of agents', input: { checkoutGb: '12', agents: '1,000,000', tasks: '20,000,000', hours: '2', price: '0.15' } }
] as const satisfies { id: string; label: string; input: CostInput }[];

const fields = {
	checkoutGb: { places: 1, max: 10_000, label: 'a checkout size up to 10,000 GB' },
	agents: { places: 0, max: 10_000_000, label: 'a whole number of agents up to 10 million' },
	tasks: { places: 0, max: 1_000_000_000, label: 'a whole number of tasks up to 1 billion' },
	hours: { places: 2, max: 24, label: 'hours from 0 to 24' },
	price: { places: 4, max: 100, label: 'a price up to $100 an hour' }
} as const satisfies Record<CostField, { places: number; max: number; label: string }>;

// Parses a plain decimal into an integer count of 10^-places units, so the money math never touches floats.
const scaled = (raw: string, places: number, max: number): number | null => {
	const m = /^(\d+)(?:\.(\d+))?$/.exec(raw.trim().replace(/^\$/, '').replaceAll(',', ''));
	if (!m?.[1] || (m[2]?.length ?? 0) > places) return null;
	const value = Number(m[1]) * 10 ** places + Number((m[2] ?? '').padEnd(places, '0') || 0);
	return value > max * 10 ** places ? null : value;
};

export type CostResult =
	| { ok: true; diskTenthsGb: number; checkouts: number; sandboxHundredthsH: number; spendCents: number }
	| { ok: false; errors: Partial<Record<CostField, string>> };

export const sameCost = (a: CostInput, b: CostInput) =>
	(Object.keys(a) as CostField[]).every((k) => a[k].replaceAll(',', '').trim() === b[k].replaceAll(',', '').trim());

export const localCost = (input: CostInput, days = 30): CostResult => {
	const errors: Partial<Record<CostField, string>> = {};
	const v = {} as Record<CostField, number>;
	for (const key of Object.keys(fields) as CostField[]) {
		const f = fields[key];
		const n = scaled(input[key], f.places, f.max);
		if (n === null) errors[key] = `Enter ${f.label}.`;
		else v[key] = n;
	}
	if (Object.keys(errors).length > 0) return { ok: false, errors };
	const sandboxHundredthsH = v.tasks * v.hours;
	// Hundredths of an hour x ten-thousandths of a dollar x days is millionths of a dollar, past 2^53 at the
	// top of the ranges, so it is multiplied as BigInt and rounded half up to cents.
	const millionths = BigInt(sandboxHundredthsH) * BigInt(v.price) * BigInt(days);
	return {
		ok: true,
		diskTenthsGb: v.checkoutGb * v.agents,
		checkouts: v.tasks,
		sandboxHundredthsH,
		spendCents: Number((millionths + 5_000n) / 10_000n)
	};
};

export const caissonFaq = [
	{ q: 'Why is it called a caisson?', a: 'A caisson is the sealed chamber engineers sink into a river to build a bridge pier where it stands, without draining the river. Ours does the same for code: each agent works on the real repository, sealed off on its own branch, while everything around it keeps moving.' },
	{ q: 'Does an agent need a copy of the repository?', a: 'No. It works against the repository on Sailfork. Your own clones are untouched and keep working the way they do today.' },
	{ q: 'Can several agents work on one repository at once?', a: 'Yes. Each opens its own caisson on its own branch, and their work meets only when you merge their pull requests.' },
	{ q: 'How many agents can work at once?', a: 'Caissons are built for organizations running millions of agents across large repositories. Tell us your numbers when you ask for early access.' },
	{ q: 'How does an agent’s work reach my main branch?', a: 'Through a pull request, behind the same reviews and branch rules as a change from a person. An agent cannot skip them.' },
	{ q: 'What happens to the caisson when the work is done?', a: 'Its commits live on the branch, the same as any pushed work. There is nothing left on a machine to delete.' },
	{ q: 'When can I use it?', a: 'Caissons are in private testing. Ask for early access and we will write when your organization can turn them on.' }
];