@qlairoslabs/acp-client 0.10.0 → 0.10.1
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 +85 -0
- package/package.json +4 -2
- package/src/agent-session.ts +2 -0
- package/src/agent.ts +380 -86
- package/src/audit/config.ts +4 -0
- package/src/audit/projection.ts +11 -0
- package/src/checkpointing/recovery.ts +23 -1
- package/src/index.ts +23 -0
- package/src/out-of-band-messages.ts +187 -0
- package/src/persistence/backend.ts +46 -1
- package/src/persistence/error-records.ts +104 -0
- package/src/persistence/sqlite-conversation-records.ts +38 -0
- package/src/persistence/sqlite-schema.ts +128 -47
|
@@ -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 = 12;
|
|
4
4
|
|
|
5
5
|
const BASE_SCHEMA = `
|
|
6
6
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
@@ -65,7 +65,8 @@ CREATE TABLE IF NOT EXISTS conversation_records (
|
|
|
65
65
|
'agent_action',
|
|
66
66
|
'thinking',
|
|
67
67
|
'activity',
|
|
68
|
-
'metering'
|
|
68
|
+
'metering',
|
|
69
|
+
'error'
|
|
69
70
|
)),
|
|
70
71
|
timestamp TEXT NOT NULL,
|
|
71
72
|
operation_id TEXT,
|
|
@@ -210,6 +211,24 @@ CREATE TABLE IF NOT EXISTS conversation_metering_references (
|
|
|
210
211
|
CREATE INDEX IF NOT EXISTS conversation_metering_operation_idx
|
|
211
212
|
ON conversation_metering_references(operation_id, record_row_id);
|
|
212
213
|
|
|
214
|
+
CREATE TABLE IF NOT EXISTS conversation_error_records (
|
|
215
|
+
record_row_id INTEGER PRIMARY KEY REFERENCES conversation_records(row_id) ON DELETE CASCADE,
|
|
216
|
+
source TEXT NOT NULL CHECK (source IN (
|
|
217
|
+
'provider_session', 'transport', 'operation', 'recovery'
|
|
218
|
+
)),
|
|
219
|
+
code TEXT NOT NULL CHECK (length(trim(code)) > 0),
|
|
220
|
+
recoverability TEXT NOT NULL CHECK (recoverability IN (
|
|
221
|
+
'retryable', 'recoverable', 'terminal', 'unknown'
|
|
222
|
+
)),
|
|
223
|
+
message TEXT,
|
|
224
|
+
transition_id TEXT,
|
|
225
|
+
internal_session_id TEXT,
|
|
226
|
+
details_json TEXT CHECK (details_json IS NULL OR json_valid(details_json))
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
CREATE INDEX IF NOT EXISTS conversation_error_source_code_idx
|
|
230
|
+
ON conversation_error_records(source, code, record_row_id);
|
|
231
|
+
|
|
213
232
|
CREATE TABLE IF NOT EXISTS conversation_checkpoints (
|
|
214
233
|
agent_conversation_id TEXT PRIMARY KEY REFERENCES conversations(agent_conversation_id),
|
|
215
234
|
revision INTEGER NOT NULL CHECK (revision >= 0),
|
|
@@ -518,58 +537,120 @@ END;
|
|
|
518
537
|
`;
|
|
519
538
|
|
|
520
539
|
export function migrateSqliteAgentSchema(database: Database): void {
|
|
521
|
-
database.
|
|
540
|
+
const hasMigrations = database.query<{ present: number }, []>(
|
|
541
|
+
"SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'schema_migrations'",
|
|
542
|
+
).get() !== null;
|
|
543
|
+
const existing = hasMigrations
|
|
544
|
+
? Number(database.query<{ version: number }, []>(
|
|
545
|
+
"SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations",
|
|
546
|
+
).get()?.version ?? 0)
|
|
547
|
+
: 0;
|
|
548
|
+
if (existing !== 0 && ![8, 9, 10, 11, SQLITE_AGENT_SCHEMA_VERSION].includes(existing)) {
|
|
549
|
+
throw new Error(
|
|
550
|
+
`SQLite agent schema ${existing} is incompatible with ${SQLITE_AGENT_SCHEMA_VERSION}; recreate the database`,
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
// Admitting the `error` kind means rebuilding conversation_records: its kind CHECK is a
|
|
554
|
+
// closed list SQLite cannot alter in place. Foreign keys must be off for that drop, or
|
|
555
|
+
// the implicit delete would cascade into every detail table.
|
|
556
|
+
const rebuildRecords = existing > 0 && existing < 12;
|
|
557
|
+
if (rebuildRecords) database.exec("PRAGMA foreign_keys = OFF");
|
|
522
558
|
try {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
const existing = Number(database.query<{ version: number }, []>(
|
|
559
|
+
database.exec("BEGIN IMMEDIATE");
|
|
560
|
+
try {
|
|
561
|
+
database.exec(BASE_SCHEMA);
|
|
562
|
+
const row = database.query<{ version: number }, []>(
|
|
528
563
|
"SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations",
|
|
529
|
-
).get()
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
);
|
|
564
|
+
).get();
|
|
565
|
+
const version = Number(row?.version ?? 0);
|
|
566
|
+
if (version > 0 && version < 10) {
|
|
567
|
+
database.exec(`ALTER TABLE conversation_messages ADD COLUMN consumer_metadata_json TEXT
|
|
568
|
+
CHECK (consumer_metadata_json IS NULL OR json_valid(consumer_metadata_json))`);
|
|
569
|
+
database.exec("ALTER TABLE conversations ADD COLUMN last_human_message_at TEXT");
|
|
570
|
+
database.exec(`UPDATE conversations SET last_human_message_at = (
|
|
571
|
+
SELECT r.timestamp FROM conversation_records r
|
|
572
|
+
JOIN conversation_messages m ON m.record_row_id = r.row_id
|
|
573
|
+
JOIN participants p ON p.participant_id = m.participant_id
|
|
574
|
+
WHERE r.agent_conversation_id = conversations.agent_conversation_id
|
|
575
|
+
AND r.kind = 'message' AND m.role = 'user' AND p.kind = 'user'
|
|
576
|
+
ORDER BY r.sequence DESC LIMIT 1
|
|
577
|
+
)`);
|
|
535
578
|
}
|
|
579
|
+
if (version > 0 && version < 11) {
|
|
580
|
+
database.exec("ALTER TABLE conversation_messages ADD COLUMN logical_message_id TEXT");
|
|
581
|
+
}
|
|
582
|
+
database.exec(`CREATE INDEX IF NOT EXISTS conversation_messages_logical_idx
|
|
583
|
+
ON conversation_messages(logical_message_id, record_row_id)
|
|
584
|
+
WHERE logical_message_id IS NOT NULL`);
|
|
585
|
+
if (version > 0 && version < 12) {
|
|
586
|
+
rebuildConversationRecordsForErrorKind(database);
|
|
587
|
+
const violations = database.query<Record<string, unknown>, []>("PRAGMA foreign_key_check").all();
|
|
588
|
+
if (violations.length > 0) {
|
|
589
|
+
throw new Error("SQLite schema migration to version 12 left foreign key violations");
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (version < SQLITE_AGENT_SCHEMA_VERSION) {
|
|
593
|
+
database.query<unknown, [number, string]>(
|
|
594
|
+
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
|
595
|
+
).run(SQLITE_AGENT_SCHEMA_VERSION, new Date().toISOString());
|
|
596
|
+
}
|
|
597
|
+
database.exec("COMMIT");
|
|
598
|
+
} catch (error) {
|
|
599
|
+
if (database.inTransaction) database.exec("ROLLBACK");
|
|
600
|
+
throw error;
|
|
536
601
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
"SELECT COALESCE(MAX(version), 0) AS version FROM schema_migrations",
|
|
540
|
-
).get();
|
|
541
|
-
const version = Number(row?.version ?? 0);
|
|
542
|
-
if (version > 0 && version < 10) {
|
|
543
|
-
database.exec(`ALTER TABLE conversation_messages ADD COLUMN consumer_metadata_json TEXT
|
|
544
|
-
CHECK (consumer_metadata_json IS NULL OR json_valid(consumer_metadata_json))`);
|
|
545
|
-
database.exec("ALTER TABLE conversations ADD COLUMN last_human_message_at TEXT");
|
|
546
|
-
database.exec(`UPDATE conversations SET last_human_message_at = (
|
|
547
|
-
SELECT r.timestamp FROM conversation_records r
|
|
548
|
-
JOIN conversation_messages m ON m.record_row_id = r.row_id
|
|
549
|
-
JOIN participants p ON p.participant_id = m.participant_id
|
|
550
|
-
WHERE r.agent_conversation_id = conversations.agent_conversation_id
|
|
551
|
-
AND r.kind = 'message' AND m.role = 'user' AND p.kind = 'user'
|
|
552
|
-
ORDER BY r.sequence DESC LIMIT 1
|
|
553
|
-
)`);
|
|
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`);
|
|
561
|
-
if (version < SQLITE_AGENT_SCHEMA_VERSION) {
|
|
562
|
-
database.query<unknown, [number, string]>(
|
|
563
|
-
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
|
564
|
-
).run(SQLITE_AGENT_SCHEMA_VERSION, new Date().toISOString());
|
|
565
|
-
}
|
|
566
|
-
database.exec("COMMIT");
|
|
567
|
-
} catch (error) {
|
|
568
|
-
if (database.inTransaction) database.exec("ROLLBACK");
|
|
569
|
-
throw error;
|
|
602
|
+
} finally {
|
|
603
|
+
if (rebuildRecords) database.exec("PRAGMA foreign_keys = ON");
|
|
570
604
|
}
|
|
571
605
|
}
|
|
572
606
|
|
|
607
|
+
/**
|
|
608
|
+
* Rebuild `conversation_records` so its `kind` CHECK admits `error`.
|
|
609
|
+
*
|
|
610
|
+
* Row ids are copied verbatim, so every detail table's `record_row_id` reference survives
|
|
611
|
+
* the swap. The caller runs this with foreign keys disabled and verifies with
|
|
612
|
+
* `PRAGMA foreign_key_check` afterward.
|
|
613
|
+
*/
|
|
614
|
+
function rebuildConversationRecordsForErrorKind(database: Database): void {
|
|
615
|
+
database.exec(`CREATE TABLE conversation_records_v12 (
|
|
616
|
+
row_id INTEGER PRIMARY KEY,
|
|
617
|
+
record_id TEXT NOT NULL UNIQUE,
|
|
618
|
+
agent_session_id TEXT NOT NULL,
|
|
619
|
+
agent_conversation_id TEXT NOT NULL REFERENCES conversations(agent_conversation_id),
|
|
620
|
+
sequence INTEGER NOT NULL CHECK (sequence > 0),
|
|
621
|
+
kind TEXT NOT NULL CHECK (kind IN (
|
|
622
|
+
'message',
|
|
623
|
+
'tool_call',
|
|
624
|
+
'permission',
|
|
625
|
+
'configuration',
|
|
626
|
+
'protocol',
|
|
627
|
+
'native_action',
|
|
628
|
+
'agent_action',
|
|
629
|
+
'thinking',
|
|
630
|
+
'activity',
|
|
631
|
+
'metering',
|
|
632
|
+
'error'
|
|
633
|
+
)),
|
|
634
|
+
timestamp TEXT NOT NULL,
|
|
635
|
+
operation_id TEXT,
|
|
636
|
+
metadata TEXT,
|
|
637
|
+
UNIQUE (agent_conversation_id, sequence)
|
|
638
|
+
)`);
|
|
639
|
+
database.exec(`INSERT INTO conversation_records_v12(
|
|
640
|
+
row_id, record_id, agent_session_id, agent_conversation_id, sequence, kind, timestamp, operation_id, metadata
|
|
641
|
+
) SELECT row_id, record_id, agent_session_id, agent_conversation_id, sequence, kind, timestamp, operation_id, metadata
|
|
642
|
+
FROM conversation_records`);
|
|
643
|
+
database.exec("DROP TABLE conversation_records");
|
|
644
|
+
database.exec("ALTER TABLE conversation_records_v12 RENAME TO conversation_records");
|
|
645
|
+
database.exec(`CREATE INDEX IF NOT EXISTS conversation_records_kind_idx
|
|
646
|
+
ON conversation_records(agent_conversation_id, kind, sequence)`);
|
|
647
|
+
database.exec(`CREATE INDEX IF NOT EXISTS conversation_records_time_idx
|
|
648
|
+
ON conversation_records(timestamp, agent_conversation_id)`);
|
|
649
|
+
database.exec(`CREATE INDEX IF NOT EXISTS conversation_records_operation_idx
|
|
650
|
+
ON conversation_records(agent_conversation_id, operation_id, sequence)
|
|
651
|
+
WHERE operation_id IS NOT NULL`);
|
|
652
|
+
}
|
|
653
|
+
|
|
573
654
|
export function installSqliteFts(database: Database): boolean {
|
|
574
655
|
const row = database.query<{ enabled: number }, []>(
|
|
575
656
|
"SELECT sqlite_compileoption_used('ENABLE_FTS5') AS enabled",
|