@vorionsys/runtime 0.1.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/dist/common/logger.d.ts +12 -0
- package/dist/common/logger.d.ts.map +1 -0
- package/dist/common/logger.js +23 -0
- package/dist/common/logger.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/intent-pipeline/index.d.ts +65 -0
- package/dist/intent-pipeline/index.d.ts.map +1 -0
- package/dist/intent-pipeline/index.js +244 -0
- package/dist/intent-pipeline/index.js.map +1 -0
- package/dist/intent-pipeline/types.d.ts +78 -0
- package/dist/intent-pipeline/types.d.ts.map +1 -0
- package/dist/intent-pipeline/types.js +17 -0
- package/dist/intent-pipeline/types.js.map +1 -0
- package/dist/proof-committer/index.d.ts +86 -0
- package/dist/proof-committer/index.d.ts.map +1 -0
- package/dist/proof-committer/index.js +252 -0
- package/dist/proof-committer/index.js.map +1 -0
- package/dist/proof-committer/types.d.ts +107 -0
- package/dist/proof-committer/types.d.ts.map +1 -0
- package/dist/proof-committer/types.js +58 -0
- package/dist/proof-committer/types.js.map +1 -0
- package/dist/stores/index.d.ts +10 -0
- package/dist/stores/index.d.ts.map +1 -0
- package/dist/stores/index.js +10 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/sqlite-proof-store.d.ts +61 -0
- package/dist/stores/sqlite-proof-store.d.ts.map +1 -0
- package/dist/stores/sqlite-proof-store.js +239 -0
- package/dist/stores/sqlite-proof-store.js.map +1 -0
- package/dist/stores/sqlite-trust-store.d.ts +124 -0
- package/dist/stores/sqlite-trust-store.d.ts.map +1 -0
- package/dist/stores/sqlite-trust-store.js +297 -0
- package/dist/stores/sqlite-trust-store.js.map +1 -0
- package/dist/trust-facade/index.d.ts +72 -0
- package/dist/trust-facade/index.d.ts.map +1 -0
- package/dist/trust-facade/index.js +410 -0
- package/dist/trust-facade/index.js.map +1 -0
- package/dist/trust-facade/types.d.ts +211 -0
- package/dist/trust-facade/types.d.ts.map +1 -0
- package/dist/trust-facade/types.js +45 -0
- package/dist/trust-facade/types.js.map +1 -0
- package/package.json +84 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite Proof Store
|
|
3
|
+
*
|
|
4
|
+
* Persistent storage for proof commitments and batches using SQLite.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import Database from 'better-sqlite3';
|
|
9
|
+
import { createLogger } from '../common/logger.js';
|
|
10
|
+
const logger = createLogger({ component: 'sqlite-proof-store' });
|
|
11
|
+
/**
|
|
12
|
+
* SQLite implementation of ProofStore
|
|
13
|
+
*/
|
|
14
|
+
export class SQLiteProofStore {
|
|
15
|
+
db;
|
|
16
|
+
config;
|
|
17
|
+
// Prepared statements for performance
|
|
18
|
+
stmts = null;
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.config = {
|
|
21
|
+
walMode: true,
|
|
22
|
+
...config,
|
|
23
|
+
};
|
|
24
|
+
this.db = new Database(this.config.dbPath);
|
|
25
|
+
// Enable WAL mode for better write performance
|
|
26
|
+
if (this.config.walMode) {
|
|
27
|
+
this.db.pragma('journal_mode = WAL');
|
|
28
|
+
}
|
|
29
|
+
this.initializeSchema();
|
|
30
|
+
this.prepareStatements();
|
|
31
|
+
logger.info({ dbPath: this.config.dbPath }, 'SQLite proof store initialized');
|
|
32
|
+
}
|
|
33
|
+
initializeSchema() {
|
|
34
|
+
this.db.exec(`
|
|
35
|
+
-- Batches table
|
|
36
|
+
CREATE TABLE IF NOT EXISTS proof_batches (
|
|
37
|
+
batch_id TEXT PRIMARY KEY,
|
|
38
|
+
merkle_root TEXT NOT NULL,
|
|
39
|
+
signature TEXT NOT NULL,
|
|
40
|
+
created_at TEXT NOT NULL,
|
|
41
|
+
event_count INTEGER NOT NULL
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
-- Commitments table
|
|
45
|
+
CREATE TABLE IF NOT EXISTS proof_commitments (
|
|
46
|
+
id TEXT PRIMARY KEY,
|
|
47
|
+
hash TEXT NOT NULL,
|
|
48
|
+
timestamp INTEGER NOT NULL,
|
|
49
|
+
batch_id TEXT NOT NULL,
|
|
50
|
+
event_type TEXT NOT NULL,
|
|
51
|
+
entity_id TEXT NOT NULL,
|
|
52
|
+
payload TEXT NOT NULL,
|
|
53
|
+
correlation_id TEXT,
|
|
54
|
+
FOREIGN KEY (batch_id) REFERENCES proof_batches(batch_id)
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
-- Index for entity lookups
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_commitments_entity_id ON proof_commitments(entity_id);
|
|
59
|
+
|
|
60
|
+
-- Index for batch lookups
|
|
61
|
+
CREATE INDEX IF NOT EXISTS idx_commitments_batch_id ON proof_commitments(batch_id);
|
|
62
|
+
|
|
63
|
+
-- Index for timestamp queries
|
|
64
|
+
CREATE INDEX IF NOT EXISTS idx_commitments_timestamp ON proof_commitments(timestamp);
|
|
65
|
+
`);
|
|
66
|
+
logger.debug('Database schema initialized');
|
|
67
|
+
}
|
|
68
|
+
prepareStatements() {
|
|
69
|
+
this.stmts = {
|
|
70
|
+
insertBatch: this.db.prepare(`
|
|
71
|
+
INSERT INTO proof_batches (batch_id, merkle_root, signature, created_at, event_count)
|
|
72
|
+
VALUES (@batchId, @merkleRoot, @signature, @createdAt, @eventCount)
|
|
73
|
+
`),
|
|
74
|
+
insertCommitment: this.db.prepare(`
|
|
75
|
+
INSERT INTO proof_commitments (id, hash, timestamp, batch_id, event_type, entity_id, payload, correlation_id)
|
|
76
|
+
VALUES (@id, @hash, @timestamp, @batchId, @eventType, @entityId, @payload, @correlationId)
|
|
77
|
+
`),
|
|
78
|
+
getBatch: this.db.prepare(`
|
|
79
|
+
SELECT batch_id, merkle_root, signature, created_at, event_count
|
|
80
|
+
FROM proof_batches
|
|
81
|
+
WHERE batch_id = ?
|
|
82
|
+
`),
|
|
83
|
+
getCommitment: this.db.prepare(`
|
|
84
|
+
SELECT id, hash, timestamp, batch_id, event_type, entity_id, payload, correlation_id
|
|
85
|
+
FROM proof_commitments
|
|
86
|
+
WHERE id = ?
|
|
87
|
+
`),
|
|
88
|
+
getCommitmentsForEntity: this.db.prepare(`
|
|
89
|
+
SELECT id, hash, timestamp, batch_id, event_type, entity_id, payload, correlation_id
|
|
90
|
+
FROM proof_commitments
|
|
91
|
+
WHERE entity_id = ?
|
|
92
|
+
ORDER BY timestamp DESC
|
|
93
|
+
`),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Write a batch to storage
|
|
98
|
+
*/
|
|
99
|
+
async writeBatch(batch) {
|
|
100
|
+
if (!this.stmts)
|
|
101
|
+
throw new Error('Store not initialized');
|
|
102
|
+
const transaction = this.db.transaction(() => {
|
|
103
|
+
// Insert batch
|
|
104
|
+
this.stmts.insertBatch.run({
|
|
105
|
+
batchId: batch.batchId,
|
|
106
|
+
merkleRoot: batch.merkleRoot,
|
|
107
|
+
signature: batch.signature,
|
|
108
|
+
createdAt: batch.createdAt.toISOString(),
|
|
109
|
+
eventCount: batch.eventCount,
|
|
110
|
+
});
|
|
111
|
+
// Insert all commitments
|
|
112
|
+
for (const commitment of batch.commitments) {
|
|
113
|
+
this.stmts.insertCommitment.run({
|
|
114
|
+
id: commitment.id,
|
|
115
|
+
hash: commitment.hash,
|
|
116
|
+
timestamp: commitment.timestamp,
|
|
117
|
+
batchId: batch.batchId,
|
|
118
|
+
eventType: commitment.event.type,
|
|
119
|
+
entityId: commitment.event.entityId,
|
|
120
|
+
payload: JSON.stringify(commitment.event.payload),
|
|
121
|
+
correlationId: commitment.event.correlationId ?? null,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
transaction();
|
|
126
|
+
logger.debug({ batchId: batch.batchId, commitmentCount: batch.commitments.length }, 'Batch written to SQLite');
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get a batch by ID
|
|
130
|
+
*/
|
|
131
|
+
async getBatch(batchId) {
|
|
132
|
+
if (!this.stmts)
|
|
133
|
+
throw new Error('Store not initialized');
|
|
134
|
+
const row = this.stmts.getBatch.get(batchId);
|
|
135
|
+
if (!row)
|
|
136
|
+
return null;
|
|
137
|
+
// Get all commitments for this batch
|
|
138
|
+
const commitmentRows = this.db.prepare(`
|
|
139
|
+
SELECT id, hash, timestamp, event_type, entity_id, payload, correlation_id
|
|
140
|
+
FROM proof_commitments
|
|
141
|
+
WHERE batch_id = ?
|
|
142
|
+
`).all(batchId);
|
|
143
|
+
const commitments = commitmentRows.map((c) => ({
|
|
144
|
+
id: c.id,
|
|
145
|
+
hash: c.hash,
|
|
146
|
+
timestamp: c.timestamp,
|
|
147
|
+
event: {
|
|
148
|
+
type: c.event_type,
|
|
149
|
+
entityId: c.entity_id,
|
|
150
|
+
payload: JSON.parse(c.payload),
|
|
151
|
+
timestamp: c.timestamp,
|
|
152
|
+
correlationId: c.correlation_id ?? undefined,
|
|
153
|
+
},
|
|
154
|
+
}));
|
|
155
|
+
return {
|
|
156
|
+
batchId: row.batch_id,
|
|
157
|
+
merkleRoot: row.merkle_root,
|
|
158
|
+
signature: row.signature,
|
|
159
|
+
createdAt: new Date(row.created_at),
|
|
160
|
+
eventCount: row.event_count,
|
|
161
|
+
commitments,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get commitment by ID
|
|
166
|
+
*/
|
|
167
|
+
async getCommitment(commitmentId) {
|
|
168
|
+
if (!this.stmts)
|
|
169
|
+
throw new Error('Store not initialized');
|
|
170
|
+
const row = this.stmts.getCommitment.get(commitmentId);
|
|
171
|
+
if (!row)
|
|
172
|
+
return null;
|
|
173
|
+
return {
|
|
174
|
+
id: row.id,
|
|
175
|
+
hash: row.hash,
|
|
176
|
+
timestamp: row.timestamp,
|
|
177
|
+
event: {
|
|
178
|
+
type: row.event_type,
|
|
179
|
+
entityId: row.entity_id,
|
|
180
|
+
payload: JSON.parse(row.payload),
|
|
181
|
+
timestamp: row.timestamp,
|
|
182
|
+
correlationId: row.correlation_id ?? undefined,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get all commitments for an entity
|
|
188
|
+
*/
|
|
189
|
+
async getCommitmentsForEntity(entityId) {
|
|
190
|
+
if (!this.stmts)
|
|
191
|
+
throw new Error('Store not initialized');
|
|
192
|
+
const rows = this.stmts.getCommitmentsForEntity.all(entityId);
|
|
193
|
+
return rows.map((row) => ({
|
|
194
|
+
id: row.id,
|
|
195
|
+
hash: row.hash,
|
|
196
|
+
timestamp: row.timestamp,
|
|
197
|
+
event: {
|
|
198
|
+
type: row.event_type,
|
|
199
|
+
entityId: row.entity_id,
|
|
200
|
+
payload: JSON.parse(row.payload),
|
|
201
|
+
timestamp: row.timestamp,
|
|
202
|
+
correlationId: row.correlation_id ?? undefined,
|
|
203
|
+
},
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get statistics
|
|
208
|
+
*/
|
|
209
|
+
getStats() {
|
|
210
|
+
const batchCount = this.db.prepare('SELECT COUNT(*) as count FROM proof_batches').get();
|
|
211
|
+
const commitmentCount = this.db.prepare('SELECT COUNT(*) as count FROM proof_commitments').get();
|
|
212
|
+
return {
|
|
213
|
+
batches: batchCount.count,
|
|
214
|
+
commitments: commitmentCount.count,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Clear all data (useful for testing)
|
|
219
|
+
*/
|
|
220
|
+
clear() {
|
|
221
|
+
this.db.exec('DELETE FROM proof_commitments');
|
|
222
|
+
this.db.exec('DELETE FROM proof_batches');
|
|
223
|
+
logger.debug('All data cleared');
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Close the database connection
|
|
227
|
+
*/
|
|
228
|
+
close() {
|
|
229
|
+
this.db.close();
|
|
230
|
+
logger.info('SQLite proof store closed');
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Create a new SQLite proof store
|
|
235
|
+
*/
|
|
236
|
+
export function createSQLiteProofStore(config) {
|
|
237
|
+
return new SQLiteProofStore(config);
|
|
238
|
+
}
|
|
239
|
+
//# sourceMappingURL=sqlite-proof-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-proof-store.js","sourceRoot":"","sources":["../../src/stores/sqlite-proof-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAGnD,MAAM,MAAM,GAAG,YAAY,CAAC,EAAE,SAAS,EAAE,oBAAoB,EAAE,CAAC,CAAC;AASjE;;GAEG;AACH,MAAM,OAAO,gBAAgB;IACnB,EAAE,CAAoB;IACtB,MAAM,CAAyB;IAEvC,sCAAsC;IAC9B,KAAK,GAMF,IAAI,CAAC;IAEhB,YAAY,MAA8B;QACxC,IAAI,CAAC,MAAM,GAAG;YACZ,OAAO,EAAE,IAAI;YACb,GAAG,MAAM;SACV,CAAC;QAEF,IAAI,CAAC,EAAE,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAE3C,+CAA+C;QAC/C,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACxB,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;QACvC,CAAC;QAED,IAAI,CAAC,gBAAgB,EAAE,CAAC;QACxB,IAAI,CAAC,iBAAiB,EAAE,CAAC;QAEzB,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,gCAAgC,CAAC,CAAC;IAChF,CAAC;IAEO,g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,KAAK;QACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAChB,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;IAC3C,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAAC,MAA8B;IACnE,OAAO,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite Trust Store
|
|
3
|
+
*
|
|
4
|
+
* Persistent storage for agent trust data using SQLite.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import type { TrustTier, ObservationTier } from '../trust-facade/types.js';
|
|
9
|
+
export interface SQLiteTrustStoreConfig {
|
|
10
|
+
/** Database file path (use ':memory:' for in-memory) */
|
|
11
|
+
dbPath: string;
|
|
12
|
+
/** Enable WAL mode for better concurrency (default: true) */
|
|
13
|
+
walMode?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Agent trust record
|
|
17
|
+
*/
|
|
18
|
+
export interface AgentTrustRecord {
|
|
19
|
+
agentId: string;
|
|
20
|
+
name: string;
|
|
21
|
+
score: number;
|
|
22
|
+
tier: TrustTier;
|
|
23
|
+
observationTier: ObservationTier;
|
|
24
|
+
observationCeiling: number;
|
|
25
|
+
capabilities: string[];
|
|
26
|
+
admittedAt: Date;
|
|
27
|
+
expiresAt: Date;
|
|
28
|
+
lastActivityAt: Date;
|
|
29
|
+
isRevoked: boolean;
|
|
30
|
+
revokedReason?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Trust signal record
|
|
34
|
+
*/
|
|
35
|
+
export interface TrustSignalRecord {
|
|
36
|
+
id: string;
|
|
37
|
+
agentId: string;
|
|
38
|
+
type: 'success' | 'failure' | 'violation' | 'neutral';
|
|
39
|
+
source: string;
|
|
40
|
+
weight: number;
|
|
41
|
+
scoreBefore: number;
|
|
42
|
+
scoreAfter: number;
|
|
43
|
+
context?: Record<string, unknown>;
|
|
44
|
+
timestamp: Date;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Trust store interface
|
|
48
|
+
*/
|
|
49
|
+
export interface TrustStore {
|
|
50
|
+
/** Save or update an agent's trust record */
|
|
51
|
+
saveAgent(record: AgentTrustRecord): Promise<void>;
|
|
52
|
+
/** Get an agent's trust record */
|
|
53
|
+
getAgent(agentId: string): Promise<AgentTrustRecord | null>;
|
|
54
|
+
/** Update an agent's score */
|
|
55
|
+
updateScore(agentId: string, newScore: number, newTier: TrustTier): Promise<void>;
|
|
56
|
+
/** Revoke an agent */
|
|
57
|
+
revokeAgent(agentId: string, reason: string): Promise<void>;
|
|
58
|
+
/** Record a trust signal */
|
|
59
|
+
recordSignal(signal: TrustSignalRecord): Promise<void>;
|
|
60
|
+
/** Get signals for an agent */
|
|
61
|
+
getSignals(agentId: string, limit?: number): Promise<TrustSignalRecord[]>;
|
|
62
|
+
/** List all active agents */
|
|
63
|
+
listActiveAgents(): Promise<AgentTrustRecord[]>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* SQLite implementation of TrustStore
|
|
67
|
+
*/
|
|
68
|
+
export declare class SQLiteTrustStore implements TrustStore {
|
|
69
|
+
private db;
|
|
70
|
+
private config;
|
|
71
|
+
private stmts;
|
|
72
|
+
constructor(config: SQLiteTrustStoreConfig);
|
|
73
|
+
private initializeSchema;
|
|
74
|
+
private prepareStatements;
|
|
75
|
+
/**
|
|
76
|
+
* Save or update an agent's trust record
|
|
77
|
+
*/
|
|
78
|
+
saveAgent(record: AgentTrustRecord): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Get an agent's trust record
|
|
81
|
+
*/
|
|
82
|
+
getAgent(agentId: string): Promise<AgentTrustRecord | null>;
|
|
83
|
+
/**
|
|
84
|
+
* Update an agent's score
|
|
85
|
+
*/
|
|
86
|
+
updateScore(agentId: string, newScore: number, newTier: TrustTier): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Revoke an agent
|
|
89
|
+
*/
|
|
90
|
+
revokeAgent(agentId: string, reason: string): Promise<void>;
|
|
91
|
+
/**
|
|
92
|
+
* Record a trust signal
|
|
93
|
+
*/
|
|
94
|
+
recordSignal(signal: TrustSignalRecord): Promise<void>;
|
|
95
|
+
/**
|
|
96
|
+
* Get signals for an agent
|
|
97
|
+
*/
|
|
98
|
+
getSignals(agentId: string, limit?: number): Promise<TrustSignalRecord[]>;
|
|
99
|
+
/**
|
|
100
|
+
* List all active agents
|
|
101
|
+
*/
|
|
102
|
+
listActiveAgents(): Promise<AgentTrustRecord[]>;
|
|
103
|
+
/**
|
|
104
|
+
* Get statistics
|
|
105
|
+
*/
|
|
106
|
+
getStats(): {
|
|
107
|
+
agents: number;
|
|
108
|
+
activeAgents: number;
|
|
109
|
+
signals: number;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Clear all data (useful for testing)
|
|
113
|
+
*/
|
|
114
|
+
clear(): void;
|
|
115
|
+
/**
|
|
116
|
+
* Close the database connection
|
|
117
|
+
*/
|
|
118
|
+
close(): void;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create a new SQLite trust store
|
|
122
|
+
*/
|
|
123
|
+
export declare function createSQLiteTrustStore(config: SQLiteTrustStoreConfig): SQLiteTrustStore;
|
|
124
|
+
//# sourceMappingURL=sqlite-trust-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-trust-store.d.ts","sourceRoot":"","sources":["../../src/stores/sqlite-trust-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAI3E,MAAM,WAAW,sBAAsB;IACrC,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,OAAO,CAAC,EAAE,OAAO,CAAC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gBAAgB,EAAE,CAAC;IAkCrD;;OAEG;IACH,QAAQ,IAAI;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;IAYrE;;OAEG;IACH,KAAK,IAAI,IAAI;IAMb;;OAEG;IACH,KAAK,IAAI,IAAI;CAId;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,sBAAsB,GAAG,gBAAgB,CAEvF"}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SQLite Trust Store
|
|
3
|
+
*
|
|
4
|
+
* Persistent storage for agent trust data using SQLite.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
import Database from 'better-sqlite3';
|
|
9
|
+
import { createLogger } from '../common/logger.js';
|
|
10
|
+
const logger = createLogger({ component: 'sqlite-trust-store' });
|
|
11
|
+
/**
|
|
12
|
+
* SQLite implementation of TrustStore
|
|
13
|
+
*/
|
|
14
|
+
export class SQLiteTrustStore {
|
|
15
|
+
db;
|
|
16
|
+
config;
|
|
17
|
+
// Prepared statements
|
|
18
|
+
stmts = null;
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.config = {
|
|
21
|
+
walMode: true,
|
|
22
|
+
...config,
|
|
23
|
+
};
|
|
24
|
+
this.db = new Database(this.config.dbPath);
|
|
25
|
+
if (this.config.walMode) {
|
|
26
|
+
this.db.pragma('journal_mode = WAL');
|
|
27
|
+
}
|
|
28
|
+
this.initializeSchema();
|
|
29
|
+
this.prepareStatements();
|
|
30
|
+
logger.info({ dbPath: this.config.dbPath }, 'SQLite trust store initialized');
|
|
31
|
+
}
|
|
32
|
+
initializeSchema() {
|
|
33
|
+
this.db.exec(`
|
|
34
|
+
-- Agents table
|
|
35
|
+
CREATE TABLE IF NOT EXISTS agents (
|
|
36
|
+
agent_id TEXT PRIMARY KEY,
|
|
37
|
+
name TEXT NOT NULL,
|
|
38
|
+
score INTEGER NOT NULL,
|
|
39
|
+
tier INTEGER NOT NULL,
|
|
40
|
+
observation_tier TEXT NOT NULL,
|
|
41
|
+
observation_ceiling INTEGER NOT NULL,
|
|
42
|
+
capabilities TEXT NOT NULL,
|
|
43
|
+
admitted_at TEXT NOT NULL,
|
|
44
|
+
expires_at TEXT NOT NULL,
|
|
45
|
+
last_activity_at TEXT NOT NULL,
|
|
46
|
+
is_revoked INTEGER NOT NULL DEFAULT 0,
|
|
47
|
+
revoked_reason TEXT
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
-- Trust signals table
|
|
51
|
+
CREATE TABLE IF NOT EXISTS trust_signals (
|
|
52
|
+
id TEXT PRIMARY KEY,
|
|
53
|
+
agent_id TEXT NOT NULL,
|
|
54
|
+
type TEXT NOT NULL,
|
|
55
|
+
source TEXT NOT NULL,
|
|
56
|
+
weight REAL NOT NULL,
|
|
57
|
+
score_before INTEGER NOT NULL,
|
|
58
|
+
score_after INTEGER NOT NULL,
|
|
59
|
+
context TEXT,
|
|
60
|
+
timestamp TEXT NOT NULL,
|
|
61
|
+
FOREIGN KEY (agent_id) REFERENCES agents(agent_id)
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
-- Indexes
|
|
65
|
+
CREATE INDEX IF NOT EXISTS idx_signals_agent_id ON trust_signals(agent_id);
|
|
66
|
+
CREATE INDEX IF NOT EXISTS idx_signals_timestamp ON trust_signals(timestamp);
|
|
67
|
+
CREATE INDEX IF NOT EXISTS idx_agents_is_revoked ON agents(is_revoked);
|
|
68
|
+
`);
|
|
69
|
+
logger.debug('Trust store schema initialized');
|
|
70
|
+
}
|
|
71
|
+
prepareStatements() {
|
|
72
|
+
this.stmts = {
|
|
73
|
+
upsertAgent: this.db.prepare(`
|
|
74
|
+
INSERT INTO agents (
|
|
75
|
+
agent_id, name, score, tier, observation_tier, observation_ceiling,
|
|
76
|
+
capabilities, admitted_at, expires_at, last_activity_at, is_revoked, revoked_reason
|
|
77
|
+
) VALUES (
|
|
78
|
+
@agentId, @name, @score, @tier, @observationTier, @observationCeiling,
|
|
79
|
+
@capabilities, @admittedAt, @expiresAt, @lastActivityAt, @isRevoked, @revokedReason
|
|
80
|
+
)
|
|
81
|
+
ON CONFLICT(agent_id) DO UPDATE SET
|
|
82
|
+
name = @name,
|
|
83
|
+
score = @score,
|
|
84
|
+
tier = @tier,
|
|
85
|
+
observation_tier = @observationTier,
|
|
86
|
+
observation_ceiling = @observationCeiling,
|
|
87
|
+
capabilities = @capabilities,
|
|
88
|
+
expires_at = @expiresAt,
|
|
89
|
+
last_activity_at = @lastActivityAt,
|
|
90
|
+
is_revoked = @isRevoked,
|
|
91
|
+
revoked_reason = @revokedReason
|
|
92
|
+
`),
|
|
93
|
+
getAgent: this.db.prepare(`
|
|
94
|
+
SELECT agent_id, name, score, tier, observation_tier, observation_ceiling,
|
|
95
|
+
capabilities, admitted_at, expires_at, last_activity_at, is_revoked, revoked_reason
|
|
96
|
+
FROM agents
|
|
97
|
+
WHERE agent_id = ?
|
|
98
|
+
`),
|
|
99
|
+
updateScore: this.db.prepare(`
|
|
100
|
+
UPDATE agents
|
|
101
|
+
SET score = @score, tier = @tier, last_activity_at = @lastActivityAt
|
|
102
|
+
WHERE agent_id = @agentId
|
|
103
|
+
`),
|
|
104
|
+
revokeAgent: this.db.prepare(`
|
|
105
|
+
UPDATE agents
|
|
106
|
+
SET is_revoked = 1, revoked_reason = @reason, last_activity_at = @lastActivityAt
|
|
107
|
+
WHERE agent_id = @agentId
|
|
108
|
+
`),
|
|
109
|
+
insertSignal: this.db.prepare(`
|
|
110
|
+
INSERT INTO trust_signals (id, agent_id, type, source, weight, score_before, score_after, context, timestamp)
|
|
111
|
+
VALUES (@id, @agentId, @type, @source, @weight, @scoreBefore, @scoreAfter, @context, @timestamp)
|
|
112
|
+
`),
|
|
113
|
+
getSignals: this.db.prepare(`
|
|
114
|
+
SELECT id, agent_id, type, source, weight, score_before, score_after, context, timestamp
|
|
115
|
+
FROM trust_signals
|
|
116
|
+
WHERE agent_id = ?
|
|
117
|
+
ORDER BY timestamp DESC
|
|
118
|
+
LIMIT ?
|
|
119
|
+
`),
|
|
120
|
+
listActiveAgents: this.db.prepare(`
|
|
121
|
+
SELECT agent_id, name, score, tier, observation_tier, observation_ceiling,
|
|
122
|
+
capabilities, admitted_at, expires_at, last_activity_at, is_revoked, revoked_reason
|
|
123
|
+
FROM agents
|
|
124
|
+
WHERE is_revoked = 0
|
|
125
|
+
ORDER BY last_activity_at DESC
|
|
126
|
+
`),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Save or update an agent's trust record
|
|
131
|
+
*/
|
|
132
|
+
async saveAgent(record) {
|
|
133
|
+
if (!this.stmts)
|
|
134
|
+
throw new Error('Store not initialized');
|
|
135
|
+
this.stmts.upsertAgent.run({
|
|
136
|
+
agentId: record.agentId,
|
|
137
|
+
name: record.name,
|
|
138
|
+
score: record.score,
|
|
139
|
+
tier: record.tier,
|
|
140
|
+
observationTier: record.observationTier,
|
|
141
|
+
observationCeiling: record.observationCeiling,
|
|
142
|
+
capabilities: JSON.stringify(record.capabilities),
|
|
143
|
+
admittedAt: record.admittedAt.toISOString(),
|
|
144
|
+
expiresAt: record.expiresAt.toISOString(),
|
|
145
|
+
lastActivityAt: record.lastActivityAt.toISOString(),
|
|
146
|
+
isRevoked: record.isRevoked ? 1 : 0,
|
|
147
|
+
revokedReason: record.revokedReason ?? null,
|
|
148
|
+
});
|
|
149
|
+
logger.debug({ agentId: record.agentId }, 'Agent record saved');
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get an agent's trust record
|
|
153
|
+
*/
|
|
154
|
+
async getAgent(agentId) {
|
|
155
|
+
if (!this.stmts)
|
|
156
|
+
throw new Error('Store not initialized');
|
|
157
|
+
const row = this.stmts.getAgent.get(agentId);
|
|
158
|
+
if (!row)
|
|
159
|
+
return null;
|
|
160
|
+
return {
|
|
161
|
+
agentId: row.agent_id,
|
|
162
|
+
name: row.name,
|
|
163
|
+
score: row.score,
|
|
164
|
+
tier: row.tier,
|
|
165
|
+
observationTier: row.observation_tier,
|
|
166
|
+
observationCeiling: row.observation_ceiling,
|
|
167
|
+
capabilities: JSON.parse(row.capabilities),
|
|
168
|
+
admittedAt: new Date(row.admitted_at),
|
|
169
|
+
expiresAt: new Date(row.expires_at),
|
|
170
|
+
lastActivityAt: new Date(row.last_activity_at),
|
|
171
|
+
isRevoked: row.is_revoked === 1,
|
|
172
|
+
revokedReason: row.revoked_reason ?? undefined,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Update an agent's score
|
|
177
|
+
*/
|
|
178
|
+
async updateScore(agentId, newScore, newTier) {
|
|
179
|
+
if (!this.stmts)
|
|
180
|
+
throw new Error('Store not initialized');
|
|
181
|
+
this.stmts.updateScore.run({
|
|
182
|
+
agentId,
|
|
183
|
+
score: newScore,
|
|
184
|
+
tier: newTier,
|
|
185
|
+
lastActivityAt: new Date().toISOString(),
|
|
186
|
+
});
|
|
187
|
+
logger.debug({ agentId, newScore, newTier }, 'Agent score updated');
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Revoke an agent
|
|
191
|
+
*/
|
|
192
|
+
async revokeAgent(agentId, reason) {
|
|
193
|
+
if (!this.stmts)
|
|
194
|
+
throw new Error('Store not initialized');
|
|
195
|
+
this.stmts.revokeAgent.run({
|
|
196
|
+
agentId,
|
|
197
|
+
reason,
|
|
198
|
+
lastActivityAt: new Date().toISOString(),
|
|
199
|
+
});
|
|
200
|
+
logger.warn({ agentId, reason }, 'Agent revoked');
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Record a trust signal
|
|
204
|
+
*/
|
|
205
|
+
async recordSignal(signal) {
|
|
206
|
+
if (!this.stmts)
|
|
207
|
+
throw new Error('Store not initialized');
|
|
208
|
+
this.stmts.insertSignal.run({
|
|
209
|
+
id: signal.id,
|
|
210
|
+
agentId: signal.agentId,
|
|
211
|
+
type: signal.type,
|
|
212
|
+
source: signal.source,
|
|
213
|
+
weight: signal.weight,
|
|
214
|
+
scoreBefore: signal.scoreBefore,
|
|
215
|
+
scoreAfter: signal.scoreAfter,
|
|
216
|
+
context: signal.context ? JSON.stringify(signal.context) : null,
|
|
217
|
+
timestamp: signal.timestamp.toISOString(),
|
|
218
|
+
});
|
|
219
|
+
logger.debug({ signalId: signal.id, agentId: signal.agentId, type: signal.type }, 'Signal recorded');
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get signals for an agent
|
|
223
|
+
*/
|
|
224
|
+
async getSignals(agentId, limit = 100) {
|
|
225
|
+
if (!this.stmts)
|
|
226
|
+
throw new Error('Store not initialized');
|
|
227
|
+
const rows = this.stmts.getSignals.all(agentId, limit);
|
|
228
|
+
return rows.map((row) => ({
|
|
229
|
+
id: row.id,
|
|
230
|
+
agentId: row.agent_id,
|
|
231
|
+
type: row.type,
|
|
232
|
+
source: row.source,
|
|
233
|
+
weight: row.weight,
|
|
234
|
+
scoreBefore: row.score_before,
|
|
235
|
+
scoreAfter: row.score_after,
|
|
236
|
+
context: row.context ? JSON.parse(row.context) : undefined,
|
|
237
|
+
timestamp: new Date(row.timestamp),
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* List all active agents
|
|
242
|
+
*/
|
|
243
|
+
async listActiveAgents() {
|
|
244
|
+
if (!this.stmts)
|
|
245
|
+
throw new Error('Store not initialized');
|
|
246
|
+
const rows = this.stmts.listActiveAgents.all();
|
|
247
|
+
return rows.map((row) => ({
|
|
248
|
+
agentId: row.agent_id,
|
|
249
|
+
name: row.name,
|
|
250
|
+
score: row.score,
|
|
251
|
+
tier: row.tier,
|
|
252
|
+
observationTier: row.observation_tier,
|
|
253
|
+
observationCeiling: row.observation_ceiling,
|
|
254
|
+
capabilities: JSON.parse(row.capabilities),
|
|
255
|
+
admittedAt: new Date(row.admitted_at),
|
|
256
|
+
expiresAt: new Date(row.expires_at),
|
|
257
|
+
lastActivityAt: new Date(row.last_activity_at),
|
|
258
|
+
isRevoked: row.is_revoked === 1,
|
|
259
|
+
revokedReason: row.revoked_reason ?? undefined,
|
|
260
|
+
}));
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Get statistics
|
|
264
|
+
*/
|
|
265
|
+
getStats() {
|
|
266
|
+
const agentCount = this.db.prepare('SELECT COUNT(*) as count FROM agents').get();
|
|
267
|
+
const activeCount = this.db.prepare('SELECT COUNT(*) as count FROM agents WHERE is_revoked = 0').get();
|
|
268
|
+
const signalCount = this.db.prepare('SELECT COUNT(*) as count FROM trust_signals').get();
|
|
269
|
+
return {
|
|
270
|
+
agents: agentCount.count,
|
|
271
|
+
activeAgents: activeCount.count,
|
|
272
|
+
signals: signalCount.count,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Clear all data (useful for testing)
|
|
277
|
+
*/
|
|
278
|
+
clear() {
|
|
279
|
+
this.db.exec('DELETE FROM trust_signals');
|
|
280
|
+
this.db.exec('DELETE FROM agents');
|
|
281
|
+
logger.debug('All trust data cleared');
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Close the database connection
|
|
285
|
+
*/
|
|
286
|
+
close() {
|
|
287
|
+
this.db.close();
|
|
288
|
+
logger.info('SQLite trust store closed');
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Create a new SQLite trust store
|
|
293
|
+
*/
|
|
294
|
+
export function createSQLiteTrustStore(config) {
|
|
295
|
+
return new SQLiteTrustStore(config);
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=sqlite-trust-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-trust-store.js","sourceRoot":"","sources":["../../src/stores/sqlite-trust-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,QAAQ,MAAM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