Skip to content

feat: multi-store intelligence with adapters, decomposition and read-only pricing - #20

Merged
Akaliup merged 6 commits into
masterfrom
feat/inventory-pricing-decomposition
Nov 14, 2026
Merged

Akaliup merged 6 commits into
masterfrom
feat/inventory-pricing-decomposition

Conversation

@Akaliup

@Akaliup Akaliup commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

概要

  • 新增 Amazon / Shopify 风格 CSV 适配器目录(订单、广告、库存),preview/commit 接口接受 adapter 参数,批次记录适配器 ID;原有校验、原子导入与幂等逻辑不变
  • SKU 唯一性改为租户内唯一,新增迁移 0006_tenant_scoped_sku,SQLite 重建保留订单关联,跨租户重名时拒绝回退
  • 复用既有八项利润贡献桥,新增确定性量价结构拆解(volume / price / mix / residual,整数美分闭合到销售额变化)
  • 新增只读调价建议,输出 rule_version 与 INSUFFICIENT_DATA 分支,不执行改价
  • MCP 只读允许列表新增 get_saved_weekly_report,按当前服务租户过滤,其他租户报告拒绝返回
  • 前端导入中心支持适配器选择,周报页展示拆解证据与调价建议;新增真实流程截图 E2E
  • 同步 docs/CONTRACT.md、frontend/src/types.ts 与测试;补充平台 CSV 样例、六张截图与交付记录

迁移

backend/migrations/versions/0006_tenant_scoped_sku.py,接在 0005_weekly_chat 后。

  • SQLite:重建 products,移除旧全局唯一约束,保留数据与子表外键
  • PostgreSQL:使用约束变更路径
  • 回退前检查跨租户重名,存在重复则拒绝,不删数据
  • 未对运行库执行迁移;PostgreSQL 路径未验证

实际验证

在最终代码上实际执行:

  • .venv/Scripts/python.exe -m pytest backend/tests -q → 348 passed, 6 skipped(跳过项为未配置 PROFITOPS_TEST_POSTGRES_URL 的 PostgreSQL 集成测试)
  • npm --prefix frontend test → 24 passed
  • npm --prefix frontend run build → 通过(仅 chunk 超过 500 kB 警告)
  • .venv/Scripts/python.exe scripts/mcp_check.py → 通过,七个只读工具可用,写入与问答工具未暴露,未配置模型
  • .venv/Scripts/python.exe scripts/smoke.py --base-url http://127.0.0.1:8023 → 21/21;临时 SQLite、LLM_PROVIDER=disabled、hashing embedding,启动目录不含本地 .env
  • Chromium 截图验收(本轮早前执行)→ 1 passed,真实 HTTP 导入、周报生成与追问降级,无响应拦截
  • python -m pytest backend/tests/test_migrations.py ... 相关迁移用例包含在完整后端结果内

说明:本地未跟踪的 .env 设置了 KNOWLEDGE_MIN_SCORE=0.42,会让离线 hashing embedding 的冒烟检索低于阈值;冒烟改用不含 .env 的工作目录启动,代码与依赖不变。

未验证

  • 真实 DeepSeek 回答与引用高亮(本轮未调用付费模型)
  • PostgreSQL 迁移与集成测试(6 项 skipped)
  • 容器与公网部署
  • 运行库迁移(仅使用临时测试库)

边界

  • 全部执行为 simulation,无真实广告、采购、支付或改价副作用
  • 契约变更已同步后端测试、frontend/src/types.ts 与 docs/CONTRACT.md
  • 未提交 .env、API Key、数据库、缓存或构建产物;plans/ 未纳入版本

概要

  • 新增三个离线 CSV 适配器:amazon-orders、shopify-orders、amazon-ads。没有库存适配器。preview/commit 接受 adapter,服务端再次校验;非法 JSON、未知 adapter、kind 不匹配返回 422,整批不入账。不连接卖家后台
  • SKU 唯一性改为租户内唯一,迁移 0006_tenant_scoped_sku
  • 八项利润桥保留。销售额变化按整数美分拆成 volume / price / mix / residual,并闭合到 sales_change。新建或下架 SKU 不估算缺失 ASP,其销售额变化进入销量项后再拆结构效应
  • 只读调价建议,规则版本 pricing-rules-v1。可变成本为 0 且不能证明成本已导入时不给建议价
  • MCP 只读增加 get_saved_weekly_report,按当前服务租户过滤。没有新增周报快照缓存;提交 d104329 说明里的 “cache weekly snapshots by id” 与代码不符
  • 官方与适配器订单样例包含 2020-01-01 至 2020-01-05 的合成上一周订单。end_date=2020-01-12 的量价拆解为确定整数美分,不是已验证的经营因果
  • python scripts/dev.py --demo-db 只使用 data/demo-copy.db 并打印 URL,不迁移 data/profitops.db。不带该参数时仍使用 Settings / .env

迁移

backend/migrations/versions/0006_tenant_scoped_sku.py,接在 0005_weekly_chat 后。

Alembic URL 优先级:-x url= / -x database_url=,然后本次 Config 里非占位的 sqlalchemy.url,然后 PROFITOPS_DATABASE_URL / ALEMBIC_DATABASE_URL,最后才是 Settings().database_url。

  • 未对运行库执行迁移;PostgreSQL 路径未验证

实际验证

在本轮代码上实际执行(Linux,仓库内 .venv,临时 SQLite):

  • PYTHONPATH=backend .venv/bin/python -m pytest backend/tests -q → 360 passed, 6 skipped(跳过项为未配置 PROFITOPS_TEST_POSTGRES_URL 的 PostgreSQL 集成测试)
  • npm --prefix frontend test -- --run → 27 passed
  • npm --prefix frontend run build → 通过(仅 chunk 超过 500 kB 警告)
  • PYTHONPATH=backend .venv/bin/python scripts/mcp_check.py → 通过,七个只读工具可用,写入与问答工具为 unknown_tool,llm_configured=false

未验证

  • 真实 DeepSeek 回答与引用高亮(未调用付费模型)
  • PostgreSQL 迁移与集成测试(6 项 skipped)
  • 容器与公网部署
  • 运行库迁移(测试未创建或打开 data/profitops.db)
  • 浏览器重截周报截图。既有 docs/screenshots/weekly-decomposition.png 仍是上期销量为零时的画面,利润环比会与补上上一周订单后的页面不一致;当期销售额 US$160.01 仍与样例当期订单一致

边界

  • 数字由确定性代码以整数美分计算;LLM 只解释并引用 evidence id
  • 全部执行为 simulation,无真实广告、采购、支付或改价
  • 归因销售不计入利润桥
  • 未提交 .env、API Key、数据库、缓存、构建产物或 plans/
  • 本轮修复由编码 Agent 实现,需人工审查后再合并。不要在审查通过前合并
Open in Web Open in Cursor 

lz and others added 6 commits November 14, 2026 14:38
Add Amazon/Shopify style CSV presets for orders, ads and inventory, exposed through a read-only adapter catalog and accepted by preview/commit. Import batches record the adapter id; validation, atomic ingest and idempotency stay unchanged.

Scope product SKU uniqueness to tenant so the same SKU can exist in different tenants; migration 0006 rebuilds SQLite products, preserves order references and refuses unsafe rollback.
…aved weekly tool

Extend the existing profit bridge with a deterministic volume/price/mix decomposition that closes to the sales delta in integer cents, and add bounded price suggestions marked as read-only recommendations.

Expose get_saved_weekly_report through the read-only MCP allowlist with tenant filtering, cache weekly snapshots by id, and keep chat answers gated by numeric and evidence checks.
Add adapter selection to the import center, show volume/price/mix evidence and read-only price suggestions on the intelligence page, and sync API types with the backend contract.

Add a Playwright screenshot spec that drives the real import, weekly and chat flows without response interception.
Document the adapter catalog, tenant-scoped SKU migration, decomposition evidence and saved weekly MCP tool. Add Amazon/Shopify CSV samples, six browser screenshots and the final verification record.
Weekly chat now treats asp, suggested price, current price, attributed sales, and sales change as cents, while counts and percentages stay unconverted. Alembic uses -x, then an explicit sqlalchemy.url, before the process environment. Sample orders include a prior synthetic week so the 2020-01-12 report has a closed volume, price, and mix split, including new and discontinued SKUs. Price suggestions abstain when cost is unproven, and --demo-db starts data/demo-copy.db without migrating the runtime database.

Co-authored-by: Akaliup <Akaliup@users.noreply.github.com>
The five-minute path uses python scripts/dev.py --demo-db. The docs describe the sales decomposition, read-only pricing rules, the three offline adapters, and the new citable ids. They no longer say the sample has no prior-week sales or that MCP tests are a fixed count of 9.

Co-authored-by: Akaliup <Akaliup@users.noreply.github.com>
@Akaliup
Akaliup merged commit 25ef911 into master Nov 14, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants