mainsequence 9.0.11.dev50__py3-none-any.whl → 9.0.12.dev51__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -147,6 +147,13 @@ person, read `User.get_by_uid(uid)`: a workload identity has `identity_type`
147
147
  `"workload"`. Caller authentication is not resource authorization; apply the
148
148
  endpoint's application policy using the verified user UID.
149
149
 
150
+ To list the people, Teams and workloads the caller can see, for example to
151
+ offer sharing candidates, read inside `reads_as_caller()` from
152
+ `mainsequence.server.fastapi`: `User.filter`, `User.get_by_uid`, `Team.filter`
153
+ and `Team.get_by_uid` then answer with the caller's visibility, and workload
154
+ rows carry `managed_by_caller`. Outside a signed HTTP request it raises
155
+ `RequestIdentityError`; it never reads as the application instead.
156
+
150
157
  WebSockets retain the platform's gateway ticket/header contract rather than
151
158
  the HTTP Bearer/assertion flow; the getter is available while the authenticated
152
159
  connection is active. See the version-matched
@@ -2,8 +2,11 @@
2
2
 
3
3
  from contextlib import contextmanager
4
4
  from contextvars import ContextVar
5
- from dataclasses import dataclass
5
+ from dataclasses import dataclass, field
6
6
  from typing import Any
7
+ from urllib.parse import urlsplit
8
+
9
+ CALLER_ASSERTION_HEADER = "X-MainSequence-Caller-Assertion"
7
10
 
8
11
 
9
12
  class RequestIdentityError(RuntimeError):
@@ -14,11 +17,18 @@ class RequestIdentityError(RuntimeError):
14
17
  class _RequestScope:
15
18
  user: Any = None
16
19
  active: bool = True
20
+ # The verified assertion of a hosted HTTP request, kept only so that
21
+ # reads_as_caller() can present it (ADR-0036, amended for #198).
22
+ caller_assertion: str | None = field(default=None, repr=False)
17
23
 
18
24
 
19
25
  _current_scope: ContextVar[_RequestScope | None] = ContextVar(
20
26
  "mainsequence_request_identity", default=None
21
27
  )
28
+ # The request scope whose caller the directory reads answer as, inside reads_as_caller().
29
+ _caller_reads: ContextVar[_RequestScope | None] = ContextVar(
30
+ "mainsequence_caller_reads", default=None
31
+ )
22
32
 
23
33
 
24
34
  @contextmanager
@@ -31,6 +41,7 @@ def _request_scope():
31
41
  # Copied child-task contexts hold this same frame and must also expire.
32
42
  scope.active = False
33
43
  scope.user = None
44
+ scope.caller_assertion = None
34
45
  _current_scope.reset(token)
35
46
 
36
47
 
@@ -42,3 +53,46 @@ def _get_request_identity():
42
53
  "once in the application and call User.get_logged_user() inside a request."
43
54
  )
44
55
  return scope.user
56
+
57
+
58
+ @contextmanager
59
+ def _reads_as_caller():
60
+ scope = _current_scope.get()
61
+ if scope is None or not scope.active or scope.user is None:
62
+ raise RequestIdentityError(
63
+ "Reading as the caller needs an authenticated request. Install request "
64
+ "identity once in the application and call reads_as_caller() inside a request."
65
+ )
66
+ if scope.caller_assertion is None:
67
+ raise RequestIdentityError(
68
+ "This request carries no caller assertion. Reading as the caller needs a "
69
+ "hosted HTTP request in assertion mode, not local mode or a WebSocket."
70
+ )
71
+ token = _caller_reads.set(scope)
72
+ try:
73
+ yield
74
+ finally:
75
+ _caller_reads.reset(token)
76
+
77
+
78
+ def _origin(url: str) -> tuple[str, str | None, int | None]:
79
+ parts = urlsplit(url)
80
+ scheme = parts.scheme.lower()
81
+ return scheme, parts.hostname, parts.port or {"http": 80, "https": 443}.get(scheme)
82
+
83
+
84
+ def _caller_read_headers(url: str, endpoint_url: str) -> dict[str, str]:
85
+ """The header a directory read sends to ``url``: none outside reads_as_caller()."""
86
+ scope = _caller_reads.get()
87
+ if scope is None:
88
+ return {}
89
+ if not scope.active or scope.caller_assertion is None:
90
+ raise RequestIdentityError(
91
+ "The request that reads_as_caller() belongs to has ended; its caller "
92
+ "assertion is no longer available."
93
+ )
94
+ if _origin(url) != _origin(endpoint_url):
95
+ raise RequestIdentityError(
96
+ "The caller assertion is sent only to the configured platform endpoint."
97
+ )
98
+ return {CALLER_ASSERTION_HEADER: scope.caller_assertion}
@@ -180,6 +180,11 @@ class BaseObjectOrm:
180
180
  def _sdk_owned_query_context(cls, operation: str) -> dict[str, Any]:
181
181
  return {}
182
182
 
183
+ @classmethod
184
+ def _read_request_headers(cls, url: str) -> dict[str, str]:
185
+ """Headers a filter or detail read adds to its own request to ``url``."""
186
+ return {}
187
+
183
188
  @classmethod
184
189
  def _sdk_owned_create_context(cls, operation: str) -> dict[str, Any]:
185
190
  return {}
@@ -540,12 +545,13 @@ class BaseObjectOrm:
540
545
 
541
546
  while next_url:
542
547
  req_payload = {"params": params} if params else {}
548
+ headers = cls._read_request_headers(next_url)
543
549
  r = make_request(
544
550
  s=cls.build_session(),
545
551
  loaders=cls.LOADERS,
546
552
  r_type="GET",
547
553
  url=next_url,
548
- payload=req_payload,
554
+ payload={**req_payload, "headers": headers} if headers else req_payload,
549
555
  time_out=timeout,
550
556
  )
551
557
  raise_for_response(r, payload=req_payload)
@@ -610,12 +616,16 @@ class BaseObjectOrm:
610
616
  **cls._sdk_owned_query_context(operation),
611
617
  }
612
618
 
619
+ payload = {"params": params} # neede to pass special serializer
620
+ headers = cls._read_request_headers(detail_url)
621
+ if headers:
622
+ payload["headers"] = headers
613
623
  r = make_request(
614
624
  s=cls.build_session(),
615
625
  loaders=cls.LOADERS,
616
626
  r_type="GET",
617
627
  url=detail_url,
618
- payload={"params": params}, # neede to pass special serializer
628
+ payload=payload,
619
629
  time_out=timeout,
620
630
  )
621
631
  raise_for_response(r)
@@ -6,7 +6,11 @@ from uuid import UUID
6
6
 
7
7
  from pydantic import BaseModel, ConfigDict, Field, StrictBool, field_validator, model_validator
8
8
 
9
- from mainsequence._request_identity import RequestIdentityError, _get_request_identity
9
+ from mainsequence._request_identity import (
10
+ RequestIdentityError,
11
+ _caller_read_headers,
12
+ _get_request_identity,
13
+ )
10
14
  from mainsequence.defaults import STANDARD_BACKEND_URL
11
15
 
12
16
  from .base import (
@@ -336,7 +340,18 @@ class TeamMembershipUpdateResult(BasePydanticModel):
336
340
  )
337
341
 
338
342
 
339
- class Team(PermissionManagedObjectMixin, BasePydanticModel, UserApiBaseObjectOrm):
343
+ class _CallerDirectoryReadMixin:
344
+ """Inside ``reads_as_caller()``, ``filter`` and ``get`` reads answer as the
345
+ request's caller (ADR-0036, amended for #198)."""
346
+
347
+ @classmethod
348
+ def _read_request_headers(cls, url: str) -> dict[str, str]:
349
+ return _caller_read_headers(url, cls.get_object_url())
350
+
351
+
352
+ class Team(
353
+ _CallerDirectoryReadMixin, PermissionManagedObjectMixin, BasePydanticModel, UserApiBaseObjectOrm
354
+ ):
340
355
  ENDPOINT: ClassVar[str] = "teams"
341
356
  FILTERSET_FIELDS: ClassVar[dict[str, list[str]] | None] = {
342
357
  "uid": ["exact", "in"],
@@ -960,7 +975,7 @@ class ShareableAccessState(BasePydanticModel):
960
975
  )
961
976
 
962
977
 
963
- class User(UserApiBaseObjectOrm, BasePydanticModel):
978
+ class User(_CallerDirectoryReadMixin, UserApiBaseObjectOrm, BasePydanticModel):
964
979
  """A platform User: a person, or the workload identity a deployed Job,
965
980
  FastAPI release or Agent runs as.
966
981
 
@@ -974,6 +989,10 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
974
989
  The reverse direction is ``workload_user_uid`` on ``Job``,
975
990
  ``ResourceRelease`` and ``Agent``: the UID of the User that workload runs
976
991
  as, to pass to ``User.get_by_uid`` and to sharing calls.
992
+
993
+ Inside ``mainsequence.server.fastapi.reads_as_caller()``, ``User.filter``
994
+ and ``User.get_by_uid`` answer with what the request's caller may see, and
995
+ workload rows carry ``managed_by_caller``.
977
996
  """
978
997
 
979
998
  # Platform facts, not application authorization policy. None means the
@@ -986,6 +1005,7 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
986
1005
  "uid": ["exact", "in"],
987
1006
  "email": ["exact", "contains", "in"],
988
1007
  "identity_type": ["exact"],
1008
+ "search": ["exact"],
989
1009
  }
990
1010
  FILTER_VALUE_NORMALIZERS: ClassVar[dict[str, str]] = {
991
1011
  "uid": "uid",
@@ -994,6 +1014,7 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
994
1014
  "email__contains": "str",
995
1015
  "email__in": "str",
996
1016
  "identity_type": "str",
1017
+ "search": "str",
997
1018
  }
998
1019
 
999
1020
  id: int | None = Field(
@@ -1038,6 +1059,15 @@ class User(UserApiBaseObjectOrm, BasePydanticModel):
1038
1059
  description="UID of the Agent a workload identity belongs to; None otherwise.",
1039
1060
  examples=[None],
1040
1061
  )
1062
+ managed_by_caller: bool | None = Field(
1063
+ None,
1064
+ title="Managed By Caller",
1065
+ description=(
1066
+ "On a workload identity read inside `reads_as_caller()`: whether the "
1067
+ "request's caller manages that workload. None when the row does not carry it."
1068
+ ),
1069
+ examples=[True],
1070
+ )
1041
1071
  profile_picture: str | None = Field(
1042
1072
  None,
1043
1073
  title="Profile Picture URL",
@@ -16,7 +16,8 @@ import requests
16
16
  from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
17
17
  from jwt import InvalidKeyError, InvalidTokenError
18
18
 
19
- ASSERTION_HEADER = "X-MainSequence-Caller-Assertion"
19
+ from mainsequence._request_identity import CALLER_ASSERTION_HEADER as ASSERTION_HEADER
20
+
20
21
  ASSERTION_TYPE = "mainsequence-caller-assertion+jwt"
21
22
  ASSERTION_ALGORITHM = "EdDSA"
22
23
  MAX_ASSERTION_SECONDS = 300
@@ -6,11 +6,12 @@ import asyncio
6
6
  import json
7
7
  import os
8
8
  import re
9
+ from contextlib import contextmanager
9
10
  from uuid import UUID
10
11
 
11
12
  import requests
12
13
 
13
- from mainsequence._request_identity import _request_scope
14
+ from mainsequence._request_identity import _reads_as_caller, _request_scope
14
15
  from mainsequence.client.models_user import RequestUserIdentity
15
16
 
16
17
  from .caller_assertions import (
@@ -220,6 +221,7 @@ class _RequestIdentityMiddleware:
220
221
  )
221
222
  state["resource_release_uid"] = caller.release_uid
222
223
  state["organization_environment_uid"] = caller.environment_uid
224
+ context.caller_assertion = assertion
223
225
  else:
224
226
  user = await asyncio.to_thread(_local_user, scope)
225
227
  context.user = user
@@ -267,4 +269,19 @@ def install_request_identity(app):
267
269
  )
268
270
 
269
271
 
270
- __all__ = ["install_request_identity"]
272
+ @contextmanager
273
+ def reads_as_caller():
274
+ """Read the directory as the request's caller instead of as the application.
275
+
276
+ Inside the block, ``User.filter``, ``User.get_by_uid``, ``Team.filter`` and
277
+ ``Team.get_by_uid`` present the caller assertion the request arrived with,
278
+ and the platform answers with what the caller may see. No other call sends
279
+ it. Raises ``RequestIdentityError`` outside an authenticated request or when
280
+ the request carries no assertion (local mode, WebSockets); it never reads as
281
+ the application instead. See ADR-0036.
282
+ """
283
+ with _reads_as_caller():
284
+ yield
285
+
286
+
287
+ __all__ = ["install_request_identity", "reads_as_caller"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mainsequence
3
- Version: 9.0.11.dev50
3
+ Version: 9.0.12.dev51
4
4
  Summary: Main Sequence SDK
5
5
  Author-email: Main Sequence GmbH <dev@main-sequence.io>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  agent_scaffold/AGENTS.md,sha256=W91rpnyQuoQIa-3dsebIYyuwnbGLvkmuq_8X5k2hTAw,6399
2
2
  agent_scaffold/skills/a2a_sdk_execution/SKILL.md,sha256=NKtcXibE8TxZzKzz1KzJ2cVZ_EeuJJkb660U6wLvZ3s,11809
3
- agent_scaffold/skills/application_surfaces/api_surfaces/SKILL.md,sha256=BUaqO3MkZ-d5sdh2xNPNQ904aO3RIJZfQW54FhDRxkc,16225
3
+ agent_scaffold/skills/application_surfaces/api_surfaces/SKILL.md,sha256=jYuZvxtfKh9MpWPxmT1EqPnkW_i4BbZfvDnWRiJHqRk,16659
4
4
  agent_scaffold/skills/mainsequence-inference/SKILL.md,sha256=yv8aAHLQ2FBBSWPIFadY7pGNTwtR-yiR_-UzaGRN9-g,12502
5
5
  agent_scaffold/skills/maintenance/bug_auditor/SKILL.md,sha256=CCKs7edyZ9RK-WtAgXJXucokJFXZxk8sRUe0_imFtu8,8610
6
6
  agent_scaffold/skills/maintenance/code_repository_maintenance/SKILL.md,sha256=pPsZaERlfokSJ6b6vLjpA5U0KuKxbOFzzcxOVW_ikiI,17935
@@ -10,7 +10,7 @@ agent_scaffold/skills/platform_operations/orchestration_and_releases/SKILL.md,sh
10
10
  agent_scaffold/skills/sdk_code_repository_execution/SKILL.md,sha256=zly7MheB-Sjd0x6l_oPCOTBqkL2E4SSXwTlfSwdIPfQ,10196
11
11
  mainsequence/__init__.py,sha256=70CbIo7ZG1mzi5uwkqDtVESunBE0RanHjeFg4ptfsA0,385
12
12
  mainsequence/__main__.py,sha256=KuAP69uduS2ImrPIgNMzbGBRepZzjqvUONu5vFh4v4A,176
13
- mainsequence/_request_identity.py,sha256=jiroPFVDM8vphEf9VXuggQJKUOGohfk7-pDwJSOILEw,1211
13
+ mainsequence/_request_identity.py,sha256=BZVboogZPnRoka9vNYa3DOzex1qYDNOCiJ-2be9Jzqw,3406
14
14
  mainsequence/bootstrap.py,sha256=AvG55Y46RcshVqJZsyqvUDN62CJg4CDbHu0Bvsbwfzs,2910
15
15
  mainsequence/code_repository_context.py,sha256=JRrWjwpnUy8I-OjSd9hHlrcq8-Ow3Zeh_N6cYvmtA3o,28545
16
16
  mainsequence/code_repository_skills.py,sha256=AyC1HFAxQXHXNBGbarVUJS2cHd4X5HwDx6u8XBophbE,23611
@@ -37,7 +37,7 @@ mainsequence/cli/ui.py,sha256=memd6pmjALfGj8zndGCx_z80ztQeqTcOvcofU0un34U,2277
37
37
  mainsequence/client/__init__.py,sha256=lsr9JWxeVqOG0KRoHErW5M40ACPSR7SAEE508dTN5XM,672
38
38
  mainsequence/client/_transport.py,sha256=l2xkbJ21SbrJhzgOf58Yikxg-uzOQPyo-H5hENdCBo8,5374
39
39
  mainsequence/client/agent_runtime_models.py,sha256=8H4VEZWgnlYx3r7XY9eQq4D7hfzLRyhrIRyy2ZVae2Y,67718
40
- mainsequence/client/base.py,sha256=T3k3xptM09TreiVogrxAPWoIy8zM6Dtn3Cbwsnb33ag,42678
40
+ mainsequence/client/base.py,sha256=ZxuwUIscBNQGvByd7NGbn03VtNU9nazqkEouyKfVLk0,43129
41
41
  mainsequence/client/client.py,sha256=Jaw9RYObdVDS5J2O0pln99-XpuW9e4vW-SsbJRNldfw,3866
42
42
  mainsequence/client/compute_validation.py,sha256=ByeX0CkIE7qq06ggZFfN92U3Qp2e9cvo2RTIGWa5_Vw,6662
43
43
  mainsequence/client/exceptions.py,sha256=xx2L4l3OMNsTybbYkiTKK398ahnutEascM0It35tGr0,3815
@@ -45,18 +45,18 @@ mainsequence/client/github_issues.py,sha256=pSTUw3UPrmE6wqnpoJctartTaNSq6iu6PQRr
45
45
  mainsequence/client/inference.py,sha256=tYKHRxLR6mA5iLmgd3qURwweWc2OPQuw0__tD3LQgSI,7748
46
46
  mainsequence/client/models_foundry.py,sha256=JwQdPLDSO4dZI-KC0SUdmN6izmNIKMb9IzZlYXy0Mig,36488
47
47
  mainsequence/client/models_helpers.py,sha256=nlbHuQAylTOHAZOq4ltJQp6QvC3NW_kw7tZFosGc5rM,47311
48
- mainsequence/client/models_user.py,sha256=vpnzwemq9SVXHndlIhS731veJz9yzQ8rz5b0mP42KVQ,41166
48
+ mainsequence/client/models_user.py,sha256=YFEClKcKZpWLCy8Gmg8tZXIHmJbkdQHkeOBJXmp0HG0,42169
49
49
  mainsequence/client/observability.py,sha256=_WrDKCaNk_TXmKElKCXC1inork6Exxxe8FRY4m1GI9s,16008
50
50
  mainsequence/client/utils.py,sha256=d6lXJPAOe683gb-ph9x0GJcvK5c5Pg9gr90ihDgQGug,32484
51
51
  mainsequence/client/value_sets.py,sha256=7TA-mpuAd3hOduNgO1WTQQTuNOGaFgue-VqCsDWKaC8,6181
52
52
  mainsequence/instrumentation/__init__.py,sha256=J_9XmfjprElvz0u2t9rTyJT0kFdyKpxdbkdQiaw28_U,316
53
53
  mainsequence/instrumentation/utils.py,sha256=b-ihjSioAepeLfMpudM2SqCtvnxMqIXileHx3klBCQU,3377
54
54
  mainsequence/server/__init__.py,sha256=BHUAhtdWPDRdxX07NYO_6wrBv5mf0GsXflecO52PmUc,73
55
- mainsequence/server/caller_assertions.py,sha256=f9jHhVeoFK1ScDCmW8h3lEIU5V7zKw70SUSVV2P7wUI,10975
56
- mainsequence/server/fastapi.py,sha256=Y5QB51CRVbuUVRUcWh5GKQ5b5vMPjbs9scioTt684oQ,10712
57
- mainsequence-9.0.11.dev50.dist-info/licenses/LICENSE,sha256=b6Ecsk8wjsEby3lbSzdljR6hHiFGHBizt9iWV2p1c_I,1074
58
- mainsequence-9.0.11.dev50.dist-info/METADATA,sha256=fIx_7NHIGCF8alTSS09AH7RLc0Hj3jEs_vZIeWbq84s,7403
59
- mainsequence-9.0.11.dev50.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
60
- mainsequence-9.0.11.dev50.dist-info/entry_points.txt,sha256=XGAOq3-mF2lWWpeRJvoaWyMarRcK167mH1g124aGdUs,88
61
- mainsequence-9.0.11.dev50.dist-info/top_level.txt,sha256=BFeblM_Q9q0cUWBvTxnfWcWQEHtf5XZi9nOS-Gdfwus,28
62
- mainsequence-9.0.11.dev50.dist-info/RECORD,,
55
+ mainsequence/server/caller_assertions.py,sha256=Me8of2Ku8in-97LWU2oY-0oD1OW6zF-xOVVpEQ0eoe4,11010
56
+ mainsequence/server/fastapi.py,sha256=zclo7ARHKUKSTmOc0KdxUSwfsxlx6SJuaiAe-19nzWg,11465
57
+ mainsequence-9.0.12.dev51.dist-info/licenses/LICENSE,sha256=b6Ecsk8wjsEby3lbSzdljR6hHiFGHBizt9iWV2p1c_I,1074
58
+ mainsequence-9.0.12.dev51.dist-info/METADATA,sha256=F7sYdFmk6Ni6QFEbA2jMXCaoD6wxb_w4WSSUsZ9dPk4,7403
59
+ mainsequence-9.0.12.dev51.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
60
+ mainsequence-9.0.12.dev51.dist-info/entry_points.txt,sha256=XGAOq3-mF2lWWpeRJvoaWyMarRcK167mH1g124aGdUs,88
61
+ mainsequence-9.0.12.dev51.dist-info/top_level.txt,sha256=BFeblM_Q9q0cUWBvTxnfWcWQEHtf5XZi9nOS-Gdfwus,28
62
+ mainsequence-9.0.12.dev51.dist-info/RECORD,,