Nightly categorization of new supplier products
Node · Node · intermediate · modification
Adds the nightly product categorization job. `src/catalog/categorize.js` holds the system prompt (it lists the taxonomy slugs), builds the per-product prompt from the title and description, and maps the model's answer to a known slug or `null`; `test/catalog/categorize.test.js` covers those three helpers. `src/jobs/categorizeProducts.js` reads up to 200 pending products, asks the model for each one through the shared `complete` helper with a 16-token cap, saves the result with the model id and a timestamp, and leaves a product pending when its call fails. `src/llm/models.js` gains a `categorize` entry with the validated snapshot, and `src/jobs/index.js` schedules the job for 02:30 UTC.
Shelfwise, an Express 4 back office for a home-goods store, on Node 22 in CommonJS JavaScript; `@anthropic-ai/sdk` 1.4.0. Scheduled jobs run in a separate worker process (`src/worker.js`, unchanged), which calls `registerJobs(scheduler)`; the existing scheduler takes 5-field cron expressions in UTC and never starts a job while its previous run is still going. `src/llm/client.js` (unchanged) builds the one `Anthropic` client. `src/llm/complete.js` (unchanged) exports `complete({ system, user, model = MODELS.default, maxTokens = 256 })`, which calls `anthropic.messages.create` with that `model`, `system`, `max_tokens: maxTokens`, `temperature: 0` and a single user message, and returns the text of the reply's text blocks joined (`''` when there is none); it does not catch errors. `src/repos/productRepo.js` (MongoDB driver 6.x, unchanged): `findPending(limit)` returns up to `limit` products without a `categorization` field, oldest first, as `{ _id, title, description }`; `saveCategorization(id, { category, model, categorizedAt })` sets `categorization` only if the product still has none and returns whether it did; nothing but this job sets `categorization` on a pending product. Products come from contracted suppliers through the existing feed importer, which enforces a `title` of 1 to 200 characters and a `description` of 0 to 2,000 characters; a misplaced product is corrected by hand in the admin. `src/catalog/taxonomy.js` (unchanged) exports `CATEGORIES`, the store's slugs (lower-case letters and hyphens), among them `kitchen-cookware` and `garden-tools`. `src/logger.js` and `src/jobs/purgeSessions.js` are unchanged. Anthropic's model policy ("Model IDs and versioning" docs page): models before the 4.6 generation carry a date in the ID, and a dated model ID such as `claude-haiku-4-5-20251001` is a pinned version whose underlying model stays constant for the lifetime of the ID; on the Claude API these models also have a shorter alias (`claude-haiku-4-5`) that points to the most recent dated snapshot for that minor version, and that guarantee does not cover the alias. From the 4.6 generation on, a dateless ID such as `claude-sonnet-5-5` is itself the pinned snapshot.
Requirements
- A new nightly job, `categorize-products`, runs at 02:30 UTC and asks the model to place each pending product (one that has no `categorization` yet) in one of the store's taxonomy categories, up to 200 products per run, oldest first. The model answers with a slug; an answer that is a known slug once trimmed and lower-cased is saved as the product's `category`, and any other answer is saved as `category: null`, which the merchandisers place by hand from the existing "Uncategorized" admin filter.
- A model call that throws is logged with the product id, and that product stays pending for the next run; the job carries on with the other products. The job returns and logs the counts `placed`, `unplaced` (saved with `null`) and `failed`.
- Categorization runs on `claude-haiku-4-5-20251001`, the dated snapshot the merchandising team validated against its labelled sample of 500 products (ADR-0007). The weekly category report compares counts week over week, so every model request this job makes uses that snapshot, a model change is a reviewed diff followed by the same validation, and each saved categorization records the snapshot under `model`.
- Unchanged: the admin title suggestions keep `MODELS.default`, the dateless alias `claude-haiku-4-5`, on purpose (they are meant to follow Anthropic's newest Haiku 4.5 snapshot), and the merchandiser assistant keeps `MODELS.assistant`.
Files touched
- src/llm/models.js
- src/catalog/categorize.js
- src/jobs/categorizeProducts.js
- src/jobs/index.js
- test/catalog/categorize.test.js