Skip to content
astra-voidPublic

About

Static analysis for partial failures in TypeScript backends

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Midwrite

v0.1.0: experimental. Midwrite is a young static analyzer with a narrow scope. It misses real problems, and some of what it reports is intentional in your code. Read this page before relying on it.

Midwrite looks at TypeScript backends for places where a crash, restart, timeout or failed call between two separately committed side effects leaves the system half-done. For example, the database says an order is paid, but the order.completed event was never published.

It reads your source and never runs it. No account, no network, no telemetry.

Install

From npm, with prebuilt binaries for macOS (arm64, x64), Linux (arm64, x64; static musl builds) and Windows (x64, arm64; static analysis only, no runtime replay):

npm install --save-dev midwrite

Or build from source with Rust 1.97 or newer, on any platform the code compiles on:

cargo install --path crates/midwrite-cli

The v0.1.0 GitHub release carries prebuilt archives for the same six platforms, each with a SHA-256 checksum file. What has been checked about this release, and when (clean installs, checksums, CI): release status.

The JavaScript client for replay harnesses is on npm as @midwrite/replay. You only need it if you write a harness by hand; midwrite init-replay vendors its own copy.

A real bug it found

Kan's Stripe checkout webhook creates a workspace, then its subscription, as two separate writes:

// Reduced from Kan's checkout.session.completed handler.
if (!existingWorkspace) {
  await workspaceRepo.create(db, workspaceInput);   // commits
  // A failure here leaves a workspace without its subscription.
  await subscriptionRepo.create(db, subscriptionInput);
}
// When Stripe retries, existingWorkspace is present, so neither create runs.

Midwrite found this across modules, through the repository helpers (MW1004). If the second write fails, Stripe redelivers the event, the guard sees the workspace, and the subscription is never created.

What this example does and doesn't show:

What a warning looks like

export async function completeOrder(orderId: string) {
  await db.update(orders).set({ status: "paid" }).where(eq(orders.id, orderId));
  // <- a crash here leaves a paid order without an event
  await queue.publish("order.completed", { orderId });
}
warning[MW1001]: database commit followed by an independent external effect
  --> src/orders.ts:4:9 (in `completeOrder`, confidence: high)
    |
  4 |   await queue.publish("order.completed", { orderId });
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = `db.update(orders)` commits before `queue.publish("order.completed", { orderId })` (message-publish); a failure in between leaves the database changed without the external effect
  failure sequence:
    1. `db.update(orders)` (2:9) commits immediately: it is not inside a recognized transaction.
    2. The process crashes or is restarted, or the call fails, after the commit and before `queue.publish("order.completed", { orderId })` completes.
    3. `queue.publish("order.completed", { orderId })` is never performed.
    4. Result: the database change is durable but the external effect never happens, and nothing records that it is still owed.
  help: Record the intent in the same database transaction as the state change (transactional outbox) and perform the external effect from a relay that retries until it succeeds, or make a reconciliation job derive the missing effect from database state.
Code Default severity Meaning
MW1001 warning A database commit, then an external effect
MW1002 warning An external effect, then a database commit
MW1003 warning An external effect inside a transaction, before it commits
MW1004 info (warning when a write bypasses its transaction) Two independent database commits
MW1005 info Two independent external effects
MW9001 info Not a finding: Midwrite could not analyze something here

midwrite explain MW1001 prints the full explanation of a code.

What a warning means

When Midwrite reports a window, the window almost always exists. Whether it is a bug is a separate question, and the answer is "no" more often than "yes".

Two scorings on code the analyzer was never tuned against give the best estimate of how it behaves on yours:

  • Current held-out set (scores the current analyzer): four projects added on 2026-11-12 and frozen before scoring, never tuned against: lmsqueezy/nextjs-billing, crafter-station/petdex, mickasmt/next-saas-stripe-starter and kriasoft/react-starter-kit. Midwrite reported 32 hazards.
  • Historical held-out scoring (an older analyzer): Kan, Dittofeed and vercel/chatbot, scored on 2026-11-12 before any tuning on them, with 127 hazards. These projects have since moved to the dev set.
current held-out historical held-out
The reported window exists in the code 30 / 32 (94%) 122 / 127 (96%)
…and nothing in the app handles it (a likely bug) 3 / 32 (9%) 57 / 127 (45%)
…but the app accepts or repairs it (best-effort email, job retries, webhook redelivery) 27 / 32 (84%) 65 / 127 (51%)
False positive: the window is not real 2 / 32 (6%) 5 / 127 (4%)

The current set is small. Its 95% interval for "the window exists" runs from 80% to 98%. Most of its findings come from one project (petdex, 24), where most of the reported windows sit in best-effort code: emails in a silent try/catch, or storage writes that a scheduled job backfills.

So treat a warning as a place worth five minutes of review, not as a confirmed bug. Midwrite cannot see retries, reconciliation jobs or idempotent consumers that live outside the analyzed code. You can declare them (see configuration).

How much to trust these numbers:

  • The historical projects are no longer held out. After that scoring, they were moved to the dev set and the analyzer was tuned on their misses and false positives. On those same projects 99% (165/166) of reported windows are real by the analyzer's own change, 100% only after one label correction no separate reviewer has checked, and 52% (86/166) are unhandled bugs. That is a tuned dev result and says little about your code.
  • The current held-out projects were chosen by a fixed rule, not by hand: the first eligible projects in a star-ordered GitHub search. The rule did not consider how well Midwrite does on them. One of them (react-starter-kit) does all its writes inside an auth library and produced no findings and no audited windows.
  • Every label was written by AI agents, not by people. The agents that labeled findings were separate from the ones that audited the code, but no human has reviewed the labels yet.
  • The 0.9 precision gate passed only after an earlier set change. The first scoring was 0.89, and one project that scored 0.60 was moved to the dev set so its false positives could be fixed.
  • On the dev projects, which the rules were tuned on before 2026-11-12, it was lower: 84%.

The details are in the evaluation harness.

What it misses

Recall is the weak side. Expect Midwrite to stay silent about real problems.

  • On the current held-out set it found 7 of 18 audited windows (39%). Four of the misses are in petdex operator scripts that the project's configuration excludes; inside the analyzed code it found 7 of 14. In the historical held-out scoring before tuning it found 49 of 61 (80%). That denominator comes from capped AI audits of code that already uses the supported stack, so the real miss rate on your code is likely higher.
  • Where effects went through helpers it couldn't follow, recall dropped. On Dittofeed (ClickHouse and Temporal behind helpers) it found 11 of 20. After tuning on these projects it finds all 71 known windows there. That shows the specific gaps were fixed, not that recall is now complete on other code.
  • Anything outside the supported stack below is not seen at all.

Common reasons it stays silent:

  • Methods on values whose class it can't determine: parameters, SDK clients, factory results.
  • Packages in node_modules, dynamic import(), CommonJS require.
  • Calls more than 3 levels deep (--max-call-depth), recursion, .then() continuation bodies, callbacks run by code it doesn't analyze.
  • Writes through anything other than Drizzle: raw SQL, Prisma, Supabase table writes, other ORMs.
  • Second and later loop iterations, and top-level module code.

When it notices something it can't follow next to a recognized effect, it says so with MW9001 instead of guessing. The full list is in limitations.

Supported stack

Supported today
Language TypeScript (.ts, .tsx, .mts, .cts), parsed with Oxc
Database Drizzle ORM on PostgreSQL only: writes, reads, db.transaction(...), savepoints
External effects What you list in midwrite.json, plus built-in rules for Stripe, Resend, BullMQ, Redis (ioredis, redis, Upstash), Supabase storage and auth admin, S3 (including multipart uploads), ClickHouse inserts, Polar, Nodemailer, SendGrid, Twilio, Vercel Blob
Code structure Functions in the same file and in imported files (relative imports, tsconfig paths, workspace packages), classes with known receivers, object services, callbacks passed to analyzed functions
Platforms Full test suite on macOS arm64 and Linux aarch64. npm binaries for macOS x64, Linux x64 and Windows x64/arm64 are built and smoke-tested in CI only. Windows has static analysis only.

Not supported: Prisma, TypeORM, Kysely, Sequelize, MikroORM, raw SQL, MySQL, SQLite and libSQL dialect semantics, and plain .js/.jsx files (they are not scanned). More detail is in supported patterns.

Use

midwrite check .
midwrite check ./src --format json

midwrite.json tells it which names are your database handle and which calls are side effects:

{
  "databaseReceivers": [{ "name": "db", "importedFrom": "@/server/db" }],
  "externalEffects": [
    { "receiver": "queue", "method": "publish", "category": "message-publish" },
    { "function": "sendEmail", "category": "email", "importedFrom": "@/lib/email" }
  ]
}

Configuration entries are trusted as written. Prefer importedFrom, so a parameter that happens to be named db doesn't match. See configuration.

To adopt it in CI without fixing every existing finding first, record a baseline:

midwrite check . --write-baseline midwrite-baseline.json
midwrite check . --baseline midwrite-baseline.json

Exit status: 0 clean, 1 a diagnostic at or above --fail-on (default warning), 2 usage or configuration error, 3 some files could not be parsed. The quickstart and the check guide cover policies, inline suppressions and output formats (text, JSON, SARIF).

More experimental commands

check is the part this release is about. The binary also contains heavier tools that are less tested on real code. Their evidence comes only from first-party fixtures:

  • midwrite trace: the failure path behind a finding, as a static description.
  • midwrite replay --allow-execution: runs a project-owned harness against a disposable PostgreSQL, kills it at a checkpoint, and checks the surviving state. This executes code.
  • midwrite model-check and midwrite discover: bounded model checking of concurrent and crash/retry scenarios against a model of PostgreSQL.

No bug in an external application has been confirmed with these tools. See docs/README.md for guides, and security before running anything with --allow-execution.

Documentation

Feedback

This release exists to learn where Midwrite is wrong. A false positive, a missed window, or a finding you couldn't understand are all useful reports. Templates are in docs/alpha.

Contributing

See CONTRIBUTING.md and testing. New behavior comes with a fixture under fixtures/ whose // @expect annotations state the exact diagnostics.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project shall be dual licensed as above, without any additional terms or conditions.

About

Static analysis for partial failures in TypeScript backends

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages