CallMeDaniel Claude Opus 4.6 (1M context) commited on
Commit
83f3ff9
·
1 Parent(s): a9d0aec

refactor: type ContextVar design state as DesignState

Browse files

Change set_design_state() and get_design_state() to use DesignState
objects instead of dicts. Update QueryDesignStateTool._run() to use
the state directly without reconstruction. Update callers in
CrewOrchestrator to pass DesignState objects instead of model_dump().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (2) hide show
  1. agents/crew_orchestrator.py +2 -2
  2. agents/tools.py +9 -9
agents/crew_orchestrator.py CHANGED
@@ -181,7 +181,7 @@ class CrewOrchestrator(BaseOrchestrator):
181
  state = state.update_from_messages([], user_message=message)
182
 
183
  # Expose design state to the orchestrator tool so agents can query it.
184
- set_design_state(state.model_dump())
185
 
186
  context = _build_agent_context(message, state, approved_plan=approved_plan)
187
 
@@ -316,7 +316,7 @@ class CrewOrchestrator(BaseOrchestrator):
316
  # from user message so gap analysis reflects current turn data.
317
  state = DesignState(**(design_state or {}))
318
  state = state.update_from_messages([], user_message=message)
319
- set_design_state(state.model_dump())
320
 
321
  mock = MockChatBackend(output_dir=self.output_dir)
322
  result = mock.chat_turn(message, history, mentions, design_state=state.model_dump(), plan_context=plan_context)
 
181
  state = state.update_from_messages([], user_message=message)
182
 
183
  # Expose design state to the orchestrator tool so agents can query it.
184
+ set_design_state(state)
185
 
186
  context = _build_agent_context(message, state, approved_plan=approved_plan)
187
 
 
316
  # from user message so gap analysis reflects current turn data.
317
  state = DesignState(**(design_state or {}))
318
  state = state.update_from_messages([], user_message=message)
319
+ set_design_state(state)
320
 
321
  mock = MockChatBackend(output_dir=self.output_dir)
322
  result = mock.chat_turn(message, history, mentions, design_state=state.model_dump(), plan_context=plan_context)
agents/tools.py CHANGED
@@ -12,6 +12,8 @@ from typing import Type
12
 
13
  from pydantic import BaseModel, Field
14
 
 
 
15
  logger = logging.getLogger(__name__)
16
 
17
  try:
@@ -27,7 +29,7 @@ except ImportError:
27
  # ── Per-request state (ContextVar — async-safe) ─────────────────────────
28
 
29
  _last_shape_var: ContextVar[object | None] = ContextVar("last_shape", default=None)
30
- _design_state_var: ContextVar[dict | None] = ContextVar("design_state", default=None)
31
 
32
 
33
  def set_last_shape(shape):
@@ -36,10 +38,10 @@ def set_last_shape(shape):
36
  def get_last_shape():
37
  return _last_shape_var.get()
38
 
39
- def set_design_state(state_dict: dict):
40
- _design_state_var.set(state_dict)
41
 
42
- def get_design_state() -> dict | None:
43
  return _design_state_var.get()
44
 
45
 
@@ -115,17 +117,15 @@ class QueryDesignStateTool(BaseTool):
115
  args_schema: Type[BaseModel] = QueryDesignStateInput
116
 
117
  def _run(self, check: str = "all") -> str:
118
- from agents.design_state import DesignState, compute_score
119
  from config.settings import settings
120
 
121
  if check not in VALID_CHECKS:
122
  return json.dumps({"error": f"Invalid check: {check!r}. Valid: {sorted(VALID_CHECKS)}"})
123
 
124
- state_dict = get_design_state()
125
- if state_dict is None:
126
  return json.dumps({"error": "No design state available."})
127
-
128
- state = DesignState(**state_dict)
129
  score = compute_score(state)
130
  threshold = settings.planning.threshold
131
 
 
12
 
13
  from pydantic import BaseModel, Field
14
 
15
+ from agents.design_state import DesignState
16
+
17
  logger = logging.getLogger(__name__)
18
 
19
  try:
 
29
  # ── Per-request state (ContextVar — async-safe) ─────────────────────────
30
 
31
  _last_shape_var: ContextVar[object | None] = ContextVar("last_shape", default=None)
32
+ _design_state_var: ContextVar[DesignState | None] = ContextVar("design_state", default=None)
33
 
34
 
35
  def set_last_shape(shape):
 
38
  def get_last_shape():
39
  return _last_shape_var.get()
40
 
41
+ def set_design_state(state: DesignState):
42
+ _design_state_var.set(state)
43
 
44
+ def get_design_state() -> DesignState | None:
45
  return _design_state_var.get()
46
 
47
 
 
117
  args_schema: Type[BaseModel] = QueryDesignStateInput
118
 
119
  def _run(self, check: str = "all") -> str:
120
+ from agents.design_state import compute_score
121
  from config.settings import settings
122
 
123
  if check not in VALID_CHECKS:
124
  return json.dumps({"error": f"Invalid check: {check!r}. Valid: {sorted(VALID_CHECKS)}"})
125
 
126
+ state = get_design_state()
127
+ if state is None:
128
  return json.dumps({"error": "No design state available."})
 
 
129
  score = compute_score(state)
130
  threshold = settings.planning.threshold
131