Tasks

One way to complete a task, whether it is one or two hundred

Completing a task ran different code depending on how many you picked and where you clicked. I made it one operation over a set, run with the user's own permissions.

The problem

The agency ran its work in Asana, and the platform was meant to replace it. A teammate built the first task system in December 2025, with templates, task types and saved views. I started on it in March 2026, rebuilt it, and have owned it since.

Two problems shaped the rebuild. Recurrence was a fixed list of four options. And completing a task ran different code depending on how many tasks were selected and where the click happened.

The second one caused real drift. Archiving through the assistant skipped a milestone update the app performed. Undoing the completion of a recurring task left two open copies. Every new side effect had to be added in several places, and nothing failed when one was missed.

The decision

A single task is a set of one, not a separate path.

Completing and reopening became one server operation that takes a change and a set of tasks. The single edit, the bulk edit and the assistant's tools all call it. A caller authenticates the user, picks the organization and shapes the response. Nothing else.

I rejected three other fixes. Looping the single-task code means a failure halfway leaves some tasks changed and some not. Keeping two versions aligned by tests only catches the differences someone thought to test. Putting the rules in browser code leaves out the assistant and background jobs, which never run it.

Single edits, bulk edits and assistant tools all call one completion operation. It opens one transaction as the user, which completes the selected tasks under row-level security, cascades to subtasks and spawns the next occurrence as the system, then commits. Milestone recomputes, email and jobs happen after commit.
Every surface calls one operation. What must be right commits together; the rest waits for the commit.

Running as the user

Atomic writes used to cost a safety check. A multi-step write needs a direct database connection, and that connection logs in with a role that bypasses row-level security. The SQL becomes the only tenant check, and one forgotten organization filter becomes a write into another organization.

So the transaction takes the identity the route already verified and switches to the same role an ordinary signed-in request uses.

A transaction that runs as the userIllustrative
async function runAsUser<T>(user: VerifiedUser, fn: (tx: UserTx) => Promise<T>) {
  return inTransaction(async (tx) => {
    // For the life of this transaction only, the database treats every
    // statement as coming from this user, so its access rules apply.
    await actAs(tx, user);
    return fn(new UserTx(tx, user));
  });
}

Both settings end with the transaction, so a pooled connection never carries one person's identity into the next request. I measured the cost at about one and a half times, around forty milliseconds at the 95th percentile on a change of 200 tasks.

Three tiers of side effects

Each side effect goes in exactly one tier. The test for the first is whether a partial failure would leave data wrong, as opposed to merely stale.

Some first-tier writes are the system's consequence of the change, not the user's own act. They run in the same transaction with elevated rights, and only after the database confirms the user belongs to that organization.

Elevation is narrow and temporaryIllustrative
// System work, such as creating the next occurrence, needs wider access.
// It is granted only for an organization the user belongs to, and only
// for the duration of the callback.
async withSystemAccess<R>(organizationId: string, fn: (tx: Tx) => Promise<R>) {
  if (!(await this.userBelongsTo(organizationId))) {
    throw new OutOfScopeError(organizationId);
  }
  await elevate(this.tx);
  try {
    return await fn(this.tx);
  } finally {
    await dropBackToUser(this.tx);
  }
}
The whole completion, in one transactionIllustrative
return runAsUser(user, async (tx) => {
  // As the user: the database judges every selected row.
  const selected = await lockTasks(tx, taskIds);
  const plan = planCompletion(selected, input);
  await markCompleted(tx, plan.newlyCompleted);

  // As the system, every statement filtered to this organization.
  return tx.withSystemAccess(organizationId, async (sys) => {
    const cascaded = await completeOpenSubtasks(sys, plan.parents);
    const spawned = await createNextOccurrences(sys, plan.parents);
    return { plan, cascaded, spawned };
  });
});

Recurrence without a scheduler

Recurrence became a rule model with six kinds, wired through the editor, the assistant's tools and the Asana importer.

Six kinds of repeatIllustrative
type RecurrenceRule =
  | { kind: 'daily' }
  | { kind: 'weekly'; daysOfWeek: Weekday[]; every: number }
  | { kind: 'monthly-by-date'; dayOfMonth: number; every: number }
  | { kind: 'monthly-nth-weekday'; nth: number; weekday: Weekday; every: number }
  | { kind: 'yearly'; month: number; dayOfMonth: number }
  | { kind: 'after-completion'; days: number };

The next occurrence is created inside the same transaction that completes the current one. There is no scheduled job that can fall behind or run twice. A completed recurring task with no successor, or with two, cannot be committed.

Copies state their contract

Each way of copying a task states what it copies, what it resets and what it never copies. That rule came from a bug: recurring tasks carried logged time onto each new occurrence. Fixing it meant deleting 611 cloned time entries.

Where it stands

What I can't tell you from the record: adoption or usage numbers. The repo holds none.