@agent-deck/backend 1.11.6 → 1.11.8
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/.tsbuildinfo +1 -1
- package/dist/models/database.d.ts +80 -1
- package/dist/models/database.d.ts.map +1 -1
- package/dist/models/database.js +256 -0
- package/dist/models/database.js.map +1 -1
- package/dist/routes/playbook-patches.d.ts.map +1 -1
- package/dist/routes/playbook-patches.js +8 -2
- package/dist/routes/playbook-patches.js.map +1 -1
- package/dist/routes/playbooks.d.ts.map +1 -1
- package/dist/routes/playbooks.js +21 -0
- package/dist/routes/playbooks.js.map +1 -1
- package/dist/routes/services.d.ts.map +1 -1
- package/dist/routes/services.js +52 -0
- package/dist/routes/services.js.map +1 -1
- package/dist/routes/usage.d.ts +8 -0
- package/dist/routes/usage.d.ts.map +1 -0
- package/dist/routes/usage.js +130 -0
- package/dist/routes/usage.js.map +1 -0
- package/dist/scope/display.d.ts +1 -1
- package/dist/scope/display.d.ts.map +1 -1
- package/dist/scope/display.js +167 -6
- package/dist/scope/display.js.map +1 -1
- package/dist/scope/live-display-registry.d.ts +24 -2
- package/dist/scope/live-display-registry.d.ts.map +1 -1
- package/dist/scope/live-display-registry.js +17 -9
- package/dist/scope/live-display-registry.js.map +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -0
- package/dist/server/index.js.map +1 -1
- package/dist/services/card-usage-classifier.d.ts +49 -0
- package/dist/services/card-usage-classifier.d.ts.map +1 -0
- package/dist/services/card-usage-classifier.js +105 -0
- package/dist/services/card-usage-classifier.js.map +1 -0
- package/dist/trusted-session/route-policy-registry.d.ts.map +1 -1
- package/dist/trusted-session/route-policy-registry.js +4 -0
- package/dist/trusted-session/route-policy-registry.js.map +1 -1
- package/package.json +2 -2
- package/static-ui/assets/index-CQf3Vouc.css +1 -0
- package/static-ui/assets/index-DCImqPRz.js +235 -0
- package/static-ui/index.html +2 -2
- package/static-ui/assets/index-BzI4_7mn.js +0 -235
- package/static-ui/assets/index-CN9PFSm4.css +0 -1
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import Database from 'better-sqlite3';
|
|
2
|
-
import { Service, CreateServiceInput, UpdateServiceInput, Deck, CreateDeckInput, UpdateDeckInput, DeckService, AddServiceToDeckInput, RemoveServiceFromDeckInput, ReorderDeckServicesInput, Credential, AddCredentialToDeckInput, RemoveCredentialFromDeckInput, DeckCredential, ExecRun, Playbook, CreatePlaybookInput, UpdatePlaybookInput, AddPlaybookToDeckInput, RemovePlaybookFromDeckInput, DeckPlaybook, PlaybookPatch, PlaybookVersion, PlaybookEvent, FeedbackSignal, FeedbackSignalSource, FeedbackSignalStatus, StoreCredentialMeta, StoreDeck, StorePlaybookFile, StoreService } from '@agent-deck/shared';
|
|
2
|
+
import { Service, CreateServiceInput, UpdateServiceInput, Deck, CreateDeckInput, UpdateDeckInput, DeckService, AddServiceToDeckInput, RemoveServiceFromDeckInput, ReorderDeckServicesInput, Credential, AddCredentialToDeckInput, RemoveCredentialFromDeckInput, DeckCredential, ExecRun, Playbook, CreatePlaybookInput, UpdatePlaybookInput, AddPlaybookToDeckInput, RemovePlaybookFromDeckInput, DeckPlaybook, PlaybookPatch, PlaybookVersion, PlaybookEvent, CardUsageCardType, CardUsageEvent, FeedbackSignal, FeedbackSignalSource, FeedbackSignalStatus, StoreCredentialMeta, StoreDeck, StorePlaybookFile, StoreService } from '@agent-deck/shared';
|
|
3
3
|
export declare const STORE_CONTENT_HASH = "store_content_hash";
|
|
4
4
|
export declare const STORE_LAST_REINDEX = "store_last_reindex";
|
|
5
|
+
/**
|
|
6
|
+
* One-way hash for card-usage session correlation (NOT-292).
|
|
7
|
+
*
|
|
8
|
+
* The `x-agent-deck-session-id` header value is a live runtime-session
|
|
9
|
+
* bearer: presenting it alone authenticates as that agent. It must never be
|
|
10
|
+
* persisted verbatim nor exposed via the usage API, which any agent session
|
|
11
|
+
* or notebook credential can call. SHA-256 keeps per-session correlation for
|
|
12
|
+
* analysis while making the stored value unusable as a credential.
|
|
13
|
+
*/
|
|
14
|
+
export declare function hashCardUsageSessionId(raw: string | null | undefined): string | null;
|
|
5
15
|
export type StoreSnapshot = {
|
|
6
16
|
services: StoreService[];
|
|
7
17
|
credentials: StoreCredentialMeta[];
|
|
@@ -96,6 +106,13 @@ export declare class DatabaseManager {
|
|
|
96
106
|
getDeckPlaybooks(deckId: string): Promise<DeckPlaybook[]>;
|
|
97
107
|
listDeckNamesForPlaybook(playbookId: string): Promise<string[]>;
|
|
98
108
|
upsertDeckWorkspace(workspaceRoot: string, deckId: string): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* NOT-296: latest saved deck assignment for a workspace root (stub-sync
|
|
111
|
+
* registry). Used only when no live MCP session matches, so the status
|
|
112
|
+
* line can label the saved deck as a workspace default instead of
|
|
113
|
+
* implying it is the current session deck.
|
|
114
|
+
*/
|
|
115
|
+
getLatestDeckIdForWorkspace(workspaceRoot: string): Promise<string | null>;
|
|
99
116
|
listDeckWorkspaces(deckId: string): Promise<Array<{
|
|
100
117
|
workspaceRoot: string;
|
|
101
118
|
lastBoundAt: string;
|
|
@@ -140,6 +157,68 @@ export declare class DatabaseManager {
|
|
|
140
157
|
source: string;
|
|
141
158
|
}): Promise<PlaybookEvent>;
|
|
142
159
|
countPlaybookEvents(playbookId: string, event?: 'fetched'): Promise<number>;
|
|
160
|
+
private static readonly CARD_USAGE_CARD_TYPES;
|
|
161
|
+
private static usageObservationKey;
|
|
162
|
+
/**
|
|
163
|
+
* Mark the moment usage tracking began for every card type, so a fully
|
|
164
|
+
* observed zero-use window (timestamp set, no events) stays
|
|
165
|
+
* distinguishable from a store that predates tracking. INSERT OR IGNORE
|
|
166
|
+
* keeps the earliest start across restarts and upgrades.
|
|
167
|
+
*/
|
|
168
|
+
private seedUsageObservationStarts;
|
|
169
|
+
/**
|
|
170
|
+
* Observation start for a card type: when tracking began. A timestamp with
|
|
171
|
+
* no events in the window is a fully observed zero-use window; null only
|
|
172
|
+
* means the store predates usage tracking. Lets downstream classification
|
|
173
|
+
* distinguish "no evidence yet" (null) from observed zero use.
|
|
174
|
+
*/
|
|
175
|
+
getUsageObservationStart(cardType: CardUsageCardType): Promise<string | null>;
|
|
176
|
+
getUsageObservationStarts(): Promise<Record<CardUsageCardType, string | null>>;
|
|
177
|
+
private ensureUsageObservationStart;
|
|
178
|
+
private mapCardUsageEventRow;
|
|
179
|
+
recordCardUsageEvent(input: {
|
|
180
|
+
id?: string;
|
|
181
|
+
cardType: CardUsageCardType;
|
|
182
|
+
cardId: string;
|
|
183
|
+
deckId?: string | null;
|
|
184
|
+
action: string;
|
|
185
|
+
success?: boolean | null;
|
|
186
|
+
source: string;
|
|
187
|
+
sessionId?: string | null;
|
|
188
|
+
occurredAt?: string;
|
|
189
|
+
}): Promise<CardUsageEvent>;
|
|
190
|
+
static encodeCardUsageCursor(createdAt: string, id: string): string;
|
|
191
|
+
static decodeCardUsageCursor(cursor: string): {
|
|
192
|
+
createdAt: string;
|
|
193
|
+
id: string;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Chronological, cursor-paginated raw usage events. Ordering is
|
|
197
|
+
* deterministic: (created_at ASC, id ASC). Both bounds are inclusive.
|
|
198
|
+
*/
|
|
199
|
+
listCardUsageEvents(input: {
|
|
200
|
+
from: string;
|
|
201
|
+
to: string;
|
|
202
|
+
cursor?: string | null;
|
|
203
|
+
limit: number;
|
|
204
|
+
}): Promise<{
|
|
205
|
+
events: CardUsageEvent[];
|
|
206
|
+
nextCursor: string | null;
|
|
207
|
+
}>;
|
|
208
|
+
/**
|
|
209
|
+
* Successful usage per card in `[windowStart, windowEnd)`. Only
|
|
210
|
+
* `success = 1` events count; failures, unknowns, and out-of-window
|
|
211
|
+
* events are ignored. Cards without in-window successes are absent.
|
|
212
|
+
*/
|
|
213
|
+
getSuccessfulCardUsageSummary(input: {
|
|
214
|
+
windowStart: string;
|
|
215
|
+
windowEnd: string;
|
|
216
|
+
}): Promise<Array<{
|
|
217
|
+
cardType: CardUsageCardType;
|
|
218
|
+
cardId: string;
|
|
219
|
+
usageCount: number;
|
|
220
|
+
lastUsedAt: string;
|
|
221
|
+
}>>;
|
|
143
222
|
private mapFeedbackSignalRow;
|
|
144
223
|
createFeedbackSignal(input: {
|
|
145
224
|
id: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/models/database.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/models/database.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAEtC,OAAO,EACL,OAAO,EACP,kBAAkB,EAClB,kBAAkB,EAClB,IAAI,EACJ,eAAe,EACf,eAAe,EACf,WAAW,EACX,qBAAqB,EACrB,0BAA0B,EAC1B,wBAAwB,EACxB,UAAU,EACV,wBAAwB,EACxB,6BAA6B,EAC7B,cAAc,EACd,OAAO,EACP,QAAQ,EACR,mBAAmB,EACnB,mBAAmB,EACnB,sBAAsB,EACtB,2BAA2B,EAC3B,YAAY,EACZ,aAAa,EACb,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,cAAc,EACd,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,SAAS,EACT,iBAAiB,EACjB,YAAY,EACb,MAAM,oBAAoB,CAAC;AAO5B,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AACvD,eAAO,MAAM,kBAAkB,uBAAuB,CAAC;AAEvD;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CASpF;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,WAAW,EAAE,mBAAmB,EAAE,CAAC;IACnC,SA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AAG,SAAqB,GACnD,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;IAiBjC,iFAAiF;IAC3E,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAW7D,6DAA6D;IACvD,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAWvD,6BAA6B,CAAC,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC;IAO7E,iBAAiB,IAAI,QAAQ,CAAC,QAAQ;IAItC,KAAK,IAAI,IAAI;CAGd"}
|
package/dist/models/database.js
CHANGED
|
@@ -4,10 +4,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.DatabaseManager = exports.STORE_LAST_REINDEX = exports.STORE_CONTENT_HASH = void 0;
|
|
7
|
+
exports.hashCardUsageSessionId = hashCardUsageSessionId;
|
|
7
8
|
const better_sqlite3_1 = __importDefault(require("better-sqlite3"));
|
|
9
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
10
|
const shared_1 = require("@agent-deck/shared");
|
|
9
11
|
exports.STORE_CONTENT_HASH = 'store_content_hash';
|
|
10
12
|
exports.STORE_LAST_REINDEX = 'store_last_reindex';
|
|
13
|
+
/**
|
|
14
|
+
* One-way hash for card-usage session correlation (NOT-292).
|
|
15
|
+
*
|
|
16
|
+
* The `x-agent-deck-session-id` header value is a live runtime-session
|
|
17
|
+
* bearer: presenting it alone authenticates as that agent. It must never be
|
|
18
|
+
* persisted verbatim nor exposed via the usage API, which any agent session
|
|
19
|
+
* or notebook credential can call. SHA-256 keeps per-session correlation for
|
|
20
|
+
* analysis while making the stored value unusable as a credential.
|
|
21
|
+
*/
|
|
22
|
+
function hashCardUsageSessionId(raw) {
|
|
23
|
+
if (typeof raw !== 'string') {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const trimmed = raw.trim();
|
|
27
|
+
if (!trimmed) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
return (0, node_crypto_1.createHash)('sha256').update(trimmed, 'utf8').digest('hex');
|
|
31
|
+
}
|
|
11
32
|
class DatabaseManager {
|
|
12
33
|
db;
|
|
13
34
|
constructor(dbPath = 'agent_deck.db') {
|
|
@@ -15,6 +36,7 @@ class DatabaseManager {
|
|
|
15
36
|
this.createTables();
|
|
16
37
|
this.migrate();
|
|
17
38
|
this.createIndexes();
|
|
39
|
+
this.seedUsageObservationStarts();
|
|
18
40
|
}
|
|
19
41
|
tableExists(tableName) {
|
|
20
42
|
const row = this.db
|
|
@@ -293,6 +315,21 @@ class DatabaseManager {
|
|
|
293
315
|
created_at TEXT NOT NULL,
|
|
294
316
|
FOREIGN KEY (playbook_id) REFERENCES playbooks (id) ON DELETE CASCADE
|
|
295
317
|
)
|
|
318
|
+
`);
|
|
319
|
+
// Normalized, privacy-safe card-usage events (NOT-292). No payloads,
|
|
320
|
+
// arguments, results, URLs, headers, or secrets are stored here.
|
|
321
|
+
this.db.exec(`
|
|
322
|
+
CREATE TABLE IF NOT EXISTS card_usage_events (
|
|
323
|
+
id TEXT PRIMARY KEY,
|
|
324
|
+
card_type TEXT NOT NULL,
|
|
325
|
+
card_id TEXT NOT NULL,
|
|
326
|
+
deck_id TEXT,
|
|
327
|
+
action TEXT NOT NULL,
|
|
328
|
+
success INTEGER,
|
|
329
|
+
source TEXT NOT NULL,
|
|
330
|
+
session_id TEXT,
|
|
331
|
+
created_at TEXT NOT NULL
|
|
332
|
+
)
|
|
296
333
|
`);
|
|
297
334
|
this.db.exec(`
|
|
298
335
|
CREATE TABLE IF NOT EXISTS feedback_signals (
|
|
@@ -533,6 +570,8 @@ class DatabaseManager {
|
|
|
533
570
|
CREATE INDEX IF NOT EXISTS idx_playbook_patches_status ON playbook_patches(status);
|
|
534
571
|
CREATE INDEX IF NOT EXISTS idx_playbook_versions_playbook ON playbook_versions(playbook_id);
|
|
535
572
|
CREATE INDEX IF NOT EXISTS idx_playbook_events_playbook ON playbook_events(playbook_id);
|
|
573
|
+
CREATE INDEX IF NOT EXISTS idx_card_usage_card ON card_usage_events(card_type, card_id, created_at);
|
|
574
|
+
CREATE INDEX IF NOT EXISTS idx_card_usage_time ON card_usage_events(created_at, id);
|
|
536
575
|
CREATE INDEX IF NOT EXISTS idx_feedback_signals_status ON feedback_signals(status);
|
|
537
576
|
CREATE INDEX IF NOT EXISTS idx_feedback_signals_playbook ON feedback_signals(candidate_playbook_id);
|
|
538
577
|
CREATE INDEX IF NOT EXISTS idx_feedback_signals_linked_patch ON feedback_signals(linked_patch_id);
|
|
@@ -1203,6 +1242,19 @@ class DatabaseManager {
|
|
|
1203
1242
|
const insertCredential = this.db.prepare(`
|
|
1204
1243
|
INSERT INTO exec_run_credentials (exec_run_id, credential_id)
|
|
1205
1244
|
VALUES (@exec_run_id, @credential_id)
|
|
1245
|
+
`);
|
|
1246
|
+
// NOT-292: credential-backed exec runs count as credential use. Only
|
|
1247
|
+
// credential ids are recorded — never command contents or secrets.
|
|
1248
|
+
// Recorded inside the same transaction so a committed exec run always
|
|
1249
|
+
// has its usage events and vice versa.
|
|
1250
|
+
const execSuccess = input.exitCode === undefined ? null : input.exitCode === 0;
|
|
1251
|
+
const execOccurredAt = input.finishedAt ?? input.startedAt;
|
|
1252
|
+
const insertUsageEvent = this.db.prepare(`
|
|
1253
|
+
INSERT INTO card_usage_events (
|
|
1254
|
+
id, card_type, card_id, deck_id, action, success, source, session_id, created_at
|
|
1255
|
+
) VALUES (
|
|
1256
|
+
@id, @card_type, @card_id, @deck_id, @action, @success, @source, @session_id, @created_at
|
|
1257
|
+
)
|
|
1206
1258
|
`);
|
|
1207
1259
|
const transaction = this.db.transaction(() => {
|
|
1208
1260
|
insertRun.run({
|
|
@@ -1220,6 +1272,22 @@ class DatabaseManager {
|
|
|
1220
1272
|
credential_id: credentialId,
|
|
1221
1273
|
});
|
|
1222
1274
|
}
|
|
1275
|
+
for (const credentialId of input.credentialIds) {
|
|
1276
|
+
insertUsageEvent.run({
|
|
1277
|
+
id: (0, shared_1.generateId)(),
|
|
1278
|
+
card_type: 'credential',
|
|
1279
|
+
card_id: credentialId,
|
|
1280
|
+
deck_id: execRun.deckId ?? null,
|
|
1281
|
+
action: 'exec_run',
|
|
1282
|
+
success: execSuccess === null ? null : execSuccess ? 1 : 0,
|
|
1283
|
+
source: 'exec',
|
|
1284
|
+
session_id: null,
|
|
1285
|
+
created_at: execOccurredAt,
|
|
1286
|
+
});
|
|
1287
|
+
}
|
|
1288
|
+
if (input.credentialIds.length > 0) {
|
|
1289
|
+
this.ensureUsageObservationStart('credential', execOccurredAt);
|
|
1290
|
+
}
|
|
1223
1291
|
});
|
|
1224
1292
|
transaction();
|
|
1225
1293
|
return execRun;
|
|
@@ -1396,6 +1464,22 @@ class DatabaseManager {
|
|
|
1396
1464
|
last_bound_at: now,
|
|
1397
1465
|
});
|
|
1398
1466
|
}
|
|
1467
|
+
/**
|
|
1468
|
+
* NOT-296: latest saved deck assignment for a workspace root (stub-sync
|
|
1469
|
+
* registry). Used only when no live MCP session matches, so the status
|
|
1470
|
+
* line can label the saved deck as a workspace default instead of
|
|
1471
|
+
* implying it is the current session deck.
|
|
1472
|
+
*/
|
|
1473
|
+
async getLatestDeckIdForWorkspace(workspaceRoot) {
|
|
1474
|
+
const row = this.db.prepare(`
|
|
1475
|
+
SELECT deck_id
|
|
1476
|
+
FROM deck_workspaces
|
|
1477
|
+
WHERE workspace_root = ?
|
|
1478
|
+
ORDER BY last_bound_at DESC
|
|
1479
|
+
LIMIT 1
|
|
1480
|
+
`).get(workspaceRoot);
|
|
1481
|
+
return row?.deck_id ?? null;
|
|
1482
|
+
}
|
|
1399
1483
|
async listDeckWorkspaces(deckId) {
|
|
1400
1484
|
const rows = this.db.prepare(`
|
|
1401
1485
|
SELECT workspace_root, last_bound_at
|
|
@@ -1614,6 +1698,178 @@ class DatabaseManager {
|
|
|
1614
1698
|
.get(playbookId, event);
|
|
1615
1699
|
return row.count;
|
|
1616
1700
|
}
|
|
1701
|
+
static CARD_USAGE_CARD_TYPES = [
|
|
1702
|
+
'service',
|
|
1703
|
+
'credential',
|
|
1704
|
+
'playbook',
|
|
1705
|
+
];
|
|
1706
|
+
static usageObservationKey(cardType) {
|
|
1707
|
+
return `card_usage_observed_from:${cardType}`;
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* Mark the moment usage tracking began for every card type, so a fully
|
|
1711
|
+
* observed zero-use window (timestamp set, no events) stays
|
|
1712
|
+
* distinguishable from a store that predates tracking. INSERT OR IGNORE
|
|
1713
|
+
* keeps the earliest start across restarts and upgrades.
|
|
1714
|
+
*/
|
|
1715
|
+
seedUsageObservationStarts() {
|
|
1716
|
+
const now = new Date().toISOString();
|
|
1717
|
+
const stmt = this.db.prepare('INSERT OR IGNORE INTO store_meta (key, value) VALUES (?, ?)');
|
|
1718
|
+
for (const cardType of DatabaseManager.CARD_USAGE_CARD_TYPES) {
|
|
1719
|
+
stmt.run(DatabaseManager.usageObservationKey(cardType), now);
|
|
1720
|
+
}
|
|
1721
|
+
}
|
|
1722
|
+
/**
|
|
1723
|
+
* Observation start for a card type: when tracking began. A timestamp with
|
|
1724
|
+
* no events in the window is a fully observed zero-use window; null only
|
|
1725
|
+
* means the store predates usage tracking. Lets downstream classification
|
|
1726
|
+
* distinguish "no evidence yet" (null) from observed zero use.
|
|
1727
|
+
*/
|
|
1728
|
+
async getUsageObservationStart(cardType) {
|
|
1729
|
+
return this.getStoreMeta(DatabaseManager.usageObservationKey(cardType));
|
|
1730
|
+
}
|
|
1731
|
+
async getUsageObservationStarts() {
|
|
1732
|
+
return {
|
|
1733
|
+
service: await this.getUsageObservationStart('service'),
|
|
1734
|
+
credential: await this.getUsageObservationStart('credential'),
|
|
1735
|
+
playbook: await this.getUsageObservationStart('playbook'),
|
|
1736
|
+
};
|
|
1737
|
+
}
|
|
1738
|
+
ensureUsageObservationStart(cardType, at) {
|
|
1739
|
+
this.db
|
|
1740
|
+
.prepare('INSERT OR IGNORE INTO store_meta (key, value) VALUES (?, ?)')
|
|
1741
|
+
.run(DatabaseManager.usageObservationKey(cardType), at);
|
|
1742
|
+
}
|
|
1743
|
+
mapCardUsageEventRow(row) {
|
|
1744
|
+
return {
|
|
1745
|
+
id: row.id,
|
|
1746
|
+
cardType: row.card_type,
|
|
1747
|
+
cardId: row.card_id,
|
|
1748
|
+
deckId: row.deck_id ?? null,
|
|
1749
|
+
action: row.action,
|
|
1750
|
+
success: row.success === null || row.success === undefined ? null : row.success === 1,
|
|
1751
|
+
source: row.source,
|
|
1752
|
+
sessionId: row.session_id ?? null,
|
|
1753
|
+
createdAt: row.created_at,
|
|
1754
|
+
};
|
|
1755
|
+
}
|
|
1756
|
+
async recordCardUsageEvent(input) {
|
|
1757
|
+
if (!DatabaseManager.CARD_USAGE_CARD_TYPES.includes(input.cardType)) {
|
|
1758
|
+
throw new Error(`Unknown card usage card type: ${input.cardType}`);
|
|
1759
|
+
}
|
|
1760
|
+
const event = {
|
|
1761
|
+
id: input.id ?? (0, shared_1.generateId)(),
|
|
1762
|
+
cardType: input.cardType,
|
|
1763
|
+
cardId: input.cardId,
|
|
1764
|
+
deckId: input.deckId ?? null,
|
|
1765
|
+
action: input.action,
|
|
1766
|
+
success: input.success ?? null,
|
|
1767
|
+
source: input.source,
|
|
1768
|
+
// Never persist the raw runtime-session bearer — only its one-way hash.
|
|
1769
|
+
sessionId: hashCardUsageSessionId(input.sessionId ?? null),
|
|
1770
|
+
createdAt: input.occurredAt ?? new Date().toISOString(),
|
|
1771
|
+
};
|
|
1772
|
+
this.db.prepare(`
|
|
1773
|
+
INSERT INTO card_usage_events (
|
|
1774
|
+
id, card_type, card_id, deck_id, action, success, source, session_id, created_at
|
|
1775
|
+
) VALUES (
|
|
1776
|
+
@id, @card_type, @card_id, @deck_id, @action, @success, @source, @session_id, @created_at
|
|
1777
|
+
)
|
|
1778
|
+
`).run({
|
|
1779
|
+
id: event.id,
|
|
1780
|
+
card_type: event.cardType,
|
|
1781
|
+
card_id: event.cardId,
|
|
1782
|
+
deck_id: event.deckId,
|
|
1783
|
+
action: event.action,
|
|
1784
|
+
success: event.success === null ? null : event.success ? 1 : 0,
|
|
1785
|
+
source: event.source,
|
|
1786
|
+
session_id: event.sessionId,
|
|
1787
|
+
created_at: event.createdAt,
|
|
1788
|
+
});
|
|
1789
|
+
this.ensureUsageObservationStart(event.cardType, event.createdAt);
|
|
1790
|
+
return event;
|
|
1791
|
+
}
|
|
1792
|
+
static encodeCardUsageCursor(createdAt, id) {
|
|
1793
|
+
return Buffer.from(JSON.stringify({ c: createdAt, i: id }), 'utf8').toString('base64url');
|
|
1794
|
+
}
|
|
1795
|
+
static decodeCardUsageCursor(cursor) {
|
|
1796
|
+
let parsed;
|
|
1797
|
+
try {
|
|
1798
|
+
parsed = JSON.parse(Buffer.from(cursor, 'base64url').toString('utf8'));
|
|
1799
|
+
}
|
|
1800
|
+
catch {
|
|
1801
|
+
throw new Error('Invalid cursor');
|
|
1802
|
+
}
|
|
1803
|
+
if (typeof parsed !== 'object' ||
|
|
1804
|
+
parsed === null ||
|
|
1805
|
+
typeof parsed.c !== 'string' ||
|
|
1806
|
+
typeof parsed.i !== 'string' ||
|
|
1807
|
+
Number.isNaN(Date.parse(parsed.c))) {
|
|
1808
|
+
throw new Error('Invalid cursor');
|
|
1809
|
+
}
|
|
1810
|
+
return {
|
|
1811
|
+
createdAt: parsed.c,
|
|
1812
|
+
id: parsed.i,
|
|
1813
|
+
};
|
|
1814
|
+
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Chronological, cursor-paginated raw usage events. Ordering is
|
|
1817
|
+
* deterministic: (created_at ASC, id ASC). Both bounds are inclusive.
|
|
1818
|
+
*/
|
|
1819
|
+
async listCardUsageEvents(input) {
|
|
1820
|
+
let after = null;
|
|
1821
|
+
if (input.cursor) {
|
|
1822
|
+
after = DatabaseManager.decodeCardUsageCursor(input.cursor);
|
|
1823
|
+
}
|
|
1824
|
+
const rows = this.db.prepare(`
|
|
1825
|
+
SELECT * FROM card_usage_events
|
|
1826
|
+
WHERE created_at >= @from
|
|
1827
|
+
AND created_at <= @to
|
|
1828
|
+
AND (
|
|
1829
|
+
@after_created_at IS NULL
|
|
1830
|
+
OR created_at > @after_created_at
|
|
1831
|
+
OR (created_at = @after_created_at AND id > @after_id)
|
|
1832
|
+
)
|
|
1833
|
+
ORDER BY created_at ASC, id ASC
|
|
1834
|
+
LIMIT @limit
|
|
1835
|
+
`).all({
|
|
1836
|
+
from: input.from,
|
|
1837
|
+
to: input.to,
|
|
1838
|
+
after_created_at: after?.createdAt ?? null,
|
|
1839
|
+
after_id: after?.id ?? null,
|
|
1840
|
+
limit: input.limit + 1,
|
|
1841
|
+
});
|
|
1842
|
+
const page = rows.slice(0, input.limit);
|
|
1843
|
+
const events = page.map((row) => this.mapCardUsageEventRow(row));
|
|
1844
|
+
const nextCursor = rows.length > input.limit && events.length > 0
|
|
1845
|
+
? DatabaseManager.encodeCardUsageCursor(events[events.length - 1].createdAt, events[events.length - 1].id)
|
|
1846
|
+
: null;
|
|
1847
|
+
return { events, nextCursor };
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Successful usage per card in `[windowStart, windowEnd)`. Only
|
|
1851
|
+
* `success = 1` events count; failures, unknowns, and out-of-window
|
|
1852
|
+
* events are ignored. Cards without in-window successes are absent.
|
|
1853
|
+
*/
|
|
1854
|
+
async getSuccessfulCardUsageSummary(input) {
|
|
1855
|
+
const rows = this.db.prepare(`
|
|
1856
|
+
SELECT card_type, card_id, COUNT(*) AS usage_count, MAX(created_at) AS last_used_at
|
|
1857
|
+
FROM card_usage_events
|
|
1858
|
+
WHERE success = 1
|
|
1859
|
+
AND created_at >= @windowStart
|
|
1860
|
+
AND created_at < @windowEnd
|
|
1861
|
+
GROUP BY card_type, card_id
|
|
1862
|
+
`).all({
|
|
1863
|
+
windowStart: input.windowStart,
|
|
1864
|
+
windowEnd: input.windowEnd,
|
|
1865
|
+
});
|
|
1866
|
+
return rows.map((row) => ({
|
|
1867
|
+
cardType: row.card_type,
|
|
1868
|
+
cardId: row.card_id,
|
|
1869
|
+
usageCount: Number(row.usage_count),
|
|
1870
|
+
lastUsedAt: row.last_used_at,
|
|
1871
|
+
}));
|
|
1872
|
+
}
|
|
1617
1873
|
mapFeedbackSignalRow(row) {
|
|
1618
1874
|
return {
|
|
1619
1875
|
id: row.id,
|