cursedbelt-server 4.52.0 → 4.54.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/d1/pullD1.d.ts +18 -2
- package/dist/server/d1/pullD1.js +34 -6
- package/dist/server/engagement/index.d.ts +1 -1
- package/dist/server/engagement/index.js +1 -1
- package/dist/server/engagement/policy.d.ts +12 -1
- package/dist/server/engagement/policy.js +14 -2
- package/package.json +1 -1
- package/src/server/d1/pullD1.spec.ts +49 -1
- package/src/server/d1/pullD1.ts +42 -7
- package/src/server/engagement/engagement.spec.ts +16 -0
- package/src/server/engagement/index.ts +1 -0
- package/src/server/engagement/policy.ts +16 -2
|
@@ -101,13 +101,29 @@ export declare const wranglerArgv: (bun?: string) => string[];
|
|
|
101
101
|
*/
|
|
102
102
|
export declare function spawnToFile(argv: string[], options?: {
|
|
103
103
|
cwd?: string;
|
|
104
|
-
|
|
104
|
+
/** The child's whole environment, or a function of this call's own scratch directory that returns it. */
|
|
105
|
+
env?: Record<string, string | undefined> | ((scratch: string) => Record<string, string | undefined>);
|
|
105
106
|
}): {
|
|
106
107
|
code: number;
|
|
107
108
|
stdout: string;
|
|
108
109
|
stderr: string;
|
|
109
110
|
};
|
|
110
|
-
/**
|
|
111
|
+
/**
|
|
112
|
+
* The environment {@link spawnWrangler} gives one call: this process's, wrangler's debug log pointed
|
|
113
|
+
* at a file in the call's own `scratch` directory, then the caller's `overlay` — which may still
|
|
114
|
+
* name a `WRANGLER_LOG_PATH` of its own.
|
|
115
|
+
*
|
|
116
|
+
* 🔴 Why the log is the call's own. Wrangler writes a `wrangler-<stamp>.log` into its global
|
|
117
|
+
* `logs/` for EVERY invocation, and with `--json` that log holds the command's whole result again.
|
|
118
|
+
* A backup is hundreds of paged `d1 execute --json` SELECTs, so every nightly backup copied its
|
|
119
|
+
* database into `~/Library/Preferences/.wrangler/logs`, in plain text. Measured 2026-10-28 (forge
|
|
120
|
+
* task 3877): 9,393 logs, 1.24 GB in five days; photos' nightly backup alone made 600 calls and 169 MB of them
|
|
121
|
+
* (its 171,590-row gazetteer at {@link PAGE_ROWS} a page); the runner's prune keeps five days.
|
|
122
|
+
* A file ending `.log` is a file to wrangler, not a directory, and the scratch directory goes
|
|
123
|
+
* with the call — unless the call failed, when {@link spawnToFile} leaves the log for its reader.
|
|
124
|
+
*/
|
|
125
|
+
export declare function wranglerSpawnEnv(scratch: string, overlay?: Record<string, string | undefined>, base?: Record<string, string | undefined>): Record<string, string | undefined>;
|
|
126
|
+
/** The real {@link Wrangler}: `bun x wrangler …` in `cwd`, with `env` added to this process's ({@link wranglerSpawnEnv}). */
|
|
111
127
|
export declare function spawnWrangler(options: {
|
|
112
128
|
cwd: string;
|
|
113
129
|
env?: Record<string, string | undefined>;
|
package/dist/server/d1/pullD1.js
CHANGED
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
* below still refuses a load short of what was read.
|
|
45
45
|
*/
|
|
46
46
|
import { Database } from 'bun:sqlite';
|
|
47
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
47
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
48
48
|
import { tmpdir } from 'node:os';
|
|
49
49
|
import { join } from 'node:path';
|
|
50
50
|
/** Which side a backup must read. Pure, so every branch is testable without a network. */
|
|
@@ -130,18 +130,46 @@ export const wranglerArgv = (bun = process.execPath) => [bun, 'x', 'wrangler'];
|
|
|
130
130
|
export function spawnToFile(argv, options = {}) {
|
|
131
131
|
const dir = mkdtempSync(join(tmpdir(), 'cursedbelt-spawn-'));
|
|
132
132
|
const out = join(dir, 'stdout');
|
|
133
|
+
let code = 1;
|
|
133
134
|
try {
|
|
134
|
-
const
|
|
135
|
+
const env = typeof options.env === 'function' ? options.env(dir) : options.env;
|
|
136
|
+
const r = Bun.spawnSync(argv, { cwd: options.cwd, env, stdout: Bun.file(out), stderr: 'pipe' });
|
|
137
|
+
code = r.exitCode ?? 1;
|
|
135
138
|
const stdout = existsSync(out) ? readFileSync(out, 'utf8') : '';
|
|
136
|
-
return { code
|
|
139
|
+
return { code, stdout, stderr: r.stderr.toString() };
|
|
137
140
|
}
|
|
138
141
|
finally {
|
|
139
|
-
|
|
142
|
+
// A failed call keeps whatever the child wrote beside its stdout — wrangler's debug log, whose
|
|
143
|
+
// path its own error message prints — for the OS to reap from tmpdir. A green one keeps nothing.
|
|
144
|
+
if (code === 0)
|
|
145
|
+
rmSync(dir, { recursive: true, force: true });
|
|
146
|
+
else {
|
|
147
|
+
rmSync(out, { force: true });
|
|
148
|
+
if (readdirSync(dir).length === 0)
|
|
149
|
+
rmSync(dir, { recursive: true, force: true });
|
|
150
|
+
}
|
|
140
151
|
}
|
|
141
152
|
}
|
|
142
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* The environment {@link spawnWrangler} gives one call: this process's, wrangler's debug log pointed
|
|
155
|
+
* at a file in the call's own `scratch` directory, then the caller's `overlay` — which may still
|
|
156
|
+
* name a `WRANGLER_LOG_PATH` of its own.
|
|
157
|
+
*
|
|
158
|
+
* 🔴 Why the log is the call's own. Wrangler writes a `wrangler-<stamp>.log` into its global
|
|
159
|
+
* `logs/` for EVERY invocation, and with `--json` that log holds the command's whole result again.
|
|
160
|
+
* A backup is hundreds of paged `d1 execute --json` SELECTs, so every nightly backup copied its
|
|
161
|
+
* database into `~/Library/Preferences/.wrangler/logs`, in plain text. Measured 2026-10-28 (forge
|
|
162
|
+
* task 3877): 9,393 logs, 1.24 GB in five days; photos' nightly backup alone made 600 calls and 169 MB of them
|
|
163
|
+
* (its 171,590-row gazetteer at {@link PAGE_ROWS} a page); the runner's prune keeps five days.
|
|
164
|
+
* A file ending `.log` is a file to wrangler, not a directory, and the scratch directory goes
|
|
165
|
+
* with the call — unless the call failed, when {@link spawnToFile} leaves the log for its reader.
|
|
166
|
+
*/
|
|
167
|
+
export function wranglerSpawnEnv(scratch, overlay = {}, base = process.env) {
|
|
168
|
+
return { ...base, WRANGLER_LOG_PATH: join(scratch, 'wrangler.log'), ...overlay };
|
|
169
|
+
}
|
|
170
|
+
/** The real {@link Wrangler}: `bun x wrangler …` in `cwd`, with `env` added to this process's ({@link wranglerSpawnEnv}). */
|
|
143
171
|
export function spawnWrangler(options) {
|
|
144
|
-
return (args) => spawnToFile([...wranglerArgv(), ...args], { cwd: options.cwd, env:
|
|
172
|
+
return (args) => spawnToFile([...wranglerArgv(), ...args], { cwd: options.cwd, env: (scratch) => wranglerSpawnEnv(scratch, options.env) });
|
|
145
173
|
}
|
|
146
174
|
/**
|
|
147
175
|
* 🔴 Trailing NUL bytes off a `wrangler d1 export` file, in place. Returns how many were removed.
|
|
@@ -51,6 +51,6 @@ export { type AppEngagementSnapshot, createEngagementRecorder, type EngagementAp
|
|
|
51
51
|
export { type EngagementEnv, type EngagementEnvOptions, readEngagementEnv } from "./env.js";
|
|
52
52
|
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
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
|
+
export { type ActionRef, classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, ENGAGEMENT_ZONE, 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";
|
|
55
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, type EngagementDay, EngagementStore, type EngagementStoreOptions, type EngagementUser, type EngagementUserPlace, } from "./store.js";
|
|
56
56
|
export { type ActionEngagement, type AppEngagement, type DailyEngagement, type PlaceEngagement, returnCurve, summarize, type SummarizeInput, type UserEngagement, } from "./summary.js";
|
|
@@ -51,6 +51,6 @@ export { createEngagementRecorder, readAppEngagement, } from "./api.js";
|
|
|
51
51
|
export { readEngagementEnv } from "./env.js";
|
|
52
52
|
export { actionsFromManifest } from "./manifestPlaces.js";
|
|
53
53
|
export { engagementActionsFor, engagementPlacesFor } from "./places.js";
|
|
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
|
+
export { classifyInterest, dayKey, daysSince, ENGAGEMENT_EXCLUDED_APPS, ENGAGEMENT_ZONE, engagementAllowedForApp, foldView, INTEREST_ACTIVE_DAYS, INTEREST_FADING_DAYS, MAX_DWELL_SLICE_MS, normalizePlaceId, OTHER_PLACE, resolvePlace, SESSION_GAP_MS, } from "./policy.js";
|
|
55
55
|
export { ENGAGEMENT_DAY_RETENTION_DAYS, EngagementStore, } from "./store.js";
|
|
56
56
|
export { returnCurve, summarize, } from "./summary.js";
|
|
@@ -145,7 +145,18 @@ export interface ViewFold {
|
|
|
145
145
|
* number boring, never negative.
|
|
146
146
|
*/
|
|
147
147
|
export declare function foldView(lastTs: number | null, ts: number): ViewFold;
|
|
148
|
-
/**
|
|
148
|
+
/**
|
|
149
|
+
* The zone every daily row is counted in: the owner's (forge task 3846).
|
|
150
|
+
*
|
|
151
|
+
* A site-wide daily count has to pick ONE zone — two viewers in two zones cannot both have
|
|
152
|
+
* their own midnight on the same row — and the person who reads these charts is the owner,
|
|
153
|
+
* in Central time. Until 2026-10-28 the rows were UTC days, which turn over at 7 pm Central,
|
|
154
|
+
* so every evening visit landed on tomorrow. Rows written before then stay UTC days: a
|
|
155
|
+
* `(user, place, day)` row holds no hours to re-bucket it by. The console labels the chart
|
|
156
|
+
* with this zone (station `views/metrics/EngagementPanel.tsx`).
|
|
157
|
+
*/
|
|
158
|
+
export declare const ENGAGEMENT_ZONE = "America/Chicago";
|
|
159
|
+
/** `2026-08-14` for a timestamp, on {@link ENGAGEMENT_ZONE}'s clock. The day bucket every row is keyed on. */
|
|
149
160
|
export declare function dayKey(ts: number): string;
|
|
150
161
|
/** How many whole days ago `ts` was, relative to `now`. */
|
|
151
162
|
export declare function daysSince(ts: number, now: number): number;
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
* a drop-off signal stated in days rather than as a rate. Nothing is a ratio
|
|
39
39
|
* over a denominator of two.
|
|
40
40
|
*/
|
|
41
|
+
import { formatDate } from "cwip/date";
|
|
41
42
|
/** The bucket a view lands in when its place is not on the app's allowlist. */
|
|
42
43
|
export const OTHER_PLACE = "other";
|
|
43
44
|
/**
|
|
@@ -198,9 +199,20 @@ export function foldView(lastTs, ts) {
|
|
|
198
199
|
return { newSession: true, activeMs: 0 };
|
|
199
200
|
return { newSession: false, activeMs: Math.min(gap, MAX_DWELL_SLICE_MS) };
|
|
200
201
|
}
|
|
201
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* The zone every daily row is counted in: the owner's (forge task 3846).
|
|
204
|
+
*
|
|
205
|
+
* A site-wide daily count has to pick ONE zone — two viewers in two zones cannot both have
|
|
206
|
+
* their own midnight on the same row — and the person who reads these charts is the owner,
|
|
207
|
+
* in Central time. Until 2026-10-28 the rows were UTC days, which turn over at 7 pm Central,
|
|
208
|
+
* so every evening visit landed on tomorrow. Rows written before then stay UTC days: a
|
|
209
|
+
* `(user, place, day)` row holds no hours to re-bucket it by. The console labels the chart
|
|
210
|
+
* with this zone (station `views/metrics/EngagementPanel.tsx`).
|
|
211
|
+
*/
|
|
212
|
+
export const ENGAGEMENT_ZONE = "America/Chicago";
|
|
213
|
+
/** `2026-08-14` for a timestamp, on {@link ENGAGEMENT_ZONE}'s clock. The day bucket every row is keyed on. */
|
|
202
214
|
export function dayKey(ts) {
|
|
203
|
-
return new Date(ts)
|
|
215
|
+
return formatDate(new Date(ts), "YYYY-MM-DD", ENGAGEMENT_ZONE);
|
|
204
216
|
}
|
|
205
217
|
/** How many whole days ago `ts` was, relative to `now`. */
|
|
206
218
|
export function daysSince(ts, now) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.54.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split — storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Database } from 'bun:sqlite';
|
|
2
2
|
import { afterEach, describe, expect, test } from 'bun:test';
|
|
3
|
-
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import {
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
chooseBackupSource,
|
|
9
9
|
pullD1ToSqlite,
|
|
10
10
|
servedRuntime,
|
|
11
|
+
spawnWrangler,
|
|
11
12
|
stripTrailingNuls,
|
|
12
13
|
type Wrangler,
|
|
13
14
|
wranglerArgv,
|
|
@@ -471,6 +472,53 @@ END;`;
|
|
|
471
472
|
expect(JSON.parse(r.stdout)).toHaveLength(1);
|
|
472
473
|
});
|
|
473
474
|
|
|
475
|
+
test("🔴 a wrangler call's debug log is the call's own — gone with a green call, kept for a red one", () => {
|
|
476
|
+
// `bun x wrangler` runs the cwd's own `node_modules/.bin/wrangler` first, so this one stands
|
|
477
|
+
// in for the real thing. It logs as wrangler does: every message, a `--json` result included,
|
|
478
|
+
// appended to `$WRANGLER_LOG_PATH`, else to a file of its own in the global `logs/`.
|
|
479
|
+
const cwd = scratch();
|
|
480
|
+
const home = join(cwd, 'home');
|
|
481
|
+
const global = join(home, 'Library', 'Preferences', '.wrangler', 'logs');
|
|
482
|
+
mkdirSync(join(cwd, 'node_modules', '.bin'), { recursive: true });
|
|
483
|
+
const fake = join(cwd, 'node_modules', '.bin', 'wrangler');
|
|
484
|
+
writeFileSync(
|
|
485
|
+
fake,
|
|
486
|
+
[
|
|
487
|
+
'#!/bin/sh',
|
|
488
|
+
`log="\${WRANGLER_LOG_PATH:-${global}}"`,
|
|
489
|
+
'case "$log" in *.log) ;; *) mkdir -p "$log"; log="$log/wrangler-$$.log" ;; esac',
|
|
490
|
+
'printf "the whole page again" >> "$log"',
|
|
491
|
+
'printf "[]"',
|
|
492
|
+
'exit "$1"',
|
|
493
|
+
].join('\n'),
|
|
494
|
+
);
|
|
495
|
+
chmodSync(fake, 0o755);
|
|
496
|
+
const wrangler = spawnWrangler({ cwd, env: { HOME: home } });
|
|
497
|
+
const kept = () => readdirSync(tmpdir()).filter((name) => name.startsWith('cursedbelt-spawn-'));
|
|
498
|
+
const before = new Set(kept());
|
|
499
|
+
|
|
500
|
+
const green = wrangler(['0']);
|
|
501
|
+
expect(green.code).toBe(0);
|
|
502
|
+
expect(green.stdout).toBe('[]');
|
|
503
|
+
// The failure this exists for: a nightly backup's every page, copied into the global logs.
|
|
504
|
+
expect(existsSync(global) ? readdirSync(global) : []).toEqual([]);
|
|
505
|
+
expect(kept().filter((name) => !before.has(name))).toEqual([]);
|
|
506
|
+
|
|
507
|
+
const red = wrangler(['1']);
|
|
508
|
+
expect(red.code).toBe(1);
|
|
509
|
+
expect(existsSync(global) ? readdirSync(global) : []).toEqual([]);
|
|
510
|
+
const left = kept().filter((name) => !before.has(name));
|
|
511
|
+
expect(left).toHaveLength(1);
|
|
512
|
+
const log = join(tmpdir(), left[0] as string, 'wrangler.log');
|
|
513
|
+
dirs.push(join(log, '..'));
|
|
514
|
+
expect(readFileSync(log, 'utf8')).toBe('the whole page again');
|
|
515
|
+
|
|
516
|
+
// A caller that names its own log path keeps it.
|
|
517
|
+
const mine = join(cwd, 'mine.log');
|
|
518
|
+
expect(spawnWrangler({ cwd, env: { HOME: home, WRANGLER_LOG_PATH: mine } })(['0']).code).toBe(0);
|
|
519
|
+
expect(readFileSync(mine, 'utf8')).toBe('the whole page again');
|
|
520
|
+
});
|
|
521
|
+
|
|
474
522
|
test('a cut page names its byte count, so a 64 KiB multiple is visible in the log', async () => {
|
|
475
523
|
const cut: Wrangler = () => ({ code: 0, stdout: '[{"results":[{"name":"ite', stderr: '' });
|
|
476
524
|
await expect(pull(scratch(), cut)).rejects.toThrow(/\(25 bytes of stdout\)/);
|
package/src/server/d1/pullD1.ts
CHANGED
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
*/
|
|
46
46
|
|
|
47
47
|
import { Database } from 'bun:sqlite';
|
|
48
|
-
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
48
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
49
49
|
import { tmpdir } from 'node:os';
|
|
50
50
|
import { join } from 'node:path';
|
|
51
51
|
|
|
@@ -149,22 +149,57 @@ export const wranglerArgv = (bun: string = process.execPath): string[] => [bun,
|
|
|
149
149
|
*/
|
|
150
150
|
export function spawnToFile(
|
|
151
151
|
argv: string[],
|
|
152
|
-
options: {
|
|
152
|
+
options: {
|
|
153
|
+
cwd?: string;
|
|
154
|
+
/** The child's whole environment, or a function of this call's own scratch directory that returns it. */
|
|
155
|
+
env?: Record<string, string | undefined> | ((scratch: string) => Record<string, string | undefined>);
|
|
156
|
+
} = {},
|
|
153
157
|
): { code: number; stdout: string; stderr: string } {
|
|
154
158
|
const dir = mkdtempSync(join(tmpdir(), 'cursedbelt-spawn-'));
|
|
155
159
|
const out = join(dir, 'stdout');
|
|
160
|
+
let code = 1;
|
|
156
161
|
try {
|
|
157
|
-
const
|
|
162
|
+
const env = typeof options.env === 'function' ? options.env(dir) : options.env;
|
|
163
|
+
const r = Bun.spawnSync(argv, { cwd: options.cwd, env, stdout: Bun.file(out), stderr: 'pipe' });
|
|
164
|
+
code = r.exitCode ?? 1;
|
|
158
165
|
const stdout = existsSync(out) ? readFileSync(out, 'utf8') : '';
|
|
159
|
-
return { code
|
|
166
|
+
return { code, stdout, stderr: r.stderr.toString() };
|
|
160
167
|
} finally {
|
|
161
|
-
|
|
168
|
+
// A failed call keeps whatever the child wrote beside its stdout — wrangler's debug log, whose
|
|
169
|
+
// path its own error message prints — for the OS to reap from tmpdir. A green one keeps nothing.
|
|
170
|
+
if (code === 0) rmSync(dir, { recursive: true, force: true });
|
|
171
|
+
else {
|
|
172
|
+
rmSync(out, { force: true });
|
|
173
|
+
if (readdirSync(dir).length === 0) rmSync(dir, { recursive: true, force: true });
|
|
174
|
+
}
|
|
162
175
|
}
|
|
163
176
|
}
|
|
164
177
|
|
|
165
|
-
/**
|
|
178
|
+
/**
|
|
179
|
+
* The environment {@link spawnWrangler} gives one call: this process's, wrangler's debug log pointed
|
|
180
|
+
* at a file in the call's own `scratch` directory, then the caller's `overlay` — which may still
|
|
181
|
+
* name a `WRANGLER_LOG_PATH` of its own.
|
|
182
|
+
*
|
|
183
|
+
* 🔴 Why the log is the call's own. Wrangler writes a `wrangler-<stamp>.log` into its global
|
|
184
|
+
* `logs/` for EVERY invocation, and with `--json` that log holds the command's whole result again.
|
|
185
|
+
* A backup is hundreds of paged `d1 execute --json` SELECTs, so every nightly backup copied its
|
|
186
|
+
* database into `~/Library/Preferences/.wrangler/logs`, in plain text. Measured 2026-10-28 (forge
|
|
187
|
+
* task 3877): 9,393 logs, 1.24 GB in five days; photos' nightly backup alone made 600 calls and 169 MB of them
|
|
188
|
+
* (its 171,590-row gazetteer at {@link PAGE_ROWS} a page); the runner's prune keeps five days.
|
|
189
|
+
* A file ending `.log` is a file to wrangler, not a directory, and the scratch directory goes
|
|
190
|
+
* with the call — unless the call failed, when {@link spawnToFile} leaves the log for its reader.
|
|
191
|
+
*/
|
|
192
|
+
export function wranglerSpawnEnv(
|
|
193
|
+
scratch: string,
|
|
194
|
+
overlay: Record<string, string | undefined> = {},
|
|
195
|
+
base: Record<string, string | undefined> = process.env,
|
|
196
|
+
): Record<string, string | undefined> {
|
|
197
|
+
return { ...base, WRANGLER_LOG_PATH: join(scratch, 'wrangler.log'), ...overlay };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The real {@link Wrangler}: `bun x wrangler …` in `cwd`, with `env` added to this process's ({@link wranglerSpawnEnv}). */
|
|
166
201
|
export function spawnWrangler(options: { cwd: string; env?: Record<string, string | undefined> }): Wrangler {
|
|
167
|
-
return (args) => spawnToFile([...wranglerArgv(), ...args], { cwd: options.cwd, env:
|
|
202
|
+
return (args) => spawnToFile([...wranglerArgv(), ...args], { cwd: options.cwd, env: (scratch) => wranglerSpawnEnv(scratch, options.env) });
|
|
168
203
|
}
|
|
169
204
|
|
|
170
205
|
/**
|
|
@@ -19,6 +19,8 @@ import { createEngagementRecorder, readAppEngagement } from "./api.js";
|
|
|
19
19
|
import { readEngagementEnv } from "./env.js";
|
|
20
20
|
import {
|
|
21
21
|
classifyInterest,
|
|
22
|
+
dayKey,
|
|
23
|
+
ENGAGEMENT_ZONE,
|
|
22
24
|
engagementAllowedForApp,
|
|
23
25
|
foldView,
|
|
24
26
|
MAX_DWELL_SLICE_MS,
|
|
@@ -242,6 +244,20 @@ describe("sessionization — the gap rule", () => {
|
|
|
242
244
|
});
|
|
243
245
|
});
|
|
244
246
|
|
|
247
|
+
describe("a daily row is a day on the owner's clock (forge task 3846)", () => {
|
|
248
|
+
test("an evening visit in Central time lands on that evening's day, not UTC's tomorrow", () => {
|
|
249
|
+
expect(ENGAGEMENT_ZONE).toBe("America/Chicago");
|
|
250
|
+
expect(dayKey(Date.parse("2026-10-28T02:16:00.000Z"))).toBe("2026-10-27");
|
|
251
|
+
expect(dayKey(Date.parse("2026-10-28T05:00:00.000Z"))).toBe("2026-10-28");
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test("the store writes the visit into that row", () => {
|
|
255
|
+
const s = store();
|
|
256
|
+
s.recordView({ userKey: "u", place: "files", ts: Date.parse("2026-10-28T02:16:00.000Z") });
|
|
257
|
+
expect(s.days(Date.parse("2026-10-27T12:00:00.000Z")).map((d) => d.day)).toEqual(["2026-10-27"]);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
|
|
245
261
|
describe("the store credits dwell BACKWARDS, to the page you were reading", () => {
|
|
246
262
|
test("two minutes on `files` then a move to `ask` scores files, not ask", () => {
|
|
247
263
|
// Crediting forwards would make the most-abandoned page in the app score
|
|
@@ -39,6 +39,8 @@
|
|
|
39
39
|
* over a denominator of two.
|
|
40
40
|
*/
|
|
41
41
|
|
|
42
|
+
import { formatDate } from "cwip/date";
|
|
43
|
+
|
|
42
44
|
/** The bucket a view lands in when its place is not on the app's allowlist. */
|
|
43
45
|
export const OTHER_PLACE = "other";
|
|
44
46
|
|
|
@@ -226,9 +228,21 @@ export function foldView(lastTs: number | null, ts: number): ViewFold {
|
|
|
226
228
|
return { newSession: false, activeMs: Math.min(gap, MAX_DWELL_SLICE_MS) };
|
|
227
229
|
}
|
|
228
230
|
|
|
229
|
-
/**
|
|
231
|
+
/**
|
|
232
|
+
* The zone every daily row is counted in: the owner's (forge task 3846).
|
|
233
|
+
*
|
|
234
|
+
* A site-wide daily count has to pick ONE zone — two viewers in two zones cannot both have
|
|
235
|
+
* their own midnight on the same row — and the person who reads these charts is the owner,
|
|
236
|
+
* in Central time. Until 2026-10-28 the rows were UTC days, which turn over at 7 pm Central,
|
|
237
|
+
* so every evening visit landed on tomorrow. Rows written before then stay UTC days: a
|
|
238
|
+
* `(user, place, day)` row holds no hours to re-bucket it by. The console labels the chart
|
|
239
|
+
* with this zone (station `views/metrics/EngagementPanel.tsx`).
|
|
240
|
+
*/
|
|
241
|
+
export const ENGAGEMENT_ZONE = "America/Chicago";
|
|
242
|
+
|
|
243
|
+
/** `2026-08-14` for a timestamp, on {@link ENGAGEMENT_ZONE}'s clock. The day bucket every row is keyed on. */
|
|
230
244
|
export function dayKey(ts: number): string {
|
|
231
|
-
return new Date(ts)
|
|
245
|
+
return formatDate(new Date(ts), "YYYY-MM-DD", ENGAGEMENT_ZONE);
|
|
232
246
|
}
|
|
233
247
|
|
|
234
248
|
/** How many whole days ago `ts` was, relative to `now`. */
|