@qlairoslabs/acp-client 0.7.3 → 0.8.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-audit.md +58 -0
- package/CONSUMER_GUIDE.md +474 -19
- package/package.json +6 -1
- package/src/agent-adapter-registry.ts +66 -0
- package/src/agent-conversation-continuation.ts +121 -0
- package/src/agent-conversation-contracts.ts +279 -0
- package/src/agent-conversation-errors.ts +31 -0
- package/src/agent-conversation-owner.ts +55 -0
- package/src/agent-conversation-presentation.ts +217 -0
- package/src/agent-conversation-queue.ts +44 -0
- package/src/agent-conversation.ts +41 -0
- package/src/agent-ids.ts +50 -0
- package/src/agent-session.ts +1022 -682
- package/src/agent.ts +1175 -0
- package/src/audit/config.ts +186 -0
- package/src/audit/contracts.ts +59 -0
- package/src/audit/index.ts +50 -0
- package/src/audit/projection.ts +125 -0
- package/src/audit/reader.ts +371 -0
- package/src/audit/runtime.ts +1060 -0
- package/src/checkpointing/config.ts +91 -0
- package/src/checkpointing/contracts.ts +45 -0
- package/src/checkpointing/coordinator.ts +100 -0
- package/src/checkpointing/ids.ts +5 -0
- package/src/checkpointing/index.ts +6 -0
- package/src/checkpointing/persistence/sqlite-repository.ts +159 -0
- package/src/checkpointing/recovery.ts +114 -0
- package/src/checkpointing/strategy.ts +139 -0
- package/src/controller.ts +37 -5
- package/src/identity.ts +62 -0
- package/src/index.ts +121 -0
- package/src/internal-inference/config.ts +68 -0
- package/src/internal-inference/contracts.ts +218 -0
- package/src/internal-inference/coordination/handles.ts +133 -0
- package/src/internal-inference/coordination/manager.ts +487 -0
- package/src/internal-inference/ids.ts +10 -0
- package/src/internal-inference/index.ts +10 -0
- package/src/internal-inference/persistence/records.ts +132 -0
- package/src/internal-inference/persistence/repository.ts +35 -0
- package/src/internal-inference/persistence/sqlite-repository.ts +551 -0
- package/src/internal-inference/runtime/harness-runtime.ts +78 -0
- package/src/internal-inference/strategy/registry.ts +42 -0
- package/src/metering/engine.ts +47 -0
- package/src/metering/record.ts +4 -1
- package/src/observation/contracts.ts +10 -0
- package/src/observation/index.ts +8 -0
- package/src/observation/subscription-hub.ts +43 -0
- package/src/persistence/activity-projection.ts +26 -12
- package/src/persistence/backend.ts +774 -0
- package/src/persistence/index.ts +99 -0
- package/src/persistence/managed-creation.ts +18 -2
- package/src/persistence/managed-load.ts +19 -5
- package/src/persistence/managed-open-progress.ts +54 -0
- package/src/persistence/presentation.ts +3 -0
- package/src/persistence/sqlite-backend.ts +1927 -0
- package/src/persistence/sqlite-conversation-records.ts +425 -0
- package/src/persistence/sqlite-owner-lock.ts +106 -0
- package/src/persistence/sqlite-schema.ts +561 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Agent audit log — breaking changes and release notes
|
|
2
|
+
|
|
3
|
+
Pre-1.0 changes introduced by the SQLite-backed Agent audit stream
|
|
4
|
+
(`.res/somelogs.md/9-16+/9-18-SPEC-agent-audit-log.md`). Fold this into the release notes.
|
|
5
|
+
|
|
6
|
+
## 1. SQLite schema version 8
|
|
7
|
+
|
|
8
|
+
Schema version 8 adds the durable Agent audit state and pending outbox. Pre-release
|
|
9
|
+
version-7 databases are intentionally not migrated; recreate development and smoke-test
|
|
10
|
+
databases before opening them with this release.
|
|
11
|
+
|
|
12
|
+
The SQLite database remains the conversation-recovery source. Audit JSONL files are an
|
|
13
|
+
independent operational history, not a second recovery backend.
|
|
14
|
+
|
|
15
|
+
## 2. One optional audit stream per Agent
|
|
16
|
+
|
|
17
|
+
`Agent.open()` accepts `auditLog`. Its caller-selected directory contains one logical
|
|
18
|
+
stream across all conversations owned by that agent, split into numbered JSONL segments.
|
|
19
|
+
Capture policy is evaluated from the normalized observation independently of canonical
|
|
20
|
+
SQLite capture, so either side may retain more detail than the other.
|
|
21
|
+
|
|
22
|
+
Canonical persistence and outbox enqueue share a transaction. Delivery happens in the
|
|
23
|
+
background, is ordered, durable, and at least once. Entries carry stable audit sequence,
|
|
24
|
+
outbox, source-observation, agent, and optional conversation identifiers.
|
|
25
|
+
|
|
26
|
+
## 3. Rotation, recovery, and observability
|
|
27
|
+
|
|
28
|
+
Size and age triggers rotate the stream without changing its identity. Startup validates
|
|
29
|
+
the contiguous segment set, repairs only an incomplete highest tail, and reconciles the
|
|
30
|
+
bounded physically written but unacknowledged suffix against SQLite. Missing delivered
|
|
31
|
+
history and identity conflicts fail closed.
|
|
32
|
+
|
|
33
|
+
`agent.audit` exposes `status()`, `subscribe()`, `flush()`, and `retry()`. `Agent.close()`
|
|
34
|
+
attempts a bounded flush and preserves undelivered rows for the next open.
|
|
35
|
+
|
|
36
|
+
`AgentAuditLogReader` provides paginated `logical` and `physical` views. Logical mode
|
|
37
|
+
deduplicates the bounded crash-replay window; physical mode preserves it for diagnostics.
|
|
38
|
+
|
|
39
|
+
## 4. Explicit single-owner rule
|
|
40
|
+
|
|
41
|
+
One live process owns an agent SQLite database, and one live writer owns an audit
|
|
42
|
+
destination. A second open fails with `AgentDatabaseInUseError` or
|
|
43
|
+
`AuditLogDestinationInUseError`. Any number of conversations may still execute
|
|
44
|
+
concurrently through that owning `Agent` instance.
|
|
45
|
+
|
|
46
|
+
## 5. Accepted initial limitations
|
|
47
|
+
|
|
48
|
+
- Audit configuration cannot change while an Agent is open.
|
|
49
|
+
- All numbered segments are required; automatic retention is not included.
|
|
50
|
+
- Enabling audit later does not backfill canonical history.
|
|
51
|
+
- Age rotation is evaluated on delivery, not by an idle timer.
|
|
52
|
+
- Files are plaintext and unsigned.
|
|
53
|
+
- Entry-level syncing prioritizes crash recovery over maximum raw-protocol throughput.
|
|
54
|
+
- Custom persistence backends cannot enable Agent audit until they implement the durable
|
|
55
|
+
audit outbox contract.
|
|
56
|
+
|
|
57
|
+
The older `SessionManager` transcript/audit system remains supported and is not silently
|
|
58
|
+
redirected to this Agent-level stream.
|
package/CONSUMER_GUIDE.md
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
1
|
# Consumer guide
|
|
2
2
|
|
|
3
3
|
This guide is for applications embedding `@qlairoslabs/acp-client`, especially
|
|
4
|
-
orchestrators that own several
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
orchestrators that own several agents and conversations. The recommended durable API is
|
|
5
|
+
`Agent`, backed by a caller-located SQLite database. The library owns ACP communication,
|
|
6
|
+
conversation queues, typed records, search, identity lifecycle hooks, and persistence.
|
|
7
|
+
The consumer owns database placement, adapter definitions, user interface, and application
|
|
8
|
+
policy.
|
|
9
|
+
|
|
10
|
+
`AgentSession` remains the compatibility name for one live `AgentConversation`. The
|
|
11
|
+
older `SessionManager`/JSONL workflow is still supported and documented later in this
|
|
12
|
+
guide, but new durable applications should normally begin with `Agent`.
|
|
7
13
|
|
|
8
14
|
The package requires Bun 1.3.5 or newer.
|
|
9
15
|
|
|
10
16
|
## Mental model
|
|
11
17
|
|
|
12
|
-
There are three
|
|
18
|
+
There are three primary runtime layers:
|
|
13
19
|
|
|
14
20
|
| Layer | Main API | Examples |
|
|
15
21
|
|---|---|---|
|
|
16
|
-
|
|
|
17
|
-
| One stable
|
|
22
|
+
| Agent identity and persistence | `Agent` | Own SQLite persistence, adapter registry, identity runtime, and conversations |
|
|
23
|
+
| One stable conversation | `AgentConversation` (`AgentSession` compatibility alias) | Prompt, interrupt, handoff, switch adapters, reset, observe state, and configure the harness |
|
|
18
24
|
| Active harness thread | ACP and `internalSessionCommands` | Harness-specific commands such as `/usage` and `/model` |
|
|
19
25
|
|
|
20
|
-
An `agentSessionId`
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
An `agentSessionId` (`ags-*`) identifies the agent. An `agentConversationId` (`agc-*`)
|
|
27
|
+
identifies one of its conversations. An `internalSessionId` identifies the replaceable
|
|
28
|
+
provider or harness thread behind that conversation.
|
|
23
29
|
|
|
24
30
|
```text
|
|
25
31
|
consumer
|
|
26
|
-
└──
|
|
27
|
-
└──
|
|
28
|
-
└──
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
`SessionManager` persists and discovers logical sessions. It does not retain live
|
|
32
|
-
`AgentSession` objects. An orchestrator normally keeps those separately:
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
const liveSessions = new Map<string, AgentSession>();
|
|
32
|
+
└── Agent ags-… stable agent identity + SQLite database
|
|
33
|
+
└── AgentConversation agc-… stable conversation identity
|
|
34
|
+
└── AgentController
|
|
35
|
+
└── internal thread abc replaceable harness identity
|
|
36
36
|
```
|
|
37
37
|
|
|
38
38
|
## Install and import
|
|
@@ -44,17 +44,451 @@ bun add @qlairoslabs/acp-client
|
|
|
44
44
|
```ts
|
|
45
45
|
import {
|
|
46
46
|
ADAPTERS,
|
|
47
|
+
Agent,
|
|
48
|
+
AgentAuditLogReader,
|
|
47
49
|
AgentSession,
|
|
48
50
|
AuditLogReader,
|
|
49
51
|
SessionManager,
|
|
50
52
|
TranscriptReader,
|
|
51
53
|
formatResponseMessages,
|
|
52
54
|
type Adapter,
|
|
55
|
+
type AgentSessionOpenStage,
|
|
53
56
|
type AgentSessionNotification,
|
|
54
57
|
type PromptHandlers,
|
|
55
58
|
} from "@qlairoslabs/acp-client";
|
|
56
59
|
```
|
|
57
60
|
|
|
61
|
+
## Start with one SQLite-backed agent
|
|
62
|
+
|
|
63
|
+
The database path is explicit and belongs to the consumer. One database contains one
|
|
64
|
+
agent identity and any number of independently addressable conversations.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { Agent, ADAPTERS } from "@qlairoslabs/acp-client";
|
|
68
|
+
|
|
69
|
+
const agent = await Agent.open({
|
|
70
|
+
persistence: {
|
|
71
|
+
kind: "sqlite",
|
|
72
|
+
path: "/absolute/path/to/state/atlas.sqlite",
|
|
73
|
+
},
|
|
74
|
+
adapters: {
|
|
75
|
+
codex: ADAPTERS.codex,
|
|
76
|
+
},
|
|
77
|
+
defaultAdapterId: "codex",
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
const conversation = await agent.conversations.create();
|
|
82
|
+
const result = await conversation.prompt("hi");
|
|
83
|
+
|
|
84
|
+
for (const message of result.messages) {
|
|
85
|
+
console.log(message.text);
|
|
86
|
+
}
|
|
87
|
+
} finally {
|
|
88
|
+
await agent.close();
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The user message and each assistant-message boundary are stored as separate typed rows.
|
|
93
|
+
Reopen the same database with `Agent.open()` and call
|
|
94
|
+
`agent.conversations.open(agentConversationId)` to continue its active provider thread.
|
|
95
|
+
|
|
96
|
+
SQLite keeps ordering and type-specific content separate. `conversation_records` is a
|
|
97
|
+
narrow canonical envelope containing the conversation-local sequence, kind, timestamp,
|
|
98
|
+
operation correlation, and optional metadata. Every envelope has exactly one typed detail
|
|
99
|
+
row in tables such as `conversation_messages`, `conversation_tool_call_events`, or
|
|
100
|
+
`conversation_activity_events`. A mixed timeline is read as one envelope page followed by
|
|
101
|
+
bounded, batched subtype hydration; it never performs one query per record.
|
|
102
|
+
|
|
103
|
+
Messages store ordinary text directly in `content_text`; structured or multimodal content
|
|
104
|
+
uses `content_json`. Tool calls, permissions, configuration, actions, thinking, and
|
|
105
|
+
activities retain meaningful lifecycle transitions as independently sequenced records.
|
|
106
|
+
Raw ACP protocol bodies remain JSON because their external shape is intentionally open.
|
|
107
|
+
Each completed metered operation adds an ordered `metering` reference whose complete
|
|
108
|
+
financial record remains in `metering_records`; the timeline does not duplicate billing
|
|
109
|
+
payloads.
|
|
110
|
+
|
|
111
|
+
`activity` is reserved for model activity such as plans. Complete session observations
|
|
112
|
+
such as usage, available commands, mode/config announcements, session metadata, echoed
|
|
113
|
+
user messages, and unknown protocol updates are not reclassified as model activity.
|
|
114
|
+
Their semantic state is retained by the relevant configuration, metering, or checkpoint
|
|
115
|
+
storage, while consumers that need exact diagnostic traffic can enable protocol capture.
|
|
116
|
+
|
|
117
|
+
SQLite protocol capture supports `"none"`, `"metadata"`, and `"raw"`; protocol
|
|
118
|
+
`"content"` is rejected because coalesced content already belongs to the typed semantic
|
|
119
|
+
records. Exact method filters provide a compact protocol skeleton without persisting the
|
|
120
|
+
high-volume update stream:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
capture: {
|
|
124
|
+
protocol: {
|
|
125
|
+
level: "metadata",
|
|
126
|
+
excludeMethods: ["session/update"],
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`includeMethods` is also available and cannot be combined with `excludeMethods`. Both use
|
|
132
|
+
exact ACP method names. Methodless JSON-RPC responses remain captured because their method
|
|
133
|
+
cannot be determined without correlating request IDs. Filtering happens before canonical
|
|
134
|
+
sequence allocation, so omitted frames create neither rows nor sequence gaps.
|
|
135
|
+
|
|
136
|
+
FTS5 indexes deliberately extracted searchable text rather than serialized record JSON.
|
|
137
|
+
The initial indexes cover messages, tool calls, and activities. Consumer metadata and raw
|
|
138
|
+
protocol bodies are not searched implicitly.
|
|
139
|
+
|
|
140
|
+
Schema version 8 adds the durable Agent audit outbox to the typed layout. Pre-release
|
|
141
|
+
version-7 SQLite files are not migrated because they were used only by development smoke
|
|
142
|
+
tests. Opening one fails with a clear recreate instruction; remove the old smoke-test
|
|
143
|
+
database and run the scenario again.
|
|
144
|
+
|
|
145
|
+
### Agent audit log
|
|
146
|
+
|
|
147
|
+
An SQLite-backed `Agent` can copy selected observations into an independently located,
|
|
148
|
+
rotating JSONL audit stream. The stream belongs to the whole agent, so observations from
|
|
149
|
+
all of its conversations share one ordered audit sequence. Audit capture is independent
|
|
150
|
+
of canonical conversation capture: for example, an application may omit message content
|
|
151
|
+
from SQLite while retaining it in the audit destination, or do the reverse.
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
const agent = await Agent.open({
|
|
155
|
+
persistence: { kind: "sqlite", path: "/state/atlas.sqlite" },
|
|
156
|
+
adapters: { codex: ADAPTERS.codex },
|
|
157
|
+
auditLog: {
|
|
158
|
+
directory: "/audit/atlas",
|
|
159
|
+
filePrefix: "audit",
|
|
160
|
+
capture: {
|
|
161
|
+
message: "content",
|
|
162
|
+
protocol: {
|
|
163
|
+
level: "metadata",
|
|
164
|
+
excludeMethods: ["session/update"],
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
rotation: {
|
|
168
|
+
maxBytes: 64 * 1024 * 1024,
|
|
169
|
+
maxAgeMs: 24 * 60 * 60 * 1_000,
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
The default prefix is `audit`. Segments are named `audit.000001.jsonl`,
|
|
176
|
+
`audit.000002.jsonl`, and so on. A durable pending outbox in the agent database makes
|
|
177
|
+
canonical persistence and audit intent one transaction. A background writer then appends
|
|
178
|
+
and syncs those entries without delaying `prompt()`. Delivery is at least once: after a
|
|
179
|
+
crash, a physically written entry may be encountered again during reconciliation, and
|
|
180
|
+
its stable audit sequence and outbox ID identify the replay.
|
|
181
|
+
|
|
182
|
+
Observe delivery or explicitly wait for it when an application boundary requires that:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
const unsubscribe = agent.audit?.subscribe((event) => {
|
|
186
|
+
console.log(event.kind, event.snapshot.pendingCount);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const snapshot = await agent.audit?.status();
|
|
190
|
+
const flushed = await agent.audit?.flush({ timeoutMs: 10_000 });
|
|
191
|
+
if (flushed?.status === "blocked") {
|
|
192
|
+
// Repair or restore the destination before reopening the agent.
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
await agent.audit?.retry(); // manually end retry backoff after a transient failure
|
|
196
|
+
unsubscribe?.();
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
`Agent.close()` makes a bounded flush attempt using `shutdownGraceMs` (two seconds by
|
|
200
|
+
default), then releases the audit destination and agent database. Pending rows remain in
|
|
201
|
+
SQLite if the destination was unavailable and resume on the next open.
|
|
202
|
+
|
|
203
|
+
Read the stream without opening its agent database:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const reader = await AgentAuditLogReader.open({
|
|
207
|
+
directory: "/audit/atlas",
|
|
208
|
+
filePrefix: "audit",
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const logical = await reader.readPage({ mode: "logical", limit: 100 });
|
|
212
|
+
const next = logical.nextCursor
|
|
213
|
+
? await reader.readPage({ mode: "logical", limit: 100, cursor: logical.nextCursor })
|
|
214
|
+
: null;
|
|
215
|
+
|
|
216
|
+
// Diagnostic view: preserves physical replay entries instead of deduplicating them.
|
|
217
|
+
const physical = await reader.readPage({ mode: "physical", limit: 100 });
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Logical mode is the normal consumer view and deduplicates the bounded crash-replay
|
|
221
|
+
window. Physical mode exposes what is actually present in the segment files. Reader
|
|
222
|
+
cursors describe a stable snapshot and fail if required segments have disappeared or
|
|
223
|
+
been truncated.
|
|
224
|
+
|
|
225
|
+
There is one active owner per agent database and one active writer per audit destination.
|
|
226
|
+
Opening either concurrently from another process fails with `AgentDatabaseInUseError` or
|
|
227
|
+
`AuditLogDestinationInUseError`; this does not restrict concurrent conversations within
|
|
228
|
+
the owning `Agent`. A stream whose identity, ordering, or synced history conflicts with
|
|
229
|
+
SQLite fails closed with `AuditLogIdentityConflictError` or
|
|
230
|
+
`AuditLogHistoryMissingError` instead of silently starting a new history.
|
|
231
|
+
|
|
232
|
+
The initial Agent audit design deliberately accepts these constraints:
|
|
233
|
+
|
|
234
|
+
- Configuration is fixed while the agent is open. Close and reopen to change it.
|
|
235
|
+
- Every numbered segment remains part of the logical stream; automatic audit retention
|
|
236
|
+
and deletion are not provided.
|
|
237
|
+
- Time-based rotation is checked when an entry is delivered, so an idle segment is not
|
|
238
|
+
rotated merely because time passed.
|
|
239
|
+
- Disabling audit and later enabling it creates a new stream from that point; canonical
|
|
240
|
+
history is not backfilled.
|
|
241
|
+
- JSONL is plaintext and not cryptographically signed. Protect, archive, and retain the
|
|
242
|
+
destination according to application policy.
|
|
243
|
+
- Durability currently favors an entry-level sync boundary. Raw protocol capture can
|
|
244
|
+
therefore create backlog under sustained high-volume streaming; exclude
|
|
245
|
+
`session/update` or use metadata/semantic capture unless exact raw traffic is required.
|
|
246
|
+
|
|
247
|
+
SQLite remains the source used for conversation recovery. Audit segments are an
|
|
248
|
+
independent operational history and are not imported to reconstruct provider sessions.
|
|
249
|
+
This API is distinct from the legacy `AuditLogReader` documented under
|
|
250
|
+
“Configure transcripts” below, which reads per-`AgentSession` v1 audit files.
|
|
251
|
+
|
|
252
|
+
The minimal end-to-end executable example is
|
|
253
|
+
[`smoke-tests/end-to-end/index.ts`](./smoke-tests/end-to-end/index.ts). It sends `"hi"`
|
|
254
|
+
through an ACP harness and independently queries SQLite to assert that the canonical
|
|
255
|
+
user-message row contains `"hi"`:
|
|
256
|
+
|
|
257
|
+
```bash
|
|
258
|
+
bun smoke-tests/end-to-end/index.ts
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
By default it owns and removes a temporary directory. Set `ACP_TEST8_DATA_DIR` to retain
|
|
262
|
+
the database at a caller-selected location for inspection:
|
|
263
|
+
|
|
264
|
+
```bash
|
|
265
|
+
ACP_TEST8_DATA_DIR=./smoke-tests/test8-test/data \
|
|
266
|
+
bun smoke-tests/end-to-end/index.ts
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
The smoke scenario uses a 64 MiB audit segment limit by default, so a short run may
|
|
270
|
+
produce only one segment. To assert rotation with a deliberately small test threshold,
|
|
271
|
+
set `ACP_TEST8_AUDIT_MAX_BYTES=1024`. This is a test override, not a suggested
|
|
272
|
+
production setting.
|
|
273
|
+
|
|
274
|
+
The current smoke scenario defaults to Codex because its second process opening verifies
|
|
275
|
+
that the provider thread remembers prior conversation context. It may use credentials and
|
|
276
|
+
consume model tokens.
|
|
277
|
+
|
|
278
|
+
Provider-session identity and persisted history are always hard assertions. Exact semantic
|
|
279
|
+
recall is also required by default for Codex, while other adapters report a warning because
|
|
280
|
+
their selected model may ignore correctly loaded context. Set
|
|
281
|
+
`ACP_TEST8_REQUIRE_SEMANTIC_RECALL=1` to require the exact recall assertion for any adapter,
|
|
282
|
+
or `0` to make it observational.
|
|
283
|
+
|
|
284
|
+
### SQLite metering persistence
|
|
285
|
+
|
|
286
|
+
SQLite-backed agents always persist the bounded metering state needed to restore lifetime
|
|
287
|
+
totals and the active provider-counter baseline. By default they also retain every complete
|
|
288
|
+
`OperationMeteringRecord` locally:
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
const agent = await Agent.open({
|
|
292
|
+
persistence: { kind: "sqlite", path: "/state/atlas.sqlite" },
|
|
293
|
+
adapters: { codex: ADAPTERS.codex },
|
|
294
|
+
defaultAdapterId: "codex",
|
|
295
|
+
meteringPersistence: {
|
|
296
|
+
meteringRecords: "all", // default
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
const page = await agent.persistence.metering.list(conversationId);
|
|
301
|
+
const records = page.records;
|
|
302
|
+
const state = await agent.persistence.metering.state(conversationId);
|
|
303
|
+
|
|
304
|
+
// Internal work not caused by a conversation has a separate agent-level scope.
|
|
305
|
+
const internalState = await agent.persistence.metering.agentState(agent.id);
|
|
306
|
+
const internalRecords = await agent.persistence.metering.listAgent(agent.id);
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Consumers with a separate authoritative billing store can omit the detailed local history:
|
|
310
|
+
|
|
311
|
+
```ts
|
|
312
|
+
meteringPersistence: {
|
|
313
|
+
meteringRecords: "none",
|
|
314
|
+
}
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
`"none"` still writes a minimal operation marker for idempotency, adds the independently
|
|
318
|
+
configured conversation-timeline reference, and always advances the bounded restoration
|
|
319
|
+
state. It only omits the full serialized metering record. Continue to
|
|
320
|
+
consume `result.metering` or `metering_recorded` notifications when delivering records to
|
|
321
|
+
an external billing repository.
|
|
322
|
+
|
|
323
|
+
### Run durable internal inference
|
|
324
|
+
|
|
325
|
+
`internalInference` is agent-scoped background model work for checkpoint, recovery, and
|
|
326
|
+
identity features. It is not a participant turn and does not write its prompt or output
|
|
327
|
+
into the visible transcript. Attempts are queued in SQLite, leased to one worker, retried
|
|
328
|
+
with a new `aii-…` attempt ID, observable, cancellable, and resumed when the database is
|
|
329
|
+
reopened.
|
|
330
|
+
|
|
331
|
+
Consumers register versioned strategies. A strategy derives a prompt from durable input,
|
|
332
|
+
then validates and publishes the result inside the transaction that completes the attempt.
|
|
333
|
+
The generic runtime deliberately does not define checkpoint or identity artifact schemas.
|
|
334
|
+
|
|
335
|
+
```ts
|
|
336
|
+
import {
|
|
337
|
+
ADAPTERS,
|
|
338
|
+
Agent,
|
|
339
|
+
HarnessInternalInferenceRuntime,
|
|
340
|
+
type InternalInferenceStrategy,
|
|
341
|
+
} from "@qlairoslabs/acp-client";
|
|
342
|
+
|
|
343
|
+
const strategy: InternalInferenceStrategy<{ throughSequence: number }> = {
|
|
344
|
+
strategyId: "checkpoint-v1",
|
|
345
|
+
purpose: "conversation_checkpoint",
|
|
346
|
+
version: 1,
|
|
347
|
+
validatePayload(value) {
|
|
348
|
+
const sequence = (value as { throughSequence?: unknown }).throughSequence;
|
|
349
|
+
if (!Number.isSafeInteger(sequence)) throw new Error("invalid sequence");
|
|
350
|
+
return { throughSequence: sequence as number };
|
|
351
|
+
},
|
|
352
|
+
async prepare(context, input) {
|
|
353
|
+
const page = await context.persistence.messages.list(context.attempt.agentConversationId!, {
|
|
354
|
+
throughSequence: input.throughSequence,
|
|
355
|
+
});
|
|
356
|
+
return {
|
|
357
|
+
prompt: `Summarize this conversation. Do not use tools.\n${JSON.stringify(page.messages)}`,
|
|
358
|
+
};
|
|
359
|
+
},
|
|
360
|
+
async complete(context, result) {
|
|
361
|
+
// Validate and write the artifact through context.persistence here.
|
|
362
|
+
return {
|
|
363
|
+
internalInferenceId: result.internalInferenceId,
|
|
364
|
+
rootInternalInferenceId: context.attempt.rootId,
|
|
365
|
+
internalSessionId: result.internalSessionId,
|
|
366
|
+
purpose: context.attempt.purpose,
|
|
367
|
+
completedAt: new Date().toISOString(),
|
|
368
|
+
};
|
|
369
|
+
},
|
|
370
|
+
classifyError() {
|
|
371
|
+
return { kind: "retryable", code: "provider_failure" };
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const runtime = new HarnessInternalInferenceRuntime({
|
|
376
|
+
controllerConfig: (request) => ({
|
|
377
|
+
adapter: ADAPTERS.codex,
|
|
378
|
+
adapterId: request.adapterId,
|
|
379
|
+
cwd: process.cwd(),
|
|
380
|
+
}),
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
const agent = await Agent.open({
|
|
384
|
+
persistence: { kind: "sqlite", path: "/state/atlas.sqlite" },
|
|
385
|
+
adapters: { codex: ADAPTERS.codex },
|
|
386
|
+
defaultAdapterId: "codex",
|
|
387
|
+
internalInference: {
|
|
388
|
+
runtime, // optional; omit to use the agent's harness adapter and runtime defaults
|
|
389
|
+
strategies: [strategy],
|
|
390
|
+
adapter: "same",
|
|
391
|
+
maxConcurrency: 1,
|
|
392
|
+
retry: { maxAttempts: 3, backoffMs: [1_000, 10_000, 60_000] },
|
|
393
|
+
retention: { retain: 100 },
|
|
394
|
+
},
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
const run = await agent.internalInferences!.enqueue({
|
|
398
|
+
purpose: "conversation_checkpoint",
|
|
399
|
+
agentConversationId: conversation.agentConversationId,
|
|
400
|
+
input: {
|
|
401
|
+
strategyId: "checkpoint-v1",
|
|
402
|
+
version: 1,
|
|
403
|
+
payload: { throughSequence: 42 },
|
|
404
|
+
},
|
|
405
|
+
requestedThroughSequence: 42,
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
const unsubscribe = run.subscribe(({ snapshot }) => {
|
|
409
|
+
console.log(snapshot.stage, snapshot.status);
|
|
410
|
+
});
|
|
411
|
+
const completion = await run.result;
|
|
412
|
+
unsubscribe();
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
The harness runtime starts a fresh disposable provider session and inherits the supplied
|
|
416
|
+
launcher, environment, working directory, and container boundary. Empty MCP/client
|
|
417
|
+
handlers and cancelled permission requests do not disable filesystem or terminal tools
|
|
418
|
+
built into the harness; enforce that boundary with the launcher or container when needed.
|
|
419
|
+
Custom runtimes should call `lifecycle.sessionStarted(...)` before prompting so strict
|
|
420
|
+
metering preflight can run. Completed custom results are metered as a fallback.
|
|
421
|
+
|
|
422
|
+
### Generate recovery checkpoints and recover missing provider sessions
|
|
423
|
+
|
|
424
|
+
Enable `checkpointing` on an SQLite-backed `Agent` to generate rolling continuation
|
|
425
|
+
summaries outside the visible provider thread:
|
|
426
|
+
|
|
427
|
+
```ts
|
|
428
|
+
const agent = await Agent.open({
|
|
429
|
+
persistence: { kind: "sqlite", path: "/state/atlas.sqlite" },
|
|
430
|
+
adapters: { codex: ADAPTERS.codex },
|
|
431
|
+
defaultAdapterId: "codex",
|
|
432
|
+
checkpointing: {
|
|
433
|
+
schedule: {
|
|
434
|
+
milestones: [1, 5, 10, 15, 30, 50],
|
|
435
|
+
everyAfterMilestones: 50,
|
|
436
|
+
},
|
|
437
|
+
recovery: {
|
|
438
|
+
onLoadFailure: "recover", // default
|
|
439
|
+
maxTailMessages: 100,
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Checkpoint generation is durably enqueued after the configured successful response counts;
|
|
446
|
+
the visible prompt does not wait for the summary model call. Each new checkpoint combines
|
|
447
|
+
the previous summary with canonical records added since its covered message boundary.
|
|
448
|
+
Checkpoint artifacts and their `conversation_checkpoint` timeline actions are committed in
|
|
449
|
+
the same transaction as the generating internal inference.
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
const checkpoint = await agent.checkpointing!.latest(conversationId);
|
|
453
|
+
|
|
454
|
+
// Explicitly request a checkpoint outside the configured milestones.
|
|
455
|
+
await agent.checkpointing!.request(conversationId);
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
When `conversations.open()` cannot load the recorded provider session and the error is
|
|
459
|
+
classified as a missing session, the agent creates a fresh provider thread and privately
|
|
460
|
+
seeds it from the latest checkpoint plus canonical messages written after that checkpoint.
|
|
461
|
+
The seed is metered as `conversation_recovery` and recorded as a native action; it is not a
|
|
462
|
+
user or assistant transcript message.
|
|
463
|
+
|
|
464
|
+
```ts
|
|
465
|
+
const opened = await agent.conversations.open(conversationId, {
|
|
466
|
+
onRecoveryProgress(event) {
|
|
467
|
+
console.log(event.stage, event.checkpointId);
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
if (opened.providerSession === "recovered") {
|
|
472
|
+
console.log(opened.recovery);
|
|
473
|
+
}
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
ACP does not standardize missing-session errors. The default classifier recognizes common
|
|
477
|
+
“session not found,” “does not exist,” and missing Codex rollout-path failures. A harness
|
|
478
|
+
with different errors can provide `recovery.isMissingProviderSession(error)`. Authentication,
|
|
479
|
+
network, and arbitrary internal failures remain fatal by default. Set `onLoadFailure:
|
|
480
|
+
"error"` to disable automatic recovery.
|
|
481
|
+
|
|
482
|
+
Checkpointing requires message capture at `"content"` or `"raw"`. The checkpoint is a
|
|
483
|
+
continuation aid, not the authoritative transcript: complete messages and other records
|
|
484
|
+
remain independently queryable from SQLite.
|
|
485
|
+
|
|
486
|
+
## Legacy direct and managed sessions
|
|
487
|
+
|
|
488
|
+
The remaining sections describe the compatibility `AgentSession` and
|
|
489
|
+
`SessionManager`/JSONL APIs. They remain useful when an application needs a transient
|
|
490
|
+
connection or the existing file-oriented persistence model.
|
|
491
|
+
|
|
58
492
|
Interactive helpers are a separate entry point:
|
|
59
493
|
|
|
60
494
|
```ts
|
|
@@ -143,11 +577,17 @@ async function createSession(adapterId: string): Promise<AgentSession> {
|
|
|
143
577
|
persistence: {
|
|
144
578
|
transcript: { preset: "activity" },
|
|
145
579
|
},
|
|
580
|
+
onOpenProgress: projectOpenProgress,
|
|
146
581
|
});
|
|
147
582
|
|
|
148
583
|
liveSessions.set(session.agentSessionId, session);
|
|
149
584
|
return session;
|
|
150
585
|
}
|
|
586
|
+
|
|
587
|
+
function projectOpenProgress(event: AgentSessionOpenStage): void {
|
|
588
|
+
// Project this ephemeral event into your own UI/WebSocket protocol.
|
|
589
|
+
console.log(event.stage, event);
|
|
590
|
+
}
|
|
151
591
|
```
|
|
152
592
|
|
|
153
593
|
`agentSessionId` and `creationId` are generated when omitted. A consumer that needs
|
|
@@ -346,6 +786,7 @@ async function loadSession(agentSessionId: string): Promise<AgentSession> {
|
|
|
346
786
|
adapter: resolveAdapter(info.adapterId),
|
|
347
787
|
adapterId: info.adapterId,
|
|
348
788
|
defaultPermission: "cancel",
|
|
789
|
+
onOpenProgress: projectOpenProgress,
|
|
349
790
|
});
|
|
350
791
|
|
|
351
792
|
console.log(opened.continuation);
|
|
@@ -359,6 +800,20 @@ async function loadSession(agentSessionId: string): Promise<AgentSession> {
|
|
|
359
800
|
}
|
|
360
801
|
```
|
|
361
802
|
|
|
803
|
+
Managed creation and loading report the same ordered progress vocabulary:
|
|
804
|
+
|
|
805
|
+
```text
|
|
806
|
+
initializing
|
|
807
|
+
→ initialized (ACP capabilities are available)
|
|
808
|
+
→ internal_session_acquired (created or resumed)
|
|
809
|
+
→ ready (managed persistence is finalized and promptability is known)
|
|
810
|
+
```
|
|
811
|
+
|
|
812
|
+
The callback exists because the live `AgentSession` is not available for subscription
|
|
813
|
+
until the operation returns. Progress is ephemeral and is not transcript or registry
|
|
814
|
+
authority. A failed operation does not report `ready`, and a retry begins a new sequence.
|
|
815
|
+
Callback failures are logged and do not fail or roll back the managed operation.
|
|
816
|
+
|
|
362
817
|
Loading never silently creates a replacement provider thread. By default it attempts the
|
|
363
818
|
persisted `internalSessionId`. If that thread is unavailable, continuation reports the
|
|
364
819
|
failure and the returned session may be non-promptable.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qlairoslabs/acp-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Embeddable Agent Client Protocol (ACP) client library with degraded PTY fallback.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -34,11 +34,16 @@
|
|
|
34
34
|
"files": [
|
|
35
35
|
"src",
|
|
36
36
|
"CONSUMER_GUIDE.md",
|
|
37
|
+
"CHANGELOG-audit.md",
|
|
37
38
|
"CHANGELOG-metering.md"
|
|
38
39
|
],
|
|
39
40
|
"exports": {
|
|
40
41
|
".": "./src/index.ts",
|
|
41
42
|
"./activity": "./src/activity.ts",
|
|
43
|
+
"./conversation": "./src/agent-conversation.ts",
|
|
44
|
+
"./agent": "./src/agent.ts",
|
|
45
|
+
"./internal-inference": "./src/internal-inference/index.ts",
|
|
46
|
+
"./persistence": "./src/persistence/index.ts",
|
|
42
47
|
"./repl": "./src/repl/index.ts",
|
|
43
48
|
"./degraded": "./src/degraded/index.ts",
|
|
44
49
|
"./parser": "./src/parser/index.ts",
|