@kindgi/api 0.1.4-rc.1 → 0.1.4-rc.3

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 (220) hide show
  1. package/dist/agent-binding.d.ts +12 -2
  2. package/dist/agent-binding.d.ts.map +1 -1
  3. package/dist/agent-pins.d.ts.map +1 -1
  4. package/dist/agent-pins.js +4 -1
  5. package/dist/agent-pins.js.map +1 -1
  6. package/dist/app.d.ts +8 -0
  7. package/dist/app.d.ts.map +1 -1
  8. package/dist/app.js +23 -4
  9. package/dist/app.js.map +1 -1
  10. package/dist/deploy-versions.d.ts +8 -5
  11. package/dist/deploy-versions.d.ts.map +1 -1
  12. package/dist/deploy-versions.js +3 -9
  13. package/dist/deploy-versions.js.map +1 -1
  14. package/dist/errors.d.ts.map +1 -1
  15. package/dist/errors.js +25 -0
  16. package/dist/errors.js.map +1 -1
  17. package/dist/eval-case-binding.d.ts +10 -0
  18. package/dist/eval-case-binding.d.ts.map +1 -1
  19. package/dist/eval-run-binding.d.ts +14 -3
  20. package/dist/eval-run-binding.d.ts.map +1 -1
  21. package/dist/eval-run-binding.js.map +1 -1
  22. package/dist/flow-pins.d.ts +20 -7
  23. package/dist/flow-pins.d.ts.map +1 -1
  24. package/dist/flow-pins.js +36 -11
  25. package/dist/flow-pins.js.map +1 -1
  26. package/dist/gate-policy-binding.d.ts +164 -0
  27. package/dist/gate-policy-binding.d.ts.map +1 -0
  28. package/dist/gate-policy-binding.js +12 -0
  29. package/dist/gate-policy-binding.js.map +1 -0
  30. package/dist/gate.d.ts +56 -0
  31. package/dist/gate.d.ts.map +1 -0
  32. package/dist/gate.js +359 -0
  33. package/dist/gate.js.map +1 -0
  34. package/dist/handler-binding.d.ts +14 -2
  35. package/dist/handler-binding.d.ts.map +1 -1
  36. package/dist/hitl-binding.d.ts +5 -0
  37. package/dist/hitl-binding.d.ts.map +1 -1
  38. package/dist/index.d.ts +10 -4
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +3 -1
  41. package/dist/index.js.map +1 -1
  42. package/dist/judged-dispatcher.d.ts +16 -1
  43. package/dist/judged-dispatcher.d.ts.map +1 -1
  44. package/dist/judged-dispatcher.js +45 -6
  45. package/dist/judged-dispatcher.js.map +1 -1
  46. package/dist/judgment-binding.d.ts +38 -0
  47. package/dist/judgment-binding.d.ts.map +1 -1
  48. package/dist/judgment-binding.js +19 -0
  49. package/dist/judgment-binding.js.map +1 -1
  50. package/dist/live-version-binding.d.ts +206 -0
  51. package/dist/live-version-binding.d.ts.map +1 -0
  52. package/dist/live-version-binding.js +4 -0
  53. package/dist/live-version-binding.js.map +1 -0
  54. package/dist/middleware/authorize.d.ts.map +1 -1
  55. package/dist/middleware/authorize.js +13 -2
  56. package/dist/middleware/authorize.js.map +1 -1
  57. package/dist/middleware/project-ref.d.ts +25 -0
  58. package/dist/middleware/project-ref.d.ts.map +1 -0
  59. package/dist/middleware/project-ref.js +72 -0
  60. package/dist/middleware/project-ref.js.map +1 -0
  61. package/dist/openapi/generate.d.ts.map +1 -1
  62. package/dist/openapi/generate.js +4 -0
  63. package/dist/openapi/generate.js.map +1 -1
  64. package/dist/openapi/operations.d.ts +6 -0
  65. package/dist/openapi/operations.d.ts.map +1 -1
  66. package/dist/openapi/operations.js +512 -22
  67. package/dist/openapi/operations.js.map +1 -1
  68. package/dist/openapi/schemas.d.ts +37 -0
  69. package/dist/openapi/schemas.d.ts.map +1 -1
  70. package/dist/openapi/schemas.js +709 -5
  71. package/dist/openapi/schemas.js.map +1 -1
  72. package/dist/publish-refused.d.ts +18 -0
  73. package/dist/publish-refused.d.ts.map +1 -0
  74. package/dist/publish-refused.js +22 -0
  75. package/dist/publish-refused.js.map +1 -0
  76. package/dist/retention-binding.d.ts +29 -0
  77. package/dist/retention-binding.d.ts.map +1 -1
  78. package/dist/routes/agent-releases.d.ts +40 -0
  79. package/dist/routes/agent-releases.d.ts.map +1 -0
  80. package/dist/routes/agent-releases.js +462 -0
  81. package/dist/routes/agent-releases.js.map +1 -0
  82. package/dist/routes/agents.d.ts +7 -1
  83. package/dist/routes/agents.d.ts.map +1 -1
  84. package/dist/routes/agents.js +31 -3
  85. package/dist/routes/agents.js.map +1 -1
  86. package/dist/routes/approvals.d.ts.map +1 -1
  87. package/dist/routes/approvals.js +40 -9
  88. package/dist/routes/approvals.js.map +1 -1
  89. package/dist/routes/audit.d.ts.map +1 -1
  90. package/dist/routes/audit.js +7 -0
  91. package/dist/routes/audit.js.map +1 -1
  92. package/dist/routes/auth.js +1 -1
  93. package/dist/routes/auth.js.map +1 -1
  94. package/dist/routes/conversations.d.ts +0 -7
  95. package/dist/routes/conversations.d.ts.map +1 -1
  96. package/dist/routes/conversations.js +19 -3
  97. package/dist/routes/conversations.js.map +1 -1
  98. package/dist/routes/deployments.d.ts +6 -0
  99. package/dist/routes/deployments.d.ts.map +1 -1
  100. package/dist/routes/deployments.js +33 -5
  101. package/dist/routes/deployments.js.map +1 -1
  102. package/dist/routes/env.d.ts.map +1 -1
  103. package/dist/routes/env.js +1 -0
  104. package/dist/routes/env.js.map +1 -1
  105. package/dist/routes/eval-comparison.d.ts +2 -1
  106. package/dist/routes/eval-comparison.d.ts.map +1 -1
  107. package/dist/routes/eval-comparison.js +22 -9
  108. package/dist/routes/eval-comparison.js.map +1 -1
  109. package/dist/routes/flows.d.ts +14 -7
  110. package/dist/routes/flows.d.ts.map +1 -1
  111. package/dist/routes/flows.js +10 -8
  112. package/dist/routes/flows.js.map +1 -1
  113. package/dist/routes/gate-policies.d.ts +19 -0
  114. package/dist/routes/gate-policies.d.ts.map +1 -0
  115. package/dist/routes/gate-policies.js +191 -0
  116. package/dist/routes/gate-policies.js.map +1 -0
  117. package/dist/routes/gate-policy-spec.d.ts +17 -0
  118. package/dist/routes/gate-policy-spec.d.ts.map +1 -0
  119. package/dist/routes/gate-policy-spec.js +192 -0
  120. package/dist/routes/gate-policy-spec.js.map +1 -0
  121. package/dist/routes/gate-policy-wire.d.ts +3 -0
  122. package/dist/routes/gate-policy-wire.d.ts.map +1 -0
  123. package/dist/routes/gate-policy-wire.js +18 -0
  124. package/dist/routes/gate-policy-wire.js.map +1 -0
  125. package/dist/routes/hierarchy-errors.d.ts +11 -0
  126. package/dist/routes/hierarchy-errors.d.ts.map +1 -1
  127. package/dist/routes/hierarchy-errors.js +13 -0
  128. package/dist/routes/hierarchy-errors.js.map +1 -1
  129. package/dist/routes/judged-suites.js +7 -0
  130. package/dist/routes/judged-suites.js.map +1 -1
  131. package/dist/routes/judgments.d.ts +4 -1
  132. package/dist/routes/judgments.d.ts.map +1 -1
  133. package/dist/routes/judgments.js +92 -19
  134. package/dist/routes/judgments.js.map +1 -1
  135. package/dist/routes/live-scope-wire.d.ts +32 -0
  136. package/dist/routes/live-scope-wire.d.ts.map +1 -0
  137. package/dist/routes/live-scope-wire.js +71 -0
  138. package/dist/routes/live-scope-wire.js.map +1 -0
  139. package/dist/routes/policies.d.ts +6 -3
  140. package/dist/routes/policies.d.ts.map +1 -1
  141. package/dist/routes/policies.js +55 -3
  142. package/dist/routes/policies.js.map +1 -1
  143. package/dist/routes/projects.d.ts.map +1 -1
  144. package/dist/routes/projects.js +47 -3
  145. package/dist/routes/projects.js.map +1 -1
  146. package/dist/routes/retention.d.ts.map +1 -1
  147. package/dist/routes/retention.js +18 -2
  148. package/dist/routes/retention.js.map +1 -1
  149. package/dist/routes/reviewers.d.ts +8 -1
  150. package/dist/routes/reviewers.d.ts.map +1 -1
  151. package/dist/routes/reviewers.js +20 -3
  152. package/dist/routes/reviewers.js.map +1 -1
  153. package/dist/routes/runs.d.ts.map +1 -1
  154. package/dist/routes/runs.js +22 -13
  155. package/dist/routes/runs.js.map +1 -1
  156. package/dist/routes/secrets.d.ts.map +1 -1
  157. package/dist/routes/secrets.js +2 -0
  158. package/dist/routes/secrets.js.map +1 -1
  159. package/dist/routes/segments.d.ts +17 -0
  160. package/dist/routes/segments.d.ts.map +1 -0
  161. package/dist/routes/segments.js +68 -0
  162. package/dist/routes/segments.js.map +1 -0
  163. package/dist/routes/teams.d.ts.map +1 -1
  164. package/dist/routes/teams.js +42 -3
  165. package/dist/routes/teams.js.map +1 -1
  166. package/dist/routes/uuid-param.d.ts +11 -0
  167. package/dist/routes/uuid-param.d.ts.map +1 -0
  168. package/dist/routes/uuid-param.js +19 -0
  169. package/dist/routes/uuid-param.js.map +1 -0
  170. package/openapi.json +7008 -3624
  171. package/package.json +21 -21
  172. package/src/agent-binding.ts +12 -2
  173. package/src/agent-pins.ts +3 -1
  174. package/src/app.ts +37 -3
  175. package/src/deploy-versions.ts +11 -14
  176. package/src/errors.ts +25 -0
  177. package/src/eval-case-binding.ts +7 -0
  178. package/src/eval-run-binding.ts +24 -3
  179. package/src/flow-pins.ts +49 -9
  180. package/src/gate-policy-binding.ts +171 -0
  181. package/src/gate.ts +467 -0
  182. package/src/handler-binding.ts +14 -2
  183. package/src/hitl-binding.ts +5 -0
  184. package/src/index.ts +46 -1
  185. package/src/judged-dispatcher.ts +74 -6
  186. package/src/judgment-binding.ts +62 -0
  187. package/src/live-version-binding.ts +237 -0
  188. package/src/middleware/authorize.ts +18 -2
  189. package/src/middleware/project-ref.ts +88 -0
  190. package/src/openapi/generate.ts +5 -0
  191. package/src/openapi/operations.ts +619 -22
  192. package/src/openapi/schemas.ts +788 -5
  193. package/src/publish-refused.ts +31 -0
  194. package/src/retention-binding.ts +30 -0
  195. package/src/routes/agent-releases.ts +581 -0
  196. package/src/routes/agents.ts +43 -2
  197. package/src/routes/approvals.ts +58 -8
  198. package/src/routes/audit.ts +10 -0
  199. package/src/routes/auth.ts +1 -1
  200. package/src/routes/conversations.ts +31 -3
  201. package/src/routes/deployments.ts +50 -5
  202. package/src/routes/env.ts +1 -0
  203. package/src/routes/eval-comparison.ts +24 -10
  204. package/src/routes/flows.ts +27 -11
  205. package/src/routes/gate-policies.ts +226 -0
  206. package/src/routes/gate-policy-spec.ts +229 -0
  207. package/src/routes/gate-policy-wire.ts +20 -0
  208. package/src/routes/hierarchy-errors.ts +14 -0
  209. package/src/routes/judged-suites.ts +5 -0
  210. package/src/routes/judgments.ts +107 -23
  211. package/src/routes/live-scope-wire.ts +90 -0
  212. package/src/routes/policies.ts +61 -3
  213. package/src/routes/projects.ts +54 -2
  214. package/src/routes/retention.ts +21 -2
  215. package/src/routes/reviewers.ts +23 -4
  216. package/src/routes/runs.ts +33 -20
  217. package/src/routes/secrets.ts +2 -0
  218. package/src/routes/segments.ts +75 -0
  219. package/src/routes/teams.ts +53 -3
  220. package/src/routes/uuid-param.ts +28 -0
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (C) 2026 Kindgi Inc.
3
+
4
+ import type { AgentRegistryBinding } from './agent-binding.js';
5
+ import type { FlowRegistryBinding } from './flow-binding.js';
6
+ import type { GuardrailRegistryBinding } from './guardrail-binding.js';
7
+ import type { ToolRegistryBinding } from './tool-binding.js';
8
+
9
+ /**
10
+ * A deploy's primitive came back from its registry with an outcome other
11
+ * than `ok` or `already-registered` (e.g. `project-not-found`): the deploy
12
+ * stops, what it published is rolled back, and the route answers with
13
+ * that outcome's own status and code (T205). Before, such an outcome was
14
+ * skipped, and the deployment was recorded without that primitive.
15
+ */
16
+ export class PublishRefused extends Error {
17
+ constructor(
18
+ readonly primitive: 'tool' | 'guardrail' | 'agent' | 'flow',
19
+ readonly id: string,
20
+ readonly code: Exclude<
21
+ | Awaited<ReturnType<ToolRegistryBinding['publish']>>['kind']
22
+ | Awaited<ReturnType<GuardrailRegistryBinding['register']>>['kind']
23
+ | Awaited<ReturnType<AgentRegistryBinding['publish']>>['kind']
24
+ | Awaited<ReturnType<FlowRegistryBinding['publish']>>['kind'],
25
+ 'ok' | 'already-registered'
26
+ >,
27
+ ) {
28
+ super(`The ${primitive} ${id} wasn't published: ${code}`);
29
+ this.name = 'PublishRefused';
30
+ }
31
+ }
@@ -35,9 +35,12 @@ export interface RetentionBinding {
35
35
  export interface RetentionScheduledInput {
36
36
  readonly tenantId: TenantId;
37
37
  readonly domain?: RetentionDomain;
38
+ /** Rows per domain at most: each domain is read up to `limit`. */
38
39
  readonly limit: number;
39
40
  readonly pastGraceOnly?: boolean;
40
41
  readonly now?: Date;
42
+ /** Where a previous page stopped (its `nextCursor`, the runtime's own encoding). */
43
+ readonly cursor?: string;
41
44
  }
42
45
 
43
46
  export interface RetentionScheduledItem {
@@ -57,6 +60,31 @@ export interface RetentionScheduledPage {
57
60
  readonly domainsMissingAdapter: readonly RetentionDomain[];
58
61
  /** Domains that have an adapter but no matching policy — tombstones sit forever. */
59
62
  readonly unpolicedDomains: readonly RetentionDomain[];
63
+ /** Domains more than one retention policy covers (see `RetentionPolicyConflict`). */
64
+ readonly conflicts?: readonly RetentionPolicyConflict[];
65
+ /**
66
+ * More rows are scheduled than this page holds: some domain stopped at
67
+ * `limit`. A runtime that doesn't say leaves it out, and the route then
68
+ * reports `true` when some domain's rows fill `limit` (there may be more).
69
+ */
70
+ readonly hasMore?: boolean;
71
+ /** Pass as `cursor` to continue where this page stopped. Absent: nothing more, or no way to continue. */
72
+ readonly nextCursor?: string;
73
+ }
74
+
75
+ /**
76
+ * A domain more than one retention policy covers. Publishing refuses a
77
+ * second policy for a domain (`409 policy-scope-taken`), so only policies
78
+ * stored before that rule can do this. The one whose latest version is
79
+ * highest applies, and on equal versions the lower policy id; unregister
80
+ * the others.
81
+ */
82
+ export interface RetentionPolicyConflict {
83
+ readonly domain: RetentionDomain;
84
+ /** Every policy id that covers the domain, sorted. */
85
+ readonly policyIds: readonly string[];
86
+ /** The one of them that applies. */
87
+ readonly appliedPolicyId: string;
60
88
  }
61
89
 
62
90
  export interface RetentionSweepInput {
@@ -75,4 +103,6 @@ export interface RetentionSweepResult {
75
103
  readonly missingAdapter?: true;
76
104
  }[];
77
105
  readonly totalPurged: number;
106
+ /** Domains more than one retention policy covers (see `RetentionPolicyConflict`). */
107
+ readonly conflicts?: readonly RetentionPolicyConflict[];
78
108
  }
@@ -0,0 +1,581 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Copyright (C) 2026 Kindgi Inc.
3
+
4
+ import type { Context, Hono } from 'hono';
5
+
6
+ import type { AgentId } from '@kindgi/agents';
7
+ import type { Cursor, LiveScope, OrgId, ProjectId, RunId, Semver, TenantId } from '@kindgi/types';
8
+
9
+ import type { ProjectBinding } from '@kindgi/platform';
10
+
11
+ import type { AgentRegistryBinding } from '../agent-binding.js';
12
+ import { statusFor, toWireError } from '../errors.js';
13
+ import type { EvalRun, EvalRunBinding } from '../eval-run-binding.js';
14
+ import type { GatePolicy } from '../gate-policy-binding.js';
15
+ import { type GateResult, evaluateGate } from '../gate.js';
16
+ import type { JudgedComparisonSummary } from '../judged-dispatcher.js';
17
+ import type {
18
+ AgentReleaseBindings,
19
+ LiveResolveInput,
20
+ Promotion,
21
+ PromotionActor,
22
+ } from '../live-version-binding.js';
23
+ import type { AppEnv } from '../types.js';
24
+ import { serializeGatePolicy } from './gate-policy-wire.js';
25
+ import { liveScopeToWire, parseLiveScopeBody } from './live-scope-wire.js';
26
+ import { clampLimit } from './pagination.js';
27
+ import { parseSegmentsQuery } from './segments.js';
28
+
29
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
30
+
31
+ /** The routes that change what's live: authorized as `promote` on the agent, not `admin`. */
32
+ export function isPromotionWrite(method: string, path: string): boolean {
33
+ return method === 'POST' && /\/(promotions|live\/rollback|live\/unpin)$/.test(path);
34
+ }
35
+
36
+ /** `POST …/promotions/check` changes nothing: `read` on the agent. */
37
+ export function isPromotionCheck(method: string, path: string): boolean {
38
+ return method === 'POST' && path.endsWith('/promotions/check');
39
+ }
40
+
41
+ /** What the gate reads besides the releases: comparisons, and a project's org. */
42
+ export interface AgentReleaseGateDeps {
43
+ readonly evalRuns?: EvalRunBinding;
44
+ readonly projects?: ProjectBinding;
45
+ }
46
+
47
+ /**
48
+ * Live versions of an agent per scope, and the promotions that set them
49
+ * (evals step 4):
50
+ * GET /:agentId/live the version a run would use for a project and segment path
51
+ * GET /:agentId/live-versions every pin
52
+ * POST /:agentId/promotions make a version live for a scope, through its gate
53
+ * (201 promoted, 202 waiting for approval, 422 gate-failed)
54
+ * POST /:agentId/promotions/check what the gate would say, writing nothing
55
+ * GET /:agentId/gate-policy the gate policy that applies to a scope
56
+ * GET /:agentId/promotions[/:id] the history
57
+ * POST /:agentId/live/rollback back to the scope's previous live version
58
+ * POST /:agentId/live/unpin remove the scope's pin (it falls back to the scope above)
59
+ */
60
+ export function mountAgentReleaseRoutes(
61
+ r: Hono<AppEnv>,
62
+ registry: AgentRegistryBinding,
63
+ releases: AgentReleaseBindings,
64
+ deps: AgentReleaseGateDeps = {},
65
+ ): void {
66
+ r.get('/:agentId/live', async (c) => {
67
+ const requestId = c.get('requestId');
68
+ const tenantId = c.get('tenantId') as TenantId;
69
+ const agentId = c.req.param('agentId');
70
+ const projectId = c.req.query('projectId');
71
+ if (projectId !== undefined && !UUID_RE.test(projectId)) {
72
+ c.status(statusFor('bad-input') as never);
73
+ return c.json(
74
+ toWireError(
75
+ { code: 'bad-input', message: '`projectId` must be a project id (a UUID)' },
76
+ requestId,
77
+ ),
78
+ );
79
+ }
80
+ const segments = parseSegmentsQuery(c.req.queries('segment') ?? []);
81
+ if (segments.kind === 'err') {
82
+ c.status(statusFor('bad-input') as never);
83
+ return c.json(toWireError({ code: 'bad-input', message: segments.message }, requestId));
84
+ }
85
+ if (segments.segments !== undefined && projectId === undefined) {
86
+ c.status(statusFor('bad-input') as never);
87
+ return c.json(
88
+ toWireError(
89
+ { code: 'bad-input', message: 'A `segment` path needs a `projectId`' },
90
+ requestId,
91
+ ),
92
+ );
93
+ }
94
+ const resolved = await releases.live.resolve({
95
+ tenantId,
96
+ agentId,
97
+ ...(projectId !== undefined && { projectId: projectId as ProjectId }),
98
+ ...(segments.segments !== undefined && { segments: segments.segments }),
99
+ });
100
+ if (resolved !== null) {
101
+ return c.json({
102
+ agentId,
103
+ version: resolved.version as unknown as string,
104
+ via: 'live',
105
+ liveScope: liveScopeToWire(resolved.scope),
106
+ });
107
+ }
108
+ // Nothing pinned on the way up: what a run gets is the latest registered version.
109
+ const latest = await registry.get({ tenantId, agentId: agentId as AgentId });
110
+ if (latest === null) {
111
+ c.status(statusFor('agent-not-found') as never);
112
+ return c.json(
113
+ toWireError(
114
+ { code: 'agent-not-found', message: `No agent "${agentId}" is registered` },
115
+ requestId,
116
+ ),
117
+ );
118
+ }
119
+ return c.json({ agentId, version: latest.version as unknown as string, via: 'latest' });
120
+ });
121
+
122
+ r.get('/:agentId/live-versions', async (c) => {
123
+ const tenantId = c.get('tenantId') as TenantId;
124
+ const pins = await releases.live.list({ tenantId, agentId: c.req.param('agentId') });
125
+ return c.json({
126
+ data: pins.map((p) => ({
127
+ agentId: p.agentId,
128
+ scope: liveScopeToWire(p.scope),
129
+ version: p.version as unknown as string,
130
+ promotionId: p.promotionId,
131
+ setAt: p.setAt as unknown as string,
132
+ })),
133
+ });
134
+ });
135
+
136
+ r.post('/:agentId/promotions', async (c) => {
137
+ const requestId = c.get('requestId');
138
+ const parsed = await readPromotionBody(c);
139
+ if (parsed.kind === 'err') return badInput(c, requestId, parsed.message);
140
+ const tenantId = c.get('tenantId') as TenantId;
141
+ const agentId = c.req.param('agentId');
142
+ const input = {
143
+ tenantId,
144
+ agentId,
145
+ version: parsed.value.version,
146
+ scope: parsed.value.scope,
147
+ requestedBy: actorOf(c),
148
+ ...(parsed.value.reason !== undefined && { reason: parsed.value.reason }),
149
+ ...(parsed.value.evalRunId !== undefined && { evalRunId: parsed.value.evalRunId }),
150
+ };
151
+ const policy = await policyFor(releases, tenantId, agentId, parsed.value.scope);
152
+ if (releases.promotions.request === undefined) {
153
+ if (policy !== null) {
154
+ c.status(statusFor('promotion-gate-unsupported') as never);
155
+ return c.json(
156
+ toWireError(
157
+ {
158
+ code: 'promotion-gate-unsupported',
159
+ message: `Gate policy ${policy.id} ${policy.version} applies to this promotion, and this deployment can't record a gated promotion yet.`,
160
+ },
161
+ requestId,
162
+ ),
163
+ );
164
+ }
165
+ // No gate, and a binding from before gates: promote as before.
166
+ const outcome = await releases.promotions.promote(input);
167
+ if (outcome.kind === 'err') return failed(c, requestId, outcome.error);
168
+ c.status(201);
169
+ return c.json(serializePromotion(outcome.value));
170
+ }
171
+ const gate = await runGate(
172
+ registry,
173
+ releases,
174
+ deps,
175
+ { ...parsed.value, tenantId, agentId },
176
+ policy,
177
+ );
178
+ if (gate.kind === 'err') return failed(c, requestId, gate.error);
179
+ const outcome = await releases.promotions.request({
180
+ ...input,
181
+ gate: {
182
+ policy: policy === null ? null : { id: policy.id, version: policy.version },
183
+ checks: gate.result.checks,
184
+ passed: gate.result.passed,
185
+ ...(gate.result.approval !== undefined && { approval: gate.result.approval }),
186
+ servingVersion: gate.servingVersion as Semver,
187
+ },
188
+ });
189
+ if (outcome.kind === 'err') return failed(c, requestId, outcome.error);
190
+ const promotion = outcome.value;
191
+ if (promotion.status === 'refused') {
192
+ const checks = promotion.checks ?? gate.result.checks;
193
+ c.status(statusFor('gate-failed') as never);
194
+ return c.json(
195
+ toWireError(
196
+ {
197
+ code: 'gate-failed',
198
+ message: `The gate refused ${agentId} ${parsed.value.version}: ${checks
199
+ .filter((check) => !check.passed)
200
+ .map((check) => check.message)
201
+ .join(' ')}`,
202
+ promotionId: promotion.id,
203
+ policy: promotion.policy ?? null,
204
+ checks,
205
+ },
206
+ requestId,
207
+ ),
208
+ );
209
+ }
210
+ c.status(promotion.status === 'pending-approval' ? 202 : 201);
211
+ return c.json(serializePromotion(promotion));
212
+ });
213
+
214
+ r.post('/:agentId/promotions/check', async (c) => {
215
+ const requestId = c.get('requestId');
216
+ const parsed = await readPromotionBody(c);
217
+ if (parsed.kind === 'err') return badInput(c, requestId, parsed.message);
218
+ const tenantId = c.get('tenantId') as TenantId;
219
+ const agentId = c.req.param('agentId');
220
+ const policy = await policyFor(releases, tenantId, agentId, parsed.value.scope);
221
+ const gate = await runGate(
222
+ registry,
223
+ releases,
224
+ deps,
225
+ { ...parsed.value, tenantId, agentId },
226
+ policy,
227
+ );
228
+ if (gate.kind === 'err') return failed(c, requestId, gate.error);
229
+ const { result } = gate;
230
+ return c.json({
231
+ outcome: !result.passed
232
+ ? 'gate-failed'
233
+ : result.approval !== undefined
234
+ ? 'needs-approval'
235
+ : 'would-promote',
236
+ policy: policy === null ? null : { id: policy.id, version: policy.version },
237
+ checks: result.checks,
238
+ ...(result.approval !== undefined && { approval: result.approval }),
239
+ });
240
+ });
241
+
242
+ r.get('/:agentId/gate-policy', async (c) => {
243
+ const requestId = c.get('requestId');
244
+ const scope = scopeFromQuery((n) => c.req.query(n), c.req.queries('segment') ?? []);
245
+ if (scope.kind === 'err') return badInput(c, requestId, scope.message);
246
+ if (scope.scope === undefined) {
247
+ return badInput(c, requestId, '`scopeKind` is required: the scope a promotion would be for');
248
+ }
249
+ const policy = await policyFor(
250
+ releases,
251
+ c.get('tenantId') as TenantId,
252
+ c.req.param('agentId'),
253
+ scope.scope,
254
+ );
255
+ return c.json({ policy: policy === null ? null : serializeGatePolicy(policy) });
256
+ });
257
+
258
+ r.get('/:agentId/promotions', async (c) => {
259
+ const requestId = c.get('requestId');
260
+ const scope = scopeFromQuery((n) => c.req.query(n), c.req.queries('segment') ?? []);
261
+ if (scope.kind === 'err') return badInput(c, requestId, scope.message);
262
+ const cursor = c.req.query('cursor');
263
+ const page = await releases.promotions.list({
264
+ tenantId: c.get('tenantId') as TenantId,
265
+ agentId: c.req.param('agentId'),
266
+ limit: clampLimit(c.req.query('limit')),
267
+ ...(scope.scope !== undefined && { scope: scope.scope }),
268
+ ...(cursor !== undefined && cursor.length > 0 && { cursor: cursor as Cursor }),
269
+ });
270
+ return c.json({
271
+ data: page.data.map(serializePromotion),
272
+ hasMore: page.nextCursor !== undefined,
273
+ ...(page.nextCursor !== undefined && { nextCursor: page.nextCursor as unknown as string }),
274
+ });
275
+ });
276
+
277
+ r.get('/:agentId/promotions/:promotionId', async (c) => {
278
+ const requestId = c.get('requestId');
279
+ const found = await releases.promotions.get(
280
+ c.get('tenantId') as TenantId,
281
+ c.req.param('promotionId'),
282
+ );
283
+ if (found === null || found.agentId !== c.req.param('agentId')) {
284
+ c.status(statusFor('promotion-not-found') as never);
285
+ return c.json(
286
+ toWireError(
287
+ { code: 'promotion-not-found', message: 'No such promotion for this agent' },
288
+ requestId,
289
+ ),
290
+ );
291
+ }
292
+ return c.json(serializePromotion(found));
293
+ });
294
+
295
+ r.post('/:agentId/live/rollback', async (c) => {
296
+ const requestId = c.get('requestId');
297
+ const body = await readBody(c);
298
+ if (body === undefined) return badJson(c, requestId);
299
+ const scope = parseLiveScopeBody(body.scope);
300
+ if (scope.kind === 'err') return badInput(c, requestId, scope.message);
301
+ if (
302
+ body.toVersion !== undefined &&
303
+ (typeof body.toVersion !== 'string' || body.toVersion === '')
304
+ ) {
305
+ return badInput(c, requestId, '`toVersion` must be a version when supplied');
306
+ }
307
+ const text = optionalText(body, ['reason']);
308
+ if (text.kind === 'err') return badInput(c, requestId, text.message);
309
+ const outcome = await releases.promotions.rollback({
310
+ tenantId: c.get('tenantId') as TenantId,
311
+ agentId: c.req.param('agentId'),
312
+ scope: scope.scope,
313
+ requestedBy: actorOf(c),
314
+ ...(typeof body.toVersion === 'string' && { toVersion: body.toVersion as Semver }),
315
+ ...text.value,
316
+ });
317
+ if (outcome.kind === 'err') return failed(c, requestId, outcome.error);
318
+ return c.json(serializePromotion(outcome.value));
319
+ });
320
+
321
+ r.post('/:agentId/live/unpin', async (c) => {
322
+ const requestId = c.get('requestId');
323
+ const body = await readBody(c);
324
+ if (body === undefined) return badJson(c, requestId);
325
+ const scope = parseLiveScopeBody(body.scope);
326
+ if (scope.kind === 'err') return badInput(c, requestId, scope.message);
327
+ const text = optionalText(body, ['reason']);
328
+ if (text.kind === 'err') return badInput(c, requestId, text.message);
329
+ const outcome = await releases.promotions.unpin({
330
+ tenantId: c.get('tenantId') as TenantId,
331
+ agentId: c.req.param('agentId'),
332
+ scope: scope.scope,
333
+ requestedBy: actorOf(c),
334
+ ...text.value,
335
+ });
336
+ if (outcome.kind === 'err') return failed(c, requestId, outcome.error);
337
+ return c.json(serializePromotion(outcome.value));
338
+ });
339
+ }
340
+
341
+ type Ctx = Context<AppEnv>;
342
+
343
+ async function readBody(c: Ctx): Promise<Record<string, unknown> | undefined> {
344
+ try {
345
+ const body: unknown = await c.req.json();
346
+ return body !== null && typeof body === 'object' && !Array.isArray(body)
347
+ ? (body as Record<string, unknown>)
348
+ : undefined;
349
+ } catch {
350
+ return undefined;
351
+ }
352
+ }
353
+
354
+ function badJson(c: Ctx, requestId: string) {
355
+ return badInput(c, requestId, 'Request body must be a JSON object');
356
+ }
357
+
358
+ function badInput(c: Ctx, requestId: string, message: string) {
359
+ c.status(statusFor('bad-input') as never);
360
+ return c.json(toWireError({ code: 'bad-input', message }, requestId));
361
+ }
362
+
363
+ function failed(c: Ctx, requestId: string, error: { code: string; message: string }) {
364
+ c.status(statusFor(error.code) as never);
365
+ return c.json(toWireError(error, requestId));
366
+ }
367
+
368
+ /** Optional short strings (`reason`, `evalRunId`): absent, or a non-empty string. */
369
+ function optionalText(
370
+ body: Readonly<Record<string, unknown>>,
371
+ fields: readonly ('reason' | 'evalRunId')[],
372
+ ):
373
+ | { kind: 'ok'; value: { reason?: string; evalRunId?: string } }
374
+ | { kind: 'err'; message: string } {
375
+ const value: { reason?: string; evalRunId?: string } = {};
376
+ for (const field of fields) {
377
+ const v = body[field];
378
+ if (v === undefined) continue;
379
+ if (typeof v !== 'string' || v.trim() === '' || v.length > 2000) {
380
+ return { kind: 'err', message: `\`${field}\` must be a non-empty string when supplied` };
381
+ }
382
+ value[field] = v;
383
+ }
384
+ return { kind: 'ok', value };
385
+ }
386
+
387
+ /** The promotion body: `{version, scope, evalRunId?, reason?}`. */
388
+ async function readPromotionBody(c: Ctx): Promise<
389
+ | {
390
+ kind: 'ok';
391
+ value: { version: Semver; scope: LiveScope; evalRunId?: string; reason?: string };
392
+ }
393
+ | { kind: 'err'; message: string }
394
+ > {
395
+ const body = await readBody(c);
396
+ if (body === undefined) return { kind: 'err', message: 'Request body must be a JSON object' };
397
+ const scope = parseLiveScopeBody(body.scope);
398
+ if (scope.kind === 'err') return scope;
399
+ if (typeof body.version !== 'string' || body.version.length === 0) {
400
+ return { kind: 'err', message: '`version` is required: the agent version to make live' };
401
+ }
402
+ const text = optionalText(body, ['reason', 'evalRunId']);
403
+ if (text.kind === 'err') return text;
404
+ return {
405
+ kind: 'ok',
406
+ value: { version: body.version as Semver, scope: scope.scope, ...text.value },
407
+ };
408
+ }
409
+
410
+ /** The gate policy for a promotion of `agentId` for `scope`; `null` when none applies. */
411
+ async function policyFor(
412
+ releases: AgentReleaseBindings,
413
+ tenantId: TenantId,
414
+ agentId: string,
415
+ scope: LiveScope,
416
+ ): Promise<GatePolicy | null> {
417
+ return releases.gatePolicies === undefined
418
+ ? null
419
+ : releases.gatePolicies.resolve({ tenantId, agentId, scope });
420
+ }
421
+
422
+ /** The scope's coordinates, as a run in it would resolve its version. */
423
+ function coordinatesOf(scope: LiveScope): Omit<LiveResolveInput, 'tenantId' | 'agentId'> {
424
+ switch (scope.kind) {
425
+ case 'tenant':
426
+ return {};
427
+ case 'org':
428
+ return { orgId: scope.orgId };
429
+ case 'project':
430
+ return { projectId: scope.projectId };
431
+ case 'segment':
432
+ return { projectId: scope.projectId, segments: scope.path };
433
+ }
434
+ }
435
+
436
+ /** A finished comparison's summary, or `null` for any other eval run. */
437
+ function summaryOf(run: EvalRun): JudgedComparisonSummary | null {
438
+ const summary = run.result?.summary;
439
+ return run.kind === 'judged' && summary !== null && typeof summary === 'object'
440
+ ? (summary as JudgedComparisonSummary)
441
+ : null;
442
+ }
443
+
444
+ /**
445
+ * The gate for a promotion request: what serves the scope now, the
446
+ * comparison it names, and the policy's checks against them. With no
447
+ * policy there's nothing to check.
448
+ */
449
+ async function runGate(
450
+ registry: AgentRegistryBinding,
451
+ releases: AgentReleaseBindings,
452
+ deps: AgentReleaseGateDeps,
453
+ req: {
454
+ readonly tenantId: TenantId;
455
+ readonly agentId: string;
456
+ readonly version: Semver;
457
+ readonly scope: LiveScope;
458
+ readonly evalRunId?: string;
459
+ },
460
+ policy: GatePolicy | null,
461
+ ): Promise<
462
+ | { kind: 'ok'; result: GateResult; servingVersion: string }
463
+ | { kind: 'err'; error: { code: string; message: string } }
464
+ > {
465
+ const { tenantId, agentId } = req;
466
+ const err = (code: string, message: string) => ({
467
+ kind: 'err' as const,
468
+ error: { code, message },
469
+ });
470
+ const promoted = await registry.getVersion({
471
+ tenantId,
472
+ agentId: agentId as AgentId,
473
+ version: req.version,
474
+ });
475
+ if (promoted === null || promoted.unregisteredAt !== undefined) {
476
+ return err('agent-version-not-found', `${agentId} has no active version ${req.version}`);
477
+ }
478
+ const live = await releases.live.resolve({ tenantId, agentId, ...coordinatesOf(req.scope) });
479
+ let servingVersion = live?.version as unknown as string | undefined;
480
+ if (servingVersion === undefined) {
481
+ const latest = await registry.get({ tenantId, agentId: agentId as AgentId });
482
+ if (latest === null) return err('agent-not-found', `No agent "${agentId}" is registered`);
483
+ servingVersion = latest.version as unknown as string;
484
+ }
485
+ if (policy === null) return { kind: 'ok', result: { checks: [], passed: true }, servingVersion };
486
+
487
+ let summary: JudgedComparisonSummary | null = null;
488
+ if (req.evalRunId !== undefined) {
489
+ const run =
490
+ deps.evalRuns === undefined
491
+ ? null
492
+ : await deps.evalRuns.get({ tenantId, runId: req.evalRunId as RunId });
493
+ if (run === null) return err('eval-run-not-found', `No eval run ${req.evalRunId}`);
494
+ summary = summaryOf(run);
495
+ if (summary === null) {
496
+ return err(
497
+ 'bad-input',
498
+ `Eval run ${req.evalRunId} isn't a finished comparison: it has no comparison summary`,
499
+ );
500
+ }
501
+ }
502
+ let summaryProjectOrgId: string | undefined;
503
+ const judgedIn = summary?.scope.projectId;
504
+ if (req.scope.kind === 'org' && judgedIn !== undefined && deps.projects !== undefined) {
505
+ const project = await deps.projects.get(tenantId, judgedIn as ProjectId);
506
+ summaryProjectOrgId = project?.orgId as unknown as string | undefined;
507
+ }
508
+ const result = evaluateGate({
509
+ spec: policy.spec,
510
+ promotion: {
511
+ agentId,
512
+ version: req.version as unknown as string,
513
+ pinsDigest: promoted.pinsDigest ?? null,
514
+ scope: req.scope,
515
+ },
516
+ summary,
517
+ servingVersion,
518
+ ...(summaryProjectOrgId !== undefined && { summaryProjectOrgId }),
519
+ now: new Date(),
520
+ });
521
+ return { kind: 'ok', result, servingVersion };
522
+ }
523
+
524
+ /** Who asked, from the request's principal. */
525
+ function actorOf(c: Ctx): PromotionActor {
526
+ const actor = c.get('principal')?.actor;
527
+ if (actor === undefined) return { kind: 'service', id: 'unknown' };
528
+ return { kind: actor.kind === 'user' ? 'user' : 'service', id: actor.id };
529
+ }
530
+
531
+ /** `?scopeKind=tenant|org|project|segment&scopeId=…&segment=key:value` for the history filter. */
532
+ export function scopeFromQuery(
533
+ query: (name: string) => string | undefined,
534
+ segmentValues: readonly string[],
535
+ ): { kind: 'ok'; scope?: LiveScope } | { kind: 'err'; message: string } {
536
+ const kind = query('scopeKind');
537
+ const id = query('scopeId');
538
+ if (kind === undefined || kind === '') {
539
+ return id === undefined
540
+ ? { kind: 'ok' }
541
+ : { kind: 'err', message: '`scopeId` needs `scopeKind`' };
542
+ }
543
+ if (kind === 'tenant') return { kind: 'ok', scope: { kind: 'tenant' } };
544
+ if (id === undefined || !UUID_RE.test(id)) {
545
+ return { kind: 'err', message: '`scopeId` must be an org or project id (a UUID)' };
546
+ }
547
+ if (kind === 'org') return { kind: 'ok', scope: { kind: 'org', orgId: id as OrgId } };
548
+ if (kind === 'project')
549
+ return { kind: 'ok', scope: { kind: 'project', projectId: id as ProjectId } };
550
+ if (kind === 'segment') {
551
+ const path = parseSegmentsQuery(segmentValues);
552
+ if (path.kind === 'err') return path;
553
+ if (path.segments === undefined)
554
+ return { kind: 'err', message: 'A segment scope needs `segment`' };
555
+ return {
556
+ kind: 'ok',
557
+ scope: { kind: 'segment', projectId: id as ProjectId, path: path.segments },
558
+ };
559
+ }
560
+ return { kind: 'err', message: '`scopeKind` must be one of tenant, org, project, segment' };
561
+ }
562
+
563
+ export function serializePromotion(p: Promotion): Record<string, unknown> {
564
+ return {
565
+ id: p.id,
566
+ agentId: p.agentId,
567
+ scope: liveScopeToWire(p.scope),
568
+ action: p.action,
569
+ fromVersion: p.fromVersion as unknown as string | null,
570
+ toVersion: p.toVersion as unknown as string | null,
571
+ requestedBy: p.requestedBy,
572
+ ...(p.reason !== undefined && { reason: p.reason }),
573
+ ...(p.evalRunId !== undefined && { evalRunId: p.evalRunId }),
574
+ createdAt: p.createdAt as unknown as string,
575
+ ...(p.status !== undefined && { status: p.status }),
576
+ ...(p.policy !== undefined && { policy: p.policy }),
577
+ ...(p.checks !== undefined && { checks: p.checks }),
578
+ ...(p.approvalId !== undefined && { approvalId: p.approvalId }),
579
+ ...(p.resolvedAt !== undefined && { resolvedAt: p.resolvedAt as unknown as string }),
580
+ };
581
+ }