@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
@@ -7,6 +7,70 @@
7
7
 
8
8
  import type { SessionRecord, SessionStore } from "./session-store.ts";
9
9
  import type { SessionListEntry } from "./types.ts";
10
+ import {
11
+ resolveManagedPersistence,
12
+ enforceSessionPersistenceConstraints,
13
+ resolvePersistenceRoot,
14
+ type ManagedSessionPersistenceOptions,
15
+ type ResolvedAgentSessionPersistence,
16
+ type SessionPersistenceConstraints,
17
+ } from "./persistence/config.ts";
18
+ import { FileAgentSessionStore } from "./persistence/file-registry-store.ts";
19
+ import {
20
+ summarizeAgentSessionRecord,
21
+ type AgentSessionListPage,
22
+ type AgentSessionStore,
23
+ type AgentSessionSummary,
24
+ type ListAgentSessionsOptions,
25
+ type ListSessionMaintenanceOptions,
26
+ type SessionMaintenance,
27
+ type SessionMaintenancePage,
28
+ } from "./persistence/registry.ts";
29
+ import {
30
+ importLegacySessionRegistry,
31
+ type LegacyRegistryImportResult,
32
+ } from "./persistence/legacy-registry-import.ts";
33
+ import {
34
+ deleteManagedAgentSession,
35
+ type DeleteAgentSessionOptions,
36
+ } from "./persistence/deletion.ts";
37
+ import {
38
+ createManagedAgentSession,
39
+ type ManagedAgentSessionCreateConfig,
40
+ } from "./persistence/managed-creation.ts";
41
+ import type { AgentSession } from "./agent-session.ts";
42
+ import {
43
+ loadManagedAgentSession,
44
+ type ManagedAgentSessionLoadConfig,
45
+ } from "./persistence/managed-load.ts";
46
+ import type { AgentSessionOpenResult } from "./agent-session.ts";
47
+ import {
48
+ getManagedAgentSessionCheckpoint,
49
+ type GetAgentSessionCheckpointOptions,
50
+ type ResolvedAgentSessionCheckpoint,
51
+ } from "./persistence/managed-checkpoint.ts";
52
+ import {
53
+ reconfigureManagedAgentSessionPersistence,
54
+ type ReconfigureAgentSessionPersistenceOptions,
55
+ } from "./persistence/reconfiguration.ts";
56
+ import {
57
+ restoreManagedAgentSession,
58
+ type RestoreAgentSessionOptions,
59
+ } from "./persistence/restoration.ts";
60
+
61
+ export interface SessionManagerConfig {
62
+ readonly root?: string;
63
+ readonly store?: AgentSessionStore;
64
+ readonly sessionDefaults?: ManagedSessionPersistenceOptions;
65
+ readonly sessionConstraints?: SessionPersistenceConstraints;
66
+ }
67
+
68
+ export class SessionManagerModeError extends Error {
69
+ constructor(message: string) {
70
+ super(message);
71
+ this.name = "SessionManagerModeError";
72
+ }
73
+ }
10
74
 
11
75
  /** A row in the merged session view. `source` says where it was seen. */
12
76
  export interface MergedSession {
@@ -27,32 +91,58 @@ export interface MergedSession {
27
91
  export type SessionInput = Omit<SessionRecord, "createdAt" | "updatedAt">;
28
92
 
29
93
  export class SessionManager {
30
- constructor(private readonly store: SessionStore) {}
94
+ private readonly legacyStore: SessionStore | null;
95
+ private readonly agentStore: AgentSessionStore | null;
96
+ private readonly managedRoot: string | null;
97
+ private readonly sessionDefaults?: ManagedSessionPersistenceOptions;
98
+ private readonly sessionConstraints: SessionPersistenceConstraints;
99
+
100
+ constructor(store: SessionStore | AgentSessionStore, managedConfig?: SessionManagerConfig) {
101
+ if (isAgentSessionStore(store)) {
102
+ this.legacyStore = null;
103
+ this.agentStore = store;
104
+ this.managedRoot = resolvePersistenceRoot(managedConfig?.root);
105
+ this.sessionDefaults = managedConfig?.sessionDefaults;
106
+ this.sessionConstraints = managedConfig?.sessionConstraints ?? {};
107
+ } else {
108
+ this.legacyStore = store;
109
+ this.agentStore = null;
110
+ this.managedRoot = null;
111
+ this.sessionConstraints = {};
112
+ }
113
+ }
114
+
115
+ static async open(config: SessionManagerConfig = {}): Promise<SessionManager> {
116
+ const root = resolvePersistenceRoot(config.root);
117
+ const store = config.store ?? new FileAgentSessionStore({ root });
118
+ return new SessionManager(store, { ...config, root });
119
+ }
31
120
 
32
121
  /** Upsert a record: preserves createdAt, bumps updatedAt. Returns the stored record. */
33
122
  async record(input: SessionInput): Promise<SessionRecord> {
123
+ const store = this.requireLegacyStore("record()");
34
124
  const now = new Date().toISOString();
35
- const existing = await this.store.get(input.agentSessionId);
125
+ const existing = await store.get(input.agentSessionId);
36
126
  const record: SessionRecord = {
37
127
  ...input,
38
128
  createdAt: existing?.createdAt ?? now,
39
129
  updatedAt: now,
40
130
  };
41
- await this.store.save(record);
131
+ await store.save(record);
42
132
  return record;
43
133
  }
44
134
 
45
135
  get(agentSessionId: string): Promise<SessionRecord | null> {
46
- return this.store.get(agentSessionId);
136
+ return this.requireLegacyStore("get()").get(agentSessionId);
47
137
  }
48
138
 
49
139
  delete(agentSessionId: string): Promise<void> {
50
- return this.store.delete(agentSessionId);
140
+ return this.requireLegacyStore("delete()").delete(agentSessionId);
51
141
  }
52
142
 
53
143
  /** Catalog records, newest first. */
54
144
  async list(): Promise<SessionRecord[]> {
55
- const records = await this.store.list();
145
+ const records = await this.requireLegacyStore("list()").list();
56
146
  return records.sort((a, b) => (a.updatedAt < b.updatedAt ? 1 : -1));
57
147
  }
58
148
 
@@ -65,8 +155,168 @@ export class SessionManager {
65
155
  agentEntries: SessionListEntry[],
66
156
  adapter: string,
67
157
  ): Promise<MergedSession[]> {
68
- return mergeSessions(await this.store.list(), agentEntries, adapter);
158
+ return mergeSessions(await this.requireLegacyStore("listMerged()").list(), agentEntries, adapter);
159
+ }
160
+
161
+ async listAgentSessions(options?: ListAgentSessionsOptions): Promise<AgentSessionListPage> {
162
+ return await this.requireAgentStore("listAgentSessions()").listAgentSessions(options);
163
+ }
164
+
165
+ async getAgentSessionInfo(
166
+ agentSessionId: string,
167
+ options: { readonly includeDeleted?: boolean } = {},
168
+ ): Promise<AgentSessionSummary | null> {
169
+ const store = this.requireAgentStore("getAgentSessionInfo()");
170
+ const [record, maintenance] = await Promise.all([
171
+ store.getAgentSessionRecord(agentSessionId),
172
+ store.getMaintenance(agentSessionId),
173
+ ]);
174
+ if (!record) return null;
175
+ if (!options.includeDeleted && (record.deletion || maintenance?.kind === "deletion")) return null;
176
+ return summarizeAgentSessionRecord(record, maintenance ?? undefined);
177
+ }
178
+
179
+ async getSessionMaintenance(agentSessionId: string): Promise<SessionMaintenance | null> {
180
+ return await this.requireAgentStore("getSessionMaintenance()").getMaintenance(agentSessionId);
181
+ }
182
+
183
+ async listSessionMaintenance(options?: ListSessionMaintenanceOptions): Promise<SessionMaintenancePage> {
184
+ return await this.requireAgentStore("listSessionMaintenance()").listMaintenance(options);
185
+ }
186
+
187
+ /** Explicitly import a compatibility SessionStore after its transcripts were migrated. */
188
+ async importLegacySessions(source: SessionStore): Promise<LegacyRegistryImportResult> {
189
+ return importLegacySessionRegistry(source, this.requireAgentStore("importLegacySessions()"));
190
+ }
191
+
192
+ /** Tombstone a managed session and optionally delete its known persistence artifacts. */
193
+ async deleteAgentSession(
194
+ agentSessionId: string,
195
+ options: DeleteAgentSessionOptions,
196
+ ): Promise<void> {
197
+ await deleteManagedAgentSession(
198
+ this.requireAgentStore("deleteAgentSession()"),
199
+ agentSessionId,
200
+ options,
201
+ );
69
202
  }
203
+
204
+ /** Create a transcript-backed logical session under a prepared registry transaction. */
205
+ async createAgentSession(config: ManagedAgentSessionCreateConfig): Promise<AgentSession> {
206
+ const store = this.requireAgentStore("createAgentSession()");
207
+ return await createManagedAgentSession({
208
+ store,
209
+ persistence: this.resolveSessionPersistence(config.persistence),
210
+ }, config);
211
+ }
212
+
213
+ /** Load a cataloged logical session from its immutable persistence authority. */
214
+ async loadAgentSession(
215
+ agentSessionId: string,
216
+ config: ManagedAgentSessionLoadConfig,
217
+ ): Promise<AgentSessionOpenResult> {
218
+ await this.enforceRecordedConstraints(agentSessionId);
219
+ return await loadManagedAgentSession(
220
+ this.requireAgentStore("loadAgentSession()"),
221
+ agentSessionId,
222
+ config,
223
+ );
224
+ }
225
+
226
+ /** Resolve the current continuation checkpoint without starting its harness. */
227
+ async getAgentSessionCheckpoint(
228
+ agentSessionId: string,
229
+ options?: GetAgentSessionCheckpointOptions,
230
+ ): Promise<ResolvedAgentSessionCheckpoint | null> {
231
+ return await getManagedAgentSessionCheckpoint(
232
+ this.requireAgentStore("getAgentSessionCheckpoint()"),
233
+ agentSessionId,
234
+ options,
235
+ );
236
+ }
237
+
238
+ /** Replace or disable stopped-session persistence under a prepared transaction. */
239
+ async reconfigureAgentSessionPersistence(
240
+ agentSessionId: string,
241
+ options: ReconfigureAgentSessionPersistenceOptions,
242
+ ): Promise<AgentSessionSummary> {
243
+ const record = await this.requireAgentStore("reconfigureAgentSessionPersistence()")
244
+ .getAgentSessionRecord(agentSessionId);
245
+ if (record) {
246
+ enforceSessionPersistenceConstraints({
247
+ transcript: options.transcript
248
+ ? options.transcript.action === "replace"
249
+ : record.transcript !== null && record.deletion?.transcriptDeletedAt === undefined,
250
+ auditLog: options.auditLog
251
+ ? options.auditLog.action === "replace"
252
+ : record.auditLog !== null && record.deletion?.auditLogDeletedAt === undefined,
253
+ }, this.sessionConstraints);
254
+ }
255
+ return await reconfigureManagedAgentSessionPersistence(
256
+ this.requireAgentStore("reconfigureAgentSessionPersistence()"),
257
+ agentSessionId,
258
+ options,
259
+ );
260
+ }
261
+
262
+ /** Explicitly reactivate a tombstoned logical session. */
263
+ async restoreAgentSession(
264
+ agentSessionId: string,
265
+ options: RestoreAgentSessionOptions,
266
+ ): Promise<AgentSessionOpenResult> {
267
+ const record = await this.requireAgentStore("restoreAgentSession()").getAgentSessionRecord(agentSessionId);
268
+ if (record) {
269
+ const transcriptRetained = record.transcript !== null
270
+ && record.deletion?.transcriptDeletedAt === undefined;
271
+ const auditRetained = record.auditLog !== null
272
+ && record.deletion?.auditLogDeletedAt === undefined;
273
+ enforceSessionPersistenceConstraints({
274
+ transcript: transcriptRetained || Boolean(options.replacementTranscript),
275
+ auditLog: auditRetained || Boolean(options.replacementAuditLog),
276
+ }, this.sessionConstraints);
277
+ }
278
+ return await restoreManagedAgentSession(
279
+ this.requireAgentStore("restoreAgentSession()"),
280
+ agentSessionId,
281
+ options,
282
+ );
283
+ }
284
+
285
+ private async enforceRecordedConstraints(agentSessionId: string): Promise<void> {
286
+ const record = await this.requireAgentStore("managed persistence constraints").getAgentSessionRecord(agentSessionId);
287
+ if (!record) return;
288
+ enforceSessionPersistenceConstraints({
289
+ transcript: record.transcript !== null && record.deletion?.transcriptDeletedAt === undefined,
290
+ auditLog: record.auditLog !== null && record.deletion?.auditLogDeletedAt === undefined,
291
+ }, this.sessionConstraints);
292
+ }
293
+
294
+ /** Shared normalization for managed creation and per-session persistence policy. */
295
+ resolveSessionPersistence(
296
+ options?: ManagedSessionPersistenceOptions,
297
+ ): ResolvedAgentSessionPersistence {
298
+ if (!this.managedRoot) throw new SessionManagerModeError("resolveSessionPersistence() requires a SessionManager created through SessionManager.open()");
299
+ return resolveManagedPersistence(
300
+ this.managedRoot,
301
+ this.sessionDefaults,
302
+ options,
303
+ this.sessionConstraints,
304
+ );
305
+ }
306
+
307
+ private requireLegacyStore(operation: string): SessionStore {
308
+ if (!this.legacyStore) throw new SessionManagerModeError(`${operation} is available only on compatibility SessionManager instances`);
309
+ return this.legacyStore;
310
+ }
311
+
312
+ private requireAgentStore(operation: string): AgentSessionStore {
313
+ if (!this.agentStore) throw new SessionManagerModeError(`${operation} requires a SessionManager created through SessionManager.open()`);
314
+ return this.agentStore;
315
+ }
316
+ }
317
+
318
+ function isAgentSessionStore(store: SessionStore | AgentSessionStore): store is AgentSessionStore {
319
+ return "getAgentSessionRecord" in store && typeof store.getAgentSessionRecord === "function";
70
320
  }
71
321
 
72
322
  /** Pure merge — exported for testing. */
package/src/transcript.ts CHANGED
@@ -204,7 +204,7 @@ export async function readTranscriptLines(
204
204
 
205
205
  const out: TranscriptLine[] = [];
206
206
  for (const raw of content.split(/\r?\n/)) {
207
- const line = parseTranscriptLine(raw, options.before, types);
207
+ const line = parseLegacyTranscriptLine(raw, options.before, types);
208
208
  if (line) out.push(line);
209
209
  }
210
210
  return out;
@@ -298,7 +298,7 @@ async function readTail(
298
298
 
299
299
  const raws = decoder.decode(body).split(/\r?\n/);
300
300
  for (let i = raws.length - 1; i >= 0 && newestFirst.length < limit; i--) {
301
- const line = parseTranscriptLine(raws[i]!, before, types);
301
+ const line = parseLegacyTranscriptLine(raws[i]!, before, types);
302
302
  if (line) newestFirst.push(line);
303
303
  }
304
304
  }
@@ -316,7 +316,7 @@ async function readTail(
316
316
  * paths tally what this returns, so narrowing `types` narrows what the budget is spent on
317
317
  * without either loop knowing about it.
318
318
  */
319
- function parseTranscriptLine(
319
+ export function parseLegacyTranscriptLine(
320
320
  raw: string,
321
321
  before?: number,
322
322
  types?: Set<string>,