@qlairoslabs/acp-client 0.7.4 → 0.9.2

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 (55) hide show
  1. package/CHANGELOG-audit.md +58 -0
  2. package/CONSUMER_GUIDE.md +571 -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 +60 -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 +1032 -674
  14. package/src/agent.ts +1223 -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 +127 -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/identity/checkpoint-backed.ts +248 -0
  30. package/src/identity.ts +76 -0
  31. package/src/index.ts +132 -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 +839 -0
  50. package/src/persistence/index.ts +113 -0
  51. package/src/persistence/sqlite-backend.ts +2192 -0
  52. package/src/persistence/sqlite-conversation-records.ts +430 -0
  53. package/src/persistence/sqlite-owner-lock.ts +106 -0
  54. package/src/persistence/sqlite-schema.ts +589 -0
  55. package/src/persistence/timeline-reconcile.ts +72 -0
@@ -0,0 +1,218 @@
1
+ import type { AgentConversationId, AgentSessionId } from "../agent-ids.ts";
2
+ import type { AgentSessionMeteringState } from "../metering/state.ts";
3
+ import type { OperationMeteringRecord, OperationOutcome } from "../metering/record.ts";
4
+ import type { StatefulSubscribable, Subscribable } from "../observation/contracts.ts";
5
+ import type { PersistenceJsonValue, PersistenceScope } from "../persistence/backend.ts";
6
+ import type { ResponseMessage, TokenUsage } from "../types.ts";
7
+
8
+ export type InternalInferenceId = `aii-${string}`;
9
+ export type BuiltinInternalInferencePurpose =
10
+ | "conversation_checkpoint"
11
+ | "conversation_recovery"
12
+ | "identity_update";
13
+ export type InternalInferencePurpose = BuiltinInternalInferencePurpose | (string & {});
14
+ export type InternalInferenceStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
15
+ export type InternalInferenceChainStatus = "active" | "completed" | "failed" | "cancelled";
16
+ export type InternalInferenceStage =
17
+ | "queued"
18
+ | "claiming"
19
+ | "preparing_input"
20
+ | "starting_harness"
21
+ | "creating_session"
22
+ | "prompting"
23
+ | "persisting_result"
24
+ | "completed"
25
+ | "retry_wait"
26
+ | "failed"
27
+ | "cancelled";
28
+
29
+ export interface InternalInferenceInputDescriptor {
30
+ readonly version: number;
31
+ readonly strategyId: string;
32
+ readonly payload: PersistenceJsonValue;
33
+ }
34
+
35
+ export interface InternalInferenceRequest {
36
+ readonly internalInferenceId: InternalInferenceId;
37
+ readonly purpose: InternalInferencePurpose;
38
+ readonly agentSessionId: AgentSessionId;
39
+ readonly agentConversationId?: AgentConversationId;
40
+ readonly adapterId: string;
41
+ readonly prompt: string;
42
+ readonly cwd?: string;
43
+ readonly metadata?: string;
44
+ }
45
+
46
+ export interface InternalInferenceResult {
47
+ readonly internalInferenceId: InternalInferenceId;
48
+ readonly internalSessionId: string;
49
+ readonly adapterId: string;
50
+ readonly model: string | null;
51
+ readonly messages: readonly ResponseMessage[];
52
+ readonly usage: TokenUsage | null;
53
+ readonly stopReason?: string;
54
+ readonly metadata?: string;
55
+ }
56
+
57
+ export interface InternalInferenceCompletion {
58
+ readonly internalInferenceId: InternalInferenceId;
59
+ readonly rootInternalInferenceId: InternalInferenceId;
60
+ readonly internalSessionId: string;
61
+ readonly purpose: InternalInferencePurpose;
62
+ readonly artifactId?: string;
63
+ readonly meteringOperationId?: string;
64
+ readonly completedAt: string;
65
+ readonly metadata?: string;
66
+ }
67
+
68
+ export interface InternalInferenceSnapshot {
69
+ readonly id: InternalInferenceId;
70
+ readonly rootId: InternalInferenceId;
71
+ readonly agentSessionId: AgentSessionId;
72
+ readonly retryOfId?: InternalInferenceId;
73
+ readonly revision: number;
74
+ readonly purpose: InternalInferencePurpose;
75
+ readonly status: InternalInferenceStatus;
76
+ readonly stage: InternalInferenceStage;
77
+ readonly adapterId: string;
78
+ readonly agentConversationId?: AgentConversationId;
79
+ readonly sourceCheckpointId?: string;
80
+ readonly sourceFromSequence?: number;
81
+ readonly sourceThroughSequence?: number;
82
+ readonly requestedAt: string;
83
+ readonly startedAt?: string;
84
+ readonly completedAt?: string;
85
+ readonly attemptNumber: number;
86
+ readonly error?: string;
87
+ }
88
+
89
+ export interface InternalInferenceRunSnapshot {
90
+ readonly rootId: InternalInferenceId;
91
+ readonly status: InternalInferenceChainStatus;
92
+ readonly revision: number;
93
+ readonly activeAttemptId?: InternalInferenceId;
94
+ readonly requestedThroughSequence?: number;
95
+ readonly error?: string;
96
+ }
97
+
98
+ export interface InternalInferenceEvent {
99
+ readonly kind: "changed";
100
+ /** Monotonic within one manager process, including ephemeral progress changes. */
101
+ readonly sequence: number;
102
+ readonly inferenceId: InternalInferenceId;
103
+ readonly rootId: InternalInferenceId;
104
+ readonly revision: number;
105
+ readonly snapshot: InternalInferenceSnapshot;
106
+ }
107
+
108
+ export interface InternalInference extends StatefulSubscribable<InternalInferenceSnapshot, InternalInferenceEvent> {
109
+ readonly id: InternalInferenceId;
110
+ readonly result: Promise<InternalInferenceCompletion>;
111
+ cancel(): Promise<void>;
112
+ }
113
+
114
+ export interface InternalInferenceRun extends StatefulSubscribable<InternalInferenceRunSnapshot, InternalInferenceEvent> {
115
+ readonly rootId: InternalInferenceId;
116
+ readonly activeAttempt: InternalInference;
117
+ readonly result: Promise<InternalInferenceCompletion>;
118
+ cancel(): Promise<void>;
119
+ }
120
+
121
+ export interface EnqueueInternalInferenceRequest {
122
+ readonly purpose: InternalInferencePurpose;
123
+ readonly agentConversationId?: AgentConversationId;
124
+ /** Overrides the configured policy; otherwise the manager resolves and snapshots it. */
125
+ readonly adapterId?: string;
126
+ readonly executionProfileId?: string;
127
+ readonly input: InternalInferenceInputDescriptor;
128
+ readonly sourceCheckpointId?: string;
129
+ readonly sourceFromSequence?: number;
130
+ readonly sourceThroughSequence?: number;
131
+ readonly requestedThroughSequence?: number;
132
+ readonly metadata?: string;
133
+ }
134
+
135
+ export interface InternalInferenceManager extends Subscribable<InternalInferenceEvent> {
136
+ enqueue(request: EnqueueInternalInferenceRequest): Promise<InternalInferenceRun>;
137
+ getAttempt(id: InternalInferenceId): Promise<InternalInference | null>;
138
+ getRun(rootId: InternalInferenceId): Promise<InternalInferenceRun | null>;
139
+ list(options?: InternalInferenceListOptions): Promise<InternalInferenceSnapshotPage>;
140
+ cancel(rootId: InternalInferenceId, reason?: string): Promise<void>;
141
+ }
142
+
143
+ export interface InternalInferenceListOptions {
144
+ readonly agentConversationId?: AgentConversationId;
145
+ readonly purpose?: InternalInferencePurpose;
146
+ readonly status?: InternalInferenceStatus;
147
+ readonly rootId?: InternalInferenceId;
148
+ readonly limit?: number;
149
+ readonly cursor?: string;
150
+ }
151
+
152
+ export interface InternalInferenceSnapshotPage {
153
+ readonly attempts: readonly InternalInferenceSnapshot[];
154
+ readonly nextCursor?: string;
155
+ }
156
+
157
+ export interface InternalInferenceRuntime {
158
+ run(
159
+ request: InternalInferenceRequest,
160
+ signal?: AbortSignal,
161
+ lifecycle?: InternalInferenceRuntimeLifecycle,
162
+ ): Promise<InternalInferenceResult>;
163
+ }
164
+
165
+ export interface InternalInferenceRuntimeLifecycle {
166
+ stageChanged?(stage: Extract<InternalInferenceStage, "creating_session" | "prompting">): void;
167
+ sessionStarted(input: {
168
+ readonly internalSessionId: string;
169
+ readonly model: string | null;
170
+ }): void | Promise<void>;
171
+ }
172
+
173
+ export interface InternalInferenceMeteringOperation {
174
+ close(input: {
175
+ readonly outcome: OperationOutcome;
176
+ readonly result?: InternalInferenceResult;
177
+ readonly error?: unknown;
178
+ }): Promise<OperationMeteringRecord>;
179
+ /** Releases restoration barriers after the record transaction settles. */
180
+ settled(): void;
181
+ }
182
+
183
+ export type InternalInferenceRetryDisposition =
184
+ | { readonly kind: "terminal"; readonly code: string }
185
+ | { readonly kind: "retryable"; readonly code: string };
186
+
187
+ export interface PreparedInternalInference {
188
+ readonly prompt: string;
189
+ readonly cwd?: string;
190
+ readonly metadata?: string;
191
+ }
192
+
193
+ export interface InternalInferencePreparationContext {
194
+ readonly attempt: InternalInferenceSnapshot;
195
+ /** Canonical agent persistence used to derive the prompt from durable source data. */
196
+ readonly persistence: PersistenceScope;
197
+ readonly signal?: AbortSignal;
198
+ }
199
+
200
+ export interface InternalInferenceCompletionContext {
201
+ readonly attempt: InternalInferenceSnapshot;
202
+ /** Transaction-scoped persistence used to publish the purpose artifact atomically. */
203
+ readonly persistence: PersistenceScope;
204
+ readonly meteringState?: AgentSessionMeteringState;
205
+ }
206
+
207
+ export interface InternalInferenceStrategy<TPayload = PersistenceJsonValue> {
208
+ readonly strategyId: string;
209
+ readonly purpose: InternalInferencePurpose;
210
+ readonly version: number;
211
+ validatePayload(payload: PersistenceJsonValue): TPayload;
212
+ prepare(context: InternalInferencePreparationContext, payload: TPayload): Promise<PreparedInternalInference>;
213
+ complete(
214
+ context: InternalInferenceCompletionContext,
215
+ result: InternalInferenceResult,
216
+ ): Promise<InternalInferenceCompletion>;
217
+ classifyError(error: unknown): InternalInferenceRetryDisposition;
218
+ }
@@ -0,0 +1,133 @@
1
+ import { SubscriptionHub } from "../../observation/subscription-hub.ts";
2
+ import type {
3
+ InternalInference,
4
+ InternalInferenceCompletion,
5
+ InternalInferenceEvent,
6
+ InternalInferenceId,
7
+ InternalInferenceRun,
8
+ InternalInferenceRunSnapshot,
9
+ InternalInferenceSnapshot,
10
+ } from "../contracts.ts";
11
+ import type {
12
+ InternalInferenceAttemptRecord,
13
+ InternalInferenceChainRecord,
14
+ } from "../persistence/records.ts";
15
+
16
+ export class AttemptHandle implements InternalInference {
17
+ private readonly hub = new SubscriptionHub<InternalInferenceEvent>();
18
+ private current: InternalInferenceSnapshot;
19
+ private readonly deferred = promiseWithResolvers<InternalInferenceCompletion>();
20
+ readonly result = this.deferred.promise;
21
+
22
+ constructor(record: InternalInferenceAttemptRecord, private readonly cancelRun: (reason?: string) => Promise<void>) {
23
+ this.current = snapshotOf(record);
24
+ this.result.catch(() => {});
25
+ this.settle(record);
26
+ }
27
+
28
+ get id(): InternalInferenceId { return this.current.id; }
29
+ get snapshot(): InternalInferenceSnapshot { return this.current; }
30
+ subscribe(listener: (event: InternalInferenceEvent) => void): () => void { return this.hub.subscribe(listener); }
31
+ cancel(): Promise<void> { return this.cancelRun(); }
32
+ emit(event: InternalInferenceEvent): void { this.hub.emit(event); }
33
+ update(record: InternalInferenceAttemptRecord): void {
34
+ if (record.revision < this.current.revision) return;
35
+ this.current = snapshotOf(record);
36
+ this.settle(record);
37
+ }
38
+ setStage(stage: InternalInferenceSnapshot["stage"]): void {
39
+ if (this.current.status !== "running") return;
40
+ this.current = Object.freeze({ ...this.current, stage });
41
+ }
42
+ private settle(record: InternalInferenceAttemptRecord): void {
43
+ if (record.status === "completed" && record.completion) this.deferred.resolve(record.completion);
44
+ if (record.status === "failed" || record.status === "cancelled") {
45
+ this.deferred.reject(new Error(record.error ?? `internal inference ${record.status}`));
46
+ }
47
+ }
48
+ }
49
+
50
+ export class RunHandle implements InternalInferenceRun {
51
+ private readonly hub = new SubscriptionHub<InternalInferenceEvent>();
52
+ private current: InternalInferenceRunSnapshot;
53
+ private attempt: InternalInference;
54
+ private readonly deferred = promiseWithResolvers<InternalInferenceCompletion>();
55
+ readonly result = this.deferred.promise;
56
+
57
+ constructor(
58
+ chain: InternalInferenceChainRecord,
59
+ attempt: InternalInference,
60
+ private readonly cancelRun: (reason?: string) => Promise<void>,
61
+ ) {
62
+ this.current = runSnapshotOf(chain);
63
+ this.attempt = attempt;
64
+ this.result.catch(() => {});
65
+ this.settle(chain, attempt);
66
+ }
67
+ get rootId(): InternalInferenceId { return this.current.rootId; }
68
+ get activeAttempt(): InternalInference { return this.attempt; }
69
+ get snapshot(): InternalInferenceRunSnapshot { return this.current; }
70
+ subscribe(listener: (event: InternalInferenceEvent) => void): () => void { return this.hub.subscribe(listener); }
71
+ cancel(): Promise<void> { return this.cancelRun(); }
72
+ update(chain: InternalInferenceChainRecord, attempt: InternalInference): void {
73
+ if (chain.revision < this.current.revision) return;
74
+ this.current = runSnapshotOf(chain);
75
+ this.attempt = attempt;
76
+ this.settle(chain, attempt);
77
+ }
78
+ private settle(chain: InternalInferenceChainRecord, attempt: InternalInference): void {
79
+ if (chain.status === "completed") void attempt.result.then(this.deferred.resolve, this.deferred.reject);
80
+ if (chain.status === "failed" || chain.status === "cancelled") {
81
+ this.deferred.reject(new Error(chain.error ?? `internal inference run ${chain.status}`));
82
+ }
83
+ }
84
+ }
85
+
86
+ export function snapshotOf(record: InternalInferenceAttemptRecord): InternalInferenceSnapshot {
87
+ return Object.freeze({
88
+ id: record.internalInferenceId,
89
+ rootId: record.rootInternalInferenceId,
90
+ agentSessionId: record.agentSessionId,
91
+ ...(record.retryOfInternalInferenceId ? { retryOfId: record.retryOfInternalInferenceId } : {}),
92
+ revision: record.revision,
93
+ purpose: record.purpose,
94
+ status: record.status,
95
+ stage: record.status === "running" ? "prompting" : record.status,
96
+ adapterId: record.adapterId,
97
+ ...(record.agentConversationId ? { agentConversationId: record.agentConversationId } : {}),
98
+ ...(record.sourceCheckpointId ? { sourceCheckpointId: record.sourceCheckpointId } : {}),
99
+ ...(record.sourceFromSequence === undefined
100
+ ? {}
101
+ : { sourceFromSequence: record.sourceFromSequence }),
102
+ ...(record.sourceThroughSequence === undefined
103
+ ? {}
104
+ : { sourceThroughSequence: record.sourceThroughSequence }),
105
+ requestedAt: record.requestedAt,
106
+ ...(record.startedAt ? { startedAt: record.startedAt } : {}),
107
+ ...(record.completedAt ? { completedAt: record.completedAt } : {}),
108
+ attemptNumber: record.attemptNumber,
109
+ ...(record.error ? { error: record.error } : {}),
110
+ });
111
+ }
112
+
113
+ function runSnapshotOf(chain: InternalInferenceChainRecord): InternalInferenceRunSnapshot {
114
+ return Object.freeze({
115
+ rootId: chain.rootInternalInferenceId,
116
+ status: chain.status,
117
+ revision: chain.revision,
118
+ ...(chain.activeInternalInferenceId ? { activeAttemptId: chain.activeInternalInferenceId } : {}),
119
+ ...(chain.requestedThroughSequence === undefined ? {} : { requestedThroughSequence: chain.requestedThroughSequence }),
120
+ ...(chain.error ? { error: chain.error } : {}),
121
+ });
122
+ }
123
+
124
+ function promiseWithResolvers<T>(): {
125
+ promise: Promise<T>;
126
+ resolve: (value: T | PromiseLike<T>) => void;
127
+ reject: (reason?: unknown) => void;
128
+ } {
129
+ let resolve!: (value: T | PromiseLike<T>) => void;
130
+ let reject!: (reason?: unknown) => void;
131
+ const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej; });
132
+ return { promise, resolve, reject };
133
+ }