@opengeni/contracts 2.5.0-canary.2 → 2.7.0

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.
@@ -0,0 +1,266 @@
1
+ import { z } from "zod";
2
+ import { normalizeAutomaticSessionTitle } from "./session-titles";
3
+
4
+ export const WORK_CLAIM_NAMESPACE_MAX_BYTES = 64;
5
+ export const WORK_CLAIM_CANONICAL_KEY_MAX_BYTES = 512;
6
+ export const WORK_CLAIM_DISPLAY_LABEL_MAX_BYTES = 256;
7
+ export const WORK_CLAIM_VERSION_VALUE_MAX_BYTES = 256;
8
+ export const WORK_CLAIM_ACTIVE_SESSION_CAP = 64;
9
+ export const WORK_CLAIM_DISCOVERY_LIMIT = 8;
10
+ export const WORK_CLAIM_DISCOVERY_DEFAULT_LIMIT = 4;
11
+ export const WORK_DISCOVERY_QUERY_MAX_CHARS = 200;
12
+ export const WORK_DISCOVERY_RECENT_HOURS_MAX = 24 * 365;
13
+
14
+ const utf8Bytes = (value: string): number => new TextEncoder().encode(value).byteLength;
15
+ const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f]/u;
16
+ const NAMESPACE_PATTERN = /^[a-z0-9](?:[a-z0-9._:-]{0,62}[a-z0-9])?$/u;
17
+
18
+ function boundedCanonicalText(maxBytes: number, label: string) {
19
+ return z
20
+ .string()
21
+ .min(1)
22
+ .superRefine((value, context) => {
23
+ if (value !== value.trim()) {
24
+ context.addIssue({
25
+ code: z.ZodIssueCode.custom,
26
+ message: `${label} must not have leading or trailing whitespace`,
27
+ });
28
+ }
29
+ if (value !== value.normalize("NFKC")) {
30
+ context.addIssue({
31
+ code: z.ZodIssueCode.custom,
32
+ message: `${label} must use canonical Unicode normalization`,
33
+ });
34
+ }
35
+ if (CONTROL_CHARACTERS.test(value)) {
36
+ context.addIssue({
37
+ code: z.ZodIssueCode.custom,
38
+ message: `${label} must not contain control characters`,
39
+ });
40
+ }
41
+ if (utf8Bytes(value) > maxBytes) {
42
+ context.addIssue({
43
+ code: z.ZodIssueCode.custom,
44
+ message: `${label} must be at most ${maxBytes} UTF-8 bytes`,
45
+ });
46
+ }
47
+ });
48
+ }
49
+
50
+ export function normalizeWorkClaimNamespace(value: string): string {
51
+ return value.normalize("NFKC").trim().toLowerCase();
52
+ }
53
+
54
+ export function normalizeWorkClaimCanonicalKey(value: string): string {
55
+ return value.normalize("NFKC").trim();
56
+ }
57
+
58
+ export function normalizeWorkClaimDisplayLabel(value: string): string | null {
59
+ const normalized = normalizeAutomaticSessionTitle(value);
60
+ if (!normalized || utf8Bytes(normalized) > WORK_CLAIM_DISPLAY_LABEL_MAX_BYTES) return null;
61
+ return normalized;
62
+ }
63
+
64
+ export const WorkClaimNamespace = z
65
+ .string()
66
+ .min(1)
67
+ .max(WORK_CLAIM_NAMESPACE_MAX_BYTES)
68
+ .regex(NAMESPACE_PATTERN)
69
+ .refine((value) => value === normalizeWorkClaimNamespace(value), {
70
+ message: "work claim namespace must already be canonical lowercase text",
71
+ });
72
+ export type WorkClaimNamespace = z.infer<typeof WorkClaimNamespace>;
73
+
74
+ export const WorkClaimCanonicalKey = boundedCanonicalText(
75
+ WORK_CLAIM_CANONICAL_KEY_MAX_BYTES,
76
+ "work claim canonical key",
77
+ );
78
+ export type WorkClaimCanonicalKey = z.infer<typeof WorkClaimCanonicalKey>;
79
+
80
+ export const WorkClaimDisplayLabel = z
81
+ .string()
82
+ .min(1)
83
+ .superRefine((value, context) => {
84
+ if (utf8Bytes(value) > WORK_CLAIM_DISPLAY_LABEL_MAX_BYTES) {
85
+ context.addIssue({
86
+ code: z.ZodIssueCode.custom,
87
+ message: `work claim display label must be at most ${WORK_CLAIM_DISPLAY_LABEL_MAX_BYTES} UTF-8 bytes`,
88
+ });
89
+ }
90
+ if (normalizeWorkClaimDisplayLabel(value) !== value) {
91
+ context.addIssue({
92
+ code: z.ZodIssueCode.custom,
93
+ message: "work claim display label must be a safe concise semantic label",
94
+ });
95
+ }
96
+ });
97
+ export type WorkClaimDisplayLabel = z.infer<typeof WorkClaimDisplayLabel>;
98
+
99
+ export const WorkClaimSubjectType = z.enum([
100
+ "repository",
101
+ "branch",
102
+ "pull_request",
103
+ "issue",
104
+ "artifact",
105
+ "release",
106
+ "ci_run",
107
+ "other",
108
+ ]);
109
+ export type WorkClaimSubjectType = z.infer<typeof WorkClaimSubjectType>;
110
+
111
+ export const WorkClaimRole = z.enum(["working", "reviewing", "monitoring", "delivering"]);
112
+ export type WorkClaimRole = z.infer<typeof WorkClaimRole>;
113
+
114
+ export const WorkClaimState = z.enum(["active", "released", "superseded", "stale"]);
115
+ export type WorkClaimState = z.infer<typeof WorkClaimState>;
116
+
117
+ export const WorkClaimProvenance = z.enum([
118
+ "explicit_agent",
119
+ "user_api",
120
+ "trusted_integration",
121
+ "session_resource",
122
+ "system_lifecycle",
123
+ ]);
124
+ export type WorkClaimProvenance = z.infer<typeof WorkClaimProvenance>;
125
+
126
+ export const WorkClaimVersionKind = z.enum([
127
+ "git_commit",
128
+ "branch_head",
129
+ "pull_request_head",
130
+ "artifact_version",
131
+ "release_version",
132
+ "ci_run",
133
+ "other",
134
+ ]);
135
+ export type WorkClaimVersionKind = z.infer<typeof WorkClaimVersionKind>;
136
+
137
+ export const WorkClaimVersionValue = boundedCanonicalText(
138
+ WORK_CLAIM_VERSION_VALUE_MAX_BYTES,
139
+ "work claim version value",
140
+ );
141
+ export type WorkClaimVersionValue = z.infer<typeof WorkClaimVersionValue>;
142
+
143
+ export const WorkClaimReleaseReason = z.enum([
144
+ "completed",
145
+ "cancelled",
146
+ "failed",
147
+ "superseded",
148
+ "no_longer_active",
149
+ "corrected",
150
+ "external_state_changed",
151
+ "other",
152
+ ]);
153
+ export type WorkClaimReleaseReason = z.infer<typeof WorkClaimReleaseReason>;
154
+
155
+ export const WorkClaimMutationKind = z.enum([
156
+ "created",
157
+ "updated",
158
+ "released",
159
+ "superseded",
160
+ "stale",
161
+ ]);
162
+ export type WorkClaimMutationKind = z.infer<typeof WorkClaimMutationKind>;
163
+
164
+ export const WorkClaim = z.object({
165
+ id: z.string().uuid(),
166
+ sessionId: z.string().uuid(),
167
+ rootSessionId: z.string().uuid(),
168
+ subject: z.object({
169
+ namespace: WorkClaimNamespace,
170
+ type: WorkClaimSubjectType,
171
+ canonicalKey: WorkClaimCanonicalKey,
172
+ displayLabel: WorkClaimDisplayLabel.nullable(),
173
+ }),
174
+ role: WorkClaimRole,
175
+ state: WorkClaimState,
176
+ revision: z.number().int().positive(),
177
+ provenance: WorkClaimProvenance,
178
+ version: z
179
+ .object({
180
+ kind: WorkClaimVersionKind,
181
+ value: WorkClaimVersionValue,
182
+ })
183
+ .nullable(),
184
+ observedAt: z.string().datetime({ offset: true }),
185
+ createdAt: z.string().datetime({ offset: true }),
186
+ updatedAt: z.string().datetime({ offset: true }),
187
+ settledAt: z.string().datetime({ offset: true }).nullable(),
188
+ });
189
+ export type WorkClaim = z.infer<typeof WorkClaim>;
190
+
191
+ export const WorkClaimMutationResult = z.object({
192
+ claim: WorkClaim,
193
+ mutation: WorkClaimMutationKind,
194
+ replayed: z.boolean(),
195
+ });
196
+ export type WorkClaimMutationResult = z.infer<typeof WorkClaimMutationResult>;
197
+
198
+ export const WorkClaimDiscoverySummary = WorkClaim.pick({
199
+ id: true,
200
+ sessionId: true,
201
+ subject: true,
202
+ role: true,
203
+ state: true,
204
+ revision: true,
205
+ provenance: true,
206
+ version: true,
207
+ observedAt: true,
208
+ updatedAt: true,
209
+ settledAt: true,
210
+ });
211
+ export type WorkClaimDiscoverySummary = z.infer<typeof WorkClaimDiscoverySummary>;
212
+
213
+ export const WorkClaimSubjectFilter = z
214
+ .object({
215
+ namespace: WorkClaimNamespace,
216
+ type: WorkClaimSubjectType,
217
+ canonicalKey: WorkClaimCanonicalKey,
218
+ })
219
+ .strict();
220
+ export type WorkClaimSubjectFilter = z.infer<typeof WorkClaimSubjectFilter>;
221
+
222
+ export const WorkDiscoveryMatchClass = z.enum(["exact_subject", "title", "goal", "fuzzy"]);
223
+ export type WorkDiscoveryMatchClass = z.infer<typeof WorkDiscoveryMatchClass>;
224
+
225
+ export const WorkDiscoveryMatchedField = z.enum([
226
+ "subject",
227
+ "title",
228
+ "goal",
229
+ "claim_key",
230
+ "claim_label",
231
+ ]);
232
+ export type WorkDiscoveryMatchedField = z.infer<typeof WorkDiscoveryMatchedField>;
233
+
234
+ /**
235
+ * Deliberately stable bands rather than a raw ranking score. Raw full-text or
236
+ * trigram values are query-relative and become a misleading cross-query API.
237
+ */
238
+ export const WorkDiscoveryScoreBand = z.enum(["exact", "strong", "related"]);
239
+ export type WorkDiscoveryScoreBand = z.infer<typeof WorkDiscoveryScoreBand>;
240
+
241
+ export const WorkDiscoveryMatch = z
242
+ .object({
243
+ class: WorkDiscoveryMatchClass,
244
+ field: WorkDiscoveryMatchedField,
245
+ scoreBand: WorkDiscoveryScoreBand,
246
+ claimId: z.string().uuid().nullable(),
247
+ })
248
+ .strict();
249
+ export type WorkDiscoveryMatch = z.infer<typeof WorkDiscoveryMatch>;
250
+
251
+ /**
252
+ * Provider-neutral related-work evidence. The two literal booleans are part of
253
+ * the wire contract so a consumer cannot accidentally present a claim as a
254
+ * lock, authorization grant, ownership transfer, or mandatory instruction.
255
+ */
256
+ export const WorkDiscoveryProjection = z
257
+ .object({
258
+ claims: z.array(WorkClaimDiscoverySummary).max(WORK_CLAIM_DISCOVERY_LIMIT),
259
+ claimsTruncated: z.boolean(),
260
+ match: WorkDiscoveryMatch.nullable(),
261
+ possibleOverlap: z.boolean(),
262
+ advisoryOnly: z.literal(true),
263
+ noAdditionalAccess: z.literal(true),
264
+ })
265
+ .strict();
266
+ export type WorkDiscoveryProjection = z.infer<typeof WorkDiscoveryProjection>;