@qlairoslabs/acp-client 0.6.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.
Files changed (40) hide show
  1. package/CONSUMER_GUIDE.md +579 -292
  2. package/README.md +25 -0
  3. package/package.json +1 -1
  4. package/src/agent-session.ts +2188 -96
  5. package/src/cli/index.ts +33 -5
  6. package/src/cli/managed.ts +220 -0
  7. package/src/controller.ts +13 -0
  8. package/src/index.ts +228 -3
  9. package/src/metering/engine.ts +9 -1
  10. package/src/metering/record.ts +2 -1
  11. package/src/persistence/activity-projection.ts +71 -0
  12. package/src/persistence/audit-log-reader.ts +506 -0
  13. package/src/persistence/audit-runtime.ts +162 -0
  14. package/src/persistence/checkpoint.ts +304 -0
  15. package/src/persistence/config.ts +538 -0
  16. package/src/persistence/configuration-state.ts +191 -0
  17. package/src/persistence/deletion.ts +227 -0
  18. package/src/persistence/file-registry-store.ts +740 -0
  19. package/src/persistence/integrity.ts +147 -0
  20. package/src/persistence/legacy-registry-import.ts +240 -0
  21. package/src/persistence/managed-checkpoint.ts +81 -0
  22. package/src/persistence/managed-creation.ts +456 -0
  23. package/src/persistence/managed-load.ts +172 -0
  24. package/src/persistence/managed-registry.ts +57 -0
  25. package/src/persistence/migration.ts +298 -0
  26. package/src/persistence/paths.ts +172 -0
  27. package/src/persistence/projection.ts +373 -0
  28. package/src/persistence/reconfiguration.ts +453 -0
  29. package/src/persistence/records.ts +873 -0
  30. package/src/persistence/reducer.ts +498 -0
  31. package/src/persistence/registry.ts +446 -0
  32. package/src/persistence/restoration.ts +689 -0
  33. package/src/persistence/session-log-writer.ts +480 -0
  34. package/src/persistence/state-runtime.ts +176 -0
  35. package/src/persistence/transcript-reader.ts +980 -0
  36. package/src/persistence/transcript-runtime.ts +667 -0
  37. package/src/persistence/transcript-scanner.ts +385 -0
  38. package/src/persistence/truncation.ts +276 -0
  39. package/src/session-manager.ts +257 -7
  40. package/src/transcript.ts +3 -3
@@ -0,0 +1,373 @@
1
+ import type {
2
+ AuditLogCapturePolicy,
3
+ TranscriptCapturePolicy,
4
+ } from "./config.ts";
5
+ import type {
6
+ AgentSessionCheckpoint,
7
+ JsonObject,
8
+ JsonValue,
9
+ PersistedProtocol,
10
+ } from "./checkpoint.ts";
11
+ import type {
12
+ AuditInternalSessionDescriptor,
13
+ AuditLogActivityRecord,
14
+ AuditLogEnvelope,
15
+ KnownAuditLogRecord,
16
+ KnownTranscriptRecord,
17
+ OperationAbortReason,
18
+ OperationEvent,
19
+ OperationKind,
20
+ TranscriptActivityRecord,
21
+ TranscriptEnvelope,
22
+ TranscriptLifecycleRecord,
23
+ TranscriptTransitionRecord,
24
+ InternalSessionDescriptor,
25
+ InternalSessionHistoryEntry,
26
+ } from "./records.ts";
27
+ import type { PersistedMachineCode } from "./integrity.ts";
28
+
29
+ export interface TranscriptProjectionContext {
30
+ readonly transcriptId: string;
31
+ readonly generation: number;
32
+ readonly agentSessionId: string;
33
+ readonly seq: number;
34
+ readonly ts: number;
35
+ readonly policy: TranscriptCapturePolicy;
36
+ }
37
+
38
+ export interface AuditProjectionContext {
39
+ readonly auditLogId: string;
40
+ readonly generation: number;
41
+ readonly agentSessionId: string;
42
+ readonly seq: number;
43
+ readonly ts: number;
44
+ readonly policy: AuditLogCapturePolicy;
45
+ }
46
+
47
+ export interface NormalizedActivityProjection {
48
+ readonly activityId: string;
49
+ readonly activityRevision: number;
50
+ readonly operationId: string;
51
+ readonly reason: TranscriptActivityRecord["reason"];
52
+ readonly kind: string;
53
+ readonly status?: string;
54
+ /** Content-free allowlist. Titles are intentionally absent because agents derive them from arguments. */
55
+ readonly metadata?: {
56
+ readonly providerActivityId?: string;
57
+ readonly startedAt?: number;
58
+ readonly endedAt?: number;
59
+ readonly durationMs?: number;
60
+ };
61
+ /** Invocation arguments, commands, paths, and patches. */
62
+ readonly input?: JsonObject;
63
+ /** Tool results, output, and returned content. */
64
+ readonly content?: JsonObject;
65
+ }
66
+
67
+ export type NormalizedPersistenceEvent =
68
+ | {
69
+ readonly kind: "operation";
70
+ readonly event: OperationEvent;
71
+ readonly operationId: string;
72
+ readonly operationKind: OperationKind;
73
+ readonly stateRevision?: number;
74
+ readonly errorCode?: PersistedMachineCode;
75
+ readonly abortReason?: OperationAbortReason;
76
+ }
77
+ | { readonly kind: "checkpoint"; readonly checkpoint: AgentSessionCheckpoint }
78
+ | {
79
+ readonly kind: "message";
80
+ readonly operationId: string;
81
+ readonly role: "user" | "assistant";
82
+ readonly text: string;
83
+ readonly structured?: { readonly version: 1; readonly messages: readonly { readonly messageId?: string; readonly text: string }[] };
84
+ readonly status?: string;
85
+ readonly stopReason?: string;
86
+ }
87
+ | {
88
+ readonly kind: "reasoning";
89
+ readonly reasoningId: string;
90
+ readonly reasoningRevision: number;
91
+ readonly operationId: string;
92
+ readonly text: string;
93
+ readonly status: "in_progress" | "completed";
94
+ }
95
+ | { readonly kind: "activity"; readonly activity: NormalizedActivityProjection }
96
+ | {
97
+ readonly kind: "configuration";
98
+ readonly event: "announced" | "selected" | "restored" | "restore_failed" | "resolved";
99
+ readonly configId?: string;
100
+ readonly value?: JsonValue;
101
+ readonly definitions?: JsonValue;
102
+ readonly schemaDigest?: string;
103
+ }
104
+ | {
105
+ readonly kind: "permission";
106
+ readonly permissionId: string;
107
+ readonly activityId?: string;
108
+ readonly event: "requested" | "resolved";
109
+ readonly outcome?: string;
110
+ readonly fullDetails?: JsonValue;
111
+ }
112
+ | {
113
+ readonly kind: "transition";
114
+ readonly transitionId: string;
115
+ readonly transitionKind: TranscriptTransitionRecord["kind"];
116
+ readonly event: TranscriptTransitionRecord["event"];
117
+ readonly from: InternalSessionDescriptor | null;
118
+ readonly to?: InternalSessionDescriptor;
119
+ readonly historyEntry?: InternalSessionHistoryEntry;
120
+ readonly activationState?: TranscriptTransitionRecord["activationState"];
121
+ readonly failedAfter?: TranscriptTransitionRecord["failedAfter"];
122
+ readonly summary?: string;
123
+ readonly reasonCode?: PersistedMachineCode;
124
+ readonly reasonText?: string;
125
+ readonly errorCode?: PersistedMachineCode;
126
+ }
127
+ | {
128
+ readonly kind: "metering_reference";
129
+ readonly operationId: string;
130
+ readonly meteringRecordId: string;
131
+ readonly status: "available" | "partial" | "unavailable";
132
+ readonly summary?: JsonValue;
133
+ }
134
+ | { readonly kind: "lifecycle"; readonly event: TranscriptLifecycleRecord["event"]; readonly detail?: JsonValue }
135
+ | {
136
+ readonly kind: "protocol_metadata";
137
+ readonly event: "initialized" | "capabilities_updated";
138
+ readonly protocol: PersistedProtocol;
139
+ readonly agent?: { readonly name?: string; readonly title?: string; readonly version?: string };
140
+ readonly capabilities: Readonly<Record<string, boolean>>;
141
+ }
142
+ | {
143
+ readonly kind: "raw_protocol";
144
+ readonly protocol: string;
145
+ readonly direction: "client_to_agent" | "agent_to_client";
146
+ readonly event: string;
147
+ readonly data: JsonValue;
148
+ };
149
+
150
+ export function projectTranscriptEvent(
151
+ event: NormalizedPersistenceEvent,
152
+ context: TranscriptProjectionContext,
153
+ ): KnownTranscriptRecord | null {
154
+ const base = transcriptEnvelope(context);
155
+ switch (event.kind) {
156
+ case "operation":
157
+ return {
158
+ ...base, t: "operation", event: event.event, operationId: event.operationId,
159
+ operationKind: event.operationKind,
160
+ ...(event.stateRevision === undefined ? {} : { stateRevision: event.stateRevision }),
161
+ ...(event.errorCode === undefined ? {} : { errorCode: event.errorCode }),
162
+ ...(event.abortReason === undefined ? {} : { abortReason: event.abortReason }),
163
+ };
164
+ case "checkpoint":
165
+ return { ...base, t: "checkpoint", stateRevision: event.checkpoint.stateRevision, checkpoint: structuredClone(event.checkpoint) };
166
+ case "message":
167
+ if (context.policy.messages === "none") return null;
168
+ return {
169
+ ...base, t: "message", operationId: event.operationId, role: event.role, text: event.text,
170
+ ...(event.structured === undefined ? {} : { structured: structuredClone(event.structured) }),
171
+ ...(event.status === undefined ? {} : { status: event.status }),
172
+ ...(event.stopReason === undefined ? {} : { stopReason: event.stopReason }),
173
+ };
174
+ case "reasoning":
175
+ if (context.policy.reasoning === "none") return null;
176
+ return { ...base, t: "reasoning", reasoningId: event.reasoningId, reasoningRevision: event.reasoningRevision, operationId: event.operationId, text: event.text, status: event.status };
177
+ case "activity": {
178
+ const data = activityData(event.activity, context.policy.tools);
179
+ if (!data) return null;
180
+ return activityRecord(base, event.activity, data);
181
+ }
182
+ case "configuration": {
183
+ const projected = configurationData(event, context.policy.configuration);
184
+ return projected ? { ...base, t: "configuration", ...projected } : null;
185
+ }
186
+ case "permission": {
187
+ if (context.policy.permissions === "none") return null;
188
+ return {
189
+ ...base, t: "permission", permissionId: event.permissionId, event: event.event,
190
+ ...(event.activityId === undefined ? {} : { activityId: event.activityId }),
191
+ ...(event.outcome === undefined ? {} : { outcome: event.outcome }),
192
+ ...(context.policy.permissions === "full" && event.fullDetails !== undefined
193
+ ? { details: structuredClone(event.fullDetails) }
194
+ : {}),
195
+ };
196
+ }
197
+ case "transition":
198
+ return transcriptTransition(base, event, context.policy.transitions);
199
+ case "metering_reference":
200
+ if (!context.policy.usage) return null;
201
+ return {
202
+ ...base, t: "metering_reference", operationId: event.operationId,
203
+ meteringRecordId: event.meteringRecordId, status: event.status,
204
+ ...(event.summary === undefined ? {} : { summary: structuredClone(event.summary) }),
205
+ };
206
+ case "lifecycle":
207
+ if (!context.policy.lifecycle) return null;
208
+ return { ...base, t: "lifecycle", event: event.event, ...(event.detail === undefined ? {} : { detail: structuredClone(event.detail) }) };
209
+ case "protocol_metadata":
210
+ if (context.policy.protocol === "none") return null;
211
+ return {
212
+ ...base, t: "protocol", event: event.event, protocol: structuredClone(event.protocol),
213
+ ...(event.agent === undefined ? {} : { agent: structuredClone(event.agent) }),
214
+ capabilities: sortBooleanRecord(event.capabilities),
215
+ };
216
+ case "raw_protocol":
217
+ if (context.policy.protocol !== "raw") return null;
218
+ return { ...base, t: "raw", protocol: event.protocol, direction: event.direction, event: event.event, data: structuredClone(event.data) };
219
+ }
220
+ }
221
+
222
+ export function projectAuditEvent(
223
+ event: NormalizedPersistenceEvent,
224
+ context: AuditProjectionContext,
225
+ ): KnownAuditLogRecord | null {
226
+ const base = auditEnvelope(context);
227
+ switch (event.kind) {
228
+ case "operation":
229
+ return {
230
+ ...base, t: "operation", event: event.event, operationId: event.operationId,
231
+ operationKind: event.operationKind,
232
+ ...(event.errorCode === undefined ? {} : { errorCode: event.errorCode }),
233
+ ...(event.abortReason === undefined ? {} : { abortReason: event.abortReason }),
234
+ };
235
+ case "checkpoint":
236
+ case "message":
237
+ case "protocol_metadata":
238
+ case "raw_protocol":
239
+ return null;
240
+ case "reasoning":
241
+ if (context.policy.reasoning === "none") return null;
242
+ return { ...base, t: "reasoning", reasoningId: event.reasoningId, reasoningRevision: event.reasoningRevision, operationId: event.operationId, text: event.text, status: event.status };
243
+ case "activity": {
244
+ const data = activityData(event.activity, context.policy.tools);
245
+ if (!data) return null;
246
+ return activityRecord(base, event.activity, data);
247
+ }
248
+ case "configuration": {
249
+ const projected = configurationData(event, context.policy.configuration);
250
+ return projected ? { ...base, t: "configuration", ...projected } : null;
251
+ }
252
+ case "permission":
253
+ if (context.policy.permissions === "none") return null;
254
+ return {
255
+ ...base, t: "permission", permissionId: event.permissionId, event: event.event,
256
+ ...(event.activityId === undefined ? {} : { activityId: event.activityId }),
257
+ ...(event.outcome === undefined ? {} : { outcome: event.outcome }),
258
+ ...(context.policy.permissions === "full" && event.fullDetails !== undefined
259
+ ? { details: structuredClone(event.fullDetails) }
260
+ : {}),
261
+ };
262
+ case "transition":
263
+ if (context.policy.transitions === "none") return null;
264
+ return {
265
+ ...base, t: "transition", transitionId: event.transitionId, kind: event.transitionKind,
266
+ event: event.event, from: auditDescriptor(event.from),
267
+ ...(event.to === undefined ? {} : { to: auditDescriptor(event.to)! }),
268
+ ...(event.failedAfter === undefined ? {} : { failedAfter: event.failedAfter }),
269
+ ...(event.reasonCode === undefined ? {} : { reasonCode: event.reasonCode }),
270
+ ...(event.errorCode === undefined ? {} : { errorCode: event.errorCode }),
271
+ };
272
+ case "metering_reference":
273
+ if (!context.policy.usage) return null;
274
+ return {
275
+ ...base, t: "metering_reference", operationId: event.operationId,
276
+ meteringRecordId: event.meteringRecordId, status: event.status,
277
+ ...(event.summary === undefined ? {} : { summary: structuredClone(event.summary) }),
278
+ };
279
+ case "lifecycle":
280
+ if (!context.policy.lifecycle) return null;
281
+ return { ...base, t: "lifecycle", event: event.event, ...(event.detail === undefined ? {} : { detail: structuredClone(event.detail) }) };
282
+ }
283
+ }
284
+
285
+ function transcriptEnvelope(context: TranscriptProjectionContext): TranscriptEnvelope {
286
+ return { v: 2, t: "raw", transcriptId: context.transcriptId, generation: context.generation, agentSessionId: context.agentSessionId, seq: context.seq, ts: context.ts };
287
+ }
288
+
289
+ function auditEnvelope(context: AuditProjectionContext): AuditLogEnvelope {
290
+ return { v: 1, t: "lifecycle", auditLogId: context.auditLogId, generation: context.generation, agentSessionId: context.agentSessionId, seq: context.seq, ts: context.ts };
291
+ }
292
+
293
+ function activityData(
294
+ activity: NormalizedActivityProjection,
295
+ level: TranscriptCapturePolicy["tools"],
296
+ ): JsonObject | null {
297
+ if (level === "none") return null;
298
+ const data: JsonObject = {};
299
+ if (activity.metadata?.providerActivityId !== undefined) data.providerActivityId = activity.metadata.providerActivityId;
300
+ if (activity.metadata?.startedAt !== undefined) data.startedAt = activity.metadata.startedAt;
301
+ if (activity.metadata?.endedAt !== undefined) data.endedAt = activity.metadata.endedAt;
302
+ if (activity.metadata?.durationMs !== undefined) data.durationMs = activity.metadata.durationMs;
303
+ if (level === "input" || level === "content") {
304
+ if (activity.input !== undefined) data.input = structuredClone(activity.input);
305
+ }
306
+ if (level === "content" && activity.content !== undefined) data.content = structuredClone(activity.content);
307
+ return data;
308
+ }
309
+
310
+ function activityRecord(base: TranscriptEnvelope, activity: NormalizedActivityProjection, data: JsonObject): TranscriptActivityRecord;
311
+ function activityRecord(base: AuditLogEnvelope, activity: NormalizedActivityProjection, data: JsonObject): AuditLogActivityRecord;
312
+ function activityRecord(
313
+ base: TranscriptEnvelope | AuditLogEnvelope,
314
+ activity: NormalizedActivityProjection,
315
+ data: JsonObject,
316
+ ): TranscriptActivityRecord | AuditLogActivityRecord {
317
+ return {
318
+ ...base, t: "activity", activityId: activity.activityId,
319
+ activityRevision: activity.activityRevision, operationId: activity.operationId,
320
+ reason: activity.reason, kind: activity.kind,
321
+ ...(activity.status === undefined ? {} : { status: activity.status }), data,
322
+ } as TranscriptActivityRecord | AuditLogActivityRecord;
323
+ }
324
+
325
+ function configurationData(
326
+ event: Extract<NormalizedPersistenceEvent, { kind: "configuration" }>,
327
+ level: TranscriptCapturePolicy["configuration"],
328
+ ): Omit<Extract<KnownTranscriptRecord, { t: "configuration" }>, keyof TranscriptEnvelope | "t"> | null {
329
+ if (level === "none") return null;
330
+ return {
331
+ event: event.event,
332
+ ...(event.configId === undefined ? {} : { configId: event.configId }),
333
+ ...(event.value === undefined ? {} : { value: structuredClone(event.value) }),
334
+ ...(level === "definitions" && event.definitions !== undefined ? { definitions: structuredClone(event.definitions) } : {}),
335
+ ...(event.schemaDigest === undefined ? {} : { schemaDigest: event.schemaDigest }),
336
+ };
337
+ }
338
+
339
+ function transcriptTransition(
340
+ base: TranscriptEnvelope,
341
+ event: Extract<NormalizedPersistenceEvent, { kind: "transition" }>,
342
+ level: TranscriptCapturePolicy["transitions"],
343
+ ): TranscriptTransitionRecord {
344
+ // Transition routing and activation/history are continuation structure and are retained
345
+ // even when presentation capture is disabled. Only prose/content follows the preset.
346
+ return {
347
+ ...base, t: "transition", transitionId: event.transitionId, kind: event.transitionKind,
348
+ event: event.event, from: structuredClone(event.from),
349
+ ...(event.to === undefined ? {} : { to: structuredClone(event.to) }),
350
+ ...(event.historyEntry === undefined ? {} : { historyEntry: structuredClone(event.historyEntry) }),
351
+ ...(event.activationState === undefined ? {} : { activationState: structuredClone(event.activationState) }),
352
+ ...(event.failedAfter === undefined ? {} : { failedAfter: event.failedAfter }),
353
+ ...(event.reasonCode === undefined ? {} : { reasonCode: event.reasonCode }),
354
+ ...(event.errorCode === undefined ? {} : { errorCode: event.errorCode }),
355
+ ...(level === "content" && event.summary !== undefined ? { summary: event.summary } : {}),
356
+ ...(level === "content" && event.reasonText !== undefined ? { reasonText: event.reasonText } : {}),
357
+ };
358
+ }
359
+
360
+ function auditDescriptor(value: InternalSessionDescriptor | null): AuditInternalSessionDescriptor | null {
361
+ if (value === null) return null;
362
+ return {
363
+ internalSessionId: value.internalSessionId,
364
+ adapterId: value.adapterId,
365
+ mode: value.mode,
366
+ model: value.model,
367
+ protocol: structuredClone(value.protocol),
368
+ };
369
+ }
370
+
371
+ function sortBooleanRecord(value: Readonly<Record<string, boolean>>): Readonly<Record<string, boolean>> {
372
+ return Object.fromEntries(Object.entries(value).sort(([a], [b]) => a.localeCompare(b)));
373
+ }