@qlairoslabs/acp-client 0.7.3 → 0.8.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 (58) hide show
  1. package/CHANGELOG-audit.md +58 -0
  2. package/CONSUMER_GUIDE.md +474 -19
  3. package/package.json +6 -1
  4. package/src/agent-adapter-registry.ts +66 -0
  5. package/src/agent-conversation-continuation.ts +121 -0
  6. package/src/agent-conversation-contracts.ts +279 -0
  7. package/src/agent-conversation-errors.ts +31 -0
  8. package/src/agent-conversation-owner.ts +55 -0
  9. package/src/agent-conversation-presentation.ts +217 -0
  10. package/src/agent-conversation-queue.ts +44 -0
  11. package/src/agent-conversation.ts +41 -0
  12. package/src/agent-ids.ts +50 -0
  13. package/src/agent-session.ts +1022 -682
  14. package/src/agent.ts +1175 -0
  15. package/src/audit/config.ts +186 -0
  16. package/src/audit/contracts.ts +59 -0
  17. package/src/audit/index.ts +50 -0
  18. package/src/audit/projection.ts +125 -0
  19. package/src/audit/reader.ts +371 -0
  20. package/src/audit/runtime.ts +1060 -0
  21. package/src/checkpointing/config.ts +91 -0
  22. package/src/checkpointing/contracts.ts +45 -0
  23. package/src/checkpointing/coordinator.ts +100 -0
  24. package/src/checkpointing/ids.ts +5 -0
  25. package/src/checkpointing/index.ts +6 -0
  26. package/src/checkpointing/persistence/sqlite-repository.ts +159 -0
  27. package/src/checkpointing/recovery.ts +114 -0
  28. package/src/checkpointing/strategy.ts +139 -0
  29. package/src/controller.ts +37 -5
  30. package/src/identity.ts +62 -0
  31. package/src/index.ts +121 -0
  32. package/src/internal-inference/config.ts +68 -0
  33. package/src/internal-inference/contracts.ts +218 -0
  34. package/src/internal-inference/coordination/handles.ts +133 -0
  35. package/src/internal-inference/coordination/manager.ts +487 -0
  36. package/src/internal-inference/ids.ts +10 -0
  37. package/src/internal-inference/index.ts +10 -0
  38. package/src/internal-inference/persistence/records.ts +132 -0
  39. package/src/internal-inference/persistence/repository.ts +35 -0
  40. package/src/internal-inference/persistence/sqlite-repository.ts +551 -0
  41. package/src/internal-inference/runtime/harness-runtime.ts +78 -0
  42. package/src/internal-inference/strategy/registry.ts +42 -0
  43. package/src/metering/engine.ts +47 -0
  44. package/src/metering/record.ts +4 -1
  45. package/src/observation/contracts.ts +10 -0
  46. package/src/observation/index.ts +8 -0
  47. package/src/observation/subscription-hub.ts +43 -0
  48. package/src/persistence/activity-projection.ts +26 -12
  49. package/src/persistence/backend.ts +774 -0
  50. package/src/persistence/index.ts +99 -0
  51. package/src/persistence/managed-creation.ts +18 -2
  52. package/src/persistence/managed-load.ts +19 -5
  53. package/src/persistence/managed-open-progress.ts +54 -0
  54. package/src/persistence/presentation.ts +3 -0
  55. package/src/persistence/sqlite-backend.ts +1927 -0
  56. package/src/persistence/sqlite-conversation-records.ts +425 -0
  57. package/src/persistence/sqlite-owner-lock.ts +106 -0
  58. package/src/persistence/sqlite-schema.ts +561 -0
@@ -0,0 +1,99 @@
1
+ export {
2
+ CONVERSATION_RECORD_KINDS,
3
+ DEFAULT_CONVERSATION_RECORD_CAPTURE,
4
+ IMPLICIT_USER_PARTICIPANT_ID,
5
+ PARTICIPANT_KINDS,
6
+ PersistenceCapabilityError,
7
+ PersistenceConflictError,
8
+ isConversationRecordKind,
9
+ isParticipantKind,
10
+ normalizeParticipantRef,
11
+ resolveConversationRecordCapture,
12
+ shouldCaptureProtocolMethod,
13
+ searchableText,
14
+ } from "./backend.ts";
15
+ export type {
16
+ ActivityRecord,
17
+ AgentActionRecord,
18
+ AgentConversationSummary,
19
+ AgentAuditState,
20
+ AgentPersistenceIdentity,
21
+ AuditOutboxItem,
22
+ AuditOutboxBacklog,
23
+ AuditOutboxPersistence,
24
+ ConfigurationRecord,
25
+ ConversationCheckpointPersistence,
26
+ ConversationListOptions,
27
+ ConversationMessagePersistence,
28
+ ConversationPage,
29
+ ConversationPersistence,
30
+ ConversationRecord,
31
+ ConversationRecordCaptureConfig,
32
+ ConversationRecordCapturePolicy,
33
+ ConversationRecordEnvelope,
34
+ ConversationRecordKind,
35
+ ConversationRecordPage,
36
+ ConversationRecordPersistence,
37
+ ConversationRuntimeCheckpoint,
38
+ CreateAgentConversationInput,
39
+ IdentityMemoryObservation,
40
+ IdentityPersistence,
41
+ IdentityProjectionState,
42
+ MessageListOptions,
43
+ MessagePage,
44
+ MessageRecord,
45
+ AgentMeteringCommitInput,
46
+ MeteringCommitInput,
47
+ MeteringPersistence,
48
+ MeteringRecordListOptions,
49
+ MeteringRecordPage,
50
+ MeteringRecordRetention,
51
+ MeteringReferenceRecord,
52
+ ProtocolRecordCapture,
53
+ ProtocolRecordCaptureLevel,
54
+ ProtocolRecordCaptureOptions,
55
+ ResolvedProtocolRecordCapturePolicy,
56
+ NativeActionRecord,
57
+ NewAuditOutboxItem,
58
+ NewConversationRecord,
59
+ NewIdentityMemoryObservation,
60
+ ParticipantKind,
61
+ ParticipantRef,
62
+ PersistedContent,
63
+ PersistedContentBlock,
64
+ PersistedIdentitySnapshot,
65
+ PersistedAgentMeteringState,
66
+ PersistedConversationMeteringState,
67
+ PersistenceBackend,
68
+ PersistenceCapabilities,
69
+ PersistenceExtension,
70
+ PersistenceExtensionCatalog,
71
+ PersistenceExtensionMigrationContext,
72
+ PersistenceExtensionRegistration,
73
+ PersistenceJsonPrimitive,
74
+ PersistenceJsonValue,
75
+ PersistenceScope,
76
+ PermissionRecord,
77
+ ProtocolRecord,
78
+ ProviderSessionPersistence,
79
+ ProviderSessionRecord,
80
+ RecordCaptureLevel,
81
+ RecordListOptions,
82
+ SearchMatch,
83
+ SearchPersistence,
84
+ SearchQuery,
85
+ ThinkingRecord,
86
+ ToolCallRecord,
87
+ VectorIndex,
88
+ VectorSearchMatch,
89
+ VectorSearchQuery,
90
+ } from "./backend.ts";
91
+
92
+ export {
93
+ AgentDatabaseInUseError,
94
+ SqliteAgentPersistence,
95
+ hasSafeConcurrentWal,
96
+ requireSqliteSearch,
97
+ } from "./sqlite-backend.ts";
98
+ export type { SqliteAgentPersistenceConfig } from "./sqlite-backend.ts";
99
+ export { SQLITE_AGENT_SCHEMA_VERSION } from "./sqlite-schema.ts";
@@ -27,6 +27,10 @@ import {
27
27
  } from "./session-log-writer.ts";
28
28
  import { attachManagedRegistryPublisher } from "./managed-registry.ts";
29
29
  import { AuditPersistenceRuntime } from "./audit-runtime.ts";
30
+ import {
31
+ createManagedOpenProgressReporter,
32
+ type AgentSessionOpenProgressListener,
33
+ } from "./managed-open-progress.ts";
30
34
 
31
35
  // Retain the former module-level export while the canonical error now applies to every
32
36
  // maintenance kind and lives with the shared registry contract.
@@ -50,6 +54,8 @@ export interface ManagedAgentSessionCreateConfig extends Omit<
50
54
  readonly creationId?: string;
51
55
  readonly mode?: "normal";
52
56
  readonly persistence?: ManagedSessionPersistenceOptions;
57
+ /** Observe ephemeral managed-open progress before the AgentSession is returned. */
58
+ readonly onOpenProgress?: AgentSessionOpenProgressListener;
53
59
  }
54
60
 
55
61
  export class ManagedAgentSessionCreationError extends Error {
@@ -74,6 +80,10 @@ export async function createManagedAgentSession(
74
80
  }
75
81
  const agentSessionId = nonblankOrGenerated(config.agentSessionId, "agentSessionId");
76
82
  const creationId = nonblankOrGenerated(config.creationId, "creationId");
83
+ const reportOpenProgress = createManagedOpenProgressReporter(
84
+ config.onOpenProgress,
85
+ config.logger,
86
+ );
77
87
  const paths = await resolveSessionLogPaths(context.persistence, agentSessionId);
78
88
  const transcriptPath = paths.transcriptPath;
79
89
  const requestDigest = sha256CanonicalJson({
@@ -158,20 +168,22 @@ export async function createManagedAgentSession(
158
168
  })
159
169
  : undefined;
160
170
  lockTransferred = true;
171
+ reportOpenProgress({ stage: "initializing" });
161
172
  const opened = active.details.stagedRecord.transcript
162
173
  ? await AgentSession.openManaged({
163
174
  ...runtimeConfig(config),
164
175
  agentSessionId,
165
176
  persistence: directPersistence(context.persistence, transcriptPath),
166
177
  checkpoint: active.details.stagedRecord.checkpoint,
167
- }, transcriptLock, audit)
178
+ }, transcriptLock, audit, undefined, reportOpenProgress)
168
179
  : await AgentSession.openManagedRegistry({
169
180
  ...runtimeConfig(config),
170
181
  agentSessionId,
171
182
  persistence: directPersistence(context.persistence, null),
172
183
  checkpoint: active.details.stagedRecord.checkpoint,
173
- }, active.details.stagedRecord.internalSessionHistory, audit);
184
+ }, active.details.stagedRecord.internalSessionHistory, audit, undefined, reportOpenProgress);
174
185
  attachManagedRegistryPublisher(opened.session, context.store);
186
+ reportOpenProgress({ stage: "ready", promptability: opened.session.promptability });
175
187
  return opened.session;
176
188
  }
177
189
 
@@ -191,6 +203,7 @@ export async function createManagedAgentSession(
191
203
  }
192
204
  const stagedContinuation = active.details.stagedContinuation;
193
205
  lockTransferred = true;
206
+ reportOpenProgress({ stage: "initializing" });
194
207
  session = await AgentSession.createManaged({
195
208
  ...runtimeConfig(config),
196
209
  agentSessionId,
@@ -224,6 +237,7 @@ export async function createManagedAgentSession(
224
237
  active = update;
225
238
  return state;
226
239
  },
240
+ onOpenProgress: reportOpenProgress,
227
241
  });
228
242
 
229
243
  const snapshot = session.getManagedPersistenceSnapshot();
@@ -246,6 +260,7 @@ export async function createManagedAgentSession(
246
260
  { save: record },
247
261
  );
248
262
  attachManagedRegistryPublisher(session, context.store);
263
+ reportOpenProgress({ stage: "ready", promptability: session.promptability });
249
264
  return session;
250
265
  } catch (error) {
251
266
  await session?.stop().catch(() => undefined);
@@ -266,6 +281,7 @@ function runtimeConfig(config: ManagedAgentSessionCreateConfig): Omit<
266
281
  creationId: _creationId,
267
282
  persistence: _persistence,
268
283
  mode: _mode,
284
+ onOpenProgress: _onOpenProgress,
269
285
  ...runtime
270
286
  } = config;
271
287
  return runtime;
@@ -16,11 +16,18 @@ import {
16
16
  projectManagedAgentSessionRecord,
17
17
  } from "./managed-registry.ts";
18
18
  import { AuditPersistenceRuntime } from "./audit-runtime.ts";
19
+ import {
20
+ createManagedOpenProgressReporter,
21
+ type AgentSessionOpenProgressListener,
22
+ } from "./managed-open-progress.ts";
19
23
 
20
24
  export interface ManagedAgentSessionLoadConfig extends Omit<
21
25
  AgentSessionOpenConfig,
22
26
  "agentSessionId" | "persistence" | "checkpoint"
23
- > {}
27
+ > {
28
+ /** Observe ephemeral managed-open progress before the open result is returned. */
29
+ readonly onOpenProgress?: AgentSessionOpenProgressListener;
30
+ }
24
31
 
25
32
  export class ManagedAgentSessionNotFoundError extends Error {
26
33
  constructor(readonly agentSessionId: string) {
@@ -49,6 +56,10 @@ export async function loadManagedAgentSession(
49
56
  agentSessionId: string,
50
57
  config: ManagedAgentSessionLoadConfig,
51
58
  ): Promise<AgentSessionOpenResult> {
59
+ const reportOpenProgress = createManagedOpenProgressReporter(
60
+ config.onOpenProgress,
61
+ config.logger,
62
+ );
52
63
  const [record, maintenance] = await Promise.all([
53
64
  store.getAgentSessionRecord(agentSessionId),
54
65
  store.getMaintenance(agentSessionId),
@@ -62,6 +73,8 @@ export async function loadManagedAgentSession(
62
73
  if (record.adapterId !== config.adapterId && !hasReconstructionStrategy(config)) {
63
74
  throw new AdapterMismatchError(record.adapterId, config.adapterId);
64
75
  }
76
+ const { onOpenProgress: _onOpenProgress, ...openConfig } = config;
77
+ reportOpenProgress({ stage: "initializing" });
65
78
 
66
79
  const audit = record.auditLog
67
80
  ? await AuditPersistenceRuntime.resume({
@@ -75,12 +88,12 @@ export async function loadManagedAgentSession(
75
88
  try {
76
89
  opened = record.transcript
77
90
  ? await AgentSession.openManaged({
78
- ...config,
91
+ ...openConfig,
79
92
  agentSessionId,
80
93
  persistence: persistenceFromManagedRecord(record),
81
- }, undefined, audit)
94
+ }, undefined, audit, undefined, reportOpenProgress)
82
95
  : await AgentSession.openManagedRegistry({
83
- ...config,
96
+ ...openConfig,
84
97
  agentSessionId,
85
98
  checkpoint: record.checkpoint,
86
99
  persistence: {
@@ -88,7 +101,7 @@ export async function loadManagedAgentSession(
88
101
  auditLog: false,
89
102
  sessionConfiguration: record.sessionConfigurationPersistence ?? { values: "typed" },
90
103
  },
91
- }, record.internalSessionHistory, audit);
104
+ }, record.internalSessionHistory, audit, undefined, reportOpenProgress);
92
105
  } catch (error) {
93
106
  await audit?.close().catch(() => undefined);
94
107
  throw error;
@@ -118,6 +131,7 @@ export async function loadManagedAgentSession(
118
131
  expectedIncarnationId: current.incarnationId,
119
132
  });
120
133
  attachManagedRegistryPublisher(opened.session, store);
134
+ reportOpenProgress({ stage: "ready", promptability: opened.promptability });
121
135
  return opened;
122
136
  } catch (error) {
123
137
  await opened.session.stop().catch(() => undefined);
@@ -0,0 +1,54 @@
1
+ import type { Capabilities } from "../capabilities.ts";
2
+ import type { AgentSessionPromptability } from "../agent-session.ts";
3
+ import type { Logger } from "../logger.ts";
4
+
5
+ /** Ephemeral progress reported while a managed logical session is becoming usable. */
6
+ export type AgentSessionOpenStage =
7
+ | { readonly stage: "initializing" }
8
+ | { readonly stage: "initialized"; readonly capabilities: Capabilities }
9
+ | {
10
+ readonly stage: "internal_session_acquired";
11
+ readonly internalSessionId: string;
12
+ readonly disposition: "created" | "resumed";
13
+ }
14
+ | {
15
+ readonly stage: "ready";
16
+ readonly promptability: AgentSessionPromptability;
17
+ };
18
+
19
+ /**
20
+ * A presentation hook, not a persistence or transaction hook.
21
+ *
22
+ * Calls are made in lifecycle order. A retry starts a new sequence, and a failed open
23
+ * does not report `ready`. Listener failures are logged and never fail the managed open.
24
+ */
25
+ export type AgentSessionOpenProgressListener = (event: AgentSessionOpenStage) => void;
26
+
27
+ /** @internal A listener with failure isolation already applied by the managed coordinator. */
28
+ export type ManagedOpenProgressReporter = (event: AgentSessionOpenStage) => void;
29
+
30
+ /** @internal */
31
+ export function createManagedOpenProgressReporter(
32
+ listener: AgentSessionOpenProgressListener | undefined,
33
+ logger: Logger | undefined,
34
+ ): ManagedOpenProgressReporter {
35
+ if (!listener) return () => undefined;
36
+ return (event) => {
37
+ try {
38
+ const returned: unknown = listener(event);
39
+ if (isPromiseLike(returned)) {
40
+ void Promise.resolve(returned).catch((error) => {
41
+ logger?.warn("managed_open_progress_listener_failed", { error: String(error) });
42
+ });
43
+ }
44
+ } catch (error) {
45
+ logger?.warn("managed_open_progress_listener_failed", { error: String(error) });
46
+ }
47
+ };
48
+ }
49
+
50
+ function isPromiseLike(value: unknown): value is PromiseLike<unknown> {
51
+ return typeof value === "object"
52
+ && value !== null
53
+ && typeof (value as { then?: unknown }).then === "function";
54
+ }
@@ -19,6 +19,8 @@ interface TranscriptPresentationItemBase {
19
19
  readonly revision: number;
20
20
  /** Physical transcript sequence containing this revision. */
21
21
  readonly recordSequence: number;
22
+ /** Unix epoch milliseconds from the persisted record envelope. */
23
+ readonly timestamp: number;
22
24
  readonly operationId: string;
23
25
  readonly transcriptId: string;
24
26
  readonly generation: number;
@@ -64,6 +66,7 @@ export function presentationItemFromRecord(
64
66
  position: presentation.position,
65
67
  revision: presentation.revision,
66
68
  recordSequence: record.seq,
69
+ timestamp: record.ts,
67
70
  operationId: record.operationId,
68
71
  transcriptId: record.transcriptId,
69
72
  generation: record.generation,