feat(strategy-state): persist instance-scoped slot maps
This commit is contained in:
@@ -207,7 +207,7 @@ def make_slots_rpc_handler(
|
||||
):
|
||||
"""Build the RPC handler for `trex.slots.req` (REQ-073/074/116).
|
||||
|
||||
Request: {filter?: str, spy?: bool, strategy?: str}
|
||||
Request: {filter?: str, spy?: bool, strategy?: str, strategy_instance_id?: str}
|
||||
filter: "all"|"inactive"|"pending"|"holding"|"error" (default "all")
|
||||
spy=true: watchdog not updated (operator view, REQ-034 CHG-01)
|
||||
strategy: exact-match on strategy_name (REQ-116, optional). When specified
|
||||
@@ -224,6 +224,15 @@ def make_slots_rpc_handler(
|
||||
body = req.payload or {}
|
||||
filter_val = str(body.get("filter") or "all").lower()
|
||||
spy = bool(body.get("spy", False))
|
||||
strategy_instance_id = body.get("strategy_instance_id")
|
||||
if not spy and (not isinstance(strategy_instance_id, str) or not strategy_instance_id):
|
||||
return RpcResponse(payload={
|
||||
"slots": [],
|
||||
"available_slots": None,
|
||||
"degraded": False,
|
||||
"reason": "bad_request:strategy_instance_id",
|
||||
"message": "strategy_instance_id must be a non-empty string",
|
||||
})
|
||||
# REQ-116 (BAT-055): optional strategy filter — exact-match on strategy_name.
|
||||
# None means no filter (all slots returned regardless of strategy_name).
|
||||
strategy_filter = _opt_str(body.get("strategy"))
|
||||
@@ -290,24 +299,29 @@ def make_slots_rpc_handler(
|
||||
row["qty"] = None
|
||||
row["qty_source"] = "unavailable"
|
||||
|
||||
# dcy-028 / REQ-073/074·079 (C-1): attach opaque strategy_state map per
|
||||
# row via one batched store read (read piggyback — no extra round trip).
|
||||
# Only entries matching the slot's current occupancy_seq are surfaced.
|
||||
# Strategy requests receive only their exact entry; operator requests
|
||||
# receive the complete slot map without changing watchdog behavior.
|
||||
if strategy_state_store is not None and slots:
|
||||
slot_ids = [s.slot_id for s in slots]
|
||||
occ = {s.slot_id: getattr(s, "occupancy_seq", 0) for s in slots}
|
||||
state_map = strategy_state_store.get_for_slots(slot_ids, occ)
|
||||
state_map = strategy_state_store.get_for_slots(slot_ids)
|
||||
for row, s in zip(slot_rows, slots, strict=True):
|
||||
entries = state_map.get(s.slot_id)
|
||||
if entries:
|
||||
row["strategy_state"] = {
|
||||
strat: {
|
||||
"version": e.version,
|
||||
"data": e.data,
|
||||
"updated_at": e.updated_at.isoformat(),
|
||||
if spy:
|
||||
row["strategy_state"] = {
|
||||
instance_id: {
|
||||
"version": e.version,
|
||||
"data": e.data,
|
||||
"updated_at": e.updated_at.isoformat(),
|
||||
}
|
||||
for instance_id, e in entries.items()
|
||||
}
|
||||
elif (entry := entries.get(strategy_instance_id)) is not None:
|
||||
row["strategy_state"] = {
|
||||
"version": entry.version,
|
||||
"data": entry.data,
|
||||
"updated_at": entry.updated_at.isoformat(),
|
||||
}
|
||||
for strat, e in entries.items()
|
||||
}
|
||||
|
||||
# dcy-018 / REQ-029: cash-estimated overlay (best-effort)
|
||||
if ledger_outbox is not None:
|
||||
@@ -406,15 +420,12 @@ def make_strategy_state_set_handler(
|
||||
):
|
||||
"""Build the RPC handler for `trex.slots.strategy_state.set` (REQ-080).
|
||||
|
||||
Request: {slot_id, strategy_id, expected_version:int,
|
||||
expected_occupancy_seq:int, data:str}
|
||||
Response (ok): {ok:true, slot_id, strategy_id, version:int}
|
||||
Response (fail): {ok:false, slot_id, strategy_id, reason, version?|occupancy_seq?}
|
||||
Request: {slot_id, strategy_instance_id, expected_version:int, data:str}
|
||||
Response (ok): {ok:true, slot_id, strategy_instance_id, version:int}
|
||||
Response (fail): {ok:false, slot_id, strategy_instance_id, reason, version?}
|
||||
|
||||
reason ∈ {occupancy_changed, version_conflict, slot_inactive,
|
||||
state_too_large, bad_request:<detail>}. The store owns the CAS
|
||||
(REQ-079); the handler supplies the slot's current occupancy_seq and
|
||||
active-ness and never interprets `data` (opaque).
|
||||
The store owns entry-version CAS; the handler supplies active-ness and never
|
||||
interprets opaque data.
|
||||
"""
|
||||
_ACTIVE_STATES = {"pending_buy", "holding", "pending_sell"}
|
||||
|
||||
@@ -422,9 +433,10 @@ def make_strategy_state_set_handler(
|
||||
body = req.payload or {}
|
||||
try:
|
||||
slot_id = UUID(str(body["slot_id"]))
|
||||
strategy_id = str(body["strategy_id"])
|
||||
strategy_instance_id = body["strategy_instance_id"]
|
||||
if not isinstance(strategy_instance_id, str) or not strategy_instance_id:
|
||||
raise ValueError("strategy_instance_id must be a non-empty string")
|
||||
expected_version = int(body["expected_version"])
|
||||
expected_occupancy_seq = int(body["expected_occupancy_seq"])
|
||||
data = body["data"]
|
||||
if not isinstance(data, str):
|
||||
raise ValueError("data must be a string")
|
||||
@@ -432,27 +444,23 @@ def make_strategy_state_set_handler(
|
||||
return RpcResponse(payload={
|
||||
"ok": False,
|
||||
"slot_id": body.get("slot_id"),
|
||||
"strategy_id": body.get("strategy_id"),
|
||||
"strategy_instance_id": body.get("strategy_instance_id"),
|
||||
"reason": f"bad_request:{exc}",
|
||||
})
|
||||
|
||||
slot = slot_repo.fetch_slot(slot_id)
|
||||
slot_active = slot is not None and slot.status.value in _ACTIVE_STATES
|
||||
current_occupancy_seq = getattr(slot, "occupancy_seq", 0) if slot else 0
|
||||
|
||||
result = strategy_state_store.set(
|
||||
slot_id,
|
||||
strategy_id,
|
||||
expected_occupancy_seq=expected_occupancy_seq,
|
||||
strategy_instance_id,
|
||||
expected_version=expected_version,
|
||||
data=data,
|
||||
current_occupancy_seq=current_occupancy_seq,
|
||||
slot_active=slot_active,
|
||||
)
|
||||
payload = {
|
||||
"ok": result.ok,
|
||||
"slot_id": str(slot_id),
|
||||
"strategy_id": strategy_id,
|
||||
"strategy_instance_id": strategy_instance_id,
|
||||
}
|
||||
if result.ok:
|
||||
payload["version"] = result.version
|
||||
@@ -460,8 +468,6 @@ def make_strategy_state_set_handler(
|
||||
payload["reason"] = result.reason
|
||||
if result.version is not None:
|
||||
payload["version"] = result.version
|
||||
if result.occupancy_seq is not None:
|
||||
payload["occupancy_seq"] = result.occupancy_seq
|
||||
return RpcResponse(payload=payload)
|
||||
|
||||
return handler
|
||||
|
||||
Reference in New Issue
Block a user