@qlairoslabs/acp-client 0.5.0 → 0.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.
- package/CHANGELOG-metering.md +288 -0
- package/CONSUMER_GUIDE.md +579 -292
- package/README.md +25 -0
- package/package.json +6 -3
- package/src/agent-session.ts +2589 -96
- package/src/cli/index.ts +33 -5
- package/src/cli/managed.ts +220 -0
- package/src/client.ts +57 -1
- package/src/controller.ts +43 -0
- package/src/index.ts +379 -3
- package/src/metering/catalog/build.ts +285 -0
- package/src/metering/catalog/index.ts +243 -0
- package/src/metering/catalog/live.ts +171 -0
- package/src/metering/catalog/models-dev.catalog.json +8797 -0
- package/src/metering/catalog/models-dev.meta.json +6 -0
- package/src/metering/categories.ts +129 -0
- package/src/metering/certification.ts +86 -0
- package/src/metering/context.ts +134 -0
- package/src/metering/delta.ts +108 -0
- package/src/metering/engine.ts +999 -0
- package/src/metering/money.ts +356 -0
- package/src/metering/opencode/channel.ts +389 -0
- package/src/metering/opencode/index.ts +159 -0
- package/src/metering/opencode/plugin.js +90 -0
- package/src/metering/opencode/profile.ts +159 -0
- package/src/metering/pricing.ts +285 -0
- package/src/metering/provider.ts +121 -0
- package/src/metering/rating.ts +349 -0
- package/src/metering/record.ts +275 -0
- package/src/metering/state.ts +421 -0
- package/src/metering/usage.ts +72 -0
- package/src/persistence/activity-projection.ts +71 -0
- package/src/persistence/audit-log-reader.ts +506 -0
- package/src/persistence/audit-runtime.ts +162 -0
- package/src/persistence/checkpoint.ts +304 -0
- package/src/persistence/config.ts +538 -0
- package/src/persistence/configuration-state.ts +191 -0
- package/src/persistence/deletion.ts +227 -0
- package/src/persistence/file-registry-store.ts +740 -0
- package/src/persistence/integrity.ts +147 -0
- package/src/persistence/legacy-registry-import.ts +240 -0
- package/src/persistence/managed-checkpoint.ts +81 -0
- package/src/persistence/managed-creation.ts +456 -0
- package/src/persistence/managed-load.ts +172 -0
- package/src/persistence/managed-registry.ts +57 -0
- package/src/persistence/migration.ts +298 -0
- package/src/persistence/paths.ts +172 -0
- package/src/persistence/projection.ts +373 -0
- package/src/persistence/reconfiguration.ts +453 -0
- package/src/persistence/records.ts +873 -0
- package/src/persistence/reducer.ts +498 -0
- package/src/persistence/registry.ts +446 -0
- package/src/persistence/restoration.ts +689 -0
- package/src/persistence/session-log-writer.ts +480 -0
- package/src/persistence/state-runtime.ts +176 -0
- package/src/persistence/transcript-reader.ts +980 -0
- package/src/persistence/transcript-runtime.ts +667 -0
- package/src/persistence/transcript-scanner.ts +385 -0
- package/src/persistence/truncation.ts +276 -0
- package/src/session-manager.ts +257 -7
- package/src/transcript.ts +3 -3
- package/src/types.ts +28 -5
|
@@ -0,0 +1,873 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AuditLogCapturePolicy,
|
|
3
|
+
PersistenceLimits,
|
|
4
|
+
TranscriptCapturePolicy,
|
|
5
|
+
TranscriptPreset,
|
|
6
|
+
} from "./config.ts";
|
|
7
|
+
import {
|
|
8
|
+
validateAuditLogCapturePolicy,
|
|
9
|
+
validatePersistenceLimits,
|
|
10
|
+
validateTranscriptCapturePolicy,
|
|
11
|
+
} from "./config.ts";
|
|
12
|
+
import {
|
|
13
|
+
validateAgentSessionCheckpoint,
|
|
14
|
+
validateSessionConfigurationCheckpoint,
|
|
15
|
+
type AgentSessionBoundary,
|
|
16
|
+
type AgentSessionCheckpoint,
|
|
17
|
+
type JsonObject,
|
|
18
|
+
type JsonValue,
|
|
19
|
+
type PersistedProtocol,
|
|
20
|
+
type SessionConfigurationCheckpoint,
|
|
21
|
+
type TransitionKind,
|
|
22
|
+
} from "./checkpoint.ts";
|
|
23
|
+
import {
|
|
24
|
+
canonicalizeJson,
|
|
25
|
+
isPersistedMachineCode,
|
|
26
|
+
type PersistedMachineCode,
|
|
27
|
+
} from "./integrity.ts";
|
|
28
|
+
|
|
29
|
+
export type OperationKind =
|
|
30
|
+
| "prompt"
|
|
31
|
+
| "set_config"
|
|
32
|
+
| "internal_session_command"
|
|
33
|
+
| "handoff_summary"
|
|
34
|
+
| "handoff_seed"
|
|
35
|
+
| "switch_seed"
|
|
36
|
+
| "other";
|
|
37
|
+
|
|
38
|
+
export type OperationEvent =
|
|
39
|
+
| "started"
|
|
40
|
+
| "settled"
|
|
41
|
+
| "failed"
|
|
42
|
+
| "cancelled"
|
|
43
|
+
| "interrupted"
|
|
44
|
+
| "aborted";
|
|
45
|
+
|
|
46
|
+
export type OperationAbortReason =
|
|
47
|
+
| "pre_dispatch_persistence_failure"
|
|
48
|
+
| "pre_dispatch_cancelled";
|
|
49
|
+
|
|
50
|
+
export interface PersistedTruncation {
|
|
51
|
+
readonly field: string;
|
|
52
|
+
readonly reason: "field_limit" | "record_limit";
|
|
53
|
+
readonly originalBytes: number;
|
|
54
|
+
readonly originalSha256?: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface OmittedPersistedValue {
|
|
58
|
+
readonly omitted: true;
|
|
59
|
+
readonly reason: "field_limit" | "record_limit" | "max_depth";
|
|
60
|
+
readonly originalBytes?: number;
|
|
61
|
+
readonly originalSha256?: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface SessionLogInstallation {
|
|
65
|
+
readonly kind: "creation" | "persistence_reconfiguration" | "restoration";
|
|
66
|
+
readonly operationId: string;
|
|
67
|
+
readonly requestDigest: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface InternalSessionHistoryTransition {
|
|
71
|
+
readonly kind: TransitionKind;
|
|
72
|
+
readonly transitionId: string;
|
|
73
|
+
readonly reasonCode?: PersistedMachineCode;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface InternalSessionHistoryEntry {
|
|
77
|
+
readonly internalSessionId: string;
|
|
78
|
+
readonly adapterId: string;
|
|
79
|
+
readonly model: string | null;
|
|
80
|
+
readonly sessionConfiguration: SessionConfigurationCheckpoint;
|
|
81
|
+
readonly protocol: PersistedProtocol;
|
|
82
|
+
readonly activatedAt: string;
|
|
83
|
+
readonly deactivatedAt: string;
|
|
84
|
+
readonly transition: InternalSessionHistoryTransition;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface InternalSessionDescriptor {
|
|
88
|
+
readonly internalSessionId: string | null;
|
|
89
|
+
readonly adapterId: string;
|
|
90
|
+
readonly mode: "normal" | "degraded";
|
|
91
|
+
readonly model: string | null;
|
|
92
|
+
readonly sessionConfiguration: SessionConfigurationCheckpoint;
|
|
93
|
+
readonly activatedAt: string | null;
|
|
94
|
+
readonly protocol: PersistedProtocol;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type TranscriptRecordType =
|
|
98
|
+
| "header"
|
|
99
|
+
| "checkpoint"
|
|
100
|
+
| "operation"
|
|
101
|
+
| "message"
|
|
102
|
+
| "reasoning"
|
|
103
|
+
| "activity"
|
|
104
|
+
| "configuration"
|
|
105
|
+
| "permission"
|
|
106
|
+
| "transition"
|
|
107
|
+
| "metering_reference"
|
|
108
|
+
| "lifecycle"
|
|
109
|
+
| "protocol"
|
|
110
|
+
| "raw";
|
|
111
|
+
|
|
112
|
+
export interface TranscriptEnvelope {
|
|
113
|
+
readonly v: 2;
|
|
114
|
+
readonly t: TranscriptRecordType;
|
|
115
|
+
readonly transcriptId: string;
|
|
116
|
+
readonly generation: number;
|
|
117
|
+
readonly agentSessionId: string;
|
|
118
|
+
readonly seq: number;
|
|
119
|
+
readonly ts: number;
|
|
120
|
+
readonly operationId?: string;
|
|
121
|
+
readonly stateRevision?: number;
|
|
122
|
+
readonly truncation?: readonly PersistedTruncation[];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface TranscriptHeaderRecord extends TranscriptEnvelope {
|
|
126
|
+
readonly t: "header";
|
|
127
|
+
readonly event: "created" | "upgraded" | "replaced";
|
|
128
|
+
readonly createdAt: string;
|
|
129
|
+
readonly preset: TranscriptPreset;
|
|
130
|
+
readonly effectivePolicy: TranscriptCapturePolicy;
|
|
131
|
+
readonly effectiveLimits: PersistenceLimits;
|
|
132
|
+
readonly includeTruncationDigests: boolean;
|
|
133
|
+
readonly upgradedFrom?: { readonly version: 1; readonly boundaryOffset: number };
|
|
134
|
+
readonly installation?: SessionLogInstallation;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface TranscriptCheckpointRecord extends TranscriptEnvelope {
|
|
138
|
+
readonly t: "checkpoint";
|
|
139
|
+
readonly stateRevision: number;
|
|
140
|
+
readonly checkpoint: AgentSessionCheckpoint;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface TranscriptOperationRecord extends TranscriptEnvelope {
|
|
144
|
+
readonly t: "operation";
|
|
145
|
+
readonly event: OperationEvent;
|
|
146
|
+
readonly operationId: string;
|
|
147
|
+
readonly operationKind: OperationKind;
|
|
148
|
+
readonly errorCode?: PersistedMachineCode;
|
|
149
|
+
readonly abortReason?: OperationAbortReason;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface TranscriptResponseMessageV1 {
|
|
153
|
+
readonly messageId?: string;
|
|
154
|
+
readonly text: string;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface TranscriptStructuredMessagesV1 {
|
|
158
|
+
readonly version: 1;
|
|
159
|
+
readonly messages: readonly TranscriptResponseMessageV1[];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface TranscriptMessageRecord extends TranscriptEnvelope {
|
|
163
|
+
readonly t: "message";
|
|
164
|
+
readonly operationId: string;
|
|
165
|
+
readonly role: "user" | "assistant";
|
|
166
|
+
readonly text: string;
|
|
167
|
+
readonly structured?: TranscriptStructuredMessagesV1;
|
|
168
|
+
readonly status?: string;
|
|
169
|
+
readonly stopReason?: string;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface TranscriptReasoningRecord extends TranscriptEnvelope {
|
|
173
|
+
readonly t: "reasoning";
|
|
174
|
+
readonly reasoningId: string;
|
|
175
|
+
readonly reasoningRevision: number;
|
|
176
|
+
readonly operationId: string;
|
|
177
|
+
readonly text: string;
|
|
178
|
+
readonly status: "in_progress" | "completed";
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export interface TranscriptActivityRecord extends TranscriptEnvelope {
|
|
182
|
+
readonly t: "activity";
|
|
183
|
+
readonly activityId: string;
|
|
184
|
+
readonly activityRevision: number;
|
|
185
|
+
readonly operationId: string;
|
|
186
|
+
readonly reason: "announced" | "input" | "settled" | "flushed" | "snapshot";
|
|
187
|
+
readonly kind: string;
|
|
188
|
+
readonly status?: string;
|
|
189
|
+
readonly data: Readonly<Record<string, JsonValue>>;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export interface TranscriptConfigurationRecord extends TranscriptEnvelope {
|
|
193
|
+
readonly t: "configuration";
|
|
194
|
+
readonly event: "announced" | "selected" | "restored" | "restore_failed" | "resolved";
|
|
195
|
+
readonly configId?: string;
|
|
196
|
+
readonly value?: JsonValue;
|
|
197
|
+
readonly definitions?: JsonValue;
|
|
198
|
+
readonly schemaDigest?: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface TranscriptPermissionRecord extends TranscriptEnvelope {
|
|
202
|
+
readonly t: "permission";
|
|
203
|
+
readonly permissionId: string;
|
|
204
|
+
readonly activityId?: string;
|
|
205
|
+
readonly event: "requested" | "resolved";
|
|
206
|
+
readonly outcome?: string;
|
|
207
|
+
readonly details?: JsonValue;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export interface TranscriptTransitionRecord extends TranscriptEnvelope {
|
|
211
|
+
readonly t: "transition";
|
|
212
|
+
readonly transitionId: string;
|
|
213
|
+
readonly kind: TransitionKind;
|
|
214
|
+
readonly event: "started" | "target_created" | "target_activated" | "completed" | "failed";
|
|
215
|
+
readonly from: InternalSessionDescriptor | null;
|
|
216
|
+
readonly to?: InternalSessionDescriptor;
|
|
217
|
+
readonly historyEntry?: InternalSessionHistoryEntry;
|
|
218
|
+
readonly activationState?: {
|
|
219
|
+
readonly resultingStateRevision: number;
|
|
220
|
+
readonly updatedAt: string;
|
|
221
|
+
readonly transitionAt: string;
|
|
222
|
+
readonly boundary: AgentSessionBoundary;
|
|
223
|
+
};
|
|
224
|
+
readonly failedAfter?: "start" | "target_created" | "target_activated";
|
|
225
|
+
readonly summary?: string;
|
|
226
|
+
readonly reasonCode?: PersistedMachineCode;
|
|
227
|
+
readonly reasonText?: string;
|
|
228
|
+
readonly errorCode?: PersistedMachineCode;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export interface TranscriptMeteringReferenceRecord extends TranscriptEnvelope {
|
|
232
|
+
readonly t: "metering_reference";
|
|
233
|
+
readonly operationId: string;
|
|
234
|
+
readonly meteringRecordId: string;
|
|
235
|
+
readonly status: "available" | "partial" | "unavailable";
|
|
236
|
+
readonly summary?: JsonValue;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export interface TranscriptLifecycleRecord extends TranscriptEnvelope {
|
|
240
|
+
readonly t: "lifecycle";
|
|
241
|
+
readonly event:
|
|
242
|
+
| "session_opened"
|
|
243
|
+
| "stop_requested"
|
|
244
|
+
| "session_stopped"
|
|
245
|
+
| "transport_exited"
|
|
246
|
+
| "recovery_required"
|
|
247
|
+
| "recovery_resolved";
|
|
248
|
+
readonly detail?: JsonValue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export interface TranscriptProtocolMetadataRecord extends TranscriptEnvelope {
|
|
252
|
+
readonly t: "protocol";
|
|
253
|
+
readonly event: "initialized" | "capabilities_updated";
|
|
254
|
+
readonly protocol: PersistedProtocol;
|
|
255
|
+
readonly agent?: { readonly name?: string; readonly title?: string; readonly version?: string };
|
|
256
|
+
readonly capabilities: Readonly<Record<string, boolean>>;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export interface TranscriptRawRecord extends TranscriptEnvelope {
|
|
260
|
+
readonly t: "raw";
|
|
261
|
+
readonly protocol: string;
|
|
262
|
+
readonly direction: "client_to_agent" | "agent_to_client";
|
|
263
|
+
readonly event: string;
|
|
264
|
+
readonly data: JsonValue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export type KnownTranscriptRecord =
|
|
268
|
+
| TranscriptHeaderRecord
|
|
269
|
+
| TranscriptCheckpointRecord
|
|
270
|
+
| TranscriptOperationRecord
|
|
271
|
+
| TranscriptMessageRecord
|
|
272
|
+
| TranscriptReasoningRecord
|
|
273
|
+
| TranscriptActivityRecord
|
|
274
|
+
| TranscriptConfigurationRecord
|
|
275
|
+
| TranscriptPermissionRecord
|
|
276
|
+
| TranscriptTransitionRecord
|
|
277
|
+
| TranscriptMeteringReferenceRecord
|
|
278
|
+
| TranscriptLifecycleRecord
|
|
279
|
+
| TranscriptProtocolMetadataRecord
|
|
280
|
+
| TranscriptRawRecord;
|
|
281
|
+
|
|
282
|
+
export interface UnknownTranscriptRecord {
|
|
283
|
+
readonly v: number;
|
|
284
|
+
readonly t: string;
|
|
285
|
+
readonly transcriptId: string;
|
|
286
|
+
readonly generation: number;
|
|
287
|
+
readonly agentSessionId: string;
|
|
288
|
+
readonly seq: number;
|
|
289
|
+
readonly ts: number;
|
|
290
|
+
readonly value: JsonObject;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export type AuditLogRecordType =
|
|
294
|
+
| "header"
|
|
295
|
+
| "operation"
|
|
296
|
+
| "reasoning"
|
|
297
|
+
| "activity"
|
|
298
|
+
| "configuration"
|
|
299
|
+
| "permission"
|
|
300
|
+
| "transition"
|
|
301
|
+
| "metering_reference"
|
|
302
|
+
| "lifecycle";
|
|
303
|
+
|
|
304
|
+
export interface AuditLogEnvelope {
|
|
305
|
+
readonly v: 1;
|
|
306
|
+
readonly t: AuditLogRecordType;
|
|
307
|
+
readonly auditLogId: string;
|
|
308
|
+
readonly generation: number;
|
|
309
|
+
readonly agentSessionId: string;
|
|
310
|
+
readonly seq: number;
|
|
311
|
+
readonly ts: number;
|
|
312
|
+
readonly operationId?: string;
|
|
313
|
+
readonly truncation?: readonly PersistedTruncation[];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export interface AuditLogHeaderRecord extends AuditLogEnvelope {
|
|
317
|
+
readonly t: "header";
|
|
318
|
+
readonly event: "created" | "replaced";
|
|
319
|
+
readonly createdAt: string;
|
|
320
|
+
readonly effectivePolicy: AuditLogCapturePolicy;
|
|
321
|
+
readonly effectiveLimits: PersistenceLimits;
|
|
322
|
+
readonly includeTruncationDigests: boolean;
|
|
323
|
+
readonly installation: SessionLogInstallation;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export interface AuditLogOperationRecord extends AuditLogEnvelope {
|
|
327
|
+
readonly t: "operation";
|
|
328
|
+
readonly event: OperationEvent;
|
|
329
|
+
readonly operationId: string;
|
|
330
|
+
readonly operationKind: OperationKind;
|
|
331
|
+
readonly errorCode?: PersistedMachineCode;
|
|
332
|
+
readonly abortReason?: OperationAbortReason;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface AuditLogReasoningRecord extends AuditLogEnvelope {
|
|
336
|
+
readonly t: "reasoning";
|
|
337
|
+
readonly reasoningId: string;
|
|
338
|
+
readonly reasoningRevision: number;
|
|
339
|
+
readonly operationId: string;
|
|
340
|
+
readonly text: string;
|
|
341
|
+
readonly status: "in_progress" | "completed";
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export interface AuditLogActivityRecord extends AuditLogEnvelope {
|
|
345
|
+
readonly t: "activity";
|
|
346
|
+
readonly activityId: string;
|
|
347
|
+
readonly activityRevision: number;
|
|
348
|
+
readonly operationId: string;
|
|
349
|
+
readonly reason: TranscriptActivityRecord["reason"];
|
|
350
|
+
readonly kind: string;
|
|
351
|
+
readonly status?: string;
|
|
352
|
+
readonly data: Readonly<Record<string, JsonValue>>;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export interface AuditLogConfigurationRecord extends AuditLogEnvelope {
|
|
356
|
+
readonly t: "configuration";
|
|
357
|
+
readonly event: TranscriptConfigurationRecord["event"];
|
|
358
|
+
readonly configId?: string;
|
|
359
|
+
readonly value?: JsonValue;
|
|
360
|
+
readonly definitions?: JsonValue;
|
|
361
|
+
readonly schemaDigest?: string;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
export interface AuditLogPermissionRecord extends AuditLogEnvelope {
|
|
365
|
+
readonly t: "permission";
|
|
366
|
+
readonly permissionId: string;
|
|
367
|
+
readonly activityId?: string;
|
|
368
|
+
readonly event: "requested" | "resolved";
|
|
369
|
+
readonly outcome?: string;
|
|
370
|
+
readonly details?: JsonValue;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export type AuditInternalSessionDescriptor = Omit<InternalSessionDescriptor, "sessionConfiguration" | "activatedAt">;
|
|
374
|
+
|
|
375
|
+
export interface AuditLogTransitionRecord extends AuditLogEnvelope {
|
|
376
|
+
readonly t: "transition";
|
|
377
|
+
readonly transitionId: string;
|
|
378
|
+
readonly kind: TransitionKind;
|
|
379
|
+
readonly event: TranscriptTransitionRecord["event"];
|
|
380
|
+
readonly from: AuditInternalSessionDescriptor | null;
|
|
381
|
+
readonly to?: AuditInternalSessionDescriptor;
|
|
382
|
+
readonly failedAfter?: "start" | "target_created" | "target_activated";
|
|
383
|
+
readonly errorCode?: PersistedMachineCode;
|
|
384
|
+
readonly reasonCode?: PersistedMachineCode;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export interface AuditLogMeteringReferenceRecord extends AuditLogEnvelope {
|
|
388
|
+
readonly t: "metering_reference";
|
|
389
|
+
readonly operationId: string;
|
|
390
|
+
readonly meteringRecordId: string;
|
|
391
|
+
readonly status: "available" | "partial" | "unavailable";
|
|
392
|
+
readonly summary?: JsonValue;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export interface AuditLogLifecycleRecord extends AuditLogEnvelope {
|
|
396
|
+
readonly t: "lifecycle";
|
|
397
|
+
readonly event: TranscriptLifecycleRecord["event"];
|
|
398
|
+
readonly detail?: JsonValue;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export type KnownAuditLogRecord =
|
|
402
|
+
| AuditLogHeaderRecord
|
|
403
|
+
| AuditLogOperationRecord
|
|
404
|
+
| AuditLogReasoningRecord
|
|
405
|
+
| AuditLogActivityRecord
|
|
406
|
+
| AuditLogConfigurationRecord
|
|
407
|
+
| AuditLogPermissionRecord
|
|
408
|
+
| AuditLogTransitionRecord
|
|
409
|
+
| AuditLogMeteringReferenceRecord
|
|
410
|
+
| AuditLogLifecycleRecord;
|
|
411
|
+
|
|
412
|
+
export interface UnknownAuditLogRecord {
|
|
413
|
+
readonly v: number;
|
|
414
|
+
readonly t: string;
|
|
415
|
+
readonly auditLogId: string;
|
|
416
|
+
readonly generation: number;
|
|
417
|
+
readonly agentSessionId: string;
|
|
418
|
+
readonly seq: number;
|
|
419
|
+
readonly ts: number;
|
|
420
|
+
readonly value: JsonObject;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
export interface SessionLogRecordExpectation {
|
|
424
|
+
readonly agentSessionId?: string;
|
|
425
|
+
readonly logId?: string;
|
|
426
|
+
readonly generation?: number;
|
|
427
|
+
/** Normalized path of the transcript currently being read. */
|
|
428
|
+
readonly transcriptPath?: string;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export class SessionLogRecordValidationError extends Error {
|
|
432
|
+
constructor(message: string) {
|
|
433
|
+
super(message);
|
|
434
|
+
this.name = "SessionLogRecordValidationError";
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function validateTranscriptRecord(
|
|
439
|
+
value: unknown,
|
|
440
|
+
expected: SessionLogRecordExpectation = {},
|
|
441
|
+
): asserts value is KnownTranscriptRecord {
|
|
442
|
+
const record = validateEnvelope(value, "transcript", expected);
|
|
443
|
+
if (record.v !== 2) fail("unsupported transcript record version");
|
|
444
|
+
if (!TRANSCRIPT_TYPES.has(record.t as string)) fail("unknown transcript record type");
|
|
445
|
+
validateRecognizedRecord(record, "transcript", expected);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function validateAuditLogRecord(
|
|
449
|
+
value: unknown,
|
|
450
|
+
expected: SessionLogRecordExpectation = {},
|
|
451
|
+
): asserts value is KnownAuditLogRecord {
|
|
452
|
+
const record = validateEnvelope(value, "audit", expected);
|
|
453
|
+
if (record.v !== 1) fail("unsupported audit-log record version");
|
|
454
|
+
if (!AUDIT_TYPES.has(record.t as string)) fail("unknown audit-log record type");
|
|
455
|
+
validateRecognizedRecord(record, "audit", expected);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
const TRANSCRIPT_TYPES = new Set<string>([
|
|
459
|
+
"header", "checkpoint", "operation", "message", "reasoning", "activity",
|
|
460
|
+
"configuration", "permission", "transition", "metering_reference", "lifecycle",
|
|
461
|
+
"protocol", "raw",
|
|
462
|
+
]);
|
|
463
|
+
const AUDIT_TYPES = new Set<string>([
|
|
464
|
+
"header", "operation", "reasoning", "activity", "configuration", "permission",
|
|
465
|
+
"transition", "metering_reference", "lifecycle",
|
|
466
|
+
]);
|
|
467
|
+
|
|
468
|
+
function validateEnvelope(
|
|
469
|
+
value: unknown,
|
|
470
|
+
sink: "transcript" | "audit",
|
|
471
|
+
expected: SessionLogRecordExpectation,
|
|
472
|
+
): Record<string, unknown> {
|
|
473
|
+
if (!isRecord(value)) fail(`${sink} record must be an object`);
|
|
474
|
+
// Reject non-JSON values and excessive nesting before any field is trusted.
|
|
475
|
+
try {
|
|
476
|
+
canonicalizeJson(value);
|
|
477
|
+
} catch (error) {
|
|
478
|
+
fail(`${sink} record is not canonical JSON: ${error instanceof Error ? error.message : String(error)}`);
|
|
479
|
+
}
|
|
480
|
+
nonblank(value.t, "t");
|
|
481
|
+
nonblank(value.agentSessionId, "agentSessionId");
|
|
482
|
+
positiveInteger(value.generation, "generation");
|
|
483
|
+
positiveInteger(value.seq, "seq");
|
|
484
|
+
epochMilliseconds(value.ts, "ts");
|
|
485
|
+
optionalNonblank(value.operationId, "operationId");
|
|
486
|
+
if (value.stateRevision !== undefined) positiveInteger(value.stateRevision, "stateRevision");
|
|
487
|
+
if (value.truncation !== undefined) validateTruncation(value.truncation);
|
|
488
|
+
|
|
489
|
+
const idField = sink === "transcript" ? "transcriptId" : "auditLogId";
|
|
490
|
+
nonblank(value[idField], idField);
|
|
491
|
+
equalExpected(value.agentSessionId, expected.agentSessionId, "agentSessionId");
|
|
492
|
+
equalExpected(value[idField], expected.logId, idField);
|
|
493
|
+
equalExpected(value.generation, expected.generation, "generation");
|
|
494
|
+
return value;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
function validateRecognizedRecord(
|
|
498
|
+
record: Record<string, unknown>,
|
|
499
|
+
sink: "transcript" | "audit",
|
|
500
|
+
expected: SessionLogRecordExpectation,
|
|
501
|
+
): void {
|
|
502
|
+
switch (record.t) {
|
|
503
|
+
case "header":
|
|
504
|
+
validateHeader(record, sink);
|
|
505
|
+
return;
|
|
506
|
+
case "checkpoint":
|
|
507
|
+
if (sink !== "transcript") fail("checkpoint is invalid audit content");
|
|
508
|
+
validateCheckpointRecord(record, expected);
|
|
509
|
+
return;
|
|
510
|
+
case "operation":
|
|
511
|
+
validateOperation(record, sink);
|
|
512
|
+
return;
|
|
513
|
+
case "message":
|
|
514
|
+
if (sink !== "transcript") fail("message is invalid audit content");
|
|
515
|
+
requiredOperationId(record);
|
|
516
|
+
if (record.role !== "user" && record.role !== "assistant") fail("message.role is invalid");
|
|
517
|
+
if (typeof record.text !== "string") fail("message.text must be a string");
|
|
518
|
+
if (record.structured !== undefined) validateStructuredMessages(record.structured);
|
|
519
|
+
optionalString(record.status, "message.status");
|
|
520
|
+
optionalString(record.stopReason, "message.stopReason");
|
|
521
|
+
return;
|
|
522
|
+
case "reasoning":
|
|
523
|
+
requiredOperationId(record);
|
|
524
|
+
nonblank(record.reasoningId, "reasoningId");
|
|
525
|
+
positiveInteger(record.reasoningRevision, "reasoningRevision");
|
|
526
|
+
if (typeof record.text !== "string") fail("reasoning.text must be a string");
|
|
527
|
+
oneOf(record.status, ["in_progress", "completed"], "reasoning.status");
|
|
528
|
+
return;
|
|
529
|
+
case "activity":
|
|
530
|
+
requiredOperationId(record);
|
|
531
|
+
nonblank(record.activityId, "activityId");
|
|
532
|
+
positiveInteger(record.activityRevision, "activityRevision");
|
|
533
|
+
oneOf(record.reason, ["announced", "input", "settled", "flushed", "snapshot"], "activity.reason");
|
|
534
|
+
nonblank(record.kind, "activity.kind");
|
|
535
|
+
if (!isRecord(record.data)) fail("activity.data must be an object");
|
|
536
|
+
return;
|
|
537
|
+
case "configuration":
|
|
538
|
+
oneOf(record.event, ["announced", "selected", "restored", "restore_failed", "resolved"], "configuration.event");
|
|
539
|
+
optionalNonblank(record.configId, "configuration.configId");
|
|
540
|
+
optionalSha256(record.schemaDigest, "configuration.schemaDigest");
|
|
541
|
+
return;
|
|
542
|
+
case "permission":
|
|
543
|
+
nonblank(record.permissionId, "permissionId");
|
|
544
|
+
optionalNonblank(record.activityId, "activityId");
|
|
545
|
+
oneOf(record.event, ["requested", "resolved"], "permission.event");
|
|
546
|
+
optionalString(record.outcome, "permission.outcome");
|
|
547
|
+
return;
|
|
548
|
+
case "transition":
|
|
549
|
+
validateTransition(record, sink);
|
|
550
|
+
return;
|
|
551
|
+
case "metering_reference":
|
|
552
|
+
requiredOperationId(record);
|
|
553
|
+
nonblank(record.meteringRecordId, "meteringRecordId");
|
|
554
|
+
oneOf(record.status, ["available", "partial", "unavailable"], "metering_reference.status");
|
|
555
|
+
return;
|
|
556
|
+
case "lifecycle":
|
|
557
|
+
oneOf(record.event, ["session_opened", "stop_requested", "session_stopped", "transport_exited", "recovery_required", "recovery_resolved"], "lifecycle.event");
|
|
558
|
+
return;
|
|
559
|
+
case "protocol":
|
|
560
|
+
if (sink !== "transcript") fail("protocol is invalid audit content");
|
|
561
|
+
oneOf(record.event, ["initialized", "capabilities_updated"], "protocol.event");
|
|
562
|
+
validateProtocol(record.protocol, "protocol.protocol");
|
|
563
|
+
if (record.agent !== undefined) validateAgentIdentity(record.agent);
|
|
564
|
+
if (!isRecord(record.capabilities) || Object.values(record.capabilities).some((item) => typeof item !== "boolean")) {
|
|
565
|
+
fail("protocol.capabilities must contain only booleans");
|
|
566
|
+
}
|
|
567
|
+
if (!isLexicallySorted(Object.keys(record.capabilities))) {
|
|
568
|
+
fail("protocol.capabilities keys must be lexically sorted");
|
|
569
|
+
}
|
|
570
|
+
return;
|
|
571
|
+
case "raw":
|
|
572
|
+
if (sink !== "transcript") fail("raw is invalid audit content");
|
|
573
|
+
nonblank(record.protocol, "raw.protocol");
|
|
574
|
+
oneOf(record.direction, ["client_to_agent", "agent_to_client"], "raw.direction");
|
|
575
|
+
nonblank(record.event, "raw.event");
|
|
576
|
+
if (!("data" in record)) fail("raw.data is required");
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
fail("unreachable record type");
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function validateHeader(record: Record<string, unknown>, sink: "transcript" | "audit"): void {
|
|
583
|
+
oneOf(record.event, sink === "transcript" ? ["created", "upgraded", "replaced"] : ["created", "replaced"], "header.event");
|
|
584
|
+
iso(record.createdAt, "header.createdAt");
|
|
585
|
+
if (!isRecord(record.effectivePolicy)) fail("header.effectivePolicy must be an object");
|
|
586
|
+
if (!isRecord(record.effectiveLimits)) fail("header.effectiveLimits must be an object");
|
|
587
|
+
if (typeof record.includeTruncationDigests !== "boolean") fail("header.includeTruncationDigests must be boolean");
|
|
588
|
+
try {
|
|
589
|
+
validatePersistenceLimits(record.effectiveLimits as unknown as PersistenceLimits);
|
|
590
|
+
if (sink === "transcript") {
|
|
591
|
+
validateTranscriptCapturePolicy(record.effectivePolicy as unknown as TranscriptCapturePolicy);
|
|
592
|
+
} else {
|
|
593
|
+
validateAuditLogCapturePolicy(record.effectivePolicy as unknown as AuditLogCapturePolicy);
|
|
594
|
+
}
|
|
595
|
+
} catch (error) {
|
|
596
|
+
fail(`header policy or limits are invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
597
|
+
}
|
|
598
|
+
if (sink === "transcript") {
|
|
599
|
+
oneOf(record.preset, ["conversation", "activity", "detailed", "diagnostic"], "header.preset");
|
|
600
|
+
if (record.event === "upgraded") {
|
|
601
|
+
if (!isRecord(record.upgradedFrom) || record.upgradedFrom.version !== 1) fail("upgraded header requires upgradedFrom v1");
|
|
602
|
+
nonnegativeInteger(record.upgradedFrom.boundaryOffset, "header.upgradedFrom.boundaryOffset");
|
|
603
|
+
if (record.installation !== undefined) fail("upgraded header must not contain installation");
|
|
604
|
+
} else {
|
|
605
|
+
if (record.upgradedFrom !== undefined) fail("upgradedFrom is allowed only on upgraded headers");
|
|
606
|
+
validateInstallation(record.installation, "header.installation");
|
|
607
|
+
}
|
|
608
|
+
} else {
|
|
609
|
+
validateInstallation(record.installation, "header.installation");
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function validateCheckpointRecord(record: Record<string, unknown>, expected: SessionLogRecordExpectation): void {
|
|
614
|
+
positiveInteger(record.stateRevision, "checkpoint.stateRevision");
|
|
615
|
+
try {
|
|
616
|
+
validateAgentSessionCheckpoint(record.checkpoint, {
|
|
617
|
+
agentSessionId: record.agentSessionId as string,
|
|
618
|
+
transcriptId: record.transcriptId as string,
|
|
619
|
+
transcriptGeneration: record.generation as number,
|
|
620
|
+
...(expected.transcriptPath === undefined ? {} : { transcriptPath: expected.transcriptPath }),
|
|
621
|
+
});
|
|
622
|
+
} catch (error) {
|
|
623
|
+
fail(`checkpoint payload is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
624
|
+
}
|
|
625
|
+
const checkpoint = record.checkpoint as AgentSessionCheckpoint;
|
|
626
|
+
if (record.stateRevision !== checkpoint.stateRevision) fail("checkpoint record stateRevision mismatch");
|
|
627
|
+
if (checkpoint.transcript === null) fail("transcript checkpoint record requires transcript identity");
|
|
628
|
+
if (checkpoint.transcript.coveredSequence >= (record.seq as number)) {
|
|
629
|
+
fail("checkpoint coveredSequence must precede its physical record");
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
function validateOperation(record: Record<string, unknown>, sink: "transcript" | "audit"): void {
|
|
634
|
+
requiredOperationId(record);
|
|
635
|
+
oneOf(record.event, ["started", "settled", "failed", "cancelled", "interrupted", "aborted"], "operation.event");
|
|
636
|
+
oneOf(record.operationKind, ["prompt", "set_config", "internal_session_command", "handoff_summary", "handoff_seed", "switch_seed", "other"], "operation.operationKind");
|
|
637
|
+
optionalMachineCode(record.errorCode, "operation.errorCode");
|
|
638
|
+
if (record.event === "aborted") {
|
|
639
|
+
oneOf(record.abortReason, ["pre_dispatch_persistence_failure", "pre_dispatch_cancelled"], "operation.abortReason");
|
|
640
|
+
if (record.errorCode !== undefined) fail("aborted operation cannot contain errorCode");
|
|
641
|
+
} else if (record.abortReason !== undefined) {
|
|
642
|
+
fail("operation.abortReason is allowed only for aborted events");
|
|
643
|
+
}
|
|
644
|
+
if (sink === "transcript" && (record.event === "started" || record.event === "aborted")) {
|
|
645
|
+
positiveInteger(record.stateRevision, "operation.stateRevision");
|
|
646
|
+
epochMilliseconds(record.ts, "operation.ts");
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function validateTransition(record: Record<string, unknown>, sink: "transcript" | "audit"): void {
|
|
651
|
+
nonblank(record.transitionId, "transitionId");
|
|
652
|
+
oneOf(record.kind, ["handoff", "switch", "mode_change", "replacement"], "transition.kind");
|
|
653
|
+
oneOf(record.event, ["started", "target_created", "target_activated", "completed", "failed"], "transition.event");
|
|
654
|
+
if (record.from !== null && !isRecord(record.from)) fail("transition.from must be null or an object");
|
|
655
|
+
if (record.to !== undefined && !isRecord(record.to)) fail("transition.to must be an object");
|
|
656
|
+
if (isRecord(record.from)) validateSessionDescriptor(record.from, sink, "transition.from");
|
|
657
|
+
if (isRecord(record.to)) validateSessionDescriptor(record.to, sink, "transition.to");
|
|
658
|
+
optionalMachineCode(record.reasonCode, "transition.reasonCode");
|
|
659
|
+
optionalMachineCode(record.errorCode, "transition.errorCode");
|
|
660
|
+
if (record.event === "failed") {
|
|
661
|
+
oneOf(record.failedAfter, ["start", "target_created", "target_activated"], "transition.failedAfter");
|
|
662
|
+
} else if (record.failedAfter !== undefined) {
|
|
663
|
+
fail("transition.failedAfter is allowed only for failed events");
|
|
664
|
+
}
|
|
665
|
+
if (sink === "audit") {
|
|
666
|
+
for (const forbidden of ["sessionConfiguration", "historyEntry", "activationState", "summary", "reasonText"]) {
|
|
667
|
+
if (record[forbidden] !== undefined) fail(`${forbidden} is invalid audit transition content`);
|
|
668
|
+
}
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
if (record.event === "target_activated") {
|
|
672
|
+
if (!isRecord(record.to)) fail("target_activated transition requires to");
|
|
673
|
+
if (!isRecord(record.activationState)) fail("target_activated transition requires activationState");
|
|
674
|
+
positiveInteger(record.activationState.resultingStateRevision, "transition.activationState.resultingStateRevision");
|
|
675
|
+
iso(record.activationState.updatedAt, "transition.activationState.updatedAt");
|
|
676
|
+
iso(record.activationState.transitionAt, "transition.activationState.transitionAt");
|
|
677
|
+
validateBoundary(record.activationState.boundary, "transition.activationState.boundary");
|
|
678
|
+
if (isRecord(record.from) && record.from.internalSessionId !== null) {
|
|
679
|
+
validateHistoryEntry(record.historyEntry, record.from, record);
|
|
680
|
+
} else if (record.historyEntry !== undefined) {
|
|
681
|
+
fail("target_activated from a null internal session cannot contain historyEntry");
|
|
682
|
+
}
|
|
683
|
+
} else if (record.activationState !== undefined || record.historyEntry !== undefined) {
|
|
684
|
+
fail("activationState and historyEntry are allowed only on target_activated");
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
function validateHistoryEntry(
|
|
689
|
+
value: unknown,
|
|
690
|
+
from: Record<string, unknown>,
|
|
691
|
+
transition: Record<string, unknown>,
|
|
692
|
+
): void {
|
|
693
|
+
if (!isRecord(value)) fail("target_activated transition requires historyEntry for its prior internal session");
|
|
694
|
+
nonblank(value.internalSessionId, "transition.historyEntry.internalSessionId");
|
|
695
|
+
nonblank(value.adapterId, "transition.historyEntry.adapterId");
|
|
696
|
+
if (value.model !== null) nonblank(value.model, "transition.historyEntry.model");
|
|
697
|
+
validateProtocol(value.protocol, "transition.historyEntry.protocol");
|
|
698
|
+
iso(value.activatedAt, "transition.historyEntry.activatedAt");
|
|
699
|
+
iso(value.deactivatedAt, "transition.historyEntry.deactivatedAt");
|
|
700
|
+
if (!isRecord(value.transition)) fail("transition.historyEntry.transition must be an object");
|
|
701
|
+
if (value.transition.kind !== transition.kind || value.transition.transitionId !== transition.transitionId) {
|
|
702
|
+
fail("transition.historyEntry transition identity mismatch");
|
|
703
|
+
}
|
|
704
|
+
optionalMachineCode(value.transition.reasonCode, "transition.historyEntry.transition.reasonCode");
|
|
705
|
+
try {
|
|
706
|
+
validateSessionConfigurationCheckpoint(value.sessionConfiguration, value.adapterId as string);
|
|
707
|
+
} catch (error) {
|
|
708
|
+
fail(`transition.historyEntry.sessionConfiguration is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
709
|
+
}
|
|
710
|
+
const comparableEntry = {
|
|
711
|
+
internalSessionId: value.internalSessionId,
|
|
712
|
+
adapterId: value.adapterId,
|
|
713
|
+
model: value.model,
|
|
714
|
+
sessionConfiguration: value.sessionConfiguration,
|
|
715
|
+
protocol: value.protocol,
|
|
716
|
+
activatedAt: value.activatedAt,
|
|
717
|
+
};
|
|
718
|
+
const comparableFrom = {
|
|
719
|
+
internalSessionId: from.internalSessionId,
|
|
720
|
+
adapterId: from.adapterId,
|
|
721
|
+
model: from.model,
|
|
722
|
+
sessionConfiguration: from.sessionConfiguration,
|
|
723
|
+
protocol: from.protocol,
|
|
724
|
+
activatedAt: from.activatedAt,
|
|
725
|
+
};
|
|
726
|
+
if (canonicalizeJson(comparableEntry) !== canonicalizeJson(comparableFrom)) {
|
|
727
|
+
fail("transition.historyEntry does not match from descriptor");
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
function validateBoundary(value: unknown, label: string): void {
|
|
732
|
+
if (!isRecord(value)) fail(`${label} must be an object`);
|
|
733
|
+
if (value.lastSettledOperationId !== null) nonblank(value.lastSettledOperationId, `${label}.lastSettledOperationId`);
|
|
734
|
+
if (value.status === "settled") return;
|
|
735
|
+
if (value.status !== "in_flight") fail(`${label}.status is invalid`);
|
|
736
|
+
nonblank(value.interruptedOperationId, `${label}.interruptedOperationId`);
|
|
737
|
+
oneOf(value.cause, ["operation_started", "transport_error", "harness_exit", "host_interrupt", "restored_after_unclean_exit"], `${label}.cause`);
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
function validateSessionDescriptor(
|
|
741
|
+
value: Record<string, unknown>,
|
|
742
|
+
sink: "transcript" | "audit",
|
|
743
|
+
label: string,
|
|
744
|
+
): void {
|
|
745
|
+
if (value.internalSessionId !== null) nonblank(value.internalSessionId, `${label}.internalSessionId`);
|
|
746
|
+
nonblank(value.adapterId, `${label}.adapterId`);
|
|
747
|
+
oneOf(value.mode, ["normal", "degraded"], `${label}.mode`);
|
|
748
|
+
if (value.model !== null) nonblank(value.model, `${label}.model`);
|
|
749
|
+
validateProtocol(value.protocol, `${label}.protocol`);
|
|
750
|
+
if (sink === "audit") {
|
|
751
|
+
if (value.sessionConfiguration !== undefined) fail("sessionConfiguration is invalid audit transition content");
|
|
752
|
+
if (value.activatedAt !== undefined) fail("activatedAt is invalid audit transition content");
|
|
753
|
+
} else {
|
|
754
|
+
try {
|
|
755
|
+
validateSessionConfigurationCheckpoint(value.sessionConfiguration, value.adapterId as string);
|
|
756
|
+
} catch (error) {
|
|
757
|
+
fail(`${label}.sessionConfiguration is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
758
|
+
}
|
|
759
|
+
if (value.activatedAt !== null) iso(value.activatedAt, `${label}.activatedAt`);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function validateAgentIdentity(value: unknown): void {
|
|
764
|
+
if (!isRecord(value)) fail("protocol.agent must be an object");
|
|
765
|
+
optionalNonblank(value.name, "protocol.agent.name");
|
|
766
|
+
optionalNonblank(value.title, "protocol.agent.title");
|
|
767
|
+
optionalNonblank(value.version, "protocol.agent.version");
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function validateStructuredMessages(value: unknown): void {
|
|
771
|
+
if (!isRecord(value) || value.version !== 1 || !Array.isArray(value.messages)) {
|
|
772
|
+
fail("message.structured must be a version-1 message collection");
|
|
773
|
+
}
|
|
774
|
+
for (const [index, message] of value.messages.entries()) {
|
|
775
|
+
if (!isRecord(message) || typeof message.text !== "string") {
|
|
776
|
+
fail(`message.structured.messages[${index}] is invalid`);
|
|
777
|
+
}
|
|
778
|
+
optionalNonblank(message.messageId, `message.structured.messages[${index}].messageId`);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
function validateInstallation(value: unknown, label: string): void {
|
|
783
|
+
if (!isRecord(value)) fail(`${label} must be an object`);
|
|
784
|
+
oneOf(value.kind, ["creation", "persistence_reconfiguration", "restoration"], `${label}.kind`);
|
|
785
|
+
nonblank(value.operationId, `${label}.operationId`);
|
|
786
|
+
sha256(value.requestDigest, `${label}.requestDigest`);
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
function validateTruncation(value: unknown): void {
|
|
790
|
+
if (!Array.isArray(value)) fail("truncation must be an array");
|
|
791
|
+
for (const [index, item] of value.entries()) {
|
|
792
|
+
if (!isRecord(item)) fail(`truncation[${index}] must be an object`);
|
|
793
|
+
nonblank(item.field, `truncation[${index}].field`);
|
|
794
|
+
oneOf(item.reason, ["field_limit", "record_limit"], `truncation[${index}].reason`);
|
|
795
|
+
nonnegativeInteger(item.originalBytes, `truncation[${index}].originalBytes`);
|
|
796
|
+
optionalSha256(item.originalSha256, `truncation[${index}].originalSha256`);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function validateProtocol(value: unknown, label: string): void {
|
|
801
|
+
if (!isRecord(value)) fail(`${label} must be an object`);
|
|
802
|
+
if (value.name === "pty") return;
|
|
803
|
+
if (value.name !== "acp") fail(`${label}.name is invalid`);
|
|
804
|
+
positiveInteger(value.version, `${label}.version`);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function requiredOperationId(record: Record<string, unknown>): void {
|
|
808
|
+
nonblank(record.operationId, "operationId");
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
function optionalMachineCode(value: unknown, label: string): void {
|
|
812
|
+
if (value !== undefined && !isPersistedMachineCode(value)) fail(`${label} is invalid`);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function optionalSha256(value: unknown, label: string): void {
|
|
816
|
+
if (value !== undefined) sha256(value, label);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function sha256(value: unknown, label: string): void {
|
|
820
|
+
if (typeof value !== "string" || !/^[0-9a-f]{64}$/.test(value)) fail(`${label} must be lowercase SHA-256`);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
function nonblank(value: unknown, label: string): asserts value is string {
|
|
824
|
+
if (typeof value !== "string" || !value.trim()) fail(`${label} must be nonblank`);
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
function optionalNonblank(value: unknown, label: string): void {
|
|
828
|
+
if (value !== undefined) nonblank(value, label);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
function optionalString(value: unknown, label: string): void {
|
|
832
|
+
if (value !== undefined && typeof value !== "string") fail(`${label} must be a string`);
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
function positiveInteger(value: unknown, label: string): void {
|
|
836
|
+
if (!Number.isSafeInteger(value) || (value as number) < 1) fail(`${label} must be a positive safe integer`);
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function nonnegativeInteger(value: unknown, label: string): void {
|
|
840
|
+
if (!Number.isSafeInteger(value) || (value as number) < 0) fail(`${label} must be a nonnegative safe integer`);
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function epochMilliseconds(value: unknown, label: string): void {
|
|
844
|
+
if (!Number.isSafeInteger(value) || !Number.isFinite(new Date(value as number).valueOf())) {
|
|
845
|
+
fail(`${label} must be safe Unix epoch milliseconds in the ECMAScript date range`);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
function iso(value: unknown, label: string): void {
|
|
850
|
+
if (typeof value !== "string" || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString() !== value) {
|
|
851
|
+
fail(`${label} must be canonical ISO-8601`);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
function oneOf(value: unknown, choices: readonly string[], label: string): void {
|
|
856
|
+
if (typeof value !== "string" || !choices.includes(value)) fail(`${label} is invalid`);
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
function isLexicallySorted(values: readonly string[]): boolean {
|
|
860
|
+
return values.every((value, index) => index === 0 || values[index - 1]!.localeCompare(value) <= 0);
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
function equalExpected(actual: unknown, expected: unknown, label: string): void {
|
|
864
|
+
if (expected !== undefined && actual !== expected) fail(`${label} mismatch`);
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
868
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function fail(message: string): never {
|
|
872
|
+
throw new SessionLogRecordValidationError(message);
|
|
873
|
+
}
|