Spaces:
Sleeping
Download README.md from Code-Father/store: direct link, hf CLI and curl.
- Browser
- Download file 20.6 kB
-
https://huggingface.co/spaces/Code-Father/store/resolve/main/README.md
- Command line
-
hf download hf://spaces/Code-Father/store/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Code-Father/store/resolve/main/README.md
title: Store Environment Server
emoji: 🛒
colorFrom: green
colorTo: yellow
sdk: docker
pinned: false
app_port: 8000
base_path: /web
tags:
- openenv
Store Environment
This repository now contains a CSV-backed neighborhood grocery simulation for OpenEnv. The environment is intentionally rooted in a kirana-store business loop: manage cash, stock, supplier orders, customer satisfaction, pricing, and durable operations upgrades over repeated decision windows.
This README documents the current implementation, not the older echo-demo scaffold and not the earlier narrative plans. The runtime source of truth is:
store/data/*.csvstore/models.pystore/server/store_environment.pystore/client.pystore/tests/*.py
If a note in plan.md, plan2.md, or rules.md conflicts with the code or the
seed CSVs, this document follows the code and the current CSVs.
At A Glance
- Episode horizon:
30simulated days by default - Decision window:
3committed days per step - Reward lookahead:
6simulated days per step - Action shape: one bundled action containing
orders,prices, andaction_ids - Observation shape: full store snapshot plus inventory, pending orders, market catalog, seller profiles, marketing catalog, and an action guide
- State storage: in-memory per environment instance, seeded from CSV files on
reset() - Reward form:
0.5 + normalized_difference
Seed Data
The environment seeds itself from the CSV files in store/data/.
Current seed snapshot
store_state.csv: 1 rowstock.csv: 30 rowsmarket_offers.csv: 100 rowsseller_profiles.csv: 4 rowspending_orders.csv: 0 rowsmarketing_actions.csv: 15 rows
The opening store snapshot is currently:
| Field | Value |
|---|---|
day |
1 |
current_daily_expense_inr |
3000 |
bank_balance_inr |
120000 |
customer_satisfaction |
70 |
order_processing_capacity |
500 |
cumulative_profit_inr |
0 |
The current opening stock value is 237,192 INR, which sits inside the intended
220k-260k INR band.
Files And Roles
| File | Role |
|---|---|
store/models.py |
Action, observation, and data models |
store/server/store_environment.py |
Core simulation engine |
store/client.py |
Client wrapper for remote HTTP/WebSocket usage |
store/server/app.py |
FastAPI app factory entrypoint |
store/data/*.csv |
Seed data and catalog source of truth |
store/tests/test_seed_csvs.py |
Seed integrity checks |
store/tests/test_store_environment.py |
Behavioral simulation tests |
Action Contract
The agent sends one bundled action per decision window.
from store import OrderRequest, PriceUpdate, StoreAction
action = StoreAction(
orders=[
OrderRequest(product="Rice", seller="Seller A", quantity=25),
OrderRequest(product="Bread", seller="Seller C", quantity=12),
],
prices=[PriceUpdate(product="Milk", price=33)],
action_ids=[7],
)
Passing is:
StoreAction()
Action fields
| Field | Type | Meaning |
|---|---|---|
orders |
list[OrderRequest] |
Supplier orders to place at the start of the step |
prices |
list[PriceUpdate] | None |
Optional selling-price updates for stocked products |
action_ids |
list[int] |
Durable marketing or operations actions to activate |
Nested action models
| Model | Fields |
|---|---|
OrderRequest |
product, seller, quantity |
PriceUpdate |
product, price |
Validation rules for actions
quantitymust be a positive integer.sellermust be one ofSeller A,Seller B,Seller C, orSeller D.orders[*].productmust exist inmarket_offers.csv.prices[*].productmust already exist in current stock.prices[*].pricemust be positive.action_idsmust exist inmarketing_actions.csv.- Duplicate price updates for the same product are rejected.
- Duplicate marketing action IDs in one step are rejected.
- Already-active marketing actions cannot be activated again.
Observation Contract
StoreObservation is the agent-facing state. The OpenEnv transport State
still only carries episode_id and step_count, but the business state lives in
the observation payload.
Top-level observation fields
| Field | Meaning |
|---|---|
store |
Current top-level business snapshot |
stock |
Current in-store inventory rows |
pending_orders |
In-flight supplier orders |
market |
Full supplier catalog with prices and ETAs |
seller_profiles |
Seller behavior notes |
marketing_actions |
Full action catalog plus active flags |
available_action |
Agent guide showing the action shape |
summary |
Human-readable step summary |
reward |
Reward returned for the step |
done |
Whether the episode has terminated |
metadata |
Reward breakdown and simulation diagnostics |
Store snapshot fields
store is a StoreSnapshot with:
daycurrent_daily_expense_inrbank_balance_inrcustomer_satisfactionorder_processing_capacitycumulative_profit_inractive_action_idsactive_recurring_cost_inrepisode_horizon_dayscommit_window_daysreward_horizon_days
Available action guide
Each observation includes an available_action block with:
name: alwaysactiondescription: short instruction about the bundled step contractpass_action: emptyStoreAction()example_action: a concrete non-empty examplenotes: quick reminders about pass behavior, stock-only pricing, and durable marketing actions
Step Lifecycle
One call to step() performs a full decision-window update.
High-level order of operations
- Validate the bundle.
- Read the current live state.
- Compute starting capital.
- Clone the live runtime state into two branches:
- commit branch for the next
3days of live state - reward branch for the
6-day reward forecast
- commit branch for the next
- Apply the same action bundle to both branches once at the start of the window.
- Update customer satisfaction once per decision window.
- Simulate daily operations on both branches.
- Commit only the
3-day branch back to the live environment state. - Compute reward from the
6-day branch. - Increment
step_countand setdoneif the committed state crosses a terminal condition.
What the action does at step start
- Supplier orders are placed immediately and enter the pending queue.
- The store pays the supplier cost immediately when the order is placed.
- Price updates apply immediately to stocked items.
- Marketing actions activate immediately and remain active for the rest of the episode.
- Customer satisfaction is recomputed once for the decision window after the bundle is applied.
What happens during simulated days
For each simulated day:
- Active recurring marketing cost is charged.
- Demand is converted into sales.
- Sales revenue is added to bank balance.
- Daily operating expense is charged.
- COGS is tracked in cumulative profit accounting.
- Pending orders are decremented by one day.
- Arriving orders are received into stock.
- The day counter advances by one.
Simulation Formulas
The formulas below reflect the current implementation in
store/server/store_environment.py.
Starting capital
starting_capital = starting_bank_balance + total_inventory_value
total_inventory_value is the sum of stock_value_inr across current stock
rows.
Effective daily orders
effective_daily_orders = order_processing_capacity * (customer_satisfaction / 100)
This means the current satisfaction score is treated as a percentage scale.
Product margin
margin_percent = ((selling_price_inr - buying_price_inr) / selling_price_inr) * 100
The implementation uses the current selling price, not the buying price, as the denominator.
Product selling score
product_selling_score = max(0, demand_score - margin_percent * 3)
This is computed separately for each stocked item with positive on-hand quantity.
Total selling score
total_product_selling_score = sum(product_selling_score for each stocked item)
Daily sales allocation
desired_sales_for_item = effective_daily_orders * product_selling_score / total_product_selling_score
actual_sales = min(quantity_on_hand, desired_sales_for_item)
Sales are proportional to selling score across stocked items. Because the model
is proportional and not integer-rounded, quantity_on_hand can become
fractional after simulated sales.
Customer satisfaction update
Before the window is simulated, the environment applies any marketing action delays to customer satisfaction. Then it recomputes a score-based multiplier.
adjusted_satisfaction = current_satisfaction + sum(customer_satisfaction_delta for activated actions)
if after_score > 0:
next_satisfaction = (1 + ((after_score - before_score) / after_score)) * adjusted_satisfaction
else:
next_satisfaction = adjusted_satisfaction
next_satisfaction is then clamped to the range [1, 100]
before_score is the total selling score before the bundle is applied.
after_score is the total selling score after price updates and before the
simulated days begin.
Order arrival
When a pending order reaches days_remaining <= 0:
- If the product already exists in stock, the order is merged into that row and the buying price becomes the weighted average of the existing and incoming units.
- If the product does not exist in stock, a new stock row is created.
- Newly created stock uses:
selling_price = max(buying_price + 1, ceil(buying_price * 1.18))
The 1.18 markup is the implementation default for a newly introduced SKU.
Reward
Reward is computed from the 6-day reward branch:
difference =
(ending_inventory_value + ending_bank_balance)
- (starting_inventory_value + starting_bank_balance)
- recurring_costs_paid_over_6_days
normalized_difference = difference / starting_capital
reward = 0.5 + normalized_difference
If starting_capital <= 0, the implementation falls back to
normalized_difference = 0.
Reward metadata
The step response includes reward diagnostics in metadata:
reward_breakdown.starting_capital_inrreward_breakdown.ending_capital_inrreward_breakdown.difference_inrreward_breakdown.normalized_differencereward_breakdown.commit_window_daysreward_breakdown.reward_horizon_daysreward_breakdown.recurring_costs_paid_inrcommit_metricsreward_metricsdone_reason
The code rounds the high-level reward breakdown to two decimals, except for
normalized_difference, which is kept at six decimal places in metadata.
Seller Behavior And ETA Sampling
Supplier ETA is deterministic within an episode because the environment seeds its RNG from the reset count. Different resets get different seeds, but the same episode run is repeatable once started.
Seller profiles
| Seller | Behavior | ETA sampling |
|---|---|---|
| Seller A | Overcommits | quoted + {0, 1, 2} with delay-heavy weights |
| Seller B | Strict | Uses quoted ETA exactly |
| Seller C | Undercommits | quoted + {-2, -1, 0} with early-heavy weights |
| Seller D | Variable | quoted + {-1, 0, 1, 2} with mixed weights |
Actual ETA rules
- The quoted ETA comes from the corresponding seller column in
market_offers.csv. - Actual ETA is sampled once when the order is placed.
- The sampled ETA is floored at
1. - Each simulated day decrements
days_remainingby1. - When
days_remaining <= 0, the order is received.
Exact weights in the current code
- Seller A: offsets
[0, 1, 2]with weights[2, 4, 5] - Seller B: offset
[0] - Seller C: offsets
[-2, -1, 0]with weights[5, 4, 2] - Seller D: offsets
[-1, 0, 1, 2]with weights[2, 4, 3, 2]
These weights are implementation details, not a business guarantee.
Data Contract
store_state.csv
Current columns:
daycurrent_daily_expense_inrbank_balance_inrcustomer_satisfactionorder_processing_capacitycumulative_profit_inr
stock.csv
Current columns:
product_nameunitbuying_price_inrselling_price_inrdemand_scorequantity_on_handstock_value_inr
The seed data includes 30 stocked products and the stored value must equal
buying_price_inr * quantity_on_hand on each row.
market_offers.csv
Current columns:
product_namecategoryunitdemand_scoreseller_a_price_inrseller_a_etd_daysseller_b_price_inrseller_b_etd_daysseller_c_price_inrseller_c_etd_daysseller_d_price_inrseller_d_etd_days
The current catalog contains 100 products.
seller_profiles.csv
Current columns:
seller_nametimeline_behaviorreliability_notedefault_etd_days
pending_orders.csv
Current columns:
order_idproduct_namequantityseller_namebuy_price_inrdays_remainingstatuscreated_day
The seed file starts empty.
marketing_actions.csv
Current columns:
action_iddescriptioncustomer_satisfaction_deltacost_inrrecurring_cost_inrorder_processing_capacity_delta
The current catalog contains 15 durable action rows.
Episode End Conditions
The environment uses a default episode horizon of 30 days, but each step
advances the live state by 3 days, so a full default episode lasts 10
decision windows.
The episode ends when either:
- committed bank balance drops below
0 - committed day exceeds
30
The current implementation checks day > 30 after the commit branch is
applied, so the tenth decision window takes the day counter to 31 and ends
the episode.
Practical Edge Cases
Fractional inventory
Sales are proportional, so on-hand inventory can become fractional. The models use floats for inventory fields for that reason.
Empty sales mix
If all stocked items have zero selling score or zero quantity, the sales loop returns zero revenue and zero COGS for that day.
Zero or negative starting capital
The reward math guards against division by zero by falling back to a zero normalized difference.
Already-active marketing actions
Once a marketing action is active, a later step that tries to re-activate it raises a validation error.
Product pricing
Price updates only apply to products already in stock. If a product is not in stock yet, you must wait for a supplier order to arrive before changing its price.
New stock rows
If a pending order arrives for a product not currently in stock, the environment creates a new stock row on arrival. This is rare in the seeded scenario, but the code supports it.
How To Run
The project is designed to run through uv so the declared dependencies are
available. In this workspace, the global shell does not include openenv-core
or pytest, so prefer the project environment.
Install dependencies
cd store
uv sync
Start the server
cd store
uv run --project . server --port 8000
Start the Gradio playground
cd store
uv run --project . gradio
By default the Gradio UI launches on http://127.0.0.1:7860 and runs the
environment in-process, so you do not need the FastAPI server running to play
with the simulation manually.
Validate the OpenEnv package
cd store
uv run --project . openenv validate .
Run the tests
cd store
uv run --project . python -m pytest
How To Play Manually
You can interact with the simulation directly from Python without starting a network server:
from store import OrderRequest, PriceUpdate, StoreAction
from store.server.store_environment import StoreEnvironment
env = StoreEnvironment()
obs = env.reset()
print(obs.store)
print(obs.summary)
step = env.step(
StoreAction(
orders=[OrderRequest(product="Rice", seller="Seller A", quantity=25)],
prices=[PriceUpdate(product="Milk", price=33)],
action_ids=[7],
)
)
print(step.reward)
print(step.done)
print(step.metadata["reward_breakdown"])
print(step.store.day)
Passing a step
env.step(StoreAction())
Using the Gradio UI
The Gradio frontend lives in store/gradio_app.py and is designed as a local
playground for the same bundled action contract:
Orders JSON: JSON array of{product, seller, quantity}objectsPrice Updates JSON: JSON array of{product, price}objectsAction IDs: either a JSON array like[7, 12], a single integer, or a comma-separated string like7, 12
UI controls:
Reset Episode: creates a freshStoreEnvironmentinstance and resets itSubmit Step: validates the current inputs and executes one stepPass Step: executes a real pass action immediatelyLoad Example: fills the inputs using the observation's bundled example action
The UI displays:
- current store snapshot
- stock table
- pending orders
- market catalog
- marketing action catalog
- reward breakdown
- raw observation metadata
Invalid JSON or action validation errors are shown in the UI while preserving the current episode state so you can correct the inputs and try again.
Inspecting the current state
The most useful fields to inspect after each step are:
obs.storeobs.stockobs.pending_ordersobs.marketing_actionsobs.metadata["reward_breakdown"]obs.metadata["done_reason"]
OpenEnv Client Usage
If you want to connect to a running server, use StoreEnv:
from store import OrderRequest, PriceUpdate, StoreAction, StoreEnv
with StoreEnv(base_url="http://localhost:8000") as client:
reset_result = client.reset()
print(reset_result.observation.store)
step_result = client.step(
StoreAction(
orders=[OrderRequest(product="Rice", seller="Seller A", quantity=10)],
prices=[PriceUpdate(product="Milk", price=33)],
action_ids=[7],
)
)
print(step_result.reward)
print(step_result.observation.summary)
The client serializes the action bundle to JSON and reconstructs typed observations from the server response.
Tests
The current test suite includes:
store/tests/test_seed_csvs.pystore/tests/test_store_environment.py
These tests cover:
- CSV shape and seed integrity
- opening store values
- market catalog completeness
- marketing action rows
- reset behavior
- 3-day commit semantics
- 6-day reward forecasting
- ETA sampling
- customer satisfaction changes
- durable marketing actions
- episode termination
- client serialization and parsing
Useful test command
cd store
uv run --project . python -m pytest store/tests/test_seed_csvs.py store/tests/test_store_environment.py
Known Limitations
- The simulation is deterministic aside from seller ETA sampling, which is seeded per reset.
- Demand is not a full market model; it is based on stock-level selling score proportions.
- There is no spoilage, shrinkage, theft, or seasonality model.
- There is no supplier cancellation workflow.
- Sales can become fractional because the simulation allocates demand proportionally.
- The environment exposes the full market and marketing catalogs every step, so observations are intentionally verbose.
- Reward is unbounded and can be greater than
1.0or less than0.0. plan.mdandplan2.mdare useful design notes, but the code and CSVs are the authoritative implementation.
Assumptions
- The agent-facing state is the observation payload, not the OpenEnv transport
State. - CSV seed files are runtime truth and are re-read on every reset.
- The bundled action is the only supported step input.
- Marketing actions are durable and stay active once activated.
- A 30-day horizon with 3-day windows is the intended default episode shape.
- Current seed values in
store_state.csvare intentional and should not be replaced with the older numbers from the narrative docs unless the code and tests are updated together.
Packaging Notes
The OpenEnv manifest is store/openenv.yaml, and the server entry point is
store.server.app:app with the script entry point server = "store.server.app:main".
The current package also declares:
openenv-core[core]>=0.2.2pydantic>=2.0.0
Further Reading
store/models.pystore/server/store_environment.pystore/tests/test_store_environment.pystore/tests/test_seed_csvs.py