Skip to content

Commit a70543b

Browse files
[Authz 4/N]: RBAC changes for listing agents, schedules, sessions (#600)
* [Authz 4/N]: RBAC changes for listing agents, schedules, sessions * Regenerate OpenAPI document and TypeScript SDK --------- Co-authored-by: trueforge-dev-bot[bot] <trueforge-dev-bot[bot]@users.noreply.github.com>
1 parent 4b1aa55 commit a70543b

30 files changed

Lines changed: 630 additions & 193 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@truefoundry/trueforge-core': minor
3+
'@truefoundry/trueforge': minor
4+
---
5+
6+
Agent access is decided only by the `Authorizer`: standalone/OIDC lets everyone list and use agents and restricts update/delete to the creator; TrueFoundry uses external permissions api.
7+
8+
You can read a session, turn, events, metrics, schedule, or its runs if you created it, or if you manage the named agent it is bound to. Creating still requires permission to use that agent. Only the creator can update, delete, or cancel a session, create a turn, or download sandbox files. Only the creator can update, delete, pause, resume, or run a schedule. An OIDC settings admin can no longer see other users' schedules.

‎.github/fern/openapi/openapi.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6704,7 +6704,7 @@
67046704
},
67056705
"/api/v1/schedules/{schedule_id}/runs": {
67066706
"get": {
6707-
"description": "List runs of a schedule, newest `scheduled_for` first. Only the schedule creator (or an admin) may list its runs.",
6707+
"description": "List runs of a schedule, newest `scheduled_for` first. Available to its creator or a manager of its agent.",
67086708
"parameters": [
67096709
{
67106710
"description": "Immutable schedule identifier.",
@@ -6738,7 +6738,7 @@
67386738
}
67396739
}
67406740
},
6741-
"description": "The caller is not the schedule creator."
6741+
"description": "The caller cannot read the schedule."
67426742
},
67436743
"404": {
67446744
"content": {

‎docs/openapi.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6704,7 +6704,7 @@
67046704
},
67056705
"/api/v1/schedules/{schedule_id}/runs": {
67066706
"get": {
6707-
"description": "List runs of a schedule, newest `scheduled_for` first. Only the schedule creator (or an admin) may list its runs.",
6707+
"description": "List runs of a schedule, newest `scheduled_for` first. Available to its creator or a manager of its agent.",
67086708
"parameters": [
67096709
{
67106710
"description": "Immutable schedule identifier.",
@@ -6738,7 +6738,7 @@
67386738
}
67396739
}
67406740
},
6741-
"description": "The caller is not the schedule creator."
6741+
"description": "The caller cannot read the schedule."
67426742
},
67436743
"404": {
67446744
"content": {

‎packages/trueforge-core/src/agent-session/store/ISessionStore.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,13 @@ export interface ListSessionsInput {
6464
end_timestamp: Date | undefined;
6565
/** When set, only sessions bound to this named agent id. */
6666
agent_id: string | undefined;
67-
/** When set, only sessions whose `created_by_subject.subject_id` matches. */
68-
created_by_subject_id: string | undefined;
67+
/** When set, match creator or named-agent binding. Empty `agent_ids` means creator-only. */
68+
created_by_or_agent_ids:
69+
| {
70+
created_by_subject_id: string;
71+
agent_ids: readonly string[];
72+
}
73+
| undefined;
6974
}
7075

7176
/** Turn row fields without assembled snapshot (create input / listTurns). */
@@ -256,9 +261,9 @@ export interface ISessionStore<
256261
* (`order` defaults to `desc`, `session_id` tie-break). `page_token` is a
257262
* keyset cursor on `(updated_at, session_id)`. `start_timestamp` /
258263
* `end_timestamp` are inclusive instant bounds on `created_at`. Optional
259-
* `agent_id` filters ref-bound sessions; optional `created_by_subject_id`
260-
* filters by `created_by_subject.subject_id`. Does **not** bump
261-
* `last_activity_timestamp_ms` (read path).
264+
* `agent_id` filters ref-bound sessions. `created_by_or_agent_ids` matches
265+
* rows created by its subject OR bound to one of its agent ids. Does **not**
266+
* bump `last_activity_timestamp_ms` (read path).
262267
*/
263268
listSessions(
264269
input: ListSessionsInput,

‎packages/trueforge-core/src/agent-session/store/InMemorySessionStore.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export class InMemorySessionStore<
263263
input: ListSessionsInput,
264264
): Promise<{ data: SessionRecord<TSessionCustom>[]; pagination: TokenPagination }> {
265265
const records: SessionRecord<TSessionCustom>[] = [];
266+
const createdByOrAgentIds = input.created_by_or_agent_ids;
266267
for (const stored of this.sessions.values()) {
267268
if (stored.record.tenant_id !== input.tenant_id) {
268269
continue;
@@ -273,11 +274,14 @@ export class InMemorySessionStore<
273274
) {
274275
continue;
275276
}
276-
if (
277-
input.created_by_subject_id !== undefined &&
278-
stored.record.created_by_subject.subject_id !== input.created_by_subject_id
279-
) {
280-
continue;
277+
if (createdByOrAgentIds !== undefined) {
278+
const creatorMatches =
279+
stored.record.created_by_subject.subject_id === createdByOrAgentIds.created_by_subject_id;
280+
const agentMatches =
281+
stored.record.agent.type === 'reference' && createdByOrAgentIds.agent_ids.includes(stored.record.agent.id);
282+
if (!creatorMatches && !agentMatches) {
283+
continue;
284+
}
281285
}
282286
const createdAt = stored.record.created_at.getTime();
283287
if (input.start_timestamp !== undefined && createdAt < input.start_timestamp.getTime()) {

‎packages/trueforge-core/tests/agent-session/store/storeContractSuite.ts‎

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
215215
start_timestamp: undefined,
216216
end_timestamp: undefined,
217217
agent_id: undefined,
218-
created_by_subject_id: undefined,
218+
created_by_or_agent_ids: undefined,
219219
});
220220
expect(listed.data.map(s => s.session_id)).toContain('created-by-session');
221221
expect(listed.data.find(s => s.session_id === 'created-by-session')?.created_by_subject.subject_id).toBe(
@@ -241,7 +241,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
241241

242242
const filtered = await store.listSessions({
243243
agent_id: 'agent-abc',
244-
created_by_subject_id: undefined,
244+
created_by_or_agent_ids: undefined,
245245
tenant_id: tenant,
246246
limit: 10,
247247
page_token: undefined,
@@ -578,7 +578,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
578578

579579
const listed = await store.listSessions({
580580
agent_id: undefined,
581-
created_by_subject_id: undefined,
581+
created_by_or_agent_ids: undefined,
582582
tenant_id: tenant,
583583
limit: 10,
584584
page_token: undefined,
@@ -834,7 +834,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
834834

835835
const desc = await store.listSessions({
836836
agent_id: undefined,
837-
created_by_subject_id: undefined,
837+
created_by_or_agent_ids: undefined,
838838
tenant_id: tenant,
839839
limit: 10,
840840
page_token: undefined,
@@ -846,7 +846,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
846846

847847
const asc = await store.listSessions({
848848
agent_id: undefined,
849-
created_by_subject_id: undefined,
849+
created_by_or_agent_ids: undefined,
850850
tenant_id: tenant,
851851
limit: 10,
852852
page_token: undefined,
@@ -863,7 +863,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
863863

864864
const first = await store.listSessions({
865865
agent_id: undefined,
866-
created_by_subject_id: undefined,
866+
created_by_or_agent_ids: undefined,
867867
tenant_id: tenant,
868868
limit: 2,
869869
page_token: undefined,
@@ -875,7 +875,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
875875
expect(first.pagination.next_page_token).toBeDefined();
876876
const second = await store.listSessions({
877877
agent_id: undefined,
878-
created_by_subject_id: undefined,
878+
created_by_or_agent_ids: undefined,
879879
tenant_id: tenant,
880880
limit: 2,
881881
page_token: first.pagination.next_page_token,
@@ -888,7 +888,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
888888

889889
const all = await store.listSessions({
890890
agent_id: undefined,
891-
created_by_subject_id: undefined,
891+
created_by_or_agent_ids: undefined,
892892
tenant_id: tenant,
893893
limit: 10,
894894
page_token: undefined,
@@ -903,7 +903,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
903903
const middleCreatedAt = middleSession.created_at;
904904
const bounded = await store.listSessions({
905905
agent_id: undefined,
906-
created_by_subject_id: undefined,
906+
created_by_or_agent_ids: undefined,
907907
tenant_id: tenant,
908908
limit: 10,
909909
order: 'asc',
@@ -928,7 +928,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
928928

929929
const listArgs = {
930930
agent_id: undefined,
931-
created_by_subject_id: undefined,
931+
created_by_or_agent_ids: undefined,
932932
tenant_id: tenant,
933933
order: undefined,
934934
start_timestamp: undefined,
@@ -954,7 +954,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
954954

955955
const listArgs = {
956956
agent_id: undefined,
957-
created_by_subject_id: undefined,
957+
created_by_or_agent_ids: undefined,
958958
tenant_id: tenant,
959959
order: 'desc' as const,
960960
start_timestamp: undefined,
@@ -975,7 +975,7 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
975975
expect(page2.pagination.next_page_token).toBeUndefined();
976976
});
977977

978-
it('filters by created_by_subject_id', async () => {
978+
it('filters by creator or named-agent ids', async () => {
979979
const store = createStore();
980980
await store.createSession({
981981
tenant_id: tenant,
@@ -995,10 +995,31 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
995995
metadata: {},
996996
external_id: null,
997997
});
998+
await store.createSession({
999+
tenant_id: tenant,
1000+
session_id: 'managed-session',
1001+
created_by_subject: { subject_id: 'charlie', subject_type: 'user', subject_display_name: 'charlie' },
1002+
agent: { type: 'reference', id: 'managed-agent', name: null },
1003+
custom: null,
1004+
metadata: {},
1005+
external_id: null,
1006+
});
1007+
await store.createSession({
1008+
tenant_id: tenant,
1009+
session_id: 'unmanaged-session',
1010+
created_by_subject: { subject_id: 'dave', subject_type: 'user', subject_display_name: 'dave' },
1011+
agent: { type: 'reference', id: 'unmanaged-agent', name: null },
1012+
custom: null,
1013+
metadata: {},
1014+
external_id: null,
1015+
});
9981016

9991017
const aliceOnly = await store.listSessions({
10001018
agent_id: undefined,
1001-
created_by_subject_id: 'alice',
1019+
created_by_or_agent_ids: {
1020+
created_by_subject_id: 'alice',
1021+
agent_ids: [],
1022+
},
10021023
tenant_id: tenant,
10031024
limit: 10,
10041025
page_token: undefined,
@@ -1009,9 +1030,42 @@ export function runStoreContractSuite(createStore: () => ISessionStore) {
10091030
expect(aliceOnly.data.map(s => s.session_id)).toEqual(['alice-session']);
10101031
expect(aliceOnly.data[0]?.created_by_subject.subject_id).toBe('alice');
10111032

1033+
const aliceOrManaged = await store.listSessions({
1034+
agent_id: undefined,
1035+
created_by_or_agent_ids: {
1036+
created_by_subject_id: 'alice',
1037+
agent_ids: ['managed-agent'],
1038+
},
1039+
tenant_id: tenant,
1040+
limit: 10,
1041+
page_token: undefined,
1042+
order: 'asc',
1043+
start_timestamp: undefined,
1044+
end_timestamp: undefined,
1045+
});
1046+
expect(aliceOrManaged.data.map(s => s.session_id)).toEqual(['alice-session', 'managed-session']);
1047+
1048+
const managedAgentOnly = await store.listSessions({
1049+
agent_id: 'managed-agent',
1050+
created_by_or_agent_ids: {
1051+
created_by_subject_id: 'alice',
1052+
agent_ids: ['managed-agent'],
1053+
},
1054+
tenant_id: tenant,
1055+
limit: 10,
1056+
page_token: undefined,
1057+
order: undefined,
1058+
start_timestamp: undefined,
1059+
end_timestamp: undefined,
1060+
});
1061+
expect(managedAgentOnly.data.map(s => s.session_id)).toEqual(['managed-session']);
1062+
10121063
const unmatched = await store.listSessions({
10131064
agent_id: undefined,
1014-
created_by_subject_id: 'nobody',
1065+
created_by_or_agent_ids: {
1066+
created_by_subject_id: 'nobody',
1067+
agent_ids: [],
1068+
},
10151069
tenant_id: tenant,
10161070
limit: 10,
10171071
page_token: undefined,

‎packages/trueforge-sdk/reference.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,7 @@ await client.schedules.delete("schedule_id");
11711171
<dl>
11721172
<dd>
11731173

1174-
List runs of a schedule, newest `scheduled_for` first. Only the schedule creator (or an admin) may list its runs.
1174+
List runs of a schedule, newest `scheduled_for` first. Available to its creator or a manager of its agent.
11751175
</dd>
11761176
</dl>
11771177
</dd>

‎packages/trueforge-sdk/src/api/resources/schedules/client/Client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ export class SchedulesClient {
751751
}
752752

753753
/**
754-
* List runs of a schedule, newest `scheduled_for` first. Only the schedule creator (or an admin) may list its runs.
754+
* List runs of a schedule, newest `scheduled_for` first. Available to its creator or a manager of its agent.
755755
*
756756
* @param {string} schedule_id - Immutable schedule identifier.
757757
* @param {SchedulesClient.RequestOptions} requestOptions - Request-specific configuration.

‎packages/trueforge/src/apis/agentAccess.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,43 @@ export async function listAccessibleAgents<TTransaction>(input: {
4141
external_ids: access.agent_external_ids,
4242
});
4343
}
44+
45+
/**
46+
* Resolve only explicit manage grants to internal ids. An `all` scope does not
47+
* identify explicit grants and must not widen related-resource visibility.
48+
*/
49+
export async function resolveManagedAgentIds<TTransaction>(input: {
50+
store: IAgentStore<TTransaction>;
51+
context: RequestContext;
52+
authorizer: Authorizer;
53+
}): Promise<string[]> {
54+
const { store, context, authorizer } = input;
55+
const access = await authorizer.listAgentAccess({ context, action: 'manage' });
56+
if (access.kind === 'all') {
57+
return [];
58+
}
59+
const agents = await store.listAgents({
60+
tenant_id: context.tenant_id,
61+
external_ids: access.agent_external_ids,
62+
});
63+
return agents.map(agent => agent.id);
64+
}
65+
66+
/** Related rows are readable by their creator or a manager of the bound named agent. */
67+
export async function canReadAgentBoundResource<TTransaction>(input: {
68+
store: IAgentStore<TTransaction>;
69+
context: RequestContext;
70+
authorizer: Authorizer;
71+
created_by_subject_id: string;
72+
agent_id: string | undefined;
73+
}): Promise<boolean> {
74+
const { store, context, authorizer, created_by_subject_id, agent_id } = input;
75+
if (created_by_subject_id === context.subject.id) {
76+
return true;
77+
}
78+
if (agent_id === undefined) {
79+
return false;
80+
}
81+
const managedAgentIds = await resolveManagedAgentIds({ store, context, authorizer });
82+
return managedAgentIds.includes(agent_id);
83+
}

0 commit comments

Comments
 (0)