@app-vitals/agent-admin 1.107.3 → 1.107.4

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.
package/src/admin-ui.ts CHANGED
@@ -138,6 +138,7 @@ import {
138
138
  import {
139
139
  type CallerScopeResolver,
140
140
  memberOnlyCallerScopeResolver,
141
+ scopeCanManageAgent,
141
142
  scopeIncludesAgent,
142
143
  type ViewerRole,
143
144
  } from "./caller-scope.ts";
@@ -1646,21 +1647,21 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
1646
1647
  // ─── Agent detail ─────────────────────────────────────────────────────────
1647
1648
 
1648
1649
  /**
1649
- * SSP-4.3: with self-serve on, anyone who passes assertAgentAccess for an
1650
- * agent (owner or member) fully controls it — settings, members, delete.
1651
- * Flag off: admin-only, so existing platform AgentMembers gain no power.
1650
+ * Agent management (settings, members, credentials, tools, crons, tokens,
1651
+ * delete) is limited to platform admins and the owner of the agent's own
1652
+ * account (scopeCanManageAgent). Members keep using the agent (read, chat).
1652
1653
  */
1653
- const canManageAgents = (isAdmin: boolean): boolean =>
1654
- isAdmin || deps.selfServe?.enabled === true;
1655
-
1656
1654
  async function assertCanManageAgent(
1657
1655
  agentId: string,
1658
1656
  userEmail: string,
1659
1657
  isAdmin: boolean,
1660
1658
  ): Promise<boolean> {
1661
1659
  if (isAdmin) return true;
1662
- if (!canManageAgents(false)) return false;
1663
- return assertAgentAccess(agentId, userEmail, false);
1660
+ const [scope, agent] = await Promise.all([
1661
+ callerScopeResolver(userEmail, false),
1662
+ agentService.getDetail(agentId),
1663
+ ]);
1664
+ return scopeCanManageAgent(scope, agent?.accountId);
1664
1665
  }
1665
1666
 
1666
1667
  const ERROR_MESSAGES: Record<string, string> = {
@@ -2681,6 +2682,11 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2681
2682
  if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
2682
2683
  return new Response("Forbidden", { status: 403 });
2683
2684
  }
2685
+ const canManage = await assertCanManageAgent(
2686
+ agentId,
2687
+ c.var.userEmail,
2688
+ c.var.isAdmin,
2689
+ );
2684
2690
 
2685
2691
  const rawError = c.req.query("error") ?? undefined;
2686
2692
  const error = rawError ? (ERROR_MESSAGES[rawError] ?? rawError) : undefined;
@@ -2722,7 +2728,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2722
2728
  agentToolService.list(agentId),
2723
2729
  agentTokenService.listForAgent(agentId),
2724
2730
  agentPluginService.list(agentId),
2725
- canManageAgents(c.var.isAdmin)
2731
+ canManage
2726
2732
  ? agentMemberService.listByAgentId(agentId)
2727
2733
  : Promise.resolve([]),
2728
2734
  agentMarketplaceService?.list(agentId) ?? Promise.resolve([]),
@@ -2794,7 +2800,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2794
2800
  c.var.isAdmin,
2795
2801
  {
2796
2802
  section,
2797
- canManage: canManageAgents(c.var.isAdmin),
2803
+ canManage,
2798
2804
  error,
2799
2805
  newToken,
2800
2806
  successMsg,
@@ -2841,7 +2847,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2841
2847
  if (!agent) {
2842
2848
  return new Response("Agent not found", { status: 404 });
2843
2849
  }
2844
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
2850
+ if (
2851
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
2852
+ ) {
2845
2853
  return new Response("Forbidden", { status: 403 });
2846
2854
  }
2847
2855
  const envResult = await agentEnvService.getByAgentId(agentId);
@@ -2901,7 +2909,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2901
2909
 
2902
2910
  app.post("/admin/agents/:id/envs", requireAuth, async (c) => {
2903
2911
  const agentId = c.req.param("id");
2904
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
2912
+ if (
2913
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
2914
+ ) {
2905
2915
  return new Response("Forbidden", { status: 403 });
2906
2916
  }
2907
2917
  let key: string | undefined;
@@ -2940,7 +2950,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2940
2950
 
2941
2951
  app.post("/admin/agents/:id/envs/delete", requireAuth, async (c) => {
2942
2952
  const agentId = c.req.param("id");
2943
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
2953
+ if (
2954
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
2955
+ ) {
2944
2956
  return new Response("Forbidden", { status: 403 });
2945
2957
  }
2946
2958
  let key: string | undefined;
@@ -2977,7 +2989,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
2977
2989
  ): Promise<Response> {
2978
2990
  const agentId = c.req.param("id") ?? "";
2979
2991
  if (!agentsApi) return new Response("Not found", { status: 404 });
2980
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
2992
+ if (
2993
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
2994
+ ) {
2981
2995
  return new Response("Forbidden", { status: 403 });
2982
2996
  }
2983
2997
  const res = await callApi(
@@ -3046,7 +3060,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3046
3060
 
3047
3061
  app.post("/admin/agents/:id/repos/add", requireAuth, async (c) => {
3048
3062
  const agentId = c.req.param("id");
3049
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3063
+ if (
3064
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3065
+ ) {
3050
3066
  return new Response("Forbidden", { status: 403 });
3051
3067
  }
3052
3068
  let repo: string | undefined;
@@ -3083,7 +3099,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3083
3099
 
3084
3100
  app.post("/admin/agents/:id/repos/delete", requireAuth, async (c) => {
3085
3101
  const agentId = c.req.param("id");
3086
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3102
+ if (
3103
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3104
+ ) {
3087
3105
  return new Response("Forbidden", { status: 403 });
3088
3106
  }
3089
3107
  let repo: string | undefined;
@@ -3111,7 +3129,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3111
3129
  requireAuth,
3112
3130
  async (c) => {
3113
3131
  const agentId = c.req.param("id");
3114
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3132
+ if (
3133
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3134
+ ) {
3115
3135
  return new Response("Forbidden", { status: 403 });
3116
3136
  }
3117
3137
  let login: string | undefined;
@@ -3149,7 +3169,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3149
3169
  requireAuth,
3150
3170
  async (c) => {
3151
3171
  const agentId = c.req.param("id");
3152
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3172
+ if (
3173
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3174
+ ) {
3153
3175
  return new Response("Forbidden", { status: 403 });
3154
3176
  }
3155
3177
  let login: string | undefined;
@@ -3182,7 +3204,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3182
3204
  requireAuth,
3183
3205
  async (c) => {
3184
3206
  const agentId = c.req.param("id");
3185
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3207
+ if (
3208
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3209
+ ) {
3186
3210
  return new Response("Forbidden", { status: 403 });
3187
3211
  }
3188
3212
  let login: string | undefined;
@@ -3220,7 +3244,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3220
3244
  requireAuth,
3221
3245
  async (c) => {
3222
3246
  const agentId = c.req.param("id");
3223
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3247
+ if (
3248
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3249
+ ) {
3224
3250
  return new Response("Forbidden", { status: 403 });
3225
3251
  }
3226
3252
  let login: string | undefined;
@@ -3332,7 +3358,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3332
3358
 
3333
3359
  app.post("/admin/agents/:id/crons", requireAuth, async (c) => {
3334
3360
  const agentId = c.req.param("id");
3335
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3361
+ if (
3362
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3363
+ ) {
3336
3364
  return new Response("Forbidden", { status: 403 });
3337
3365
  }
3338
3366
  let schedule: string | undefined;
@@ -3409,7 +3437,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3409
3437
  app.post("/admin/agents/:id/crons/:cronId/toggle", requireAuth, async (c) => {
3410
3438
  const agentId = c.req.param("id");
3411
3439
  const cronId = c.req.param("cronId");
3412
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3440
+ if (
3441
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3442
+ ) {
3413
3443
  return new Response("Forbidden", { status: 403 });
3414
3444
  }
3415
3445
  let enabled = true;
@@ -3430,7 +3460,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3430
3460
  app.post("/admin/agents/:id/crons/:cronId/update", requireAuth, async (c) => {
3431
3461
  const agentId = c.req.param("id");
3432
3462
  const cronId = c.req.param("cronId");
3433
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3463
+ if (
3464
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3465
+ ) {
3434
3466
  return new Response("Forbidden", { status: 403 });
3435
3467
  }
3436
3468
  let schedule = "";
@@ -3493,7 +3525,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3493
3525
  app.post("/admin/agents/:id/crons/:cronId/delete", requireAuth, async (c) => {
3494
3526
  const agentId = c.req.param("id");
3495
3527
  const cronId = c.req.param("cronId");
3496
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3528
+ if (
3529
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3530
+ ) {
3497
3531
  return new Response("Forbidden", { status: 403 });
3498
3532
  }
3499
3533
  try {
@@ -3518,7 +3552,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3518
3552
 
3519
3553
  app.post("/admin/agents/:id/tools", requireAuth, async (c) => {
3520
3554
  const agentId = c.req.param("id");
3521
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3555
+ if (
3556
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3557
+ ) {
3522
3558
  return new Response("Forbidden", { status: 403 });
3523
3559
  }
3524
3560
  let pattern: string | undefined;
@@ -3545,7 +3581,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3545
3581
  app.post("/admin/agents/:id/tools/:toolId/toggle", requireAuth, async (c) => {
3546
3582
  const agentId = c.req.param("id");
3547
3583
  const toolId = c.req.param("toolId");
3548
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3584
+ if (
3585
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3586
+ ) {
3549
3587
  return new Response("Forbidden", { status: 403 });
3550
3588
  }
3551
3589
  let enabled = true;
@@ -3566,7 +3604,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3566
3604
  app.post("/admin/agents/:id/tools/:toolId/delete", requireAuth, async (c) => {
3567
3605
  const agentId = c.req.param("id");
3568
3606
  const toolId = c.req.param("toolId");
3569
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3607
+ if (
3608
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3609
+ ) {
3570
3610
  return new Response("Forbidden", { status: 403 });
3571
3611
  }
3572
3612
  try {
@@ -3583,7 +3623,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3583
3623
 
3584
3624
  app.post("/admin/agents/:id/marketplaces", requireAuth, async (c) => {
3585
3625
  const agentId = c.req.param("id");
3586
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3626
+ if (
3627
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3628
+ ) {
3587
3629
  return new Response("Forbidden", { status: 403 });
3588
3630
  }
3589
3631
  let name: string | undefined;
@@ -3619,7 +3661,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3619
3661
 
3620
3662
  app.post("/admin/agents/:id/marketplaces/delete", requireAuth, async (c) => {
3621
3663
  const agentId = c.req.param("id");
3622
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3664
+ if (
3665
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3666
+ ) {
3623
3667
  return new Response("Forbidden", { status: 403 });
3624
3668
  }
3625
3669
  let marketplaceId: string | undefined;
@@ -3646,7 +3690,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3646
3690
 
3647
3691
  app.post("/admin/agents/:id/plugins", requireAuth, async (c) => {
3648
3692
  const agentId = c.req.param("id");
3649
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3693
+ if (
3694
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3695
+ ) {
3650
3696
  return new Response("Forbidden", { status: 403 });
3651
3697
  }
3652
3698
  let raw: string | undefined;
@@ -3675,7 +3721,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3675
3721
 
3676
3722
  app.post("/admin/agents/:id/plugins/delete", requireAuth, async (c) => {
3677
3723
  const agentId = c.req.param("id");
3678
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3724
+ if (
3725
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3726
+ ) {
3679
3727
  return new Response("Forbidden", { status: 403 });
3680
3728
  }
3681
3729
  let pluginId: string | undefined;
@@ -3699,9 +3747,12 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3699
3747
 
3700
3748
  app.post("/admin/agents/:id/tokens", requireAuth, async (c) => {
3701
3749
  const agentId = c.req.param("id");
3702
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3750
+ if (
3751
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3752
+ ) {
3703
3753
  return new Response("Forbidden", { status: 403 });
3704
3754
  }
3755
+ const canManage = true; // gated by assertCanManageAgent above
3705
3756
  let label: string | undefined;
3706
3757
  try {
3707
3758
  const formData = await c.req.formData();
@@ -3752,7 +3803,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3752
3803
  agentToolService.list(agentId),
3753
3804
  agentTokenService.listForAgent(agentId),
3754
3805
  agentPluginService.list(agentId),
3755
- canManageAgents(c.var.isAdmin)
3806
+ canManage
3756
3807
  ? agentMemberService.listByAgentId(agentId)
3757
3808
  : Promise.resolve([]),
3758
3809
  agentMarketplaceService?.list(agentId) ?? Promise.resolve([]),
@@ -3783,7 +3834,7 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3783
3834
  c.var.isAdmin,
3784
3835
  {
3785
3836
  section: "access",
3786
- canManage: canManageAgents(c.var.isAdmin),
3837
+ canManage,
3787
3838
  newToken: rawToken,
3788
3839
  timezone,
3789
3840
  extensionCardsHtml: cards.header,
@@ -3823,7 +3874,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3823
3874
  async (c) => {
3824
3875
  const agentId = c.req.param("id");
3825
3876
  const tokenId = c.req.param("tokenId");
3826
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3877
+ if (
3878
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3879
+ ) {
3827
3880
  return new Response("Forbidden", { status: 403 });
3828
3881
  }
3829
3882
  try {
@@ -3846,7 +3899,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3846
3899
 
3847
3900
  app.post("/admin/agents/:id/sync-manifest", requireAuth, async (c) => {
3848
3901
  const agentId = c.req.param("id");
3849
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
3902
+ if (
3903
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
3904
+ ) {
3850
3905
  return new Response("Forbidden", { status: 403 });
3851
3906
  }
3852
3907
  const agent = await agentService.getDetail(agentId);
@@ -3987,7 +4042,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
3987
4042
 
3988
4043
  app.post("/admin/agents/:id/connect-slack", requireAuth, async (c) => {
3989
4044
  const agentId = c.req.param("id");
3990
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4045
+ if (
4046
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4047
+ ) {
3991
4048
  return new Response("Forbidden", { status: 403 });
3992
4049
  }
3993
4050
 
@@ -4031,7 +4088,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4031
4088
  requireAuth,
4032
4089
  async (c) => {
4033
4090
  const agentId = c.req.param("id");
4034
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4091
+ if (
4092
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4093
+ ) {
4035
4094
  return new Response("Forbidden", { status: 403 });
4036
4095
  }
4037
4096
  const userEmail = c.var.userEmail;
@@ -4141,7 +4200,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4141
4200
  requireAuth,
4142
4201
  async (c) => {
4143
4202
  const agentId = c.req.param("id");
4144
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4203
+ if (
4204
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4205
+ ) {
4145
4206
  return new Response("Forbidden", { status: 403 });
4146
4207
  }
4147
4208
  const userEmail = c.var.userEmail;
@@ -4202,7 +4263,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4202
4263
 
4203
4264
  app.post("/admin/agents/:id/connect-github", requireAuth, async (c) => {
4204
4265
  const agentId = c.req.param("id");
4205
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4266
+ if (
4267
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4268
+ ) {
4206
4269
  return new Response("Forbidden", { status: 403 });
4207
4270
  }
4208
4271
  const userEmail = c.var.userEmail;
@@ -4328,7 +4391,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4328
4391
  requireAuth,
4329
4392
  async (c) => {
4330
4393
  const agentId = c.req.param("id");
4331
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4394
+ if (
4395
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4396
+ ) {
4332
4397
  return new Response("Forbidden", { status: 403 });
4333
4398
  }
4334
4399
  const userEmail = c.var.userEmail;
@@ -4430,7 +4495,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4430
4495
  requireAuth,
4431
4496
  async (c) => {
4432
4497
  const agentId = c.req.param("id");
4433
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4498
+ if (
4499
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4500
+ ) {
4434
4501
  return new Response("Forbidden", { status: 403 });
4435
4502
  }
4436
4503
  const userEmail = c.var.userEmail;
@@ -4485,7 +4552,9 @@ export function createAdminUIApp(deps: AdminUIDeps): Hono<AdminUIEnv> {
4485
4552
  requireAuth,
4486
4553
  async (c) => {
4487
4554
  const agentId = c.req.param("id");
4488
- if (!(await assertAgentAccess(agentId, c.var.userEmail, c.var.isAdmin))) {
4555
+ if (
4556
+ !(await assertCanManageAgent(agentId, c.var.userEmail, c.var.isAdmin))
4557
+ ) {
4489
4558
  return new Response("Forbidden", { status: 403 });
4490
4559
  }
4491
4560
  const result = await githubProvisioningService.getAddOrgUrl(agentId);
@@ -0,0 +1,269 @@
1
+ /**
2
+ * admin/src/agent-management-access.integration.test.ts
3
+ * Agent management (JSON API) against a real PostgreSQL DB with self-serve on:
4
+ * only platform admins and the owner of the agent's own account may manage an
5
+ * agent; a member of the agent (or of its account) and an owner of another
6
+ * account get 403, while read access is unchanged.
7
+ *
8
+ * Requires DATABASE_URL_ADMIN_TEST to be set; skips otherwise.
9
+ */
10
+
11
+ import { afterEach, beforeEach, describe, expect, it } from "bun:test";
12
+ import { sign } from "hono/jwt";
13
+ import type { PrismaClient } from "../prisma/client/client.ts";
14
+ import { AccountService } from "./accounts.ts";
15
+ import { AgentChatTokenService } from "./agent-chat-tokens.ts";
16
+ import { AgentCronJobService } from "./agent-cron-jobs.ts";
17
+ import { AgentCronRunStatsService } from "./agent-cron-run-stats.ts";
18
+ import { AgentCronRunService } from "./agent-cron-runs.ts";
19
+ import { AgentEnvService } from "./agent-envs.ts";
20
+ import { AgentGitHubInstallationsService } from "./agent-github-installations.ts";
21
+ import { AgentMemberService } from "./agent-members.ts";
22
+ import { AgentPluginService } from "./agent-plugins.ts";
23
+ import type { AgentProvisioner } from "./agent-provisioner.ts";
24
+ import { NoopAgentProvisioner } from "./agent-provisioner.ts";
25
+ import { AgentTokenService } from "./agent-tokens.ts";
26
+ import { AgentToolService } from "./agent-tools.ts";
27
+ import { AgentService } from "./agents.ts";
28
+ import type { AdminDeps } from "./agents-api.ts";
29
+ import { createAdminApp } from "./agents-api.ts";
30
+ import {
31
+ callerScopeDepsFromServices,
32
+ createCallerScopeResolver,
33
+ } from "./caller-scope.ts";
34
+ import { NoopChatServiceProvisioningClient } from "./chat-service-provisioning-client.ts";
35
+ import { createAdminPrismaClient } from "./prisma-client.ts";
36
+ import type {
37
+ SpecialtyType,
38
+ SpecialtyTypeResolver,
39
+ } from "./specialty-types.ts";
40
+ import { makeTokenCrypto } from "./token-crypto.ts";
41
+
42
+ const TEST_DB = process.env.DATABASE_URL_ADMIN_TEST;
43
+ const describeOrSkip = TEST_DB ? describe : describe.skip;
44
+
45
+ const SESSION_SECRET = "test-admin-session-secret-32-bytes!";
46
+ const REAL_KEY =
47
+ "0000000000000000000000000000000000000000000000000000000000000001";
48
+
49
+ /** A minimal valid "coding" manifest used to drive seeding assertions below. */
50
+ const CODING_TYPE: SpecialtyType = {
51
+ name: "coding",
52
+ displayName: "Coding Agent",
53
+ templatesDir: "workspace/",
54
+ tools: [
55
+ "Read",
56
+ "Write",
57
+ "Edit",
58
+ "Glob",
59
+ "Grep",
60
+ "Bash",
61
+ "WebSearch",
62
+ "WebFetch",
63
+ "Skill",
64
+ "Agent",
65
+ ],
66
+ requiredEnv: [],
67
+ };
68
+
69
+ function fakeSpecialtyTypes(
70
+ byType: Record<string, SpecialtyType> = { coding: CODING_TYPE },
71
+ ): SpecialtyTypeResolver {
72
+ return {
73
+ getType(specialty: string): SpecialtyType {
74
+ return byType[specialty] ?? (byType.coding as SpecialtyType);
75
+ },
76
+ tryGetType(specialty: string): SpecialtyType | undefined {
77
+ return byType[specialty];
78
+ },
79
+ listTypes() {
80
+ return Object.entries(byType).map(([name, manifest]) => ({
81
+ name,
82
+ displayName: manifest.displayName,
83
+ }));
84
+ },
85
+ };
86
+ }
87
+
88
+ function makeDeps(
89
+ prisma: PrismaClient,
90
+ provisioner: AgentProvisioner = new NoopAgentProvisioner(),
91
+ specialtyRegistry: SpecialtyTypeResolver = fakeSpecialtyTypes(),
92
+ ): AdminDeps {
93
+ const savedKey = process.env.SHIPWRIGHT_ENCRYPTION_KEY;
94
+ process.env.SHIPWRIGHT_ENCRYPTION_KEY = REAL_KEY;
95
+ const crypto = makeTokenCrypto();
96
+ if (savedKey === undefined) {
97
+ delete process.env.SHIPWRIGHT_ENCRYPTION_KEY;
98
+ } else {
99
+ process.env.SHIPWRIGHT_ENCRYPTION_KEY = savedKey;
100
+ }
101
+
102
+ return {
103
+ agentService: new AgentService(prisma),
104
+ agentEnvService: new AgentEnvService(prisma, crypto),
105
+ agentCronJobService: new AgentCronJobService(prisma),
106
+ agentCronRunService: new AgentCronRunService(prisma),
107
+ agentCronRunStatsService: new AgentCronRunStatsService(prisma),
108
+ agentToolService: new AgentToolService(prisma),
109
+ agentTokenService: new AgentTokenService(prisma),
110
+ agentPluginService: new AgentPluginService(prisma),
111
+ agentMemberService: new AgentMemberService(prisma),
112
+ specialtyRegistry,
113
+ agentChatTokenService: new AgentChatTokenService(prisma),
114
+ agentGitHubInstallationsService: new AgentGitHubInstallationsService(
115
+ prisma,
116
+ ),
117
+ prisma,
118
+ provisioner,
119
+ serviceIntegrations: [
120
+ {
121
+ name: "task-store",
122
+ listTokensForAgent: async () => [],
123
+ revokeToken: async () => {},
124
+ },
125
+ ],
126
+ chatService: new NoopChatServiceProvisioningClient(),
127
+ slack: {
128
+ deleteApp: async () => {},
129
+ },
130
+ decrypt: (value: string) => crypto.decrypt(value),
131
+ sessionSecret: SESSION_SECRET,
132
+ };
133
+ }
134
+
135
+ const cookieFor = (email: string, isAdmin: boolean) =>
136
+ sign(
137
+ {
138
+ isAdmin,
139
+ userId: `user-${email}`,
140
+ email,
141
+ iat: Math.floor(Date.now() / 1000),
142
+ exp: Math.floor(Date.now() / 1000) + 3600,
143
+ },
144
+ SESSION_SECRET,
145
+ "HS256",
146
+ );
147
+
148
+ describeOrSkip("agent management access (integration)", () => {
149
+ let prisma: PrismaClient;
150
+ let app: ReturnType<typeof createAdminApp>;
151
+ let agentId: string;
152
+
153
+ const call = async (
154
+ email: string,
155
+ isAdmin: boolean,
156
+ method: string,
157
+ path: string,
158
+ body?: unknown,
159
+ ) =>
160
+ app.request(`/agents/${agentId}${path}`, {
161
+ method,
162
+ body: JSON.stringify(body ?? {}),
163
+ headers: {
164
+ "Content-Type": "application/json",
165
+ Cookie: `admin_session=${await cookieFor(email, isAdmin)}`,
166
+ },
167
+ });
168
+
169
+ beforeEach(async () => {
170
+ prisma = createAdminPrismaClient(TEST_DB as string);
171
+ await prisma.agentWorkQueueSnapshot.deleteMany();
172
+ await prisma.agentPlugin.deleteMany();
173
+ await prisma.agentPhaseMethodology.deleteMany();
174
+ await prisma.agentToken.deleteMany();
175
+ await prisma.agentCronJob.deleteMany();
176
+ await prisma.agentTool.deleteMany();
177
+ await prisma.agentEnv.deleteMany();
178
+ await prisma.agentMember.deleteMany();
179
+ await prisma.agent.deleteMany();
180
+ await prisma.accountMember.deleteMany();
181
+ await prisma.account.deleteMany();
182
+
183
+ const one = await prisma.account.create({ data: { name: "Account One" } });
184
+ const two = await prisma.account.create({ data: { name: "Account Two" } });
185
+ const mk = (accountId: string, email: string, role: string) =>
186
+ prisma.accountMember.create({ data: { accountId, email, role } });
187
+ await mk(one.id, "owner-one@example.com", "owner");
188
+ const member = await mk(one.id, "member-one@example.com", "member");
189
+ await mk(two.id, "owner-two@example.com", "owner");
190
+ agentId = (
191
+ await prisma.agent.create({
192
+ data: { name: "Test Agent", accountId: one.id },
193
+ })
194
+ ).id;
195
+ await prisma.agentMember.create({
196
+ data: { agentId, memberId: member.id },
197
+ });
198
+
199
+ const deps = makeDeps(prisma);
200
+ app = createAdminApp({
201
+ ...deps,
202
+ callerScopeResolver: createCallerScopeResolver(
203
+ callerScopeDepsFromServices({
204
+ accountService: new AccountService(prisma),
205
+ agentMemberService: new AgentMemberService(prisma),
206
+ }),
207
+ true,
208
+ ),
209
+ });
210
+ });
211
+
212
+ afterEach(async () => {
213
+ await prisma.$disconnect();
214
+ });
215
+
216
+ const writes = (): Array<[string, string, unknown?]> => [
217
+ ["PATCH", "", { cleanupAfterDays: 20 }],
218
+ ["POST", "/envs", { key: "K", value: "V" }],
219
+ ["POST", "/tokens", { label: "l" }],
220
+ ["DELETE", ""],
221
+ ];
222
+
223
+ it("member of the agent: reads and asks stay open, every management route is 403", async () => {
224
+ const email = "member-one@example.com";
225
+ expect((await call(email, false, "GET", "")).status).toBe(200);
226
+ expect((await call(email, false, "GET", "/envs")).status).toBe(200);
227
+ for (const [method, path, body] of writes()) {
228
+ const res = await call(email, false, method, path, body);
229
+ expect([method, path, res.status]).toEqual([method, path, 403]);
230
+ }
231
+ const row = await prisma.agent.findUniqueOrThrow({
232
+ where: { id: agentId },
233
+ });
234
+ expect(row.cleanupAfterDays).not.toBe(20);
235
+ expect(await prisma.agentToken.count({ where: { agentId } })).toBe(0);
236
+ });
237
+
238
+ it("owner of another account: no access at all", async () => {
239
+ const email = "owner-two@example.com";
240
+ expect((await call(email, false, "GET", "")).status).toBe(403);
241
+ for (const [method, path, body] of writes()) {
242
+ const res = await call(email, false, method, path, body);
243
+ expect([method, path, res.status]).toEqual([method, path, 403]);
244
+ }
245
+ expect(await prisma.agent.count({ where: { id: agentId } })).toBe(1);
246
+ });
247
+
248
+ it("owner of the agent's account manages it, including delete", async () => {
249
+ const email = "owner-one@example.com";
250
+ expect((await call(email, false, "GET", "")).status).toBe(200);
251
+ expect(
252
+ (await call(email, false, "PATCH", "", { cleanupAfterDays: 20 })).status,
253
+ ).toBe(200);
254
+ expect(
255
+ (await call(email, false, "POST", "/envs", { key: "K", value: "V" }))
256
+ .status,
257
+ ).toBe(201);
258
+ expect((await call(email, false, "DELETE", "")).status).toBe(200);
259
+ expect(await prisma.agent.count({ where: { id: agentId } })).toBe(0);
260
+ });
261
+
262
+ it("platform admin manages any agent", async () => {
263
+ const email = "admin@example.com";
264
+ expect(
265
+ (await call(email, true, "PATCH", "", { cleanupAfterDays: 20 })).status,
266
+ ).toBe(200);
267
+ expect((await call(email, true, "DELETE", "")).status).toBe(200);
268
+ });
269
+ });