Skip to content

Commit 3c958fb

Browse files
fix(sdk): isolate runtime auth client cache
1 parent 8c5677f commit 3c958fb

5 files changed

Lines changed: 394 additions & 36 deletions

File tree

sdks/python/src/agent_control/__init__.py

Lines changed: 121 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,19 @@ def _run_coro_in_new_loop[T](coro: Coroutine[Any, Any, T]) -> T:
215215
asyncio.set_event_loop(None)
216216

217217

218+
def _ad_hoc_client(
219+
*,
220+
server_url: str,
221+
api_key: str | None,
222+
api_key_header: str | None,
223+
) -> AgentControlClient:
224+
return AgentControlClient(
225+
base_url=server_url,
226+
api_key=api_key,
227+
api_key_header=api_key_header if api_key_header is not None else state.api_key_header,
228+
)
229+
230+
218231
def _snapshot_refresh_context() -> _RefreshContext:
219232
"""Capture a consistent session snapshot for a refresh request."""
220233
with _session_lock:
@@ -770,6 +783,7 @@ async def get_agent(
770783
agent_name: str,
771784
server_url: str | None = None,
772785
api_key: str | None = None,
786+
api_key_header: str | None = None,
773787
) -> dict[str, Any]:
774788
"""
775789
Get agent details from the server by name.
@@ -809,7 +823,11 @@ async def main():
809823
"""
810824
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
811825

812-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
826+
async with _ad_hoc_client(
827+
server_url=_final_server_url,
828+
api_key=api_key,
829+
api_key_header=api_key_header,
830+
) as client:
813831
return await agents.get_agent(client, agent_name)
814832

815833

@@ -833,6 +851,7 @@ def current_agent() -> Agent | None:
833851
async def list_agents(
834852
server_url: str | None = None,
835853
api_key: str | None = None,
854+
api_key_header: str | None = None,
836855
cursor: str | None = None,
837856
limit: int = 20,
838857
) -> dict[str, Any]:
@@ -873,7 +892,11 @@ async def main():
873892
"""
874893
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
875894

876-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
895+
async with _ad_hoc_client(
896+
server_url=_final_server_url,
897+
api_key=api_key,
898+
api_key_header=api_key_header,
899+
) as client:
877900
return await agents.list_agents(client, cursor=cursor, limit=limit)
878901

879902

@@ -887,21 +910,31 @@ async def add_agent_policy(
887910
policy_id: int,
888911
server_url: str | None = None,
889912
api_key: str | None = None,
913+
api_key_header: str | None = None,
890914
) -> dict[str, Any]:
891915
"""Associate a policy with an agent (idempotent)."""
892916
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
893-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
917+
async with _ad_hoc_client(
918+
server_url=_final_server_url,
919+
api_key=api_key,
920+
api_key_header=api_key_header,
921+
) as client:
894922
return await agents.add_agent_policy(client, agent_name, policy_id)
895923

896924

897925
async def get_agent_policies(
898926
agent_name: str,
899927
server_url: str | None = None,
900928
api_key: str | None = None,
929+
api_key_header: str | None = None,
901930
) -> dict[str, Any]:
902931
"""List policy IDs associated with an agent."""
903932
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
904-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
933+
async with _ad_hoc_client(
934+
server_url=_final_server_url,
935+
api_key=api_key,
936+
api_key_header=api_key_header,
937+
) as client:
905938
return await agents.get_agent_policies(client, agent_name)
906939

907940

@@ -910,21 +943,31 @@ async def remove_agent_policy_association(
910943
policy_id: int,
911944
server_url: str | None = None,
912945
api_key: str | None = None,
946+
api_key_header: str | None = None,
913947
) -> dict[str, Any]:
914948
"""Remove one policy association from an agent (idempotent)."""
915949
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
916-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
950+
async with _ad_hoc_client(
951+
server_url=_final_server_url,
952+
api_key=api_key,
953+
api_key_header=api_key_header,
954+
) as client:
917955
return await agents.remove_agent_policy_association(client, agent_name, policy_id)
918956

919957

920958
async def remove_all_agent_policies(
921959
agent_name: str,
922960
server_url: str | None = None,
923961
api_key: str | None = None,
962+
api_key_header: str | None = None,
924963
) -> dict[str, Any]:
925964
"""Remove all policy associations from an agent."""
926965
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
927-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
966+
async with _ad_hoc_client(
967+
server_url=_final_server_url,
968+
api_key=api_key,
969+
api_key_header=api_key_header,
970+
) as client:
928971
return await agents.remove_all_agent_policies(client, agent_name)
929972

930973

@@ -933,10 +976,15 @@ async def add_agent_control(
933976
control_id: int,
934977
server_url: str | None = None,
935978
api_key: str | None = None,
979+
api_key_header: str | None = None,
936980
) -> dict[str, Any]:
937981
"""Associate a control with an agent (idempotent)."""
938982
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
939-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
983+
async with _ad_hoc_client(
984+
server_url=_final_server_url,
985+
api_key=api_key,
986+
api_key_header=api_key_header,
987+
) as client:
940988
return await agents.add_agent_control(client, agent_name, control_id)
941989

942990

@@ -945,10 +993,15 @@ async def remove_agent_control(
945993
control_id: int,
946994
server_url: str | None = None,
947995
api_key: str | None = None,
996+
api_key_header: str | None = None,
948997
) -> dict[str, Any]:
949998
"""Remove a direct control association from an agent (idempotent)."""
950999
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
951-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1000+
async with _ad_hoc_client(
1001+
server_url=_final_server_url,
1002+
api_key=api_key,
1003+
api_key_header=api_key_header,
1004+
) as client:
9521005
return await agents.remove_agent_control(client, agent_name, control_id)
9531006

9541007

@@ -960,6 +1013,7 @@ async def remove_agent_control(
9601013
async def list_controls(
9611014
server_url: str | None = None,
9621015
api_key: str | None = None,
1016+
api_key_header: str | None = None,
9631017
cursor: int | None = None,
9641018
limit: int = 20,
9651019
name: str | None = None,
@@ -1013,7 +1067,11 @@ async def main():
10131067
"""
10141068
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
10151069

1016-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1070+
async with _ad_hoc_client(
1071+
server_url=_final_server_url,
1072+
api_key=api_key,
1073+
api_key_header=api_key_header,
1074+
) as client:
10171075
return await controls.list_controls(
10181076
client,
10191077
cursor=cursor,
@@ -1033,6 +1091,7 @@ async def create_control(
10331091
data: dict[str, Any] | ControlDefinition | TemplateControlInput,
10341092
server_url: str | None = None,
10351093
api_key: str | None = None,
1094+
api_key_header: str | None = None,
10361095
) -> dict[str, Any]:
10371096
"""
10381097
Create a new control with configuration.
@@ -1080,19 +1139,28 @@ async def main():
10801139
"""
10811140
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
10821141

1083-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1142+
async with _ad_hoc_client(
1143+
server_url=_final_server_url,
1144+
api_key=api_key,
1145+
api_key_header=api_key_header,
1146+
) as client:
10841147
return await controls.create_control(client, name, data=data)
10851148

10861149

10871150
async def validate_control_data(
10881151
data: dict[str, Any] | ControlDefinition | TemplateControlInput,
10891152
server_url: str | None = None,
10901153
api_key: str | None = None,
1154+
api_key_header: str | None = None,
10911155
) -> dict[str, Any]:
10921156
"""Validate raw or template-backed control data without saving it."""
10931157
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
10941158

1095-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1159+
async with _ad_hoc_client(
1160+
server_url=_final_server_url,
1161+
api_key=api_key,
1162+
api_key_header=api_key_header,
1163+
) as client:
10961164
return await controls.validate_control_data(client, data=data)
10971165

10981166

@@ -1101,11 +1169,16 @@ async def render_control_template(
11011169
template_values: dict[str, TemplateValue],
11021170
server_url: str | None = None,
11031171
api_key: str | None = None,
1172+
api_key_header: str | None = None,
11041173
) -> dict[str, Any]:
11051174
"""Render a template-backed control preview without persisting it."""
11061175
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
11071176

1108-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1177+
async with _ad_hoc_client(
1178+
server_url=_final_server_url,
1179+
api_key=api_key,
1180+
api_key_header=api_key_header,
1181+
) as client:
11091182
return await controls.render_control_template(
11101183
client,
11111184
template=template,
@@ -1117,6 +1190,7 @@ async def get_control(
11171190
control_id: int,
11181191
server_url: str | None = None,
11191192
api_key: str | None = None,
1193+
api_key_header: str | None = None,
11201194
) -> dict[str, Any]:
11211195
"""
11221196
Get a control by ID from the server.
@@ -1147,7 +1221,11 @@ async def main():
11471221
"""
11481222
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
11491223

1150-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1224+
async with _ad_hoc_client(
1225+
server_url=_final_server_url,
1226+
api_key=api_key,
1227+
api_key_header=api_key_header,
1228+
) as client:
11511229
return await controls.get_control(client, control_id)
11521230

11531231

@@ -1156,6 +1234,7 @@ async def delete_control(
11561234
force: bool = False,
11571235
server_url: str | None = None,
11581236
api_key: str | None = None,
1237+
api_key_header: str | None = None,
11591238
) -> dict[str, Any]:
11601239
"""
11611240
Delete a control from the server.
@@ -1192,7 +1271,11 @@ async def main():
11921271
"""
11931272
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
11941273

1195-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1274+
async with _ad_hoc_client(
1275+
server_url=_final_server_url,
1276+
api_key=api_key,
1277+
api_key_header=api_key_header,
1278+
) as client:
11961279
return await controls.delete_control(client, control_id, force=force)
11971280

11981281

@@ -1202,6 +1285,7 @@ async def update_control(
12021285
enabled: bool | None = None,
12031286
server_url: str | None = None,
12041287
api_key: str | None = None,
1288+
api_key_header: str | None = None,
12051289
) -> dict[str, Any]:
12061290
"""
12071291
Update control metadata (name and/or enabled status).
@@ -1242,7 +1326,11 @@ async def main():
12421326
"""
12431327
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
12441328

1245-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1329+
async with _ad_hoc_client(
1330+
server_url=_final_server_url,
1331+
api_key=api_key,
1332+
api_key_header=api_key_header,
1333+
) as client:
12461334
return await controls.update_control(client, control_id, name=name, enabled=enabled)
12471335

12481336

@@ -1256,6 +1344,7 @@ async def add_control_to_policy(
12561344
control_id: int,
12571345
server_url: str | None = None,
12581346
api_key: str | None = None,
1347+
api_key_header: str | None = None,
12591348
) -> dict[str, Any]:
12601349
"""
12611350
Add a control to a policy.
@@ -1289,7 +1378,11 @@ async def main():
12891378
"""
12901379
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
12911380

1292-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1381+
async with _ad_hoc_client(
1382+
server_url=_final_server_url,
1383+
api_key=api_key,
1384+
api_key_header=api_key_header,
1385+
) as client:
12931386
return await policies.add_control_to_policy(client, policy_id, control_id)
12941387

12951388

@@ -1298,6 +1391,7 @@ async def remove_control_from_policy(
12981391
control_id: int,
12991392
server_url: str | None = None,
13001393
api_key: str | None = None,
1394+
api_key_header: str | None = None,
13011395
) -> dict[str, Any]:
13021396
"""
13031397
Remove a control from a policy.
@@ -1331,14 +1425,19 @@ async def main():
13311425
"""
13321426
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
13331427

1334-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1428+
async with _ad_hoc_client(
1429+
server_url=_final_server_url,
1430+
api_key=api_key,
1431+
api_key_header=api_key_header,
1432+
) as client:
13351433
return await policies.remove_control_from_policy(client, policy_id, control_id)
13361434

13371435

13381436
async def list_policy_controls(
13391437
policy_id: int,
13401438
server_url: str | None = None,
13411439
api_key: str | None = None,
1440+
api_key_header: str | None = None,
13421441
) -> dict[str, Any]:
13431442
"""
13441443
List all controls associated with a policy.
@@ -1368,7 +1467,11 @@ async def main():
13681467
"""
13691468
_final_server_url = server_url or os.getenv('AGENT_CONTROL_URL') or 'http://localhost:8000'
13701469

1371-
async with AgentControlClient(base_url=_final_server_url, api_key=api_key) as client:
1470+
async with _ad_hoc_client(
1471+
server_url=_final_server_url,
1472+
api_key=api_key,
1473+
api_key_header=api_key_header,
1474+
) as client:
13721475
return await policies.list_policy_controls(client, policy_id)
13731476

13741477

0 commit comments

Comments
 (0)