@qlairoslabs/acp-client 0.9.4 → 0.10.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.
@@ -0,0 +1,46 @@
1
+ import type { AgentMode } from "../controller.ts";
2
+
3
+ export interface ProviderAttachmentDescriptor {
4
+ readonly attachmentAttemptId: string;
5
+ readonly internalSessionId: string;
6
+ readonly adapterId: string;
7
+ readonly mode: AgentMode;
8
+ readonly model?: string;
9
+ readonly acquiredAt: string;
10
+ readonly providerSessionRecordId?: string;
11
+ readonly transitionId?: string;
12
+ }
13
+
14
+ export type ProviderAttachmentState =
15
+ | { readonly kind: "absent" }
16
+ | { readonly kind: "runtime_only"; readonly descriptor: ProviderAttachmentDescriptor }
17
+ | { readonly kind: "recorded"; readonly descriptor: ProviderAttachmentDescriptor };
18
+
19
+ export interface ProviderAttachmentCommitAcknowledgement {
20
+ readonly attachmentAttemptId: string;
21
+ readonly providerSessionRecordId: string;
22
+ }
23
+
24
+ export function runtimeOnlyProviderAttachment(
25
+ descriptor: ProviderAttachmentDescriptor,
26
+ ): ProviderAttachmentState {
27
+ return Object.freeze({ kind: "runtime_only", descriptor: Object.freeze({ ...descriptor }) });
28
+ }
29
+
30
+ export function recordedProviderAttachment(
31
+ descriptor: ProviderAttachmentDescriptor,
32
+ ): ProviderAttachmentState {
33
+ return Object.freeze({ kind: "recorded", descriptor: Object.freeze({ ...descriptor }) });
34
+ }
35
+
36
+ export function acknowledgeProviderAttachment(
37
+ state: ProviderAttachmentState,
38
+ acknowledgement: ProviderAttachmentCommitAcknowledgement,
39
+ ): ProviderAttachmentState {
40
+ if (state.kind !== "runtime_only") return state;
41
+ if (state.descriptor.attachmentAttemptId !== acknowledgement.attachmentAttemptId) return state;
42
+ return recordedProviderAttachment({
43
+ ...state.descriptor,
44
+ providerSessionRecordId: acknowledgement.providerSessionRecordId,
45
+ });
46
+ }
@@ -221,7 +221,12 @@ export function reduceTranscriptState(
221
221
  }
222
222
  pendingRepair = null;
223
223
  }
224
- if (!expectedRepair) assertRoutingStable(candidate, checkpoint);
224
+ // A terminal checkpoint may fill in a thread slot the conversation never had. That is
225
+ // the first attachment, not a routing change: nothing is superseded, so there is no
226
+ // `from` descriptor to validate and no history entry to write.
227
+ if (!expectedRepair) {
228
+ assertRoutingStable(candidate, checkpoint, active?.terminal === "settled");
229
+ }
225
230
  if (active) {
226
231
  if (active.terminal) {
227
232
  if (candidate.stateRevision <= checkpoint.stateRevision) {
@@ -422,12 +427,20 @@ function assertCheckpointIdentityStable(
422
427
  function assertRoutingStable(
423
428
  candidate: AgentSessionCheckpoint,
424
429
  previous: AgentSessionCheckpoint,
430
+ allowInitialAttachment = false,
425
431
  ): void {
432
+ // A thread slot that was never filled can be filled by the checkpoint that settles the
433
+ // turn justifying it. Replacing an already selected thread stays a transition.
434
+ // The checkpoint constructor already refuses an id without its activation time, so
435
+ // exempting both fields together cannot smuggle a half-formed attachment through.
436
+ const attaching = allowInitialAttachment
437
+ && previous.internalSessionId === null
438
+ && candidate.internalSessionId !== null;
426
439
  const candidateRouting = {
427
440
  adapterId: candidate.adapterId,
428
441
  mode: candidate.mode,
429
- internalSessionId: candidate.internalSessionId,
430
- internalSessionActivatedAt: candidate.internalSessionActivatedAt,
442
+ internalSessionId: attaching ? null : candidate.internalSessionId,
443
+ internalSessionActivatedAt: attaching ? null : candidate.internalSessionActivatedAt,
431
444
  protocol: candidate.protocol,
432
445
  lastTransition: candidate.lastTransition ?? null,
433
446
  };
@@ -135,6 +135,9 @@ export class RegistryPersistenceRuntime implements PersistenceStateRuntime {
135
135
  ts = Date.now(),
136
136
  ): Promise<AgentSessionCheckpoint> {
137
137
  const active = this.requireActive();
138
+ if (changes.providerAttachment && this.current.internalSessionId !== null) {
139
+ throw new Error("replacing a selected provider thread is a transition, not a terminal checkpoint change");
140
+ }
138
141
  this.current = nextCheckpoint(this.current, {
139
142
  ...changes,
140
143
  boundary: changes.boundary ?? { status: "settled", lastSettledOperationId: active.operationId },
@@ -180,6 +183,10 @@ function nextCheckpoint(
180
183
  ...(changes.model === undefined ? {} : { model: changes.model }),
181
184
  ...(changes.sessionConfiguration === undefined ? {} : { sessionConfiguration: changes.sessionConfiguration }),
182
185
  ...(changes.metering === undefined ? {} : { metering: changes.metering }),
186
+ ...(changes.providerAttachment === undefined ? {} : {
187
+ internalSessionId: changes.providerAttachment.internalSessionId,
188
+ internalSessionActivatedAt: changes.providerAttachment.activatedAt,
189
+ }),
183
190
  stateRevision: source.stateRevision + 1,
184
191
  boundary: changes.boundary ?? source.boundary,
185
192
  updatedAt: new Date(ts).toISOString(),
@@ -43,6 +43,18 @@ export interface TranscriptTerminalCheckpointChanges {
43
43
  readonly sessionConfiguration?: SessionConfigurationCheckpoint;
44
44
  readonly metering?: AgentSessionCheckpoint["metering"];
45
45
  readonly boundary?: AgentSessionBoundary;
46
+ /**
47
+ * Fill in a thread slot the conversation never had.
48
+ *
49
+ * A newly created provider thread is kept in memory until a completed turn justifies
50
+ * selecting it for a later resume; the turn's own terminal checkpoint is what records
51
+ * it, so the response and the thread it came from commit together. Replacing an already
52
+ * selected thread is a transition and is rejected here.
53
+ */
54
+ readonly providerAttachment?: {
55
+ readonly internalSessionId: string;
56
+ readonly activatedAt: string;
57
+ };
46
58
  }
47
59
 
48
60
  type NormalizedTransitionEvent = Extract<NormalizedPersistenceEvent, { kind: "transition" }>;
@@ -452,6 +464,11 @@ export class TranscriptPersistenceRuntime implements PersistenceStateRuntime {
452
464
  ): Promise<AgentSessionCheckpoint> {
453
465
  this.assertUsable();
454
466
  const active = this.requireActive();
467
+ if (changes.providerAttachment && this._checkpoint.internalSessionId !== null) {
468
+ throw new TranscriptRuntimeStateError(
469
+ "replacing a selected provider thread is a transition, not a terminal checkpoint change",
470
+ );
471
+ }
455
472
  const terminal: TranscriptOperationRecord = {
456
473
  ...this.envelope(this.writer.nextSequence, ts),
457
474
  t: "operation",
@@ -469,6 +486,10 @@ export class TranscriptPersistenceRuntime implements PersistenceStateRuntime {
469
486
  ? {}
470
487
  : { sessionConfiguration: structuredClone(changes.sessionConfiguration) }),
471
488
  ...(changes.metering === undefined ? {} : { metering: structuredClone(changes.metering) }),
489
+ ...(changes.providerAttachment === undefined ? {} : {
490
+ internalSessionId: changes.providerAttachment.internalSessionId,
491
+ internalSessionActivatedAt: changes.providerAttachment.activatedAt,
492
+ }),
472
493
  stateRevision: this._checkpoint.stateRevision + 1,
473
494
  transcript: { ...this._checkpoint.transcript!, coveredSequence: terminal.seq },
474
495
  boundary: changes.boundary ?? { status: "settled", lastSettledOperationId: active.operationId },