cursedbelt-server 4.47.0 → 4.48.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.
Files changed (36) hide show
  1. package/dist/server/d1/pullD1.js +14 -1
  2. package/dist/server/engagement/actions.d.ts +20 -0
  3. package/dist/server/engagement/actions.js +62 -0
  4. package/dist/server/engagement/api.d.ts +13 -2
  5. package/dist/server/engagement/api.js +30 -2
  6. package/dist/server/engagement/d1.d.ts +13 -2
  7. package/dist/server/engagement/d1.js +30 -2
  8. package/dist/server/engagement/index.d.ts +4 -3
  9. package/dist/server/engagement/index.js +2 -1
  10. package/dist/server/engagement/manifestPlaces.d.ts +7 -1
  11. package/dist/server/engagement/manifestPlaces.js +21 -0
  12. package/dist/server/engagement/places.d.ts +3 -0
  13. package/dist/server/engagement/places.js +9 -2
  14. package/dist/server/engagement/policy.d.ts +21 -0
  15. package/dist/server/engagement/policy.js +27 -0
  16. package/dist/server/engagement/store.d.ts +12 -3
  17. package/dist/server/engagement/store.js +30 -2
  18. package/dist/server/engagement/summary.d.ts +15 -1
  19. package/dist/server/engagement/summary.js +15 -0
  20. package/dist/server/engagement/types.d.ts +9 -0
  21. package/docs/engagement.md +10 -1
  22. package/package.json +1 -1
  23. package/src/server/d1/pullD1.spec.ts +12 -0
  24. package/src/server/d1/pullD1.ts +16 -1
  25. package/src/server/engagement/actions.ts +71 -0
  26. package/src/server/engagement/api.ts +36 -1
  27. package/src/server/engagement/d1.spec.ts +49 -3
  28. package/src/server/engagement/d1.ts +35 -2
  29. package/src/server/engagement/engagement.spec.ts +102 -0
  30. package/src/server/engagement/index.ts +4 -1
  31. package/src/server/engagement/manifestPlaces.ts +21 -1
  32. package/src/server/engagement/places.ts +12 -2
  33. package/src/server/engagement/policy.ts +34 -0
  34. package/src/server/engagement/store.ts +33 -4
  35. package/src/server/engagement/summary.ts +33 -0
  36. package/src/server/engagement/types.ts +10 -0
@@ -141,6 +141,40 @@ export function normalizePlaceId(raw: string): string {
141
141
  );
142
142
  }
143
143
 
144
+ /**
145
+ * Resolve the action a browser reported to one of the app's DECLARED actions (task 3470).
146
+ *
147
+ * A place says where somebody was; an action says what they DID there — a search run, a track
148
+ * played, a download started. The owner's question ("what features do they use and ignore") is
149
+ * about the second, and it carries the same risk as the first: the obvious way to record "a
150
+ * search" is to record the search. So rule 1 holds for actions exactly as for places — an action
151
+ * is a NAME from the app's own `routes.manifest.json` `actions` list, never a value. There is no
152
+ * field for the query, the track or the URL, and anything off the list records as
153
+ * {@link OTHER_PLACE}, so an app that forgot to declare a name sees an `other` row rather than
154
+ * a feature that reads as unused.
155
+ */
156
+ export function resolveAction(claimed: string, allowed: readonly (string | ActionRef)[]): string {
157
+ const want = normalizeActionId(claimed);
158
+ if (!want) return OTHER_PLACE;
159
+ for (const entry of allowed) {
160
+ const id = typeof entry === "string" ? entry : entry.id;
161
+ if (normalizeActionId(id) === want) return id;
162
+ }
163
+ return OTHER_PLACE;
164
+ }
165
+
166
+ /** One declared action — `routes.manifest.json`'s `actions[]`. */
167
+ export interface ActionRef {
168
+ id: string;
169
+ /** What the console shows beside the id — "play a track". */
170
+ label?: string;
171
+ }
172
+
173
+ /** `Play ` → `play`. Case and surrounding space are one action, not two. */
174
+ export function normalizeActionId(raw: string): string {
175
+ return raw.trim().toLowerCase();
176
+ }
177
+
144
178
  /**
145
179
  * How long a gap between two views before the second one starts a new session.
146
180
  *
@@ -34,10 +34,11 @@
34
34
  import { Database } from "bun:sqlite";
35
35
  import { mkdirSync } from "node:fs";
36
36
  import { dirname, resolve } from "node:path";
37
+ import { type ActionUse, actionStatements, ENGAGEMENT_ACTION_DDL } from "./actions.js";
37
38
  import { dayKey, foldView } from "./policy.js";
38
- import type { EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
39
+ import type { EngagementActionUse, EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
39
40
 
40
- export type { EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
41
+ export type { EngagementActionUse, EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
41
42
 
42
43
  /**
43
44
  * `bun:sqlite`'s `create: true` creates the FILE, never the directory above it, so a first boot
@@ -123,6 +124,8 @@ export class EngagementStore {
123
124
  last_ts INTEGER NOT NULL,
124
125
  last_place TEXT NOT NULL DEFAULT ''
125
126
  )`);
127
+ // Feature actions (task 3470) — `actions.ts` says why they are their own two tables.
128
+ for (const ddl of ENGAGEMENT_ACTION_DDL) this.handle.run(ddl);
126
129
  }
127
130
 
128
131
  /**
@@ -202,6 +205,30 @@ export class EngagementStore {
202
205
  return fold;
203
206
  }
204
207
 
208
+ /**
209
+ * Count one resolved action. Like {@link recordView}, `place` and `action` must ALREADY be
210
+ * resolved against the app's manifest — the door (`api.ts`) does it. Touches no clock and no
211
+ * dwell: an action is something done on the page the view already counted.
212
+ */
213
+ recordAction(use: ActionUse): void {
214
+ const statements = actionStatements(use);
215
+ this.handle.transaction(() => {
216
+ for (const { sql, params } of statements) this.handle.run(sql, params);
217
+ })();
218
+ if (++this.writes % 256 === 0) this.prune(use.ts);
219
+ }
220
+
221
+ /** Every (user, place, action) lifetime roll-up, most used first. */
222
+ actions(): EngagementActionUse[] {
223
+ return this.handle
224
+ .query<EngagementActionUse, []>(
225
+ `SELECT user_key AS userKey, place, action, uses, first_ts AS firstTs, last_ts AS lastTs
226
+ FROM engagement_actions
227
+ ORDER BY uses DESC`,
228
+ )
229
+ .all();
230
+ }
231
+
205
232
  /** Every account's roll-up for this app, most recently seen first. */
206
233
  users(): EngagementUser[] {
207
234
  return this.handle
@@ -241,14 +268,16 @@ export class EngagementStore {
241
268
  /**
242
269
  * Drop daily rows older than the retention window.
243
270
  *
244
- * 🔴 Only `engagement_days`. The two roll-ups are deliberately untouched —
271
+ * 🔴 Only the daily grains (`engagement_days`, `engagement_action_days`). The roll-ups are deliberately untouched —
245
272
  * see the file header. An agent "completing" this by trimming them too would
246
273
  * delete the only record that somebody used to be here, which is the entire
247
274
  * drop-off signal.
248
275
  */
249
276
  prune(at: number = Date.now()): number {
250
277
  const cutoff = dayKey(at - this.retentionDays * 86_400_000);
251
- return this.handle.run("DELETE FROM engagement_days WHERE day < ?", [cutoff]).changes;
278
+ // The action DAYS age out with the view days; `engagement_actions` is a roll-up and stays.
279
+ const actionDays = this.handle.run("DELETE FROM engagement_action_days WHERE day < ?", [cutoff]).changes;
280
+ return this.handle.run("DELETE FROM engagement_days WHERE day < ?", [cutoff]).changes + actionDays;
252
281
  }
253
282
 
254
283
  /** Row counts + age bounds, for the retention card. */
@@ -16,6 +16,7 @@
16
16
  import {
17
17
  classifyInterest,
18
18
  dayKey,
19
+ type EngagementActionUse,
19
20
  type EngagementDay,
20
21
  type EngagementUser,
21
22
  type EngagementUserPlace,
@@ -50,6 +51,17 @@ export interface PlaceEngagement {
50
51
  msPerView: number;
51
52
  }
52
53
 
54
+ /** One declared action on one place, across every account — "what features they use" (task 3470). */
55
+ export interface ActionEngagement {
56
+ place: string;
57
+ name: string;
58
+ /** Times it was used, every account together. */
59
+ count: number;
60
+ /** Distinct accounts that have used it. */
61
+ accounts: number;
62
+ lastTs: number;
63
+ }
64
+
53
65
  /** One day of the app's whole activity. */
54
66
  export interface DailyEngagement {
55
67
  day: string;
@@ -74,6 +86,8 @@ export interface AppEngagement {
74
86
  lastTs: number;
75
87
  }>;
76
88
  daily: DailyEngagement[];
89
+ /** Per place, per action — most used first. Empty for an app that records no actions. */
90
+ actions: ActionEngagement[];
77
91
  totals: {
78
92
  users: number;
79
93
  views: number;
@@ -94,6 +108,8 @@ export interface SummarizeInput {
94
108
  users: EngagementUser[];
95
109
  places: EngagementUserPlace[];
96
110
  days: EngagementDay[];
111
+ /** The (user, place, action) roll-ups. Absent → no actions. */
112
+ actions?: EngagementActionUse[];
97
113
  }
98
114
 
99
115
  export function summarize(input: SummarizeInput): AppEngagement {
@@ -168,6 +184,22 @@ export function summarize(input: SummarizeInput): AppEngagement {
168
184
  }))
169
185
  .sort((a, b) => b.views - a.views);
170
186
 
187
+ const byAction = new Map<string, ActionEngagement>();
188
+ for (const row of input.actions ?? []) {
189
+ const key = `${row.place}\u0000${row.action}`;
190
+ const existing = byAction.get(key);
191
+ if (existing) {
192
+ existing.count += row.uses;
193
+ existing.accounts += 1;
194
+ existing.lastTs = Math.max(existing.lastTs, row.lastTs);
195
+ } else {
196
+ byAction.set(key, { place: row.place, name: row.action, count: row.uses, accounts: 1, lastTs: row.lastTs });
197
+ }
198
+ }
199
+ const actions = [...byAction.values()].sort(
200
+ (a, b) => b.count - a.count || a.place.localeCompare(b.place) || a.name.localeCompare(b.name),
201
+ );
202
+
171
203
  return {
172
204
  app,
173
205
  generatedAt: now,
@@ -175,6 +207,7 @@ export function summarize(input: SummarizeInput): AppEngagement {
175
207
  places,
176
208
  userPlaces,
177
209
  daily,
210
+ actions,
178
211
  totals: {
179
212
  users: users.length,
180
213
  views: users.reduce((n, u) => n + u.views, 0),
@@ -48,6 +48,16 @@ export interface EngagementUserPlace {
48
48
  lastTs: number;
49
49
  }
50
50
 
51
+ /** One (user, place, action) lifetime roll-up — "who uses this feature, and when last" (task 3470). */
52
+ export interface EngagementActionUse {
53
+ userKey: string;
54
+ place: string;
55
+ action: string;
56
+ uses: number;
57
+ firstTs: number;
58
+ lastTs: number;
59
+ }
60
+
51
61
  /** One account's relationship with the whole app. */
52
62
  export interface EngagementUser {
53
63
  userKey: string;