@qlairoslabs/acp-client 0.9.2 → 0.9.3
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 +12 -5
- package/package.json +1 -1
- package/src/agent-session.ts +23 -3
- package/src/agent.ts +12 -4
- package/src/persistence/backend.ts +2 -0
- package/src/persistence/sqlite-backend.ts +13 -0
- package/src/persistence/sqlite-conversation-records.ts +6 -2
- package/src/persistence/sqlite-schema.ts +10 -2
- package/src/persistence/timeline-reconcile.ts +1 -0
package/CONSUMER_GUIDE.md
CHANGED
|
@@ -165,8 +165,9 @@ FTS5 indexes deliberately extracted searchable text rather than serialized recor
|
|
|
165
165
|
The initial indexes cover messages, tool calls, and activities. Consumer metadata and raw
|
|
166
166
|
protocol bodies are not searched implicitly.
|
|
167
167
|
|
|
168
|
-
Schema version
|
|
169
|
-
agent databases in place.
|
|
168
|
+
Schema version 11 adds stable assistant-message identities and migrates version-8,
|
|
169
|
+
version-9, and version-10 agent databases in place. Version 10 added message consumer
|
|
170
|
+
metadata. Pre-release version-7 SQLite files are not migrated because
|
|
170
171
|
they were used only by development smoke tests; opening one gives a recreate instruction.
|
|
171
172
|
|
|
172
173
|
### Read SQLite conversation history and previews
|
|
@@ -190,8 +191,12 @@ if (older) items = reconcileConversationTimeline(items, older.records);
|
|
|
190
191
|
```
|
|
191
192
|
|
|
192
193
|
Every physical record has a stable `recordId` and a conversation-local `sequence`.
|
|
193
|
-
The reconciliation helper keeps distinct messages separate and groups revisions of
|
|
194
|
-
tool call, permission, thinking block, activity, or transition by its
|
|
194
|
+
The reconciliation helper keeps distinct messages separate and groups revisions of an
|
|
195
|
+
assistant message, tool call, permission, thinking block, activity, or transition by its
|
|
196
|
+
logical ID. Assistant snapshots take their position from the first streamed text; the
|
|
197
|
+
completed revision supplies their final content and status. Providers that expose a
|
|
198
|
+
message only in the final prompt result cannot supply an earlier position. Records
|
|
199
|
+
written before schema version 11 have no such boundaries to reconstruct. The helper's
|
|
195
200
|
items retain every revision, carry the latest merged fields in `current`, and sort by
|
|
196
201
|
the earliest loaded sequence. Loading an older page may move an item to its true
|
|
197
202
|
earlier position when its first revision arrives. Configuration, protocol, agent
|
|
@@ -222,7 +227,9 @@ also `user`. SQLite maintains that timestamp on conversation writes and backfill
|
|
|
222
227
|
once for pre-version-10 databases; preview reads do not rescan long message histories.
|
|
223
228
|
Preview paging follows conversation `updatedAt` order; that list may
|
|
224
229
|
reorder as conversations advance, so consumers should refresh it rather than treat
|
|
225
|
-
offset cursors as a frozen snapshot. `
|
|
230
|
+
offset cursors as a frozen snapshot. `messages.list()` returns the latest visible
|
|
231
|
+
revision per logical message; `records.list()` and `records.listRecent()` retain all
|
|
232
|
+
physical revisions for timeline reconstruction and audit. `listRecent` and `listPreviews` are SQLite
|
|
226
233
|
capabilities; custom persistence backends may not implement them.
|
|
227
234
|
|
|
228
235
|
### Agent audit log
|
package/package.json
CHANGED
package/src/agent-session.ts
CHANGED
|
@@ -2905,11 +2905,27 @@ export class AgentConversation implements AgentClient, Bridgeable {
|
|
|
2905
2905
|
stopReason?: string,
|
|
2906
2906
|
force = false,
|
|
2907
2907
|
) => {
|
|
2908
|
-
if (!this.v2Transcript
|
|
2908
|
+
if ((!this.v2Transcript && !this.ownerLifecycle) || !assistantMessage
|
|
2909
|
+
|| (!assistantMessage.dirty && !force)) return;
|
|
2909
2910
|
assistantMessage.revision++;
|
|
2910
2911
|
assistantMessage.dirty = false;
|
|
2911
2912
|
const message = assistantMessage;
|
|
2912
|
-
this.
|
|
2913
|
+
if (this.ownerAgentSessionId && status === "in_progress") {
|
|
2914
|
+
this.queueOwnedRecord({
|
|
2915
|
+
recordId: `${message.id}:snapshot:${message.revision}`,
|
|
2916
|
+
agentSessionId: this.ownerAgentSessionId,
|
|
2917
|
+
agentConversationId: this.agentConversationId,
|
|
2918
|
+
kind: "message",
|
|
2919
|
+
timestamp: new Date().toISOString(),
|
|
2920
|
+
role: "assistant",
|
|
2921
|
+
logicalMessageId: message.id,
|
|
2922
|
+
content: message.text,
|
|
2923
|
+
status,
|
|
2924
|
+
metadata: JSON.stringify({ operationId: item.turnId,
|
|
2925
|
+
...(message.providerMessageId ? { providerMessageId: message.providerMessageId } : {}) }),
|
|
2926
|
+
});
|
|
2927
|
+
}
|
|
2928
|
+
if (this.v2Transcript) this.presentation.queuePresentation({
|
|
2913
2929
|
kind: "message",
|
|
2914
2930
|
operationId: item.turnId,
|
|
2915
2931
|
role: "assistant",
|
|
@@ -2942,7 +2958,7 @@ export class AgentConversation implements AgentClient, Bridgeable {
|
|
|
2942
2958
|
item.handlers?.onRecord?.({ current: null, delta: null, emitted: [settledReasoning] });
|
|
2943
2959
|
}
|
|
2944
2960
|
response.append(chunk);
|
|
2945
|
-
if (this.v2Transcript) {
|
|
2961
|
+
if (this.v2Transcript || this.ownerLifecycle) {
|
|
2946
2962
|
if (chunk.startsMessage) {
|
|
2947
2963
|
flushAssistantMessage("completed", undefined, assistantMessage !== null);
|
|
2948
2964
|
beginAssistantMessage(chunk.messageId);
|
|
@@ -3062,6 +3078,7 @@ export class AgentConversation implements AgentClient, Bridgeable {
|
|
|
3062
3078
|
});
|
|
3063
3079
|
}
|
|
3064
3080
|
const result = Object.freeze({ ...res, operationId: item.turnId, metering: record });
|
|
3081
|
+
await this.ownerRecordWrite;
|
|
3065
3082
|
await this.ownerLifecycle?.afterPrompt({
|
|
3066
3083
|
operationId: item.turnId,
|
|
3067
3084
|
agentSessionId: this.ownerAgentSessionId!,
|
|
@@ -3088,6 +3105,9 @@ export class AgentConversation implements AgentClient, Bridgeable {
|
|
|
3088
3105
|
? Object.freeze([{ text }])
|
|
3089
3106
|
: Object.freeze([]);
|
|
3090
3107
|
if (!providerSettled) {
|
|
3108
|
+
await this.ownerRecordWrite.catch((persistenceError) => {
|
|
3109
|
+
this.log.warn("owned_runtime_persistence_failed", { error: String(persistenceError) });
|
|
3110
|
+
});
|
|
3091
3111
|
await this.ownerLifecycle?.promptFailed({
|
|
3092
3112
|
operationId: item.turnId,
|
|
3093
3113
|
agentSessionId: this.ownerAgentSessionId!,
|
package/src/agent.ts
CHANGED
|
@@ -943,6 +943,7 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
943
943
|
kind: "message",
|
|
944
944
|
timestamp,
|
|
945
945
|
role: "assistant",
|
|
946
|
+
logicalMessageId: `message:${input.operationId}:assistant:${index + 1}`,
|
|
946
947
|
content: message.text,
|
|
947
948
|
status,
|
|
948
949
|
metadata: JSON.stringify({
|
|
@@ -968,6 +969,7 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
968
969
|
kind: "message",
|
|
969
970
|
timestamp,
|
|
970
971
|
role: "assistant",
|
|
972
|
+
logicalMessageId: `message:${input.operationId}:assistant:${index + 1}`,
|
|
971
973
|
content: message.text,
|
|
972
974
|
status: "failed",
|
|
973
975
|
metadata: JSON.stringify({
|
|
@@ -1007,7 +1009,12 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
1007
1009
|
}
|
|
1008
1010
|
|
|
1009
1011
|
async record(record: NewConversationRecord): Promise<void> {
|
|
1010
|
-
|
|
1012
|
+
const streamingSnapshot = record.kind === "message" && record.status === "in_progress";
|
|
1013
|
+
const persisted = await this.persistObservations([record], !streamingSnapshot);
|
|
1014
|
+
// A streaming snapshot is a display revision, not a second completed response.
|
|
1015
|
+
if (!streamingSnapshot) {
|
|
1016
|
+
await this.updateIdentity(persisted);
|
|
1017
|
+
}
|
|
1011
1018
|
}
|
|
1012
1019
|
|
|
1013
1020
|
async meteringRecorded(record: OperationMeteringRecord): Promise<void> {
|
|
@@ -1073,9 +1080,10 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
1073
1080
|
|
|
1074
1081
|
private async persistObservations(
|
|
1075
1082
|
records: readonly NewConversationRecord[],
|
|
1083
|
+
audit = true,
|
|
1076
1084
|
): Promise<readonly ConversationRecord[]> {
|
|
1077
1085
|
if (records.length === 0) return Object.freeze([]);
|
|
1078
|
-
if (!this.settings.auditLog && records.every((record) => {
|
|
1086
|
+
if ((!audit || !this.settings.auditLog) && records.every((record) => {
|
|
1079
1087
|
if (record.kind !== "protocol") return this.settings.capture[record.kind] === "none";
|
|
1080
1088
|
return this.settings.capture.protocol.level === "none"
|
|
1081
1089
|
|| !shouldCaptureProtocolMethod(this.settings.capture.protocol, record.method);
|
|
@@ -1093,7 +1101,7 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
1093
1101
|
if (canonicalAccepted) {
|
|
1094
1102
|
persisted.push(await persistence.records.append(captureRuntimeRecord(record, level)));
|
|
1095
1103
|
}
|
|
1096
|
-
if (this.settings.auditLog) {
|
|
1104
|
+
if (audit && this.settings.auditLog) {
|
|
1097
1105
|
await enqueueAuditObservation(persistence, {
|
|
1098
1106
|
scope: "conversation",
|
|
1099
1107
|
sourceObservationId: record.recordId,
|
|
@@ -1103,7 +1111,7 @@ class PersistedConversationLifecycle implements AgentConversationOwnerLifecycle
|
|
|
1103
1111
|
}
|
|
1104
1112
|
}
|
|
1105
1113
|
});
|
|
1106
|
-
if (this.settings.auditLog) this.agent.audit?.notifyPending();
|
|
1114
|
+
if (audit && this.settings.auditLog) this.agent.audit?.notifyPending();
|
|
1107
1115
|
return Object.freeze(persisted);
|
|
1108
1116
|
}
|
|
1109
1117
|
|
|
@@ -72,6 +72,8 @@ export type PersistedContent = string | readonly PersistedContentBlock[];
|
|
|
72
72
|
|
|
73
73
|
export interface MessageRecord extends ConversationRecordEnvelope<"message"> {
|
|
74
74
|
readonly role: "assistant" | "user";
|
|
75
|
+
/** Stable identity shared by the snapshots and final revision of one assistant message. */
|
|
76
|
+
readonly logicalMessageId?: string;
|
|
75
77
|
/** The local agent is implied when omitted on an assistant message. */
|
|
76
78
|
readonly participantId?: string;
|
|
77
79
|
/** Opaque JSON supplied by the consumer for this message only. */
|
|
@@ -805,6 +805,19 @@ class SqliteConversationMessagePersistence implements ConversationMessagePersist
|
|
|
805
805
|
where.push(`m.participant_id IN (${options.participantIds.map(() => "?").join(", ")})`);
|
|
806
806
|
parameters.push(...options.participantIds);
|
|
807
807
|
}
|
|
808
|
+
// Message snapshots remain in the ordered record stream, but the message API
|
|
809
|
+
// returns only the latest revision visible at the requested sequence boundary.
|
|
810
|
+
where.push(`(m.logical_message_id IS NULL OR NOT EXISTS (
|
|
811
|
+
SELECT 1 FROM conversation_messages newer_m
|
|
812
|
+
JOIN conversation_records newer_r ON newer_r.row_id = newer_m.record_row_id
|
|
813
|
+
WHERE newer_r.agent_conversation_id = r.agent_conversation_id
|
|
814
|
+
AND newer_m.logical_message_id = m.logical_message_id
|
|
815
|
+
AND newer_r.sequence > r.sequence
|
|
816
|
+
${options.throughSequence === undefined ? "" : "AND newer_r.sequence <= ?"}
|
|
817
|
+
))`);
|
|
818
|
+
if (options.throughSequence !== undefined) {
|
|
819
|
+
parameters.push(nonNegativeInteger(options.throughSequence, "throughSequence"));
|
|
820
|
+
}
|
|
808
821
|
parameters.push(limit + 1, offset);
|
|
809
822
|
const rows = this.database.query<ConversationRecordRow, SqlValue[]>(
|
|
810
823
|
`SELECT r.* FROM conversation_records r
|
|
@@ -46,8 +46,8 @@ export function insertConversationRecordDetail(
|
|
|
46
46
|
run(database,
|
|
47
47
|
`INSERT INTO conversation_messages(
|
|
48
48
|
record_row_id, role, participant_id, content_text, content_json, status, search_text,
|
|
49
|
-
consumer_metadata_json
|
|
50
|
-
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
49
|
+
consumer_metadata_json, logical_message_id
|
|
50
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
51
51
|
rowId,
|
|
52
52
|
record.role,
|
|
53
53
|
record.participantId ?? null,
|
|
@@ -56,6 +56,7 @@ export function insertConversationRecordDetail(
|
|
|
56
56
|
record.status ?? null,
|
|
57
57
|
searchableContent(record.content),
|
|
58
58
|
encodeJson(record.consumerMetadata),
|
|
59
|
+
record.logicalMessageId ?? null,
|
|
59
60
|
);
|
|
60
61
|
return;
|
|
61
62
|
}
|
|
@@ -264,6 +265,9 @@ function recordFromRows(
|
|
|
264
265
|
...(detail.consumer_metadata_json !== null
|
|
265
266
|
? { consumerMetadata: decodeJson(detail.consumer_metadata_json) }
|
|
266
267
|
: {}),
|
|
268
|
+
...(detail.logical_message_id != null
|
|
269
|
+
? { logicalMessageId: string(detail.logical_message_id) }
|
|
270
|
+
: {}),
|
|
267
271
|
content: decodeContent(detail.content_text, detail.content_json),
|
|
268
272
|
...(detail.status !== null ? { status: string(detail.status) as "in_progress" | "completed" | "cancelled" | "failed" } : {}),
|
|
269
273
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Database } from "bun:sqlite";
|
|
2
2
|
|
|
3
|
-
export const SQLITE_AGENT_SCHEMA_VERSION =
|
|
3
|
+
export const SQLITE_AGENT_SCHEMA_VERSION = 11;
|
|
4
4
|
|
|
5
5
|
const BASE_SCHEMA = `
|
|
6
6
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
@@ -90,6 +90,7 @@ CREATE TABLE IF NOT EXISTS conversation_messages (
|
|
|
90
90
|
status TEXT CHECK (status IS NULL OR status IN ('in_progress', 'completed', 'cancelled', 'failed')),
|
|
91
91
|
search_text TEXT NOT NULL,
|
|
92
92
|
consumer_metadata_json TEXT CHECK (consumer_metadata_json IS NULL OR json_valid(consumer_metadata_json)),
|
|
93
|
+
logical_message_id TEXT,
|
|
93
94
|
CHECK ((content_text IS NULL) <> (content_json IS NULL)),
|
|
94
95
|
CHECK (content_json IS NULL OR json_valid(content_json))
|
|
95
96
|
);
|
|
@@ -526,7 +527,8 @@ export function migrateSqliteAgentSchema(database: Database): void {
|
|
|
526
527
|
const existing = Number(database.query<{ version: number }, []>(
|
|
527
528
|
"SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations",
|
|
528
529
|
).get()?.version ?? 0);
|
|
529
|
-
if (existing !== 0 && existing !== 8 && existing !== 9 && existing !==
|
|
530
|
+
if (existing !== 0 && existing !== 8 && existing !== 9 && existing !== 10
|
|
531
|
+
&& existing !== SQLITE_AGENT_SCHEMA_VERSION) {
|
|
530
532
|
throw new Error(
|
|
531
533
|
`SQLite agent schema ${existing} is incompatible with ${SQLITE_AGENT_SCHEMA_VERSION}; recreate the database`,
|
|
532
534
|
);
|
|
@@ -550,6 +552,12 @@ export function migrateSqliteAgentSchema(database: Database): void {
|
|
|
550
552
|
ORDER BY r.sequence DESC LIMIT 1
|
|
551
553
|
)`);
|
|
552
554
|
}
|
|
555
|
+
if (version > 0 && version < 11) {
|
|
556
|
+
database.exec("ALTER TABLE conversation_messages ADD COLUMN logical_message_id TEXT");
|
|
557
|
+
}
|
|
558
|
+
database.exec(`CREATE INDEX IF NOT EXISTS conversation_messages_logical_idx
|
|
559
|
+
ON conversation_messages(logical_message_id, record_row_id)
|
|
560
|
+
WHERE logical_message_id IS NOT NULL`);
|
|
553
561
|
if (version < SQLITE_AGENT_SCHEMA_VERSION) {
|
|
554
562
|
database.query<unknown, [number, string]>(
|
|
555
563
|
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
|
@@ -52,6 +52,7 @@ export function conversationTimelineItemId(record: ConversationRecord): string {
|
|
|
52
52
|
const prefix = `${record.agentConversationId}:${record.kind}:`;
|
|
53
53
|
const scope = operationScope(record);
|
|
54
54
|
switch (record.kind) {
|
|
55
|
+
case "message": return `${prefix}${JSON.stringify([record.logicalMessageId ?? record.recordId])}`;
|
|
55
56
|
case "tool_call": return `${prefix}${JSON.stringify([scope, record.toolCallId])}`;
|
|
56
57
|
case "permission": return `${prefix}${JSON.stringify([scope, record.permissionId])}`;
|
|
57
58
|
case "thinking": return `${prefix}${JSON.stringify([scope, record.thinkingId])}`;
|