background-work

background-work is a cursor rule for Cursor from BetterTyped/hype-stack. It costs 0 tokens per session (924 once invoked), scanned A, original, MIT.

Guidance for running delayed and background work, including scheduled jobs and a Postgres-backed task queue. A task queue stores work until a worker can process it, while a scheduler decides when work starts.

In plain words
What is it for?
Use it to add scheduled jobs, queue tasks such as uploads or reports, set concurrency, and run workers safely.
Why use it?
It prevents slow work from blocking database transactions and explains how retries, duplicate runs, and separate workers should be handled.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit Use it to add scheduled jobs, queue tasks such as uploads or reports, set concurrency, and run workers safely.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/bettertyped/hype-stack/background-work
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/BetterTyped/hype-stack

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for background-work

README.md
[![agentmods](https://agentmods.dev/badge/rules/bettertyped/hype-stack/background-work/github.svg)](https://agentmods.dev/rules/bettertyped/hype-stack/background-work)
Your own site
<a href="https://agentmods.dev/rules/bettertyped/hype-stack/background-work"><img src="https://agentmods.dev/badge/rules/bettertyped/hype-stack/background-work/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for background-work

Your own site · 80×15
<a href="https://agentmods.dev/rules/bettertyped/hype-stack/background-work"><img src="https://agentmods.dev/badge/rules/bettertyped/hype-stack/background-work.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 924 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00000 $0.00924
Opus 5.5 $0.00000 $0.00370
Sonnet 5.5 $0.00000 $0.00185
Haiku 4.5 $0.00000 $0.00092

Measured 26d ago against content hash b0d54d3a36db, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

background-work scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 26d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/background-work.mdc · 70 lines

How it starts

The opening of the file, as written. The whole thing — 70 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Background Work

Two tools, two questions. The scheduler (src/libs/scheduler) answers when. The task queue (src/libs/queue) answers how much at once.

Job Task
What a cron entry one queued unit of work
Registered in src/jobs/index.ts src/queues/index.ts
Runs once per fire across all instances on any instance, up to the queue's concurrency each
Good for sweeps, digests, cleanup, "every night at 3" parsing an upload, calling a model, sending a batch
Must be fast: it holds a database transaction while it runs safe to run twice

If a job finds slow work, it enqueues tasks and returns. It never does the slow work itself.

Defining a queue

// src/features/reports/queues/render-report.queue.ts
export const renderReportQueue = defineQueue({
  name: "render-report",
  schema: z.object({ reportId: z.string() }),
  concurrency: 2,
  exclusive: true,
  run: async ({ data, signal }) => {
    await renderReport(data.reportId, signal);
  },
  onExhausted: async ({ data }) => {
    await markReportFailed(data.reportId);
  },
});

Then add it to the array in src/queues/index.ts. enqueue throws for a queue that is not registered there.

Enqueue in the same transaction as the row

await postgres.qb.transaction().execute(async (trx) => {
  await trx.insertInto("report").values(report).execute();
  await enqueue(renderReportQueue, { reportId: report.id }, { trx, key: report.id });
});

The task commits or rolls back with the row. A row can never sit in queued with nothing behind it. Never enqueue before the transaction, and never after it in a separate step.

Rules

  • Payloads are ids, not data. Put { reportId } in the task and read the row in the handler. The row is the truth, the payload is a pointer.
  • Status lives in your table. Keep a status column the UI can read. The queue is plumbing, not a place the frontend looks.
  • Handlers run twice sometimes. A crash or a deploy mid-run means a retry. Checkpoint progress in your own tables and resume from it.
  • Errors a retry cannot fix are not thrown. Bad file, unsupported format: write the failure to your row and return. Throw only when trying again could help.
  • onExhausted marks the row failed. It fires once, after the last retry. Sentry gets the error either way.
  • Pass signal to slow calls. It aborts on expiry and on shutdown.
  • No try/catch to swallow errors in a handler. Same rule as routes: let it throw, the queue logs, retries, and reports.
  • Use exclusive with a key when the task is "process row X", so a double click cannot process it twice.

Read the full file on GitHub · 70 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 26d ago Changed · -8 lines b0d54d3a36db
  2. 27d ago First seen · 78 lines · 0 tokens per session scan A f392effb8dd2

Subscribe to this mod's changes

background-work is a cursor rule published in the GitHub repository BetterTyped/hype-stack (12 stars, last pushed 14d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 924 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-19.