What is the thinking behind introducing in the OH-...
# proj-sdk
c
What is the thinking behind introducing in the OH-app-server a fairly large amount of • user-specific models • table row • “resolving” a conversation by user • sandbox created by user? Seems too many to be accidental. 😅
w
CC @straight-sundown-84319 The database is sqllite. I'm personally hesitant to introduce sqllite on top of our existing file store, but it does mean that we only need one service for accessing conversations for both enterprise and OSS Can you explain more about the other things you're seeing?
s
Here's my reasoning... With regard to tables: In V0 we had completely different storage implementations depending on whether you were using SAAS or OSS - we settled on SQL for all metadata except the actual event streams. One implementation for both, using alembic for migrations. It should mean less work in the long run Resolving a conversation by user. Although conversation ownership is not a concern in a single user OSS world, it becomes a major concern whenever multiple users are sharing an instance. It made sense to me to hide this within the service layer, rather than having
if shared_instance filter_by_user
on each call to a service. For single user cases, we create a root user and use the same logic. This works quite well for cases like
app_conversation_router.py
- we list conversations owned by the user, (And we are able to do the filtering in sql rather than pulling everything from storage and doing it on the app server). When we created a conversation it is associated with the current user. It meant I was able to use our existing authentication mechanism transparently without altering anything. Quite rapidly after this, it became apparent that there are cases where unconstrained data is also required. (For example, the
webhook_router.py
needs to grab a sandbox to see if the session_api_key given matches it. This is where the unsecured resolvers come in. Now that I think of it though - this case is unique to authentication, so I can probably remove unsecured resolvers for everything except sandboxes.
I think there is scope for simplification here
TLDR: • We use sql in all cases. • We hide the details of user filtering inside the service classes • For OSS, the user is always
root
.
Let me think on this a little more...
yeah - there are a few simplifications I'll apply across the board.
c
@straight-sundown-84319 On V0, towards its EOL , I analyzed the coupling between the Saas and OH, and came up with a few proposals. A big focus I had was the interface that is shared, that is, the routes that both use, routes signature / dependencies. On two entities: user and tokens. On user, I ended up with some definition of a
UserContext
that will encapsulate "user_id" and other user-specific things, maybe paths in V0, maybe containing tokens or a class that does something similar for tokens. The idea underlying these is really the same idea you already implemented with
TokenSource
🙏 I think you said you liked it 😊 I am hoping we can apply it here! Because I do think it would work on V0, I even drafted some initial PR to look at the code and make sure I'm not missing things, and if that is so, idk, is there a reason why something like that wouldn't work on V1? It was this stuff (sorry it's a lot of stuffs): https://github.com/All-Hands-AI/OpenHands/issues/10850
s
Let me review and get back to you later tonight
gratitude thank you 1
c
Just to note, so what I hear is that there's no intention to actually use it, and in that case, I would really love an alternative solution. I'm happy to give it more thought if something's wrong with the linked one, or anything
w
Which piece specifically do you want to replace Engel?
c
user_id
lolsob
s
I'm at the gym, but will be back later - The issue I see is that it is difficult to remove filter criteria from the innermost part of services and keep them efficient - most will execute some sort of query, and regardless of the medium, those queries are typically more efficient if the filter criteria is present up front. ( Think executing an SQL query with a where clause vs without one and applying the criteria afterwards.)
The solution as is at least hides the internal implementation details of services from each other
c
I think encapsulating it for routes still makes it available where needed… Just to understand, is the performance concern present in OH too since it’s stored and there’s an index… but I’m a bit confused here (why an index on user?)
> The database is sqllite. I'm personally hesitant to introduce sqllite on top of our existing file store, but it does mean that we only need one service for accessing conversations for both enterprise and OSS And postgres?
Copy code
if self.host:
                url = f'postgresql+asyncpg://{self.user}:{self.password}@{self.host}:{self.port}/{self.name}'
s
It will use postgres if a db host is specified, and fallback to sqlite if not
The idea is that we can use one set of persistence code for both use cases
(And alembic is used for migrations in both cases)
(Though there are 2 distinct sets of alembic migrations)
🤔 1
With regard to user_ids - on some level we need a user id before executing a query for filtering - whether we use SQL, nosql or a file store. We settled on SQL because it means we have less duplication. But on some level we will be doing "where user_id=?". If we put this in a user context object, we end up passing that around rather than an id. (We have this with the user_info object for when we need other user attributes like keys, secrets, email, etc) But putting the service criteria in a user info / user context means that object needs to know about every service - which feels more complex rather than less so. As is, we isolate the logic for retrieving the various types (sandboxes, conversations, event_callbacks) within a service designed for that task and only that task. I feel this is clean. I am going to clean up the resolver logic tonight too TBH my worries here actually surround the resolvers rather than the services - they feel overly complex and I am looking for simplifications (Getting a resolver from a manager works well with fastapi but feels too complicated and I'm looking for a simpler syntax)
The reason for being explicit in the resolvers "get_for_current_user" vs "get_unsecured" was terror at inadvertently using an unsecured service in a context where a secured one was expected
c
Re: the inadvertently using an unsecured service issue, quick questions to make sure I understand correctly • does this refer to secured sandbox, a sandbox with agent_server? • does OH (open source) connect with a secured sandbox?
s
This refers to the app server - where a user says "Give me a list of all my conversations" - and they get ones that don't belong to them 😱
The app server will connect to sandboxes which run the agent server. Both these are OSS - though the OSS code and SAAS code will now have more in common
c
I hear you, but then the second question still stands… the list of conversations that don’t belong to this user, is that ever an OH concern? It isn’t, as far as I can tell. I am not trying to debate the point, just to understand first. You have put a lot of work in that PR! And I see there are a lot of changes, the interface of OH with the outside world changes… 🤔 That means the shared interface 🤔
s
That is why the services themselves typically have no reference to user_id - you have methods like "search_sandboxes" and "get_sandboxes" - the my sandboxes filter is hidden in the service itself
The only place it leaks out is the resolver
c
But putting the service criteria in a user info / user context means that object needs to know about every service - which feels more complex rather than less so.
Isn’t it the other way around maybe, the service needs to get / know user info / user context, rather than getting a primitive?
I do think we … really need to find another name for this resolver… it’s the 3rd ! lolsob
s
We have the UserInfo object which we could pass into each service rather than the Id primitive if the consensus is that is simpler - but the service will then simply extract the user_id and use it. I figured it was simpler just to use the primitive directly 🤷‍♂️ Particularly given that the dependency injection handles all the plumbing
I called these resolvers "Managers", but I don't much like that either
Maybe I should call them "Injectors"
The code becomes:
Copy code
def my_fastapi_endpoint(user_service = Depends(user_service_injector)):
  ... Do something with the service
(And
UserServiceManager
becomes
UserServiceIniector
)
I suppose if we used the info object rather than the primitive, we get a solution to the "you are not logged in" vs "we don't care about users" issue
Not logged in - no user info
Logged in but we don't care about IDs - you have a user info but it has no user id primitive
I guess that is more explicit and secure
And our injectors then have only one resolve method
If you want a custom user info, you pass one in
I like this
c
Yes, and it could be even more… We could put into this object more things we need, because it’s in FastAPI Depends, it becomes the new shared interface between OH and Saas
s
We actually already have it
UserInfo
c
Yes
s
I just haven't used it in this context yet
c
I know, though for some reason I don’t love that name, idk why…
s
Ok - I'll update the PR - and I'll call it user context
I think that better mirrors other systems
And hopefully gives more of a clue as to what it is
c
Ah, maybe I know why: I think what I’m trying to suggest really is the “request context pattern” which names it a lot with “context”, yes
nod 1
s
Thank you
❤️ 1
I think this has been really productive