fix: assign an id when creating a product or collection - #90
Open
jsnapoli1 wants to merge 1 commit into
Open
Conversation
createProduct keyed on `product.id`, but no caller supplies one — not the admin UI, not the agent's create_product tool. So every create wrote to the literal key `product:undefined` and pushed `undefined` into products:all. The request still returned 201, with a body that had no id, and the product was invisible everywhere afterwards. Via the agent this is especially confusing: the tool sees a 2xx and truthfully reports "Created product", while the products tab stays empty. Observed on a live deployment: three creates left a single `product:undefined` key and `products:all` = [null,null,null]. createCollection had the identical defect. Both now generate a UUID when no id is supplied, and filter nulls out of the index on write so stores that already ran the broken path repair themselves on the next create rather than needing manual KV surgery. The `uuid` package is a declared dependency that nothing imports, which suggests id generation was lost in a refactor. This uses crypto.randomUUID instead, which is available in Workers and needs no dependency. Adds tests/integration/product-id.test.js (5 cases). All five fail against the current code and pass with this change. Claude-Session: https://claude.ai/code/session_015XLFFfHWNeazSuz6UcsQ4C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Creating a product silently does nothing.
POST /api/admin/productsreturns201, but the product never appears in the products tab, the storefront, or any listing.KVManager.createProductkeys onproduct.id— but no caller supplies one. Not the admin UI, not the agent'screate_producttool. So every create writes to the literal keyproduct:undefinedand pushesundefinedintoproducts:all.Observed on a live deployment after three create attempts:
The response body confirms it —
201, and noidfield:{"name":"Agent Flow Probe","price":20,"currency":"USD", "stripePriceId":"price_unlinked","stripeProductId":"prod_unlinked", ...}Via the agent this is particularly bad.
dispatchsees a 2xx andsummarizeResulttruthfully reportsCreated product "X" (undefined)— so the agent tells the user it worked while the products tab stays empty. That's how I found this: the agent claimed success and nothing appeared.createCollectionhas the identical defect.Fix
Generate a UUID when no id is supplied, and filter nulls out of the index on write so stores that already ran the broken path repair themselves on the next create instead of needing manual KV surgery.
crypto.randomUUID()rather than theuuidpackage — it's available in Workers and needs no dependency. Worth notinguuidis declared inpackage.jsonbut imported nowhere insrc/, which suggests id generation was lost in a refactor rather than never written.Verification
Adds
tests/integration/product-id.test.js(5 cases): id returned on create, created product appears in the listing, noproduct:undefinedkey is written, existing nulls are cleaned from the index, and collections get ids too.All five fail against current
mainand pass with this change:Full suite: 283 passed (24 files).
npm run lint— 0 errors.Note for existing stores
Any store that created products before this fix has a
products:allcontaining nulls and a staleproduct:undefinedkey. The null-filtering makes the index self-heal on the next create; the orphanedproduct:undefinedkey is harmless but can be deleted:Products created before the fix are not recoverable — only the last one was ever stored, and without an id it can't be indexed.
https://claude.ai/code/session_015XLFFfHWNeazSuz6UcsQ4C