@qlairoslabs/acp-client 0.9.3 → 0.9.4

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/CONSUMER_GUIDE.md CHANGED
@@ -92,6 +92,8 @@ try {
92
92
  The user message and each assistant-message boundary are stored as separate typed rows.
93
93
  Reopen the same database with `Agent.open()` and call
94
94
  `agent.conversations.open(agentConversationId)` to continue its active provider thread.
95
+ `Agent.close()` interrupts live prompts and waits for their finalization and queued
96
+ conversation writes before closing the SQLite backend; an interrupted prompt may reject.
95
97
 
96
98
  ### Attach consumer data to a prompt message
97
99
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qlairoslabs/acp-client",
3
- "version": "0.9.3",
3
+ "version": "0.9.4",
4
4
  "description": "Embeddable Agent Client Protocol (ACP) client library with degraded PTY fallback.",
5
5
  "type": "module",
6
6
  "module": "src/index.ts",
@@ -6,6 +6,7 @@
6
6
  export class AgentConversationQueue<T> {
7
7
  readonly #items: T[] = [];
8
8
  #draining = false;
9
+ #activeDrain: Promise<void> | null = null;
9
10
 
10
11
  get queuedCount(): number {
11
12
  return this.#items.length;
@@ -25,20 +26,37 @@ export class AgentConversationQueue<T> {
25
26
  }
26
27
 
27
28
  /**
28
- * Drain every item serially. Concurrent calls share the active drain by becoming
29
- * no-ops; an item enqueued by an active operation is observed by the same loop.
29
+ * Drain every item serially. Concurrent calls share the active drain; an item
30
+ * enqueued by an active operation is observed by the same loop.
30
31
  */
31
- async drain(execute: (item: T) => Promise<void>): Promise<void> {
32
- if (this.#draining) return;
32
+ drain(execute: (item: T) => Promise<void>): Promise<void> {
33
+ if (this.#activeDrain) return this.#activeDrain;
33
34
  this.#draining = true;
34
- try {
35
- while (this.#items.length > 0) {
36
- const item = this.#items.shift()!;
37
- await execute(item);
35
+ let resolveDrain!: () => void;
36
+ let rejectDrain!: (error: unknown) => void;
37
+ const active = new Promise<void>((resolve, reject) => {
38
+ resolveDrain = resolve;
39
+ rejectDrain = reject;
40
+ });
41
+ // Install the waitable promise before invoking execute: callbacks may call
42
+ // stop() synchronously while the first item is being started.
43
+ this.#activeDrain = active;
44
+ void (async () => {
45
+ try {
46
+ while (this.#items.length > 0) {
47
+ const item = this.#items.shift()!;
48
+ await execute(item);
49
+ }
50
+ } finally {
51
+ this.#draining = false;
52
+ this.#activeDrain = null;
38
53
  }
39
- } finally {
40
- this.#draining = false;
41
- }
54
+ })().then(resolveDrain, rejectDrain);
55
+ return active;
42
56
  }
43
- }
44
57
 
58
+ /** Wait for the active executor, including its current item's finalization. */
59
+ waitForIdle(): Promise<void> {
60
+ return this.#activeDrain ?? Promise.resolve();
61
+ }
62
+ }
@@ -423,6 +423,7 @@ export class AgentConversation implements AgentClient, Bridgeable {
423
423
  private readonly log: Logger;
424
424
  private transcriptWrite: Promise<void> = Promise.resolve();
425
425
  private readonly operations = new AgentConversationQueue<QueueItem>();
426
+ private stopPromise: Promise<void> | null = null;
426
427
  private phase: AgentSessionPhase = "idle";
427
428
  private notificationSeq = 0;
428
429
  private readonly listeners = new Set<AgentSessionListener>();
@@ -3475,20 +3476,51 @@ export class AgentConversation implements AgentClient, Bridgeable {
3475
3476
  this.controller.interrupt(options);
3476
3477
  }
3477
3478
 
3478
- async stop(): Promise<void> {
3479
- if (this.phase === "stopped") return;
3480
- // Stop is terminal for this stable object. Drop work that never started, interrupt the
3481
- // active turn, and publish stopped before awaiting process teardown so a concurrent
3482
- // prompt cannot slip into a controller that is going away.
3483
- this.interrupt({ clearQueue: true });
3479
+ stop(): Promise<void> {
3480
+ if (this.stopPromise) return this.stopPromise;
3481
+ // Publish the terminal state synchronously. All callers then share the same
3482
+ // completion promise, including callers arriving while teardown is in progress.
3484
3483
  this.phase = "stopped";
3485
- this.emitStateIfChanged();
3486
- await this.controller.stop();
3487
- await this.ownerRecordWrite.catch((error) => {
3488
- this.log.warn("owned_runtime_persistence_failed", { error: String(error) });
3484
+ const failures: unknown[] = [];
3485
+ const completion = Promise.resolve().then(async () => {
3486
+ try {
3487
+ await this.controller.stop();
3488
+ } catch (error) {
3489
+ failures.push(error);
3490
+ }
3491
+ // Closing the provider can reject an active prompt. Its catch/finally path may
3492
+ // enqueue final message and metering writes, so the pre-stop write tail is not
3493
+ // sufficient evidence that persistence is quiescent.
3494
+ try {
3495
+ await this.operations.waitForIdle();
3496
+ } catch (error) {
3497
+ failures.push(error);
3498
+ }
3499
+ await this.ownerRecordWrite.catch((error) => {
3500
+ this.log.warn("owned_runtime_persistence_failed", { error: String(error) });
3501
+ });
3502
+ await this.presentation.drain().catch(() => undefined);
3503
+ try {
3504
+ await this.transcriptWrite;
3505
+ } catch (error) {
3506
+ failures.push(error);
3507
+ }
3508
+ try {
3509
+ await this.v2Transcript?.close();
3510
+ } catch (error) {
3511
+ failures.push(error);
3512
+ }
3513
+ if (failures.length === 1) throw failures[0];
3514
+ if (failures.length > 1) throw new AggregateError(failures, "AgentSession shutdown failed");
3489
3515
  });
3490
- await this.presentation.drain().catch(() => undefined);
3491
- await this.v2Transcript?.close();
3516
+ this.stopPromise = completion;
3517
+ try {
3518
+ this.interrupt({ clearQueue: true });
3519
+ } catch (error) {
3520
+ failures.push(error);
3521
+ }
3522
+ this.emitStateIfChanged();
3523
+ return completion;
3492
3524
  }
3493
3525
 
3494
3526
  // --- mode swap (catalog-affecting) -------------------------------------------
package/src/agent.ts CHANGED
@@ -824,10 +824,18 @@ class DefaultAgentConversationManager implements AgentConversationManager {
824
824
  : { kind: "new" },
825
825
  persistence: false,
826
826
  });
827
- this.live.set(id, conversation);
828
- conversation.subscribe((notification) => {
827
+ const openedConversation = conversation;
828
+ this.live.set(id, openedConversation);
829
+ openedConversation.subscribe((notification) => {
829
830
  if (notification.event.kind === "state_changed" && notification.event.state.phase === "stopped") {
830
- if (this.live.get(id) === conversation) this.live.delete(id);
831
+ // The stopped state is published before the active turn finishes its
832
+ // error/finalization path. Keep it discoverable to stopAll() until stop()
833
+ // has drained those writes, or Agent.close() may close SQLite too early.
834
+ void openedConversation.stop().then(() => {
835
+ if (this.live.get(id) === openedConversation) this.live.delete(id);
836
+ }, () => {
837
+ if (this.live.get(id) === openedConversation) this.live.delete(id);
838
+ });
831
839
  }
832
840
  });
833
841
  if (conversation.internalSessionId && !config.deferProviderSessionPersistence) {