cursedbelt-server 4.47.1 → 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.
- package/dist/server/engagement/actions.d.ts +20 -0
- package/dist/server/engagement/actions.js +62 -0
- package/dist/server/engagement/api.d.ts +13 -2
- package/dist/server/engagement/api.js +30 -2
- package/dist/server/engagement/d1.d.ts +13 -2
- package/dist/server/engagement/d1.js +30 -2
- package/dist/server/engagement/index.d.ts +4 -3
- package/dist/server/engagement/index.js +2 -1
- package/dist/server/engagement/manifestPlaces.d.ts +7 -1
- package/dist/server/engagement/manifestPlaces.js +21 -0
- package/dist/server/engagement/places.d.ts +3 -0
- package/dist/server/engagement/places.js +9 -2
- package/dist/server/engagement/policy.d.ts +21 -0
- package/dist/server/engagement/policy.js +27 -0
- package/dist/server/engagement/store.d.ts +12 -3
- package/dist/server/engagement/store.js +30 -2
- package/dist/server/engagement/summary.d.ts +15 -1
- package/dist/server/engagement/summary.js +15 -0
- package/dist/server/engagement/types.d.ts +9 -0
- package/docs/engagement.md +10 -1
- package/package.json +1 -1
- package/src/server/engagement/actions.ts +71 -0
- package/src/server/engagement/api.ts +36 -1
- package/src/server/engagement/d1.spec.ts +49 -3
- package/src/server/engagement/d1.ts +35 -2
- package/src/server/engagement/engagement.spec.ts +102 -0
- package/src/server/engagement/index.ts +4 -1
- package/src/server/engagement/manifestPlaces.ts +21 -1
- package/src/server/engagement/places.ts +12 -2
- package/src/server/engagement/policy.ts +34 -0
- package/src/server/engagement/store.ts +33 -4
- package/src/server/engagement/summary.ts +33 -0
- package/src/server/engagement/types.ts +10 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const ENGAGEMENT_ACTION_DDL: readonly string[];
|
|
2
|
+
/** One resolved action, ready to count. `place` and `action` have been through the allowlists. */
|
|
3
|
+
export interface ActionUse {
|
|
4
|
+
userKey: string;
|
|
5
|
+
place: string;
|
|
6
|
+
action: string;
|
|
7
|
+
ts: number;
|
|
8
|
+
}
|
|
9
|
+
/** The two upserts one action is — run them together (a transaction, or one D1 batch). */
|
|
10
|
+
export declare function actionStatements(use: ActionUse): Array<{
|
|
11
|
+
sql: string;
|
|
12
|
+
params: (string | number)[];
|
|
13
|
+
}>;
|
|
14
|
+
/** Read the claimed `{ place, action }` out of a request body — a string each, or a reason why not. */
|
|
15
|
+
export declare function claimedAction(body: unknown): {
|
|
16
|
+
place: string;
|
|
17
|
+
action: string;
|
|
18
|
+
} | {
|
|
19
|
+
error: string;
|
|
20
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Feature ACTIONS — what somebody DID on a page, beside where they were (task 3470).
|
|
3
|
+
*
|
|
4
|
+
* A place-view answers "which pages do they open". The owner's question goes one step further:
|
|
5
|
+
* *"what features they use and ignore … and know which users to ask first when I am planning to
|
|
6
|
+
* add or delete a feature"*. A page is not a feature — music's library page holds play, search,
|
|
7
|
+
* rate and download — so this records a declared ACTION NAME per (account, place).
|
|
8
|
+
*
|
|
9
|
+
* ── The privacy contract is the places' contract, unchanged ─────────────────
|
|
10
|
+
* The action is a NAME from the app's own `routes.manifest.json` `actions` list
|
|
11
|
+
* (`policy.ts:resolveAction`); the place is resolved against the same manifest as a view. There is
|
|
12
|
+
* no field for a value — `search`, never the query; `play`, never the track — so nothing a person
|
|
13
|
+
* typed or chose can reach a row. The account is the session's subject, never the body's.
|
|
14
|
+
*
|
|
15
|
+
* ── Counts and clocks, two grains ───────────────────────────────────────────
|
|
16
|
+
* engagement_actions (user, place, action) → uses, first, last. LIFETIME, never trimmed:
|
|
17
|
+
* it is the row that answers "who uses this most, and when did they last",
|
|
18
|
+
* and trimming it would make a feature's old regulars vanish.
|
|
19
|
+
* engagement_action_days (user, place, action, day) → uses. Trimmed with the view days, for
|
|
20
|
+
* "used this month" against "used once, long ago".
|
|
21
|
+
* No event log, no sequence: an action does not touch the sessionization clock or dwell.
|
|
22
|
+
*
|
|
23
|
+
* 🔴 Its own DDL, not appended to `ENGAGEMENT_DDL`: an app whose schema is generated from that list
|
|
24
|
+
* (music, family, desk, arch) would otherwise go red on the version bump alone. An app opts in by
|
|
25
|
+
* declaring `actions` and applying {@link ENGAGEMENT_ACTION_DDL}; the Bun store always creates both.
|
|
26
|
+
*
|
|
27
|
+
* Pure and Worker-safe: the Bun store and the D1 recorder both write through
|
|
28
|
+
* {@link actionStatements}, so there is one write, not two that agree.
|
|
29
|
+
*/
|
|
30
|
+
import { dayKey } from "./policy.js";
|
|
31
|
+
export const ENGAGEMENT_ACTION_DDL = [
|
|
32
|
+
"CREATE TABLE IF NOT EXISTS engagement_actions ( user_key TEXT NOT NULL, place TEXT NOT NULL, action TEXT NOT NULL, uses INTEGER NOT NULL DEFAULT 0, first_ts INTEGER NOT NULL, last_ts INTEGER NOT NULL, PRIMARY KEY (user_key, place, action) )",
|
|
33
|
+
"CREATE TABLE IF NOT EXISTS engagement_action_days ( user_key TEXT NOT NULL, place TEXT NOT NULL, action TEXT NOT NULL, day TEXT NOT NULL, uses INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (user_key, place, action, day) )",
|
|
34
|
+
"CREATE INDEX IF NOT EXISTS idx_engagement_action_days_day ON engagement_action_days (day)",
|
|
35
|
+
];
|
|
36
|
+
/** The two upserts one action is — run them together (a transaction, or one D1 batch). */
|
|
37
|
+
export function actionStatements(use) {
|
|
38
|
+
const { userKey, place, action, ts } = use;
|
|
39
|
+
return [
|
|
40
|
+
{
|
|
41
|
+
sql: `INSERT INTO engagement_actions (user_key, place, action, uses, first_ts, last_ts)
|
|
42
|
+
VALUES (?, ?, ?, 1, ?, ?)
|
|
43
|
+
ON CONFLICT(user_key, place, action) DO UPDATE SET uses = uses + 1, last_ts = excluded.last_ts`,
|
|
44
|
+
params: [userKey, place, action, ts, ts],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
sql: `INSERT INTO engagement_action_days (user_key, place, action, day, uses)
|
|
48
|
+
VALUES (?, ?, ?, ?, 1)
|
|
49
|
+
ON CONFLICT(user_key, place, action, day) DO UPDATE SET uses = uses + 1`,
|
|
50
|
+
params: [userKey, place, action, dayKey(ts)],
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
/** Read the claimed `{ place, action }` out of a request body — a string each, or a reason why not. */
|
|
55
|
+
export function claimedAction(body) {
|
|
56
|
+
const b = body;
|
|
57
|
+
if (typeof b?.action !== "string")
|
|
58
|
+
return { error: "action must be a string" };
|
|
59
|
+
if (typeof b.place !== "string")
|
|
60
|
+
return { error: "place must be a string" };
|
|
61
|
+
return { place: b.place, action: b.action };
|
|
62
|
+
}
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* POST /api/engagement/view → the beacon. SESSION-gated; the user id comes
|
|
5
5
|
* from the session, never from the body.
|
|
6
|
+
* POST /api/engagement/action → a feature used on a page — `{ place, action }`, both
|
|
7
|
+
* NAMES resolved against the manifest (`actions.ts`).
|
|
8
|
+
* Mounted only for an app that passes `actions`.
|
|
6
9
|
*
|
|
7
10
|
* 🔴 **There is no HTTP read surface, on purpose** (task `113`/`268`, 2026-09-22).
|
|
8
11
|
* `GET /api/admin/engagement` used to sit here behind `SATELLITE_METRICS_TOKEN` —
|
|
@@ -31,7 +34,7 @@
|
|
|
31
34
|
*/
|
|
32
35
|
import { Hono } from "hono";
|
|
33
36
|
import type { Context } from "hono";
|
|
34
|
-
import { type PlaceRef } from "./policy.js";
|
|
37
|
+
import { type ActionRef, type PlaceRef } from "./policy.js";
|
|
35
38
|
import type { EngagementStore } from "./store.js";
|
|
36
39
|
import { type AppEngagement } from "./summary.js";
|
|
37
40
|
export interface EngagementApiOptions {
|
|
@@ -44,6 +47,12 @@ export interface EngagementApiOptions {
|
|
|
44
47
|
* rule 1 for why this is the whole privacy story.
|
|
45
48
|
*/
|
|
46
49
|
places: readonly PlaceRef[];
|
|
50
|
+
/**
|
|
51
|
+
* The allowlist of ACTION names — the manifest's `actions` (`engagementActionsFor`). Present,
|
|
52
|
+
* the recorder grows `POST /action`; an undeclared name records as `other`, as a place does.
|
|
53
|
+
* Absent, there is no action door at all (task 3470).
|
|
54
|
+
*/
|
|
55
|
+
actions?: readonly ActionRef[];
|
|
47
56
|
/** Resolve the CURRENT request's account. Null → 401, nothing recorded. */
|
|
48
57
|
resolveUser: (c: Context) => string | null;
|
|
49
58
|
/** Injectable clock, so the tests are not timing-dependent. */
|
|
@@ -56,6 +65,8 @@ export interface AppEngagementSnapshot extends AppEngagement {
|
|
|
56
65
|
/** The manifest ids this app CAN report — so a reader can show a place
|
|
57
66
|
* with zero views, which is the half of the question that is easy to lose. */
|
|
58
67
|
knownPlaces: string[];
|
|
68
|
+
/** The action names this app declares — a declared action nobody used is the "ignored" half. */
|
|
69
|
+
knownActions: string[];
|
|
59
70
|
returnCurve: Array<{
|
|
60
71
|
day: string;
|
|
61
72
|
joined: number;
|
|
@@ -68,4 +79,4 @@ export interface AppEngagementSnapshot extends AppEngagement {
|
|
|
68
79
|
oldestDay: string | null;
|
|
69
80
|
};
|
|
70
81
|
}
|
|
71
|
-
export declare function readAppEngagement(store: EngagementStore, app: string, now: number, knownPlaces?: readonly PlaceRef[]): AppEngagementSnapshot;
|
|
82
|
+
export declare function readAppEngagement(store: EngagementStore, app: string, now: number, knownPlaces?: readonly PlaceRef[], knownActions?: readonly ActionRef[]): AppEngagementSnapshot;
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* POST /api/engagement/view → the beacon. SESSION-gated; the user id comes
|
|
5
5
|
* from the session, never from the body.
|
|
6
|
+
* POST /api/engagement/action → a feature used on a page — `{ place, action }`, both
|
|
7
|
+
* NAMES resolved against the manifest (`actions.ts`).
|
|
8
|
+
* Mounted only for an app that passes `actions`.
|
|
6
9
|
*
|
|
7
10
|
* 🔴 **There is no HTTP read surface, on purpose** (task `113`/`268`, 2026-09-22).
|
|
8
11
|
* `GET /api/admin/engagement` used to sit here behind `SATELLITE_METRICS_TOKEN` —
|
|
@@ -30,7 +33,8 @@
|
|
|
30
33
|
* rule and its reason: `docs/engagement.md` §2 in this package.
|
|
31
34
|
*/
|
|
32
35
|
import { Hono } from "hono";
|
|
33
|
-
import {
|
|
36
|
+
import { claimedAction } from "./actions.js";
|
|
37
|
+
import { OTHER_PLACE, resolveAction, resolvePlace } from "./policy.js";
|
|
34
38
|
import { returnCurve, summarize } from "./summary.js";
|
|
35
39
|
/** The recorder half — session-gated, mounted at `/api/engagement`. */
|
|
36
40
|
export function createEngagementRecorder(options) {
|
|
@@ -61,9 +65,31 @@ export function createEngagementRecorder(options) {
|
|
|
61
65
|
// unpopular feature.
|
|
62
66
|
return c.json({ ok: true, place, bucketed: place === OTHER_PLACE, ...fold });
|
|
63
67
|
});
|
|
68
|
+
const actions = options.actions;
|
|
69
|
+
if (actions) {
|
|
70
|
+
app.post("/action", async (c) => {
|
|
71
|
+
const userKey = options.resolveUser(c);
|
|
72
|
+
if (!userKey)
|
|
73
|
+
return c.json({ error: "unauthenticated" }, 401);
|
|
74
|
+
let body;
|
|
75
|
+
try {
|
|
76
|
+
body = await c.req.json();
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return c.json({ error: "expected a json body" }, 400);
|
|
80
|
+
}
|
|
81
|
+
const claimed = claimedAction(body);
|
|
82
|
+
if ("error" in claimed)
|
|
83
|
+
return c.json({ error: claimed.error }, 400);
|
|
84
|
+
const place = resolvePlace(claimed.place, options.places);
|
|
85
|
+
const action = resolveAction(claimed.action, actions);
|
|
86
|
+
options.store.recordAction({ userKey, place, action, ts: now() });
|
|
87
|
+
return c.json({ ok: true, place, action, bucketed: place === OTHER_PLACE || action === OTHER_PLACE });
|
|
88
|
+
});
|
|
89
|
+
}
|
|
64
90
|
return app;
|
|
65
91
|
}
|
|
66
|
-
export function readAppEngagement(store, app, now, knownPlaces = []) {
|
|
92
|
+
export function readAppEngagement(store, app, now, knownPlaces = [], knownActions = []) {
|
|
67
93
|
const users = store.users();
|
|
68
94
|
const summary = summarize({
|
|
69
95
|
app,
|
|
@@ -74,10 +100,12 @@ export function readAppEngagement(store, app, now, knownPlaces = []) {
|
|
|
74
100
|
// silently return less, which reads as a quiet period rather than as a
|
|
75
101
|
// window boundary.
|
|
76
102
|
days: store.days(now - 120 * 86_400_000),
|
|
103
|
+
actions: store.actions(),
|
|
77
104
|
});
|
|
78
105
|
return {
|
|
79
106
|
...summary,
|
|
80
107
|
knownPlaces: knownPlaces.map((p) => p.id),
|
|
108
|
+
knownActions: knownActions.map((a) => a.id),
|
|
81
109
|
returnCurve: returnCurve(users, now),
|
|
82
110
|
retention: store.stats(),
|
|
83
111
|
};
|
|
@@ -31,13 +31,16 @@
|
|
|
31
31
|
*
|
|
32
32
|
* gated.route("/api/engagement", createD1EngagementRecorder({
|
|
33
33
|
* db, places: placesFromManifest(routesManifest), resolveUser: (c) => sso.userOf(c)?.id ?? null,
|
|
34
|
+
* // Optional — feature actions (task 3470); the D1 then needs ENGAGEMENT_ACTION_DDL too.
|
|
35
|
+
* actions: actionsFromManifest(routesManifest),
|
|
34
36
|
* }));
|
|
35
37
|
* ```
|
|
36
38
|
*/
|
|
37
39
|
import type { D1LikeDatabase } from "../d1/types.js";
|
|
38
40
|
import type { Context } from "hono";
|
|
39
41
|
import { Hono } from "hono";
|
|
40
|
-
import { type
|
|
42
|
+
import { type ActionUse } from "./actions.js";
|
|
43
|
+
import { type ActionRef, type PlaceRef } from "./policy.js";
|
|
41
44
|
/** The kit store's own tables and indexes, as one-line DDL — see the header. */
|
|
42
45
|
export declare const ENGAGEMENT_DDL: readonly string[];
|
|
43
46
|
/**
|
|
@@ -53,6 +56,8 @@ export declare function recordViewD1(db: D1LikeDatabase, input: {
|
|
|
53
56
|
newSession: boolean;
|
|
54
57
|
activeMs: number;
|
|
55
58
|
}>;
|
|
59
|
+
/** Count one resolved action — the Bun store's `recordAction`, as ONE D1 batch (`./actions.ts`). */
|
|
60
|
+
export declare function recordActionD1(db: D1LikeDatabase, use: ActionUse): Promise<void>;
|
|
56
61
|
/**
|
|
57
62
|
* The recorder as the kit's `createEngagementRecorder` mounts it — `POST /view` under
|
|
58
63
|
* `ENGAGEMENT_BASE_PATH` — over D1. Recording only: there is no read surface here, as there is
|
|
@@ -61,7 +66,13 @@ export declare function recordViewD1(db: D1LikeDatabase, input: {
|
|
|
61
66
|
export declare function createD1EngagementRecorder(options: {
|
|
62
67
|
db: D1LikeDatabase;
|
|
63
68
|
places: readonly PlaceRef[];
|
|
69
|
+
/**
|
|
70
|
+
* The manifest's declared actions (`actionsFromManifest`). Present, `POST /action` is mounted
|
|
71
|
+
* and the D1 needs {@link ENGAGEMENT_ACTION_DDL} applied; absent, there is no action door.
|
|
72
|
+
*/
|
|
73
|
+
actions?: readonly ActionRef[];
|
|
64
74
|
resolveUser: (c: Context) => string | null;
|
|
65
75
|
now?: () => number;
|
|
66
76
|
}): Hono;
|
|
67
|
-
export {
|
|
77
|
+
export { ENGAGEMENT_ACTION_DDL } from "./actions.js";
|
|
78
|
+
export { actionsFromManifest, placesFromManifest } from "./manifestPlaces.js";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Hono } from "hono";
|
|
2
|
-
import {
|
|
2
|
+
import { actionStatements, claimedAction } from "./actions.js";
|
|
3
|
+
import { dayKey, foldView, OTHER_PLACE, resolveAction, resolvePlace } from "./policy.js";
|
|
3
4
|
/** The kit store's own tables and indexes, as one-line DDL — see the header. */
|
|
4
5
|
export const ENGAGEMENT_DDL = [
|
|
5
6
|
"CREATE TABLE IF NOT EXISTS engagement_days ( user_key TEXT NOT NULL, place TEXT NOT NULL, day TEXT NOT NULL, views INTEGER NOT NULL DEFAULT 0, active_ms INTEGER NOT NULL DEFAULT 0, sessions INTEGER NOT NULL DEFAULT 0, first_ts INTEGER NOT NULL, last_ts INTEGER NOT NULL, PRIMARY KEY (user_key, place, day) )",
|
|
@@ -50,6 +51,10 @@ export async function recordViewD1(db, input) {
|
|
|
50
51
|
await db.batch(statements);
|
|
51
52
|
return fold;
|
|
52
53
|
}
|
|
54
|
+
/** Count one resolved action — the Bun store's `recordAction`, as ONE D1 batch (`./actions.ts`). */
|
|
55
|
+
export async function recordActionD1(db, use) {
|
|
56
|
+
await db.batch(actionStatements(use).map(({ sql, params }) => db.prepare(sql).bind(...params)));
|
|
57
|
+
}
|
|
53
58
|
/**
|
|
54
59
|
* The recorder as the kit's `createEngagementRecorder` mounts it — `POST /view` under
|
|
55
60
|
* `ENGAGEMENT_BASE_PATH` — over D1. Recording only: there is no read surface here, as there is
|
|
@@ -76,6 +81,29 @@ export function createD1EngagementRecorder(options) {
|
|
|
76
81
|
const fold = await recordViewD1(options.db, { userKey, place, ts: now() });
|
|
77
82
|
return c.json({ ok: true, place, bucketed: place === OTHER_PLACE, ...fold });
|
|
78
83
|
});
|
|
84
|
+
const actions = options.actions;
|
|
85
|
+
if (actions) {
|
|
86
|
+
app.post("/action", async (c) => {
|
|
87
|
+
const userKey = options.resolveUser(c);
|
|
88
|
+
if (!userKey)
|
|
89
|
+
return c.json({ error: "unauthenticated" }, 401);
|
|
90
|
+
let body;
|
|
91
|
+
try {
|
|
92
|
+
body = await c.req.json();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return c.json({ error: "expected a json body" }, 400);
|
|
96
|
+
}
|
|
97
|
+
const claimed = claimedAction(body);
|
|
98
|
+
if ("error" in claimed)
|
|
99
|
+
return c.json({ error: claimed.error }, 400);
|
|
100
|
+
const place = resolvePlace(claimed.place, options.places);
|
|
101
|
+
const action = resolveAction(claimed.action, actions);
|
|
102
|
+
await recordActionD1(options.db, { userKey, place, action, ts: now() });
|
|
103
|
+
return c.json({ ok: true, place, action, bucketed: place === OTHER_PLACE || action === OTHER_PLACE });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
79
106
|
return app;
|
|
80
107
|
}
|
|
81
|
-
export {
|
|
108
|
+
export { ENGAGEMENT_ACTION_DDL } from "./actions.js";
|
|
109
|
+
export { actionsFromManifest, placesFromManifest } from "./manifestPlaces.js";
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
*/
|
|
50
50
|
export { type AppEngagementSnapshot, createEngagementRecorder, type EngagementApiOptions, readAppEngagement, } from "./api.js";
|
|
51
51
|
export { type EngagementEnv, type EngagementEnvOptions, readEngagementEnv } from "./env.js";
|
|
52
|
-
export {
|
|
53
|
-
export {
|
|
52
|
+
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
|
+
export { type EngagementPlace, engagementActionsFor, engagementPlacesFor } from "./places.js";
|
|
54
|
+
export { type ActionRef, classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, engagementAllowedForApp, foldView, INTEREST_ACTIVE_DAYS, INTEREST_FADING_DAYS, type InterestVerdict, MAX_DWELL_SLICE_MS, normalizePlaceId, OTHER_PLACE, type PlaceRef, resolvePlace, SESSION_GAP_MS, type ViewFold, } from "./policy.js";
|
|
54
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, type EngagementDay, EngagementStore, type EngagementStoreOptions, type EngagementUser, type EngagementUserPlace, } from "./store.js";
|
|
55
|
-
export { type AppEngagement, type DailyEngagement, type PlaceEngagement, returnCurve, summarize, type SummarizeInput, type UserEngagement, } from "./summary.js";
|
|
56
|
+
export { type ActionEngagement, type AppEngagement, type DailyEngagement, type PlaceEngagement, returnCurve, summarize, type SummarizeInput, type UserEngagement, } from "./summary.js";
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
*/
|
|
50
50
|
export { createEngagementRecorder, readAppEngagement, } from "./api.js";
|
|
51
51
|
export { readEngagementEnv } from "./env.js";
|
|
52
|
-
export {
|
|
52
|
+
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
|
+
export { engagementActionsFor, engagementPlacesFor } from "./places.js";
|
|
53
54
|
export { classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, engagementAllowedForApp, foldView, INTEREST_ACTIVE_DAYS, INTEREST_FADING_DAYS, MAX_DWELL_SLICE_MS, normalizePlaceId, OTHER_PLACE, resolvePlace, SESSION_GAP_MS, } from "./policy.js";
|
|
54
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, EngagementStore, } from "./store.js";
|
|
55
56
|
export { returnCurve, summarize, } from "./summary.js";
|
|
@@ -5,5 +5,11 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Anything it cannot read is `[]`, never a throw: see `places.ts`, "Missing is EMPTY".
|
|
7
7
|
*/
|
|
8
|
-
import type { PlaceRef } from "./policy.js";
|
|
8
|
+
import type { ActionRef, PlaceRef } from "./policy.js";
|
|
9
9
|
export declare function placesFromManifest(doc: unknown): PlaceRef[];
|
|
10
|
+
/**
|
|
11
|
+
* A parsed `routes.manifest.json` → the ACTION allowlist (task 3470): its top-level `actions`,
|
|
12
|
+
* each `{ id, label }` or a bare id. The same contract as the places — anything unreadable is
|
|
13
|
+
* `[]`, never a throw — and the same consequence: an app with no `actions` has no action door.
|
|
14
|
+
*/
|
|
15
|
+
export declare function actionsFromManifest(doc: unknown): ActionRef[];
|
|
@@ -15,3 +15,24 @@ export function placesFromManifest(doc) {
|
|
|
15
15
|
places.sort((a, b) => a.id.localeCompare(b.id));
|
|
16
16
|
return places;
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* A parsed `routes.manifest.json` → the ACTION allowlist (task 3470): its top-level `actions`,
|
|
20
|
+
* each `{ id, label }` or a bare id. The same contract as the places — anything unreadable is
|
|
21
|
+
* `[]`, never a throw — and the same consequence: an app with no `actions` has no action door.
|
|
22
|
+
*/
|
|
23
|
+
export function actionsFromManifest(doc) {
|
|
24
|
+
const raw = Array.isArray(doc) ? undefined : doc?.actions;
|
|
25
|
+
if (!Array.isArray(raw))
|
|
26
|
+
return [];
|
|
27
|
+
const seen = new Set();
|
|
28
|
+
const actions = [];
|
|
29
|
+
for (const item of raw) {
|
|
30
|
+
const a = (typeof item === "string" ? { id: item } : item);
|
|
31
|
+
if (!a || typeof a.id !== "string" || !a.id.trim() || seen.has(a.id))
|
|
32
|
+
continue;
|
|
33
|
+
seen.add(a.id);
|
|
34
|
+
actions.push(typeof a.label === "string" ? { id: a.id, label: a.label } : { id: a.id });
|
|
35
|
+
}
|
|
36
|
+
actions.sort((a, b) => a.id.localeCompare(b.id));
|
|
37
|
+
return actions;
|
|
38
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ActionRef } from "./policy.js";
|
|
1
2
|
/**
|
|
2
3
|
* One declared page: the id the board shows, and the URL shape that reaches it.
|
|
3
4
|
*
|
|
@@ -20,3 +21,5 @@ export interface EngagementPlace {
|
|
|
20
21
|
* manifest is copied into both.
|
|
21
22
|
*/
|
|
22
23
|
export declare function engagementPlacesFor(dir: string): EngagementPlace[];
|
|
24
|
+
/** The declared ACTIONS in `<dir>/routes.manifest.json` — `[]` when it has none (task 3470). */
|
|
25
|
+
export declare function engagementActionsFor(dir: string): ActionRef[];
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
*/
|
|
25
25
|
import { existsSync, readFileSync } from "node:fs";
|
|
26
26
|
import { join } from "node:path";
|
|
27
|
-
import { placesFromManifest } from "./manifestPlaces.js";
|
|
27
|
+
import { actionsFromManifest, placesFromManifest } from "./manifestPlaces.js";
|
|
28
28
|
/** Where the manifest sits, in a source tree and in a built artifact alike. */
|
|
29
29
|
const MANIFEST = "routes.manifest.json";
|
|
30
30
|
/**
|
|
@@ -35,11 +35,18 @@ const MANIFEST = "routes.manifest.json";
|
|
|
35
35
|
* manifest is copied into both.
|
|
36
36
|
*/
|
|
37
37
|
export function engagementPlacesFor(dir) {
|
|
38
|
+
return readManifest(dir, placesFromManifest);
|
|
39
|
+
}
|
|
40
|
+
/** The declared ACTIONS in `<dir>/routes.manifest.json` — `[]` when it has none (task 3470). */
|
|
41
|
+
export function engagementActionsFor(dir) {
|
|
42
|
+
return readManifest(dir, actionsFromManifest);
|
|
43
|
+
}
|
|
44
|
+
function readManifest(dir, read) {
|
|
38
45
|
const path = join(dir, MANIFEST);
|
|
39
46
|
if (!existsSync(path))
|
|
40
47
|
return [];
|
|
41
48
|
try {
|
|
42
|
-
return
|
|
49
|
+
return read(JSON.parse(readFileSync(path, "utf8")));
|
|
43
50
|
}
|
|
44
51
|
catch {
|
|
45
52
|
return [];
|
|
@@ -83,6 +83,27 @@ export declare function resolvePlace(claimed: string, allowed: readonly (string
|
|
|
83
83
|
* unpopular features rather than one popular one.
|
|
84
84
|
*/
|
|
85
85
|
export declare function normalizePlaceId(raw: string): string;
|
|
86
|
+
/**
|
|
87
|
+
* Resolve the action a browser reported to one of the app's DECLARED actions (task 3470).
|
|
88
|
+
*
|
|
89
|
+
* A place says where somebody was; an action says what they DID there — a search run, a track
|
|
90
|
+
* played, a download started. The owner's question ("what features do they use and ignore") is
|
|
91
|
+
* about the second, and it carries the same risk as the first: the obvious way to record "a
|
|
92
|
+
* search" is to record the search. So rule 1 holds for actions exactly as for places — an action
|
|
93
|
+
* is a NAME from the app's own `routes.manifest.json` `actions` list, never a value. There is no
|
|
94
|
+
* field for the query, the track or the URL, and anything off the list records as
|
|
95
|
+
* {@link OTHER_PLACE}, so an app that forgot to declare a name sees an `other` row rather than
|
|
96
|
+
* a feature that reads as unused.
|
|
97
|
+
*/
|
|
98
|
+
export declare function resolveAction(claimed: string, allowed: readonly (string | ActionRef)[]): string;
|
|
99
|
+
/** One declared action — `routes.manifest.json`'s `actions[]`. */
|
|
100
|
+
export interface ActionRef {
|
|
101
|
+
id: string;
|
|
102
|
+
/** What the console shows beside the id — "play a track". */
|
|
103
|
+
label?: string;
|
|
104
|
+
}
|
|
105
|
+
/** `Play ` → `play`. Case and surrounding space are one action, not two. */
|
|
106
|
+
export declare function normalizeActionId(raw: string): string;
|
|
86
107
|
/**
|
|
87
108
|
* How long a gap between two views before the second one starts a new session.
|
|
88
109
|
*
|
|
@@ -128,6 +128,33 @@ export function normalizePlaceId(raw) {
|
|
|
128
128
|
.replace(/^\/?#/, "")
|
|
129
129
|
.replace(/^\/+|\/+$/g, ""));
|
|
130
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* Resolve the action a browser reported to one of the app's DECLARED actions (task 3470).
|
|
133
|
+
*
|
|
134
|
+
* A place says where somebody was; an action says what they DID there — a search run, a track
|
|
135
|
+
* played, a download started. The owner's question ("what features do they use and ignore") is
|
|
136
|
+
* about the second, and it carries the same risk as the first: the obvious way to record "a
|
|
137
|
+
* search" is to record the search. So rule 1 holds for actions exactly as for places — an action
|
|
138
|
+
* is a NAME from the app's own `routes.manifest.json` `actions` list, never a value. There is no
|
|
139
|
+
* field for the query, the track or the URL, and anything off the list records as
|
|
140
|
+
* {@link OTHER_PLACE}, so an app that forgot to declare a name sees an `other` row rather than
|
|
141
|
+
* a feature that reads as unused.
|
|
142
|
+
*/
|
|
143
|
+
export function resolveAction(claimed, allowed) {
|
|
144
|
+
const want = normalizeActionId(claimed);
|
|
145
|
+
if (!want)
|
|
146
|
+
return OTHER_PLACE;
|
|
147
|
+
for (const entry of allowed) {
|
|
148
|
+
const id = typeof entry === "string" ? entry : entry.id;
|
|
149
|
+
if (normalizeActionId(id) === want)
|
|
150
|
+
return id;
|
|
151
|
+
}
|
|
152
|
+
return OTHER_PLACE;
|
|
153
|
+
}
|
|
154
|
+
/** `Play ` → `play`. Case and surrounding space are one action, not two. */
|
|
155
|
+
export function normalizeActionId(raw) {
|
|
156
|
+
return raw.trim().toLowerCase();
|
|
157
|
+
}
|
|
131
158
|
/**
|
|
132
159
|
* How long a gap between two views before the second one starts a new session.
|
|
133
160
|
*
|
|
@@ -32,8 +32,9 @@
|
|
|
32
32
|
* every lapsed user look like a new one.
|
|
33
33
|
*/
|
|
34
34
|
import { Database } from "bun:sqlite";
|
|
35
|
-
import
|
|
36
|
-
|
|
35
|
+
import { type ActionUse } from "./actions.js";
|
|
36
|
+
import type { EngagementActionUse, EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
|
|
37
|
+
export type { EngagementActionUse, EngagementDay, EngagementUser, EngagementUserPlace } from "./types.js";
|
|
37
38
|
/** How much history the daily grain keeps. */
|
|
38
39
|
export declare const ENGAGEMENT_DAY_RETENTION_DAYS = 120;
|
|
39
40
|
export interface EngagementStoreOptions {
|
|
@@ -66,6 +67,14 @@ export declare class EngagementStore {
|
|
|
66
67
|
newSession: boolean;
|
|
67
68
|
activeMs: number;
|
|
68
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Count one resolved action. Like {@link recordView}, `place` and `action` must ALREADY be
|
|
72
|
+
* resolved against the app's manifest — the door (`api.ts`) does it. Touches no clock and no
|
|
73
|
+
* dwell: an action is something done on the page the view already counted.
|
|
74
|
+
*/
|
|
75
|
+
recordAction(use: ActionUse): void;
|
|
76
|
+
/** Every (user, place, action) lifetime roll-up, most used first. */
|
|
77
|
+
actions(): EngagementActionUse[];
|
|
69
78
|
/** Every account's roll-up for this app, most recently seen first. */
|
|
70
79
|
users(): EngagementUser[];
|
|
71
80
|
/** Every (user, place) roll-up — the per-FEATURE grain, whole-fleet readable. */
|
|
@@ -75,7 +84,7 @@ export declare class EngagementStore {
|
|
|
75
84
|
/**
|
|
76
85
|
* Drop daily rows older than the retention window.
|
|
77
86
|
*
|
|
78
|
-
* 🔴 Only `engagement_days
|
|
87
|
+
* 🔴 Only the daily grains (`engagement_days`, `engagement_action_days`). The roll-ups are deliberately untouched —
|
|
79
88
|
* see the file header. An agent "completing" this by trimming them too would
|
|
80
89
|
* delete the only record that somebody used to be here, which is the entire
|
|
81
90
|
* drop-off signal.
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
import { Database } from "bun:sqlite";
|
|
35
35
|
import { mkdirSync } from "node:fs";
|
|
36
36
|
import { dirname, resolve } from "node:path";
|
|
37
|
+
import { actionStatements, ENGAGEMENT_ACTION_DDL } from "./actions.js";
|
|
37
38
|
import { dayKey, foldView } from "./policy.js";
|
|
38
39
|
/**
|
|
39
40
|
* `bun:sqlite`'s `create: true` creates the FILE, never the directory above it, so a first boot
|
|
@@ -110,6 +111,9 @@ export class EngagementStore {
|
|
|
110
111
|
last_ts INTEGER NOT NULL,
|
|
111
112
|
last_place TEXT NOT NULL DEFAULT ''
|
|
112
113
|
)`);
|
|
114
|
+
// Feature actions (task 3470) — `actions.ts` says why they are their own two tables.
|
|
115
|
+
for (const ddl of ENGAGEMENT_ACTION_DDL)
|
|
116
|
+
this.handle.run(ddl);
|
|
113
117
|
}
|
|
114
118
|
/**
|
|
115
119
|
* Record one resolved place-view.
|
|
@@ -165,6 +169,28 @@ export class EngagementStore {
|
|
|
165
169
|
this.prune(ts);
|
|
166
170
|
return fold;
|
|
167
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Count one resolved action. Like {@link recordView}, `place` and `action` must ALREADY be
|
|
174
|
+
* resolved against the app's manifest — the door (`api.ts`) does it. Touches no clock and no
|
|
175
|
+
* dwell: an action is something done on the page the view already counted.
|
|
176
|
+
*/
|
|
177
|
+
recordAction(use) {
|
|
178
|
+
const statements = actionStatements(use);
|
|
179
|
+
this.handle.transaction(() => {
|
|
180
|
+
for (const { sql, params } of statements)
|
|
181
|
+
this.handle.run(sql, params);
|
|
182
|
+
})();
|
|
183
|
+
if (++this.writes % 256 === 0)
|
|
184
|
+
this.prune(use.ts);
|
|
185
|
+
}
|
|
186
|
+
/** Every (user, place, action) lifetime roll-up, most used first. */
|
|
187
|
+
actions() {
|
|
188
|
+
return this.handle
|
|
189
|
+
.query(`SELECT user_key AS userKey, place, action, uses, first_ts AS firstTs, last_ts AS lastTs
|
|
190
|
+
FROM engagement_actions
|
|
191
|
+
ORDER BY uses DESC`)
|
|
192
|
+
.all();
|
|
193
|
+
}
|
|
168
194
|
/** Every account's roll-up for this app, most recently seen first. */
|
|
169
195
|
users() {
|
|
170
196
|
return this.handle
|
|
@@ -195,14 +221,16 @@ export class EngagementStore {
|
|
|
195
221
|
/**
|
|
196
222
|
* Drop daily rows older than the retention window.
|
|
197
223
|
*
|
|
198
|
-
* 🔴 Only `engagement_days
|
|
224
|
+
* 🔴 Only the daily grains (`engagement_days`, `engagement_action_days`). The roll-ups are deliberately untouched —
|
|
199
225
|
* see the file header. An agent "completing" this by trimming them too would
|
|
200
226
|
* delete the only record that somebody used to be here, which is the entire
|
|
201
227
|
* drop-off signal.
|
|
202
228
|
*/
|
|
203
229
|
prune(at = Date.now()) {
|
|
204
230
|
const cutoff = dayKey(at - this.retentionDays * 86_400_000);
|
|
205
|
-
|
|
231
|
+
// The action DAYS age out with the view days; `engagement_actions` is a roll-up and stays.
|
|
232
|
+
const actionDays = this.handle.run("DELETE FROM engagement_action_days WHERE day < ?", [cutoff]).changes;
|
|
233
|
+
return this.handle.run("DELETE FROM engagement_days WHERE day < ?", [cutoff]).changes + actionDays;
|
|
206
234
|
}
|
|
207
235
|
/** Row counts + age bounds, for the retention card. */
|
|
208
236
|
stats() {
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* drives it with hand-written rows and checks arithmetic a human can verify —
|
|
14
14
|
* which is the whole design constraint at n = 3. No I/O, no `Date.now()`.
|
|
15
15
|
*/
|
|
16
|
-
import { type EngagementDay, type EngagementUser, type EngagementUserPlace, type InterestVerdict } from "./types.js";
|
|
16
|
+
import { type EngagementActionUse, type EngagementDay, type EngagementUser, type EngagementUserPlace, type InterestVerdict } from "./types.js";
|
|
17
17
|
/** One account's relationship with one app. */
|
|
18
18
|
export interface UserEngagement {
|
|
19
19
|
userKey: string;
|
|
@@ -40,6 +40,16 @@ export interface PlaceEngagement {
|
|
|
40
40
|
/** Mean ms of attention per view. The "does it hold interest" number. */
|
|
41
41
|
msPerView: number;
|
|
42
42
|
}
|
|
43
|
+
/** One declared action on one place, across every account — "what features they use" (task 3470). */
|
|
44
|
+
export interface ActionEngagement {
|
|
45
|
+
place: string;
|
|
46
|
+
name: string;
|
|
47
|
+
/** Times it was used, every account together. */
|
|
48
|
+
count: number;
|
|
49
|
+
/** Distinct accounts that have used it. */
|
|
50
|
+
accounts: number;
|
|
51
|
+
lastTs: number;
|
|
52
|
+
}
|
|
43
53
|
/** One day of the app's whole activity. */
|
|
44
54
|
export interface DailyEngagement {
|
|
45
55
|
day: string;
|
|
@@ -63,6 +73,8 @@ export interface AppEngagement {
|
|
|
63
73
|
lastTs: number;
|
|
64
74
|
}>;
|
|
65
75
|
daily: DailyEngagement[];
|
|
76
|
+
/** Per place, per action — most used first. Empty for an app that records no actions. */
|
|
77
|
+
actions: ActionEngagement[];
|
|
66
78
|
totals: {
|
|
67
79
|
users: number;
|
|
68
80
|
views: number;
|
|
@@ -82,6 +94,8 @@ export interface SummarizeInput {
|
|
|
82
94
|
users: EngagementUser[];
|
|
83
95
|
places: EngagementUserPlace[];
|
|
84
96
|
days: EngagementDay[];
|
|
97
|
+
/** The (user, place, action) roll-ups. Absent → no actions. */
|
|
98
|
+
actions?: EngagementActionUse[];
|
|
85
99
|
}
|
|
86
100
|
export declare function summarize(input: SummarizeInput): AppEngagement;
|
|
87
101
|
/**
|
|
@@ -82,6 +82,20 @@ export function summarize(input) {
|
|
|
82
82
|
lastTs: p.lastTs,
|
|
83
83
|
}))
|
|
84
84
|
.sort((a, b) => b.views - a.views);
|
|
85
|
+
const byAction = new Map();
|
|
86
|
+
for (const row of input.actions ?? []) {
|
|
87
|
+
const key = `${row.place}\u0000${row.action}`;
|
|
88
|
+
const existing = byAction.get(key);
|
|
89
|
+
if (existing) {
|
|
90
|
+
existing.count += row.uses;
|
|
91
|
+
existing.accounts += 1;
|
|
92
|
+
existing.lastTs = Math.max(existing.lastTs, row.lastTs);
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
byAction.set(key, { place: row.place, name: row.action, count: row.uses, accounts: 1, lastTs: row.lastTs });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const actions = [...byAction.values()].sort((a, b) => b.count - a.count || a.place.localeCompare(b.place) || a.name.localeCompare(b.name));
|
|
85
99
|
return {
|
|
86
100
|
app,
|
|
87
101
|
generatedAt: now,
|
|
@@ -89,6 +103,7 @@ export function summarize(input) {
|
|
|
89
103
|
places,
|
|
90
104
|
userPlaces,
|
|
91
105
|
daily,
|
|
106
|
+
actions,
|
|
92
107
|
totals: {
|
|
93
108
|
users: users.length,
|
|
94
109
|
views: users.reduce((n, u) => n + u.views, 0),
|
|
@@ -29,6 +29,15 @@ export interface EngagementUserPlace {
|
|
|
29
29
|
firstTs: number;
|
|
30
30
|
lastTs: number;
|
|
31
31
|
}
|
|
32
|
+
/** One (user, place, action) lifetime roll-up — "who uses this feature, and when last" (task 3470). */
|
|
33
|
+
export interface EngagementActionUse {
|
|
34
|
+
userKey: string;
|
|
35
|
+
place: string;
|
|
36
|
+
action: string;
|
|
37
|
+
uses: number;
|
|
38
|
+
firstTs: number;
|
|
39
|
+
lastTs: number;
|
|
40
|
+
}
|
|
32
41
|
/** One account's relationship with the whole app. */
|
|
33
42
|
export interface EngagementUser {
|
|
34
43
|
userKey: string;
|