cursedbelt-server 4.51.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/dropzone/scan.d.ts +15 -0
- package/dist/server/dropzone/scan.js +4 -0
- 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/docs/skipped-tests.md +1 -0
- 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/dropzone/links.spec.ts +51 -2
- package/src/server/dropzone/scan.ts +18 -0
- 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.
|
|
@@ -46,6 +46,14 @@ export interface DroppedFile {
|
|
|
46
46
|
ext: string;
|
|
47
47
|
size: number;
|
|
48
48
|
mtimeMs: number;
|
|
49
|
+
/**
|
|
50
|
+
* The inode and the change time, from the same `stat` as `size` — what tells a file that was
|
|
51
|
+
* REPLACED from one that kept its name and size (collections' linked ledger keys on it, forge
|
|
52
|
+
* task 3585). Optional only so a handler's test can build a `DroppedFile` without them; the walk
|
|
53
|
+
* always sets both.
|
|
54
|
+
*/
|
|
55
|
+
ino?: number;
|
|
56
|
+
ctimeMs?: number;
|
|
49
57
|
/** Every other FILE name in the same directory — see {@link ScanOptions.captureSiblings}. */
|
|
50
58
|
siblings: string[];
|
|
51
59
|
/**
|
|
@@ -126,6 +134,13 @@ export interface ScanOptions {
|
|
|
126
134
|
* — an app's quarantine or `done/`, a name that is the owner's own inside a linked folder.
|
|
127
135
|
*/
|
|
128
136
|
skipTopDirs?: readonly string[];
|
|
137
|
+
/**
|
|
138
|
+
* Skip every directory whose name starts with a dot, at any depth — another tool's state, never
|
|
139
|
+
* content. Matters most with {@link followLinks}: a link to a whole drive would otherwise walk its
|
|
140
|
+
* `.Spotlight-V100`, `.fseventsd` and `.Trashes`. Off by default, so no consumer changes
|
|
141
|
+
* behavior unasked.
|
|
142
|
+
*/
|
|
143
|
+
skipHiddenDirs?: boolean;
|
|
129
144
|
/**
|
|
130
145
|
* Walk THROUGH symlinks: a linked folder is ingested in place (`links.ts`). Off by default, so
|
|
131
146
|
* no consumer changes behavior unasked. See {@link walkDropzone}.
|
|
@@ -97,6 +97,8 @@ export async function walkDropzone(root, opts = {}) {
|
|
|
97
97
|
ext: extOf(name),
|
|
98
98
|
size: info.size,
|
|
99
99
|
mtimeMs: info.mtimeMs,
|
|
100
|
+
ino: info.ino,
|
|
101
|
+
ctimeMs: info.ctimeMs,
|
|
100
102
|
siblings,
|
|
101
103
|
...(linked ? { linked: { realPath: await realpath(path) } } : {}),
|
|
102
104
|
});
|
|
@@ -152,6 +154,8 @@ export async function walkDropzone(root, opts = {}) {
|
|
|
152
154
|
if (isDir) {
|
|
153
155
|
if (skip.has(entry.name))
|
|
154
156
|
continue;
|
|
157
|
+
if (opts.skipHiddenDirs && entry.name.startsWith("."))
|
|
158
|
+
continue;
|
|
155
159
|
if (dir === root && namedIn(opts.skipTopDirs, entry.name))
|
|
156
160
|
continue;
|
|
157
161
|
if (!(await firstVisit(path)))
|
|
@@ -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/docs/skipped-tests.md
CHANGED
|
@@ -16,4 +16,5 @@ carrying one goes red until it is removed. Inherited from `cursedbelt` at the
|
|
|
16
16
|
| `src/server/storage/imageThumbnail.spec.ts` | `ffmpeg` on `PATH` |
|
|
17
17
|
| `src/server/storage/videoProcessor.posterOnly.spec.ts` | `ffmpeg` on `PATH` |
|
|
18
18
|
| `src/server/storage/files/adapter.photo.display.spec.ts` | `ffmpeg` on `PATH` |
|
|
19
|
+
| `src/server/dropzone/links.spec.ts` | macOS — a real Finder alias is written and resolved through `osascript` and Foundation; runs on every `bun run test` on this Mac (green 2026-10-26) |
|
|
19
20
|
| `src/server/storage/fileUploadClient-chunked.integration.spec.ts` | a real `binary-server` checkout — set `BINARY_SERVER_DIR` to its path (`apps/binary-server` in this generation). No default by design: a published package must not go looking in one machine's directory layout. Moved here from `cursedbelt` at the 2026-09-15 split, with the storage internals it drives. |
|
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
|
/**
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
-
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readlinkSync, realpathSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import { join } from "node:path";
|
|
5
|
-
import { adoptFinderAliases, DROPZONE_LINK, ensureDropzoneLink, isFinderAlias, linkSource } from "./links.js";
|
|
5
|
+
import { adoptFinderAliases, DROPZONE_LINK, ensureDropzoneLink, isFinderAlias, isMountPoint, linkedRoomBytes, linkSource, resolveFinderAlias } from "./links.js";
|
|
6
6
|
import { scanDropzone, sweepDropzone, walkDropzone, type DropOutcome } from "./scan.js";
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -92,6 +92,24 @@ describe("walkDropzone with followLinks", () => {
|
|
|
92
92
|
const { files } = await walkDropzone(root, { followLinks: true, skipTopDirs: ["dups"] });
|
|
93
93
|
expect(files.map((f) => f.relativePath)).toEqual(["Drive/dups/y.mp3"]);
|
|
94
94
|
});
|
|
95
|
+
|
|
96
|
+
test("skipHiddenDirs leaves another tool's state alone at every depth — a linked drive's `.Spotlight-V100` too (forge task 3754)", async () => {
|
|
97
|
+
write(join(root, ".cache", "x.mp3"));
|
|
98
|
+
write(join(drive, ".Spotlight-V100", "store.db"));
|
|
99
|
+
write(join(drive, "Album", "a.mp3"));
|
|
100
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
101
|
+
const asked = await walkDropzone(root, { followLinks: true, skipHiddenDirs: true });
|
|
102
|
+
expect(asked.files.map((f) => f.relativePath)).toEqual(["Drive/Album/a.mp3"]);
|
|
103
|
+
// Off by default: no consumer changes behavior unasked.
|
|
104
|
+
expect((await walkDropzone(root, { followLinks: true })).files).toHaveLength(3);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test("each file carries its inode and change time, from the one stat — a replaced file is told from a kept one", async () => {
|
|
108
|
+
const at = write(join(drive, "a.mp3"));
|
|
109
|
+
symlinkSync(drive, join(root, "Drive"));
|
|
110
|
+
const [file] = (await walkDropzone(root, { followLinks: true })).files;
|
|
111
|
+
expect(file).toMatchObject({ ino: statSync(at).ino, ctimeMs: statSync(at).ctimeMs });
|
|
112
|
+
});
|
|
95
113
|
});
|
|
96
114
|
|
|
97
115
|
describe("sweepDropzone never deletes a linked file", () => {
|
|
@@ -143,6 +161,24 @@ describe("Finder aliases", () => {
|
|
|
143
161
|
expect(lstatSync(join(root, "done", "old alias")).isFile()).toBe(true);
|
|
144
162
|
});
|
|
145
163
|
|
|
164
|
+
// Waiting for macOS: skipped elsewhere, because only osascript and Foundation write and resolve a real alias.
|
|
165
|
+
test.skipIf(process.platform !== "darwin")("🔴 a REAL alias becomes a symlink to its target — headless, through Foundation (from collections, forge task 3754)", async () => {
|
|
166
|
+
const at = join(root, "Videos", "tod alias");
|
|
167
|
+
mkdirSync(join(root, "Videos"));
|
|
168
|
+
// Written through Foundation the way Finder writes one.
|
|
169
|
+
const script =
|
|
170
|
+
'function run(argv){ObjC.import("Foundation");const t=$.NSURL.fileURLWithPath(argv[0]);' +
|
|
171
|
+
"const d=t.bookmarkDataWithOptionsIncludingResourceValuesForKeysRelativeToURLError(1<<10,$(),$(),null);" +
|
|
172
|
+
"return $.NSURL.writeBookmarkDataToURLOptionsError(d,$.NSURL.fileURLWithPath(argv[1]),1<<10,null);}";
|
|
173
|
+
const child = Bun.spawn(["osascript", "-l", "JavaScript", "-e", script, drive, at], { stdout: "ignore", stderr: "pipe" });
|
|
174
|
+
if ((await child.exited) !== 0) throw new Error(await new Response(child.stderr).text());
|
|
175
|
+
expect(resolveFinderAlias(at)).toBe(realpathSync(drive));
|
|
176
|
+
|
|
177
|
+
expect(await adoptFinderAliases(root)).toEqual({ linked: [`Videos/tod alias → ${realpathSync(drive)}`], unresolved: [] });
|
|
178
|
+
expect(lstatSync(at).isSymbolicLink()).toBe(true);
|
|
179
|
+
expect(readlinkSync(at)).toBe(realpathSync(drive));
|
|
180
|
+
});
|
|
181
|
+
|
|
146
182
|
test("a dry run says what it would do and changes nothing", async () => {
|
|
147
183
|
alias(join(root, "tod alias"));
|
|
148
184
|
const result = await adoptFinderAliases(root, { dryRun: true, resolve: () => drive });
|
|
@@ -157,6 +193,16 @@ describe("linkSource and the link beside the code", () => {
|
|
|
157
193
|
expect(linkSource("/Volumes/X/a", () => true).state).toBe("deleted");
|
|
158
194
|
expect(linkSource("/Volumes/X/a", () => false)).toEqual({ state: "unmounted", target: "/Volumes/X/a", volume: "/Volumes/X" });
|
|
159
195
|
expect(linkSource(join(base, "gone")).state).toBe("deleted");
|
|
196
|
+
// A plain folder — what macOS can leave in /Volumes after an unclean eject — is not a mount point.
|
|
197
|
+
expect(isMountPoint(drive)).toBe(false);
|
|
198
|
+
expect(isMountPoint("/")).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("linkedRoomBytes is what statfs says minus the floor and what is owed; a store that is not there holds nothing back", () => {
|
|
202
|
+
const room = linkedRoomBytes(base);
|
|
203
|
+
expect(typeof room).toBe("number");
|
|
204
|
+
expect(linkedRoomBytes(base, 1024)).toBe((room as number) - 1024);
|
|
205
|
+
expect(linkedRoomBytes(join(base, "no", "such", "store"))).toBeNull();
|
|
160
206
|
});
|
|
161
207
|
|
|
162
208
|
test("ensureDropzoneLink makes the folder and the link, is quiet when right, and never replaces a real file", async () => {
|
|
@@ -167,6 +213,9 @@ describe("linkSource and the link beside the code", () => {
|
|
|
167
213
|
expect(readlinkSync(join(app, DROPZONE_LINK))).toBe(target);
|
|
168
214
|
expect(existsSync(target)).toBe(true);
|
|
169
215
|
expect(await ensureDropzoneLink(app, target)).toBeNull();
|
|
216
|
+
const moved = join(base, "state", "moved");
|
|
217
|
+
expect(await ensureDropzoneLink(app, moved)).toContain("linked");
|
|
218
|
+
expect(readlinkSync(join(app, DROPZONE_LINK))).toBe(moved);
|
|
170
219
|
|
|
171
220
|
const other = join(base, "other");
|
|
172
221
|
mkdirSync(other);
|
|
@@ -51,6 +51,14 @@ export interface DroppedFile {
|
|
|
51
51
|
ext: string;
|
|
52
52
|
size: number;
|
|
53
53
|
mtimeMs: number;
|
|
54
|
+
/**
|
|
55
|
+
* The inode and the change time, from the same `stat` as `size` — what tells a file that was
|
|
56
|
+
* REPLACED from one that kept its name and size (collections' linked ledger keys on it, forge
|
|
57
|
+
* task 3585). Optional only so a handler's test can build a `DroppedFile` without them; the walk
|
|
58
|
+
* always sets both.
|
|
59
|
+
*/
|
|
60
|
+
ino?: number;
|
|
61
|
+
ctimeMs?: number;
|
|
54
62
|
/** Every other FILE name in the same directory — see {@link ScanOptions.captureSiblings}. */
|
|
55
63
|
siblings: string[];
|
|
56
64
|
/**
|
|
@@ -114,6 +122,13 @@ export interface ScanOptions {
|
|
|
114
122
|
* — an app's quarantine or `done/`, a name that is the owner's own inside a linked folder.
|
|
115
123
|
*/
|
|
116
124
|
skipTopDirs?: readonly string[];
|
|
125
|
+
/**
|
|
126
|
+
* Skip every directory whose name starts with a dot, at any depth — another tool's state, never
|
|
127
|
+
* content. Matters most with {@link followLinks}: a link to a whole drive would otherwise walk its
|
|
128
|
+
* `.Spotlight-V100`, `.fseventsd` and `.Trashes`. Off by default, so no consumer changes
|
|
129
|
+
* behavior unasked.
|
|
130
|
+
*/
|
|
131
|
+
skipHiddenDirs?: boolean;
|
|
117
132
|
/**
|
|
118
133
|
* Walk THROUGH symlinks: a linked folder is ingested in place (`links.ts`). Off by default, so
|
|
119
134
|
* no consumer changes behavior unasked. See {@link walkDropzone}.
|
|
@@ -208,6 +223,8 @@ export async function walkDropzone(root: string, opts: ScanOptions = {}): Promis
|
|
|
208
223
|
ext: extOf(name),
|
|
209
224
|
size: info.size,
|
|
210
225
|
mtimeMs: info.mtimeMs,
|
|
226
|
+
ino: info.ino,
|
|
227
|
+
ctimeMs: info.ctimeMs,
|
|
211
228
|
siblings,
|
|
212
229
|
...(linked ? { linked: { realPath: await realpath(path) } } : {}),
|
|
213
230
|
});
|
|
@@ -258,6 +275,7 @@ export async function walkDropzone(root: string, opts: ScanOptions = {}): Promis
|
|
|
258
275
|
}
|
|
259
276
|
if (isDir) {
|
|
260
277
|
if (skip.has(entry.name)) continue;
|
|
278
|
+
if (opts.skipHiddenDirs && entry.name.startsWith(".")) continue;
|
|
261
279
|
if (dir === root && namedIn(opts.skipTopDirs, entry.name)) continue;
|
|
262
280
|
if (!(await firstVisit(path))) continue;
|
|
263
281
|
await walk(path, viaLink);
|
|
@@ -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`. */
|