Skip to content

Commit 0405792

Browse files
authored
chore: apply ruff format to all Python files (#78)
49 files reformatted to match ruff 0.15.5 default style. Fix UP038 lint: use X | Y instead of (X, Y) in isinstance call. Prerequisite for enforcing format check in CI.
1 parent acf1b18 commit 0405792

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1078
-644
lines changed

src/edictum_server/auth/local.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def __init__(
4040

4141
def _sign(self, data: str) -> str:
4242
"""HMAC-SHA256 sign session data, return 'hmac_hex:json' string."""
43-
mac = hmac.new(
44-
self._secret_key.encode(), data.encode(), hashlib.sha256
45-
).hexdigest()
43+
mac = hmac.new(self._secret_key.encode(), data.encode(), hashlib.sha256).hexdigest()
4644
return f"{mac}:{data}"
4745

4846
def _verify_and_parse(self, raw: str) -> dict[str, object] | None:
@@ -88,7 +86,7 @@ async def authenticate(self, request: Request) -> DashboardAuthContext:
8886
# Enforce absolute session lifetime (7 days max)
8987
created_at = data.get("created_at")
9088
expired = (
91-
isinstance(created_at, (int, float))
89+
isinstance(created_at, int | float)
9290
and time.time() - created_at > _MAX_ABSOLUTE_LIFETIME
9391
)
9492
if expired:

src/edictum_server/routes/agent_registrations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Agent registration management endpoints (dashboard auth)."""
2+
23
from __future__ import annotations
34

45
from typing import Any

src/edictum_server/routes/agents.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ async def agent_fleet_status(
3232
for conn in connections:
3333
status = "unknown"
3434
if conn.policy_version and conn.env:
35-
status = await check_drift(
36-
db, auth.tenant_id, conn.policy_version, conn.env
37-
)
35+
status = await check_drift(db, auth.tenant_id, conn.policy_version, conn.env)
3836

3937
entries.append(
4038
AgentStatusEntry(

src/edictum_server/routes/ai_usage.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ async def get_usage_stats(
3333
func.coalesce(func.sum(AiUsageLog.output_tokens), 0).label("total_output"),
3434
func.sum(AiUsageLog.estimated_cost_usd).label("total_cost"),
3535
func.count().label("query_count"),
36-
func.coalesce(func.avg(
37-
AiUsageLog.output_tokens * 1000.0 / func.nullif(AiUsageLog.duration_ms, 0)
38-
), 0.0).label("avg_tps"),
36+
func.coalesce(
37+
func.avg(
38+
AiUsageLog.output_tokens * 1000.0 / func.nullif(AiUsageLog.duration_ms, 0)
39+
),
40+
0.0,
41+
).label("avg_tps"),
3942
).where(
4043
AiUsageLog.tenant_id == auth.tenant_id,
4144
AiUsageLog.created_at >= since,
@@ -51,10 +54,13 @@ async def get_usage_stats(
5154
func.sum(AiUsageLog.output_tokens).label("output_tokens"),
5255
func.sum(AiUsageLog.estimated_cost_usd).label("cost_usd"),
5356
func.count().label("queries"),
54-
).where(
57+
)
58+
.where(
5559
AiUsageLog.tenant_id == auth.tenant_id,
5660
AiUsageLog.created_at >= since,
57-
).group_by("day").order_by("day")
61+
)
62+
.group_by("day")
63+
.order_by("day")
5864
)
5965
daily_rows = daily_q.all()
6066

src/edictum_server/routes/assignment_rules.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Assignment rule CRUD endpoints (dashboard auth)."""
2+
23
from __future__ import annotations
34

45
import re
@@ -149,7 +150,9 @@ async def delete_rule(
149150
async def resolve_agent_bundle(
150151
agent_id: str,
151152
env: str | None = Query(
152-
default=None, max_length=64, description="Filter rules by environment",
153+
default=None,
154+
max_length=64,
155+
description="Filter rules by environment",
153156
),
154157
auth: AuthContext = Depends(require_dashboard_auth),
155158
db: AsyncSession = Depends(get_db),
@@ -160,7 +163,10 @@ async def resolve_agent_bundle(
160163
When omitted, all tenant rules are evaluated (useful for dashboard previews).
161164
"""
162165
bundle_name, source, rule_id, rule_pattern = await svc.resolve_bundle(
163-
db, auth.tenant_id, agent_id, env=env,
166+
db,
167+
auth.tenant_id,
168+
agent_id,
169+
env=env,
164170
)
165171
return ResolvedAssignment(
166172
bundle_name=bundle_name,

src/edictum_server/routes/compositions.py

Lines changed: 70 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ async def get_composition_endpoint(
7070
items = await get_composition_items(db, comp)
7171
return CompositionDetail(
7272
**_to_summary(comp, len(items)).model_dump(),
73-
id=comp.id, tenant_id=comp.tenant_id,
73+
id=comp.id,
74+
tenant_id=comp.tenant_id,
7475
contracts=[CompositionItemDetail(**i) for i in items],
75-
tools_config=comp.tools_config, observability=comp.observability,
76+
tools_config=comp.tools_config,
77+
observability=comp.observability,
7678
)
7779

7880

@@ -86,59 +88,91 @@ async def create_composition_endpoint(
8688
"""Create a new bundle composition."""
8789
try:
8890
comp = await create_composition(
89-
db, auth.tenant_id, body.name, auth.email or auth.user_id or "unknown",
90-
description=body.description, defaults_mode=body.defaults_mode,
91-
update_strategy=body.update_strategy, contracts=body.contracts,
92-
tools_config=body.tools_config, observability=body.observability,
91+
db,
92+
auth.tenant_id,
93+
body.name,
94+
auth.email or auth.user_id or "unknown",
95+
description=body.description,
96+
defaults_mode=body.defaults_mode,
97+
update_strategy=body.update_strategy,
98+
contracts=body.contracts,
99+
tools_config=body.tools_config,
100+
observability=body.observability,
93101
)
94102
await db.commit()
95103
except ValueError as exc:
96104
status = 409 if "already exists" in str(exc) else 422
97105
raise HTTPException(status_code=status, detail=str(exc)) from exc
98-
push.push_to_dashboard(auth.tenant_id, {
99-
"type": "composition_changed", "composition_name": comp.name,
100-
})
106+
push.push_to_dashboard(
107+
auth.tenant_id,
108+
{
109+
"type": "composition_changed",
110+
"composition_name": comp.name,
111+
},
112+
)
101113
items = await get_composition_items(db, comp)
102114
return CompositionDetail(
103-
name=comp.name, description=comp.description,
104-
defaults_mode=comp.defaults_mode, update_strategy=comp.update_strategy,
105-
contract_count=len(items), updated_at=comp.updated_at,
106-
created_by=comp.created_by, id=comp.id, tenant_id=comp.tenant_id,
115+
name=comp.name,
116+
description=comp.description,
117+
defaults_mode=comp.defaults_mode,
118+
update_strategy=comp.update_strategy,
119+
contract_count=len(items),
120+
updated_at=comp.updated_at,
121+
created_by=comp.created_by,
122+
id=comp.id,
123+
tenant_id=comp.tenant_id,
107124
contracts=[CompositionItemDetail(**i) for i in items],
108-
tools_config=comp.tools_config, observability=comp.observability,
125+
tools_config=comp.tools_config,
126+
observability=comp.observability,
109127
)
110128

111129

112130
@router.put("/{name}", response_model=CompositionDetail)
113131
async def update_composition_endpoint(
114-
name: str, body: CompositionUpdateRequest,
132+
name: str,
133+
body: CompositionUpdateRequest,
115134
auth: AuthContext = Depends(require_dashboard_auth),
116135
db: AsyncSession = Depends(get_db),
117136
push: PushManager = Depends(get_push_manager),
118137
) -> CompositionDetail:
119138
"""Update a composition (add/remove/reorder contracts)."""
120139
try:
121140
comp = await update_composition(
122-
db, auth.tenant_id, name,
123-
description=body.description, defaults_mode=body.defaults_mode,
124-
update_strategy=body.update_strategy, contracts=body.contracts,
125-
tools_config=body.tools_config, observability=body.observability,
141+
db,
142+
auth.tenant_id,
143+
name,
144+
description=body.description,
145+
defaults_mode=body.defaults_mode,
146+
update_strategy=body.update_strategy,
147+
contracts=body.contracts,
148+
tools_config=body.tools_config,
149+
observability=body.observability,
126150
)
127151
await db.commit()
128152
except ValueError as exc:
129153
status = 404 if "not found" in str(exc).lower() else 422
130154
raise HTTPException(status_code=status, detail=str(exc)) from exc
131-
push.push_to_dashboard(auth.tenant_id, {
132-
"type": "composition_changed", "composition_name": comp.name,
133-
})
155+
push.push_to_dashboard(
156+
auth.tenant_id,
157+
{
158+
"type": "composition_changed",
159+
"composition_name": comp.name,
160+
},
161+
)
134162
items = await get_composition_items(db, comp)
135163
return CompositionDetail(
136-
name=comp.name, description=comp.description,
137-
defaults_mode=comp.defaults_mode, update_strategy=comp.update_strategy,
138-
contract_count=len(items), updated_at=comp.updated_at,
139-
created_by=comp.created_by, id=comp.id, tenant_id=comp.tenant_id,
164+
name=comp.name,
165+
description=comp.description,
166+
defaults_mode=comp.defaults_mode,
167+
update_strategy=comp.update_strategy,
168+
contract_count=len(items),
169+
updated_at=comp.updated_at,
170+
created_by=comp.created_by,
171+
id=comp.id,
172+
tenant_id=comp.tenant_id,
140173
contracts=[CompositionItemDetail(**i) for i in items],
141-
tools_config=comp.tools_config, observability=comp.observability,
174+
tools_config=comp.tools_config,
175+
observability=comp.observability,
142176
)
143177

144178

@@ -172,7 +206,8 @@ async def preview_endpoint(
172206

173207
@router.post("/{name}/deploy", response_model=ComposeDeployResponse, status_code=201)
174208
async def deploy_endpoint(
175-
name: str, body: ComposeDeployRequest,
209+
name: str,
210+
body: ComposeDeployRequest,
176211
auth: AuthContext = Depends(require_dashboard_auth),
177212
db: AsyncSession = Depends(get_db),
178213
push: PushManager = Depends(get_push_manager),
@@ -188,8 +223,13 @@ async def deploy_endpoint(
188223
raise HTTPException(status_code=422, detail=str(exc)) from exc
189224
try:
190225
result = await deploy_composition(
191-
db, auth.tenant_id, comp, body.env,
192-
auth.email or auth.user_id or "unknown", signing_secret, push,
226+
db,
227+
auth.tenant_id,
228+
comp,
229+
body.env,
230+
auth.email or auth.user_id or "unknown",
231+
signing_secret,
232+
push,
193233
)
194234
await db.commit()
195235
except ValueError as exc:

src/edictum_server/routes/events.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ async def post_events(
6767
# Notify dashboard subscribers about new events. Push one summary
6868
# message per batch rather than one per event to avoid flooding.
6969
if accepted > 0:
70-
push.push_to_dashboard(auth.tenant_id, {
71-
"type": "event_created",
72-
"accepted": accepted,
73-
})
70+
push.push_to_dashboard(
71+
auth.tenant_id,
72+
{
73+
"type": "event_created",
74+
"accepted": accepted,
75+
},
76+
)
7477

7578
return EventIngestResponse(accepted=accepted, duplicates=duplicates)
7679

src/edictum_server/schemas/agent_registrations.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Schemas for agent registration and assignment rule endpoints."""
2+
23
from __future__ import annotations
34

45
import uuid
@@ -9,6 +10,7 @@
910

1011
# --- Agent Registration ---
1112

13+
1214
class AgentRegistrationResponse(BaseModel):
1315
id: uuid.UUID
1416
agent_id: str
@@ -37,6 +39,7 @@ class BulkAssignResponse(BaseModel):
3739

3840
# --- Assignment Rules ---
3941

42+
4043
class AssignmentRuleCreate(BaseModel):
4144
priority: int = Field(..., ge=0)
4245
pattern: str = Field(..., min_length=1, max_length=200)
@@ -65,6 +68,7 @@ class AssignmentRuleResponse(BaseModel):
6568

6669
class ResolvedAssignment(BaseModel):
6770
"""Result of bundle resolution for an agent."""
71+
6872
bundle_name: str | None
6973
source: str # "explicit" | "rule" | "agent_provided" | "none"
7074
rule_id: uuid.UUID | None = None

src/edictum_server/schemas/compositions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ class CompositionCreateRequest(BaseModel):
3737
@classmethod
3838
def validate_name(cls, v: str) -> str:
3939
if len(v) > _BUNDLE_NAME_MAX_LEN:
40-
raise ValueError(
41-
f"name must be at most {_BUNDLE_NAME_MAX_LEN} characters"
42-
)
40+
raise ValueError(f"name must be at most {_BUNDLE_NAME_MAX_LEN} characters")
4341
if not _BUNDLE_NAME_RE.match(v):
4442
raise ValueError(
4543
"name must match [a-z0-9][a-z0-9._-]* "

src/edictum_server/schemas/evaluate.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,28 @@ class EvaluateRequest(BaseModel):
1919
"""Request body for contract evaluation playground."""
2020

2121
yaml_content: str = Field(
22-
..., max_length=1_048_576, description="YAML contract bundle to evaluate against",
22+
...,
23+
max_length=1_048_576,
24+
description="YAML contract bundle to evaluate against",
2325
)
2426
tool_name: str = Field(
25-
..., max_length=255, description="Tool name to simulate",
27+
...,
28+
max_length=255,
29+
description="Tool name to simulate",
2630
)
2731
tool_args: dict[str, Any] = Field(
28-
default_factory=dict, description="Tool arguments",
32+
default_factory=dict,
33+
description="Tool arguments",
2934
)
3035
environment: str = Field(
31-
default="production", max_length=64, description="Environment context",
36+
default="production",
37+
max_length=64,
38+
description="Environment context",
3239
)
3340
agent_id: str = Field(
34-
default="test-agent", max_length=255, description="Simulated agent ID",
41+
default="test-agent",
42+
max_length=255,
43+
description="Simulated agent ID",
3544
)
3645
principal: PrincipalInput | None = Field(
3746
default=None, description="Optional principal identity"

0 commit comments

Comments
 (0)