# Scripting Conventions
<!--
ref: @any-corp/engineering/scripting-conventions
type: protocol
tags: [engineering, scripts, automation, ops, scripting]
triggers: [script, automation, one-off, batch job, cron, ops script]
version: 1
updated_at: 2026-04-30T17:25:41.085Z
-->

How we write the scripts that aren't application code: ops tools, one-offs, ETLs, internal CLIs, glue.

## Three tiers, three different bars

| Tier | Bar | Where it lives |
|---|---|---|
| **One-off** — runs once, then deleted | "Does the thing, no surprises" | Local; `/tmp` or `~/scripts/` |
| **Recurring** — runs again periodically | Reviewable, documented, idempotent | `tools/` in the relevant service repo, or a dedicated `ops/` repo |
| **Production** — anyone can run it, including in incidents | Tested, observable, safe defaults, no surprises | `ops/` repo, code-reviewed |

Don't spend a day writing a script that runs once. Don't spend an hour writing a script that 5 people will use weekly.

## Languages — pick the right one

| Use case | Default | Notes |
|---|---|---|
| Anything > 50 lines | TypeScript with Bun | Real types, real testing, fast startup |
| Anything I want to ship to other people | TypeScript with Bun | See above |
| Quick filesystem / pipe / shell glue | Bash | Up to ~30 lines |
| Data manipulation / pandas | Python | When the libraries make it easier |
| Anything network-calling production | TypeScript with Bun | Rate-limit, retry, observe |

**Don't write Bash > 30 lines.** Past that, it's brittle, untestable, and unreadable. Move to Bun + TypeScript.

## Bun scripts — the standard shape

```typescript
#!/usr/bin/env bun
// purpose: <one-line description>
// usage:   bun ./script.ts [--flag]
// safe:    yes / no / dry-run-by-default

import { parseArgs } from "node:util";

const { values: args } = parseArgs({
  args: Bun.argv.slice(2),
  options: {
    "dry-run": { type: "boolean", default: true },
    "really": { type: "boolean", default: false },
  },
});

const isDestructive = !args["dry-run"] && args["really"];

async function main() {
  if (isDestructive) {
    console.error("⚠ Running destructive mode. CTRL-C now to abort.");
    await new Promise(r => setTimeout(r, 3000));
  }
  // ...
}

await main();
```

Conventions:
- **Top of file**: purpose, usage, whether it's safe.
- **Dry-run by default** for anything destructive. Require an explicit `--really` to actually do the thing.
- **Standard library only** for one-offs. Reach for `bun add` only when the task earns it.
- **Exit codes:** 0 = success, non-0 = something went wrong. Don't swallow.

## Bash scripts — the standard shape

```bash
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

# purpose: <one-line description>
# usage:   ./script.sh [args]

# Refuse to run unless we're in the right directory
[[ -f ".any-corp-marker" ]] || { echo "Run from repo root"; exit 1; }

# Logic
```

Required:
- `set -euo pipefail` at the top, every time.
- **Quote everything**: `"$var"`, `"$@"`. Unquoted variables are bugs waiting.
- **Refuse to run** if the environment is wrong (wrong directory, missing tool, missing env var).
- **Echo what you're about to do** before doing it for anything destructive.

## Idempotency

A recurring script should be safe to run twice in a row.

- Use upsert, not insert.
- Check before deleting.
- Lock or guard against concurrent runs (file lock, named lease, distributed lock).
- For batch jobs, write a "what I did" log so a re-run can reconcile.

## Production-tier scripts

Anything that's run during incidents, by on-call, or by anyone outside the author's head.

Required:
- Lives in `ops/` repo with a README.
- `--help` flag actually works.
- Logs structured (JSON), goes to the same observability stack as the app.
- Has a unit test for the parsing logic and an integration test for the network calls.
- Has a **dry-run mode**.
- Has **rate limits and retries** for network calls.
- Has **circuit breakers** if it might blow up downstream.
- Reviewed via PR like any other code.

## Cron jobs and scheduled tasks

- **One source of truth** for the schedule, in `ops/cron.yaml` (or equivalent) — not scattered in service repos.
- **Time zones explicit.** Always UTC in the schedule definition; convert at the seam.
- **Run logs** in observability. We page on missed runs, not just failed ones.
- **Idempotent**, every time.

## Secrets in scripts

- Read from environment / secret manager, never hardcoded.
- Never commit a `.env`. Use `.env.example` with placeholders.
- For local dev, use 1Password CLI:
  ```bash
  op run --env-file=.env.example -- ./script.ts
  ```

## What we don't do

- We don't ship Bash scripts > 30 lines unreviewed.
- We don't put production-touching scripts only on someone's laptop.
- We don't run "this script worked once" in incidents without re-reading what it does.
- We don't paste credentials into Slack to "kick something off real quick."

---

*Related: `@content/youtube/hormozi/2025-01-04-the-ultimate-sales-training-for-2026-full-course` · `@integrations/linkedin` · `@integrations/agent-browser` · `@protocol/protocol-api` · `@content/youtube/wes-mcdowell/the-5-biggest-youtube-mistakes-businesses-make-1-200-channels-studied`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*