@narumitw/pi-analytics 0.49.8 → 0.49.10
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/README.md +13 -36
- package/dist/index.ts +53 -106
- package/dist/index.ts.map +2 -2
- package/package.json +49 -50
- package/src/analytics.ts +368 -358
- package/src/collector.ts +250 -262
- package/src/errors.ts +12 -12
- package/src/menu.ts +325 -343
- package/src/skills.ts +55 -57
- package/src/storage/files.ts +352 -374
- package/src/storage/format.ts +159 -201
- package/src/storage/queries.ts +200 -204
- package/src/storage/store.ts +39 -43
- package/src/types.ts +66 -79
package/src/storage/files.ts
CHANGED
|
@@ -1,444 +1,422 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { createReadStream, type Dirent } from "node:fs";
|
|
3
|
-
import {
|
|
4
|
-
chmod,
|
|
5
|
-
lstat,
|
|
6
|
-
mkdir,
|
|
7
|
-
open,
|
|
8
|
-
readdir,
|
|
9
|
-
readFile,
|
|
10
|
-
rename,
|
|
11
|
-
rm,
|
|
12
|
-
rmdir,
|
|
13
|
-
unlink,
|
|
14
|
-
writeFile,
|
|
15
|
-
} from "node:fs/promises";
|
|
3
|
+
import { chmod, lstat, mkdir, open, readdir, readFile, rename, rm, rmdir, unlink, writeFile } from "node:fs/promises";
|
|
16
4
|
import path from "node:path";
|
|
17
5
|
import type { SettledRun } from "../types.js";
|
|
18
|
-
import {
|
|
19
|
-
AnalyticsStorageFormatError,
|
|
20
|
-
decodeStoredRun,
|
|
21
|
-
encodeStoredRun,
|
|
22
|
-
MAX_STORED_RUN_BYTES,
|
|
23
|
-
} from "./format.js";
|
|
6
|
+
import { AnalyticsStorageFormatError, decodeStoredRun, encodeStoredRun, MAX_STORED_RUN_BYTES } from "./format.js";
|
|
24
7
|
|
|
25
|
-
const GENERATION_PATTERN =
|
|
26
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
|
|
8
|
+
const GENERATION_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u;
|
|
27
9
|
const DEFAULT_WRITE_TIMEOUT_MS = 5_000;
|
|
28
10
|
const YIELD_EVERY_RECORDS = 100;
|
|
29
11
|
|
|
30
12
|
export interface ClearAnalyticsResult {
|
|
31
|
-
|
|
13
|
+
cleanupIncomplete: boolean;
|
|
32
14
|
}
|
|
33
15
|
|
|
34
16
|
export class AnalyticsGenerationChangedError extends Error {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
17
|
+
constructor() {
|
|
18
|
+
super("The active analytics generation changed during the read.");
|
|
19
|
+
this.name = "AnalyticsGenerationChangedError";
|
|
20
|
+
}
|
|
39
21
|
}
|
|
40
22
|
|
|
41
23
|
export class AnalyticsRunFiles {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
24
|
+
private readonly createId: () => string;
|
|
25
|
+
private readonly writeTimeoutMs: number;
|
|
26
|
+
private readonly beforeAppend?: (generation: string, signal: AbortSignal) => Promise<void>;
|
|
27
|
+
private readonly beforeCleanupEntry?: (signal?: AbortSignal) => Promise<void>;
|
|
28
|
+
private readonly beforeReadFile?: (signal?: AbortSignal) => Promise<void>;
|
|
29
|
+
private writerId: string;
|
|
30
|
+
private mutationTail: Promise<void> = Promise.resolve();
|
|
31
|
+
private readonly lifecycle = new AbortController();
|
|
32
|
+
private closed = false;
|
|
51
33
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
34
|
+
constructor(
|
|
35
|
+
readonly path: string,
|
|
36
|
+
options: {
|
|
37
|
+
createId?: () => string;
|
|
38
|
+
writeTimeoutMs?: number;
|
|
39
|
+
beforeAppend?: (generation: string, signal: AbortSignal) => Promise<void>;
|
|
40
|
+
beforeCleanupEntry?: (signal?: AbortSignal) => Promise<void>;
|
|
41
|
+
beforeReadFile?: (signal?: AbortSignal) => Promise<void>;
|
|
42
|
+
} = {},
|
|
43
|
+
) {
|
|
44
|
+
this.createId = options.createId ?? randomUUID;
|
|
45
|
+
this.writeTimeoutMs = options.writeTimeoutMs ?? DEFAULT_WRITE_TIMEOUT_MS;
|
|
46
|
+
this.beforeAppend = options.beforeAppend;
|
|
47
|
+
this.beforeCleanupEntry = options.beforeCleanupEntry;
|
|
48
|
+
this.beforeReadFile = options.beforeReadFile;
|
|
49
|
+
this.writerId = validCreatedId(this.createId());
|
|
50
|
+
}
|
|
69
51
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
52
|
+
append(run: SettledRun, signal?: AbortSignal): Promise<void> {
|
|
53
|
+
if (this.closed) return Promise.reject(new Error("Analytics storage is closed."));
|
|
54
|
+
const frame = encodeStoredRun(run);
|
|
55
|
+
return this.enqueueMutation(() =>
|
|
56
|
+
withDeadline(signal, this.lifecycle.signal, this.writeTimeoutMs, (operationSignal) =>
|
|
57
|
+
this.appendFrame(frame, operationSignal),
|
|
58
|
+
),
|
|
59
|
+
);
|
|
60
|
+
}
|
|
79
61
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
62
|
+
async *read(signal?: AbortSignal): AsyncIterable<SettledRun> {
|
|
63
|
+
throwIfAborted(signal);
|
|
64
|
+
const generation = await this.readOrCreateGeneration(signal);
|
|
65
|
+
const directory = this.generationPath(generation);
|
|
66
|
+
let entries: Dirent[];
|
|
67
|
+
try {
|
|
68
|
+
entries = await readdir(directory, { withFileTypes: true });
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (isNodeError(error) && error.code === "ENOENT") {
|
|
71
|
+
await this.assertGenerationUnchanged(generation, signal);
|
|
72
|
+
throw new AnalyticsGenerationChangedError();
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
let count = 0;
|
|
77
|
+
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
78
|
+
throwIfAborted(signal);
|
|
79
|
+
if (!entry.name.endsWith(".jsonl")) continue;
|
|
80
|
+
const filePath = path.join(directory, entry.name);
|
|
81
|
+
try {
|
|
82
|
+
await this.beforeReadFile?.(signal);
|
|
83
|
+
throwIfAborted(signal);
|
|
84
|
+
await assertPrivateRegularFile(filePath);
|
|
85
|
+
for await (const run of readFrames(filePath, signal)) {
|
|
86
|
+
yield run;
|
|
87
|
+
count += 1;
|
|
88
|
+
if (count % YIELD_EVERY_RECORDS === 0) await yieldToEventLoop(signal);
|
|
89
|
+
}
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (isNodeError(error) && error.code === "ENOENT") {
|
|
92
|
+
await this.assertGenerationUnchanged(generation, signal);
|
|
93
|
+
throw new AnalyticsGenerationChangedError();
|
|
94
|
+
}
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
await this.assertGenerationUnchanged(generation, signal);
|
|
99
|
+
}
|
|
118
100
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
101
|
+
clear(signal?: AbortSignal): Promise<ClearAnalyticsResult> {
|
|
102
|
+
if (this.closed) return Promise.reject(new Error("Analytics storage is closed."));
|
|
103
|
+
let result: ClearAnalyticsResult = { cleanupIncomplete: false };
|
|
104
|
+
return this.enqueueMutation(() =>
|
|
105
|
+
withLinkedSignals(signal, this.lifecycle.signal, async (operationSignal) => {
|
|
106
|
+
throwIfAborted(operationSignal);
|
|
107
|
+
await this.readOrCreateGeneration(operationSignal);
|
|
108
|
+
const next = validCreatedId(this.createId());
|
|
109
|
+
await this.publishGeneration(next);
|
|
110
|
+
this.writerId = validCreatedId(this.createId());
|
|
111
|
+
const current = await readGenerationMarker(path.join(this.path, "current"));
|
|
112
|
+
await ensurePrivateDirectory(this.generationPath(current));
|
|
113
|
+
if (!(await this.cleanupObsoleteGenerations(operationSignal))) {
|
|
114
|
+
result = { cleanupIncomplete: true };
|
|
115
|
+
}
|
|
116
|
+
}),
|
|
117
|
+
).then(() => result);
|
|
118
|
+
}
|
|
137
119
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
120
|
+
async close(): Promise<void> {
|
|
121
|
+
if (this.closed) return;
|
|
122
|
+
this.closed = true;
|
|
123
|
+
this.lifecycle.abort(new DOMException("Analytics storage closed", "AbortError"));
|
|
124
|
+
await this.mutationTail.catch(() => undefined);
|
|
125
|
+
}
|
|
144
126
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
127
|
+
private enqueueMutation(operation: () => Promise<void>): Promise<void> {
|
|
128
|
+
const result = this.mutationTail.then(operation);
|
|
129
|
+
this.mutationTail = result.catch(() => undefined);
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
150
132
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
133
|
+
private async appendFrame(frame: string, signal: AbortSignal): Promise<void> {
|
|
134
|
+
throwIfAborted(signal);
|
|
135
|
+
let obsoleteDirectory: string | undefined;
|
|
136
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
137
|
+
const generation = await this.readOrCreateGeneration(signal);
|
|
138
|
+
const directory = this.generationPath(generation);
|
|
139
|
+
await ensurePrivateDirectory(directory);
|
|
140
|
+
const filePath = path.join(directory, `${this.writerId}.jsonl`);
|
|
141
|
+
await assertOptionalPrivateRegularFile(filePath);
|
|
142
|
+
throwIfAborted(signal);
|
|
143
|
+
await this.beforeAppend?.(generation, signal);
|
|
144
|
+
throwIfAborted(signal);
|
|
145
|
+
try {
|
|
146
|
+
await writeFile(filePath, frame, {
|
|
147
|
+
encoding: "utf8",
|
|
148
|
+
flag: "a",
|
|
149
|
+
mode: 0o600,
|
|
150
|
+
signal,
|
|
151
|
+
});
|
|
152
|
+
if (process.platform !== "win32") await chmod(filePath, 0o600);
|
|
153
|
+
const current = await readGenerationMarker(path.join(this.path, "current"), signal);
|
|
154
|
+
if (current === generation) {
|
|
155
|
+
if (obsoleteDirectory) {
|
|
156
|
+
await cleanupGeneration(obsoleteDirectory, signal).catch(() => undefined);
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
obsoleteDirectory = directory;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
this.writerId = validCreatedId(this.createId());
|
|
163
|
+
throwIfAborted(signal);
|
|
164
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
throw new AnalyticsGenerationChangedError();
|
|
168
|
+
}
|
|
187
169
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
170
|
+
private async readOrCreateGeneration(signal?: AbortSignal): Promise<string> {
|
|
171
|
+
throwIfAborted(signal);
|
|
172
|
+
await ensurePrivateDirectory(this.path);
|
|
173
|
+
await ensurePrivateDirectory(path.join(this.path, "generations"));
|
|
174
|
+
const markerPath = path.join(this.path, "current");
|
|
175
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
176
|
+
let generation: string;
|
|
177
|
+
try {
|
|
178
|
+
generation = await readGenerationMarker(markerPath, signal);
|
|
179
|
+
} catch (error) {
|
|
180
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
181
|
+
generation = validCreatedId(this.createId());
|
|
182
|
+
throwIfAborted(signal);
|
|
183
|
+
try {
|
|
184
|
+
await createPrivateFile(markerPath, `${generation}\n`);
|
|
185
|
+
} catch (createError) {
|
|
186
|
+
if (!isNodeError(createError) || createError.code !== "EEXIST") throw createError;
|
|
187
|
+
generation = await readGenerationMarker(markerPath, signal);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
await ensurePrivateDirectory(this.generationPath(generation));
|
|
191
|
+
const current = await readGenerationMarker(markerPath, signal);
|
|
192
|
+
if (current === generation) return generation;
|
|
193
|
+
}
|
|
194
|
+
throw new AnalyticsGenerationChangedError();
|
|
195
|
+
}
|
|
214
196
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
197
|
+
private async publishGeneration(generation: string): Promise<void> {
|
|
198
|
+
const markerPath = path.join(this.path, "current");
|
|
199
|
+
const temporaryPath = path.join(this.path, `.current.${validCreatedId(this.createId())}.tmp`);
|
|
200
|
+
await createPrivateFile(temporaryPath, `${generation}\n`);
|
|
201
|
+
try {
|
|
202
|
+
await rename(temporaryPath, markerPath);
|
|
203
|
+
if (process.platform !== "win32") await chmod(markerPath, 0o600);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
await rm(temporaryPath, { force: true }).catch(() => undefined);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
227
209
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
210
|
+
private async cleanupObsoleteGenerations(signal?: AbortSignal): Promise<boolean> {
|
|
211
|
+
let complete = true;
|
|
212
|
+
const root = path.join(this.path, "generations");
|
|
213
|
+
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
214
|
+
const active = await readGenerationMarker(path.join(this.path, "current"));
|
|
215
|
+
if (entry.name === active) continue;
|
|
216
|
+
if (!entry.isDirectory() || !GENERATION_PATTERN.test(entry.name)) {
|
|
217
|
+
complete = false;
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
try {
|
|
221
|
+
await cleanupGeneration(path.join(root, entry.name), signal, this.beforeCleanupEntry);
|
|
222
|
+
} catch {
|
|
223
|
+
complete = false;
|
|
224
|
+
if (signal?.aborted) break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const active = await readGenerationMarker(path.join(this.path, "current"));
|
|
228
|
+
const remaining = await readdir(root, { withFileTypes: true });
|
|
229
|
+
return complete && remaining.every((entry) => entry.isDirectory() && entry.name === active);
|
|
230
|
+
}
|
|
249
231
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
232
|
+
private async assertGenerationUnchanged(generation: string, signal?: AbortSignal): Promise<void> {
|
|
233
|
+
const current = await readGenerationMarker(path.join(this.path, "current"), signal);
|
|
234
|
+
if (current !== generation) throw new AnalyticsGenerationChangedError();
|
|
235
|
+
}
|
|
254
236
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
237
|
+
private generationPath(generation: string): string {
|
|
238
|
+
return path.join(this.path, "generations", generation);
|
|
239
|
+
}
|
|
258
240
|
}
|
|
259
241
|
|
|
260
242
|
async function* readFrames(filePath: string, signal?: AbortSignal): AsyncIterable<SettledRun> {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
243
|
+
let pending = "";
|
|
244
|
+
const stream = createReadStream(filePath, { encoding: "utf8", signal });
|
|
245
|
+
for await (const chunk of stream) {
|
|
246
|
+
throwIfAborted(signal);
|
|
247
|
+
pending += String(chunk);
|
|
248
|
+
while (true) {
|
|
249
|
+
const newline = pending.indexOf("\n");
|
|
250
|
+
if (newline < 0) break;
|
|
251
|
+
const line = pending.slice(0, newline);
|
|
252
|
+
pending = pending.slice(newline + 1);
|
|
253
|
+
if (!line) continue;
|
|
254
|
+
yield decodeStoredRun(line);
|
|
255
|
+
}
|
|
256
|
+
if (Buffer.byteLength(pending) > MAX_STORED_RUN_BYTES) {
|
|
257
|
+
throw new AnalyticsStorageFormatError("Analytics record is too large to read safely.");
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
// A writer always terminates complete frames with a newline. An unterminated tail is a
|
|
261
|
+
// crash residue and is ignored; this writer file will not be reused after its process exits.
|
|
280
262
|
}
|
|
281
263
|
|
|
282
264
|
async function readGenerationMarker(markerPath: string, signal?: AbortSignal): Promise<string> {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
265
|
+
await assertPrivateRegularFile(markerPath);
|
|
266
|
+
throwIfAborted(signal);
|
|
267
|
+
const value = (await readFile(markerPath, { encoding: "utf8", signal })).trim();
|
|
268
|
+
if (!GENERATION_PATTERN.test(value)) {
|
|
269
|
+
throw new AnalyticsStorageFormatError("Analytics generation marker is invalid.");
|
|
270
|
+
}
|
|
271
|
+
return value;
|
|
290
272
|
}
|
|
291
273
|
|
|
292
274
|
async function ensurePrivateDirectory(directoryPath: string): Promise<void> {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
275
|
+
try {
|
|
276
|
+
const metadata = await lstat(directoryPath);
|
|
277
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
278
|
+
throw new Error("Analytics storage paths must be regular directories, not links.");
|
|
279
|
+
}
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (!isNodeError(error) || error.code !== "ENOENT") throw error;
|
|
282
|
+
await mkdir(directoryPath, { recursive: true, mode: 0o700 });
|
|
283
|
+
const metadata = await lstat(directoryPath);
|
|
284
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink()) {
|
|
285
|
+
throw new Error("Analytics storage paths must be regular directories, not links.");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (process.platform !== "win32") await chmod(directoryPath, 0o700);
|
|
307
289
|
}
|
|
308
290
|
|
|
309
291
|
async function assertOptionalPrivateRegularFile(filePath: string): Promise<void> {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
292
|
+
try {
|
|
293
|
+
await assertPrivateRegularFile(filePath);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
if (isNodeError(error) && error.code === "ENOENT") return;
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
316
298
|
}
|
|
317
299
|
|
|
318
300
|
async function assertPrivateRegularFile(filePath: string): Promise<void> {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
301
|
+
const metadata = await lstat(filePath);
|
|
302
|
+
if (!metadata.isFile() || metadata.isSymbolicLink()) {
|
|
303
|
+
throw new Error("Analytics storage files must be regular files, not links.");
|
|
304
|
+
}
|
|
305
|
+
if (process.platform !== "win32") await chmod(filePath, 0o600);
|
|
324
306
|
}
|
|
325
307
|
|
|
326
308
|
async function createPrivateFile(filePath: string, content: string): Promise<void> {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
309
|
+
const handle = await open(filePath, "wx", 0o600);
|
|
310
|
+
try {
|
|
311
|
+
await handle.writeFile(content, "utf8");
|
|
312
|
+
await handle.sync();
|
|
313
|
+
} finally {
|
|
314
|
+
await handle.close();
|
|
315
|
+
}
|
|
334
316
|
}
|
|
335
317
|
|
|
336
318
|
async function cleanupGeneration(
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
319
|
+
directoryPath: string,
|
|
320
|
+
signal?: AbortSignal,
|
|
321
|
+
beforeEntry?: (signal?: AbortSignal) => Promise<void>,
|
|
340
322
|
): Promise<void> {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
323
|
+
throwIfAborted(signal);
|
|
324
|
+
let entries: Dirent[];
|
|
325
|
+
try {
|
|
326
|
+
entries = await readdir(directoryPath, { withFileTypes: true });
|
|
327
|
+
} catch (error) {
|
|
328
|
+
if (isNodeError(error) && error.code === "ENOENT") return;
|
|
329
|
+
throw error;
|
|
330
|
+
}
|
|
331
|
+
for (const entry of entries) {
|
|
332
|
+
throwIfAborted(signal);
|
|
333
|
+
await beforeEntry?.(signal);
|
|
334
|
+
throwIfAborted(signal);
|
|
335
|
+
if (!entry.isFile() || !entry.name.endsWith(".jsonl")) {
|
|
336
|
+
throw new Error("Analytics generation contains an unexpected storage entry.");
|
|
337
|
+
}
|
|
338
|
+
await unlink(path.join(directoryPath, entry.name));
|
|
339
|
+
}
|
|
340
|
+
throwIfAborted(signal);
|
|
341
|
+
await rmdir(directoryPath);
|
|
360
342
|
}
|
|
361
343
|
|
|
362
344
|
async function withLinkedSignals<T>(
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
345
|
+
callerSignal: AbortSignal | undefined,
|
|
346
|
+
lifecycleSignal: AbortSignal,
|
|
347
|
+
operation: (signal: AbortSignal) => Promise<T>,
|
|
366
348
|
): Promise<T> {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
lifecycleSignal.removeEventListener("abort", lifecycleAbort);
|
|
383
|
-
}
|
|
349
|
+
throwIfAborted(callerSignal);
|
|
350
|
+
throwIfAborted(lifecycleSignal);
|
|
351
|
+
const controller = new AbortController();
|
|
352
|
+
const abort = (signal: AbortSignal) =>
|
|
353
|
+
controller.abort(signal.reason ?? new DOMException("Analytics operation aborted", "AbortError"));
|
|
354
|
+
const callerAbort = () => callerSignal && abort(callerSignal);
|
|
355
|
+
const lifecycleAbort = () => abort(lifecycleSignal);
|
|
356
|
+
callerSignal?.addEventListener("abort", callerAbort, { once: true });
|
|
357
|
+
lifecycleSignal.addEventListener("abort", lifecycleAbort, { once: true });
|
|
358
|
+
try {
|
|
359
|
+
return await operation(controller.signal);
|
|
360
|
+
} finally {
|
|
361
|
+
callerSignal?.removeEventListener("abort", callerAbort);
|
|
362
|
+
lifecycleSignal.removeEventListener("abort", lifecycleAbort);
|
|
363
|
+
}
|
|
384
364
|
}
|
|
385
365
|
|
|
386
366
|
async function withDeadline<T>(
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
367
|
+
callerSignal: AbortSignal | undefined,
|
|
368
|
+
lifecycleSignal: AbortSignal,
|
|
369
|
+
timeoutMs: number,
|
|
370
|
+
operation: (signal: AbortSignal) => Promise<T>,
|
|
391
371
|
): Promise<T> {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
lifecycleSignal.removeEventListener("abort", lifecycleAbort);
|
|
423
|
-
}
|
|
372
|
+
throwIfAborted(callerSignal);
|
|
373
|
+
throwIfAborted(lifecycleSignal);
|
|
374
|
+
const controller = new AbortController();
|
|
375
|
+
const abort = (signal: AbortSignal) =>
|
|
376
|
+
controller.abort(signal.reason ?? new DOMException("Analytics operation aborted", "AbortError"));
|
|
377
|
+
const callerAbort = () => callerSignal && abort(callerSignal);
|
|
378
|
+
const lifecycleAbort = () => abort(lifecycleSignal);
|
|
379
|
+
callerSignal?.addEventListener("abort", callerAbort, { once: true });
|
|
380
|
+
lifecycleSignal.addEventListener("abort", lifecycleAbort, { once: true });
|
|
381
|
+
const timer = setTimeout(
|
|
382
|
+
() => controller.abort(new DOMException("Analytics write timed out", "TimeoutError")),
|
|
383
|
+
Math.max(1, timeoutMs),
|
|
384
|
+
);
|
|
385
|
+
try {
|
|
386
|
+
return await operation(controller.signal);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
if (
|
|
389
|
+
controller.signal.aborted &&
|
|
390
|
+
error instanceof Error &&
|
|
391
|
+
error.name === "AbortError" &&
|
|
392
|
+
error.cause === controller.signal.reason
|
|
393
|
+
) {
|
|
394
|
+
throw controller.signal.reason;
|
|
395
|
+
}
|
|
396
|
+
throw error;
|
|
397
|
+
} finally {
|
|
398
|
+
clearTimeout(timer);
|
|
399
|
+
callerSignal?.removeEventListener("abort", callerAbort);
|
|
400
|
+
lifecycleSignal.removeEventListener("abort", lifecycleAbort);
|
|
401
|
+
}
|
|
424
402
|
}
|
|
425
403
|
|
|
426
404
|
async function yieldToEventLoop(signal?: AbortSignal): Promise<void> {
|
|
427
|
-
|
|
428
|
-
|
|
405
|
+
await new Promise<void>((resolve) => setImmediate(resolve));
|
|
406
|
+
throwIfAborted(signal);
|
|
429
407
|
}
|
|
430
408
|
|
|
431
409
|
function validCreatedId(value: string): string {
|
|
432
|
-
|
|
433
|
-
|
|
410
|
+
if (!GENERATION_PATTERN.test(value)) throw new Error("Analytics storage received an invalid ID.");
|
|
411
|
+
return value;
|
|
434
412
|
}
|
|
435
413
|
|
|
436
414
|
function throwIfAborted(signal?: AbortSignal): void {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
415
|
+
if (signal?.aborted) {
|
|
416
|
+
throw signal.reason ?? new DOMException("Analytics operation aborted", "AbortError");
|
|
417
|
+
}
|
|
440
418
|
}
|
|
441
419
|
|
|
442
420
|
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
|
|
443
|
-
|
|
421
|
+
return error instanceof Error && "code" in error;
|
|
444
422
|
}
|