@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,304 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
restoreMeteringState,
|
|
4
|
+
type AgentSessionMeteringState,
|
|
5
|
+
} from "../metering/state.ts";
|
|
6
|
+
import { checkpointDigest, checkpointDigestMatches, PersistenceSerializationError } from "./integrity.ts";
|
|
7
|
+
|
|
8
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | JsonObject;
|
|
9
|
+
export interface JsonObject { [key: string]: JsonValue }
|
|
10
|
+
|
|
11
|
+
export type TransitionKind = "handoff" | "switch" | "mode_change" | "replacement";
|
|
12
|
+
export type PersistedProtocol = { name: "acp"; version: number } | { name: "pty" };
|
|
13
|
+
|
|
14
|
+
export interface PersistedSessionConfigSelection {
|
|
15
|
+
readonly configId: string;
|
|
16
|
+
readonly type: "select" | "boolean" | "unknown";
|
|
17
|
+
readonly value: JsonValue;
|
|
18
|
+
readonly ordinal: number;
|
|
19
|
+
readonly source: "explicit_selection" | "accepted_effective";
|
|
20
|
+
readonly recordedAt: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SessionConfigurationCheckpoint {
|
|
24
|
+
readonly adapterId: string;
|
|
25
|
+
readonly schemaDigest: string | null;
|
|
26
|
+
readonly announcedConfigIds: readonly string[];
|
|
27
|
+
readonly restorationTargets: readonly PersistedSessionConfigSelection[];
|
|
28
|
+
readonly effective: Readonly<Record<string, JsonValue>>;
|
|
29
|
+
readonly omittedConfigIds: readonly string[];
|
|
30
|
+
readonly restorationFidelity: "exact" | "partial";
|
|
31
|
+
readonly observedAt: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type AgentSessionBoundary =
|
|
35
|
+
| { readonly status: "settled"; readonly lastSettledOperationId: string | null }
|
|
36
|
+
| {
|
|
37
|
+
readonly status: "in_flight";
|
|
38
|
+
readonly lastSettledOperationId: string | null;
|
|
39
|
+
readonly interruptedOperationId: string;
|
|
40
|
+
readonly cause:
|
|
41
|
+
| "operation_started"
|
|
42
|
+
| "transport_error"
|
|
43
|
+
| "harness_exit"
|
|
44
|
+
| "host_interrupt"
|
|
45
|
+
| "restored_after_unclean_exit";
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export interface AgentSessionCheckpoint {
|
|
49
|
+
readonly version: 1;
|
|
50
|
+
readonly agentSessionId: string;
|
|
51
|
+
readonly incarnationId: string;
|
|
52
|
+
readonly stateRevision: number;
|
|
53
|
+
readonly digest: string;
|
|
54
|
+
readonly adapterId: string;
|
|
55
|
+
readonly mode: "normal" | "degraded";
|
|
56
|
+
readonly internalSessionId: string | null;
|
|
57
|
+
readonly internalSessionActivatedAt: string | null;
|
|
58
|
+
readonly internalSessionHistoryRevision: number;
|
|
59
|
+
readonly internalSessionHistoryDigest: string;
|
|
60
|
+
readonly lastTransition?: {
|
|
61
|
+
readonly transitionId: string;
|
|
62
|
+
readonly kind: TransitionKind;
|
|
63
|
+
readonly at: string;
|
|
64
|
+
};
|
|
65
|
+
readonly model: string | null;
|
|
66
|
+
readonly sessionConfiguration: SessionConfigurationCheckpoint;
|
|
67
|
+
readonly protocol: PersistedProtocol;
|
|
68
|
+
readonly transcript: null | {
|
|
69
|
+
readonly transcriptId: string;
|
|
70
|
+
readonly generation: number;
|
|
71
|
+
readonly path: string;
|
|
72
|
+
readonly coveredSequence: number;
|
|
73
|
+
};
|
|
74
|
+
readonly metering?: AgentSessionMeteringState;
|
|
75
|
+
readonly boundary: AgentSessionBoundary;
|
|
76
|
+
readonly updatedAt: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export type AgentSessionCheckpointInput = Omit<AgentSessionCheckpoint, "version" | "digest">;
|
|
80
|
+
|
|
81
|
+
export interface CheckpointIdentityExpectation {
|
|
82
|
+
readonly agentSessionId?: string;
|
|
83
|
+
readonly incarnationId?: string;
|
|
84
|
+
readonly adapterId?: string;
|
|
85
|
+
readonly transcriptId?: string;
|
|
86
|
+
readonly transcriptGeneration?: number;
|
|
87
|
+
readonly transcriptPath?: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export class CheckpointValidationError extends Error {
|
|
91
|
+
constructor(message: string) {
|
|
92
|
+
super(message);
|
|
93
|
+
this.name = "CheckpointValidationError";
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function createAgentSessionCheckpoint(
|
|
98
|
+
input: AgentSessionCheckpointInput,
|
|
99
|
+
): AgentSessionCheckpoint {
|
|
100
|
+
const withoutDigest = { version: 1 as const, ...structuredClone(input) };
|
|
101
|
+
const checkpoint = {
|
|
102
|
+
...withoutDigest,
|
|
103
|
+
digest: checkpointDigest(withoutDigest as unknown as Record<string, unknown>),
|
|
104
|
+
};
|
|
105
|
+
validateAgentSessionCheckpoint(checkpoint);
|
|
106
|
+
return checkpoint;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function validateAgentSessionCheckpoint(
|
|
110
|
+
value: unknown,
|
|
111
|
+
expected: CheckpointIdentityExpectation = {},
|
|
112
|
+
): asserts value is AgentSessionCheckpoint {
|
|
113
|
+
if (!isRecord(value)) throw new CheckpointValidationError("checkpoint must be an object");
|
|
114
|
+
if (value.version !== 1) throw new CheckpointValidationError("unsupported checkpoint version");
|
|
115
|
+
nonblank(value.agentSessionId, "agentSessionId");
|
|
116
|
+
nonblank(value.incarnationId, "incarnationId");
|
|
117
|
+
nonblank(value.adapterId, "adapterId");
|
|
118
|
+
if (value.mode !== "normal" && value.mode !== "degraded") {
|
|
119
|
+
throw new CheckpointValidationError("mode is invalid");
|
|
120
|
+
}
|
|
121
|
+
positiveInteger(value.stateRevision, "stateRevision");
|
|
122
|
+
nonnegativeInteger(value.internalSessionHistoryRevision, "internalSessionHistoryRevision");
|
|
123
|
+
sha256(value.internalSessionHistoryDigest, "internalSessionHistoryDigest");
|
|
124
|
+
nullableNonblank(value.internalSessionId, "internalSessionId");
|
|
125
|
+
nullableIso(value.internalSessionActivatedAt, "internalSessionActivatedAt");
|
|
126
|
+
nullableNonblank(value.model, "model");
|
|
127
|
+
iso(value.updatedAt, "updatedAt");
|
|
128
|
+
validateProtocol(value.protocol);
|
|
129
|
+
validateBoundary(value.boundary);
|
|
130
|
+
validateSessionConfigurationCheckpoint(value.sessionConfiguration, value.adapterId as string);
|
|
131
|
+
validateTranscript(value.transcript);
|
|
132
|
+
validateLastTransition(value.lastTransition);
|
|
133
|
+
validateMetering(value.metering, value.agentSessionId as string);
|
|
134
|
+
|
|
135
|
+
if ((value.internalSessionId === null) !== (value.internalSessionActivatedAt === null)) {
|
|
136
|
+
throw new CheckpointValidationError(
|
|
137
|
+
"internalSessionId and internalSessionActivatedAt must both be null or both be present",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
if (typeof value.internalSessionActivatedAt === "string") {
|
|
141
|
+
timestampNotAfter(value.internalSessionActivatedAt, value.updatedAt as string, "internalSessionActivatedAt");
|
|
142
|
+
}
|
|
143
|
+
if (isRecord(value.lastTransition)) {
|
|
144
|
+
timestampNotAfter(value.lastTransition.at as string, value.updatedAt as string, "lastTransition.at");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
if (!checkpointDigestMatches(value)) throw new CheckpointValidationError("checkpoint digest mismatch");
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if (error instanceof CheckpointValidationError) throw error;
|
|
151
|
+
if (error instanceof PersistenceSerializationError) {
|
|
152
|
+
throw new CheckpointValidationError(`checkpoint is not canonical JSON: ${error.message}`);
|
|
153
|
+
}
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
equalExpected(value.agentSessionId, expected.agentSessionId, "agentSessionId");
|
|
158
|
+
equalExpected(value.incarnationId, expected.incarnationId, "incarnationId");
|
|
159
|
+
equalExpected(value.adapterId, expected.adapterId, "adapterId");
|
|
160
|
+
if (expected.transcriptId !== undefined || expected.transcriptGeneration !== undefined || expected.transcriptPath !== undefined) {
|
|
161
|
+
if (!isRecord(value.transcript)) throw new CheckpointValidationError("checkpoint has no transcript identity");
|
|
162
|
+
equalExpected(value.transcript.transcriptId, expected.transcriptId, "transcriptId");
|
|
163
|
+
equalExpected(value.transcript.generation, expected.transcriptGeneration, "transcript generation");
|
|
164
|
+
equalExpected(value.transcript.path, expected.transcriptPath, "transcript path");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function validateSessionConfigurationCheckpoint(value: unknown, adapterId: string): asserts value is SessionConfigurationCheckpoint {
|
|
169
|
+
if (!isRecord(value)) throw new CheckpointValidationError("sessionConfiguration must be an object");
|
|
170
|
+
if (value.adapterId !== adapterId) throw new CheckpointValidationError("sessionConfiguration adapterId mismatch");
|
|
171
|
+
if (value.schemaDigest !== null) sha256(value.schemaDigest, "sessionConfiguration.schemaDigest");
|
|
172
|
+
uniqueStringArray(value.announcedConfigIds, "sessionConfiguration.announcedConfigIds");
|
|
173
|
+
uniqueStringArray(value.omittedConfigIds, "sessionConfiguration.omittedConfigIds");
|
|
174
|
+
if (!Array.isArray(value.restorationTargets)) throw new CheckpointValidationError("sessionConfiguration.restorationTargets must be an array");
|
|
175
|
+
if (!isRecord(value.effective)) throw new CheckpointValidationError("sessionConfiguration.effective must be an object");
|
|
176
|
+
if (value.restorationFidelity !== "exact" && value.restorationFidelity !== "partial") throw new CheckpointValidationError("sessionConfiguration.restorationFidelity is invalid");
|
|
177
|
+
iso(value.observedAt, "sessionConfiguration.observedAt");
|
|
178
|
+
|
|
179
|
+
const targetIds = new Set<string>();
|
|
180
|
+
let previousOrdinal = -1;
|
|
181
|
+
for (const [index, target] of value.restorationTargets.entries()) {
|
|
182
|
+
if (!isRecord(target)) {
|
|
183
|
+
throw new CheckpointValidationError(`sessionConfiguration.restorationTargets[${index}] must be an object`);
|
|
184
|
+
}
|
|
185
|
+
nonblank(target.configId, `sessionConfiguration.restorationTargets[${index}].configId`);
|
|
186
|
+
if (targetIds.has(target.configId)) {
|
|
187
|
+
throw new CheckpointValidationError("sessionConfiguration.restorationTargets contains duplicate configId");
|
|
188
|
+
}
|
|
189
|
+
targetIds.add(target.configId);
|
|
190
|
+
if (!(["select", "boolean", "unknown"] as unknown[]).includes(target.type)) {
|
|
191
|
+
throw new CheckpointValidationError(`sessionConfiguration.restorationTargets[${index}].type is invalid`);
|
|
192
|
+
}
|
|
193
|
+
nonnegativeInteger(target.ordinal, `sessionConfiguration.restorationTargets[${index}].ordinal`);
|
|
194
|
+
if ((target.ordinal as number) <= previousOrdinal) {
|
|
195
|
+
throw new CheckpointValidationError("sessionConfiguration.restorationTargets ordinals must be strictly increasing");
|
|
196
|
+
}
|
|
197
|
+
previousOrdinal = target.ordinal as number;
|
|
198
|
+
if (target.source !== "explicit_selection" && target.source !== "accepted_effective") {
|
|
199
|
+
throw new CheckpointValidationError(`sessionConfiguration.restorationTargets[${index}].source is invalid`);
|
|
200
|
+
}
|
|
201
|
+
iso(target.recordedAt, `sessionConfiguration.restorationTargets[${index}].recordedAt`);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const announced = new Set(value.announcedConfigIds as string[]);
|
|
205
|
+
for (const omitted of value.omittedConfigIds as string[]) {
|
|
206
|
+
if (!announced.has(omitted)) {
|
|
207
|
+
throw new CheckpointValidationError("sessionConfiguration.omittedConfigIds must be announced");
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (value.restorationFidelity === "exact" && (value.omittedConfigIds as string[]).length > 0) {
|
|
211
|
+
throw new CheckpointValidationError("exact session configuration cannot contain omittedConfigIds");
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function validateMetering(value: unknown, agentSessionId: string): void {
|
|
216
|
+
if (value === undefined) return;
|
|
217
|
+
const restored = restoreMeteringState(value as AgentSessionMeteringState, agentSessionId);
|
|
218
|
+
if (restored.status !== "restored") {
|
|
219
|
+
const reason = restored.status === "ignored" ? restored.reason : "initial_state_missing";
|
|
220
|
+
throw new CheckpointValidationError(`metering state is invalid: ${reason}`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function validateTranscript(value: unknown): void {
|
|
225
|
+
if (value === null) return;
|
|
226
|
+
if (!isRecord(value)) throw new CheckpointValidationError("transcript must be null or an object");
|
|
227
|
+
nonblank(value.transcriptId, "transcript.transcriptId");
|
|
228
|
+
positiveInteger(value.generation, "transcript.generation");
|
|
229
|
+
if (typeof value.path !== "string" || !isAbsolute(value.path)) throw new CheckpointValidationError("transcript.path must be absolute");
|
|
230
|
+
nonnegativeInteger(value.coveredSequence, "transcript.coveredSequence");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function validateProtocol(value: unknown): void {
|
|
234
|
+
if (!isRecord(value)) throw new CheckpointValidationError("protocol must be an object");
|
|
235
|
+
if (value.name === "pty") return;
|
|
236
|
+
if (value.name !== "acp") throw new CheckpointValidationError("protocol.name is invalid");
|
|
237
|
+
positiveInteger(value.version, "protocol.version");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function validateBoundary(value: unknown): void {
|
|
241
|
+
if (!isRecord(value)) throw new CheckpointValidationError("boundary must be an object");
|
|
242
|
+
nullableNonblank(value.lastSettledOperationId, "boundary.lastSettledOperationId");
|
|
243
|
+
if (value.status === "settled") return;
|
|
244
|
+
if (value.status !== "in_flight") throw new CheckpointValidationError("boundary.status is invalid");
|
|
245
|
+
nonblank(value.interruptedOperationId, "boundary.interruptedOperationId");
|
|
246
|
+
if (!["operation_started", "transport_error", "harness_exit", "host_interrupt", "restored_after_unclean_exit"].includes(value.cause as string)) {
|
|
247
|
+
throw new CheckpointValidationError("boundary.cause is invalid");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function validateLastTransition(value: unknown): void {
|
|
252
|
+
if (value === undefined) return;
|
|
253
|
+
if (!isRecord(value)) throw new CheckpointValidationError("lastTransition must be an object");
|
|
254
|
+
nonblank(value.transitionId, "lastTransition.transitionId");
|
|
255
|
+
if (!["handoff", "switch", "mode_change", "replacement"].includes(value.kind as string)) throw new CheckpointValidationError("lastTransition.kind is invalid");
|
|
256
|
+
iso(value.at, "lastTransition.at");
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function equalExpected(actual: unknown, expected: unknown, label: string): void {
|
|
260
|
+
if (expected !== undefined && actual !== expected) throw new CheckpointValidationError(`${label} mismatch`);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function nonblank(value: unknown, label: string): asserts value is string {
|
|
264
|
+
if (typeof value !== "string" || !value.trim()) throw new CheckpointValidationError(`${label} must be nonblank`);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function nullableNonblank(value: unknown, label: string): void {
|
|
268
|
+
if (value !== null) nonblank(value, label);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function uniqueStringArray(value: unknown, label: string): void {
|
|
272
|
+
if (!Array.isArray(value) || value.some((item) => typeof item !== "string" || !item.trim())) throw new CheckpointValidationError(`${label} must contain nonblank strings`);
|
|
273
|
+
if (new Set(value).size !== value.length) throw new CheckpointValidationError(`${label} must not contain duplicates`);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function positiveInteger(value: unknown, label: string): void {
|
|
277
|
+
if (!Number.isSafeInteger(value) || (value as number) < 1) throw new CheckpointValidationError(`${label} must be a positive safe integer`);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function nonnegativeInteger(value: unknown, label: string): void {
|
|
281
|
+
if (!Number.isSafeInteger(value) || (value as number) < 0) throw new CheckpointValidationError(`${label} must be a nonnegative safe integer`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function sha256(value: unknown, label: string): void {
|
|
285
|
+
if (typeof value !== "string" || !/^[0-9a-f]{64}$/.test(value)) throw new CheckpointValidationError(`${label} must be lowercase SHA-256`);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function nullableIso(value: unknown, label: string): void {
|
|
289
|
+
if (value !== null) iso(value, label);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function iso(value: unknown, label: string): void {
|
|
293
|
+
if (typeof value !== "string" || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString() !== value) throw new CheckpointValidationError(`${label} must be canonical ISO-8601`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function timestampNotAfter(value: string, upperBound: string, label: string): void {
|
|
297
|
+
if (Date.parse(value) > Date.parse(upperBound)) {
|
|
298
|
+
throw new CheckpointValidationError(`${label} must not be after updatedAt`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
303
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
304
|
+
}
|