Automations

An automations builder, proved before anything switched to it

Replacing hard-coded business rules with a builder people can edit, without changing what a client receives on the day of the switch.

The problem

Automations existed in two limited forms. One settings page could create tasks when a contract was signed and nothing else. The email sequence engine treated every step as an email, so it could not say: wait two business days, then create a task.

Nothing in the app could author an automation for the newer engine. The only thing that created one was a seed script. Meanwhile background functions and database triggers changed data as side effects, and no single place listed them.

Built in, or a team automation?

Before building a canvas I had to decide what belongs on it. I ask two questions of every rule. Would two agencies reasonably want different behaviour? Could an agency turn it off without leaving data that contradicts itself?

If both answers are no, the rule is built in. It shows in settings, and it is locked.

A built-in rule, declaredIllustrative
{
  id: 'signed-contract-becomes-active',
  when: 'A contract is signed',
  does: ['Marks it active', 'Sets its win probability to 100%'],
  why: 'A signed contract is active business. Billing, revenue and the ' +
    'pipeline all count active contracts, so one left in draft would be ' +
    'missing from all three.',
  reads: ['contract.signedAt'],
  writes: ['contract.status', 'contract.probability'],
  // Where the behaviour actually lives, so the entry can be checked.
  implementedBy: [{ kind: 'code' }, { kind: 'database trigger' }],
  logsRuns: true,
}

Declare it or exempt it

Every background function and database trigger must be declared in that registry or explicitly exempted with a reason. A test fails otherwise.

The test that keeps the list honestIllustrative
it('every background function is declared or exempted', () => {
  const unaccounted = registeredFunctions.filter(
    (id) => !declared.has(id) && !exemptions.has(id),
  );
  expect(unaccounted).toEqual([]);
});

it('nothing is declared that no longer exists', () => {
  const stale = [...declared].filter((id) => !registeredFunctions.includes(id));
  expect(stale).toEqual([]);
});

I rejected routing the business code through the registry. Declaring and verifying is the contract. Moving a rule's code is its own change, with its own tests.

A run log that cannot change an outcome

The run log answers why something happened. It describes the work and is never part of it. Recording a run happens after the business write commits, never throws, and gives up after three seconds. The worst case is a missing log row.

The log is append-only, and the database is what refuses edits and deletes.

Append-only, enforced by a triggerIllustrative
CREATE FUNCTION refuse_run_log_changes() RETURNS trigger AS $$
BEGIN
  RAISE EXCEPTION 'the run log is append-only';
END;
$$ LANGUAGE plpgsql;

-- Rows can be added. They cannot be edited, removed or wiped.
CREATE TRIGGER run_log_no_edits
  BEFORE UPDATE OR DELETE ON run_log
  FOR EACH ROW EXECUTE FUNCTION refuse_run_log_changes();

CREATE TRIGGER run_log_no_truncate
  BEFORE TRUNCATE ON run_log
  FOR EACH STATEMENT EXECUTE FUNCTION refuse_run_log_changes();

Copies and shadow runs

I did not cut over. I first copied the live welcome and billing sequences into the builder as drafts that could not be published. Every way a copy could not match the original was listed as a gap, never dropped silently.

Then I added shadow runs. On real contract signings, the system recorded what each copy would have done and compared it with what the live code did.

A real contract signing goes to the live sequence, which sends, and to the builder copy, which only records what it would have done. The two are compared send by send. A difference becomes a listed gap or a fix. A match allows a switch made in one transaction, which can be switched back.
The copy runs in the shadow of the live code until the two agree. Only then does anything switch.
What counts as the same sendIllustrative
// Two sends count as the same if both happened, or both didn't,
// and their times are within a small tolerance of each other.
function sameSend(live: Send | null, copy: Send | null): boolean {
  if (Boolean(live) !== Boolean(copy)) return false;
  if (!live || !copy) return true;
  return Math.abs(live.at.getTime() - copy.at.getTime()) < TOLERANCE_MS;
}

The welcome copy matched three of five emails to the minute, and the other two differed on a listed gap. The payment chase copy matched on all five failed invoices.

Switching over marks the old sequence as replaced and publishes the copy in one transaction, so there is never a moment when both start runs or neither does. Switching back is one transaction too.

Forty-one messy cases, four times

I wrote 41 deliberately messy cases covering welcome, payment reminders, failed invoices, renewals, form chasers, kickoff tasks and win-back. A harness seeded one client per case, signed contracts through the public signing page, moved a shared clock forward day by day and fired payments and failures.

A browser agent played five people: an operations admin, an account manager, a new admin at a different agency, an executive skimming, and a client. I ran the cases four times, fixing what each round found and rerunning the same stories. Findings were numbered to 56.

Where it stands

Edits save to a draft, and publishing makes a version live for new runs only. The assistant can build, edit and dry-run automations, but it writes drafts only. Publishing and switching an automation on or off stay with a person.

What I can't tell you from the record: the stress test ran locally, not in production. The repo does not establish whether the builder is switched on in production, whether welcome and billing run on it there, or how many automations people have built.