@catamorphic/fastify-plugin 0.1.0-alpha.38 → 0.1.0-alpha.41

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.
Files changed (81) hide show
  1. package/NOTICE.txt +7 -0
  2. package/dist/app.d.ts +2 -0
  3. package/dist/app.d.ts.map +1 -1
  4. package/dist/chunk-jytcc42y.js +592 -0
  5. package/dist/chunk-jytcc42y.js.map +11 -0
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1679 -411
  9. package/dist/index.js.map +29 -25
  10. package/dist/mcp-scopes.d.ts +12 -0
  11. package/dist/mcp-scopes.d.ts.map +1 -0
  12. package/dist/mcp-server.d.ts +158 -0
  13. package/dist/mcp-server.d.ts.map +1 -0
  14. package/dist/mcp-shared.d.ts +3 -50
  15. package/dist/mcp-shared.d.ts.map +1 -1
  16. package/dist/mcp.d.ts +4 -2
  17. package/dist/mcp.d.ts.map +1 -1
  18. package/dist/mcp.js +6 -4
  19. package/dist/mcp.js.map +1 -1
  20. package/dist/plugin.d.ts +26 -2
  21. package/dist/plugin.d.ts.map +1 -1
  22. package/dist/project-capabilities.d.ts.map +1 -1
  23. package/dist/project-mcp-surface.d.ts +3 -4
  24. package/dist/project-mcp-surface.d.ts.map +1 -1
  25. package/dist/routes/agent.d.ts.map +1 -1
  26. package/dist/routes/apps-mcp.d.ts.map +1 -1
  27. package/dist/routes/client-runners.d.ts.map +1 -1
  28. package/dist/routes/connection-mcp.d.ts.map +1 -1
  29. package/dist/routes/documents.d.ts.map +1 -1
  30. package/dist/routes/environments.d.ts.map +1 -1
  31. package/dist/routes/project-mcp.d.ts.map +1 -1
  32. package/dist/routes/projects.d.ts.map +1 -1
  33. package/dist/routes/publications.d.ts.map +1 -1
  34. package/dist/routes/run-errors.d.ts.map +1 -1
  35. package/dist/routes/runs.d.ts.map +1 -1
  36. package/dist/routes/server-mcp.d.ts +17 -0
  37. package/dist/routes/server-mcp.d.ts.map +1 -0
  38. package/dist/routes/session-mailboxes.d.ts.map +1 -1
  39. package/dist/routes/triggers.d.ts.map +1 -1
  40. package/dist/routes/watchers.d.ts.map +1 -1
  41. package/dist/routes/workflow-enablements.d.ts.map +1 -1
  42. package/dist/routes/workflows.d.ts.map +1 -1
  43. package/dist/schemas.d.ts +3717 -28
  44. package/dist/schemas.d.ts.map +1 -1
  45. package/dist/session-mcp.d.ts +54 -0
  46. package/dist/session-mcp.d.ts.map +1 -0
  47. package/dist/session-tools.d.ts +49 -0
  48. package/dist/session-tools.d.ts.map +1 -0
  49. package/package.json +22 -20
  50. package/src/app.ts +3 -0
  51. package/src/index.ts +1 -0
  52. package/src/mcp-scopes.ts +69 -0
  53. package/src/mcp-server.ts +827 -0
  54. package/src/mcp-shared.ts +3 -151
  55. package/src/mcp.ts +6 -5
  56. package/src/plugin.ts +73 -2
  57. package/src/project-capabilities.ts +5 -3
  58. package/src/project-mcp-program.ts +2 -2
  59. package/src/project-mcp-surface.ts +18 -239
  60. package/src/routes/agent.ts +384 -41
  61. package/src/routes/apps-mcp.ts +84 -82
  62. package/src/routes/client-runners.ts +3 -1
  63. package/src/routes/connection-mcp.ts +6 -13
  64. package/src/routes/documents.ts +4 -0
  65. package/src/routes/environments.ts +49 -1
  66. package/src/routes/project-mcp.ts +176 -154
  67. package/src/routes/projects.ts +6 -0
  68. package/src/routes/publications.ts +2 -0
  69. package/src/routes/run-errors.ts +5 -1
  70. package/src/routes/runs.ts +4 -0
  71. package/src/routes/server-mcp.ts +151 -0
  72. package/src/routes/session-mailboxes.ts +1 -0
  73. package/src/routes/triggers.ts +6 -1
  74. package/src/routes/watchers.ts +1 -0
  75. package/src/routes/workflow-enablements.ts +132 -1
  76. package/src/routes/workflows.ts +4 -0
  77. package/src/schemas.ts +354 -6
  78. package/src/session-mcp.ts +696 -0
  79. package/src/session-tools.ts +169 -0
  80. package/dist/chunk-baagvvm3.js +0 -164
  81. package/dist/chunk-baagvvm3.js.map +0 -10
@@ -26,11 +26,16 @@ import {
26
26
  NoCompatibleEnvironmentError,
27
27
  ProjectNotFoundError,
28
28
  parseChatKey,
29
+ SESSION_OPERATIONS,
30
+ SessionInputNotFoundError,
29
31
  SessionMirrorBehindError,
30
32
  SessionMirrorDivergedError,
33
+ type SessionOperationErrorCode,
34
+ sessionOperationError,
35
+ TokenScopeRequiredError,
31
36
  UnsupportedAgentTopologyError,
32
37
  } from "@catamorphic/core";
33
- import type { FastifyInstance } from "fastify";
38
+ import type { FastifyInstance, FastifyReply } from "fastify";
34
39
  import type { ZodTypeProvider } from "fastify-type-provider-zod";
35
40
  import { z } from "zod";
36
41
  import type { RouteContext } from "../app.js";
@@ -47,6 +52,8 @@ import {
47
52
  AgentSessionPeerSchema,
48
53
  AgentSessionSchema,
49
54
  AgentSessionsQuerySchema,
55
+ AgentSessionUsageQuerySchema,
56
+ AgentSessionUsageSchema,
50
57
  AgentSubsessionIdParamsSchema,
51
58
  AgentSubsessionSchema,
52
59
  ArchiveAgentSessionSchema,
@@ -70,6 +77,8 @@ import {
70
77
  ProjectAgentEntrySchema,
71
78
  ProjectIdParamsSchema,
72
79
  RenameAgentSessionSchema,
80
+ RestartTurnResultSchema,
81
+ RestartTurnSchema,
73
82
  ResumeAgentSessionSchema,
74
83
  SessionCommandSchema,
75
84
  SessionConflictSchema,
@@ -77,21 +86,68 @@ import {
77
86
  SessionItemsPageSchema,
78
87
  SessionItemsQuerySchema,
79
88
  SessionMirrorQuerySchema,
89
+ SessionMissedEventsQuerySchema,
90
+ SessionMissedEventsSchema,
91
+ SessionOperationErrorSchema,
92
+ SessionResultsQuerySchema,
93
+ SessionResultsSchema,
80
94
  SessionStreamMessageSchema,
81
95
  SkillSchema,
96
+ StartSessionSchema,
97
+ TurnAttemptsSchema,
98
+ TurnIdParamsSchema,
82
99
  UpdateAgentSessionActivitySchema,
83
100
  UpdateAgentSessionMetadataSchema,
84
101
  UpdateAgentSessionSchema,
85
- WaitForAgentSubsessionsSchema,
86
102
  } from "../schemas.js";
87
103
  import { SessionEventStream } from "../session-event-stream.js";
88
104
 
105
+ /** The HTTP status each operation error answers with. */
106
+ const SESSION_OPERATION_STATUS = {
107
+ not_found: 404,
108
+ not_authorized: 403,
109
+ invalid_input: 400,
110
+ agent_unknown: 400,
111
+ environment_unavailable: 409,
112
+ environment_full: 409,
113
+ authentication_required: 428,
114
+ session_closed: 409,
115
+ session_elsewhere: 409,
116
+ request_not_pending: 409,
117
+ idempotency_conflict: 409,
118
+ internal: 500,
119
+ } as const satisfies Record<SessionOperationErrorCode, number>;
120
+
121
+ const SessionOperationErrorResponses = {
122
+ 400: SessionOperationErrorSchema,
123
+ 403: SessionOperationErrorSchema,
124
+ 404: SessionOperationErrorSchema,
125
+ 409: SessionOperationErrorSchema,
126
+ 428: SessionOperationErrorSchema,
127
+ 500: SessionOperationErrorSchema,
128
+ 503: SessionOperationErrorSchema,
129
+ };
130
+
131
+ /** A session operation's failure as its route answers it (ADR 0346). */
132
+ function sendSessionOperationError(reply: FastifyReply, error: unknown) {
133
+ // A grant that lacks a scope is answered as on every route: a step-up.
134
+ if (error instanceof TokenScopeRequiredError) throw error;
135
+ const refused = sessionOperationError(error);
136
+ return reply.status(SESSION_OPERATION_STATUS[refused.code]).send({
137
+ error: refused.message,
138
+ code: refused.code,
139
+ retryable: refused.retryable,
140
+ ...(refused.sessionId ? { sessionId: refused.sessionId } : {}),
141
+ });
142
+ }
143
+
89
144
  export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
90
145
  const typed = app.withTypeProvider<ZodTypeProvider>();
91
146
 
92
147
  typed.route({
93
148
  method: "POST",
94
149
  url: "/projects/:projectId/agent/sessions",
150
+ config: { tokenScope: "sessions:run" },
95
151
  schema: {
96
152
  params: ProjectIdParamsSchema,
97
153
  body: CreateAgentSessionSchema,
@@ -196,6 +252,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
196
252
  typed.route({
197
253
  method: "PATCH",
198
254
  url: "/projects/:projectId/agent/sessions/:sessionId",
255
+ config: { tokenScope: "sessions:run" },
199
256
  schema: {
200
257
  params: AgentSessionIdParamsSchema,
201
258
  body: UpdateAgentSessionSchema,
@@ -309,6 +366,92 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
309
366
  },
310
367
  });
311
368
 
369
+ // Start a chat with its first message (ADR 0346): the same operation
370
+ // the MCP endpoints, in-product agents and workflows run. It answers at
371
+ // once with the chat's state; the turn runs in the background.
372
+ typed.route({
373
+ method: "POST",
374
+ url: "/projects/:projectId/agent/sessions/start",
375
+ config: { tokenScope: "sessions:run" },
376
+ schema: {
377
+ params: ProjectIdParamsSchema,
378
+ body: StartSessionSchema,
379
+ response: {
380
+ 200: SESSION_OPERATIONS.session_start.output,
381
+ ...SessionOperationErrorResponses,
382
+ },
383
+ },
384
+ handler: async (request, reply) => {
385
+ const operations = ctx.core?.sessionOperations;
386
+ if (!operations)
387
+ return reply.status(503).send({
388
+ error: "Coding agent not configured",
389
+ code: "internal" as const,
390
+ retryable: true,
391
+ });
392
+ const identity = resolveIdentity(request);
393
+ try {
394
+ return reply.send(
395
+ SESSION_OPERATIONS.session_start.output.parse(
396
+ await operations.execute({
397
+ identity,
398
+ projectId: request.params.projectId,
399
+ operation: "session_start",
400
+ args: request.body,
401
+ // A person's client sends their own words (ADR 0217).
402
+ author: { kind: "user", externalUserId: identity.externalUserId },
403
+ }),
404
+ ),
405
+ );
406
+ } catch (error) {
407
+ return sendSessionOperationError(reply, error);
408
+ }
409
+ },
410
+ });
411
+
412
+ // Hold until one of the chats needs its caller, at most 50 seconds (ADR
413
+ // 0347): `changed: false` when nothing happened, to be asked again.
414
+ typed.route({
415
+ method: "POST",
416
+ url: "/projects/:projectId/agent/sessions/wait",
417
+ config: { tokenScope: "work:read" },
418
+ schema: {
419
+ params: ProjectIdParamsSchema,
420
+ body: SESSION_OPERATIONS.session_wait.input,
421
+ response: {
422
+ 200: SESSION_OPERATIONS.session_wait.output,
423
+ ...SessionOperationErrorResponses,
424
+ },
425
+ },
426
+ handler: async (request, reply) => {
427
+ const operations = ctx.core?.sessionOperations;
428
+ if (!operations)
429
+ return reply.status(503).send({
430
+ error: "Coding agent not configured",
431
+ code: "internal" as const,
432
+ retryable: true,
433
+ });
434
+ const identity = resolveIdentity(request);
435
+ try {
436
+ return reply.send(
437
+ SESSION_OPERATIONS.session_wait.output.parse(
438
+ await operations.execute({
439
+ identity,
440
+ projectId: request.params.projectId,
441
+ operation: "session_wait",
442
+ args: request.body,
443
+ author: { kind: "user", externalUserId: identity.externalUserId },
444
+ // A caller that went away has stopped waiting.
445
+ signal: hungUp(reply),
446
+ }),
447
+ ),
448
+ );
449
+ } catch (error) {
450
+ return sendSessionOperationError(reply, error);
451
+ }
452
+ },
453
+ });
454
+
312
455
  typed.route({
313
456
  method: "GET",
314
457
  url: "/projects/:projectId/agent/sessions/:sessionId",
@@ -384,6 +527,149 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
384
527
  },
385
528
  });
386
529
 
530
+ // The chat's accepted typed results, newest first (ADR 0323): the same
531
+ // read workflows make with `results`.
532
+ typed.route({
533
+ method: "GET",
534
+ url: "/projects/:projectId/agent/sessions/:sessionId/results",
535
+ schema: {
536
+ params: AgentSessionIdParamsSchema,
537
+ querystring: SessionResultsQuerySchema,
538
+ response: {
539
+ 200: SessionResultsSchema,
540
+ 400: ErrorSchema,
541
+ 404: ErrorSchema,
542
+ 503: ErrorSchema,
543
+ },
544
+ },
545
+ handler: async (request, reply) => {
546
+ const agentSessions = ctx.core?.agentSessions;
547
+ if (!agentSessions)
548
+ return reply.status(503).send({ error: "Coding agent not configured" });
549
+ const query = request.query;
550
+ try {
551
+ const results = await agentSessions.results(
552
+ resolveIdentity(request),
553
+ request.params.projectId,
554
+ request.params.sessionId,
555
+ {
556
+ ...(query.name ? { name: query.name } : {}),
557
+ ...(query.family ? { family: true } : {}),
558
+ ...(query.turnId ? { turnId: query.turnId } : {}),
559
+ ...(query.afterInput ? { afterInput: query.afterInput } : {}),
560
+ ...(query.limit ? { limit: query.limit } : {}),
561
+ },
562
+ );
563
+ return reply.send({ results });
564
+ } catch (error) {
565
+ if (
566
+ error instanceof ProjectNotFoundError ||
567
+ error instanceof AgentSessionNotFoundError
568
+ )
569
+ return reply.status(404).send({ error: "Session not found" });
570
+ if (error instanceof SessionInputNotFoundError)
571
+ return reply.status(400).send({ error: error.message });
572
+ throw error;
573
+ }
574
+ },
575
+ });
576
+
577
+ // A turn's attempts with the masked tails of their runner's and harness's
578
+ // own output (ADR 0333), for anyone who may read the chat.
579
+ typed.route({
580
+ method: "GET",
581
+ url: "/projects/:projectId/agent/sessions/:sessionId/turns/:turnId/attempts",
582
+ schema: {
583
+ params: TurnIdParamsSchema,
584
+ response: {
585
+ 200: TurnAttemptsSchema,
586
+ 404: ErrorSchema,
587
+ 503: ErrorSchema,
588
+ },
589
+ },
590
+ handler: async (request, reply) => {
591
+ const agentSessions = ctx.core?.agentSessions;
592
+ if (!agentSessions)
593
+ return reply.status(503).send({ error: "Coding agent not configured" });
594
+ try {
595
+ const attempts = await agentSessions.attempts(
596
+ resolveIdentity(request),
597
+ request.params.projectId,
598
+ request.params.sessionId,
599
+ { turnId: request.params.turnId },
600
+ );
601
+ return reply.send({ attempts });
602
+ } catch (error) {
603
+ if (
604
+ error instanceof ProjectNotFoundError ||
605
+ error instanceof AgentSessionNotFoundError
606
+ )
607
+ return reply.status(404).send({ error: "Session not found" });
608
+ throw error;
609
+ }
610
+ },
611
+ });
612
+
613
+ // Restart a running turn's attempt (ADR 0333): the same session action
614
+ // workflows and the project MCP use, recorded under the caller's key.
615
+ typed.route({
616
+ method: "POST",
617
+ url: "/projects/:projectId/agent/sessions/:sessionId/turns/:turnId/restart",
618
+ config: { tokenScope: "sessions:run" },
619
+ schema: {
620
+ params: TurnIdParamsSchema,
621
+ body: RestartTurnSchema,
622
+ response: {
623
+ 200: RestartTurnResultSchema,
624
+ 202: z.object({ delivery: z.literal("queued") }).passthrough(),
625
+ 404: ErrorSchema,
626
+ 409: ErrorSchema,
627
+ 503: ErrorSchema,
628
+ },
629
+ },
630
+ handler: async (request, reply) => {
631
+ const actions = ctx.core?.sessionOperations;
632
+ if (!actions)
633
+ return reply.status(503).send({ error: "Coding agent not configured" });
634
+ const identity = resolveIdentity(request);
635
+ try {
636
+ const result = await actions.execute({
637
+ identity,
638
+ projectId: request.params.projectId,
639
+ operation: "session_restart",
640
+ args: {
641
+ sessionId: request.params.sessionId,
642
+ turnId: request.params.turnId,
643
+ attemptId: request.body.attemptId,
644
+ reason: request.body.reason,
645
+ ...(request.body.onlyIfStalled !== undefined
646
+ ? { onlyIfStalled: request.body.onlyIfStalled }
647
+ : {}),
648
+ idempotencyKey: request.body.idempotencyKey,
649
+ },
650
+ author: { kind: "user", externalUserId: identity.externalUserId },
651
+ });
652
+ const parsed = RestartTurnResultSchema.safeParse(result);
653
+ if (parsed.success) return reply.send(parsed.data);
654
+ // Not the turn's running attempt, or no longer stalled: nothing ends.
655
+ const refused = z
656
+ .object({ restarted: z.literal(false), message: z.string() })
657
+ .safeParse(result);
658
+ if (refused.success)
659
+ return reply.status(409).send({ error: refused.data.message });
660
+ // Another host holds the chat: delivered there, not yet applied.
661
+ return reply.status(202).send({ delivery: "queued" as const });
662
+ } catch (error) {
663
+ if (
664
+ error instanceof ProjectNotFoundError ||
665
+ error instanceof AgentSessionNotFoundError
666
+ )
667
+ return reply.status(404).send({ error: "Session not found" });
668
+ throw error;
669
+ }
670
+ },
671
+ });
672
+
387
673
  // The session's events after a cursor, as server-sent events (ADR 0197):
388
674
  // the gap (or `reset` with a fresh snapshot when it is too large), then
389
675
  // live events, with a heartbeat every 15 seconds. A reader that falls too
@@ -457,6 +743,46 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
457
743
  },
458
744
  });
459
745
 
746
+ // What a reader has missed since its sequence, read once (ADR 0197): the
747
+ // messages the stream above opens with, for a reader that holds no
748
+ // stream (off screen, or past a client's connection limit). It pays for
749
+ // what changed; reading the snapshot again pays for the whole chat.
750
+ typed.route({
751
+ method: "GET",
752
+ url: "/projects/:projectId/agent/sessions/:sessionId/events/missed",
753
+ schema: {
754
+ params: AgentSessionIdParamsSchema,
755
+ querystring: SessionMissedEventsQuerySchema,
756
+ response: {
757
+ 200: SessionMissedEventsSchema,
758
+ 404: ErrorSchema,
759
+ 503: ErrorSchema,
760
+ },
761
+ },
762
+ handler: async (request, reply) => {
763
+ const agentSessions = ctx.core?.agentSessions;
764
+ if (!agentSessions)
765
+ return reply.status(503).send({ error: "Coding agent not configured" });
766
+ try {
767
+ return reply.send({
768
+ messages: await agentSessions.missedEvents(
769
+ resolveIdentity(request),
770
+ request.params.projectId,
771
+ request.params.sessionId,
772
+ { after: request.query.after },
773
+ ),
774
+ });
775
+ } catch (error) {
776
+ if (
777
+ error instanceof ProjectNotFoundError ||
778
+ error instanceof AgentSessionNotFoundError
779
+ )
780
+ return reply.status(404).send({ error: "Session not found" });
781
+ throw error;
782
+ }
783
+ },
784
+ });
785
+
460
786
  // A person's command to a session (ADR 0197): send, interrupt, retry,
461
787
  // queue edits, send now, answer a request, roll back. Each carries a
462
788
  // client `commandId`; sending it again returns the first receipt. A
@@ -464,6 +790,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
464
790
  typed.route({
465
791
  method: "POST",
466
792
  url: "/projects/:projectId/agent/sessions/:sessionId/commands",
793
+ config: { tokenScope: "sessions:run" },
467
794
  // Base64 media rides in a send's attachments; the rest of the API keeps
468
795
  // Fastify's default cap.
469
796
  bodyLimit: 96 * 1024 * 1024,
@@ -522,6 +849,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
522
849
  typed.route({
523
850
  method: "POST",
524
851
  url: "/projects/:projectId/agent/sessions/:sessionId/resume",
852
+ config: { tokenScope: "sessions:run" },
525
853
  schema: {
526
854
  params: AgentSessionIdParamsSchema,
527
855
  body: ResumeAgentSessionSchema,
@@ -718,6 +1046,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
718
1046
  typed.route({
719
1047
  method: "POST",
720
1048
  url: "/projects/:projectId/agent/sessions/:sessionId/attention/acknowledge",
1049
+ config: { tokenScope: "sessions:run" },
721
1050
  schema: {
722
1051
  params: AgentSessionIdParamsSchema,
723
1052
  body: z
@@ -756,6 +1085,48 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
756
1085
  },
757
1086
  });
758
1087
 
1088
+ typed.route({
1089
+ method: "GET",
1090
+ url: "/projects/:projectId/agent/sessions/:sessionId/usage",
1091
+ schema: {
1092
+ params: AgentSessionIdParamsSchema,
1093
+ querystring: AgentSessionUsageQuerySchema,
1094
+ response: {
1095
+ 200: AgentSessionUsageSchema,
1096
+ 403: ErrorSchema,
1097
+ 404: ErrorSchema,
1098
+ 503: ErrorSchema,
1099
+ },
1100
+ },
1101
+ handler: async (request, reply) => {
1102
+ const agentSessions = ctx.core?.agentSessions;
1103
+ if (!agentSessions) {
1104
+ return reply.status(503).send({ error: "Coding agent not configured" });
1105
+ }
1106
+ try {
1107
+ return reply.send(
1108
+ await agentSessions.usage({
1109
+ identity: resolveIdentity(request),
1110
+ projectId: request.params.projectId,
1111
+ sessionId: request.params.sessionId,
1112
+ family: request.query.family ?? false,
1113
+ }),
1114
+ );
1115
+ } catch (error) {
1116
+ if (
1117
+ error instanceof ProjectNotFoundError ||
1118
+ error instanceof AgentSessionNotFoundError
1119
+ ) {
1120
+ return reply.status(404).send({ error: "Session not found" });
1121
+ }
1122
+ if (error instanceof AccessDeniedError) {
1123
+ return reply.status(403).send({ error: error.message });
1124
+ }
1125
+ throw error;
1126
+ }
1127
+ },
1128
+ });
1129
+
759
1130
  typed.route({
760
1131
  method: "GET",
761
1132
  url: "/projects/:projectId/agent/sessions/:sessionId/peers",
@@ -795,6 +1166,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
795
1166
  typed.route({
796
1167
  method: "POST",
797
1168
  url: "/projects/:projectId/agent/sessions/:sessionId/subsessions",
1169
+ config: { tokenScope: "sessions:run" },
798
1170
  schema: {
799
1171
  params: AgentSessionIdParamsSchema,
800
1172
  body: CreateAgentSubsessionSchema,
@@ -880,48 +1252,10 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
880
1252
  },
881
1253
  });
882
1254
 
883
- typed.route({
884
- method: "POST",
885
- url: "/projects/:projectId/agent/sessions/:sessionId/subsessions/wait",
886
- schema: {
887
- params: AgentSessionIdParamsSchema,
888
- body: WaitForAgentSubsessionsSchema,
889
- response: {
890
- 200: AgentSubsessionSchema.array(),
891
- 404: ErrorSchema,
892
- 503: ErrorSchema,
893
- },
894
- },
895
- handler: async (request, reply) => {
896
- const agentSessions = ctx.core?.agentSessions;
897
- if (!agentSessions) {
898
- return reply.status(503).send({ error: "Coding agent not configured" });
899
- }
900
- try {
901
- return reply.send(
902
- await agentSessions.waitForSubsessions(
903
- resolveIdentity(request),
904
- request.params.projectId,
905
- request.params.sessionId,
906
- // A caller that went away has stopped waiting.
907
- { ...request.body, stopWaiting: hungUp(reply) },
908
- ),
909
- );
910
- } catch (error) {
911
- if (
912
- error instanceof ProjectNotFoundError ||
913
- error instanceof AgentSessionNotFoundError
914
- ) {
915
- return reply.status(404).send({ error: "Session not found" });
916
- }
917
- throw error;
918
- }
919
- },
920
- });
921
-
922
1255
  typed.route({
923
1256
  method: "POST",
924
1257
  url: "/projects/:projectId/agent/sessions/:sessionId/subsessions/:childSessionId/interrupt",
1258
+ config: { tokenScope: "sessions:run" },
925
1259
  schema: {
926
1260
  params: AgentSubsessionIdParamsSchema,
927
1261
  response: {
@@ -962,6 +1296,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
962
1296
  typed.route({
963
1297
  method: "POST",
964
1298
  url: "/projects/:projectId/agent/sessions/:sessionId/attention",
1299
+ config: { tokenScope: "sessions:run" },
965
1300
  schema: {
966
1301
  params: AgentSessionIdParamsSchema,
967
1302
  response: {
@@ -998,6 +1333,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
998
1333
  typed.route({
999
1334
  method: "PATCH",
1000
1335
  url: "/projects/:projectId/agent/sessions/:sessionId/activity",
1336
+ config: { tokenScope: "sessions:run" },
1001
1337
  schema: {
1002
1338
  params: AgentSessionIdParamsSchema,
1003
1339
  body: UpdateAgentSessionActivitySchema,
@@ -1041,6 +1377,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1041
1377
  typed.route({
1042
1378
  method: "PATCH",
1043
1379
  url: "/projects/:projectId/agent/sessions/:sessionId/title",
1380
+ config: { tokenScope: "sessions:run" },
1044
1381
  schema: {
1045
1382
  params: AgentSessionIdParamsSchema,
1046
1383
  body: RenameAgentSessionSchema,
@@ -1084,6 +1421,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1084
1421
  typed.route({
1085
1422
  method: "PATCH",
1086
1423
  url: "/projects/:projectId/agent/sessions/:sessionId/metadata",
1424
+ config: { tokenScope: "sessions:run" },
1087
1425
  schema: {
1088
1426
  params: AgentSessionIdParamsSchema,
1089
1427
  body: UpdateAgentSessionMetadataSchema,
@@ -1132,6 +1470,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1132
1470
  typed.route({
1133
1471
  method: "POST",
1134
1472
  url: "/projects/:projectId/agent/sessions/:sessionId/fork",
1473
+ config: { tokenScope: "sessions:run" },
1135
1474
  schema: {
1136
1475
  params: AgentSessionIdParamsSchema,
1137
1476
  body: ForkAgentSessionSchema,
@@ -1169,6 +1508,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1169
1508
  typed.route({
1170
1509
  method: "DELETE",
1171
1510
  url: "/projects/:projectId/agent/sessions/:sessionId",
1511
+ config: { tokenScope: "sessions:run" },
1172
1512
  schema: {
1173
1513
  params: AgentSessionIdParamsSchema,
1174
1514
  response: { 200: AgentSessionSchema, 404: ErrorSchema, 503: ErrorSchema },
@@ -1248,6 +1588,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1248
1588
  typed.route({
1249
1589
  method: "DELETE",
1250
1590
  url: "/projects/:projectId/agent/chats/:key",
1591
+ config: { tokenScope: "sessions:run" },
1251
1592
  schema: {
1252
1593
  params: KeyedChatParamsSchema,
1253
1594
  querystring: KeyedChatQuerySchema,
@@ -1327,6 +1668,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1327
1668
  typed.route({
1328
1669
  method: "POST",
1329
1670
  url: "/projects/:projectId/agent/sessions/:sessionId/archive",
1671
+ config: { tokenScope: "sessions:run" },
1330
1672
  schema: {
1331
1673
  params: AgentSessionIdParamsSchema,
1332
1674
  body: ArchiveAgentSessionSchema,
@@ -1373,6 +1715,7 @@ export function registerAgentRoutes(app: FastifyInstance, ctx: RouteContext) {
1373
1715
  typed.route({
1374
1716
  method: "POST",
1375
1717
  url: "/projects/:projectId/agent/sessions/:sessionId/unarchive",
1718
+ config: { tokenScope: "sessions:run" },
1376
1719
  schema: {
1377
1720
  params: AgentSessionIdParamsSchema,
1378
1721
  response: {