npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@agent-post-office/spam-filter

v0.2.1

Published

Worker-safe additive email spam scorer used by Agent Post Office. Pure TypeScript, no required runtime dependencies. Optional SpamAssassin rule subpath.

Readme

@agent-post-office/spam-filter

A small, dependency-light spam scorer for agent mailboxes. Pure TypeScript. The core entry has no runtime dependencies. Runs in Cloudflare Workers, browsers, and Node.

Extracted from Agent Post Office.

Never drops mail. This package only scores. Your app decides whether to tag, move to a spam folder, or leave the message alone.

Status: v0.2.0. Package on npm. Release steps are in RELEASING.md.

Scorer matches APO main 8ccceaf: content rules, Authentication-Results, allow/block lists, and Bayes from operator marks. SpamAssassin rules are an optional subpath, @agent-post-office/spam-filter/spamassassin.

Quick start

npm install @agent-post-office/spam-filter
import {
  scoreMessage,
  applySpamFilter,
  tokenizeMessage,
  spamProbabilityFromStore,
  type SpamStore,
} from "@agent-post-office/spam-filter";

const assessment = scoreMessage({
  from: "[email protected]",
  subject: "We tested your site",
  text: "We noticed your product. Got 15 minutes? Book a demo.",
  authenticationResults: "mx.example.net; dkim=pass; spf=pass; dmarc=pass",
  recipientDomain: "example.com",
});

// assessment: { score: 0-100, reasons: string[], verdict: "spam" | "ham" }

const disposition = applySpamFilter("tag", assessment, []);
// tag mode: spam stays folder "inbox" with a "spam" label
// quarantine mode: spam uses folder "spam"

How scoring works

Additive score from 0 to 100. The final score is Math.min(100, sum). Verdict is spam when score >= threshold.

Default threshold is 60 (DEFAULT_SPAM_THRESHOLD). Callers pass threshold on scoreSpam / scoreMessage. In APO, SPAM_THRESHOLD overrides the default instance-wide, and inbox_spam_settings.threshold overrides that per inbox. inbox_spam_settings.mode also overrides the instance SPAM_FILTER mode per inbox.

The package never rejects mail. Callers map the verdict with applySpamFilter.

Allow and block lists (checked first)

| Signal | Effect | | --- | --- | | Exact sender block | score 100, spam | | Exact sender allow | score 0, ham | | Domain block (exact or subdomain) | score 100, spam | | Domain allow | score 0, ham |

Authentication-Results

| Signal | Points | | --- | --- | | dmarc=fail | +40 | | spf=fail | +20 | | dkim=fail | +20 | | spf=softfail | +10 |

pass never adds points. A single auth failure alone stays under 60 when the threshold is 60.

Content rules

Each content weight is at most 19, so one content rule stays ham on its own and stays ham with one authentication failure (largest auth failure is DMARC fail at 40: 19 + 40 = 59). There is no cap on how many content rules may fire together; only the total score is clamped to 100.

| Rule | Points | Notes | | --- | --- | --- | | cold_audit | +19 | Subject matches "we tested your" | | outreach_opener | +14 | Cold-outreach openers | | meeting_ask | +16 | Asks for minutes / a call / a demo | | marketing_subdomain | +12 | Marketing-style subdomain plus a pitch | | unrelated_incident | +12 | Downtime/outage subject naming another host, plus a pitch | | verification_subject | +16 | Verify / confirm email or account | | urgency_notice | +8 | "Important notice" with a verification subject | | freemail_verification | +8 | Freemail sender with a verification subject | | empty_verification_body | +10 | Empty body with a verification subject |

Exports: CONTENT_RULE_WEIGHTS, AUTH_FAILURE_WEIGHTS, verdictForScore.

Cloudflare Email Routing already rejects many blocklisted IPs before the Worker runs. This package targets authenticated cold sales and phishing-style verification notices that still pass DKIM and DMARC.

Bayes from operator marks

Bayes is one more additive signal on the same 0-100 score.

| Gate | Behavior | | --- | --- | | Marks per class | Contributes nothing until each class has at least SPAM_LEARN_MIN marks (default 20) | | Posterior floor | Contributes nothing when spam probability is below 0.90 | | Points | round(52 * (p - 0.90) / 0.10), capped at 52 (BAYES_CAP) | | Ham posteriors | Add no points (learning cannot hide a rules hit) |

Use the same tokenizer for training and scoring:

import { tokenizeMessage, canonicalTokens, trainingDelta, signalProbability } from "@agent-post-office/spam-filter";

const tokens = canonicalTokens(tokenizeMessage({ from, subject, text }));

Hosts keep storage. Implement SpamStore and call spamProbabilityFromStore / scoreWithStore, or pass { bayes: { spamProbability } } into scoreSpam after loading counts yourself.

Results

Illustrative local runs use example.com addresses only.

| Case | Score | Verdict | Notes | | --- | --- | --- | --- | | Plain message, auth pass | 0 | ham | No content or auth signals | | Crafted cold-pitch + verification copy | 93 | spam | 7 content rules |

Live APO check on one production instance (read-only, 2026-11-06, Asia/Jerusalem), Worker on main 8ccceaf with SPAM_FILTER=tag and SPAM_LEARN=on:

| Probe | Score | Verdict | Reasons | Received (Asia/Jerusalem) | | --- | --- | --- | --- | --- | | Ham | 0 | ham | (none) | 12:48:19 | | Spam | 93 | spam | same 7 content rules | 14:27:57 |

Tag mode kept both messages in the inbox; the spam probe received the spam label. No message content or addresses are included here.

Why this package exists

Worker-side research (2026-11-06) found no drop-in replacement for authenticated cold B2B pitches:

| Option | Finding | | --- | --- | | SpamGuard | Bundles for Workers; scored an APO-shaped cold pitch at 0.3 / not spam | | formshield-ai | Aimed at form spam, not mailbox cold pitches | | spamscanner | BUSL-1.1; needs Node built-ins; does not bundle cleanly for Workers | | Bayes libraries (bayes, nbayes, classificator, wink) | Run in a Worker; useful as a learning layer, which this package now includes as pure functions | | SpamAssassin / rspamd hosts | A full engine still needs an external process. A rule subset ships as the optional subpath below |

The core scorer stays the one above. The SpamAssassin subset is a supplementary generic-spam signal with its own entry point. See Optional SpamAssassin rules.

Optional SpamAssassin rules

Import this only when you want it. The package root does not import it, so a core-only bundle stays the same size and the default threshold stays 60.

npm install @agent-post-office/spam-filter [email protected] [email protected]

postal-mime and htmlparser2 are optional peer dependencies, pinned to those exact versions. Installing the package on its own does not install them.

import { scoreMessage } from "@agent-post-office/spam-filter";
import {
  scoreSpamAssassin,
  foldSpamAssassin,
  SPAMASSASSIN_STANDALONE_SPAM_SCORE,
} from "@agent-post-office/spam-filter/spamassassin";

const sa = await scoreSpamAssassin(rawRfc5322);
// sa.score is SpamAssassin's own points. SpamAssassin's own threshold is 5.
// sa.hits is { name, score }[]. sa.skipped is set only when a rule was not run.

const core = scoreMessage({
  from,
  subject,
  text,
  authenticationResults, // host-supplied; this header inside the raw message is not a trust boundary
  recipientDomain,
});

// Defaults: gtubeAlwaysSpam true, standaloneSpamScore 10.
// Below 10, add min(52, round(native)) only when native >= 5. That addition stays under 60.
// At 10 or more, or when GTUBE hits, the verdict is spam even if the core score is under 60.
const blended = foldSpamAssassin(core, sa, {
  threshold: 60,
  gtubeAlwaysSpam: true,
  standaloneSpamScore: SPAMASSASSIN_STANDALONE_SPAM_SCORE,
});

| Native score | Core points added (cap 52) | Verdict when the core score is 0 | | --- | --- | --- | | below 5, or negative | 0 | ham | | 5 up to but not including 10 | round(native) | ham | | 10 or more | min(52, round(native)) | spam | | GTUBE hit (native 1000) | 52 | spam, including when an allow list matched |

gtubeAlwaysSpam defaults to true. standaloneSpamScore defaults to 10. Set standaloneSpamScore to null to keep the numeric fold only. Set gtubeAlwaysSpam to false to let a GTUBE hit follow the numeric rule. A block decision stays as the core scorer made it. An allow decision stays ham unless GTUBE hits. The added reason is spamassassin. Pass the same threshold you used for the core score. Below 10, that threshold is what turns the summed score into a verdict. At 10 or above, the verdict is spam even when the summed score is under the threshold. The reported score is still the capped sum, so a native 10 on a core score of 0 is score 10 and verdict spam.

scoreSpamAssassin accepts a string, ArrayBuffer, or Uint8Array of the raw RFC5322 message.

| Option | Effect | | --- | --- | | knownHeaders | Rules for any other header are skipped, so a missing header is not scored as absent. ALL and full-message rules are skipped too. | | incompleteBody | Body, rawbody, uri, and full rules are skipped. Header rules still run. A meta rule runs only when every dependency ran. |

Skipped rule names come back on skipped. They are omitted from the score.

What this layer covers

The rule table is generated from SpamAssassin update channel r1939780 and checked in. npm run sa:verify downloads https://sa-update.spamassassin.org/1939780.tar.gz, checks sha256 746079a187eefcee1806d077f54f1c0c0f8cb7ac39ce4f8cd94e49ec7a6ca04a, recompiles, and diffs src/spamassassin/generated.ts. npm run sa:verify -- --write regenerates that file. The script needs tar and network. The scorer itself never fetches rules. npm test does not download the snapshot. GitHub Actions runs that check weekly and on workflow_dispatch (.github/workflows/sa-verify.yml). The workflow has permissions: contents: read and no secrets. It fails when the snapshot hash or the regenerated table differs. Pull-request CI does not download the snapshot.

if version lines are evaluated as SpamAssassin 4.000001. Changing that version changes which rules are selected.

1536 rules are in the table. 322 carry a non-zero score. 100 have score 0 and do not match. The standard GTUBE string scores at least 1000 native points.

On a fresh run of the public corpus (sha256 checked, messages not stored in this repository). Flagged means a GTUBE hit or a native score of 10 or more, which is what the default fold marks spam on its own:

| Corpus | Messages scored | Native score ≥ 5 | Flagged | | --- | --- | --- | --- | | 20050311_spam_2 | 1396 | 205 | 26 | | 20030228_easy_ham | 2500 | 8 | 0 |

Those archives stay a pinned download. Their redistribution terms are unclear, and they contain other people's mail, so they are not committed. npm run eval:public downloads them, checks sha256, scores them, and fails if those counts change. npm run eval:public -- --list also prints the filename and hit names for each flagged message. It does not print message text. The cache is vendor/spamassassin/corpus/, which is gitignored.

| Corpus | Archive | sha256 | | --- | --- | --- | | easy_ham | https://spamassassin.apache.org/old/publiccorpus/20030228_easy_ham.tar.bz2 | 2b7b65904bcfcc31d2b5f51946f2d261370b257402cbbd62930b46ab83367438 | | spam_2 | https://spamassassin.apache.org/old/publiccorpus/20050311_spam_2.tar.bz2 | 44280a0e28bf7645b2279e8e42659271ad82c6f40bf8d364dbda7f1df344a765 |

The eight easy-ham messages at or above 5 are upstream hits, and each of them scores under 10, so the default fold leaves them ham. The headers and body patterns those rules name are present in the messages. On the two MIXED_HREF_CASE messages, a line scan and a whole-part scan still show a mixed-case href difference, which is what that rule matches. Rule scores were left as published.

| File | Hits | | --- | --- | | easy_ham/00024.59c2cb781c60594315241e2b50ea70e2 | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE | | easy_ham/00062.009f5a1a8fa88f0b38299ad01562bb37 | AXB_XMAILER_MIMEOLE_OL_024C2, MIXED_HREF_CASE, MSOE_MID_WRONG_CASE | | easy_ham/00115.d5db4a9d477aa17a19669e3945b7aedb | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE | | easy_ham/00116.22aef63fc606e0ad46b5593bc897469a | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE | | easy_ham/00118.f15bf997342540b404a4672c47d57d55 | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE | | easy_ham/00123.91b85aaea560d92f9199f9ba74e77918 | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE | | easy_ham/00166.8feace9f17d092d9532e62c35c37ce95 | DRUGS_ANXIETY, FSL_HAS_TINYURL, MIXED_HREF_CASE | | easy_ham/00225.13c1eaece69dd93afacadb48189e65fc | MAILING_LIST_MULTI, AXB_XMAILER_MIMEOLE_OL_024C2, HK_RANDOM_REPLYTO, MSOE_MID_WRONG_CASE |

Median time on that spam set was about 3.4 ms in this environment. The slowest easy-ham message was about 203 ms. An earlier development pass also found no hits on Agent Post Office cold-pitch samples. Those samples are not in this repository.

Minified browser bundles from esbuild (legal comments stripped):

| Entry | Raw | Gzip | | --- | --- | --- | | @agent-post-office/spam-filter | 10,882 bytes | 4,336 bytes | | @agent-post-office/spam-filter/spamassassin | 679,670 bytes | 177,627 bytes |

The add-on figure includes postal-mime and htmlparser2. The core bundle contains neither, and it contains none of the rule table.

Limitations

  • This is a rule-only subset. Eval plugins (401 eval: declarations in the snapshot), DNS tests (uridnssub, urirhssub, askdns, and tflags net), Bayes (tflags learn), mimeheader directives, and trust-path headers (X-Spam-Relays-*, ALL-*, EnvelopeFrom) are absent. A meta rule is dropped when any dependency was dropped, so a missing test cannot become a hit. The mimeheader rules (62 directives in this snapshot) are left for a later change.
  • MIME structure and HTML visibility are approximate. postal-mime's public text and html fields synthesize the missing alternative, which would double-count parts. The scorer walks the parser's MIME tree instead: root, childNodes, contentType.parsed.value, and getTextContent. Those fields are not a public API. They were checked against postal-mime 2.7.4. If the shape is missing, scoring throws SpamAssassinError rather than guessing. Upgrading postal-mime means re-checking this walk.
  • JavaScript regular expressions can spend a long time on a hostile pattern. There is no per-rule time limit. Raw input over 2,000,000 bytes throws before parsing. Each text part keeps at most 50,000 visible characters and 500,000 rawbody characters. Paragraphs are capped at 2,048 bytes and rawbody chunks at 4,096.
  • Header rules see the raw message, including any Authentication-Results the sender wrote. Core authentication points still come from the header the host passes to scoreMessage.
  • Native points and the core 0-100 score are different scales. The points cap is 52. A GTUBE hit, or a native score of 10 or more, flags even when that capped sum is under 60. Below 10 the fold cannot flag on its own. The core default threshold stays 60.

See NOTICE. SpamAssassin is a trademark of the Apache Software Foundation. These are modified rule representations translated to JavaScript, not the SpamAssassin engine. No endorsement is implied.

Evaluation fixtures

test/corpus is a labeled set of synthetic messages: 45 total, 25 ham and 20 spam. npm run eval builds the package, scores every fixture with scoreMessage and with scoreSpamAssassin, and applies the default fold. The verdict that must match the label is that folded verdict. The command prints a core-only confusion matrix and a folded confusion matrix. It exits non-zero when a folded verdict disagrees with its label, or when the false-positive or false-negative total differs from expectedCounts in test/corpus/manifest.json. Pull-request CI runs this.

A disagreement is allowed only through expected.verdict plus expected.reason on that fixture. The committed counts are 0 false positives and 2 false negatives:

| Fixture | Why the folded verdict stays ham | | --- | --- | | spam-advance-fee | Advance-fee wording scores 7.797 native points and matches no core rule. Below 10 the fold only adds the rounded points. | | spam-pharmacy | Pharmacy wording scores 9.592 native points and matches no core rule. 9.592 is under 10, so the fold does not flag it on its own. |

spam-gtube is spam at core score 0 because the GTUBE rule hits. ham-allow-overrides-pitch stays ham: an allow decision is left in place when the message has no GTUBE hit.

Folded confusion matrix for this set:

| label \ verdict | ham | spam | | --- | --- | --- | | ham | 25 | 0 | | spam | 2 | 18 |

Do not add real mail. Not from Agent Post Office, not from a mailbox, and not from the SpamAssassin public corpus. Addresses must use example.com, example.org, example.net, or the single-label domain invalid. Names must be placeholders. npm run eval rejects any other domain.

Add a fixture

  1. Add test/corpus/ham/<id>.eml or test/corpus/spam/<id>.eml. Include a normal Date and Message-ID so missing-header rules do not fire by accident.
  2. Add a row to test/corpus/manifest.json with id, label (ham or spam), file, a short reason, source set to synthetic, recipientDomain, and authenticationResults. The runner passes that authentication header to the core scorer. It is the host-supplied header.
  3. Optional lists covers an allow or block decision. Optional expected is only for a verdict that is supposed to disagree with the label. If you add one, update expectedCounts.
  4. Run npm run eval. If the folded verdict disagrees with the label and that is not intentional, change the fixture or the scorer. Do not quiet a surprise by adding expected without a reason you can defend.

Install

npm install @agent-post-office/spam-filter

Or pin a git tag:

"@agent-post-office/spam-filter": "github:Agent-Post-Office/agent-post-office-spam-filter#v0.2.0"

The package prepare script runs tsc so git installs build dist/.

Usage

import {
  scoreSpam,
  scoreMessage,
  parseAuthenticationResults,
  applySpamFilter,
  resolveSpamConfig,
  tokenizeMessage,
  signalProbability,
  bayesContribution,
  spamProbabilityFromStore,
  scoreWithStore,
  type SpamStore,
  CONTENT_RULE_WEIGHTS,
  AUTH_FAILURE_WEIGHTS,
  BAYES_CAP,
  DEFAULT_SPAM_LEARN_MIN,
} from "@agent-post-office/spam-filter";

const assessment = scoreMessage(
  {
    from: message.from,
    subject: message.headers.get("subject"),
    text: plainTextBody,
    authenticationResults: message.headers.get("authentication-results"),
    recipientDomain: "example.com",
  },
  {
    threshold: 60,
    lists: {
      allowSenders: [],
      blockSenders: [],
      allowDomains: [],
      blockDomains: [],
    },
  },
);

const { mode, threshold } = resolveSpamConfig({
  SPAM_FILTER: "tag",
  SPAM_THRESHOLD: "60",
});

const disposition = applySpamFilter(mode, assessment, existingLabels);

API

| Export | Purpose | | --- | --- | | scoreMessage(input, options?) | Score one message. Defaults threshold 60 and empty lists. | | scoreSpam(input) | Same scorer with an explicit SpamScoreInput (includes optional bayes). | | parseAuthenticationResults(header) | Parse Authentication-Results into dkim/spf/dmarc states. | | applySpamFilter(mode, assessment, labels) | Map a verdict to folder + labels. Modes: off, tag, quarantine. | | resolveSpamConfig(env, inbox?) | Resolve mode and threshold from env and per-inbox overrides. | | verdictForScore(score, threshold) | spam when score >= threshold, else ham. | | tokenizeMessage(input) | Tokenize for training and Bayes scoring (same function). | | canonicalTokens / trainingDelta | Training helpers for host mark/backfill paths. | | signalProbability(model, tokens, minMarks) | Posterior or null when under the mark gate. | | classify(tokens, model) | Raw posterior. | | bayesContribution(probability) | Points + bayes_spam_p reason, or nothing below 0.90. | | spamLearnEnabled / spamLearnMin | Parse SPAM_LEARN / SPAM_LEARN_MIN. | | SpamStore / spamProbabilityFromStore / scoreWithStore | Host storage interface and helpers. | | CONTENT_RULE_WEIGHTS / AUTH_FAILURE_WEIGHTS / BAYES_CAP | Weight tables and Bayes cap. | | spamFlagged(disposition) | True when folder is spam or labels include spam. |

Configuration reference

Package options

| Option | Default | Meaning | | --- | --- | --- | | threshold | 60 | Spam when score >= threshold. Passed on scoreSpam / scoreMessage. | | lists.allowSenders | [] | Exact sender addresses forced to ham | | lists.blockSenders | [] | Exact sender addresses forced to spam | | lists.allowDomains | [] | Sender domains (and subdomains) forced to ham | | lists.blockDomains | [] | Sender domains (and subdomains) forced to spam | | bayes.spamProbability | omitted | Optional posterior from the host; omitted/null adds nothing |

resolveSpamConfig reads:

| Input | Default | Meaning | | --- | --- | --- | | env.SPAM_FILTER / per-inbox mode | tag | off, tag, or quarantine | | env.SPAM_THRESHOLD / per-inbox threshold | 60 | Integer 0 to 100 |

How Agent Post Office wires this in

These settings live in the APO Worker host. The package stays storage-free.

| APO setting | Meaning | | --- | --- | | SPAM_FILTER=off | Score may still be stored; disposition stays inbox with no spam label | | SPAM_FILTER=tag | Default. Automatic spam verdict keeps folder=inbox and adds the spam label | | SPAM_FILTER=quarantine | Automatic spam verdict sets folder=spam and adds the spam label | | SPAM_THRESHOLD | Instance-wide threshold override (default 60) | | inbox_spam_settings.threshold | Per-inbox threshold override | | inbox_spam_settings.mode | Per-inbox mode override | | SPAM_LEARN=on | Enables Bayes training and scoring (code default is off) | | SPAM_LEARN_MIN | Minimum operator marks per class before Bayes contributes (default 20) |

Operator marks and lists in APO:

  • REST, MCP, and CLI mark spam / not-spam and manage allow/block lists
  • Mark-spam always moves the message to the spam folder, even when automatic mode is tag
  • Automatic tag mode only affects inbound scoring disposition (inbox + label), not the explicit mark-spam action
  • Backfill: POST /v1/inboxes/{id}/spam-training, MCP backfill_spam_training, and CLI. Default page size 25, maximum 100. Idempotent; operator marks only

What stays in the host app

  • Storage for messages, folders, sender lists, token counts, and class counts (SpamStore)
  • Mark spam / not-spam tools and backfill
  • Queue parse wiring and MIME parsing for the core scorer
  • Whether to call @agent-post-office/spam-filter/spamassassin, and whether to fold or branch on its native score

License

Apache-2.0. See LICENSE and NOTICE.