fix(tokenize): drop Limit=1 in customer resolution scan

DynamoDB's `Limit` caps items scanned *before* the FilterExpression runs, so
`scan(FilterExpression=name==X, Limit=1)` returns nothing when the first row
scanned isn't the match. Worked with one seeded customer; adding a second
(cust-0002) made 王小明 fail to resolve to a customer_id, so the RAG tool
returned "token did not resolve" and the agent replied "no customer found".
Remove the Limit; the small table is filtered in full.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 18:38:00 +10:00
parent 2443e0e6f3
commit b46819163b

View File

@@ -89,14 +89,17 @@ def _analyze(text, language="zh"):
def _resolve_customer_id(name):
# Demo-grade name -> customer_id resolution (one seeded customer, so a scan is
# Demo-grade name -> customer_id resolution (small table, so a filtered scan is
# fine). Best effort: if it fails we still tokenize, just without a RAG link.
# NOTE: no `Limit` -- in DynamoDB, Limit caps items *scanned* before the filter
# runs, so `Limit=1` returns nothing when the first row scanned isn't the match
# (broke once a 2nd customer existed). A GSI on `name` would be the prod fix.
if not CUSTOMERS:
return None
try:
from boto3.dynamodb.conditions import Attr
res = CUSTOMERS.scan(FilterExpression=Attr("name").eq(name), Limit=1)
res = CUSTOMERS.scan(FilterExpression=Attr("name").eq(name))
items = res.get("Items", [])
return items[0]["customer_id"] if items else None
except Exception: