@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,446 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import type {
|
|
3
|
+
AuditLogCapturePolicy,
|
|
4
|
+
PersistenceLimits,
|
|
5
|
+
TranscriptCapturePolicy,
|
|
6
|
+
TranscriptPreset,
|
|
7
|
+
} from "./config.ts";
|
|
8
|
+
import {
|
|
9
|
+
validateAuditLogCapturePolicy,
|
|
10
|
+
validatePersistenceLimits,
|
|
11
|
+
validateTranscriptCapturePolicy,
|
|
12
|
+
} from "./config.ts";
|
|
13
|
+
import {
|
|
14
|
+
validateAgentSessionCheckpoint,
|
|
15
|
+
validateSessionConfigurationCheckpoint,
|
|
16
|
+
type AgentSessionCheckpoint,
|
|
17
|
+
type JsonValue,
|
|
18
|
+
type PersistedProtocol,
|
|
19
|
+
type SessionConfigurationCheckpoint,
|
|
20
|
+
} from "./checkpoint.ts";
|
|
21
|
+
import { canonicalizeJson, sha256CanonicalJson } from "./integrity.ts";
|
|
22
|
+
import type { InternalSessionHistoryEntry } from "./records.ts";
|
|
23
|
+
|
|
24
|
+
export interface StagedContinuationState {
|
|
25
|
+
readonly adapterId: string;
|
|
26
|
+
readonly mode: "normal" | "degraded";
|
|
27
|
+
readonly internalSessionId: string | null;
|
|
28
|
+
readonly internalSessionActivatedAt: string | null;
|
|
29
|
+
readonly model: string | null;
|
|
30
|
+
readonly sessionConfiguration: SessionConfigurationCheckpoint;
|
|
31
|
+
readonly protocol: PersistedProtocol;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface MaintenanceBase {
|
|
35
|
+
readonly incarnationId: string;
|
|
36
|
+
readonly baseRecordRevision: number | null;
|
|
37
|
+
readonly operationId: string;
|
|
38
|
+
readonly requestDigest: string;
|
|
39
|
+
readonly status: "prepared";
|
|
40
|
+
readonly startedAt: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CreationMaintenance extends MaintenanceBase {
|
|
44
|
+
readonly kind: "creation";
|
|
45
|
+
readonly baseRecordRevision: null;
|
|
46
|
+
readonly details: {
|
|
47
|
+
readonly transcriptPath: string | null;
|
|
48
|
+
readonly auditLogPath: string | null;
|
|
49
|
+
readonly stagedContinuation?: StagedContinuationState;
|
|
50
|
+
readonly stagedRecord?: AgentSessionRecord;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface RestorationMaintenance extends MaintenanceBase {
|
|
55
|
+
readonly kind: "restoration";
|
|
56
|
+
readonly baseRecordRevision: number;
|
|
57
|
+
readonly details: {
|
|
58
|
+
readonly transcript:
|
|
59
|
+
| { readonly action: "retain"; readonly path: string }
|
|
60
|
+
| { readonly action: "disable" }
|
|
61
|
+
| { readonly action: "replace"; readonly path: string };
|
|
62
|
+
readonly auditLog:
|
|
63
|
+
| { readonly action: "retain"; readonly path: string }
|
|
64
|
+
| { readonly action: "disable" }
|
|
65
|
+
| { readonly action: "replace"; readonly path: string };
|
|
66
|
+
readonly stagedContinuation?: StagedContinuationState;
|
|
67
|
+
readonly stagedRecord?: AgentSessionRecord;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface PersistenceReconfigurationMaintenance extends MaintenanceBase {
|
|
72
|
+
readonly kind: "persistence_reconfiguration";
|
|
73
|
+
readonly baseRecordRevision: number;
|
|
74
|
+
readonly details: {
|
|
75
|
+
readonly transcript:
|
|
76
|
+
| null
|
|
77
|
+
| { readonly action: "disable"; readonly previousPath: string }
|
|
78
|
+
| { readonly action: "replace"; readonly previousPath: string; readonly replacementPath: string };
|
|
79
|
+
readonly auditLog:
|
|
80
|
+
| null
|
|
81
|
+
| { readonly action: "disable"; readonly previousPath: string }
|
|
82
|
+
| { readonly action: "replace"; readonly previousPath: string; readonly replacementPath: string };
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface DeletionMaintenance extends MaintenanceBase {
|
|
87
|
+
readonly kind: "deletion";
|
|
88
|
+
readonly baseRecordRevision: number;
|
|
89
|
+
readonly details: {
|
|
90
|
+
readonly deleteTranscript: boolean;
|
|
91
|
+
readonly transcriptPath: string | null;
|
|
92
|
+
readonly deleteAuditLog: boolean;
|
|
93
|
+
readonly auditLogPath: string | null;
|
|
94
|
+
readonly removeRegistryEntry: boolean;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export type SessionMaintenance =
|
|
99
|
+
| CreationMaintenance
|
|
100
|
+
| RestorationMaintenance
|
|
101
|
+
| PersistenceReconfigurationMaintenance
|
|
102
|
+
| DeletionMaintenance;
|
|
103
|
+
|
|
104
|
+
export interface MaintenanceCompletionReceipt {
|
|
105
|
+
readonly agentSessionId: string;
|
|
106
|
+
readonly incarnationId: string;
|
|
107
|
+
readonly kind: SessionMaintenance["kind"];
|
|
108
|
+
readonly operationId: string;
|
|
109
|
+
readonly requestDigest: string;
|
|
110
|
+
readonly outcome: "completed";
|
|
111
|
+
readonly completedAt: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type PrepareMaintenanceResult =
|
|
115
|
+
| { readonly status: "prepared"; readonly maintenance: SessionMaintenance }
|
|
116
|
+
| { readonly status: "already_prepared"; readonly maintenance: SessionMaintenance }
|
|
117
|
+
| { readonly status: "already_completed"; readonly receipt: MaintenanceCompletionReceipt };
|
|
118
|
+
|
|
119
|
+
export interface AgentSessionRecord {
|
|
120
|
+
readonly agentSessionId: string;
|
|
121
|
+
readonly incarnationId: string;
|
|
122
|
+
readonly recordRevision: number;
|
|
123
|
+
readonly checkpoint: AgentSessionCheckpoint;
|
|
124
|
+
readonly adapterId: string;
|
|
125
|
+
readonly mode: "normal" | "degraded";
|
|
126
|
+
readonly internalSessionId: string | null;
|
|
127
|
+
readonly internalSessionActivatedAt: string | null;
|
|
128
|
+
readonly internalSessionHistory: readonly InternalSessionHistoryEntry[];
|
|
129
|
+
readonly cwd?: string;
|
|
130
|
+
readonly title?: string;
|
|
131
|
+
readonly model: string | null;
|
|
132
|
+
readonly sessionConfiguration: SessionConfigurationCheckpoint;
|
|
133
|
+
/** Immutable persistence policy needed to keep filtering restoration targets after reopen. */
|
|
134
|
+
readonly sessionConfigurationPersistence?: {
|
|
135
|
+
readonly values: "none" | "typed" | "all";
|
|
136
|
+
readonly include: readonly string[];
|
|
137
|
+
readonly exclude: readonly string[];
|
|
138
|
+
};
|
|
139
|
+
readonly protocol: PersistedProtocol;
|
|
140
|
+
readonly agent?: { readonly name?: string; readonly title?: string; readonly version?: string };
|
|
141
|
+
readonly transcript: null | {
|
|
142
|
+
readonly path: string;
|
|
143
|
+
readonly transcriptId: string;
|
|
144
|
+
readonly generation: number;
|
|
145
|
+
readonly preset: TranscriptPreset;
|
|
146
|
+
readonly effectivePolicy: TranscriptCapturePolicy;
|
|
147
|
+
readonly effectiveLimits: PersistenceLimits;
|
|
148
|
+
readonly includeTruncationDigests: boolean;
|
|
149
|
+
};
|
|
150
|
+
readonly auditLog: null | {
|
|
151
|
+
readonly path: string;
|
|
152
|
+
readonly auditLogId: string;
|
|
153
|
+
readonly generation: number;
|
|
154
|
+
readonly effectivePolicy: AuditLogCapturePolicy;
|
|
155
|
+
readonly effectiveLimits: PersistenceLimits;
|
|
156
|
+
readonly includeTruncationDigests: boolean;
|
|
157
|
+
};
|
|
158
|
+
readonly metadata?: Readonly<Record<string, JsonValue>>;
|
|
159
|
+
readonly deletion?: {
|
|
160
|
+
readonly deletedAt: string;
|
|
161
|
+
readonly transcriptDeletedAt?: string;
|
|
162
|
+
readonly auditLogDeletedAt?: string;
|
|
163
|
+
};
|
|
164
|
+
readonly createdAt: string;
|
|
165
|
+
readonly updatedAt: string;
|
|
166
|
+
readonly lastOpenedAt: string;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface AgentSessionsRegistryFile {
|
|
170
|
+
readonly version: 1;
|
|
171
|
+
readonly revision: number;
|
|
172
|
+
readonly updatedAt: string;
|
|
173
|
+
readonly sessions: Readonly<Record<string, AgentSessionRecord>>;
|
|
174
|
+
readonly maintenance: Readonly<Record<string, SessionMaintenance>>;
|
|
175
|
+
readonly maintenanceReceipts: Readonly<Record<string, readonly MaintenanceCompletionReceipt[]>>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export type AgentSessionListCursor = string & { readonly __agentSessionListCursor: unique symbol };
|
|
179
|
+
export type SessionMaintenanceListCursor = string & { readonly __sessionMaintenanceListCursor: unique symbol };
|
|
180
|
+
|
|
181
|
+
export interface ListAgentSessionsOptions {
|
|
182
|
+
readonly cursor?: AgentSessionListCursor;
|
|
183
|
+
readonly limit?: number;
|
|
184
|
+
readonly includeDeleted?: boolean;
|
|
185
|
+
readonly order?: "updated_desc" | "updated_asc" | "created_desc" | "created_asc";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface AgentSessionListPage {
|
|
189
|
+
readonly sessions: readonly AgentSessionSummary[];
|
|
190
|
+
readonly nextCursor: AgentSessionListCursor | null;
|
|
191
|
+
readonly hasMore: boolean;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface ListSessionMaintenanceOptions {
|
|
195
|
+
readonly cursor?: SessionMaintenanceListCursor;
|
|
196
|
+
readonly limit?: number;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export interface SessionMaintenanceSummary {
|
|
200
|
+
readonly agentSessionId: string;
|
|
201
|
+
readonly incarnationId: string;
|
|
202
|
+
readonly kind: SessionMaintenance["kind"];
|
|
203
|
+
readonly operationId: string;
|
|
204
|
+
readonly startedAt: string;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface SessionMaintenancePage {
|
|
208
|
+
readonly maintenance: readonly SessionMaintenanceSummary[];
|
|
209
|
+
readonly nextCursor: SessionMaintenanceListCursor | null;
|
|
210
|
+
readonly hasMore: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface AgentSessionSummary {
|
|
214
|
+
readonly agentSessionId: string;
|
|
215
|
+
readonly incarnationId: string;
|
|
216
|
+
readonly recordRevision: number;
|
|
217
|
+
readonly adapterId: string;
|
|
218
|
+
readonly mode: "normal" | "degraded";
|
|
219
|
+
readonly model: string | null;
|
|
220
|
+
readonly title?: string;
|
|
221
|
+
readonly cwd?: string;
|
|
222
|
+
readonly createdAt: string;
|
|
223
|
+
readonly updatedAt: string;
|
|
224
|
+
readonly recovery: "clean" | "required";
|
|
225
|
+
readonly transcriptAvailable: boolean;
|
|
226
|
+
readonly auditLogAvailable: boolean;
|
|
227
|
+
readonly lifecycle: "active" | "maintenance_pending" | "deleted";
|
|
228
|
+
readonly maintenance?: {
|
|
229
|
+
readonly kind: SessionMaintenance["kind"];
|
|
230
|
+
readonly operationId: string;
|
|
231
|
+
readonly startedAt: string;
|
|
232
|
+
};
|
|
233
|
+
readonly deletedAt?: string;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export interface AgentSessionStore {
|
|
237
|
+
getAgentSessionRecord(agentSessionId: string): Promise<AgentSessionRecord | null>;
|
|
238
|
+
getMaintenance(agentSessionId: string): Promise<SessionMaintenance | null>;
|
|
239
|
+
listMaintenance(options?: ListSessionMaintenanceOptions): Promise<SessionMaintenancePage>;
|
|
240
|
+
saveAgentSessionRecord(
|
|
241
|
+
record: AgentSessionRecord,
|
|
242
|
+
options:
|
|
243
|
+
| { readonly expectedRecordRevision: null; readonly expectedIncarnationId: null }
|
|
244
|
+
| { readonly expectedRecordRevision: number; readonly expectedIncarnationId: string },
|
|
245
|
+
): Promise<AgentSessionRecord>;
|
|
246
|
+
deleteAgentSessionRecord(
|
|
247
|
+
agentSessionId: string,
|
|
248
|
+
options: { readonly expectedRecordRevision: number; readonly expectedIncarnationId: string },
|
|
249
|
+
): Promise<void>;
|
|
250
|
+
listAgentSessions(options?: ListAgentSessionsOptions): Promise<AgentSessionListPage>;
|
|
251
|
+
prepareMaintenance(
|
|
252
|
+
agentSessionId: string,
|
|
253
|
+
maintenance: SessionMaintenance,
|
|
254
|
+
options: { readonly expectedRecordRevision: number | null },
|
|
255
|
+
): Promise<PrepareMaintenanceResult>;
|
|
256
|
+
updateMaintenance(
|
|
257
|
+
agentSessionId: string,
|
|
258
|
+
expectedIncarnationId: string,
|
|
259
|
+
operationId: string,
|
|
260
|
+
update: SessionMaintenance,
|
|
261
|
+
): Promise<void>;
|
|
262
|
+
finalizeMaintenance(
|
|
263
|
+
agentSessionId: string,
|
|
264
|
+
expectedIncarnationId: string,
|
|
265
|
+
operationId: string,
|
|
266
|
+
requestDigest: string,
|
|
267
|
+
mutation:
|
|
268
|
+
| { readonly save: AgentSessionRecord; readonly deleteRecord?: never }
|
|
269
|
+
| { readonly save?: never; readonly deleteRecord: true },
|
|
270
|
+
): Promise<void>;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export class AgentSessionRecordValidationError extends Error {
|
|
274
|
+
constructor(message: string) {
|
|
275
|
+
super(message);
|
|
276
|
+
this.name = "AgentSessionRecordValidationError";
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export class AgentSessionStoreConflictError extends Error {
|
|
281
|
+
constructor(message: string) {
|
|
282
|
+
super(message);
|
|
283
|
+
this.name = "AgentSessionStoreConflictError";
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export class SessionMaintenanceInProgressError extends Error {
|
|
288
|
+
constructor(readonly agentSessionId: string) {
|
|
289
|
+
super(`session ${agentSessionId} has a prepared maintenance operation`);
|
|
290
|
+
this.name = "SessionMaintenanceInProgressError";
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** A non-void maintenance retry found a durable completion receipt. */
|
|
295
|
+
export class MaintenanceAlreadyCompletedError extends Error {
|
|
296
|
+
constructor(readonly receipt: MaintenanceCompletionReceipt) {
|
|
297
|
+
super(`managed ${receipt.kind} ${receipt.operationId} already completed; load or inspect the persisted session`);
|
|
298
|
+
this.name = "MaintenanceAlreadyCompletedError";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function internalSessionHistoryDigest(
|
|
303
|
+
entries: readonly InternalSessionHistoryEntry[],
|
|
304
|
+
): string {
|
|
305
|
+
return sha256CanonicalJson(entries);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function summarizeAgentSessionRecord(
|
|
309
|
+
record: AgentSessionRecord,
|
|
310
|
+
maintenance?: SessionMaintenance,
|
|
311
|
+
): AgentSessionSummary {
|
|
312
|
+
const lifecycle = maintenance ? "maintenance_pending" : record.deletion ? "deleted" : "active";
|
|
313
|
+
return {
|
|
314
|
+
agentSessionId: record.agentSessionId,
|
|
315
|
+
incarnationId: record.incarnationId,
|
|
316
|
+
recordRevision: record.recordRevision,
|
|
317
|
+
adapterId: record.adapterId,
|
|
318
|
+
mode: record.mode,
|
|
319
|
+
model: record.model,
|
|
320
|
+
...(record.title === undefined ? {} : { title: record.title }),
|
|
321
|
+
...(record.cwd === undefined ? {} : { cwd: record.cwd }),
|
|
322
|
+
createdAt: record.createdAt,
|
|
323
|
+
updatedAt: record.updatedAt,
|
|
324
|
+
recovery: record.checkpoint.boundary.status === "settled" ? "clean" : "required",
|
|
325
|
+
transcriptAvailable: record.transcript !== null && record.deletion?.transcriptDeletedAt === undefined,
|
|
326
|
+
auditLogAvailable: record.auditLog !== null && record.deletion?.auditLogDeletedAt === undefined,
|
|
327
|
+
lifecycle,
|
|
328
|
+
...(maintenance ? { maintenance: { kind: maintenance.kind, operationId: maintenance.operationId, startedAt: maintenance.startedAt } } : {}),
|
|
329
|
+
...(record.deletion ? { deletedAt: record.deletion.deletedAt } : {}),
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function validateAgentSessionRecord(value: unknown): asserts value is AgentSessionRecord {
|
|
334
|
+
if (!isRecord(value)) fail("agent-session record must be an object");
|
|
335
|
+
try {
|
|
336
|
+
canonicalizeJson(value);
|
|
337
|
+
validateAgentSessionCheckpoint(value.checkpoint, {
|
|
338
|
+
agentSessionId: requiredString(value.agentSessionId, "agentSessionId"),
|
|
339
|
+
incarnationId: requiredString(value.incarnationId, "incarnationId"),
|
|
340
|
+
adapterId: requiredString(value.adapterId, "adapterId"),
|
|
341
|
+
});
|
|
342
|
+
} catch (error) {
|
|
343
|
+
fail(`record or checkpoint is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
344
|
+
}
|
|
345
|
+
positiveInteger(value.recordRevision, "recordRevision");
|
|
346
|
+
oneOf(value.mode, ["normal", "degraded"], "mode");
|
|
347
|
+
if (!Array.isArray(value.internalSessionHistory)) fail("internalSessionHistory must be an array");
|
|
348
|
+
const checkpoint = value.checkpoint as AgentSessionCheckpoint;
|
|
349
|
+
for (const field of ["adapterId", "mode", "internalSessionId", "internalSessionActivatedAt", "model", "protocol", "sessionConfiguration"] as const) {
|
|
350
|
+
if (canonicalizeJson(value[field]) !== canonicalizeJson(checkpoint[field])) {
|
|
351
|
+
fail(`${field} projection disagrees with checkpoint`);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (value.internalSessionHistory.length !== checkpoint.internalSessionHistoryRevision) {
|
|
355
|
+
fail("internalSessionHistory revision disagrees with checkpoint");
|
|
356
|
+
}
|
|
357
|
+
if (internalSessionHistoryDigest(value.internalSessionHistory as InternalSessionHistoryEntry[]) !== checkpoint.internalSessionHistoryDigest) {
|
|
358
|
+
fail("internalSessionHistory digest disagrees with checkpoint");
|
|
359
|
+
}
|
|
360
|
+
try {
|
|
361
|
+
validateSessionConfigurationCheckpoint(value.sessionConfiguration, value.adapterId as string);
|
|
362
|
+
} catch (error) {
|
|
363
|
+
fail(`sessionConfiguration is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
364
|
+
}
|
|
365
|
+
validateLogMetadata(value.transcript, "transcript", checkpoint);
|
|
366
|
+
validateLogMetadata(value.auditLog, "auditLog", checkpoint);
|
|
367
|
+
optionalString(value.cwd, "cwd");
|
|
368
|
+
optionalString(value.title, "title");
|
|
369
|
+
iso(value.createdAt, "createdAt");
|
|
370
|
+
iso(value.updatedAt, "updatedAt");
|
|
371
|
+
iso(value.lastOpenedAt, "lastOpenedAt");
|
|
372
|
+
if (Date.parse(value.createdAt as string) > Date.parse(value.updatedAt as string)) fail("createdAt cannot be after updatedAt");
|
|
373
|
+
if (value.deletion !== undefined) validateDeletion(value.deletion);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function validateLogMetadata(
|
|
377
|
+
value: unknown,
|
|
378
|
+
kind: "transcript" | "auditLog",
|
|
379
|
+
checkpoint: AgentSessionCheckpoint,
|
|
380
|
+
): void {
|
|
381
|
+
if (value === null) {
|
|
382
|
+
if (kind === "transcript" && checkpoint.transcript !== null) fail("transcript projection disagrees with checkpoint");
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
if (!isRecord(value)) fail(`${kind} must be null or an object`);
|
|
386
|
+
if (typeof value.path !== "string" || !isAbsolute(value.path)) fail(`${kind}.path must be absolute`);
|
|
387
|
+
positiveInteger(value.generation, `${kind}.generation`);
|
|
388
|
+
if (!isRecord(value.effectivePolicy) || !isRecord(value.effectiveLimits)) fail(`${kind} policy and limits must be objects`);
|
|
389
|
+
if (typeof value.includeTruncationDigests !== "boolean") fail(`${kind}.includeTruncationDigests must be boolean`);
|
|
390
|
+
try {
|
|
391
|
+
validatePersistenceLimits(value.effectiveLimits as unknown as PersistenceLimits);
|
|
392
|
+
if (kind === "transcript") {
|
|
393
|
+
requiredString(value.transcriptId, "transcript.transcriptId");
|
|
394
|
+
oneOf(value.preset, ["conversation", "activity", "detailed", "diagnostic"], "transcript.preset");
|
|
395
|
+
validateTranscriptCapturePolicy(value.effectivePolicy as unknown as TranscriptCapturePolicy);
|
|
396
|
+
if (checkpoint.transcript === null) fail("transcript projection disagrees with checkpoint");
|
|
397
|
+
if (value.path !== checkpoint.transcript.path || value.transcriptId !== checkpoint.transcript.transcriptId || value.generation !== checkpoint.transcript.generation) {
|
|
398
|
+
fail("transcript identity projection disagrees with checkpoint");
|
|
399
|
+
}
|
|
400
|
+
} else {
|
|
401
|
+
requiredString(value.auditLogId, "auditLog.auditLogId");
|
|
402
|
+
validateAuditLogCapturePolicy(value.effectivePolicy as unknown as AuditLogCapturePolicy);
|
|
403
|
+
}
|
|
404
|
+
} catch (error) {
|
|
405
|
+
if (error instanceof AgentSessionRecordValidationError) throw error;
|
|
406
|
+
fail(`${kind} metadata is invalid: ${error instanceof Error ? error.message : String(error)}`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function validateDeletion(value: unknown): void {
|
|
411
|
+
if (!isRecord(value)) fail("deletion must be an object");
|
|
412
|
+
iso(value.deletedAt, "deletion.deletedAt");
|
|
413
|
+
if (value.transcriptDeletedAt !== undefined) iso(value.transcriptDeletedAt, "deletion.transcriptDeletedAt");
|
|
414
|
+
if (value.auditLogDeletedAt !== undefined) iso(value.auditLogDeletedAt, "deletion.auditLogDeletedAt");
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function requiredString(value: unknown, label: string): string {
|
|
418
|
+
if (typeof value !== "string" || !value.trim()) fail(`${label} must be nonblank`);
|
|
419
|
+
return value;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function optionalString(value: unknown, label: string): void {
|
|
423
|
+
if (value !== undefined && typeof value !== "string") fail(`${label} must be a string`);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function positiveInteger(value: unknown, label: string): void {
|
|
427
|
+
if (!Number.isSafeInteger(value) || (value as number) < 1) fail(`${label} must be a positive safe integer`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function iso(value: unknown, label: string): void {
|
|
431
|
+
if (typeof value !== "string" || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString() !== value) {
|
|
432
|
+
fail(`${label} must be canonical ISO-8601`);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
function oneOf(value: unknown, allowed: readonly string[], label: string): void {
|
|
437
|
+
if (typeof value !== "string" || !allowed.includes(value)) fail(`${label} is invalid`);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
441
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function fail(message: string): never {
|
|
445
|
+
throw new AgentSessionRecordValidationError(message);
|
|
446
|
+
}
|